[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
|
---|
[cf26ba9] | 35 | * @brief Kernel symbol resolver.
|
---|
| 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>
|
---|
[ab08b42] | 44 |
|
---|
[cf26ba9] | 45 | /** Return entry that seems most likely to correspond to argument.
|
---|
[ab08b42] | 46 | *
|
---|
[a783ca4] | 47 | * Return entry that seems most likely to correspond
|
---|
| 48 | * to address passed in the argument.
|
---|
| 49 | *
|
---|
| 50 | * @param addr Address.
|
---|
| 51 | *
|
---|
| 52 | * @return Pointer to respective symbol string on success, NULL otherwise.
|
---|
[ab08b42] | 53 | */
|
---|
[7f1c620] | 54 | char * get_symtab_entry(unative_t addr)
|
---|
[ab08b42] | 55 | {
|
---|
| 56 | count_t i;
|
---|
| 57 |
|
---|
[5d494b3] | 58 | for (i = 1; symbol_table[i].address_le; ++i) {
|
---|
[7f1c620] | 59 | if (addr < uint64_t_le2host(symbol_table[i].address_le))
|
---|
[ab08b42] | 60 | break;
|
---|
| 61 | }
|
---|
[5d494b3] | 62 | if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le))
|
---|
| 63 | return symbol_table[i - 1].symbol_name;
|
---|
[ab08b42] | 64 | return NULL;
|
---|
| 65 | }
|
---|
[6e716a59] | 66 |
|
---|
[cf26ba9] | 67 | /** Find symbols that match the parameter forward and print them.
|
---|
[0c8e692] | 68 | *
|
---|
| 69 | * @param name - search string
|
---|
| 70 | * @param startpos - starting position, changes to found position
|
---|
| 71 | * @return Pointer to the part of string that should be completed or NULL
|
---|
| 72 | */
|
---|
| 73 | static char * symtab_search_one(const char *name, int *startpos)
|
---|
| 74 | {
|
---|
[6c441cf8] | 75 | unsigned int namelen = strlen(name);
|
---|
[0c8e692] | 76 | char *curname;
|
---|
[5d494b3] | 77 | int i, j;
|
---|
[0c8e692] | 78 | int colonoffset = -1;
|
---|
| 79 |
|
---|
[5d494b3] | 80 | for (i = 0; name[i]; i++)
|
---|
[0c8e692] | 81 | if (name[i] == ':') {
|
---|
| 82 | colonoffset = i;
|
---|
| 83 | break;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[5d494b3] | 86 | for (i = *startpos; symbol_table[i].address_le; ++i) {
|
---|
[0c8e692] | 87 | /* Find a ':' in name */
|
---|
| 88 | curname = symbol_table[i].symbol_name;
|
---|
[5d494b3] | 89 | for (j = 0; curname[j] && curname[j] != ':'; j++)
|
---|
[0c8e692] | 90 | ;
|
---|
| 91 | if (!curname[j])
|
---|
| 92 | continue;
|
---|
| 93 | j -= colonoffset;
|
---|
| 94 | curname += j;
|
---|
| 95 | if (strlen(curname) < namelen)
|
---|
| 96 | continue;
|
---|
| 97 | if (strncmp(curname, name, namelen) == 0) {
|
---|
| 98 | *startpos = i;
|
---|
[5d494b3] | 99 | return curname + namelen;
|
---|
[0c8e692] | 100 | }
|
---|
| 101 | }
|
---|
| 102 | return NULL;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[6e716a59] | 105 | /** Return address that corresponds to the entry
|
---|
| 106 | *
|
---|
[0c8e692] | 107 | * Search symbol table, and if there is one match, return it
|
---|
[6e716a59] | 108 | *
|
---|
| 109 | * @param name Name of the symbol
|
---|
| 110 | * @return 0 - Not found, -1 - Duplicate symbol, other - address of symbol
|
---|
| 111 | */
|
---|
[7f1c620] | 112 | uintptr_t get_symbol_addr(const char *name)
|
---|
[6e716a59] | 113 | {
|
---|
| 114 | count_t found = 0;
|
---|
[7f1c620] | 115 | uintptr_t addr = NULL;
|
---|
[0c8e692] | 116 | char *hint;
|
---|
| 117 | int i;
|
---|
[6e716a59] | 118 |
|
---|
[0c8e692] | 119 | i = 0;
|
---|
[5d494b3] | 120 | while ((hint = symtab_search_one(name, &i))) {
|
---|
[0c8e692] | 121 | if (!strlen(hint)) {
|
---|
[7f1c620] | 122 | addr = uint64_t_le2host(symbol_table[i].address_le);
|
---|
[6e716a59] | 123 | found++;
|
---|
| 124 | }
|
---|
[0c8e692] | 125 | i++;
|
---|
[6e716a59] | 126 | }
|
---|
[0c8e692] | 127 | if (found > 1)
|
---|
[7f1c620] | 128 | return ((uintptr_t) -1);
|
---|
[0c8e692] | 129 | return addr;
|
---|
[6e716a59] | 130 | }
|
---|
| 131 |
|
---|
[0c8e692] | 132 | /** Find symbols that match parameter and prints them */
|
---|
[6e716a59] | 133 | void symtab_print_search(const char *name)
|
---|
| 134 | {
|
---|
| 135 | int i;
|
---|
[7f1c620] | 136 | uintptr_t addr;
|
---|
[6e716a59] | 137 | char *realname;
|
---|
| 138 |
|
---|
[0c8e692] | 139 |
|
---|
| 140 | i = 0;
|
---|
| 141 | while (symtab_search_one(name, &i)) {
|
---|
[7f1c620] | 142 | addr = uint64_t_le2host(symbol_table[i].address_le);
|
---|
[0c8e692] | 143 | realname = symbol_table[i].symbol_name;
|
---|
[8ed4014] | 144 | printf("%p: %s\n", addr, realname);
|
---|
[0c8e692] | 145 | i++;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /** Symtab completion
|
---|
| 150 | *
|
---|
[9179d0a] | 151 | * @param input - Search string, completes to symbol name
|
---|
[0c8e692] | 152 | * @returns - 0 - nothing found, 1 - success, >1 print duplicates
|
---|
| 153 | */
|
---|
[91ef0d95] | 154 | int symtab_compl(char *input)
|
---|
[0c8e692] | 155 | {
|
---|
[5d494b3] | 156 | char output[MAX_SYMBOL_NAME + 1];
|
---|
[0c8e692] | 157 | int startpos = 0;
|
---|
| 158 | char *foundtxt;
|
---|
| 159 | int found = 0;
|
---|
| 160 | int i;
|
---|
[91ef0d95] | 161 | char *name = input;
|
---|
| 162 |
|
---|
| 163 | /* Allow completion of pointers */
|
---|
| 164 | if (name[0] == '*' || name[0] == '&')
|
---|
| 165 | name++;
|
---|
[0c8e692] | 166 |
|
---|
| 167 | /* Do not print everything */
|
---|
| 168 | if (!strlen(name))
|
---|
| 169 | return 0;
|
---|
[91ef0d95] | 170 |
|
---|
[0c8e692] | 171 |
|
---|
| 172 | output[0] = '\0';
|
---|
| 173 |
|
---|
| 174 | while ((foundtxt = symtab_search_one(name, &startpos))) {
|
---|
| 175 | startpos++;
|
---|
| 176 | if (!found)
|
---|
[5d494b3] | 177 | strncpy(output, foundtxt, strlen(foundtxt) + 1);
|
---|
[0c8e692] | 178 | else {
|
---|
[5d494b3] | 179 | for (i = 0; output[i] && foundtxt[i] &&
|
---|
| 180 | output[i] == foundtxt[i]; i++)
|
---|
[0c8e692] | 181 | ;
|
---|
| 182 | output[i] = '\0';
|
---|
| 183 | }
|
---|
| 184 | found++;
|
---|
| 185 | }
|
---|
| 186 | if (!found)
|
---|
| 187 | return 0;
|
---|
| 188 |
|
---|
[3550c393] | 189 | if (found > 1 && !strlen(output)) {
|
---|
[0c8e692] | 190 | printf("\n");
|
---|
| 191 | startpos = 0;
|
---|
| 192 | while ((foundtxt = symtab_search_one(name, &startpos))) {
|
---|
| 193 | printf("%s\n", symbol_table[startpos].symbol_name);
|
---|
| 194 | startpos++;
|
---|
[6e716a59] | 195 | }
|
---|
| 196 | }
|
---|
[91ef0d95] | 197 | strncpy(input, output, MAX_SYMBOL_NAME);
|
---|
[0c8e692] | 198 | return found;
|
---|
| 199 |
|
---|
[6e716a59] | 200 | }
|
---|
[b45c443] | 201 |
|
---|
[06e1e95] | 202 | /** @}
|
---|
[b45c443] | 203 | */
|
---|