Function which prints stack trace of the program



 DEVELOP > c-Plus-Plus > Function which prints stack trace of the program

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "khan"
Date: 08 Nov 2007 03:42:46 AM
Object: Function which prints stack trace of the program
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
.

User: "James Kanze"

Title: Re: Function which prints stack trace of the program 08 Nov 2007 04:52:28 AM
On Nov 8, 10:42 am, khan <d.nayeem.k...@gmail.com> wrote:

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).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.

User: "Scott Gifford"

Title: Re: Function which prints stack trace of the program 08 Nov 2007 04:35:47 PM
khan <d.nayeem.khan@gmail.com> writes:

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.
.
User: "Brendon Costa"

Title: Re: Function which prints stack trace of the program 09 Nov 2007 04:19:34 PM
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.
.
User: "Victor Bazarov"

Title: Re: Function which prints stack trace of the program 09 Nov 2007 04:25:48 PM
Brendon Costa wrote:

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[s].
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.

User: "James Kanze"

Title: Re: Function which prints stack trace of the program 10 Nov 2007 06:57:30 AM
On Nov 9, 11:19 pm, Brendon Costa <bren...@christian.net> wrote:

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).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.



User: "Ian Collins"

Title: Re: Function which prints stack trace of the program 08 Nov 2007 04:22:30 AM
khan wrote:

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.
--
Ian Collins.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER