Skip to content
Advertisement

How to access the keys or values of Python GDB Value

I have a struct in GDB and want to run a script which examines this struct. In Python GDB you can easily access the struct via

(gdb) python mystruct = gdb.parse_and_eval("mystruct")

Now I got this variable called mystruct which is a GDB.Value object. And I can access all the members of the struct by simply using this object as a dictionary (likemystruct['member']).

The problem is, that my script doesn’t know which members a certain struct has. So I wanted to get the keys (or even the values) from this GDB.Value object. But neither mystruct.values() nor mystruct.keys() is working here.

Is there no possibility to access this information? I think it’s highly unlikely that you can’t access this information, but I didn’t found it anywhere. A dir(mystruct) showed me that there also is no keys or values function. I can see all the members by printing the mystruct, but isn’t there a way to get the members in python?

Advertisement

Answer

From GDB documentation:

You can get the type of mystruct like so:

tp = mystruct.type

and iterate over the fields via tp.fields()

No evil workarounds required ;-)

Update: GDB 7.4 has just been released. From the announcement:

Type objects for struct and union types now allow access to the fields using standard Python dictionary (mapping) methods.

Advertisement