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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dc0e41c was a363016, checked in by Aurelio Colosimo <aurelio@…>, 10 years ago

kconsole tab completion: call* argument hints restored by using proper callback

  • Property mode set to 100644
File size: 5.8 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>
[f0d7bd9]45#include <console/prompt.h>
[ab08b42]46
[68ad9d2]47/** Get name of a symbol that seems most likely to correspond to address.
[ab08b42]48 *
[c19aa612]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.
[68ad9d2]52 *
53 * @return Zero on success or negative error code, ENOENT if not found,
54 * ENOTSUP if symbol table not available.
[a783ca4]55 *
[ab08b42]56 */
[a000878c]57int symtab_name_lookup(uintptr_t addr, const char **name, uintptr_t *offset)
[ab08b42]58{
[e16e0d59]59#ifdef CONFIG_SYMTAB
[98000fb]60 size_t i;
[68ad9d2]61
62 for (i = 1; symbol_table[i].address_le; i++) {
[7f1c620]63 if (addr < uint64_t_le2host(symbol_table[i].address_le))
[ab08b42]64 break;
65 }
[68ad9d2]66
[e16e0d59]67 if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) {
68 *name = symbol_table[i - 1].symbol_name;
[0dee005]69 if (offset)
70 *offset = addr -
71 uint64_t_le2host(symbol_table[i - 1].address_le);
[e16e0d59]72 return EOK;
73 }
[68ad9d2]74
[e16e0d59]75 *name = NULL;
76 return ENOENT;
[68ad9d2]77
[e16e0d59]78#else
79 *name = NULL;
80 return ENOTSUP;
81#endif
82}
83
84/** Lookup symbol by address and format for display.
85 *
[c19aa612]86 * Returns name of closest corresponding symbol,
87 * "unknown" if none exists and "N/A" if no symbol
88 * information is available.
[e16e0d59]89 *
[68ad9d2]90 * @param addr Address.
91 * @param name Place to store pointer to the symbol name.
92 *
93 * @return Pointer to a human-readable string.
[e16e0d59]94 *
95 */
[a000878c]96const char *symtab_fmt_name_lookup(uintptr_t addr)
[e16e0d59]97{
[a000878c]98 const char *name;
[0dee005]99 int rc = symtab_name_lookup(addr, &name, NULL);
[68ad9d2]100
[e16e0d59]101 switch (rc) {
[68ad9d2]102 case EOK:
103 return name;
104 case ENOENT:
[c19aa612]105 return "unknown";
[68ad9d2]106 default:
107 return "N/A";
[e16e0d59]108 }
[ab08b42]109}
[6e716a59]110
[e16e0d59]111#ifdef CONFIG_SYMTAB
112
[cf26ba9]113/** Find symbols that match the parameter forward and print them.
[0c8e692]114 *
[68ad9d2]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 *
[0c8e692]120 */
[98000fb]121static const char *symtab_search_one(const char *name, size_t *startpos)
[0c8e692]122{
[98000fb]123 size_t namelen = str_length(name);
[68ad9d2]124
[98000fb]125 size_t pos;
[68ad9d2]126 for (pos = *startpos; symbol_table[pos].address_le; pos++) {
127 const char *curname = symbol_table[pos].symbol_name;
128
[37c312a]129 /* Find a ':' in curname */
[68ad9d2]130 const char *colon = str_chr(curname, ':');
131 if (colon == NULL)
[0c8e692]132 continue;
[68ad9d2]133
134 if (str_length(curname) < namelen)
[0c8e692]135 continue;
[68ad9d2]136
137 if (str_lcmp(name, curname, namelen) == 0) {
138 *startpos = pos;
139 return (curname + str_lsize(curname, namelen));
[0c8e692]140 }
141 }
[68ad9d2]142
[0c8e692]143 return NULL;
144}
145
[e16e0d59]146#endif
147
[68ad9d2]148/** Return address that corresponds to the entry.
[6e716a59]149 *
[0c8e692]150 * Search symbol table, and if there is one match, return it
[6e716a59]151 *
[68ad9d2]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.
[e16e0d59]157 *
[6e716a59]158 */
[e16e0d59]159int symtab_addr_lookup(const char *name, uintptr_t *addr)
[6e716a59]160{
[e16e0d59]161#ifdef CONFIG_SYMTAB
[98000fb]162 size_t found = 0;
163 size_t pos = 0;
[68ad9d2]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);
[6e716a59]169 found++;
170 }
[68ad9d2]171 pos++;
[6e716a59]172 }
[68ad9d2]173
[0c8e692]174 if (found > 1)
[e16e0d59]175 return EOVERFLOW;
[68ad9d2]176
[e16e0d59]177 if (found < 1)
178 return ENOENT;
[68ad9d2]179
[e16e0d59]180 return EOK;
[68ad9d2]181
[e16e0d59]182#else
183 return ENOTSUP;
184#endif
[6e716a59]185}
186
[68ad9d2]187/** Find symbols that match parameter and print them */
[6e716a59]188void symtab_print_search(const char *name)
189{
[e16e0d59]190#ifdef CONFIG_SYMTAB
[98000fb]191 size_t pos = 0;
[68ad9d2]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;
[7e752b2]195 printf("%p: %s\n", (void *) addr, realname);
[68ad9d2]196 pos++;
[0c8e692]197 }
[68ad9d2]198
[e16e0d59]199#else
200 printf("No symbol information available.\n");
201#endif
[0c8e692]202}
203
[a363016]204/** Symtab completion enum, see kernel/generic/include/kconsole.h */
205const char* symtab_hints_enum(const char *input, const char **help,
206 void **ctx)
[0c8e692]207{
[e16e0d59]208#ifdef CONFIG_SYMTAB
[a363016]209 size_t len = str_length(input);
210 struct symtab_entry **entry = (struct symtab_entry**)ctx;
[e435537]211
[a363016]212 if (*entry == NULL)
213 *entry = symbol_table;
[68ad9d2]214
[a363016]215 for (; (*entry)->address_le; (*entry)++) {
216 const char *curname = (*entry)->symbol_name;
[68ad9d2]217
[a363016]218 /* Find a ':' in curname */
219 const char *colon = str_chr(curname, ':');
220 if (colon == NULL)
221 continue;
222
223 if (str_length(curname) < len)
224 continue;
[e435537]225
[a363016]226 if (str_lcmp(input, curname, len) == 0) {
227 (*entry)++;
228 if (help)
229 *help = NULL;
230 return (curname + str_lsize(curname, len));
231 }
[6e716a59]232 }
[68ad9d2]233
[a363016]234 return NULL;
[68ad9d2]235
[e16e0d59]236#else
[a363016]237 return NULL;
[e16e0d59]238#endif
[6e716a59]239}
[b45c443]240
[06e1e95]241/** @}
[b45c443]242 */
Note: See TracBrowser for help on using the repository browser.