C语言 打印所有全局变量/局部变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6261392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:51:48  来源:igfitidea点击:

Printing all global variables/local variables?

cvariablesgdbglobal-variables

提问by cpuer

How can I print all global variables/local variables? Is that possible in gdb?

如何打印所有全局变量/局部变量?这在gdb中可能吗?

回答by kennytm

Type info variablesto list "All global and static variable names".

键入info variables以列出“所有全局和静态变量名称”。

Type info localsto list "Local variables of current stack frame" (names and values), including static variables in that function.

键入info locals以列出“当前堆栈帧的局部变量”(名称和值),包括该函数中的静态变量。

Type info argsto list "Arguments of the current stack frame" (names and values).

键入info args以列出“当前堆栈帧的参数”(名称和值)。

回答by Samuel ?slund

In case you want to see the local variables of a calling function use select-framebefore info locals

如果您想查看调用函数的局部变量,请使用select-frame之前info locals

E.g.:

例如:

(gdb) bt
#0  0xfec3c0b5 in _lwp_kill () from /lib/libc.so.1
#1  0xfec36f39 in thr_kill () from /lib/libc.so.1
#2  0xfebe3603 in raise () from /lib/libc.so.1
#3  0xfebc2961 in abort () from /lib/libc.so.1
#4  0xfebc2bef in _assert_c99 () from /lib/libc.so.1
#5  0x08053260 in main (argc=1, argv=0x8047958) at ber.c:480
(gdb) info locals
No symbol table info available.
(gdb) select-frame 5
(gdb) info locals
i = 28
(gdb) 

回答by Evgeni Sergeev

In addition, since info localsdoes not display the arguments to the function you're in, use

此外,由于info locals不显示您所在函数的参数,请使用

(gdb) info args

For example:

例如:

int main(int argc, char *argv[]) {
    argc = 6*7;    //Break here.
    return 0;
}

argcand argvwon't be shown by info locals. The message will be "No locals."

argc并且argv不会被 显示info locals。消息将是“没有本地人”。

Reference: info locals command.

参考:info locals 命令