1 | /*
|
---|
2 | * Copyright (c) 2005 Ondrej Palkovsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup genericdebug
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Kernel symbol resolver.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <symtab.h>
|
---|
39 | #include <byteorder.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <print.h>
|
---|
42 | #include <typedefs.h>
|
---|
43 | #include <typedefs.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <console/prompt.h>
|
---|
46 |
|
---|
47 | /** Get name of a symbol that seems most likely to correspond to address.
|
---|
48 | *
|
---|
49 | * @param addr Address.
|
---|
50 | * @param name Place to store pointer to the symbol name.
|
---|
51 | * @param offset Place to store offset from the symbol address.
|
---|
52 | *
|
---|
53 | * @return Zero on success or negative error code, ENOENT if not found,
|
---|
54 | * ENOTSUP if symbol table not available.
|
---|
55 | *
|
---|
56 | */
|
---|
57 | int symtab_name_lookup(uintptr_t addr, const char **name, uintptr_t *offset)
|
---|
58 | {
|
---|
59 | #ifdef CONFIG_SYMTAB
|
---|
60 | size_t i;
|
---|
61 |
|
---|
62 | for (i = 1; symbol_table[i].address_le; i++) {
|
---|
63 | if (addr < uint64_t_le2host(symbol_table[i].address_le))
|
---|
64 | break;
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) {
|
---|
68 | *name = symbol_table[i - 1].symbol_name;
|
---|
69 | if (offset)
|
---|
70 | *offset = addr -
|
---|
71 | uint64_t_le2host(symbol_table[i - 1].address_le);
|
---|
72 | return EOK;
|
---|
73 | }
|
---|
74 |
|
---|
75 | *name = NULL;
|
---|
76 | return ENOENT;
|
---|
77 |
|
---|
78 | #else
|
---|
79 | *name = NULL;
|
---|
80 | return ENOTSUP;
|
---|
81 | #endif
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Lookup symbol by address and format for display.
|
---|
85 | *
|
---|
86 | * Returns name of closest corresponding symbol,
|
---|
87 | * "unknown" if none exists and "N/A" if no symbol
|
---|
88 | * information is available.
|
---|
89 | *
|
---|
90 | * @param addr Address.
|
---|
91 | * @param name Place to store pointer to the symbol name.
|
---|
92 | *
|
---|
93 | * @return Pointer to a human-readable string.
|
---|
94 | *
|
---|
95 | */
|
---|
96 | const char *symtab_fmt_name_lookup(uintptr_t addr)
|
---|
97 | {
|
---|
98 | const char *name;
|
---|
99 | int rc = symtab_name_lookup(addr, &name, NULL);
|
---|
100 |
|
---|
101 | switch (rc) {
|
---|
102 | case EOK:
|
---|
103 | return name;
|
---|
104 | case ENOENT:
|
---|
105 | return "unknown";
|
---|
106 | default:
|
---|
107 | return "N/A";
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | #ifdef CONFIG_SYMTAB
|
---|
112 |
|
---|
113 | /** Find symbols that match the parameter forward and print them.
|
---|
114 | *
|
---|
115 | * @param name Search string
|
---|
116 | * @param startpos Starting position, changes to found position
|
---|
117 | *
|
---|
118 | * @return Pointer to the part of string that should be completed or NULL.
|
---|
119 | *
|
---|
120 | */
|
---|
121 | static const char *symtab_search_one(const char *name, size_t *startpos)
|
---|
122 | {
|
---|
123 | size_t namelen = str_length(name);
|
---|
124 |
|
---|
125 | size_t pos;
|
---|
126 | for (pos = *startpos; symbol_table[pos].address_le; pos++) {
|
---|
127 | const char *curname = symbol_table[pos].symbol_name;
|
---|
128 |
|
---|
129 | /* Find a ':' in curname */
|
---|
130 | const char *colon = str_chr(curname, ':');
|
---|
131 | if (colon == NULL)
|
---|
132 | continue;
|
---|
133 |
|
---|
134 | if (str_length(curname) < namelen)
|
---|
135 | continue;
|
---|
136 |
|
---|
137 | if (str_lcmp(name, curname, namelen) == 0) {
|
---|
138 | *startpos = pos;
|
---|
139 | return (curname + str_lsize(curname, namelen));
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | return NULL;
|
---|
144 | }
|
---|
145 |
|
---|
146 | #endif
|
---|
147 |
|
---|
148 | /** Return address that corresponds to the entry.
|
---|
149 | *
|
---|
150 | * Search symbol table, and if there is one match, return it
|
---|
151 | *
|
---|
152 | * @param name Name of the symbol
|
---|
153 | * @param addr Place to store symbol address
|
---|
154 | *
|
---|
155 | * @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate
|
---|
156 | * symbol, ENOTSUP - no symbol information available.
|
---|
157 | *
|
---|
158 | */
|
---|
159 | int symtab_addr_lookup(const char *name, uintptr_t *addr)
|
---|
160 | {
|
---|
161 | #ifdef CONFIG_SYMTAB
|
---|
162 | size_t found = 0;
|
---|
163 | size_t pos = 0;
|
---|
164 | const char *hint;
|
---|
165 |
|
---|
166 | while ((hint = symtab_search_one(name, &pos))) {
|
---|
167 | if (str_length(hint) == 0) {
|
---|
168 | *addr = uint64_t_le2host(symbol_table[pos].address_le);
|
---|
169 | found++;
|
---|
170 | }
|
---|
171 | pos++;
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (found > 1)
|
---|
175 | return EOVERFLOW;
|
---|
176 |
|
---|
177 | if (found < 1)
|
---|
178 | return ENOENT;
|
---|
179 |
|
---|
180 | return EOK;
|
---|
181 |
|
---|
182 | #else
|
---|
183 | return ENOTSUP;
|
---|
184 | #endif
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Find symbols that match parameter and print them */
|
---|
188 | void symtab_print_search(const char *name)
|
---|
189 | {
|
---|
190 | #ifdef CONFIG_SYMTAB
|
---|
191 | size_t pos = 0;
|
---|
192 | while (symtab_search_one(name, &pos)) {
|
---|
193 | uintptr_t addr = uint64_t_le2host(symbol_table[pos].address_le);
|
---|
194 | char *realname = symbol_table[pos].symbol_name;
|
---|
195 | printf("%p: %s\n", (void *) addr, realname);
|
---|
196 | pos++;
|
---|
197 | }
|
---|
198 |
|
---|
199 | #else
|
---|
200 | printf("No symbol information available.\n");
|
---|
201 | #endif
|
---|
202 | }
|
---|
203 |
|
---|
204 | /** Symtab completion
|
---|
205 | *
|
---|
206 | * @param input Search string, completes to symbol name
|
---|
207 | * @param size Input buffer size
|
---|
208 | *
|
---|
209 | * @return 0 - nothing found, 1 - success, >1 print duplicates
|
---|
210 | *
|
---|
211 | */
|
---|
212 | int symtab_compl(char *input, size_t size, indev_t *indev)
|
---|
213 | {
|
---|
214 | #ifdef CONFIG_SYMTAB
|
---|
215 | const char *name = input;
|
---|
216 |
|
---|
217 | /* Allow completion of pointers */
|
---|
218 | if ((name[0] == '*') || (name[0] == '&'))
|
---|
219 | name++;
|
---|
220 |
|
---|
221 | /* Do not print all symbols */
|
---|
222 | if (str_length(name) == 0)
|
---|
223 | return 0;
|
---|
224 |
|
---|
225 | size_t found = 0;
|
---|
226 | size_t pos = 0;
|
---|
227 | const char *hint;
|
---|
228 | char output[MAX_SYMBOL_NAME];
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Maximum Match Length: Length of longest matching common substring in
|
---|
232 | * case more than one match is found.
|
---|
233 | */
|
---|
234 | size_t max_match_len = size;
|
---|
235 | size_t max_match_len_tmp = size;
|
---|
236 | size_t input_len = str_length(input);
|
---|
237 | char *sym_name;
|
---|
238 | size_t hints_to_show = MAX_TAB_HINTS - 1;
|
---|
239 | size_t total_hints_shown = 0;
|
---|
240 | bool continue_showing_hints = true;
|
---|
241 |
|
---|
242 | output[0] = 0;
|
---|
243 |
|
---|
244 | while ((hint = symtab_search_one(name, &pos)))
|
---|
245 | pos++;
|
---|
246 |
|
---|
247 | pos = 0;
|
---|
248 |
|
---|
249 | while ((hint = symtab_search_one(name, &pos))) {
|
---|
250 | if ((found == 0) || (str_length(output) > str_length(hint)))
|
---|
251 | str_cpy(output, MAX_SYMBOL_NAME, hint);
|
---|
252 |
|
---|
253 | pos++;
|
---|
254 | found++;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * If the number of possible completions is more than MAX_TAB_HINTS,
|
---|
259 | * ask the user whether to display them or not.
|
---|
260 | */
|
---|
261 | if (found > MAX_TAB_HINTS) {
|
---|
262 | printf("\n");
|
---|
263 | continue_showing_hints =
|
---|
264 | console_prompt_display_all_hints(indev, found);
|
---|
265 | }
|
---|
266 |
|
---|
267 | if ((found > 1) && (str_length(output) != 0)) {
|
---|
268 | printf("\n");
|
---|
269 | pos = 0;
|
---|
270 | while (symtab_search_one(name, &pos)) {
|
---|
271 | sym_name = symbol_table[pos].symbol_name;
|
---|
272 | pos++;
|
---|
273 |
|
---|
274 | if (continue_showing_hints) {
|
---|
275 | /* We are still showing hints */
|
---|
276 | printf("%s\n", sym_name);
|
---|
277 | --hints_to_show;
|
---|
278 | ++total_hints_shown;
|
---|
279 |
|
---|
280 | if ((hints_to_show == 0) && (total_hints_shown != found)) {
|
---|
281 | /* Ask the user to continue */
|
---|
282 | continue_showing_hints =
|
---|
283 | console_prompt_more_hints(indev, &hints_to_show);
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | for (max_match_len_tmp = 0;
|
---|
288 | (output[max_match_len_tmp] ==
|
---|
289 | sym_name[input_len + max_match_len_tmp]) &&
|
---|
290 | (max_match_len_tmp < max_match_len); ++max_match_len_tmp);
|
---|
291 |
|
---|
292 | max_match_len = max_match_len_tmp;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /* Keep only the characters common in all completions */
|
---|
296 | output[max_match_len] = 0;
|
---|
297 | }
|
---|
298 |
|
---|
299 | if (found > 0)
|
---|
300 | str_cpy(input, size, output);
|
---|
301 |
|
---|
302 | return found;
|
---|
303 |
|
---|
304 | #else
|
---|
305 | return 0;
|
---|
306 | #endif
|
---|
307 | }
|
---|
308 |
|
---|
309 | /** @}
|
---|
310 | */
|
---|