cgdbgdb-python

using gdb python api, how to fetch the value of a c variable during debug session


I am new to gdb python api. But I am not sure how can I get the get the value of a particular variable during the debug session

import gdb

print("Import Sucess")
gdb.execute('b 36')
gdb.execute('r')
gdb.execute('stepi')
#o=gdb.parameter('print check_counter')
print("Check Counter")
print(gdb.value)
print("Done")

Solution

  • Use gdb.parse_and_eval to get a gdb.Value with the value of your variable.

    For instance, if you have a variable called "v" in your program you can do

    v = gdb.parse_and_eval("v")
    

    The v variable that you get in the gdb's python interpreter is a gdb.Value object that you can print, get the value of a field (if the original v variable is an object), etc.