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