Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Paul Koning <paulkoning@comcast.net>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@redhat.com>, Li Yu <raise.sail@gmail.com>
Subject: Re: [RFA] Python: iterator for deep traversal of gdb.Type struct/union fields
Date: Tue, 25 Oct 2011 19:03:00 -0000	[thread overview]
Message-ID: <4C0389F5-A2DF-4864-8E33-BB893C885C98@comcast.net> (raw)
In-Reply-To: <4E8F0244-3A3F-4AE2-A13C-DA9503282A97@comcast.net>


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


  reply	other threads:[~2011-10-25 18:54 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-30 11:04 [PATCH v2] gdb/python: add missing handling for anonymous members of struct and union Li Yu
2011-09-30 16:15 ` Paul Koning
2011-10-01 14:01   ` Li Yu
2011-10-01 18:54     ` Paul Koning
2011-10-04 16:37 ` Tom Tromey
2011-10-04 18:05   ` Paul Koning
2011-10-04 20:24     ` Tom Tromey
2011-10-04 20:41       ` Paul Koning
2011-10-19 20:52         ` Tom Tromey
2011-10-19 20:59           ` Paul Koning
2011-10-20 18:49             ` Tom Tromey
2011-10-25 18:34               ` [RFA] Python: iterator for deep traversal of gdb.Type struct/union fields Paul Koning
2011-10-25 19:03                 ` Paul Koning [this message]
2011-10-25 20:16                   ` Eli Zaretskii
2011-10-26 17:14                     ` Paul Koning
2011-10-27 13:01                       ` Doug Evans
2011-10-27 14:52                         ` Paul_Koning
2011-10-27 19:57                           ` Tom Tromey
     [not found]                             ` <09787EF419216C41A903FD14EE5506DD030CF168FB@AUSX7MCPC103.AMER.DELL.COM>
2011-10-27 21:56                               ` Paul Koning
2011-10-27 22:14                                 ` Eli Zaretskii
2011-10-28 14:55                                   ` Paul Koning

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4C0389F5-A2DF-4864-8E33-BB893C885C98@comcast.net \
    --to=paulkoning@comcast.net \
    --cc=gdb-patches@sourceware.org \
    --cc=raise.sail@gmail.com \
    --cc=tromey@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox