1 /* Return list with names for address in backtrace.
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <assert.h>
20 #include <execinfo.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <ldsodefs.h>
26
27 #if __ELF_NATIVE_CLASS == 32
28 # define WORD_WIDTH 8
29 #else
30 /* We assume 64bits. */
31 # define WORD_WIDTH 16
32 #endif
33
34
35 char **
__backtrace_symbols(void * const * array,int size)36 __backtrace_symbols (void *const *array, int size)
37 {
38 Dl_info info[size];
39 int status[size];
40 int cnt;
41 size_t total = 0;
42 char **result;
43
44 /* Fill in the information we can get from `dladdr'. */
45 for (cnt = 0; cnt < size; ++cnt)
46 {
47 struct link_map *map;
48 status[cnt] = _dl_addr (array[cnt], &info[cnt], &map, NULL);
49 if (status[cnt] && info[cnt].dli_fname && info[cnt].dli_fname[0] != '\0')
50 {
51 /* We have some info, compute the length of the string which will be
52 "<file-name>(<sym-name>+offset) [address]. */
53 total += (strlen (info[cnt].dli_fname ?: "")
54 + strlen (info[cnt].dli_sname ?: "")
55 + 3 + WORD_WIDTH + 3 + WORD_WIDTH + 5);
56
57 /* The load bias is more useful to the user than the load
58 address. The use of these addresses is to calculate an
59 address in the ELF file, so its prelinked bias is not
60 something we want to subtract out. */
61 info[cnt].dli_fbase = (void *) map->l_addr;
62 }
63 else
64 total += 5 + WORD_WIDTH;
65 }
66
67 /* Allocate memory for the result. */
68 result = (char **) malloc (size * sizeof (char *) + total);
69 if (result != NULL)
70 {
71 char *last = (char *) (result + size);
72
73 for (cnt = 0; cnt < size; ++cnt)
74 {
75 result[cnt] = last;
76
77 if (status[cnt]
78 && info[cnt].dli_fname != NULL && info[cnt].dli_fname[0] != '\0')
79 {
80 if (info[cnt].dli_sname == NULL)
81 /* We found no symbol name to use, so describe it as
82 relative to the file. */
83 info[cnt].dli_saddr = info[cnt].dli_fbase;
84
85 if (info[cnt].dli_sname == NULL && info[cnt].dli_saddr == 0)
86 last += 1 + sprintf (last, "%s(%s) [%p]",
87 info[cnt].dli_fname ?: "",
88 info[cnt].dli_sname ?: "",
89 array[cnt]);
90 else
91 {
92 char sign;
93 ptrdiff_t offset;
94 if (array[cnt] >= (void *) info[cnt].dli_saddr)
95 {
96 sign = '+';
97 offset = array[cnt] - info[cnt].dli_saddr;
98 }
99 else
100 {
101 sign = '-';
102 offset = info[cnt].dli_saddr - array[cnt];
103 }
104
105 last += 1 + sprintf (last, "%s(%s%c%#tx) [%p]",
106 info[cnt].dli_fname ?: "",
107 info[cnt].dli_sname ?: "",
108 sign, offset, array[cnt]);
109 }
110 }
111 else
112 last += 1 + sprintf (last, "[%p]", array[cnt]);
113 }
114 assert (last <= (char *) result + size * sizeof (char *) + total);
115 }
116
117 return result;
118 }
119 weak_alias (__backtrace_symbols, backtrace_symbols)
120