Stack traces in C/C++ programs in solaris & linux

In Java, there are direct APIs for getting stack traces. But, for C, there are no popular APIs for doing the same. So, I was just searching for this feature in linux. Then I came across this link. It provides very clean and simple interface for getting stack traces. They even gave the sample program in the end of the article, demonstrating how to use it. Note: We have to compile the program with -rdynamic flag. In solaris, I know, with dtrace tool, we can do all these tricks. But, if I want to get/use the stack trace inside the C program, dtrace doesn’t help. After searching for “stack” in /usr/include/*.h contents, I found some functions in ucontext.h, which are relevant to what I want. Then, after seeing man page of one of their functions [man printstack], it is confirmed. I have written a small program to demonstrate stack traces in C program. You can download it here. For C++ programs, we may have to use c++filt [like ./a.out | c++filt] to get the correct functions names from mangled ones.



  1. October 13, 2006 at 10:14 pm

    On Solaris, take a look at walkcontext(3C) and printstack(3C).

  2. Sriram said,

    October 15, 2006 at 3:15 pm

    Hey Dude, great to see your posts ! Shall go through then
    sometime .

  3. rydahl said,

    March 23, 2007 at 3:00 pm

    I’m on solaris 8 . I don’t have printstack or walkcontext. I tested getcontext and it works . But how can I get the symbols out of the “stack_t” ?

  4. DP said,

    March 26, 2007 at 3:56 pm

    walkcontext, printstack functions are defined in

    Maybe, you can have a look at it.

  5. Praveen said,

    May 22, 2007 at 2:47 pm

    I’m Solaris 8 too,

    1) like rydahl said earlier – there is no printstack or walkcontext – but you can use the code from Solaris 10 which DP has pointed out to build your own library.

    2) This printstack doesnt handle static functions i suppose. As in those functions which are exposed with GLOBAL binding are only displayed, LOCAL bindings are not displayed – not very sure about WEAK bindigs (from the ELF file context)

  6. DP said,

    May 24, 2007 at 7:14 pm

    I saw this paragraph in printstack man page in solaris 10.

    The printstack() function uses walkcontext() to print a symbolic stack trace to the specified file descriptor. This is useful for reporting errors from signal handlers. The printstack() function uses dladdr1() (see dladdr(3C)) to obtain symbolic symbol names. As a result, only global symbols are reported as symbol names by printstack().

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: