source: mainline/kernel/generic/src/debug/symtab.c@ a5c86478

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a5c86478 was 0dee005, checked in by Jakub Jermar <jakub@…>, 15 years ago

Change symtab_name_lookup() to also return the offset from the matching symbol
address.

Use uintptr_t instead of unative_t for addresses.

Formatting changes.

  • Property mode set to 100644
File size: 6.0 KB
Line 
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 <string.h>
41#include <print.h>
42#include <arch/types.h>
43#include <typedefs.h>
44#include <errno.h>
45
46/** Get name of a symbol that seems most likely to correspond to address.
47 *
48 * @param addr Address.
49 * @param name Place to store pointer to the symbol name.
50 * @param offset Place to store offset from the symbol address.
51 *
52 * @return Zero on success or negative error code, ENOENT if not found,
53 * ENOTSUP if symbol table not available.
54 *
55 */
56int symtab_name_lookup(uintptr_t addr, char **name, uintptr_t *offset)
57{
58#ifdef CONFIG_SYMTAB
59 size_t i;
60
61 for (i = 1; symbol_table[i].address_le; i++) {
62 if (addr < uint64_t_le2host(symbol_table[i].address_le))
63 break;
64 }
65
66 if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) {
67 *name = symbol_table[i - 1].symbol_name;
68 if (offset)
69 *offset = addr -
70 uint64_t_le2host(symbol_table[i - 1].address_le);
71 return EOK;
72 }
73
74 *name = NULL;
75 return ENOENT;
76
77#else
78 *name = NULL;
79 return ENOTSUP;
80#endif
81}
82
83/** Lookup symbol by address and format for display.
84 *
85 * Returns name of closest corresponding symbol, "Not found" if none exists
86 * or "N/A" if no symbol information is available.
87 *
88 * @param addr Address.
89 * @param name Place to store pointer to the symbol name.
90 *
91 * @return Pointer to a human-readable string.
92 *
93 */
94char *symtab_fmt_name_lookup(uintptr_t addr)
95{
96 char *name;
97 int rc = symtab_name_lookup(addr, &name, NULL);
98
99 switch (rc) {
100 case EOK:
101 return name;
102 case ENOENT:
103 return "Not found";
104 default:
105 return "N/A";
106 }
107}
108
109#ifdef CONFIG_SYMTAB
110
111/** Find symbols that match the parameter forward and print them.
112 *
113 * @param name Search string
114 * @param startpos Starting position, changes to found position
115 *
116 * @return Pointer to the part of string that should be completed or NULL.
117 *
118 */
119static const char *symtab_search_one(const char *name, size_t *startpos)
120{
121 size_t namelen = str_length(name);
122
123 size_t pos;
124 for (pos = *startpos; symbol_table[pos].address_le; pos++) {
125 const char *curname = symbol_table[pos].symbol_name;
126
127 /* Find a ':' in curname */
128 const char *colon = str_chr(curname, ':');
129 if (colon == NULL)
130 continue;
131
132 if (str_length(curname) < namelen)
133 continue;
134
135 if (str_lcmp(name, curname, namelen) == 0) {
136 *startpos = pos;
137 return (curname + str_lsize(curname, namelen));
138 }
139 }
140
141 return NULL;
142}
143
144#endif
145
146/** Return address that corresponds to the entry.
147 *
148 * Search symbol table, and if there is one match, return it
149 *
150 * @param name Name of the symbol
151 * @param addr Place to store symbol address
152 *
153 * @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate
154 * symbol, ENOTSUP - no symbol information available.
155 *
156 */
157int symtab_addr_lookup(const char *name, uintptr_t *addr)
158{
159#ifdef CONFIG_SYMTAB
160 size_t found = 0;
161 size_t pos = 0;
162 const char *hint;
163
164 while ((hint = symtab_search_one(name, &pos))) {
165 if (str_length(hint) == 0) {
166 *addr = uint64_t_le2host(symbol_table[pos].address_le);
167 found++;
168 }
169 pos++;
170 }
171
172 if (found > 1)
173 return EOVERFLOW;
174
175 if (found < 1)
176 return ENOENT;
177
178 return EOK;
179
180#else
181 return ENOTSUP;
182#endif
183}
184
185/** Find symbols that match parameter and print them */
186void symtab_print_search(const char *name)
187{
188#ifdef CONFIG_SYMTAB
189 size_t pos = 0;
190 while (symtab_search_one(name, &pos)) {
191 uintptr_t addr = uint64_t_le2host(symbol_table[pos].address_le);
192 char *realname = symbol_table[pos].symbol_name;
193 printf("%p: %s\n", addr, realname);
194 pos++;
195 }
196
197#else
198 printf("No symbol information available.\n");
199#endif
200}
201
202/** Symtab completion
203 *
204 * @param input Search string, completes to symbol name
205 * @param size Input buffer size
206 *
207 * @return 0 - nothing found, 1 - success, >1 print duplicates
208 *
209 */
210int symtab_compl(char *input, size_t size)
211{
212#ifdef CONFIG_SYMTAB
213 const char *name = input;
214
215 /* Allow completion of pointers */
216 if ((name[0] == '*') || (name[0] == '&'))
217 name++;
218
219 /* Do not print all symbols */
220 if (str_length(name) == 0)
221 return 0;
222
223 size_t found = 0;
224 size_t pos = 0;
225 const char *hint;
226 char output[MAX_SYMBOL_NAME];
227
228 output[0] = 0;
229
230 while ((hint = symtab_search_one(name, &pos))) {
231 if ((found == 0) || (str_length(output) > str_length(hint)))
232 str_cpy(output, MAX_SYMBOL_NAME, hint);
233
234 pos++;
235 found++;
236 }
237
238 if ((found > 1) && (str_length(output) != 0)) {
239 printf("\n");
240 pos = 0;
241 while ((hint = symtab_search_one(name, &pos))) {
242 printf("%s\n", symbol_table[pos].symbol_name);
243 pos++;
244 }
245 }
246
247 if (found > 0)
248 str_cpy(input, size, output);
249
250 return found;
251
252#else
253 return 0;
254#endif
255}
256
257/** @}
258 */
Note: See TracBrowser for help on using the repository browser.