问题
I'm using python2.6's gdb module while debugging a C program, and would like to convert a gdb.Value instance into a python numeral object (variable) based off the instance's '.Type'.
E.g. turn my C program's SomeStruct->some_float_val = 1./6;
to a Python gdb.Value via sfv=gdb.parse_and_eval('SomeStruct->some_double_val')
, but THEN turn this into a double precision floating point python variable -- knowing that str(sfv.type.strip_typedefs())=='double'
and its size is 8B -- WITHOUT just converting through a string using dbl=float(str(sfv))
or Value.string()
but rather something like unpacking the bytes using struct
to get the correct double value.
Every link returned from my searches points https://sourceware.org/gdb/onlinedocs/gdb/Values-From-Inferior.html#Values-From-Inferior, but I can't see how to convert a Value instance into a python variable cleanly, say the Value wasn't even in C memory but represented a gdb.Value.address (so can't use Inferior.read_memory()
), how would one turn this into a Python int without casting string values?
回答1:
You can convert it directly from the Value
using int
or float
:
(gdb) python print int(gdb.Value(0))
0
(gdb) python print float(gdb.Value(0.0))
0.0
There seems to be at least one glitch in the system, though, as float(gdb.Value(0))
does not work.
来源:https://stackoverflow.com/questions/29932049/how-to-convert-a-gdb-value-to-a-python-numeral-object-while-debugging-c-program