Monday, June 3, 2013

dump function call trace

dump function call trace: You can get the function call graph, some of the time for tracking bug, understand the program structure is of particular importance.

Kernel function call trace
The kernel provides dump_stack () function, use this function, you can print out the current function call path.
dump_stack is an architecture-related functions, for the arm platform, implementation of this function position arch/arm/kernel/traps.c

Application layer function call trace
glibc provides a backtrace and backtrace_symbols function, the application layer can use their print function calls
backtrace, backtrace_symbols examples of the use
  1. # Include <stdio.h>  
  2. # Include <stdlib.h>  
  3. # Include <execinfo.h>  
  4.   
  5. void  do_backtrace ()  
  6. {  
  7. # Define BACKTRACE_SIZ 100  
  8.     void  * array [BACKTRACE_SIZ];  
  9.     int  size, i;  
  10.     char  ** strings;  
  11.   
  12.     size = backtrace (array, BACKTRACE_SIZ);  
  13.     strings = backtrace_symbols (array, size);  
  14.   
  15.     for  (i = 0; i <size; + + i) {  
  16.         printf ( "% P:% s \ N" , array [i], strings [i]);  
  17.     }  
  18.   
  19.     printf ( "----------------------------------------------- ---------- \ N " );  
  20.     free (strings);  
  21. }  
  22.   
  23. int  foo ()  
  24. {  
  25.     do_backtrace ();  
  26. }  
  27.   
  28. int  Bar ( void )  
  29. {  
  30.     foo ();  
  31.     return  0;  
  32. }  
  33.   
  34. int  Boo ( void )  
  35. {  
  36.     bar ();  
  37.     return  0;  
  38. }  
  39.   
  40. int  baz ( void )  
  41. {  
  42.     boo ();  
  43.     return  0;  
  44. }  
  45.   
  46. int  main ( void )  
  47. {  
  48.     baz ();  
  49.     return  0;  
  50. }  

No comments:

Post a Comment