Gdb macros are not present even when using -g3 or -ggdb3 or -gdwarf-4

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 using

 $ gdb ./sample 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 

It worked. I need this to work because I use libraries that have #define names for hardware peripherals and memory addresses.

This seems like a direct contradiction to the behavior shown on the gdb source site .

What am I doing wrong?

+4
source share
4 answers

It seems like macros should be β€œput into scope” one way or another. If you follow the examples exactly on the page you link to, they work as advertised (at least they do for me).

Example ( tc is your source file):

 $ gcc -O0 -g3 tc $ 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/tc: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) (gdb) 

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 tc, line 6. (gdb) r Starting program: /home/foo/tmp/a.out Breakpoint 1, main () at tc:6 6 printf("%d\n", M); (gdb) macro expand ADD(1) expands to: (42 + 1) // Macros loaded (gdb) 
+9
source

First try doing list :

 (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) (gdb) 
+1
source

I got a problem with the sample and realized that I was using the old version of gcc.

I used to use gcc 3.46 and gdb 7.3, the macro extension did not work, updating gcc to 4.5.2 and gdb to 7.5 resolved the problem.

+1
source

gdb works with an executable where all your macros are replaced only during preprocessing, so M is not present in context

0
source

Source: https://habr.com/ru/post/1411266/


All Articles