为什么GDB在以下示例中不打印宏值?
❯ cat sample.c
#include <stdio.h>
#define M 42
int main(int argc, const char **argv)
{
printf("M: %d\n", M);
return 0;
}
❯ rm -f sample
❯ gcc -Wall -g3 -ggdb -gdwarf-2 sample.c -o sample
❯ gdb sample
gdb> break main
gdb> run
gdb> info macro M
The symbol `M' has no definition as a C/C++ preprocessor macro
at <user-defined>:-1
gdb> continue
Continuing.
M: 42
谢谢!
❯ gcc --version
Apple LLVM version 7.3.0 (clang-703.0.29)
❯ gdb --version
GNU gdb (GDB) 7.10.1
答案 0 :(得分:2)
GCC 4.4.7和GDB 7.2的结果与您报告的结果不同。使用了源代码和编译命令后,我的GDB会话如下所示:
> gdb sample
[ ... startup banner ... ]
(gdb) break main
Breakpoint 1 at 0x4004d3: file sample.c, line 7.
(gdb) run
Starting program: /home/jbolling/tmp/sample
Breakpoint 1, main (argc=1, argv=0x7fffffffcba8) at sample.c:7
7 printf("M: %d\n", M);
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6_7.7.x86_64
(gdb) info macro M
Defined at /home/jbolling/tmp/sample.c:3
#define M 42
(gdb) continue
Continuing.
M: 42
Program exited normally.
(gdb)
我怀疑这里的关键区别以及您没有看到M
定义的原因是GDB意识到与函数main
处的断点关联的源位置。您报告的GDB输出提供了关于此的线索:
gdb> info macro M The symbol `M' has no definition as a C/C++ preprocessor macro at <user-defined>:-1
特别注意GDB报告的位置:“&lt; user-defined&gt;”文件,行号-1。在我的GDB运行中,断点与main()
正文中的第一个源行相关联。我倾向于相信,如果你打破那里,那么GDB将在该位置正确报告宏的定义。