LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Building a stack trace in C++ (http://www.linuxquestions.org/questions/programming-9/building-a-stack-trace-in-c-460132/)

Kikazaru 07-01-2006 08:05 PM

Building a stack trace in C++
 
I was wondering if there's a neat way to build a stack trace when you throw an exception... (I'm using g++-3.3)

I managed to get a source file and line number report with exceptions by defining:

#define throw_error(x) throw_detailed_error(__FILE__, __LINE__, x)

int error_line = 0;
char *error_file = "Unknown error";

void report_error(enum error_type et) {
std::cerr << "Exception: " << error_strings[et] << ", ";
std::cerr << error_file << " : " << error_line << std::endl;
}

void throw_detailed_error(char *file, int line, enum error_type et) {
error_line = line;
error_file = strdup(file);

throw et;
}

I "try" everything at the top level in main, catch exceptions and use report_error to display the source file and line number. I'd like to print the whole stack in this way, but it seems like I would have to catch exceptions in EVERY function, tag the source file/line number onto a list and rethrow the exception. That seems like a real pain.

zhangmaike 07-01-2006 09:46 PM

You could run it under gdb.

Start gdb with:
Code:

gdb PROGRAM_FILE
Type run to run it, and when the exception occurs and execution stops, type bt for a backtrace (it will print the stack, line numbers, functions, etc.)

Another way:

As long as your program is using throw or abort() to exit, the system will dump your program's core if the limit on the size of the core dump is reasonable.

Run
Code:

ulimit -c
to see the current upper limit in core dump size and

Code:

ulimit -c 100000000
to set it to unlimited.

When your program exits, you'll see something like
Code:

Aborted (core dumped)
A file will be created in the current directory with a name like "core". Run gdb on your program with the -c COREFILENAME option, and you can work on what is essentially a snapshot of your program running just before it aborted. Typing bt will give you a stack trace just as before.

paulsm4 07-02-2006 12:02 AM

And of course you could always write it in Java, where the stack trace is automatically available as a method call of your exception object ;-)

Kikazaru 07-02-2006 03:36 AM

Arg... I just wrote a reply that got munched by some browser malfunction.

To reiterate my lost post:

Thanks for the suggestions. I realized I can get a stack trace with gdb, but if you have an intermittent bug it can be tricky to replicate in gdb, and sometimes compiling with debugging options affects things so I was hoping to prepare a backtrace manually, in the code.

It certainly seems like Java is a winner in this respect... but is there really no nifty was to do it in C++?

paulsm4 07-02-2006 02:29 PM

Actually, there is. Just add "abort()" to your C++ exception handler(s). Voila - instant stack trace.

There are actually very compelling arguments in favor of leaving "abort()" in production code: if your program encounters a truly irrecoverable error, you *want* to try to capture as much information about the current state of the program as possible. Including the stack traceback. And "abort()" is the way to do it!

PS:
It's not, of course, the *only* solution. But (in many, many cases) it's a perfectly acceptable solution!

mhcox 07-02-2006 03:10 PM

Quote:

Originally Posted by Kikazaru
Arg... I just wrote a reply that got munched by some browser malfunction.

To reiterate my lost post:

Thanks for the suggestions. I realized I can get a stack trace with gdb, but if you have an intermittent bug it can be tricky to replicate in gdb, and sometimes compiling with debugging options affects things so I was hoping to prepare a backtrace manually, in the code.

It certainly seems like Java is a winner in this respect... but is there really no nifty was to do it in C++?

You don't have to be in gdb to use abort() to generate a core file. Instead of throwing the exception, call abort().

Another way of generating the core is to call the macro assert(). Assert takes a bool condition. If the condition is false it prints a message with the bool condition in it an calls abort(). The assert() processing can be disabled in production code by defining NDEBUG on the compiler command line.

Mike


All times are GMT -5. The time now is 09:18 AM.