Search code examples
cdebugginggdbclang

Debug Preprocessor Macro in clang


It seems that I cannot get information on all the preprocessor macros of a clang program from gdb.

Consider this simple code:

#include <stdio.h>

#define MACRO_B   2

int main()
{
    printf("MACRO A: %d\n", (int) MACRO_A);
    printf("MACRO B: %d\n", (int) MACRO_B);
    return 0;
}

I define MACRO_A on the command line with:

clang -ggdb3 -fdebug-macro -DMACRO_A=1 -o test test.c

According to the documentation, the option -fdebug-macro should be enough to add information on both MACRO_A and MACRO_B, however using gdb I cannot access to information on MACRO_A:

(gdb) info macro MACRO_B
Defined at /data/work/external/developer/SO/debug_preprocessing_macro_clang/test.c:3
#define MACRO_B 2
(gdb) info macro MACRO_A
The symbol `MACRO_A' has no definition as a C/C++ preprocessor macro
at /data/work/external/developer/SO/debug_preprocessing_macro_clang/test.c:7

I'm using clang version 12.0.0-3ubuntu1~20.04.5 and gdb 9.2-0ubuntu1~20.04.1.

Is there something that I'm missing?


Solution

  • I suspect this relates to this GDB bug which relates to handling of macros defined on the command line when using Clang.

    This bug was fixed in GDB 13 and later, so I think the answer is, update GDB.

    When I use GDB 14, and Clang 15, I see this behaviour:

    (gdb) info macro MACRO_A
    Defined at /tmp/clang-macro/test.c:0
    -DMACRO_A=1
    (gdb) info macro MACRO_B
    Defined at /tmp/clang-macro/test.c:3
    #define MACRO_B 2