backtrace.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Return backtrace of current program state.
  2. Copyright (C) 2013-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <libc-lock.h>
  16. #include <dlfcn.h>
  17. #include <execinfo.h>
  18. #include <stdlib.h>
  19. #include <unwind.h>
  20. struct trace_arg
  21. {
  22. void **array;
  23. int cnt, size;
  24. void *lastfp, *lastsp;
  25. };
  26. #ifdef SHARED
  27. static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
  28. static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
  29. static _Unwind_Ptr (*unwind_getcfa) (struct _Unwind_Context *);
  30. static _Unwind_Ptr (*unwind_getgr) (struct _Unwind_Context *, int);
  31. static void *libgcc_handle;
  32. static void
  33. init (void)
  34. {
  35. libgcc_handle = __libc_dlopen ("libgcc_s.so.2");
  36. if (libgcc_handle == NULL)
  37. return;
  38. unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
  39. unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
  40. unwind_getcfa = __libc_dlsym (libgcc_handle, "_Unwind_GetCFA");
  41. unwind_getgr = __libc_dlsym (libgcc_handle, "_Unwind_GetGR");
  42. if (unwind_getip == NULL || unwind_getgr == NULL || unwind_getcfa == NULL)
  43. {
  44. unwind_backtrace = NULL;
  45. __libc_dlclose (libgcc_handle);
  46. libgcc_handle = NULL;
  47. }
  48. }
  49. #else
  50. # define unwind_backtrace _Unwind_Backtrace
  51. # define unwind_getip _Unwind_GetIP
  52. # define unwind_getcfa _Unwind_GetCFA
  53. # define unwind_getgr _Unwind_GetGR
  54. #endif
  55. static _Unwind_Reason_Code
  56. backtrace_helper (struct _Unwind_Context *ctx, void *a)
  57. {
  58. struct trace_arg *arg = a;
  59. /* We are first called with address in the __backtrace function.
  60. Skip it. */
  61. if (arg->cnt != -1)
  62. arg->array[arg->cnt] = (void *) unwind_getip (ctx);
  63. if (++arg->cnt == arg->size)
  64. return _URC_END_OF_STACK;
  65. /* %fp is DWARF2 register 14 on M68K. */
  66. arg->lastfp = (void *) unwind_getgr (ctx, 14);
  67. arg->lastsp = (void *) unwind_getcfa (ctx);
  68. return _URC_NO_REASON;
  69. }
  70. /* This is a global variable set at program start time. It marks the
  71. highest used stack address. */
  72. extern void *__libc_stack_end;
  73. /* This is the stack layout we see with every stack frame
  74. if not compiled without frame pointer.
  75. +-----------------+ +-----------------+
  76. %fp -> | %fp last frame--------> | %fp last frame--->...
  77. | | | |
  78. | return address | | return address |
  79. +-----------------+ +-----------------+
  80. First try as far to get as far as possible using
  81. _Unwind_Backtrace which handles -fomit-frame-pointer
  82. as well, but requires .eh_frame info. Then fall back to
  83. walking the stack manually. */
  84. struct layout
  85. {
  86. struct layout *fp;
  87. void *ret;
  88. };
  89. int
  90. __backtrace (void **array, int size)
  91. {
  92. struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
  93. if (size <= 0)
  94. return 0;
  95. #ifdef SHARED
  96. __libc_once_define (static, once);
  97. __libc_once (once, init);
  98. if (unwind_backtrace == NULL)
  99. return 0;
  100. #endif
  101. unwind_backtrace (backtrace_helper, &arg);
  102. if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
  103. --arg.cnt;
  104. else if (arg.cnt < size)
  105. {
  106. struct layout *fp = (struct layout *) arg.lastfp;
  107. while (arg.cnt < size)
  108. {
  109. /* Check for out of range. */
  110. if ((void *) fp < arg.lastsp || (void *) fp > __libc_stack_end
  111. || ((long) fp & 1))
  112. break;
  113. array[arg.cnt++] = fp->ret;
  114. fp = fp->fp;
  115. }
  116. }
  117. return arg.cnt != -1 ? arg.cnt : 0;
  118. }
  119. weak_alias (__backtrace, backtrace)
  120. libc_hidden_def (__backtrace)
  121. #ifdef SHARED
  122. /* Free all resources if necessary. */
  123. libc_freeres_fn (free_mem)
  124. {
  125. unwind_backtrace = NULL;
  126. if (libgcc_handle != NULL)
  127. {
  128. __libc_dlclose (libgcc_handle);
  129. libgcc_handle = NULL;
  130. }
  131. }
  132. #endif