generic swap(): iphase: rename swap() to swap_byte_order()
[linux-2.6/mini2440.git] / kernel / kallsyms.c
blobe694afa0eb8c440e2184166ed8669ca08950103e
1 /*
2 * kallsyms.c: in-kernel printing of symbolic oopses and stack traces.
4 * Rewritten and vastly simplified by Rusty Russell for in-kernel
5 * module loader:
6 * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
8 * ChangeLog:
10 * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
11 * Changed the compression method from stem compression to "table lookup"
12 * compression (see scripts/kallsyms.c for a more complete description)
14 #include <linux/kallsyms.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/seq_file.h>
18 #include <linux/fs.h>
19 #include <linux/err.h>
20 #include <linux/proc_fs.h>
21 #include <linux/sched.h> /* for cond_resched */
22 #include <linux/mm.h>
23 #include <linux/ctype.h>
25 #include <asm/sections.h>
27 #ifdef CONFIG_KALLSYMS_ALL
28 #define all_var 1
29 #else
30 #define all_var 0
31 #endif
33 extern const unsigned long kallsyms_addresses[];
34 extern const u8 kallsyms_names[];
36 /* tell the compiler that the count isn't in the small data section if the arch
37 * has one (eg: FRV)
39 extern const unsigned long kallsyms_num_syms
40 __attribute__((__section__(".rodata")));
42 extern const u8 kallsyms_token_table[];
43 extern const u16 kallsyms_token_index[];
45 extern const unsigned long kallsyms_markers[];
47 static inline int is_kernel_inittext(unsigned long addr)
49 if (addr >= (unsigned long)_sinittext
50 && addr <= (unsigned long)_einittext)
51 return 1;
52 return 0;
55 static inline int is_kernel_text(unsigned long addr)
57 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_etext)
58 return 1;
59 return in_gate_area_no_task(addr);
62 static inline int is_kernel(unsigned long addr)
64 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
65 return 1;
66 return in_gate_area_no_task(addr);
69 static int is_ksym_addr(unsigned long addr)
71 if (all_var)
72 return is_kernel(addr);
74 return is_kernel_text(addr) || is_kernel_inittext(addr);
77 /* expand a compressed symbol data into the resulting uncompressed string,
78 given the offset to where the symbol is in the compressed stream */
79 static unsigned int kallsyms_expand_symbol(unsigned int off, char *result)
81 int len, skipped_first = 0;
82 const u8 *tptr, *data;
84 /* get the compressed symbol length from the first symbol byte */
85 data = &kallsyms_names[off];
86 len = *data;
87 data++;
89 /* update the offset to return the offset for the next symbol on
90 * the compressed stream */
91 off += len + 1;
93 /* for every byte on the compressed symbol data, copy the table
94 entry for that byte */
95 while(len) {
96 tptr = &kallsyms_token_table[ kallsyms_token_index[*data] ];
97 data++;
98 len--;
100 while (*tptr) {
101 if(skipped_first) {
102 *result = *tptr;
103 result++;
104 } else
105 skipped_first = 1;
106 tptr++;
110 *result = '\0';
112 /* return to offset to the next symbol */
113 return off;
116 /* get symbol type information. This is encoded as a single char at the
117 * begining of the symbol name */
118 static char kallsyms_get_symbol_type(unsigned int off)
120 /* get just the first code, look it up in the token table, and return the
121 * first char from this token */
122 return kallsyms_token_table[ kallsyms_token_index[ kallsyms_names[off+1] ] ];
126 /* find the offset on the compressed stream given and index in the
127 * kallsyms array */
128 static unsigned int get_symbol_offset(unsigned long pos)
130 const u8 *name;
131 int i;
133 /* use the closest marker we have. We have markers every 256 positions,
134 * so that should be close enough */
135 name = &kallsyms_names[ kallsyms_markers[pos>>8] ];
137 /* sequentially scan all the symbols up to the point we're searching for.
138 * Every symbol is stored in a [<len>][<len> bytes of data] format, so we
139 * just need to add the len to the current pointer for every symbol we
140 * wish to skip */
141 for(i = 0; i < (pos&0xFF); i++)
142 name = name + (*name) + 1;
144 return name - kallsyms_names;
147 /* Lookup the address for this symbol. Returns 0 if not found. */
148 unsigned long kallsyms_lookup_name(const char *name)
150 char namebuf[KSYM_NAME_LEN];
151 unsigned long i;
152 unsigned int off;
154 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
155 off = kallsyms_expand_symbol(off, namebuf);
157 if (strcmp(namebuf, name) == 0)
158 return kallsyms_addresses[i];
160 return module_kallsyms_lookup_name(name);
163 static unsigned long get_symbol_pos(unsigned long addr,
164 unsigned long *symbolsize,
165 unsigned long *offset)
167 unsigned long symbol_start = 0, symbol_end = 0;
168 unsigned long i, low, high, mid;
170 /* do a binary search on the sorted kallsyms_addresses array */
171 low = 0;
172 high = kallsyms_num_syms;
174 while (high - low > 1) {
175 mid = low + (high - low) / 2;
176 if (kallsyms_addresses[mid] <= addr)
177 low = mid;
178 else
179 high = mid;
183 * search for the first aliased symbol. Aliased
184 * symbols are symbols with the same address
186 while (low && kallsyms_addresses[low-1] == kallsyms_addresses[low])
187 --low;
189 symbol_start = kallsyms_addresses[low];
191 /* Search for next non-aliased symbol */
192 for (i = low + 1; i < kallsyms_num_syms; i++) {
193 if (kallsyms_addresses[i] > symbol_start) {
194 symbol_end = kallsyms_addresses[i];
195 break;
199 /* if we found no next symbol, we use the end of the section */
200 if (!symbol_end) {
201 if (is_kernel_inittext(addr))
202 symbol_end = (unsigned long)_einittext;
203 else if (all_var)
204 symbol_end = (unsigned long)_end;
205 else
206 symbol_end = (unsigned long)_etext;
209 if (symbolsize)
210 *symbolsize = symbol_end - symbol_start;
211 if (offset)
212 *offset = addr - symbol_start;
214 return low;
218 * Lookup an address but don't bother to find any names.
220 int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
221 unsigned long *offset)
223 char namebuf[KSYM_NAME_LEN];
224 if (is_ksym_addr(addr))
225 return !!get_symbol_pos(addr, symbolsize, offset);
227 return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf);
231 * Lookup an address
232 * - modname is set to NULL if it's in the kernel
233 * - we guarantee that the returned name is valid until we reschedule even if
234 * it resides in a module
235 * - we also guarantee that modname will be valid until rescheduled
237 const char *kallsyms_lookup(unsigned long addr,
238 unsigned long *symbolsize,
239 unsigned long *offset,
240 char **modname, char *namebuf)
242 namebuf[KSYM_NAME_LEN - 1] = 0;
243 namebuf[0] = 0;
245 if (is_ksym_addr(addr)) {
246 unsigned long pos;
248 pos = get_symbol_pos(addr, symbolsize, offset);
249 /* Grab name */
250 kallsyms_expand_symbol(get_symbol_offset(pos), namebuf);
251 if (modname)
252 *modname = NULL;
253 return namebuf;
256 /* see if it's in a module */
257 return module_address_lookup(addr, symbolsize, offset, modname,
258 namebuf);
261 int lookup_symbol_name(unsigned long addr, char *symname)
263 symname[0] = '\0';
264 symname[KSYM_NAME_LEN - 1] = '\0';
266 if (is_ksym_addr(addr)) {
267 unsigned long pos;
269 pos = get_symbol_pos(addr, NULL, NULL);
270 /* Grab name */
271 kallsyms_expand_symbol(get_symbol_offset(pos), symname);
272 return 0;
274 /* see if it's in a module */
275 return lookup_module_symbol_name(addr, symname);
278 int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
279 unsigned long *offset, char *modname, char *name)
281 name[0] = '\0';
282 name[KSYM_NAME_LEN - 1] = '\0';
284 if (is_ksym_addr(addr)) {
285 unsigned long pos;
287 pos = get_symbol_pos(addr, size, offset);
288 /* Grab name */
289 kallsyms_expand_symbol(get_symbol_offset(pos), name);
290 modname[0] = '\0';
291 return 0;
293 /* see if it's in a module */
294 return lookup_module_symbol_attrs(addr, size, offset, modname, name);
297 /* Look up a kernel symbol and return it in a text buffer. */
298 int sprint_symbol(char *buffer, unsigned long address)
300 char *modname;
301 const char *name;
302 unsigned long offset, size;
303 int len;
305 name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
306 if (!name)
307 return sprintf(buffer, "0x%lx", address);
309 if (name != buffer)
310 strcpy(buffer, name);
311 len = strlen(buffer);
312 buffer += len;
314 if (modname)
315 len += sprintf(buffer, "+%#lx/%#lx [%s]",
316 offset, size, modname);
317 else
318 len += sprintf(buffer, "+%#lx/%#lx", offset, size);
320 return len;
323 /* Look up a kernel symbol and print it to the kernel messages. */
324 void __print_symbol(const char *fmt, unsigned long address)
326 char buffer[KSYM_SYMBOL_LEN];
328 sprint_symbol(buffer, address);
330 printk(fmt, buffer);
333 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
334 struct kallsym_iter
336 loff_t pos;
337 unsigned long value;
338 unsigned int nameoff; /* If iterating in core kernel symbols */
339 char type;
340 char name[KSYM_NAME_LEN];
341 char module_name[MODULE_NAME_LEN];
342 int exported;
345 static int get_ksymbol_mod(struct kallsym_iter *iter)
347 if (module_get_kallsym(iter->pos - kallsyms_num_syms, &iter->value,
348 &iter->type, iter->name, iter->module_name,
349 &iter->exported) < 0)
350 return 0;
351 return 1;
354 /* Returns space to next name. */
355 static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
357 unsigned off = iter->nameoff;
359 iter->module_name[0] = '\0';
360 iter->value = kallsyms_addresses[iter->pos];
362 iter->type = kallsyms_get_symbol_type(off);
364 off = kallsyms_expand_symbol(off, iter->name);
366 return off - iter->nameoff;
369 static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
371 iter->name[0] = '\0';
372 iter->nameoff = get_symbol_offset(new_pos);
373 iter->pos = new_pos;
376 /* Returns false if pos at or past end of file. */
377 static int update_iter(struct kallsym_iter *iter, loff_t pos)
379 /* Module symbols can be accessed randomly. */
380 if (pos >= kallsyms_num_syms) {
381 iter->pos = pos;
382 return get_ksymbol_mod(iter);
385 /* If we're not on the desired position, reset to new position. */
386 if (pos != iter->pos)
387 reset_iter(iter, pos);
389 iter->nameoff += get_ksymbol_core(iter);
390 iter->pos++;
392 return 1;
395 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
397 (*pos)++;
399 if (!update_iter(m->private, *pos))
400 return NULL;
401 return p;
404 static void *s_start(struct seq_file *m, loff_t *pos)
406 if (!update_iter(m->private, *pos))
407 return NULL;
408 return m->private;
411 static void s_stop(struct seq_file *m, void *p)
415 static int s_show(struct seq_file *m, void *p)
417 struct kallsym_iter *iter = m->private;
419 /* Some debugging symbols have no name. Ignore them. */
420 if (!iter->name[0])
421 return 0;
423 if (iter->module_name[0]) {
424 char type;
426 /* Label it "global" if it is exported,
427 * "local" if not exported. */
428 type = iter->exported ? toupper(iter->type) :
429 tolower(iter->type);
430 seq_printf(m, "%0*lx %c %s\t[%s]\n",
431 (int)(2*sizeof(void*)),
432 iter->value, type, iter->name, iter->module_name);
433 } else
434 seq_printf(m, "%0*lx %c %s\n",
435 (int)(2*sizeof(void*)),
436 iter->value, iter->type, iter->name);
437 return 0;
440 static const struct seq_operations kallsyms_op = {
441 .start = s_start,
442 .next = s_next,
443 .stop = s_stop,
444 .show = s_show
447 static int kallsyms_open(struct inode *inode, struct file *file)
449 /* We keep iterator in m->private, since normal case is to
450 * s_start from where we left off, so we avoid doing
451 * using get_symbol_offset for every symbol */
452 struct kallsym_iter *iter;
453 int ret;
455 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
456 if (!iter)
457 return -ENOMEM;
458 reset_iter(iter, 0);
460 ret = seq_open(file, &kallsyms_op);
461 if (ret == 0)
462 ((struct seq_file *)file->private_data)->private = iter;
463 else
464 kfree(iter);
465 return ret;
468 static const struct file_operations kallsyms_operations = {
469 .open = kallsyms_open,
470 .read = seq_read,
471 .llseek = seq_lseek,
472 .release = seq_release_private,
475 static int __init kallsyms_init(void)
477 proc_create("kallsyms", 0444, NULL, &kallsyms_operations);
478 return 0;
480 __initcall(kallsyms_init);
482 EXPORT_SYMBOL(__print_symbol);
483 EXPORT_SYMBOL_GPL(sprint_symbol);