Function which prints stack trace of the program

Discussion in 'C++' started by khan, Nov 8, 2007.

  1. khan

    khan Guest

    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
     
    khan, Nov 8, 2007
    #1
    1. Advertising

  2. khan

    Ian Collins Guest

    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.
     
    Ian Collins, Nov 8, 2007
    #2
    1. Advertising

  3. khan

    James Kanze Guest

    On Nov 8, 10:42 am, khan <> 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:
    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
     
    James Kanze, Nov 8, 2007
    #3
  4. khan <> 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.
     
    Scott Gifford, Nov 8, 2007
    #4
  5. 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.
     
    Brendon Costa, Nov 9, 2007
    #5
  6. 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.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 9, 2007
    #6
  7. khan

    James Kanze Guest

    On Nov 9, 11:19 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 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:
    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
     
    James Kanze, Nov 10, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    23
    Views:
    4,012
    George W. Cherry
    Oct 6, 2003
  2. Prashanth Badabagni

    program that prints its source code

    Prashanth Badabagni, Sep 21, 2003, in forum: C Programming
    Replies:
    7
    Views:
    451
    Kevin Easton
    Sep 24, 2003
  3. Srinivas Mudireddy
    Replies:
    3
    Views:
    1,713
    Peter Nilsson
    Oct 1, 2009
  4. Dun Peal
    Replies:
    6
    Views:
    967
    Gabriel Genellina
    Jun 8, 2011
  5. Ron M
    Replies:
    9
    Views:
    621
    Stephen Kellett
    Nov 12, 2005
Loading...

Share This Page