This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [RFA] Python: iterator for deep traversal of gdb.Type struct/union fields
On Oct 25, 2011, at 2:23 PM, Paul Koning wrote:
>
> On Oct 20, 2011, at 12:34 PM, Tom Tromey wrote:
>
>>>>>>> "Paul" == Paul Koning <paulkoning@comcast.net> writes:
>>
>> Paul> So I think that amounts to rejecting Yu's patch.
>>
>> Yeah, I'm afraid so.
>>
>> Paul> Also, given my point 3, does that mean we should change val["foo"] so
>> Paul> it doesn't recurse down into anonymous fields as it does today? That
>> Paul> would be a change in behavior for an existing feature.
>>
>> I am not sure about this one :(
>>
>>>> I would be in favor of helper functions in gdb.types, though.
>>
>> Paul> What did you have in mind?
>>
>> The sort of deep iterator you posted earlier.
>>
>> Tom
>
> How about this?
I missed the documentation part for lib/gdb/types.py. Here is a revised patch that adds the documentation.
paul
ChangeLog:
2011-10-25 Paul Koning <paul_koning@dell.com>
* python/lib/gdb/types.py (deepitems): New function.
testsuite/ChangeLog:
2011-10-25 Paul Koning <paul_koning@dell.com>
* gdb.python/lib-types.cc (struct A): New structure.
* gdb.python/lib-types.exp (deepitems): New tests.
doc/ChangeLog:
2011-10-25 Paul Koning <paul_koning@dell.com>
* gdb.texinfo (gdb.types): Document new deepitems function.
Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.881
diff -u -r1.881 gdb.texinfo
--- doc/gdb.texinfo 25 Oct 2011 18:35:19 -0000 1.881
+++ doc/gdb.texinfo 25 Oct 2011 18:50:28 -0000
@@ -24424,6 +24424,34 @@
@item make_enum_dict (@var{enum_type})
Return a Python @code{dictionary} type produced from @var{enum_type}.
+
+@item deepitems (@var{type})
+Returns a Python iterator similar to the standard
+@code{gdb.Type.iteritems ()} method, except that the iterator returned
+by @code{deepitems} will recursively traverse anonymous struct or
+union fields. For example:
+
+@smallexample
+struct A
+@{
+ int a;
+ union @{
+ int b0;
+ int b1;
+ @};
+@};
+@end smallexample
+
+Then in gdb:
+@smallexample
+(gdb) python import gdb.types
+(gdb) python struct_a = gdb.lookup_type("struct A")
+(gdb) python print struct_a.keys ()
+@{['a', '']@}
+(gdb) python print [k for k,v in gdb.types.deepitems(struct_a)]
+@{['a', 'b0', 'b1']@}
+@end smallexample
+
@end table
@node gdb.prompt
Index: python/lib/gdb/types.py
===================================================================
RCS file: /cvs/src/src/gdb/python/lib/gdb/types.py,v
retrieving revision 1.2
diff -u -r1.2 types.py
--- python/lib/gdb/types.py 1 Jan 2011 15:33:27 -0000 1.2
+++ python/lib/gdb/types.py 25 Oct 2011 18:50:28 -0000
@@ -89,3 +89,23 @@
# The enum's value is stored in "bitpos".
enum_dict[field.name] = field.bitpos
return enum_dict
+
+
+def deepitems (type_):
+ """Return an iterator that recursively traverses anonymous fields.
+
+ Arguments:
+ type_: The type to traverse. It should be one of
+ gdb.TYPE_CODE_STRUCT or gdb.TYPE_CODE_UNION.
+
+ Returns:
+ an iterator similar to gdb.Type.iteritems(), i.e., it returns
+ pairs of key, value, but for any anonymous struct or union
+ field that field is traversed recursively, depth-first.
+ """
+ for k, v in type_.iteritems ():
+ if k:
+ yield k, v
+ else:
+ for i in deepitems (v.type):
+ yield i
Index: testsuite/gdb.python/lib-types.cc
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.python/lib-types.cc,v
retrieving revision 1.2
diff -u -r1.2 lib-types.cc
--- testsuite/gdb.python/lib-types.cc 1 Jan 2011 15:33:49 -0000 1.2
+++ testsuite/gdb.python/lib-types.cc 25 Oct 2011 18:50:30 -0000
@@ -54,6 +54,34 @@
enum1 enum1_obj (A);
+struct A
+{
+ int a;
+ union {
+ int b0;
+ int b1;
+ union {
+ int bb0;
+ int bb1;
+ union {
+ int bbb0;
+ int bbb1;
+ };
+ };
+ };
+ int c;
+ union {
+ union {
+ int dd0;
+ int dd1;
+ };
+ int d2;
+ int d3;
+ };
+};
+
+struct A a = {1,20,3,40};
+
int
main ()
{
Index: testsuite/gdb.python/lib-types.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.python/lib-types.exp,v
retrieving revision 1.3
diff -u -r1.3 lib-types.exp
--- testsuite/gdb.python/lib-types.exp 1 Jan 2011 15:33:49 -0000 1.3
+++ testsuite/gdb.python/lib-types.exp 25 Oct 2011 18:50:30 -0000
@@ -138,3 +138,8 @@
gdb_test_no_output "python enum1_list = enum1_dict.items ()"
gdb_test_no_output "python enum1_list.sort ()"
gdb_test "python print enum1_list" {\[\('A', 0L\), \('B', 1L\), \('C', 2L\)\]}
+
+# test deepitems
+gdb_test_no_output "python struct_a = gdb.lookup_type ('struct A')"
+gdb_test "python print struct_a.keys ()" {\['a', '', 'c', ''\]}
+gdb_test "python print \[k for k,v in gdb.types.deepitems(struct_a)\]" {\['a', 'b0', 'b1', 'bb0', 'bb1', 'bbb0', 'bbb1', 'c', 'dd0', 'dd1', 'd2', 'd3'\]}