Debugging 如何在gdb中打印std::string_视图?

Debugging 如何在gdb中打印std::string_视图?,debugging,gdb,printf,Debugging,Gdb,Printf,只是试着调试一些东西,然后: (gdb) Thread 1 "SciTE" hit Breakpoint 2, PropSetFile::Set (this=this@entry=0x7fffffffbea0, key="LS_COLORS", val="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=3

只是试着调试一些东西,然后:

(gdb) 
Thread 1 "SciTE" hit Breakpoint 2, PropSetFile::Set (this=this@entry=0x7fffffffbea0, 
    key="LS_COLORS", 
    val="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.a"...) at ./../src/PropSetFile.cxx:91
91  void PropSetFile::Set(std::string_view key, std::string_view val) {

(gdb) p key
$2 = "LS_COLORS"
(gdb) ptype key
type = std::string_view
好的,如果我只说
p key
,那么我就可以打印它的内容

但是我想做一个
dprintf
,意思是
printf

(gdb) printf "'%s'\n", key
'Value can't be converted to integer.
(gdb) printf "'%s'\n", key.c_str()
Can't take address of "key" which isn't an lvalue.
(gdb) printf "'%s'\n", *(char **)key
Invalid cast.
(gdb) printf "'%s'\n", (char *)key
Invalid cast.
(gdb) printf "'%s'\n", std::string(key).c_str()
Cannot look up value of a typedef `std::__cxx11::string'.

那么,如何在
gdb
printf
/
dprintf
命令中打印这个变量呢?

这里有一个使用gdb-python的解决方法:

(gdb) break PropSetFile::Set
Breakpoint 4 at 0x555555667700: file ./../src/PropSetFile.cxx, line 91.
(gdb) commands
Type commands for breakpoint(s) 4, one per line.
End with a line saying just "end".
>python print( "key {} val {}".format(gdb.parse_and_eval("key"), gdb.parse_and_eval("val")) )
>c
>end
(gdb)
。。。但是输出太冗长了,这就是为什么我首先想要
dprintf

(gdb) c
Continuing.
Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0, 
    key="XDG_MENU_PREFIX", val="gnome-") at ./../src/PropSetFile.cxx:91
91  void PropSetFile::Set(std::string_view key, std::string_view val) {
key "XDG_MENU_PREFIX" val "gnome-"

Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0, 
    key="KIGITHUB", val="https://github.com/KiCad") at ./../src/PropSetFile.cxx:91
91  void PropSetFile::Set(std::string_view key, std::string_view val) {
key "KIGITHUB" val "https://github.com/KiCad"
...
。。。所以我想这个问题还没有解决