Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can C macros be expanded in gdb when the program was compiled using clang?

I have a macro that fetch's the J-th bit of an integer:

#define TWO_TO_THE(POWER) (1 << POWER)

#define JTH_BIT(BITS, J)  ((TWO_TO_THE((J-1)) & BITS) != 0)

However, I can't use it from gdb by issuing the command print JTH_BIT(i,j), can macros in C even be used while debugging?

like image 814
tlehman Avatar asked Dec 25 '22 13:12

tlehman


2 Answers

Macros are handled by the pre-processor. The compiler doesn't even know about them.

However, if lucky, GCC's option -g3 would do the job and generate code allowing gdb to expand a macro.

From the gdb documentation (emphasis by me):

-glevel

[...] Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.

like image 124
alk Avatar answered Jan 23 '23 13:01

alk


It should work, if you compile your program with the right options. In gcc, you need to say "-g3" when you compile

See here and here.

like image 26
Tim Avatar answered Jan 23 '23 13:01

Tim