debugging 即使使用-g3、-ggdb 3或-gdwarf-4,也不会显示GDB宏符号

4szc88ey  于 2022-11-14  发布在  其他
关注(0)|答案(4)|浏览(214)

我有一个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

这曾经是工作的。我需要这个工作,因为我正在使用的库 *#定义 * 名称的硬件外设和内存地址。
这似乎与on the Sourceware GDB site)所示的行为直接矛盾。
我做错了什么?

ddrv8njm

ddrv8njm1#

看起来宏需要以这样或那样的方式“纳入范围”。如果你完全按照你链接到的页面中的示例操作,它们就会像广告上说的那样工作(至少对我来说是这样)。
示例(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 ./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
oxosxuxt

oxosxuxt2#

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

v1l68za43#

我得到了示例问题,并意识到我使用的是GCC的旧版本。
以前我用的是GCC 3.46和GDB 7.3,宏扩展不起作用,升级GCC到4.5.2和GDB到7.5解决了这个问题。

yzxexxkh

yzxexxkh4#

GDB只在预处理时替换所有宏的可执行文件上工作,因此 M 不存在于上下文中。

相关问题