6

Why GDB doesn't print a macro's value in the following example?

❯ 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

Thanks!

❯ gcc --version
Apple LLVM version 7.3.0 (clang-703.0.29)
❯ gdb --version
GNU gdb (GDB) 7.10.1
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40
itsnikolay
  • 17,415
  • 4
  • 65
  • 64
  • 1
    Just asking, if you remove `-O2` does it make any difference? – Sourav Ghosh Jun 03 '16 at 11:12
  • @SouravGhosh no effect =( – itsnikolay Jun 03 '16 at 11:20
  • 1
    In [tag:c] it should be `int main(int argc, char **argv)` the `argv` is modifiable so no `const`. – Iharob Al Asimi Jun 03 '16 at 11:26
  • @iharob thanks, but still no effect =/ – itsnikolay Jun 03 '16 at 11:31
  • Did you try with another compiler ? If gdb cannot print info on the macro, it is because your compiler did not add the information about it in the DWARF. You can check which information about macro are in your ELF with the command `readelf --debug-dump=macro sample`. – blatinox Jun 03 '16 at 11:45
  • [This link](http://www.slac.stanford.edu/comp/unix/package/rtems/doc/html/gdb/gdb.info.Macros.html) says GDB "can evaluate expressions containing macro invocations" and "GDB uses the macros in scope at the current listing location" but you break at `main`, not on the line where `M` is expanded (`M` might be redefined before you reach its usage). – Weather Vane Jun 03 '16 at 11:50
  • I added the `clang` tag. Your issue is that `gcc` on your platform is really `clang`, and `clang/llvm` emits an empty `.debug_macinfo` section and no `.debug_macro` section. – Mark Plotnick Jun 03 '16 at 15:17

1 Answers1

2

I get different results with GCC 4.4.7 and GDB 7.2 than what you report. Having used your source and your compilation command, my GDB session looks like this:

> 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) 

I suspect that the key difference here, and the reason that you aren't seeing a definition of M, is in GDB's sense of the source location associated with a breakpoint at function main. The GDB output you reported provides a clue about this:

gdb> info macro M
  The symbol `M' has no definition as a C/C++ preprocessor macro
  at <user-defined>:-1

Note in particular the location GDB reports: "<user-defined>" file, line number -1. In my GDB run, the breakpoint was associated with the first source line in the body of main(). I am inclined to believe that if you break there then GDB will report correctly on the macro's definition at that location.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157