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?
frame.read_var (symbol) should allow you to read the value of a symbol. Is this bug still valid?
Global/static values are not part of any frame.
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
what about this: (gdb) python print gdb.parse_and_eval("foo") 0 (gdb) python print gdb.parse_and_eval("foo").__class__.__name__
(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 :)
> 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
(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.
(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.
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
Fixed.