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