14
The new GDB-Python scripting API looks quite powerful and should be very useful. However writing a useful script to iterate through the fields in a struct of C or C++ is not trivial. Does any one know some solid samples that does exactly that?
Thanks in advance.
Update the final sample: Replace the _print_fields() in early sample.
if l.type.code == gdb.TYPE_CODE_STRUCT:
print "Found a struct %s " % n
#self._print_fields(n, t)
self._print_deep_items(n, t, l)
else:
print "Found no struct"
def _print_deep_items (self, n_, type_, instance_):
for fld in type_.fields():
fn = fld.name
ft = fld.type
fv = instance_[fn]
if fv.type.code == gdb.TYPE_CODE_STRUCT:
print " Found a sub struct %s " % fn
self._print_deep_items(fn, ft, fv)
else:
print " Field %s " % fn, " type %s " % ft.tag, " value %s " % fv
And the output:
variable s1 type S1
Found a struct s1
Field v1 type None value 0
Field v2 type None value 0
Found a sub struct v3
Field w3 type None value 0
Update with the first sample: Got the following sample code working. This is not the optimal as it does a look-up on every field after composing the string field name. abarnert is showing a promising and elegant approach, the working code is updated in the above final update section.
import gdb
class PrintGList(gdb.Command):
"""print fields of a struct: wzd struct_object
Iterate through the fields of a struct, and display
a human-readable form of the objects."""
def __init__(self):
gdb.Command.__init__(self, "wzd", gdb.COMMAND_DATA, gdb.COMPLETE_SYMBOL, True)
def invoke(self, arg, from_tty):
arg_list = gdb.string_to_argv(arg)
if le