如何访问Python GDB Value的键或值
我在 GDB 中有一个结构体,想要运行一个检查该结构体的脚本。在 Python GDB 中,您可以通过以下方式轻松访问该结构。
(gdb) python mystruct = gdb.parse_and_eval("mystruct")
现在我得到了这个名为 mystruct 的变量,它是一个 GDB.Value 对象。我只需将此对象用作字典即可访问该结构的所有成员(例如mystruct['member']
)。
问题是,我的脚本不知道某个结构有哪些成员。所以我想从这个 GDB.Value 对象中获取键(甚至是值)。但 mystruct.values() 和 mystruct.keys() 在这里都不起作用。
是否无法访问此信息?我认为您不太可能无法访问此信息,但我没有在任何地方找到它。 dir(mystruct)
告诉我也没有键或值函数。我可以通过打印 mystruct 来查看所有成员,但是没有办法在 python 中获取成员吗?
I have a struct in GDB and want to run a script which examines this struct. In Python GDB you can easily access the struct via
(gdb) python mystruct = gdb.parse_and_eval("mystruct")
Now I got this variable called mystruct which is a GDB.Value object. And I can access all the members of the struct by simply using this object as a dictionary (likemystruct['member']
).
The problem is, that my script doesn't know which members a certain struct has. So I wanted to get the keys (or even the values) from this GDB.Value object. But neither mystruct.values()
nor mystruct.keys()
is working here.
Is there no possibility to access this information? I think it's highly unlikely that you can't access this information, but I didn't found it anywhere. A dir(mystruct)
showed me that there also is no keys or values function. I can see all the members by printing the mystruct, but isn't there a way to get the members in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 GDB 文档:
您可以获取
mystruct
的类型如下所示:并迭代 字段 通过 tp.fields( )
不需要邪恶的解决方法;-)
更新:
GDB 7.4 刚刚发布。来自公告:
From GDB documentation:
You can get the type of
mystruct
like so:and iterate over the fields via
tp.fields()
No evil workarounds required ;-)
Update:
GDB 7.4 has just been released. From the announcement:
邪恶的解决方法:
我不知道这是否具有普遍性。作为演示,我编写了一个最小的示例
test.cpp
现在我像往常一样运行
g++ -g test.cpp -o test
并启动gdb test.以下是会话记录示例:
Evil workaround:
I don't know if this is generalisable. As a demo, I wrote a minimal example
test.cpp
Now I run
g++ -g test.cpp -o test
as usual and fire upgdb test
. Here is a example session transcript:这些天:
...它变得容易多了,属性是 dict 中的键 - 这是一个示例:
test_struct.c
:然后编译并输入 gdb:
...然后在 gdb 中:
These days:
... it got a lot easier, properties are keys in dict - here is an example:
test_struct.c
:Then compile and enter gdb:
... then in gdb: