From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24742 invoked by alias); 25 Oct 2011 18:24:13 -0000 Received: (qmail 24732 invoked by uid 22791); 25 Oct 2011 18:24:12 -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 X-Spam-Check-By: sourceware.org Received: from qmta14.westchester.pa.mail.comcast.net (HELO qmta14.westchester.pa.mail.comcast.net) (76.96.59.212) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 25 Oct 2011 18:23:58 +0000 Received: from omta15.westchester.pa.mail.comcast.net ([76.96.62.87]) by qmta14.westchester.pa.mail.comcast.net with comcast id p54v1h0041swQuc5E6Pyji; Tue, 25 Oct 2011 18:23:58 +0000 Received: from [10.127.238.91] ([65.206.2.68]) by omta15.westchester.pa.mail.comcast.net with comcast id p6Pl1h02X1U2a2h3b6PokG; Tue, 25 Oct 2011 18:23:56 +0000 Subject: [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: Date: Tue, 25 Oct 2011 18:34:00 -0000 Cc: Li Yu , gdb-patches@sourceware.org Content-Transfer-Encoding: quoted-printable Message-Id: <4E8F0244-3A3F-4AE2-A13C-DA9503282A97@comcast.net> References: <4E8595F6.7080004@gmail.com> <2460DAAE-C437-469A-BA1A-47343C5DBB45@comcast.net> To: Tom Tromey 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/msg00671.txt.bz2 On Oct 20, 2011, at 12:34 PM, Tom Tromey wrote: >>>>>> "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? Th= at > 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 How about this? paul ChangeLog: 2011-10-25 Paul Koning * python/lib/gdb/types.py (deepitems): New function. =09 testsuite/ChangeLog: 2011-10-25 Paul Koning * gdb.python/lib-types.cc (struct A): New structure. * gdb.python/lib-types.exp (deepitems): New tests. 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:22:10 -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:22:10 -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= '\]} 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:22:10 -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