自己动手实现arm函数栈帧回溯【转】

时间:2023-03-09 01:21:36
自己动手实现arm函数栈帧回溯【转】

转自:http://blog.****.net/dragon101788/article/details/18668505

内核版本:2.6.14

glibc版本:2.3.6

CPU平台:arm

glic中其实有这些函数,当时用的uclib版本较低,没有这些函数,但又需要,只能自己实现了(较高的版本应该有这些函数,换版本很麻烦),而且可以加深自己对这方面的理解.原理性的东西就不深入讲解了,直接上例子!

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <assert.h>
  5. #include <ucontext.h>
  6. void A(int a);
  7. void B(int b);
  8. void C(int c);
  9. void DebugBacktrace(unsigned int sn , siginfo_t  *si , void *ptr);
  10. typedef struct
  11. {
  12. const char *dli_fname;  /* File name of defining object.  */
  13. void *dli_fbase;        /* Load address of that object.  */
  14. const char *dli_sname;  /* Name of nearest symbol.比如函数名*/
  15. void *dli_saddr;        /* Exact value of nearest symbol.比如函数的起始地址*/
  16. } Dl_info;
  17. struct ucontext_ce123 {
  18. unsigned long     uc_flags;
  19. struct ucontext  *uc_link;
  20. stack_t       uc_stack;
  21. struct sigcontext uc_mcontext;
  22. sigset_t      uc_sigmask;   /* mask last for extensibility */
  23. }ucontext_ce123_;
  24. struct sigframe_ce123 {
  25. struct sigcontext sc;//保存一组寄存器上下文
  26. unsigned long extramask[1];
  27. unsigned long retcode;//保存返回地址
  28. //struct aux_sigframe aux __attribute__((aligned(8)));
  29. }sigframe_ce123;
  30. int backtrace_ce123 (void **array, int size);
  31. char ** backtrace_symbols_ce123 (void *const *array, int size);
  32. int backtrace_ce123 (void **array, int size)
  33. {
  34. if (size <= 0)
  35. return 0;
  36. int *fp = 0, *next_fp = 0;
  37. int cnt = 0;
  38. int ret = 0;
  39. __asm__(
  40. "mov %0, fp\n"
  41. : "=r"(fp)
  42. );
  43. array[cnt++] = (void *)(*(fp-1));
  44. next_fp = (int *)(*(fp-3));
  45. while((cnt <= size) && (next_fp != 0))
  46. {
  47. array[cnt++] = (void *)*(next_fp - 1);
  48. next_fp = (int *)(*(next_fp-3));
  49. }
  50. ret = ((cnt <= size)?cnt:size);
  51. printf("Backstrace (%d deep)\n", ret);
  52. return ret;
  53. }
  54. char ** backtrace_symbols_ce123 (void *const *array, int size)
  55. {
  56. # define WORD_WIDTH 8
  57. Dl_info info[size];
  58. int status[size];
  59. int cnt;
  60. size_t total = 0;
  61. char **result;
  62. /* Fill in the information we can get from `dladdr'.  */
  63. for (cnt = 0; cnt < size; ++cnt)
  64. {
  65. status[cnt] = _dl_addr (array[cnt], &info[cnt]);
  66. if (status[cnt] && info[cnt].dli_fname && info[cnt].dli_fname[0] != '\0')
  67. /* We have some info, compute the length of the string which will be
  68. "<file-name>(<sym-name>) [+offset].  */
  69. total += (strlen (info[cnt].dli_fname ?: "")
  70. + (info[cnt].dli_sname ? strlen (info[cnt].dli_sname) + 3 + WORD_WIDTH + 3 : 1)
  71. + WORD_WIDTH + 5);
  72. else
  73. total += 5 + WORD_WIDTH;
  74. }
  75. /* Allocate memory for the result.  */
  76. result = (char **) malloc (size * sizeof (char *) + total);
  77. if (result != NULL)
  78. {
  79. char *last = (char *) (result + size);
  80. for (cnt = 0; cnt < size; ++cnt)
  81. {
  82. result[cnt] = last;
  83. if (status[cnt] && info[cnt].dli_fname && info[cnt].dli_fname[0] != '\0')
  84. {
  85. char buf[20];
  86. if (array[cnt] >= (void *) info[cnt].dli_saddr)
  87. sprintf (buf, "+%#lx", \
  88. (unsigned long)(array[cnt] - info[cnt].dli_saddr));
  89. else
  90. sprintf (buf, "-%#lx", \
  91. (unsigned long)(info[cnt].dli_saddr - array[cnt]));
  92. last += 1 + sprintf (last, "%s%s%s%s%s[%p]",
  93. info[cnt].dli_fname ?: "",
  94. info[cnt].dli_sname ? "(" : "",
  95. info[cnt].dli_sname ?: "",
  96. info[cnt].dli_sname ? buf : "",
  97. info[cnt].dli_sname ? ") " : " ",
  98. array[cnt]);
  99. }
  100. else
  101. last += 1 + sprintf (last, "[%p]", array[cnt]);
  102. }
  103. assert (last <= (char *) result + size * sizeof (char *) + total);
  104. }
  105. return result;
  106. }
  107. void A(int a)
  108. {
  109. printf("%d: A call B\n", a);
  110. B(2);
  111. }
  112. void B(int b)
  113. {
  114. printf("%d: B call C\n", b);
  115. C(3);       /* 这个函数调用将导致段错误*/
  116. }
  117. void C(int c)
  118. {
  119. char *p = (char *)c;
  120. *p = 'A';   /* 如果参数c不是一个可用的地址值,则这条语句导致段错误 */
  121. printf("%d: function C\n", c);
  122. }
  123. /* SIGSEGV信号的处理函数,回溯栈,打印函数的调用关系*/
  124. void DebugBacktrace(unsigned int sn , siginfo_t  *si , void *ptr)
  125. {
  126. /*int *ip = 0;
  127. __asm__(
  128. "mov %0, ip\n"
  129. : "=r"(ip)
  130. );
  131. printf("sp = 0x%x\n", ip);
  132. struct sigframe_ce123 * sigframe = (struct sigframe_ce123 * )ip;*/
  133. if(NULL != ptr)
  134. {
  135. printf("\n\nunhandled page fault (%d) at: 0x%08x\n", si->si_signo,si->si_addr);
  136. struct ucontext_ce123 *ucontext = (struct ucontext_ce123 *)ptr;
  137. int pc = ucontext->uc_mcontext.arm_pc;
  138. void *pc_array[1];
  139. pc_array[0] = pc;
  140. char **pc_name = backtrace_symbols_ce123(pc_array, 1);
  141. printf("%d: %s\n", 0, *pc_name);
  142. #define SIZE 100
  143. void *array[SIZE];
  144. int size, i;
  145. char **strings;
  146. size = backtrace_ce123(array, SIZE);
  147. strings = backtrace_symbols_ce123(array, size);
  148. for(i=0;i<size;i++)
  149. printf("%d: %s\n", i+1, strings[i]);
  150. free(strings);
  151. }
  152. else
  153. printf("error!\n");
  154. exit(-1);
  155. }
  156. int main(int argc, char **argv)
  157. {
  158. char a;
  159. struct sigaction s;
  160. s.sa_flags = SA_SIGINFO;
  161. s.sa_sigaction = (void *)DebugBacktrace;
  162. sigaction (SIGSEGV,&s,NULL);
  163. A(1);
  164. C(&a);
  165. return 0;
  166. }

代码的下载地址:http://download.****.net/detail/ce123/5063160

编译命令:arm-linux-gcc -rdynamic -o segfault segfault.c

_dl_addr链接不通过时,使用-ldl。如果没有该函数,可用dladdr函数代替。

自己动手实现arm函数栈帧回溯【转】