backtracesyms.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Return list with names for address in backtrace.
  2. Copyright (C) 1998,1999,2000,2001,2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If
  15. not, see <http://www.gnu.org/licenses/>.
  16. Based on glibc/sysdeps/generic/elf/backtracesyms.c
  17. Copyright (C) 2010 STMicroelectronics Ltd
  18. Author(s): Carmelo Amoroso <carmelo.amoroso@st.com>
  19. * Modified to work with uClibc
  20. - updated headers inclusion
  21. - updated formatting and style
  22. - updated to use dladdr from libdl */
  23. #include <execinfo.h>
  24. #include <assert.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <dlfcn.h>
  29. #include <link.h> /* required for __ELF_NATIVE_CLASS */
  30. #if __ELF_NATIVE_CLASS == 32
  31. # define WORD_WIDTH 8
  32. #else
  33. /* We assyme 64bits. */
  34. # define WORD_WIDTH 16
  35. #endif
  36. char ** backtrace_symbols (void *const *array, int size)
  37. {
  38. Dl_info info[size];
  39. int status[size];
  40. int cnt;
  41. size_t total = 0;
  42. char **result;
  43. /* Fill in the information we can get from `dladdr'. */
  44. for (cnt = 0; cnt < size; ++cnt) {
  45. status[cnt] = dladdr (array[cnt], &info[cnt]);
  46. if (status[cnt] && info[cnt].dli_fname &&
  47. info[cnt].dli_fname[0] != '\0')
  48. /*
  49. * We have some info, compute the length of the string which will be
  50. * "<file-name>(<sym-name>) [+offset].
  51. */
  52. total += (strlen (info[cnt].dli_fname ?: "") +
  53. (info[cnt].dli_sname ?
  54. strlen (info[cnt].dli_sname) + 3 + WORD_WIDTH + 3 : 1)
  55. + WORD_WIDTH + 5);
  56. else
  57. total += 5 + WORD_WIDTH;
  58. }
  59. /* Allocate memory for the result. */
  60. result = (char **) malloc (size * sizeof (char *) + total);
  61. if (result != NULL) {
  62. char *last = (char *) (result + size);
  63. for (cnt = 0; cnt < size; ++cnt) {
  64. result[cnt] = last;
  65. if (status[cnt] && info[cnt].dli_fname
  66. && info[cnt].dli_fname[0] != '\0') {
  67. char buf[20];
  68. if (array[cnt] >= (void *) info[cnt].dli_saddr)
  69. sprintf (buf, "+%#lx",
  70. (unsigned long)(array[cnt] - info[cnt].dli_saddr));
  71. else
  72. sprintf (buf, "-%#lx",
  73. (unsigned long)(info[cnt].dli_saddr - array[cnt]));
  74. last += 1 + sprintf (last, "%s%s%s%s%s[%p]",
  75. info[cnt].dli_fname ?: "",
  76. info[cnt].dli_sname ? "(" : "",
  77. info[cnt].dli_sname ?: "",
  78. info[cnt].dli_sname ? buf : "",
  79. info[cnt].dli_sname ? ") " : " ",
  80. array[cnt]);
  81. } else
  82. last += 1 + sprintf (last, "[%p]", array[cnt]);
  83. }
  84. assert (last <= (char *) result + size * sizeof (char *) + total);
  85. }
  86. return result;
  87. }