我在 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 中获取成员吗?
最佳答案
来自 GDB documentation :
你可以像这样获取mystruct
的类型:
tp = mystruct.type
并遍历 fields通过 tp.fields()
不需要邪恶的解决方法;-)
更新: GDB 7.4 刚刚发布。来自announcement :
Type objects for struct and union types now allow access to the fields using standard Python dictionary (mapping) methods.
关于python - 如何访问 Python GDB Value 的键或值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8912338/