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