[RFA] Python: iterator for deep traversal of gdb.Type struct/union fields

Paul Koning
2011-10-25T18:24:13Z

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?

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.

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:22:10 -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:22:10 -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'\]}
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:22:10 -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