嵌入式 linux下利用backtrace追踪函数调用堆栈以及定位段错误

时间:2023-03-08 17:59:26
2015-05-27 14:19 184人阅读 评论(0) 收藏 举报
嵌入式 linux下利用backtrace追踪函数调用堆栈以及定位段错误 分类:
嵌入式(928) 嵌入式 linux下利用backtrace追踪函数调用堆栈以及定位段错误

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

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

  1. int backtrace(void **buffer,int size)
  1. <span style="font-size:12px;">int backtrace(void **buffer,int size)</span>

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

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

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

  1. char ** backtrace_symbols (void *const *buffer, int size)
  1. <span style="font-size:12px;">char ** backtrace_symbols (void *const *buffer, int size)</span>

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

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

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

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

  1. void backtrace_symbols_fd (void *const *buffer, int size, int fd)
  1. <span style="font-size:12px;">void backtrace_symbols_fd (void *const *buffer, int size, int fd)</span>

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

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

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

输出如下:

  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]
  1. <span style="font-size:12px;">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]
  6. </span>

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

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

举例如下:

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

编译程序:

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

输出如下:

  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]
  1. <span style="font-size:12px;">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]</span>

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

接着:

objdump -d test > test.s

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

  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
  1. <span style="font-size:12px;">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</span>

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

我们也可以通过addr2line来查看

  1. addr2line 0x804888c -e backstrace_debug -f
  1. <span style="font-size:12px;">addr2line 0x804888c -e backstrace_debug -f</span>

输出:

  1. func_b
  2. /home/astrol/c/backstrace_debug.c:57
  1. <span style="font-size:12px;">func_b
  2. /home/astrol/c/backstrace_debug.c:57
  3. </span>

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

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