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; see the file COPYING.LIB. If
18 not, see <http://www.gnu.org/licenses/>.
20 Based on glibc/sysdeps/generic/elf/backtracesymsfd.c
22 Copyright (C) 2010 STMicroelectronics Ltd
23 Author(s): Carmelo Amoroso <carmelo.amoroso@st.com>
24 * Modified to work with uClibc
25 - updated headers inclusion
26 - updated formatting and style
27 - updated to use dladdr from libdl
28 - updated to use snprintf instead of _itoa_word */
35 #include <link.h> /* required for __ELF_NATIVE_CLASS */
37 #if __ELF_NATIVE_CLASS == 32
40 /* We assyme 64bits. */
41 # define WORD_WIDTH 16
44 #define BUF_SIZE (WORD_WIDTH + 1)
46 void backtrace_symbols_fd (void *const *array
, int size
, int fd
)
51 for (cnt
= 0; cnt
< size
; ++cnt
) {
57 memset(buf
, 0, sizeof(buf
));
58 if (dladdr (array
[cnt
], &info
)
59 && info
.dli_fname
&& info
.dli_fname
[0] != '\0') {
60 /* Name of the file. */
61 iov
[0].iov_base
= (void *) info
.dli_fname
;
62 iov
[0].iov_len
= strlen (info
.dli_fname
);
66 if (info
.dli_sname
!= NULL
) {
68 memset(buf2
, 0, sizeof(buf2
));
71 iov
[1].iov_base
= (void *) "(";
73 iov
[2].iov_base
= (void *) info
.dli_sname
;
74 iov
[2].iov_len
= strlen (info
.dli_sname
);
76 if (array
[cnt
] >= (void *) info
.dli_saddr
) {
77 iov
[3].iov_base
= (void *) "+0x";
78 diff
= array
[cnt
] - info
.dli_saddr
;
80 iov
[3].iov_base
= (void *) "-0x";
81 diff
= info
.dli_saddr
- array
[cnt
];
86 /* convert diff to a string in hex format */
87 len
= snprintf(buf2
, sizeof(buf2
), "%lx", (unsigned long) diff
);
88 iov
[4].iov_base
= buf2
;
91 iov
[5].iov_base
= (void *) ")";
98 iov
[last
].iov_base
= (void *) "[0x";
99 iov
[last
].iov_len
= 3;
102 /* convert array[cnt] to a string in hex format */
103 len
= snprintf(buf
, sizeof(buf
), "%lx", (unsigned long) array
[cnt
]);
104 iov
[last
].iov_base
= buf
;
105 iov
[last
].iov_len
= len
;
109 iov
[last
].iov_base
= (void *) "]\n";
110 iov
[last
].iov_len
= 2;
113 writev (fd
, iov
, last
);