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