Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Getting call stack within C/C++ program

SOLVED
Go to solution
Alex Vinokur
Frequent Advisor

Getting call stack within C/C++ program

Hi,

Is there any system call that enables to get call stack within C/C++ program?

(Sometimes) I would like to use it instead of assert().


assert(condition);
If assert() fails we have got coredump and can get call stack using gdb.


I need something like
if (!condition)
{
get-call-stack();
}

Thanks
5 REPLIES
Dennis Handly
Acclaimed Contributor
Solution

Re: Getting call stack within C/C++ program

If you want to print a stack trace of the current thread to stderr, on Integrity you can do:
#include
U_STACK_TRACE();

For C applications, you need to link with -lunwind.
Alex Vinokur
Frequent Advisor

Re: Getting call stack within C/C++ program

Excellent. Thanks.
Two questions are below

// --- a.cpp ----
#include

void foo1 (int i)
{
U_STACK_TRACE();
}

void foo2 (int i1, int i2)
{
foo1 (i1 + i2);
}

int main()
{
foo2 (100, 200);
return 0;
}
// ----------

> aCC +DD64 -AA -g a.cpp

> a.out
(0) 0x4000000000000e50 _Z4foo1i + 0x40 at a.cpp:6 [./a.out]
(1) 0x4000000000000f20 _Z4foo2ii + 0x30 at a.cpp:11 [./a.out]
(2) 0x4000000000000fe0 main + 0x30 at a.cpp:16 [./a.out]
(3) 0xc000000000042450 main_opd_entry + 0x50 [/usr/lib/hpux64/dld.so]


====================
Question-1. Is it possible to print demangled names?
Ouestion-2. Is it possiblr to print also values of function arguments?

Thanks
Alex Vinokur
Frequent Advisor

Re: Getting call stack within C/C++ program

No problem with demangled names.

> ./a.out | & c++filt
(0) 0x4000000000000e50 foo1(int) + 0x40 at a.cpp:6 [./a.out]
(1) 0x4000000000000f20 foo2(int,int) + 0x30 at a.cpp:11 [./a.out]
(2) 0x4000000000000fe0 main + 0x30 at a.cpp:16 [./a.out]
(3) 0xc000000000042450 main_opd_entry + 0x50 [/usr/lib/hpux64/dld.so]


So, only second question: values of function arguments?

Thanks


Dennis Handly
Acclaimed Contributor

Re: Getting call stack within C/C++ program

>values of function arguments?

You would have to create your own. See uwx(3X) and unwind(5).

But depending on the opt level, all you can print out are the 8 parm registers and they may have been changed when you are getting a stack trace.
Highlighted
Alex Vinokur
Frequent Advisor

Re: Getting call stack within C/C++ program

Dennis, thank you.