Piy*_*sal 9 unix debugging gdb
有没有办法在gdb中输出print命令的输出?在我的情况下,我正在使用gdb调试核心转储,我正在调试的对象包含很多元素.我发现很难找到匹配的属性,即:
(gdb) print *this | grep <attribute>
Run Code Online (Sandbox Code Playgroud)
谢谢.
(gdb)print*this | grep的
"标准"的方式来实现这一目标是使用Meta-X gdb在emacs.
替代:
(gdb) set logging on
(gdb) print *this
(gdb) set logging off
(gdb) shell grep attribute gdb.txt
Run Code Online (Sandbox Code Playgroud)
与上述相比,cnicutar提到的补丁确实看起来很有吸引力.我猜测它(或它的等价物)从未被提交的原因是大多数GDB维护者使用的emacs,所以首先没有这个问题.
最简单的方法是利用 gdb python。单线:
\n\ngdb \xce\xbb py ["attribute" in line and print(line) for line in gdb.execute("p *this", to_string=True).splitlines()]Run Code Online (Sandbox Code Playgroud)\n\n假设您已启用命令历史记录,您只需键入一次,然后按Ctrl+R b.exec将其从历史记录中删除。接下来只需更改attribute并*this根据您的要求即可。
您也可以像这样简单地实现:
\n\ngdb \xce\xbb grep_cmd "p *this" attributeRun Code Online (Sandbox Code Playgroud)\n\n为此,只需将以下内容添加到您的.gdbinit文件中:
gdb \xce\xbb py ["attribute" in line and print(line) for line in gdb.execute("p *this", to_string=True).splitlines()]Run Code Online (Sandbox Code Playgroud)\n
您可以使用pipe命令
>>> pipe maintenance info sections | grep .text
[15] 0x5555555551c0->0x5555555554d5 at 0x000011c0: .text ...
>>> pipe maintenance info sections | grep .text | wc
1 10 100
Run Code Online (Sandbox Code Playgroud)