python - 如何访问 Python GDB Value 的键或值

标签 python linux gdb

我在 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/

相关文章:

python - 使用 Python 在通用网页中查找 "date"

python - 将 python 代码向量化为 numpy

c - 如何在 GDB 中的文件上设置观察点以在文件被修改时中断?

c++ - gdb 核心无法访问内存

java - 有什么方法可以中断、终止或以其他方式解除(释放同步锁)单个死锁的 Java 线程,从而允许其他线程继续执行?

python - 如何高效地将数千张高清照片加载到 pandas df 中并转换为 HDF?

python : Print list of list as tables

linux - 文件开头的 #!/usr/bin/... 是什么意思?

linux - 前台程序和后台程序有什么区别?

linux - 无法在 CentOS 6 上使用绑定(bind)绕过网关