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