Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a preprocessor macro

I recently came across this project. The code is largely written in C and the API consists of just a few C functions. Unfortunately the project seems to contain some bugs, in particular I keep getting "double free or corruption" errors. I am trying to use valgrind and gdb to find out what is wrong. The problem seems to be in the memory allocator. Unfortunately the first valgrind error occurs in some ~400 line long preprocessor macro defined in a header. Unfortunately gdb can't break on the generated code. The stack trace is not very useful either. Is there any technique which can be used to deal with these kind of errors?

like image 695
hfhc2 Avatar asked Jul 11 '16 11:07

hfhc2


People also ask

How do I debug a macro in Visual Studio?

Open up a source file from your manage project in Visual Studio and set a breakpoint on a line. Start debugging in Visual Studio by pressing F5. In Excel, open up your worksheet and start debugging your VBA code using Excel's debugger.

How do I use macros in GDB?

If there is a current stack frame, GDB uses the macros in scope at that frame's source code line. Otherwise, GDB uses the macros in scope at the current listing location; see List. Whenever GDB evaluates an expression, it always expands any macro invocations present in the expression.

What is preprocessor macros in Xcode?

Preprocessor Macros are directives executed by the compiler, so they're not part of Objective C. We've used them many times before with the #import statement. But to stay with our Lite and Pro example: you can use a Preprocessor if/then statement to check which version is being compiled.


1 Answers

The online compiler Wandbox.org has a "CPP" mode that is very useful to experiment with the C preprocessor.

See an example here: https://wandbox.org/permlink/tFUsKMIXaQj8hhte

You can do the same thing offline, with gcc -P or cl.exe /E

like image 126
Benoit Blanchon Avatar answered Oct 02 '22 00:10

Benoit Blanchon