How to obtain runtime stack information in linux

Source: Internet
Author: User
How to obtain the stack information during running in linux-Linux general technology-Linux programming and kernel information. The following is a detailed description. "Scott A. Herod" wrote:
>
> Hello,
>
> Java's got a nice method for getting the stack trace at runtime.
> I use it often for debugging as an "assert" mechanic. Is there
> Something similar that I can do with C ++?
>
> Thanks,
>
> Scott
> _______________________________________________
> Web Page: http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug

There is if you use-rdynamic and-g. Incidentally, this may cause
Normal core dumps to provide less or different info. Here is a basic
Sample of a stack frame print function, which is probably specific
Linux:

# Include
# Include

Void show_stackframe ()
{
Void * trace [64];
Char ** messages = NULL;
Int trace_size = 0;

Cerr <"Begin Stack Frame Dump" <endl;
Cerr <"(if useful symbols are not found, try recompiling"
<"With-rdynamic during link, and-g without-O #)"
<Endl;
Trace_size = backtrace (trace, 64 );
Messages = backtrace_symbols (trace, trace_size );
For (int I = 0; I <trace_size; ++ I ){
Cerr <"Traced:" <messages <Endl;
}
Cerr <"End Stack Frame Dump" <endl;
}


I 've used this at times to debug asyncronous code and figure out what
Had been called from where, I actually keep a commented out version in
My game code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.