backtracesyms.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Return list with names for address in backtrace.
  2. Copyright (C) 1998-2026 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. <https://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <execinfo.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ldsodefs.h>
  21. #if __ELF_NATIVE_CLASS == 32
  22. # define WORD_WIDTH 8
  23. #else
  24. /* We assume 64bits. */
  25. # define WORD_WIDTH 16
  26. #endif
  27. char **
  28. __backtrace_symbols (void *const *array, int size)
  29. {
  30. Dl_info info[size];
  31. int status[size];
  32. int cnt;
  33. size_t total = 0;
  34. char **result;
  35. /* Fill in the information we can get from `dladdr'. */
  36. for (cnt = 0; cnt < size; ++cnt)
  37. {
  38. struct link_map *map;
  39. status[cnt] = _dl_addr (array[cnt], &info[cnt], &map, NULL);
  40. if (status[cnt] && info[cnt].dli_fname && info[cnt].dli_fname[0] != '\0')
  41. {
  42. /* We have some info, compute the length of the string which will be
  43. "<file-name>(<sym-name>+offset) [address]. */
  44. total += (strlen (info[cnt].dli_fname ?: "")
  45. + strlen (info[cnt].dli_sname ?: "")
  46. + 3 + WORD_WIDTH + 3 + WORD_WIDTH + 5);
  47. /* The load bias is more useful to the user than the load
  48. address. The use of these addresses is to calculate an
  49. address in the ELF file, so its prelinked bias is not
  50. something we want to subtract out. */
  51. info[cnt].dli_fbase = (void *) map->l_addr;
  52. }
  53. else
  54. total += 5 + WORD_WIDTH;
  55. }
  56. /* Allocate memory for the result. */
  57. result = (char **) malloc (size * sizeof (char *) + total);
  58. if (result != NULL)
  59. {
  60. char *last = (char *) (result + size);
  61. for (cnt = 0; cnt < size; ++cnt)
  62. {
  63. result[cnt] = last;
  64. if (status[cnt]
  65. && info[cnt].dli_fname != NULL && info[cnt].dli_fname[0] != '\0')
  66. {
  67. if (info[cnt].dli_sname == NULL)
  68. /* We found no symbol name to use, so describe it as
  69. relative to the file. */
  70. info[cnt].dli_saddr = info[cnt].dli_fbase;
  71. if (info[cnt].dli_sname == NULL && info[cnt].dli_saddr == NULL)
  72. last += 1 + sprintf (last, "%s(%s) [%p]",
  73. info[cnt].dli_fname ?: "",
  74. info[cnt].dli_sname ?: "",
  75. array[cnt]);
  76. else
  77. {
  78. char sign;
  79. ptrdiff_t offset;
  80. if (array[cnt] >= (void *) info[cnt].dli_saddr)
  81. {
  82. sign = '+';
  83. offset = array[cnt] - info[cnt].dli_saddr;
  84. }
  85. else
  86. {
  87. sign = '-';
  88. offset = info[cnt].dli_saddr - array[cnt];
  89. }
  90. last += 1 + sprintf (last, "%s(%s%c%#tx) [%p]",
  91. info[cnt].dli_fname ?: "",
  92. info[cnt].dli_sname ?: "",
  93. sign, offset, array[cnt]);
  94. }
  95. }
  96. else
  97. last += 1 + sprintf (last, "[%p]", array[cnt]);
  98. }
  99. assert (last <= (char *) result + size * sizeof (char *) + total);
  100. }
  101. return result;
  102. }
  103. weak_alias (__backtrace_symbols, backtrace_symbols)