diff options
Diffstat (limited to 'kernel/debug')
| -rw-r--r-- | kernel/debug/Makefile | 6 | ||||
| -rw-r--r-- | kernel/debug/debug_core.c | 983 | ||||
| -rw-r--r-- | kernel/debug/debug_core.h | 81 | ||||
| -rw-r--r-- | kernel/debug/gdbstub.c | 1014 | ||||
| -rw-r--r-- | kernel/debug/kdb/.gitignore | 1 | ||||
| -rw-r--r-- | kernel/debug/kdb/Makefile | 25 | ||||
| -rw-r--r-- | kernel/debug/kdb/kdb_bp.c | 564 | ||||
| -rw-r--r-- | kernel/debug/kdb/kdb_bt.c | 210 | ||||
| -rw-r--r-- | kernel/debug/kdb/kdb_cmds | 35 | ||||
| -rw-r--r-- | kernel/debug/kdb/kdb_debugger.c | 169 | ||||
| -rw-r--r-- | kernel/debug/kdb/kdb_io.c | 826 | ||||
| -rw-r--r-- | kernel/debug/kdb/kdb_keyboard.c | 212 | ||||
| -rw-r--r-- | kernel/debug/kdb/kdb_main.c | 2846 | ||||
| -rw-r--r-- | kernel/debug/kdb/kdb_private.h | 300 | ||||
| -rw-r--r-- | kernel/debug/kdb/kdb_support.c | 927 |
15 files changed, 8199 insertions, 0 deletions
diff --git a/kernel/debug/Makefile b/kernel/debug/Makefile new file mode 100644 index 000000000000..a85edc339985 --- /dev/null +++ b/kernel/debug/Makefile | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | # | ||
| 2 | # Makefile for the linux kernel debugger | ||
| 3 | # | ||
| 4 | |||
| 5 | obj-$(CONFIG_KGDB) += debug_core.o gdbstub.o | ||
| 6 | obj-$(CONFIG_KGDB_KDB) += kdb/ | ||
diff --git a/kernel/debug/debug_core.c b/kernel/debug/debug_core.c new file mode 100644 index 000000000000..51d14fe87648 --- /dev/null +++ b/kernel/debug/debug_core.c | |||
| @@ -0,0 +1,983 @@ | |||
| 1 | /* | ||
| 2 | * Kernel Debug Core | ||
| 3 | * | ||
| 4 | * Maintainer: Jason Wessel <jason.wessel@windriver.com> | ||
| 5 | * | ||
| 6 | * Copyright (C) 2000-2001 VERITAS Software Corporation. | ||
| 7 | * Copyright (C) 2002-2004 Timesys Corporation | ||
| 8 | * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com> | ||
| 9 | * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz> | ||
| 10 | * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org> | ||
| 11 | * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd. | ||
| 12 | * Copyright (C) 2005-2009 Wind River Systems, Inc. | ||
| 13 | * Copyright (C) 2007 MontaVista Software, Inc. | ||
| 14 | * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | ||
| 15 | * | ||
| 16 | * Contributors at various stages not listed above: | ||
| 17 | * Jason Wessel ( jason.wessel@windriver.com ) | ||
| 18 | * George Anzinger <george@mvista.com> | ||
| 19 | * Anurekh Saxena (anurekh.saxena@timesys.com) | ||
| 20 | * Lake Stevens Instrument Division (Glenn Engel) | ||
| 21 | * Jim Kingdon, Cygnus Support. | ||
| 22 | * | ||
| 23 | * Original KGDB stub: David Grothe <dave@gcom.com>, | ||
| 24 | * Tigran Aivazian <tigran@sco.com> | ||
| 25 | * | ||
| 26 | * This file is licensed under the terms of the GNU General Public License | ||
| 27 | * version 2. This program is licensed "as is" without any warranty of any | ||
| 28 | * kind, whether express or implied. | ||
| 29 | */ | ||
| 30 | #include <linux/pid_namespace.h> | ||
| 31 | #include <linux/clocksource.h> | ||
| 32 | #include <linux/interrupt.h> | ||
| 33 | #include <linux/spinlock.h> | ||
| 34 | #include <linux/console.h> | ||
| 35 | #include <linux/threads.h> | ||
| 36 | #include <linux/uaccess.h> | ||
| 37 | #include <linux/kernel.h> | ||
| 38 | #include <linux/module.h> | ||
| 39 | #include <linux/ptrace.h> | ||
| 40 | #include <linux/string.h> | ||
| 41 | #include <linux/delay.h> | ||
| 42 | #include <linux/sched.h> | ||
| 43 | #include <linux/sysrq.h> | ||
| 44 | #include <linux/init.h> | ||
| 45 | #include <linux/kgdb.h> | ||
| 46 | #include <linux/kdb.h> | ||
| 47 | #include <linux/pid.h> | ||
| 48 | #include <linux/smp.h> | ||
| 49 | #include <linux/mm.h> | ||
| 50 | |||
| 51 | #include <asm/cacheflush.h> | ||
| 52 | #include <asm/byteorder.h> | ||
| 53 | #include <asm/atomic.h> | ||
| 54 | #include <asm/system.h> | ||
| 55 | |||
| 56 | #include "debug_core.h" | ||
| 57 | |||
| 58 | static int kgdb_break_asap; | ||
| 59 | |||
| 60 | struct debuggerinfo_struct kgdb_info[NR_CPUS]; | ||
| 61 | |||
| 62 | /** | ||
| 63 | * kgdb_connected - Is a host GDB connected to us? | ||
| 64 | */ | ||
| 65 | int kgdb_connected; | ||
| 66 | EXPORT_SYMBOL_GPL(kgdb_connected); | ||
| 67 | |||
| 68 | /* All the KGDB handlers are installed */ | ||
| 69 | int kgdb_io_module_registered; | ||
| 70 | |||
| 71 | /* Guard for recursive entry */ | ||
| 72 | static int exception_level; | ||
| 73 | |||
| 74 | struct kgdb_io *dbg_io_ops; | ||
| 75 | static DEFINE_SPINLOCK(kgdb_registration_lock); | ||
| 76 | |||
| 77 | /* kgdb console driver is loaded */ | ||
| 78 | static int kgdb_con_registered; | ||
| 79 | /* determine if kgdb console output should be used */ | ||
| 80 | static int kgdb_use_con; | ||
| 81 | /* Flag for alternate operations for early debugging */ | ||
| 82 | bool dbg_is_early = true; | ||
| 83 | /* Next cpu to become the master debug core */ | ||
| 84 | int dbg_switch_cpu; | ||
| 85 | |||
| 86 | /* Use kdb or gdbserver mode */ | ||
| 87 | int dbg_kdb_mode = 1; | ||
| 88 | |||
| 89 | static int __init opt_kgdb_con(char *str) | ||
| 90 | { | ||
| 91 | kgdb_use_con = 1; | ||
| 92 | return 0; | ||
| 93 | } | ||
| 94 | |||
| 95 | early_param("kgdbcon", opt_kgdb_con); | ||
| 96 | |||
| 97 | module_param(kgdb_use_con, int, 0644); | ||
| 98 | |||
| 99 | /* | ||
| 100 | * Holds information about breakpoints in a kernel. These breakpoints are | ||
| 101 | * added and removed by gdb. | ||
| 102 | */ | ||
| 103 | static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = { | ||
| 104 | [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED } | ||
| 105 | }; | ||
| 106 | |||
| 107 | /* | ||
| 108 | * The CPU# of the active CPU, or -1 if none: | ||
| 109 | */ | ||
| 110 | atomic_t kgdb_active = ATOMIC_INIT(-1); | ||
| 111 | EXPORT_SYMBOL_GPL(kgdb_active); | ||
| 112 | |||
| 113 | /* | ||
| 114 | * We use NR_CPUs not PERCPU, in case kgdb is used to debug early | ||
| 115 | * bootup code (which might not have percpu set up yet): | ||
| 116 | */ | ||
| 117 | static atomic_t passive_cpu_wait[NR_CPUS]; | ||
| 118 | static atomic_t cpu_in_kgdb[NR_CPUS]; | ||
| 119 | static atomic_t kgdb_break_tasklet_var; | ||
| 120 | atomic_t kgdb_setting_breakpoint; | ||
| 121 | |||
| 122 | struct task_struct *kgdb_usethread; | ||
| 123 | struct task_struct *kgdb_contthread; | ||
| 124 | |||
| 125 | int kgdb_single_step; | ||
| 126 | static pid_t kgdb_sstep_pid; | ||
| 127 | |||
| 128 | /* to keep track of the CPU which is doing the single stepping*/ | ||
| 129 | atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1); | ||
| 130 | |||
| 131 | /* | ||
| 132 | * If you are debugging a problem where roundup (the collection of | ||
| 133 | * all other CPUs) is a problem [this should be extremely rare], | ||
| 134 | * then use the nokgdbroundup option to avoid roundup. In that case | ||
| 135 | * the other CPUs might interfere with your debugging context, so | ||
| 136 | * use this with care: | ||
| 137 | */ | ||
| 138 | static int kgdb_do_roundup = 1; | ||
| 139 | |||
| 140 | static int __init opt_nokgdbroundup(char *str) | ||
| 141 | { | ||
| 142 | kgdb_do_roundup = 0; | ||
| 143 | |||
| 144 | return 0; | ||
| 145 | } | ||
| 146 | |||
| 147 | early_param("nokgdbroundup", opt_nokgdbroundup); | ||
| 148 | |||
| 149 | /* | ||
| 150 | * Finally, some KGDB code :-) | ||
| 151 | */ | ||
| 152 | |||
| 153 | /* | ||
| 154 | * Weak aliases for breakpoint management, | ||
| 155 | * can be overriden by architectures when needed: | ||
| 156 | */ | ||
| 157 | int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr) | ||
| 158 | { | ||
| 159 | int err; | ||
| 160 | |||
| 161 | err = probe_kernel_read(saved_instr, (char *)addr, BREAK_INSTR_SIZE); | ||
| 162 | if (err) | ||
| 163 | return err; | ||
| 164 | |||
| 165 | return probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr, | ||
| 166 | BREAK_INSTR_SIZE); | ||
| 167 | } | ||
| 168 | |||
| 169 | int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle) | ||
| 170 | { | ||
| 171 | return probe_kernel_write((char *)addr, | ||
| 172 | (char *)bundle, BREAK_INSTR_SIZE); | ||
| 173 | } | ||
| 174 | |||
| 175 | int __weak kgdb_validate_break_address(unsigned long addr) | ||
| 176 | { | ||
| 177 | char tmp_variable[BREAK_INSTR_SIZE]; | ||
| 178 | int err; | ||
| 179 | /* Validate setting the breakpoint and then removing it. In the | ||
| 180 | * remove fails, the kernel needs to emit a bad message because we | ||
| 181 | * are deep trouble not being able to put things back the way we | ||
| 182 | * found them. | ||
| 183 | */ | ||
| 184 | err = kgdb_arch_set_breakpoint(addr, tmp_variable); | ||
| 185 | if (err) | ||
| 186 | return err; | ||
| 187 | err = kgdb_arch_remove_breakpoint(addr, tmp_variable); | ||
| 188 | if (err) | ||
| 189 | printk(KERN_ERR "KGDB: Critical breakpoint error, kernel " | ||
| 190 | "memory destroyed at: %lx", addr); | ||
| 191 | return err; | ||
| 192 | } | ||
| 193 | |||
| 194 | unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs) | ||
| 195 | { | ||
| 196 | return instruction_pointer(regs); | ||
| 197 | } | ||
| 198 | |||
| 199 | int __weak kgdb_arch_init(void) | ||
| 200 | { | ||
| 201 | return 0; | ||
| 202 | } | ||
| 203 | |||
| 204 | int __weak kgdb_skipexception(int exception, struct pt_regs *regs) | ||
| 205 | { | ||
| 206 | return 0; | ||
| 207 | } | ||
| 208 | |||
| 209 | /** | ||
| 210 | * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb. | ||
| 211 | * @regs: Current &struct pt_regs. | ||
| 212 | * | ||
| 213 | * This function will be called if the particular architecture must | ||
| 214 | * disable hardware debugging while it is processing gdb packets or | ||
| 215 | * handling exception. | ||
| 216 | */ | ||
| 217 | void __weak kgdb_disable_hw_debug(struct pt_regs *regs) | ||
| 218 | { | ||
| 219 | } | ||
| 220 | |||
| 221 | /* | ||
| 222 | * Some architectures need cache flushes when we set/clear a | ||
| 223 | * breakpoint: | ||
| 224 | */ | ||
| 225 | static void kgdb_flush_swbreak_addr(unsigned long addr) | ||
| 226 | { | ||
| 227 | if (!CACHE_FLUSH_IS_SAFE) | ||
| 228 | return; | ||
| 229 | |||
| 230 | if (current->mm && current->mm->mmap_cache) { | ||
| 231 | flush_cache_range(current->mm->mmap_cache, | ||
| 232 | addr, addr + BREAK_INSTR_SIZE); | ||
| 233 | } | ||
| 234 | /* Force flush instruction cache if it was outside the mm */ | ||
| 235 | flush_icache_range(addr, addr + BREAK_INSTR_SIZE); | ||
| 236 | } | ||
| 237 | |||
| 238 | /* | ||
| 239 | * SW breakpoint management: | ||
| 240 | */ | ||
| 241 | int dbg_activate_sw_breakpoints(void) | ||
| 242 | { | ||
| 243 | unsigned long addr; | ||
| 244 | int error; | ||
| 245 | int ret = 0; | ||
| 246 | int i; | ||
| 247 | |||
| 248 | for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | ||
| 249 | if (kgdb_break[i].state != BP_SET) | ||
| 250 | continue; | ||
| 251 | |||
| 252 | addr = kgdb_break[i].bpt_addr; | ||
| 253 | error = kgdb_arch_set_breakpoint(addr, | ||
| 254 | kgdb_break[i].saved_instr); | ||
| 255 | if (error) { | ||
| 256 | ret = error; | ||
| 257 | printk(KERN_INFO "KGDB: BP install failed: %lx", addr); | ||
| 258 | continue; | ||
| 259 | } | ||
| 260 | |||
| 261 | kgdb_flush_swbreak_addr(addr); | ||
| 262 | kgdb_break[i].state = BP_ACTIVE; | ||
| 263 | } | ||
| 264 | return ret; | ||
| 265 | } | ||
| 266 | |||
| 267 | int dbg_set_sw_break(unsigned long addr) | ||
| 268 | { | ||
| 269 | int err = kgdb_validate_break_address(addr); | ||
| 270 | int breakno = -1; | ||
| 271 | int i; | ||
| 272 | |||
| 273 | if (err) | ||
| 274 | return err; | ||
| 275 | |||
| 276 | for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | ||
| 277 | if ((kgdb_break[i].state == BP_SET) && | ||
| 278 | (kgdb_break[i].bpt_addr == addr)) | ||
| 279 | return -EEXIST; | ||
| 280 | } | ||
| 281 | for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | ||
| 282 | if (kgdb_break[i].state == BP_REMOVED && | ||
| 283 | kgdb_break[i].bpt_addr == addr) { | ||
| 284 | breakno = i; | ||
| 285 | break; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | if (breakno == -1) { | ||
| 290 | for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | ||
| 291 | if (kgdb_break[i].state == BP_UNDEFINED) { | ||
| 292 | breakno = i; | ||
| 293 | break; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | if (breakno == -1) | ||
| 299 | return -E2BIG; | ||
| 300 | |||
| 301 | kgdb_break[breakno].state = BP_SET; | ||
| 302 | kgdb_break[breakno].type = BP_BREAKPOINT; | ||
| 303 | kgdb_break[breakno].bpt_addr = addr; | ||
| 304 | |||
| 305 | return 0; | ||
| 306 | } | ||
| 307 | |||
| 308 | int dbg_deactivate_sw_breakpoints(void) | ||
| 309 | { | ||
| 310 | unsigned long addr; | ||
| 311 | int error; | ||
| 312 | int ret = 0; | ||
| 313 | int i; | ||
| 314 | |||
| 315 | for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | ||
| 316 | if (kgdb_break[i].state != BP_ACTIVE) | ||
| 317 | continue; | ||
| 318 | addr = kgdb_break[i].bpt_addr; | ||
| 319 | error = kgdb_arch_remove_breakpoint(addr, | ||
| 320 | kgdb_break[i].saved_instr); | ||
| 321 | if (error) { | ||
| 322 | printk(KERN_INFO "KGDB: BP remove failed: %lx\n", addr); | ||
| 323 | ret = error; | ||
| 324 | } | ||
| 325 | |||
| 326 | kgdb_flush_swbreak_addr(addr); | ||
| 327 | kgdb_break[i].state = BP_SET; | ||
| 328 | } | ||
| 329 | return ret; | ||
| 330 | } | ||
| 331 | |||
| 332 | int dbg_remove_sw_break(unsigned long addr) | ||
| 333 | { | ||
| 334 | int i; | ||
| 335 | |||
| 336 | for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | ||
| 337 | if ((kgdb_break[i].state == BP_SET) && | ||
| 338 | (kgdb_break[i].bpt_addr == addr)) { | ||
| 339 | kgdb_break[i].state = BP_REMOVED; | ||
| 340 | return 0; | ||
| 341 | } | ||
| 342 | } | ||
| 343 | return -ENOENT; | ||
| 344 | } | ||
| 345 | |||
| 346 | int kgdb_isremovedbreak(unsigned long addr) | ||
| 347 | { | ||
| 348 | int i; | ||
| 349 | |||
| 350 | for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | ||
| 351 | if ((kgdb_break[i].state == BP_REMOVED) && | ||
| 352 | (kgdb_break[i].bpt_addr == addr)) | ||
| 353 | return 1; | ||
| 354 | } | ||
| 355 | return 0; | ||
| 356 | } | ||
| 357 | |||
| 358 | int dbg_remove_all_break(void) | ||
| 359 | { | ||
| 360 | unsigned long addr; | ||
| 361 | int error; | ||
| 362 | int i; | ||
| 363 | |||
| 364 | /* Clear memory breakpoints. */ | ||
| 365 | for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | ||
| 366 | if (kgdb_break[i].state != BP_ACTIVE) | ||
| 367 | goto setundefined; | ||
| 368 | addr = kgdb_break[i].bpt_addr; | ||
| 369 | error = kgdb_arch_remove_breakpoint(addr, | ||
| 370 | kgdb_break[i].saved_instr); | ||
| 371 | if (error) | ||
| 372 | printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n", | ||
| 373 | addr); | ||
| 374 | setundefined: | ||
| 375 | kgdb_break[i].state = BP_UNDEFINED; | ||
| 376 | } | ||
| 377 | |||
| 378 | /* Clear hardware breakpoints. */ | ||
| 379 | if (arch_kgdb_ops.remove_all_hw_break) | ||
| 380 | arch_kgdb_ops.remove_all_hw_break(); | ||
| 381 | |||
| 382 | return 0; | ||
| 383 | } | ||
| 384 | |||
| 385 | /* | ||
| 386 | * Return true if there is a valid kgdb I/O module. Also if no | ||
| 387 | * debugger is attached a message can be printed to the console about | ||
| 388 | * waiting for the debugger to attach. | ||
| 389 | * | ||
| 390 | * The print_wait argument is only to be true when called from inside | ||
| 391 | * the core kgdb_handle_exception, because it will wait for the | ||
| 392 | * debugger to attach. | ||
| 393 | */ | ||
| 394 | static int kgdb_io_ready(int print_wait) | ||
| 395 | { | ||
| 396 | if (!dbg_io_ops) | ||
| 397 | return 0; | ||
| 398 | if (kgdb_connected) | ||
| 399 | return 1; | ||
| 400 | if (atomic_read(&kgdb_setting_breakpoint)) | ||
| 401 | return 1; | ||
| 402 | if (print_wait) { | ||
| 403 | #ifdef CONFIG_KGDB_KDB | ||
| 404 | if (!dbg_kdb_mode) | ||
| 405 | printk(KERN_CRIT "KGDB: waiting... or $3#33 for KDB\n"); | ||
| 406 | #else | ||
| 407 | printk(KERN_CRIT "KGDB: Waiting for remote debugger\n"); | ||
| 408 | #endif | ||
| 409 | } | ||
| 410 | return 1; | ||
| 411 | } | ||
| 412 | |||
| 413 | static int kgdb_reenter_check(struct kgdb_state *ks) | ||
| 414 | { | ||
| 415 | unsigned long addr; | ||
| 416 | |||
| 417 | if (atomic_read(&kgdb_active) != raw_smp_processor_id()) | ||
| 418 | return 0; | ||
| 419 | |||
| 420 | /* Panic on recursive debugger calls: */ | ||
| 421 | exception_level++; | ||
| 422 | addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs); | ||
| 423 | dbg_deactivate_sw_breakpoints(); | ||
| 424 | |||
| 425 | /* | ||
| 426 | * If the break point removed ok at the place exception | ||
| 427 | * occurred, try to recover and print a warning to the end | ||
| 428 | * user because the user planted a breakpoint in a place that | ||
| 429 | * KGDB needs in order to function. | ||
| 430 | */ | ||
| 431 | if (dbg_remove_sw_break(addr) == 0) { | ||
| 432 | exception_level = 0; | ||
| 433 | kgdb_skipexception(ks->ex_vector, ks->linux_regs); | ||
| 434 | dbg_activate_sw_breakpoints(); | ||
| 435 | printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n", | ||
| 436 | addr); | ||
| 437 | WARN_ON_ONCE(1); | ||
| 438 | |||
| 439 | return 1; | ||
| 440 | } | ||
| 441 | dbg_remove_all_break(); | ||
| 442 | kgdb_skipexception(ks->ex_vector, ks->linux_regs); | ||
| 443 | |||
| 444 | if (exception_level > 1) { | ||
| 445 | dump_stack(); | ||
| 446 | panic("Recursive entry to debugger"); | ||
| 447 | } | ||
| 448 | |||
| 449 | printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n"); | ||
| 450 | #ifdef CONFIG_KGDB_KDB | ||
| 451 | /* Allow kdb to debug itself one level */ | ||
| 452 | return 0; | ||
| 453 | #endif | ||
| 454 | dump_stack(); | ||
| 455 | panic("Recursive entry to debugger"); | ||
| 456 | |||
| 457 | return 1; | ||
| 458 | } | ||
| 459 | |||
| 460 | static void dbg_cpu_switch(int cpu, int next_cpu) | ||
| 461 | { | ||
| 462 | /* Mark the cpu we are switching away from as a slave when it | ||
| 463 | * holds the kgdb_active token. This must be done so that the | ||
| 464 | * that all the cpus wait in for the debug core will not enter | ||
| 465 | * again as the master. */ | ||
| 466 | if (cpu == atomic_read(&kgdb_active)) { | ||
| 467 | kgdb_info[cpu].exception_state |= DCPU_IS_SLAVE; | ||
| 468 | kgdb_info[cpu].exception_state &= ~DCPU_WANT_MASTER; | ||
| 469 | } | ||
| 470 | kgdb_info[next_cpu].exception_state |= DCPU_NEXT_MASTER; | ||
| 471 | } | ||
| 472 | |||
| 473 | static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs) | ||
| 474 | { | ||
| 475 | unsigned long flags; | ||
| 476 | int sstep_tries = 100; | ||
| 477 | int error; | ||
| 478 | int i, cpu; | ||
| 479 | int trace_on = 0; | ||
| 480 | acquirelock: | ||
| 481 | /* | ||
| 482 | * Interrupts will be restored by the 'trap return' code, except when | ||
| 483 | * single stepping. | ||
| 484 | */ | ||
| 485 | local_irq_save(flags); | ||
| 486 | |||
| 487 | cpu = ks->cpu; | ||
| 488 | kgdb_info[cpu].debuggerinfo = regs; | ||
| 489 | kgdb_info[cpu].task = current; | ||
| 490 | kgdb_info[cpu].ret_state = 0; | ||
| 491 | kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT; | ||
| 492 | /* | ||
| 493 | * Make sure the above info reaches the primary CPU before | ||
| 494 | * our cpu_in_kgdb[] flag setting does: | ||
| 495 | */ | ||
| 496 | atomic_inc(&cpu_in_kgdb[cpu]); | ||
| 497 | |||
| 498 | if (exception_level == 1) | ||
| 499 | goto cpu_master_loop; | ||
| 500 | |||
| 501 | /* | ||
| 502 | * CPU will loop if it is a slave or request to become a kgdb | ||
| 503 | * master cpu and acquire the kgdb_active lock: | ||
| 504 | */ | ||
| 505 | while (1) { | ||
| 506 | cpu_loop: | ||
| 507 | if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) { | ||
| 508 | kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER; | ||
| 509 | goto cpu_master_loop; | ||
| 510 | } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) { | ||
| 511 | if (atomic_cmpxchg(&kgdb_active, -1, cpu) == cpu) | ||
| 512 | break; | ||
| 513 | } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) { | ||
| 514 | if (!atomic_read(&passive_cpu_wait[cpu])) | ||
| 515 | goto return_normal; | ||
| 516 | } else { | ||
| 517 | return_normal: | ||
| 518 | /* Return to normal operation by executing any | ||
| 519 | * hw breakpoint fixup. | ||
| 520 | */ | ||
| 521 | if (arch_kgdb_ops.correct_hw_break) | ||
| 522 | arch_kgdb_ops.correct_hw_break(); | ||
| 523 | if (trace_on) | ||
| 524 | tracing_on(); | ||
| 525 | atomic_dec(&cpu_in_kgdb[cpu]); | ||
| 526 | touch_softlockup_watchdog_sync(); | ||
| 527 | clocksource_touch_watchdog(); | ||
| 528 | local_irq_restore(flags); | ||
| 529 | return 0; | ||
| 530 | } | ||
| 531 | cpu_relax(); | ||
| 532 | } | ||
| 533 | |||
| 534 | /* | ||
| 535 | * For single stepping, try to only enter on the processor | ||
| 536 | * that was single stepping. To gaurd against a deadlock, the | ||
| 537 | * kernel will only try for the value of sstep_tries before | ||
| 538 | * giving up and continuing on. | ||
| 539 | */ | ||
| 540 | if (atomic_read(&kgdb_cpu_doing_single_step) != -1 && | ||
| 541 | (kgdb_info[cpu].task && | ||
| 542 | kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) { | ||
| 543 | atomic_set(&kgdb_active, -1); | ||
| 544 | touch_softlockup_watchdog_sync(); | ||
| 545 | clocksource_touch_watchdog(); | ||
| 546 | local_irq_restore(flags); | ||
| 547 | |||
| 548 | goto acquirelock; | ||
| 549 | } | ||
| 550 | |||
| 551 | if (!kgdb_io_ready(1)) { | ||
| 552 | kgdb_info[cpu].ret_state = 1; | ||
| 553 | goto kgdb_restore; /* No I/O connection, resume the system */ | ||
| 554 | } | ||
| 555 | |||
| 556 | /* | ||
| 557 | * Don't enter if we have hit a removed breakpoint. | ||
| 558 | */ | ||
| 559 | if (kgdb_skipexception(ks->ex_vector, ks->linux_regs)) | ||
| 560 | goto kgdb_restore; | ||
| 561 | |||
| 562 | /* Call the I/O driver's pre_exception routine */ | ||
| 563 | if (dbg_io_ops->pre_exception) | ||
| 564 | dbg_io_ops->pre_exception(); | ||
| 565 | |||
| 566 | kgdb_disable_hw_debug(ks->linux_regs); | ||
| 567 | |||
| 568 | /* | ||
| 569 | * Get the passive CPU lock which will hold all the non-primary | ||
| 570 | * CPU in a spin state while the debugger is active | ||
| 571 | */ | ||
| 572 | if (!kgdb_single_step) { | ||
| 573 | for (i = 0; i < NR_CPUS; i++) | ||
| 574 | atomic_inc(&passive_cpu_wait[i]); | ||
| 575 | } | ||
| 576 | |||
| 577 | #ifdef CONFIG_SMP | ||
| 578 | /* Signal the other CPUs to enter kgdb_wait() */ | ||
| 579 | if ((!kgdb_single_step) && kgdb_do_roundup) | ||
| 580 | kgdb_roundup_cpus(flags); | ||
| 581 | #endif | ||
| 582 | |||
| 583 | /* | ||
| 584 | * Wait for the other CPUs to be notified and be waiting for us: | ||
| 585 | */ | ||
| 586 | for_each_online_cpu(i) { | ||
| 587 | while (kgdb_do_roundup && !atomic_read(&cpu_in_kgdb[i])) | ||
| 588 | cpu_relax(); | ||
| 589 | } | ||
| 590 | |||
| 591 | /* | ||
| 592 | * At this point the primary processor is completely | ||
| 593 | * in the debugger and all secondary CPUs are quiescent | ||
| 594 | */ | ||
| 595 | dbg_deactivate_sw_breakpoints(); | ||
| 596 | kgdb_single_step = 0; | ||
| 597 | kgdb_contthread = current; | ||
| 598 | exception_level = 0; | ||
| 599 | trace_on = tracing_is_on(); | ||
| 600 | if (trace_on) | ||
| 601 | tracing_off(); | ||
| 602 | |||
| 603 | while (1) { | ||
| 604 | cpu_master_loop: | ||
| 605 | if (dbg_kdb_mode) { | ||
| 606 | kgdb_connected = 1; | ||
| 607 | error = kdb_stub(ks); | ||
| 608 | kgdb_connected = 0; | ||
| 609 | } else { | ||
| 610 | error = gdb_serial_stub(ks); | ||
| 611 | } | ||
| 612 | |||
| 613 | if (error == DBG_PASS_EVENT) { | ||
| 614 | dbg_kdb_mode = !dbg_kdb_mode; | ||
| 615 | } else if (error == DBG_SWITCH_CPU_EVENT) { | ||
| 616 | dbg_cpu_switch(cpu, dbg_switch_cpu); | ||
| 617 | goto cpu_loop; | ||
| 618 | } else { | ||
| 619 | kgdb_info[cpu].ret_state = error; | ||
| 620 | break; | ||
| 621 | } | ||
| 622 | } | ||
| 623 | |||
| 624 | /* Call the I/O driver's post_exception routine */ | ||
| 625 | if (dbg_io_ops->post_exception) | ||
| 626 | dbg_io_ops->post_exception(); | ||
| 627 | |||
| 628 | atomic_dec(&cpu_in_kgdb[ks->cpu]); | ||
| 629 | |||
| 630 | if (!kgdb_single_step) { | ||
| 631 | for (i = NR_CPUS-1; i >= 0; i--) | ||
| 632 | atomic_dec(&passive_cpu_wait[i]); | ||
| 633 | /* | ||
| 634 | * Wait till all the CPUs have quit from the debugger, | ||
| 635 | * but allow a CPU that hit an exception and is | ||
| 636 | * waiting to become the master to remain in the debug | ||
| 637 | * core. | ||
| 638 | */ | ||
| 639 | for_each_online_cpu(i) { | ||
| 640 | while (kgdb_do_roundup && | ||
| 641 | atomic_read(&cpu_in_kgdb[i]) && | ||
| 642 | !(kgdb_info[i].exception_state & | ||
| 643 | DCPU_WANT_MASTER)) | ||
| 644 | cpu_relax(); | ||
| 645 | } | ||
| 646 | } | ||
| 647 | |||
| 648 | kgdb_restore: | ||
| 649 | if (atomic_read(&kgdb_cpu_doing_single_step) != -1) { | ||
| 650 | int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step); | ||
| 651 | if (kgdb_info[sstep_cpu].task) | ||
| 652 | kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid; | ||
| 653 | else | ||
| 654 | kgdb_sstep_pid = 0; | ||
| 655 | } | ||
| 656 | if (trace_on) | ||
| 657 | tracing_on(); | ||
| 658 | /* Free kgdb_active */ | ||
| 659 | atomic_set(&kgdb_active, -1); | ||
| 660 | touch_softlockup_watchdog_sync(); | ||
| 661 | clocksource_touch_watchdog(); | ||
| 662 | local_irq_restore(flags); | ||
| 663 | |||
| 664 | return kgdb_info[cpu].ret_state; | ||
| 665 | } | ||
| 666 | |||
| 667 | /* | ||
| 668 | * kgdb_handle_exception() - main entry point from a kernel exception | ||
| 669 | * | ||
| 670 | * Locking hierarchy: | ||
| 671 | * interface locks, if any (begin_session) | ||
| 672 | * kgdb lock (kgdb_active) | ||
| 673 | */ | ||
| 674 | int | ||
| 675 | kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs) | ||
| 676 | { | ||
| 677 | struct kgdb_state kgdb_var; | ||
| 678 | struct kgdb_state *ks = &kgdb_var; | ||
| 679 | int ret; | ||
| 680 | |||
| 681 | ks->cpu = raw_smp_processor_id(); | ||
| 682 | ks->ex_vector = evector; | ||
| 683 | ks->signo = signo; | ||
| 684 | ks->err_code = ecode; | ||
| 685 | ks->kgdb_usethreadid = 0; | ||
| 686 | ks->linux_regs = regs; | ||
| 687 | |||
| 688 | if (kgdb_reenter_check(ks)) | ||
| 689 | return 0; /* Ouch, double exception ! */ | ||
| 690 | kgdb_info[ks->cpu].exception_state |= DCPU_WANT_MASTER; | ||
| 691 | ret = kgdb_cpu_enter(ks, regs); | ||
| 692 | kgdb_info[ks->cpu].exception_state &= ~(DCPU_WANT_MASTER | | ||
| 693 | DCPU_IS_SLAVE); | ||
| 694 | return ret; | ||
| 695 | } | ||
| 696 | |||
| 697 | int kgdb_nmicallback(int cpu, void *regs) | ||
| 698 | { | ||
| 699 | #ifdef CONFIG_SMP | ||
| 700 | struct kgdb_state kgdb_var; | ||
| 701 | struct kgdb_state *ks = &kgdb_var; | ||
| 702 | |||
| 703 | memset(ks, 0, sizeof(struct kgdb_state)); | ||
| 704 | ks->cpu = cpu; | ||
| 705 | ks->linux_regs = regs; | ||
| 706 | |||
| 707 | if (!atomic_read(&cpu_in_kgdb[cpu]) && | ||
| 708 | atomic_read(&kgdb_active) != -1 && | ||
| 709 | atomic_read(&kgdb_active) != cpu) { | ||
| 710 | kgdb_info[cpu].exception_state |= DCPU_IS_SLAVE; | ||
| 711 | kgdb_cpu_enter(ks, regs); | ||
| 712 | kgdb_info[cpu].exception_state &= ~DCPU_IS_SLAVE; | ||
| 713 | return 0; | ||
| 714 | } | ||
| 715 | #endif | ||
| 716 | return 1; | ||
| 717 | } | ||
| 718 | |||
| 719 | static void kgdb_console_write(struct console *co, const char *s, | ||
| 720 | unsigned count) | ||
| 721 | { | ||
| 722 | unsigned long flags; | ||
| 723 | |||
| 724 | /* If we're debugging, or KGDB has not connected, don't try | ||
| 725 | * and print. */ | ||
| 726 | if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode) | ||
| 727 | return; | ||
| 728 | |||
| 729 | local_irq_save(flags); | ||
| 730 | gdbstub_msg_write(s, count); | ||
| 731 | local_irq_restore(flags); | ||
| 732 | } | ||
| 733 | |||
| 734 | static struct console kgdbcons = { | ||
| 735 | .name = "kgdb", | ||
| 736 | .write = kgdb_console_write, | ||
| 737 | .flags = CON_PRINTBUFFER | CON_ENABLED, | ||
| 738 | .index = -1, | ||
| 739 | }; | ||
| 740 | |||
| 741 | #ifdef CONFIG_MAGIC_SYSRQ | ||
| 742 | static void sysrq_handle_dbg(int key, struct tty_struct *tty) | ||
| 743 | { | ||
| 744 | if (!dbg_io_ops) { | ||
| 745 | printk(KERN_CRIT "ERROR: No KGDB I/O module available\n"); | ||
| 746 | return; | ||
| 747 | } | ||
| 748 | if (!kgdb_connected) { | ||
| 749 | #ifdef CONFIG_KGDB_KDB | ||
| 750 | if (!dbg_kdb_mode) | ||
| 751 | printk(KERN_CRIT "KGDB or $3#33 for KDB\n"); | ||
| 752 | #else | ||
| 753 | printk(KERN_CRIT "Entering KGDB\n"); | ||
| 754 | #endif | ||
| 755 | } | ||
| 756 | |||
| 757 | kgdb_breakpoint(); | ||
| 758 | } | ||
| 759 | |||
| 760 | static struct sysrq_key_op sysrq_dbg_op = { | ||
| 761 | .handler = sysrq_handle_dbg, | ||
| 762 | .help_msg = "debug(G)", | ||
| 763 | .action_msg = "DEBUG", | ||
| 764 | }; | ||
| 765 | #endif | ||
| 766 | |||
| 767 | static int kgdb_panic_event(struct notifier_block *self, | ||
| 768 | unsigned long val, | ||
| 769 | void *data) | ||
| 770 | { | ||
| 771 | if (dbg_kdb_mode) | ||
| 772 | kdb_printf("PANIC: %s\n", (char *)data); | ||
| 773 | kgdb_breakpoint(); | ||
| 774 | return NOTIFY_DONE; | ||
| 775 | } | ||
| 776 | |||
| 777 | static struct notifier_block kgdb_panic_event_nb = { | ||
| 778 | .notifier_call = kgdb_panic_event, | ||
| 779 | .priority = INT_MAX, | ||
| 780 | }; | ||
| 781 | |||
| 782 | void __weak kgdb_arch_late(void) | ||
| 783 | { | ||
| 784 | } | ||
| 785 | |||
| 786 | void __init dbg_late_init(void) | ||
| 787 | { | ||
| 788 | dbg_is_early = false; | ||
| 789 | if (kgdb_io_module_registered) | ||
| 790 | kgdb_arch_late(); | ||
| 791 | kdb_init(KDB_INIT_FULL); | ||
| 792 | } | ||
| 793 | |||
| 794 | static void kgdb_register_callbacks(void) | ||
| 795 | { | ||
| 796 | if (!kgdb_io_module_registered) { | ||
| 797 | kgdb_io_module_registered = 1; | ||
| 798 | kgdb_arch_init(); | ||
| 799 | if (!dbg_is_early) | ||
| 800 | kgdb_arch_late(); | ||
| 801 | atomic_notifier_chain_register(&panic_notifier_list, | ||
| 802 | &kgdb_panic_event_nb); | ||
| 803 | #ifdef CONFIG_MAGIC_SYSRQ | ||
| 804 | register_sysrq_key('g', &sysrq_dbg_op); | ||
| 805 | #endif | ||
| 806 | if (kgdb_use_con && !kgdb_con_registered) { | ||
| 807 | register_console(&kgdbcons); | ||
| 808 | kgdb_con_registered = 1; | ||
| 809 | } | ||
| 810 | } | ||
| 811 | } | ||
| 812 | |||
| 813 | static void kgdb_unregister_callbacks(void) | ||
| 814 | { | ||
| 815 | /* | ||
| 816 | * When this routine is called KGDB should unregister from the | ||
| 817 | * panic handler and clean up, making sure it is not handling any | ||
| 818 | * break exceptions at the time. | ||
| 819 | */ | ||
| 820 | if (kgdb_io_module_registered) { | ||
| 821 | kgdb_io_module_registered = 0; | ||
| 822 | atomic_notifier_chain_unregister(&panic_notifier_list, | ||
| 823 | &kgdb_panic_event_nb); | ||
| 824 | kgdb_arch_exit(); | ||
| 825 | #ifdef CONFIG_MAGIC_SYSRQ | ||
| 826 | unregister_sysrq_key('g', &sysrq_dbg_op); | ||
| 827 | #endif | ||
| 828 | if (kgdb_con_registered) { | ||
| 829 | unregister_console(&kgdbcons); | ||
| 830 | kgdb_con_registered = 0; | ||
| 831 | } | ||
| 832 | } | ||
| 833 | } | ||
| 834 | |||
| 835 | /* | ||
| 836 | * There are times a tasklet needs to be used vs a compiled in | ||
| 837 | * break point so as to cause an exception outside a kgdb I/O module, | ||
| 838 | * such as is the case with kgdboe, where calling a breakpoint in the | ||
| 839 | * I/O driver itself would be fatal. | ||
| 840 | */ | ||
| 841 | static void kgdb_tasklet_bpt(unsigned long ing) | ||
| 842 | { | ||
| 843 | kgdb_breakpoint(); | ||
| 844 | atomic_set(&kgdb_break_tasklet_var, 0); | ||
| 845 | } | ||
| 846 | |||
| 847 | static DECLARE_TASKLET(kgdb_tasklet_breakpoint, kgdb_tasklet_bpt, 0); | ||
| 848 | |||
| 849 | void kgdb_schedule_breakpoint(void) | ||
| 850 | { | ||
| 851 | if (atomic_read(&kgdb_break_tasklet_var) || | ||
| 852 | atomic_read(&kgdb_active) != -1 || | ||
| 853 | atomic_read(&kgdb_setting_breakpoint)) | ||
| 854 | return; | ||
| 855 | atomic_inc(&kgdb_break_tasklet_var); | ||
| 856 | tasklet_schedule(&kgdb_tasklet_breakpoint); | ||
| 857 | } | ||
| 858 | EXPORT_SYMBOL_GPL(kgdb_schedule_breakpoint); | ||
| 859 | |||
| 860 | static void kgdb_initial_breakpoint(void) | ||
| 861 | { | ||
| 862 | kgdb_break_asap = 0; | ||
| 863 | |||
| 864 | printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n"); | ||
| 865 | kgdb_breakpoint(); | ||
| 866 | } | ||
| 867 | |||
| 868 | /** | ||
| 869 | * kgdb_register_io_module - register KGDB IO module | ||
| 870 | * @new_dbg_io_ops: the io ops vector | ||
| 871 | * | ||
| 872 | * Register it with the KGDB core. | ||
| 873 | */ | ||
| 874 | int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops) | ||
| 875 | { | ||
| 876 | int err; | ||
| 877 | |||
| 878 | spin_lock(&kgdb_registration_lock); | ||
| 879 | |||
| 880 | if (dbg_io_ops) { | ||
| 881 | spin_unlock(&kgdb_registration_lock); | ||
| 882 | |||
| 883 | printk(KERN_ERR "kgdb: Another I/O driver is already " | ||
| 884 | "registered with KGDB.\n"); | ||
| 885 | return -EBUSY; | ||
| 886 | } | ||
| 887 | |||
| 888 | if (new_dbg_io_ops->init) { | ||
| 889 | err = new_dbg_io_ops->init(); | ||
| 890 | if (err) { | ||
| 891 | spin_unlock(&kgdb_registration_lock); | ||
| 892 | return err; | ||
| 893 | } | ||
| 894 | } | ||
| 895 | |||
| 896 | dbg_io_ops = new_dbg_io_ops; | ||
| 897 | |||
| 898 | spin_unlock(&kgdb_registration_lock); | ||
| 899 | |||
| 900 | printk(KERN_INFO "kgdb: Registered I/O driver %s.\n", | ||
| 901 | new_dbg_io_ops->name); | ||
| 902 | |||
| 903 | /* Arm KGDB now. */ | ||
| 904 | kgdb_register_callbacks(); | ||
| 905 | |||
| 906 | if (kgdb_break_asap) | ||
| 907 | kgdb_initial_breakpoint(); | ||
| 908 | |||
| 909 | return 0; | ||
| 910 | } | ||
| 911 | EXPORT_SYMBOL_GPL(kgdb_register_io_module); | ||
| 912 | |||
| 913 | /** | ||
| 914 | * kkgdb_unregister_io_module - unregister KGDB IO module | ||
| 915 | * @old_dbg_io_ops: the io ops vector | ||
| 916 | * | ||
| 917 | * Unregister it with the KGDB core. | ||
| 918 | */ | ||
| 919 | void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops) | ||
| 920 | { | ||
| 921 | BUG_ON(kgdb_connected); | ||
| 922 | |||
| 923 | /* | ||
| 924 | * KGDB is no longer able to communicate out, so | ||
| 925 | * unregister our callbacks and reset state. | ||
| 926 | */ | ||
| 927 | kgdb_unregister_callbacks(); | ||
| 928 | |||
| 929 | spin_lock(&kgdb_registration_lock); | ||
| 930 | |||
| 931 | WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops); | ||
| 932 | dbg_io_ops = NULL; | ||
| 933 | |||
| 934 | spin_unlock(&kgdb_registration_lock); | ||
| 935 | |||
| 936 | printk(KERN_INFO | ||
| 937 | "kgdb: Unregistered I/O driver %s, debugger disabled.\n", | ||
| 938 | old_dbg_io_ops->name); | ||
| 939 | } | ||
| 940 | EXPORT_SYMBOL_GPL(kgdb_unregister_io_module); | ||
| 941 | |||
| 942 | int dbg_io_get_char(void) | ||
| 943 | { | ||
| 944 | int ret = dbg_io_ops->read_char(); | ||
| 945 | if (ret == NO_POLL_CHAR) | ||
| 946 | return -1; | ||
| 947 | if (!dbg_kdb_mode) | ||
| 948 | return ret; | ||
| 949 | if (ret == 127) | ||
| 950 | return 8; | ||
| 951 | return ret; | ||
| 952 | } | ||
| 953 | |||
| 954 | /** | ||
| 955 | * kgdb_breakpoint - generate breakpoint exception | ||
| 956 | * | ||
| 957 | * This function will generate a breakpoint exception. It is used at the | ||
| 958 | * beginning of a program to sync up with a debugger and can be used | ||
| 959 | * otherwise as a quick means to stop program execution and "break" into | ||
| 960 | * the debugger. | ||
| 961 | */ | ||
| 962 | void kgdb_breakpoint(void) | ||
| 963 | { | ||
| 964 | atomic_inc(&kgdb_setting_breakpoint); | ||
| 965 | wmb(); /* Sync point before breakpoint */ | ||
| 966 | arch_kgdb_breakpoint(); | ||
| 967 | wmb(); /* Sync point after breakpoint */ | ||
| 968 | atomic_dec(&kgdb_setting_breakpoint); | ||
| 969 | } | ||
| 970 | EXPORT_SYMBOL_GPL(kgdb_breakpoint); | ||
| 971 | |||
| 972 | static int __init opt_kgdb_wait(char *str) | ||
| 973 | { | ||
| 974 | kgdb_break_asap = 1; | ||
| 975 | |||
| 976 | kdb_init(KDB_INIT_EARLY); | ||
| 977 | if (kgdb_io_module_registered) | ||
| 978 | kgdb_initial_breakpoint(); | ||
| 979 | |||
| 980 | return 0; | ||
| 981 | } | ||
| 982 | |||
| 983 | early_param("kgdbwait", opt_kgdb_wait); | ||
diff --git a/kernel/debug/debug_core.h b/kernel/debug/debug_core.h new file mode 100644 index 000000000000..c5d753d80f67 --- /dev/null +++ b/kernel/debug/debug_core.h | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | /* | ||
| 2 | * Created by: Jason Wessel <jason.wessel@windriver.com> | ||
| 3 | * | ||
| 4 | * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. | ||
| 5 | * | ||
| 6 | * This file is licensed under the terms of the GNU General Public | ||
| 7 | * License version 2. This program is licensed "as is" without any | ||
| 8 | * warranty of any kind, whether express or implied. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #ifndef _DEBUG_CORE_H_ | ||
| 12 | #define _DEBUG_CORE_H_ | ||
| 13 | /* | ||
| 14 | * These are the private implementation headers between the kernel | ||
| 15 | * debugger core and the debugger front end code. | ||
| 16 | */ | ||
| 17 | |||
| 18 | /* kernel debug core data structures */ | ||
| 19 | struct kgdb_state { | ||
| 20 | int ex_vector; | ||
| 21 | int signo; | ||
| 22 | int err_code; | ||
| 23 | int cpu; | ||
| 24 | int pass_exception; | ||
| 25 | unsigned long thr_query; | ||
| 26 | unsigned long threadid; | ||
| 27 | long kgdb_usethreadid; | ||
| 28 | struct pt_regs *linux_regs; | ||
| 29 | }; | ||
| 30 | |||
| 31 | /* Exception state values */ | ||
| 32 | #define DCPU_WANT_MASTER 0x1 /* Waiting to become a master kgdb cpu */ | ||
| 33 | #define DCPU_NEXT_MASTER 0x2 /* Transition from one master cpu to another */ | ||
| 34 | #define DCPU_IS_SLAVE 0x4 /* Slave cpu enter exception */ | ||
| 35 | #define DCPU_SSTEP 0x8 /* CPU is single stepping */ | ||
| 36 | |||
| 37 | struct debuggerinfo_struct { | ||
| 38 | void *debuggerinfo; | ||
| 39 | struct task_struct *task; | ||
| 40 | int exception_state; | ||
| 41 | int ret_state; | ||
| 42 | int irq_depth; | ||
| 43 | }; | ||
| 44 | |||
| 45 | extern struct debuggerinfo_struct kgdb_info[]; | ||
| 46 | |||
| 47 | /* kernel debug core break point routines */ | ||
| 48 | extern int dbg_remove_all_break(void); | ||
| 49 | extern int dbg_set_sw_break(unsigned long addr); | ||
| 50 | extern int dbg_remove_sw_break(unsigned long addr); | ||
| 51 | extern int dbg_activate_sw_breakpoints(void); | ||
| 52 | extern int dbg_deactivate_sw_breakpoints(void); | ||
| 53 | |||
| 54 | /* polled character access to i/o module */ | ||
| 55 | extern int dbg_io_get_char(void); | ||
| 56 | |||
| 57 | /* stub return value for switching between the gdbstub and kdb */ | ||
| 58 | #define DBG_PASS_EVENT -12345 | ||
| 59 | /* Switch from one cpu to another */ | ||
| 60 | #define DBG_SWITCH_CPU_EVENT -123456 | ||
| 61 | extern int dbg_switch_cpu; | ||
| 62 | |||
| 63 | /* gdbstub interface functions */ | ||
| 64 | extern int gdb_serial_stub(struct kgdb_state *ks); | ||
| 65 | extern void gdbstub_msg_write(const char *s, int len); | ||
| 66 | |||
| 67 | /* gdbstub functions used for kdb <-> gdbstub transition */ | ||
| 68 | extern int gdbstub_state(struct kgdb_state *ks, char *cmd); | ||
| 69 | extern int dbg_kdb_mode; | ||
| 70 | |||
| 71 | #ifdef CONFIG_KGDB_KDB | ||
| 72 | extern int kdb_stub(struct kgdb_state *ks); | ||
| 73 | extern int kdb_parse(const char *cmdstr); | ||
| 74 | #else /* ! CONFIG_KGDB_KDB */ | ||
| 75 | static inline int kdb_stub(struct kgdb_state *ks) | ||
| 76 | { | ||
| 77 | return DBG_PASS_EVENT; | ||
| 78 | } | ||
| 79 | #endif /* CONFIG_KGDB_KDB */ | ||
| 80 | |||
| 81 | #endif /* _DEBUG_CORE_H_ */ | ||
diff --git a/kernel/debug/gdbstub.c b/kernel/debug/gdbstub.c new file mode 100644 index 000000000000..6e81fd59566b --- /dev/null +++ b/kernel/debug/gdbstub.c | |||
| @@ -0,0 +1,1014 @@ | |||
| 1 | /* | ||
| 2 | * Kernel Debug Core | ||
| 3 | * | ||
| 4 | * Maintainer: Jason Wessel <jason.wessel@windriver.com> | ||
| 5 | * | ||
| 6 | * Copyright (C) 2000-2001 VERITAS Software Corporation. | ||
| 7 | * Copyright (C) 2002-2004 Timesys Corporation | ||
| 8 | * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com> | ||
| 9 | * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz> | ||
| 10 | * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org> | ||
| 11 | * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd. | ||
| 12 | * Copyright (C) 2005-2009 Wind River Systems, Inc. | ||
| 13 | * Copyright (C) 2007 MontaVista Software, Inc. | ||
| 14 | * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | ||
| 15 | * | ||
| 16 | * Contributors at various stages not listed above: | ||
| 17 | * Jason Wessel ( jason.wessel@windriver.com ) | ||
| 18 | * George Anzinger <george@mvista.com> | ||
| 19 | * Anurekh Saxena (anurekh.saxena@timesys.com) | ||
| 20 | * Lake Stevens Instrument Division (Glenn Engel) | ||
| 21 | * Jim Kingdon, Cygnus Support. | ||
| 22 | * | ||
| 23 | * Original KGDB stub: David Grothe <dave@gcom.com>, | ||
| 24 | * Tigran Aivazian <tigran@sco.com> | ||
| 25 | * | ||
| 26 | * This file is licensed under the terms of the GNU General Public License | ||
| 27 | * version 2. This program is licensed "as is" without any warranty of any | ||
| 28 | * kind, whether express or implied. | ||
| 29 | */ | ||
| 30 | |||
| 31 | #include <linux/kernel.h> | ||
| 32 | #include <linux/kgdb.h> | ||
| 33 | #include <linux/kdb.h> | ||
| 34 | #include <linux/reboot.h> | ||
| 35 | #include <linux/uaccess.h> | ||
| 36 | #include <asm/cacheflush.h> | ||
| 37 | #include <asm/unaligned.h> | ||
| 38 | #include "debug_core.h" | ||
| 39 | |||
| 40 | #define KGDB_MAX_THREAD_QUERY 17 | ||
| 41 | |||
| 42 | /* Our I/O buffers. */ | ||
| 43 | static char remcom_in_buffer[BUFMAX]; | ||
| 44 | static char remcom_out_buffer[BUFMAX]; | ||
| 45 | |||
| 46 | /* Storage for the registers, in GDB format. */ | ||
| 47 | static unsigned long gdb_regs[(NUMREGBYTES + | ||
| 48 | sizeof(unsigned long) - 1) / | ||
| 49 | sizeof(unsigned long)]; | ||
| 50 | |||
| 51 | /* | ||
| 52 | * GDB remote protocol parser: | ||
| 53 | */ | ||
| 54 | |||
| 55 | static int hex(char ch) | ||
| 56 | { | ||
| 57 | if ((ch >= 'a') && (ch <= 'f')) | ||
| 58 | return ch - 'a' + 10; | ||
| 59 | if ((ch >= '0') && (ch <= '9')) | ||
| 60 | return ch - '0'; | ||
| 61 | if ((ch >= 'A') && (ch <= 'F')) | ||
| 62 | return ch - 'A' + 10; | ||
| 63 | return -1; | ||
| 64 | } | ||
| 65 | |||
| 66 | #ifdef CONFIG_KGDB_KDB | ||
| 67 | static int gdbstub_read_wait(void) | ||
| 68 | { | ||
| 69 | int ret = -1; | ||
| 70 | int i; | ||
| 71 | |||
| 72 | /* poll any additional I/O interfaces that are defined */ | ||
| 73 | while (ret < 0) | ||
| 74 | for (i = 0; kdb_poll_funcs[i] != NULL; i++) { | ||
| 75 | ret = kdb_poll_funcs[i](); | ||
| 76 | if (ret > 0) | ||
| 77 | break; | ||
| 78 | } | ||
| 79 | return ret; | ||
| 80 | } | ||
| 81 | #else | ||
| 82 | static int gdbstub_read_wait(void) | ||
| 83 | { | ||
| 84 | int ret = dbg_io_ops->read_char(); | ||
| 85 | while (ret == NO_POLL_CHAR) | ||
| 86 | ret = dbg_io_ops->read_char(); | ||
| 87 | return ret; | ||
| 88 | } | ||
| 89 | #endif | ||
| 90 | /* scan for the sequence $<data>#<checksum> */ | ||
| 91 | static void get_packet(char *buffer) | ||
| 92 | { | ||
| 93 | unsigned char checksum; | ||
| 94 | unsigned char xmitcsum; | ||
| 95 | int count; | ||
| 96 | char ch; | ||
| 97 | |||
| 98 | do { | ||
| 99 | /* | ||
| 100 | * Spin and wait around for the start character, ignore all | ||
| 101 | * other characters: | ||
| 102 | */ | ||
| 103 | while ((ch = (gdbstub_read_wait())) != '$') | ||
| 104 | /* nothing */; | ||
| 105 | |||
| 106 | kgdb_connected = 1; | ||
| 107 | checksum = 0; | ||
| 108 | xmitcsum = -1; | ||
| 109 | |||
| 110 | count = 0; | ||
| 111 | |||
| 112 | /* | ||
| 113 | * now, read until a # or end of buffer is found: | ||
| 114 | */ | ||
| 115 | while (count < (BUFMAX - 1)) { | ||
| 116 | ch = gdbstub_read_wait(); | ||
| 117 | if (ch == '#') | ||
| 118 | break; | ||
| 119 | checksum = checksum + ch; | ||
| 120 | buffer[count] = ch; | ||
| 121 | count = count + 1; | ||
| 122 | } | ||
| 123 | buffer[count] = 0; | ||
| 124 | |||
| 125 | if (ch == '#') { | ||
| 126 | xmitcsum = hex(gdbstub_read_wait()) << 4; | ||
| 127 | xmitcsum += hex(gdbstub_read_wait()); | ||
| 128 | |||
| 129 | if (checksum != xmitcsum) | ||
| 130 | /* failed checksum */ | ||
| 131 | dbg_io_ops->write_char('-'); | ||
| 132 | else | ||
| 133 | /* successful transfer */ | ||
| 134 | dbg_io_ops->write_char('+'); | ||
| 135 | if (dbg_io_ops->flush) | ||
| 136 | dbg_io_ops->flush(); | ||
| 137 | } | ||
| 138 | } while (checksum != xmitcsum); | ||
| 139 | } | ||
| 140 | |||
| 141 | /* | ||
| 142 | * Send the packet in buffer. | ||
| 143 | * Check for gdb connection if asked for. | ||
| 144 | */ | ||
| 145 | static void put_packet(char *buffer) | ||
| 146 | { | ||
| 147 | unsigned char checksum; | ||
| 148 | int count; | ||
| 149 | char ch; | ||
| 150 | |||
| 151 | /* | ||
| 152 | * $<packet info>#<checksum>. | ||
| 153 | */ | ||
| 154 | while (1) { | ||
| 155 | dbg_io_ops->write_char('$'); | ||
| 156 | checksum = 0; | ||
| 157 | count = 0; | ||
| 158 | |||
| 159 | while ((ch = buffer[count])) { | ||
| 160 | dbg_io_ops->write_char(ch); | ||
| 161 | checksum += ch; | ||
| 162 | count++; | ||
| 163 | } | ||
| 164 | |||
| 165 | dbg_io_ops->write_char('#'); | ||
| 166 | dbg_io_ops->write_char(hex_asc_hi(checksum)); | ||
| 167 | dbg_io_ops->write_char(hex_asc_lo(checksum)); | ||
| 168 | if (dbg_io_ops->flush) | ||
| 169 | dbg_io_ops->flush(); | ||
| 170 | |||
| 171 | /* Now see what we get in reply. */ | ||
| 172 | ch = gdbstub_read_wait(); | ||
| 173 | |||
| 174 | if (ch == 3) | ||
| 175 | ch = gdbstub_read_wait(); | ||
| 176 | |||
| 177 | /* If we get an ACK, we are done. */ | ||
| 178 | if (ch == '+') | ||
| 179 | return; | ||
| 180 | |||
| 181 | /* | ||
| 182 | * If we get the start of another packet, this means | ||
| 183 | * that GDB is attempting to reconnect. We will NAK | ||
| 184 | * the packet being sent, and stop trying to send this | ||
| 185 | * packet. | ||
| 186 | */ | ||
| 187 | if (ch == '$') { | ||
| 188 | dbg_io_ops->write_char('-'); | ||
| 189 | if (dbg_io_ops->flush) | ||
| 190 | dbg_io_ops->flush(); | ||
| 191 | return; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | static char gdbmsgbuf[BUFMAX + 1]; | ||
| 197 | |||
| 198 | void gdbstub_msg_write(const char *s, int len) | ||
| 199 | { | ||
| 200 | char *bufptr; | ||
| 201 | int wcount; | ||
| 202 | int i; | ||
| 203 | |||
| 204 | if (len == 0) | ||
| 205 | len = strlen(s); | ||
| 206 | |||
| 207 | /* 'O'utput */ | ||
| 208 | gdbmsgbuf[0] = 'O'; | ||
| 209 | |||
| 210 | /* Fill and send buffers... */ | ||
| 211 | while (len > 0) { | ||
| 212 | bufptr = gdbmsgbuf + 1; | ||
| 213 | |||
| 214 | /* Calculate how many this time */ | ||
| 215 | if ((len << 1) > (BUFMAX - 2)) | ||
| 216 | wcount = (BUFMAX - 2) >> 1; | ||
| 217 | else | ||
| 218 | wcount = len; | ||
| 219 | |||
| 220 | /* Pack in hex chars */ | ||
| 221 | for (i = 0; i < wcount; i++) | ||
| 222 | bufptr = pack_hex_byte(bufptr, s[i]); | ||
| 223 | *bufptr = '\0'; | ||
| 224 | |||
| 225 | /* Move up */ | ||
| 226 | s += wcount; | ||
| 227 | len -= wcount; | ||
| 228 | |||
| 229 | /* Write packet */ | ||
| 230 | put_packet(gdbmsgbuf); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | /* | ||
| 235 | * Convert the memory pointed to by mem into hex, placing result in | ||
| 236 | * buf. Return a pointer to the last char put in buf (null). May | ||
| 237 | * return an error. | ||
| 238 | */ | ||
| 239 | int kgdb_mem2hex(char *mem, char *buf, int count) | ||
| 240 | { | ||
| 241 | char *tmp; | ||
| 242 | int err; | ||
| 243 | |||
| 244 | /* | ||
| 245 | * We use the upper half of buf as an intermediate buffer for the | ||
| 246 | * raw memory copy. Hex conversion will work against this one. | ||
| 247 | */ | ||
| 248 | tmp = buf + count; | ||
| 249 | |||
| 250 | err = probe_kernel_read(tmp, mem, count); | ||
| 251 | if (!err) { | ||
| 252 | while (count > 0) { | ||
| 253 | buf = pack_hex_byte(buf, *tmp); | ||
| 254 | tmp++; | ||
| 255 | count--; | ||
| 256 | } | ||
| 257 | |||
| 258 | *buf = 0; | ||
| 259 | } | ||
| 260 | |||
| 261 | return err; | ||
| 262 | } | ||
| 263 | |||
| 264 | /* | ||
| 265 | * Convert the hex array pointed to by buf into binary to be placed in | ||
| 266 | * mem. Return a pointer to the character AFTER the last byte | ||
| 267 | * written. May return an error. | ||
| 268 | */ | ||
| 269 | int kgdb_hex2mem(char *buf, char *mem, int count) | ||
| 270 | { | ||
| 271 | char *tmp_raw; | ||
| 272 | char *tmp_hex; | ||
| 273 | |||
| 274 | /* | ||
| 275 | * We use the upper half of buf as an intermediate buffer for the | ||
| 276 | * raw memory that is converted from hex. | ||
| 277 | */ | ||
| 278 | tmp_raw = buf + count * 2; | ||
| 279 | |||
| 280 | tmp_hex = tmp_raw - 1; | ||
| 281 | while (tmp_hex >= buf) { | ||
| 282 | tmp_raw--; | ||
| 283 | *tmp_raw = hex(*tmp_hex--); | ||
| 284 | *tmp_raw |= hex(*tmp_hex--) << 4; | ||
| 285 | } | ||
| 286 | |||
| 287 | return probe_kernel_write(mem, tmp_raw, count); | ||
| 288 | } | ||
| 289 | |||
| 290 | /* | ||
| 291 | * While we find nice hex chars, build a long_val. | ||
| 292 | * Return number of chars processed. | ||
| 293 | */ | ||
| 294 | int kgdb_hex2long(char **ptr, unsigned long *long_val) | ||
| 295 | { | ||
| 296 | int hex_val; | ||
| 297 | int num = 0; | ||
| 298 | int negate = 0; | ||
| 299 | |||
| 300 | *long_val = 0; | ||
| 301 | |||
| 302 | if (**ptr == '-') { | ||
| 303 | negate = 1; | ||
| 304 | (*ptr)++; | ||
| 305 | } | ||
| 306 | while (**ptr) { | ||
| 307 | hex_val = hex(**ptr); | ||
| 308 | if (hex_val < 0) | ||
| 309 | break; | ||
| 310 | |||
| 311 | *long_val = (*long_val << 4) | hex_val; | ||
| 312 | num++; | ||
| 313 | (*ptr)++; | ||
| 314 | } | ||
| 315 | |||
| 316 | if (negate) | ||
| 317 | *long_val = -*long_val; | ||
| 318 | |||
| 319 | return num; | ||
| 320 | } | ||
| 321 | |||
| 322 | /* | ||
| 323 | * Copy the binary array pointed to by buf into mem. Fix $, #, and | ||
| 324 | * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success. | ||
| 325 | * The input buf is overwitten with the result to write to mem. | ||
| 326 | */ | ||
| 327 | static int kgdb_ebin2mem(char *buf, char *mem, int count) | ||
| 328 | { | ||
| 329 | int size = 0; | ||
| 330 | char *c = buf; | ||
| 331 | |||
| 332 | while (count-- > 0) { | ||
| 333 | c[size] = *buf++; | ||
| 334 | if (c[size] == 0x7d) | ||
| 335 | c[size] = *buf++ ^ 0x20; | ||
| 336 | size++; | ||
| 337 | } | ||
| 338 | |||
| 339 | return probe_kernel_write(mem, c, size); | ||
| 340 | } | ||
| 341 | |||
| 342 | /* Write memory due to an 'M' or 'X' packet. */ | ||
| 343 | static int write_mem_msg(int binary) | ||
| 344 | { | ||
| 345 | char *ptr = &remcom_in_buffer[1]; | ||
| 346 | unsigned long addr; | ||
| 347 | unsigned long length; | ||
| 348 | int err; | ||
| 349 | |||
| 350 | if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' && | ||
| 351 | kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') { | ||
| 352 | if (binary) | ||
| 353 | err = kgdb_ebin2mem(ptr, (char *)addr, length); | ||
| 354 | else | ||
| 355 | err = kgdb_hex2mem(ptr, (char *)addr, length); | ||
| 356 | if (err) | ||
| 357 | return err; | ||
| 358 | if (CACHE_FLUSH_IS_SAFE) | ||
| 359 | flush_icache_range(addr, addr + length); | ||
| 360 | return 0; | ||
| 361 | } | ||
| 362 | |||
| 363 | return -EINVAL; | ||
| 364 | } | ||
| 365 | |||
| 366 | static void error_packet(char *pkt, int error) | ||
| 367 | { | ||
| 368 | error = -error; | ||
| 369 | pkt[0] = 'E'; | ||
| 370 | pkt[1] = hex_asc[(error / 10)]; | ||
| 371 | pkt[2] = hex_asc[(error % 10)]; | ||
| 372 | pkt[3] = '\0'; | ||
| 373 | } | ||
| 374 | |||
| 375 | /* | ||
| 376 | * Thread ID accessors. We represent a flat TID space to GDB, where | ||
| 377 | * the per CPU idle threads (which under Linux all have PID 0) are | ||
| 378 | * remapped to negative TIDs. | ||
| 379 | */ | ||
| 380 | |||
| 381 | #define BUF_THREAD_ID_SIZE 16 | ||
| 382 | |||
| 383 | static char *pack_threadid(char *pkt, unsigned char *id) | ||
| 384 | { | ||
| 385 | char *limit; | ||
| 386 | |||
| 387 | limit = pkt + BUF_THREAD_ID_SIZE; | ||
| 388 | while (pkt < limit) | ||
| 389 | pkt = pack_hex_byte(pkt, *id++); | ||
| 390 | |||
| 391 | return pkt; | ||
| 392 | } | ||
| 393 | |||
| 394 | static void int_to_threadref(unsigned char *id, int value) | ||
| 395 | { | ||
| 396 | unsigned char *scan; | ||
| 397 | int i = 4; | ||
| 398 | |||
| 399 | scan = (unsigned char *)id; | ||
| 400 | while (i--) | ||
| 401 | *scan++ = 0; | ||
| 402 | put_unaligned_be32(value, scan); | ||
| 403 | } | ||
| 404 | |||
| 405 | static struct task_struct *getthread(struct pt_regs *regs, int tid) | ||
| 406 | { | ||
| 407 | /* | ||
| 408 | * Non-positive TIDs are remapped to the cpu shadow information | ||
| 409 | */ | ||
| 410 | if (tid == 0 || tid == -1) | ||
| 411 | tid = -atomic_read(&kgdb_active) - 2; | ||
| 412 | if (tid < -1 && tid > -NR_CPUS - 2) { | ||
| 413 | if (kgdb_info[-tid - 2].task) | ||
| 414 | return kgdb_info[-tid - 2].task; | ||
| 415 | else | ||
| 416 | return idle_task(-tid - 2); | ||
| 417 | } | ||
| 418 | if (tid <= 0) { | ||
| 419 | printk(KERN_ERR "KGDB: Internal thread select error\n"); | ||
| 420 | dump_stack(); | ||
| 421 | return NULL; | ||
| 422 | } | ||
| 423 | |||
| 424 | /* | ||
| 425 | * find_task_by_pid_ns() does not take the tasklist lock anymore | ||
| 426 | * but is nicely RCU locked - hence is a pretty resilient | ||
| 427 | * thing to use: | ||
| 428 | */ | ||
| 429 | return find_task_by_pid_ns(tid, &init_pid_ns); | ||
| 430 | } | ||
| 431 | |||
| 432 | |||
| 433 | /* | ||
| 434 | * Remap normal tasks to their real PID, | ||
| 435 | * CPU shadow threads are mapped to -CPU - 2 | ||
| 436 | */ | ||
| 437 | static inline int shadow_pid(int realpid) | ||
| 438 | { | ||
| 439 | if (realpid) | ||
| 440 | return realpid; | ||
| 441 | |||
| 442 | return -raw_smp_processor_id() - 2; | ||
| 443 | } | ||
| 444 | |||
| 445 | /* | ||
| 446 | * All the functions that start with gdb_cmd are the various | ||
| 447 | * operations to implement the handlers for the gdbserial protocol | ||
| 448 | * where KGDB is communicating with an external debugger | ||
| 449 | */ | ||
| 450 | |||
| 451 | /* Handle the '?' status packets */ | ||
| 452 | static void gdb_cmd_status(struct kgdb_state *ks) | ||
| 453 | { | ||
| 454 | /* | ||
| 455 | * We know that this packet is only sent | ||
| 456 | * during initial connect. So to be safe, | ||
| 457 | * we clear out our breakpoints now in case | ||
| 458 | * GDB is reconnecting. | ||
| 459 | */ | ||
| 460 | dbg_remove_all_break(); | ||
| 461 | |||
| 462 | remcom_out_buffer[0] = 'S'; | ||
| 463 | pack_hex_byte(&remcom_out_buffer[1], ks->signo); | ||
| 464 | } | ||
| 465 | |||
| 466 | /* Handle the 'g' get registers request */ | ||
| 467 | static void gdb_cmd_getregs(struct kgdb_state *ks) | ||
| 468 | { | ||
| 469 | struct task_struct *thread; | ||
| 470 | void *local_debuggerinfo; | ||
| 471 | int i; | ||
| 472 | |||
| 473 | thread = kgdb_usethread; | ||
| 474 | if (!thread) { | ||
| 475 | thread = kgdb_info[ks->cpu].task; | ||
| 476 | local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo; | ||
| 477 | } else { | ||
| 478 | local_debuggerinfo = NULL; | ||
| 479 | for_each_online_cpu(i) { | ||
| 480 | /* | ||
| 481 | * Try to find the task on some other | ||
| 482 | * or possibly this node if we do not | ||
| 483 | * find the matching task then we try | ||
| 484 | * to approximate the results. | ||
| 485 | */ | ||
| 486 | if (thread == kgdb_info[i].task) | ||
| 487 | local_debuggerinfo = kgdb_info[i].debuggerinfo; | ||
| 488 | } | ||
| 489 | } | ||
| 490 | |||
| 491 | /* | ||
| 492 | * All threads that don't have debuggerinfo should be | ||
| 493 | * in schedule() sleeping, since all other CPUs | ||
| 494 | * are in kgdb_wait, and thus have debuggerinfo. | ||
| 495 | */ | ||
| 496 | if (local_debuggerinfo) { | ||
| 497 | pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo); | ||
| 498 | } else { | ||
| 499 | /* | ||
| 500 | * Pull stuff saved during switch_to; nothing | ||
| 501 | * else is accessible (or even particularly | ||
| 502 | * relevant). | ||
| 503 | * | ||
| 504 | * This should be enough for a stack trace. | ||
| 505 | */ | ||
| 506 | sleeping_thread_to_gdb_regs(gdb_regs, thread); | ||
| 507 | } | ||
| 508 | kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES); | ||
| 509 | } | ||
| 510 | |||
| 511 | /* Handle the 'G' set registers request */ | ||
| 512 | static void gdb_cmd_setregs(struct kgdb_state *ks) | ||
| 513 | { | ||
| 514 | kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES); | ||
| 515 | |||
| 516 | if (kgdb_usethread && kgdb_usethread != current) { | ||
| 517 | error_packet(remcom_out_buffer, -EINVAL); | ||
| 518 | } else { | ||
| 519 | gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs); | ||
| 520 | strcpy(remcom_out_buffer, "OK"); | ||
| 521 | } | ||
| 522 | } | ||
| 523 | |||
| 524 | /* Handle the 'm' memory read bytes */ | ||
| 525 | static void gdb_cmd_memread(struct kgdb_state *ks) | ||
| 526 | { | ||
| 527 | char *ptr = &remcom_in_buffer[1]; | ||
| 528 | unsigned long length; | ||
| 529 | unsigned long addr; | ||
| 530 | int err; | ||
| 531 | |||
| 532 | if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' && | ||
| 533 | kgdb_hex2long(&ptr, &length) > 0) { | ||
| 534 | err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length); | ||
| 535 | if (err) | ||
| 536 | error_packet(remcom_out_buffer, err); | ||
| 537 | } else { | ||
| 538 | error_packet(remcom_out_buffer, -EINVAL); | ||
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | /* Handle the 'M' memory write bytes */ | ||
| 543 | static void gdb_cmd_memwrite(struct kgdb_state *ks) | ||
| 544 | { | ||
| 545 | int err = write_mem_msg(0); | ||
| 546 | |||
| 547 | if (err) | ||
| 548 | error_packet(remcom_out_buffer, err); | ||
| 549 | else | ||
| 550 | strcpy(remcom_out_buffer, "OK"); | ||
| 551 | } | ||
| 552 | |||
| 553 | /* Handle the 'X' memory binary write bytes */ | ||
| 554 | static void gdb_cmd_binwrite(struct kgdb_state *ks) | ||
| 555 | { | ||
| 556 | int err = write_mem_msg(1); | ||
| 557 | |||
| 558 | if (err) | ||
| 559 | error_packet(remcom_out_buffer, err); | ||
| 560 | else | ||
| 561 | strcpy(remcom_out_buffer, "OK"); | ||
| 562 | } | ||
| 563 | |||
| 564 | /* Handle the 'D' or 'k', detach or kill packets */ | ||
| 565 | static void gdb_cmd_detachkill(struct kgdb_state *ks) | ||
| 566 | { | ||
| 567 | int error; | ||
| 568 | |||
| 569 | /* The detach case */ | ||
| 570 | if (remcom_in_buffer[0] == 'D') { | ||
| 571 | error = dbg_remove_all_break(); | ||
| 572 | if (error < 0) { | ||
| 573 | error_packet(remcom_out_buffer, error); | ||
| 574 | } else { | ||
| 575 | strcpy(remcom_out_buffer, "OK"); | ||
| 576 | kgdb_connected = 0; | ||
| 577 | } | ||
| 578 | put_packet(remcom_out_buffer); | ||
| 579 | } else { | ||
| 580 | /* | ||
| 581 | * Assume the kill case, with no exit code checking, | ||
| 582 | * trying to force detach the debugger: | ||
| 583 | */ | ||
| 584 | dbg_remove_all_break(); | ||
| 585 | kgdb_connected = 0; | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | /* Handle the 'R' reboot packets */ | ||
| 590 | static int gdb_cmd_reboot(struct kgdb_state *ks) | ||
| 591 | { | ||
| 592 | /* For now, only honor R0 */ | ||
| 593 | if (strcmp(remcom_in_buffer, "R0") == 0) { | ||
| 594 | printk(KERN_CRIT "Executing emergency reboot\n"); | ||
| 595 | strcpy(remcom_out_buffer, "OK"); | ||
| 596 | put_packet(remcom_out_buffer); | ||
| 597 | |||
| 598 | /* | ||
| 599 | * Execution should not return from | ||
| 600 | * machine_emergency_restart() | ||
| 601 | */ | ||
| 602 | machine_emergency_restart(); | ||
| 603 | kgdb_connected = 0; | ||
| 604 | |||
| 605 | return 1; | ||
| 606 | } | ||
| 607 | return 0; | ||
| 608 | } | ||
| 609 | |||
| 610 | /* Handle the 'q' query packets */ | ||
| 611 | static void gdb_cmd_query(struct kgdb_state *ks) | ||
| 612 | { | ||
| 613 | struct task_struct *g; | ||
| 614 | struct task_struct *p; | ||
| 615 | unsigned char thref[8]; | ||
| 616 | char *ptr; | ||
| 617 | int i; | ||
| 618 | int cpu; | ||
| 619 | int finished = 0; | ||
| 620 | |||
| 621 | switch (remcom_in_buffer[1]) { | ||
| 622 | case 's': | ||
| 623 | case 'f': | ||
| 624 | if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) | ||
| 625 | break; | ||
| 626 | |||
| 627 | i = 0; | ||
| 628 | remcom_out_buffer[0] = 'm'; | ||
| 629 | ptr = remcom_out_buffer + 1; | ||
| 630 | if (remcom_in_buffer[1] == 'f') { | ||
| 631 | /* Each cpu is a shadow thread */ | ||
| 632 | for_each_online_cpu(cpu) { | ||
| 633 | ks->thr_query = 0; | ||
| 634 | int_to_threadref(thref, -cpu - 2); | ||
| 635 | pack_threadid(ptr, thref); | ||
| 636 | ptr += BUF_THREAD_ID_SIZE; | ||
| 637 | *(ptr++) = ','; | ||
| 638 | i++; | ||
| 639 | } | ||
| 640 | } | ||
| 641 | |||
| 642 | do_each_thread(g, p) { | ||
| 643 | if (i >= ks->thr_query && !finished) { | ||
| 644 | int_to_threadref(thref, p->pid); | ||
| 645 | pack_threadid(ptr, thref); | ||
| 646 | ptr += BUF_THREAD_ID_SIZE; | ||
| 647 | *(ptr++) = ','; | ||
| 648 | ks->thr_query++; | ||
| 649 | if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0) | ||
| 650 | finished = 1; | ||
| 651 | } | ||
| 652 | i++; | ||
| 653 | } while_each_thread(g, p); | ||
| 654 | |||
| 655 | *(--ptr) = '\0'; | ||
| 656 | break; | ||
| 657 | |||
| 658 | case 'C': | ||
| 659 | /* Current thread id */ | ||
| 660 | strcpy(remcom_out_buffer, "QC"); | ||
| 661 | ks->threadid = shadow_pid(current->pid); | ||
| 662 | int_to_threadref(thref, ks->threadid); | ||
| 663 | pack_threadid(remcom_out_buffer + 2, thref); | ||
| 664 | break; | ||
| 665 | case 'T': | ||
| 666 | if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) | ||
| 667 | break; | ||
| 668 | |||
| 669 | ks->threadid = 0; | ||
| 670 | ptr = remcom_in_buffer + 17; | ||
| 671 | kgdb_hex2long(&ptr, &ks->threadid); | ||
| 672 | if (!getthread(ks->linux_regs, ks->threadid)) { | ||
| 673 | error_packet(remcom_out_buffer, -EINVAL); | ||
| 674 | break; | ||
| 675 | } | ||
| 676 | if ((int)ks->threadid > 0) { | ||
| 677 | kgdb_mem2hex(getthread(ks->linux_regs, | ||
| 678 | ks->threadid)->comm, | ||
| 679 | remcom_out_buffer, 16); | ||
| 680 | } else { | ||
| 681 | static char tmpstr[23 + BUF_THREAD_ID_SIZE]; | ||
| 682 | |||
| 683 | sprintf(tmpstr, "shadowCPU%d", | ||
| 684 | (int)(-ks->threadid - 2)); | ||
| 685 | kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr)); | ||
| 686 | } | ||
| 687 | break; | ||
| 688 | #ifdef CONFIG_KGDB_KDB | ||
| 689 | case 'R': | ||
| 690 | if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) { | ||
| 691 | int len = strlen(remcom_in_buffer + 6); | ||
| 692 | |||
| 693 | if ((len % 2) != 0) { | ||
| 694 | strcpy(remcom_out_buffer, "E01"); | ||
| 695 | break; | ||
| 696 | } | ||
| 697 | kgdb_hex2mem(remcom_in_buffer + 6, | ||
| 698 | remcom_out_buffer, len); | ||
| 699 | len = len / 2; | ||
| 700 | remcom_out_buffer[len++] = 0; | ||
| 701 | |||
| 702 | kdb_parse(remcom_out_buffer); | ||
| 703 | strcpy(remcom_out_buffer, "OK"); | ||
| 704 | } | ||
| 705 | break; | ||
| 706 | #endif | ||
| 707 | } | ||
| 708 | } | ||
| 709 | |||
| 710 | /* Handle the 'H' task query packets */ | ||
| 711 | static void gdb_cmd_task(struct kgdb_state *ks) | ||
| 712 | { | ||
| 713 | struct task_struct *thread; | ||
| 714 | char *ptr; | ||
| 715 | |||
| 716 | switch (remcom_in_buffer[1]) { | ||
| 717 | case 'g': | ||
| 718 | ptr = &remcom_in_buffer[2]; | ||
| 719 | kgdb_hex2long(&ptr, &ks->threadid); | ||
| 720 | thread = getthread(ks->linux_regs, ks->threadid); | ||
| 721 | if (!thread && ks->threadid > 0) { | ||
| 722 | error_packet(remcom_out_buffer, -EINVAL); | ||
| 723 | break; | ||
| 724 | } | ||
| 725 | kgdb_usethread = thread; | ||
| 726 | ks->kgdb_usethreadid = ks->threadid; | ||
| 727 | strcpy(remcom_out_buffer, "OK"); | ||
| 728 | break; | ||
| 729 | case 'c': | ||
| 730 | ptr = &remcom_in_buffer[2]; | ||
| 731 | kgdb_hex2long(&ptr, &ks->threadid); | ||
| 732 | if (!ks->threadid) { | ||
| 733 | kgdb_contthread = NULL; | ||
| 734 | } else { | ||
| 735 | thread = getthread(ks->linux_regs, ks->threadid); | ||
| 736 | if (!thread && ks->threadid > 0) { | ||
| 737 | error_packet(remcom_out_buffer, -EINVAL); | ||
| 738 | break; | ||
| 739 | } | ||
| 740 | kgdb_contthread = thread; | ||
| 741 | } | ||
| 742 | strcpy(remcom_out_buffer, "OK"); | ||
| 743 | break; | ||
| 744 | } | ||
| 745 | } | ||
| 746 | |||
| 747 | /* Handle the 'T' thread query packets */ | ||
| 748 | static void gdb_cmd_thread(struct kgdb_state *ks) | ||
| 749 | { | ||
| 750 | char *ptr = &remcom_in_buffer[1]; | ||
| 751 | struct task_struct *thread; | ||
| 752 | |||
| 753 | kgdb_hex2long(&ptr, &ks->threadid); | ||
| 754 | thread = getthread(ks->linux_regs, ks->threadid); | ||
| 755 | if (thread) | ||
| 756 | strcpy(remcom_out_buffer, "OK"); | ||
| 757 | else | ||
| 758 | error_packet(remcom_out_buffer, -EINVAL); | ||
| 759 | } | ||
| 760 | |||
| 761 | /* Handle the 'z' or 'Z' breakpoint remove or set packets */ | ||
| 762 | static void gdb_cmd_break(struct kgdb_state *ks) | ||
| 763 | { | ||
| 764 | /* | ||
| 765 | * Since GDB-5.3, it's been drafted that '0' is a software | ||
| 766 | * breakpoint, '1' is a hardware breakpoint, so let's do that. | ||
| 767 | */ | ||
| 768 | char *bpt_type = &remcom_in_buffer[1]; | ||
| 769 | char *ptr = &remcom_in_buffer[2]; | ||
| 770 | unsigned long addr; | ||
| 771 | unsigned long length; | ||
| 772 | int error = 0; | ||
| 773 | |||
| 774 | if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') { | ||
| 775 | /* Unsupported */ | ||
| 776 | if (*bpt_type > '4') | ||
| 777 | return; | ||
| 778 | } else { | ||
| 779 | if (*bpt_type != '0' && *bpt_type != '1') | ||
| 780 | /* Unsupported. */ | ||
| 781 | return; | ||
| 782 | } | ||
| 783 | |||
| 784 | /* | ||
| 785 | * Test if this is a hardware breakpoint, and | ||
| 786 | * if we support it: | ||
| 787 | */ | ||
| 788 | if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)) | ||
| 789 | /* Unsupported. */ | ||
| 790 | return; | ||
| 791 | |||
| 792 | if (*(ptr++) != ',') { | ||
| 793 | error_packet(remcom_out_buffer, -EINVAL); | ||
| 794 | return; | ||
| 795 | } | ||
| 796 | if (!kgdb_hex2long(&ptr, &addr)) { | ||
| 797 | error_packet(remcom_out_buffer, -EINVAL); | ||
| 798 | return; | ||
| 799 | } | ||
| 800 | if (*(ptr++) != ',' || | ||
| 801 | !kgdb_hex2long(&ptr, &length)) { | ||
| 802 | error_packet(remcom_out_buffer, -EINVAL); | ||
| 803 | return; | ||
| 804 | } | ||
| 805 | |||
| 806 | if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0') | ||
| 807 | error = dbg_set_sw_break(addr); | ||
| 808 | else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0') | ||
| 809 | error = dbg_remove_sw_break(addr); | ||
| 810 | else if (remcom_in_buffer[0] == 'Z') | ||
| 811 | error = arch_kgdb_ops.set_hw_breakpoint(addr, | ||
| 812 | (int)length, *bpt_type - '0'); | ||
| 813 | else if (remcom_in_buffer[0] == 'z') | ||
| 814 | error = arch_kgdb_ops.remove_hw_breakpoint(addr, | ||
| 815 | (int) length, *bpt_type - '0'); | ||
| 816 | |||
| 817 | if (error == 0) | ||
| 818 | strcpy(remcom_out_buffer, "OK"); | ||
| 819 | else | ||
| 820 | error_packet(remcom_out_buffer, error); | ||
| 821 | } | ||
| 822 | |||
| 823 | /* Handle the 'C' signal / exception passing packets */ | ||
| 824 | static int gdb_cmd_exception_pass(struct kgdb_state *ks) | ||
| 825 | { | ||
| 826 | /* C09 == pass exception | ||
| 827 | * C15 == detach kgdb, pass exception | ||
| 828 | */ | ||
| 829 | if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') { | ||
| 830 | |||
| 831 | ks->pass_exception = 1; | ||
| 832 | remcom_in_buffer[0] = 'c'; | ||
| 833 | |||
| 834 | } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') { | ||
| 835 | |||
| 836 | ks->pass_exception = 1; | ||
| 837 | remcom_in_buffer[0] = 'D'; | ||
| 838 | dbg_remove_all_break(); | ||
| 839 | kgdb_connected = 0; | ||
| 840 | return 1; | ||
| 841 | |||
| 842 | } else { | ||
| 843 | gdbstub_msg_write("KGDB only knows signal 9 (pass)" | ||
| 844 | " and 15 (pass and disconnect)\n" | ||
| 845 | "Executing a continue without signal passing\n", 0); | ||
| 846 | remcom_in_buffer[0] = 'c'; | ||
| 847 | } | ||
| 848 | |||
| 849 | /* Indicate fall through */ | ||
| 850 | return -1; | ||
| 851 | } | ||
| 852 | |||
| 853 | /* | ||
| 854 | * This function performs all gdbserial command procesing | ||
| 855 | */ | ||
| 856 | int gdb_serial_stub(struct kgdb_state *ks) | ||
| 857 | { | ||
| 858 | int error = 0; | ||
| 859 | int tmp; | ||
| 860 | |||
| 861 | /* Clear the out buffer. */ | ||
| 862 | memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); | ||
| 863 | |||
| 864 | if (kgdb_connected) { | ||
| 865 | unsigned char thref[8]; | ||
| 866 | char *ptr; | ||
| 867 | |||
| 868 | /* Reply to host that an exception has occurred */ | ||
| 869 | ptr = remcom_out_buffer; | ||
| 870 | *ptr++ = 'T'; | ||
| 871 | ptr = pack_hex_byte(ptr, ks->signo); | ||
| 872 | ptr += strlen(strcpy(ptr, "thread:")); | ||
| 873 | int_to_threadref(thref, shadow_pid(current->pid)); | ||
| 874 | ptr = pack_threadid(ptr, thref); | ||
| 875 | *ptr++ = ';'; | ||
| 876 | put_packet(remcom_out_buffer); | ||
| 877 | } | ||
| 878 | |||
| 879 | kgdb_usethread = kgdb_info[ks->cpu].task; | ||
| 880 | ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid); | ||
| 881 | ks->pass_exception = 0; | ||
| 882 | |||
| 883 | while (1) { | ||
| 884 | error = 0; | ||
| 885 | |||
| 886 | /* Clear the out buffer. */ | ||
| 887 | memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); | ||
| 888 | |||
| 889 | get_packet(remcom_in_buffer); | ||
| 890 | |||
| 891 | switch (remcom_in_buffer[0]) { | ||
| 892 | case '?': /* gdbserial status */ | ||
| 893 | gdb_cmd_status(ks); | ||
| 894 | break; | ||
| 895 | case 'g': /* return the value of the CPU registers */ | ||
| 896 | gdb_cmd_getregs(ks); | ||
| 897 | break; | ||
| 898 | case 'G': /* set the value of the CPU registers - return OK */ | ||
| 899 | gdb_cmd_setregs(ks); | ||
| 900 | break; | ||
| 901 | case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ | ||
| 902 | gdb_cmd_memread(ks); | ||
| 903 | break; | ||
| 904 | case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */ | ||
| 905 | gdb_cmd_memwrite(ks); | ||
| 906 | break; | ||
| 907 | case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */ | ||
| 908 | gdb_cmd_binwrite(ks); | ||
| 909 | break; | ||
| 910 | /* kill or detach. KGDB should treat this like a | ||
| 911 | * continue. | ||
| 912 | */ | ||
| 913 | case 'D': /* Debugger detach */ | ||
| 914 | case 'k': /* Debugger detach via kill */ | ||
| 915 | gdb_cmd_detachkill(ks); | ||
| 916 | goto default_handle; | ||
| 917 | case 'R': /* Reboot */ | ||
| 918 | if (gdb_cmd_reboot(ks)) | ||
| 919 | goto default_handle; | ||
| 920 | break; | ||
| 921 | case 'q': /* query command */ | ||
| 922 | gdb_cmd_query(ks); | ||
| 923 | break; | ||
| 924 | case 'H': /* task related */ | ||
| 925 | gdb_cmd_task(ks); | ||
| 926 | break; | ||
| 927 | case 'T': /* Query thread status */ | ||
| 928 | gdb_cmd_thread(ks); | ||
| 929 | break; | ||
| 930 | case 'z': /* Break point remove */ | ||
| 931 | case 'Z': /* Break point set */ | ||
| 932 | gdb_cmd_break(ks); | ||
| 933 | break; | ||
| 934 | #ifdef CONFIG_KGDB_KDB | ||
| 935 | case '3': /* Escape into back into kdb */ | ||
| 936 | if (remcom_in_buffer[1] == '\0') { | ||
| 937 | gdb_cmd_detachkill(ks); | ||
| 938 | return DBG_PASS_EVENT; | ||
| 939 | } | ||
| 940 | #endif | ||
| 941 | case 'C': /* Exception passing */ | ||
| 942 | tmp = gdb_cmd_exception_pass(ks); | ||
| 943 | if (tmp > 0) | ||
| 944 | goto default_handle; | ||
| 945 | if (tmp == 0) | ||
| 946 | break; | ||
| 947 | /* Fall through on tmp < 0 */ | ||
| 948 | case 'c': /* Continue packet */ | ||
| 949 | case 's': /* Single step packet */ | ||
| 950 | if (kgdb_contthread && kgdb_contthread != current) { | ||
| 951 | /* Can't switch threads in kgdb */ | ||
| 952 | error_packet(remcom_out_buffer, -EINVAL); | ||
| 953 | break; | ||
| 954 | } | ||
| 955 | dbg_activate_sw_breakpoints(); | ||
| 956 | /* Fall through to default processing */ | ||
| 957 | default: | ||
| 958 | default_handle: | ||
| 959 | error = kgdb_arch_handle_exception(ks->ex_vector, | ||
| 960 | ks->signo, | ||
| 961 | ks->err_code, | ||
| 962 | remcom_in_buffer, | ||
| 963 | remcom_out_buffer, | ||
| 964 | ks->linux_regs); | ||
| 965 | /* | ||
| 966 | * Leave cmd processing on error, detach, | ||
| 967 | * kill, continue, or single step. | ||
| 968 | */ | ||
| 969 | if (error >= 0 || remcom_in_buffer[0] == 'D' || | ||
| 970 | remcom_in_buffer[0] == 'k') { | ||
| 971 | error = 0; | ||
| 972 | goto kgdb_exit; | ||
| 973 | } | ||
| 974 | |||
| 975 | } | ||
| 976 | |||
| 977 | /* reply to the request */ | ||
| 978 | put_packet(remcom_out_buffer); | ||
| 979 | } | ||
| 980 | |||
| 981 | kgdb_exit: | ||
| 982 | if (ks->pass_exception) | ||
| 983 | error = 1; | ||
| 984 | return error; | ||
| 985 | } | ||
| 986 | |||
| 987 | int gdbstub_state(struct kgdb_state *ks, char *cmd) | ||
| 988 | { | ||
| 989 | int error; | ||
| 990 | |||
| 991 | switch (cmd[0]) { | ||
| 992 | case 'e': | ||
| 993 | error = kgdb_arch_handle_exception(ks->ex_vector, | ||
| 994 | ks->signo, | ||
| 995 | ks->err_code, | ||
| 996 | remcom_in_buffer, | ||
| 997 | remcom_out_buffer, | ||
| 998 | ks->linux_regs); | ||
| 999 | return error; | ||
| 1000 | case 's': | ||
| 1001 | case 'c': | ||
| 1002 | strcpy(remcom_in_buffer, cmd); | ||
| 1003 | return 0; | ||
| 1004 | case '?': | ||
| 1005 | gdb_cmd_status(ks); | ||
| 1006 | break; | ||
| 1007 | case '\0': | ||
| 1008 | strcpy(remcom_out_buffer, ""); | ||
| 1009 | break; | ||
| 1010 | } | ||
| 1011 | dbg_io_ops->write_char('+'); | ||
| 1012 | put_packet(remcom_out_buffer); | ||
| 1013 | return 0; | ||
| 1014 | } | ||
diff --git a/kernel/debug/kdb/.gitignore b/kernel/debug/kdb/.gitignore new file mode 100644 index 000000000000..396d12eda9e8 --- /dev/null +++ b/kernel/debug/kdb/.gitignore | |||
| @@ -0,0 +1 @@ | |||
| gen-kdb_cmds.c | |||
diff --git a/kernel/debug/kdb/Makefile b/kernel/debug/kdb/Makefile new file mode 100644 index 000000000000..d4fc58f4b88d --- /dev/null +++ b/kernel/debug/kdb/Makefile | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | # This file is subject to the terms and conditions of the GNU General Public | ||
| 2 | # License. See the file "COPYING" in the main directory of this archive | ||
| 3 | # for more details. | ||
| 4 | # | ||
| 5 | # Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved. | ||
| 6 | # Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. | ||
| 7 | # | ||
| 8 | |||
| 9 | CCVERSION := $(shell $(CC) -v 2>&1 | sed -ne '$$p') | ||
| 10 | obj-y := kdb_io.o kdb_main.o kdb_support.o kdb_bt.o gen-kdb_cmds.o kdb_bp.o kdb_debugger.o | ||
| 11 | obj-$(CONFIG_KDB_KEYBOARD) += kdb_keyboard.o | ||
| 12 | |||
| 13 | clean-files := gen-kdb_cmds.c | ||
| 14 | |||
| 15 | quiet_cmd_gen-kdb = GENKDB $@ | ||
| 16 | cmd_gen-kdb = $(AWK) 'BEGIN {print "\#include <linux/stddef.h>"; print "\#include <linux/init.h>"} \ | ||
| 17 | /^\#/{next} \ | ||
| 18 | /^[ \t]*$$/{next} \ | ||
| 19 | {gsub(/"/, "\\\"", $$0); \ | ||
| 20 | print "static __initdata char kdb_cmd" cmds++ "[] = \"" $$0 "\\n\";"} \ | ||
| 21 | END {print "extern char *kdb_cmds[]; char __initdata *kdb_cmds[] = {"; for (i = 0; i < cmds; ++i) {print " kdb_cmd" i ","}; print(" NULL\n};");}' \ | ||
| 22 | $(filter-out %/Makefile,$^) > $@# | ||
| 23 | |||
| 24 | $(obj)/gen-kdb_cmds.c: $(src)/kdb_cmds $(src)/Makefile | ||
| 25 | $(call cmd,gen-kdb) | ||
diff --git a/kernel/debug/kdb/kdb_bp.c b/kernel/debug/kdb/kdb_bp.c new file mode 100644 index 000000000000..75bd9b3ebbb7 --- /dev/null +++ b/kernel/debug/kdb/kdb_bp.c | |||
| @@ -0,0 +1,564 @@ | |||
| 1 | /* | ||
| 2 | * Kernel Debugger Architecture Independent Breakpoint Handler | ||
| 3 | * | ||
| 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 | ||
| 6 | * for more details. | ||
| 7 | * | ||
| 8 | * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved. | ||
| 9 | * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <linux/string.h> | ||
| 13 | #include <linux/kernel.h> | ||
| 14 | #include <linux/init.h> | ||
| 15 | #include <linux/kdb.h> | ||
| 16 | #include <linux/kgdb.h> | ||
| 17 | #include <linux/smp.h> | ||
| 18 | #include <linux/sched.h> | ||
| 19 | #include <linux/interrupt.h> | ||
| 20 | #include "kdb_private.h" | ||
| 21 | |||
| 22 | /* | ||
| 23 | * Table of kdb_breakpoints | ||
| 24 | */ | ||
| 25 | kdb_bp_t kdb_breakpoints[KDB_MAXBPT]; | ||
| 26 | |||
| 27 | static void kdb_setsinglestep(struct pt_regs *regs) | ||
| 28 | { | ||
| 29 | KDB_STATE_SET(DOING_SS); | ||
| 30 | } | ||
| 31 | |||
| 32 | static char *kdb_rwtypes[] = { | ||
| 33 | "Instruction(i)", | ||
| 34 | "Instruction(Register)", | ||
| 35 | "Data Write", | ||
| 36 | "I/O", | ||
| 37 | "Data Access" | ||
| 38 | }; | ||
| 39 | |||
| 40 | static char *kdb_bptype(kdb_bp_t *bp) | ||
| 41 | { | ||
| 42 | if (bp->bp_type < 0 || bp->bp_type > 4) | ||
| 43 | return ""; | ||
| 44 | |||
| 45 | return kdb_rwtypes[bp->bp_type]; | ||
| 46 | } | ||
| 47 | |||
| 48 | static int kdb_parsebp(int argc, const char **argv, int *nextargp, kdb_bp_t *bp) | ||
| 49 | { | ||
| 50 | int nextarg = *nextargp; | ||
| 51 | int diag; | ||
| 52 | |||
| 53 | bp->bph_length = 1; | ||
| 54 | if ((argc + 1) != nextarg) { | ||
| 55 | if (strnicmp(argv[nextarg], "datar", sizeof("datar")) == 0) | ||
| 56 | bp->bp_type = BP_ACCESS_WATCHPOINT; | ||
| 57 | else if (strnicmp(argv[nextarg], "dataw", sizeof("dataw")) == 0) | ||
| 58 | bp->bp_type = BP_WRITE_WATCHPOINT; | ||
| 59 | else if (strnicmp(argv[nextarg], "inst", sizeof("inst")) == 0) | ||
| 60 | bp->bp_type = BP_HARDWARE_BREAKPOINT; | ||
| 61 | else | ||
| 62 | return KDB_ARGCOUNT; | ||
| 63 | |||
| 64 | bp->bph_length = 1; | ||
| 65 | |||
| 66 | nextarg++; | ||
| 67 | |||
| 68 | if ((argc + 1) != nextarg) { | ||
| 69 | unsigned long len; | ||
| 70 | |||
| 71 | diag = kdbgetularg((char *)argv[nextarg], | ||
| 72 | &len); | ||
| 73 | if (diag) | ||
| 74 | return diag; | ||
| 75 | |||
| 76 | |||
| 77 | if (len > 8) | ||
| 78 | return KDB_BADLENGTH; | ||
| 79 | |||
| 80 | bp->bph_length = len; | ||
| 81 | nextarg++; | ||
| 82 | } | ||
| 83 | |||
| 84 | if ((argc + 1) != nextarg) | ||
| 85 | return KDB_ARGCOUNT; | ||
| 86 | } | ||
| 87 | |||
| 88 | *nextargp = nextarg; | ||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | static int _kdb_bp_remove(kdb_bp_t *bp) | ||
| 93 | { | ||
| 94 | int ret = 1; | ||
| 95 | if (!bp->bp_installed) | ||
| 96 | return ret; | ||
| 97 | if (!bp->bp_type) | ||
| 98 | ret = dbg_remove_sw_break(bp->bp_addr); | ||
| 99 | else | ||
| 100 | ret = arch_kgdb_ops.remove_hw_breakpoint(bp->bp_addr, | ||
| 101 | bp->bph_length, | ||
| 102 | bp->bp_type); | ||
| 103 | if (ret == 0) | ||
| 104 | bp->bp_installed = 0; | ||
| 105 | return ret; | ||
| 106 | } | ||
| 107 | |||
| 108 | static void kdb_handle_bp(struct pt_regs *regs, kdb_bp_t *bp) | ||
| 109 | { | ||
| 110 | if (KDB_DEBUG(BP)) | ||
| 111 | kdb_printf("regs->ip = 0x%lx\n", instruction_pointer(regs)); | ||
| 112 | |||
| 113 | /* | ||
| 114 | * Setup single step | ||
| 115 | */ | ||
| 116 | kdb_setsinglestep(regs); | ||
| 117 | |||
| 118 | /* | ||
| 119 | * Reset delay attribute | ||
| 120 | */ | ||
| 121 | bp->bp_delay = 0; | ||
| 122 | bp->bp_delayed = 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | static int _kdb_bp_install(struct pt_regs *regs, kdb_bp_t *bp) | ||
| 126 | { | ||
| 127 | int ret; | ||
| 128 | /* | ||
| 129 | * Install the breakpoint, if it is not already installed. | ||
| 130 | */ | ||
| 131 | |||
| 132 | if (KDB_DEBUG(BP)) | ||
| 133 | kdb_printf("%s: bp_installed %d\n", | ||
| 134 | __func__, bp->bp_installed); | ||
| 135 | if (!KDB_STATE(SSBPT)) | ||
| 136 | bp->bp_delay = 0; | ||
| 137 | if (bp->bp_installed) | ||
| 138 | return 1; | ||
| 139 | if (bp->bp_delay || (bp->bp_delayed && KDB_STATE(DOING_SS))) { | ||
| 140 | if (KDB_DEBUG(BP)) | ||
| 141 | kdb_printf("%s: delayed bp\n", __func__); | ||
| 142 | kdb_handle_bp(regs, bp); | ||
| 143 | return 0; | ||
| 144 | } | ||
| 145 | if (!bp->bp_type) | ||
| 146 | ret = dbg_set_sw_break(bp->bp_addr); | ||
| 147 | else | ||
| 148 | ret = arch_kgdb_ops.set_hw_breakpoint(bp->bp_addr, | ||
| 149 | bp->bph_length, | ||
| 150 | bp->bp_type); | ||
| 151 | if (ret == 0) { | ||
| 152 | bp->bp_installed = 1; | ||
| 153 | } else { | ||
| 154 | kdb_printf("%s: failed to set breakpoint at 0x%lx\n", | ||
| 155 | __func__, bp->bp_addr); | ||
| 156 | return 1; | ||
| 157 | } | ||
| 158 | return 0; | ||
| 159 | } | ||
| 160 | |||
| 161 | /* | ||
| 162 | * kdb_bp_install | ||
| 163 | * | ||
| 164 | * Install kdb_breakpoints prior to returning from the | ||
| 165 | * kernel debugger. This allows the kdb_breakpoints to be set | ||
| 166 | * upon functions that are used internally by kdb, such as | ||
| 167 | * printk(). This function is only called once per kdb session. | ||
| 168 | */ | ||
| 169 | void kdb_bp_install(struct pt_regs *regs) | ||
| 170 | { | ||
| 171 | int i; | ||
| 172 | |||
| 173 | for (i = 0; i < KDB_MAXBPT; i++) { | ||
| 174 | kdb_bp_t *bp = &kdb_breakpoints[i]; | ||
| 175 | |||
| 176 | if (KDB_DEBUG(BP)) { | ||
| 177 | kdb_printf("%s: bp %d bp_enabled %d\n", | ||
| 178 | __func__, i, bp->bp_enabled); | ||
| 179 | } | ||
| 180 | if (bp->bp_enabled) | ||
| 181 | _kdb_bp_install(regs, bp); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | /* | ||
| 186 | * kdb_bp_remove | ||
| 187 | * | ||
| 188 | * Remove kdb_breakpoints upon entry to the kernel debugger. | ||
| 189 | * | ||
| 190 | * Parameters: | ||
| 191 | * None. | ||
| 192 | * Outputs: | ||
| 193 | * None. | ||
| 194 | * Returns: | ||
| 195 | * None. | ||
| 196 | * Locking: | ||
| 197 | * None. | ||
| 198 | * Remarks: | ||
| 199 | */ | ||
| 200 | void kdb_bp_remove(void) | ||
| 201 | { | ||
| 202 | int i; | ||
| 203 | |||
| 204 | for (i = KDB_MAXBPT - 1; i >= 0; i--) { | ||
| 205 | kdb_bp_t *bp = &kdb_breakpoints[i]; | ||
| 206 | |||
| 207 | if (KDB_DEBUG(BP)) { | ||
| 208 | kdb_printf("%s: bp %d bp_enabled %d\n", | ||
| 209 | __func__, i, bp->bp_enabled); | ||
| 210 | } | ||
| 211 | if (bp->bp_enabled) | ||
| 212 | _kdb_bp_remove(bp); | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | /* | ||
| 218 | * kdb_printbp | ||
| 219 | * | ||
| 220 | * Internal function to format and print a breakpoint entry. | ||
| 221 | * | ||
| 222 | * Parameters: | ||
| 223 | * None. | ||
| 224 | * Outputs: | ||
| 225 | * None. | ||
| 226 | * Returns: | ||
| 227 | * None. | ||
| 228 | * Locking: | ||
| 229 | * None. | ||
| 230 | * Remarks: | ||
| 231 | */ | ||
| 232 | |||
| 233 | static void kdb_printbp(kdb_bp_t *bp, int i) | ||
| 234 | { | ||
| 235 | kdb_printf("%s ", kdb_bptype(bp)); | ||
| 236 | kdb_printf("BP #%d at ", i); | ||
| 237 | kdb_symbol_print(bp->bp_addr, NULL, KDB_SP_DEFAULT); | ||
| 238 | |||
| 239 | if (bp->bp_enabled) | ||
| 240 | kdb_printf("\n is enabled"); | ||
| 241 | else | ||
| 242 | kdb_printf("\n is disabled"); | ||
| 243 | |||
| 244 | kdb_printf("\taddr at %016lx, hardtype=%d installed=%d\n", | ||
| 245 | bp->bp_addr, bp->bp_type, bp->bp_installed); | ||
| 246 | |||
| 247 | kdb_printf("\n"); | ||
| 248 | } | ||
| 249 | |||
| 250 | /* | ||
| 251 | * kdb_bp | ||
| 252 | * | ||
| 253 | * Handle the bp commands. | ||
| 254 | * | ||
| 255 | * [bp|bph] <addr-expression> [DATAR|DATAW] | ||
| 256 | * | ||
| 257 | * Parameters: | ||
| 258 | * argc Count of arguments in argv | ||
| 259 | * argv Space delimited command line arguments | ||
| 260 | * Outputs: | ||
| 261 | * None. | ||
| 262 | * Returns: | ||
| 263 | * Zero for success, a kdb diagnostic if failure. | ||
| 264 | * Locking: | ||
| 265 | * None. | ||
| 266 | * Remarks: | ||
| 267 | * | ||
| 268 | * bp Set breakpoint on all cpus. Only use hardware assist if need. | ||
| 269 | * bph Set breakpoint on all cpus. Force hardware register | ||
| 270 | */ | ||
| 271 | |||
| 272 | static int kdb_bp(int argc, const char **argv) | ||
| 273 | { | ||
| 274 | int i, bpno; | ||
| 275 | kdb_bp_t *bp, *bp_check; | ||
| 276 | int diag; | ||
| 277 | int free; | ||
| 278 | char *symname = NULL; | ||
| 279 | long offset = 0ul; | ||
| 280 | int nextarg; | ||
| 281 | kdb_bp_t template = {0}; | ||
| 282 | |||
| 283 | if (argc == 0) { | ||
| 284 | /* | ||
| 285 | * Display breakpoint table | ||
| 286 | */ | ||
| 287 | for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; | ||
| 288 | bpno++, bp++) { | ||
| 289 | if (bp->bp_free) | ||
| 290 | continue; | ||
| 291 | kdb_printbp(bp, bpno); | ||
| 292 | } | ||
| 293 | |||
| 294 | return 0; | ||
| 295 | } | ||
| 296 | |||
| 297 | nextarg = 1; | ||
| 298 | diag = kdbgetaddrarg(argc, argv, &nextarg, &template.bp_addr, | ||
| 299 | &offset, &symname); | ||
| 300 | if (diag) | ||
| 301 | return diag; | ||
| 302 | if (!template.bp_addr) | ||
| 303 | return KDB_BADINT; | ||
| 304 | |||
| 305 | /* | ||
| 306 | * Find an empty bp structure to allocate | ||
| 307 | */ | ||
| 308 | free = KDB_MAXBPT; | ||
| 309 | for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) { | ||
| 310 | if (bp->bp_free) | ||
| 311 | break; | ||
| 312 | } | ||
| 313 | |||
| 314 | if (bpno == KDB_MAXBPT) | ||
| 315 | return KDB_TOOMANYBPT; | ||
| 316 | |||
| 317 | if (strcmp(argv[0], "bph") == 0) { | ||
| 318 | template.bp_type = BP_HARDWARE_BREAKPOINT; | ||
| 319 | diag = kdb_parsebp(argc, argv, &nextarg, &template); | ||
| 320 | if (diag) | ||
| 321 | return diag; | ||
| 322 | } else { | ||
| 323 | template.bp_type = BP_BREAKPOINT; | ||
| 324 | } | ||
| 325 | |||
| 326 | /* | ||
| 327 | * Check for clashing breakpoints. | ||
| 328 | * | ||
| 329 | * Note, in this design we can't have hardware breakpoints | ||
| 330 | * enabled for both read and write on the same address. | ||
| 331 | */ | ||
| 332 | for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT; | ||
| 333 | i++, bp_check++) { | ||
| 334 | if (!bp_check->bp_free && | ||
| 335 | bp_check->bp_addr == template.bp_addr) { | ||
| 336 | kdb_printf("You already have a breakpoint at " | ||
| 337 | kdb_bfd_vma_fmt0 "\n", template.bp_addr); | ||
| 338 | return KDB_DUPBPT; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | template.bp_enabled = 1; | ||
| 343 | |||
| 344 | /* | ||
| 345 | * Actually allocate the breakpoint found earlier | ||
| 346 | */ | ||
| 347 | *bp = template; | ||
| 348 | bp->bp_free = 0; | ||
| 349 | |||
| 350 | kdb_printbp(bp, bpno); | ||
| 351 | |||
| 352 | return 0; | ||
| 353 | } | ||
| 354 | |||
| 355 | /* | ||
| 356 | * kdb_bc | ||
| 357 | * | ||
| 358 | * Handles the 'bc', 'be', and 'bd' commands | ||
| 359 | * | ||
| 360 | * [bd|bc|be] <breakpoint-number> | ||
| 361 | * [bd|bc|be] * | ||
| 362 | * | ||
| 363 | * Parameters: | ||
| 364 | * argc Count of arguments in argv | ||
| 365 | * argv Space delimited command line arguments | ||
| 366 | * Outputs: | ||
| 367 | * None. | ||
| 368 | * Returns: | ||
| 369 | * Zero for success, a kdb diagnostic for failure | ||
| 370 | * Locking: | ||
| 371 | * None. | ||
| 372 | * Remarks: | ||
| 373 | */ | ||
| 374 | static int kdb_bc(int argc, const char **argv) | ||
| 375 | { | ||
| 376 | unsigned long addr; | ||
| 377 | kdb_bp_t *bp = NULL; | ||
| 378 | int lowbp = KDB_MAXBPT; | ||
| 379 | int highbp = 0; | ||
| 380 | int done = 0; | ||
| 381 | int i; | ||
| 382 | int diag = 0; | ||
| 383 | |||
| 384 | int cmd; /* KDBCMD_B? */ | ||
| 385 | #define KDBCMD_BC 0 | ||
| 386 | #define KDBCMD_BE 1 | ||
| 387 | #define KDBCMD_BD 2 | ||
| 388 | |||
| 389 | if (strcmp(argv[0], "be") == 0) | ||
| 390 | cmd = KDBCMD_BE; | ||
| 391 | else if (strcmp(argv[0], "bd") == 0) | ||
| 392 | cmd = KDBCMD_BD; | ||
| 393 | else | ||
| 394 | cmd = KDBCMD_BC; | ||
| 395 | |||
| 396 | if (argc != 1) | ||
| 397 | return KDB_ARGCOUNT; | ||
| 398 | |||
| 399 | if (strcmp(argv[1], "*") == 0) { | ||
| 400 | lowbp = 0; | ||
| 401 | highbp = KDB_MAXBPT; | ||
| 402 | } else { | ||
| 403 | diag = kdbgetularg(argv[1], &addr); | ||
| 404 | if (diag) | ||
| 405 | return diag; | ||
| 406 | |||
| 407 | /* | ||
| 408 | * For addresses less than the maximum breakpoint number, | ||
| 409 | * assume that the breakpoint number is desired. | ||
| 410 | */ | ||
| 411 | if (addr < KDB_MAXBPT) { | ||
| 412 | bp = &kdb_breakpoints[addr]; | ||
| 413 | lowbp = highbp = addr; | ||
| 414 | highbp++; | ||
| 415 | } else { | ||
| 416 | for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; | ||
| 417 | i++, bp++) { | ||
| 418 | if (bp->bp_addr == addr) { | ||
| 419 | lowbp = highbp = i; | ||
| 420 | highbp++; | ||
| 421 | break; | ||
| 422 | } | ||
| 423 | } | ||
| 424 | } | ||
| 425 | } | ||
| 426 | |||
| 427 | /* | ||
| 428 | * Now operate on the set of breakpoints matching the input | ||
| 429 | * criteria (either '*' for all, or an individual breakpoint). | ||
| 430 | */ | ||
| 431 | for (bp = &kdb_breakpoints[lowbp], i = lowbp; | ||
| 432 | i < highbp; | ||
| 433 | i++, bp++) { | ||
| 434 | if (bp->bp_free) | ||
| 435 | continue; | ||
| 436 | |||
| 437 | done++; | ||
| 438 | |||
| 439 | switch (cmd) { | ||
| 440 | case KDBCMD_BC: | ||
| 441 | bp->bp_enabled = 0; | ||
| 442 | |||
| 443 | kdb_printf("Breakpoint %d at " | ||
| 444 | kdb_bfd_vma_fmt " cleared\n", | ||
| 445 | i, bp->bp_addr); | ||
| 446 | |||
| 447 | bp->bp_addr = 0; | ||
| 448 | bp->bp_free = 1; | ||
| 449 | |||
| 450 | break; | ||
| 451 | case KDBCMD_BE: | ||
| 452 | bp->bp_enabled = 1; | ||
| 453 | |||
| 454 | kdb_printf("Breakpoint %d at " | ||
| 455 | kdb_bfd_vma_fmt " enabled", | ||
| 456 | i, bp->bp_addr); | ||
| 457 | |||
| 458 | kdb_printf("\n"); | ||
| 459 | break; | ||
| 460 | case KDBCMD_BD: | ||
| 461 | if (!bp->bp_enabled) | ||
| 462 | break; | ||
| 463 | |||
| 464 | bp->bp_enabled = 0; | ||
| 465 | |||
| 466 | kdb_printf("Breakpoint %d at " | ||
| 467 | kdb_bfd_vma_fmt " disabled\n", | ||
| 468 | i, bp->bp_addr); | ||
| 469 | |||
| 470 | break; | ||
| 471 | } | ||
| 472 | if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) { | ||
| 473 | bp->bp_delay = 0; | ||
| 474 | KDB_STATE_CLEAR(SSBPT); | ||
| 475 | } | ||
| 476 | } | ||
| 477 | |||
| 478 | return (!done) ? KDB_BPTNOTFOUND : 0; | ||
| 479 | } | ||
| 480 | |||
| 481 | /* | ||
| 482 | * kdb_ss | ||
| 483 | * | ||
| 484 | * Process the 'ss' (Single Step) and 'ssb' (Single Step to Branch) | ||
| 485 | * commands. | ||
| 486 | * | ||
| 487 | * ss | ||
| 488 | * ssb | ||
| 489 | * | ||
| 490 | * Parameters: | ||
| 491 | * argc Argument count | ||
| 492 | * argv Argument vector | ||
| 493 | * Outputs: | ||
| 494 | * None. | ||
| 495 | * Returns: | ||
| 496 | * KDB_CMD_SS[B] for success, a kdb error if failure. | ||
| 497 | * Locking: | ||
| 498 | * None. | ||
| 499 | * Remarks: | ||
| 500 | * | ||
| 501 | * Set the arch specific option to trigger a debug trap after the next | ||
| 502 | * instruction. | ||
| 503 | * | ||
| 504 | * For 'ssb', set the trace flag in the debug trap handler | ||
| 505 | * after printing the current insn and return directly without | ||
| 506 | * invoking the kdb command processor, until a branch instruction | ||
| 507 | * is encountered. | ||
| 508 | */ | ||
| 509 | |||
| 510 | static int kdb_ss(int argc, const char **argv) | ||
| 511 | { | ||
| 512 | int ssb = 0; | ||
| 513 | |||
| 514 | ssb = (strcmp(argv[0], "ssb") == 0); | ||
| 515 | if (argc != 0) | ||
| 516 | return KDB_ARGCOUNT; | ||
| 517 | /* | ||
| 518 | * Set trace flag and go. | ||
| 519 | */ | ||
| 520 | KDB_STATE_SET(DOING_SS); | ||
| 521 | if (ssb) { | ||
| 522 | KDB_STATE_SET(DOING_SSB); | ||
| 523 | return KDB_CMD_SSB; | ||
| 524 | } | ||
| 525 | return KDB_CMD_SS; | ||
| 526 | } | ||
| 527 | |||
| 528 | /* Initialize the breakpoint table and register breakpoint commands. */ | ||
| 529 | |||
| 530 | void __init kdb_initbptab(void) | ||
| 531 | { | ||
| 532 | int i; | ||
| 533 | kdb_bp_t *bp; | ||
| 534 | |||
| 535 | /* | ||
| 536 | * First time initialization. | ||
| 537 | */ | ||
| 538 | memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints)); | ||
| 539 | |||
| 540 | for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) | ||
| 541 | bp->bp_free = 1; | ||
| 542 | |||
| 543 | kdb_register_repeat("bp", kdb_bp, "[<vaddr>]", | ||
| 544 | "Set/Display breakpoints", 0, KDB_REPEAT_NO_ARGS); | ||
| 545 | kdb_register_repeat("bl", kdb_bp, "[<vaddr>]", | ||
| 546 | "Display breakpoints", 0, KDB_REPEAT_NO_ARGS); | ||
| 547 | if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT) | ||
| 548 | kdb_register_repeat("bph", kdb_bp, "[<vaddr>]", | ||
| 549 | "[datar [length]|dataw [length]] Set hw brk", 0, KDB_REPEAT_NO_ARGS); | ||
| 550 | kdb_register_repeat("bc", kdb_bc, "<bpnum>", | ||
| 551 | "Clear Breakpoint", 0, KDB_REPEAT_NONE); | ||
| 552 | kdb_register_repeat("be", kdb_bc, "<bpnum>", | ||
| 553 | "Enable Breakpoint", 0, KDB_REPEAT_NONE); | ||
| 554 | kdb_register_repeat("bd", kdb_bc, "<bpnum>", | ||
| 555 | "Disable Breakpoint", 0, KDB_REPEAT_NONE); | ||
| 556 | |||
| 557 | kdb_register_repeat("ss", kdb_ss, "", | ||
| 558 | "Single Step", 1, KDB_REPEAT_NO_ARGS); | ||
| 559 | kdb_register_repeat("ssb", kdb_ss, "", | ||
| 560 | "Single step to branch/call", 0, KDB_REPEAT_NO_ARGS); | ||
| 561 | /* | ||
| 562 | * Architecture dependent initialization. | ||
| 563 | */ | ||
| 564 | } | ||
diff --git a/kernel/debug/kdb/kdb_bt.c b/kernel/debug/kdb/kdb_bt.c new file mode 100644 index 000000000000..2f62fe85f16a --- /dev/null +++ b/kernel/debug/kdb/kdb_bt.c | |||
| @@ -0,0 +1,210 @@ | |||
| 1 | /* | ||
| 2 | * Kernel Debugger Architecture Independent Stack Traceback | ||
| 3 | * | ||
| 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 | ||
| 6 | * for more details. | ||
| 7 | * | ||
| 8 | * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved. | ||
| 9 | * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <linux/ctype.h> | ||
| 13 | #include <linux/string.h> | ||
| 14 | #include <linux/kernel.h> | ||
| 15 | #include <linux/sched.h> | ||
| 16 | #include <linux/kdb.h> | ||
| 17 | #include <linux/nmi.h> | ||
| 18 | #include <asm/system.h> | ||
| 19 | #include "kdb_private.h" | ||
| 20 | |||
| 21 | |||
| 22 | static void kdb_show_stack(struct task_struct *p, void *addr) | ||
| 23 | { | ||
| 24 | int old_lvl = console_loglevel; | ||
| 25 | console_loglevel = 15; | ||
| 26 | kdb_trap_printk++; | ||
| 27 | kdb_set_current_task(p); | ||
| 28 | if (addr) { | ||
| 29 | show_stack((struct task_struct *)p, addr); | ||
| 30 | } else if (kdb_current_regs) { | ||
| 31 | #ifdef CONFIG_X86 | ||
| 32 | show_stack(p, &kdb_current_regs->sp); | ||
| 33 | #else | ||
| 34 | show_stack(p, NULL); | ||
| 35 | #endif | ||
| 36 | } else { | ||
| 37 | show_stack(p, NULL); | ||
| 38 | } | ||
| 39 | console_loglevel = old_lvl; | ||
| 40 | kdb_trap_printk--; | ||
| 41 | } | ||
| 42 | |||
| 43 | /* | ||
| 44 | * kdb_bt | ||
| 45 | * | ||
| 46 | * This function implements the 'bt' command. Print a stack | ||
| 47 | * traceback. | ||
| 48 | * | ||
| 49 | * bt [<address-expression>] (addr-exp is for alternate stacks) | ||
| 50 | * btp <pid> Kernel stack for <pid> | ||
| 51 | * btt <address-expression> Kernel stack for task structure at | ||
| 52 | * <address-expression> | ||
| 53 | * bta [DRSTCZEUIMA] All useful processes, optionally | ||
| 54 | * filtered by state | ||
| 55 | * btc [<cpu>] The current process on one cpu, | ||
| 56 | * default is all cpus | ||
| 57 | * | ||
| 58 | * bt <address-expression> refers to a address on the stack, that location | ||
| 59 | * is assumed to contain a return address. | ||
| 60 | * | ||
| 61 | * btt <address-expression> refers to the address of a struct task. | ||
| 62 | * | ||
| 63 | * Inputs: | ||
| 64 | * argc argument count | ||
| 65 | * argv argument vector | ||
| 66 | * Outputs: | ||
| 67 | * None. | ||
| 68 | * Returns: | ||
| 69 | * zero for success, a kdb diagnostic if error | ||
| 70 | * Locking: | ||
| 71 | * none. | ||
| 72 | * Remarks: | ||
| 73 | * Backtrack works best when the code uses frame pointers. But even | ||
| 74 | * without frame pointers we should get a reasonable trace. | ||
| 75 | * | ||
| 76 | * mds comes in handy when examining the stack to do a manual traceback or | ||
| 77 | * to get a starting point for bt <address-expression>. | ||
| 78 | */ | ||
| 79 | |||
| 80 | static int | ||
| 81 | kdb_bt1(struct task_struct *p, unsigned long mask, | ||
| 82 | int argcount, int btaprompt) | ||
| 83 | { | ||
| 84 | char buffer[2]; | ||
| 85 | if (kdb_getarea(buffer[0], (unsigned long)p) || | ||
| 86 | kdb_getarea(buffer[0], (unsigned long)(p+1)-1)) | ||
| 87 | return KDB_BADADDR; | ||
| 88 | if (!kdb_task_state(p, mask)) | ||
| 89 | return 0; | ||
| 90 | kdb_printf("Stack traceback for pid %d\n", p->pid); | ||
| 91 | kdb_ps1(p); | ||
| 92 | kdb_show_stack(p, NULL); | ||
| 93 | if (btaprompt) { | ||
| 94 | kdb_getstr(buffer, sizeof(buffer), | ||
| 95 | "Enter <q> to end, <cr> to continue:"); | ||
| 96 | if (buffer[0] == 'q') { | ||
| 97 | kdb_printf("\n"); | ||
| 98 | return 1; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | touch_nmi_watchdog(); | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | int | ||
| 106 | kdb_bt(int argc, const char **argv) | ||
| 107 | { | ||
| 108 | int diag; | ||
| 109 | int argcount = 5; | ||
| 110 | int btaprompt = 1; | ||
| 111 | int nextarg; | ||
| 112 | unsigned long addr; | ||
| 113 | long offset; | ||
| 114 | |||
| 115 | kdbgetintenv("BTARGS", &argcount); /* Arguments to print */ | ||
| 116 | kdbgetintenv("BTAPROMPT", &btaprompt); /* Prompt after each | ||
| 117 | * proc in bta */ | ||
| 118 | |||
| 119 | if (strcmp(argv[0], "bta") == 0) { | ||
| 120 | struct task_struct *g, *p; | ||
| 121 | unsigned long cpu; | ||
| 122 | unsigned long mask = kdb_task_state_string(argc ? argv[1] : | ||
| 123 | NULL); | ||
| 124 | if (argc == 0) | ||
| 125 | kdb_ps_suppressed(); | ||
| 126 | /* Run the active tasks first */ | ||
| 127 | for_each_online_cpu(cpu) { | ||
| 128 | p = kdb_curr_task(cpu); | ||
| 129 | if (kdb_bt1(p, mask, argcount, btaprompt)) | ||
| 130 | return 0; | ||
| 131 | } | ||
| 132 | /* Now the inactive tasks */ | ||
| 133 | kdb_do_each_thread(g, p) { | ||
| 134 | if (task_curr(p)) | ||
| 135 | continue; | ||
| 136 | if (kdb_bt1(p, mask, argcount, btaprompt)) | ||
| 137 | return 0; | ||
| 138 | } kdb_while_each_thread(g, p); | ||
| 139 | } else if (strcmp(argv[0], "btp") == 0) { | ||
| 140 | struct task_struct *p; | ||
| 141 | unsigned long pid; | ||
| 142 | if (argc != 1) | ||
| 143 | return KDB_ARGCOUNT; | ||
| 144 | diag = kdbgetularg((char *)argv[1], &pid); | ||
| 145 | if (diag) | ||
| 146 | return diag; | ||
| 147 | p = find_task_by_pid_ns(pid, &init_pid_ns); | ||
| 148 | if (p) { | ||
| 149 | kdb_set_current_task(p); | ||
| 150 | return kdb_bt1(p, ~0UL, argcount, 0); | ||
| 151 | } | ||
| 152 | kdb_printf("No process with pid == %ld found\n", pid); | ||
| 153 | return 0; | ||
| 154 | } else if (strcmp(argv[0], "btt") == 0) { | ||
| 155 | if (argc != 1) | ||
| 156 | return KDB_ARGCOUNT; | ||
| 157 | diag = kdbgetularg((char *)argv[1], &addr); | ||
| 158 | if (diag) | ||
| 159 | return diag; | ||
| 160 | kdb_set_current_task((struct task_struct *)addr); | ||
| 161 | return kdb_bt1((struct task_struct *)addr, ~0UL, argcount, 0); | ||
| 162 | } else if (strcmp(argv[0], "btc") == 0) { | ||
| 163 | unsigned long cpu = ~0; | ||
| 164 | struct task_struct *save_current_task = kdb_current_task; | ||
| 165 | char buf[80]; | ||
| 166 | if (argc > 1) | ||
| 167 | return KDB_ARGCOUNT; | ||
| 168 | if (argc == 1) { | ||
| 169 | diag = kdbgetularg((char *)argv[1], &cpu); | ||
| 170 | if (diag) | ||
| 171 | return diag; | ||
| 172 | } | ||
| 173 | /* Recursive use of kdb_parse, do not use argv after | ||
| 174 | * this point */ | ||
| 175 | argv = NULL; | ||
| 176 | if (cpu != ~0) { | ||
| 177 | if (cpu >= num_possible_cpus() || !cpu_online(cpu)) { | ||
| 178 | kdb_printf("no process for cpu %ld\n", cpu); | ||
| 179 | return 0; | ||
| 180 | } | ||
| 181 | sprintf(buf, "btt 0x%p\n", KDB_TSK(cpu)); | ||
| 182 | kdb_parse(buf); | ||
| 183 | return 0; | ||
| 184 | } | ||
| 185 | kdb_printf("btc: cpu status: "); | ||
| 186 | kdb_parse("cpu\n"); | ||
| 187 | for_each_online_cpu(cpu) { | ||
| 188 | sprintf(buf, "btt 0x%p\n", KDB_TSK(cpu)); | ||
| 189 | kdb_parse(buf); | ||
| 190 | touch_nmi_watchdog(); | ||
| 191 | } | ||
| 192 | kdb_set_current_task(save_current_task); | ||
| 193 | return 0; | ||
| 194 | } else { | ||
| 195 | if (argc) { | ||
| 196 | nextarg = 1; | ||
| 197 | diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, | ||
| 198 | &offset, NULL); | ||
| 199 | if (diag) | ||
| 200 | return diag; | ||
| 201 | kdb_show_stack(kdb_current_task, (void *)addr); | ||
| 202 | return 0; | ||
| 203 | } else { | ||
| 204 | return kdb_bt1(kdb_current_task, ~0UL, argcount, 0); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | /* NOTREACHED */ | ||
| 209 | return 0; | ||
| 210 | } | ||
diff --git a/kernel/debug/kdb/kdb_cmds b/kernel/debug/kdb/kdb_cmds new file mode 100644 index 000000000000..56c88e4db309 --- /dev/null +++ b/kernel/debug/kdb/kdb_cmds | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | # Initial commands for kdb, alter to suit your needs. | ||
| 2 | # These commands are executed in kdb_init() context, no SMP, no | ||
| 3 | # processes. Commands that require process data (including stack or | ||
| 4 | # registers) are not reliable this early. set and bp commands should | ||
| 5 | # be safe. Global breakpoint commands affect each cpu as it is booted. | ||
| 6 | |||
| 7 | # Standard debugging information for first level support, just type archkdb | ||
| 8 | # or archkdbcpu or archkdbshort at the kdb prompt. | ||
| 9 | |||
| 10 | defcmd dumpcommon "" "Common kdb debugging" | ||
| 11 | set BTAPROMPT 0 | ||
| 12 | set LINES 10000 | ||
| 13 | -summary | ||
| 14 | -cpu | ||
| 15 | -ps | ||
| 16 | -dmesg 600 | ||
| 17 | -bt | ||
| 18 | endefcmd | ||
| 19 | |||
| 20 | defcmd dumpall "" "First line debugging" | ||
| 21 | set BTSYMARG 1 | ||
| 22 | set BTARGS 9 | ||
| 23 | pid R | ||
| 24 | -dumpcommon | ||
| 25 | -bta | ||
| 26 | endefcmd | ||
| 27 | |||
| 28 | defcmd dumpcpu "" "Same as dumpall but only tasks on cpus" | ||
| 29 | set BTSYMARG 1 | ||
| 30 | set BTARGS 9 | ||
| 31 | pid R | ||
| 32 | -dumpcommon | ||
| 33 | -btc | ||
| 34 | endefcmd | ||
| 35 | |||
diff --git a/kernel/debug/kdb/kdb_debugger.c b/kernel/debug/kdb/kdb_debugger.c new file mode 100644 index 000000000000..bf6e8270e957 --- /dev/null +++ b/kernel/debug/kdb/kdb_debugger.c | |||
| @@ -0,0 +1,169 @@ | |||
| 1 | /* | ||
| 2 | * Created by: Jason Wessel <jason.wessel@windriver.com> | ||
| 3 | * | ||
| 4 | * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. | ||
| 5 | * | ||
| 6 | * This file is licensed under the terms of the GNU General Public | ||
| 7 | * License version 2. This program is licensed "as is" without any | ||
| 8 | * warranty of any kind, whether express or implied. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <linux/kgdb.h> | ||
| 12 | #include <linux/kdb.h> | ||
| 13 | #include <linux/kdebug.h> | ||
| 14 | #include "kdb_private.h" | ||
| 15 | #include "../debug_core.h" | ||
| 16 | |||
| 17 | /* | ||
| 18 | * KDB interface to KGDB internals | ||
| 19 | */ | ||
| 20 | get_char_func kdb_poll_funcs[] = { | ||
| 21 | dbg_io_get_char, | ||
| 22 | NULL, | ||
| 23 | NULL, | ||
| 24 | NULL, | ||
| 25 | NULL, | ||
| 26 | NULL, | ||
| 27 | }; | ||
| 28 | EXPORT_SYMBOL_GPL(kdb_poll_funcs); | ||
| 29 | |||
| 30 | int kdb_poll_idx = 1; | ||
| 31 | EXPORT_SYMBOL_GPL(kdb_poll_idx); | ||
| 32 | |||
| 33 | int kdb_stub(struct kgdb_state *ks) | ||
| 34 | { | ||
| 35 | int error = 0; | ||
| 36 | kdb_bp_t *bp; | ||
| 37 | unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs); | ||
| 38 | kdb_reason_t reason = KDB_REASON_OOPS; | ||
| 39 | kdb_dbtrap_t db_result = KDB_DB_NOBPT; | ||
| 40 | int i; | ||
| 41 | |||
| 42 | if (KDB_STATE(REENTRY)) { | ||
| 43 | reason = KDB_REASON_SWITCH; | ||
| 44 | KDB_STATE_CLEAR(REENTRY); | ||
| 45 | addr = instruction_pointer(ks->linux_regs); | ||
| 46 | } | ||
| 47 | ks->pass_exception = 0; | ||
| 48 | if (atomic_read(&kgdb_setting_breakpoint)) | ||
| 49 | reason = KDB_REASON_KEYBOARD; | ||
| 50 | |||
| 51 | for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) { | ||
| 52 | if ((bp->bp_enabled) && (bp->bp_addr == addr)) { | ||
| 53 | reason = KDB_REASON_BREAK; | ||
| 54 | db_result = KDB_DB_BPT; | ||
| 55 | if (addr != instruction_pointer(ks->linux_regs)) | ||
| 56 | kgdb_arch_set_pc(ks->linux_regs, addr); | ||
| 57 | break; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) { | ||
| 61 | for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) { | ||
| 62 | if (bp->bp_free) | ||
| 63 | continue; | ||
| 64 | if (bp->bp_addr == addr) { | ||
| 65 | bp->bp_delay = 1; | ||
| 66 | bp->bp_delayed = 1; | ||
| 67 | /* | ||
| 68 | * SSBPT is set when the kernel debugger must single step a | ||
| 69 | * task in order to re-establish an instruction breakpoint | ||
| 70 | * which uses the instruction replacement mechanism. It is | ||
| 71 | * cleared by any action that removes the need to single-step | ||
| 72 | * the breakpoint. | ||
| 73 | */ | ||
| 74 | reason = KDB_REASON_BREAK; | ||
| 75 | db_result = KDB_DB_BPT; | ||
| 76 | KDB_STATE_SET(SSBPT); | ||
| 77 | break; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 && | ||
| 83 | ks->signo == SIGTRAP) { | ||
| 84 | reason = KDB_REASON_SSTEP; | ||
| 85 | db_result = KDB_DB_BPT; | ||
| 86 | } | ||
| 87 | /* Set initial kdb state variables */ | ||
| 88 | KDB_STATE_CLEAR(KGDB_TRANS); | ||
| 89 | kdb_initial_cpu = ks->cpu; | ||
| 90 | kdb_current_task = kgdb_info[ks->cpu].task; | ||
| 91 | kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo; | ||
| 92 | /* Remove any breakpoints as needed by kdb and clear single step */ | ||
| 93 | kdb_bp_remove(); | ||
| 94 | KDB_STATE_CLEAR(DOING_SS); | ||
| 95 | KDB_STATE_CLEAR(DOING_SSB); | ||
| 96 | KDB_STATE_SET(PAGER); | ||
| 97 | /* zero out any offline cpu data */ | ||
| 98 | for_each_present_cpu(i) { | ||
| 99 | if (!cpu_online(i)) { | ||
| 100 | kgdb_info[i].debuggerinfo = NULL; | ||
| 101 | kgdb_info[i].task = NULL; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) { | ||
| 105 | ks->pass_exception = 1; | ||
| 106 | KDB_FLAG_SET(CATASTROPHIC); | ||
| 107 | } | ||
| 108 | kdb_initial_cpu = ks->cpu; | ||
| 109 | if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) { | ||
| 110 | KDB_STATE_CLEAR(SSBPT); | ||
| 111 | KDB_STATE_CLEAR(DOING_SS); | ||
| 112 | } else { | ||
| 113 | /* Start kdb main loop */ | ||
| 114 | error = kdb_main_loop(KDB_REASON_ENTER, reason, | ||
| 115 | ks->err_code, db_result, ks->linux_regs); | ||
| 116 | } | ||
| 117 | /* | ||
| 118 | * Upon exit from the kdb main loop setup break points and restart | ||
| 119 | * the system based on the requested continue state | ||
| 120 | */ | ||
| 121 | kdb_initial_cpu = -1; | ||
| 122 | kdb_current_task = NULL; | ||
| 123 | kdb_current_regs = NULL; | ||
| 124 | KDB_STATE_CLEAR(PAGER); | ||
| 125 | kdbnearsym_cleanup(); | ||
| 126 | if (error == KDB_CMD_KGDB) { | ||
| 127 | if (KDB_STATE(DOING_KGDB) || KDB_STATE(DOING_KGDB2)) { | ||
| 128 | /* | ||
| 129 | * This inteface glue which allows kdb to transition in into | ||
| 130 | * the gdb stub. In order to do this the '?' or '' gdb serial | ||
| 131 | * packet response is processed here. And then control is | ||
| 132 | * passed to the gdbstub. | ||
| 133 | */ | ||
| 134 | if (KDB_STATE(DOING_KGDB)) | ||
| 135 | gdbstub_state(ks, "?"); | ||
| 136 | else | ||
| 137 | gdbstub_state(ks, ""); | ||
| 138 | KDB_STATE_CLEAR(DOING_KGDB); | ||
| 139 | KDB_STATE_CLEAR(DOING_KGDB2); | ||
| 140 | } | ||
| 141 | return DBG_PASS_EVENT; | ||
| 142 | } | ||
| 143 | kdb_bp_install(ks->linux_regs); | ||
| 144 | dbg_activate_sw_breakpoints(); | ||
| 145 | /* Set the exit state to a single step or a continue */ | ||
| 146 | if (KDB_STATE(DOING_SS)) | ||
| 147 | gdbstub_state(ks, "s"); | ||
| 148 | else | ||
| 149 | gdbstub_state(ks, "c"); | ||
| 150 | |||
| 151 | KDB_FLAG_CLEAR(CATASTROPHIC); | ||
| 152 | |||
| 153 | /* Invoke arch specific exception handling prior to system resume */ | ||
| 154 | kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e"); | ||
| 155 | if (ks->pass_exception) | ||
| 156 | kgdb_info[ks->cpu].ret_state = 1; | ||
| 157 | if (error == KDB_CMD_CPU) { | ||
| 158 | KDB_STATE_SET(REENTRY); | ||
| 159 | /* | ||
| 160 | * Force clear the single step bit because kdb emulates this | ||
| 161 | * differently vs the gdbstub | ||
| 162 | */ | ||
| 163 | kgdb_single_step = 0; | ||
| 164 | dbg_deactivate_sw_breakpoints(); | ||
| 165 | return DBG_SWITCH_CPU_EVENT; | ||
| 166 | } | ||
| 167 | return kgdb_info[ks->cpu].ret_state; | ||
| 168 | } | ||
| 169 | |||
diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c new file mode 100644 index 000000000000..c9b7f4f90bba --- /dev/null +++ b/kernel/debug/kdb/kdb_io.c | |||
| @@ -0,0 +1,826 @@ | |||
| 1 | /* | ||
| 2 | * Kernel Debugger Architecture Independent Console I/O handler | ||
| 3 | * | ||
| 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 | ||
| 6 | * for more details. | ||
| 7 | * | ||
| 8 | * Copyright (c) 1999-2006 Silicon Graphics, Inc. All Rights Reserved. | ||
| 9 | * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <linux/module.h> | ||
| 13 | #include <linux/types.h> | ||
| 14 | #include <linux/ctype.h> | ||
| 15 | #include <linux/kernel.h> | ||
| 16 | #include <linux/init.h> | ||
| 17 | #include <linux/kdev_t.h> | ||
| 18 | #include <linux/console.h> | ||
| 19 | #include <linux/string.h> | ||
| 20 | #include <linux/sched.h> | ||
| 21 | #include <linux/smp.h> | ||
| 22 | #include <linux/nmi.h> | ||
| 23 | #include <linux/delay.h> | ||
| 24 | #include <linux/kgdb.h> | ||
| 25 | #include <linux/kdb.h> | ||
| 26 | #include <linux/kallsyms.h> | ||
| 27 | #include "kdb_private.h" | ||
| 28 | |||
| 29 | #define CMD_BUFLEN 256 | ||
| 30 | char kdb_prompt_str[CMD_BUFLEN]; | ||
| 31 | |||
| 32 | int kdb_trap_printk; | ||
| 33 | |||
| 34 | static void kgdb_transition_check(char *buffer) | ||
| 35 | { | ||
| 36 | int slen = strlen(buffer); | ||
| 37 | if (strncmp(buffer, "$?#3f", slen) != 0 && | ||
| 38 | strncmp(buffer, "$qSupported#37", slen) != 0 && | ||
| 39 | strncmp(buffer, "+$qSupported#37", slen) != 0) { | ||
| 40 | KDB_STATE_SET(KGDB_TRANS); | ||
| 41 | kdb_printf("%s", buffer); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | static int kdb_read_get_key(char *buffer, size_t bufsize) | ||
| 46 | { | ||
| 47 | #define ESCAPE_UDELAY 1000 | ||
| 48 | #define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY) /* 2 seconds worth of udelays */ | ||
| 49 | char escape_data[5]; /* longest vt100 escape sequence is 4 bytes */ | ||
| 50 | char *ped = escape_data; | ||
| 51 | int escape_delay = 0; | ||
| 52 | get_char_func *f, *f_escape = NULL; | ||
| 53 | int key; | ||
| 54 | |||
| 55 | for (f = &kdb_poll_funcs[0]; ; ++f) { | ||
| 56 | if (*f == NULL) { | ||
| 57 | /* Reset NMI watchdog once per poll loop */ | ||
| 58 | touch_nmi_watchdog(); | ||
| 59 | f = &kdb_poll_funcs[0]; | ||
| 60 | } | ||
| 61 | if (escape_delay == 2) { | ||
| 62 | *ped = '\0'; | ||
| 63 | ped = escape_data; | ||
| 64 | --escape_delay; | ||
| 65 | } | ||
| 66 | if (escape_delay == 1) { | ||
| 67 | key = *ped++; | ||
| 68 | if (!*ped) | ||
| 69 | --escape_delay; | ||
| 70 | break; | ||
| 71 | } | ||
| 72 | key = (*f)(); | ||
| 73 | if (key == -1) { | ||
| 74 | if (escape_delay) { | ||
| 75 | udelay(ESCAPE_UDELAY); | ||
| 76 | --escape_delay; | ||
| 77 | } | ||
| 78 | continue; | ||
| 79 | } | ||
| 80 | if (bufsize <= 2) { | ||
| 81 | if (key == '\r') | ||
| 82 | key = '\n'; | ||
| 83 | *buffer++ = key; | ||
| 84 | *buffer = '\0'; | ||
| 85 | return -1; | ||
| 86 | } | ||
| 87 | if (escape_delay == 0 && key == '\e') { | ||
| 88 | escape_delay = ESCAPE_DELAY; | ||
| 89 | ped = escape_data; | ||
| 90 | f_escape = f; | ||
| 91 | } | ||
| 92 | if (escape_delay) { | ||
| 93 | *ped++ = key; | ||
| 94 | if (f_escape != f) { | ||
| 95 | escape_delay = 2; | ||
| 96 | continue; | ||
| 97 | } | ||
| 98 | if (ped - escape_data == 1) { | ||
| 99 | /* \e */ | ||
| 100 | continue; | ||
| 101 | } else if (ped - escape_data == 2) { | ||
| 102 | /* \e<something> */ | ||
| 103 | if (key != '[') | ||
| 104 | escape_delay = 2; | ||
| 105 | continue; | ||
| 106 | } else if (ped - escape_data == 3) { | ||
| 107 | /* \e[<something> */ | ||
| 108 | int mapkey = 0; | ||
| 109 | switch (key) { | ||
| 110 | case 'A': /* \e[A, up arrow */ | ||
| 111 | mapkey = 16; | ||
| 112 | break; | ||
| 113 | case 'B': /* \e[B, down arrow */ | ||
| 114 | mapkey = 14; | ||
| 115 | break; | ||
| 116 | case 'C': /* \e[C, right arrow */ | ||
| 117 | mapkey = 6; | ||
| 118 | break; | ||
| 119 | case 'D': /* \e[D, left arrow */ | ||
| 120 | mapkey = 2; | ||
| 121 | break; | ||
| 122 | case '1': /* dropthrough */ | ||
| 123 | case '3': /* dropthrough */ | ||
| 124 | /* \e[<1,3,4>], may be home, del, end */ | ||
| 125 | case '4': | ||
| 126 | mapkey = -1; | ||
| 127 | break; | ||
| 128 | } | ||
| 129 | if (mapkey != -1) { | ||
| 130 | if (mapkey > 0) { | ||
| 131 | escape_data[0] = mapkey; | ||
| 132 | escape_data[1] = '\0'; | ||
| 133 | } | ||
| 134 | escape_delay = 2; | ||
| 135 | } | ||
| 136 | continue; | ||
| 137 | } else if (ped - escape_data == 4) { | ||
| 138 | /* \e[<1,3,4><something> */ | ||
| 139 | int mapkey = 0; | ||
| 140 | if (key == '~') { | ||
| 141 | switch (escape_data[2]) { | ||
| 142 | case '1': /* \e[1~, home */ | ||
| 143 | mapkey = 1; | ||
| 144 | break; | ||
| 145 | case '3': /* \e[3~, del */ | ||
| 146 | mapkey = 4; | ||
| 147 | break; | ||
| 148 | case '4': /* \e[4~, end */ | ||
| 149 | mapkey = 5; | ||
| 150 | break; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | if (mapkey > 0) { | ||
| 154 | escape_data[0] = mapkey; | ||
| 155 | escape_data[1] = '\0'; | ||
| 156 | } | ||
| 157 | escape_delay = 2; | ||
| 158 | continue; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | break; /* A key to process */ | ||
| 162 | } | ||
| 163 | return key; | ||
| 164 | } | ||
| 165 | |||
| 166 | /* | ||
| 167 | * kdb_read | ||
| 168 | * | ||
| 169 | * This function reads a string of characters, terminated by | ||
| 170 | * a newline, or by reaching the end of the supplied buffer, | ||
| 171 | * from the current kernel debugger console device. | ||
| 172 | * Parameters: | ||
| 173 | * buffer - Address of character buffer to receive input characters. | ||
| 174 | * bufsize - size, in bytes, of the character buffer | ||
| 175 | * Returns: | ||
| 176 | * Returns a pointer to the buffer containing the received | ||
| 177 | * character string. This string will be terminated by a | ||
| 178 | * newline character. | ||
| 179 | * Locking: | ||
| 180 | * No locks are required to be held upon entry to this | ||
| 181 | * function. It is not reentrant - it relies on the fact | ||
| 182 | * that while kdb is running on only one "master debug" cpu. | ||
| 183 | * Remarks: | ||
| 184 | * | ||
| 185 | * The buffer size must be >= 2. A buffer size of 2 means that the caller only | ||
| 186 | * wants a single key. | ||
| 187 | * | ||
| 188 | * An escape key could be the start of a vt100 control sequence such as \e[D | ||
| 189 | * (left arrow) or it could be a character in its own right. The standard | ||
| 190 | * method for detecting the difference is to wait for 2 seconds to see if there | ||
| 191 | * are any other characters. kdb is complicated by the lack of a timer service | ||
| 192 | * (interrupts are off), by multiple input sources and by the need to sometimes | ||
| 193 | * return after just one key. Escape sequence processing has to be done as | ||
| 194 | * states in the polling loop. | ||
| 195 | */ | ||
| 196 | |||
| 197 | static char *kdb_read(char *buffer, size_t bufsize) | ||
| 198 | { | ||
| 199 | char *cp = buffer; | ||
| 200 | char *bufend = buffer+bufsize-2; /* Reserve space for newline | ||
| 201 | * and null byte */ | ||
| 202 | char *lastchar; | ||
| 203 | char *p_tmp; | ||
| 204 | char tmp; | ||
| 205 | static char tmpbuffer[CMD_BUFLEN]; | ||
| 206 | int len = strlen(buffer); | ||
| 207 | int len_tmp; | ||
| 208 | int tab = 0; | ||
| 209 | int count; | ||
| 210 | int i; | ||
| 211 | int diag, dtab_count; | ||
| 212 | int key; | ||
| 213 | |||
| 214 | |||
| 215 | diag = kdbgetintenv("DTABCOUNT", &dtab_count); | ||
| 216 | if (diag) | ||
| 217 | dtab_count = 30; | ||
| 218 | |||
| 219 | if (len > 0) { | ||
| 220 | cp += len; | ||
| 221 | if (*(buffer+len-1) == '\n') | ||
| 222 | cp--; | ||
| 223 | } | ||
| 224 | |||
| 225 | lastchar = cp; | ||
| 226 | *cp = '\0'; | ||
| 227 | kdb_printf("%s", buffer); | ||
| 228 | poll_again: | ||
| 229 | key = kdb_read_get_key(buffer, bufsize); | ||
| 230 | if (key == -1) | ||
| 231 | return buffer; | ||
| 232 | if (key != 9) | ||
| 233 | tab = 0; | ||
| 234 | switch (key) { | ||
| 235 | case 8: /* backspace */ | ||
| 236 | if (cp > buffer) { | ||
| 237 | if (cp < lastchar) { | ||
| 238 | memcpy(tmpbuffer, cp, lastchar - cp); | ||
| 239 | memcpy(cp-1, tmpbuffer, lastchar - cp); | ||
| 240 | } | ||
| 241 | *(--lastchar) = '\0'; | ||
| 242 | --cp; | ||
| 243 | kdb_printf("\b%s \r", cp); | ||
| 244 | tmp = *cp; | ||
| 245 | *cp = '\0'; | ||
| 246 | kdb_printf(kdb_prompt_str); | ||
| 247 | kdb_printf("%s", buffer); | ||
| 248 | *cp = tmp; | ||
| 249 | } | ||
| 250 | break; | ||
| 251 | case 13: /* enter */ | ||
| 252 | *lastchar++ = '\n'; | ||
| 253 | *lastchar++ = '\0'; | ||
| 254 | kdb_printf("\n"); | ||
| 255 | return buffer; | ||
| 256 | case 4: /* Del */ | ||
| 257 | if (cp < lastchar) { | ||
| 258 | memcpy(tmpbuffer, cp+1, lastchar - cp - 1); | ||
| 259 | memcpy(cp, tmpbuffer, lastchar - cp - 1); | ||
| 260 | *(--lastchar) = '\0'; | ||
| 261 | kdb_printf("%s \r", cp); | ||
| 262 | tmp = *cp; | ||
| 263 | *cp = '\0'; | ||
| 264 | kdb_printf(kdb_prompt_str); | ||
| 265 | kdb_printf("%s", buffer); | ||
| 266 | *cp = tmp; | ||
| 267 | } | ||
| 268 | break; | ||
| 269 | case 1: /* Home */ | ||
| 270 | if (cp > buffer) { | ||
| 271 | kdb_printf("\r"); | ||
| 272 | kdb_printf(kdb_prompt_str); | ||
| 273 | cp = buffer; | ||
| 274 | } | ||
| 275 | break; | ||
| 276 | case 5: /* End */ | ||
| 277 | if (cp < lastchar) { | ||
| 278 | kdb_printf("%s", cp); | ||
| 279 | cp = lastchar; | ||
| 280 | } | ||
| 281 | break; | ||
| 282 | case 2: /* Left */ | ||
| 283 | if (cp > buffer) { | ||
| 284 | kdb_printf("\b"); | ||
| 285 | --cp; | ||
| 286 | } | ||
| 287 | break; | ||
| 288 | case 14: /* Down */ | ||
| 289 | memset(tmpbuffer, ' ', | ||
| 290 | strlen(kdb_prompt_str) + (lastchar-buffer)); | ||
| 291 | *(tmpbuffer+strlen(kdb_prompt_str) + | ||
| 292 | (lastchar-buffer)) = '\0'; | ||
| 293 | kdb_printf("\r%s\r", tmpbuffer); | ||
| 294 | *lastchar = (char)key; | ||
| 295 | *(lastchar+1) = '\0'; | ||
| 296 | return lastchar; | ||
| 297 | case 6: /* Right */ | ||
| 298 | if (cp < lastchar) { | ||
| 299 | kdb_printf("%c", *cp); | ||
| 300 | ++cp; | ||
| 301 | } | ||
| 302 | break; | ||
| 303 | case 16: /* Up */ | ||
| 304 | memset(tmpbuffer, ' ', | ||
| 305 | strlen(kdb_prompt_str) + (lastchar-buffer)); | ||
| 306 | *(tmpbuffer+strlen(kdb_prompt_str) + | ||
| 307 | (lastchar-buffer)) = '\0'; | ||
| 308 | kdb_printf("\r%s\r", tmpbuffer); | ||
| 309 | *lastchar = (char)key; | ||
| 310 | *(lastchar+1) = '\0'; | ||
| 311 | return lastchar; | ||
| 312 | case 9: /* Tab */ | ||
| 313 | if (tab < 2) | ||
| 314 | ++tab; | ||
| 315 | p_tmp = buffer; | ||
| 316 | while (*p_tmp == ' ') | ||
| 317 | p_tmp++; | ||
| 318 | if (p_tmp > cp) | ||
| 319 | break; | ||
| 320 | memcpy(tmpbuffer, p_tmp, cp-p_tmp); | ||
| 321 | *(tmpbuffer + (cp-p_tmp)) = '\0'; | ||
| 322 | p_tmp = strrchr(tmpbuffer, ' '); | ||
| 323 | if (p_tmp) | ||
| 324 | ++p_tmp; | ||
| 325 | else | ||
| 326 | p_tmp = tmpbuffer; | ||
| 327 | len = strlen(p_tmp); | ||
| 328 | count = kallsyms_symbol_complete(p_tmp, | ||
| 329 | sizeof(tmpbuffer) - | ||
| 330 | (p_tmp - tmpbuffer)); | ||
| 331 | if (tab == 2 && count > 0) { | ||
| 332 | kdb_printf("\n%d symbols are found.", count); | ||
| 333 | if (count > dtab_count) { | ||
| 334 | count = dtab_count; | ||
| 335 | kdb_printf(" But only first %d symbols will" | ||
| 336 | " be printed.\nYou can change the" | ||
| 337 | " environment variable DTABCOUNT.", | ||
| 338 | count); | ||
| 339 | } | ||
| 340 | kdb_printf("\n"); | ||
| 341 | for (i = 0; i < count; i++) { | ||
| 342 | if (kallsyms_symbol_next(p_tmp, i) < 0) | ||
| 343 | break; | ||
| 344 | kdb_printf("%s ", p_tmp); | ||
| 345 | *(p_tmp + len) = '\0'; | ||
| 346 | } | ||
| 347 | if (i >= dtab_count) | ||
| 348 | kdb_printf("..."); | ||
| 349 | kdb_printf("\n"); | ||
| 350 | kdb_printf(kdb_prompt_str); | ||
| 351 | kdb_printf("%s", buffer); | ||
| 352 | } else if (tab != 2 && count > 0) { | ||
| 353 | len_tmp = strlen(p_tmp); | ||
| 354 | strncpy(p_tmp+len_tmp, cp, lastchar-cp+1); | ||
| 355 | len_tmp = strlen(p_tmp); | ||
| 356 | strncpy(cp, p_tmp+len, len_tmp-len + 1); | ||
| 357 | len = len_tmp - len; | ||
| 358 | kdb_printf("%s", cp); | ||
| 359 | cp += len; | ||
| 360 | lastchar += len; | ||
| 361 | } | ||
| 362 | kdb_nextline = 1; /* reset output line number */ | ||
| 363 | break; | ||
| 364 | default: | ||
| 365 | if (key >= 32 && lastchar < bufend) { | ||
| 366 | if (cp < lastchar) { | ||
| 367 | memcpy(tmpbuffer, cp, lastchar - cp); | ||
| 368 | memcpy(cp+1, tmpbuffer, lastchar - cp); | ||
| 369 | *++lastchar = '\0'; | ||
| 370 | *cp = key; | ||
| 371 | kdb_printf("%s\r", cp); | ||
| 372 | ++cp; | ||
| 373 | tmp = *cp; | ||
| 374 | *cp = '\0'; | ||
| 375 | kdb_printf(kdb_prompt_str); | ||
| 376 | kdb_printf("%s", buffer); | ||
| 377 | *cp = tmp; | ||
| 378 | } else { | ||
| 379 | *++lastchar = '\0'; | ||
| 380 | *cp++ = key; | ||
| 381 | /* The kgdb transition check will hide | ||
| 382 | * printed characters if we think that | ||
| 383 | * kgdb is connecting, until the check | ||
| 384 | * fails */ | ||
| 385 | if (!KDB_STATE(KGDB_TRANS)) | ||
| 386 | kgdb_transition_check(buffer); | ||
| 387 | else | ||
| 388 | kdb_printf("%c", key); | ||
| 389 | } | ||
| 390 | /* Special escape to kgdb */ | ||
| 391 | if (lastchar - buffer >= 5 && | ||
| 392 | strcmp(lastchar - 5, "$?#3f") == 0) { | ||
| 393 | strcpy(buffer, "kgdb"); | ||
| 394 | KDB_STATE_SET(DOING_KGDB); | ||
| 395 | return buffer; | ||
| 396 | } | ||
| 397 | if (lastchar - buffer >= 14 && | ||
| 398 | strcmp(lastchar - 14, "$qSupported#37") == 0) { | ||
| 399 | strcpy(buffer, "kgdb"); | ||
| 400 | KDB_STATE_SET(DOING_KGDB2); | ||
| 401 | return buffer; | ||
| 402 | } | ||
| 403 | } | ||
| 404 | break; | ||
| 405 | } | ||
| 406 | goto poll_again; | ||
| 407 | } | ||
| 408 | |||
| 409 | /* | ||
| 410 | * kdb_getstr | ||
| 411 | * | ||
| 412 | * Print the prompt string and read a command from the | ||
| 413 | * input device. | ||
| 414 | * | ||
| 415 | * Parameters: | ||
| 416 | * buffer Address of buffer to receive command | ||
| 417 | * bufsize Size of buffer in bytes | ||
| 418 | * prompt Pointer to string to use as prompt string | ||
| 419 | * Returns: | ||
| 420 | * Pointer to command buffer. | ||
| 421 | * Locking: | ||
| 422 | * None. | ||
| 423 | * Remarks: | ||
| 424 | * For SMP kernels, the processor number will be | ||
| 425 | * substituted for %d, %x or %o in the prompt. | ||
| 426 | */ | ||
| 427 | |||
| 428 | char *kdb_getstr(char *buffer, size_t bufsize, char *prompt) | ||
| 429 | { | ||
| 430 | if (prompt && kdb_prompt_str != prompt) | ||
| 431 | strncpy(kdb_prompt_str, prompt, CMD_BUFLEN); | ||
| 432 | kdb_printf(kdb_prompt_str); | ||
| 433 | kdb_nextline = 1; /* Prompt and input resets line number */ | ||
| 434 | return kdb_read(buffer, bufsize); | ||
| 435 | } | ||
| 436 | |||
| 437 | /* | ||
| 438 | * kdb_input_flush | ||
| 439 | * | ||
| 440 | * Get rid of any buffered console input. | ||
| 441 | * | ||
| 442 | * Parameters: | ||
| 443 | * none | ||
| 444 | * Returns: | ||
| 445 | * nothing | ||
| 446 | * Locking: | ||
| 447 | * none | ||
| 448 | * Remarks: | ||
| 449 | * Call this function whenever you want to flush input. If there is any | ||
| 450 | * outstanding input, it ignores all characters until there has been no | ||
| 451 | * data for approximately 1ms. | ||
| 452 | */ | ||
| 453 | |||
| 454 | static void kdb_input_flush(void) | ||
| 455 | { | ||
| 456 | get_char_func *f; | ||
| 457 | int res; | ||
| 458 | int flush_delay = 1; | ||
| 459 | while (flush_delay) { | ||
| 460 | flush_delay--; | ||
| 461 | empty: | ||
| 462 | touch_nmi_watchdog(); | ||
| 463 | for (f = &kdb_poll_funcs[0]; *f; ++f) { | ||
| 464 | res = (*f)(); | ||
| 465 | if (res != -1) { | ||
| 466 | flush_delay = 1; | ||
| 467 | goto empty; | ||
| 468 | } | ||
| 469 | } | ||
| 470 | if (flush_delay) | ||
| 471 | mdelay(1); | ||
| 472 | } | ||
| 473 | } | ||
| 474 | |||
| 475 | /* | ||
| 476 | * kdb_printf | ||
| 477 | * | ||
| 478 | * Print a string to the output device(s). | ||
| 479 | * | ||
| 480 | * Parameters: | ||
| 481 | * printf-like format and optional args. | ||
| 482 | * Returns: | ||
| 483 | * 0 | ||
| 484 | * Locking: | ||
| 485 | * None. | ||
| 486 | * Remarks: | ||
| 487 | * use 'kdbcons->write()' to avoid polluting 'log_buf' with | ||
| 488 | * kdb output. | ||
| 489 | * | ||
| 490 | * If the user is doing a cmd args | grep srch | ||
| 491 | * then kdb_grepping_flag is set. | ||
| 492 | * In that case we need to accumulate full lines (ending in \n) before | ||
| 493 | * searching for the pattern. | ||
| 494 | */ | ||
| 495 | |||
| 496 | static char kdb_buffer[256]; /* A bit too big to go on stack */ | ||
| 497 | static char *next_avail = kdb_buffer; | ||
| 498 | static int size_avail; | ||
| 499 | static int suspend_grep; | ||
| 500 | |||
| 501 | /* | ||
| 502 | * search arg1 to see if it contains arg2 | ||
| 503 | * (kdmain.c provides flags for ^pat and pat$) | ||
| 504 | * | ||
| 505 | * return 1 for found, 0 for not found | ||
| 506 | */ | ||
| 507 | static int kdb_search_string(char *searched, char *searchfor) | ||
| 508 | { | ||
| 509 | char firstchar, *cp; | ||
| 510 | int len1, len2; | ||
| 511 | |||
| 512 | /* not counting the newline at the end of "searched" */ | ||
| 513 | len1 = strlen(searched)-1; | ||
| 514 | len2 = strlen(searchfor); | ||
| 515 | if (len1 < len2) | ||
| 516 | return 0; | ||
| 517 | if (kdb_grep_leading && kdb_grep_trailing && len1 != len2) | ||
| 518 | return 0; | ||
| 519 | if (kdb_grep_leading) { | ||
| 520 | if (!strncmp(searched, searchfor, len2)) | ||
| 521 | return 1; | ||
| 522 | } else if (kdb_grep_trailing) { | ||
| 523 | if (!strncmp(searched+len1-len2, searchfor, len2)) | ||
| 524 | return 1; | ||
| 525 | } else { | ||
| 526 | firstchar = *searchfor; | ||
| 527 | cp = searched; | ||
| 528 | while ((cp = strchr(cp, firstchar))) { | ||
| 529 | if (!strncmp(cp, searchfor, len2)) | ||
| 530 | return 1; | ||
| 531 | cp++; | ||
| 532 | } | ||
| 533 | } | ||
| 534 | return 0; | ||
| 535 | } | ||
| 536 | |||
| 537 | int vkdb_printf(const char *fmt, va_list ap) | ||
| 538 | { | ||
| 539 | int diag; | ||
| 540 | int linecount; | ||
| 541 | int logging, saved_loglevel = 0; | ||
| 542 | int saved_trap_printk; | ||
| 543 | int got_printf_lock = 0; | ||
| 544 | int retlen = 0; | ||
| 545 | int fnd, len; | ||
| 546 | char *cp, *cp2, *cphold = NULL, replaced_byte = ' '; | ||
| 547 | char *moreprompt = "more> "; | ||
| 548 | struct console *c = console_drivers; | ||
| 549 | static DEFINE_SPINLOCK(kdb_printf_lock); | ||
| 550 | unsigned long uninitialized_var(flags); | ||
| 551 | |||
| 552 | preempt_disable(); | ||
| 553 | saved_trap_printk = kdb_trap_printk; | ||
| 554 | kdb_trap_printk = 0; | ||
| 555 | |||
| 556 | /* Serialize kdb_printf if multiple cpus try to write at once. | ||
| 557 | * But if any cpu goes recursive in kdb, just print the output, | ||
| 558 | * even if it is interleaved with any other text. | ||
| 559 | */ | ||
| 560 | if (!KDB_STATE(PRINTF_LOCK)) { | ||
| 561 | KDB_STATE_SET(PRINTF_LOCK); | ||
| 562 | spin_lock_irqsave(&kdb_printf_lock, flags); | ||
| 563 | got_printf_lock = 1; | ||
| 564 | atomic_inc(&kdb_event); | ||
| 565 | } else { | ||
| 566 | __acquire(kdb_printf_lock); | ||
| 567 | } | ||
| 568 | |||
| 569 | diag = kdbgetintenv("LINES", &linecount); | ||
| 570 | if (diag || linecount <= 1) | ||
| 571 | linecount = 24; | ||
| 572 | |||
| 573 | diag = kdbgetintenv("LOGGING", &logging); | ||
| 574 | if (diag) | ||
| 575 | logging = 0; | ||
| 576 | |||
| 577 | if (!kdb_grepping_flag || suspend_grep) { | ||
| 578 | /* normally, every vsnprintf starts a new buffer */ | ||
| 579 | next_avail = kdb_buffer; | ||
| 580 | size_avail = sizeof(kdb_buffer); | ||
| 581 | } | ||
| 582 | vsnprintf(next_avail, size_avail, fmt, ap); | ||
| 583 | |||
| 584 | /* | ||
| 585 | * If kdb_parse() found that the command was cmd xxx | grep yyy | ||
| 586 | * then kdb_grepping_flag is set, and kdb_grep_string contains yyy | ||
| 587 | * | ||
| 588 | * Accumulate the print data up to a newline before searching it. | ||
| 589 | * (vsnprintf does null-terminate the string that it generates) | ||
| 590 | */ | ||
| 591 | |||
| 592 | /* skip the search if prints are temporarily unconditional */ | ||
| 593 | if (!suspend_grep && kdb_grepping_flag) { | ||
| 594 | cp = strchr(kdb_buffer, '\n'); | ||
| 595 | if (!cp) { | ||
| 596 | /* | ||
| 597 | * Special cases that don't end with newlines | ||
| 598 | * but should be written without one: | ||
| 599 | * The "[nn]kdb> " prompt should | ||
| 600 | * appear at the front of the buffer. | ||
| 601 | * | ||
| 602 | * The "[nn]more " prompt should also be | ||
| 603 | * (MOREPROMPT -> moreprompt) | ||
| 604 | * written * but we print that ourselves, | ||
| 605 | * we set the suspend_grep flag to make | ||
| 606 | * it unconditional. | ||
| 607 | * | ||
| 608 | */ | ||
| 609 | if (next_avail == kdb_buffer) { | ||
| 610 | /* | ||
| 611 | * these should occur after a newline, | ||
| 612 | * so they will be at the front of the | ||
| 613 | * buffer | ||
| 614 | */ | ||
| 615 | cp2 = kdb_buffer; | ||
| 616 | len = strlen(kdb_prompt_str); | ||
| 617 | if (!strncmp(cp2, kdb_prompt_str, len)) { | ||
| 618 | /* | ||
| 619 | * We're about to start a new | ||
| 620 | * command, so we can go back | ||
| 621 | * to normal mode. | ||
| 622 | */ | ||
| 623 | kdb_grepping_flag = 0; | ||
| 624 | goto kdb_printit; | ||
| 625 | } | ||
| 626 | } | ||
| 627 | /* no newline; don't search/write the buffer | ||
| 628 | until one is there */ | ||
| 629 | len = strlen(kdb_buffer); | ||
| 630 | next_avail = kdb_buffer + len; | ||
| 631 | size_avail = sizeof(kdb_buffer) - len; | ||
| 632 | goto kdb_print_out; | ||
| 633 | } | ||
| 634 | |||
| 635 | /* | ||
| 636 | * The newline is present; print through it or discard | ||
| 637 | * it, depending on the results of the search. | ||
| 638 | */ | ||
| 639 | cp++; /* to byte after the newline */ | ||
| 640 | replaced_byte = *cp; /* remember what/where it was */ | ||
| 641 | cphold = cp; | ||
| 642 | *cp = '\0'; /* end the string for our search */ | ||
| 643 | |||
| 644 | /* | ||
| 645 | * We now have a newline at the end of the string | ||
| 646 | * Only continue with this output if it contains the | ||
| 647 | * search string. | ||
| 648 | */ | ||
| 649 | fnd = kdb_search_string(kdb_buffer, kdb_grep_string); | ||
| 650 | if (!fnd) { | ||
| 651 | /* | ||
| 652 | * At this point the complete line at the start | ||
| 653 | * of kdb_buffer can be discarded, as it does | ||
| 654 | * not contain what the user is looking for. | ||
| 655 | * Shift the buffer left. | ||
| 656 | */ | ||
| 657 | *cphold = replaced_byte; | ||
| 658 | strcpy(kdb_buffer, cphold); | ||
| 659 | len = strlen(kdb_buffer); | ||
| 660 | next_avail = kdb_buffer + len; | ||
| 661 | size_avail = sizeof(kdb_buffer) - len; | ||
| 662 | goto kdb_print_out; | ||
| 663 | } | ||
| 664 | /* | ||
| 665 | * at this point the string is a full line and | ||
| 666 | * should be printed, up to the null. | ||
| 667 | */ | ||
| 668 | } | ||
| 669 | kdb_printit: | ||
| 670 | |||
| 671 | /* | ||
| 672 | * Write to all consoles. | ||
| 673 | */ | ||
| 674 | retlen = strlen(kdb_buffer); | ||
| 675 | if (!dbg_kdb_mode && kgdb_connected) { | ||
| 676 | gdbstub_msg_write(kdb_buffer, retlen); | ||
| 677 | } else { | ||
| 678 | if (!dbg_io_ops->is_console) { | ||
| 679 | len = strlen(kdb_buffer); | ||
| 680 | cp = kdb_buffer; | ||
| 681 | while (len--) { | ||
| 682 | dbg_io_ops->write_char(*cp); | ||
| 683 | cp++; | ||
| 684 | } | ||
| 685 | } | ||
| 686 | while (c) { | ||
| 687 | c->write(c, kdb_buffer, retlen); | ||
| 688 | touch_nmi_watchdog(); | ||
| 689 | c = c->next; | ||
| 690 | } | ||
| 691 | } | ||
| 692 | if (logging) { | ||
| 693 | saved_loglevel = console_loglevel; | ||
| 694 | console_loglevel = 0; | ||
| 695 | printk(KERN_INFO "%s", kdb_buffer); | ||
| 696 | } | ||
| 697 | |||
| 698 | if (KDB_STATE(PAGER) && strchr(kdb_buffer, '\n')) | ||
| 699 | kdb_nextline++; | ||
| 700 | |||
| 701 | /* check for having reached the LINES number of printed lines */ | ||
| 702 | if (kdb_nextline == linecount) { | ||
| 703 | char buf1[16] = ""; | ||
| 704 | #if defined(CONFIG_SMP) | ||
| 705 | char buf2[32]; | ||
| 706 | #endif | ||
| 707 | |||
| 708 | /* Watch out for recursion here. Any routine that calls | ||
| 709 | * kdb_printf will come back through here. And kdb_read | ||
| 710 | * uses kdb_printf to echo on serial consoles ... | ||
| 711 | */ | ||
| 712 | kdb_nextline = 1; /* In case of recursion */ | ||
| 713 | |||
| 714 | /* | ||
| 715 | * Pause until cr. | ||
| 716 | */ | ||
| 717 | moreprompt = kdbgetenv("MOREPROMPT"); | ||
| 718 | if (moreprompt == NULL) | ||
| 719 | moreprompt = "more> "; | ||
| 720 | |||
| 721 | #if defined(CONFIG_SMP) | ||
| 722 | if (strchr(moreprompt, '%')) { | ||
| 723 | sprintf(buf2, moreprompt, get_cpu()); | ||
| 724 | put_cpu(); | ||
| 725 | moreprompt = buf2; | ||
| 726 | } | ||
| 727 | #endif | ||
| 728 | |||
| 729 | kdb_input_flush(); | ||
| 730 | c = console_drivers; | ||
| 731 | |||
| 732 | if (!dbg_io_ops->is_console) { | ||
| 733 | len = strlen(moreprompt); | ||
| 734 | cp = moreprompt; | ||
| 735 | while (len--) { | ||
| 736 | dbg_io_ops->write_char(*cp); | ||
| 737 | cp++; | ||
| 738 | } | ||
| 739 | } | ||
| 740 | while (c) { | ||
| 741 | c->write(c, moreprompt, strlen(moreprompt)); | ||
| 742 | touch_nmi_watchdog(); | ||
| 743 | c = c->next; | ||
| 744 | } | ||
| 745 | |||
| 746 | if (logging) | ||
| 747 | printk("%s", moreprompt); | ||
| 748 | |||
| 749 | kdb_read(buf1, 2); /* '2' indicates to return | ||
| 750 | * immediately after getting one key. */ | ||
| 751 | kdb_nextline = 1; /* Really set output line 1 */ | ||
| 752 | |||
| 753 | /* empty and reset the buffer: */ | ||
| 754 | kdb_buffer[0] = '\0'; | ||
| 755 | next_avail = kdb_buffer; | ||
| 756 | size_avail = sizeof(kdb_buffer); | ||
| 757 | if ((buf1[0] == 'q') || (buf1[0] == 'Q')) { | ||
| 758 | /* user hit q or Q */ | ||
| 759 | KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */ | ||
| 760 | KDB_STATE_CLEAR(PAGER); | ||
| 761 | /* end of command output; back to normal mode */ | ||
| 762 | kdb_grepping_flag = 0; | ||
| 763 | kdb_printf("\n"); | ||
| 764 | } else if (buf1[0] == ' ') { | ||
| 765 | kdb_printf("\n"); | ||
| 766 | suspend_grep = 1; /* for this recursion */ | ||
| 767 | } else if (buf1[0] == '\n') { | ||
| 768 | kdb_nextline = linecount - 1; | ||
| 769 | kdb_printf("\r"); | ||
| 770 | suspend_grep = 1; /* for this recursion */ | ||
| 771 | } else if (buf1[0] && buf1[0] != '\n') { | ||
| 772 | /* user hit something other than enter */ | ||
| 773 | suspend_grep = 1; /* for this recursion */ | ||
| 774 | kdb_printf("\nOnly 'q' or 'Q' are processed at more " | ||
| 775 | "prompt, input ignored\n"); | ||
| 776 | } else if (kdb_grepping_flag) { | ||
| 777 | /* user hit enter */ | ||
| 778 | suspend_grep = 1; /* for this recursion */ | ||
| 779 | kdb_printf("\n"); | ||
| 780 | } | ||
| 781 | kdb_input_flush(); | ||
| 782 | } | ||
| 783 | |||
| 784 | /* | ||
| 785 | * For grep searches, shift the printed string left. | ||
| 786 | * replaced_byte contains the character that was overwritten with | ||
| 787 | * the terminating null, and cphold points to the null. | ||
| 788 | * Then adjust the notion of available space in the buffer. | ||
| 789 | */ | ||
| 790 | if (kdb_grepping_flag && !suspend_grep) { | ||
| 791 | *cphold = replaced_byte; | ||
| 792 | strcpy(kdb_buffer, cphold); | ||
| 793 | len = strlen(kdb_buffer); | ||
| 794 | next_avail = kdb_buffer + len; | ||
| 795 | size_avail = sizeof(kdb_buffer) - len; | ||
| 796 | } | ||
| 797 | |||
| 798 | kdb_print_out: | ||
| 799 | suspend_grep = 0; /* end of what may have been a recursive call */ | ||
| 800 | if (logging) | ||
| 801 | console_loglevel = saved_loglevel; | ||
| 802 | if (KDB_STATE(PRINTF_LOCK) && got_printf_lock) { | ||
| 803 | got_printf_lock = 0; | ||
| 804 | spin_unlock_irqrestore(&kdb_printf_lock, flags); | ||
| 805 | KDB_STATE_CLEAR(PRINTF_LOCK); | ||
| 806 | atomic_dec(&kdb_event); | ||
| 807 | } else { | ||
| 808 | __release(kdb_printf_lock); | ||
| 809 | } | ||
| 810 | kdb_trap_printk = saved_trap_printk; | ||
| 811 | preempt_enable(); | ||
| 812 | return retlen; | ||
| 813 | } | ||
| 814 | |||
| 815 | int kdb_printf(const char *fmt, ...) | ||
| 816 | { | ||
| 817 | va_list ap; | ||
| 818 | int r; | ||
| 819 | |||
| 820 | va_start(ap, fmt); | ||
| 821 | r = vkdb_printf(fmt, ap); | ||
| 822 | va_end(ap); | ||
| 823 | |||
| 824 | return r; | ||
| 825 | } | ||
| 826 | |||
diff --git a/kernel/debug/kdb/kdb_keyboard.c b/kernel/debug/kdb/kdb_keyboard.c new file mode 100644 index 000000000000..4bca634975c0 --- /dev/null +++ b/kernel/debug/kdb/kdb_keyboard.c | |||
| @@ -0,0 +1,212 @@ | |||
| 1 | /* | ||
| 2 | * Kernel Debugger Architecture Dependent Console I/O handler | ||
| 3 | * | ||
| 4 | * This file is subject to the terms and conditions of the GNU General Public | ||
| 5 | * License. | ||
| 6 | * | ||
| 7 | * Copyright (c) 1999-2006 Silicon Graphics, Inc. All Rights Reserved. | ||
| 8 | * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <linux/kdb.h> | ||
| 12 | #include <linux/keyboard.h> | ||
| 13 | #include <linux/ctype.h> | ||
| 14 | #include <linux/module.h> | ||
| 15 | #include <linux/io.h> | ||
| 16 | |||
| 17 | /* Keyboard Controller Registers on normal PCs. */ | ||
| 18 | |||
| 19 | #define KBD_STATUS_REG 0x64 /* Status register (R) */ | ||
| 20 | #define KBD_DATA_REG 0x60 /* Keyboard data register (R/W) */ | ||
| 21 | |||
| 22 | /* Status Register Bits */ | ||
| 23 | |||
| 24 | #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */ | ||
| 25 | #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */ | ||
| 26 | |||
| 27 | static int kbd_exists; | ||
| 28 | |||
| 29 | /* | ||
| 30 | * Check if the keyboard controller has a keypress for us. | ||
| 31 | * Some parts (Enter Release, LED change) are still blocking polled here, | ||
| 32 | * but hopefully they are all short. | ||
| 33 | */ | ||
| 34 | int kdb_get_kbd_char(void) | ||
| 35 | { | ||
| 36 | int scancode, scanstatus; | ||
| 37 | static int shift_lock; /* CAPS LOCK state (0-off, 1-on) */ | ||
| 38 | static int shift_key; /* Shift next keypress */ | ||
| 39 | static int ctrl_key; | ||
| 40 | u_short keychar; | ||
| 41 | |||
| 42 | if (KDB_FLAG(NO_I8042) || KDB_FLAG(NO_VT_CONSOLE) || | ||
| 43 | (inb(KBD_STATUS_REG) == 0xff && inb(KBD_DATA_REG) == 0xff)) { | ||
| 44 | kbd_exists = 0; | ||
| 45 | return -1; | ||
| 46 | } | ||
| 47 | kbd_exists = 1; | ||
| 48 | |||
| 49 | if ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0) | ||
| 50 | return -1; | ||
| 51 | |||
| 52 | /* | ||
| 53 | * Fetch the scancode | ||
| 54 | */ | ||
| 55 | scancode = inb(KBD_DATA_REG); | ||
| 56 | scanstatus = inb(KBD_STATUS_REG); | ||
| 57 | |||
| 58 | /* | ||
| 59 | * Ignore mouse events. | ||
| 60 | */ | ||
| 61 | if (scanstatus & KBD_STAT_MOUSE_OBF) | ||
| 62 | return -1; | ||
| 63 | |||
| 64 | /* | ||
| 65 | * Ignore release, trigger on make | ||
| 66 | * (except for shift keys, where we want to | ||
| 67 | * keep the shift state so long as the key is | ||
| 68 | * held down). | ||
| 69 | */ | ||
| 70 | |||
| 71 | if (((scancode&0x7f) == 0x2a) || ((scancode&0x7f) == 0x36)) { | ||
| 72 | /* | ||
| 73 | * Next key may use shift table | ||
| 74 | */ | ||
| 75 | if ((scancode & 0x80) == 0) | ||
| 76 | shift_key = 1; | ||
| 77 | else | ||
| 78 | shift_key = 0; | ||
| 79 | return -1; | ||
| 80 | } | ||
| 81 | |||
| 82 | if ((scancode&0x7f) == 0x1d) { | ||
| 83 | /* | ||
| 84 | * Left ctrl key | ||
| 85 | */ | ||
| 86 | if ((scancode & 0x80) == 0) | ||
| 87 | ctrl_key = 1; | ||
| 88 | else | ||
| 89 | ctrl_key = 0; | ||
| 90 | return -1; | ||
| 91 | } | ||
| 92 | |||
| 93 | if ((scancode & 0x80) != 0) | ||
| 94 | return -1; | ||
| 95 | |||
| 96 | scancode &= 0x7f; | ||
| 97 | |||
| 98 | /* | ||
| 99 | * Translate scancode | ||
| 100 | */ | ||
| 101 | |||
| 102 | if (scancode == 0x3a) { | ||
| 103 | /* | ||
| 104 | * Toggle caps lock | ||
| 105 | */ | ||
| 106 | shift_lock ^= 1; | ||
| 107 | |||
| 108 | #ifdef KDB_BLINK_LED | ||
| 109 | kdb_toggleled(0x4); | ||
| 110 | #endif | ||
| 111 | return -1; | ||
| 112 | } | ||
| 113 | |||
| 114 | if (scancode == 0x0e) { | ||
| 115 | /* | ||
| 116 | * Backspace | ||
| 117 | */ | ||
| 118 | return 8; | ||
| 119 | } | ||
| 120 | |||
| 121 | /* Special Key */ | ||
| 122 | switch (scancode) { | ||
| 123 | case 0xF: /* Tab */ | ||
| 124 | return 9; | ||
| 125 | case 0x53: /* Del */ | ||
| 126 | return 4; | ||
| 127 | case 0x47: /* Home */ | ||
| 128 | return 1; | ||
| 129 | case 0x4F: /* End */ | ||
| 130 | return 5; | ||
| 131 | case 0x4B: /* Left */ | ||
| 132 | return 2; | ||
| 133 | case 0x48: /* Up */ | ||
| 134 | return 16; | ||
| 135 | case 0x50: /* Down */ | ||
| 136 | return 14; | ||
| 137 | case 0x4D: /* Right */ | ||
| 138 | return 6; | ||
| 139 | } | ||
| 140 | |||
| 141 | if (scancode == 0xe0) | ||
| 142 | return -1; | ||
| 143 | |||
| 144 | /* | ||
| 145 | * For Japanese 86/106 keyboards | ||
| 146 | * See comment in drivers/char/pc_keyb.c. | ||
| 147 | * - Masahiro Adegawa | ||
| 148 | */ | ||
| 149 | if (scancode == 0x73) | ||
| 150 | scancode = 0x59; | ||
| 151 | else if (scancode == 0x7d) | ||
| 152 | scancode = 0x7c; | ||
| 153 | |||
| 154 | if (!shift_lock && !shift_key && !ctrl_key) { | ||
| 155 | keychar = plain_map[scancode]; | ||
| 156 | } else if ((shift_lock || shift_key) && key_maps[1]) { | ||
| 157 | keychar = key_maps[1][scancode]; | ||
| 158 | } else if (ctrl_key && key_maps[4]) { | ||
| 159 | keychar = key_maps[4][scancode]; | ||
| 160 | } else { | ||
| 161 | keychar = 0x0020; | ||
| 162 | kdb_printf("Unknown state/scancode (%d)\n", scancode); | ||
| 163 | } | ||
| 164 | keychar &= 0x0fff; | ||
| 165 | if (keychar == '\t') | ||
| 166 | keychar = ' '; | ||
| 167 | switch (KTYP(keychar)) { | ||
| 168 | case KT_LETTER: | ||
| 169 | case KT_LATIN: | ||
| 170 | if (isprint(keychar)) | ||
| 171 | break; /* printable characters */ | ||
| 172 | /* drop through */ | ||
| 173 | case KT_SPEC: | ||
| 174 | if (keychar == K_ENTER) | ||
| 175 | break; | ||
| 176 | /* drop through */ | ||
| 177 | default: | ||
| 178 | return -1; /* ignore unprintables */ | ||
| 179 | } | ||
| 180 | |||
| 181 | if ((scancode & 0x7f) == 0x1c) { | ||
| 182 | /* | ||
| 183 | * enter key. All done. Absorb the release scancode. | ||
| 184 | */ | ||
| 185 | while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0) | ||
| 186 | ; | ||
| 187 | |||
| 188 | /* | ||
| 189 | * Fetch the scancode | ||
| 190 | */ | ||
| 191 | scancode = inb(KBD_DATA_REG); | ||
| 192 | scanstatus = inb(KBD_STATUS_REG); | ||
| 193 | |||
| 194 | while (scanstatus & KBD_STAT_MOUSE_OBF) { | ||
| 195 | scancode = inb(KBD_DATA_REG); | ||
| 196 | scanstatus = inb(KBD_STATUS_REG); | ||
| 197 | } | ||
| 198 | |||
| 199 | if (scancode != 0x9c) { | ||
| 200 | /* | ||
| 201 | * Wasn't an enter-release, why not? | ||
| 202 | */ | ||
| 203 | kdb_printf("kdb: expected enter got 0x%x status 0x%x\n", | ||
| 204 | scancode, scanstatus); | ||
| 205 | } | ||
| 206 | |||
| 207 | return 13; | ||
| 208 | } | ||
| 209 | |||
| 210 | return keychar & 0xff; | ||
| 211 | } | ||
| 212 | EXPORT_SYMBOL_GPL(kdb_get_kbd_char); | ||
diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c new file mode 100644 index 000000000000..ebe4a287419e --- /dev/null +++ b/kernel/debug/kdb/kdb_main.c | |||
| @@ -0,0 +1,2846 @@ | |||
| 1 | /* | ||
| 2 | * Kernel Debugger Architecture Independent Main Code | ||
| 3 | * | ||
| 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 | ||
| 6 | * for more details. | ||
| 7 | * | ||
| 8 | * Copyright (C) 1999-2004 Silicon Graphics, Inc. All Rights Reserved. | ||
| 9 | * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com> | ||
| 10 | * Xscale (R) modifications copyright (C) 2003 Intel Corporation. | ||
| 11 | * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include <linux/ctype.h> | ||
| 15 | #include <linux/string.h> | ||
| 16 | #include <linux/kernel.h> | ||
| 17 | #include <linux/reboot.h> | ||
| 18 | #include <linux/sched.h> | ||
| 19 | #include <linux/sysrq.h> | ||
| 20 | #include <linux/smp.h> | ||
| 21 | #include <linux/utsname.h> | ||
| 22 | #include <linux/vmalloc.h> | ||
| 23 | #include <linux/module.h> | ||
| 24 | #include <linux/mm.h> | ||
| 25 | #include <linux/init.h> | ||
| 26 | #include <linux/kallsyms.h> | ||
| 27 | #include <linux/kgdb.h> | ||
| 28 | #include <linux/kdb.h> | ||
| 29 | #include <linux/notifier.h> | ||
| 30 | #include <linux/interrupt.h> | ||
| 31 | #include <linux/delay.h> | ||
| 32 | #include <linux/nmi.h> | ||
| 33 | #include <linux/time.h> | ||
| 34 | #include <linux/ptrace.h> | ||
| 35 | #include <linux/sysctl.h> | ||
| 36 | #include <linux/cpu.h> | ||
| 37 | #include <linux/kdebug.h> | ||
| 38 | #include <linux/proc_fs.h> | ||
| 39 | #include <linux/uaccess.h> | ||
| 40 | #include <linux/slab.h> | ||
| 41 | #include "kdb_private.h" | ||
| 42 | |||
| 43 | #define GREP_LEN 256 | ||
| 44 | char kdb_grep_string[GREP_LEN]; | ||
| 45 | int kdb_grepping_flag; | ||
| 46 | EXPORT_SYMBOL(kdb_grepping_flag); | ||
| 47 | int kdb_grep_leading; | ||
| 48 | int kdb_grep_trailing; | ||
| 49 | |||
| 50 | /* | ||
| 51 | * Kernel debugger state flags | ||
| 52 | */ | ||
| 53 | int kdb_flags; | ||
| 54 | atomic_t kdb_event; | ||
| 55 | |||
| 56 | /* | ||
| 57 | * kdb_lock protects updates to kdb_initial_cpu. Used to | ||
| 58 | * single thread processors through the kernel debugger. | ||
| 59 | */ | ||
| 60 | int kdb_initial_cpu = -1; /* cpu number that owns kdb */ | ||
| 61 | int kdb_nextline = 1; | ||
| 62 | int kdb_state; /* General KDB state */ | ||
| 63 | |||
| 64 | struct task_struct *kdb_current_task; | ||
| 65 | EXPORT_SYMBOL(kdb_current_task); | ||
| 66 | struct pt_regs *kdb_current_regs; | ||
| 67 | |||
| 68 | const char *kdb_diemsg; | ||
| 69 | static int kdb_go_count; | ||
| 70 | #ifdef CONFIG_KDB_CONTINUE_CATASTROPHIC | ||
| 71 | static unsigned int kdb_continue_catastrophic = | ||
| 72 | CONFIG_KDB_CONTINUE_CATASTROPHIC; | ||
| 73 | #else | ||
| 74 | static unsigned int kdb_continue_catastrophic; | ||
| 75 | #endif | ||
| 76 | |||
| 77 | /* kdb_commands describes the available commands. */ | ||
| 78 | static kdbtab_t *kdb_commands; | ||
| 79 | #define KDB_BASE_CMD_MAX 50 | ||
| 80 | static int kdb_max_commands = KDB_BASE_CMD_MAX; | ||
| 81 | static kdbtab_t kdb_base_commands[50]; | ||
| 82 | #define for_each_kdbcmd(cmd, num) \ | ||
| 83 | for ((cmd) = kdb_base_commands, (num) = 0; \ | ||
| 84 | num < kdb_max_commands; \ | ||
| 85 | num == KDB_BASE_CMD_MAX ? cmd = kdb_commands : cmd++, num++) | ||
| 86 | |||
| 87 | typedef struct _kdbmsg { | ||
| 88 | int km_diag; /* kdb diagnostic */ | ||
| 89 | char *km_msg; /* Corresponding message text */ | ||
| 90 | } kdbmsg_t; | ||
| 91 | |||
| 92 | #define KDBMSG(msgnum, text) \ | ||
| 93 | { KDB_##msgnum, text } | ||
| 94 | |||
| 95 | static kdbmsg_t kdbmsgs[] = { | ||
| 96 | KDBMSG(NOTFOUND, "Command Not Found"), | ||
| 97 | KDBMSG(ARGCOUNT, "Improper argument count, see usage."), | ||
| 98 | KDBMSG(BADWIDTH, "Illegal value for BYTESPERWORD use 1, 2, 4 or 8, " | ||
| 99 | "8 is only allowed on 64 bit systems"), | ||
| 100 | KDBMSG(BADRADIX, "Illegal value for RADIX use 8, 10 or 16"), | ||
| 101 | KDBMSG(NOTENV, "Cannot find environment variable"), | ||
| 102 | KDBMSG(NOENVVALUE, "Environment variable should have value"), | ||
| 103 | KDBMSG(NOTIMP, "Command not implemented"), | ||
| 104 | KDBMSG(ENVFULL, "Environment full"), | ||
| 105 | KDBMSG(ENVBUFFULL, "Environment buffer full"), | ||
| 106 | KDBMSG(TOOMANYBPT, "Too many breakpoints defined"), | ||
| 107 | #ifdef CONFIG_CPU_XSCALE | ||
| 108 | KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"), | ||
| 109 | #else | ||
| 110 | KDBMSG(TOOMANYDBREGS, "More breakpoints than db registers defined"), | ||
| 111 | #endif | ||
| 112 | KDBMSG(DUPBPT, "Duplicate breakpoint address"), | ||
| 113 | KDBMSG(BPTNOTFOUND, "Breakpoint not found"), | ||
| 114 | KDBMSG(BADMODE, "Invalid IDMODE"), | ||
| 115 | KDBMSG(BADINT, "Illegal numeric value"), | ||
| 116 | KDBMSG(INVADDRFMT, "Invalid symbolic address format"), | ||
| 117 | KDBMSG(BADREG, "Invalid register name"), | ||
| 118 | KDBMSG(BADCPUNUM, "Invalid cpu number"), | ||
| 119 | KDBMSG(BADLENGTH, "Invalid length field"), | ||
| 120 | KDBMSG(NOBP, "No Breakpoint exists"), | ||
| 121 | KDBMSG(BADADDR, "Invalid address"), | ||
| 122 | }; | ||
| 123 | #undef KDBMSG | ||
| 124 | |||
| 125 | static const int __nkdb_err = sizeof(kdbmsgs) / sizeof(kdbmsg_t); | ||
| 126 | |||
| 127 | |||
| 128 | /* | ||
| 129 | * Initial environment. This is all kept static and local to | ||
| 130 | * this file. We don't want to rely on the memory allocation | ||
| 131 | * mechanisms in the kernel, so we use a very limited allocate-only | ||
| 132 | * heap for new and altered environment variables. The entire | ||
| 133 | * environment is limited to a fixed number of entries (add more | ||
| 134 | * to __env[] if required) and a fixed amount of heap (add more to | ||
| 135 | * KDB_ENVBUFSIZE if required). | ||
| 136 | */ | ||
| 137 | |||
| 138 | static char *__env[] = { | ||
| 139 | #if defined(CONFIG_SMP) | ||
| 140 | "PROMPT=[%d]kdb> ", | ||
| 141 | "MOREPROMPT=[%d]more> ", | ||
| 142 | #else | ||
| 143 | "PROMPT=kdb> ", | ||
| 144 | "MOREPROMPT=more> ", | ||
| 145 | #endif | ||
| 146 | "RADIX=16", | ||
| 147 | "MDCOUNT=8", /* lines of md output */ | ||
| 148 | "BTARGS=9", /* 9 possible args in bt */ | ||
| 149 | KDB_PLATFORM_ENV, | ||
| 150 | "DTABCOUNT=30", | ||
| 151 | "NOSECT=1", | ||
| 152 | (char *)0, | ||
| 153 | (char *)0, | ||
| 154 | (char *)0, | ||
| 155 | (char *)0, | ||
| 156 | (char *)0, | ||
| 157 | (char *)0, | ||
| 158 | (char *)0, | ||
| 159 | (char *)0, | ||
| 160 | (char *)0, | ||
| 161 | (char *)0, | ||
| 162 | (char *)0, | ||
| 163 | (char *)0, | ||
| 164 | (char *)0, | ||
| 165 | (char *)0, | ||
| 166 | (char *)0, | ||
| 167 | (char *)0, | ||
| 168 | (char *)0, | ||
| 169 | (char *)0, | ||
| 170 | (char *)0, | ||
| 171 | (char *)0, | ||
| 172 | (char *)0, | ||
| 173 | (char *)0, | ||
| 174 | (char *)0, | ||
| 175 | }; | ||
| 176 | |||
| 177 | static const int __nenv = (sizeof(__env) / sizeof(char *)); | ||
| 178 | |||
| 179 | struct task_struct *kdb_curr_task(int cpu) | ||
| 180 | { | ||
| 181 | struct task_struct *p = curr_task(cpu); | ||
| 182 | #ifdef _TIF_MCA_INIT | ||
| 183 | if ((task_thread_info(p)->flags & _TIF_MCA_INIT) && KDB_TSK(cpu)) | ||
| 184 | p = krp->p; | ||
| 185 | #endif | ||
| 186 | return p; | ||
| 187 | } | ||
| 188 | |||
| 189 | /* | ||
| 190 | * kdbgetenv - This function will return the character string value of | ||
| 191 | * an environment variable. | ||
| 192 | * Parameters: | ||
| 193 | * match A character string representing an environment variable. | ||
| 194 | * Returns: | ||
| 195 | * NULL No environment variable matches 'match' | ||
| 196 | * char* Pointer to string value of environment variable. | ||
| 197 | */ | ||
| 198 | char *kdbgetenv(const char *match) | ||
| 199 | { | ||
| 200 | char **ep = __env; | ||
| 201 | int matchlen = strlen(match); | ||
| 202 | int i; | ||
| 203 | |||
| 204 | for (i = 0; i < __nenv; i++) { | ||
| 205 | char *e = *ep++; | ||
| 206 | |||
| 207 | if (!e) | ||
| 208 | continue; | ||
| 209 | |||
| 210 | if ((strncmp(match, e, matchlen) == 0) | ||
| 211 | && ((e[matchlen] == '\0') | ||
| 212 | || (e[matchlen] == '='))) { | ||
| 213 | char *cp = strchr(e, '='); | ||
| 214 | return cp ? ++cp : ""; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | return NULL; | ||
| 218 | } | ||
| 219 | |||
| 220 | /* | ||
| 221 | * kdballocenv - This function is used to allocate bytes for | ||
| 222 | * environment entries. | ||
| 223 | * Parameters: | ||
| 224 | * match A character string representing a numeric value | ||
| 225 | * Outputs: | ||
| 226 | * *value the unsigned long representation of the env variable 'match' | ||
| 227 | * Returns: | ||
| 228 | * Zero on success, a kdb diagnostic on failure. | ||
| 229 | * Remarks: | ||
| 230 | * We use a static environment buffer (envbuffer) to hold the values | ||
| 231 | * of dynamically generated environment variables (see kdb_set). Buffer | ||
| 232 | * space once allocated is never free'd, so over time, the amount of space | ||
| 233 | * (currently 512 bytes) will be exhausted if env variables are changed | ||
| 234 | * frequently. | ||
| 235 | */ | ||
| 236 | static char *kdballocenv(size_t bytes) | ||
| 237 | { | ||
| 238 | #define KDB_ENVBUFSIZE 512 | ||
| 239 | static char envbuffer[KDB_ENVBUFSIZE]; | ||
| 240 | static int envbufsize; | ||
| 241 | char *ep = NULL; | ||
| 242 | |||
| 243 | if ((KDB_ENVBUFSIZE - envbufsize) >= bytes) { | ||
| 244 | ep = &envbuffer[envbufsize]; | ||
| 245 | envbufsize += bytes; | ||
| 246 | } | ||
| 247 | return ep; | ||
| 248 | } | ||
| 249 | |||
| 250 | /* | ||
| 251 | * kdbgetulenv - This function will return the value of an unsigned | ||
| 252 | * long-valued environment variable. | ||
| 253 | * Parameters: | ||
| 254 | * match A character string representing a numeric value | ||
| 255 | * Outputs: | ||
| 256 | * *value the unsigned long represntation of the env variable 'match' | ||
| 257 | * Returns: | ||
| 258 | * Zero on success, a kdb diagnostic on failure. | ||
| 259 | */ | ||
| 260 | static int kdbgetulenv(const char *match, unsigned long *value) | ||
| 261 | { | ||
| 262 | char *ep; | ||
| 263 | |||
| 264 | ep = kdbgetenv(match); | ||
| 265 | if (!ep) | ||
| 266 | return KDB_NOTENV; | ||
| 267 | if (strlen(ep) == 0) | ||
| 268 | return KDB_NOENVVALUE; | ||
| 269 | |||
| 270 | *value = simple_strtoul(ep, NULL, 0); | ||
| 271 | |||
| 272 | return 0; | ||
| 273 | } | ||
| 274 | |||
| 275 | /* | ||
| 276 | * kdbgetintenv - This function will return the value of an | ||
| 277 | * integer-valued environment variable. | ||
| 278 | * Parameters: | ||
| 279 | * match A character string representing an integer-valued env variable | ||
| 280 | * Outputs: | ||
| 281 | * *value the integer representation of the environment variable 'match' | ||
| 282 | * Returns: | ||
| 283 | * Zero on success, a kdb diagnostic on failure. | ||
| 284 | */ | ||
| 285 | int kdbgetintenv(const char *match, int *value) | ||
| 286 | { | ||
| 287 | unsigned long val; | ||
| 288 | int diag; | ||
| 289 | |||
| 290 | diag = kdbgetulenv(match, &val); | ||
| 291 | if (!diag) | ||
| 292 | *value = (int) val; | ||
| 293 | return diag; | ||
| 294 | } | ||
| 295 | |||
| 296 | /* | ||
| 297 | * kdbgetularg - This function will convert a numeric string into an | ||
| 298 | * unsigned long value. | ||
| 299 | * Parameters: | ||
| 300 | * arg A character string representing a numeric value | ||
| 301 | * Outputs: | ||
| 302 | * *value the unsigned long represntation of arg. | ||
| 303 | * Returns: | ||
| 304 | * Zero on success, a kdb diagnostic on failure. | ||
| 305 | */ | ||
| 306 | int kdbgetularg(const char *arg, unsigned long *value) | ||
| 307 | { | ||
| 308 | char *endp; | ||
| 309 | unsigned long val; | ||
| 310 | |||
| 311 | val = simple_strtoul(arg, &endp, 0); | ||
| 312 | |||
| 313 | if (endp == arg) { | ||
| 314 | /* | ||
| 315 | * Try base 16, for us folks too lazy to type the | ||
| 316 | * leading 0x... | ||
| 317 | */ | ||
| 318 | val = simple_strtoul(arg, &endp, 16); | ||
| 319 | if (endp == arg) | ||
| 320 | return KDB_BADINT; | ||
| 321 | } | ||
| 322 | |||
| 323 | *value = val; | ||
| 324 | |||
| 325 | return 0; | ||
| 326 | } | ||
| 327 | |||
| 328 | /* | ||
| 329 | * kdb_set - This function implements the 'set' command. Alter an | ||
| 330 | * existing environment variable or create a new one. | ||
| 331 | */ | ||
| 332 | int kdb_set(int argc, const char **argv) | ||
| 333 | { | ||
| 334 | int i; | ||
| 335 | char *ep; | ||
| 336 | size_t varlen, vallen; | ||
| 337 | |||
| 338 | /* | ||
| 339 | * we can be invoked two ways: | ||
| 340 | * set var=value argv[1]="var", argv[2]="value" | ||
| 341 | * set var = value argv[1]="var", argv[2]="=", argv[3]="value" | ||
| 342 | * - if the latter, shift 'em down. | ||
| 343 | */ | ||
| 344 | if (argc == 3) { | ||
| 345 | argv[2] = argv[3]; | ||
| 346 | argc--; | ||
| 347 | } | ||
| 348 | |||
| 349 | if (argc != 2) | ||
| 350 | return KDB_ARGCOUNT; | ||
| 351 | |||
| 352 | /* | ||
| 353 | * Check for internal variables | ||
| 354 | */ | ||
| 355 | if (strcmp(argv[1], "KDBDEBUG") == 0) { | ||
| 356 | unsigned int debugflags; | ||
| 357 | char *cp; | ||
| 358 | |||
| 359 | debugflags = simple_strtoul(argv[2], &cp, 0); | ||
| 360 | if (cp == argv[2] || debugflags & ~KDB_DEBUG_FLAG_MASK) { | ||
| 361 | kdb_printf("kdb: illegal debug flags '%s'\n", | ||
| 362 | argv[2]); | ||
| 363 | return 0; | ||
| 364 | } | ||
| 365 | kdb_flags = (kdb_flags & | ||
| 366 | ~(KDB_DEBUG_FLAG_MASK << KDB_DEBUG_FLAG_SHIFT)) | ||
| 367 | | (debugflags << KDB_DEBUG_FLAG_SHIFT); | ||
| 368 | |||
| 369 | return 0; | ||
| 370 | } | ||
| 371 | |||
| 372 | /* | ||
| 373 | * Tokenizer squashed the '=' sign. argv[1] is variable | ||
| 374 | * name, argv[2] = value. | ||
| 375 | */ | ||
| 376 | varlen = strlen(argv[1]); | ||
| 377 | vallen = strlen(argv[2]); | ||
| 378 | ep = kdballocenv(varlen + vallen + 2); | ||
| 379 | if (ep == (char *)0) | ||
| 380 | return KDB_ENVBUFFULL; | ||
| 381 | |||
| 382 | sprintf(ep, "%s=%s", argv[1], argv[2]); | ||
| 383 | |||
| 384 | ep[varlen+vallen+1] = '\0'; | ||
| 385 | |||
| 386 | for (i = 0; i < __nenv; i++) { | ||
| 387 | if (__env[i] | ||
| 388 | && ((strncmp(__env[i], argv[1], varlen) == 0) | ||
| 389 | && ((__env[i][varlen] == '\0') | ||
| 390 | || (__env[i][varlen] == '=')))) { | ||
| 391 | __env[i] = ep; | ||
| 392 | return 0; | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | /* | ||
| 397 | * Wasn't existing variable. Fit into slot. | ||
| 398 | */ | ||
| 399 | for (i = 0; i < __nenv-1; i++) { | ||
| 400 | if (__env[i] == (char *)0) { | ||
| 401 | __env[i] = ep; | ||
| 402 | return 0; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | return KDB_ENVFULL; | ||
| 407 | } | ||
| 408 | |||
| 409 | static int kdb_check_regs(void) | ||
| 410 | { | ||
| 411 | if (!kdb_current_regs) { | ||
| 412 | kdb_printf("No current kdb registers." | ||
| 413 | " You may need to select another task\n"); | ||
| 414 | return KDB_BADREG; | ||
| 415 | } | ||
| 416 | return 0; | ||
| 417 | } | ||
| 418 | |||
| 419 | /* | ||
| 420 | * kdbgetaddrarg - This function is responsible for parsing an | ||
| 421 | * address-expression and returning the value of the expression, | ||
| 422 | * symbol name, and offset to the caller. | ||
| 423 | * | ||
| 424 | * The argument may consist of a numeric value (decimal or | ||
| 425 | * hexidecimal), a symbol name, a register name (preceeded by the | ||
| 426 | * percent sign), an environment variable with a numeric value | ||
| 427 | * (preceeded by a dollar sign) or a simple arithmetic expression | ||
| 428 | * consisting of a symbol name, +/-, and a numeric constant value | ||
| 429 | * (offset). | ||
| 430 | * Parameters: | ||
| 431 | * argc - count of arguments in argv | ||
| 432 | * argv - argument vector | ||
| 433 | * *nextarg - index to next unparsed argument in argv[] | ||
| 434 | * regs - Register state at time of KDB entry | ||
| 435 | * Outputs: | ||
| 436 | * *value - receives the value of the address-expression | ||
| 437 | * *offset - receives the offset specified, if any | ||
| 438 | * *name - receives the symbol name, if any | ||
| 439 | * *nextarg - index to next unparsed argument in argv[] | ||
| 440 | * Returns: | ||
| 441 | * zero is returned on success, a kdb diagnostic code is | ||
| 442 | * returned on error. | ||
| 443 | */ | ||
| 444 | int kdbgetaddrarg(int argc, const char **argv, int *nextarg, | ||
| 445 | unsigned long *value, long *offset, | ||
| 446 | char **name) | ||
| 447 | { | ||
| 448 | unsigned long addr; | ||
| 449 | unsigned long off = 0; | ||
| 450 | int positive; | ||
| 451 | int diag; | ||
| 452 | int found = 0; | ||
| 453 | char *symname; | ||
| 454 | char symbol = '\0'; | ||
| 455 | char *cp; | ||
| 456 | kdb_symtab_t symtab; | ||
| 457 | |||
| 458 | /* | ||
| 459 | * Process arguments which follow the following syntax: | ||
| 460 | * | ||
| 461 | * symbol | numeric-address [+/- numeric-offset] | ||
| 462 | * %register | ||
| 463 | * $environment-variable | ||
| 464 | */ | ||
| 465 | |||
| 466 | if (*nextarg > argc) | ||
| 467 | return KDB_ARGCOUNT; | ||
| 468 | |||
| 469 | symname = (char *)argv[*nextarg]; | ||
| 470 | |||
| 471 | /* | ||
| 472 | * If there is no whitespace between the symbol | ||
| 473 | * or address and the '+' or '-' symbols, we | ||
| 474 | * remember the character and replace it with a | ||
| 475 | * null so the symbol/value can be properly parsed | ||
| 476 | */ | ||
| 477 | cp = strpbrk(symname, "+-"); | ||
| 478 | if (cp != NULL) { | ||
| 479 | symbol = *cp; | ||
| 480 | *cp++ = '\0'; | ||
| 481 | } | ||
| 482 | |||
| 483 | if (symname[0] == '$') { | ||
| 484 | diag = kdbgetulenv(&symname[1], &addr); | ||
| 485 | if (diag) | ||
| 486 | return diag; | ||
| 487 | } else if (symname[0] == '%') { | ||
| 488 | diag = kdb_check_regs(); | ||
| 489 | if (diag) | ||
| 490 | return diag; | ||
| 491 | /* Implement register values with % at a later time as it is | ||
| 492 | * arch optional. | ||
| 493 | */ | ||
| 494 | return KDB_NOTIMP; | ||
| 495 | } else { | ||
| 496 | found = kdbgetsymval(symname, &symtab); | ||
| 497 | if (found) { | ||
| 498 | addr = symtab.sym_start; | ||
| 499 | } else { | ||
| 500 | diag = kdbgetularg(argv[*nextarg], &addr); | ||
| 501 | if (diag) | ||
| 502 | return diag; | ||
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | if (!found) | ||
| 507 | found = kdbnearsym(addr, &symtab); | ||
| 508 | |||
| 509 | (*nextarg)++; | ||
| 510 | |||
| 511 | if (name) | ||
| 512 | *name = symname; | ||
| 513 | if (value) | ||
| 514 | *value = addr; | ||
| 515 | if (offset && name && *name) | ||
| 516 | *offset = addr - symtab.sym_start; | ||
| 517 | |||
| 518 | if ((*nextarg > argc) | ||
| 519 | && (symbol == '\0')) | ||
| 520 | return 0; | ||
| 521 | |||
| 522 | /* | ||
| 523 | * check for +/- and offset | ||
| 524 | */ | ||
| 525 | |||
| 526 | if (symbol == '\0') { | ||
| 527 | if ((argv[*nextarg][0] != '+') | ||
| 528 | && (argv[*nextarg][0] != '-')) { | ||
| 529 | /* | ||
| 530 | * Not our argument. Return. | ||
| 531 | */ | ||
| 532 | return 0; | ||
| 533 | } else { | ||
| 534 | positive = (argv[*nextarg][0] == '+'); | ||
| 535 | (*nextarg)++; | ||
| 536 | } | ||
| 537 | } else | ||
| 538 | positive = (symbol == '+'); | ||
| 539 | |||
| 540 | /* | ||
| 541 | * Now there must be an offset! | ||
| 542 | */ | ||
| 543 | if ((*nextarg > argc) | ||
| 544 | && (symbol == '\0')) { | ||
| 545 | return KDB_INVADDRFMT; | ||
| 546 | } | ||
| 547 | |||
| 548 | if (!symbol) { | ||
| 549 | cp = (char *)argv[*nextarg]; | ||
| 550 | (*nextarg)++; | ||
| 551 | } | ||
| 552 | |||
| 553 | diag = kdbgetularg(cp, &off); | ||
| 554 | if (diag) | ||
| 555 | return diag; | ||
| 556 | |||
| 557 | if (!positive) | ||
| 558 | off = -off; | ||
| 559 | |||
| 560 | if (offset) | ||
| 561 | *offset += off; | ||
| 562 | |||
| 563 | if (value) | ||
| 564 | *value += off; | ||
| 565 | |||
| 566 | return 0; | ||
| 567 | } | ||
| 568 | |||
| 569 | static void kdb_cmderror(int diag) | ||
| 570 | { | ||
| 571 | int i; | ||
| 572 | |||
| 573 | if (diag >= 0) { | ||
| 574 | kdb_printf("no error detected (diagnostic is %d)\n", diag); | ||
| 575 | return; | ||
| 576 | } | ||
| 577 | |||
| 578 | for (i = 0; i < __nkdb_err; i++) { | ||
| 579 | if (kdbmsgs[i].km_diag == diag) { | ||
| 580 | kdb_printf("diag: %d: %s\n", diag, kdbmsgs[i].km_msg); | ||
| 581 | return; | ||
| 582 | } | ||
| 583 | } | ||
| 584 | |||
| 585 | kdb_printf("Unknown diag %d\n", -diag); | ||
| 586 | } | ||
| 587 | |||
| 588 | /* | ||
| 589 | * kdb_defcmd, kdb_defcmd2 - This function implements the 'defcmd' | ||
| 590 | * command which defines one command as a set of other commands, | ||
| 591 | * terminated by endefcmd. kdb_defcmd processes the initial | ||
| 592 | * 'defcmd' command, kdb_defcmd2 is invoked from kdb_parse for | ||
| 593 | * the following commands until 'endefcmd'. | ||
| 594 | * Inputs: | ||
| 595 | * argc argument count | ||
| 596 | * argv argument vector | ||
| 597 | * Returns: | ||
| 598 | * zero for success, a kdb diagnostic if error | ||
| 599 | */ | ||
| 600 | struct defcmd_set { | ||
| 601 | int count; | ||
| 602 | int usable; | ||
| 603 | char *name; | ||
| 604 | char *usage; | ||
| 605 | char *help; | ||
| 606 | char **command; | ||
| 607 | }; | ||
| 608 | static struct defcmd_set *defcmd_set; | ||
| 609 | static int defcmd_set_count; | ||
| 610 | static int defcmd_in_progress; | ||
| 611 | |||
| 612 | /* Forward references */ | ||
| 613 | static int kdb_exec_defcmd(int argc, const char **argv); | ||
| 614 | |||
| 615 | static int kdb_defcmd2(const char *cmdstr, const char *argv0) | ||
| 616 | { | ||
| 617 | struct defcmd_set *s = defcmd_set + defcmd_set_count - 1; | ||
| 618 | char **save_command = s->command; | ||
| 619 | if (strcmp(argv0, "endefcmd") == 0) { | ||
| 620 | defcmd_in_progress = 0; | ||
| 621 | if (!s->count) | ||
| 622 | s->usable = 0; | ||
| 623 | if (s->usable) | ||
| 624 | kdb_register(s->name, kdb_exec_defcmd, | ||
| 625 | s->usage, s->help, 0); | ||
| 626 | return 0; | ||
| 627 | } | ||
| 628 | if (!s->usable) | ||
| 629 | return KDB_NOTIMP; | ||
| 630 | s->command = kmalloc((s->count + 1) * sizeof(*(s->command)), GFP_KDB); | ||
| 631 | if (!s->command) { | ||
| 632 | kdb_printf("Could not allocate new kdb_defcmd table for %s\n", | ||
| 633 | cmdstr); | ||
| 634 | s->usable = 0; | ||
| 635 | return KDB_NOTIMP; | ||
| 636 | } | ||
| 637 | memcpy(s->command, save_command, s->count * sizeof(*(s->command))); | ||
| 638 | s->command[s->count++] = kdb_strdup(cmdstr, GFP_KDB); | ||
| 639 | kfree(save_command); | ||
| 640 | return 0; | ||
| 641 | } | ||
| 642 | |||
| 643 | static int kdb_defcmd(int argc, const char **argv) | ||
| 644 | { | ||
| 645 | struct defcmd_set *save_defcmd_set = defcmd_set, *s; | ||
| 646 | if (defcmd_in_progress) { | ||
| 647 | kdb_printf("kdb: nested defcmd detected, assuming missing " | ||
| 648 | "endefcmd\n"); | ||
| 649 | kdb_defcmd2("endefcmd", "endefcmd"); | ||
| 650 | } | ||
| 651 | if (argc == 0) { | ||
| 652 | int i; | ||
| 653 | for (s = defcmd_set; s < defcmd_set + defcmd_set_count; ++s) { | ||
| 654 | kdb_printf("defcmd %s \"%s\" \"%s\"\n", s->name, | ||
| 655 | s->usage, s->help); | ||
| 656 | for (i = 0; i < s->count; ++i) | ||
| 657 | kdb_printf("%s", s->command[i]); | ||
| 658 | kdb_printf("endefcmd\n"); | ||
| 659 | } | ||
| 660 | return 0; | ||
| 661 | } | ||
| 662 | if (argc != 3) | ||
| 663 | return KDB_ARGCOUNT; | ||
| 664 | defcmd_set = kmalloc((defcmd_set_count + 1) * sizeof(*defcmd_set), | ||
| 665 | GFP_KDB); | ||
| 666 | if (!defcmd_set) { | ||
| 667 | kdb_printf("Could not allocate new defcmd_set entry for %s\n", | ||
| 668 | argv[1]); | ||
| 669 | defcmd_set = save_defcmd_set; | ||
| 670 | return KDB_NOTIMP; | ||
| 671 | } | ||
| 672 | memcpy(defcmd_set, save_defcmd_set, | ||
| 673 | defcmd_set_count * sizeof(*defcmd_set)); | ||
| 674 | kfree(save_defcmd_set); | ||
| 675 | s = defcmd_set + defcmd_set_count; | ||
| 676 | memset(s, 0, sizeof(*s)); | ||
| 677 | s->usable = 1; | ||
| 678 | s->name = kdb_strdup(argv[1], GFP_KDB); | ||
| 679 | s->usage = kdb_strdup(argv[2], GFP_KDB); | ||
| 680 | s->help = kdb_strdup(argv[3], GFP_KDB); | ||
| 681 | if (s->usage[0] == '"') { | ||
| 682 | strcpy(s->usage, s->usage+1); | ||
| 683 | s->usage[strlen(s->usage)-1] = '\0'; | ||
| 684 | } | ||
| 685 | if (s->help[0] == '"') { | ||
| 686 | strcpy(s->help, s->help+1); | ||
| 687 | s->help[strlen(s->help)-1] = '\0'; | ||
| 688 | } | ||
| 689 | ++defcmd_set_count; | ||
| 690 | defcmd_in_progress = 1; | ||
| 691 | return 0; | ||
| 692 | } | ||
| 693 | |||
| 694 | /* | ||
| 695 | * kdb_exec_defcmd - Execute the set of commands associated with this | ||
| 696 | * defcmd name. | ||
| 697 | * Inputs: | ||
| 698 | * argc argument count | ||
| 699 | * argv argument vector | ||
| 700 | * Returns: | ||
| 701 | * zero for success, a kdb diagnostic if error | ||
| 702 | */ | ||
| 703 | static int kdb_exec_defcmd(int argc, const char **argv) | ||
| 704 | { | ||
| 705 | int i, ret; | ||
| 706 | struct defcmd_set *s; | ||
| 707 | if (argc != 0) | ||
| 708 | return KDB_ARGCOUNT; | ||
| 709 | for (s = defcmd_set, i = 0; i < defcmd_set_count; ++i, ++s) { | ||
| 710 | if (strcmp(s->name, argv[0]) == 0) | ||
| 711 | break; | ||
| 712 | } | ||
| 713 | if (i == defcmd_set_count) { | ||
| 714 | kdb_printf("kdb_exec_defcmd: could not find commands for %s\n", | ||
| 715 | argv[0]); | ||
| 716 | return KDB_NOTIMP; | ||
| 717 | } | ||
| 718 | for (i = 0; i < s->count; ++i) { | ||
| 719 | /* Recursive use of kdb_parse, do not use argv after | ||
| 720 | * this point */ | ||
| 721 | argv = NULL; | ||
| 722 | kdb_printf("[%s]kdb> %s\n", s->name, s->command[i]); | ||
| 723 | ret = kdb_parse(s->command[i]); | ||
| 724 | if (ret) | ||
| 725 | return ret; | ||
| 726 | } | ||
| 727 | return 0; | ||
| 728 | } | ||
| 729 | |||
| 730 | /* Command history */ | ||
| 731 | #define KDB_CMD_HISTORY_COUNT 32 | ||
| 732 | #define CMD_BUFLEN 200 /* kdb_printf: max printline | ||
| 733 | * size == 256 */ | ||
| 734 | static unsigned int cmd_head, cmd_tail; | ||
| 735 | static unsigned int cmdptr; | ||
| 736 | static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN]; | ||
| 737 | static char cmd_cur[CMD_BUFLEN]; | ||
| 738 | |||
| 739 | /* | ||
| 740 | * The "str" argument may point to something like | grep xyz | ||
| 741 | */ | ||
| 742 | static void parse_grep(const char *str) | ||
| 743 | { | ||
| 744 | int len; | ||
| 745 | char *cp = (char *)str, *cp2; | ||
| 746 | |||
| 747 | /* sanity check: we should have been called with the \ first */ | ||
| 748 | if (*cp != '|') | ||
| 749 | return; | ||
| 750 | cp++; | ||
| 751 | while (isspace(*cp)) | ||
| 752 | cp++; | ||
| 753 | if (strncmp(cp, "grep ", 5)) { | ||
| 754 | kdb_printf("invalid 'pipe', see grephelp\n"); | ||
| 755 | return; | ||
| 756 | } | ||
| 757 | cp += 5; | ||
| 758 | while (isspace(*cp)) | ||
| 759 | cp++; | ||
| 760 | cp2 = strchr(cp, '\n'); | ||
| 761 | if (cp2) | ||
| 762 | *cp2 = '\0'; /* remove the trailing newline */ | ||
| 763 | len = strlen(cp); | ||
| 764 | if (len == 0) { | ||
| 765 | kdb_printf("invalid 'pipe', see grephelp\n"); | ||
| 766 | return; | ||
| 767 | } | ||
| 768 | /* now cp points to a nonzero length search string */ | ||
| 769 | if (*cp == '"') { | ||
| 770 | /* allow it be "x y z" by removing the "'s - there must | ||
| 771 | be two of them */ | ||
| 772 | cp++; | ||
| 773 | cp2 = strchr(cp, '"'); | ||
| 774 | if (!cp2) { | ||
| 775 | kdb_printf("invalid quoted string, see grephelp\n"); | ||
| 776 | return; | ||
| 777 | } | ||
| 778 | *cp2 = '\0'; /* end the string where the 2nd " was */ | ||
| 779 | } | ||
| 780 | kdb_grep_leading = 0; | ||
| 781 | if (*cp == '^') { | ||
| 782 | kdb_grep_leading = 1; | ||
| 783 | cp++; | ||
| 784 | } | ||
| 785 | len = strlen(cp); | ||
| 786 | kdb_grep_trailing = 0; | ||
| 787 | if (*(cp+len-1) == '$') { | ||
| 788 | kdb_grep_trailing = 1; | ||
| 789 | *(cp+len-1) = '\0'; | ||
| 790 | } | ||
| 791 | len = strlen(cp); | ||
| 792 | if (!len) | ||
| 793 | return; | ||
| 794 | if (len >= GREP_LEN) { | ||
| 795 | kdb_printf("search string too long\n"); | ||
| 796 | return; | ||
| 797 | } | ||
| 798 | strcpy(kdb_grep_string, cp); | ||
| 799 | kdb_grepping_flag++; | ||
| 800 | return; | ||
| 801 | } | ||
| 802 | |||
| 803 | /* | ||
| 804 | * kdb_parse - Parse the command line, search the command table for a | ||
| 805 | * matching command and invoke the command function. This | ||
| 806 | * function may be called recursively, if it is, the second call | ||
| 807 | * will overwrite argv and cbuf. It is the caller's | ||
| 808 | * responsibility to save their argv if they recursively call | ||
| 809 | * kdb_parse(). | ||
| 810 | * Parameters: | ||
| 811 | * cmdstr The input command line to be parsed. | ||
| 812 | * regs The registers at the time kdb was entered. | ||
| 813 | * Returns: | ||
| 814 | * Zero for success, a kdb diagnostic if failure. | ||
| 815 | * Remarks: | ||
| 816 | * Limited to 20 tokens. | ||
| 817 | * | ||
| 818 | * Real rudimentary tokenization. Basically only whitespace | ||
| 819 | * is considered a token delimeter (but special consideration | ||
| 820 | * is taken of the '=' sign as used by the 'set' command). | ||
| 821 | * | ||
| 822 | * The algorithm used to tokenize the input string relies on | ||
| 823 | * there being at least one whitespace (or otherwise useless) | ||
| 824 | * character between tokens as the character immediately following | ||
| 825 | * the token is altered in-place to a null-byte to terminate the | ||
| 826 | * token string. | ||
| 827 | */ | ||
| 828 | |||
| 829 | #define MAXARGC 20 | ||
| 830 | |||
| 831 | int kdb_parse(const char *cmdstr) | ||
| 832 | { | ||
| 833 | static char *argv[MAXARGC]; | ||
| 834 | static int argc; | ||
| 835 | static char cbuf[CMD_BUFLEN+2]; | ||
| 836 | char *cp; | ||
| 837 | char *cpp, quoted; | ||
| 838 | kdbtab_t *tp; | ||
| 839 | int i, escaped, ignore_errors = 0, check_grep; | ||
| 840 | |||
| 841 | /* | ||
| 842 | * First tokenize the command string. | ||
| 843 | */ | ||
| 844 | cp = (char *)cmdstr; | ||
| 845 | kdb_grepping_flag = check_grep = 0; | ||
| 846 | |||
| 847 | if (KDB_FLAG(CMD_INTERRUPT)) { | ||
| 848 | /* Previous command was interrupted, newline must not | ||
| 849 | * repeat the command */ | ||
| 850 | KDB_FLAG_CLEAR(CMD_INTERRUPT); | ||
| 851 | KDB_STATE_SET(PAGER); | ||
| 852 | argc = 0; /* no repeat */ | ||
| 853 | } | ||
| 854 | |||
| 855 | if (*cp != '\n' && *cp != '\0') { | ||
| 856 | argc = 0; | ||
| 857 | cpp = cbuf; | ||
| 858 | while (*cp) { | ||
| 859 | /* skip whitespace */ | ||
| 860 | while (isspace(*cp)) | ||
| 861 | cp++; | ||
| 862 | if ((*cp == '\0') || (*cp == '\n') || | ||
| 863 | (*cp == '#' && !defcmd_in_progress)) | ||
| 864 | break; | ||
| 865 | /* special case: check for | grep pattern */ | ||
| 866 | if (*cp == '|') { | ||
| 867 | check_grep++; | ||
| 868 | break; | ||
| 869 | } | ||
| 870 | if (cpp >= cbuf + CMD_BUFLEN) { | ||
| 871 | kdb_printf("kdb_parse: command buffer " | ||
| 872 | "overflow, command ignored\n%s\n", | ||
| 873 | cmdstr); | ||
| 874 | return KDB_NOTFOUND; | ||
| 875 | } | ||
| 876 | if (argc >= MAXARGC - 1) { | ||
| 877 | kdb_printf("kdb_parse: too many arguments, " | ||
| 878 | "command ignored\n%s\n", cmdstr); | ||
| 879 | return KDB_NOTFOUND; | ||
| 880 | } | ||
| 881 | argv[argc++] = cpp; | ||
| 882 | escaped = 0; | ||
| 883 | quoted = '\0'; | ||
| 884 | /* Copy to next unquoted and unescaped | ||
| 885 | * whitespace or '=' */ | ||
| 886 | while (*cp && *cp != '\n' && | ||
| 887 | (escaped || quoted || !isspace(*cp))) { | ||
| 888 | if (cpp >= cbuf + CMD_BUFLEN) | ||
| 889 | break; | ||
| 890 | if (escaped) { | ||
| 891 | escaped = 0; | ||
| 892 | *cpp++ = *cp++; | ||
| 893 | continue; | ||
| 894 | } | ||
| 895 | if (*cp == '\\') { | ||
| 896 | escaped = 1; | ||
| 897 | ++cp; | ||
| 898 | continue; | ||
| 899 | } | ||
| 900 | if (*cp == quoted) | ||
| 901 | quoted = '\0'; | ||
| 902 | else if (*cp == '\'' || *cp == '"') | ||
| 903 | quoted = *cp; | ||
| 904 | *cpp = *cp++; | ||
| 905 | if (*cpp == '=' && !quoted) | ||
| 906 | break; | ||
| 907 | ++cpp; | ||
| 908 | } | ||
| 909 | *cpp++ = '\0'; /* Squash a ws or '=' character */ | ||
| 910 | } | ||
| 911 | } | ||
| 912 | if (!argc) | ||
| 913 | return 0; | ||
| 914 | if (check_grep) | ||
| 915 | parse_grep(cp); | ||
| 916 | if (defcmd_in_progress) { | ||
| 917 | int result = kdb_defcmd2(cmdstr, argv[0]); | ||
| 918 | if (!defcmd_in_progress) { | ||
| 919 | argc = 0; /* avoid repeat on endefcmd */ | ||
| 920 | *(argv[0]) = '\0'; | ||
| 921 | } | ||
| 922 | return result; | ||
| 923 | } | ||
| 924 | if (argv[0][0] == '-' && argv[0][1] && | ||
| 925 | (argv[0][1] < '0' || argv[0][1] > '9')) { | ||
| 926 | ignore_errors = 1; | ||
| 927 | ++argv[0]; | ||
| 928 | } | ||
| 929 | |||
| 930 | for_each_kdbcmd(tp, i) { | ||
| 931 | if (tp->cmd_name) { | ||
| 932 | /* | ||
| 933 | * If this command is allowed to be abbreviated, | ||
| 934 | * check to see if this is it. | ||
| 935 | */ | ||
| 936 | |||
| 937 | if (tp->cmd_minlen | ||
| 938 | && (strlen(argv[0]) <= tp->cmd_minlen)) { | ||
| 939 | if (strncmp(argv[0], | ||
| 940 | tp->cmd_name, | ||
| 941 | tp->cmd_minlen) == 0) { | ||
| 942 | break; | ||
| 943 | } | ||
| 944 | } | ||
| 945 | |||
| 946 | if (strcmp(argv[0], tp->cmd_name) == 0) | ||
| 947 | break; | ||
| 948 | } | ||
| 949 | } | ||
| 950 | |||
| 951 | /* | ||
| 952 | * If we don't find a command by this name, see if the first | ||
| 953 | * few characters of this match any of the known commands. | ||
| 954 | * e.g., md1c20 should match md. | ||
| 955 | */ | ||
| 956 | if (i == kdb_max_commands) { | ||
| 957 | for_each_kdbcmd(tp, i) { | ||
| 958 | if (tp->cmd_name) { | ||
| 959 | if (strncmp(argv[0], | ||
| 960 | tp->cmd_name, | ||
| 961 | strlen(tp->cmd_name)) == 0) { | ||
| 962 | break; | ||
| 963 | } | ||
| 964 | } | ||
| 965 | } | ||
| 966 | } | ||
| 967 | |||
| 968 | if (i < kdb_max_commands) { | ||
| 969 | int result; | ||
| 970 | KDB_STATE_SET(CMD); | ||
| 971 | result = (*tp->cmd_func)(argc-1, (const char **)argv); | ||
| 972 | if (result && ignore_errors && result > KDB_CMD_GO) | ||
| 973 | result = 0; | ||
| 974 | KDB_STATE_CLEAR(CMD); | ||
| 975 | switch (tp->cmd_repeat) { | ||
| 976 | case KDB_REPEAT_NONE: | ||
| 977 | argc = 0; | ||
| 978 | if (argv[0]) | ||
| 979 | *(argv[0]) = '\0'; | ||
| 980 | break; | ||
| 981 | case KDB_REPEAT_NO_ARGS: | ||
| 982 | argc = 1; | ||
| 983 | if (argv[1]) | ||
| 984 | *(argv[1]) = '\0'; | ||
| 985 | break; | ||
| 986 | case KDB_REPEAT_WITH_ARGS: | ||
| 987 | break; | ||
| 988 | } | ||
| 989 | return result; | ||
| 990 | } | ||
| 991 | |||
| 992 | /* | ||
| 993 | * If the input with which we were presented does not | ||
| 994 | * map to an existing command, attempt to parse it as an | ||
| 995 | * address argument and display the result. Useful for | ||
| 996 | * obtaining the address of a variable, or the nearest symbol | ||
| 997 | * to an address contained in a register. | ||
| 998 | */ | ||
| 999 | { | ||
| 1000 | unsigned long value; | ||
| 1001 | char *name = NULL; | ||
| 1002 | long offset; | ||
| 1003 | int nextarg = 0; | ||
| 1004 | |||
| 1005 | if (kdbgetaddrarg(0, (const char **)argv, &nextarg, | ||
| 1006 | &value, &offset, &name)) { | ||
| 1007 | return KDB_NOTFOUND; | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | kdb_printf("%s = ", argv[0]); | ||
| 1011 | kdb_symbol_print(value, NULL, KDB_SP_DEFAULT); | ||
| 1012 | kdb_printf("\n"); | ||
| 1013 | return 0; | ||
| 1014 | } | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | |||
| 1018 | static int handle_ctrl_cmd(char *cmd) | ||
| 1019 | { | ||
| 1020 | #define CTRL_P 16 | ||
| 1021 | #define CTRL_N 14 | ||
| 1022 | |||
| 1023 | /* initial situation */ | ||
| 1024 | if (cmd_head == cmd_tail) | ||
| 1025 | return 0; | ||
| 1026 | switch (*cmd) { | ||
| 1027 | case CTRL_P: | ||
| 1028 | if (cmdptr != cmd_tail) | ||
| 1029 | cmdptr = (cmdptr-1) % KDB_CMD_HISTORY_COUNT; | ||
| 1030 | strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN); | ||
| 1031 | return 1; | ||
| 1032 | case CTRL_N: | ||
| 1033 | if (cmdptr != cmd_head) | ||
| 1034 | cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT; | ||
| 1035 | strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN); | ||
| 1036 | return 1; | ||
| 1037 | } | ||
| 1038 | return 0; | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | /* | ||
| 1042 | * kdb_reboot - This function implements the 'reboot' command. Reboot | ||
| 1043 | * the system immediately, or loop for ever on failure. | ||
| 1044 | */ | ||
| 1045 | static int kdb_reboot(int argc, const char **argv) | ||
| 1046 | { | ||
| 1047 | emergency_restart(); | ||
| 1048 | kdb_printf("Hmm, kdb_reboot did not reboot, spinning here\n"); | ||
| 1049 | while (1) | ||
| 1050 | cpu_relax(); | ||
| 1051 | /* NOTREACHED */ | ||
| 1052 | return 0; | ||
| 1053 | } | ||
| 1054 | |||
| 1055 | static void kdb_dumpregs(struct pt_regs *regs) | ||
| 1056 | { | ||
| 1057 | int old_lvl = console_loglevel; | ||
| 1058 | console_loglevel = 15; | ||
| 1059 | kdb_trap_printk++; | ||
| 1060 | show_regs(regs); | ||
| 1061 | kdb_trap_printk--; | ||
| 1062 | kdb_printf("\n"); | ||
| 1063 | console_loglevel = old_lvl; | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | void kdb_set_current_task(struct task_struct *p) | ||
| 1067 | { | ||
| 1068 | kdb_current_task = p; | ||
| 1069 | |||
| 1070 | if (kdb_task_has_cpu(p)) { | ||
| 1071 | kdb_current_regs = KDB_TSKREGS(kdb_process_cpu(p)); | ||
| 1072 | return; | ||
| 1073 | } | ||
| 1074 | kdb_current_regs = NULL; | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | /* | ||
| 1078 | * kdb_local - The main code for kdb. This routine is invoked on a | ||
| 1079 | * specific processor, it is not global. The main kdb() routine | ||
| 1080 | * ensures that only one processor at a time is in this routine. | ||
| 1081 | * This code is called with the real reason code on the first | ||
| 1082 | * entry to a kdb session, thereafter it is called with reason | ||
| 1083 | * SWITCH, even if the user goes back to the original cpu. | ||
| 1084 | * Inputs: | ||
| 1085 | * reason The reason KDB was invoked | ||
| 1086 | * error The hardware-defined error code | ||
| 1087 | * regs The exception frame at time of fault/breakpoint. | ||
| 1088 | * db_result Result code from the break or debug point. | ||
| 1089 | * Returns: | ||
| 1090 | * 0 KDB was invoked for an event which it wasn't responsible | ||
| 1091 | * 1 KDB handled the event for which it was invoked. | ||
| 1092 | * KDB_CMD_GO User typed 'go'. | ||
| 1093 | * KDB_CMD_CPU User switched to another cpu. | ||
| 1094 | * KDB_CMD_SS Single step. | ||
| 1095 | * KDB_CMD_SSB Single step until branch. | ||
| 1096 | */ | ||
| 1097 | static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs, | ||
| 1098 | kdb_dbtrap_t db_result) | ||
| 1099 | { | ||
| 1100 | char *cmdbuf; | ||
| 1101 | int diag; | ||
| 1102 | struct task_struct *kdb_current = | ||
| 1103 | kdb_curr_task(raw_smp_processor_id()); | ||
| 1104 | |||
| 1105 | KDB_DEBUG_STATE("kdb_local 1", reason); | ||
| 1106 | kdb_go_count = 0; | ||
| 1107 | if (reason == KDB_REASON_DEBUG) { | ||
| 1108 | /* special case below */ | ||
| 1109 | } else { | ||
| 1110 | kdb_printf("\nEntering kdb (current=0x%p, pid %d) ", | ||
| 1111 | kdb_current, kdb_current->pid); | ||
| 1112 | #if defined(CONFIG_SMP) | ||
| 1113 | kdb_printf("on processor %d ", raw_smp_processor_id()); | ||
| 1114 | #endif | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | switch (reason) { | ||
| 1118 | case KDB_REASON_DEBUG: | ||
| 1119 | { | ||
| 1120 | /* | ||
| 1121 | * If re-entering kdb after a single step | ||
| 1122 | * command, don't print the message. | ||
| 1123 | */ | ||
| 1124 | switch (db_result) { | ||
| 1125 | case KDB_DB_BPT: | ||
| 1126 | kdb_printf("\nEntering kdb (0x%p, pid %d) ", | ||
| 1127 | kdb_current, kdb_current->pid); | ||
| 1128 | #if defined(CONFIG_SMP) | ||
| 1129 | kdb_printf("on processor %d ", raw_smp_processor_id()); | ||
| 1130 | #endif | ||
| 1131 | kdb_printf("due to Debug @ " kdb_machreg_fmt "\n", | ||
| 1132 | instruction_pointer(regs)); | ||
| 1133 | break; | ||
| 1134 | case KDB_DB_SSB: | ||
| 1135 | /* | ||
| 1136 | * In the midst of ssb command. Just return. | ||
| 1137 | */ | ||
| 1138 | KDB_DEBUG_STATE("kdb_local 3", reason); | ||
| 1139 | return KDB_CMD_SSB; /* Continue with SSB command */ | ||
| 1140 | |||
| 1141 | break; | ||
| 1142 | case KDB_DB_SS: | ||
| 1143 | break; | ||
| 1144 | case KDB_DB_SSBPT: | ||
| 1145 | KDB_DEBUG_STATE("kdb_local 4", reason); | ||
| 1146 | return 1; /* kdba_db_trap did the work */ | ||
| 1147 | default: | ||
| 1148 | kdb_printf("kdb: Bad result from kdba_db_trap: %d\n", | ||
| 1149 | db_result); | ||
| 1150 | break; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | } | ||
| 1154 | break; | ||
| 1155 | case KDB_REASON_ENTER: | ||
| 1156 | if (KDB_STATE(KEYBOARD)) | ||
| 1157 | kdb_printf("due to Keyboard Entry\n"); | ||
| 1158 | else | ||
| 1159 | kdb_printf("due to KDB_ENTER()\n"); | ||
| 1160 | break; | ||
| 1161 | case KDB_REASON_KEYBOARD: | ||
| 1162 | KDB_STATE_SET(KEYBOARD); | ||
| 1163 | kdb_printf("due to Keyboard Entry\n"); | ||
| 1164 | break; | ||
| 1165 | case KDB_REASON_ENTER_SLAVE: | ||
| 1166 | /* drop through, slaves only get released via cpu switch */ | ||
| 1167 | case KDB_REASON_SWITCH: | ||
| 1168 | kdb_printf("due to cpu switch\n"); | ||
| 1169 | break; | ||
| 1170 | case KDB_REASON_OOPS: | ||
| 1171 | kdb_printf("Oops: %s\n", kdb_diemsg); | ||
| 1172 | kdb_printf("due to oops @ " kdb_machreg_fmt "\n", | ||
| 1173 | instruction_pointer(regs)); | ||
| 1174 | kdb_dumpregs(regs); | ||
| 1175 | break; | ||
| 1176 | case KDB_REASON_NMI: | ||
| 1177 | kdb_printf("due to NonMaskable Interrupt @ " | ||
| 1178 | kdb_machreg_fmt "\n", | ||
| 1179 | instruction_pointer(regs)); | ||
| 1180 | kdb_dumpregs(regs); | ||
| 1181 | break; | ||
| 1182 | case KDB_REASON_SSTEP: | ||
| 1183 | case KDB_REASON_BREAK: | ||
| 1184 | kdb_printf("due to %s @ " kdb_machreg_fmt "\n", | ||
| 1185 | reason == KDB_REASON_BREAK ? | ||
| 1186 | "Breakpoint" : "SS trap", instruction_pointer(regs)); | ||
| 1187 | /* | ||
| 1188 | * Determine if this breakpoint is one that we | ||
| 1189 | * are interested in. | ||
| 1190 | */ | ||
| 1191 | if (db_result != KDB_DB_BPT) { | ||
| 1192 | kdb_printf("kdb: error return from kdba_bp_trap: %d\n", | ||
| 1193 | db_result); | ||
| 1194 | KDB_DEBUG_STATE("kdb_local 6", reason); | ||
| 1195 | return 0; /* Not for us, dismiss it */ | ||
| 1196 | } | ||
| 1197 | break; | ||
| 1198 | case KDB_REASON_RECURSE: | ||
| 1199 | kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n", | ||
| 1200 | instruction_pointer(regs)); | ||
| 1201 | break; | ||
| 1202 | default: | ||
| 1203 | kdb_printf("kdb: unexpected reason code: %d\n", reason); | ||
| 1204 | KDB_DEBUG_STATE("kdb_local 8", reason); | ||
| 1205 | return 0; /* Not for us, dismiss it */ | ||
| 1206 | } | ||
| 1207 | |||
| 1208 | while (1) { | ||
| 1209 | /* | ||
| 1210 | * Initialize pager context. | ||
| 1211 | */ | ||
| 1212 | kdb_nextline = 1; | ||
| 1213 | KDB_STATE_CLEAR(SUPPRESS); | ||
| 1214 | |||
| 1215 | cmdbuf = cmd_cur; | ||
| 1216 | *cmdbuf = '\0'; | ||
| 1217 | *(cmd_hist[cmd_head]) = '\0'; | ||
| 1218 | |||
| 1219 | if (KDB_FLAG(ONLY_DO_DUMP)) { | ||
| 1220 | /* kdb is off but a catastrophic error requires a dump. | ||
| 1221 | * Take the dump and reboot. | ||
| 1222 | * Turn on logging so the kdb output appears in the log | ||
| 1223 | * buffer in the dump. | ||
| 1224 | */ | ||
| 1225 | const char *setargs[] = { "set", "LOGGING", "1" }; | ||
| 1226 | kdb_set(2, setargs); | ||
| 1227 | kdb_reboot(0, NULL); | ||
| 1228 | /*NOTREACHED*/ | ||
| 1229 | } | ||
| 1230 | |||
| 1231 | do_full_getstr: | ||
| 1232 | #if defined(CONFIG_SMP) | ||
| 1233 | snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"), | ||
| 1234 | raw_smp_processor_id()); | ||
| 1235 | #else | ||
| 1236 | snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT")); | ||
| 1237 | #endif | ||
| 1238 | if (defcmd_in_progress) | ||
| 1239 | strncat(kdb_prompt_str, "[defcmd]", CMD_BUFLEN); | ||
| 1240 | |||
| 1241 | /* | ||
| 1242 | * Fetch command from keyboard | ||
| 1243 | */ | ||
| 1244 | cmdbuf = kdb_getstr(cmdbuf, CMD_BUFLEN, kdb_prompt_str); | ||
| 1245 | if (*cmdbuf != '\n') { | ||
| 1246 | if (*cmdbuf < 32) { | ||
| 1247 | if (cmdptr == cmd_head) { | ||
| 1248 | strncpy(cmd_hist[cmd_head], cmd_cur, | ||
| 1249 | CMD_BUFLEN); | ||
| 1250 | *(cmd_hist[cmd_head] + | ||
| 1251 | strlen(cmd_hist[cmd_head])-1) = '\0'; | ||
| 1252 | } | ||
| 1253 | if (!handle_ctrl_cmd(cmdbuf)) | ||
| 1254 | *(cmd_cur+strlen(cmd_cur)-1) = '\0'; | ||
| 1255 | cmdbuf = cmd_cur; | ||
| 1256 | goto do_full_getstr; | ||
| 1257 | } else { | ||
| 1258 | strncpy(cmd_hist[cmd_head], cmd_cur, | ||
| 1259 | CMD_BUFLEN); | ||
| 1260 | } | ||
| 1261 | |||
| 1262 | cmd_head = (cmd_head+1) % KDB_CMD_HISTORY_COUNT; | ||
| 1263 | if (cmd_head == cmd_tail) | ||
| 1264 | cmd_tail = (cmd_tail+1) % KDB_CMD_HISTORY_COUNT; | ||
| 1265 | } | ||
| 1266 | |||
| 1267 | cmdptr = cmd_head; | ||
| 1268 | diag = kdb_parse(cmdbuf); | ||
| 1269 | if (diag == KDB_NOTFOUND) { | ||
| 1270 | kdb_printf("Unknown kdb command: '%s'\n", cmdbuf); | ||
| 1271 | diag = 0; | ||
| 1272 | } | ||
| 1273 | if (diag == KDB_CMD_GO | ||
| 1274 | || diag == KDB_CMD_CPU | ||
| 1275 | || diag == KDB_CMD_SS | ||
| 1276 | || diag == KDB_CMD_SSB | ||
| 1277 | || diag == KDB_CMD_KGDB) | ||
| 1278 | break; | ||
| 1279 | |||
| 1280 | if (diag) | ||
| 1281 | kdb_cmderror(diag); | ||
| 1282 | } | ||
| 1283 | KDB_DEBUG_STATE("kdb_local 9", diag); | ||
| 1284 | return diag; | ||
| 1285 | } | ||
| 1286 | |||
| 1287 | |||
| 1288 | /* | ||
| 1289 | * kdb_print_state - Print the state data for the current processor | ||
| 1290 | * for debugging. | ||
| 1291 | * Inputs: | ||
| 1292 | * text Identifies the debug point | ||
| 1293 | * value Any integer value to be printed, e.g. reason code. | ||
| 1294 | */ | ||
| 1295 | void kdb_print_state(const char *text, int value) | ||
| 1296 | { | ||
| 1297 | kdb_printf("state: %s cpu %d value %d initial %d state %x\n", | ||
| 1298 | text, raw_smp_processor_id(), value, kdb_initial_cpu, | ||
| 1299 | kdb_state); | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | /* | ||
| 1303 | * kdb_main_loop - After initial setup and assignment of the | ||
| 1304 | * controlling cpu, all cpus are in this loop. One cpu is in | ||
| 1305 | * control and will issue the kdb prompt, the others will spin | ||
| 1306 | * until 'go' or cpu switch. | ||
| 1307 | * | ||
| 1308 | * To get a consistent view of the kernel stacks for all | ||
| 1309 | * processes, this routine is invoked from the main kdb code via | ||
| 1310 | * an architecture specific routine. kdba_main_loop is | ||
| 1311 | * responsible for making the kernel stacks consistent for all | ||
| 1312 | * processes, there should be no difference between a blocked | ||
| 1313 | * process and a running process as far as kdb is concerned. | ||
| 1314 | * Inputs: | ||
| 1315 | * reason The reason KDB was invoked | ||
| 1316 | * error The hardware-defined error code | ||
| 1317 | * reason2 kdb's current reason code. | ||
| 1318 | * Initially error but can change | ||
| 1319 | * acording to kdb state. | ||
| 1320 | * db_result Result code from break or debug point. | ||
| 1321 | * regs The exception frame at time of fault/breakpoint. | ||
| 1322 | * should always be valid. | ||
| 1323 | * Returns: | ||
| 1324 | * 0 KDB was invoked for an event which it wasn't responsible | ||
| 1325 | * 1 KDB handled the event for which it was invoked. | ||
| 1326 | */ | ||
| 1327 | int kdb_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error, | ||
| 1328 | kdb_dbtrap_t db_result, struct pt_regs *regs) | ||
| 1329 | { | ||
| 1330 | int result = 1; | ||
| 1331 | /* Stay in kdb() until 'go', 'ss[b]' or an error */ | ||
| 1332 | while (1) { | ||
| 1333 | /* | ||
| 1334 | * All processors except the one that is in control | ||
| 1335 | * will spin here. | ||
| 1336 | */ | ||
| 1337 | KDB_DEBUG_STATE("kdb_main_loop 1", reason); | ||
| 1338 | while (KDB_STATE(HOLD_CPU)) { | ||
| 1339 | /* state KDB is turned off by kdb_cpu to see if the | ||
| 1340 | * other cpus are still live, each cpu in this loop | ||
| 1341 | * turns it back on. | ||
| 1342 | */ | ||
| 1343 | if (!KDB_STATE(KDB)) | ||
| 1344 | KDB_STATE_SET(KDB); | ||
| 1345 | } | ||
| 1346 | |||
| 1347 | KDB_STATE_CLEAR(SUPPRESS); | ||
| 1348 | KDB_DEBUG_STATE("kdb_main_loop 2", reason); | ||
| 1349 | if (KDB_STATE(LEAVING)) | ||
| 1350 | break; /* Another cpu said 'go' */ | ||
| 1351 | /* Still using kdb, this processor is in control */ | ||
| 1352 | result = kdb_local(reason2, error, regs, db_result); | ||
| 1353 | KDB_DEBUG_STATE("kdb_main_loop 3", result); | ||
| 1354 | |||
| 1355 | if (result == KDB_CMD_CPU) | ||
| 1356 | break; | ||
| 1357 | |||
| 1358 | if (result == KDB_CMD_SS) { | ||
| 1359 | KDB_STATE_SET(DOING_SS); | ||
| 1360 | break; | ||
| 1361 | } | ||
| 1362 | |||
| 1363 | if (result == KDB_CMD_SSB) { | ||
| 1364 | KDB_STATE_SET(DOING_SS); | ||
| 1365 | KDB_STATE_SET(DOING_SSB); | ||
| 1366 | break; | ||
| 1367 | } | ||
| 1368 | |||
| 1369 | if (result == KDB_CMD_KGDB) { | ||
| 1370 | if (!(KDB_STATE(DOING_KGDB) || KDB_STATE(DOING_KGDB2))) | ||
| 1371 | kdb_printf("Entering please attach debugger " | ||
| 1372 | "or use $D#44+ or $3#33\n"); | ||
| 1373 | break; | ||
| 1374 | } | ||
| 1375 | if (result && result != 1 && result != KDB_CMD_GO) | ||
| 1376 | kdb_printf("\nUnexpected kdb_local return code %d\n", | ||
| 1377 | result); | ||
| 1378 | KDB_DEBUG_STATE("kdb_main_loop 4", reason); | ||
| 1379 | break; | ||
| 1380 | } | ||
| 1381 | if (KDB_STATE(DOING_SS)) | ||
| 1382 | KDB_STATE_CLEAR(SSBPT); | ||
| 1383 | |||
| 1384 | return result; | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | /* | ||
| 1388 | * kdb_mdr - This function implements the guts of the 'mdr', memory | ||
| 1389 | * read command. | ||
| 1390 | * mdr <addr arg>,<byte count> | ||
| 1391 | * Inputs: | ||
| 1392 | * addr Start address | ||
| 1393 | * count Number of bytes | ||
| 1394 | * Returns: | ||
| 1395 | * Always 0. Any errors are detected and printed by kdb_getarea. | ||
| 1396 | */ | ||
| 1397 | static int kdb_mdr(unsigned long addr, unsigned int count) | ||
| 1398 | { | ||
| 1399 | unsigned char c; | ||
| 1400 | while (count--) { | ||
| 1401 | if (kdb_getarea(c, addr)) | ||
| 1402 | return 0; | ||
| 1403 | kdb_printf("%02x", c); | ||
| 1404 | addr++; | ||
| 1405 | } | ||
| 1406 | kdb_printf("\n"); | ||
| 1407 | return 0; | ||
| 1408 | } | ||
| 1409 | |||
| 1410 | /* | ||
| 1411 | * kdb_md - This function implements the 'md', 'md1', 'md2', 'md4', | ||
| 1412 | * 'md8' 'mdr' and 'mds' commands. | ||
| 1413 | * | ||
| 1414 | * md|mds [<addr arg> [<line count> [<radix>]]] | ||
| 1415 | * mdWcN [<addr arg> [<line count> [<radix>]]] | ||
| 1416 | * where W = is the width (1, 2, 4 or 8) and N is the count. | ||
| 1417 | * for eg., md1c20 reads 20 bytes, 1 at a time. | ||
| 1418 | * mdr <addr arg>,<byte count> | ||
| 1419 | */ | ||
| 1420 | static void kdb_md_line(const char *fmtstr, unsigned long addr, | ||
| 1421 | int symbolic, int nosect, int bytesperword, | ||
| 1422 | int num, int repeat, int phys) | ||
| 1423 | { | ||
| 1424 | /* print just one line of data */ | ||
| 1425 | kdb_symtab_t symtab; | ||
| 1426 | char cbuf[32]; | ||
| 1427 | char *c = cbuf; | ||
| 1428 | int i; | ||
| 1429 | unsigned long word; | ||
| 1430 | |||
| 1431 | memset(cbuf, '\0', sizeof(cbuf)); | ||
| 1432 | if (phys) | ||
| 1433 | kdb_printf("phys " kdb_machreg_fmt0 " ", addr); | ||
| 1434 | else | ||
| 1435 | kdb_printf(kdb_machreg_fmt0 " ", addr); | ||
| 1436 | |||
| 1437 | for (i = 0; i < num && repeat--; i++) { | ||
| 1438 | if (phys) { | ||
| 1439 | if (kdb_getphysword(&word, addr, bytesperword)) | ||
| 1440 | break; | ||
| 1441 | } else if (kdb_getword(&word, addr, bytesperword)) | ||
| 1442 | break; | ||
| 1443 | kdb_printf(fmtstr, word); | ||
| 1444 | if (symbolic) | ||
| 1445 | kdbnearsym(word, &symtab); | ||
| 1446 | else | ||
| 1447 | memset(&symtab, 0, sizeof(symtab)); | ||
| 1448 | if (symtab.sym_name) { | ||
| 1449 | kdb_symbol_print(word, &symtab, 0); | ||
| 1450 | if (!nosect) { | ||
| 1451 | kdb_printf("\n"); | ||
| 1452 | kdb_printf(" %s %s " | ||
| 1453 | kdb_machreg_fmt " " | ||
| 1454 | kdb_machreg_fmt " " | ||
| 1455 | kdb_machreg_fmt, symtab.mod_name, | ||
| 1456 | symtab.sec_name, symtab.sec_start, | ||
| 1457 | symtab.sym_start, symtab.sym_end); | ||
| 1458 | } | ||
| 1459 | addr += bytesperword; | ||
| 1460 | } else { | ||
| 1461 | union { | ||
| 1462 | u64 word; | ||
| 1463 | unsigned char c[8]; | ||
| 1464 | } wc; | ||
| 1465 | unsigned char *cp; | ||
| 1466 | #ifdef __BIG_ENDIAN | ||
| 1467 | cp = wc.c + 8 - bytesperword; | ||
| 1468 | #else | ||
| 1469 | cp = wc.c; | ||
| 1470 | #endif | ||
| 1471 | wc.word = word; | ||
| 1472 | #define printable_char(c) \ | ||
| 1473 | ({unsigned char __c = c; isascii(__c) && isprint(__c) ? __c : '.'; }) | ||
| 1474 | switch (bytesperword) { | ||
| 1475 | case 8: | ||
| 1476 | *c++ = printable_char(*cp++); | ||
| 1477 | *c++ = printable_char(*cp++); | ||
| 1478 | *c++ = printable_char(*cp++); | ||
| 1479 | *c++ = printable_char(*cp++); | ||
| 1480 | addr += 4; | ||
| 1481 | case 4: | ||
| 1482 | *c++ = printable_char(*cp++); | ||
| 1483 | *c++ = printable_char(*cp++); | ||
| 1484 | addr += 2; | ||
| 1485 | case 2: | ||
| 1486 | *c++ = printable_char(*cp++); | ||
| 1487 | addr++; | ||
| 1488 | case 1: | ||
| 1489 | *c++ = printable_char(*cp++); | ||
| 1490 | addr++; | ||
| 1491 | break; | ||
| 1492 | } | ||
| 1493 | #undef printable_char | ||
| 1494 | } | ||
| 1495 | } | ||
| 1496 | kdb_printf("%*s %s\n", (int)((num-i)*(2*bytesperword + 1)+1), | ||
| 1497 | " ", cbuf); | ||
| 1498 | } | ||
| 1499 | |||
| 1500 | static int kdb_md(int argc, const char **argv) | ||
| 1501 | { | ||
| 1502 | static unsigned long last_addr; | ||
| 1503 | static int last_radix, last_bytesperword, last_repeat; | ||
| 1504 | int radix = 16, mdcount = 8, bytesperword = KDB_WORD_SIZE, repeat; | ||
| 1505 | int nosect = 0; | ||
| 1506 | char fmtchar, fmtstr[64]; | ||
| 1507 | unsigned long addr; | ||
| 1508 | unsigned long word; | ||
| 1509 | long offset = 0; | ||
| 1510 | int symbolic = 0; | ||
| 1511 | int valid = 0; | ||
| 1512 | int phys = 0; | ||
| 1513 | |||
| 1514 | kdbgetintenv("MDCOUNT", &mdcount); | ||
| 1515 | kdbgetintenv("RADIX", &radix); | ||
| 1516 | kdbgetintenv("BYTESPERWORD", &bytesperword); | ||
| 1517 | |||
| 1518 | /* Assume 'md <addr>' and start with environment values */ | ||
| 1519 | repeat = mdcount * 16 / bytesperword; | ||
| 1520 | |||
| 1521 | if (strcmp(argv[0], "mdr") == 0) { | ||
| 1522 | if (argc != 2) | ||
| 1523 | return KDB_ARGCOUNT; | ||
| 1524 | valid = 1; | ||
| 1525 | } else if (isdigit(argv[0][2])) { | ||
| 1526 | bytesperword = (int)(argv[0][2] - '0'); | ||
| 1527 | if (bytesperword == 0) { | ||
| 1528 | bytesperword = last_bytesperword; | ||
| 1529 | if (bytesperword == 0) | ||
| 1530 | bytesperword = 4; | ||
| 1531 | } | ||
| 1532 | last_bytesperword = bytesperword; | ||
| 1533 | repeat = mdcount * 16 / bytesperword; | ||
| 1534 | if (!argv[0][3]) | ||
| 1535 | valid = 1; | ||
| 1536 | else if (argv[0][3] == 'c' && argv[0][4]) { | ||
| 1537 | char *p; | ||
| 1538 | repeat = simple_strtoul(argv[0] + 4, &p, 10); | ||
| 1539 | mdcount = ((repeat * bytesperword) + 15) / 16; | ||
| 1540 | valid = !*p; | ||
| 1541 | } | ||
| 1542 | last_repeat = repeat; | ||
| 1543 | } else if (strcmp(argv[0], "md") == 0) | ||
| 1544 | valid = 1; | ||
| 1545 | else if (strcmp(argv[0], "mds") == 0) | ||
| 1546 | valid = 1; | ||
| 1547 | else if (strcmp(argv[0], "mdp") == 0) { | ||
| 1548 | phys = valid = 1; | ||
| 1549 | } | ||
| 1550 | if (!valid) | ||
| 1551 | return KDB_NOTFOUND; | ||
| 1552 | |||
| 1553 | if (argc == 0) { | ||
| 1554 | if (last_addr == 0) | ||
| 1555 | return KDB_ARGCOUNT; | ||
| 1556 | addr = last_addr; | ||
| 1557 | radix = last_radix; | ||
| 1558 | bytesperword = last_bytesperword; | ||
| 1559 | repeat = last_repeat; | ||
| 1560 | mdcount = ((repeat * bytesperword) + 15) / 16; | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | if (argc) { | ||
| 1564 | unsigned long val; | ||
| 1565 | int diag, nextarg = 1; | ||
| 1566 | diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, | ||
| 1567 | &offset, NULL); | ||
| 1568 | if (diag) | ||
| 1569 | return diag; | ||
| 1570 | if (argc > nextarg+2) | ||
| 1571 | return KDB_ARGCOUNT; | ||
| 1572 | |||
| 1573 | if (argc >= nextarg) { | ||
| 1574 | diag = kdbgetularg(argv[nextarg], &val); | ||
| 1575 | if (!diag) { | ||
| 1576 | mdcount = (int) val; | ||
| 1577 | repeat = mdcount * 16 / bytesperword; | ||
| 1578 | } | ||
| 1579 | } | ||
| 1580 | if (argc >= nextarg+1) { | ||
| 1581 | diag = kdbgetularg(argv[nextarg+1], &val); | ||
| 1582 | if (!diag) | ||
| 1583 | radix = (int) val; | ||
| 1584 | } | ||
| 1585 | } | ||
| 1586 | |||
| 1587 | if (strcmp(argv[0], "mdr") == 0) | ||
| 1588 | return kdb_mdr(addr, mdcount); | ||
| 1589 | |||
| 1590 | switch (radix) { | ||
| 1591 | case 10: | ||
| 1592 | fmtchar = 'd'; | ||
| 1593 | break; | ||
| 1594 | case 16: | ||
| 1595 | fmtchar = 'x'; | ||
| 1596 | break; | ||
| 1597 | case 8: | ||
| 1598 | fmtchar = 'o'; | ||
| 1599 | break; | ||
| 1600 | default: | ||
| 1601 | return KDB_BADRADIX; | ||
| 1602 | } | ||
| 1603 | |||
| 1604 | last_radix = radix; | ||
| 1605 | |||
| 1606 | if (bytesperword > KDB_WORD_SIZE) | ||
| 1607 | return KDB_BADWIDTH; | ||
| 1608 | |||
| 1609 | switch (bytesperword) { | ||
| 1610 | case 8: | ||
| 1611 | sprintf(fmtstr, "%%16.16l%c ", fmtchar); | ||
| 1612 | break; | ||
| 1613 | case 4: | ||
| 1614 | sprintf(fmtstr, "%%8.8l%c ", fmtchar); | ||
| 1615 | break; | ||
| 1616 | case 2: | ||
| 1617 | sprintf(fmtstr, "%%4.4l%c ", fmtchar); | ||
| 1618 | break; | ||
| 1619 | case 1: | ||
| 1620 | sprintf(fmtstr, "%%2.2l%c ", fmtchar); | ||
| 1621 | break; | ||
| 1622 | default: | ||
| 1623 | return KDB_BADWIDTH; | ||
| 1624 | } | ||
| 1625 | |||
| 1626 | last_repeat = repeat; | ||
| 1627 | last_bytesperword = bytesperword; | ||
| 1628 | |||
| 1629 | if (strcmp(argv[0], "mds") == 0) { | ||
| 1630 | symbolic = 1; | ||
| 1631 | /* Do not save these changes as last_*, they are temporary mds | ||
| 1632 | * overrides. | ||
| 1633 | */ | ||
| 1634 | bytesperword = KDB_WORD_SIZE; | ||
| 1635 | repeat = mdcount; | ||
| 1636 | kdbgetintenv("NOSECT", &nosect); | ||
| 1637 | } | ||
| 1638 | |||
| 1639 | /* Round address down modulo BYTESPERWORD */ | ||
| 1640 | |||
| 1641 | addr &= ~(bytesperword-1); | ||
| 1642 | |||
| 1643 | while (repeat > 0) { | ||
| 1644 | unsigned long a; | ||
| 1645 | int n, z, num = (symbolic ? 1 : (16 / bytesperword)); | ||
| 1646 | |||
| 1647 | if (KDB_FLAG(CMD_INTERRUPT)) | ||
| 1648 | return 0; | ||
| 1649 | for (a = addr, z = 0; z < repeat; a += bytesperword, ++z) { | ||
| 1650 | if (phys) { | ||
| 1651 | if (kdb_getphysword(&word, a, bytesperword) | ||
| 1652 | || word) | ||
| 1653 | break; | ||
| 1654 | } else if (kdb_getword(&word, a, bytesperword) || word) | ||
| 1655 | break; | ||
| 1656 | } | ||
| 1657 | n = min(num, repeat); | ||
| 1658 | kdb_md_line(fmtstr, addr, symbolic, nosect, bytesperword, | ||
| 1659 | num, repeat, phys); | ||
| 1660 | addr += bytesperword * n; | ||
| 1661 | repeat -= n; | ||
| 1662 | z = (z + num - 1) / num; | ||
| 1663 | if (z > 2) { | ||
| 1664 | int s = num * (z-2); | ||
| 1665 | kdb_printf(kdb_machreg_fmt0 "-" kdb_machreg_fmt0 | ||
| 1666 | " zero suppressed\n", | ||
| 1667 | addr, addr + bytesperword * s - 1); | ||
| 1668 | addr += bytesperword * s; | ||
| 1669 | repeat -= s; | ||
| 1670 | } | ||
| 1671 | } | ||
| 1672 | last_addr = addr; | ||
| 1673 | |||
| 1674 | return 0; | ||
| 1675 | } | ||
| 1676 | |||
| 1677 | /* | ||
| 1678 | * kdb_mm - This function implements the 'mm' command. | ||
| 1679 | * mm address-expression new-value | ||
| 1680 | * Remarks: | ||
| 1681 | * mm works on machine words, mmW works on bytes. | ||
| 1682 | */ | ||
| 1683 | static int kdb_mm(int argc, const char **argv) | ||
| 1684 | { | ||
| 1685 | int diag; | ||
| 1686 | unsigned long addr; | ||
| 1687 | long offset = 0; | ||
| 1688 | unsigned long contents; | ||
| 1689 | int nextarg; | ||
| 1690 | int width; | ||
| 1691 | |||
| 1692 | if (argv[0][2] && !isdigit(argv[0][2])) | ||
| 1693 | return KDB_NOTFOUND; | ||
| 1694 | |||
| 1695 | if (argc < 2) | ||
| 1696 | return KDB_ARGCOUNT; | ||
| 1697 | |||
| 1698 | nextarg = 1; | ||
| 1699 | diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL); | ||
| 1700 | if (diag) | ||
| 1701 | return diag; | ||
| 1702 | |||
| 1703 | if (nextarg > argc) | ||
| 1704 | return KDB_ARGCOUNT; | ||
| 1705 | diag = kdbgetaddrarg(argc, argv, &nextarg, &contents, NULL, NULL); | ||
| 1706 | if (diag) | ||
| 1707 | return diag; | ||
| 1708 | |||
| 1709 | if (nextarg != argc + 1) | ||
| 1710 | return KDB_ARGCOUNT; | ||
| 1711 | |||
| 1712 | width = argv[0][2] ? (argv[0][2] - '0') : (KDB_WORD_SIZE); | ||
| 1713 | diag = kdb_putword(addr, contents, width); | ||
| 1714 | if (diag) | ||
| 1715 | return diag; | ||
| 1716 | |||
| 1717 | kdb_printf(kdb_machreg_fmt " = " kdb_machreg_fmt "\n", addr, contents); | ||
| 1718 | |||
| 1719 | return 0; | ||
| 1720 | } | ||
| 1721 | |||
| 1722 | /* | ||
| 1723 | * kdb_go - This function implements the 'go' command. | ||
| 1724 | * go [address-expression] | ||
| 1725 | */ | ||
| 1726 | static int kdb_go(int argc, const char **argv) | ||
| 1727 | { | ||
| 1728 | unsigned long addr; | ||
| 1729 | int diag; | ||
| 1730 | int nextarg; | ||
| 1731 | long offset; | ||
| 1732 | |||
| 1733 | if (argc == 1) { | ||
| 1734 | if (raw_smp_processor_id() != kdb_initial_cpu) { | ||
| 1735 | kdb_printf("go <address> must be issued from the " | ||
| 1736 | "initial cpu, do cpu %d first\n", | ||
| 1737 | kdb_initial_cpu); | ||
| 1738 | return KDB_ARGCOUNT; | ||
| 1739 | } | ||
| 1740 | nextarg = 1; | ||
| 1741 | diag = kdbgetaddrarg(argc, argv, &nextarg, | ||
| 1742 | &addr, &offset, NULL); | ||
| 1743 | if (diag) | ||
| 1744 | return diag; | ||
| 1745 | } else if (argc) { | ||
| 1746 | return KDB_ARGCOUNT; | ||
| 1747 | } | ||
| 1748 | |||
| 1749 | diag = KDB_CMD_GO; | ||
| 1750 | if (KDB_FLAG(CATASTROPHIC)) { | ||
| 1751 | kdb_printf("Catastrophic error detected\n"); | ||
| 1752 | kdb_printf("kdb_continue_catastrophic=%d, ", | ||
| 1753 | kdb_continue_catastrophic); | ||
| 1754 | if (kdb_continue_catastrophic == 0 && kdb_go_count++ == 0) { | ||
| 1755 | kdb_printf("type go a second time if you really want " | ||
| 1756 | "to continue\n"); | ||
| 1757 | return 0; | ||
| 1758 | } | ||
| 1759 | if (kdb_continue_catastrophic == 2) { | ||
| 1760 | kdb_printf("forcing reboot\n"); | ||
| 1761 | kdb_reboot(0, NULL); | ||
| 1762 | } | ||
| 1763 | kdb_printf("attempting to continue\n"); | ||
| 1764 | } | ||
| 1765 | return diag; | ||
| 1766 | } | ||
| 1767 | |||
| 1768 | /* | ||
| 1769 | * kdb_rd - This function implements the 'rd' command. | ||
| 1770 | */ | ||
| 1771 | static int kdb_rd(int argc, const char **argv) | ||
| 1772 | { | ||
| 1773 | int diag = kdb_check_regs(); | ||
| 1774 | if (diag) | ||
| 1775 | return diag; | ||
| 1776 | |||
| 1777 | kdb_dumpregs(kdb_current_regs); | ||
| 1778 | return 0; | ||
| 1779 | } | ||
| 1780 | |||
| 1781 | /* | ||
| 1782 | * kdb_rm - This function implements the 'rm' (register modify) command. | ||
| 1783 | * rm register-name new-contents | ||
| 1784 | * Remarks: | ||
| 1785 | * Currently doesn't allow modification of control or | ||
| 1786 | * debug registers. | ||
| 1787 | */ | ||
| 1788 | static int kdb_rm(int argc, const char **argv) | ||
| 1789 | { | ||
| 1790 | int diag; | ||
| 1791 | int ind = 0; | ||
| 1792 | unsigned long contents; | ||
| 1793 | |||
| 1794 | if (argc != 2) | ||
| 1795 | return KDB_ARGCOUNT; | ||
| 1796 | /* | ||
| 1797 | * Allow presence or absence of leading '%' symbol. | ||
| 1798 | */ | ||
| 1799 | if (argv[1][0] == '%') | ||
| 1800 | ind = 1; | ||
| 1801 | |||
| 1802 | diag = kdbgetularg(argv[2], &contents); | ||
| 1803 | if (diag) | ||
| 1804 | return diag; | ||
| 1805 | |||
| 1806 | diag = kdb_check_regs(); | ||
| 1807 | if (diag) | ||
| 1808 | return diag; | ||
| 1809 | kdb_printf("ERROR: Register set currently not implemented\n"); | ||
| 1810 | return 0; | ||
| 1811 | } | ||
| 1812 | |||
| 1813 | #if defined(CONFIG_MAGIC_SYSRQ) | ||
| 1814 | /* | ||
| 1815 | * kdb_sr - This function implements the 'sr' (SYSRQ key) command | ||
| 1816 | * which interfaces to the soi-disant MAGIC SYSRQ functionality. | ||
| 1817 | * sr <magic-sysrq-code> | ||
| 1818 | */ | ||
| 1819 | static int kdb_sr(int argc, const char **argv) | ||
| 1820 | { | ||
| 1821 | if (argc != 1) | ||
| 1822 | return KDB_ARGCOUNT; | ||
| 1823 | kdb_trap_printk++; | ||
| 1824 | __handle_sysrq(*argv[1], NULL, 0); | ||
| 1825 | kdb_trap_printk--; | ||
| 1826 | |||
| 1827 | return 0; | ||
| 1828 | } | ||
| 1829 | #endif /* CONFIG_MAGIC_SYSRQ */ | ||
| 1830 | |||
| 1831 | /* | ||
| 1832 | * kdb_ef - This function implements the 'regs' (display exception | ||
| 1833 | * frame) command. This command takes an address and expects to | ||
| 1834 | * find an exception frame at that address, formats and prints | ||
| 1835 | * it. | ||
| 1836 | * regs address-expression | ||
| 1837 | * Remarks: | ||
| 1838 | * Not done yet. | ||
| 1839 | */ | ||
| 1840 | static int kdb_ef(int argc, const char **argv) | ||
| 1841 | { | ||
| 1842 | int diag; | ||
| 1843 | unsigned long addr; | ||
| 1844 | long offset; | ||
| 1845 | int nextarg; | ||
| 1846 | |||
| 1847 | if (argc != 1) | ||
| 1848 | return KDB_ARGCOUNT; | ||
| 1849 | |||
| 1850 | nextarg = 1; | ||
| 1851 | diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL); | ||
| 1852 | if (diag) | ||
| 1853 | return diag; | ||
| 1854 | show_regs((struct pt_regs *)addr); | ||
| 1855 | return 0; | ||
| 1856 | } | ||
| 1857 | |||
| 1858 | #if defined(CONFIG_MODULES) | ||
| 1859 | /* | ||
| 1860 | * kdb_lsmod - This function implements the 'lsmod' command. Lists | ||
| 1861 | * currently loaded kernel modules. | ||
| 1862 | * Mostly taken from userland lsmod. | ||
| 1863 | */ | ||
| 1864 | static int kdb_lsmod(int argc, const char **argv) | ||
| 1865 | { | ||
| 1866 | struct module *mod; | ||
| 1867 | |||
| 1868 | if (argc != 0) | ||
| 1869 | return KDB_ARGCOUNT; | ||
| 1870 | |||
| 1871 | kdb_printf("Module Size modstruct Used by\n"); | ||
| 1872 | list_for_each_entry(mod, kdb_modules, list) { | ||
| 1873 | |||
| 1874 | kdb_printf("%-20s%8u 0x%p ", mod->name, | ||
| 1875 | mod->core_size, (void *)mod); | ||
| 1876 | #ifdef CONFIG_MODULE_UNLOAD | ||
| 1877 | kdb_printf("%4d ", module_refcount(mod)); | ||
| 1878 | #endif | ||
| 1879 | if (mod->state == MODULE_STATE_GOING) | ||
| 1880 | kdb_printf(" (Unloading)"); | ||
| 1881 | else if (mod->state == MODULE_STATE_COMING) | ||
| 1882 | kdb_printf(" (Loading)"); | ||
| 1883 | else | ||
| 1884 | kdb_printf(" (Live)"); | ||
| 1885 | kdb_printf(" 0x%p", mod->module_core); | ||
| 1886 | |||
| 1887 | #ifdef CONFIG_MODULE_UNLOAD | ||
| 1888 | { | ||
| 1889 | struct module_use *use; | ||
| 1890 | kdb_printf(" [ "); | ||
| 1891 | list_for_each_entry(use, &mod->source_list, | ||
| 1892 | source_list) | ||
| 1893 | kdb_printf("%s ", use->target->name); | ||
| 1894 | kdb_printf("]\n"); | ||
| 1895 | } | ||
| 1896 | #endif | ||
| 1897 | } | ||
| 1898 | |||
| 1899 | return 0; | ||
| 1900 | } | ||
| 1901 | |||
| 1902 | #endif /* CONFIG_MODULES */ | ||
| 1903 | |||
| 1904 | /* | ||
| 1905 | * kdb_env - This function implements the 'env' command. Display the | ||
| 1906 | * current environment variables. | ||
| 1907 | */ | ||
| 1908 | |||
| 1909 | static int kdb_env(int argc, const char **argv) | ||
| 1910 | { | ||
| 1911 | int i; | ||
| 1912 | |||
| 1913 | for (i = 0; i < __nenv; i++) { | ||
| 1914 | if (__env[i]) | ||
| 1915 | kdb_printf("%s\n", __env[i]); | ||
| 1916 | } | ||
| 1917 | |||
| 1918 | if (KDB_DEBUG(MASK)) | ||
| 1919 | kdb_printf("KDBFLAGS=0x%x\n", kdb_flags); | ||
| 1920 | |||
| 1921 | return 0; | ||
| 1922 | } | ||
| 1923 | |||
| 1924 | #ifdef CONFIG_PRINTK | ||
| 1925 | /* | ||
| 1926 | * kdb_dmesg - This function implements the 'dmesg' command to display | ||
| 1927 | * the contents of the syslog buffer. | ||
| 1928 | * dmesg [lines] [adjust] | ||
| 1929 | */ | ||
| 1930 | static int kdb_dmesg(int argc, const char **argv) | ||
| 1931 | { | ||
| 1932 | char *syslog_data[4], *start, *end, c = '\0', *p; | ||
| 1933 | int diag, logging, logsize, lines = 0, adjust = 0, n; | ||
| 1934 | |||
| 1935 | if (argc > 2) | ||
| 1936 | return KDB_ARGCOUNT; | ||
| 1937 | if (argc) { | ||
| 1938 | char *cp; | ||
| 1939 | lines = simple_strtol(argv[1], &cp, 0); | ||
| 1940 | if (*cp) | ||
| 1941 | lines = 0; | ||
| 1942 | if (argc > 1) { | ||
| 1943 | adjust = simple_strtoul(argv[2], &cp, 0); | ||
| 1944 | if (*cp || adjust < 0) | ||
| 1945 | adjust = 0; | ||
| 1946 | } | ||
| 1947 | } | ||
| 1948 | |||
| 1949 | /* disable LOGGING if set */ | ||
| 1950 | diag = kdbgetintenv("LOGGING", &logging); | ||
| 1951 | if (!diag && logging) { | ||
| 1952 | const char *setargs[] = { "set", "LOGGING", "0" }; | ||
| 1953 | kdb_set(2, setargs); | ||
| 1954 | } | ||
| 1955 | |||
| 1956 | /* syslog_data[0,1] physical start, end+1. syslog_data[2,3] | ||
| 1957 | * logical start, end+1. */ | ||
| 1958 | kdb_syslog_data(syslog_data); | ||
| 1959 | if (syslog_data[2] == syslog_data[3]) | ||
| 1960 | return 0; | ||
| 1961 | logsize = syslog_data[1] - syslog_data[0]; | ||
| 1962 | start = syslog_data[2]; | ||
| 1963 | end = syslog_data[3]; | ||
| 1964 | #define KDB_WRAP(p) (((p - syslog_data[0]) % logsize) + syslog_data[0]) | ||
| 1965 | for (n = 0, p = start; p < end; ++p) { | ||
| 1966 | c = *KDB_WRAP(p); | ||
| 1967 | if (c == '\n') | ||
| 1968 | ++n; | ||
| 1969 | } | ||
| 1970 | if (c != '\n') | ||
| 1971 | ++n; | ||
| 1972 | if (lines < 0) { | ||
| 1973 | if (adjust >= n) | ||
| 1974 | kdb_printf("buffer only contains %d lines, nothing " | ||
| 1975 | "printed\n", n); | ||
| 1976 | else if (adjust - lines >= n) | ||
| 1977 | kdb_printf("buffer only contains %d lines, last %d " | ||
| 1978 | "lines printed\n", n, n - adjust); | ||
| 1979 | if (adjust) { | ||
| 1980 | for (; start < end && adjust; ++start) { | ||
| 1981 | if (*KDB_WRAP(start) == '\n') | ||
| 1982 | --adjust; | ||
| 1983 | } | ||
| 1984 | if (start < end) | ||
| 1985 | ++start; | ||
| 1986 | } | ||
| 1987 | for (p = start; p < end && lines; ++p) { | ||
| 1988 | if (*KDB_WRAP(p) == '\n') | ||
| 1989 | ++lines; | ||
| 1990 | } | ||
| 1991 | end = p; | ||
| 1992 | } else if (lines > 0) { | ||
| 1993 | int skip = n - (adjust + lines); | ||
| 1994 | if (adjust >= n) { | ||
| 1995 | kdb_printf("buffer only contains %d lines, " | ||
| 1996 | "nothing printed\n", n); | ||
| 1997 | skip = n; | ||
| 1998 | } else if (skip < 0) { | ||
| 1999 | lines += skip; | ||
| 2000 | skip = 0; | ||
| 2001 | kdb_printf("buffer only contains %d lines, first " | ||
| 2002 | "%d lines printed\n", n, lines); | ||
| 2003 | } | ||
| 2004 | for (; start < end && skip; ++start) { | ||
| 2005 | if (*KDB_WRAP(start) == '\n') | ||
| 2006 | --skip; | ||
| 2007 | } | ||
| 2008 | for (p = start; p < end && lines; ++p) { | ||
| 2009 | if (*KDB_WRAP(p) == '\n') | ||
| 2010 | --lines; | ||
| 2011 | } | ||
| 2012 | end = p; | ||
| 2013 | } | ||
| 2014 | /* Do a line at a time (max 200 chars) to reduce protocol overhead */ | ||
| 2015 | c = '\n'; | ||
| 2016 | while (start != end) { | ||
| 2017 | char buf[201]; | ||
| 2018 | p = buf; | ||
| 2019 | if (KDB_FLAG(CMD_INTERRUPT)) | ||
| 2020 | return 0; | ||
| 2021 | while (start < end && (c = *KDB_WRAP(start)) && | ||
| 2022 | (p - buf) < sizeof(buf)-1) { | ||
| 2023 | ++start; | ||
| 2024 | *p++ = c; | ||
| 2025 | if (c == '\n') | ||
| 2026 | break; | ||
| 2027 | } | ||
| 2028 | *p = '\0'; | ||
| 2029 | kdb_printf("%s", buf); | ||
| 2030 | } | ||
| 2031 | if (c != '\n') | ||
| 2032 | kdb_printf("\n"); | ||
| 2033 | |||
| 2034 | return 0; | ||
| 2035 | } | ||
| 2036 | #endif /* CONFIG_PRINTK */ | ||
| 2037 | /* | ||
| 2038 | * kdb_cpu - This function implements the 'cpu' command. | ||
| 2039 | * cpu [<cpunum>] | ||
| 2040 | * Returns: | ||
| 2041 | * KDB_CMD_CPU for success, a kdb diagnostic if error | ||
| 2042 | */ | ||
| 2043 | static void kdb_cpu_status(void) | ||
| 2044 | { | ||
| 2045 | int i, start_cpu, first_print = 1; | ||
| 2046 | char state, prev_state = '?'; | ||
| 2047 | |||
| 2048 | kdb_printf("Currently on cpu %d\n", raw_smp_processor_id()); | ||
| 2049 | kdb_printf("Available cpus: "); | ||
| 2050 | for (start_cpu = -1, i = 0; i < NR_CPUS; i++) { | ||
| 2051 | if (!cpu_online(i)) { | ||
| 2052 | state = 'F'; /* cpu is offline */ | ||
| 2053 | } else { | ||
| 2054 | state = ' '; /* cpu is responding to kdb */ | ||
| 2055 | if (kdb_task_state_char(KDB_TSK(i)) == 'I') | ||
| 2056 | state = 'I'; /* idle task */ | ||
| 2057 | } | ||
| 2058 | if (state != prev_state) { | ||
| 2059 | if (prev_state != '?') { | ||
| 2060 | if (!first_print) | ||
| 2061 | kdb_printf(", "); | ||
| 2062 | first_print = 0; | ||
| 2063 | kdb_printf("%d", start_cpu); | ||
| 2064 | if (start_cpu < i-1) | ||
| 2065 | kdb_printf("-%d", i-1); | ||
| 2066 | if (prev_state != ' ') | ||
| 2067 | kdb_printf("(%c)", prev_state); | ||
| 2068 | } | ||
| 2069 | prev_state = state; | ||
| 2070 | start_cpu = i; | ||
| 2071 | } | ||
| 2072 | } | ||
| 2073 | /* print the trailing cpus, ignoring them if they are all offline */ | ||
| 2074 | if (prev_state != 'F') { | ||
| 2075 | if (!first_print) | ||
| 2076 | kdb_printf(", "); | ||
| 2077 | kdb_printf("%d", start_cpu); | ||
| 2078 | if (start_cpu < i-1) | ||
| 2079 | kdb_printf("-%d", i-1); | ||
| 2080 | if (prev_state != ' ') | ||
| 2081 | kdb_printf("(%c)", prev_state); | ||
| 2082 | } | ||
| 2083 | kdb_printf("\n"); | ||
| 2084 | } | ||
| 2085 | |||
| 2086 | static int kdb_cpu(int argc, const char **argv) | ||
| 2087 | { | ||
| 2088 | unsigned long cpunum; | ||
| 2089 | int diag; | ||
| 2090 | |||
| 2091 | if (argc == 0) { | ||
| 2092 | kdb_cpu_status(); | ||
| 2093 | return 0; | ||
| 2094 | } | ||
| 2095 | |||
| 2096 | if (argc != 1) | ||
| 2097 | return KDB_ARGCOUNT; | ||
| 2098 | |||
| 2099 | diag = kdbgetularg(argv[1], &cpunum); | ||
| 2100 | if (diag) | ||
| 2101 | return diag; | ||
| 2102 | |||
| 2103 | /* | ||
| 2104 | * Validate cpunum | ||
| 2105 | */ | ||
| 2106 | if ((cpunum > NR_CPUS) || !cpu_online(cpunum)) | ||
| 2107 | return KDB_BADCPUNUM; | ||
| 2108 | |||
| 2109 | dbg_switch_cpu = cpunum; | ||
| 2110 | |||
| 2111 | /* | ||
| 2112 | * Switch to other cpu | ||
| 2113 | */ | ||
| 2114 | return KDB_CMD_CPU; | ||
| 2115 | } | ||
| 2116 | |||
| 2117 | /* The user may not realize that ps/bta with no parameters does not print idle | ||
| 2118 | * or sleeping system daemon processes, so tell them how many were suppressed. | ||
| 2119 | */ | ||
| 2120 | void kdb_ps_suppressed(void) | ||
| 2121 | { | ||
| 2122 | int idle = 0, daemon = 0; | ||
| 2123 | unsigned long mask_I = kdb_task_state_string("I"), | ||
| 2124 | mask_M = kdb_task_state_string("M"); | ||
| 2125 | unsigned long cpu; | ||
| 2126 | const struct task_struct *p, *g; | ||
| 2127 | for_each_online_cpu(cpu) { | ||
| 2128 | p = kdb_curr_task(cpu); | ||
| 2129 | if (kdb_task_state(p, mask_I)) | ||
| 2130 | ++idle; | ||
| 2131 | } | ||
| 2132 | kdb_do_each_thread(g, p) { | ||
| 2133 | if (kdb_task_state(p, mask_M)) | ||
| 2134 | ++daemon; | ||
| 2135 | } kdb_while_each_thread(g, p); | ||
| 2136 | if (idle || daemon) { | ||
| 2137 | if (idle) | ||
| 2138 | kdb_printf("%d idle process%s (state I)%s\n", | ||
| 2139 | idle, idle == 1 ? "" : "es", | ||
| 2140 | daemon ? " and " : ""); | ||
| 2141 | if (daemon) | ||
| 2142 | kdb_printf("%d sleeping system daemon (state M) " | ||
| 2143 | "process%s", daemon, | ||
| 2144 | daemon == 1 ? "" : "es"); | ||
| 2145 | kdb_printf(" suppressed,\nuse 'ps A' to see all.\n"); | ||
| 2146 | } | ||
| 2147 | } | ||
| 2148 | |||
| 2149 | /* | ||
| 2150 | * kdb_ps - This function implements the 'ps' command which shows a | ||
| 2151 | * list of the active processes. | ||
| 2152 | * ps [DRSTCZEUIMA] All processes, optionally filtered by state | ||
| 2153 | */ | ||
| 2154 | void kdb_ps1(const struct task_struct *p) | ||
| 2155 | { | ||
| 2156 | int cpu; | ||
| 2157 | unsigned long tmp; | ||
| 2158 | |||
| 2159 | if (!p || probe_kernel_read(&tmp, (char *)p, sizeof(unsigned long))) | ||
| 2160 | return; | ||
| 2161 | |||
| 2162 | cpu = kdb_process_cpu(p); | ||
| 2163 | kdb_printf("0x%p %8d %8d %d %4d %c 0x%p %c%s\n", | ||
| 2164 | (void *)p, p->pid, p->parent->pid, | ||
| 2165 | kdb_task_has_cpu(p), kdb_process_cpu(p), | ||
| 2166 | kdb_task_state_char(p), | ||
| 2167 | (void *)(&p->thread), | ||
| 2168 | p == kdb_curr_task(raw_smp_processor_id()) ? '*' : ' ', | ||
| 2169 | p->comm); | ||
| 2170 | if (kdb_task_has_cpu(p)) { | ||
| 2171 | if (!KDB_TSK(cpu)) { | ||
| 2172 | kdb_printf(" Error: no saved data for this cpu\n"); | ||
| 2173 | } else { | ||
| 2174 | if (KDB_TSK(cpu) != p) | ||
| 2175 | kdb_printf(" Error: does not match running " | ||
| 2176 | "process table (0x%p)\n", KDB_TSK(cpu)); | ||
| 2177 | } | ||
| 2178 | } | ||
| 2179 | } | ||
| 2180 | |||
| 2181 | static int kdb_ps(int argc, const char **argv) | ||
| 2182 | { | ||
| 2183 | struct task_struct *g, *p; | ||
| 2184 | unsigned long mask, cpu; | ||
| 2185 | |||
| 2186 | if (argc == 0) | ||
| 2187 | kdb_ps_suppressed(); | ||
| 2188 | kdb_printf("%-*s Pid Parent [*] cpu State %-*s Command\n", | ||
| 2189 | (int)(2*sizeof(void *))+2, "Task Addr", | ||
| 2190 | (int)(2*sizeof(void *))+2, "Thread"); | ||
| 2191 | mask = kdb_task_state_string(argc ? argv[1] : NULL); | ||
| 2192 | /* Run the active tasks first */ | ||
| 2193 | for_each_online_cpu(cpu) { | ||
| 2194 | if (KDB_FLAG(CMD_INTERRUPT)) | ||
| 2195 | return 0; | ||
| 2196 | p = kdb_curr_task(cpu); | ||
| 2197 | if (kdb_task_state(p, mask)) | ||
| 2198 | kdb_ps1(p); | ||
| 2199 | } | ||
| 2200 | kdb_printf("\n"); | ||
| 2201 | /* Now the real tasks */ | ||
| 2202 | kdb_do_each_thread(g, p) { | ||
| 2203 | if (KDB_FLAG(CMD_INTERRUPT)) | ||
| 2204 | return 0; | ||
| 2205 | if (kdb_task_state(p, mask)) | ||
| 2206 | kdb_ps1(p); | ||
| 2207 | } kdb_while_each_thread(g, p); | ||
| 2208 | |||
| 2209 | return 0; | ||
| 2210 | } | ||
| 2211 | |||
| 2212 | /* | ||
| 2213 | * kdb_pid - This function implements the 'pid' command which switches | ||
| 2214 | * the currently active process. | ||
| 2215 | * pid [<pid> | R] | ||
| 2216 | */ | ||
| 2217 | static int kdb_pid(int argc, const char **argv) | ||
| 2218 | { | ||
| 2219 | struct task_struct *p; | ||
| 2220 | unsigned long val; | ||
| 2221 | int diag; | ||
| 2222 | |||
| 2223 | if (argc > 1) | ||
| 2224 | return KDB_ARGCOUNT; | ||
| 2225 | |||
| 2226 | if (argc) { | ||
| 2227 | if (strcmp(argv[1], "R") == 0) { | ||
| 2228 | p = KDB_TSK(kdb_initial_cpu); | ||
| 2229 | } else { | ||
| 2230 | diag = kdbgetularg(argv[1], &val); | ||
| 2231 | if (diag) | ||
| 2232 | return KDB_BADINT; | ||
| 2233 | |||
| 2234 | p = find_task_by_pid_ns((pid_t)val, &init_pid_ns); | ||
| 2235 | if (!p) { | ||
| 2236 | kdb_printf("No task with pid=%d\n", (pid_t)val); | ||
| 2237 | return 0; | ||
| 2238 | } | ||
| 2239 | } | ||
| 2240 | kdb_set_current_task(p); | ||
| 2241 | } | ||
| 2242 | kdb_printf("KDB current process is %s(pid=%d)\n", | ||
| 2243 | kdb_current_task->comm, | ||
| 2244 | kdb_current_task->pid); | ||
| 2245 | |||
| 2246 | return 0; | ||
| 2247 | } | ||
| 2248 | |||
| 2249 | /* | ||
| 2250 | * kdb_ll - This function implements the 'll' command which follows a | ||
| 2251 | * linked list and executes an arbitrary command for each | ||
| 2252 | * element. | ||
| 2253 | */ | ||
| 2254 | static int kdb_ll(int argc, const char **argv) | ||
| 2255 | { | ||
| 2256 | int diag; | ||
| 2257 | unsigned long addr; | ||
| 2258 | long offset = 0; | ||
| 2259 | unsigned long va; | ||
| 2260 | unsigned long linkoffset; | ||
| 2261 | int nextarg; | ||
| 2262 | const char *command; | ||
| 2263 | |||
| 2264 | if (argc != 3) | ||
| 2265 | return KDB_ARGCOUNT; | ||
| 2266 | |||
| 2267 | nextarg = 1; | ||
| 2268 | diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL); | ||
| 2269 | if (diag) | ||
| 2270 | return diag; | ||
| 2271 | |||
| 2272 | diag = kdbgetularg(argv[2], &linkoffset); | ||
| 2273 | if (diag) | ||
| 2274 | return diag; | ||
| 2275 | |||
| 2276 | /* | ||
| 2277 | * Using the starting address as | ||
| 2278 | * the first element in the list, and assuming that | ||
| 2279 | * the list ends with a null pointer. | ||
| 2280 | */ | ||
| 2281 | |||
| 2282 | va = addr; | ||
| 2283 | command = kdb_strdup(argv[3], GFP_KDB); | ||
| 2284 | if (!command) { | ||
| 2285 | kdb_printf("%s: cannot duplicate command\n", __func__); | ||
| 2286 | return 0; | ||
| 2287 | } | ||
| 2288 | /* Recursive use of kdb_parse, do not use argv after this point */ | ||
| 2289 | argv = NULL; | ||
| 2290 | |||
| 2291 | while (va) { | ||
| 2292 | char buf[80]; | ||
| 2293 | |||
| 2294 | if (KDB_FLAG(CMD_INTERRUPT)) | ||
| 2295 | return 0; | ||
| 2296 | |||
| 2297 | sprintf(buf, "%s " kdb_machreg_fmt "\n", command, va); | ||
| 2298 | diag = kdb_parse(buf); | ||
| 2299 | if (diag) | ||
| 2300 | return diag; | ||
| 2301 | |||
| 2302 | addr = va + linkoffset; | ||
| 2303 | if (kdb_getword(&va, addr, sizeof(va))) | ||
| 2304 | return 0; | ||
| 2305 | } | ||
| 2306 | kfree(command); | ||
| 2307 | |||
| 2308 | return 0; | ||
| 2309 | } | ||
| 2310 | |||
| 2311 | static int kdb_kgdb(int argc, const char **argv) | ||
| 2312 | { | ||
| 2313 | return KDB_CMD_KGDB; | ||
| 2314 | } | ||
| 2315 | |||
| 2316 | /* | ||
| 2317 | * kdb_help - This function implements the 'help' and '?' commands. | ||
| 2318 | */ | ||
| 2319 | static int kdb_help(int argc, const char **argv) | ||
| 2320 | { | ||
| 2321 | kdbtab_t *kt; | ||
| 2322 | int i; | ||
| 2323 | |||
| 2324 | kdb_printf("%-15.15s %-20.20s %s\n", "Command", "Usage", "Description"); | ||
| 2325 | kdb_printf("-----------------------------" | ||
| 2326 | "-----------------------------\n"); | ||
| 2327 | for_each_kdbcmd(kt, i) { | ||
| 2328 | if (kt->cmd_name) | ||
| 2329 | kdb_printf("%-15.15s %-20.20s %s\n", kt->cmd_name, | ||
| 2330 | kt->cmd_usage, kt->cmd_help); | ||
| 2331 | if (KDB_FLAG(CMD_INTERRUPT)) | ||
| 2332 | return 0; | ||
| 2333 | } | ||
| 2334 | return 0; | ||
| 2335 | } | ||
| 2336 | |||
| 2337 | /* | ||
| 2338 | * kdb_kill - This function implements the 'kill' commands. | ||
| 2339 | */ | ||
| 2340 | static int kdb_kill(int argc, const char **argv) | ||
| 2341 | { | ||
| 2342 | long sig, pid; | ||
| 2343 | char *endp; | ||
| 2344 | struct task_struct *p; | ||
| 2345 | struct siginfo info; | ||
| 2346 | |||
| 2347 | if (argc != 2) | ||
| 2348 | return KDB_ARGCOUNT; | ||
| 2349 | |||
| 2350 | sig = simple_strtol(argv[1], &endp, 0); | ||
| 2351 | if (*endp) | ||
| 2352 | return KDB_BADINT; | ||
| 2353 | if (sig >= 0) { | ||
| 2354 | kdb_printf("Invalid signal parameter.<-signal>\n"); | ||
| 2355 | return 0; | ||
| 2356 | } | ||
| 2357 | sig = -sig; | ||
| 2358 | |||
| 2359 | pid = simple_strtol(argv[2], &endp, 0); | ||
| 2360 | if (*endp) | ||
| 2361 | return KDB_BADINT; | ||
| 2362 | if (pid <= 0) { | ||
| 2363 | kdb_printf("Process ID must be large than 0.\n"); | ||
| 2364 | return 0; | ||
| 2365 | } | ||
| 2366 | |||
| 2367 | /* Find the process. */ | ||
| 2368 | p = find_task_by_pid_ns(pid, &init_pid_ns); | ||
| 2369 | if (!p) { | ||
| 2370 | kdb_printf("The specified process isn't found.\n"); | ||
| 2371 | return 0; | ||
| 2372 | } | ||
| 2373 | p = p->group_leader; | ||
| 2374 | info.si_signo = sig; | ||
| 2375 | info.si_errno = 0; | ||
| 2376 | info.si_code = SI_USER; | ||
| 2377 | info.si_pid = pid; /* same capabilities as process being signalled */ | ||
| 2378 | info.si_uid = 0; /* kdb has root authority */ | ||
| 2379 | kdb_send_sig_info(p, &info); | ||
| 2380 | return 0; | ||
| 2381 | } | ||
| 2382 | |||
| 2383 | struct kdb_tm { | ||
| 2384 | int tm_sec; /* seconds */ | ||
| 2385 | int tm_min; /* minutes */ | ||
| 2386 | int tm_hour; /* hours */ | ||
| 2387 | int tm_mday; /* day of the month */ | ||
| 2388 | int tm_mon; /* month */ | ||
| 2389 | int tm_year; /* year */ | ||
| 2390 | }; | ||
| 2391 | |||
| 2392 | static void kdb_gmtime(struct timespec *tv, struct kdb_tm *tm) | ||
| 2393 | { | ||
| 2394 | /* This will work from 1970-2099, 2100 is not a leap year */ | ||
| 2395 | static int mon_day[] = { 31, 29, 31, 30, 31, 30, 31, | ||
| 2396 | 31, 30, 31, 30, 31 }; | ||
| 2397 | memset(tm, 0, sizeof(*tm)); | ||
| 2398 | tm->tm_sec = tv->tv_sec % (24 * 60 * 60); | ||
| 2399 | tm->tm_mday = tv->tv_sec / (24 * 60 * 60) + | ||
| 2400 | (2 * 365 + 1); /* shift base from 1970 to 1968 */ | ||
| 2401 | tm->tm_min = tm->tm_sec / 60 % 60; | ||
| 2402 | tm->tm_hour = tm->tm_sec / 60 / 60; | ||
| 2403 | tm->tm_sec = tm->tm_sec % 60; | ||
| 2404 | tm->tm_year = 68 + 4*(tm->tm_mday / (4*365+1)); | ||
| 2405 | tm->tm_mday %= (4*365+1); | ||
| 2406 | mon_day[1] = 29; | ||
| 2407 | while (tm->tm_mday >= mon_day[tm->tm_mon]) { | ||
| 2408 | tm->tm_mday -= mon_day[tm->tm_mon]; | ||
| 2409 | if (++tm->tm_mon == 12) { | ||
| 2410 | tm->tm_mon = 0; | ||
| 2411 | ++tm->tm_year; | ||
| 2412 | mon_day[1] = 28; | ||
| 2413 | } | ||
| 2414 | } | ||
| 2415 | ++tm->tm_mday; | ||
| 2416 | } | ||
| 2417 | |||
| 2418 | /* | ||
| 2419 | * Most of this code has been lifted from kernel/timer.c::sys_sysinfo(). | ||
| 2420 | * I cannot call that code directly from kdb, it has an unconditional | ||
| 2421 | * cli()/sti() and calls routines that take locks which can stop the debugger. | ||
| 2422 | */ | ||
| 2423 | static void kdb_sysinfo(struct sysinfo *val) | ||
| 2424 | { | ||
| 2425 | struct timespec uptime; | ||
| 2426 | do_posix_clock_monotonic_gettime(&uptime); | ||
| 2427 | memset(val, 0, sizeof(*val)); | ||
| 2428 | val->uptime = uptime.tv_sec; | ||
| 2429 | val->loads[0] = avenrun[0]; | ||
| 2430 | val->loads[1] = avenrun[1]; | ||
| 2431 | val->loads[2] = avenrun[2]; | ||
| 2432 | val->procs = nr_threads-1; | ||
| 2433 | si_meminfo(val); | ||
| 2434 | |||
| 2435 | return; | ||
| 2436 | } | ||
| 2437 | |||
| 2438 | /* | ||
| 2439 | * kdb_summary - This function implements the 'summary' command. | ||
| 2440 | */ | ||
| 2441 | static int kdb_summary(int argc, const char **argv) | ||
| 2442 | { | ||
| 2443 | struct kdb_tm tm; | ||
| 2444 | struct sysinfo val; | ||
| 2445 | |||
| 2446 | if (argc) | ||
| 2447 | return KDB_ARGCOUNT; | ||
| 2448 | |||
| 2449 | kdb_printf("sysname %s\n", init_uts_ns.name.sysname); | ||
| 2450 | kdb_printf("release %s\n", init_uts_ns.name.release); | ||
| 2451 | kdb_printf("version %s\n", init_uts_ns.name.version); | ||
| 2452 | kdb_printf("machine %s\n", init_uts_ns.name.machine); | ||
| 2453 | kdb_printf("nodename %s\n", init_uts_ns.name.nodename); | ||
| 2454 | kdb_printf("domainname %s\n", init_uts_ns.name.domainname); | ||
| 2455 | kdb_printf("ccversion %s\n", __stringify(CCVERSION)); | ||
| 2456 | |||
| 2457 | kdb_gmtime(&xtime, &tm); | ||
| 2458 | kdb_printf("date %04d-%02d-%02d %02d:%02d:%02d " | ||
| 2459 | "tz_minuteswest %d\n", | ||
| 2460 | 1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday, | ||
| 2461 | tm.tm_hour, tm.tm_min, tm.tm_sec, | ||
| 2462 | sys_tz.tz_minuteswest); | ||
| 2463 | |||
| 2464 | kdb_sysinfo(&val); | ||
| 2465 | kdb_printf("uptime "); | ||
| 2466 | if (val.uptime > (24*60*60)) { | ||
| 2467 | int days = val.uptime / (24*60*60); | ||
| 2468 | val.uptime %= (24*60*60); | ||
| 2469 | kdb_printf("%d day%s ", days, days == 1 ? "" : "s"); | ||
| 2470 | } | ||
| 2471 | kdb_printf("%02ld:%02ld\n", val.uptime/(60*60), (val.uptime/60)%60); | ||
| 2472 | |||
| 2473 | /* lifted from fs/proc/proc_misc.c::loadavg_read_proc() */ | ||
| 2474 | |||
| 2475 | #define LOAD_INT(x) ((x) >> FSHIFT) | ||
| 2476 | #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) | ||
| 2477 | kdb_printf("load avg %ld.%02ld %ld.%02ld %ld.%02ld\n", | ||
| 2478 | LOAD_INT(val.loads[0]), LOAD_FRAC(val.loads[0]), | ||
| 2479 | LOAD_INT(val.loads[1]), LOAD_FRAC(val.loads[1]), | ||
| 2480 | LOAD_INT(val.loads[2]), LOAD_FRAC(val.loads[2])); | ||
| 2481 | #undef LOAD_INT | ||
| 2482 | #undef LOAD_FRAC | ||
| 2483 | /* Display in kilobytes */ | ||
| 2484 | #define K(x) ((x) << (PAGE_SHIFT - 10)) | ||
| 2485 | kdb_printf("\nMemTotal: %8lu kB\nMemFree: %8lu kB\n" | ||
| 2486 | "Buffers: %8lu kB\n", | ||
| 2487 | val.totalram, val.freeram, val.bufferram); | ||
| 2488 | return 0; | ||
| 2489 | } | ||
| 2490 | |||
| 2491 | /* | ||
| 2492 | * kdb_per_cpu - This function implements the 'per_cpu' command. | ||
| 2493 | */ | ||
| 2494 | static int kdb_per_cpu(int argc, const char **argv) | ||
| 2495 | { | ||
| 2496 | char buf[256], fmtstr[64]; | ||
| 2497 | kdb_symtab_t symtab; | ||
| 2498 | cpumask_t suppress = CPU_MASK_NONE; | ||
| 2499 | int cpu, diag; | ||
| 2500 | unsigned long addr, val, bytesperword = 0, whichcpu = ~0UL; | ||
| 2501 | |||
| 2502 | if (argc < 1 || argc > 3) | ||
| 2503 | return KDB_ARGCOUNT; | ||
| 2504 | |||
| 2505 | snprintf(buf, sizeof(buf), "per_cpu__%s", argv[1]); | ||
| 2506 | if (!kdbgetsymval(buf, &symtab)) { | ||
| 2507 | kdb_printf("%s is not a per_cpu variable\n", argv[1]); | ||
| 2508 | return KDB_BADADDR; | ||
| 2509 | } | ||
| 2510 | if (argc >= 2) { | ||
| 2511 | diag = kdbgetularg(argv[2], &bytesperword); | ||
| 2512 | if (diag) | ||
| 2513 | return diag; | ||
| 2514 | } | ||
| 2515 | if (!bytesperword) | ||
| 2516 | bytesperword = KDB_WORD_SIZE; | ||
| 2517 | else if (bytesperword > KDB_WORD_SIZE) | ||
| 2518 | return KDB_BADWIDTH; | ||
| 2519 | sprintf(fmtstr, "%%0%dlx ", (int)(2*bytesperword)); | ||
| 2520 | if (argc >= 3) { | ||
| 2521 | diag = kdbgetularg(argv[3], &whichcpu); | ||
| 2522 | if (diag) | ||
| 2523 | return diag; | ||
| 2524 | if (!cpu_online(whichcpu)) { | ||
| 2525 | kdb_printf("cpu %ld is not online\n", whichcpu); | ||
| 2526 | return KDB_BADCPUNUM; | ||
| 2527 | } | ||
| 2528 | } | ||
| 2529 | |||
| 2530 | /* Most architectures use __per_cpu_offset[cpu], some use | ||
| 2531 | * __per_cpu_offset(cpu), smp has no __per_cpu_offset. | ||
| 2532 | */ | ||
| 2533 | #ifdef __per_cpu_offset | ||
| 2534 | #define KDB_PCU(cpu) __per_cpu_offset(cpu) | ||
| 2535 | #else | ||
| 2536 | #ifdef CONFIG_SMP | ||
| 2537 | #define KDB_PCU(cpu) __per_cpu_offset[cpu] | ||
| 2538 | #else | ||
| 2539 | #define KDB_PCU(cpu) 0 | ||
| 2540 | #endif | ||
| 2541 | #endif | ||
| 2542 | |||
| 2543 | for_each_online_cpu(cpu) { | ||
| 2544 | if (whichcpu != ~0UL && whichcpu != cpu) | ||
| 2545 | continue; | ||
| 2546 | addr = symtab.sym_start + KDB_PCU(cpu); | ||
| 2547 | diag = kdb_getword(&val, addr, bytesperword); | ||
| 2548 | if (diag) { | ||
| 2549 | kdb_printf("%5d " kdb_bfd_vma_fmt0 " - unable to " | ||
| 2550 | "read, diag=%d\n", cpu, addr, diag); | ||
| 2551 | continue; | ||
| 2552 | } | ||
| 2553 | #ifdef CONFIG_SMP | ||
| 2554 | if (!val) { | ||
| 2555 | cpu_set(cpu, suppress); | ||
| 2556 | continue; | ||
| 2557 | } | ||
| 2558 | #endif /* CONFIG_SMP */ | ||
| 2559 | kdb_printf("%5d ", cpu); | ||
| 2560 | kdb_md_line(fmtstr, addr, | ||
| 2561 | bytesperword == KDB_WORD_SIZE, | ||
| 2562 | 1, bytesperword, 1, 1, 0); | ||
| 2563 | } | ||
| 2564 | if (cpus_weight(suppress) == 0) | ||
| 2565 | return 0; | ||
| 2566 | kdb_printf("Zero suppressed cpu(s):"); | ||
| 2567 | for (cpu = first_cpu(suppress); cpu < num_possible_cpus(); | ||
| 2568 | cpu = next_cpu(cpu, suppress)) { | ||
| 2569 | kdb_printf(" %d", cpu); | ||
| 2570 | if (cpu == num_possible_cpus() - 1 || | ||
| 2571 | next_cpu(cpu, suppress) != cpu + 1) | ||
| 2572 | continue; | ||
| 2573 | while (cpu < num_possible_cpus() && | ||
| 2574 | next_cpu(cpu, suppress) == cpu + 1) | ||
| 2575 | ++cpu; | ||
| 2576 | kdb_printf("-%d", cpu); | ||
| 2577 | } | ||
| 2578 | kdb_printf("\n"); | ||
| 2579 | |||
| 2580 | #undef KDB_PCU | ||
| 2581 | |||
| 2582 | return 0; | ||
| 2583 | } | ||
| 2584 | |||
| 2585 | /* | ||
| 2586 | * display help for the use of cmd | grep pattern | ||
| 2587 | */ | ||
| 2588 | static int kdb_grep_help(int argc, const char **argv) | ||
| 2589 | { | ||
| 2590 | kdb_printf("Usage of cmd args | grep pattern:\n"); | ||
| 2591 | kdb_printf(" Any command's output may be filtered through an "); | ||
| 2592 | kdb_printf("emulated 'pipe'.\n"); | ||
| 2593 | kdb_printf(" 'grep' is just a key word.\n"); | ||
| 2594 | kdb_printf(" The pattern may include a very limited set of " | ||
| 2595 | "metacharacters:\n"); | ||
| 2596 | kdb_printf(" pattern or ^pattern or pattern$ or ^pattern$\n"); | ||
| 2597 | kdb_printf(" And if there are spaces in the pattern, you may " | ||
| 2598 | "quote it:\n"); | ||
| 2599 | kdb_printf(" \"pat tern\" or \"^pat tern\" or \"pat tern$\"" | ||
| 2600 | " or \"^pat tern$\"\n"); | ||
| 2601 | return 0; | ||
| 2602 | } | ||
| 2603 | |||
| 2604 | /* | ||
| 2605 | * kdb_register_repeat - This function is used to register a kernel | ||
| 2606 | * debugger command. | ||
| 2607 | * Inputs: | ||
| 2608 | * cmd Command name | ||
| 2609 | * func Function to execute the command | ||
| 2610 | * usage A simple usage string showing arguments | ||
| 2611 | * help A simple help string describing command | ||
| 2612 | * repeat Does the command auto repeat on enter? | ||
| 2613 | * Returns: | ||
| 2614 | * zero for success, one if a duplicate command. | ||
| 2615 | */ | ||
| 2616 | #define kdb_command_extend 50 /* arbitrary */ | ||
| 2617 | int kdb_register_repeat(char *cmd, | ||
| 2618 | kdb_func_t func, | ||
| 2619 | char *usage, | ||
| 2620 | char *help, | ||
| 2621 | short minlen, | ||
| 2622 | kdb_repeat_t repeat) | ||
| 2623 | { | ||
| 2624 | int i; | ||
| 2625 | kdbtab_t *kp; | ||
| 2626 | |||
| 2627 | /* | ||
| 2628 | * Brute force method to determine duplicates | ||
| 2629 | */ | ||
| 2630 | for_each_kdbcmd(kp, i) { | ||
| 2631 | if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) { | ||
| 2632 | kdb_printf("Duplicate kdb command registered: " | ||
| 2633 | "%s, func %p help %s\n", cmd, func, help); | ||
| 2634 | return 1; | ||
| 2635 | } | ||
| 2636 | } | ||
| 2637 | |||
| 2638 | /* | ||
| 2639 | * Insert command into first available location in table | ||
| 2640 | */ | ||
| 2641 | for_each_kdbcmd(kp, i) { | ||
| 2642 | if (kp->cmd_name == NULL) | ||
| 2643 | break; | ||
| 2644 | } | ||
| 2645 | |||
| 2646 | if (i >= kdb_max_commands) { | ||
| 2647 | kdbtab_t *new = kmalloc((kdb_max_commands - KDB_BASE_CMD_MAX + | ||
| 2648 | kdb_command_extend) * sizeof(*new), GFP_KDB); | ||
| 2649 | if (!new) { | ||
| 2650 | kdb_printf("Could not allocate new kdb_command " | ||
| 2651 | "table\n"); | ||
| 2652 | return 1; | ||
| 2653 | } | ||
| 2654 | if (kdb_commands) { | ||
| 2655 | memcpy(new, kdb_commands, | ||
| 2656 | kdb_max_commands * sizeof(*new)); | ||
| 2657 | kfree(kdb_commands); | ||
| 2658 | } | ||
| 2659 | memset(new + kdb_max_commands, 0, | ||
| 2660 | kdb_command_extend * sizeof(*new)); | ||
| 2661 | kdb_commands = new; | ||
| 2662 | kp = kdb_commands + kdb_max_commands; | ||
| 2663 | kdb_max_commands += kdb_command_extend; | ||
| 2664 | } | ||
| 2665 | |||
| 2666 | kp->cmd_name = cmd; | ||
| 2667 | kp->cmd_func = func; | ||
| 2668 | kp->cmd_usage = usage; | ||
| 2669 | kp->cmd_help = help; | ||
| 2670 | kp->cmd_flags = 0; | ||
| 2671 | kp->cmd_minlen = minlen; | ||
| 2672 | kp->cmd_repeat = repeat; | ||
| 2673 | |||
| 2674 | return 0; | ||
| 2675 | } | ||
| 2676 | |||
| 2677 | /* | ||
| 2678 | * kdb_register - Compatibility register function for commands that do | ||
| 2679 | * not need to specify a repeat state. Equivalent to | ||
| 2680 | * kdb_register_repeat with KDB_REPEAT_NONE. | ||
| 2681 | * Inputs: | ||
| 2682 | * cmd Command name | ||
| 2683 | * func Function to execute the command | ||
| 2684 | * usage A simple usage string showing arguments | ||
| 2685 | * help A simple help string describing command | ||
| 2686 | * Returns: | ||
| 2687 | * zero for success, one if a duplicate command. | ||
| 2688 | */ | ||
| 2689 | int kdb_register(char *cmd, | ||
| 2690 | kdb_func_t func, | ||
| 2691 | char *usage, | ||
| 2692 | char *help, | ||
| 2693 | short minlen) | ||
| 2694 | { | ||
| 2695 | return kdb_register_repeat(cmd, func, usage, help, minlen, | ||
| 2696 | KDB_REPEAT_NONE); | ||
| 2697 | } | ||
| 2698 | |||
| 2699 | /* | ||
| 2700 | * kdb_unregister - This function is used to unregister a kernel | ||
| 2701 | * debugger command. It is generally called when a module which | ||
| 2702 | * implements kdb commands is unloaded. | ||
| 2703 | * Inputs: | ||
| 2704 | * cmd Command name | ||
| 2705 | * Returns: | ||
| 2706 | * zero for success, one command not registered. | ||
| 2707 | */ | ||
| 2708 | int kdb_unregister(char *cmd) | ||
| 2709 | { | ||
| 2710 | int i; | ||
| 2711 | kdbtab_t *kp; | ||
| 2712 | |||
| 2713 | /* | ||
| 2714 | * find the command. | ||
| 2715 | */ | ||
| 2716 | for (i = 0, kp = kdb_commands; i < kdb_max_commands; i++, kp++) { | ||
| 2717 | if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) { | ||
| 2718 | kp->cmd_name = NULL; | ||
| 2719 | return 0; | ||
| 2720 | } | ||
| 2721 | } | ||
| 2722 | |||
| 2723 | /* Couldn't find it. */ | ||
| 2724 | return 1; | ||
| 2725 | } | ||
| 2726 | |||
| 2727 | /* Initialize the kdb command table. */ | ||
| 2728 | static void __init kdb_inittab(void) | ||
| 2729 | { | ||
| 2730 | int i; | ||
| 2731 | kdbtab_t *kp; | ||
| 2732 | |||
| 2733 | for_each_kdbcmd(kp, i) | ||
| 2734 | kp->cmd_name = NULL; | ||
| 2735 | |||
| 2736 | kdb_register_repeat("md", kdb_md, "<vaddr>", | ||
| 2737 | "Display Memory Contents, also mdWcN, e.g. md8c1", 1, | ||
| 2738 | KDB_REPEAT_NO_ARGS); | ||
| 2739 | kdb_register_repeat("mdr", kdb_md, "<vaddr> <bytes>", | ||
| 2740 | "Display Raw Memory", 0, KDB_REPEAT_NO_ARGS); | ||
| 2741 | kdb_register_repeat("mdp", kdb_md, "<paddr> <bytes>", | ||
| 2742 | "Display Physical Memory", 0, KDB_REPEAT_NO_ARGS); | ||
| 2743 | kdb_register_repeat("mds", kdb_md, "<vaddr>", | ||
| 2744 | "Display Memory Symbolically", 0, KDB_REPEAT_NO_ARGS); | ||
| 2745 | kdb_register_repeat("mm", kdb_mm, "<vaddr> <contents>", | ||
| 2746 | "Modify Memory Contents", 0, KDB_REPEAT_NO_ARGS); | ||
| 2747 | kdb_register_repeat("go", kdb_go, "[<vaddr>]", | ||
| 2748 | "Continue Execution", 1, KDB_REPEAT_NONE); | ||
| 2749 | kdb_register_repeat("rd", kdb_rd, "", | ||
| 2750 | "Display Registers", 0, KDB_REPEAT_NONE); | ||
| 2751 | kdb_register_repeat("rm", kdb_rm, "<reg> <contents>", | ||
| 2752 | "Modify Registers", 0, KDB_REPEAT_NONE); | ||
| 2753 | kdb_register_repeat("ef", kdb_ef, "<vaddr>", | ||
| 2754 | "Display exception frame", 0, KDB_REPEAT_NONE); | ||
| 2755 | kdb_register_repeat("bt", kdb_bt, "[<vaddr>]", | ||
| 2756 | "Stack traceback", 1, KDB_REPEAT_NONE); | ||
| 2757 | kdb_register_repeat("btp", kdb_bt, "<pid>", | ||
| 2758 | "Display stack for process <pid>", 0, KDB_REPEAT_NONE); | ||
| 2759 | kdb_register_repeat("bta", kdb_bt, "[DRSTCZEUIMA]", | ||
| 2760 | "Display stack all processes", 0, KDB_REPEAT_NONE); | ||
| 2761 | kdb_register_repeat("btc", kdb_bt, "", | ||
| 2762 | "Backtrace current process on each cpu", 0, KDB_REPEAT_NONE); | ||
| 2763 | kdb_register_repeat("btt", kdb_bt, "<vaddr>", | ||
| 2764 | "Backtrace process given its struct task address", 0, | ||
| 2765 | KDB_REPEAT_NONE); | ||
| 2766 | kdb_register_repeat("ll", kdb_ll, "<first-element> <linkoffset> <cmd>", | ||
| 2767 | "Execute cmd for each element in linked list", 0, KDB_REPEAT_NONE); | ||
| 2768 | kdb_register_repeat("env", kdb_env, "", | ||
| 2769 | "Show environment variables", 0, KDB_REPEAT_NONE); | ||
| 2770 | kdb_register_repeat("set", kdb_set, "", | ||
| 2771 | "Set environment variables", 0, KDB_REPEAT_NONE); | ||
| 2772 | kdb_register_repeat("help", kdb_help, "", | ||
| 2773 | "Display Help Message", 1, KDB_REPEAT_NONE); | ||
| 2774 | kdb_register_repeat("?", kdb_help, "", | ||
| 2775 | "Display Help Message", 0, KDB_REPEAT_NONE); | ||
| 2776 | kdb_register_repeat("cpu", kdb_cpu, "<cpunum>", | ||
| 2777 | "Switch to new cpu", 0, KDB_REPEAT_NONE); | ||
| 2778 | kdb_register_repeat("kgdb", kdb_kgdb, "", | ||
| 2779 | "Enter kgdb mode", 0, KDB_REPEAT_NONE); | ||
| 2780 | kdb_register_repeat("ps", kdb_ps, "[<flags>|A]", | ||
| 2781 | "Display active task list", 0, KDB_REPEAT_NONE); | ||
| 2782 | kdb_register_repeat("pid", kdb_pid, "<pidnum>", | ||
| 2783 | "Switch to another task", 0, KDB_REPEAT_NONE); | ||
| 2784 | kdb_register_repeat("reboot", kdb_reboot, "", | ||
| 2785 | "Reboot the machine immediately", 0, KDB_REPEAT_NONE); | ||
| 2786 | #if defined(CONFIG_MODULES) | ||
| 2787 | kdb_register_repeat("lsmod", kdb_lsmod, "", | ||
| 2788 | "List loaded kernel modules", 0, KDB_REPEAT_NONE); | ||
| 2789 | #endif | ||
| 2790 | #if defined(CONFIG_MAGIC_SYSRQ) | ||
| 2791 | kdb_register_repeat("sr", kdb_sr, "<key>", | ||
| 2792 | "Magic SysRq key", 0, KDB_REPEAT_NONE); | ||
| 2793 | #endif | ||
| 2794 | #if defined(CONFIG_PRINTK) | ||
| 2795 | kdb_register_repeat("dmesg", kdb_dmesg, "[lines]", | ||
| 2796 | "Display syslog buffer", 0, KDB_REPEAT_NONE); | ||
| 2797 | #endif | ||
| 2798 | kdb_register_repeat("defcmd", kdb_defcmd, "name \"usage\" \"help\"", | ||
| 2799 | "Define a set of commands, down to endefcmd", 0, KDB_REPEAT_NONE); | ||
| 2800 | kdb_register_repeat("kill", kdb_kill, "<-signal> <pid>", | ||
| 2801 | "Send a signal to a process", 0, KDB_REPEAT_NONE); | ||
| 2802 | kdb_register_repeat("summary", kdb_summary, "", | ||
| 2803 | "Summarize the system", 4, KDB_REPEAT_NONE); | ||
| 2804 | kdb_register_repeat("per_cpu", kdb_per_cpu, "", | ||
| 2805 | "Display per_cpu variables", 3, KDB_REPEAT_NONE); | ||
| 2806 | kdb_register_repeat("grephelp", kdb_grep_help, "", | ||
| 2807 | "Display help on | grep", 0, KDB_REPEAT_NONE); | ||
| 2808 | } | ||
| 2809 | |||
| 2810 | /* Execute any commands defined in kdb_cmds. */ | ||
| 2811 | static void __init kdb_cmd_init(void) | ||
| 2812 | { | ||
| 2813 | int i, diag; | ||
| 2814 | for (i = 0; kdb_cmds[i]; ++i) { | ||
| 2815 | diag = kdb_parse(kdb_cmds[i]); | ||
| 2816 | if (diag) | ||
| 2817 | kdb_printf("kdb command %s failed, kdb diag %d\n", | ||
| 2818 | kdb_cmds[i], diag); | ||
| 2819 | } | ||
| 2820 | if (defcmd_in_progress) { | ||
| 2821 | kdb_printf("Incomplete 'defcmd' set, forcing endefcmd\n"); | ||
| 2822 | kdb_parse("endefcmd"); | ||
| 2823 | } | ||
| 2824 | } | ||
| 2825 | |||
| 2826 | /* Intialize kdb_printf, breakpoint tables and kdb state */ | ||
| 2827 | void __init kdb_init(int lvl) | ||
| 2828 | { | ||
| 2829 | static int kdb_init_lvl = KDB_NOT_INITIALIZED; | ||
| 2830 | int i; | ||
| 2831 | |||
| 2832 | if (kdb_init_lvl == KDB_INIT_FULL || lvl <= kdb_init_lvl) | ||
| 2833 | return; | ||
| 2834 | for (i = kdb_init_lvl; i < lvl; i++) { | ||
| 2835 | switch (i) { | ||
| 2836 | case KDB_NOT_INITIALIZED: | ||
| 2837 | kdb_inittab(); /* Initialize Command Table */ | ||
| 2838 | kdb_initbptab(); /* Initialize Breakpoints */ | ||
| 2839 | break; | ||
| 2840 | case KDB_INIT_EARLY: | ||
| 2841 | kdb_cmd_init(); /* Build kdb_cmds tables */ | ||
| 2842 | break; | ||
| 2843 | } | ||
| 2844 | } | ||
| 2845 | kdb_init_lvl = lvl; | ||
| 2846 | } | ||
diff --git a/kernel/debug/kdb/kdb_private.h b/kernel/debug/kdb/kdb_private.h new file mode 100644 index 000000000000..97d3ba69775d --- /dev/null +++ b/kernel/debug/kdb/kdb_private.h | |||
| @@ -0,0 +1,300 @@ | |||
| 1 | #ifndef _KDBPRIVATE_H | ||
| 2 | #define _KDBPRIVATE_H | ||
| 3 | |||
| 4 | /* | ||
| 5 | * Kernel Debugger Architecture Independent Private Headers | ||
| 6 | * | ||
| 7 | * This file is subject to the terms and conditions of the GNU General Public | ||
| 8 | * License. See the file "COPYING" in the main directory of this archive | ||
| 9 | * for more details. | ||
| 10 | * | ||
| 11 | * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved. | ||
| 12 | * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. | ||
| 13 | */ | ||
| 14 | |||
| 15 | #include <linux/kgdb.h> | ||
| 16 | #include "../debug_core.h" | ||
| 17 | |||
| 18 | /* Kernel Debugger Error codes. Must not overlap with command codes. */ | ||
| 19 | #define KDB_NOTFOUND (-1) | ||
| 20 | #define KDB_ARGCOUNT (-2) | ||
| 21 | #define KDB_BADWIDTH (-3) | ||
| 22 | #define KDB_BADRADIX (-4) | ||
| 23 | #define KDB_NOTENV (-5) | ||
| 24 | #define KDB_NOENVVALUE (-6) | ||
| 25 | #define KDB_NOTIMP (-7) | ||
| 26 | #define KDB_ENVFULL (-8) | ||
| 27 | #define KDB_ENVBUFFULL (-9) | ||
| 28 | #define KDB_TOOMANYBPT (-10) | ||
| 29 | #define KDB_TOOMANYDBREGS (-11) | ||
| 30 | #define KDB_DUPBPT (-12) | ||
| 31 | #define KDB_BPTNOTFOUND (-13) | ||
| 32 | #define KDB_BADMODE (-14) | ||
| 33 | #define KDB_BADINT (-15) | ||
| 34 | #define KDB_INVADDRFMT (-16) | ||
| 35 | #define KDB_BADREG (-17) | ||
| 36 | #define KDB_BADCPUNUM (-18) | ||
| 37 | #define KDB_BADLENGTH (-19) | ||
| 38 | #define KDB_NOBP (-20) | ||
| 39 | #define KDB_BADADDR (-21) | ||
| 40 | |||
| 41 | /* Kernel Debugger Command codes. Must not overlap with error codes. */ | ||
| 42 | #define KDB_CMD_GO (-1001) | ||
| 43 | #define KDB_CMD_CPU (-1002) | ||
| 44 | #define KDB_CMD_SS (-1003) | ||
| 45 | #define KDB_CMD_SSB (-1004) | ||
| 46 | #define KDB_CMD_KGDB (-1005) | ||
| 47 | #define KDB_CMD_KGDB2 (-1006) | ||
| 48 | |||
| 49 | /* Internal debug flags */ | ||
| 50 | #define KDB_DEBUG_FLAG_BP 0x0002 /* Breakpoint subsystem debug */ | ||
| 51 | #define KDB_DEBUG_FLAG_BB_SUMM 0x0004 /* Basic block analysis, summary only */ | ||
| 52 | #define KDB_DEBUG_FLAG_AR 0x0008 /* Activation record, generic */ | ||
| 53 | #define KDB_DEBUG_FLAG_ARA 0x0010 /* Activation record, arch specific */ | ||
| 54 | #define KDB_DEBUG_FLAG_BB 0x0020 /* All basic block analysis */ | ||
| 55 | #define KDB_DEBUG_FLAG_STATE 0x0040 /* State flags */ | ||
| 56 | #define KDB_DEBUG_FLAG_MASK 0xffff /* All debug flags */ | ||
| 57 | #define KDB_DEBUG_FLAG_SHIFT 16 /* Shift factor for dbflags */ | ||
| 58 | |||
| 59 | #define KDB_DEBUG(flag) (kdb_flags & \ | ||
| 60 | (KDB_DEBUG_FLAG_##flag << KDB_DEBUG_FLAG_SHIFT)) | ||
| 61 | #define KDB_DEBUG_STATE(text, value) if (KDB_DEBUG(STATE)) \ | ||
| 62 | kdb_print_state(text, value) | ||
| 63 | |||
| 64 | #if BITS_PER_LONG == 32 | ||
| 65 | |||
| 66 | #define KDB_PLATFORM_ENV "BYTESPERWORD=4" | ||
| 67 | |||
| 68 | #define kdb_machreg_fmt "0x%lx" | ||
| 69 | #define kdb_machreg_fmt0 "0x%08lx" | ||
| 70 | #define kdb_bfd_vma_fmt "0x%lx" | ||
| 71 | #define kdb_bfd_vma_fmt0 "0x%08lx" | ||
| 72 | #define kdb_elfw_addr_fmt "0x%x" | ||
| 73 | #define kdb_elfw_addr_fmt0 "0x%08x" | ||
| 74 | #define kdb_f_count_fmt "%d" | ||
| 75 | |||
| 76 | #elif BITS_PER_LONG == 64 | ||
| 77 | |||
| 78 | #define KDB_PLATFORM_ENV "BYTESPERWORD=8" | ||
| 79 | |||
| 80 | #define kdb_machreg_fmt "0x%lx" | ||
| 81 | #define kdb_machreg_fmt0 "0x%016lx" | ||
| 82 | #define kdb_bfd_vma_fmt "0x%lx" | ||
| 83 | #define kdb_bfd_vma_fmt0 "0x%016lx" | ||
| 84 | #define kdb_elfw_addr_fmt "0x%x" | ||
| 85 | #define kdb_elfw_addr_fmt0 "0x%016x" | ||
| 86 | #define kdb_f_count_fmt "%ld" | ||
| 87 | |||
| 88 | #endif | ||
| 89 | |||
| 90 | /* | ||
| 91 | * KDB_MAXBPT describes the total number of breakpoints | ||
| 92 | * supported by this architecure. | ||
| 93 | */ | ||
| 94 | #define KDB_MAXBPT 16 | ||
| 95 | |||
| 96 | /* Maximum number of arguments to a function */ | ||
| 97 | #define KDB_MAXARGS 16 | ||
| 98 | |||
| 99 | typedef enum { | ||
| 100 | KDB_REPEAT_NONE = 0, /* Do not repeat this command */ | ||
| 101 | KDB_REPEAT_NO_ARGS, /* Repeat the command without arguments */ | ||
| 102 | KDB_REPEAT_WITH_ARGS, /* Repeat the command including its arguments */ | ||
| 103 | } kdb_repeat_t; | ||
| 104 | |||
| 105 | typedef int (*kdb_func_t)(int, const char **); | ||
| 106 | |||
| 107 | /* Symbol table format returned by kallsyms. */ | ||
| 108 | typedef struct __ksymtab { | ||
| 109 | unsigned long value; /* Address of symbol */ | ||
| 110 | const char *mod_name; /* Module containing symbol or | ||
| 111 | * "kernel" */ | ||
| 112 | unsigned long mod_start; | ||
| 113 | unsigned long mod_end; | ||
| 114 | const char *sec_name; /* Section containing symbol */ | ||
| 115 | unsigned long sec_start; | ||
| 116 | unsigned long sec_end; | ||
| 117 | const char *sym_name; /* Full symbol name, including | ||
| 118 | * any version */ | ||
| 119 | unsigned long sym_start; | ||
| 120 | unsigned long sym_end; | ||
| 121 | } kdb_symtab_t; | ||
| 122 | extern int kallsyms_symbol_next(char *prefix_name, int flag); | ||
| 123 | extern int kallsyms_symbol_complete(char *prefix_name, int max_len); | ||
| 124 | |||
| 125 | /* Exported Symbols for kernel loadable modules to use. */ | ||
| 126 | extern int kdb_register(char *, kdb_func_t, char *, char *, short); | ||
| 127 | extern int kdb_register_repeat(char *, kdb_func_t, char *, char *, | ||
| 128 | short, kdb_repeat_t); | ||
| 129 | extern int kdb_unregister(char *); | ||
| 130 | |||
| 131 | extern int kdb_getarea_size(void *, unsigned long, size_t); | ||
| 132 | extern int kdb_putarea_size(unsigned long, void *, size_t); | ||
| 133 | |||
| 134 | /* | ||
| 135 | * Like get_user and put_user, kdb_getarea and kdb_putarea take variable | ||
| 136 | * names, not pointers. The underlying *_size functions take pointers. | ||
| 137 | */ | ||
| 138 | #define kdb_getarea(x, addr) kdb_getarea_size(&(x), addr, sizeof((x))) | ||
| 139 | #define kdb_putarea(addr, x) kdb_putarea_size(addr, &(x), sizeof((x))) | ||
| 140 | |||
| 141 | extern int kdb_getphysword(unsigned long *word, | ||
| 142 | unsigned long addr, size_t size); | ||
| 143 | extern int kdb_getword(unsigned long *, unsigned long, size_t); | ||
| 144 | extern int kdb_putword(unsigned long, unsigned long, size_t); | ||
| 145 | |||
| 146 | extern int kdbgetularg(const char *, unsigned long *); | ||
| 147 | extern int kdb_set(int, const char **); | ||
| 148 | extern char *kdbgetenv(const char *); | ||
| 149 | extern int kdbgetintenv(const char *, int *); | ||
| 150 | extern int kdbgetaddrarg(int, const char **, int*, unsigned long *, | ||
| 151 | long *, char **); | ||
| 152 | extern int kdbgetsymval(const char *, kdb_symtab_t *); | ||
| 153 | extern int kdbnearsym(unsigned long, kdb_symtab_t *); | ||
| 154 | extern void kdbnearsym_cleanup(void); | ||
| 155 | extern char *kdb_strdup(const char *str, gfp_t type); | ||
| 156 | extern void kdb_symbol_print(unsigned long, const kdb_symtab_t *, unsigned int); | ||
| 157 | |||
| 158 | /* Routine for debugging the debugger state. */ | ||
| 159 | extern void kdb_print_state(const char *, int); | ||
| 160 | |||
| 161 | extern int kdb_state; | ||
| 162 | #define KDB_STATE_KDB 0x00000001 /* Cpu is inside kdb */ | ||
| 163 | #define KDB_STATE_LEAVING 0x00000002 /* Cpu is leaving kdb */ | ||
| 164 | #define KDB_STATE_CMD 0x00000004 /* Running a kdb command */ | ||
| 165 | #define KDB_STATE_KDB_CONTROL 0x00000008 /* This cpu is under | ||
| 166 | * kdb control */ | ||
| 167 | #define KDB_STATE_HOLD_CPU 0x00000010 /* Hold this cpu inside kdb */ | ||
| 168 | #define KDB_STATE_DOING_SS 0x00000020 /* Doing ss command */ | ||
| 169 | #define KDB_STATE_DOING_SSB 0x00000040 /* Doing ssb command, | ||
| 170 | * DOING_SS is also set */ | ||
| 171 | #define KDB_STATE_SSBPT 0x00000080 /* Install breakpoint | ||
| 172 | * after one ss, independent of | ||
| 173 | * DOING_SS */ | ||
| 174 | #define KDB_STATE_REENTRY 0x00000100 /* Valid re-entry into kdb */ | ||
| 175 | #define KDB_STATE_SUPPRESS 0x00000200 /* Suppress error messages */ | ||
| 176 | #define KDB_STATE_PAGER 0x00000400 /* pager is available */ | ||
| 177 | #define KDB_STATE_GO_SWITCH 0x00000800 /* go is switching | ||
| 178 | * back to initial cpu */ | ||
| 179 | #define KDB_STATE_PRINTF_LOCK 0x00001000 /* Holds kdb_printf lock */ | ||
| 180 | #define KDB_STATE_WAIT_IPI 0x00002000 /* Waiting for kdb_ipi() NMI */ | ||
| 181 | #define KDB_STATE_RECURSE 0x00004000 /* Recursive entry to kdb */ | ||
| 182 | #define KDB_STATE_IP_ADJUSTED 0x00008000 /* Restart IP has been | ||
| 183 | * adjusted */ | ||
| 184 | #define KDB_STATE_GO1 0x00010000 /* go only releases one cpu */ | ||
| 185 | #define KDB_STATE_KEYBOARD 0x00020000 /* kdb entered via | ||
| 186 | * keyboard on this cpu */ | ||
| 187 | #define KDB_STATE_KEXEC 0x00040000 /* kexec issued */ | ||
| 188 | #define KDB_STATE_DOING_KGDB 0x00080000 /* kgdb enter now issued */ | ||
| 189 | #define KDB_STATE_DOING_KGDB2 0x00100000 /* kgdb enter now issued */ | ||
| 190 | #define KDB_STATE_KGDB_TRANS 0x00200000 /* Transition to kgdb */ | ||
| 191 | #define KDB_STATE_ARCH 0xff000000 /* Reserved for arch | ||
| 192 | * specific use */ | ||
| 193 | |||
| 194 | #define KDB_STATE(flag) (kdb_state & KDB_STATE_##flag) | ||
| 195 | #define KDB_STATE_SET(flag) ((void)(kdb_state |= KDB_STATE_##flag)) | ||
| 196 | #define KDB_STATE_CLEAR(flag) ((void)(kdb_state &= ~KDB_STATE_##flag)) | ||
| 197 | |||
| 198 | extern int kdb_nextline; /* Current number of lines displayed */ | ||
| 199 | |||
| 200 | typedef struct _kdb_bp { | ||
| 201 | unsigned long bp_addr; /* Address breakpoint is present at */ | ||
| 202 | unsigned int bp_free:1; /* This entry is available */ | ||
| 203 | unsigned int bp_enabled:1; /* Breakpoint is active in register */ | ||
| 204 | unsigned int bp_type:4; /* Uses hardware register */ | ||
| 205 | unsigned int bp_installed:1; /* Breakpoint is installed */ | ||
| 206 | unsigned int bp_delay:1; /* Do delayed bp handling */ | ||
| 207 | unsigned int bp_delayed:1; /* Delayed breakpoint */ | ||
| 208 | unsigned int bph_length; /* HW break length */ | ||
| 209 | } kdb_bp_t; | ||
| 210 | |||
| 211 | #ifdef CONFIG_KGDB_KDB | ||
| 212 | extern kdb_bp_t kdb_breakpoints[/* KDB_MAXBPT */]; | ||
| 213 | |||
| 214 | /* The KDB shell command table */ | ||
| 215 | typedef struct _kdbtab { | ||
| 216 | char *cmd_name; /* Command name */ | ||
| 217 | kdb_func_t cmd_func; /* Function to execute command */ | ||
| 218 | char *cmd_usage; /* Usage String for this command */ | ||
| 219 | char *cmd_help; /* Help message for this command */ | ||
| 220 | short cmd_flags; /* Parsing flags */ | ||
| 221 | short cmd_minlen; /* Minimum legal # command | ||
| 222 | * chars required */ | ||
| 223 | kdb_repeat_t cmd_repeat; /* Does command auto repeat on enter? */ | ||
| 224 | } kdbtab_t; | ||
| 225 | |||
| 226 | extern int kdb_bt(int, const char **); /* KDB display back trace */ | ||
| 227 | |||
| 228 | /* KDB breakpoint management functions */ | ||
| 229 | extern void kdb_initbptab(void); | ||
| 230 | extern void kdb_bp_install(struct pt_regs *); | ||
| 231 | extern void kdb_bp_remove(void); | ||
| 232 | |||
| 233 | typedef enum { | ||
| 234 | KDB_DB_BPT, /* Breakpoint */ | ||
| 235 | KDB_DB_SS, /* Single-step trap */ | ||
| 236 | KDB_DB_SSB, /* Single step to branch */ | ||
| 237 | KDB_DB_SSBPT, /* Single step over breakpoint */ | ||
| 238 | KDB_DB_NOBPT /* Spurious breakpoint */ | ||
| 239 | } kdb_dbtrap_t; | ||
| 240 | |||
| 241 | extern int kdb_main_loop(kdb_reason_t, kdb_reason_t, | ||
| 242 | int, kdb_dbtrap_t, struct pt_regs *); | ||
| 243 | |||
| 244 | /* Miscellaneous functions and data areas */ | ||
| 245 | extern int kdb_grepping_flag; | ||
| 246 | extern char kdb_grep_string[]; | ||
| 247 | extern int kdb_grep_leading; | ||
| 248 | extern int kdb_grep_trailing; | ||
| 249 | extern char *kdb_cmds[]; | ||
| 250 | extern void kdb_syslog_data(char *syslog_data[]); | ||
| 251 | extern unsigned long kdb_task_state_string(const char *); | ||
| 252 | extern char kdb_task_state_char (const struct task_struct *); | ||
| 253 | extern unsigned long kdb_task_state(const struct task_struct *p, | ||
| 254 | unsigned long mask); | ||
| 255 | extern void kdb_ps_suppressed(void); | ||
| 256 | extern void kdb_ps1(const struct task_struct *p); | ||
| 257 | extern void kdb_print_nameval(const char *name, unsigned long val); | ||
| 258 | extern void kdb_send_sig_info(struct task_struct *p, struct siginfo *info); | ||
| 259 | extern void kdb_meminfo_proc_show(void); | ||
| 260 | extern const char *kdb_walk_kallsyms(loff_t *pos); | ||
| 261 | extern char *kdb_getstr(char *, size_t, char *); | ||
| 262 | |||
| 263 | /* Defines for kdb_symbol_print */ | ||
| 264 | #define KDB_SP_SPACEB 0x0001 /* Space before string */ | ||
| 265 | #define KDB_SP_SPACEA 0x0002 /* Space after string */ | ||
| 266 | #define KDB_SP_PAREN 0x0004 /* Parenthesis around string */ | ||
| 267 | #define KDB_SP_VALUE 0x0008 /* Print the value of the address */ | ||
| 268 | #define KDB_SP_SYMSIZE 0x0010 /* Print the size of the symbol */ | ||
| 269 | #define KDB_SP_NEWLINE 0x0020 /* Newline after string */ | ||
| 270 | #define KDB_SP_DEFAULT (KDB_SP_VALUE|KDB_SP_PAREN) | ||
| 271 | |||
| 272 | #define KDB_TSK(cpu) kgdb_info[cpu].task | ||
| 273 | #define KDB_TSKREGS(cpu) kgdb_info[cpu].debuggerinfo | ||
| 274 | |||
| 275 | extern struct task_struct *kdb_curr_task(int); | ||
| 276 | |||
| 277 | #define kdb_task_has_cpu(p) (task_curr(p)) | ||
| 278 | |||
| 279 | /* Simplify coexistence with NPTL */ | ||
| 280 | #define kdb_do_each_thread(g, p) do_each_thread(g, p) | ||
| 281 | #define kdb_while_each_thread(g, p) while_each_thread(g, p) | ||
| 282 | |||
| 283 | #define GFP_KDB (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL) | ||
| 284 | |||
| 285 | extern void *debug_kmalloc(size_t size, gfp_t flags); | ||
| 286 | extern void debug_kfree(void *); | ||
| 287 | extern void debug_kusage(void); | ||
| 288 | |||
| 289 | extern void kdb_set_current_task(struct task_struct *); | ||
| 290 | extern struct task_struct *kdb_current_task; | ||
| 291 | #ifdef CONFIG_MODULES | ||
| 292 | extern struct list_head *kdb_modules; | ||
| 293 | #endif /* CONFIG_MODULES */ | ||
| 294 | |||
| 295 | extern char kdb_prompt_str[]; | ||
| 296 | |||
| 297 | #define KDB_WORD_SIZE ((int)sizeof(unsigned long)) | ||
| 298 | |||
| 299 | #endif /* CONFIG_KGDB_KDB */ | ||
| 300 | #endif /* !_KDBPRIVATE_H */ | ||
diff --git a/kernel/debug/kdb/kdb_support.c b/kernel/debug/kdb/kdb_support.c new file mode 100644 index 000000000000..45344d5c53dd --- /dev/null +++ b/kernel/debug/kdb/kdb_support.c | |||
| @@ -0,0 +1,927 @@ | |||
| 1 | /* | ||
| 2 | * Kernel Debugger Architecture Independent Support Functions | ||
| 3 | * | ||
| 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 | ||
| 6 | * for more details. | ||
| 7 | * | ||
| 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> | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include <stdarg.h> | ||
| 14 | #include <linux/types.h> | ||
| 15 | #include <linux/sched.h> | ||
| 16 | #include <linux/mm.h> | ||
| 17 | #include <linux/kallsyms.h> | ||
| 18 | #include <linux/stddef.h> | ||
| 19 | #include <linux/vmalloc.h> | ||
| 20 | #include <linux/ptrace.h> | ||
| 21 | #include <linux/module.h> | ||
| 22 | #include <linux/highmem.h> | ||
| 23 | #include <linux/hardirq.h> | ||
| 24 | #include <linux/delay.h> | ||
| 25 | #include <linux/uaccess.h> | ||
| 26 | #include <linux/kdb.h> | ||
| 27 | #include <linux/slab.h> | ||
| 28 | #include "kdb_private.h" | ||
| 29 | |||
| 30 | /* | ||
| 31 | * kdbgetsymval - Return the address of the given symbol. | ||
| 32 | * | ||
| 33 | * Parameters: | ||
| 34 | * symname Character string containing symbol name | ||
| 35 | * symtab Structure to receive results | ||
| 36 | * Returns: | ||
| 37 | * 0 Symbol not found, symtab zero filled | ||
| 38 | * 1 Symbol mapped to module/symbol/section, data in symtab | ||
| 39 | */ | ||
| 40 | int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) | ||
| 41 | { | ||
| 42 | if (KDB_DEBUG(AR)) | ||
| 43 | kdb_printf("kdbgetsymval: symname=%s, symtab=%p\n", symname, | ||
| 44 | symtab); | ||
| 45 | memset(symtab, 0, sizeof(*symtab)); | ||
| 46 | symtab->sym_start = kallsyms_lookup_name(symname); | ||
| 47 | if (symtab->sym_start) { | ||
| 48 | if (KDB_DEBUG(AR)) | ||
| 49 | kdb_printf("kdbgetsymval: returns 1, " | ||
| 50 | "symtab->sym_start=0x%lx\n", | ||
| 51 | symtab->sym_start); | ||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | if (KDB_DEBUG(AR)) | ||
| 55 | kdb_printf("kdbgetsymval: returns 0\n"); | ||
| 56 | return 0; | ||
| 57 | } | ||
| 58 | EXPORT_SYMBOL(kdbgetsymval); | ||
| 59 | |||
| 60 | static char *kdb_name_table[100]; /* arbitrary size */ | ||
| 61 | |||
| 62 | /* | ||
| 63 | * kdbnearsym - Return the name of the symbol with the nearest address | ||
| 64 | * less than 'addr'. | ||
| 65 | * | ||
| 66 | * Parameters: | ||
| 67 | * addr Address to check for symbol near | ||
| 68 | * symtab Structure to receive results | ||
| 69 | * Returns: | ||
| 70 | * 0 No sections contain this address, symtab zero filled | ||
| 71 | * 1 Address mapped to module/symbol/section, data in symtab | ||
| 72 | * Remarks: | ||
| 73 | * 2.6 kallsyms has a "feature" where it unpacks the name into a | ||
| 74 | * string. If that string is reused before the caller expects it | ||
| 75 | * then the caller sees its string change without warning. To | ||
| 76 | * avoid cluttering up the main kdb code with lots of kdb_strdup, | ||
| 77 | * tests and kfree calls, kdbnearsym maintains an LRU list of the | ||
| 78 | * last few unique strings. The list is sized large enough to | ||
| 79 | * hold active strings, no kdb caller of kdbnearsym makes more | ||
| 80 | * than ~20 later calls before using a saved value. | ||
| 81 | */ | ||
| 82 | int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab) | ||
| 83 | { | ||
| 84 | int ret = 0; | ||
| 85 | unsigned long symbolsize; | ||
| 86 | unsigned long offset; | ||
| 87 | #define knt1_size 128 /* must be >= kallsyms table size */ | ||
| 88 | char *knt1 = NULL; | ||
| 89 | |||
| 90 | if (KDB_DEBUG(AR)) | ||
| 91 | kdb_printf("kdbnearsym: addr=0x%lx, symtab=%p\n", addr, symtab); | ||
| 92 | memset(symtab, 0, sizeof(*symtab)); | ||
| 93 | |||
| 94 | if (addr < 4096) | ||
| 95 | goto out; | ||
| 96 | knt1 = debug_kmalloc(knt1_size, GFP_ATOMIC); | ||
| 97 | if (!knt1) { | ||
| 98 | kdb_printf("kdbnearsym: addr=0x%lx cannot kmalloc knt1\n", | ||
| 99 | addr); | ||
| 100 | goto out; | ||
| 101 | } | ||
| 102 | symtab->sym_name = kallsyms_lookup(addr, &symbolsize , &offset, | ||
| 103 | (char **)(&symtab->mod_name), knt1); | ||
| 104 | if (offset > 8*1024*1024) { | ||
| 105 | symtab->sym_name = NULL; | ||
| 106 | addr = offset = symbolsize = 0; | ||
| 107 | } | ||
| 108 | symtab->sym_start = addr - offset; | ||
| 109 | symtab->sym_end = symtab->sym_start + symbolsize; | ||
| 110 | ret = symtab->sym_name != NULL && *(symtab->sym_name) != '\0'; | ||
| 111 | |||
| 112 | if (ret) { | ||
| 113 | int i; | ||
| 114 | /* Another 2.6 kallsyms "feature". Sometimes the sym_name is | ||
| 115 | * set but the buffer passed into kallsyms_lookup is not used, | ||
| 116 | * so it contains garbage. The caller has to work out which | ||
| 117 | * buffer needs to be saved. | ||
| 118 | * | ||
| 119 | * What was Rusty smoking when he wrote that code? | ||
| 120 | */ | ||
| 121 | if (symtab->sym_name != knt1) { | ||
| 122 | strncpy(knt1, symtab->sym_name, knt1_size); | ||
| 123 | knt1[knt1_size-1] = '\0'; | ||
| 124 | } | ||
| 125 | for (i = 0; i < ARRAY_SIZE(kdb_name_table); ++i) { | ||
| 126 | if (kdb_name_table[i] && | ||
| 127 | strcmp(kdb_name_table[i], knt1) == 0) | ||
| 128 | break; | ||
| 129 | } | ||
| 130 | if (i >= ARRAY_SIZE(kdb_name_table)) { | ||
| 131 | debug_kfree(kdb_name_table[0]); | ||
| 132 | memcpy(kdb_name_table, kdb_name_table+1, | ||
| 133 | sizeof(kdb_name_table[0]) * | ||
| 134 | (ARRAY_SIZE(kdb_name_table)-1)); | ||
| 135 | } else { | ||
| 136 | debug_kfree(knt1); | ||
| 137 | knt1 = kdb_name_table[i]; | ||
| 138 | memcpy(kdb_name_table+i, kdb_name_table+i+1, | ||
| 139 | sizeof(kdb_name_table[0]) * | ||
| 140 | (ARRAY_SIZE(kdb_name_table)-i-1)); | ||
| 141 | } | ||
| 142 | i = ARRAY_SIZE(kdb_name_table) - 1; | ||
| 143 | kdb_name_table[i] = knt1; | ||
| 144 | symtab->sym_name = kdb_name_table[i]; | ||
| 145 | knt1 = NULL; | ||
| 146 | } | ||
| 147 | |||
| 148 | if (symtab->mod_name == NULL) | ||
| 149 | symtab->mod_name = "kernel"; | ||
| 150 | if (KDB_DEBUG(AR)) | ||
| 151 | kdb_printf("kdbnearsym: returns %d symtab->sym_start=0x%lx, " | ||
| 152 | "symtab->mod_name=%p, symtab->sym_name=%p (%s)\n", ret, | ||
| 153 | symtab->sym_start, symtab->mod_name, symtab->sym_name, | ||
| 154 | symtab->sym_name); | ||
| 155 | |||
| 156 | out: | ||
| 157 | debug_kfree(knt1); | ||
| 158 | return ret; | ||
| 159 | } | ||
| 160 | |||
| 161 | void kdbnearsym_cleanup(void) | ||
| 162 | { | ||
| 163 | int i; | ||
| 164 | for (i = 0; i < ARRAY_SIZE(kdb_name_table); ++i) { | ||
| 165 | if (kdb_name_table[i]) { | ||
| 166 | debug_kfree(kdb_name_table[i]); | ||
| 167 | kdb_name_table[i] = NULL; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | static char ks_namebuf[KSYM_NAME_LEN+1], ks_namebuf_prev[KSYM_NAME_LEN+1]; | ||
| 173 | |||
| 174 | /* | ||
| 175 | * kallsyms_symbol_complete | ||
| 176 | * | ||
| 177 | * Parameters: | ||
| 178 | * prefix_name prefix of a symbol name to lookup | ||
| 179 | * max_len maximum length that can be returned | ||
| 180 | * Returns: | ||
| 181 | * Number of symbols which match the given prefix. | ||
| 182 | * Notes: | ||
| 183 | * prefix_name is changed to contain the longest unique prefix that | ||
| 184 | * starts with this prefix (tab completion). | ||
| 185 | */ | ||
| 186 | int kallsyms_symbol_complete(char *prefix_name, int max_len) | ||
| 187 | { | ||
| 188 | loff_t pos = 0; | ||
| 189 | int prefix_len = strlen(prefix_name), prev_len = 0; | ||
| 190 | int i, number = 0; | ||
| 191 | const char *name; | ||
| 192 | |||
| 193 | while ((name = kdb_walk_kallsyms(&pos))) { | ||
| 194 | if (strncmp(name, prefix_name, prefix_len) == 0) { | ||
| 195 | strcpy(ks_namebuf, name); | ||
| 196 | /* Work out the longest name that matches the prefix */ | ||
| 197 | if (++number == 1) { | ||
| 198 | prev_len = min_t(int, max_len-1, | ||
| 199 | strlen(ks_namebuf)); | ||
| 200 | memcpy(ks_namebuf_prev, ks_namebuf, prev_len); | ||
| 201 | ks_namebuf_prev[prev_len] = '\0'; | ||
| 202 | continue; | ||
| 203 | } | ||
| 204 | for (i = 0; i < prev_len; i++) { | ||
| 205 | if (ks_namebuf[i] != ks_namebuf_prev[i]) { | ||
| 206 | prev_len = i; | ||
| 207 | ks_namebuf_prev[i] = '\0'; | ||
| 208 | break; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } | ||
| 213 | if (prev_len > prefix_len) | ||
| 214 | memcpy(prefix_name, ks_namebuf_prev, prev_len+1); | ||
| 215 | return number; | ||
| 216 | } | ||
| 217 | |||
| 218 | /* | ||
| 219 | * kallsyms_symbol_next | ||
| 220 | * | ||
| 221 | * Parameters: | ||
| 222 | * prefix_name prefix of a symbol name to lookup | ||
| 223 | * flag 0 means search from the head, 1 means continue search. | ||
| 224 | * Returns: | ||
| 225 | * 1 if a symbol matches the given prefix. | ||
| 226 | * 0 if no string found | ||
| 227 | */ | ||
| 228 | int kallsyms_symbol_next(char *prefix_name, int flag) | ||
| 229 | { | ||
| 230 | int prefix_len = strlen(prefix_name); | ||
| 231 | static loff_t pos; | ||
| 232 | const char *name; | ||
| 233 | |||
| 234 | if (!flag) | ||
| 235 | pos = 0; | ||
| 236 | |||
| 237 | while ((name = kdb_walk_kallsyms(&pos))) { | ||
| 238 | if (strncmp(name, prefix_name, prefix_len) == 0) { | ||
| 239 | strncpy(prefix_name, name, strlen(name)+1); | ||
| 240 | return 1; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | return 0; | ||
| 244 | } | ||
| 245 | |||
| 246 | /* | ||
| 247 | * kdb_symbol_print - Standard method for printing a symbol name and offset. | ||
| 248 | * Inputs: | ||
| 249 | * addr Address to be printed. | ||
| 250 | * symtab Address of symbol data, if NULL this routine does its | ||
| 251 | * own lookup. | ||
| 252 | * punc Punctuation for string, bit field. | ||
| 253 | * Remarks: | ||
| 254 | * The string and its punctuation is only printed if the address | ||
| 255 | * is inside the kernel, except that the value is always printed | ||
| 256 | * when requested. | ||
| 257 | */ | ||
| 258 | void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p, | ||
| 259 | unsigned int punc) | ||
| 260 | { | ||
| 261 | kdb_symtab_t symtab, *symtab_p2; | ||
| 262 | if (symtab_p) { | ||
| 263 | symtab_p2 = (kdb_symtab_t *)symtab_p; | ||
| 264 | } else { | ||
| 265 | symtab_p2 = &symtab; | ||
| 266 | kdbnearsym(addr, symtab_p2); | ||
| 267 | } | ||
| 268 | if (!(symtab_p2->sym_name || (punc & KDB_SP_VALUE))) | ||
| 269 | return; | ||
| 270 | if (punc & KDB_SP_SPACEB) | ||
| 271 | kdb_printf(" "); | ||
| 272 | if (punc & KDB_SP_VALUE) | ||
| 273 | kdb_printf(kdb_machreg_fmt0, addr); | ||
| 274 | if (symtab_p2->sym_name) { | ||
| 275 | if (punc & KDB_SP_VALUE) | ||
| 276 | kdb_printf(" "); | ||
| 277 | if (punc & KDB_SP_PAREN) | ||
| 278 | kdb_printf("("); | ||
| 279 | if (strcmp(symtab_p2->mod_name, "kernel")) | ||
| 280 | kdb_printf("[%s]", symtab_p2->mod_name); | ||
| 281 | kdb_printf("%s", symtab_p2->sym_name); | ||
| 282 | if (addr != symtab_p2->sym_start) | ||
| 283 | kdb_printf("+0x%lx", addr - symtab_p2->sym_start); | ||
| 284 | if (punc & KDB_SP_SYMSIZE) | ||
| 285 | kdb_printf("/0x%lx", | ||
| 286 | symtab_p2->sym_end - symtab_p2->sym_start); | ||
| 287 | if (punc & KDB_SP_PAREN) | ||
| 288 | kdb_printf(")"); | ||
| 289 | } | ||
| 290 | if (punc & KDB_SP_SPACEA) | ||
| 291 | kdb_printf(" "); | ||
| 292 | if (punc & KDB_SP_NEWLINE) | ||
| 293 | kdb_printf("\n"); | ||
| 294 | } | ||
| 295 | |||
| 296 | /* | ||
| 297 | * kdb_strdup - kdb equivalent of strdup, for disasm code. | ||
| 298 | * Inputs: | ||
| 299 | * str The string to duplicate. | ||
| 300 | * type Flags to kmalloc for the new string. | ||
| 301 | * Returns: | ||
| 302 | * Address of the new string, NULL if storage could not be allocated. | ||
| 303 | * Remarks: | ||
| 304 | * This is not in lib/string.c because it uses kmalloc which is not | ||
| 305 | * available when string.o is used in boot loaders. | ||
| 306 | */ | ||
| 307 | char *kdb_strdup(const char *str, gfp_t type) | ||
| 308 | { | ||
| 309 | int n = strlen(str)+1; | ||
| 310 | char *s = kmalloc(n, type); | ||
| 311 | if (!s) | ||
| 312 | return NULL; | ||
| 313 | return strcpy(s, str); | ||
| 314 | } | ||
| 315 | |||
| 316 | /* | ||
| 317 | * kdb_getarea_size - Read an area of data. The kdb equivalent of | ||
| 318 | * copy_from_user, with kdb messages for invalid addresses. | ||
| 319 | * Inputs: | ||
| 320 | * res Pointer to the area to receive the result. | ||
| 321 | * addr Address of the area to copy. | ||
| 322 | * size Size of the area. | ||
| 323 | * Returns: | ||
| 324 | * 0 for success, < 0 for error. | ||
| 325 | */ | ||
| 326 | int kdb_getarea_size(void *res, unsigned long addr, size_t size) | ||
| 327 | { | ||
| 328 | int ret = probe_kernel_read((char *)res, (char *)addr, size); | ||
| 329 | if (ret) { | ||
| 330 | if (!KDB_STATE(SUPPRESS)) { | ||
| 331 | kdb_printf("kdb_getarea: Bad address 0x%lx\n", addr); | ||
| 332 | KDB_STATE_SET(SUPPRESS); | ||
| 333 | } | ||
| 334 | ret = KDB_BADADDR; | ||
| 335 | } else { | ||
| 336 | KDB_STATE_CLEAR(SUPPRESS); | ||
| 337 | } | ||
| 338 | return ret; | ||
| 339 | } | ||
| 340 | |||
| 341 | /* | ||
| 342 | * kdb_putarea_size - Write an area of data. The kdb equivalent of | ||
| 343 | * copy_to_user, with kdb messages for invalid addresses. | ||
| 344 | * Inputs: | ||
| 345 | * addr Address of the area to write to. | ||
| 346 | * res Pointer to the area holding the data. | ||
| 347 | * size Size of the area. | ||
| 348 | * Returns: | ||
| 349 | * 0 for success, < 0 for error. | ||
| 350 | */ | ||
| 351 | int kdb_putarea_size(unsigned long addr, void *res, size_t size) | ||
| 352 | { | ||
| 353 | int ret = probe_kernel_read((char *)addr, (char *)res, size); | ||
| 354 | if (ret) { | ||
| 355 | if (!KDB_STATE(SUPPRESS)) { | ||
| 356 | kdb_printf("kdb_putarea: Bad address 0x%lx\n", addr); | ||
| 357 | KDB_STATE_SET(SUPPRESS); | ||
| 358 | } | ||
| 359 | ret = KDB_BADADDR; | ||
| 360 | } else { | ||
| 361 | KDB_STATE_CLEAR(SUPPRESS); | ||
| 362 | } | ||
| 363 | return ret; | ||
| 364 | } | ||
| 365 | |||
| 366 | /* | ||
| 367 | * kdb_getphys - Read data from a physical address. Validate the | ||
| 368 | * address is in range, use kmap_atomic() to get data | ||
| 369 | * similar to kdb_getarea() - but for phys addresses | ||
| 370 | * Inputs: | ||
| 371 | * res Pointer to the word to receive the result | ||
| 372 | * addr Physical address of the area to copy | ||
| 373 | * size Size of the area | ||
| 374 | * Returns: | ||
| 375 | * 0 for success, < 0 for error. | ||
| 376 | */ | ||
| 377 | static int kdb_getphys(void *res, unsigned long addr, size_t size) | ||
| 378 | { | ||
| 379 | unsigned long pfn; | ||
| 380 | void *vaddr; | ||
| 381 | struct page *page; | ||
| 382 | |||
| 383 | pfn = (addr >> PAGE_SHIFT); | ||
| 384 | if (!pfn_valid(pfn)) | ||
| 385 | return 1; | ||
| 386 | page = pfn_to_page(pfn); | ||
| 387 | vaddr = kmap_atomic(page, KM_KDB); | ||
| 388 | memcpy(res, vaddr + (addr & (PAGE_SIZE - 1)), size); | ||
| 389 | kunmap_atomic(vaddr, KM_KDB); | ||
| 390 | |||
| 391 | return 0; | ||
| 392 | } | ||
| 393 | |||
| 394 | /* | ||
| 395 | * kdb_getphysword | ||
| 396 | * Inputs: | ||
| 397 | * word Pointer to the word to receive the result. | ||
| 398 | * addr Address of the area to copy. | ||
| 399 | * size Size of the area. | ||
| 400 | * Returns: | ||
| 401 | * 0 for success, < 0 for error. | ||
| 402 | */ | ||
| 403 | int kdb_getphysword(unsigned long *word, unsigned long addr, size_t size) | ||
| 404 | { | ||
| 405 | int diag; | ||
| 406 | __u8 w1; | ||
| 407 | __u16 w2; | ||
| 408 | __u32 w4; | ||
| 409 | __u64 w8; | ||
| 410 | *word = 0; /* Default value if addr or size is invalid */ | ||
| 411 | |||
| 412 | switch (size) { | ||
| 413 | case 1: | ||
| 414 | diag = kdb_getphys(&w1, addr, sizeof(w1)); | ||
| 415 | if (!diag) | ||
| 416 | *word = w1; | ||
| 417 | break; | ||
| 418 | case 2: | ||
| 419 | diag = kdb_getphys(&w2, addr, sizeof(w2)); | ||
| 420 | if (!diag) | ||
| 421 | *word = w2; | ||
| 422 | break; | ||
| 423 | case 4: | ||
| 424 | diag = kdb_getphys(&w4, addr, sizeof(w4)); | ||
| 425 | if (!diag) | ||
| 426 | *word = w4; | ||
| 427 | break; | ||
| 428 | case 8: | ||
| 429 | if (size <= sizeof(*word)) { | ||
| 430 | diag = kdb_getphys(&w8, addr, sizeof(w8)); | ||
| 431 | if (!diag) | ||
| 432 | *word = w8; | ||
| 433 | break; | ||
| 434 | } | ||
| 435 | /* drop through */ | ||
| 436 | default: | ||
| 437 | diag = KDB_BADWIDTH; | ||
| 438 | kdb_printf("kdb_getphysword: bad width %ld\n", (long) size); | ||
| 439 | } | ||
| 440 | return diag; | ||
| 441 | } | ||
| 442 | |||
| 443 | /* | ||
| 444 | * kdb_getword - Read a binary value. Unlike kdb_getarea, this treats | ||
| 445 | * data as numbers. | ||
| 446 | * Inputs: | ||
| 447 | * word Pointer to the word to receive the result. | ||
| 448 | * addr Address of the area to copy. | ||
| 449 | * size Size of the area. | ||
| 450 | * Returns: | ||
| 451 | * 0 for success, < 0 for error. | ||
| 452 | */ | ||
| 453 | int kdb_getword(unsigned long *word, unsigned long addr, size_t size) | ||
| 454 | { | ||
| 455 | int diag; | ||
| 456 | __u8 w1; | ||
| 457 | __u16 w2; | ||
| 458 | __u32 w4; | ||
| 459 | __u64 w8; | ||
| 460 | *word = 0; /* Default value if addr or size is invalid */ | ||
| 461 | switch (size) { | ||
| 462 | case 1: | ||
| 463 | diag = kdb_getarea(w1, addr); | ||
| 464 | if (!diag) | ||
| 465 | *word = w1; | ||
| 466 | break; | ||
| 467 | case 2: | ||
| 468 | diag = kdb_getarea(w2, addr); | ||
| 469 | if (!diag) | ||
| 470 | *word = w2; | ||
| 471 | break; | ||
| 472 | case 4: | ||
| 473 | diag = kdb_getarea(w4, addr); | ||
| 474 | if (!diag) | ||
| 475 | *word = w4; | ||
| 476 | break; | ||
| 477 | case 8: | ||
| 478 | if (size <= sizeof(*word)) { | ||
| 479 | diag = kdb_getarea(w8, addr); | ||
| 480 | if (!diag) | ||
| 481 | *word = w8; | ||
| 482 | break; | ||
| 483 | } | ||
| 484 | /* drop through */ | ||
| 485 | default: | ||
| 486 | diag = KDB_BADWIDTH; | ||
| 487 | kdb_printf("kdb_getword: bad width %ld\n", (long) size); | ||
| 488 | } | ||
| 489 | return diag; | ||
| 490 | } | ||
| 491 | |||
| 492 | /* | ||
| 493 | * kdb_putword - Write a binary value. Unlike kdb_putarea, this | ||
| 494 | * treats data as numbers. | ||
| 495 | * Inputs: | ||
| 496 | * addr Address of the area to write to.. | ||
| 497 | * word The value to set. | ||
| 498 | * size Size of the area. | ||
| 499 | * Returns: | ||
| 500 | * 0 for success, < 0 for error. | ||
| 501 | */ | ||
| 502 | int kdb_putword(unsigned long addr, unsigned long word, size_t size) | ||
| 503 | { | ||
| 504 | int diag; | ||
| 505 | __u8 w1; | ||
| 506 | __u16 w2; | ||
| 507 | __u32 w4; | ||
| 508 | __u64 w8; | ||
| 509 | switch (size) { | ||
| 510 | case 1: | ||
| 511 | w1 = word; | ||
| 512 | diag = kdb_putarea(addr, w1); | ||
| 513 | break; | ||
| 514 | case 2: | ||
| 515 | w2 = word; | ||
| 516 | diag = kdb_putarea(addr, w2); | ||
| 517 | break; | ||
| 518 | case 4: | ||
| 519 | w4 = word; | ||
| 520 | diag = kdb_putarea(addr, w4); | ||
| 521 | break; | ||
| 522 | case 8: | ||
| 523 | if (size <= sizeof(word)) { | ||
| 524 | w8 = word; | ||
| 525 | diag = kdb_putarea(addr, w8); | ||
| 526 | break; | ||
| 527 | } | ||
| 528 | /* drop through */ | ||
| 529 | default: | ||
| 530 | diag = KDB_BADWIDTH; | ||
| 531 | kdb_printf("kdb_putword: bad width %ld\n", (long) size); | ||
| 532 | } | ||
| 533 | return diag; | ||
| 534 | } | ||
| 535 | |||
| 536 | /* | ||
| 537 | * kdb_task_state_string - Convert a string containing any of the | ||
| 538 | * letters DRSTCZEUIMA to a mask for the process state field and | ||
| 539 | * return the value. If no argument is supplied, return the mask | ||
| 540 | * that corresponds to environment variable PS, DRSTCZEU by | ||
| 541 | * default. | ||
| 542 | * Inputs: | ||
| 543 | * s String to convert | ||
| 544 | * Returns: | ||
| 545 | * Mask for process state. | ||
| 546 | * Notes: | ||
| 547 | * The mask folds data from several sources into a single long value, so | ||
| 548 | * be carefull not to overlap the bits. TASK_* bits are in the LSB, | ||
| 549 | * special cases like UNRUNNABLE are in the MSB. As of 2.6.10-rc1 there | ||
| 550 | * is no overlap between TASK_* and EXIT_* but that may not always be | ||
| 551 | * true, so EXIT_* bits are shifted left 16 bits before being stored in | ||
| 552 | * the mask. | ||
| 553 | */ | ||
| 554 | |||
| 555 | /* unrunnable is < 0 */ | ||
| 556 | #define UNRUNNABLE (1UL << (8*sizeof(unsigned long) - 1)) | ||
| 557 | #define RUNNING (1UL << (8*sizeof(unsigned long) - 2)) | ||
| 558 | #define IDLE (1UL << (8*sizeof(unsigned long) - 3)) | ||
| 559 | #define DAEMON (1UL << (8*sizeof(unsigned long) - 4)) | ||
| 560 | |||
| 561 | unsigned long kdb_task_state_string(const char *s) | ||
| 562 | { | ||
| 563 | long res = 0; | ||
| 564 | if (!s) { | ||
| 565 | s = kdbgetenv("PS"); | ||
| 566 | if (!s) | ||
| 567 | s = "DRSTCZEU"; /* default value for ps */ | ||
| 568 | } | ||
| 569 | while (*s) { | ||
| 570 | switch (*s) { | ||
| 571 | case 'D': | ||
| 572 | res |= TASK_UNINTERRUPTIBLE; | ||
| 573 | break; | ||
| 574 | case 'R': | ||
| 575 | res |= RUNNING; | ||
| 576 | break; | ||
| 577 | case 'S': | ||
| 578 | res |= TASK_INTERRUPTIBLE; | ||
| 579 | break; | ||
| 580 | case 'T': | ||
| 581 | res |= TASK_STOPPED; | ||
| 582 | break; | ||
| 583 | case 'C': | ||
| 584 | res |= TASK_TRACED; | ||
| 585 | break; | ||
| 586 | case 'Z': | ||
| 587 | res |= EXIT_ZOMBIE << 16; | ||
| 588 | break; | ||
| 589 | case 'E': | ||
| 590 | res |= EXIT_DEAD << 16; | ||
| 591 | break; | ||
| 592 | case 'U': | ||
| 593 | res |= UNRUNNABLE; | ||
| 594 | break; | ||
| 595 | case 'I': | ||
| 596 | res |= IDLE; | ||
| 597 | break; | ||
| 598 | case 'M': | ||
| 599 | res |= DAEMON; | ||
| 600 | break; | ||
| 601 | case 'A': | ||
| 602 | res = ~0UL; | ||
| 603 | break; | ||
| 604 | default: | ||
| 605 | kdb_printf("%s: unknown flag '%c' ignored\n", | ||
| 606 | __func__, *s); | ||
| 607 | break; | ||
| 608 | } | ||
| 609 | ++s; | ||
| 610 | } | ||
| 611 | return res; | ||
| 612 | } | ||
| 613 | |||
| 614 | /* | ||
| 615 | * kdb_task_state_char - Return the character that represents the task state. | ||
| 616 | * Inputs: | ||
| 617 | * p struct task for the process | ||
| 618 | * Returns: | ||
| 619 | * One character to represent the task state. | ||
| 620 | */ | ||
| 621 | char kdb_task_state_char (const struct task_struct *p) | ||
| 622 | { | ||
| 623 | int cpu; | ||
| 624 | char state; | ||
| 625 | unsigned long tmp; | ||
| 626 | |||
| 627 | if (!p || probe_kernel_read(&tmp, (char *)p, sizeof(unsigned long))) | ||
| 628 | return 'E'; | ||
| 629 | |||
| 630 | cpu = kdb_process_cpu(p); | ||
| 631 | state = (p->state == 0) ? 'R' : | ||
| 632 | (p->state < 0) ? 'U' : | ||
| 633 | (p->state & TASK_UNINTERRUPTIBLE) ? 'D' : | ||
| 634 | (p->state & TASK_STOPPED) ? 'T' : | ||
| 635 | (p->state & TASK_TRACED) ? 'C' : | ||
| 636 | (p->exit_state & EXIT_ZOMBIE) ? 'Z' : | ||
| 637 | (p->exit_state & EXIT_DEAD) ? 'E' : | ||
| 638 | (p->state & TASK_INTERRUPTIBLE) ? 'S' : '?'; | ||
| 639 | if (p->pid == 0) { | ||
| 640 | /* Idle task. Is it really idle, apart from the kdb | ||
| 641 | * interrupt? */ | ||
| 642 | if (!kdb_task_has_cpu(p) || kgdb_info[cpu].irq_depth == 1) { | ||
| 643 | if (cpu != kdb_initial_cpu) | ||
| 644 | state = 'I'; /* idle task */ | ||
| 645 | } | ||
| 646 | } else if (!p->mm && state == 'S') { | ||
| 647 | state = 'M'; /* sleeping system daemon */ | ||
| 648 | } | ||
| 649 | return state; | ||
| 650 | } | ||
| 651 | |||
| 652 | /* | ||
| 653 | * kdb_task_state - Return true if a process has the desired state | ||
| 654 | * given by the mask. | ||
| 655 | * Inputs: | ||
| 656 | * p struct task for the process | ||
| 657 | * mask mask from kdb_task_state_string to select processes | ||
| 658 | * Returns: | ||
| 659 | * True if the process matches at least one criteria defined by the mask. | ||
| 660 | */ | ||
| 661 | unsigned long kdb_task_state(const struct task_struct *p, unsigned long mask) | ||
| 662 | { | ||
| 663 | char state[] = { kdb_task_state_char(p), '\0' }; | ||
| 664 | return (mask & kdb_task_state_string(state)) != 0; | ||
| 665 | } | ||
| 666 | |||
| 667 | /* | ||
| 668 | * kdb_print_nameval - Print a name and its value, converting the | ||
| 669 | * value to a symbol lookup if possible. | ||
| 670 | * Inputs: | ||
| 671 | * name field name to print | ||
| 672 | * val value of field | ||
| 673 | */ | ||
| 674 | void kdb_print_nameval(const char *name, unsigned long val) | ||
| 675 | { | ||
| 676 | kdb_symtab_t symtab; | ||
| 677 | kdb_printf(" %-11.11s ", name); | ||
| 678 | if (kdbnearsym(val, &symtab)) | ||
| 679 | kdb_symbol_print(val, &symtab, | ||
| 680 | KDB_SP_VALUE|KDB_SP_SYMSIZE|KDB_SP_NEWLINE); | ||
| 681 | else | ||
| 682 | kdb_printf("0x%lx\n", val); | ||
| 683 | } | ||
| 684 | |||
| 685 | /* Last ditch allocator for debugging, so we can still debug even when | ||
| 686 | * the GFP_ATOMIC pool has been exhausted. The algorithms are tuned | ||
| 687 | * for space usage, not for speed. One smallish memory pool, the free | ||
| 688 | * chain is always in ascending address order to allow coalescing, | ||
| 689 | * allocations are done in brute force best fit. | ||
| 690 | */ | ||
| 691 | |||
| 692 | struct debug_alloc_header { | ||
| 693 | u32 next; /* offset of next header from start of pool */ | ||
| 694 | u32 size; | ||
| 695 | void *caller; | ||
| 696 | }; | ||
| 697 | |||
| 698 | /* The memory returned by this allocator must be aligned, which means | ||
| 699 | * so must the header size. Do not assume that sizeof(struct | ||
| 700 | * debug_alloc_header) is a multiple of the alignment, explicitly | ||
| 701 | * calculate the overhead of this header, including the alignment. | ||
| 702 | * The rest of this code must not use sizeof() on any header or | ||
| 703 | * pointer to a header. | ||
| 704 | */ | ||
| 705 | #define dah_align 8 | ||
| 706 | #define dah_overhead ALIGN(sizeof(struct debug_alloc_header), dah_align) | ||
| 707 | |||
| 708 | static u64 debug_alloc_pool_aligned[256*1024/dah_align]; /* 256K pool */ | ||
| 709 | static char *debug_alloc_pool = (char *)debug_alloc_pool_aligned; | ||
| 710 | static u32 dah_first, dah_first_call = 1, dah_used, dah_used_max; | ||
| 711 | |||
| 712 | /* Locking is awkward. The debug code is called from all contexts, | ||
| 713 | * including non maskable interrupts. A normal spinlock is not safe | ||
| 714 | * in NMI context. Try to get the debug allocator lock, if it cannot | ||
| 715 | * be obtained after a second then give up. If the lock could not be | ||
| 716 | * previously obtained on this cpu then only try once. | ||
| 717 | * | ||
| 718 | * sparse has no annotation for "this function _sometimes_ acquires a | ||
| 719 | * lock", so fudge the acquire/release notation. | ||
| 720 | */ | ||
| 721 | static DEFINE_SPINLOCK(dap_lock); | ||
| 722 | static int get_dap_lock(void) | ||
| 723 | __acquires(dap_lock) | ||
| 724 | { | ||
| 725 | static int dap_locked = -1; | ||
| 726 | int count; | ||
| 727 | if (dap_locked == smp_processor_id()) | ||
| 728 | count = 1; | ||
| 729 | else | ||
| 730 | count = 1000; | ||
| 731 | while (1) { | ||
| 732 | if (spin_trylock(&dap_lock)) { | ||
| 733 | dap_locked = -1; | ||
| 734 | return 1; | ||
| 735 | } | ||
| 736 | if (!count--) | ||
| 737 | break; | ||
| 738 | udelay(1000); | ||
| 739 | } | ||
| 740 | dap_locked = smp_processor_id(); | ||
| 741 | __acquire(dap_lock); | ||
| 742 | return 0; | ||
| 743 | } | ||
| 744 | |||
| 745 | void *debug_kmalloc(size_t size, gfp_t flags) | ||
| 746 | { | ||
| 747 | unsigned int rem, h_offset; | ||
| 748 | struct debug_alloc_header *best, *bestprev, *prev, *h; | ||
| 749 | void *p = NULL; | ||
| 750 | if (!get_dap_lock()) { | ||
| 751 | __release(dap_lock); /* we never actually got it */ | ||
| 752 | return NULL; | ||
| 753 | } | ||
| 754 | h = (struct debug_alloc_header *)(debug_alloc_pool + dah_first); | ||
| 755 | if (dah_first_call) { | ||
| 756 | h->size = sizeof(debug_alloc_pool_aligned) - dah_overhead; | ||
| 757 | dah_first_call = 0; | ||
| 758 | } | ||
| 759 | size = ALIGN(size, dah_align); | ||
| 760 | prev = best = bestprev = NULL; | ||
| 761 | while (1) { | ||
| 762 | if (h->size >= size && (!best || h->size < best->size)) { | ||
| 763 | best = h; | ||
| 764 | bestprev = prev; | ||
| 765 | if (h->size == size) | ||
| 766 | break; | ||
| 767 | } | ||
| 768 | if (!h->next) | ||
| 769 | break; | ||
| 770 | prev = h; | ||
| 771 | h = (struct debug_alloc_header *)(debug_alloc_pool + h->next); | ||
| 772 | } | ||
| 773 | if (!best) | ||
| 774 | goto out; | ||
| 775 | rem = best->size - size; | ||
| 776 | /* The pool must always contain at least one header */ | ||
| 777 | if (best->next == 0 && bestprev == NULL && rem < dah_overhead) | ||
| 778 | goto out; | ||
| 779 | if (rem >= dah_overhead) { | ||
| 780 | best->size = size; | ||
| 781 | h_offset = ((char *)best - debug_alloc_pool) + | ||
| 782 | dah_overhead + best->size; | ||
| 783 | h = (struct debug_alloc_header *)(debug_alloc_pool + h_offset); | ||
| 784 | h->size = rem - dah_overhead; | ||
| 785 | h->next = best->next; | ||
| 786 | } else | ||
| 787 | h_offset = best->next; | ||
| 788 | best->caller = __builtin_return_address(0); | ||
| 789 | dah_used += best->size; | ||
| 790 | dah_used_max = max(dah_used, dah_used_max); | ||
| 791 | if (bestprev) | ||
| 792 | bestprev->next = h_offset; | ||
| 793 | else | ||
| 794 | dah_first = h_offset; | ||
| 795 | p = (char *)best + dah_overhead; | ||
| 796 | memset(p, POISON_INUSE, best->size - 1); | ||
| 797 | *((char *)p + best->size - 1) = POISON_END; | ||
| 798 | out: | ||
| 799 | spin_unlock(&dap_lock); | ||
| 800 | return p; | ||
| 801 | } | ||
| 802 | |||
| 803 | void debug_kfree(void *p) | ||
| 804 | { | ||
| 805 | struct debug_alloc_header *h; | ||
| 806 | unsigned int h_offset; | ||
| 807 | if (!p) | ||
| 808 | return; | ||
| 809 | if ((char *)p < debug_alloc_pool || | ||
| 810 | (char *)p >= debug_alloc_pool + sizeof(debug_alloc_pool_aligned)) { | ||
| 811 | kfree(p); | ||
| 812 | return; | ||
| 813 | } | ||
| 814 | if (!get_dap_lock()) { | ||
| 815 | __release(dap_lock); /* we never actually got it */ | ||
| 816 | return; /* memory leak, cannot be helped */ | ||
| 817 | } | ||
| 818 | h = (struct debug_alloc_header *)((char *)p - dah_overhead); | ||
| 819 | memset(p, POISON_FREE, h->size - 1); | ||
| 820 | *((char *)p + h->size - 1) = POISON_END; | ||
| 821 | h->caller = NULL; | ||
| 822 | dah_used -= h->size; | ||
| 823 | h_offset = (char *)h - debug_alloc_pool; | ||
| 824 | if (h_offset < dah_first) { | ||
| 825 | h->next = dah_first; | ||
| 826 | dah_first = h_offset; | ||
| 827 | } else { | ||
| 828 | struct debug_alloc_header *prev; | ||
| 829 | unsigned int prev_offset; | ||
| 830 | prev = (struct debug_alloc_header *)(debug_alloc_pool + | ||
| 831 | dah_first); | ||
| 832 | while (1) { | ||
| 833 | if (!prev->next || prev->next > h_offset) | ||
| 834 | break; | ||
| 835 | prev = (struct debug_alloc_header *) | ||
| 836 | (debug_alloc_pool + prev->next); | ||
| 837 | } | ||
| 838 | prev_offset = (char *)prev - debug_alloc_pool; | ||
| 839 | if (prev_offset + dah_overhead + prev->size == h_offset) { | ||
| 840 | prev->size += dah_overhead + h->size; | ||
| 841 | memset(h, POISON_FREE, dah_overhead - 1); | ||
| 842 | *((char *)h + dah_overhead - 1) = POISON_END; | ||
| 843 | h = prev; | ||
| 844 | h_offset = prev_offset; | ||
| 845 | } else { | ||
| 846 | h->next = prev->next; | ||
| 847 | prev->next = h_offset; | ||
| 848 | } | ||
| 849 | } | ||
| 850 | if (h_offset + dah_overhead + h->size == h->next) { | ||
| 851 | struct debug_alloc_header *next; | ||
| 852 | next = (struct debug_alloc_header *) | ||
| 853 | (debug_alloc_pool + h->next); | ||
| 854 | h->size += dah_overhead + next->size; | ||
| 855 | h->next = next->next; | ||
| 856 | memset(next, POISON_FREE, dah_overhead - 1); | ||
| 857 | *((char *)next + dah_overhead - 1) = POISON_END; | ||
| 858 | } | ||
| 859 | spin_unlock(&dap_lock); | ||
| 860 | } | ||
| 861 | |||
| 862 | void debug_kusage(void) | ||
| 863 | { | ||
| 864 | struct debug_alloc_header *h_free, *h_used; | ||
| 865 | #ifdef CONFIG_IA64 | ||
| 866 | /* FIXME: using dah for ia64 unwind always results in a memory leak. | ||
| 867 | * Fix that memory leak first, then set debug_kusage_one_time = 1 for | ||
| 868 | * all architectures. | ||
| 869 | */ | ||
| 870 | static int debug_kusage_one_time; | ||
| 871 | #else | ||
| 872 | static int debug_kusage_one_time = 1; | ||
| 873 | #endif | ||
| 874 | if (!get_dap_lock()) { | ||
| 875 | __release(dap_lock); /* we never actually got it */ | ||
| 876 | return; | ||
| 877 | } | ||
| 878 | h_free = (struct debug_alloc_header *)(debug_alloc_pool + dah_first); | ||
| 879 | if (dah_first == 0 && | ||
| 880 | (h_free->size == sizeof(debug_alloc_pool_aligned) - dah_overhead || | ||
| 881 | dah_first_call)) | ||
| 882 | goto out; | ||
| 883 | if (!debug_kusage_one_time) | ||
| 884 | goto out; | ||
| 885 | debug_kusage_one_time = 0; | ||
| 886 | kdb_printf("%s: debug_kmalloc memory leak dah_first %d\n", | ||
| 887 | __func__, dah_first); | ||
| 888 | if (dah_first) { | ||
| 889 | h_used = (struct debug_alloc_header *)debug_alloc_pool; | ||
| 890 | kdb_printf("%s: h_used %p size %d\n", __func__, h_used, | ||
| 891 | h_used->size); | ||
| 892 | } | ||
| 893 | do { | ||
| 894 | h_used = (struct debug_alloc_header *) | ||
| 895 | ((char *)h_free + dah_overhead + h_free->size); | ||
| 896 | kdb_printf("%s: h_used %p size %d caller %p\n", | ||
| 897 | __func__, h_used, h_used->size, h_used->caller); | ||
| 898 | h_free = (struct debug_alloc_header *) | ||
| 899 | (debug_alloc_pool + h_free->next); | ||
| 900 | } while (h_free->next); | ||
| 901 | h_used = (struct debug_alloc_header *) | ||
| 902 | ((char *)h_free + dah_overhead + h_free->size); | ||
| 903 | if ((char *)h_used - debug_alloc_pool != | ||
| 904 | sizeof(debug_alloc_pool_aligned)) | ||
| 905 | kdb_printf("%s: h_used %p size %d caller %p\n", | ||
| 906 | __func__, h_used, h_used->size, h_used->caller); | ||
| 907 | out: | ||
| 908 | spin_unlock(&dap_lock); | ||
| 909 | } | ||
| 910 | |||
| 911 | /* Maintain a small stack of kdb_flags to allow recursion without disturbing | ||
| 912 | * the global kdb state. | ||
| 913 | */ | ||
| 914 | |||
| 915 | static int kdb_flags_stack[4], kdb_flags_index; | ||
| 916 | |||
| 917 | void kdb_save_flags(void) | ||
| 918 | { | ||
| 919 | BUG_ON(kdb_flags_index >= ARRAY_SIZE(kdb_flags_stack)); | ||
| 920 | kdb_flags_stack[kdb_flags_index++] = kdb_flags; | ||
| 921 | } | ||
| 922 | |||
| 923 | void kdb_restore_flags(void) | ||
| 924 | { | ||
| 925 | BUG_ON(kdb_flags_index <= 0); | ||
| 926 | kdb_flags = kdb_flags_stack[--kdb_flags_index]; | ||
| 927 | } | ||
