Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * kallsyms.c: in-kernel printing of symbolic oopses and stack traces. |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * ChangeLog: |
| 9 | * |
| 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) |
| 13 | */ |
| 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> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 21 | #include <linux/sched.h> /* for cond_resched */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/mm.h> |
Adam B. Jerome | 07354a0 | 2006-12-06 20:35:30 -0800 | [diff] [blame] | 23 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <asm/sections.h> |
| 26 | |
| 27 | #ifdef CONFIG_KALLSYMS_ALL |
| 28 | #define all_var 1 |
| 29 | #else |
| 30 | #define all_var 0 |
| 31 | #endif |
| 32 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 33 | /* |
| 34 | * These will be re-linked against their real values |
| 35 | * during the second link stage. |
| 36 | */ |
Sam Ravnborg | 2ea0389 | 2009-01-14 21:38:20 +0100 | [diff] [blame] | 37 | extern const unsigned long kallsyms_addresses[] __attribute__((weak)); |
| 38 | extern const u8 kallsyms_names[] __attribute__((weak)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 40 | /* |
| 41 | * Tell the compiler that the count isn't in the small data section if the arch |
| 42 | * has one (eg: FRV). |
David Howells | 9e6c1e6 | 2007-11-28 16:22:04 -0800 | [diff] [blame] | 43 | */ |
| 44 | extern const unsigned long kallsyms_num_syms |
Sam Ravnborg | 2ea0389 | 2009-01-14 21:38:20 +0100 | [diff] [blame] | 45 | __attribute__((weak, section(".rodata"))); |
David Howells | 9e6c1e6 | 2007-11-28 16:22:04 -0800 | [diff] [blame] | 46 | |
Sam Ravnborg | 2ea0389 | 2009-01-14 21:38:20 +0100 | [diff] [blame] | 47 | extern const u8 kallsyms_token_table[] __attribute__((weak)); |
| 48 | extern const u16 kallsyms_token_index[] __attribute__((weak)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Sam Ravnborg | 2ea0389 | 2009-01-14 21:38:20 +0100 | [diff] [blame] | 50 | extern const unsigned long kallsyms_markers[] __attribute__((weak)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | static inline int is_kernel_inittext(unsigned long addr) |
| 53 | { |
| 54 | if (addr >= (unsigned long)_sinittext |
| 55 | && addr <= (unsigned long)_einittext) |
| 56 | return 1; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static inline int is_kernel_text(unsigned long addr) |
| 61 | { |
Mike Frysinger | 128e8db | 2009-09-22 16:44:15 -0700 | [diff] [blame] | 62 | if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) || |
| 63 | arch_is_kernel_text(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return 1; |
| 65 | return in_gate_area_no_task(addr); |
| 66 | } |
| 67 | |
| 68 | static inline int is_kernel(unsigned long addr) |
| 69 | { |
| 70 | if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end) |
| 71 | return 1; |
| 72 | return in_gate_area_no_task(addr); |
| 73 | } |
| 74 | |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 75 | static int is_ksym_addr(unsigned long addr) |
| 76 | { |
| 77 | if (all_var) |
| 78 | return is_kernel(addr); |
| 79 | |
Robin Getz | a3b8111 | 2008-02-06 01:36:26 -0800 | [diff] [blame] | 80 | return is_kernel_text(addr) || is_kernel_inittext(addr); |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 83 | /* |
| 84 | * Expand a compressed symbol data into the resulting uncompressed string, |
| 85 | * given the offset to where the symbol is in the compressed stream. |
| 86 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | static unsigned int kallsyms_expand_symbol(unsigned int off, char *result) |
| 88 | { |
| 89 | int len, skipped_first = 0; |
Jan Beulich | aad0947 | 2006-12-08 02:35:57 -0800 | [diff] [blame] | 90 | const u8 *tptr, *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 92 | /* Get the compressed symbol length from the first symbol byte. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | data = &kallsyms_names[off]; |
| 94 | len = *data; |
| 95 | data++; |
| 96 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 97 | /* |
| 98 | * Update the offset to return the offset for the next symbol on |
| 99 | * the compressed stream. |
| 100 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | off += len + 1; |
| 102 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 103 | /* |
| 104 | * For every byte on the compressed symbol data, copy the table |
| 105 | * entry for that byte. |
| 106 | */ |
| 107 | while (len) { |
| 108 | tptr = &kallsyms_token_table[kallsyms_token_index[*data]]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | data++; |
| 110 | len--; |
| 111 | |
| 112 | while (*tptr) { |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 113 | if (skipped_first) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | *result = *tptr; |
| 115 | result++; |
| 116 | } else |
| 117 | skipped_first = 1; |
| 118 | tptr++; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | *result = '\0'; |
| 123 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 124 | /* Return to offset to the next symbol. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return off; |
| 126 | } |
| 127 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 128 | /* |
| 129 | * Get symbol type information. This is encoded as a single char at the |
| 130 | * beginning of the symbol name. |
| 131 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | static char kallsyms_get_symbol_type(unsigned int off) |
| 133 | { |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 134 | /* |
| 135 | * Get just the first code, look it up in the token table, |
| 136 | * and return the first char from this token. |
| 137 | */ |
| 138 | return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 142 | /* |
| 143 | * Find the offset on the compressed stream given and index in the |
| 144 | * kallsyms array. |
| 145 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | static unsigned int get_symbol_offset(unsigned long pos) |
| 147 | { |
Jan Beulich | aad0947 | 2006-12-08 02:35:57 -0800 | [diff] [blame] | 148 | const u8 *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | int i; |
| 150 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 151 | /* |
| 152 | * Use the closest marker we have. We have markers every 256 positions, |
| 153 | * so that should be close enough. |
| 154 | */ |
| 155 | name = &kallsyms_names[kallsyms_markers[pos >> 8]]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 157 | /* |
| 158 | * Sequentially scan all the symbols up to the point we're searching |
| 159 | * for. Every symbol is stored in a [<len>][<len> bytes of data] format, |
| 160 | * so we just need to add the len to the current pointer for every |
| 161 | * symbol we wish to skip. |
| 162 | */ |
| 163 | for (i = 0; i < (pos & 0xFF); i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | name = name + (*name) + 1; |
| 165 | |
| 166 | return name - kallsyms_names; |
| 167 | } |
| 168 | |
| 169 | /* Lookup the address for this symbol. Returns 0 if not found. */ |
| 170 | unsigned long kallsyms_lookup_name(const char *name) |
| 171 | { |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 172 | char namebuf[KSYM_NAME_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | unsigned long i; |
| 174 | unsigned int off; |
| 175 | |
| 176 | for (i = 0, off = 0; i < kallsyms_num_syms; i++) { |
| 177 | off = kallsyms_expand_symbol(off, namebuf); |
| 178 | |
| 179 | if (strcmp(namebuf, name) == 0) |
| 180 | return kallsyms_addresses[i]; |
| 181 | } |
| 182 | return module_kallsyms_lookup_name(name); |
| 183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Anders Kaseorg | 75a6661 | 2008-12-05 19:03:58 -0500 | [diff] [blame] | 185 | int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, |
| 186 | unsigned long), |
| 187 | void *data) |
| 188 | { |
| 189 | char namebuf[KSYM_NAME_LEN]; |
| 190 | unsigned long i; |
| 191 | unsigned int off; |
| 192 | int ret; |
| 193 | |
| 194 | for (i = 0, off = 0; i < kallsyms_num_syms; i++) { |
| 195 | off = kallsyms_expand_symbol(off, namebuf); |
| 196 | ret = fn(data, namebuf, NULL, kallsyms_addresses[i]); |
| 197 | if (ret != 0) |
| 198 | return ret; |
| 199 | } |
| 200 | return module_kallsyms_on_each_symbol(fn, data); |
| 201 | } |
| 202 | EXPORT_SYMBOL_GPL(kallsyms_on_each_symbol); |
| 203 | |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 204 | static unsigned long get_symbol_pos(unsigned long addr, |
| 205 | unsigned long *symbolsize, |
| 206 | unsigned long *offset) |
| 207 | { |
| 208 | unsigned long symbol_start = 0, symbol_end = 0; |
| 209 | unsigned long i, low, high, mid; |
| 210 | |
Sam Ravnborg | 2ea0389 | 2009-01-14 21:38:20 +0100 | [diff] [blame] | 211 | /* This kernel should never had been booted. */ |
| 212 | BUG_ON(!kallsyms_addresses); |
| 213 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 214 | /* Do a binary search on the sorted kallsyms_addresses array. */ |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 215 | low = 0; |
| 216 | high = kallsyms_num_syms; |
| 217 | |
| 218 | while (high - low > 1) { |
Vegard Nossum | 2fc9c4e | 2008-07-25 01:45:34 -0700 | [diff] [blame] | 219 | mid = low + (high - low) / 2; |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 220 | if (kallsyms_addresses[mid] <= addr) |
| 221 | low = mid; |
| 222 | else |
| 223 | high = mid; |
| 224 | } |
| 225 | |
| 226 | /* |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 227 | * Search for the first aliased symbol. Aliased |
| 228 | * symbols are symbols with the same address. |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 229 | */ |
| 230 | while (low && kallsyms_addresses[low-1] == kallsyms_addresses[low]) |
| 231 | --low; |
| 232 | |
| 233 | symbol_start = kallsyms_addresses[low]; |
| 234 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 235 | /* Search for next non-aliased symbol. */ |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 236 | for (i = low + 1; i < kallsyms_num_syms; i++) { |
| 237 | if (kallsyms_addresses[i] > symbol_start) { |
| 238 | symbol_end = kallsyms_addresses[i]; |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 243 | /* If we found no next symbol, we use the end of the section. */ |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 244 | if (!symbol_end) { |
| 245 | if (is_kernel_inittext(addr)) |
| 246 | symbol_end = (unsigned long)_einittext; |
| 247 | else if (all_var) |
| 248 | symbol_end = (unsigned long)_end; |
| 249 | else |
| 250 | symbol_end = (unsigned long)_etext; |
| 251 | } |
| 252 | |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 253 | if (symbolsize) |
| 254 | *symbolsize = symbol_end - symbol_start; |
| 255 | if (offset) |
| 256 | *offset = addr - symbol_start; |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 257 | |
| 258 | return low; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Lookup an address but don't bother to find any names. |
| 263 | */ |
| 264 | int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize, |
| 265 | unsigned long *offset) |
| 266 | { |
Rusty Russell | 6dd06c9 | 2008-01-29 17:13:22 -0500 | [diff] [blame] | 267 | char namebuf[KSYM_NAME_LEN]; |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 268 | if (is_ksym_addr(addr)) |
| 269 | return !!get_symbol_pos(addr, symbolsize, offset); |
| 270 | |
Rusty Russell | 6dd06c9 | 2008-01-29 17:13:22 -0500 | [diff] [blame] | 271 | return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf); |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | /* |
| 275 | * Lookup an address |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 276 | * - modname is set to NULL if it's in the kernel. |
| 277 | * - We guarantee that the returned name is valid until we reschedule even if. |
| 278 | * It resides in a module. |
| 279 | * - We also guarantee that modname will be valid until rescheduled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | */ |
| 281 | const char *kallsyms_lookup(unsigned long addr, |
| 282 | unsigned long *symbolsize, |
| 283 | unsigned long *offset, |
| 284 | char **modname, char *namebuf) |
| 285 | { |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 286 | namebuf[KSYM_NAME_LEN - 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | namebuf[0] = 0; |
| 288 | |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 289 | if (is_ksym_addr(addr)) { |
| 290 | unsigned long pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 292 | pos = get_symbol_pos(addr, symbolsize, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* Grab name */ |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 294 | kallsyms_expand_symbol(get_symbol_offset(pos), namebuf); |
Kyle McMartin | 7a74fc4 | 2007-05-30 02:43:16 -0400 | [diff] [blame] | 295 | if (modname) |
| 296 | *modname = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return namebuf; |
| 298 | } |
| 299 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 300 | /* See if it's in a module. */ |
Rusty Russell | 6dd06c9 | 2008-01-29 17:13:22 -0500 | [diff] [blame] | 301 | return module_address_lookup(addr, symbolsize, offset, modname, |
| 302 | namebuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 305 | int lookup_symbol_name(unsigned long addr, char *symname) |
| 306 | { |
| 307 | symname[0] = '\0'; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 308 | symname[KSYM_NAME_LEN - 1] = '\0'; |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 309 | |
| 310 | if (is_ksym_addr(addr)) { |
| 311 | unsigned long pos; |
| 312 | |
| 313 | pos = get_symbol_pos(addr, NULL, NULL); |
| 314 | /* Grab name */ |
| 315 | kallsyms_expand_symbol(get_symbol_offset(pos), symname); |
| 316 | return 0; |
| 317 | } |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 318 | /* See if it's in a module. */ |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 319 | return lookup_module_symbol_name(addr, symname); |
| 320 | } |
| 321 | |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 322 | int lookup_symbol_attrs(unsigned long addr, unsigned long *size, |
| 323 | unsigned long *offset, char *modname, char *name) |
| 324 | { |
| 325 | name[0] = '\0'; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 326 | name[KSYM_NAME_LEN - 1] = '\0'; |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 327 | |
| 328 | if (is_ksym_addr(addr)) { |
| 329 | unsigned long pos; |
| 330 | |
| 331 | pos = get_symbol_pos(addr, size, offset); |
| 332 | /* Grab name */ |
| 333 | kallsyms_expand_symbol(get_symbol_offset(pos), name); |
| 334 | modname[0] = '\0'; |
| 335 | return 0; |
| 336 | } |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 337 | /* See if it's in a module. */ |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 338 | return lookup_module_symbol_attrs(addr, size, offset, modname, name); |
| 339 | } |
| 340 | |
Robert Peterson | 42e3808 | 2007-04-30 15:09:48 -0700 | [diff] [blame] | 341 | /* Look up a kernel symbol and return it in a text buffer. */ |
| 342 | int sprint_symbol(char *buffer, unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | char *modname; |
| 345 | const char *name; |
| 346 | unsigned long offset, size; |
Hugh Dickins | 966c8c1 | 2008-11-19 15:36:36 -0800 | [diff] [blame] | 347 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Hugh Dickins | 966c8c1 | 2008-11-19 15:36:36 -0800 | [diff] [blame] | 349 | name = kallsyms_lookup(address, &size, &offset, &modname, buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (!name) |
Robert Peterson | 42e3808 | 2007-04-30 15:09:48 -0700 | [diff] [blame] | 351 | return sprintf(buffer, "0x%lx", address); |
Andrew Morton | 19769b7 | 2007-07-15 23:41:24 -0700 | [diff] [blame] | 352 | |
Hugh Dickins | 966c8c1 | 2008-11-19 15:36:36 -0800 | [diff] [blame] | 353 | if (name != buffer) |
| 354 | strcpy(buffer, name); |
| 355 | len = strlen(buffer); |
| 356 | buffer += len; |
| 357 | |
Andrew Morton | 19769b7 | 2007-07-15 23:41:24 -0700 | [diff] [blame] | 358 | if (modname) |
Hugh Dickins | 966c8c1 | 2008-11-19 15:36:36 -0800 | [diff] [blame] | 359 | len += sprintf(buffer, "+%#lx/%#lx [%s]", |
| 360 | offset, size, modname); |
Andrew Morton | 19769b7 | 2007-07-15 23:41:24 -0700 | [diff] [blame] | 361 | else |
Hugh Dickins | 966c8c1 | 2008-11-19 15:36:36 -0800 | [diff] [blame] | 362 | len += sprintf(buffer, "+%#lx/%#lx", offset, size); |
| 363 | |
| 364 | return len; |
Robert Peterson | 42e3808 | 2007-04-30 15:09:48 -0700 | [diff] [blame] | 365 | } |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 366 | EXPORT_SYMBOL_GPL(sprint_symbol); |
Robert Peterson | 42e3808 | 2007-04-30 15:09:48 -0700 | [diff] [blame] | 367 | |
| 368 | /* Look up a kernel symbol and print it to the kernel messages. */ |
| 369 | void __print_symbol(const char *fmt, unsigned long address) |
| 370 | { |
| 371 | char buffer[KSYM_SYMBOL_LEN]; |
| 372 | |
| 373 | sprint_symbol(buffer, address); |
| 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | printk(fmt, buffer); |
| 376 | } |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 377 | EXPORT_SYMBOL(__print_symbol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 380 | struct kallsym_iter { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | loff_t pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | unsigned long value; |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 383 | unsigned int nameoff; /* If iterating in core kernel symbols. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | char type; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 385 | char name[KSYM_NAME_LEN]; |
| 386 | char module_name[MODULE_NAME_LEN]; |
Alexey Dobriyan | ea07890 | 2007-05-08 00:28:39 -0700 | [diff] [blame] | 387 | int exported; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | }; |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | static int get_ksymbol_mod(struct kallsym_iter *iter) |
| 391 | { |
Alexey Dobriyan | ea07890 | 2007-05-08 00:28:39 -0700 | [diff] [blame] | 392 | if (module_get_kallsym(iter->pos - kallsyms_num_syms, &iter->value, |
| 393 | &iter->type, iter->name, iter->module_name, |
| 394 | &iter->exported) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | return 1; |
| 397 | } |
| 398 | |
| 399 | /* Returns space to next name. */ |
| 400 | static unsigned long get_ksymbol_core(struct kallsym_iter *iter) |
| 401 | { |
| 402 | unsigned off = iter->nameoff; |
| 403 | |
Alexey Dobriyan | ea07890 | 2007-05-08 00:28:39 -0700 | [diff] [blame] | 404 | iter->module_name[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | iter->value = kallsyms_addresses[iter->pos]; |
| 406 | |
| 407 | iter->type = kallsyms_get_symbol_type(off); |
| 408 | |
| 409 | off = kallsyms_expand_symbol(off, iter->name); |
| 410 | |
| 411 | return off - iter->nameoff; |
| 412 | } |
| 413 | |
| 414 | static void reset_iter(struct kallsym_iter *iter, loff_t new_pos) |
| 415 | { |
| 416 | iter->name[0] = '\0'; |
| 417 | iter->nameoff = get_symbol_offset(new_pos); |
| 418 | iter->pos = new_pos; |
| 419 | } |
| 420 | |
| 421 | /* Returns false if pos at or past end of file. */ |
| 422 | static int update_iter(struct kallsym_iter *iter, loff_t pos) |
| 423 | { |
| 424 | /* Module symbols can be accessed randomly. */ |
| 425 | if (pos >= kallsyms_num_syms) { |
| 426 | iter->pos = pos; |
| 427 | return get_ksymbol_mod(iter); |
| 428 | } |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /* If we're not on the desired position, reset to new position. */ |
| 431 | if (pos != iter->pos) |
| 432 | reset_iter(iter, pos); |
| 433 | |
| 434 | iter->nameoff += get_ksymbol_core(iter); |
| 435 | iter->pos++; |
| 436 | |
| 437 | return 1; |
| 438 | } |
| 439 | |
| 440 | static void *s_next(struct seq_file *m, void *p, loff_t *pos) |
| 441 | { |
| 442 | (*pos)++; |
| 443 | |
| 444 | if (!update_iter(m->private, *pos)) |
| 445 | return NULL; |
| 446 | return p; |
| 447 | } |
| 448 | |
| 449 | static void *s_start(struct seq_file *m, loff_t *pos) |
| 450 | { |
| 451 | if (!update_iter(m->private, *pos)) |
| 452 | return NULL; |
| 453 | return m->private; |
| 454 | } |
| 455 | |
| 456 | static void s_stop(struct seq_file *m, void *p) |
| 457 | { |
| 458 | } |
| 459 | |
| 460 | static int s_show(struct seq_file *m, void *p) |
| 461 | { |
| 462 | struct kallsym_iter *iter = m->private; |
| 463 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 464 | /* Some debugging symbols have no name. Ignore them. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (!iter->name[0]) |
| 466 | return 0; |
| 467 | |
Alexey Dobriyan | ea07890 | 2007-05-08 00:28:39 -0700 | [diff] [blame] | 468 | if (iter->module_name[0]) { |
| 469 | char type; |
| 470 | |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 471 | /* |
| 472 | * Label it "global" if it is exported, |
| 473 | * "local" if not exported. |
| 474 | */ |
Alexey Dobriyan | ea07890 | 2007-05-08 00:28:39 -0700 | [diff] [blame] | 475 | type = iter->exported ? toupper(iter->type) : |
| 476 | tolower(iter->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | seq_printf(m, "%0*lx %c %s\t[%s]\n", |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 478 | (int)(2 * sizeof(void *)), |
Alexey Dobriyan | ea07890 | 2007-05-08 00:28:39 -0700 | [diff] [blame] | 479 | iter->value, type, iter->name, iter->module_name); |
| 480 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | seq_printf(m, "%0*lx %c %s\n", |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 482 | (int)(2 * sizeof(void *)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | iter->value, iter->type, iter->name); |
| 484 | return 0; |
| 485 | } |
| 486 | |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 487 | static const struct seq_operations kallsyms_op = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | .start = s_start, |
| 489 | .next = s_next, |
| 490 | .stop = s_stop, |
| 491 | .show = s_show |
| 492 | }; |
| 493 | |
| 494 | static int kallsyms_open(struct inode *inode, struct file *file) |
| 495 | { |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 496 | /* |
| 497 | * We keep iterator in m->private, since normal case is to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | * s_start from where we left off, so we avoid doing |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 499 | * using get_symbol_offset for every symbol. |
| 500 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | struct kallsym_iter *iter; |
| 502 | int ret; |
| 503 | |
| 504 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); |
| 505 | if (!iter) |
| 506 | return -ENOMEM; |
| 507 | reset_iter(iter, 0); |
| 508 | |
| 509 | ret = seq_open(file, &kallsyms_op); |
| 510 | if (ret == 0) |
| 511 | ((struct seq_file *)file->private_data)->private = iter; |
| 512 | else |
| 513 | kfree(iter); |
| 514 | return ret; |
| 515 | } |
| 516 | |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 517 | static const struct file_operations kallsyms_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | .open = kallsyms_open, |
| 519 | .read = seq_read, |
| 520 | .llseek = seq_lseek, |
Martin Peschke | 5a0c6a0 | 2007-05-08 00:29:25 -0700 | [diff] [blame] | 521 | .release = seq_release_private, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | }; |
| 523 | |
| 524 | static int __init kallsyms_init(void) |
| 525 | { |
Denis V. Lunev | c33fff0 | 2008-04-29 01:02:31 -0700 | [diff] [blame] | 526 | proc_create("kallsyms", 0444, NULL, &kallsyms_operations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | return 0; |
| 528 | } |
Manish Katiyar | ad6ccfa | 2009-05-12 13:43:35 -0700 | [diff] [blame] | 529 | device_initcall(kallsyms_init); |