Subversion Repositories HelenOS-historic

Rev

Rev 1248 | Rev 1702 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
268 palkovsky 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
 
1264 jermar 29
/**
30
 * @file	symtab.c
31
 * @brief	Kernel symbol resolver.
32
 */
268 palkovsky 33
 
34
#include <symtab.h>
35
#include <typedefs.h>
292 jermar 36
#include <arch/byteorder.h>
582 palkovsky 37
#include <func.h>
38
#include <print.h>
268 palkovsky 39
 
1264 jermar 40
/** Return entry that seems most likely to correspond to argument.
268 palkovsky 41
 *
404 jermar 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.
268 palkovsky 48
 */
49
char * get_symtab_entry(__native addr)
50
{
51
	count_t i;
52
 
292 jermar 53
	for (i=1;symbol_table[i].address_le;++i) {
338 jermar 54
		if (addr < __u64_le2host(symbol_table[i].address_le))
268 palkovsky 55
			break;
56
	}
338 jermar 57
	if (addr >= __u64_le2host(symbol_table[i-1].address_le))
268 palkovsky 58
		return symbol_table[i-1].symbol_name;
59
	return NULL;
60
}
582 palkovsky 61
 
1264 jermar 62
/** Find symbols that match the parameter forward and print them.
601 palkovsky 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
 */
68
static 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
 
582 palkovsky 100
/** Return address that corresponds to the entry
101
 *
601 palkovsky 102
 * Search symbol table, and if there is one match, return it
582 palkovsky 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;
601 palkovsky 110
	__address addr = NULL;
111
	char *hint;
112
	int i;
582 palkovsky 113
 
601 palkovsky 114
	i = 0;
115
	while ((hint=symtab_search_one(name, &i))) {
116
		if (!strlen(hint)) {
117
			addr =  __u64_le2host(symbol_table[i].address_le);
582 palkovsky 118
			found++;
119
		}
601 palkovsky 120
		i++;
582 palkovsky 121
	}
601 palkovsky 122
	if (found > 1)
123
		return ((__address) -1);
124
	return addr;
582 palkovsky 125
}
126
 
601 palkovsky 127
/** Find symbols that match parameter and prints them */
582 palkovsky 128
void symtab_print_search(const char *name)
129
{
130
	int i;
131
	__address addr;
132
	char *realname;
133
 
601 palkovsky 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;
1224 cejka 139
		printf("%.*p: %s\n", sizeof(__address) * 2, addr, realname);
601 palkovsky 140
		i++;
141
	}
142
}
143
 
144
/** Symtab completion
145
 *
1248 jermar 146
 * @param input - Search string, completes to symbol name
601 palkovsky 147
 * @returns - 0 - nothing found, 1 - success, >1 print duplicates 
148
 */
616 palkovsky 149
int symtab_compl(char *input)
601 palkovsky 150
{
151
	char output[MAX_SYMBOL_NAME+1];
152
	int startpos = 0;
153
	char *foundtxt;
154
	int found = 0;
155
	int i;
616 palkovsky 156
	char *name = input;
601 palkovsky 157
 
616 palkovsky 158
	/* Allow completion of pointers  */
159
	if (name[0] == '*' || name[0] == '&')
160
		name++;
161
 
601 palkovsky 162
	/* Do not print everything */
163
	if (!strlen(name))
164
		return 0;
616 palkovsky 165
 
601 palkovsky 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';
582 palkovsky 177
		}
601 palkovsky 178
		found++;
582 palkovsky 179
	}
601 palkovsky 180
	if (!found)
181
		return 0;
182
 
602 palkovsky 183
	if (found > 1 && !strlen(output)) {
601 palkovsky 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++;
189
		}
190
	}
616 palkovsky 191
	strncpy(input, output, MAX_SYMBOL_NAME);
601 palkovsky 192
	return found;
193
 
582 palkovsky 194
}