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