backtrace.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2013, Digium, Inc.
  5. *
  6. * Matt Jordan <mjordan@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Asterisk backtrace generation
  20. *
  21. * This file provides backtrace generation utilities
  22. */
  23. /*** MODULEINFO
  24. <support_level>core</support_level>
  25. ***/
  26. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
  28. #include "asterisk/backtrace.h"
  29. #include "asterisk/utils.h"
  30. #include "asterisk/strings.h"
  31. #ifdef HAVE_BKTR
  32. #include <execinfo.h>
  33. #if defined(HAVE_DLADDR) && defined(HAVE_BFD) && defined(BETTER_BACKTRACES)
  34. #include <dlfcn.h>
  35. #include <bfd.h>
  36. #endif
  37. struct ast_bt *__ast_bt_create(void)
  38. {
  39. struct ast_bt *bt = ast_std_calloc(1, sizeof(*bt));
  40. if (!bt) {
  41. return NULL;
  42. }
  43. bt->alloced = 1;
  44. ast_bt_get_addresses(bt);
  45. return bt;
  46. }
  47. int __ast_bt_get_addresses(struct ast_bt *bt)
  48. {
  49. bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
  50. return 0;
  51. }
  52. void *__ast_bt_destroy(struct ast_bt *bt)
  53. {
  54. if (bt && bt->alloced) {
  55. ast_std_free(bt);
  56. }
  57. return NULL;
  58. }
  59. char **__ast_bt_get_symbols(void **addresses, size_t num_frames)
  60. {
  61. char **strings;
  62. #if defined(BETTER_BACKTRACES)
  63. int stackfr;
  64. bfd *bfdobj; /* bfd.h */
  65. Dl_info dli; /* dlfcn.h */
  66. long allocsize;
  67. asymbol **syms = NULL; /* bfd.h */
  68. bfd_vma offset; /* bfd.h */
  69. const char *lastslash;
  70. asection *section;
  71. const char *file, *func;
  72. unsigned int line;
  73. char address_str[128];
  74. char msg[1024];
  75. size_t strings_size;
  76. size_t *eachlen;
  77. #endif
  78. #if defined(BETTER_BACKTRACES)
  79. strings_size = num_frames * sizeof(*strings);
  80. eachlen = ast_std_calloc(num_frames, sizeof(*eachlen));
  81. strings = ast_std_calloc(num_frames, sizeof(*strings));
  82. if (!eachlen || !strings) {
  83. ast_std_free(eachlen);
  84. ast_std_free(strings);
  85. return NULL;
  86. }
  87. for (stackfr = 0; stackfr < num_frames; stackfr++) {
  88. int found = 0, symbolcount;
  89. msg[0] = '\0';
  90. if (!dladdr(addresses[stackfr], &dli)) {
  91. continue;
  92. }
  93. if (strcmp(dli.dli_fname, "asterisk") == 0) {
  94. char asteriskpath[256];
  95. if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {
  96. /* This will fail to find symbols */
  97. dli.dli_fname = "asterisk";
  98. }
  99. }
  100. lastslash = strrchr(dli.dli_fname, '/');
  101. if ((bfdobj = bfd_openr(dli.dli_fname, NULL)) &&
  102. bfd_check_format(bfdobj, bfd_object) &&
  103. (allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&
  104. (syms = ast_std_malloc(allocsize)) &&
  105. (symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {
  106. if (bfdobj->flags & DYNAMIC) {
  107. offset = addresses[stackfr] - dli.dli_fbase;
  108. } else {
  109. offset = addresses[stackfr] - (void *) 0;
  110. }
  111. for (section = bfdobj->sections; section; section = section->next) {
  112. if (!bfd_get_section_flags(bfdobj, section) & SEC_ALLOC ||
  113. section->vma > offset ||
  114. section->size + section->vma < offset) {
  115. continue;
  116. }
  117. if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {
  118. continue;
  119. }
  120. /* file can possibly be null even with a success result from bfd_find_nearest_line */
  121. file = file ? file : "";
  122. /* Stack trace output */
  123. found++;
  124. if ((lastslash = strrchr(file, '/'))) {
  125. const char *prevslash;
  126. for (prevslash = lastslash - 1; *prevslash != '/' && prevslash >= file; prevslash--) {
  127. }
  128. if (prevslash >= file) {
  129. lastslash = prevslash;
  130. }
  131. }
  132. if (dli.dli_saddr == NULL) {
  133. address_str[0] = '\0';
  134. } else {
  135. snprintf(address_str, sizeof(address_str), " (%p+%lX)",
  136. dli.dli_saddr,
  137. (unsigned long) (addresses[stackfr] - dli.dli_saddr));
  138. }
  139. snprintf(msg, sizeof(msg), "%s:%u %s()%s",
  140. lastslash ? lastslash + 1 : file, line,
  141. S_OR(func, "???"),
  142. address_str);
  143. break; /* out of section iteration */
  144. }
  145. }
  146. if (bfdobj) {
  147. bfd_close(bfdobj);
  148. ast_std_free(syms);
  149. }
  150. /* Default output, if we cannot find the information within BFD */
  151. if (!found) {
  152. if (dli.dli_saddr == NULL) {
  153. address_str[0] = '\0';
  154. } else {
  155. snprintf(address_str, sizeof(address_str), " (%p+%lX)",
  156. dli.dli_saddr,
  157. (unsigned long) (addresses[stackfr] - dli.dli_saddr));
  158. }
  159. snprintf(msg, sizeof(msg), "%s %s()%s",
  160. lastslash ? lastslash + 1 : dli.dli_fname,
  161. S_OR(dli.dli_sname, "<unknown>"),
  162. address_str);
  163. }
  164. if (!ast_strlen_zero(msg)) {
  165. char **tmp;
  166. eachlen[stackfr] = strlen(msg) + 1;
  167. if (!(tmp = ast_std_realloc(strings, strings_size + eachlen[stackfr]))) {
  168. ast_std_free(strings);
  169. strings = NULL;
  170. break; /* out of stack frame iteration */
  171. }
  172. strings = tmp;
  173. strings[stackfr] = (char *) strings + strings_size;
  174. strcpy(strings[stackfr], msg);/* Safe since we just allocated the room. */
  175. strings_size += eachlen[stackfr];
  176. }
  177. }
  178. if (strings) {
  179. /* Recalculate the offset pointers because of the reallocs. */
  180. strings[0] = (char *) strings + num_frames * sizeof(*strings);
  181. for (stackfr = 1; stackfr < num_frames; stackfr++) {
  182. strings[stackfr] = strings[stackfr - 1] + eachlen[stackfr - 1];
  183. }
  184. }
  185. ast_std_free(eachlen);
  186. #else /* !defined(BETTER_BACKTRACES) */
  187. strings = backtrace_symbols(addresses, num_frames);
  188. #endif /* defined(BETTER_BACKTRACES) */
  189. return strings;
  190. }
  191. #endif /* HAVE_BKTR */