backtracesymsfd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Write formatted list with names for addresses in backtrace to a file.
  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 <execinfo.h>
  16. #include <string.h>
  17. #include <sys/uio.h>
  18. #include <_itoa.h>
  19. #include <ldsodefs.h>
  20. #if __ELF_NATIVE_CLASS == 32
  21. # define WORD_WIDTH 8
  22. #else
  23. /* We assume 64bits. */
  24. # define WORD_WIDTH 16
  25. #endif
  26. void
  27. __backtrace_symbols_fd (void *const *array, int size, int fd)
  28. {
  29. struct iovec iov[9];
  30. int cnt;
  31. for (cnt = 0; cnt < size; ++cnt)
  32. {
  33. char buf[WORD_WIDTH];
  34. char buf2[WORD_WIDTH];
  35. Dl_info info;
  36. struct link_map *map;
  37. size_t last = 0;
  38. if (_dl_addr (array[cnt], &info, &map, NULL)
  39. && info.dli_fname != NULL && info.dli_fname[0] != '\0')
  40. {
  41. /* Name of the file. */
  42. iov[0].iov_base = (void *) info.dli_fname;
  43. iov[0].iov_len = strlen (info.dli_fname);
  44. last = 1;
  45. if (info.dli_sname != NULL || map->l_addr != 0)
  46. {
  47. size_t diff;
  48. iov[last].iov_base = (void *) "(";
  49. iov[last].iov_len = 1;
  50. ++last;
  51. if (info.dli_sname != NULL)
  52. {
  53. /* We have a symbol name. */
  54. iov[last].iov_base = (void *) info.dli_sname;
  55. iov[last].iov_len = strlen (info.dli_sname);
  56. ++last;
  57. }
  58. else
  59. /* We have no symbol, so describe it as relative to the file.
  60. The load bias is more useful to the user than the load
  61. address. The use of these addresses is to calculate an
  62. address in the ELF file, so its prelinked bias is not
  63. something we want to subtract out. */
  64. info.dli_saddr = (void *) map->l_addr;
  65. if (array[cnt] >= (void *) info.dli_saddr)
  66. {
  67. iov[last].iov_base = (void *) "+0x";
  68. diff = array[cnt] - info.dli_saddr;
  69. }
  70. else
  71. {
  72. iov[last].iov_base = (void *) "-0x";
  73. diff = info.dli_saddr - array[cnt];
  74. }
  75. iov[last].iov_len = 3;
  76. ++last;
  77. iov[last].iov_base = _itoa_word ((unsigned long int) diff,
  78. &buf2[WORD_WIDTH], 16, 0);
  79. iov[last].iov_len = (&buf2[WORD_WIDTH]
  80. - (char *) iov[last].iov_base);
  81. ++last;
  82. iov[last].iov_base = (void *) ") ";
  83. iov[last].iov_len = 2;
  84. ++last;
  85. }
  86. else
  87. {
  88. iov[last].iov_base = (void *) "() ";
  89. iov[last].iov_len = 3;
  90. ++last;
  91. }
  92. }
  93. iov[last].iov_base = (void *) "[0x";
  94. iov[last].iov_len = 3;
  95. ++last;
  96. iov[last].iov_base = _itoa_word ((unsigned long int) array[cnt],
  97. &buf[WORD_WIDTH], 16, 0);
  98. iov[last].iov_len = &buf[WORD_WIDTH] - (char *) iov[last].iov_base;
  99. ++last;
  100. iov[last].iov_base = (void *) "]\n";
  101. iov[last].iov_len = 2;
  102. ++last;
  103. __writev (fd, iov, last);
  104. }
  105. }
  106. weak_alias (__backtrace_symbols_fd, backtrace_symbols_fd)
  107. libc_hidden_def (__backtrace_symbols_fd)