Bug 12027 - Can't get to value from a global symbol using Pyhton API
Summary: Can't get to value from a global symbol using Pyhton API
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: 7.2
: P2 normal
Target Milestone: 7.5
Assignee: Tom Tromey
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-09-16 06:58 UTC by Joel Borggrén-Franck
Modified: 2012-02-07 19:49 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joel Borggrén-Franck 2010-09-16 06:58:44 UTC
Currently there is no way of getting the Value of a global symbol using the 
Python API.

There is Frame.read_var but that implies a local value.

I think there should be two api calls:

gdb.get_global_value(sym) where sym is a string or a Symbol. This should return 
the global value for sym (like Frame.read_var).

also

Symbol.get_global_value(self) which returns the global value or Nil (or raises 
if that is the Python way) if 'self' is not a global symbol.

Thanks
/Joel

For reference, an answer I got from Tom Tromey on GDB maillist:

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?
Comment 1 Phil Muldoon 2011-03-15 08:51:04 UTC
frame.read_var (symbol) should allow you to read the value of a symbol.

Is this bug still valid?
Comment 2 Joel Borggrén-Franck 2011-03-15 09:28:15 UTC
Global/static values are not part of any frame.
Comment 3 Joel Borggrén-Franck 2011-03-15 09:34:16 UTC
An example:

---
#include "stdio.h"

int foo = 0;

int main(void) {
   printf("something\n");
}
---

foo isn't part of any frame.

IIRC can't lookup foo through Frame.read_var, but even if I could, that isn't a very good API.

That is why I propose:

gdb.get_global_value(sym) and
Symbol.get_global_value(self)

cheers
/J
Comment 4 Kevin Pouget 2012-02-01 08:35:42 UTC
what about this:

(gdb) python print gdb.parse_and_eval("foo")
0
(gdb) python print gdb.parse_and_eval("foo").__class__.__name__
Comment 5 Joel Borggrén-Franck 2012-02-01 09:48:34 UTC
(In reply to comment #4)
> what about this:
> 
> (gdb) python print gdb.parse_and_eval("foo")
> 0
> (gdb) python print gdb.parse_and_eval("foo").__class__.__name__

The problem isn't that it is impossible to get the value, the problem is that there is no way of getting the value trough the Pyton API (proper).

IE the API is incomplete :)
Comment 6 Kevin Pouget 2012-02-01 09:55:38 UTC
> The problem isn't that it is impossible to get the value, the problem is that
> there is no way of getting the value trough the Pyton API (proper).

I'm not sure to get the distinction, `gdb.parse_and_eval` is part of the Python API, and the value returned and be read, modified, etc. from Python scripts; could you point something we can't currently do?

I dropped the last line in the previous comment, here it is:
> (gdb) python print gdb.parse_and_eval("foo").__class__.__name__
> Value
Comment 7 Joel Borggrén-Franck 2012-02-01 10:03:01 UTC
(In reply to comment #6)
> > The problem isn't that it is impossible to get the value, the problem is that
> > there is no way of getting the value trough the Pyton API (proper).
> 
> I'm not sure to get the distinction, `gdb.parse_and_eval` is part of the Python
> API, and the value returned and be read, modified, etc. from Python scripts;
> could you point something we can't currently do?

The distinction is that when you use parse_and_eval you aren't programming in Python, you are programming in gdb-script. If you need to use parse_and_eval all the time the value of the Pyton API diminishes.
Comment 8 Tom Tromey 2012-02-01 15:37:12 UTC
(In reply to comment #6)

> I'm not sure to get the distinction, `gdb.parse_and_eval` is part of the Python
> API, and the value returned and be read, modified, etc. from Python scripts;
> could you point something we can't currently do?

Yes.  Suppose you have variable shadowing inside a function.
Right now you can iterate over the blocks and get two different symbols,
representing the two variables with the same name.
However, parse_and_eval will only let you fetch the value of the
innermost one.
Comment 9 Sourceware Commits 2012-02-07 19:47:21 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	tromey@sourceware.org	2012-02-07 19:47:16

Modified files:
	gdb            : ChangeLog NEWS 
	gdb/doc        : ChangeLog gdb.texinfo 
	gdb/python     : py-frame.c py-symbol.c python-internal.h 
	gdb/testsuite  : ChangeLog 
	gdb/testsuite/gdb.python: py-symbol.c py-symbol.exp 

Log message:
	PR python/12027:
	* python/python-internal.h (frame_object_type): Declare.
	* python/py-symbol.c (sympy_needs_frame): New function.
	(sympy_value): New function.
	(symbol_object_getset): Add "needs_frame".
	(symbol_object_methods): Add "value".
	* python/py-frame.c (frame_object_type): No longer static.
	gdb/doc
	* gdb.texinfo (Symbols In Python): Document Symbol.needs_frame and
	Symbol.value.
	gdb/testsuite
	* gdb.python/py-symbol.exp: Test Symbol.needs_frame and
	Symbol.value.
	* gdb.python/py-symbol.c (qq): Set default value.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/ChangeLog.diff?cvsroot=src&r1=1.13812&r2=1.13813
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/NEWS.diff?cvsroot=src&r1=1.486&r2=1.487
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/doc/ChangeLog.diff?cvsroot=src&r1=1.1271&r2=1.1272
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/doc/gdb.texinfo.diff?cvsroot=src&r1=1.918&r2=1.919
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/python/py-frame.c.diff?cvsroot=src&r1=1.25&r2=1.26
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/python/py-symbol.c.diff?cvsroot=src&r1=1.12&r2=1.13
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/python/python-internal.h.diff?cvsroot=src&r1=1.55&r2=1.56
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/ChangeLog.diff?cvsroot=src&r1=1.3061&r2=1.3062
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.python/py-symbol.c.diff?cvsroot=src&r1=1.4&r2=1.5
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.python/py-symbol.exp.diff?cvsroot=src&r1=1.12&r2=1.13
Comment 10 Tom Tromey 2012-02-07 19:49:30 UTC
Fixed.