[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>
|
---|
[16da5f8e] | 40 | #include <string.h>
|
---|
[6e716a59] | 41 | #include <print.h>
|
---|
[8ed4014] | 42 | #include <arch/types.h>
|
---|
| 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 | *
|
---|
[0dee005] | 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 | */
|
---|
[0dee005] | 56 | int symtab_name_lookup(uintptr_t addr, 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 | *
|
---|
| 85 | * Returns name of closest corresponding symbol, "Not found" if none exists
|
---|
| 86 | * or "N/A" if no symbol information is available.
|
---|
| 87 | *
|
---|
[68ad9d2] | 88 | * @param addr Address.
|
---|
| 89 | * @param name Place to store pointer to the symbol name.
|
---|
| 90 | *
|
---|
| 91 | * @return Pointer to a human-readable string.
|
---|
[e16e0d59] | 92 | *
|
---|
| 93 | */
|
---|
[0dee005] | 94 | char *symtab_fmt_name_lookup(uintptr_t addr)
|
---|
[e16e0d59] | 95 | {
|
---|
| 96 | char *name;
|
---|
[0dee005] | 97 | int rc = symtab_name_lookup(addr, &name, NULL);
|
---|
[68ad9d2] | 98 |
|
---|
[e16e0d59] | 99 | switch (rc) {
|
---|
[68ad9d2] | 100 | case EOK:
|
---|
| 101 | return name;
|
---|
| 102 | case ENOENT:
|
---|
| 103 | return "Not found";
|
---|
| 104 | default:
|
---|
| 105 | return "N/A";
|
---|
[e16e0d59] | 106 | }
|
---|
[ab08b42] | 107 | }
|
---|
[6e716a59] | 108 |
|
---|
[e16e0d59] | 109 | #ifdef CONFIG_SYMTAB
|
---|
| 110 |
|
---|
[cf26ba9] | 111 | /** Find symbols that match the parameter forward and print them.
|
---|
[0c8e692] | 112 | *
|
---|
[68ad9d2] | 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 | *
|
---|
[0c8e692] | 118 | */
|
---|
[98000fb] | 119 | static const char *symtab_search_one(const char *name, size_t *startpos)
|
---|
[0c8e692] | 120 | {
|
---|
[98000fb] | 121 | size_t namelen = str_length(name);
|
---|
[68ad9d2] | 122 |
|
---|
[98000fb] | 123 | size_t pos;
|
---|
[68ad9d2] | 124 | for (pos = *startpos; symbol_table[pos].address_le; pos++) {
|
---|
| 125 | const char *curname = symbol_table[pos].symbol_name;
|
---|
| 126 |
|
---|
[37c312a] | 127 | /* Find a ':' in curname */
|
---|
[68ad9d2] | 128 | const char *colon = str_chr(curname, ':');
|
---|
| 129 | if (colon == NULL)
|
---|
[0c8e692] | 130 | continue;
|
---|
[68ad9d2] | 131 |
|
---|
| 132 | if (str_length(curname) < namelen)
|
---|
[0c8e692] | 133 | continue;
|
---|
[68ad9d2] | 134 |
|
---|
| 135 | if (str_lcmp(name, curname, namelen) == 0) {
|
---|
| 136 | *startpos = pos;
|
---|
| 137 | return (curname + str_lsize(curname, namelen));
|
---|
[0c8e692] | 138 | }
|
---|
| 139 | }
|
---|
[68ad9d2] | 140 |
|
---|
[0c8e692] | 141 | return NULL;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[e16e0d59] | 144 | #endif
|
---|
| 145 |
|
---|
[68ad9d2] | 146 | /** Return address that corresponds to the entry.
|
---|
[6e716a59] | 147 | *
|
---|
[0c8e692] | 148 | * Search symbol table, and if there is one match, return it
|
---|
[6e716a59] | 149 | *
|
---|
[68ad9d2] | 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.
|
---|
[e16e0d59] | 155 | *
|
---|
[6e716a59] | 156 | */
|
---|
[e16e0d59] | 157 | int symtab_addr_lookup(const char *name, uintptr_t *addr)
|
---|
[6e716a59] | 158 | {
|
---|
[e16e0d59] | 159 | #ifdef CONFIG_SYMTAB
|
---|
[98000fb] | 160 | size_t found = 0;
|
---|
| 161 | size_t pos = 0;
|
---|
[68ad9d2] | 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);
|
---|
[6e716a59] | 167 | found++;
|
---|
| 168 | }
|
---|
[68ad9d2] | 169 | pos++;
|
---|
[6e716a59] | 170 | }
|
---|
[68ad9d2] | 171 |
|
---|
[0c8e692] | 172 | if (found > 1)
|
---|
[e16e0d59] | 173 | return EOVERFLOW;
|
---|
[68ad9d2] | 174 |
|
---|
[e16e0d59] | 175 | if (found < 1)
|
---|
| 176 | return ENOENT;
|
---|
[68ad9d2] | 177 |
|
---|
[e16e0d59] | 178 | return EOK;
|
---|
[68ad9d2] | 179 |
|
---|
[e16e0d59] | 180 | #else
|
---|
| 181 | return ENOTSUP;
|
---|
| 182 | #endif
|
---|
[6e716a59] | 183 | }
|
---|
| 184 |
|
---|
[68ad9d2] | 185 | /** Find symbols that match parameter and print them */
|
---|
[6e716a59] | 186 | void symtab_print_search(const char *name)
|
---|
| 187 | {
|
---|
[e16e0d59] | 188 | #ifdef CONFIG_SYMTAB
|
---|
[98000fb] | 189 | size_t pos = 0;
|
---|
[68ad9d2] | 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;
|
---|
[8ed4014] | 193 | printf("%p: %s\n", addr, realname);
|
---|
[68ad9d2] | 194 | pos++;
|
---|
[0c8e692] | 195 | }
|
---|
[68ad9d2] | 196 |
|
---|
[e16e0d59] | 197 | #else
|
---|
| 198 | printf("No symbol information available.\n");
|
---|
| 199 | #endif
|
---|
[0c8e692] | 200 | }
|
---|
| 201 |
|
---|
| 202 | /** Symtab completion
|
---|
| 203 | *
|
---|
[68ad9d2] | 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 | *
|
---|
[0c8e692] | 209 | */
|
---|
[98000fb] | 210 | int symtab_compl(char *input, size_t size)
|
---|
[0c8e692] | 211 | {
|
---|
[e16e0d59] | 212 | #ifdef CONFIG_SYMTAB
|
---|
[68ad9d2] | 213 | const char *name = input;
|
---|
| 214 |
|
---|
| 215 | /* Allow completion of pointers */
|
---|
| 216 | if ((name[0] == '*') || (name[0] == '&'))
|
---|
[91ef0d95] | 217 | name++;
|
---|
[68ad9d2] | 218 |
|
---|
| 219 | /* Do not print all symbols */
|
---|
| 220 | if (str_length(name) == 0)
|
---|
[0c8e692] | 221 | return 0;
|
---|
[91ef0d95] | 222 |
|
---|
[98000fb] | 223 | size_t found = 0;
|
---|
| 224 | size_t pos = 0;
|
---|
[68ad9d2] | 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)))
|
---|
[f4b1535] | 232 | str_cpy(output, MAX_SYMBOL_NAME, hint);
|
---|
[68ad9d2] | 233 |
|
---|
| 234 | pos++;
|
---|
[0c8e692] | 235 | found++;
|
---|
| 236 | }
|
---|
[68ad9d2] | 237 |
|
---|
| 238 | if ((found > 1) && (str_length(output) != 0)) {
|
---|
[0c8e692] | 239 | printf("\n");
|
---|
[68ad9d2] | 240 | pos = 0;
|
---|
| 241 | while ((hint = symtab_search_one(name, &pos))) {
|
---|
| 242 | printf("%s\n", symbol_table[pos].symbol_name);
|
---|
| 243 | pos++;
|
---|
[6e716a59] | 244 | }
|
---|
| 245 | }
|
---|
[68ad9d2] | 246 |
|
---|
| 247 | if (found > 0)
|
---|
[f4b1535] | 248 | str_cpy(input, size, output);
|
---|
[68ad9d2] | 249 |
|
---|
[0c8e692] | 250 | return found;
|
---|
[68ad9d2] | 251 |
|
---|
[e16e0d59] | 252 | #else
|
---|
| 253 | return 0;
|
---|
| 254 | #endif
|
---|
[6e716a59] | 255 | }
|
---|
[b45c443] | 256 |
|
---|
[06e1e95] | 257 | /** @}
|
---|
[b45c443] | 258 | */
|
---|