2
* Copyright (c) 2005 Ondrej Palkovsky
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
29
/** @addtogroup genericdebug
35
* @brief Kernel symbol resolver.
39
#include <byteorder.h>
42
#include <arch/types.h>
46
/** Get name of a symbol that seems most likely to correspond to address.
48
* @param addr Address.
49
* @param name Place to store pointer to the symbol name.
51
* @return Zero on success or negative error code, ENOENT if not found,
52
* ENOTSUP if symbol table not available.
55
int symtab_name_lookup(unative_t addr, char **name)
60
for (i = 1; symbol_table[i].address_le; i++) {
61
if (addr < uint64_t_le2host(symbol_table[i].address_le))
65
if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) {
66
*name = symbol_table[i - 1].symbol_name;
79
/** Lookup symbol by address and format for display.
81
* Returns name of closest corresponding symbol, "Not found" if none exists
82
* or "N/A" if no symbol information is available.
84
* @param addr Address.
85
* @param name Place to store pointer to the symbol name.
87
* @return Pointer to a human-readable string.
90
char *symtab_fmt_name_lookup(unative_t addr)
93
int rc = symtab_name_lookup(addr, &name);
107
/** Find symbols that match the parameter forward and print them.
109
* @param name Search string
110
* @param startpos Starting position, changes to found position
112
* @return Pointer to the part of string that should be completed or NULL.
115
static const char *symtab_search_one(const char *name, size_t *startpos)
117
size_t namelen = str_length(name);
120
for (pos = *startpos; symbol_table[pos].address_le; pos++) {
121
const char *curname = symbol_table[pos].symbol_name;
123
/* Find a ':' in curname */
124
const char *colon = str_chr(curname, ':');
128
if (str_length(curname) < namelen)
131
if (str_lcmp(name, curname, namelen) == 0) {
133
return (curname + str_lsize(curname, namelen));
142
/** Return address that corresponds to the entry.
144
* Search symbol table, and if there is one match, return it
146
* @param name Name of the symbol
147
* @param addr Place to store symbol address
149
* @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate
150
* symbol, ENOTSUP - no symbol information available.
153
int symtab_addr_lookup(const char *name, uintptr_t *addr)
160
while ((hint = symtab_search_one(name, &pos))) {
161
if (str_length(hint) == 0) {
162
*addr = uint64_t_le2host(symbol_table[pos].address_le);
181
/** Find symbols that match parameter and print them */
182
void symtab_print_search(const char *name)
186
while (symtab_search_one(name, &pos)) {
187
uintptr_t addr = uint64_t_le2host(symbol_table[pos].address_le);
188
char *realname = symbol_table[pos].symbol_name;
189
printf("%p: %s\n", addr, realname);
194
printf("No symbol information available.\n");
198
/** Symtab completion
200
* @param input Search string, completes to symbol name
201
* @param size Input buffer size
203
* @return 0 - nothing found, 1 - success, >1 print duplicates
206
int symtab_compl(char *input, size_t size)
209
const char *name = input;
211
/* Allow completion of pointers */
212
if ((name[0] == '*') || (name[0] == '&'))
215
/* Do not print all symbols */
216
if (str_length(name) == 0)
222
char output[MAX_SYMBOL_NAME];
226
while ((hint = symtab_search_one(name, &pos))) {
227
if ((found == 0) || (str_length(output) > str_length(hint)))
228
str_cpy(output, MAX_SYMBOL_NAME, hint);
234
if ((found > 1) && (str_length(output) != 0)) {
237
while ((hint = symtab_search_one(name, &pos))) {
238
printf("%s\n", symbol_table[pos].symbol_name);
244
str_cpy(input, size, output);