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