backtracesymsfd.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Write formatted list with names for addresses in backtrace to a file.
  2. Copyright (C) 1998, 2000, 2003, 2005 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/backtracesymsfd.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. - updated to use snprintf instead of _itoa_word */
  24. #include <execinfo.h>
  25. #include <string.h>
  26. #include <sys/uio.h>
  27. #include <dlfcn.h>
  28. #include <stdio.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. #define BUF_SIZE (WORD_WIDTH + 1)
  37. void backtrace_symbols_fd (void *const *array, int size, int fd)
  38. {
  39. struct iovec iov[9];
  40. int cnt;
  41. for (cnt = 0; cnt < size; ++cnt) {
  42. char buf[BUF_SIZE];
  43. Dl_info info;
  44. size_t last = 0;
  45. size_t len = 0;
  46. memset(buf, 0, sizeof(buf));
  47. if (dladdr (array[cnt], &info)
  48. && info.dli_fname && info.dli_fname[0] != '\0') {
  49. /* Name of the file. */
  50. iov[0].iov_base = (void *) info.dli_fname;
  51. iov[0].iov_len = strlen (info.dli_fname);
  52. last = 1;
  53. /* Symbol name. */
  54. if (info.dli_sname != NULL) {
  55. char buf2[BUF_SIZE];
  56. memset(buf2, 0, sizeof(buf2));
  57. size_t diff;
  58. iov[1].iov_base = (void *) "(";
  59. iov[1].iov_len = 1;
  60. iov[2].iov_base = (void *) info.dli_sname;
  61. iov[2].iov_len = strlen (info.dli_sname);
  62. if (array[cnt] >= (void *) info.dli_saddr) {
  63. iov[3].iov_base = (void *) "+0x";
  64. diff = array[cnt] - info.dli_saddr;
  65. } else {
  66. iov[3].iov_base = (void *) "-0x";
  67. diff = info.dli_saddr - array[cnt];
  68. }
  69. iov[3].iov_len = 3;
  70. /* convert diff to a string in hex format */
  71. len = snprintf(buf2, sizeof(buf2), "%lx", (unsigned long) diff);
  72. iov[4].iov_base = buf2;
  73. iov[4].iov_len = len;
  74. iov[5].iov_base = (void *) ")";
  75. iov[5].iov_len = 1;
  76. last = 6;
  77. }
  78. }
  79. iov[last].iov_base = (void *) "[0x";
  80. iov[last].iov_len = 3;
  81. ++last;
  82. /* convert array[cnt] to a string in hex format */
  83. len = snprintf(buf, sizeof(buf), "%lx", (unsigned long) array[cnt]);
  84. iov[last].iov_base = buf;
  85. iov[last].iov_len = len;
  86. ++last;
  87. iov[last].iov_base = (void *) "]\n";
  88. iov[last].iov_len = 2;
  89. ++last;
  90. writev (fd, iov, last);
  91. }
  92. }