From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9702 invoked by alias); 15 Sep 2010 18:01:19 -0000 Received: (qmail 9351 invoked by uid 22791); 15 Sep 2010 18:01:13 -0000 X-SWARE-Spam-Status: No, hits=-6.2 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 15 Sep 2010 17:56:44 +0000 Received: from int-mx08.intmail.prod.int.phx2.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o8FHufYd014340 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 15 Sep 2010 13:56:41 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx08.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o8FHueUO010487; Wed, 15 Sep 2010 13:56:40 -0400 Received: from opsy.redhat.com (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id o8FHudSb017434; Wed, 15 Sep 2010 13:56:40 -0400 Received: by opsy.redhat.com (Postfix, from userid 500) id B93C33784BE; Wed, 15 Sep 2010 11:56:39 -0600 (MDT) From: Tom Tromey To: Joel =?utf-8?Q?Borggr=C3=A9n-Franck?= Cc: gdb@sourceware.org Subject: Re: Python API questions and use cases References: Date: Wed, 15 Sep 2010 18:01:00 -0000 In-Reply-To: ("Joel =?utf-8?Q?Borggr=C3=A9n-Franck=22's?= message of "Wed, 15 Sep 2010 15:43:48 +0200") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2010-09/txt/msg00087.txt.bz2 >>>>> "Joel" =3D=3D Joel Borggr=C3=A9n-Franck writes: Joel> 1) Getting the value of a global Joel> foo myGlobalFoo; Joel> in some C file, how do I access the value of myGlobalFoo from Joel> python? Joel> The only working solution I have at the moment is to escape to Joel> gdb-script with: Joel> gdb.parse_and_eval("myGlobalFoo") Joel> is this intended? This is simplest. Joel> I know I can iterate over symbols in the symbol table, but I Joel> haven't found a way to go from symbol to value. Hmm, we don't seem to expose a way to do that. Sorry about that. Could you file a bug report for this? Joel> 2) Casts Joel> given that I know that at address 0xdeadbeef there will be a Joel> struct of type foo how do I get that struct (or a pointer to that Joel> struct) as a gdb.Value object? Joel> Currently i have written a cast function: Joel> def cast(type_string, address): Joel> """Returns a gdb.Value object with type 'type_string' from memory Joel> starting at 'address'.""" Joel> return gdb.parse_and_eval("(%s)((void *)%s)" % (type_string, addr= ess)) Joel> that I think works, but is there some other way? I think the tricky part is getting a Value holding the appropriate constant. For that you might not have anything better, at present, than parse_and_eval. (This is a non-issue if the address is already a gdb.Value.) You can get a (simple) type using gdb.lookup_type. So you should be able to do something like: address.cast (gdb.lookup_type ('struct foo').pointer()) Joel> Another usecase for casts would be if I wanted to mask an address Joel> in python, given: Joel> foo *fooP; Joel> how do I do the equivalent of this in pyhton: Joel> set $check =3D ((size_t)foo) & 1 Simplest is to do it using Python math: foo =3D ... check =3D long (foo) & 1 Otherwise you can look up size_t: check =3D foo.cast (gdb.lookup_type ('size_t')) & 1 If you really want to set a convenience variable, then for the time being you will have to use parse_and_eval. We don't expose those any other way. Tom