While discovering and debugging a large project it may be quite useful to print a stack trace. In Java I could use something like
for that purpose. (Yes, I know, it looks stupid and I prefer not to commit a code like that to CVS, but this dirty hack can still be quite useful while debugging).
Is their anyway to achieve the same goal in C / C++?
Of course there is no portable solution. But several platforms provide their extensions. I recently discovered that GCC provides functions backtrace() and backtrace_symbols() declared in <execinfo.h>. An example is available here: http://linux.die.net/man/3/backtrace_symbols. We need to link with -rdynamic option. The output contains the name of executable file, the name of function (if available) and looks like:
Or, in case of C++, the output can be:
Note that static function name is omitted in case of C or replaced with some dummy name in case of C++.
I still miss the information like source file names and line numbers. But at least this could give an idea of what to debug next.
However, I still wouldn't commit a code like that to production.