* Python API questions and use cases
@ 2010-09-15 13:43 Joel Borggrén-Franck
2010-09-15 18:01 ` Tom Tromey
0 siblings, 1 reply; 13+ messages in thread
From: Joel Borggrén-Franck @ 2010-09-15 13:43 UTC (permalink / raw)
To: gdb
Hi
I'm testing out the new Python API for GDB, and have a couple of use cases
that I don't know how to solve entirely from python. I hope this is the
right mailing list.
In all these cases I'm working on debugging a core.
1) Getting the value of a global
given:
typedef struct foo {
int bar;
int baz;
} foo;
foo myGlobalFoo;
in some C file, how do I access the value of myGlobalFoo from python?
The only working solution I have at the moment is to escape to gdb-script
with:
gdb.parse_and_eval("myGlobalFoo")
is this intended?
I know I can iterate over symbols in the symbol table, but I haven't found
a way to go from symbol to value.
2) Casts
given that I know that at address 0xdeadbeef there will be a struct of type
foo how do I get that struct (or a pointer to that struct) as a gdb.Value
object?
Currently i have written a cast function:
def cast(type_string, address):
"""Returns a gdb.Value object with type 'type_string' from memory
starting at 'address'."""
return gdb.parse_and_eval("(%s)((void *)%s)" % (type_string, address))
that I think works, but is there some other way?
Another usecase for casts would be if I wanted to mask an address in python,
given:
foo *fooP;
how do I do the equivalent of this in pyhton:
set $check = ((size_t)foo) & 1
I know there is the a cast in the API but that requires that I have a
gdb.Type which I don't know how to get.
I'd be happy to help out adding solutions to these use-cases!
Cheers
Joel
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-15 13:43 Python API questions and use cases Joel Borggrén-Franck
@ 2010-09-15 18:01 ` Tom Tromey
2010-09-15 19:04 ` Phil Muldoon
2010-09-16 7:33 ` Joel Borggrén-Franck
0 siblings, 2 replies; 13+ messages in thread
From: Tom Tromey @ 2010-09-15 18:01 UTC (permalink / raw)
To: Joel Borggrén-Franck; +Cc: gdb
>>>>> "Joel" == Joel Borggrén-Franck <joel.borggren.franck@gmail.com> 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, address))
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 = ((size_t)foo) & 1
Simplest is to do it using Python math:
foo = ...
check = long (foo) & 1
Otherwise you can look up size_t:
check = 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
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-15 18:01 ` Tom Tromey
@ 2010-09-15 19:04 ` Phil Muldoon
2010-09-16 6:13 ` Joel Borggrén-Franck
2010-09-16 7:33 ` Joel Borggrén-Franck
1 sibling, 1 reply; 13+ messages in thread
From: Phil Muldoon @ 2010-09-15 19:04 UTC (permalink / raw)
To: Tom Tromey; +Cc: Joel Borggrén-Franck, gdb
On 09/15/2010 06:56 PM, Tom Tromey wrote:
>>>>>> "Joel" == Joel Borggrén-Franck <joel.borggren.franck@gmail.com> 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?
If you have the frame, you can use .read_var and pass the symbol as an
argument. You can read the value behind the symbol then (as we know
what frame; can you divine it otherwise, just from the symbol?)
Cheers,
Phil
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-15 19:04 ` Phil Muldoon
@ 2010-09-16 6:13 ` Joel Borggrén-Franck
0 siblings, 0 replies; 13+ messages in thread
From: Joel Borggrén-Franck @ 2010-09-16 6:13 UTC (permalink / raw)
To: Phil Muldoon; +Cc: gdb
On Wed, Sep 15, 2010 at 9:04 PM, Phil Muldoon <pmuldoon@redhat.com> wrote:
> On 09/15/2010 06:56 PM, Tom Tromey wrote:
>>>>>>> "Joel" == Joel Borggrén-Franck <joel.borggren.franck@gmail.com> 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?
>
> If you have the frame, you can use .read_var and pass the symbol as an
> argument. You can read the value behind the symbol then (as we know
> what frame; can you divine it otherwise, just from the symbol?)
>
This is a global symbol, so that should not work. If you can get to
global symbols through a frame you have to devise a some way of
telling the programmer if she got the value for a frame-local symbol
with the same name or the global symbol.
IMHO it is better to just add a Symbol.getGlobalValue that returns Nil
or raises if used on a frame-local symbol.
Cheers
/Joel
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-15 18:01 ` Tom Tromey
2010-09-15 19:04 ` Phil Muldoon
@ 2010-09-16 7:33 ` Joel Borggrén-Franck
2010-09-16 9:30 ` Joachim Protze
` (2 more replies)
1 sibling, 3 replies; 13+ messages in thread
From: Joel Borggrén-Franck @ 2010-09-16 7:33 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb
On Wed, Sep 15, 2010 at 7:56 PM, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> "Joel" == Joel Borggrén-Franck <joel.borggren.franck@gmail.com> writes:
>
> 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.
>
Ok. I've approached the Python API as a (eventually) complete replacement
for GDB-script, so whenever I have to use parse_and_eval I feel it's an
(ugly) hack. This is perhaps not the intention?
> Could you file a bug report for this?
>
Done :)
>
> 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.
True. Then I think this would be useful:
gdb.new_address(addr) - returns a new gdb.Value (of type (void *)) pointing
to addr. addr is checked to be within bounds of the address space of the
inferior upon creation.
> 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 = ((size_t)foo) & 1
>
> Simplest is to do it using Python math:
>
> foo = ...
> check = long (foo) & 1
>
> Otherwise you can look up size_t:
>
> check = 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.
>
Oh, thanks. I had overlooked gdb.Value.address. Using that and your
example I have all the tools I need in this last case (no need to set
a convenience var, that was only for the example).
Thanks a lot!
Cheers
Joel
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-16 7:33 ` Joel Borggrén-Franck
@ 2010-09-16 9:30 ` Joachim Protze
2010-09-16 9:57 ` André Pönitz
2010-09-16 9:49 ` André Pönitz
2010-09-16 19:09 ` Tom Tromey
2 siblings, 1 reply; 13+ messages in thread
From: Joachim Protze @ 2010-09-16 9:30 UTC (permalink / raw)
To: Joel Borggrén-Franck; +Cc: gdb
Am 16.09.2010 09:32, schrieb Joel Borggrén-Franck:
> On Wed, Sep 15, 2010 at 7:56 PM, Tom Tromey <tromey@redhat.com> wrote:
>> 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.
>
> True. Then I think this would be useful:
>
> gdb.new_address(addr) - returns a new gdb.Value (of type (void *)) pointing
> to addr. addr is checked to be within bounds of the address space of the
> inferior upon creation.
>
Where do you get this address from? I cannot imagine a case where you
get a valid address from others than from inferior. So you have the
address as gdb.Value already.
Joachim
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-16 7:33 ` Joel Borggrén-Franck
2010-09-16 9:30 ` Joachim Protze
@ 2010-09-16 9:49 ` André Pönitz
2010-09-16 11:56 ` Joel Borggrén-Franck
2010-09-16 19:09 ` Tom Tromey
2 siblings, 1 reply; 13+ messages in thread
From: André Pönitz @ 2010-09-16 9:49 UTC (permalink / raw)
To: gdb
On Thursday 16 September 2010 09:32:31 ext Joel Borggrén-Franck wrote:
> > 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.
>
> True. Then I think this would be useful:
>
> gdb.new_address(addr) - returns a new gdb.Value (of type (void *)) pointing
> to addr. addr is checked to be within bounds of the address space of the
> inferior upon creation.
gdb.Value(addr).cast(gdb.lookup_type('void').pointer()) should do the trick.
Andre'
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-16 9:30 ` Joachim Protze
@ 2010-09-16 9:57 ` André Pönitz
0 siblings, 0 replies; 13+ messages in thread
From: André Pönitz @ 2010-09-16 9:57 UTC (permalink / raw)
To: gdb
On Thursday 16 September 2010 11:30:22 ext Joachim Protze wrote:
> Am 16.09.2010 09:32, schrieb Joel Borggrén-Franck:
> > On Wed, Sep 15, 2010 at 7:56 PM, Tom Tromey <tromey@redhat.com> wrote:
> >> 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.
> >
> > True. Then I think this would be useful:
> >
> > gdb.new_address(addr) - returns a new gdb.Value (of type (void *)) pointing
> > to addr. addr is checked to be within bounds of the address space of the
> > inferior upon creation.
> >
>
> Where do you get this address from? I cannot imagine a case where you
> get a valid address from others than from inferior. So you have the
> address as gdb.Value already.
There are lots of cases where the address is not in a gdb.Value already.
The address may come from the inferior, but not through Python, e.g
by parsing some CLI output. Or the address might be the result of some
manual computation. Or the address is some fixed value also known
on the outside.
Andre'
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-16 9:49 ` André Pönitz
@ 2010-09-16 11:56 ` Joel Borggrén-Franck
2010-09-16 19:11 ` Tom Tromey
0 siblings, 1 reply; 13+ messages in thread
From: Joel Borggrén-Franck @ 2010-09-16 11:56 UTC (permalink / raw)
To: André Pönitz; +Cc: gdb
On Thu, Sep 16, 2010 at 11:48 AM, André Pönitz <andre.poenitz@nokia.com> wrote:
> On Thursday 16 September 2010 09:32:31 ext Joel Borggrén-Franck wrote:
>> > 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.
>>
>> True. Then I think this would be useful:
>>
>> gdb.new_address(addr) - returns a new gdb.Value (of type (void *)) pointing
>> to addr. addr is checked to be within bounds of the address space of the
>> inferior upon creation.
>
>
> gdb.Value(addr).cast(gdb.lookup_type('void').pointer()) should do the trick.
>
This might be a case of missing (or obscure) documentation. Is there a
place (other than the C impl.) where I can find documentation for the
constructors/inits for the different gdb.XYZ types?
With your example I can turn a string into an address without escaping to
gdb-script. That is good enough for me, I can just roll my own helpers on
top of that (which is the primary benefit of using Python imho).
Cheers
/Joel
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-16 7:33 ` Joel Borggrén-Franck
2010-09-16 9:30 ` Joachim Protze
2010-09-16 9:49 ` André Pönitz
@ 2010-09-16 19:09 ` Tom Tromey
2 siblings, 0 replies; 13+ messages in thread
From: Tom Tromey @ 2010-09-16 19:09 UTC (permalink / raw)
To: Joel Borggrén-Franck; +Cc: gdb
>>>>> "Joel" == Joel Borggrén-Franck <joel.borggren.franck@gmail.com> writes:
Joel> Ok. I've approached the Python API as a (eventually) complete replacement
Joel> for GDB-script, so whenever I have to use parse_and_eval I feel it's an
Joel> (ugly) hack. This is perhaps not the intention?
We want it to be complete, but there will probably always be some uses
where parse_and_eval is the most convenient thing to do.
We're definitely interested in hearing about holes in the API.
It is fine to just file bug reports as you run across them.
Tom
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-16 11:56 ` Joel Borggrén-Franck
@ 2010-09-16 19:11 ` Tom Tromey
2010-09-17 7:35 ` Joel Borggrén-Franck
0 siblings, 1 reply; 13+ messages in thread
From: Tom Tromey @ 2010-09-16 19:11 UTC (permalink / raw)
To: Joel Borggrén-Franck; +Cc: André Pönitz, gdb
>>>>> "Joel" == Joel Borggrén-Franck <joel.borggren.franck@gmail.com> writes:
Joel> This might be a case of missing (or obscure) documentation. Is there a
Joel> place (other than the C impl.) where I can find documentation for the
Joel> constructors/inits for the different gdb.XYZ types?
The gdb manual should have full documentation for the Python API.
I say "should" because we did find an undocumented method once, so it is
possible that there are others that we missed. But by and large the
manual describes it all.
After seeing questions on the list for the last year or so, I would say
that the manual is weak either on examples or on a tutorial. Perhaps we
should put some effort into one or the other.
Tom
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-16 19:11 ` Tom Tromey
@ 2010-09-17 7:35 ` Joel Borggrén-Franck
2010-09-21 18:02 ` Tom Tromey
0 siblings, 1 reply; 13+ messages in thread
From: Joel Borggrén-Franck @ 2010-09-17 7:35 UTC (permalink / raw)
To: Tom Tromey; +Cc: André Pönitz, gdb
On Thu, Sep 16, 2010 at 9:11 PM, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> "Joel" == Joel Borggrén-Franck <joel.borggren.franck@gmail.com> writes:
>
> Joel> This might be a case of missing (or obscure) documentation. Is there a
> Joel> place (other than the C impl.) where I can find documentation for the
> Joel> constructors/inits for the different gdb.XYZ types?
>
> The gdb manual should have full documentation for the Python API.
> I say "should" because we did find an undocumented method once, so it is
> possible that there are others that we missed. But by and large the
> manual describes it all.
>
Ok.
Looking here: http://sourceware.org/gdb/current/onlinedocs/gdb/Values-From-Inferior.html#Values-From-Inferior
I can't find documentation for the constructor André showed me:
gdb.Value(addr). From my perspective that was something I had looked
for and needed but couldn't find through the current doc. It might be
the case that it is considered implicit, like "everyone knows you can
just create new gdb.Values" but I obviously had to ask here to figure
it out :)
I think this would be a good addition.
Cheers
Joel
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Python API questions and use cases
2010-09-17 7:35 ` Joel Borggrén-Franck
@ 2010-09-21 18:02 ` Tom Tromey
0 siblings, 0 replies; 13+ messages in thread
From: Tom Tromey @ 2010-09-21 18:02 UTC (permalink / raw)
To: Joel Borggrén-Franck; +Cc: André Pönitz, gdb
>>>>> "Joel" == Joel Borggrén-Franck <joel.borggren.franck@gmail.com> writes:
Joel> Looking here: http://sourceware.org/gdb/current/onlinedocs/gdb/Values-From-Inferior.html#Values-From-Inferior
Joel> I can't find documentation for the constructor André showed me:
Joel> gdb.Value(addr).
Thanks. I actually wasn't aware of this either :-)
I will send a documentation fix for it shortly.
Tom
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2010-09-21 18:02 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-15 13:43 Python API questions and use cases Joel Borggrén-Franck
2010-09-15 18:01 ` Tom Tromey
2010-09-15 19:04 ` Phil Muldoon
2010-09-16 6:13 ` Joel Borggrén-Franck
2010-09-16 7:33 ` Joel Borggrén-Franck
2010-09-16 9:30 ` Joachim Protze
2010-09-16 9:57 ` André Pönitz
2010-09-16 9:49 ` André Pönitz
2010-09-16 11:56 ` Joel Borggrén-Franck
2010-09-16 19:11 ` Tom Tromey
2010-09-17 7:35 ` Joel Borggrén-Franck
2010-09-21 18:02 ` Tom Tromey
2010-09-16 19:09 ` Tom Tromey
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox