backtracesymsfd.c 3.4 KB

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