1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Kernel Debugger Architecture Independent Main Code 4 * 5 * Copyright (C) 1999-2004 Silicon Graphics, Inc. All Rights Reserved. 6 * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com> 7 * Xscale (R) modifications copyright (C) 2003 Intel Corporation. 8 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. 9 */ 10 11 #include <linux/ctype.h> 12 #include <linux/types.h> 13 #include <linux/string.h> 14 #include <linux/kernel.h> 15 #include <linux/kmsg_dump.h> 16 #include <linux/reboot.h> 17 #include <linux/sched.h> 18 #include <linux/sched/loadavg.h> 19 #include <linux/sched/stat.h> 20 #include <linux/sched/debug.h> 21 #include <linux/sysrq.h> 22 #include <linux/smp.h> 23 #include <linux/utsname.h> 24 #include <linux/vmalloc.h> 25 #include <linux/moduleparam.h> 26 #include <linux/mm.h> 27 #include <linux/init.h> 28 #include <linux/kallsyms.h> 29 #include <linux/kgdb.h> 30 #include <linux/kdb.h> 31 #include <linux/notifier.h> 32 #include <linux/interrupt.h> 33 #include <linux/delay.h> 34 #include <linux/nmi.h> 35 #include <linux/time.h> 36 #include <linux/ptrace.h> 37 #include <linux/sysctl.h> 38 #include <linux/cpu.h> 39 #include <linux/kdebug.h> 40 #include <linux/proc_fs.h> 41 #include <linux/uaccess.h> 42 #include <linux/slab.h> 43 #include <linux/security.h> 44 #include "kdb_private.h" 45 46 #undef MODULE_PARAM_PREFIX 47 #define MODULE_PARAM_PREFIX "kdb." 48 49 static int kdb_cmd_enabled = CONFIG_KDB_DEFAULT_ENABLE; 50 module_param_named(cmd_enable, kdb_cmd_enabled, int, 0600); 51 52 char kdb_grep_string[KDB_GREP_STRLEN]; 53 int kdb_grepping_flag; 54 EXPORT_SYMBOL(kdb_grepping_flag); 55 int kdb_grep_leading; 56 int kdb_grep_trailing; 57 58 /* 59 * Kernel debugger state flags 60 */ 61 unsigned int kdb_flags; 62 63 /* 64 * kdb_lock protects updates to kdb_initial_cpu. Used to 65 * single thread processors through the kernel debugger. 66 */ 67 int kdb_initial_cpu = -1; /* cpu number that owns kdb */ 68 int kdb_nextline = 1; 69 int kdb_state; /* General KDB state */ 70 71 struct task_struct *kdb_current_task; 72 struct pt_regs *kdb_current_regs; 73 74 const char *kdb_diemsg; 75 static int kdb_go_count; 76 #ifdef CONFIG_KDB_CONTINUE_CATASTROPHIC 77 static unsigned int kdb_continue_catastrophic = 78 CONFIG_KDB_CONTINUE_CATASTROPHIC; 79 #else 80 static unsigned int kdb_continue_catastrophic; 81 #endif 82 83 /* kdb_cmds_head describes the available commands. */ 84 static LIST_HEAD(kdb_cmds_head); 85 86 typedef struct _kdbmsg { 87 int km_diag; /* kdb diagnostic */ 88 char *km_msg; /* Corresponding message text */ 89 } kdbmsg_t; 90 91 #define KDBMSG(msgnum, text) \ 92 { KDB_##msgnum, text } 93 94 static kdbmsg_t kdbmsgs[] = { 95 KDBMSG(NOTFOUND, "Command Not Found"), 96 KDBMSG(ARGCOUNT, "Improper argument count, see usage."), 97 KDBMSG(BADWIDTH, "Illegal value for BYTESPERWORD use 1, 2, 4 or 8, " 98 "8 is only allowed on 64 bit systems"), 99 KDBMSG(BADRADIX, "Illegal value for RADIX use 8, 10 or 16"), 100 KDBMSG(NOTENV, "Cannot find environment variable"), 101 KDBMSG(NOENVVALUE, "Environment variable should have value"), 102 KDBMSG(NOTIMP, "Command not implemented"), 103 KDBMSG(ENVFULL, "Environment full"), 104 KDBMSG(KMALLOCFAILED, "Failed to allocate memory"), 105 KDBMSG(TOOMANYBPT, "Too many breakpoints defined"), 106 #ifdef CONFIG_CPU_XSCALE 107 KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"), 108 #else 109 KDBMSG(TOOMANYDBREGS, "More breakpoints than db registers defined"), 110 #endif 111 KDBMSG(DUPBPT, "Duplicate breakpoint address"), 112 KDBMSG(BPTNOTFOUND, "Breakpoint not found"), 113 KDBMSG(BADMODE, "Invalid IDMODE"), 114 KDBMSG(BADINT, "Illegal numeric value"), 115 KDBMSG(INVADDRFMT, "Invalid symbolic address format"), 116 KDBMSG(BADREG, "Invalid register name"), 117 KDBMSG(BADCPUNUM, "Invalid cpu number"), 118 KDBMSG(BADLENGTH, "Invalid length field"), 119 KDBMSG(NOBP, "No Breakpoint exists"), 120 KDBMSG(BADADDR, "Invalid address"), 121 KDBMSG(NOPERM, "Permission denied"), 122 }; 123 #undef KDBMSG 124 125 static const int __nkdb_err = ARRAY_SIZE(kdbmsgs); 126 127 128 /* 129 * Initial environment. This is all kept static and local to this file. 130 * The entire environment is limited to a fixed number of entries 131 * (add more to __env[] if required) 132 */ 133 134 static char *__env[31] = { 135 #if defined(CONFIG_SMP) 136 "PROMPT=[%d]kdb> ", 137 #else 138 "PROMPT=kdb> ", 139 #endif 140 "MOREPROMPT=more> ", 141 "RADIX=16", 142 "MDCOUNT=8", /* lines of md output */ 143 KDB_PLATFORM_ENV, 144 "DTABCOUNT=30", 145 "NOSECT=1", 146 }; 147 148 static const int __nenv = ARRAY_SIZE(__env); 149 150 /* 151 * Update the permissions flags (kdb_cmd_enabled) to match the 152 * current lockdown state. 153 * 154 * Within this function the calls to security_locked_down() are "lazy". We 155 * avoid calling them if the current value of kdb_cmd_enabled already excludes 156 * flags that might be subject to lockdown. Additionally we deliberately check 157 * the lockdown flags independently (even though read lockdown implies write 158 * lockdown) since that results in both simpler code and clearer messages to 159 * the user on first-time debugger entry. 160 * 161 * The permission masks during a read+write lockdown permits the following 162 * flags: INSPECT, SIGNAL, REBOOT (and ALWAYS_SAFE). 163 * 164 * The INSPECT commands are not blocked during lockdown because they are 165 * not arbitrary memory reads. INSPECT covers the backtrace family (sometimes 166 * forcing them to have no arguments) and lsmod. These commands do expose 167 * some kernel state but do not allow the developer seated at the console to 168 * choose what state is reported. SIGNAL and REBOOT should not be controversial, 169 * given these are allowed for root during lockdown already. 170 */ 171 static void kdb_check_for_lockdown(void) 172 { 173 const int write_flags = KDB_ENABLE_MEM_WRITE | 174 KDB_ENABLE_REG_WRITE | 175 KDB_ENABLE_FLOW_CTRL; 176 const int read_flags = KDB_ENABLE_MEM_READ | 177 KDB_ENABLE_REG_READ; 178 179 bool need_to_lockdown_write = false; 180 bool need_to_lockdown_read = false; 181 182 if (kdb_cmd_enabled & (KDB_ENABLE_ALL | write_flags)) 183 need_to_lockdown_write = 184 security_locked_down(LOCKDOWN_DBG_WRITE_KERNEL); 185 186 if (kdb_cmd_enabled & (KDB_ENABLE_ALL | read_flags)) 187 need_to_lockdown_read = 188 security_locked_down(LOCKDOWN_DBG_READ_KERNEL); 189 190 /* De-compose KDB_ENABLE_ALL if required */ 191 if (need_to_lockdown_write || need_to_lockdown_read) 192 if (kdb_cmd_enabled & KDB_ENABLE_ALL) 193 kdb_cmd_enabled = KDB_ENABLE_MASK & ~KDB_ENABLE_ALL; 194 195 if (need_to_lockdown_write) 196 kdb_cmd_enabled &= ~write_flags; 197 198 if (need_to_lockdown_read) 199 kdb_cmd_enabled &= ~read_flags; 200 } 201 202 /* 203 * Check whether the flags of the current command, the permissions of the kdb 204 * console and the lockdown state allow a command to be run. 205 */ 206 static bool kdb_check_flags(kdb_cmdflags_t flags, int permissions, 207 bool no_args) 208 { 209 /* permissions comes from userspace so needs massaging slightly */ 210 permissions &= KDB_ENABLE_MASK; 211 permissions |= KDB_ENABLE_ALWAYS_SAFE; 212 213 /* some commands change group when launched with no arguments */ 214 if (no_args) 215 permissions |= permissions << KDB_ENABLE_NO_ARGS_SHIFT; 216 217 flags |= KDB_ENABLE_ALL; 218 219 return permissions & flags; 220 } 221 222 /* 223 * kdbgetenv - This function will return the character string value of 224 * an environment variable. 225 * Parameters: 226 * match A character string representing an environment variable. 227 * Returns: 228 * NULL No environment variable matches 'match' 229 * char* Pointer to string value of environment variable. 230 */ 231 char *kdbgetenv(const char *match) 232 { 233 char **ep = __env; 234 int matchlen = strlen(match); 235 int i; 236 237 for (i = 0; i < __nenv; i++) { 238 char *e = *ep++; 239 240 if (!e) 241 continue; 242 243 if ((strncmp(match, e, matchlen) == 0) 244 && ((e[matchlen] == '\0') 245 || (e[matchlen] == '='))) { 246 char *cp = strchr(e, '='); 247 return cp ? ++cp : ""; 248 } 249 } 250 return NULL; 251 } 252 253 /* 254 * kdbgetulenv - This function will return the value of an unsigned 255 * long-valued environment variable. 256 * Parameters: 257 * match A character string representing a numeric value 258 * Outputs: 259 * *value the unsigned long representation of the env variable 'match' 260 * Returns: 261 * Zero on success, a kdb diagnostic on failure. 262 */ 263 static int kdbgetulenv(const char *match, unsigned long *value) 264 { 265 char *ep; 266 267 ep = kdbgetenv(match); 268 if (!ep) 269 return KDB_NOTENV; 270 if (strlen(ep) == 0) 271 return KDB_NOENVVALUE; 272 if (kstrtoul(ep, 0, value)) 273 return KDB_BADINT; 274 275 return 0; 276 } 277 278 /* 279 * kdbgetintenv - This function will return the value of an 280 * integer-valued environment variable. 281 * Parameters: 282 * match A character string representing an integer-valued env variable 283 * Outputs: 284 * *value the integer representation of the environment variable 'match' 285 * Returns: 286 * Zero on success, a kdb diagnostic on failure. 287 */ 288 int kdbgetintenv(const char *match, int *value) 289 { 290 unsigned long val; 291 int diag; 292 293 diag = kdbgetulenv(match, &val); 294 if (!diag) 295 *value = (int) val; 296 return diag; 297 } 298 299 /* 300 * kdb_setenv() - Alter an existing environment variable or create a new one. 301 * @var: Name of the variable 302 * @val: Value of the variable 303 * 304 * Return: Zero on success, a kdb diagnostic on failure. 305 */ 306 static int kdb_setenv(const char *var, const char *val) 307 { 308 int i; 309 char *ep; 310 size_t varlen, vallen; 311 312 varlen = strlen(var); 313 vallen = strlen(val); 314 ep = kmalloc(varlen + vallen + 2, GFP_KDB); 315 if (!ep) 316 return KDB_KMALLOCFAILED; 317 318 sprintf(ep, "%s=%s", var, val); 319 320 for (i = 0; i < __nenv; i++) { 321 if (__env[i] 322 && ((strncmp(__env[i], var, varlen) == 0) 323 && ((__env[i][varlen] == '\0') 324 || (__env[i][varlen] == '=')))) { 325 kfree_const(__env[i]); 326 __env[i] = ep; 327 return 0; 328 } 329 } 330 331 /* 332 * Wasn't existing variable. Fit into slot. 333 */ 334 for (i = 0; i < __nenv-1; i++) { 335 if (__env[i] == (char *)0) { 336 __env[i] = ep; 337 return 0; 338 } 339 } 340 341 kfree(ep); 342 return KDB_ENVFULL; 343 } 344 345 /* 346 * kdb_printenv() - Display the current environment variables. 347 */ 348 static void kdb_printenv(void) 349 { 350 int i; 351 352 for (i = 0; i < __nenv; i++) { 353 if (__env[i]) 354 kdb_printf("%s\n", __env[i]); 355 } 356 } 357 358 /* 359 * kdbgetularg - This function will convert a numeric string into an 360 * unsigned long value. 361 * Parameters: 362 * arg A character string representing a numeric value 363 * Outputs: 364 * *value the unsigned long representation of arg. 365 * Returns: 366 * Zero on success, a kdb diagnostic on failure. 367 */ 368 int kdbgetularg(const char *arg, unsigned long *value) 369 { 370 if (kstrtoul(arg, 0, value)) 371 return KDB_BADINT; 372 return 0; 373 } 374 375 int kdbgetu64arg(const char *arg, u64 *value) 376 { 377 if (kstrtou64(arg, 0, value)) 378 return KDB_BADINT; 379 return 0; 380 } 381 382 /* 383 * kdb_set - This function implements the 'set' command. Alter an 384 * existing environment variable or create a new one. 385 */ 386 int kdb_set(int argc, const char **argv) 387 { 388 /* 389 * we can be invoked two ways: 390 * set var=value argv[1]="var", argv[2]="value" 391 * set var = value argv[1]="var", argv[2]="=", argv[3]="value" 392 * - if the latter, shift 'em down. 393 */ 394 if (argc == 3) { 395 argv[2] = argv[3]; 396 argc--; 397 } 398 399 if (argc != 2) 400 return KDB_ARGCOUNT; 401 402 /* 403 * Censor sensitive variables 404 */ 405 if (strcmp(argv[1], "PROMPT") == 0 && 406 !kdb_check_flags(KDB_ENABLE_MEM_READ, kdb_cmd_enabled, false)) 407 return KDB_NOPERM; 408 409 /* 410 * Check for internal variables 411 */ 412 if (strcmp(argv[1], "KDBDEBUG") == 0) { 413 unsigned int debugflags; 414 int ret; 415 416 ret = kstrtouint(argv[2], 0, &debugflags); 417 if (ret || debugflags & ~KDB_DEBUG_FLAG_MASK) { 418 kdb_printf("kdb: illegal debug flags '%s'\n", 419 argv[2]); 420 return 0; 421 } 422 kdb_flags = (kdb_flags & ~KDB_DEBUG(MASK)) 423 | (debugflags << KDB_DEBUG_FLAG_SHIFT); 424 425 return 0; 426 } 427 428 /* 429 * Tokenizer squashed the '=' sign. argv[1] is variable 430 * name, argv[2] = value. 431 */ 432 return kdb_setenv(argv[1], argv[2]); 433 } 434 435 static int kdb_check_regs(void) 436 { 437 if (!kdb_current_regs) { 438 kdb_printf("No current kdb registers." 439 " You may need to select another task\n"); 440 return KDB_BADREG; 441 } 442 return 0; 443 } 444 445 /* 446 * kdbgetaddrarg - This function is responsible for parsing an 447 * address-expression and returning the value of the expression, 448 * symbol name, and offset to the caller. 449 * 450 * The argument may consist of a numeric value (decimal or 451 * hexadecimal), a symbol name, a register name (preceded by the 452 * percent sign), an environment variable with a numeric value 453 * (preceded by a dollar sign) or a simple arithmetic expression 454 * consisting of a symbol name, +/-, and a numeric constant value 455 * (offset). 456 * Parameters: 457 * argc - count of arguments in argv 458 * argv - argument vector 459 * *nextarg - index to next unparsed argument in argv[] 460 * regs - Register state at time of KDB entry 461 * Outputs: 462 * *value - receives the value of the address-expression 463 * *offset - receives the offset specified, if any 464 * *name - receives the symbol name, if any 465 * *nextarg - index to next unparsed argument in argv[] 466 * Returns: 467 * zero is returned on success, a kdb diagnostic code is 468 * returned on error. 469 */ 470 int kdbgetaddrarg(int argc, const char **argv, int *nextarg, 471 unsigned long *value, long *offset, 472 char **name) 473 { 474 unsigned long addr; 475 unsigned long off = 0; 476 int positive; 477 int diag; 478 int found = 0; 479 char *symname; 480 char symbol = '\0'; 481 char *cp; 482 kdb_symtab_t symtab; 483 484 /* 485 * If the enable flags prohibit both arbitrary memory access 486 * and flow control then there are no reasonable grounds to 487 * provide symbol lookup. 488 */ 489 if (!kdb_check_flags(KDB_ENABLE_MEM_READ | KDB_ENABLE_FLOW_CTRL, 490 kdb_cmd_enabled, false)) 491 return KDB_NOPERM; 492 493 /* 494 * Process arguments which follow the following syntax: 495 * 496 * symbol | numeric-address [+/- numeric-offset] 497 * %register 498 * $environment-variable 499 */ 500 501 if (*nextarg > argc) 502 return KDB_ARGCOUNT; 503 504 symname = (char *)argv[*nextarg]; 505 506 /* 507 * If there is no whitespace between the symbol 508 * or address and the '+' or '-' symbols, we 509 * remember the character and replace it with a 510 * null so the symbol/value can be properly parsed 511 */ 512 cp = strpbrk(symname, "+-"); 513 if (cp != NULL) { 514 symbol = *cp; 515 *cp++ = '\0'; 516 } 517 518 if (symname[0] == '$') { 519 diag = kdbgetulenv(&symname[1], &addr); 520 if (diag) 521 return diag; 522 } else if (symname[0] == '%') { 523 diag = kdb_check_regs(); 524 if (diag) 525 return diag; 526 /* Implement register values with % at a later time as it is 527 * arch optional. 528 */ 529 return KDB_NOTIMP; 530 } else { 531 found = kdbgetsymval(symname, &symtab); 532 if (found) { 533 addr = symtab.sym_start; 534 } else { 535 diag = kdbgetularg(argv[*nextarg], &addr); 536 if (diag) 537 return diag; 538 } 539 } 540 541 if (!found) 542 found = kdbnearsym(addr, &symtab); 543 544 (*nextarg)++; 545 546 if (name) 547 *name = symname; 548 if (value) 549 *value = addr; 550 if (offset && name && *name) 551 *offset = addr - symtab.sym_start; 552 553 if ((*nextarg > argc) 554 && (symbol == '\0')) 555 return 0; 556 557 /* 558 * check for +/- and offset 559 */ 560 561 if (symbol == '\0') { 562 if ((argv[*nextarg][0] != '+') 563 && (argv[*nextarg][0] != '-')) { 564 /* 565 * Not our argument. Return. 566 */ 567 return 0; 568 } else { 569 positive = (argv[*nextarg][0] == '+'); 570 (*nextarg)++; 571 } 572 } else 573 positive = (symbol == '+'); 574 575 /* 576 * Now there must be an offset! 577 */ 578 if ((*nextarg > argc) 579 && (symbol == '\0')) { 580 return KDB_INVADDRFMT; 581 } 582 583 if (!symbol) { 584 cp = (char *)argv[*nextarg]; 585 (*nextarg)++; 586 } 587 588 diag = kdbgetularg(cp, &off); 589 if (diag) 590 return diag; 591 592 if (!positive) 593 off = -off; 594 595 if (offset) 596 *offset += off; 597 598 if (value) 599 *value += off; 600 601 return 0; 602 } 603 604 static void kdb_cmderror(int diag) 605 { 606 int i; 607 608 if (diag >= 0) { 609 kdb_printf("no error detected (diagnostic is %d)\n", diag); 610 return; 611 } 612 613 for (i = 0; i < __nkdb_err; i++) { 614 if (kdbmsgs[i].km_diag == diag) { 615 kdb_printf("diag: %d: %s\n", diag, kdbmsgs[i].km_msg); 616 return; 617 } 618 } 619 620 kdb_printf("Unknown diag %d\n", -diag); 621 } 622 623 /* 624 * kdb_defcmd, kdb_defcmd2 - This function implements the 'defcmd' 625 * command which defines one command as a set of other commands, 626 * terminated by endefcmd. kdb_defcmd processes the initial 627 * 'defcmd' command, kdb_defcmd2 is invoked from kdb_parse for 628 * the following commands until 'endefcmd'. 629 * Inputs: 630 * argc argument count 631 * argv argument vector 632 * Returns: 633 * zero for success, a kdb diagnostic if error 634 */ 635 struct kdb_macro { 636 kdbtab_t cmd; /* Macro command */ 637 struct list_head statements; /* Associated statement list */ 638 }; 639 640 struct kdb_macro_statement { 641 char *statement; /* Statement text */ 642 struct list_head list_node; /* Statement list node */ 643 }; 644 645 static struct kdb_macro *kdb_macro; 646 static bool defcmd_in_progress; 647 648 /* Forward references */ 649 static int kdb_exec_defcmd(int argc, const char **argv); 650 651 static int kdb_defcmd2(const char *cmdstr, const char *argv0) 652 { 653 struct kdb_macro_statement *kms; 654 655 if (!kdb_macro) 656 return KDB_NOTIMP; 657 658 if (strcmp(argv0, "endefcmd") == 0) { 659 defcmd_in_progress = false; 660 if (!list_empty(&kdb_macro->statements)) 661 kdb_register(&kdb_macro->cmd); 662 return 0; 663 } 664 665 kms = kmalloc_obj(*kms, GFP_KDB); 666 if (!kms) { 667 kdb_printf("Could not allocate new kdb macro command: %s\n", 668 cmdstr); 669 return KDB_NOTIMP; 670 } 671 672 kms->statement = kdb_strdup(cmdstr, GFP_KDB); 673 list_add_tail(&kms->list_node, &kdb_macro->statements); 674 675 return 0; 676 } 677 678 static int kdb_defcmd(int argc, const char **argv) 679 { 680 kdbtab_t *mp; 681 682 if (defcmd_in_progress) { 683 kdb_printf("kdb: nested defcmd detected, assuming missing " 684 "endefcmd\n"); 685 kdb_defcmd2("endefcmd", "endefcmd"); 686 } 687 if (argc == 0) { 688 kdbtab_t *kp; 689 struct kdb_macro *kmp; 690 struct kdb_macro_statement *kms; 691 692 list_for_each_entry(kp, &kdb_cmds_head, list_node) { 693 if (kp->func == kdb_exec_defcmd) { 694 kdb_printf("defcmd %s \"%s\" \"%s\"\n", 695 kp->name, kp->usage, kp->help); 696 kmp = container_of(kp, struct kdb_macro, cmd); 697 list_for_each_entry(kms, &kmp->statements, 698 list_node) 699 kdb_printf("%s", kms->statement); 700 kdb_printf("endefcmd\n"); 701 } 702 } 703 return 0; 704 } 705 if (argc != 3) 706 return KDB_ARGCOUNT; 707 if (in_dbg_master()) { 708 kdb_printf("Command only available during kdb_init()\n"); 709 return KDB_NOTIMP; 710 } 711 kdb_macro = kzalloc_obj(*kdb_macro, GFP_KDB); 712 if (!kdb_macro) 713 goto fail_defcmd; 714 715 mp = &kdb_macro->cmd; 716 mp->func = kdb_exec_defcmd; 717 mp->minlen = 0; 718 mp->flags = KDB_ENABLE_ALWAYS_SAFE; 719 mp->name = kdb_strdup(argv[1], GFP_KDB); 720 if (!mp->name) 721 goto fail_name; 722 mp->usage = kdb_strdup_dequote(argv[2], GFP_KDB); 723 if (!mp->usage) 724 goto fail_usage; 725 mp->help = kdb_strdup_dequote(argv[3], GFP_KDB); 726 if (!mp->help) 727 goto fail_help; 728 729 INIT_LIST_HEAD(&kdb_macro->statements); 730 defcmd_in_progress = true; 731 return 0; 732 fail_help: 733 kfree(mp->usage); 734 fail_usage: 735 kfree(mp->name); 736 fail_name: 737 kfree(kdb_macro); 738 fail_defcmd: 739 kdb_printf("Could not allocate new kdb_macro entry for %s\n", argv[1]); 740 return KDB_NOTIMP; 741 } 742 743 /* 744 * kdb_exec_defcmd - Execute the set of commands associated with this 745 * defcmd name. 746 * Inputs: 747 * argc argument count 748 * argv argument vector 749 * Returns: 750 * zero for success, a kdb diagnostic if error 751 */ 752 static int kdb_exec_defcmd(int argc, const char **argv) 753 { 754 int ret; 755 kdbtab_t *kp; 756 struct kdb_macro *kmp; 757 struct kdb_macro_statement *kms; 758 759 if (argc != 0) 760 return KDB_ARGCOUNT; 761 762 list_for_each_entry(kp, &kdb_cmds_head, list_node) { 763 if (strcmp(kp->name, argv[0]) == 0) 764 break; 765 } 766 if (list_entry_is_head(kp, &kdb_cmds_head, list_node)) { 767 kdb_printf("kdb_exec_defcmd: could not find commands for %s\n", 768 argv[0]); 769 return KDB_NOTIMP; 770 } 771 kmp = container_of(kp, struct kdb_macro, cmd); 772 list_for_each_entry(kms, &kmp->statements, list_node) { 773 /* 774 * Recursive use of kdb_parse, do not use argv after this point. 775 */ 776 argv = NULL; 777 kdb_printf("[%s]kdb> %s\n", kmp->cmd.name, kms->statement); 778 ret = kdb_parse(kms->statement); 779 if (ret) 780 return ret; 781 } 782 return 0; 783 } 784 785 /* Command history */ 786 #define KDB_CMD_HISTORY_COUNT 32 787 static unsigned int cmd_head, cmd_tail; 788 static unsigned int cmdptr; 789 static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN]; 790 static char cmd_cur[CMD_BUFLEN]; 791 792 /* 793 * The "str" argument may point to something like | grep xyz 794 */ 795 static void parse_grep(const char *str) 796 { 797 int len; 798 char *cp = (char *)str, *cp2; 799 800 /* sanity check: we should have been called with the \ first */ 801 if (*cp != '|') 802 return; 803 cp++; 804 while (isspace(*cp)) 805 cp++; 806 if (!str_has_prefix(cp, "grep ")) { 807 kdb_printf("invalid 'pipe', see grephelp\n"); 808 return; 809 } 810 cp += 5; 811 while (isspace(*cp)) 812 cp++; 813 cp2 = strchr(cp, '\n'); 814 if (cp2) 815 *cp2 = '\0'; /* remove the trailing newline */ 816 len = strlen(cp); 817 if (len == 0) { 818 kdb_printf("invalid 'pipe', see grephelp\n"); 819 return; 820 } 821 /* now cp points to a nonzero length search string */ 822 if (*cp == '"') { 823 /* allow it be "x y z" by removing the "'s - there must 824 be two of them */ 825 cp++; 826 cp2 = strchr(cp, '"'); 827 if (!cp2) { 828 kdb_printf("invalid quoted string, see grephelp\n"); 829 return; 830 } 831 *cp2 = '\0'; /* end the string where the 2nd " was */ 832 } 833 kdb_grep_leading = 0; 834 if (*cp == '^') { 835 kdb_grep_leading = 1; 836 cp++; 837 } 838 len = strlen(cp); 839 kdb_grep_trailing = 0; 840 if (*(cp+len-1) == '$') { 841 kdb_grep_trailing = 1; 842 *(cp+len-1) = '\0'; 843 } 844 len = strlen(cp); 845 if (!len) 846 return; 847 if (len >= KDB_GREP_STRLEN) { 848 kdb_printf("search string too long\n"); 849 return; 850 } 851 memcpy(kdb_grep_string, cp, len + 1); 852 kdb_grepping_flag++; 853 return; 854 } 855 856 /* 857 * kdb_parse - Parse the command line, search the command table for a 858 * matching command and invoke the command function. This 859 * function may be called recursively, if it is, the second call 860 * will overwrite argv and cbuf. It is the caller's 861 * responsibility to save their argv if they recursively call 862 * kdb_parse(). 863 * Parameters: 864 * cmdstr The input command line to be parsed. 865 * regs The registers at the time kdb was entered. 866 * Returns: 867 * Zero for success, a kdb diagnostic if failure. 868 * Remarks: 869 * Limited to 20 tokens. 870 * 871 * Real rudimentary tokenization. Basically only whitespace 872 * is considered a token delimiter (but special consideration 873 * is taken of the '=' sign as used by the 'set' command). 874 * 875 * The algorithm used to tokenize the input string relies on 876 * there being at least one whitespace (or otherwise useless) 877 * character between tokens as the character immediately following 878 * the token is altered in-place to a null-byte to terminate the 879 * token string. 880 */ 881 882 #define MAXARGC 20 883 884 int kdb_parse(const char *cmdstr) 885 { 886 static char *argv[MAXARGC]; 887 static int argc; 888 static char cbuf[CMD_BUFLEN+2]; 889 char *cp; 890 char *cpp, quoted; 891 kdbtab_t *tp; 892 int escaped, ignore_errors = 0, check_grep = 0; 893 894 /* 895 * First tokenize the command string. 896 */ 897 cp = (char *)cmdstr; 898 899 if (KDB_FLAG(CMD_INTERRUPT)) { 900 /* Previous command was interrupted, newline must not 901 * repeat the command */ 902 KDB_FLAG_CLEAR(CMD_INTERRUPT); 903 KDB_STATE_SET(PAGER); 904 argc = 0; /* no repeat */ 905 } 906 907 if (*cp != '\n' && *cp != '\0') { 908 argc = 0; 909 cpp = cbuf; 910 while (*cp) { 911 /* skip whitespace */ 912 while (isspace(*cp)) 913 cp++; 914 if ((*cp == '\0') || (*cp == '\n') || 915 (*cp == '#' && !defcmd_in_progress)) 916 break; 917 /* special case: check for | grep pattern */ 918 if (*cp == '|') { 919 check_grep++; 920 break; 921 } 922 if (cpp >= cbuf + CMD_BUFLEN) { 923 kdb_printf("kdb_parse: command buffer " 924 "overflow, command ignored\n%s\n", 925 cmdstr); 926 return KDB_NOTFOUND; 927 } 928 if (argc >= MAXARGC - 1) { 929 kdb_printf("kdb_parse: too many arguments, " 930 "command ignored\n%s\n", cmdstr); 931 return KDB_NOTFOUND; 932 } 933 argv[argc++] = cpp; 934 escaped = 0; 935 quoted = '\0'; 936 /* Copy to next unquoted and unescaped 937 * whitespace or '=' */ 938 while (*cp && *cp != '\n' && 939 (escaped || quoted || !isspace(*cp))) { 940 if (cpp >= cbuf + CMD_BUFLEN) 941 break; 942 if (escaped) { 943 escaped = 0; 944 *cpp++ = *cp++; 945 continue; 946 } 947 if (*cp == '\\') { 948 escaped = 1; 949 ++cp; 950 continue; 951 } 952 if (*cp == quoted) 953 quoted = '\0'; 954 else if (*cp == '\'' || *cp == '"') 955 quoted = *cp; 956 *cpp = *cp++; 957 if (*cpp == '=' && !quoted) 958 break; 959 ++cpp; 960 } 961 *cpp++ = '\0'; /* Squash a ws or '=' character */ 962 } 963 } 964 if (!argc) 965 return 0; 966 if (check_grep) 967 parse_grep(cp); 968 if (defcmd_in_progress) { 969 int result = kdb_defcmd2(cmdstr, argv[0]); 970 if (!defcmd_in_progress) { 971 argc = 0; /* avoid repeat on endefcmd */ 972 *(argv[0]) = '\0'; 973 } 974 return result; 975 } 976 if (argv[0][0] == '-' && argv[0][1] && 977 (argv[0][1] < '0' || argv[0][1] > '9')) { 978 ignore_errors = 1; 979 ++argv[0]; 980 } 981 982 list_for_each_entry(tp, &kdb_cmds_head, list_node) { 983 /* 984 * If this command is allowed to be abbreviated, 985 * check to see if this is it. 986 */ 987 if (tp->minlen && (strlen(argv[0]) <= tp->minlen) && 988 (strncmp(argv[0], tp->name, tp->minlen) == 0)) 989 break; 990 991 if (strcmp(argv[0], tp->name) == 0) 992 break; 993 } 994 995 /* 996 * If we don't find a command by this name, see if the first 997 * few characters of this match any of the known commands. 998 * e.g., md1c20 should match md. 999 */ 1000 if (list_entry_is_head(tp, &kdb_cmds_head, list_node)) { 1001 list_for_each_entry(tp, &kdb_cmds_head, list_node) { 1002 if (strncmp(argv[0], tp->name, strlen(tp->name)) == 0) 1003 break; 1004 } 1005 } 1006 1007 if (!list_entry_is_head(tp, &kdb_cmds_head, list_node)) { 1008 int result; 1009 1010 if (!kdb_check_flags(tp->flags, kdb_cmd_enabled, argc <= 1)) 1011 return KDB_NOPERM; 1012 1013 KDB_STATE_SET(CMD); 1014 result = (*tp->func)(argc-1, (const char **)argv); 1015 if (result && ignore_errors && result > KDB_CMD_GO) 1016 result = 0; 1017 KDB_STATE_CLEAR(CMD); 1018 1019 if (tp->flags & KDB_REPEAT_WITH_ARGS) 1020 return result; 1021 1022 argc = tp->flags & KDB_REPEAT_NO_ARGS ? 1 : 0; 1023 if (argv[argc]) 1024 *(argv[argc]) = '\0'; 1025 return result; 1026 } 1027 1028 /* 1029 * If the input with which we were presented does not 1030 * map to an existing command, attempt to parse it as an 1031 * address argument and display the result. Useful for 1032 * obtaining the address of a variable, or the nearest symbol 1033 * to an address contained in a register. 1034 */ 1035 { 1036 unsigned long value; 1037 char *name = NULL; 1038 long offset; 1039 int nextarg = 0; 1040 1041 if (kdbgetaddrarg(0, (const char **)argv, &nextarg, 1042 &value, &offset, &name)) { 1043 return KDB_NOTFOUND; 1044 } 1045 1046 kdb_printf("%s = ", argv[0]); 1047 kdb_symbol_print(value, NULL, KDB_SP_DEFAULT); 1048 kdb_printf("\n"); 1049 return 0; 1050 } 1051 } 1052 1053 1054 static int handle_ctrl_cmd(char *cmd) 1055 { 1056 #define CTRL_P 16 1057 #define CTRL_N 14 1058 1059 /* initial situation */ 1060 if (cmd_head == cmd_tail) 1061 return 0; 1062 switch (*cmd) { 1063 case CTRL_P: 1064 if (cmdptr != cmd_tail) 1065 cmdptr = (cmdptr + KDB_CMD_HISTORY_COUNT - 1) % 1066 KDB_CMD_HISTORY_COUNT; 1067 strscpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN); 1068 return 1; 1069 case CTRL_N: 1070 if (cmdptr != cmd_head) 1071 cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT; 1072 strscpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN); 1073 return 1; 1074 } 1075 return 0; 1076 } 1077 1078 /* 1079 * kdb_reboot - This function implements the 'reboot' command. Reboot 1080 * the system immediately, or loop for ever on failure. 1081 */ 1082 static int kdb_reboot(int argc, const char **argv) 1083 { 1084 emergency_restart(); 1085 kdb_printf("Hmm, kdb_reboot did not reboot, spinning here\n"); 1086 while (1) 1087 cpu_relax(); 1088 /* NOTREACHED */ 1089 return 0; 1090 } 1091 1092 static void kdb_dumpregs(struct pt_regs *regs) 1093 { 1094 int old_lvl = console_loglevel; 1095 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH; 1096 kdb_trap_printk++; 1097 show_regs(regs); 1098 kdb_trap_printk--; 1099 kdb_printf("\n"); 1100 console_loglevel = old_lvl; 1101 } 1102 1103 static void kdb_set_current_task(struct task_struct *p) 1104 { 1105 kdb_current_task = p; 1106 1107 if (kdb_task_has_cpu(p)) { 1108 kdb_current_regs = KDB_TSKREGS(kdb_process_cpu(p)); 1109 return; 1110 } 1111 kdb_current_regs = NULL; 1112 } 1113 1114 static void drop_newline(char *buf) 1115 { 1116 size_t len = strlen(buf); 1117 1118 if (len == 0) 1119 return; 1120 if (*(buf + len - 1) == '\n') 1121 *(buf + len - 1) = '\0'; 1122 } 1123 1124 /* 1125 * kdb_local - The main code for kdb. This routine is invoked on a 1126 * specific processor, it is not global. The main kdb() routine 1127 * ensures that only one processor at a time is in this routine. 1128 * This code is called with the real reason code on the first 1129 * entry to a kdb session, thereafter it is called with reason 1130 * SWITCH, even if the user goes back to the original cpu. 1131 * Inputs: 1132 * reason The reason KDB was invoked 1133 * error The hardware-defined error code 1134 * regs The exception frame at time of fault/breakpoint. 1135 * db_result Result code from the break or debug point. 1136 * Returns: 1137 * 0 KDB was invoked for an event which it wasn't responsible 1138 * 1 KDB handled the event for which it was invoked. 1139 * KDB_CMD_GO User typed 'go'. 1140 * KDB_CMD_CPU User switched to another cpu. 1141 * KDB_CMD_SS Single step. 1142 */ 1143 static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs, 1144 kdb_dbtrap_t db_result) 1145 { 1146 char *cmdbuf; 1147 int diag; 1148 struct task_struct *kdb_current = 1149 curr_task(raw_smp_processor_id()); 1150 1151 KDB_DEBUG_STATE("kdb_local 1", reason); 1152 1153 kdb_check_for_lockdown(); 1154 1155 kdb_go_count = 0; 1156 if (reason == KDB_REASON_DEBUG) { 1157 /* special case below */ 1158 } else { 1159 kdb_printf("\nEntering kdb (current=0x%px, pid %d) ", 1160 kdb_current, kdb_current ? kdb_current->pid : 0); 1161 #if defined(CONFIG_SMP) 1162 kdb_printf("on processor %d ", raw_smp_processor_id()); 1163 #endif 1164 } 1165 1166 switch (reason) { 1167 case KDB_REASON_DEBUG: 1168 { 1169 /* 1170 * If re-entering kdb after a single step 1171 * command, don't print the message. 1172 */ 1173 switch (db_result) { 1174 case KDB_DB_BPT: 1175 kdb_printf("\nEntering kdb (0x%px, pid %d) ", 1176 kdb_current, kdb_current->pid); 1177 #if defined(CONFIG_SMP) 1178 kdb_printf("on processor %d ", raw_smp_processor_id()); 1179 #endif 1180 kdb_printf("due to Debug @ " kdb_machreg_fmt "\n", 1181 instruction_pointer(regs)); 1182 break; 1183 case KDB_DB_SS: 1184 break; 1185 case KDB_DB_SSBPT: 1186 KDB_DEBUG_STATE("kdb_local 4", reason); 1187 return 1; /* kdba_db_trap did the work */ 1188 default: 1189 kdb_printf("kdb: Bad result from kdba_db_trap: %d\n", 1190 db_result); 1191 break; 1192 } 1193 1194 } 1195 break; 1196 case KDB_REASON_ENTER: 1197 if (KDB_STATE(KEYBOARD)) 1198 kdb_printf("due to Keyboard Entry\n"); 1199 else 1200 kdb_printf("due to KDB_ENTER()\n"); 1201 break; 1202 case KDB_REASON_KEYBOARD: 1203 KDB_STATE_SET(KEYBOARD); 1204 kdb_printf("due to Keyboard Entry\n"); 1205 break; 1206 case KDB_REASON_ENTER_SLAVE: 1207 /* drop through, slaves only get released via cpu switch */ 1208 case KDB_REASON_SWITCH: 1209 kdb_printf("due to cpu switch\n"); 1210 break; 1211 case KDB_REASON_OOPS: 1212 kdb_printf("Oops: %s\n", kdb_diemsg); 1213 kdb_printf("due to oops @ " kdb_machreg_fmt "\n", 1214 instruction_pointer(regs)); 1215 kdb_dumpregs(regs); 1216 break; 1217 case KDB_REASON_SYSTEM_NMI: 1218 kdb_printf("due to System NonMaskable Interrupt\n"); 1219 break; 1220 case KDB_REASON_NMI: 1221 kdb_printf("due to NonMaskable Interrupt @ " 1222 kdb_machreg_fmt "\n", 1223 instruction_pointer(regs)); 1224 break; 1225 case KDB_REASON_SSTEP: 1226 case KDB_REASON_BREAK: 1227 kdb_printf("due to %s @ " kdb_machreg_fmt "\n", 1228 reason == KDB_REASON_BREAK ? 1229 "Breakpoint" : "SS trap", instruction_pointer(regs)); 1230 /* 1231 * Determine if this breakpoint is one that we 1232 * are interested in. 1233 */ 1234 if (db_result != KDB_DB_BPT) { 1235 kdb_printf("kdb: error return from kdba_bp_trap: %d\n", 1236 db_result); 1237 KDB_DEBUG_STATE("kdb_local 6", reason); 1238 return 0; /* Not for us, dismiss it */ 1239 } 1240 break; 1241 case KDB_REASON_RECURSE: 1242 kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n", 1243 instruction_pointer(regs)); 1244 break; 1245 default: 1246 kdb_printf("kdb: unexpected reason code: %d\n", reason); 1247 KDB_DEBUG_STATE("kdb_local 8", reason); 1248 return 0; /* Not for us, dismiss it */ 1249 } 1250 1251 while (1) { 1252 /* 1253 * Initialize pager context. 1254 */ 1255 kdb_nextline = 1; 1256 KDB_STATE_CLEAR(SUPPRESS); 1257 kdb_grepping_flag = 0; 1258 /* ensure the old search does not leak into '/' commands */ 1259 kdb_grep_string[0] = '\0'; 1260 1261 cmdbuf = cmd_cur; 1262 *cmdbuf = '\0'; 1263 *(cmd_hist[cmd_head]) = '\0'; 1264 1265 do_full_getstr: 1266 /* PROMPT can only be set if we have MEM_READ permission. */ 1267 snprintf(kdb_prompt_str, CMD_BUFLEN, 1268 kdbgetenv("PROMPT"), raw_smp_processor_id()); 1269 1270 /* 1271 * Fetch command from keyboard 1272 */ 1273 cmdbuf = kdb_getstr(cmdbuf, CMD_BUFLEN, kdb_prompt_str); 1274 if (*cmdbuf != '\n') { 1275 if (*cmdbuf < 32) { 1276 if (cmdptr == cmd_head) { 1277 strscpy(cmd_hist[cmd_head], cmd_cur, 1278 CMD_BUFLEN); 1279 *(cmd_hist[cmd_head] + 1280 strlen(cmd_hist[cmd_head])-1) = '\0'; 1281 } 1282 if (!handle_ctrl_cmd(cmdbuf)) 1283 *(cmd_cur+strlen(cmd_cur)-1) = '\0'; 1284 cmdbuf = cmd_cur; 1285 goto do_full_getstr; 1286 } else { 1287 strscpy(cmd_hist[cmd_head], cmd_cur, 1288 CMD_BUFLEN); 1289 } 1290 1291 cmd_head = (cmd_head+1) % KDB_CMD_HISTORY_COUNT; 1292 if (cmd_head == cmd_tail) 1293 cmd_tail = (cmd_tail+1) % KDB_CMD_HISTORY_COUNT; 1294 } 1295 1296 cmdptr = cmd_head; 1297 diag = kdb_parse(cmdbuf); 1298 if (diag == KDB_NOTFOUND) { 1299 drop_newline(cmdbuf); 1300 kdb_printf("Unknown kdb command: '%s'\n", cmdbuf); 1301 diag = 0; 1302 } 1303 if (diag == KDB_CMD_GO 1304 || diag == KDB_CMD_CPU 1305 || diag == KDB_CMD_SS 1306 || diag == KDB_CMD_KGDB) 1307 break; 1308 1309 if (diag) 1310 kdb_cmderror(diag); 1311 } 1312 KDB_DEBUG_STATE("kdb_local 9", diag); 1313 return diag; 1314 } 1315 1316 1317 /* 1318 * kdb_print_state - Print the state data for the current processor 1319 * for debugging. 1320 * Inputs: 1321 * text Identifies the debug point 1322 * value Any integer value to be printed, e.g. reason code. 1323 */ 1324 void kdb_print_state(const char *text, int value) 1325 { 1326 kdb_printf("state: %s cpu %d value %d initial %d state %x\n", 1327 text, raw_smp_processor_id(), value, kdb_initial_cpu, 1328 kdb_state); 1329 } 1330 1331 /* 1332 * kdb_main_loop - After initial setup and assignment of the 1333 * controlling cpu, all cpus are in this loop. One cpu is in 1334 * control and will issue the kdb prompt, the others will spin 1335 * until 'go' or cpu switch. 1336 * 1337 * To get a consistent view of the kernel stacks for all 1338 * processes, this routine is invoked from the main kdb code via 1339 * an architecture specific routine. kdba_main_loop is 1340 * responsible for making the kernel stacks consistent for all 1341 * processes, there should be no difference between a blocked 1342 * process and a running process as far as kdb is concerned. 1343 * Inputs: 1344 * reason The reason KDB was invoked 1345 * error The hardware-defined error code 1346 * reason2 kdb's current reason code. 1347 * Initially error but can change 1348 * according to kdb state. 1349 * db_result Result code from break or debug point. 1350 * regs The exception frame at time of fault/breakpoint. 1351 * should always be valid. 1352 * Returns: 1353 * 0 KDB was invoked for an event which it wasn't responsible 1354 * 1 KDB handled the event for which it was invoked. 1355 */ 1356 int kdb_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error, 1357 kdb_dbtrap_t db_result, struct pt_regs *regs) 1358 { 1359 int result = 1; 1360 /* Stay in kdb() until 'go', 'ss[b]' or an error */ 1361 while (1) { 1362 /* 1363 * All processors except the one that is in control 1364 * will spin here. 1365 */ 1366 KDB_DEBUG_STATE("kdb_main_loop 1", reason); 1367 while (KDB_STATE(HOLD_CPU)) { 1368 /* state KDB is turned off by kdb_cpu to see if the 1369 * other cpus are still live, each cpu in this loop 1370 * turns it back on. 1371 */ 1372 if (!KDB_STATE(KDB)) 1373 KDB_STATE_SET(KDB); 1374 } 1375 1376 KDB_STATE_CLEAR(SUPPRESS); 1377 KDB_DEBUG_STATE("kdb_main_loop 2", reason); 1378 if (KDB_STATE(LEAVING)) 1379 break; /* Another cpu said 'go' */ 1380 /* Still using kdb, this processor is in control */ 1381 result = kdb_local(reason2, error, regs, db_result); 1382 KDB_DEBUG_STATE("kdb_main_loop 3", result); 1383 1384 if (result == KDB_CMD_CPU) 1385 break; 1386 1387 if (result == KDB_CMD_SS) { 1388 KDB_STATE_SET(DOING_SS); 1389 break; 1390 } 1391 1392 if (result == KDB_CMD_KGDB) { 1393 if (!KDB_STATE(DOING_KGDB)) 1394 kdb_printf("Entering please attach debugger " 1395 "or use $D#44+ or $3#33\n"); 1396 break; 1397 } 1398 if (result && result != 1 && result != KDB_CMD_GO) 1399 kdb_printf("\nUnexpected kdb_local return code %d\n", 1400 result); 1401 KDB_DEBUG_STATE("kdb_main_loop 4", reason); 1402 break; 1403 } 1404 if (KDB_STATE(DOING_SS)) 1405 KDB_STATE_CLEAR(SSBPT); 1406 1407 /* Clean up any keyboard devices before leaving */ 1408 kdb_kbd_cleanup_state(); 1409 1410 return result; 1411 } 1412 1413 /* 1414 * kdb_mdr - This function implements the guts of the 'mdr', memory 1415 * read command. 1416 * mdr <addr arg>,<byte count> 1417 * Inputs: 1418 * addr Start address 1419 * count Number of bytes 1420 * Returns: 1421 * Always 0. Any errors are detected and printed by kdb_getarea. 1422 */ 1423 static int kdb_mdr(unsigned long addr, unsigned int count) 1424 { 1425 unsigned char c; 1426 while (count--) { 1427 if (kdb_getarea(c, addr)) 1428 return 0; 1429 kdb_printf("%02x", c); 1430 addr++; 1431 } 1432 kdb_printf("\n"); 1433 return 0; 1434 } 1435 1436 /* 1437 * kdb_md - This function implements the 'md', 'md1', 'md2', 'md4', 1438 * 'md8' 'mdr' and 'mds' commands. 1439 * 1440 * md|mds [<addr arg> [<line count> [<radix>]]] 1441 * mdWcN [<addr arg> [<line count> [<radix>]]] 1442 * where W = is the width (1, 2, 4 or 8) and N is the count. 1443 * for eg., md1c20 reads 20 bytes, 1 at a time. 1444 * mdr <addr arg>,<byte count> 1445 */ 1446 static void kdb_md_line(const char *fmtstr, unsigned long addr, 1447 int symbolic, int nosect, int bytesperword, 1448 int num, int repeat, int phys) 1449 { 1450 /* print just one line of data */ 1451 kdb_symtab_t symtab; 1452 char cbuf[32]; 1453 char *c = cbuf; 1454 int i; 1455 int j; 1456 unsigned long word; 1457 1458 memset(cbuf, '\0', sizeof(cbuf)); 1459 if (phys) 1460 kdb_printf("phys " kdb_machreg_fmt0 " ", addr); 1461 else 1462 kdb_printf(kdb_machreg_fmt0 " ", addr); 1463 1464 for (i = 0; i < num && repeat--; i++) { 1465 if (phys) { 1466 if (kdb_getphysword(&word, addr, bytesperword)) 1467 break; 1468 } else if (kdb_getword(&word, addr, bytesperword)) 1469 break; 1470 kdb_printf(fmtstr, word); 1471 if (symbolic) 1472 kdbnearsym(word, &symtab); 1473 else 1474 memset(&symtab, 0, sizeof(symtab)); 1475 if (symtab.sym_name) { 1476 kdb_symbol_print(word, &symtab, 0); 1477 if (!nosect) { 1478 kdb_printf("\n"); 1479 kdb_printf(" %s %s " 1480 kdb_machreg_fmt " " 1481 kdb_machreg_fmt " " 1482 kdb_machreg_fmt, symtab.mod_name, 1483 symtab.sec_name, symtab.sec_start, 1484 symtab.sym_start, symtab.sym_end); 1485 } 1486 addr += bytesperword; 1487 } else { 1488 union { 1489 u64 word; 1490 unsigned char c[8]; 1491 } wc; 1492 unsigned char *cp; 1493 #ifdef __BIG_ENDIAN 1494 cp = wc.c + 8 - bytesperword; 1495 #else 1496 cp = wc.c; 1497 #endif 1498 wc.word = word; 1499 #define printable_char(c) \ 1500 ({unsigned char __c = c; isascii(__c) && isprint(__c) ? __c : '.'; }) 1501 for (j = 0; j < bytesperword; j++) 1502 *c++ = printable_char(*cp++); 1503 addr += bytesperword; 1504 #undef printable_char 1505 } 1506 } 1507 kdb_printf("%*s %s\n", (int)((num-i)*(2*bytesperword + 1)+1), 1508 " ", cbuf); 1509 } 1510 1511 static int kdb_md(int argc, const char **argv) 1512 { 1513 static unsigned long last_addr; 1514 static int last_radix, last_bytesperword, last_repeat; 1515 int radix = 16, mdcount = 8, bytesperword = KDB_WORD_SIZE, repeat; 1516 int nosect = 0; 1517 char fmtchar, fmtstr[64]; 1518 unsigned long addr; 1519 unsigned long word; 1520 long offset = 0; 1521 int symbolic = 0; 1522 int valid = 0; 1523 int phys = 0; 1524 int raw = 0; 1525 1526 kdbgetintenv("MDCOUNT", &mdcount); 1527 kdbgetintenv("RADIX", &radix); 1528 kdbgetintenv("BYTESPERWORD", &bytesperword); 1529 1530 /* Assume 'md <addr>' and start with environment values */ 1531 repeat = mdcount * 16 / bytesperword; 1532 1533 if (strcmp(argv[0], "mdr") == 0) { 1534 if (argc == 2 || (argc == 0 && last_addr != 0)) 1535 valid = raw = 1; 1536 else 1537 return KDB_ARGCOUNT; 1538 } else if (isdigit(argv[0][2])) { 1539 bytesperword = (int)(argv[0][2] - '0'); 1540 if (bytesperword == 0) { 1541 bytesperword = last_bytesperword; 1542 if (bytesperword == 0) 1543 bytesperword = 4; 1544 } 1545 last_bytesperword = bytesperword; 1546 repeat = mdcount * 16 / bytesperword; 1547 if (!argv[0][3]) 1548 valid = 1; 1549 else if (argv[0][3] == 'c' && argv[0][4]) { 1550 if (kstrtouint(argv[0] + 4, 10, &repeat)) 1551 return KDB_BADINT; 1552 mdcount = ((repeat * bytesperword) + 15) / 16; 1553 valid = 1; 1554 } 1555 last_repeat = repeat; 1556 } else if (strcmp(argv[0], "md") == 0) 1557 valid = 1; 1558 else if (strcmp(argv[0], "mds") == 0) 1559 valid = 1; 1560 else if (strcmp(argv[0], "mdp") == 0) { 1561 phys = valid = 1; 1562 } 1563 if (!valid) 1564 return KDB_NOTFOUND; 1565 1566 if (argc == 0) { 1567 if (last_addr == 0) 1568 return KDB_ARGCOUNT; 1569 addr = last_addr; 1570 radix = last_radix; 1571 bytesperword = last_bytesperword; 1572 repeat = last_repeat; 1573 if (raw) 1574 mdcount = repeat; 1575 else 1576 mdcount = ((repeat * bytesperword) + 15) / 16; 1577 } 1578 1579 if (argc) { 1580 unsigned long val; 1581 int diag, nextarg = 1; 1582 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, 1583 &offset, NULL); 1584 if (diag) 1585 return diag; 1586 if (argc > nextarg+2) 1587 return KDB_ARGCOUNT; 1588 1589 if (argc >= nextarg) { 1590 diag = kdbgetularg(argv[nextarg], &val); 1591 if (!diag) { 1592 mdcount = (int) val; 1593 if (raw) 1594 repeat = mdcount; 1595 else 1596 repeat = mdcount * 16 / bytesperword; 1597 } 1598 } 1599 if (argc >= nextarg+1) { 1600 diag = kdbgetularg(argv[nextarg+1], &val); 1601 if (!diag) 1602 radix = (int) val; 1603 } 1604 } 1605 1606 if (strcmp(argv[0], "mdr") == 0) { 1607 int ret; 1608 last_addr = addr; 1609 ret = kdb_mdr(addr, mdcount); 1610 last_addr += mdcount; 1611 last_repeat = mdcount; 1612 last_bytesperword = bytesperword; // to make REPEAT happy 1613 return ret; 1614 } 1615 1616 switch (radix) { 1617 case 10: 1618 fmtchar = 'd'; 1619 break; 1620 case 16: 1621 fmtchar = 'x'; 1622 break; 1623 case 8: 1624 fmtchar = 'o'; 1625 break; 1626 default: 1627 return KDB_BADRADIX; 1628 } 1629 1630 last_radix = radix; 1631 1632 if (bytesperword > KDB_WORD_SIZE) 1633 return KDB_BADWIDTH; 1634 1635 switch (bytesperword) { 1636 case 8: 1637 sprintf(fmtstr, "%%16.16l%c ", fmtchar); 1638 break; 1639 case 4: 1640 sprintf(fmtstr, "%%8.8l%c ", fmtchar); 1641 break; 1642 case 2: 1643 sprintf(fmtstr, "%%4.4l%c ", fmtchar); 1644 break; 1645 case 1: 1646 sprintf(fmtstr, "%%2.2l%c ", fmtchar); 1647 break; 1648 default: 1649 return KDB_BADWIDTH; 1650 } 1651 1652 last_repeat = repeat; 1653 last_bytesperword = bytesperword; 1654 1655 if (strcmp(argv[0], "mds") == 0) { 1656 symbolic = 1; 1657 /* Do not save these changes as last_*, they are temporary mds 1658 * overrides. 1659 */ 1660 bytesperword = KDB_WORD_SIZE; 1661 repeat = mdcount; 1662 kdbgetintenv("NOSECT", &nosect); 1663 } 1664 1665 /* Round address down modulo BYTESPERWORD */ 1666 1667 addr &= ~(bytesperword-1); 1668 1669 while (repeat > 0) { 1670 unsigned long a; 1671 int n, z, num = (symbolic ? 1 : (16 / bytesperword)); 1672 1673 if (KDB_FLAG(CMD_INTERRUPT)) 1674 return 0; 1675 for (a = addr, z = 0; z < repeat; a += bytesperword, ++z) { 1676 if (phys) { 1677 if (kdb_getphysword(&word, a, bytesperword) 1678 || word) 1679 break; 1680 } else if (kdb_getword(&word, a, bytesperword) || word) 1681 break; 1682 } 1683 n = min(num, repeat); 1684 kdb_md_line(fmtstr, addr, symbolic, nosect, bytesperword, 1685 num, repeat, phys); 1686 addr += bytesperword * n; 1687 repeat -= n; 1688 z = (z + num - 1) / num; 1689 if (z > 2) { 1690 int s = num * (z-2); 1691 kdb_printf(kdb_machreg_fmt0 "-" kdb_machreg_fmt0 1692 " zero suppressed\n", 1693 addr, addr + bytesperword * s - 1); 1694 addr += bytesperword * s; 1695 repeat -= s; 1696 } 1697 } 1698 last_addr = addr; 1699 1700 return 0; 1701 } 1702 1703 /* 1704 * kdb_mm - This function implements the 'mm' command. 1705 * mm address-expression new-value 1706 * Remarks: 1707 * mm works on machine words, mmW works on bytes. 1708 */ 1709 static int kdb_mm(int argc, const char **argv) 1710 { 1711 int diag; 1712 unsigned long addr; 1713 long offset = 0; 1714 unsigned long contents; 1715 int nextarg; 1716 int width; 1717 1718 if (argv[0][2] && !isdigit(argv[0][2])) 1719 return KDB_NOTFOUND; 1720 1721 if (argc < 2) 1722 return KDB_ARGCOUNT; 1723 1724 nextarg = 1; 1725 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL); 1726 if (diag) 1727 return diag; 1728 1729 if (nextarg > argc) 1730 return KDB_ARGCOUNT; 1731 diag = kdbgetaddrarg(argc, argv, &nextarg, &contents, NULL, NULL); 1732 if (diag) 1733 return diag; 1734 1735 if (nextarg != argc + 1) 1736 return KDB_ARGCOUNT; 1737 1738 width = argv[0][2] ? (argv[0][2] - '0') : (KDB_WORD_SIZE); 1739 diag = kdb_putword(addr, contents, width); 1740 if (diag) 1741 return diag; 1742 1743 kdb_printf(kdb_machreg_fmt " = " kdb_machreg_fmt "\n", addr, contents); 1744 1745 return 0; 1746 } 1747 1748 /* 1749 * kdb_go - This function implements the 'go' command. 1750 * go [address-expression] 1751 */ 1752 static int kdb_go(int argc, const char **argv) 1753 { 1754 unsigned long addr; 1755 int diag; 1756 int nextarg; 1757 long offset; 1758 1759 if (raw_smp_processor_id() != kdb_initial_cpu) { 1760 kdb_printf("go must execute on the entry cpu, " 1761 "please use \"cpu %d\" and then execute go\n", 1762 kdb_initial_cpu); 1763 return KDB_BADCPUNUM; 1764 } 1765 if (argc == 1) { 1766 nextarg = 1; 1767 diag = kdbgetaddrarg(argc, argv, &nextarg, 1768 &addr, &offset, NULL); 1769 if (diag) 1770 return diag; 1771 } else if (argc) { 1772 return KDB_ARGCOUNT; 1773 } 1774 1775 diag = KDB_CMD_GO; 1776 if (KDB_FLAG(CATASTROPHIC)) { 1777 kdb_printf("Catastrophic error detected\n"); 1778 kdb_printf("kdb_continue_catastrophic=%d, ", 1779 kdb_continue_catastrophic); 1780 if (kdb_continue_catastrophic == 0 && kdb_go_count++ == 0) { 1781 kdb_printf("type go a second time if you really want " 1782 "to continue\n"); 1783 return 0; 1784 } 1785 if (kdb_continue_catastrophic == 2) { 1786 kdb_printf("forcing reboot\n"); 1787 kdb_reboot(0, NULL); 1788 } 1789 kdb_printf("attempting to continue\n"); 1790 } 1791 return diag; 1792 } 1793 1794 /* 1795 * kdb_rd - This function implements the 'rd' command. 1796 */ 1797 static int kdb_rd(int argc, const char **argv) 1798 { 1799 int len = kdb_check_regs(); 1800 #if DBG_MAX_REG_NUM > 0 1801 int i; 1802 char *rname; 1803 int rsize; 1804 u64 reg64; 1805 u32 reg32; 1806 u16 reg16; 1807 u8 reg8; 1808 1809 if (len) 1810 return len; 1811 1812 for (i = 0; i < DBG_MAX_REG_NUM; i++) { 1813 rsize = dbg_reg_def[i].size * 2; 1814 if (rsize > 16) 1815 rsize = 2; 1816 if (len + strlen(dbg_reg_def[i].name) + 4 + rsize > 80) { 1817 len = 0; 1818 kdb_printf("\n"); 1819 } 1820 if (len) 1821 len += kdb_printf(" "); 1822 switch(dbg_reg_def[i].size * 8) { 1823 case 8: 1824 rname = dbg_get_reg(i, ®8, kdb_current_regs); 1825 if (!rname) 1826 break; 1827 len += kdb_printf("%s: %02x", rname, reg8); 1828 break; 1829 case 16: 1830 rname = dbg_get_reg(i, ®16, kdb_current_regs); 1831 if (!rname) 1832 break; 1833 len += kdb_printf("%s: %04x", rname, reg16); 1834 break; 1835 case 32: 1836 rname = dbg_get_reg(i, ®32, kdb_current_regs); 1837 if (!rname) 1838 break; 1839 len += kdb_printf("%s: %08x", rname, reg32); 1840 break; 1841 case 64: 1842 rname = dbg_get_reg(i, ®64, kdb_current_regs); 1843 if (!rname) 1844 break; 1845 len += kdb_printf("%s: %016llx", rname, reg64); 1846 break; 1847 default: 1848 len += kdb_printf("%s: ??", dbg_reg_def[i].name); 1849 } 1850 } 1851 kdb_printf("\n"); 1852 #else 1853 if (len) 1854 return len; 1855 1856 kdb_dumpregs(kdb_current_regs); 1857 #endif 1858 return 0; 1859 } 1860 1861 /* 1862 * kdb_rm - This function implements the 'rm' (register modify) command. 1863 * rm register-name new-contents 1864 * Remarks: 1865 * Allows register modification with the same restrictions as gdb 1866 */ 1867 static int kdb_rm(int argc, const char **argv) 1868 { 1869 #if DBG_MAX_REG_NUM > 0 1870 int diag; 1871 const char *rname; 1872 int i; 1873 u64 reg64; 1874 u32 reg32; 1875 u16 reg16; 1876 u8 reg8; 1877 1878 if (argc != 2) 1879 return KDB_ARGCOUNT; 1880 /* 1881 * Allow presence or absence of leading '%' symbol. 1882 */ 1883 rname = argv[1]; 1884 if (*rname == '%') 1885 rname++; 1886 1887 diag = kdbgetu64arg(argv[2], ®64); 1888 if (diag) 1889 return diag; 1890 1891 diag = kdb_check_regs(); 1892 if (diag) 1893 return diag; 1894 1895 diag = KDB_BADREG; 1896 for (i = 0; i < DBG_MAX_REG_NUM; i++) { 1897 if (strcmp(rname, dbg_reg_def[i].name) == 0) { 1898 diag = 0; 1899 break; 1900 } 1901 } 1902 if (!diag) { 1903 switch(dbg_reg_def[i].size * 8) { 1904 case 8: 1905 reg8 = reg64; 1906 dbg_set_reg(i, ®8, kdb_current_regs); 1907 break; 1908 case 16: 1909 reg16 = reg64; 1910 dbg_set_reg(i, ®16, kdb_current_regs); 1911 break; 1912 case 32: 1913 reg32 = reg64; 1914 dbg_set_reg(i, ®32, kdb_current_regs); 1915 break; 1916 case 64: 1917 dbg_set_reg(i, ®64, kdb_current_regs); 1918 break; 1919 } 1920 } 1921 return diag; 1922 #else 1923 kdb_printf("ERROR: Register set currently not implemented\n"); 1924 return 0; 1925 #endif 1926 } 1927 1928 #if defined(CONFIG_MAGIC_SYSRQ) 1929 /* 1930 * kdb_sr - This function implements the 'sr' (SYSRQ key) command 1931 * which interfaces to the soi-disant MAGIC SYSRQ functionality. 1932 * sr <magic-sysrq-code> 1933 */ 1934 static int kdb_sr(int argc, const char **argv) 1935 { 1936 bool check_mask = 1937 !kdb_check_flags(KDB_ENABLE_ALL, kdb_cmd_enabled, false); 1938 1939 if (argc != 1) 1940 return KDB_ARGCOUNT; 1941 1942 kdb_trap_printk++; 1943 __handle_sysrq(*argv[1], check_mask); 1944 kdb_trap_printk--; 1945 1946 return 0; 1947 } 1948 #endif /* CONFIG_MAGIC_SYSRQ */ 1949 1950 /* 1951 * kdb_ef - This function implements the 'regs' (display exception 1952 * frame) command. This command takes an address and expects to 1953 * find an exception frame at that address, formats and prints 1954 * it. 1955 * regs address-expression 1956 * Remarks: 1957 * Not done yet. 1958 */ 1959 static int kdb_ef(int argc, const char **argv) 1960 { 1961 int diag; 1962 unsigned long addr; 1963 long offset; 1964 int nextarg; 1965 1966 if (argc != 1) 1967 return KDB_ARGCOUNT; 1968 1969 nextarg = 1; 1970 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL); 1971 if (diag) 1972 return diag; 1973 show_regs((struct pt_regs *)addr); 1974 return 0; 1975 } 1976 1977 /* 1978 * kdb_env - This function implements the 'env' command. Display the 1979 * current environment variables. 1980 */ 1981 1982 static int kdb_env(int argc, const char **argv) 1983 { 1984 kdb_printenv(); 1985 1986 if (KDB_DEBUG(MASK)) 1987 kdb_printf("KDBDEBUG=0x%x\n", 1988 (kdb_flags & KDB_DEBUG(MASK)) >> KDB_DEBUG_FLAG_SHIFT); 1989 1990 return 0; 1991 } 1992 1993 #ifdef CONFIG_PRINTK 1994 /* 1995 * kdb_dmesg - This function implements the 'dmesg' command to display 1996 * the contents of the syslog buffer. 1997 * dmesg [lines] [adjust] 1998 */ 1999 static int kdb_dmesg(int argc, const char **argv) 2000 { 2001 int diag; 2002 int logging; 2003 int lines = 0; 2004 int adjust = 0; 2005 int n = 0; 2006 int skip = 0; 2007 struct kmsg_dump_iter iter; 2008 size_t len; 2009 char buf[201]; 2010 2011 if (argc > 2) 2012 return KDB_ARGCOUNT; 2013 if (argc) { 2014 if (kstrtoint(argv[1], 0, &lines)) 2015 lines = 0; 2016 if (argc > 1 && (kstrtoint(argv[2], 0, &adjust) || adjust < 0)) 2017 adjust = 0; 2018 } 2019 2020 /* disable LOGGING if set */ 2021 diag = kdbgetintenv("LOGGING", &logging); 2022 if (!diag && logging) { 2023 const char *setargs[] = { "set", "LOGGING", "0" }; 2024 kdb_set(2, setargs); 2025 } 2026 2027 kmsg_dump_rewind(&iter); 2028 while (kmsg_dump_get_line(&iter, 1, NULL, 0, NULL)) 2029 n++; 2030 2031 if (lines < 0) { 2032 if (adjust >= n) 2033 kdb_printf("buffer only contains %d lines, nothing " 2034 "printed\n", n); 2035 else if (adjust - lines >= n) 2036 kdb_printf("buffer only contains %d lines, last %d " 2037 "lines printed\n", n, n - adjust); 2038 skip = adjust; 2039 lines = abs(lines); 2040 } else if (lines > 0) { 2041 skip = n - lines - adjust; 2042 lines = abs(lines); 2043 if (adjust >= n) { 2044 kdb_printf("buffer only contains %d lines, " 2045 "nothing printed\n", n); 2046 skip = n; 2047 } else if (skip < 0) { 2048 lines += skip; 2049 skip = 0; 2050 kdb_printf("buffer only contains %d lines, first " 2051 "%d lines printed\n", n, lines); 2052 } 2053 } else { 2054 lines = n; 2055 } 2056 2057 if (skip >= n || skip < 0) 2058 return 0; 2059 2060 kmsg_dump_rewind(&iter); 2061 while (kmsg_dump_get_line(&iter, 1, buf, sizeof(buf), &len)) { 2062 if (skip) { 2063 skip--; 2064 continue; 2065 } 2066 if (!lines--) 2067 break; 2068 if (KDB_FLAG(CMD_INTERRUPT)) 2069 return 0; 2070 2071 kdb_printf("%.*s\n", (int)len - 1, buf); 2072 } 2073 2074 return 0; 2075 } 2076 #endif /* CONFIG_PRINTK */ 2077 /* 2078 * kdb_cpu - This function implements the 'cpu' command. 2079 * cpu [<cpunum>] 2080 * Returns: 2081 * KDB_CMD_CPU for success, a kdb diagnostic if error 2082 */ 2083 static void kdb_cpu_status(void) 2084 { 2085 int i, start_cpu, first_print = 1; 2086 char state, prev_state = '?'; 2087 2088 kdb_printf("Currently on cpu %d\n", raw_smp_processor_id()); 2089 kdb_printf("Available cpus: "); 2090 for (start_cpu = -1, i = 0; i < NR_CPUS; i++) { 2091 if (!cpu_online(i)) { 2092 state = 'F'; /* cpu is offline */ 2093 } else if (!kgdb_info[i].enter_kgdb) { 2094 state = 'D'; /* cpu is online but unresponsive */ 2095 } else { 2096 state = ' '; /* cpu is responding to kdb */ 2097 if (kdb_task_state_char(KDB_TSK(i)) == '-') 2098 state = '-'; /* idle task */ 2099 } 2100 if (state != prev_state) { 2101 if (prev_state != '?') { 2102 if (!first_print) 2103 kdb_printf(", "); 2104 first_print = 0; 2105 kdb_printf("%d", start_cpu); 2106 if (start_cpu < i-1) 2107 kdb_printf("-%d", i-1); 2108 if (prev_state != ' ') 2109 kdb_printf("(%c)", prev_state); 2110 } 2111 prev_state = state; 2112 start_cpu = i; 2113 } 2114 } 2115 /* print the trailing cpus, ignoring them if they are all offline */ 2116 if (prev_state != 'F') { 2117 if (!first_print) 2118 kdb_printf(", "); 2119 kdb_printf("%d", start_cpu); 2120 if (start_cpu < i-1) 2121 kdb_printf("-%d", i-1); 2122 if (prev_state != ' ') 2123 kdb_printf("(%c)", prev_state); 2124 } 2125 kdb_printf("\n"); 2126 } 2127 2128 static int kdb_cpu(int argc, const char **argv) 2129 { 2130 unsigned long cpunum; 2131 int diag; 2132 2133 if (argc == 0) { 2134 kdb_cpu_status(); 2135 return 0; 2136 } 2137 2138 if (argc != 1) 2139 return KDB_ARGCOUNT; 2140 2141 diag = kdbgetularg(argv[1], &cpunum); 2142 if (diag) 2143 return diag; 2144 2145 /* 2146 * Validate cpunum 2147 */ 2148 if ((cpunum >= CONFIG_NR_CPUS) || !kgdb_info[cpunum].enter_kgdb) 2149 return KDB_BADCPUNUM; 2150 2151 dbg_switch_cpu = cpunum; 2152 2153 /* 2154 * Switch to other cpu 2155 */ 2156 return KDB_CMD_CPU; 2157 } 2158 2159 /* The user may not realize that ps/bta with no parameters does not print idle 2160 * or sleeping system daemon processes, so tell them how many were suppressed. 2161 */ 2162 void kdb_ps_suppressed(void) 2163 { 2164 int idle = 0, daemon = 0; 2165 unsigned long cpu; 2166 const struct task_struct *p, *g; 2167 for_each_online_cpu(cpu) { 2168 p = curr_task(cpu); 2169 if (kdb_task_state(p, "-")) 2170 ++idle; 2171 } 2172 for_each_process_thread(g, p) { 2173 if (kdb_task_state(p, "ims")) 2174 ++daemon; 2175 } 2176 if (idle || daemon) { 2177 if (idle) 2178 kdb_printf("%d idle process%s (state -)%s\n", 2179 idle, idle == 1 ? "" : "es", 2180 daemon ? " and " : ""); 2181 if (daemon) 2182 kdb_printf("%d sleeping system daemon (state [ims]) " 2183 "process%s", daemon, 2184 daemon == 1 ? "" : "es"); 2185 kdb_printf(" suppressed,\nuse 'ps A' to see all.\n"); 2186 } 2187 } 2188 2189 void kdb_ps1(const struct task_struct *p) 2190 { 2191 int cpu; 2192 unsigned long tmp; 2193 2194 if (!p || 2195 copy_from_kernel_nofault(&tmp, (char *)p, sizeof(unsigned long))) 2196 return; 2197 2198 cpu = kdb_process_cpu(p); 2199 kdb_printf("0x%px %8d %8d %d %4d %c 0x%px %c%s\n", 2200 (void *)p, p->pid, p->parent->pid, 2201 kdb_task_has_cpu(p), kdb_process_cpu(p), 2202 kdb_task_state_char(p), 2203 (void *)(&p->thread), 2204 p == curr_task(raw_smp_processor_id()) ? '*' : ' ', 2205 p->comm); 2206 if (kdb_task_has_cpu(p)) { 2207 if (!KDB_TSK(cpu)) { 2208 kdb_printf(" Error: no saved data for this cpu\n"); 2209 } else { 2210 if (KDB_TSK(cpu) != p) 2211 kdb_printf(" Error: does not match running " 2212 "process table (0x%px)\n", KDB_TSK(cpu)); 2213 } 2214 } 2215 } 2216 2217 /* 2218 * kdb_ps - This function implements the 'ps' command which shows a 2219 * list of the active processes. 2220 * 2221 * ps [<state_chars>] Show processes, optionally selecting only those whose 2222 * state character is found in <state_chars>. 2223 */ 2224 static int kdb_ps(int argc, const char **argv) 2225 { 2226 struct task_struct *g, *p; 2227 const char *mask; 2228 unsigned long cpu; 2229 2230 if (argc == 0) 2231 kdb_ps_suppressed(); 2232 kdb_printf("%-*s Pid Parent [*] cpu State %-*s Command\n", 2233 (int)(2*sizeof(void *))+2, "Task Addr", 2234 (int)(2*sizeof(void *))+2, "Thread"); 2235 mask = argc ? argv[1] : kdbgetenv("PS"); 2236 /* Run the active tasks first */ 2237 for_each_online_cpu(cpu) { 2238 if (KDB_FLAG(CMD_INTERRUPT)) 2239 return 0; 2240 p = curr_task(cpu); 2241 if (kdb_task_state(p, mask)) 2242 kdb_ps1(p); 2243 } 2244 kdb_printf("\n"); 2245 /* Now the real tasks */ 2246 for_each_process_thread(g, p) { 2247 if (KDB_FLAG(CMD_INTERRUPT)) 2248 return 0; 2249 if (kdb_task_state(p, mask)) 2250 kdb_ps1(p); 2251 } 2252 2253 return 0; 2254 } 2255 2256 /* 2257 * kdb_pid - This function implements the 'pid' command which switches 2258 * the currently active process. 2259 * pid [<pid> | R] 2260 */ 2261 static int kdb_pid(int argc, const char **argv) 2262 { 2263 struct task_struct *p; 2264 unsigned long val; 2265 int diag; 2266 2267 if (argc > 1) 2268 return KDB_ARGCOUNT; 2269 2270 if (argc) { 2271 if (strcmp(argv[1], "R") == 0) { 2272 p = KDB_TSK(kdb_initial_cpu); 2273 } else { 2274 diag = kdbgetularg(argv[1], &val); 2275 if (diag) 2276 return KDB_BADINT; 2277 2278 p = find_task_by_pid_ns((pid_t)val, &init_pid_ns); 2279 if (!p) { 2280 kdb_printf("No task with pid=%d\n", (pid_t)val); 2281 return 0; 2282 } 2283 } 2284 kdb_set_current_task(p); 2285 } 2286 kdb_printf("KDB current process is %s(pid=%d)\n", 2287 kdb_current_task->comm, 2288 kdb_current_task->pid); 2289 2290 return 0; 2291 } 2292 2293 static int kdb_kgdb(int argc, const char **argv) 2294 { 2295 return KDB_CMD_KGDB; 2296 } 2297 2298 /* 2299 * kdb_help - This function implements the 'help' and '?' commands. 2300 */ 2301 static int kdb_help(int argc, const char **argv) 2302 { 2303 kdbtab_t *kt; 2304 2305 kdb_printf("%-15.15s %-20.20s %s\n", "Command", "Usage", "Description"); 2306 kdb_printf("-----------------------------" 2307 "-----------------------------\n"); 2308 list_for_each_entry(kt, &kdb_cmds_head, list_node) { 2309 char *space = ""; 2310 if (KDB_FLAG(CMD_INTERRUPT)) 2311 return 0; 2312 if (!kdb_check_flags(kt->flags, kdb_cmd_enabled, true)) 2313 continue; 2314 if (strlen(kt->usage) > 20) 2315 space = "\n "; 2316 kdb_printf("%-15.15s %-20s%s%s\n", kt->name, 2317 kt->usage, space, kt->help); 2318 } 2319 return 0; 2320 } 2321 2322 /* 2323 * kdb_kill - This function implements the 'kill' commands. 2324 */ 2325 static int kdb_kill(int argc, const char **argv) 2326 { 2327 long sig, pid; 2328 struct task_struct *p; 2329 2330 if (argc != 2) 2331 return KDB_ARGCOUNT; 2332 2333 if (kstrtol(argv[1], 0, &sig)) 2334 return KDB_BADINT; 2335 if ((sig >= 0) || !valid_signal(-sig)) { 2336 kdb_printf("Invalid signal parameter.<-signal>\n"); 2337 return 0; 2338 } 2339 sig = -sig; 2340 2341 if (kstrtol(argv[2], 0, &pid)) 2342 return KDB_BADINT; 2343 if (pid <= 0) { 2344 kdb_printf("Process ID must be large than 0.\n"); 2345 return 0; 2346 } 2347 2348 /* Find the process. */ 2349 p = find_task_by_pid_ns(pid, &init_pid_ns); 2350 if (!p) { 2351 kdb_printf("The specified process isn't found.\n"); 2352 return 0; 2353 } 2354 p = p->group_leader; 2355 kdb_send_sig(p, sig); 2356 return 0; 2357 } 2358 2359 /* 2360 * Most of this code has been lifted from kernel/timer.c::sys_sysinfo(). 2361 * I cannot call that code directly from kdb, it has an unconditional 2362 * cli()/sti() and calls routines that take locks which can stop the debugger. 2363 */ 2364 static void kdb_sysinfo(struct sysinfo *val) 2365 { 2366 u64 uptime = ktime_get_mono_fast_ns(); 2367 2368 memset(val, 0, sizeof(*val)); 2369 val->uptime = div_u64(uptime, NSEC_PER_SEC); 2370 val->loads[0] = avenrun[0]; 2371 val->loads[1] = avenrun[1]; 2372 val->loads[2] = avenrun[2]; 2373 val->procs = nr_threads-1; 2374 si_meminfo(val); 2375 2376 return; 2377 } 2378 2379 /* 2380 * kdb_summary - This function implements the 'summary' command. 2381 */ 2382 static int kdb_summary(int argc, const char **argv) 2383 { 2384 time64_t now; 2385 struct sysinfo val; 2386 2387 if (argc) 2388 return KDB_ARGCOUNT; 2389 2390 kdb_printf("sysname %s\n", init_uts_ns.name.sysname); 2391 kdb_printf("release %s\n", init_uts_ns.name.release); 2392 kdb_printf("version %s\n", init_uts_ns.name.version); 2393 kdb_printf("machine %s\n", init_uts_ns.name.machine); 2394 kdb_printf("nodename %s\n", init_uts_ns.name.nodename); 2395 kdb_printf("domainname %s\n", init_uts_ns.name.domainname); 2396 2397 now = __ktime_get_real_seconds(); 2398 kdb_printf("date %ptTs tz_minuteswest %d\n", &now, sys_tz.tz_minuteswest); 2399 kdb_sysinfo(&val); 2400 kdb_printf("uptime "); 2401 if (val.uptime > (24*60*60)) { 2402 int days = val.uptime / (24*60*60); 2403 val.uptime %= (24*60*60); 2404 kdb_printf("%d day%s ", days, str_plural(days)); 2405 } 2406 kdb_printf("%02ld:%02ld\n", val.uptime/(60*60), (val.uptime/60)%60); 2407 2408 kdb_printf("load avg %ld.%02ld %ld.%02ld %ld.%02ld\n", 2409 LOAD_INT(val.loads[0]), LOAD_FRAC(val.loads[0]), 2410 LOAD_INT(val.loads[1]), LOAD_FRAC(val.loads[1]), 2411 LOAD_INT(val.loads[2]), LOAD_FRAC(val.loads[2])); 2412 2413 /* Display in kilobytes */ 2414 #define K(x) ((x) << (PAGE_SHIFT - 10)) 2415 kdb_printf("\nMemTotal: %8lu kB\nMemFree: %8lu kB\n" 2416 "Buffers: %8lu kB\n", 2417 K(val.totalram), K(val.freeram), K(val.bufferram)); 2418 return 0; 2419 } 2420 2421 /* 2422 * kdb_per_cpu - This function implements the 'per_cpu' command. 2423 */ 2424 static int kdb_per_cpu(int argc, const char **argv) 2425 { 2426 char fmtstr[64]; 2427 int cpu, diag, nextarg = 1; 2428 unsigned long addr, symaddr, val, bytesperword = 0, whichcpu = ~0UL; 2429 2430 if (argc < 1 || argc > 3) 2431 return KDB_ARGCOUNT; 2432 2433 diag = kdbgetaddrarg(argc, argv, &nextarg, &symaddr, NULL, NULL); 2434 if (diag) 2435 return diag; 2436 2437 if (argc >= 2) { 2438 diag = kdbgetularg(argv[2], &bytesperword); 2439 if (diag) 2440 return diag; 2441 } 2442 if (!bytesperword) 2443 bytesperword = KDB_WORD_SIZE; 2444 else if (bytesperword > KDB_WORD_SIZE) 2445 return KDB_BADWIDTH; 2446 sprintf(fmtstr, "%%0%dlx ", (int)(2*bytesperword)); 2447 if (argc >= 3) { 2448 diag = kdbgetularg(argv[3], &whichcpu); 2449 if (diag) 2450 return diag; 2451 if (whichcpu >= nr_cpu_ids || !cpu_online(whichcpu)) { 2452 kdb_printf("cpu %ld is not online\n", whichcpu); 2453 return KDB_BADCPUNUM; 2454 } 2455 } 2456 2457 /* Most architectures use __per_cpu_offset[cpu], some use 2458 * __per_cpu_offset(cpu), smp has no __per_cpu_offset. 2459 */ 2460 #ifdef __per_cpu_offset 2461 #define KDB_PCU(cpu) __per_cpu_offset(cpu) 2462 #else 2463 #ifdef CONFIG_SMP 2464 #define KDB_PCU(cpu) __per_cpu_offset[cpu] 2465 #else 2466 #define KDB_PCU(cpu) 0 2467 #endif 2468 #endif 2469 for_each_online_cpu(cpu) { 2470 if (KDB_FLAG(CMD_INTERRUPT)) 2471 return 0; 2472 2473 if (whichcpu != ~0UL && whichcpu != cpu) 2474 continue; 2475 addr = symaddr + KDB_PCU(cpu); 2476 diag = kdb_getword(&val, addr, bytesperword); 2477 if (diag) { 2478 kdb_printf("%5d " kdb_bfd_vma_fmt0 " - unable to " 2479 "read, diag=%d\n", cpu, addr, diag); 2480 continue; 2481 } 2482 kdb_printf("%5d ", cpu); 2483 kdb_md_line(fmtstr, addr, 2484 bytesperword == KDB_WORD_SIZE, 2485 1, bytesperword, 1, 1, 0); 2486 } 2487 #undef KDB_PCU 2488 return 0; 2489 } 2490 2491 /* 2492 * display help for the use of cmd | grep pattern 2493 */ 2494 static int kdb_grep_help(int argc, const char **argv) 2495 { 2496 kdb_printf("Usage of cmd args | grep pattern:\n"); 2497 kdb_printf(" Any command's output may be filtered through an "); 2498 kdb_printf("emulated 'pipe'.\n"); 2499 kdb_printf(" 'grep' is just a key word.\n"); 2500 kdb_printf(" The pattern may include a very limited set of " 2501 "metacharacters:\n"); 2502 kdb_printf(" pattern or ^pattern or pattern$ or ^pattern$\n"); 2503 kdb_printf(" And if there are spaces in the pattern, you may " 2504 "quote it:\n"); 2505 kdb_printf(" \"pat tern\" or \"^pat tern\" or \"pat tern$\"" 2506 " or \"^pat tern$\"\n"); 2507 return 0; 2508 } 2509 2510 /** 2511 * kdb_register() - This function is used to register a kernel debugger 2512 * command. 2513 * @cmd: pointer to kdb command 2514 * 2515 * Note that it's the job of the caller to keep the memory for the cmd 2516 * allocated until unregister is called. 2517 */ 2518 int kdb_register(kdbtab_t *cmd) 2519 { 2520 kdbtab_t *kp; 2521 2522 list_for_each_entry(kp, &kdb_cmds_head, list_node) { 2523 if (strcmp(kp->name, cmd->name) == 0) { 2524 kdb_printf("Duplicate kdb cmd: %s, func %p help %s\n", 2525 cmd->name, cmd->func, cmd->help); 2526 return 1; 2527 } 2528 } 2529 2530 list_add_tail(&cmd->list_node, &kdb_cmds_head); 2531 return 0; 2532 } 2533 EXPORT_SYMBOL_GPL(kdb_register); 2534 2535 /** 2536 * kdb_register_table() - This function is used to register a kdb command 2537 * table. 2538 * @kp: pointer to kdb command table 2539 * @len: length of kdb command table 2540 */ 2541 void kdb_register_table(kdbtab_t *kp, size_t len) 2542 { 2543 while (len--) { 2544 list_add_tail(&kp->list_node, &kdb_cmds_head); 2545 kp++; 2546 } 2547 } 2548 2549 /** 2550 * kdb_unregister() - This function is used to unregister a kernel debugger 2551 * command. It is generally called when a module which 2552 * implements kdb command is unloaded. 2553 * @cmd: pointer to kdb command 2554 */ 2555 void kdb_unregister(kdbtab_t *cmd) 2556 { 2557 list_del(&cmd->list_node); 2558 } 2559 EXPORT_SYMBOL_GPL(kdb_unregister); 2560 2561 static kdbtab_t maintab[] = { 2562 { .name = "md", 2563 .func = kdb_md, 2564 .usage = "<vaddr>", 2565 .help = "Display Memory Contents, also mdWcN, e.g. md8c1", 2566 .minlen = 1, 2567 .flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS, 2568 }, 2569 { .name = "mdr", 2570 .func = kdb_md, 2571 .usage = "<vaddr> <bytes>", 2572 .help = "Display Raw Memory", 2573 .flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS, 2574 }, 2575 { .name = "mdp", 2576 .func = kdb_md, 2577 .usage = "<paddr> <bytes>", 2578 .help = "Display Physical Memory", 2579 .flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS, 2580 }, 2581 { .name = "mds", 2582 .func = kdb_md, 2583 .usage = "<vaddr>", 2584 .help = "Display Memory Symbolically", 2585 .flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS, 2586 }, 2587 { .name = "mm", 2588 .func = kdb_mm, 2589 .usage = "<vaddr> <contents>", 2590 .help = "Modify Memory Contents", 2591 .flags = KDB_ENABLE_MEM_WRITE | KDB_REPEAT_NO_ARGS, 2592 }, 2593 { .name = "go", 2594 .func = kdb_go, 2595 .usage = "[<vaddr>]", 2596 .help = "Continue Execution", 2597 .minlen = 1, 2598 .flags = KDB_ENABLE_REG_WRITE | 2599 KDB_ENABLE_ALWAYS_SAFE_NO_ARGS, 2600 }, 2601 { .name = "rd", 2602 .func = kdb_rd, 2603 .usage = "", 2604 .help = "Display Registers", 2605 .flags = KDB_ENABLE_REG_READ, 2606 }, 2607 { .name = "rm", 2608 .func = kdb_rm, 2609 .usage = "<reg> <contents>", 2610 .help = "Modify Registers", 2611 .flags = KDB_ENABLE_REG_WRITE, 2612 }, 2613 { .name = "ef", 2614 .func = kdb_ef, 2615 .usage = "<vaddr>", 2616 .help = "Display exception frame", 2617 .flags = KDB_ENABLE_MEM_READ, 2618 }, 2619 { .name = "bt", 2620 .func = kdb_bt, 2621 .usage = "[<vaddr>]", 2622 .help = "Stack traceback", 2623 .minlen = 1, 2624 .flags = KDB_ENABLE_MEM_READ | KDB_ENABLE_INSPECT_NO_ARGS, 2625 }, 2626 { .name = "btp", 2627 .func = kdb_bt, 2628 .usage = "<pid>", 2629 .help = "Display stack for process <pid>", 2630 .flags = KDB_ENABLE_INSPECT, 2631 }, 2632 { .name = "bta", 2633 .func = kdb_bt, 2634 .usage = "[<state_chars>|A]", 2635 .help = "Backtrace all processes whose state matches", 2636 .flags = KDB_ENABLE_INSPECT, 2637 }, 2638 { .name = "btc", 2639 .func = kdb_bt, 2640 .usage = "", 2641 .help = "Backtrace current process on each cpu", 2642 .flags = KDB_ENABLE_INSPECT, 2643 }, 2644 { .name = "btt", 2645 .func = kdb_bt, 2646 .usage = "<vaddr>", 2647 .help = "Backtrace process given its struct task address", 2648 .flags = KDB_ENABLE_MEM_READ | KDB_ENABLE_INSPECT_NO_ARGS, 2649 }, 2650 { .name = "env", 2651 .func = kdb_env, 2652 .usage = "", 2653 .help = "Show environment variables", 2654 .flags = KDB_ENABLE_ALWAYS_SAFE, 2655 }, 2656 { .name = "set", 2657 .func = kdb_set, 2658 .usage = "", 2659 .help = "Set environment variables", 2660 .flags = KDB_ENABLE_ALWAYS_SAFE, 2661 }, 2662 { .name = "help", 2663 .func = kdb_help, 2664 .usage = "", 2665 .help = "Display Help Message", 2666 .minlen = 1, 2667 .flags = KDB_ENABLE_ALWAYS_SAFE, 2668 }, 2669 { .name = "?", 2670 .func = kdb_help, 2671 .usage = "", 2672 .help = "Display Help Message", 2673 .flags = KDB_ENABLE_ALWAYS_SAFE, 2674 }, 2675 { .name = "cpu", 2676 .func = kdb_cpu, 2677 .usage = "<cpunum>", 2678 .help = "Switch to new cpu", 2679 .flags = KDB_ENABLE_ALWAYS_SAFE_NO_ARGS, 2680 }, 2681 { .name = "kgdb", 2682 .func = kdb_kgdb, 2683 .usage = "", 2684 .help = "Enter kgdb mode", 2685 .flags = 0, 2686 }, 2687 { .name = "ps", 2688 .func = kdb_ps, 2689 .usage = "[<state_chars>|A]", 2690 .help = "Display active task list", 2691 .flags = KDB_ENABLE_INSPECT, 2692 }, 2693 { .name = "pid", 2694 .func = kdb_pid, 2695 .usage = "<pidnum>", 2696 .help = "Switch to another task", 2697 .flags = KDB_ENABLE_INSPECT, 2698 }, 2699 { .name = "reboot", 2700 .func = kdb_reboot, 2701 .usage = "", 2702 .help = "Reboot the machine immediately", 2703 .flags = KDB_ENABLE_REBOOT, 2704 }, 2705 #if defined(CONFIG_MODULES) 2706 { .name = "lsmod", 2707 .func = kdb_lsmod, 2708 .usage = "", 2709 .help = "List loaded kernel modules", 2710 .flags = KDB_ENABLE_INSPECT, 2711 }, 2712 #endif 2713 #if defined(CONFIG_MAGIC_SYSRQ) 2714 { .name = "sr", 2715 .func = kdb_sr, 2716 .usage = "<key>", 2717 .help = "Magic SysRq key", 2718 .flags = KDB_ENABLE_ALWAYS_SAFE, 2719 }, 2720 #endif 2721 #if defined(CONFIG_PRINTK) 2722 { .name = "dmesg", 2723 .func = kdb_dmesg, 2724 .usage = "[lines]", 2725 .help = "Display syslog buffer", 2726 .flags = KDB_ENABLE_ALWAYS_SAFE, 2727 }, 2728 #endif 2729 { .name = "defcmd", 2730 .func = kdb_defcmd, 2731 .usage = "name \"usage\" \"help\"", 2732 .help = "Define a set of commands, down to endefcmd", 2733 /* 2734 * Macros are always safe because when executed each 2735 * internal command re-enters kdb_parse() and is safety 2736 * checked individually. 2737 */ 2738 .flags = KDB_ENABLE_ALWAYS_SAFE, 2739 }, 2740 { .name = "kill", 2741 .func = kdb_kill, 2742 .usage = "<-signal> <pid>", 2743 .help = "Send a signal to a process", 2744 .flags = KDB_ENABLE_SIGNAL, 2745 }, 2746 { .name = "summary", 2747 .func = kdb_summary, 2748 .usage = "", 2749 .help = "Summarize the system", 2750 .minlen = 4, 2751 .flags = KDB_ENABLE_ALWAYS_SAFE, 2752 }, 2753 { .name = "per_cpu", 2754 .func = kdb_per_cpu, 2755 .usage = "<sym> [<bytes>] [<cpu>]", 2756 .help = "Display per_cpu variables", 2757 .minlen = 3, 2758 .flags = KDB_ENABLE_MEM_READ, 2759 }, 2760 { .name = "grephelp", 2761 .func = kdb_grep_help, 2762 .usage = "", 2763 .help = "Display help on | grep", 2764 .flags = KDB_ENABLE_ALWAYS_SAFE, 2765 }, 2766 }; 2767 2768 /* Initialize the kdb command table. */ 2769 static void __init kdb_inittab(void) 2770 { 2771 kdb_register_table(maintab, ARRAY_SIZE(maintab)); 2772 } 2773 2774 /* Execute any commands defined in kdb_cmds. */ 2775 static void __init kdb_cmd_init(void) 2776 { 2777 int i, diag; 2778 for (i = 0; kdb_cmds[i]; ++i) { 2779 diag = kdb_parse(kdb_cmds[i]); 2780 if (diag) 2781 kdb_printf("kdb command %s failed, kdb diag %d\n", 2782 kdb_cmds[i], diag); 2783 } 2784 if (defcmd_in_progress) { 2785 kdb_printf("Incomplete 'defcmd' set, forcing endefcmd\n"); 2786 kdb_parse("endefcmd"); 2787 } 2788 } 2789 2790 /* Initialize kdb_printf, breakpoint tables and kdb state */ 2791 void __init kdb_init(int lvl) 2792 { 2793 static int kdb_init_lvl = KDB_NOT_INITIALIZED; 2794 int i; 2795 2796 if (kdb_init_lvl == KDB_INIT_FULL || lvl <= kdb_init_lvl) 2797 return; 2798 for (i = kdb_init_lvl; i < lvl; i++) { 2799 switch (i) { 2800 case KDB_NOT_INITIALIZED: 2801 kdb_inittab(); /* Initialize Command Table */ 2802 kdb_initbptab(); /* Initialize Breakpoints */ 2803 break; 2804 case KDB_INIT_EARLY: 2805 kdb_cmd_init(); /* Build kdb_cmds tables */ 2806 break; 2807 } 2808 } 2809 kdb_init_lvl = lvl; 2810 } 2811