如何在gdb中打印std :: string_view?

时间:2019-01-03 04:31:45

标签: 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=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命令中打印此变量?

1 个答案:

答案 0 :(得分:0)

这是使用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"
...

...所以我想这个问题仍未解决...