source: mainline/kernel/generic/src/debug/symtab.c@ 9fe962d6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9fe962d6 was 5d494b3, checked in by Jakub Jermar <jakub@…>, 18 years ago

Each architecture should only announce its endianity.
The conversion macros should be defined only once.

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