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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7329e6a was 7e752b2, checked in by Martin Decky <martin@…>, 15 years ago
  • correct printf() formatting strings and corresponding arguments
  • minor cstyle changes and other small fixes
  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[ab08b42]1/*
[df4ed85]2 * Copyright (c) 2005 Ondrej Palkovsky
[ab08b42]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
[06e1e95]29/** @addtogroup genericdebug
[b45c443]30 * @{
31 */
32
[cf26ba9]33/**
[b45c443]34 * @file
[68ad9d2]35 * @brief Kernel symbol resolver.
[cf26ba9]36 */
[ab08b42]37
38#include <symtab.h>
[5d494b3]39#include <byteorder.h>
[19f857a]40#include <str.h>
[6e716a59]41#include <print.h>
[d99c1d2]42#include <typedefs.h>
[8ed4014]43#include <typedefs.h>
[e16e0d59]44#include <errno.h>
[ab08b42]45
[68ad9d2]46/** Get name of a symbol that seems most likely to correspond to address.
[ab08b42]47 *
[c19aa612]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.
[68ad9d2]51 *
52 * @return Zero on success or negative error code, ENOENT if not found,
53 * ENOTSUP if symbol table not available.
[a783ca4]54 *
[ab08b42]55 */
[a000878c]56int symtab_name_lookup(uintptr_t addr, const char **name, uintptr_t *offset)
[ab08b42]57{
[e16e0d59]58#ifdef CONFIG_SYMTAB
[98000fb]59 size_t i;
[68ad9d2]60
61 for (i = 1; symbol_table[i].address_le; i++) {
[7f1c620]62 if (addr < uint64_t_le2host(symbol_table[i].address_le))
[ab08b42]63 break;
64 }
[68ad9d2]65
[e16e0d59]66 if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) {
67 *name = symbol_table[i - 1].symbol_name;
[0dee005]68 if (offset)
69 *offset = addr -
70 uint64_t_le2host(symbol_table[i - 1].address_le);
[e16e0d59]71 return EOK;
72 }
[68ad9d2]73
[e16e0d59]74 *name = NULL;
75 return ENOENT;
[68ad9d2]76
[e16e0d59]77#else
78 *name = NULL;
79 return ENOTSUP;
80#endif
81}
82
83/** Lookup symbol by address and format for display.
84 *
[c19aa612]85 * Returns name of closest corresponding symbol,
86 * "unknown" if none exists and "N/A" if no symbol
87 * information is available.
[e16e0d59]88 *
[68ad9d2]89 * @param addr Address.
90 * @param name Place to store pointer to the symbol name.
91 *
92 * @return Pointer to a human-readable string.
[e16e0d59]93 *
94 */
[a000878c]95const char *symtab_fmt_name_lookup(uintptr_t addr)
[e16e0d59]96{
[a000878c]97 const char *name;
[0dee005]98 int rc = symtab_name_lookup(addr, &name, NULL);
[68ad9d2]99
[e16e0d59]100 switch (rc) {
[68ad9d2]101 case EOK:
102 return name;
103 case ENOENT:
[c19aa612]104 return "unknown";
[68ad9d2]105 default:
106 return "N/A";
[e16e0d59]107 }
[ab08b42]108}
[6e716a59]109
[e16e0d59]110#ifdef CONFIG_SYMTAB
111
[cf26ba9]112/** Find symbols that match the parameter forward and print them.
[0c8e692]113 *
[68ad9d2]114 * @param name Search string
115 * @param startpos Starting position, changes to found position
116 *
117 * @return Pointer to the part of string that should be completed or NULL.
118 *
[0c8e692]119 */
[98000fb]120static const char *symtab_search_one(const char *name, size_t *startpos)
[0c8e692]121{
[98000fb]122 size_t namelen = str_length(name);
[68ad9d2]123
[98000fb]124 size_t pos;
[68ad9d2]125 for (pos = *startpos; symbol_table[pos].address_le; pos++) {
126 const char *curname = symbol_table[pos].symbol_name;
127
[37c312a]128 /* Find a ':' in curname */
[68ad9d2]129 const char *colon = str_chr(curname, ':');
130 if (colon == NULL)
[0c8e692]131 continue;
[68ad9d2]132
133 if (str_length(curname) < namelen)
[0c8e692]134 continue;
[68ad9d2]135
136 if (str_lcmp(name, curname, namelen) == 0) {
137 *startpos = pos;
138 return (curname + str_lsize(curname, namelen));
[0c8e692]139 }
140 }
[68ad9d2]141
[0c8e692]142 return NULL;
143}
144
[e16e0d59]145#endif
146
[68ad9d2]147/** Return address that corresponds to the entry.
[6e716a59]148 *
[0c8e692]149 * Search symbol table, and if there is one match, return it
[6e716a59]150 *
[68ad9d2]151 * @param name Name of the symbol
152 * @param addr Place to store symbol address
153 *
154 * @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate
155 * symbol, ENOTSUP - no symbol information available.
[e16e0d59]156 *
[6e716a59]157 */
[e16e0d59]158int symtab_addr_lookup(const char *name, uintptr_t *addr)
[6e716a59]159{
[e16e0d59]160#ifdef CONFIG_SYMTAB
[98000fb]161 size_t found = 0;
162 size_t pos = 0;
[68ad9d2]163 const char *hint;
164
165 while ((hint = symtab_search_one(name, &pos))) {
166 if (str_length(hint) == 0) {
167 *addr = uint64_t_le2host(symbol_table[pos].address_le);
[6e716a59]168 found++;
169 }
[68ad9d2]170 pos++;
[6e716a59]171 }
[68ad9d2]172
[0c8e692]173 if (found > 1)
[e16e0d59]174 return EOVERFLOW;
[68ad9d2]175
[e16e0d59]176 if (found < 1)
177 return ENOENT;
[68ad9d2]178
[e16e0d59]179 return EOK;
[68ad9d2]180
[e16e0d59]181#else
182 return ENOTSUP;
183#endif
[6e716a59]184}
185
[68ad9d2]186/** Find symbols that match parameter and print them */
[6e716a59]187void symtab_print_search(const char *name)
188{
[e16e0d59]189#ifdef CONFIG_SYMTAB
[98000fb]190 size_t pos = 0;
[68ad9d2]191 while (symtab_search_one(name, &pos)) {
192 uintptr_t addr = uint64_t_le2host(symbol_table[pos].address_le);
193 char *realname = symbol_table[pos].symbol_name;
[7e752b2]194 printf("%p: %s\n", (void *) addr, realname);
[68ad9d2]195 pos++;
[0c8e692]196 }
[68ad9d2]197
[e16e0d59]198#else
199 printf("No symbol information available.\n");
200#endif
[0c8e692]201}
202
203/** Symtab completion
204 *
[68ad9d2]205 * @param input Search string, completes to symbol name
206 * @param size Input buffer size
207 *
208 * @return 0 - nothing found, 1 - success, >1 print duplicates
209 *
[0c8e692]210 */
[98000fb]211int symtab_compl(char *input, size_t size)
[0c8e692]212{
[e16e0d59]213#ifdef CONFIG_SYMTAB
[68ad9d2]214 const char *name = input;
215
216 /* Allow completion of pointers */
217 if ((name[0] == '*') || (name[0] == '&'))
[91ef0d95]218 name++;
[68ad9d2]219
220 /* Do not print all symbols */
221 if (str_length(name) == 0)
[0c8e692]222 return 0;
[91ef0d95]223
[98000fb]224 size_t found = 0;
225 size_t pos = 0;
[68ad9d2]226 const char *hint;
227 char output[MAX_SYMBOL_NAME];
228
229 output[0] = 0;
230
231 while ((hint = symtab_search_one(name, &pos))) {
232 if ((found == 0) || (str_length(output) > str_length(hint)))
[f4b1535]233 str_cpy(output, MAX_SYMBOL_NAME, hint);
[68ad9d2]234
235 pos++;
[0c8e692]236 found++;
237 }
[68ad9d2]238
239 if ((found > 1) && (str_length(output) != 0)) {
[0c8e692]240 printf("\n");
[68ad9d2]241 pos = 0;
[137691a]242 while (symtab_search_one(name, &pos)) {
[68ad9d2]243 printf("%s\n", symbol_table[pos].symbol_name);
244 pos++;
[6e716a59]245 }
246 }
[68ad9d2]247
248 if (found > 0)
[f4b1535]249 str_cpy(input, size, output);
[68ad9d2]250
[0c8e692]251 return found;
[68ad9d2]252
[e16e0d59]253#else
254 return 0;
255#endif
[6e716a59]256}
[b45c443]257
[06e1e95]258/** @}
[b45c443]259 */
Note: See TracBrowser for help on using the repository browser.