gdbgdb-python

How to return a gdb.Value() from a string?


Consider the following gdb commands that output the same text string.

(gdb) print foo
(gdb) python print(gdb.lookup_symbol('foo'))

In this case gdb.lookup_symbol() returns a gdb.Value() instance as expected, and the stringification of it is equivalent to the default gdb stringification.

But now consider the following equivalent case:

(gdb) print *&foo

The *& is a noop, but trying to use gdb.lookup_symbol('*&foo') does not work. So my question is whether it is possible to make use of gdb's command line dereferencing parser from python, and then get a gdb.Value in return?


Solution

  • trying to use gdb.lookup_symbol('*&foo') does not work.

    That's because there is no symbol named *&foo in the binary.

    whether it is possible to make use of gdb's command line dereferencing parser from python, and then get a gdb.Value in return?

    Your question is not very clear, but I think you are asking for parse_and_eval:

    Function: gdb.parse_and_eval (expression)
    Parse expression, which must be a string, as an expression in the current language,
    evaluate it, and return the result as a gdb.Value.
    
    This function can be useful when implementing a new command (see Commands In Python),
    as it provides a way to parse the command’s argument as an expression.
    It is also useful simply to compute values, for example, it is the only way to get
    the value of a convenience variable (see Convenience Vars) as a gdb.Value.