4

I am debugging a huge C source code, and it has many macro definition. currently there is a segmentation fault is occurring at a macro. I want to be able to debug macro, step in macro definition just like as a function. I tried that

./configure debugflags="-gdwarf-2 -g3"
make

but this is not working, make is failing. Without above option it compile correctly, but could not debug macro.

How can I debug macro?

1
  • It appears that -g3 will include information about macros, but not enable you to step through them. The documentation doesn't indicate that you can. Your closest bet would be to stepi at an assembly instruction level. Commented May 20, 2014 at 6:39

2 Answers 2

6

You should never expect to be able to "step into" a macro; from the compiler's point of view macros do not exist. They are removed by the preprocessing step, before the actual compilation of the code begins.

You can try to generate the pre-processed version(s) of your source (this is the -E option to GCC) and compile those explicitly, so that you have a source file that contains each macro invocation expanded where it's used, that can help to make it clearer.

This is the "classic" and compiler-independent approach. Your compiler might give you further options, so explore the documentation.

Regarding your example, debugflags is not something I recognize, it's usually put in CFLAGS but perhaps your package does it differently.

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

1 Comment

@JonathonReinhart Are you sure? I've not used -g3, I had to look it up. It instructs GCC to include the macro definitions in the program, so that debuggers can expand them. This is obviously gcc-specific functionality that depends on the debugger to support it. That will mean you're not running or debugging the same code that you will have when compiling without -g3.
5

You could convert the macro into a static inline function, e.g. from

#define max(a, b) (a) > (b) ? (a) : (b)

to

static inline max(int a, int b)
{
    return a > b ? a : b;
}

This lets the compiler create debugging information for the macro (now function).

1 Comment

thanks, but this way, I have to change the code, this is a bit a thing I am avoiding. I will do that if other there is no other option.

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.