A.E. Drew A.E. Drew - 9 months ago 43
C++ Question

Can I use Visual Studio debugger within #define functions?

I am using Visual Studio 2010 (writing C++) and have isolated an exception coming from a call to a #define function. The defined function is a bit complex and I would like to be able to step through it with the debugger to find out where the exception occurs. Is there any way to do this?

The relationships are like this:

defines a class that inherits from a third-party class declared in
. The third-party class in
includes the macro of interest, which is defined in another header, say `macro.h'.

Answer Source

You need to expand the macro into your source code before you can debug it. You can either do it manually or make Visual Studio generate preprocessor output. http://msdn.microsoft.com/en-us/library/8z9z0bx6(v=vs.80).aspx describes the process for VS 2005. You will then have to create a new "debug" project with sources replaced with .i files generated by Visual Studio's preprocessor. This is a pain for any nontrivial project so I would try doing it manually first, especially since you identified the specific macro that causes exception, so you can try and manually expand just that one. You can also use those preprocessor-generated .i files for cross-reference.
UPD: If the macro in question is used in several locations, you may want to find out which location is responsible for exception. Run it under debugger (I assume you have full debug info generated, if not, you should) and check call stack at the time of exception. Expand just that location.
Be warned: certain projects in open source community are notorious for using macros as code obfuscation tools (I guess to keep their trade secrets from being discovered by reverse engineering) so good luck...