2 * kallsyms.c: in-kernel printing of symbolic oopses and stack traces.
4 * Rewritten and vastly simplified by Rusty Russell for in-kernel
6 * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
7 * Stem compression by Andi Kleen.
9 #include <linux/kallsyms.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/seq_file.h>
14 #include <linux/err.h>
15 #include <linux/proc_fs.h>
17 /* These will be re-linked against their real values during the second link stage */
18 extern unsigned long kallsyms_addresses
[] __attribute__((weak
));
19 extern unsigned long kallsyms_num_syms
__attribute__((weak
));
20 extern char kallsyms_names
[] __attribute__((weak
));
22 /* Defined by the linker script. */
23 extern char _stext
[], _etext
[], _sinittext
[], _einittext
[];
25 static inline int is_kernel_inittext(unsigned long addr
)
27 if (addr
>= (unsigned long)_sinittext
28 && addr
<= (unsigned long)_einittext
)
33 static inline int is_kernel_text(unsigned long addr
)
35 if (addr
>= (unsigned long)_stext
&& addr
<= (unsigned long)_etext
)
40 /* Lookup the address for this symbol. Returns 0 if not found. */
41 unsigned long kallsyms_lookup_name(const char *name
)
43 char namebuf
[KSYM_NAME_LEN
+1];
47 for (i
= 0, knames
= kallsyms_names
; i
< kallsyms_num_syms
; i
++) {
48 unsigned prefix
= *knames
++;
50 strlcpy(namebuf
+ prefix
, knames
, KSYM_NAME_LEN
- prefix
);
51 if (strcmp(namebuf
, name
) == 0)
52 return kallsyms_addresses
[i
];
54 knames
+= strlen(knames
) + 1;
56 return module_kallsyms_lookup_name(name
);
59 /* Lookup an address. modname is set to NULL if it's in the kernel. */
60 const char *kallsyms_lookup(unsigned long addr
,
61 unsigned long *symbolsize
,
62 unsigned long *offset
,
63 char **modname
, char *namebuf
)
65 unsigned long i
, best
= 0;
67 /* This kernel should never had been booted. */
68 BUG_ON(!kallsyms_addresses
);
70 namebuf
[KSYM_NAME_LEN
] = 0;
73 if (is_kernel_text(addr
) || is_kernel_inittext(addr
)) {
74 unsigned long symbol_end
;
75 char *name
= kallsyms_names
;
77 /* They're sorted, we could be clever here, but who cares? */
78 for (i
= 0; i
< kallsyms_num_syms
; i
++) {
79 if (kallsyms_addresses
[i
] > kallsyms_addresses
[best
] &&
80 kallsyms_addresses
[i
] <= addr
)
85 for (i
= 0; i
<= best
; i
++) {
86 unsigned prefix
= *name
++;
87 strncpy(namebuf
+ prefix
, name
, KSYM_NAME_LEN
- prefix
);
88 name
+= strlen(name
) + 1;
91 /* At worst, symbol ends at end of section. */
92 if (is_kernel_inittext(addr
))
93 symbol_end
= (unsigned long)_einittext
;
95 symbol_end
= (unsigned long)_etext
;
97 /* Search for next non-aliased symbol */
98 for (i
= best
+1; i
< kallsyms_num_syms
; i
++) {
99 if (kallsyms_addresses
[i
] > kallsyms_addresses
[best
]) {
100 symbol_end
= kallsyms_addresses
[i
];
105 *symbolsize
= symbol_end
- kallsyms_addresses
[best
];
107 *offset
= addr
- kallsyms_addresses
[best
];
111 return module_address_lookup(addr
, symbolsize
, offset
, modname
);
114 /* Replace "%s" in format with address, or returns -errno. */
115 void __print_symbol(const char *fmt
, unsigned long address
)
119 unsigned long offset
, size
;
120 char namebuf
[KSYM_NAME_LEN
+1];
121 char buffer
[sizeof("%s+%#lx/%#lx [%s]") + KSYM_NAME_LEN
+
122 2*(BITS_PER_LONG
*3/10) + MODULE_NAME_LEN
+ 1];
124 name
= kallsyms_lookup(address
, &size
, &offset
, &modname
, namebuf
);
127 sprintf(buffer
, "0x%lx", address
);
130 sprintf(buffer
, "%s+%#lx/%#lx [%s]", name
, offset
,
133 sprintf(buffer
, "%s+%#lx/%#lx", name
, offset
, size
);
138 /* To avoid O(n^2) iteration, we carry prefix along. */
142 struct module
*owner
;
144 unsigned int nameoff
; /* If iterating in core kernel symbols */
146 char name
[KSYM_NAME_LEN
+1];
149 /* Only label it "global" if it is exported. */
150 static void upcase_if_global(struct kallsym_iter
*iter
)
152 if (is_exported(iter
->name
, iter
->owner
))
153 iter
->type
+= 'A' - 'a';
156 static int get_ksymbol_mod(struct kallsym_iter
*iter
)
158 iter
->owner
= module_get_kallsym(iter
->pos
- kallsyms_num_syms
,
160 &iter
->type
, iter
->name
);
161 if (iter
->owner
== NULL
)
164 upcase_if_global(iter
);
168 /* Returns space to next name. */
169 static unsigned long get_ksymbol_core(struct kallsym_iter
*iter
)
171 unsigned stemlen
, off
= iter
->nameoff
;
173 /* First char of each symbol name indicates prefix length
174 shared with previous name (stem compression). */
175 stemlen
= kallsyms_names
[off
++];
177 strlcpy(iter
->name
+stemlen
, kallsyms_names
+ off
,
178 KSYM_NAME_LEN
+1-stemlen
);
179 off
+= strlen(kallsyms_names
+ off
) + 1;
181 iter
->value
= kallsyms_addresses
[iter
->pos
];
182 if (is_kernel_text(iter
->value
) || is_kernel_inittext(iter
->value
))
187 upcase_if_global(iter
);
188 return off
- iter
->nameoff
;
191 static void reset_iter(struct kallsym_iter
*iter
)
193 iter
->name
[0] = '\0';
198 /* Returns false if pos at or past end of file. */
199 static int update_iter(struct kallsym_iter
*iter
, loff_t pos
)
201 /* Module symbols can be accessed randomly. */
202 if (pos
>= kallsyms_num_syms
) {
204 return get_ksymbol_mod(iter
);
207 /* If we're past the desired position, reset to start. */
211 /* We need to iterate through the previous symbols: can be slow */
212 for (; iter
->pos
!= pos
; iter
->pos
++) {
213 iter
->nameoff
+= get_ksymbol_core(iter
);
216 get_ksymbol_core(iter
);
220 static void *s_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
224 if (!update_iter(m
->private, *pos
))
229 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
231 if (!update_iter(m
->private, *pos
))
236 static void s_stop(struct seq_file
*m
, void *p
)
240 static int s_show(struct seq_file
*m
, void *p
)
242 struct kallsym_iter
*iter
= m
->private;
244 /* Some debugging symbols have no name. Ignore them. */
249 seq_printf(m
, "%0*lx %c %s\t[%s]\n",
250 (int)(2*sizeof(void*)),
251 iter
->value
, iter
->type
, iter
->name
,
252 module_name(iter
->owner
));
254 seq_printf(m
, "%0*lx %c %s\n",
255 (int)(2*sizeof(void*)),
256 iter
->value
, iter
->type
, iter
->name
);
260 struct seq_operations kallsyms_op
= {
267 static int kallsyms_open(struct inode
*inode
, struct file
*file
)
269 /* We keep iterator in m->private, since normal case is to
270 * s_start from where we left off, so we avoid O(N^2). */
271 struct kallsym_iter
*iter
;
274 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
279 ret
= seq_open(file
, &kallsyms_op
);
281 ((struct seq_file
*)file
->private_data
)->private = iter
;
287 static int kallsyms_release(struct inode
*inode
, struct file
*file
)
289 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
291 return seq_release(inode
, file
);
294 static struct file_operations kallsyms_operations
= {
295 .open
= kallsyms_open
,
298 .release
= kallsyms_release
,
301 int __init
kallsyms_init(void)
303 struct proc_dir_entry
*entry
;
305 entry
= create_proc_entry("kallsyms", 0444, NULL
);
307 entry
->proc_fops
= &kallsyms_operations
;
310 __initcall(kallsyms_init
);
312 EXPORT_SYMBOL(__print_symbol
);