LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-01-2006, 08:05 PM   #1
Kikazaru
LQ Newbie
 
Registered: Nov 2003
Location: Chiba, Japan
Distribution: Ubuntu 10.10 AMD64
Posts: 17

Rep: Reputation: 0
Question 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.
 
Old 07-01-2006, 09:46 PM   #2
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
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.
 
Old 07-02-2006, 12:02 AM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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 ;-)
 
Old 07-02-2006, 03:36 AM   #4
Kikazaru
LQ Newbie
 
Registered: Nov 2003
Location: Chiba, Japan
Distribution: Ubuntu 10.10 AMD64
Posts: 17

Original Poster
Rep: Reputation: 0
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++?
 
Old 07-02-2006, 02:29 PM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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!
 
Old 07-02-2006, 03:10 PM   #6
mhcox
Member
 
Registered: Aug 2005
Location: Albuquerque, NM
Distribution: Fedora
Posts: 30

Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference b/t Kernel stack and User stack hazzyb Linux - Software 2 09-29-2008 07:40 PM
java: stack trace eantoranz Programming 3 04-25-2005 11:38 AM
stack trace of a process node047 Linux - Newbie 2 04-01-2005 09:11 PM
How To interpret kernel stack trace kklier Linux - Enterprise 6 08-22-2004 09:24 AM
Stack trace ust Linux - General 0 02-27-2004 02:30 AM


All times are GMT -5. The time now is 01:25 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration