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 10: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 01: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 04: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++?

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!

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.