Sir*_*uke 10 c++ debugging gcc gdb
我正在使用具有非常特殊编码风格的C++代码库,包括使用'$'在类中添加成员变量.对于之前从未遇到过这种情况的人来说,它不是C++标准的正式部分,而是潜伏在后向兼容性方面.
作为我正在谈论的一个例子:
#include <iostream>
class T { public: int $x; int y; };
int main()
{
T *t = new T();
t->$x = t->y = 42;
std::cout << "t->$x = " << t->$x << std::endl;
delete t;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这在GDB中引入了一个问题.GDB通常使用$前缀变量作为魔术便利变量(例如引用以前的值).启动GDB,在cout语句中设置断点,然后尝试打印t->$x.
p t运行正常. p *t运行正常. p t->y运行正常. p t->$x返回语法错误,大概是期望$引用一个方便变量.
理想情况下,我会完全剥离$ s并花费剩余的时间来追捕那些认为这是个好主意的人(特别是对于现代代码库).这是不现实的,但我仍然需要能够使用GDB进行调试.
我希望有一个神奇的逃脱角色,但我搜索或试过的任何东西都没有用.
例子:
p this->'\044descriptor' p this->'$descriptor'p this->'$'descriptorp this->\$descriptorp this->\\$descriptor p this->'\$descriptor' p this->'\\044descriptor'p this->$$descriptorp this->'$$descriptor'等等.
在这种特殊情况下,我可以运行getter函数(p this->getDescriptor()).一个更丑陋的解决方法是打印整个类内容(p *this).我不确定我可以无限期地依赖这两者; 一些类相当大,大多数成员变量没有getter.
这可能被归类为GDB中的一个错误,具体取决于为了支持这一点而撕掉输入是否是个好主意.但是,即使它已经修复,我仍然坚持使用GDB 7.2来实现给定的体系结构/构建环境.
有任何想法吗?
更新:python import gdb; print (gdb.parse_and_eval("t")['$x'])正如评论中所建议的那样,如果你有python内置(不幸的是我没有).
小智 1
如果您获得带有 python 扩展的 gdb 版本,也许“探索”功能会有所帮助。
请参阅https://sourceware.org/gdb/onlinedocs/gdb/Data.html#Data
(gdb) explore cs
The value of `cs' is a struct/class of type `struct ComplexStruct' with
the following fields:
ss_p =
arr =
Enter the field number of choice:
Run Code Online (Sandbox Code Playgroud)
由于您不需要变量名称,因此您应该能够绕过“$”问题。
| 归档时间: |
|
| 查看次数: |
163 次 |
| 最近记录: |