为什么 GDB 不能从带有信息符号的地址中找到符号?
Why can't GDB find symbol from address with info symbol?
在尝试调试一些简单代码时,我无法让 GDB 从具有 info symbol
的内存地址中识别局部变量。
代码是使用g++ -g3 -Wall -Wextra
编译的。
int main()
{
int foo = 1234;
return foo;
}
(gdb) p foo
= 1234
(gdb) p &foo
= (int *) 0x7fffffffd7c4
(gdb) info symbol 0x7fffffffd7c4
No symbol matches 0x7fffffffd7c4.
(gdb) info address foo
Symbol "foo" is a complex DWARF expression:
0: DW_OP_fbreg -28
.
为什么在这种情况下GDB不能识别变量? info symbol
是否只适用于全局对象?
局部变量驻留在堆栈或寄存器中。访问堆栈与程序的符号 table 无关。如 Examining the symbol table
中所述
The commands described in this chapter allow you to inquire about the symbols (names of variables, functions and types) defined in your program. This information is inherent in the text of your program and does not change as your program executes.
局部变量的位置永远不会恒定,因为堆栈帧的当前位置取决于调用深度和许多其他因素。局部变量根本不是程序符号 table 的一部分,因此您无法使用 symbol
命令检查它们。
您可以通过简单地从您的 shell 调用 nm
来查看您的符号 table 中的内容,也许还可以使用 c++filt 来获得可读的名称。
> nm|c++filt
在尝试调试一些简单代码时,我无法让 GDB 从具有 info symbol
的内存地址中识别局部变量。
代码是使用g++ -g3 -Wall -Wextra
编译的。
int main()
{
int foo = 1234;
return foo;
}
(gdb) p foo
= 1234
(gdb) p &foo
= (int *) 0x7fffffffd7c4
(gdb) info symbol 0x7fffffffd7c4
No symbol matches 0x7fffffffd7c4.
(gdb) info address foo
Symbol "foo" is a complex DWARF expression:
0: DW_OP_fbreg -28
.
为什么在这种情况下GDB不能识别变量? info symbol
是否只适用于全局对象?
局部变量驻留在堆栈或寄存器中。访问堆栈与程序的符号 table 无关。如 Examining the symbol table
中所述The commands described in this chapter allow you to inquire about the symbols (names of variables, functions and types) defined in your program. This information is inherent in the text of your program and does not change as your program executes.
局部变量的位置永远不会恒定,因为堆栈帧的当前位置取决于调用深度和许多其他因素。局部变量根本不是程序符号 table 的一部分,因此您无法使用 symbol
命令检查它们。
您可以通过简单地从您的 shell 调用 nm
来查看您的符号 table 中的内容,也许还可以使用 c++filt 来获得可读的名称。
> nm|c++filt