即使使用-g3或-ggdb3或-gdwarf-4,gdb宏符号也不存在

时间:2012-05-08 09:42:46

标签: c debugging gdb

我有这个C文件(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;
}
我编译的

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

然后使用

进行调试
$ 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

这曾经工作过。我需要这个才能工作,因为我正在使用#define名称作为硬件外设和内存地址的库。

这似乎与Sourceware gdb site上显示的行为直接矛盾。

我做错了什么?

4 个答案:

答案 0 :(得分:12)

看起来宏需要以这种或那种方式“引入范围”。如果您完全按照链接到的页面中的示例,它们就像宣传的那样工作(至少它们对我有用)。

示例(t.c是您的源文件):

$ 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)
(gdb) 

或者:

$ 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
(gdb) 

答案 1 :(得分:2)

首先尝试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)

答案 2 :(得分:1)

我遇到了示例问题并意识到我使用的是旧版本的gcc。

以前我使用gcc 3.46和gdb 7.3,宏扩展不起作用,将gcc升级到4.5.2,gdb升级到7.5解决了这个问题。

答案 3 :(得分:0)

gdb适用于可执行文件,其中所有宏在预处理时被替换,因此M不存在于上下文中