From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 383 invoked by alias); 25 Oct 2011 18:54:50 -0000 Received: (qmail 372 invoked by uid 22791); 25 Oct 2011 18:54:49 -0000 X-SWARE-Spam-Status: No, hits=-2.2 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from qmta08.westchester.pa.mail.comcast.net (HELO qmta08.westchester.pa.mail.comcast.net) (76.96.62.80) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 25 Oct 2011 18:54:35 +0000 Received: from omta21.westchester.pa.mail.comcast.net ([76.96.62.72]) by qmta08.westchester.pa.mail.comcast.net with comcast id p5GM1h0071ZXKqc586ubLl; Tue, 25 Oct 2011 18:54:35 +0000 Received: from [10.127.238.91] ([65.206.2.68]) by omta21.westchester.pa.mail.comcast.net with comcast id p6uN1h01o1U2a2h3h6uQAt; Tue, 25 Oct 2011 18:54:33 +0000 Subject: Re: [RFA] Python: iterator for deep traversal of gdb.Type struct/union fields Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=us-ascii From: Paul Koning In-Reply-To: <4E8F0244-3A3F-4AE2-A13C-DA9503282A97@comcast.net> Date: Tue, 25 Oct 2011 19:03:00 -0000 Cc: Tom Tromey , Li Yu Content-Transfer-Encoding: quoted-printable Message-Id: <4C0389F5-A2DF-4864-8E33-BB893C885C98@comcast.net> References: <4E8595F6.7080004@gmail.com> <2460DAAE-C437-469A-BA1A-47343C5DBB45@comcast.net> <4E8F0244-3A3F-4AE2-A13C-DA9503282A97@comcast.net> To: gdb-patches@sourceware.org Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-10/txt/msg00675.txt.bz2 On Oct 25, 2011, at 2:23 PM, Paul Koning wrote: >=20 > On Oct 20, 2011, at 12:34 PM, Tom Tromey wrote: >=20 >>>>>>> "Paul" =3D=3D Paul Koning writes: >>=20 >> Paul> So I think that amounts to rejecting Yu's patch. >>=20 >> Yeah, I'm afraid so. >>=20 >> 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? T= hat >> Paul> would be a change in behavior for an existing feature. >>=20 >> I am not sure about this one :( >>=20 >>>> I would be in favor of helper functions in gdb.types, though. >>=20 >> Paul> What did you have in mind? >>=20 >> The sort of deep iterator you posted earlier. >>=20 >> Tom >=20 > How about this? I missed the documentation part for lib/gdb/types.py. Here is a revised pa= tch that adds the documentation. paul ChangeLog: 2011-10-25 Paul Koning * python/lib/gdb/types.py (deepitems): New function. testsuite/ChangeLog: =09 2011-10-25 Paul Koning * gdb.python/lib-types.cc (struct A): New structure. * gdb.python/lib-types.exp (deepitems): New tests. doc/ChangeLog: 2011-10-25 Paul Koning * gdb.texinfo (gdb.types): Document new deepitems function. =09 Index: doc/gdb.texinfo =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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 @@ =20 @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 =3D 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 =20 @node gdb.prompt Index: python/lib/gdb/types.py =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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] =3D 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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 @@ =20 enum1 enum1_obj (A); =20 +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 =3D {1,20,3,40}; + int main () { Index: testsuite/gdb.python/lib-types.exp =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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 =3D 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 =3D 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= '\]}