Function which prints stack trace of the program

K

khan

Hi

I am in need to write a function which prints the stack trace of
current program

The need is that if any exception occurs then apart from error message
we want to print the stack trace of the program

Has any one does something like this
if yes please share your experience with us

thanks in advance

from
khan
 
I

Ian Collins

khan said:
Hi

I am in need to write a function which prints the stack trace of
current program

The need is that if any exception occurs then apart from error message
we want to print the stack trace of the program

Has any one does something like this
if yes please share your experience with us
Yes (on an embedded target), but it is very system specific, there isn't
a portable way to do this.

I guess one answer is to use a debugger, or some other wrapper
application that can examine the state of yours when it bombs.
 
J

James Kanze

I am in need to write a function which prints the stack trace of
current program
The need is that if any exception occurs then apart from error message
we want to print the stack trace of the program
Has any one does something like this
if yes please share your experience with us

There's no portable solution, but I have code at my site
(kanze.james.neuf.fr) for PC (under Windwos or Linux) and Sparc
(both 32 bit and 64 bit).
 
S

Scott Gifford

khan said:
I am in need to write a function which prints the stack trace of
current program

[...]

Funny you mention that, I was just reading this article on how to do
this under Linux:

http://www-128.ibm.com/developerworks/linux/library/l-cppexcep.html

That's Linux-specific. The only portable way I know of is to hack
some macros together which use __FILE__ and __LINE__, and at each step
catch the exception, add their stack trace information, then re-throw
it.

Good luck!

----Scott.
 
B

Brendon Costa

Getting a stack trace is not simple cross platform. I have written a
class that will get it for, Windows, Linux and Other UNIX'es (Compiled
With GCC)

If you google the information i list below you might find examples.
Otherwise i can post the code for a class I created that does it all
on different platforms. I couldn't find James' source when i looked
briefly.

Linux is easiest:
glibc has a function: backtrace() which returns a list of addresses
for the backtrace.
Then with the resulting address list you call: backtrace_symbols()
which returns a list of strings with the addresses in a nice readable
format

The windows implementation was a harder one:
Basically you spawn of a helper thread that actually walks the
stack of the calling thread.
The spawned thread takes over and suspends the calling thread.
It then makes use of the win32 StackWalk() function (See
imagehlp.dll)
It also makes use of the Symbol engine in imagehlp.dll in order to
obtain readable strings
eventually after the spawned thread has a stack trace it passes it
to the calling thread and then un-suspends the calling thread

GCC implementation:
There is a novel approach using information generated by GCC. It is
used by BSD projects to provide a function similar to the linux:
backtrace() function. If interested i will post more information about
this later.

Brendon.
 
V

Victor Bazarov

Brendon said:
Getting a stack trace is not simple cross platform. I have written a
class that will get it for, Windows, Linux and Other UNIX'es (Compiled
With GCC)
[..] If interested i will post more information about
this later.

I recommend doing it in the platform newsgroup.

V
 
J

James Kanze

Getting a stack trace is not simple cross platform. I have written a
class that will get it for, Windows, Linux and Other UNIX'es (Compiled
With GCC)
If you google the information i list below you might find examples.
Otherwise i can post the code for a class I created that does it all
on different platforms. I couldn't find James' source when i looked
briefly.

It's in the Port sub-system, component StackTrace. If the site
is up (my provider is somewhat flakey), it should be accessible
from http://kanze.james.neuf.fr/code-en.html. If you just want
to look at the code, it's in
http://kanze.james.neuf.fr/code/Util/Port/StackTrace/index.html;
you'll have to go into the sub-directories, however, to find the
specific files. The documentation's in
http://kanze.james.neuf.fr/doc/en/Port/html/index.html.
Linux is easiest:
glibc has a function: backtrace() which returns a list of addresses
for the backtrace.

I don't use it, mainly because I had the basics of what was
necessary for PC before I ported to Linux. G++, in general, has
a couple of functions available as extensions which would be
useable.

Note that one of the main uses I make of it is to save the
walkback when allocating memory; this means that the code has to
be reasonably fast (no disk reads, for example), and the format
of the saved information has to be relatively small (I only save
the actual return address).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top