tags:

views:

267

answers:

2

While browsing the various option switches for my compiler (GNU C++ 3.2.3 is supported by my organization for my given hardware configuration), I ran across this:

-glevel
   :
Level 3 includes extra information, such as all the macro definitions
present in the program. Some debuggers support macro expansion when
you use -g3.

I compiled a test program with a few macros (such as a loop, a if-then-else on an argument), then tried the commercial debugger TotalView, and GDB on the code compiled -g3. I didn't see any difference (macros were not expanded to their original code, I couldn't 'step into' the macros, etc.).

Anyone here had the experience of getting extra debugging 'features' using -g3 on GNU compilers?

+2  A: 

I have tried -g3 off and on since 1992 and I have never gotten it to do anything useful.

Norman Ramsey
+2  A: 

Your question appears to imply that you don't understand what macros are. Of course you can't step into a macro.

The -g3 is quite useful for "macro heavy" programs. Consider:

int main()
{
  int i;
  for (i = 0; i < 20; ++i) {
#define A(x) case x: printf(#x "\n"); break
    switch(i) {
      A(1); A(2); A(3); A(4); /* line 7 */
#undef A
#define A(x) case 10+x: printf("10+" #x "\n"); break
      A(1); A(2); /* line 10 */
    }
  }
  return 0;
}

Without -g3, when you are stopped on line 7 or 10, you may have to search quite a lot for definition of A(), and there could be many such definitions, so you would then have to figure out which one is "current".

With -g3, GDB can to do the heavy lifting for you:

(gdb) b 7
Breakpoint 1 at 0x4004cc: file m.c, line 7.
(gdb) b 10
Breakpoint 2 at 0x4004fc: file m.c, line 10.
(gdb) r

Breakpoint 1, main () at m.c:7
7         A(1); A(2); A(3); A(4);
(gdb) info macro A
Defined at /tmp/m.c:5
#define A(x) case x: printf(#x "\n"); break
(gdb) c
1
2
3
4

Breakpoint 2, main () at m.c:10
10        A(1); A(2);
(gdb) info macro A
Defined at /tmp/m.c:9
#define A(x) case 10+x: printf("10+" #x "\n"); break
(gdb) q
Employed Russian
I *do* understand that you can't step into a macro. That was my attempt to understand the manual's weak explanation that "Some debuggers support macro expansion" for -g3.Anyway, thanks, your explanation shows what I can *really* do with -g3! You win the accepted answer cookie!
Don Wakefield