[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] | 57 | int 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] | 96 | const 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] | 121 | static 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] | 159 | int 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] | 188 | void 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 |
|
---|
| 204 | /** Symtab completion
|
---|
| 205 | *
|
---|
[68ad9d2] | 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 | *
|
---|
[0c8e692] | 211 | */
|
---|
[e435537] | 212 | int symtab_compl(char *input, size_t size, indev_t *indev)
|
---|
[0c8e692] | 213 | {
|
---|
[e16e0d59] | 214 | #ifdef CONFIG_SYMTAB
|
---|
[68ad9d2] | 215 | const char *name = input;
|
---|
| 216 |
|
---|
| 217 | /* Allow completion of pointers */
|
---|
| 218 | if ((name[0] == '*') || (name[0] == '&'))
|
---|
[91ef0d95] | 219 | name++;
|
---|
[68ad9d2] | 220 |
|
---|
| 221 | /* Do not print all symbols */
|
---|
| 222 | if (str_length(name) == 0)
|
---|
[0c8e692] | 223 | return 0;
|
---|
[91ef0d95] | 224 |
|
---|
[98000fb] | 225 | size_t found = 0;
|
---|
| 226 | size_t pos = 0;
|
---|
[68ad9d2] | 227 | const char *hint;
|
---|
| 228 | char output[MAX_SYMBOL_NAME];
|
---|
[e435537] | 229 |
|
---|
| 230 | /*
|
---|
| 231 | * Maximum Match Length: Length of longest matching common substring in
|
---|
| 232 | * case more than one match is found.
|
---|
| 233 | */
|
---|
[1e01a35] | 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;
|
---|
[f0d7bd9] | 240 | bool continue_showing_hints = true;
|
---|
[68ad9d2] | 241 |
|
---|
| 242 | output[0] = 0;
|
---|
[e435537] | 243 |
|
---|
| 244 | while ((hint = symtab_search_one(name, &pos)))
|
---|
| 245 | pos++;
|
---|
| 246 |
|
---|
[1e01a35] | 247 | pos = 0;
|
---|
[68ad9d2] | 248 |
|
---|
| 249 | while ((hint = symtab_search_one(name, &pos))) {
|
---|
| 250 | if ((found == 0) || (str_length(output) > str_length(hint)))
|
---|
[f4b1535] | 251 | str_cpy(output, MAX_SYMBOL_NAME, hint);
|
---|
[68ad9d2] | 252 |
|
---|
| 253 | pos++;
|
---|
[0c8e692] | 254 | found++;
|
---|
| 255 | }
|
---|
[68ad9d2] | 256 |
|
---|
[e435537] | 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 | */
|
---|
[1e01a35] | 261 | if (found > MAX_TAB_HINTS) {
|
---|
[aca4a04] | 262 | printf("\n");
|
---|
[e435537] | 263 | continue_showing_hints =
|
---|
| 264 | console_prompt_display_all_hints(indev, found);
|
---|
[1e01a35] | 265 | }
|
---|
| 266 |
|
---|
[68ad9d2] | 267 | if ((found > 1) && (str_length(output) != 0)) {
|
---|
[0c8e692] | 268 | printf("\n");
|
---|
[68ad9d2] | 269 | pos = 0;
|
---|
[137691a] | 270 | while (symtab_search_one(name, &pos)) {
|
---|
[1e01a35] | 271 | sym_name = symbol_table[pos].symbol_name;
|
---|
[68ad9d2] | 272 | pos++;
|
---|
[e435537] | 273 |
|
---|
| 274 | if (continue_showing_hints) {
|
---|
| 275 | /* We are still showing hints */
|
---|
[1e01a35] | 276 | printf("%s\n", sym_name);
|
---|
| 277 | --hints_to_show;
|
---|
| 278 | ++total_hints_shown;
|
---|
[e435537] | 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);
|
---|
[1e01a35] | 284 | }
|
---|
| 285 | }
|
---|
[e435537] | 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 |
|
---|
[1e01a35] | 292 | max_match_len = max_match_len_tmp;
|
---|
[6e716a59] | 293 | }
|
---|
[e435537] | 294 |
|
---|
| 295 | /* Keep only the characters common in all completions */
|
---|
[1e01a35] | 296 | output[max_match_len] = 0;
|
---|
[6e716a59] | 297 | }
|
---|
[68ad9d2] | 298 |
|
---|
| 299 | if (found > 0)
|
---|
[f4b1535] | 300 | str_cpy(input, size, output);
|
---|
[68ad9d2] | 301 |
|
---|
[0c8e692] | 302 | return found;
|
---|
[68ad9d2] | 303 |
|
---|
[e16e0d59] | 304 | #else
|
---|
| 305 | return 0;
|
---|
| 306 | #endif
|
---|
[6e716a59] | 307 | }
|
---|
[b45c443] | 308 |
|
---|
[06e1e95] | 309 | /** @}
|
---|
[b45c443] | 310 | */
|
---|