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