4

Here is the source file fragment:

#define TEST 34
#define PRINT_CONCAT(a, b) \
    printf("%d\n", a##b)

Compiling with GCC and linking this source file into a binary with flags -ggdb3 -O3 and running the app with gdb shows up the following behavior:

(gdb) p TEST                                                                                                                                                                                                       
$3 = 34                                                                                                                                                                                                            
(gdb) p PRINT_CONCAT                                                                                                                                                                                               
No symbol "PRINT_CONCAT" in current context. 

Is there a way to make gdb expand function macros in any way?

6
  • To see the effect of the macro, run just the preprocessor and examine the locations where the macro has been used... Commented Jul 31, 2022 at 23:51
  • @Fe2O3 It might be an approach, but when a project is huge enough it might not be a practical way... Commented Jul 31, 2022 at 23:52
  • What do you hope p PRINT_CONCAT would actually print, given that PRINT_CONCAT is a function-like macro? Commented Aug 1, 2022 at 0:03
  • 1
    Macro expansion is something that happens at compile time, so a debugging session is a poor context in which to explore it. Gdb may nevertheless be able to help you out with that a bit, but what you really ought to do is run the preprocessor (only) on some example code, and examine the result. Since you are using gcc, you can use the command gcc -E to do the preprocessing. Commented Aug 1, 2022 at 0:16
  • 1
    @JohnBollinger I agree with that, but the output of gcc -E might be very noisy and it's usually required to extract compile_command fragment for the specific file to run. Commented Aug 1, 2022 at 0:27

1 Answer 1

3

It turned to be as easy as macro expand

(gdb) macro expand PRINT_CONCAT(2, 4)                                                                                                                                                                              
expands to: printf("%d\n", 24) 

-ggdb3 is not even required, -g3 is enough. -g2 does not seem to include the desired information.

Sign up to request clarification or add additional context in comments.

2 Comments

Really nice job on figuring out how to do this given other commenters telling you to use the preprocessor because they didn't know the answer.
You may want to mention info macro PRINT_CONCAT as well (the title of your question asks for the macro definition, while the body asks for the expansion).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.