Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function which prints stack trace of the program

Reply
Thread Tools

Function which prints stack trace of the program

 
 
khan
Guest
Posts: n/a
 
      11-08-2007
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-08-2007
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.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-08-2007
On Nov 8, 10:42 am, khan <(E-Mail Removed)> 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:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Scott Gifford
Guest
Posts: n/a
 
      11-08-2007
khan <(E-Mail Removed)> 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/developerwork...-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.
 
Reply With Quote
 
Brendon Costa
Guest
Posts: n/a
 
      11-09-2007
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.


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-09-2007
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


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-10-2007
On Nov 9, 11:19 pm, Brendon Costa <(E-Mail Removed)> 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...ce/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:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function call arguments in stack trace? Dun Peal Python 6 06-08-2011 02:45 AM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
How to redirect output from Diagnostics.Trace to Page.Trace? Matthias S. ASP .Net 1 11-30-2005 09:01 AM
Getting a stack trace of a running Ruby program. Ron M Ruby 9 11-12-2005 07:08 AM
Trace: Can anyone suggest a good tool to catch trace messages? Rukmal Fernando ASP .Net 4 10-27-2003 09:03 PM



Advertisments