zoukankan      html  css  js  c++  java
  • linux下利用backtrace追踪函数调用堆栈以及定位段错误

    一般察看函数运行时堆栈的方法是使用GDB(bt命令)之类的外部调试器,但是,有些时候为了分析程序的BUG,(主要针对长时间运行程序的分析),在程序出错时打印出函数的调用堆栈是非常有用的。

    在glibc头文件"execinfo.h"中声明了三个函数用于获取当前线程的函数调用堆栈。

    [cpp] view plain copy
     
     print?
    1. int backtrace(void **buffer,int size)  

    该函数用于获取当前线程的调用堆栈,获取的信息将会被存放在buffer中,它是一个指针列表。参数 size 用来指定buffer中可以保存多少个void* 元素。函数返回值是实际获取的指针个数,最大不超过size大小

    在buffer中的指针实际是从堆栈中获取的返回地址,每一个堆栈框架有一个返回地址

    注意:某些编译器的优化选项对获取正确的调用堆栈有干扰,另外内联函数没有堆栈框架;删除框架指针也会导致无法正确解析堆栈内容

    [cpp] view plain copy
     
     print?
    1. char ** backtrace_symbols (void *const *buffer, int size)  

    backtrace_symbols将从backtrace函数获取的信息转化为一个字符串数组. 参数buffer应该是从backtrace函数获取的指针数组,size是该数组中的元素个数(backtrace的返回值)   
       
    函数返回值是一个指向字符串数组的指针,它的大小同buffer相同.每个字符串包含了一个相对于buffer中对应元素的可打印信息.它包括函数名,函数的偏移地址,和实际的返回地址

    现在,只有使用ELF二进制格式的程序才能获取函数名称和偏移地址.在其他系统,只有16进制的返回地址能被获取.另外,你可能需要传递相应的符号给链接器,以能支持函数名功能(比如,在使用GNU ld链接器的系统中,你需要传递(-rdynamic), -rdynamic可用来通知链接器将所有符号添加到动态符号表中,如果你的链接器支持-rdynamic的话,建议将其加上!)

    该函数的返回值是通过malloc函数申请的空间,因此调用者必须使用free函数来释放指针.

    注意:如果不能为字符串获取足够的空间函数的返回值将会为NULL

    [cpp] view plain copy
     
     print?
    1. void backtrace_symbols_fd (void *const *buffer, int size, int fd)  

    backtrace_symbols_fd与backtrace_symbols 函数具有相同的功能,不同的是它不会给调用者返回字符串数组,而是将结果写入文件描述符为fd的文件中,每个函数对应一行.它不需要调用malloc函数,因此适用于有可能调用该函数会失败的情况

    下面是glibc中的实例(稍有修改):

    [cpp] view plain copy
     
     print?
    1. #include <execinfo.h>  
    2. #include <stdio.h>  
    3. #include <stdlib.h>  
    4.   
    5. /* Obtain a backtrace and print it to @code{stdout}. */  
    6. void print_trace (void)  
    7. {  
    8.     void *array[10];  
    9.     size_t size;  
    10.     char **strings;  
    11.     size_t i;  
    12.    
    13.     size = backtrace (array, 10);  
    14.     strings = backtrace_symbols (array, size);  
    15.     if (NULL == strings)  
    16.     {  
    17.         perror("backtrace_synbols");  
    18.         Exit(EXIT_FAILURE);  
    19.     }  
    20.   
    21.     printf ("Obtained %zd stack frames. ", size);  
    22.   
    23.     for (i = 0; i < size; i++)  
    24.         printf ("%s ", strings[i]);  
    25.   
    26.     free (strings);  
    27.     strings = NULL;  
    28. }  
    29.   
    30. /* A dummy function to make the backtrace more interesting. */  
    31. void dummy_function (void)  
    32. {  
    33.     print_trace ();  
    34. }  
    35.   
    36. int main (int argc, char *argv[])  
    37. {  
    38.     dummy_function ();  
    39.     return 0;  
    40. }  

    输出如下:

    [cpp] view plain copy
     
     print?
    1. Obtained 4 stack frames.  
    2. ./execinfo() [0x80484dd]  
    3. ./execinfo() [0x8048549]  
    4. ./execinfo() [0x8048556]  
    5. /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0x70a113]  


     

    我们还可以利用这backtrace来定位段错误位置。

    通常情况系,程序发生段错误时系统会发送SIGSEGV信号给程序,缺省处理是退出函数。我们可以使用 signal(SIGSEGV, &your_function);函数来接管SIGSEGV信号的处理,程序在发生段错误后,自动调用我们准备好的函数,从而在那个函数里来获取当前函数调用栈。

    举例如下:

    [cpp] view plain copy
     
     print?
    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <stddef.h>  
    4. #include <execinfo.h>  
    5. #include <signal.h>  
    6.   
    7. void dump(int signo)  
    8. {  
    9.     void *buffer[30] = {0};  
    10.     size_t size;  
    11.     char **strings = NULL;  
    12.     size_t i = 0;  
    13.   
    14.     size = backtrace(buffer, 30);  
    15.     fprintf(stdout, "Obtained %zd stack frames.nm ", size);  
    16.     strings = backtrace_symbols(buffer, size);  
    17.     if (strings == NULL)  
    18.     {  
    19.         perror("backtrace_symbols.");  
    20.         exit(EXIT_FAILURE);  
    21.     }  
    22.       
    23.     for (i = 0; i < size; i++)  
    24.     {  
    25.         fprintf(stdout, "%s ", strings[i]);  
    26.     }  
    27.     free(strings);  
    28.     strings = NULL;  
    29.     exit(0);  
    30. }  
    31.   
    32. void func_c()  
    33. {  
    34.     *((volatile char *)0x0) = 0x9999;  
    35. }  
    36.   
    37. void func_b()  
    38. {  
    39.     func_c();  
    40. }  
    41.   
    42. void func_a()  
    43. {  
    44.     func_b();  
    45. }  
    46.   
    47. int main(int argc, const char *argv[])  
    48. {  
    49.     if (signal(SIGSEGV, dump) == SIG_ERR)  
    50.         perror("can't catch SIGSEGV");  
    51.     func_a();  
    52.     return 0;  
    53. }  


     

    编译程序:

    gcc -g -rdynamic test.c -o test; ./test

    输出如下:

    [cpp] view plain copy
     
     print?
    1. Obtained6stackframes.nm  
    2. ./backstrace_debug(dump+0x45)[0x80487c9]  
    3. [0x468400]  
    4. ./backstrace_debug(func_b+0x8)[0x804888c]  
    5. ./backstrace_debug(func_a+0x8)[0x8048896]  
    6. ./backstrace_debug(main+0x33)[0x80488cb]  
    7. /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x129113]  

     (这里有个疑问: 多次运行的结果是/lib/i368-linux-gnu/libc.so.6和[0x468400]的返回地址是变化的,但不变的是后三位, 不知道为什么)

    接着:

    objdump -d test > test.s

    在test.s中搜索804888c如下:

     

    [cpp] view plain copy
     
     print?
    1. 8048884 <func_b>:  
    2. 8048884:    55              push %ebp  
    3. 8048885:    89 e5            mov %esp, %ebp  
    4. 8048887:    e8 eb ff ff ff       call 8048877 <func_c>  
    5. 804888c:    5d                pop %ebp  
    6. 804888d:    c3                ret  

    其中80488c时调用(call 8048877)C函数后的地址,虽然并没有直接定位到C函数,通过汇编代码, 基本可以推出是C函数出问题了(pop指令不会导致段错误的)。

    我们也可以通过addr2line来查看

    [cpp] view plain copy
     
     print?
    1. addr2line 0x804888c -e backstrace_debug -f  

    输出:

    [cpp] view plain copy
     
     print?
    1. func_b  
    2. /home/astrol/c/backstrace_debug.c:57  


     

    以下是简单的backtrace原理实现:

     

    [cpp] view plain copy
     
     print?
    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.   
    5. #define LEN 4  
    6. #define FILENAME "stack"  
    7.   
    8. int backtrace(void **buffer, int size)  
    9. {  
    10.     int i = 0;  
    11.     unsigned long int reg_eip = 0;  
    12.     unsigned long int reg_ebp = 0;  
    13.     char cmd[size][64];  
    14.   
    15.     memset(cmd, 0, size * 64);  
    16.     __asm__ volatile (  
    17.         /* get current EBP */  
    18.         "movl %%ebp, %0  "  
    19.         :"=r"(reg_ebp)  /* output register */  
    20.         :       /* input  register */  
    21.         :"memory"   /* cloberred register */  
    22.     );    
    23.   
    24.     for (i = 0; i < size; i++)  
    25.     {  
    26.         reg_eip = *(unsigned long int *)(reg_ebp + 4);  
    27.         reg_ebp = *(unsigned long int *)(reg_ebp);  
    28.         buffer[i] = (void *)reg_eip;  
    29.         fprintf(stderr, "%p -> ", buffer[i]);  
    30.         sprintf(cmd[i], "addr2line %p -e ", buffer[i]);  
    31.         strncat(cmd[i], FILENAME" -f", strlen(FILENAME)+3);  
    32.         system(cmd[i]);  
    33.         puts("");         
    34.     }  
    35.   
    36.     return size;  
    37. }  
    38.   
    39. static void test2(void)  
    40. {  
    41.     int i = 0;  
    42.     void *buffer[LEN] = {0};  
    43.     backtrace(buffer, LEN);  
    44.     return;  
    45. }  
    46.   
    47. static void test1(void)  
    48. {  
    49.     test2();  
    50. }  
    51.   
    52. static void test(void)  
    53. {  
    54.     test1();  
    55. }  
    56.   
    57. int main(int argc, const char *argv[])  
    58. {  
    59.     test();  
    60.     return 0;  
    61. }  



     

    参考链接:http://blog.csdn.net/qqwx_1986/article/details/5942863

  • 相关阅读:
    安卓开发遇到的报错信息
    工作记录
    答辩系统问题
    DWR
    前端vue 里的tab切换 减少dom操作
    前端拖动div 效果
    vue 点击按钮弹窗,点击关闭按钮关闭弹窗。
    前段开发 jq ajax数据处理详细讲解。
    vue计算属性computed和methods的区别
    前段开发 react native tab功能
  • 原文地址:https://www.cnblogs.com/lidabo/p/5344768.html
Copyright © 2011-2022 走看看