Get all global variables / local variables in python gdb interface

After reading the Print all global variables / local variables post , I found out that we can get all the variables of the current frame on the GDB command line.

My question is how to get all the variables of the current frame in the Python GDB interface, because it info localsjust gives the results as strings, and this is not convenient for further use.

+4
source share
2 answers

Has the question changed? I am not sure, but I suspect, since my previous answer is very wrong. I vaguely remember that we were talking about global variables, and in this case this is true:

I do not think there is a way. GDB character tables are only partially affected by Python, and I believe that not being able to iterate over them is one of the holes.

However, it is easy to transfer local variables from Python. You can use gdb.selected_frame()to get the selected frame. Then from the frame you can use the method block()to get the object Block.

An object

A Blockrepresents a region. You can iterate directly Blockto get variables from this area. Then go to the area with Block.superblock. When you click on a block with an attribute function, you fall into the farthest scope of the function.

+4
source

This shows how to list all currently visible variables (once) based on Tom's suggestions.

, , , , , .

, , .

, info locals . , set.

main.py

gdb.execute('file a.out', to_string=True)
gdb.execute('break 10', to_string=True)
gdb.execute('run', to_string=True)
frame = gdb.selected_frame()
block = frame.block()
names = set()
while block:
    if(block.is_global):
        print()
        print('global vars')
    for symbol in block:
        if (symbol.is_argument or symbol.is_variable):
            name = symbol.name
            if not name in names:
                print('{} = {}'.format(name, symbol.value(frame)))
                names.add(name)
    block = block.superblock

main.c

int i = 0;
int j = 0;
int k = 0;

int main(int argc, char **argv) {
    int i = 1;
    int j = 1;
    {
        int i = 2;
        i = 2; /* Line 10. Add this dummy line so above statement takes effect. */
    }
    return 0;
}

gcc -ggdb3 -O0 -std=c99 main.c
gdb --batch -q -x main.py

:

i = 2
argc = 1
argv = 0x7fffffffd718
j = 1

global vars
k = 0

, enum, symbol.is_constant.

Ubuntu 14.04, GDB 7.7.1, GCC 4.8.4.

+1

All Articles