10

I have this C file (sample.c):

#include <stdio.h>

#define M 42
#define ADD(x) (M + x)

int main ()
{
  printf("%d\n", M);
  printf("%d\n", ADD(2));
  return 0;
}

which I compile with:

gcc -O0 -Wall -g3 sample.c -o sample

Then debug with

gdb ./sample

Output:

GNU gdb (Gentoo 7.3.1 p2) 7.3.1
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.gentoo.org/>...
Reading symbols from /tmp/sample...done.

(gdb) macro list

(gdb) macro expand ADD(2)
expands to: ADD(2)

(gdb) print M
No symbol "M" in current context.

(gdb) q

This used to work. I need this to work, because I am using libraries which #define names for hardware peripherals and memory addresses.

This seems to be in direct contradiction of the behavior that is shown on the Sourceware GDB site).

What am I doing wrong?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luke Peterson
  • 931
  • 1
  • 9
  • 25

4 Answers4

14

It looks like the macros need to be "brought in scope" one way or another. If you follow exactly the examples in the page you link to, they work as advertised (at least they do for me).

Example (t.c is your source file):

$ gcc -O0 -g3 t.c

$ gdb ./a.out

GNU gdb (Gentoo 7.3.1 p2) 7.3.1
...
Reading symbols from .../a.out...done.
(gdb) info macro ADD
The symbol `ADD' has no definition as a C/C++ preprocessor macro
at <user-defined>:-1
             // Macros not loaded yet
(gdb) list main

1    #include <stdio.h>
2    #define M 42
3    #define ADD(x) (M + x)
4    int main ()
5    {
6      printf("%d\n", M);
7      printf("%d\n", ADD(2));
8      return 0;
9    }

(gdb) info macro ADD

Defined at /home/foo/tmp/t.c:3
#define ADD(x) (M + x)
             // Macros "in scope"/loaded

(gdb) macro expand ADD(42)

expands to: (42 + 42)

(gdb) macro expand M

expands to: 42

(gdb) macro expand ADD(M)

expands to: (42 + 42)

Or:

$ gdb ./a.out

GNU gdb (Gentoo 7.3.1 p2) 7.3.1
...
Reading symbols from .../a.out...done.

(gdb) macro expand ADD(1)

expands to: ADD(1)
             // Macros not available yet

(gdb) break main

Breakpoint 1 at 0x400538: file t.c, line 6.

(gdb) r

Starting program: /home/foo/tmp/a.out
Breakpoint 1, main () at t.c:6
6      printf("%d\n", M);

(gdb) macro expand ADD(1)

expands to: (42 + 1)
             // Macros loaded
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    Unless I use gdb 7.4, I don't get the behavior demonstrated in your examples. I think this is because some how gcc 4.8 (master experimental) must have changed how debugging symbols are stored in a way that older gdb's don't recognize. So basically, I need to make sure that if I use a bleeding edge compiler, I should be using a bleeding edge debugger. Thanks. – Luke Peterson May 09 '12 at 01:58
2

Try to do a list first:

(gdb) list

1       #include <stdio.h>
2       #define M 42
3       #define ADD(x) (M + x)
4       int main ()
5       {
6         printf("%d\n", M);
7         printf("%d\n", ADD(2));
8         return 0;
9       }
10

(gdb) info macro M

Defined at /home/ouah/tst.c:2
#define M 42

(gdb) info macro ADD

Defined at /home/ouah/tst.c:3
#define ADD(x) (M + x)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ouah
  • 142,963
  • 15
  • 272
  • 331
0

I got the sample problem and realized I was using an old version of GCC.

Previously I used GCC 3.46 and GDB 7.3, macro expansion didn't work, and upgrading GCC to 4.5.2 and GDB to 7.5 solved the issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shawn ji
  • 9
  • 1
-1

GDB works on an executable where all your macros are replaced at preprocessing time only, so M is not present in context.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pawan
  • 71
  • 1
  • 8
  • 1
    I've compiled exactly the same way in the past and been able to retain #define'd macros and constants. The Sourceware gdb site even documents this behavior as been available if you give -g3 or -ggdb3. So there is something wrong. – Luke Peterson May 09 '12 at 00:21
  • @Pawan, maybe you want to delete this answer, as this is not the case for GDB, and it might confuse readers. – Elazar Leibovich Aug 17 '15 at 09:01