source: mainline/kernel/generic/src/debug/symtab.c@ 0b5203b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0b5203b was bab75df6, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Let kernel code get printf via the standard stdio header. Clean up unused includes.

  • Property mode set to 100644
File size: 5.8 KB
Line 
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
29/** @addtogroup kernel_generic_debug
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Kernel symbol resolver.
36 */
37
38#include <symtab.h>
39#include <byteorder.h>
40#include <str.h>
41#include <stdio.h>
42#include <typedefs.h>
43#include <errno.h>
44#include <console/prompt.h>
45
46/** Get name of a symbol that seems most likely to correspond to address.
47 *
48 * @param addr Address.
49 * @param name Place to store pointer to the symbol name.
50 * @param offset Place to store offset from the symbol address.
51 *
52 * @return Zero on success or an error code, ENOENT if not found,
53 * ENOTSUP if symbol table not available.
54 *
55 */
56errno_t symtab_name_lookup(uintptr_t addr, const char **name, uintptr_t *offset)
57{
58#ifdef CONFIG_SYMTAB
59 size_t i;
60
61 for (i = 1; symbol_table[i].address_le; i++) {
62 if (addr < uint64_t_le2host(symbol_table[i].address_le))
63 break;
64 }
65
66 if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) {
67 *name = symbol_table[i - 1].symbol_name;
68 if (offset)
69 *offset = addr -
70 uint64_t_le2host(symbol_table[i - 1].address_le);
71 return EOK;
72 }
73
74 *name = NULL;
75 return ENOENT;
76
77#else
78 *name = NULL;
79 return ENOTSUP;
80#endif
81}
82
83/** Lookup symbol by address and format for display.
84 *
85 * Returns name of closest corresponding symbol,
86 * "unknown" if none exists and "N/A" if no symbol
87 * information is available.
88 *
89 * @param addr Address.
90 * @param name Place to store pointer to the symbol name.
91 *
92 * @return Pointer to a human-readable string.
93 *
94 */
95const char *symtab_fmt_name_lookup(uintptr_t addr)
96{
97 const char *name;
98 errno_t rc = symtab_name_lookup(addr, &name, NULL);
99
100 switch (rc) {
101 case EOK:
102 return name;
103 case ENOENT:
104 return "unknown";
105 default:
106 return "N/A";
107 }
108}
109
110#ifdef CONFIG_SYMTAB
111
112/** Find symbols that match the parameter forward and print them.
113 *
114 * @param name Search string
115 * @param startpos Starting position, changes to found position
116 *
117 * @return Pointer to the part of string that should be completed or NULL.
118 *
119 */
120static const char *symtab_search_one(const char *name, size_t *startpos)
121{
122 size_t namelen = str_length(name);
123
124 size_t pos;
125 for (pos = *startpos; symbol_table[pos].address_le; pos++) {
126 const char *curname = symbol_table[pos].symbol_name;
127
128 /* Find a ':' in curname */
129 const char *colon = str_chr(curname, ':');
130 if (colon == NULL)
131 continue;
132
133 if (str_length(curname) < namelen)
134 continue;
135
136 if (str_lcmp(name, curname, namelen) == 0) {
137 *startpos = pos;
138 return (curname + str_lsize(curname, namelen));
139 }
140 }
141
142 return NULL;
143}
144
145#endif
146
147/** Return address that corresponds to the entry.
148 *
149 * Search symbol table, and if there is one match, return it
150 *
151 * @param name Name of the symbol
152 * @param addr Place to store symbol address
153 *
154 * @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate
155 * symbol, ENOTSUP - no symbol information available.
156 *
157 */
158errno_t symtab_addr_lookup(const char *name, uintptr_t *addr)
159{
160#ifdef CONFIG_SYMTAB
161 size_t found = 0;
162 size_t pos = 0;
163 const char *hint;
164
165 while ((hint = symtab_search_one(name, &pos))) {
166 if (str_length(hint) == 0) {
167 *addr = uint64_t_le2host(symbol_table[pos].address_le);
168 found++;
169 }
170 pos++;
171 }
172
173 if (found > 1)
174 return EOVERFLOW;
175
176 if (found < 1)
177 return ENOENT;
178
179 return EOK;
180
181#else
182 return ENOTSUP;
183#endif
184}
185
186/** Find symbols that match parameter and print them */
187void symtab_print_search(const char *name)
188{
189#ifdef CONFIG_SYMTAB
190 size_t pos = 0;
191 while (symtab_search_one(name, &pos)) {
192 uintptr_t addr = uint64_t_le2host(symbol_table[pos].address_le);
193 char *realname = symbol_table[pos].symbol_name;
194 printf("%p: %s\n", (void *) addr, realname);
195 pos++;
196 }
197
198#else
199 printf("No symbol information available.\n");
200#endif
201}
202
203/** Symtab completion enum, see kernel/generic/include/kconsole.h */
204const char *symtab_hints_enum(const char *input, const char **help,
205 void **ctx)
206{
207#ifdef CONFIG_SYMTAB
208 size_t len = str_length(input);
209 struct symtab_entry **entry = (struct symtab_entry **)ctx;
210
211 if (*entry == NULL)
212 *entry = symbol_table;
213
214 for (; (*entry)->address_le; (*entry)++) {
215 const char *curname = (*entry)->symbol_name;
216
217 /* Find a ':' in curname */
218 const char *colon = str_chr(curname, ':');
219 if (colon == NULL)
220 continue;
221
222 if (str_length(curname) < len)
223 continue;
224
225 if (str_lcmp(input, curname, len) == 0) {
226 (*entry)++;
227 if (help)
228 *help = NULL;
229 return (curname + str_lsize(curname, len));
230 }
231 }
232
233 return NULL;
234
235#else
236 return NULL;
237#endif
238}
239
240/** @}
241 */
Note: See TracBrowser for help on using the repository browser.