1 /* Return list with names for address in backtrace.
2 Copyright (C) 1998,1999,2000,2001,2003 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/backtracesyms.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 */
36 #include <link.h> /* required for __ELF_NATIVE_CLASS */
38 #if __ELF_NATIVE_CLASS == 32
41 /* We assyme 64bits. */
42 # define WORD_WIDTH 16
46 char ** backtrace_symbols (void *const *array
, int size
)
54 /* Fill in the information we can get from `dladdr'. */
55 for (cnt
= 0; cnt
< size
; ++cnt
) {
56 status
[cnt
] = dladdr (array
[cnt
], &info
[cnt
]);
57 if (status
[cnt
] && info
[cnt
].dli_fname
&&
58 info
[cnt
].dli_fname
[0] != '\0')
60 * We have some info, compute the length of the string which will be
61 * "<file-name>(<sym-name>) [+offset].
63 total
+= (strlen (info
[cnt
].dli_fname
?: "") +
64 (info
[cnt
].dli_sname
?
65 strlen (info
[cnt
].dli_sname
) + 3 + WORD_WIDTH
+ 3 : 1)
68 total
+= 5 + WORD_WIDTH
;
71 /* Allocate memory for the result. */
72 result
= (char **) malloc (size
* sizeof (char *) + total
);
74 char *last
= (char *) (result
+ size
);
75 for (cnt
= 0; cnt
< size
; ++cnt
) {
78 if (status
[cnt
] && info
[cnt
].dli_fname
79 && info
[cnt
].dli_fname
[0] != '\0') {
83 if (array
[cnt
] >= (void *) info
[cnt
].dli_saddr
)
84 sprintf (buf
, "+%#lx",
85 (unsigned long)(array
[cnt
] - info
[cnt
].dli_saddr
));
87 sprintf (buf
, "-%#lx",
88 (unsigned long)(info
[cnt
].dli_saddr
- array
[cnt
]));
90 last
+= 1 + sprintf (last
, "%s%s%s%s%s[%p]",
91 info
[cnt
].dli_fname
?: "",
92 info
[cnt
].dli_sname
? "(" : "",
93 info
[cnt
].dli_sname
?: "",
94 info
[cnt
].dli_sname
? buf
: "",
95 info
[cnt
].dli_sname
? ") " : " ",
98 last
+= 1 + sprintf (last
, "[%p]", array
[cnt
]);
100 assert (last
<= (char *) result
+ size
* sizeof (char *) + total
);