| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 Ondrej Palkovsky
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup genericdebug
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief Kernel symbol resolver.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <symtab.h>
|
|---|
| 39 | #include <byteorder.h>
|
|---|
| 40 | #include <str.h>
|
|---|
| 41 | #include <print.h>
|
|---|
| 42 | #include <typedefs.h>
|
|---|
| 43 | #include <typedefs.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 |
|
|---|
| 46 | /** Get name of a symbol that seems most likely to correspond to address.
|
|---|
| 47 | *
|
|---|
| 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.
|
|---|
| 51 | *
|
|---|
| 52 | * @return Zero on success or negative error code, ENOENT if not found,
|
|---|
| 53 | * ENOTSUP if symbol table not available.
|
|---|
| 54 | *
|
|---|
| 55 | */
|
|---|
| 56 | int symtab_name_lookup(uintptr_t addr, const char **name, uintptr_t *offset)
|
|---|
| 57 | {
|
|---|
| 58 | #ifdef CONFIG_SYMTAB
|
|---|
| 59 | size_t i;
|
|---|
| 60 |
|
|---|
| 61 | for (i = 1; symbol_table[i].address_le; i++) {
|
|---|
| 62 | if (addr < uint64_t_le2host(symbol_table[i].address_le))
|
|---|
| 63 | break;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) {
|
|---|
| 67 | *name = symbol_table[i - 1].symbol_name;
|
|---|
| 68 | if (offset)
|
|---|
| 69 | *offset = addr -
|
|---|
| 70 | uint64_t_le2host(symbol_table[i - 1].address_le);
|
|---|
| 71 | return EOK;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | *name = NULL;
|
|---|
| 75 | return ENOENT;
|
|---|
| 76 |
|
|---|
| 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,
|
|---|
| 86 | * "unknown" if none exists and "N/A" if no symbol
|
|---|
| 87 | * information is available.
|
|---|
| 88 | *
|
|---|
| 89 | * @param addr Address.
|
|---|
| 90 | * @param name Place to store pointer to the symbol name.
|
|---|
| 91 | *
|
|---|
| 92 | * @return Pointer to a human-readable string.
|
|---|
| 93 | *
|
|---|
| 94 | */
|
|---|
| 95 | const char *symtab_fmt_name_lookup(uintptr_t addr)
|
|---|
| 96 | {
|
|---|
| 97 | const char *name;
|
|---|
| 98 | int rc = symtab_name_lookup(addr, &name, NULL);
|
|---|
| 99 |
|
|---|
| 100 | switch (rc) {
|
|---|
| 101 | case EOK:
|
|---|
| 102 | return name;
|
|---|
| 103 | case ENOENT:
|
|---|
| 104 | return "unknown";
|
|---|
| 105 | default:
|
|---|
| 106 | return "N/A";
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | #ifdef CONFIG_SYMTAB
|
|---|
| 111 |
|
|---|
| 112 | /** Find symbols that match the parameter forward and print them.
|
|---|
| 113 | *
|
|---|
| 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 | *
|
|---|
| 119 | */
|
|---|
| 120 | static const char *symtab_search_one(const char *name, size_t *startpos)
|
|---|
| 121 | {
|
|---|
| 122 | size_t namelen = str_length(name);
|
|---|
| 123 |
|
|---|
| 124 | size_t pos;
|
|---|
| 125 | for (pos = *startpos; symbol_table[pos].address_le; pos++) {
|
|---|
| 126 | const char *curname = symbol_table[pos].symbol_name;
|
|---|
| 127 |
|
|---|
| 128 | /* Find a ':' in curname */
|
|---|
| 129 | const char *colon = str_chr(curname, ':');
|
|---|
| 130 | if (colon == NULL)
|
|---|
| 131 | continue;
|
|---|
| 132 |
|
|---|
| 133 | if (str_length(curname) < namelen)
|
|---|
| 134 | continue;
|
|---|
| 135 |
|
|---|
| 136 | if (str_lcmp(name, curname, namelen) == 0) {
|
|---|
| 137 | *startpos = pos;
|
|---|
| 138 | return (curname + str_lsize(curname, namelen));
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | return NULL;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | #endif
|
|---|
| 146 |
|
|---|
| 147 | /** Return address that corresponds to the entry.
|
|---|
| 148 | *
|
|---|
| 149 | * Search symbol table, and if there is one match, return it
|
|---|
| 150 | *
|
|---|
| 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.
|
|---|
| 156 | *
|
|---|
| 157 | */
|
|---|
| 158 | int symtab_addr_lookup(const char *name, uintptr_t *addr)
|
|---|
| 159 | {
|
|---|
| 160 | #ifdef CONFIG_SYMTAB
|
|---|
| 161 | size_t found = 0;
|
|---|
| 162 | size_t pos = 0;
|
|---|
| 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);
|
|---|
| 168 | found++;
|
|---|
| 169 | }
|
|---|
| 170 | pos++;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | if (found > 1)
|
|---|
| 174 | return EOVERFLOW;
|
|---|
| 175 |
|
|---|
| 176 | if (found < 1)
|
|---|
| 177 | return ENOENT;
|
|---|
| 178 |
|
|---|
| 179 | return EOK;
|
|---|
| 180 |
|
|---|
| 181 | #else
|
|---|
| 182 | return ENOTSUP;
|
|---|
| 183 | #endif
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /** Find symbols that match parameter and print them */
|
|---|
| 187 | void symtab_print_search(const char *name)
|
|---|
| 188 | {
|
|---|
| 189 | #ifdef CONFIG_SYMTAB
|
|---|
| 190 | size_t pos = 0;
|
|---|
| 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;
|
|---|
| 194 | printf("%p: %s\n", (void *) addr, realname);
|
|---|
| 195 | pos++;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | #else
|
|---|
| 199 | printf("No symbol information available.\n");
|
|---|
| 200 | #endif
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /** Symtab completion
|
|---|
| 204 | *
|
|---|
| 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 | *
|
|---|
| 210 | */
|
|---|
| 211 | int symtab_compl(char *input, size_t size)
|
|---|
| 212 | {
|
|---|
| 213 | #ifdef CONFIG_SYMTAB
|
|---|
| 214 | const char *name = input;
|
|---|
| 215 |
|
|---|
| 216 | /* Allow completion of pointers */
|
|---|
| 217 | if ((name[0] == '*') || (name[0] == '&'))
|
|---|
| 218 | name++;
|
|---|
| 219 |
|
|---|
| 220 | /* Do not print all symbols */
|
|---|
| 221 | if (str_length(name) == 0)
|
|---|
| 222 | return 0;
|
|---|
| 223 |
|
|---|
| 224 | size_t found = 0;
|
|---|
| 225 | size_t pos = 0;
|
|---|
| 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)))
|
|---|
| 233 | str_cpy(output, MAX_SYMBOL_NAME, hint);
|
|---|
| 234 |
|
|---|
| 235 | pos++;
|
|---|
| 236 | found++;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | if ((found > 1) && (str_length(output) != 0)) {
|
|---|
| 240 | printf("\n");
|
|---|
| 241 | pos = 0;
|
|---|
| 242 | while (symtab_search_one(name, &pos)) {
|
|---|
| 243 | printf("%s\n", symbol_table[pos].symbol_name);
|
|---|
| 244 | pos++;
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | if (found > 0)
|
|---|
| 249 | str_cpy(input, size, output);
|
|---|
| 250 |
|
|---|
| 251 | return found;
|
|---|
| 252 |
|
|---|
| 253 | #else
|
|---|
| 254 | return 0;
|
|---|
| 255 | #endif
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /** @}
|
|---|
| 259 | */
|
|---|