简体   繁体   中英

Can gdb print expanded preprocessor/macro results?

For example:

#include <stdlib.h>

#define A 20
#define B 22
#define C (A+B)

int main()
{
    srand(time(0));
    int i = (rand()&1) + C;
    return i;
}

In gdb,

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

How can I know what C is? Can gdb tell me? (I added rand() so we can't easily deduce what it was)

The preprocessor will have replaced C with (20+22) . Is this value available in the debuginfo to print somehow?

In a real example where the macro could be exceedingly complex, I don't want to waste my time doing the job of the preprocessor.

How can I know what C is?

First of all you need to build the program with -g3 flag so that macro information is included in debugging information, start the program and show macro definition with info macro :

(gdb) start
Temporary breakpoint 1 at 0x40114e: file 1.c, line 9.
Starting program: /tmp/a.out 
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.31-4.fc32.x86_64

Temporary breakpoint 1, main () at 1.c:9
9       srand(time(0));
(gdb) info macro C
Defined at /tmp/1.c:5
#define C (A+B)
(gdb) info macro A
Defined at /tmp/1.c:3
#define A 20
(gdb) info macro B
Defined at /tmp/1.c:4
#define B 22

You can also expand macro:

(gdb) macro expand C
expands to: (20+22)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM