2 * Kernel Debugger Architecture Independent Support Functions
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
9 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
10 * 03/02/13 added new 2.5 kallsyms <xavier.bru@bull.net>
13 #include <linux/types.h>
14 #include <linux/sched.h>
16 #include <linux/kallsyms.h>
17 #include <linux/stddef.h>
18 #include <linux/vmalloc.h>
19 #include <linux/ptrace.h>
20 #include <linux/highmem.h>
21 #include <linux/hardirq.h>
22 #include <linux/delay.h>
23 #include <linux/uaccess.h>
24 #include <linux/kdb.h>
25 #include <linux/slab.h>
26 #include <linux/ctype.h>
27 #include "kdb_private.h"
30 * kdbgetsymval - Return the address of the given symbol.
33 * symname Character string containing symbol name
34 * symtab Structure to receive results
36 * 0 Symbol not found, symtab zero filled
37 * 1 Symbol mapped to module/symbol/section, data in symtab
39 int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)
41 kdb_dbg_printf(AR, "symname=%s, symtab=%px\n", symname, symtab);
42 memset(symtab, 0, sizeof(*symtab));
43 symtab->sym_start = kallsyms_lookup_name(symname);
44 if (symtab->sym_start) {
45 kdb_dbg_printf(AR, "returns 1, symtab->sym_start=0x%lx\n",
49 kdb_dbg_printf(AR, "returns 0\n");
52 EXPORT_SYMBOL(kdbgetsymval);
55 * kdbnearsym() - Return the name of the symbol with the nearest address
57 * @addr: Address to check for near symbol
58 * @symtab: Structure to receive results
60 * WARNING: This function may return a pointer to a single statically
61 * allocated buffer (namebuf). kdb's unusual calling context (single
62 * threaded, all other CPUs halted) provides us sufficient locking for
63 * this to be safe. The only constraint imposed by the static buffer is
64 * that the caller must consume any previous reply prior to another call
65 * to lookup a new symbol.
67 * Note that, strictly speaking, some architectures may re-enter the kdb
68 * trap if the system turns out to be very badly damaged and this breaks
69 * the single-threaded assumption above. In these circumstances successful
70 * continuation and exit from the inner trap is unlikely to work and any
71 * user attempting this receives a prominent warning before being allowed
72 * to progress. In these circumstances we remain memory safe because
73 * namebuf[KSYM_NAME_LEN-1] will never change from '\0' although we do
74 * tolerate the possibility of garbled symbol display from the outer kdb
78 * * 0 - No sections contain this address, symtab zero filled
79 * * 1 - Address mapped to module/symbol/section, data in symtab
81 int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)
84 unsigned long symbolsize = 0;
85 unsigned long offset = 0;
86 static char namebuf[KSYM_NAME_LEN];
88 kdb_dbg_printf(AR, "addr=0x%lx, symtab=%px\n", addr, symtab);
89 memset(symtab, 0, sizeof(*symtab));
94 symtab->sym_name = kallsyms_lookup(addr, &symbolsize , &offset,
95 (char **)(&symtab->mod_name), namebuf);
96 if (offset > 8*1024*1024) {
97 symtab->sym_name = NULL;
98 addr = offset = symbolsize = 0;
100 symtab->sym_start = addr - offset;
101 symtab->sym_end = symtab->sym_start + symbolsize;
102 ret = symtab->sym_name != NULL && *(symtab->sym_name) != '\0';
104 if (symtab->mod_name == NULL)
105 symtab->mod_name = "kernel";
106 kdb_dbg_printf(AR, "returns %d symtab->sym_start=0x%lx, symtab->mod_name=%px, symtab->sym_name=%px (%s)\n",
107 ret, symtab->sym_start, symtab->mod_name, symtab->sym_name, symtab->sym_name);
112 static char ks_namebuf[KSYM_NAME_LEN+1], ks_namebuf_prev[KSYM_NAME_LEN+1];
115 * kallsyms_symbol_complete
118 * prefix_name prefix of a symbol name to lookup
119 * max_len maximum length that can be returned
121 * Number of symbols which match the given prefix.
123 * prefix_name is changed to contain the longest unique prefix that
124 * starts with this prefix (tab completion).
126 int kallsyms_symbol_complete(char *prefix_name, int max_len)
129 int prefix_len = strlen(prefix_name), prev_len = 0;
133 while ((name = kdb_walk_kallsyms(&pos))) {
134 if (strncmp(name, prefix_name, prefix_len) == 0) {
135 strscpy(ks_namebuf, name, sizeof(ks_namebuf));
136 /* Work out the longest name that matches the prefix */
138 prev_len = min_t(int, max_len-1,
140 memcpy(ks_namebuf_prev, ks_namebuf, prev_len);
141 ks_namebuf_prev[prev_len] = '\0';
144 for (i = 0; i < prev_len; i++) {
145 if (ks_namebuf[i] != ks_namebuf_prev[i]) {
147 ks_namebuf_prev[i] = '\0';
153 if (prev_len > prefix_len)
154 memcpy(prefix_name, ks_namebuf_prev, prev_len+1);
159 * kallsyms_symbol_next
162 * prefix_name prefix of a symbol name to lookup
163 * flag 0 means search from the head, 1 means continue search.
164 * buf_size maximum length that can be written to prefix_name
167 * 1 if a symbol matches the given prefix.
168 * 0 if no string found
170 int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size)
172 int prefix_len = strlen(prefix_name);
179 while ((name = kdb_walk_kallsyms(&pos))) {
180 if (!strncmp(name, prefix_name, prefix_len))
181 return strscpy(prefix_name, name, buf_size);
187 * kdb_symbol_print - Standard method for printing a symbol name and offset.
189 * addr Address to be printed.
190 * symtab Address of symbol data, if NULL this routine does its
192 * punc Punctuation for string, bit field.
194 * The string and its punctuation is only printed if the address
195 * is inside the kernel, except that the value is always printed
198 void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p,
201 kdb_symtab_t symtab, *symtab_p2;
203 symtab_p2 = (kdb_symtab_t *)symtab_p;
206 kdbnearsym(addr, symtab_p2);
208 if (!(symtab_p2->sym_name || (punc & KDB_SP_VALUE)))
210 if (punc & KDB_SP_SPACEB)
212 if (punc & KDB_SP_VALUE)
213 kdb_printf(kdb_machreg_fmt0, addr);
214 if (symtab_p2->sym_name) {
215 if (punc & KDB_SP_VALUE)
217 if (punc & KDB_SP_PAREN)
219 if (strcmp(symtab_p2->mod_name, "kernel"))
220 kdb_printf("[%s]", symtab_p2->mod_name);
221 kdb_printf("%s", symtab_p2->sym_name);
222 if (addr != symtab_p2->sym_start)
223 kdb_printf("+0x%lx", addr - symtab_p2->sym_start);
224 if (punc & KDB_SP_SYMSIZE)
226 symtab_p2->sym_end - symtab_p2->sym_start);
227 if (punc & KDB_SP_PAREN)
230 if (punc & KDB_SP_SPACEA)
232 if (punc & KDB_SP_NEWLINE)
237 * kdb_strdup - kdb equivalent of strdup, for disasm code.
239 * str The string to duplicate.
240 * type Flags to kmalloc for the new string.
242 * Address of the new string, NULL if storage could not be allocated.
244 * This is not in lib/string.c because it uses kmalloc which is not
245 * available when string.o is used in boot loaders.
247 char *kdb_strdup(const char *str, gfp_t type)
249 int n = strlen(str)+1;
250 char *s = kmalloc(n, type);
253 return strcpy(s, str);
257 * kdb_getarea_size - Read an area of data. The kdb equivalent of
258 * copy_from_user, with kdb messages for invalid addresses.
260 * res Pointer to the area to receive the result.
261 * addr Address of the area to copy.
262 * size Size of the area.
264 * 0 for success, < 0 for error.
266 int kdb_getarea_size(void *res, unsigned long addr, size_t size)
268 int ret = copy_from_kernel_nofault((char *)res, (char *)addr, size);
270 if (!KDB_STATE(SUPPRESS)) {
271 kdb_func_printf("Bad address 0x%lx\n", addr);
272 KDB_STATE_SET(SUPPRESS);
276 KDB_STATE_CLEAR(SUPPRESS);
282 * kdb_putarea_size - Write an area of data. The kdb equivalent of
283 * copy_to_user, with kdb messages for invalid addresses.
285 * addr Address of the area to write to.
286 * res Pointer to the area holding the data.
287 * size Size of the area.
289 * 0 for success, < 0 for error.
291 int kdb_putarea_size(unsigned long addr, void *res, size_t size)
293 int ret = copy_to_kernel_nofault((char *)addr, (char *)res, size);
295 if (!KDB_STATE(SUPPRESS)) {
296 kdb_func_printf("Bad address 0x%lx\n", addr);
297 KDB_STATE_SET(SUPPRESS);
301 KDB_STATE_CLEAR(SUPPRESS);
307 * kdb_getphys - Read data from a physical address. Validate the
308 * address is in range, use kmap_atomic() to get data
309 * similar to kdb_getarea() - but for phys addresses
311 * res Pointer to the word to receive the result
312 * addr Physical address of the area to copy
313 * size Size of the area
315 * 0 for success, < 0 for error.
317 static int kdb_getphys(void *res, unsigned long addr, size_t size)
323 pfn = (addr >> PAGE_SHIFT);
326 page = pfn_to_page(pfn);
327 vaddr = kmap_atomic(page);
328 memcpy(res, vaddr + (addr & (PAGE_SIZE - 1)), size);
329 kunmap_atomic(vaddr);
337 * word Pointer to the word to receive the result.
338 * addr Address of the area to copy.
339 * size Size of the area.
341 * 0 for success, < 0 for error.
343 int kdb_getphysword(unsigned long *word, unsigned long addr, size_t size)
350 *word = 0; /* Default value if addr or size is invalid */
354 diag = kdb_getphys(&w1, addr, sizeof(w1));
359 diag = kdb_getphys(&w2, addr, sizeof(w2));
364 diag = kdb_getphys(&w4, addr, sizeof(w4));
369 if (size <= sizeof(*word)) {
370 diag = kdb_getphys(&w8, addr, sizeof(w8));
378 kdb_func_printf("bad width %zu\n", size);
384 * kdb_getword - Read a binary value. Unlike kdb_getarea, this treats
387 * word Pointer to the word to receive the result.
388 * addr Address of the area to copy.
389 * size Size of the area.
391 * 0 for success, < 0 for error.
393 int kdb_getword(unsigned long *word, unsigned long addr, size_t size)
400 *word = 0; /* Default value if addr or size is invalid */
403 diag = kdb_getarea(w1, addr);
408 diag = kdb_getarea(w2, addr);
413 diag = kdb_getarea(w4, addr);
418 if (size <= sizeof(*word)) {
419 diag = kdb_getarea(w8, addr);
427 kdb_func_printf("bad width %zu\n", size);
433 * kdb_putword - Write a binary value. Unlike kdb_putarea, this
434 * treats data as numbers.
436 * addr Address of the area to write to..
437 * word The value to set.
438 * size Size of the area.
440 * 0 for success, < 0 for error.
442 int kdb_putword(unsigned long addr, unsigned long word, size_t size)
452 diag = kdb_putarea(addr, w1);
456 diag = kdb_putarea(addr, w2);
460 diag = kdb_putarea(addr, w4);
463 if (size <= sizeof(word)) {
465 diag = kdb_putarea(addr, w8);
471 kdb_func_printf("bad width %zu\n", size);
479 * kdb_task_state_char - Return the character that represents the task state.
481 * p struct task for the process
483 * One character to represent the task state.
485 char kdb_task_state_char (const struct task_struct *p)
492 copy_from_kernel_nofault(&tmp, (char *)p, sizeof(unsigned long)))
495 state = task_state_to_char((struct task_struct *) p);
497 if (is_idle_task(p)) {
498 /* Idle task. Is it really idle, apart from the kdb
500 cpu = kdb_process_cpu(p);
501 if (!kdb_task_has_cpu(p) || kgdb_info[cpu].irq_depth == 1) {
502 if (cpu != kdb_initial_cpu)
503 state = '-'; /* idle task */
505 } else if (!p->mm && strchr("IMS", state)) {
506 state = tolower(state); /* sleeping system daemon */
512 * kdb_task_state - Return true if a process has the desired state
515 * p struct task for the process
516 * mask set of characters used to select processes; both NULL
517 * and the empty string mean adopt a default filter, which
518 * is to suppress sleeping system daemons and the idle tasks
520 * True if the process matches at least one criteria defined by the mask.
522 bool kdb_task_state(const struct task_struct *p, const char *mask)
524 char state = kdb_task_state_char(p);
526 /* If there is no mask, then we will filter code that runs when the
527 * scheduler is idling and any system daemons that are currently
530 if (!mask || mask[0] == '\0')
531 return !strchr("-ims", state);
533 /* A is a special case that matches all states */
534 if (strchr(mask, 'A'))
537 return strchr(mask, state);
540 /* Maintain a small stack of kdb_flags to allow recursion without disturbing
541 * the global kdb state.
544 static int kdb_flags_stack[4], kdb_flags_index;
546 void kdb_save_flags(void)
548 BUG_ON(kdb_flags_index >= ARRAY_SIZE(kdb_flags_stack));
549 kdb_flags_stack[kdb_flags_index++] = kdb_flags;
552 void kdb_restore_flags(void)
554 BUG_ON(kdb_flags_index <= 0);
555 kdb_flags = kdb_flags_stack[--kdb_flags_index];