BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Getting call stack within C/C++ program

SOLVED
Go to Solution
Highlighted
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
Acclaimed Contributor

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.
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
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


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.
Frequent Advisor

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

Dennis, thank you.