在调试C程序时如何将gdb值转换为python数字对象 - python

我在调试C程序时使用python2.6的gdb模块,并希望根据实例的'.Type'将gdb.Value实例转换为python数字对象(变量)。

例如。将我的C程序的SomeStruct->some_float_val = 1./6;通过sfv=gdb.parse_and_eval('SomeStruct->some_double_val')转换为Python gdb.Value,然后将其转换为双精度浮点python变量-知道str(sfv.type.strip_typedefs())=='double'并且其大小为8B-无需仅通过字符串转换使用dbl=float(str(sfv))Value.string(),而是类似使用struct将字节解包以获取正确的double值。

我的搜索返回的每个链接都指向https://sourceware.org/gdb/onlinedocs/gdb/Values-From-Inferior.html#Values-From-Inferior,但是我看不到如何将Value实例干净地转换为python变量,比如说Value甚至不在C内存中,而是表示一个gdb.Value.address(因此可以'不使用Inferior.read_memory()),如何将其转换为Python int而不转换字符串值?

python大神给出的解决方案

您可以使用Valueint直接从float进行转换:

(gdb) python print int(gdb.Value(0))
0
(gdb) python print float(gdb.Value(0.0))
0.0

但是,由于float(gdb.Value(0))不起作用,系统中似乎至少有一个小故障。