Rev 4137 | Rev 4215 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4137 | Rev 4207 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2005 Ondrej Palkovsky |
2 | * Copyright (c) 2005 Ondrej Palkovsky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericdebug |
29 | /** @addtogroup genericdebug |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Kernel symbol resolver. |
35 | * @brief Kernel symbol resolver. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <symtab.h> |
38 | #include <symtab.h> |
39 | #include <byteorder.h> |
39 | #include <byteorder.h> |
40 | #include <string.h> |
40 | #include <string.h> |
41 | #include <print.h> |
41 | #include <print.h> |
42 | #include <arch/types.h> |
42 | #include <arch/types.h> |
43 | #include <typedefs.h> |
43 | #include <typedefs.h> |
44 | #include <errno.h> |
44 | #include <errno.h> |
45 | 45 | ||
46 | /** Get name of symbol that seems most likely to correspond to address. |
46 | /** Get name of symbol that seems most likely to correspond to address. |
47 | * |
47 | * |
48 | * @param addr Address. |
48 | * @param addr Address. |
49 | * @param name Place to store pointer to the symbol name. |
49 | * @param name Place to store pointer to the symbol name. |
50 | * |
50 | * |
51 | * @return Zero on success or negative error code, ENOENT if not found, |
51 | * @return Zero on success or negative error code, ENOENT if not found, |
52 | * ENOTSUP if symbol table not available. |
52 | * ENOTSUP if symbol table not available. |
53 | */ |
53 | */ |
54 | int symtab_name_lookup(unative_t addr, char **name) |
54 | int symtab_name_lookup(unative_t addr, char **name) |
55 | { |
55 | { |
56 | #ifdef CONFIG_SYMTAB |
56 | #ifdef CONFIG_SYMTAB |
57 | count_t i; |
57 | count_t i; |
58 | 58 | ||
59 | for (i = 1; symbol_table[i].address_le; ++i) { |
59 | for (i = 1; symbol_table[i].address_le; ++i) { |
60 | if (addr < uint64_t_le2host(symbol_table[i].address_le)) |
60 | if (addr < uint64_t_le2host(symbol_table[i].address_le)) |
61 | break; |
61 | break; |
62 | } |
62 | } |
63 | if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) { |
63 | if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) { |
64 | *name = symbol_table[i - 1].symbol_name; |
64 | *name = symbol_table[i - 1].symbol_name; |
65 | return EOK; |
65 | return EOK; |
66 | } |
66 | } |
67 | 67 | ||
68 | *name = NULL; |
68 | *name = NULL; |
69 | return ENOENT; |
69 | return ENOENT; |
70 | #else |
70 | #else |
71 | *name = NULL; |
71 | *name = NULL; |
72 | return ENOTSUP; |
72 | return ENOTSUP; |
73 | #endif |
73 | #endif |
74 | } |
74 | } |
75 | 75 | ||
76 | /** Lookup symbol by address and format for display. |
76 | /** Lookup symbol by address and format for display. |
77 | * |
77 | * |
78 | * Returns name of closest corresponding symbol, "Not found" if none exists |
78 | * Returns name of closest corresponding symbol, "Not found" if none exists |
79 | * or "N/A" if no symbol information is available. |
79 | * or "N/A" if no symbol information is available. |
80 | * |
80 | * |
81 | * @param addr Address. |
81 | * @param addr Address. |
82 | * @param name Place to store pointer to the symbol name. |
82 | * @param name Place to store pointer to the symbol name. |
83 | * |
83 | * |
84 | * @return Pointer to a human-readable string. |
84 | * @return Pointer to a human-readable string. |
85 | */ |
85 | */ |
86 | char *symtab_fmt_name_lookup(unative_t addr) |
86 | char *symtab_fmt_name_lookup(unative_t addr) |
87 | { |
87 | { |
88 | int rc; |
88 | int rc; |
89 | char *name; |
89 | char *name; |
90 | 90 | ||
91 | rc = symtab_name_lookup(addr, &name); |
91 | rc = symtab_name_lookup(addr, &name); |
92 | switch (rc) { |
92 | switch (rc) { |
93 | case EOK: return name; |
93 | case EOK: return name; |
94 | case ENOENT: return "Not found"; |
94 | case ENOENT: return "Not found"; |
95 | default: return "N/A"; |
95 | default: return "N/A"; |
96 | } |
96 | } |
97 | } |
97 | } |
98 | 98 | ||
99 | #ifdef CONFIG_SYMTAB |
99 | #ifdef CONFIG_SYMTAB |
100 | 100 | ||
101 | /** Find symbols that match the parameter forward and print them. |
101 | /** Find symbols that match the parameter forward and print them. |
102 | * |
102 | * |
103 | * @param name - search string |
103 | * @param name - search string |
104 | * @param startpos - starting position, changes to found position |
104 | * @param startpos - starting position, changes to found position |
105 | * @return Pointer to the part of string that should be completed or NULL |
105 | * @return Pointer to the part of string that should be completed or NULL |
106 | */ |
106 | */ |
107 | static char * symtab_search_one(const char *name, int *startpos) |
107 | static char * symtab_search_one(const char *name, int *startpos) |
108 | { |
108 | { |
109 | unsigned int namelen = strlen(name); |
109 | unsigned int namelen = str_size(name); |
110 | char *curname; |
110 | char *curname; |
111 | int i, j; |
111 | int i, j; |
112 | int colonoffset = -1; |
112 | int colonoffset = -1; |
113 | 113 | ||
114 | for (i = 0; name[i]; i++) |
114 | for (i = 0; name[i]; i++) |
115 | if (name[i] == ':') { |
115 | if (name[i] == ':') { |
116 | colonoffset = i; |
116 | colonoffset = i; |
117 | break; |
117 | break; |
118 | } |
118 | } |
119 | 119 | ||
120 | for (i = *startpos; symbol_table[i].address_le; ++i) { |
120 | for (i = *startpos; symbol_table[i].address_le; ++i) { |
121 | /* Find a ':' in name */ |
121 | /* Find a ':' in name */ |
122 | curname = symbol_table[i].symbol_name; |
122 | curname = symbol_table[i].symbol_name; |
123 | for (j = 0; curname[j] && curname[j] != ':'; j++) |
123 | for (j = 0; curname[j] && curname[j] != ':'; j++) |
124 | ; |
124 | ; |
125 | if (!curname[j]) |
125 | if (!curname[j]) |
126 | continue; |
126 | continue; |
127 | j -= colonoffset; |
127 | j -= colonoffset; |
128 | curname += j; |
128 | curname += j; |
129 | if (strlen(curname) < namelen) |
129 | if (str_size(curname) < namelen) |
130 | continue; |
130 | continue; |
131 | if (strncmp(curname, name, namelen) == 0) { |
131 | if (strncmp(curname, name, namelen) == 0) { |
132 | *startpos = i; |
132 | *startpos = i; |
133 | return curname + namelen; |
133 | return curname + namelen; |
134 | } |
134 | } |
135 | } |
135 | } |
136 | return NULL; |
136 | return NULL; |
137 | } |
137 | } |
138 | 138 | ||
139 | #endif |
139 | #endif |
140 | 140 | ||
141 | /** Return address that corresponds to the entry |
141 | /** Return address that corresponds to the entry |
142 | * |
142 | * |
143 | * Search symbol table, and if there is one match, return it |
143 | * Search symbol table, and if there is one match, return it |
144 | * |
144 | * |
145 | * @param name Name of the symbol |
145 | * @param name Name of the symbol |
146 | * @param addr Place to store symbol address |
146 | * @param addr Place to store symbol address |
147 | * |
147 | * |
148 | * @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate |
148 | * @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate |
149 | * symbol, ENOTSUP - no symbol information available. |
149 | * symbol, ENOTSUP - no symbol information available. |
150 | */ |
150 | */ |
151 | int symtab_addr_lookup(const char *name, uintptr_t *addr) |
151 | int symtab_addr_lookup(const char *name, uintptr_t *addr) |
152 | { |
152 | { |
153 | #ifdef CONFIG_SYMTAB |
153 | #ifdef CONFIG_SYMTAB |
154 | count_t found = 0; |
154 | count_t found = 0; |
155 | char *hint; |
155 | char *hint; |
156 | int i; |
156 | int i; |
157 | 157 | ||
158 | i = 0; |
158 | i = 0; |
159 | while ((hint = symtab_search_one(name, &i))) { |
159 | while ((hint = symtab_search_one(name, &i))) { |
160 | if (!strlen(hint)) { |
160 | if (!str_size(hint)) { |
161 | *addr = uint64_t_le2host(symbol_table[i].address_le); |
161 | *addr = uint64_t_le2host(symbol_table[i].address_le); |
162 | found++; |
162 | found++; |
163 | } |
163 | } |
164 | i++; |
164 | i++; |
165 | } |
165 | } |
166 | if (found > 1) |
166 | if (found > 1) |
167 | return EOVERFLOW; |
167 | return EOVERFLOW; |
168 | if (found < 1) |
168 | if (found < 1) |
169 | return ENOENT; |
169 | return ENOENT; |
170 | return EOK; |
170 | return EOK; |
171 | #else |
171 | #else |
172 | return ENOTSUP; |
172 | return ENOTSUP; |
173 | #endif |
173 | #endif |
174 | } |
174 | } |
175 | 175 | ||
176 | /** Find symbols that match parameter and prints them */ |
176 | /** Find symbols that match parameter and prints them */ |
177 | void symtab_print_search(const char *name) |
177 | void symtab_print_search(const char *name) |
178 | { |
178 | { |
179 | #ifdef CONFIG_SYMTAB |
179 | #ifdef CONFIG_SYMTAB |
180 | int i; |
180 | int i; |
181 | uintptr_t addr; |
181 | uintptr_t addr; |
182 | char *realname; |
182 | char *realname; |
183 | 183 | ||
184 | 184 | ||
185 | i = 0; |
185 | i = 0; |
186 | while (symtab_search_one(name, &i)) { |
186 | while (symtab_search_one(name, &i)) { |
187 | addr = uint64_t_le2host(symbol_table[i].address_le); |
187 | addr = uint64_t_le2host(symbol_table[i].address_le); |
188 | realname = symbol_table[i].symbol_name; |
188 | realname = symbol_table[i].symbol_name; |
189 | printf("%p: %s\n", addr, realname); |
189 | printf("%p: %s\n", addr, realname); |
190 | i++; |
190 | i++; |
191 | } |
191 | } |
192 | #else |
192 | #else |
193 | printf("No symbol information available.\n"); |
193 | printf("No symbol information available.\n"); |
194 | #endif |
194 | #endif |
195 | } |
195 | } |
196 | 196 | ||
197 | /** Symtab completion |
197 | /** Symtab completion |
198 | * |
198 | * |
199 | * @param input - Search string, completes to symbol name |
199 | * @param input - Search string, completes to symbol name |
200 | * @returns - 0 - nothing found, 1 - success, >1 print duplicates |
200 | * @returns - 0 - nothing found, 1 - success, >1 print duplicates |
201 | */ |
201 | */ |
202 | int symtab_compl(char *input) |
202 | int symtab_compl(char *input) |
203 | { |
203 | { |
204 | #ifdef CONFIG_SYMTAB |
204 | #ifdef CONFIG_SYMTAB |
205 | char output[MAX_SYMBOL_NAME + 1]; |
205 | char output[MAX_SYMBOL_NAME + 1]; |
206 | int startpos = 0; |
206 | int startpos = 0; |
207 | char *foundtxt; |
207 | char *foundtxt; |
208 | int found = 0; |
208 | int found = 0; |
209 | int i; |
209 | int i; |
210 | char *name = input; |
210 | char *name = input; |
211 | 211 | ||
212 | /* Allow completion of pointers */ |
212 | /* Allow completion of pointers */ |
213 | if (name[0] == '*' || name[0] == '&') |
213 | if (name[0] == '*' || name[0] == '&') |
214 | name++; |
214 | name++; |
215 | 215 | ||
216 | /* Do not print everything */ |
216 | /* Do not print everything */ |
217 | if (!strlen(name)) |
217 | if (!str_size(name)) |
218 | return 0; |
218 | return 0; |
219 | 219 | ||
220 | 220 | ||
221 | output[0] = '\0'; |
221 | output[0] = '\0'; |
222 | 222 | ||
223 | while ((foundtxt = symtab_search_one(name, &startpos))) { |
223 | while ((foundtxt = symtab_search_one(name, &startpos))) { |
224 | startpos++; |
224 | startpos++; |
225 | if (!found) |
225 | if (!found) |
226 | strncpy(output, foundtxt, strlen(foundtxt) + 1); |
226 | strncpy(output, foundtxt, str_size(foundtxt) + 1); |
227 | else { |
227 | else { |
228 | for (i = 0; output[i] && foundtxt[i] && |
228 | for (i = 0; output[i] && foundtxt[i] && |
229 | output[i] == foundtxt[i]; i++) |
229 | output[i] == foundtxt[i]; i++) |
230 | ; |
230 | ; |
231 | output[i] = '\0'; |
231 | output[i] = '\0'; |
232 | } |
232 | } |
233 | found++; |
233 | found++; |
234 | } |
234 | } |
235 | if (!found) |
235 | if (!found) |
236 | return 0; |
236 | return 0; |
237 | 237 | ||
238 | if (found > 1 && !strlen(output)) { |
238 | if (found > 1 && !str_size(output)) { |
239 | printf("\n"); |
239 | printf("\n"); |
240 | startpos = 0; |
240 | startpos = 0; |
241 | while ((foundtxt = symtab_search_one(name, &startpos))) { |
241 | while ((foundtxt = symtab_search_one(name, &startpos))) { |
242 | printf("%s\n", symbol_table[startpos].symbol_name); |
242 | printf("%s\n", symbol_table[startpos].symbol_name); |
243 | startpos++; |
243 | startpos++; |
244 | } |
244 | } |
245 | } |
245 | } |
246 | strncpy(input, output, MAX_SYMBOL_NAME); |
246 | strncpy(input, output, MAX_SYMBOL_NAME); |
247 | return found; |
247 | return found; |
248 | #else |
248 | #else |
249 | return 0; |
249 | return 0; |
250 | #endif |
250 | #endif |
251 | } |
251 | } |
252 | 252 | ||
253 | /** @} |
253 | /** @} |
254 | */ |
254 | */ |
255 | 255 |