Some Python ideas, looking for feedback

Andrew Oakley andrew@ado.is-a-geek.net
Wed Aug 17 18:37:00 GMT 2011


On Tue, 16 Aug 2011 09:45:35 -0500
<Paul_Koning@Dell.com> wrote:

> >On Tuesday 16 August 2011 13:03:42 ext Paul_Koning@Dell.com wrote:
> >> [...]
> >> And a question: is there a way to get the value corresponding to
> >> an enum type's value name? The name comes back in the fields()
> >> output but I don't see how I would get the value.  Could that be
> >> made a value attribute of the field object?
> >
> >int(...) works, or "%d" % ...
> 
> That works for Value objects, but not for fields of Type.  But it
> looks like an obvious way to make it work for Type (rather than a
> "value" attribute as I suggested earlier).

What you are looking for is (for some reason) stored in bitpos.  I
can't actually find any documentation for this :(.

You could use the function that is included with current gdb trunk
(not sure if it is any releases) in the module gdb.types.  It looks
like this:

> def make_enum_dict(enum_type):
>     """Return a dictionary from a program's enum type.
> 
>     Arguments:
>         enum_type: The enum to compute the dictionary for.
> 
>     Returns:
>         The dictionary of the enum.
> 
>     Raises:
>         TypeError: The type is not an enum.
>     """
> 
>     if enum_type.code != gdb.TYPE_CODE_ENUM:
>         raise TypeError("not an enum type")
>     enum_dict = {}
>     for field in enum_type.fields():
>         # The enum's value is stored in "bitpos".
>         enum_dict[field.name] = field.bitpos
>     return enum_dict

-- 
Andrew Oakley



More information about the Gdb mailing list