Getting call stack within C/C++ program (241 Views)
Reply
Frequent Advisor
Alex Vinokur
Posts: 52
Registered: ‎09-19-2010
Message 1 of 6 (241 Views)
Accepted Solution

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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,971
Registered: ‎03-06-2006
Message 2 of 6 (241 Views)

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.
Please use plain text.
Frequent Advisor
Alex Vinokur
Posts: 52
Registered: ‎09-19-2010
Message 3 of 6 (241 Views)

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
Please use plain text.
Frequent Advisor
Alex Vinokur
Posts: 52
Registered: ‎09-19-2010
Message 4 of 6 (241 Views)

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


Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,971
Registered: ‎03-06-2006
Message 5 of 6 (241 Views)

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.
Please use plain text.
Frequent Advisor
Alex Vinokur
Posts: 52
Registered: ‎09-19-2010
Message 6 of 6 (241 Views)

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

Dennis, thank you.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation