Getting a stack trace

Discussion in 'C Programming' started by milkyway, Sep 6, 2005.

  1. milkyway

    milkyway Guest

    Hello out there,

    When one gets an error, they can use
    fprintf (Stdout, "File %s Line %d\n", __FILE__, __LINE__);

    Is there a similar way where on can print out the stack trace?

    Any help, hints or advice is appreciated ;-)
    milkyway, Sep 6, 2005
    #1
    1. Advertising

  2. "milkyway" <> writes:
    > Hello out there,
    >
    > When one gets an error, they can use
    > fprintf (Stdout, "File %s Line %d\n", __FILE__, __LINE__);
    >
    > Is there a similar way where on can print out the stack trace?
    >
    > Any help, hints or advice is appreciated ;-)


    There is no way to do this in portable standard C, unless you keep
    track of the information yourself by adding code to each function to
    maintain a program-wide stack of trace information.

    There may be Unix- or Linux-specific ways to do this. If you want to
    discuss them, please consider dropping comp.lang.c from the
    newsgroups.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 6, 2005
    #2
    1. Advertising

  3. milkyway

    milkyway Guest

    Hi,

    Will drop but do not know how to do this. Is there a program to collect
    the stack trace under Linux

    TIA
    milkyway, Sep 6, 2005
    #3
  4. "milkyway" <> wrote in message
    news:...
    > Will drop but do not know how to do this. Is there a program to collect
    > the stack trace under Linux


    Yes. Replace the assert(expr) with:

    if (!(expr)) *(char*)0=0;

    Compile with full debug info. Run the program under gdb. When the seg fault
    trips, use the 'bt' command which will give you a stack trace. The same
    technique works under Windows.

    -Walter
    www.digitalmars.com free C, C++, D compilers
    Walter Bright, Sep 6, 2005
    #4
  5. milkyway

    Flash Gordon Guest

    milkyway wrote:
    > Hi,
    >
    > Will drop but do not know how to do this.


    Send an email to whatever support Google provide and ask them how.
    Alternatively read whatever instructions they provide.

    Whilst you are about it, learn to quote context (it is possibly and
    there has been at least one post to comp.lang.c with instructions) and
    complain at google for providing such a broken interface.

    > Is there a program to collect
    > the stack trace under Linux


    You will have to hope someone on one of the other groups knows, since
    comp.lang.c is not the right place.

    Follow ups set to exclude comp.lang.c
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Sep 6, 2005
    #5
  6. "milkyway" <> wrote in message
    news:...
    > Hello out there,
    >
    > When one gets an error, they can use
    > fprintf (Stdout, "File %s Line %d\n", __FILE__, __LINE__);
    >
    > Is there a similar way where on can print out the stack trace?
    >
    > Any help, hints or advice is appreciated ;-)


    If all you want to know is the function call sequence (not the whole stack
    contents) that has led to this situation, then there's a solution: you
    create your own stack-like structure, into which you can push either a
    number or a string (i.e. char*) and then pop it off. Upon entry to each
    function you push that function's ID (number or string) to the stack, upon
    return you pop it off. At error you just dump the contents of the stack to
    stdout, stderr or elsewhere. The dump will contain the exact call
    sequence...

    Alex
    Alexei A. Frounze, Sep 7, 2005
    #6
  7. "milkyway" <> wrote in message
    news:...
    > Hi,
    >
    > Will drop but do not know how to do this. Is there a program to collect
    > the stack trace under Linux
    >
    > TIA
    >


    There are glibc-specific ways to do this. Look in execinfo.h for
    functions like backtrace, backtrace_symbols, and backtrace_symbols_fd.
    Helpful as well will be addr2line and similar tools. Google around, now that
    you know you are looking for a 'glibc' feature to provide 'backtrace'.

    DS
    David Schwartz, Sep 7, 2005
    #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. Rukmal Fernando
    Replies:
    4
    Views:
    838
    Richard Grimes [MVP]
    Oct 27, 2003
  2. Replies:
    1
    Views:
    302
  3. James Stroud

    Getting Stack Trace on segfault

    James Stroud, Apr 6, 2007, in forum: Python
    Replies:
    5
    Views:
    487
  4. Ron M
    Replies:
    9
    Views:
    568
    Stephen Kellett
    Nov 12, 2005
  5. Bryan Duxbury
    Replies:
    0
    Views:
    91
    Bryan Duxbury
    Oct 30, 2007
Loading...

Share This Page