backtracesyms.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA.
  17. Based on glibc/sysdeps/generic/elf/backtracesyms.c
  18. Copyright (C) 2010 STMicroelectronics Ltd
  19. Author(s): Carmelo Amoroso <carmelo.amoroso@st.com>
  20. * Modified to work with uClibc
  21. - updated headers inclusion
  22. - updated formatting and style
  23. - updated to use dladdr from libdl */
  24. #include <execinfo.h>
  25. #include <assert.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <dlfcn.h>
  30. #include <link.h> /* required for __ELF_NATIVE_CLASS */
  31. #if __ELF_NATIVE_CLASS == 32
  32. # define WORD_WIDTH 8
  33. #else
  34. /* We assyme 64bits. */
  35. # define WORD_WIDTH 16
  36. #endif
  37. char ** backtrace_symbols (void *const *array, int size)
  38. {
  39. Dl_info info[size];
  40. int status[size];
  41. int cnt;
  42. size_t total = 0;
  43. char **result;
  44. /* Fill in the information we can get from `dladdr'. */
  45. for (cnt = 0; cnt < size; ++cnt) {
  46. status[cnt] = dladdr (array[cnt], &info[cnt]);
  47. if (status[cnt] && info[cnt].dli_fname &&
  48. info[cnt].dli_fname[0] != '\0')
  49. /*
  50. * We have some info, compute the length of the string which will be
  51. * "<file-name>(<sym-name>) [+offset].
  52. */
  53. total += (strlen (info[cnt].dli_fname ?: "") +
  54. (info[cnt].dli_sname ?
  55. strlen (info[cnt].dli_sname) + 3 + WORD_WIDTH + 3 : 1)
  56. + WORD_WIDTH + 5);
  57. else
  58. total += 5 + WORD_WIDTH;
  59. }
  60. /* Allocate memory for the result. */
  61. result = (char **) malloc (size * sizeof (char *) + total);
  62. if (result != NULL) {
  63. char *last = (char *) (result + size);
  64. for (cnt = 0; cnt < size; ++cnt) {
  65. result[cnt] = last;
  66. if (status[cnt] && info[cnt].dli_fname
  67. && info[cnt].dli_fname[0] != '\0') {
  68. char buf[20];
  69. if (array[cnt] >= (void *) info[cnt].dli_saddr)
  70. sprintf (buf, "+%#lx",
  71. (unsigned long)(array[cnt] - info[cnt].dli_saddr));
  72. else
  73. sprintf (buf, "-%#lx",
  74. (unsigned long)(info[cnt].dli_saddr - array[cnt]));
  75. last += 1 + sprintf (last, "%s%s%s%s%s[%p]",
  76. info[cnt].dli_fname ?: "",
  77. info[cnt].dli_sname ? "(" : "",
  78. info[cnt].dli_sname ?: "",
  79. info[cnt].dli_sname ? buf : "",
  80. info[cnt].dli_sname ? ") " : " ",
  81. array[cnt]);
  82. } else
  83. last += 1 + sprintf (last, "[%p]", array[cnt]);
  84. }
  85. assert (last <= (char *) result + size * sizeof (char *) + total);
  86. }
  87. return result;
  88. }