source: mainline/generic/src/debug/symtab.c@ dd054bc2

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

Improve Doxygen-comments.

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