Print the call stack in a program

Source: Internet
Author: User

Never spent much effort on learning and debugging, the main or peacetime debugging opportunities are relatively small, under normal circumstances, with Strace, GDB, and through the print log can basically solve the problem, there is, rather than spend energy to improve debugging skills, as well as in the design, Defensive programming and unit testing capabilities to improve, and improve the quality of your own coding, reduce the appearance of bugs or shrink the scope of bugs.

However, sometimes debugging tools are not used to find bugs, also useful when reading and analyzing source code, the following code shows how to print the call stack in your own program, and sometimes you want to know what function a function called at a given moment, just print the call stack in the function. This is useful in situations where debugging tools are inconvenient, such as when a program is running on a development board.

The following sample code I found from the Web, I removed the comment, and added my own annotation to it, and I can see how it was implemented by looking at annotations, and with the option-rdynamic and-G when compiling the code with GCC:

#include <stdio.h> #include <execinfo.h> void print_trace (void); void FUNCC () {/* print call stack to see who called this function/Print_trace ();} void Funcb () {FUNCC ();} void Funca () {FUNCB ();} int main (void) {Funca (); return 0;} void Print_trace (void) {int i; const int max_callstack_depth = 32;//The maximum depth of the print stack is required */void *TRACEB Ack[max_callstack_depth]; /* Used to store the address in the call stack * * * Use the addr2line command to print out the source code location of a function address * call format: addr2line-f-e/tmp/a.out 0x400618 * Before use, source compiles to add-rdy NAMIC-G option */char cmd[512] = "addr2line-f-e"; Char *prog = cmd + strlen (cmd); /* Get current executable program path and filename/int r = Readlink ("/proc/self/exe", prog,sizeof (CMD)-(Prog-cmd)-1); /* Popen will fork a subprocess to invoke the/bin/sh and execute the commands in the CMD string, * at the same time, a pipe will be created, because the parameter is ' W ', the pipe will be connected to the standard input, * and return a pointer fp of file pointing to the pipe created, As long as the FP to the management to write any content, * content will be sent to the standard input, * in the following code, the function address in the call stack is written to the pipe, * The Addr2line program obtains the function address from the standard input, and then prints out the source code location and function name according to the address. */FILE *FP = Popen (cmd, "w"); /* Get all the function addresses in the current call stack and put them in the traceback array/int depth = BackTrace (Traceback, max_callstack_depth); for (i = 0; i; Depth i++) {/* Gets the address of the function in the call stack and sends the address to Addr2line/fprintf (FP,%p/n, Traceback[i));/* The Addr2line command prints the source location of the function address to the label when it receives the address Quasi-Output/} fclose (FP); }

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.