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.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Based on glibc/sysdeps/generic/elf/backtracesymsfd.c
23 Copyright (C) 2010 STMicroelectronics Ltd
24 Author(s): Carmelo Amoroso <carmelo.amoroso@st.com>
25 * Modified to work with uClibc
26 - updated headers inclusion
27 - updated formatting and style
28 - updated to use dladdr from libdl
29 - updated to use snprintf instead of _itoa_word */
36 #include <link.h> /* required for __ELF_NATIVE_CLASS */
38 #if __ELF_NATIVE_CLASS == 32
41 /* We assyme 64bits. */
42 # define WORD_WIDTH 16
45 #define BUF_SIZE (WORD_WIDTH + 1)
47 void backtrace_symbols_fd (void *const *array, int size, int fd)
52 for (cnt = 0; cnt < size; ++cnt) {
58 memset(buf, 0, sizeof(buf));
59 if (dladdr (array[cnt], &info)
60 && info.dli_fname && info.dli_fname[0] != '\0') {
61 /* Name of the file. */
62 iov[0].iov_base = (void *) info.dli_fname;
63 iov[0].iov_len = strlen (info.dli_fname);
67 if (info.dli_sname != NULL) {
69 memset(buf2, 0, sizeof(buf2));
72 iov[1].iov_base = (void *) "(";
74 iov[2].iov_base = (void *) info.dli_sname;
75 iov[2].iov_len = strlen (info.dli_sname);
77 if (array[cnt] >= (void *) info.dli_saddr) {
78 iov[3].iov_base = (void *) "+0x";
79 diff = array[cnt] - info.dli_saddr;
81 iov[3].iov_base = (void *) "-0x";
82 diff = info.dli_saddr - array[cnt];
87 /* convert diff to a string in hex format */
88 len = snprintf(buf2, sizeof(buf2), "%lx", (unsigned long) diff);
89 iov[4].iov_base = buf2;
92 iov[5].iov_base = (void *) ")";
99 iov[last].iov_base = (void *) "[0x";
100 iov[last].iov_len = 3;
103 /* convert array[cnt] to a string in hex format */
104 len = snprintf(buf, sizeof(buf), "%lx", (unsigned long) array[cnt]);
105 iov[last].iov_base = buf;
106 iov[last].iov_len = len;
110 iov[last].iov_base = (void *) "]\n";
111 iov[last].iov_len = 2;
114 writev (fd, iov, last);