Output function call information (Mac OS, Linux & windows) without interrupting the program during program debugging)

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/horkychen

When searching for problems,If you do not want to interrupt the program running, output the function execution sequence under a certain condition to help determineBit.


In xcode, You can edit the action in the breakpoint settings to debug.GER command, if you useGDBAs a debugger (Project settings ),EnterBacktraceAs follows:

Remember to check "automatically continue after evaluating" so that the program will not stop at this breakpoint, but will continue to run.


Running result:

#0 A (I = 5) at/xxxx/testbacktrace/Main. C: 20

#1 0x0000000100000e72 in main (argc = 1, argv = 0x7fff5fbffa88) at/xxxx/testbacktrace/Main. C: 25


If you useLldbAs the debugger, enterBTThe running result is as follows:

* Thread #1: tid = 0x2503, 0x0000000102238e37 testbacktrace 'a + 7 at main. C: 20, stop reason = breakpoint 1.1

Frame #0: 0x0000000102238e37 testbacktrace 'a + 7 at main. C: 20

Frame #1: 0x0000000102238e72 testbacktrace 'main + 34 at main. C: 25

Frame #2: 0x0000000102238d44 testbacktrace 'start + 52



The debugger options are as follows:



You can also call the backtrace function in the code.When you debug a multi-process program,The debugger may fail to attach the target process in a timely manner,This method is useful.

# Include <execinfo. h>

Void printcallstack (void)

{

Void * callstack [1, 128];

Int I, frames = backtrace (callstack, 128 );

Char ** STRs = backtrace_symbols (callstack, frames );

For (I = 0; I <frames; ++ I)

{

Printf ("% s \ n", STRs [I]);

}

Free (STRs );

}


Int A (int I)

{

Printcallstack ();

Return I + 1;

}

...


Output result:

0 testbacktrace 0x00000001013ddd6a printcallstack + 42

1 testbacktrace 0x00000001013dde30 A + 16

2 testbacktrace 0x00000001013dde72 main + 34

3 testbacktrace 0x00000001013ddd34 start + 52


In Visual Studio in windows, there is a similar practice. You should understand it by posting two images:


* For the second method of windows, refer to capturestackb Use of the acktrace function:1. msdn2. Win32-backtrace from C code
Related Article

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.