2 * Kernel Debugger Architecture Independent Main Code
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
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.
14 * Updated for Xscale (R) architecture support
15 * Eddie Dong <eddie.dong@intel.com> 8 Jan 03
18 #include <linux/config.h>
19 #include <linux/ctype.h>
20 #include <linux/string.h>
21 #include <linux/kernel.h>
22 #include <linux/reboot.h>
23 #include <linux/sched.h>
24 #include <linux/sysrq.h>
25 #include <linux/smp.h>
26 #include <linux/utsname.h>
27 #include <linux/vmalloc.h>
28 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/kallsyms.h>
32 #include <linux/kdb.h>
33 #include <linux/kdbprivate.h>
34 #include <linux/notifier.h>
35 #include <linux/interrupt.h>
36 #include <linux/delay.h>
37 #include <linux/nmi.h>
38 #include <linux/ptrace.h>
39 #if defined(CONFIG_DUMP) || defined(CONFIG_DUMP_MODULE)
40 #include <linux/dump.h>
43 #include <asm/system.h>
46 * Kernel debugger state flags
48 volatile int kdb_flags;
52 * kdb_lock protects updates to kdb_initial_cpu. Used to
53 * single thread processors through the kernel debugger.
55 static spinlock_t kdb_lock = SPIN_LOCK_UNLOCKED;
56 volatile int kdb_initial_cpu = -1; /* cpu number that owns kdb */
57 int kdb_seqno = 2; /* how many times kdb has been entered */
59 volatile int kdb_nextline = 1;
60 static volatile int kdb_new_cpu; /* Which cpu to switch to */
62 volatile int kdb_state[NR_CPUS]; /* Per cpu state */
64 struct task_struct *kdb_current_task;
67 int kdb_on = 0; /* Default is off */
69 int kdb_on = 1; /* Default is on */
70 #endif /* CONFIG_KDB_OFF */
72 const char *kdb_diemsg;
73 struct notifier_block *kdb_notifier_list; /* racy for modules, see comments in kdb.h */
74 static int kdb_go_count;
75 #ifdef CONFIG_KDB_CONTINUE_CATASTROPHIC
76 static unsigned int kdb_continue_catastrophic = CONFIG_KDB_CONTINUE_CATASTROPHIC;
78 static unsigned int kdb_continue_catastrophic = 0;
81 #ifdef KDB_HAVE_LONGJMP
83 * Must have a setjmp buffer per CPU. Switching cpus will
84 * cause the jump buffer to be setup for the new cpu, and
85 * subsequent switches (and pager aborts) will use the
86 * appropriate per-processor values.
88 kdb_jmp_buf *kdbjmpbuf;
89 #endif /* KDB_HAVE_LONGJMP */
92 * kdb_commands describes the available commands.
94 static kdbtab_t *kdb_commands;
95 static int kdb_max_commands;
97 typedef struct _kdbmsg {
98 int km_diag; /* kdb diagnostic */
99 char *km_msg; /* Corresponding message text */
102 #define KDBMSG(msgnum, text) \
103 { KDB_##msgnum, text }
105 static kdbmsg_t kdbmsgs[] = {
106 KDBMSG(NOTFOUND,"Command Not Found"),
107 KDBMSG(ARGCOUNT, "Improper argument count, see usage."),
108 KDBMSG(BADWIDTH, "Illegal value for BYTESPERWORD use 1, 2, 4 or 8, 8 is only allowed on 64 bit systems"),
109 KDBMSG(BADRADIX, "Illegal value for RADIX use 8, 10 or 16"),
110 KDBMSG(NOTENV, "Cannot find environment variable"),
111 KDBMSG(NOENVVALUE, "Environment variable should have value"),
112 KDBMSG(NOTIMP, "Command not implemented"),
113 KDBMSG(ENVFULL, "Environment full"),
114 KDBMSG(ENVBUFFULL, "Environment buffer full"),
115 KDBMSG(TOOMANYBPT, "Too many breakpoints defined"),
116 #ifdef CONFIG_CPU_XSCALE
117 KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"),
119 KDBMSG(TOOMANYDBREGS, "More breakpoints than db registers defined"),
121 KDBMSG(DUPBPT, "Duplicate breakpoint address"),
122 KDBMSG(BPTNOTFOUND, "Breakpoint not found"),
123 KDBMSG(BADMODE, "Invalid IDMODE"),
124 KDBMSG(BADINT, "Illegal numeric value"),
125 KDBMSG(INVADDRFMT, "Invalid symbolic address format"),
126 KDBMSG(BADREG, "Invalid register name"),
127 KDBMSG(BADCPUNUM, "Invalid cpu number"),
128 KDBMSG(BADLENGTH, "Invalid length field"),
129 KDBMSG(NOBP, "No Breakpoint exists"),
130 KDBMSG(BADADDR, "Invalid address"),
134 static const int __nkdb_err = sizeof(kdbmsgs) / sizeof(kdbmsg_t);
138 * Initial environment. This is all kept static and local to
139 * this file. We don't want to rely on the memory allocation
140 * mechanisms in the kernel, so we use a very limited allocate-only
141 * heap for new and altered environment variables. The entire
142 * environment is limited to a fixed number of entries (add more
143 * to __env[] if required) and a fixed amount of heap (add more to
144 * KDB_ENVBUFSIZE if required).
147 static char *__env[] = {
148 #if defined(CONFIG_SMP)
150 "MOREPROMPT=[%d]more> ",
158 "MDCOUNT=8", /* lines of md output */
159 "BTARGS=5", /* 5 possible args in bt */
188 static const int __nenv = (sizeof(__env) / sizeof(char *));
191 * kdb_serial_str is the sequence that the user must enter on a serial
192 * console to invoke kdb. It can be a single character such as "\001"
193 * (control-A) or multiple characters such as "\eKDB". NOTE: All except the
194 * last character are passed through to the application reading from the serial
197 * I tried to make the sequence a CONFIG_ option but most of CML1 cannot cope
198 * with '\' in strings. CML2 would have been able to do it but we lost CML2.
201 const char kdb_serial_str[] = "\eKDB";
202 EXPORT_SYMBOL(kdb_serial_str);
207 * This function will return the character string value of
208 * an environment variable.
211 * match A character string representing an environment variable.
215 * NULL No environment variable matches 'match'
216 * char* Pointer to string value of environment variable.
218 * No locking considerations required.
222 kdbgetenv(const char *match)
225 int matchlen = strlen(match);
228 for(i=0; i<__nenv; i++) {
233 if ((strncmp(match, e, matchlen) == 0)
234 && ((e[matchlen] == '\0')
235 ||(e[matchlen] == '='))) {
236 char *cp = strchr(e, '=');
246 * This function is used to allocate bytes for environment entries.
249 * match A character string representing a numeric value
251 * *value the unsigned long represntation of the env variable 'match'
253 * Zero on success, a kdb diagnostic on failure.
255 * No locking considerations required. Must be called with all
258 * We use a static environment buffer (envbuffer) to hold the values
259 * of dynamically generated environment variables (see kdb_set). Buffer
260 * space once allocated is never free'd, so over time, the amount of space
261 * (currently 512 bytes) will be exhausted if env variables are changed
265 kdballocenv(size_t bytes)
267 #define KDB_ENVBUFSIZE 512
268 static char envbuffer[KDB_ENVBUFSIZE];
269 static int envbufsize;
270 char *ep = (char *)0;
272 if ((KDB_ENVBUFSIZE - envbufsize) >= bytes) {
273 ep = &envbuffer[envbufsize];
282 * This function will return the value of an unsigned long-valued
283 * environment variable.
286 * match A character string representing a numeric value
288 * *value the unsigned long represntation of the env variable 'match'
290 * Zero on success, a kdb diagnostic on failure.
292 * No locking considerations required.
297 kdbgetulenv(const char *match, unsigned long *value)
301 ep = kdbgetenv(match);
302 if (!ep) return KDB_NOTENV;
303 if (strlen(ep) == 0) return KDB_NOENVVALUE;
305 *value = simple_strtoul(ep, 0, 0);
313 * This function will return the value of an integer-valued
314 * environment variable.
317 * match A character string representing an integer-valued env variable
319 * *value the integer representation of the environment variable 'match'
321 * Zero on success, a kdb diagnostic on failure.
323 * No locking considerations required.
328 kdbgetintenv(const char *match, int *value) {
332 diag = kdbgetulenv(match, &val);
342 * This function will convert a numeric string
343 * into an unsigned long value.
346 * arg A character string representing a numeric value
348 * *value the unsigned long represntation of arg.
350 * Zero on success, a kdb diagnostic on failure.
352 * No locking considerations required.
357 kdbgetularg(const char *arg, unsigned long *value)
362 val = simple_strtoul(arg, &endp, 0);
366 * Try base 16, for us folks too lazy to type the
369 val = simple_strtoul(arg, &endp, 16);
382 * This function implements the 'set' command. Alter an existing
383 * environment variable or create a new one.
386 * argc argument count
387 * argv argument vector
388 * envp environment vector
389 * regs registers at time kdb was entered.
393 * zero for success, a kdb diagnostic if error
400 kdb_set(int argc, const char **argv, const char **envp, struct pt_regs *regs)
404 size_t varlen, vallen;
407 * we can be invoked two ways:
408 * set var=value argv[1]="var", argv[2]="value"
409 * set var = value argv[1]="var", argv[2]="=", argv[3]="value"
410 * - if the latter, shift 'em down.
421 * Check for internal variables
423 if (strcmp(argv[1], "KDBDEBUG") == 0) {
424 unsigned int debugflags;
427 debugflags = simple_strtoul(argv[2], &cp, 0);
428 if (cp == argv[2] || debugflags & ~KDB_DEBUG_FLAG_MASK) {
429 kdb_printf("kdb: illegal debug flags '%s'\n",
433 kdb_flags = (kdb_flags & ~(KDB_DEBUG_FLAG_MASK << KDB_DEBUG_FLAG_SHIFT))
434 | (debugflags << KDB_DEBUG_FLAG_SHIFT);
440 * Tokenizer squashed the '=' sign. argv[1] is variable
441 * name, argv[2] = value.
443 varlen = strlen(argv[1]);
444 vallen = strlen(argv[2]);
445 ep = kdballocenv(varlen + vallen + 2);
447 return KDB_ENVBUFFULL;
449 sprintf(ep, "%s=%s", argv[1], argv[2]);
451 ep[varlen+vallen+1]='\0';
453 for(i=0; i<__nenv; i++) {
455 && ((strncmp(__env[i], argv[1], varlen)==0)
456 && ((__env[i][varlen] == '\0')
457 || (__env[i][varlen] == '=')))) {
464 * Wasn't existing variable. Fit into slot.
466 for(i=0; i<__nenv-1; i++) {
467 if (__env[i] == (char *)0) {
479 * This function is responsible for parsing an
480 * address-expression and returning the value of
481 * the expression, symbol name, and offset to the caller.
483 * The argument may consist of a numeric value (decimal or
484 * hexidecimal), a symbol name, a register name (preceeded
485 * by the percent sign), an environment variable with a numeric
486 * value (preceeded by a dollar sign) or a simple arithmetic
487 * expression consisting of a symbol name, +/-, and a numeric
488 * constant value (offset).
491 * argc - count of arguments in argv
492 * argv - argument vector
493 * *nextarg - index to next unparsed argument in argv[]
494 * regs - Register state at time of KDB entry
496 * *value - receives the value of the address-expression
497 * *offset - receives the offset specified, if any
498 * *name - receives the symbol name, if any
499 * *nextarg - index to next unparsed argument in argv[]
502 * zero is returned on success, a kdb diagnostic code is
506 * No locking requirements.
513 kdbgetaddrarg(int argc, const char **argv, int *nextarg,
514 kdb_machreg_t *value, long *offset,
515 char **name, struct pt_regs *regs)
528 * Process arguments which follow the following syntax:
530 * symbol | numeric-address [+/- numeric-offset]
532 * $environment-variable
535 if (*nextarg > argc) {
539 symname = (char *)argv[*nextarg];
542 * If there is no whitespace between the symbol
543 * or address and the '+' or '-' symbols, we
544 * remember the character and replace it with a
545 * null so the symbol/value can be properly parsed
547 if ((cp = strpbrk(symname, "+-")) != NULL) {
552 if (symname[0] == '$') {
553 diag = kdbgetulenv(&symname[1], &addr);
556 } else if (symname[0] == '%') {
557 diag = kdba_getregcontents(&symname[1], regs, &addr);
561 found = kdbgetsymval(symname, &symtab);
563 addr = symtab.sym_start;
565 diag = kdbgetularg(argv[*nextarg], &addr);
572 found = kdbnearsym(addr, &symtab);
580 if (offset && name && *name)
581 *offset = addr - symtab.sym_start;
583 if ((*nextarg > argc)
588 * check for +/- and offset
591 if (symbol == '\0') {
592 if ((argv[*nextarg][0] != '+')
593 && (argv[*nextarg][0] != '-')) {
595 * Not our argument. Return.
599 positive = (argv[*nextarg][0] == '+');
603 positive = (symbol == '+');
606 * Now there must be an offset!
608 if ((*nextarg > argc)
609 && (symbol == '\0')) {
610 return KDB_INVADDRFMT;
614 cp = (char *)argv[*nextarg];
618 diag = kdbgetularg(cp, &off);
635 kdb_cmderror(int diag)
640 kdb_printf("no error detected\n");
644 for(i=0; i<__nkdb_err; i++) {
645 if (kdbmsgs[i].km_diag == diag) {
646 kdb_printf("diag: %d: %s\n", diag, kdbmsgs[i].km_msg);
651 kdb_printf("Unknown diag %d\n", -diag);
655 * kdb_defcmd, kdb_defcmd2
657 * This function implements the 'defcmd' command which defines one
658 * command as a set of other commands, terminated by endefcmd.
659 * kdb_defcmd processes the initial 'defcmd' command, kdb_defcmd2
660 * is invoked from kdb_parse for the following commands until
664 * argc argument count
665 * argv argument vector
666 * envp environment vector
667 * regs registers at time kdb was entered.
671 * zero for success, a kdb diagnostic if error
685 static struct defcmd_set *defcmd_set;
686 static int defcmd_set_count;
687 static int defcmd_in_progress;
689 /* Forward references */
690 static int kdb_exec_defcmd(int argc, const char **argv, const char **envp, struct pt_regs *regs);
693 kdb_defcmd2(const char *cmdstr, const char *argv0)
695 struct defcmd_set *s = defcmd_set + defcmd_set_count - 1;
696 char **save_command = s->command;
697 if (strcmp(argv0, "endefcmd") == 0) {
698 defcmd_in_progress = 0;
702 kdb_register(s->name, kdb_exec_defcmd, s->usage, s->help, 0);
707 s->command = kmalloc((s->count + 1) * sizeof(*(s->command)), GFP_KDB);
709 kdb_printf("Could not allocate new kdb_defcmd table for %s\n", cmdstr);
713 memcpy(s->command, save_command, s->count * sizeof(*(s->command)));
714 s->command[s->count++] = kdb_strdup(cmdstr, GFP_KDB);
720 kdb_defcmd(int argc, const char **argv, const char **envp, struct pt_regs *regs)
722 struct defcmd_set *save_defcmd_set = defcmd_set, *s;
723 if (defcmd_in_progress) {
724 kdb_printf("kdb: nested defcmd detected, assuming missing endefcmd\n");
725 kdb_defcmd2("endefcmd", "endefcmd");
729 for (s = defcmd_set; s < defcmd_set + defcmd_set_count; ++s) {
730 kdb_printf("defcmd %s \"%s\" \"%s\"\n", s->name, s->usage, s->help);
731 for (i = 0; i < s->count; ++i)
732 kdb_printf("%s", s->command[i]);
733 kdb_printf("endefcmd\n");
739 defcmd_set = kmalloc((defcmd_set_count + 1) * sizeof(*defcmd_set), GFP_KDB);
741 kdb_printf("Could not allocate new defcmd_set entry for %s\n", argv[1]);
742 defcmd_set = save_defcmd_set;
745 memcpy(defcmd_set, save_defcmd_set, defcmd_set_count * sizeof(*defcmd_set));
746 kfree(save_defcmd_set);
747 s = defcmd_set + defcmd_set_count;
748 memset(s, 0, sizeof(*s));
750 s->name = kdb_strdup(argv[1], GFP_KDB);
751 s->usage = kdb_strdup(argv[2], GFP_KDB);
752 s->help = kdb_strdup(argv[3], GFP_KDB);
753 if (s->usage[0] == '"') {
754 strcpy(s->usage, s->usage+1);
755 s->usage[strlen(s->usage)-1] = '\0';
757 if (s->help[0] == '"') {
758 strcpy(s->help, s->help+1);
759 s->help[strlen(s->help)-1] = '\0';
762 defcmd_in_progress = 1;
769 * Execute the set of commands associated with this defcmd name.
772 * argc argument count
773 * argv argument vector
774 * envp environment vector
775 * regs registers at time kdb was entered.
779 * zero for success, a kdb diagnostic if error
786 kdb_exec_defcmd(int argc, const char **argv, const char **envp, struct pt_regs *regs)
789 struct defcmd_set *s;
792 for (s = defcmd_set, i = 0; i < defcmd_set_count; ++i, ++s) {
793 if (strcmp(s->name, argv[0]) == 0)
796 if (i == defcmd_set_count) {
797 kdb_printf("kdb_exec_defcmd: could not find commands for %s\n", argv[0]);
800 for (i = 0; i < s->count; ++i) {
801 /* Recursive use of kdb_parse, do not use argv after this point */
803 kdb_printf("[%s]kdb> %s\n", s->name, s->command[i]);
804 if ((ret = kdb_parse(s->command[i], regs)))
810 /* Command history */
811 #define KDB_CMD_HISTORY_COUNT 32
812 #define CMD_BUFLEN 200 /* kdb_printf: max printline size == 256 */
813 static unsigned int cmd_head=0, cmd_tail=0;
814 static unsigned int cmdptr;
815 static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN];
816 static char cmd_cur[CMD_BUFLEN];
821 * Parse the command line, search the command table for a
822 * matching command and invoke the command function.
823 * This function may be called recursively, if it is, the second call
824 * will overwrite argv and cbuf. It is the caller's responsibility to
825 * save their argv if they recursively call kdb_parse().
828 * cmdstr The input command line to be parsed.
829 * regs The registers at the time kdb was entered.
833 * Zero for success, a kdb diagnostic if failure.
837 * Limited to 20 tokens.
839 * Real rudimentary tokenization. Basically only whitespace
840 * is considered a token delimeter (but special consideration
841 * is taken of the '=' sign as used by the 'set' command).
843 * The algorithm used to tokenize the input string relies on
844 * there being at least one whitespace (or otherwise useless)
845 * character between tokens as the character immediately following
846 * the token is altered in-place to a null-byte to terminate the
853 kdb_parse(const char *cmdstr, struct pt_regs *regs)
855 static char *argv[MAXARGC];
857 static char cbuf[CMD_BUFLEN+2];
861 int i, escaped, ignore_errors = 0;
864 * First tokenize the command string.
868 if (KDB_FLAG(CMD_INTERRUPT)) {
869 /* Previous command was interrupted, newline must not repeat the command */
870 KDB_FLAG_CLEAR(CMD_INTERRUPT);
871 argc = 0; /* no repeat */
874 if (*cp != '\n' && *cp != '\0') {
878 /* skip whitespace */
879 while (isspace(*cp)) cp++;
880 if ((*cp == '\0') || (*cp == '\n') || (*cp == '#' && !defcmd_in_progress))
882 if (cpp >= cbuf + CMD_BUFLEN) {
883 kdb_printf("kdb_parse: command buffer overflow, command ignored\n%s\n", cmdstr);
886 if (argc >= MAXARGC - 1) {
887 kdb_printf("kdb_parse: too many arguments, command ignored\n%s\n", cmdstr);
893 /* Copy to next unquoted and unescaped whitespace or '=' */
894 while (*cp && *cp != '\n' && (escaped || quoted || !isspace(*cp))) {
895 if (cpp >= cbuf + CMD_BUFLEN)
909 } else if (*cp == '\'' || *cp == '"') {
912 if ((*cpp = *cp++) == '=' && !quoted)
916 *cpp++ = '\0'; /* Squash a ws or '=' character */
921 if (defcmd_in_progress) {
922 int result = kdb_defcmd2(cmdstr, argv[0]);
923 if (!defcmd_in_progress) {
924 argc = 0; /* avoid repeat on endefcmd */
929 if (argv[0][0] == '-' && argv[0][1] && (argv[0][1] < '0' || argv[0][1] > '9')) {
934 for(tp=kdb_commands, i=0; i < kdb_max_commands; i++,tp++) {
937 * If this command is allowed to be abbreviated,
938 * check to see if this is it.
942 && (strlen(argv[0]) <= tp->cmd_minlen)) {
945 tp->cmd_minlen) == 0) {
950 if (strcmp(argv[0], tp->cmd_name)==0) {
957 * If we don't find a command by this name, see if the first
958 * few characters of this match any of the known commands.
959 * e.g., md1c20 should match md.
961 if (i == kdb_max_commands) {
962 for(tp=kdb_commands, i=0; i < kdb_max_commands; i++,tp++) {
966 strlen(tp->cmd_name))==0) {
973 if (i < kdb_max_commands) {
976 result = (*tp->cmd_func)(argc-1,
980 if (result && ignore_errors && result > KDB_CMD_GO)
982 KDB_STATE_CLEAR(CMD);
983 switch (tp->cmd_repeat) {
984 case KDB_REPEAT_NONE:
989 case KDB_REPEAT_NO_ARGS:
994 case KDB_REPEAT_WITH_ARGS:
1001 * If the input with which we were presented does not
1002 * map to an existing command, attempt to parse it as an
1003 * address argument and display the result. Useful for
1004 * obtaining the address of a variable, or the nearest symbol
1005 * to an address contained in a register.
1008 kdb_machreg_t value;
1013 if (kdbgetaddrarg(0, (const char **)argv, &nextarg,
1014 &value, &offset, &name, regs)) {
1015 return KDB_NOTFOUND;
1018 kdb_printf("%s = ", argv[0]);
1019 kdb_symbol_print(value, NULL, KDB_SP_DEFAULT);
1027 handle_ctrl_cmd(char *cmd)
1032 /* initial situation */
1033 if (cmd_head == cmd_tail) return 0;
1037 if (cmdptr != cmd_tail)
1038 cmdptr = (cmdptr-1) % KDB_CMD_HISTORY_COUNT;
1039 strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
1042 if (cmdptr != cmd_head)
1043 cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT;
1044 strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
1053 * Call the dump() function if the kernel is configured for LKCD.
1059 * None. dump() may or may not return.
1066 kdb_do_dump(struct pt_regs *regs)
1068 #if defined(CONFIG_DUMP) || defined(CONFIG_DUMP_MODULE)
1069 kdb_printf("Forcing dump (if configured)\n");
1070 console_loglevel = 8; /* to see the dump messages */
1071 dump("kdb_do_dump", regs);
1078 * This function implements the 'reboot' command. Reboot the system
1082 * argc argument count
1083 * argv argument vector
1084 * envp environment vector
1085 * regs registers at time kdb was entered.
1089 * zero for success, a kdb diagnostic if error
1093 * Shouldn't return from this function.
1097 kdb_reboot(int argc, const char **argv, const char **envp, struct pt_regs *regs)
1100 kdb_printf("Hmm, kdb_reboot did not reboot, spinning here\n");
1109 * The main code for kdb. This routine is invoked on a specific
1110 * processor, it is not global. The main kdb() routine ensures
1111 * that only one processor at a time is in this routine. This
1112 * code is called with the real reason code on the first entry
1113 * to a kdb session, thereafter it is called with reason SWITCH,
1114 * even if the user goes back to the original cpu.
1117 * reason The reason KDB was invoked
1118 * error The hardware-defined error code
1119 * regs The exception frame at time of fault/breakpoint. NULL
1120 * for reason SILENT, otherwise valid.
1121 * db_result Result code from the break or debug point.
1123 * 0 KDB was invoked for an event which it wasn't responsible
1124 * 1 KDB handled the event for which it was invoked.
1125 * KDB_CMD_GO User typed 'go'.
1126 * KDB_CMD_CPU User switched to another cpu.
1127 * KDB_CMD_SS Single step.
1128 * KDB_CMD_SSB Single step until branch.
1135 extern char kdb_prompt_str[];
1138 kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs, kdb_dbtrap_t db_result)
1144 if (reason != KDB_REASON_DEBUG &&
1145 reason != KDB_REASON_SILENT) {
1146 kdb_printf("\nEntering kdb (current=0x%p, pid %d) ", (void *)current, current->pid);
1147 #if defined(CONFIG_SMP)
1148 kdb_printf("on processor %d ", smp_processor_id());
1153 case KDB_REASON_DEBUG:
1156 * If re-entering kdb after a single step
1157 * command, don't print the message.
1161 kdb_printf("\nEntering kdb (0x%p) ", (void *)current);
1162 #if defined(CONFIG_SMP)
1163 kdb_printf("on processor %d ", smp_processor_id());
1165 kdb_printf("due to Debug @ " kdb_machreg_fmt "\n", kdba_getpc(regs));
1169 * In the midst of ssb command. Just return.
1171 return KDB_CMD_SSB; /* Continue with SSB command */
1177 return 1; /* kdba_db_trap did the work */
1179 kdb_printf("kdb: Bad result from kdba_db_trap: %d\n",
1186 case KDB_REASON_FAULT:
1188 case KDB_REASON_ENTER:
1189 if (KDB_STATE(KEYBOARD))
1190 kdb_printf("due to Keyboard Entry\n");
1192 kdb_printf("due to KDB_ENTER()\n");
1194 case KDB_REASON_KEYBOARD:
1195 KDB_STATE_SET(KEYBOARD);
1196 kdb_printf("due to Keyboard Entry\n");
1198 case KDB_REASON_SWITCH:
1199 kdb_printf("due to cpu switch\n");
1200 if (KDB_STATE(GO_SWITCH)) {
1201 KDB_STATE_CLEAR(GO_SWITCH);
1205 case KDB_REASON_CALL:
1207 kdb_printf("kdb() called with no registers, restricted function");
1210 case KDB_REASON_OOPS:
1211 kdb_printf("Oops: %s\n", kdb_diemsg);
1212 kdb_printf("due to oops @ " kdb_machreg_fmt "\n", kdba_getpc(regs));
1213 kdba_dumpregs(regs, NULL, NULL);
1215 case KDB_REASON_NMI:
1216 kdb_printf("due to NonMaskable Interrupt @ " kdb_machreg_fmt "\n",
1218 kdba_dumpregs(regs, NULL, NULL);
1220 case KDB_REASON_WATCHDOG:
1221 kdb_printf("due to WatchDog Interrupt @ " kdb_machreg_fmt "\n",
1223 kdba_dumpregs(regs, NULL, NULL);
1225 case KDB_REASON_BREAK:
1226 kdb_printf("due to Breakpoint @ " kdb_machreg_fmt "\n", kdba_getpc(regs));
1228 * Determine if this breakpoint is one that we
1229 * are interested in.
1231 if (db_result != KDB_DB_BPT) {
1232 kdb_printf("kdb: error return from kdba_bp_trap: %d\n", db_result);
1233 return 0; /* Not for us, dismiss it */
1236 case KDB_REASON_RECURSE:
1237 kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n", kdba_getpc(regs));
1239 case KDB_REASON_SILENT:
1240 return KDB_CMD_GO; /* Silent entry, silent exit */
1243 kdb_printf("kdb: unexpected reason code: %d\n", reason);
1244 return 0; /* Not for us, dismiss it */
1247 kdba_local_arch_setup();
1249 kdb_current_task = current;
1253 * Initialize pager context.
1256 KDB_STATE_CLEAR(SUPPRESS);
1257 #ifdef KDB_HAVE_LONGJMP
1259 * Use kdba_setjmp/kdba_longjmp to break out of
1260 * the pager early and to attempt to recover from kdb errors.
1262 KDB_STATE_CLEAR(LONGJMP);
1264 if (kdba_setjmp(&kdbjmpbuf[smp_processor_id()])) {
1265 /* Command aborted (usually in pager) */
1269 KDB_STATE_SET(LONGJMP);
1271 #endif /* KDB_HAVE_LONGJMP */
1275 *(cmd_hist[cmd_head])='\0';
1277 if (KDB_FLAG(ONLY_DO_DUMP)) {
1278 /* kdb is off but a catastrophic error requires a dump.
1279 * Take the dump and reboot.
1280 * Turn on logging so the kdb output appears in the log
1281 * buffer in the dump.
1283 const char *setargs[] = { "set", "LOGGING", "1" };
1284 kdb_set(2, setargs, NULL, regs);
1286 kdb_reboot(0, NULL, NULL, regs);
1291 #if defined(CONFIG_SMP)
1292 snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"), smp_processor_id());
1294 snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"));
1296 if (defcmd_in_progress)
1297 strncat(kdb_prompt_str, "[defcmd]", CMD_BUFLEN);
1300 * Fetch command from keyboard
1302 cmdbuf = kdb_getstr(cmdbuf, CMD_BUFLEN, kdb_prompt_str);
1303 if (*cmdbuf != '\n') {
1305 if(cmdptr == cmd_head) {
1306 strncpy(cmd_hist[cmd_head], cmd_cur, CMD_BUFLEN);
1307 *(cmd_hist[cmd_head]+strlen(cmd_hist[cmd_head])-1) = '\0';
1309 if(!handle_ctrl_cmd(cmdbuf))
1310 *(cmd_cur+strlen(cmd_cur)-1) = '\0';
1312 goto do_full_getstr;
1315 strncpy(cmd_hist[cmd_head], cmd_cur, CMD_BUFLEN);
1317 cmd_head = (cmd_head+1) % KDB_CMD_HISTORY_COUNT;
1318 if (cmd_head == cmd_tail) cmd_tail = (cmd_tail+1) % KDB_CMD_HISTORY_COUNT;
1323 diag = kdb_parse(cmdbuf, regs);
1324 if (diag == KDB_NOTFOUND) {
1325 kdb_printf("Unknown kdb command: '%s'\n", cmdbuf);
1328 if (diag == KDB_CMD_GO
1329 || diag == KDB_CMD_CPU
1330 || diag == KDB_CMD_SS
1331 || diag == KDB_CMD_SSB)
1338 kdba_local_arch_cleanup();
1347 * Print the state data for the current processor for debugging.
1350 * text Identifies the debug point
1351 * value Any integer value to be printed, e.g. reason code.
1360 void kdb_print_state(const char *text, int value)
1362 kdb_printf("state: %s cpu %d value %d initial %d state %x\n",
1363 text, smp_processor_id(), value, kdb_initial_cpu, kdb_state[smp_processor_id()]);
1367 * kdb_previous_event
1369 * Return a count of cpus that are leaving kdb, i.e. the number
1370 * of processors that are still handling the previous kdb event.
1375 * Count of cpus in previous event.
1383 kdb_previous_event(void)
1386 for (i = 0; i < NR_CPUS; ++i) {
1387 if (KDB_STATE_CPU(LEAVING, i))
1396 * Invoked once at the start of a kdb event, from the controlling cpu. Wait a
1397 * short period for the other cpus to enter kdb state.
1409 int kdb_wait_for_cpus_secs = 10; /* may be modified by ia64 MCA timeout */
1412 kdb_wait_for_cpus(void)
1415 int online = 0, kdb_data = 0, prev_kdb_data = 0, i, time;
1417 for (time = 0; time < kdb_wait_for_cpus_secs; ++time) {
1420 for (i = 0; i < NR_CPUS; ++i) {
1421 if (cpu_online(i)) {
1423 if (kdb_running_process[i].seqno >= kdb_seqno - 1)
1427 if (online == kdb_data)
1429 if (prev_kdb_data != kdb_data) {
1430 kdb_nextline = 0; /* no prompt yet */
1431 kdb_printf(" %d out of %d cpus in kdb, waiting for the rest, timeout in %d second(s)\n",
1432 kdb_data, online, kdb_wait_for_cpus_secs - time);
1433 prev_kdb_data = kdb_data;
1435 touch_nmi_watchdog();
1441 int wait = online - kdb_data;
1443 kdb_printf("All cpus are now in kdb\n");
1445 kdb_printf("%d cpu%s not in kdb, %s state is unknown\n",
1447 wait == 1 ? " is" : "s are",
1448 wait == 1 ? "its" : "their");
1450 #endif /* CONFIG_SMP */
1456 * The main kdb loop. After initial setup and assignment of the controlling
1457 * cpu, all cpus are in this loop. One cpu is in control and will issue the kdb
1458 * prompt, the others will spin until 'go' or cpu switch.
1460 * To get a consistent view of the kernel stacks for all processes, this routine
1461 * is invoked from the main kdb code via an architecture specific routine.
1462 * kdba_main_loop is responsible for making the kernel stacks consistent for all
1463 * processes, there should be no difference between a blocked process and a
1464 * running process as far as kdb is concerned.
1467 * reason The reason KDB was invoked
1468 * error The hardware-defined error code
1469 * reason2 kdb's current reason code. Initially error but can change
1470 * acording to kdb state.
1471 * db_result Result code from break or debug point.
1472 * regs The exception frame at time of fault/breakpoint. If reason
1473 * is KDB_REASON_SILENT then regs is NULL, otherwise it
1474 * should always be valid.
1476 * 0 KDB was invoked for an event which it wasn't responsible
1477 * 1 KDB handled the event for which it was invoked.
1485 kdb_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
1486 kdb_dbtrap_t db_result, struct pt_regs *regs)
1489 int wait_for_cpus = reason != KDB_REASON_SILENT;
1490 /* Stay in kdb() until 'go', 'ss[b]' or an error */
1493 * All processors except the one that is in control
1496 KDB_DEBUG_STATE("kdb_main_loop 1", reason);
1497 while (KDB_STATE(HOLD_CPU)) {
1498 /* state KDB is turned off by kdb_cpu to see if the
1499 * other cpus are still live, each cpu in this loop
1502 if (!KDB_STATE(KDB)) {
1506 KDB_STATE_CLEAR(SUPPRESS);
1507 KDB_DEBUG_STATE("kdb_main_loop 2", reason);
1508 if (KDB_STATE(LEAVING))
1509 break; /* Another cpu said 'go' */
1511 if (wait_for_cpus) {
1513 kdb_wait_for_cpus();
1515 /* Still using kdb, this processor is in control */
1516 result = kdb_local(reason2, error, regs, db_result);
1517 KDB_DEBUG_STATE("kdb_main_loop 3", result);
1519 if (result == KDB_CMD_CPU) {
1520 /* Cpu switch, hold the current cpu, release the target one. */
1521 reason2 = KDB_REASON_SWITCH;
1522 KDB_STATE_SET(HOLD_CPU);
1523 KDB_STATE_CLEAR_CPU(HOLD_CPU, kdb_new_cpu);
1527 if (result == KDB_CMD_SS) {
1528 KDB_STATE_SET(DOING_SS);
1532 if (result == KDB_CMD_SSB) {
1533 KDB_STATE_SET(DOING_SS);
1534 KDB_STATE_SET(DOING_SSB);
1538 if (result && result != 1 && result != KDB_CMD_GO)
1539 kdb_printf("\nUnexpected kdb_local return code %d\n", result);
1541 KDB_DEBUG_STATE("kdb_main_loop 4", reason);
1544 if (KDB_STATE(DOING_SS))
1545 KDB_STATE_CLEAR(SSBPT);
1552 * This function is the entry point for the kernel debugger. It
1553 * provides a command parser and associated support functions to
1554 * allow examination and control of an active kernel.
1556 * This function may be invoked directly from any
1557 * point in the kernel by calling with reason == KDB_REASON_CALL
1559 * The breakpoint trap code should invoke this function with
1560 * one of KDB_REASON_BREAK (int 03) or KDB_REASON_DEBUG (debug register)
1562 * the die_if_kernel function should invoke this function with
1565 * The kernel fault handler should invoke this function with
1566 * reason == KDB_REASON_FAULT and error == trap vector #.
1568 * In single step mode, one cpu is released to run without
1569 * breakpoints. Interrupts and NMI are reset to their original values,
1570 * the cpu is allowed to do one instruction which causes a trap
1571 * into kdb with KDB_REASON_DEBUG.
1574 * reason The reason KDB was invoked
1575 * error The hardware-defined error code
1576 * regs The exception frame at time of fault/breakpoint. If reason
1577 * is KDB_REASON_SILENT then regs is NULL, otherwise it
1578 * should always be valid.
1580 * 0 KDB was invoked for an event which it wasn't responsible
1581 * 1 KDB handled the event for which it was invoked.
1585 * No assumptions of system state. This function may be invoked
1586 * with arbitrary locks held. It will stop all other processors
1587 * in an SMP environment, disable all interrupts and does not use
1588 * the operating systems keyboard driver.
1590 * This code is reentrant but only for cpu switch. Any other
1591 * reentrancy is an error, although kdb will attempt to recover.
1593 * At the start of a kdb session the initial processor is running
1594 * kdb() and the other processors can be doing anything. When the
1595 * initial processor calls smp_kdb_stop() the other processors are
1596 * driven through kdb_ipi which calls kdb() with reason SWITCH.
1597 * That brings all processors into this routine, one with a "real"
1598 * reason code, the other with SWITCH.
1600 * Because the other processors are driven via smp_kdb_stop(),
1601 * they enter here from the NMI handler. Until the other
1602 * processors exit from here and exit from kdb_ipi, they will not
1603 * take any more NMI requests. The initial cpu will still take NMI.
1605 * Multiple race and reentrancy conditions, each with different
1606 * advoidance mechanisms.
1608 * Two cpus hit debug points at the same time.
1610 * kdb_lock and kdb_initial_cpu ensure that only one cpu gets
1611 * control of kdb. The others spin on kdb_initial_cpu until
1612 * they are driven through NMI into kdb_ipi. When the initial
1613 * cpu releases the others from NMI, they resume trying to get
1614 * kdb_initial_cpu to start a new event.
1616 * A cpu is released from kdb and starts a new event before the
1617 * original event has completely ended.
1619 * kdb_previous_event() prevents any cpu from entering
1620 * kdb_initial_cpu state until the previous event has completely
1621 * ended on all cpus.
1623 * An exception occurs inside kdb.
1625 * kdb_initial_cpu detects recursive entry to kdb and attempts
1626 * to recover. The recovery uses longjmp() which means that
1627 * recursive calls to kdb never return. Beware of assumptions
1634 * If the kdb call is recursive then longjmp takes over and
1635 * --depth is never executed.
1639 * NMI handling is tricky. The initial cpu is invoked by some kdb event,
1640 * this event could be NMI driven but usually is not. The other cpus are
1641 * driven into kdb() via kdb_ipi which uses NMI so at the start the other
1642 * cpus will not accept NMI. Some operations such as SS release one cpu
1643 * but hold all the others. Releasing a cpu means it drops back to
1644 * whatever it was doing before the kdb event, this means it drops out of
1645 * kdb_ipi and hence out of NMI status. But the software watchdog uses
1646 * NMI and we do not want spurious watchdog calls into kdb. kdba_read()
1647 * resets the watchdog counters in its input polling loop, when a kdb
1648 * command is running it is subject to NMI watchdog events.
1650 * Another problem with NMI handling is the NMI used to drive the other
1651 * cpus into kdb cannot be distinguished from the watchdog NMI. State
1652 * flag WAIT_IPI indicates that a cpu is waiting for NMI via kdb_ipi,
1653 * if not set then software NMI is ignored by kdb_ipi.
1657 * All cpus are in kdb (or they should be), all but one are
1658 * spinning on KDB_STATE(HOLD_CPU). Only one cpu is not in
1659 * HOLD_CPU state, only that cpu can handle commands.
1661 * Go command entered.
1663 * If necessary, go will switch to the initial cpu first. If the event
1664 * was caused by a software breakpoint (assumed to be global) that
1665 * requires single-step to get over the breakpoint then only release the
1666 * initial cpu, after the initial cpu has single-stepped the breakpoint
1667 * then release the rest of the cpus. If SSBPT is not required then
1668 * release all the cpus at once.
1672 kdb(kdb_reason_t reason, int error, struct pt_regs *regs)
1674 kdb_intstate_t int_state; /* Interrupt state */
1675 kdb_reason_t reason2 = reason;
1676 int result = 0; /* Default is kdb did not handle it */
1678 kdb_dbtrap_t db_result=KDB_DB_NOBPT;
1679 atomic_inc(&kdb_event);
1682 case KDB_REASON_OOPS:
1683 case KDB_REASON_NMI:
1684 case KDB_REASON_WATCHDOG:
1685 KDB_FLAG_SET(CATASTROPHIC); /* kernel state is dubious now */
1690 if (kdb_continue_catastrophic > 2) {
1691 kdb_printf("kdb_continue_catastrophic is out of range, setting to 2\n");
1692 kdb_continue_catastrophic = 2;
1694 if (!kdb_on && KDB_FLAG(CATASTROPHIC) && kdb_continue_catastrophic == 2) {
1695 KDB_FLAG_SET(ONLY_DO_DUMP);
1697 if (!kdb_on && !KDB_FLAG(ONLY_DO_DUMP))
1700 KDB_DEBUG_STATE("kdb 1", reason);
1701 KDB_STATE_CLEAR(SUPPRESS);
1703 /* Filter out userspace breakpoints first, no point in doing all
1704 * the kdb smp fiddling when it is really a gdb trap.
1705 * Save the single step status first, kdba_db_trap clears ss status.
1706 * kdba_b[dp]_trap sets SSBPT if required.
1708 ss_event = KDB_STATE(DOING_SS) || KDB_STATE(SSBPT);
1709 #ifdef CONFIG_CPU_XSCALE
1710 if ( KDB_STATE(A_XSC_ICH) ) {
1711 /* restore changed I_BIT */
1712 KDB_STATE_CLEAR(A_XSC_ICH);
1713 kdba_restore_retirq(regs, KDB_STATE(A_XSC_IRQ));
1715 kdb_printf("Stranger!!! Why IRQ bit is changed====\n");
1719 if (reason == KDB_REASON_BREAK) {
1720 db_result = kdba_bp_trap(regs, error); /* Only call this once */
1722 if (reason == KDB_REASON_DEBUG) {
1723 db_result = kdba_db_trap(regs, error); /* Only call this once */
1726 if ((reason == KDB_REASON_BREAK || reason == KDB_REASON_DEBUG)
1727 && db_result == KDB_DB_NOBPT) {
1728 KDB_DEBUG_STATE("kdb 2", reason);
1729 goto out; /* Not one of mine */
1732 /* Turn off single step if it was being used */
1734 kdba_clearsinglestep(regs);
1735 /* Single step after a breakpoint removes the need for a delayed reinstall */
1736 if (reason == KDB_REASON_BREAK || reason == KDB_REASON_DEBUG)
1737 KDB_STATE_CLEAR(SSBPT);
1740 /* kdb can validly reenter but only for certain well defined conditions */
1741 if (reason == KDB_REASON_DEBUG
1742 && !KDB_STATE(HOLD_CPU)
1744 KDB_STATE_SET(REENTRY);
1746 KDB_STATE_CLEAR(REENTRY);
1748 /* Wait for previous kdb event to completely exit before starting
1751 while (kdb_previous_event())
1753 KDB_DEBUG_STATE("kdb 3", reason);
1756 * If kdb is already active, print a message and try to recover.
1757 * If recovery is not possible and recursion is allowed or
1758 * forced recursion without recovery is set then try to recurse
1759 * in kdb. Not guaranteed to work but it makes an attempt at
1760 * debugging the debugger.
1762 if (reason != KDB_REASON_SWITCH) {
1763 if (KDB_IS_RUNNING() && !KDB_STATE(REENTRY)) {
1765 unsigned long recurse = 0;
1766 kdb_printf("kdb: Debugger re-entered on cpu %d, new reason = %d\n",
1767 smp_processor_id(), reason);
1768 /* Should only re-enter from released cpu */
1770 if (KDB_STATE(HOLD_CPU)) {
1771 kdb_printf(" Strange, cpu %d should not be running\n", smp_processor_id());
1774 if (!KDB_STATE(CMD)) {
1775 kdb_printf(" Not executing a kdb command\n");
1778 if (!KDB_STATE(LONGJMP)) {
1779 kdb_printf(" No longjmp available for recovery\n");
1782 kdbgetulenv("RECURSE", &recurse);
1784 kdb_printf(" Forced recursion is set\n");
1788 kdb_printf(" Attempting to abort command and recover\n");
1789 #ifdef KDB_HAVE_LONGJMP
1790 kdba_longjmp(&kdbjmpbuf[smp_processor_id()], 0);
1794 if (KDB_STATE(RECURSE)) {
1795 kdb_printf(" Already in recursive mode\n");
1797 kdb_printf(" Attempting recursive mode\n");
1798 KDB_STATE_SET(RECURSE);
1799 KDB_STATE_SET(REENTRY);
1800 reason2 = KDB_REASON_RECURSE;
1805 kdb_printf(" Cannot recover, allowing event to proceed\n");
1809 } else if (!KDB_IS_RUNNING()) {
1810 kdb_printf("kdb: CPU switch without kdb running, I'm confused\n");
1815 * Disable interrupts, breakpoints etc. on this processor
1816 * during kdb command processing
1820 /* bh not re-enabled during single step */
1823 kdba_disableint(&int_state);
1824 if (!KDB_STATE(KDB_CONTROL)) {
1825 kdb_bp_remove_local();
1827 KDB_STATE_SET(KDB_CONTROL);
1829 else if (KDB_DEBUG(LBR))
1833 * If not entering the debugger due to CPU switch or single step
1834 * reentry, serialize access here.
1835 * The processors may race getting to this point - if,
1836 * for example, more than one processor hits a breakpoint
1837 * at the same time. We'll serialize access to kdb here -
1838 * other processors will loop here, and the NMI from the stop
1839 * IPI will take them into kdb as switch candidates. Once
1840 * the initial processor releases the debugger, the rest of
1841 * the processors will race for it.
1843 if (reason == KDB_REASON_SWITCH
1844 || KDB_STATE(REENTRY))
1845 ; /* drop through */
1847 KDB_DEBUG_STATE("kdb 4", reason);
1848 spin_lock(&kdb_lock);
1850 while (KDB_IS_RUNNING() || kdb_previous_event()) {
1851 spin_unlock(&kdb_lock);
1853 while (KDB_IS_RUNNING() || kdb_previous_event())
1856 spin_lock(&kdb_lock);
1858 KDB_DEBUG_STATE("kdb 5", reason);
1860 kdb_initial_cpu = smp_processor_id();
1862 spin_unlock(&kdb_lock);
1863 notifier_call_chain(&kdb_notifier_list, 1, NULL);
1866 if (smp_processor_id() == kdb_initial_cpu
1867 && !KDB_STATE(REENTRY)) {
1868 KDB_STATE_CLEAR(HOLD_CPU);
1869 KDB_STATE_CLEAR(WAIT_IPI);
1871 * Remove the global breakpoints. This is only done
1872 * once from the initial processor on initial entry.
1874 kdb_bp_remove_global();
1877 * If SMP, stop other processors. The other processors
1878 * will enter kdb() with KDB_REASON_SWITCH and spin in
1881 KDB_DEBUG_STATE("kdb 6", reason);
1884 for (i = 0; i < NR_CPUS; ++i) {
1887 if (i != kdb_initial_cpu) {
1888 KDB_STATE_SET_CPU(HOLD_CPU, i);
1889 KDB_STATE_SET_CPU(WAIT_IPI, i);
1892 KDB_DEBUG_STATE("kdb 7", reason);
1894 KDB_DEBUG_STATE("kdb 8", reason);
1898 if (KDB_STATE(GO1)) {
1899 kdb_bp_remove_global(); /* They were set for single-step purposes */
1900 KDB_STATE_CLEAR(GO1);
1901 reason = KDB_REASON_SILENT; /* Now silently go */
1904 /* Set up a consistent set of process stacks before talking to the user */
1905 KDB_DEBUG_STATE("kdb 9", result);
1906 result = kdba_main_loop(reason, reason2, error, db_result, regs);
1908 KDB_DEBUG_STATE("kdb 10", result);
1909 kdba_adjust_ip(reason, error, regs);
1910 KDB_STATE_CLEAR(LONGJMP);
1911 KDB_DEBUG_STATE("kdb 11", result);
1912 /* go which requires single-step over a breakpoint must only release
1915 if (result == KDB_CMD_GO && KDB_STATE(SSBPT))
1918 if (smp_processor_id() == kdb_initial_cpu &&
1919 !KDB_STATE(DOING_SS) &&
1920 !KDB_STATE(RECURSE)) {
1922 * (Re)install the global breakpoints. This is only done
1923 * once from the initial processor on go.
1925 KDB_DEBUG_STATE("kdb 12", reason);
1926 kdb_bp_install_global(regs);
1927 if (!KDB_STATE(GO1)) {
1929 * Release all other cpus which will see KDB_STATE(LEAVING) is set.
1932 for (i = 0; i < NR_CPUS; ++i) {
1933 if (KDB_STATE_CPU(KDB, i))
1934 KDB_STATE_SET_CPU(LEAVING, i);
1935 KDB_STATE_CLEAR_CPU(WAIT_IPI, i);
1936 KDB_STATE_CLEAR_CPU(HOLD_CPU, i);
1938 /* Wait until all the other processors leave kdb */
1939 while (kdb_previous_event() != 1)
1941 notifier_call_chain(&kdb_notifier_list, 0, NULL);
1942 kdb_initial_cpu = -1; /* release kdb control */
1943 KDB_DEBUG_STATE("kdb 13", reason);
1947 KDB_DEBUG_STATE("kdb 14", result);
1948 kdba_restoreint(&int_state);
1949 #ifdef CONFIG_CPU_XSCALE
1950 if ( smp_processor_id() == kdb_initial_cpu &&
1951 ( KDB_STATE(SSBPT) | KDB_STATE(DOING_SS) )
1953 kdba_setsinglestep(regs);
1954 // disable IRQ in stack frame
1955 KDB_STATE_SET(A_XSC_ICH);
1956 if ( kdba_disable_retirq(regs) ) {
1957 KDB_STATE_SET(A_XSC_IRQ);
1960 KDB_STATE_CLEAR(A_XSC_IRQ);
1965 /* Only do this work if we are really leaving kdb */
1966 if (!(KDB_STATE(DOING_SS) || KDB_STATE(SSBPT) || KDB_STATE(RECURSE))) {
1967 KDB_DEBUG_STATE("kdb 15", result);
1968 kdb_bp_install_local(regs);
1970 __local_bh_enable();
1971 KDB_STATE_CLEAR(KDB_CONTROL);
1974 KDB_DEBUG_STATE("kdb 16", result);
1975 KDB_FLAG_CLEAR(CATASTROPHIC);
1976 KDB_STATE_CLEAR(IP_ADJUSTED); /* Re-adjust ip next time in */
1977 KDB_STATE_CLEAR(KEYBOARD);
1978 KDB_STATE_CLEAR(KDB); /* Main kdb state has been cleared */
1979 KDB_STATE_CLEAR(RECURSE);
1980 KDB_STATE_CLEAR(LEAVING); /* No more kdb work after this */
1981 KDB_DEBUG_STATE("kdb 17", reason);
1983 atomic_dec(&kdb_event);
1990 * This function implements the guts of the 'mdr' command.
1992 * mdr <addr arg>,<byte count>
1995 * addr Start address
1996 * count Number of bytes
2000 * Always 0. Any errors are detected and printed by kdb_getarea.
2007 kdb_mdr(kdb_machreg_t addr, unsigned int count)
2011 if (kdb_getarea(c, addr))
2013 kdb_printf("%02x", c);
2023 * This function implements the 'md', 'md1', 'md2', 'md4', 'md8'
2024 * 'mdr' and 'mds' commands.
2026 * md|mds [<addr arg> [<line count> [<radix>]]]
2027 * mdWcN [<addr arg> [<line count> [<radix>]]]
2028 * where W = is the width (1, 2, 4 or 8) and N is the count.
2029 * for eg., md1c20 reads 20 bytes, 1 at a time.
2030 * mdr <addr arg>,<byte count>
2033 * argc argument count
2034 * argv argument vector
2035 * envp environment vector
2036 * regs registers at time kdb was entered.
2040 * zero for success, a kdb diagnostic if error
2047 kdb_md_line(const char *fmtstr, kdb_machreg_t addr,
2048 int symbolic, int nosect, int bytesperword,
2049 int num, int repeat, int phys)
2051 /* print just one line of data */
2052 kdb_symtab_t symtab;
2058 memset(cbuf, '\0', sizeof(cbuf));
2060 kdb_printf("phys " kdb_machreg_fmt0 " ", addr);
2062 kdb_printf(kdb_machreg_fmt0 " ", addr);
2064 for (i = 0; i < num && repeat--; i++) {
2066 if (kdb_getphysword(&word, addr, bytesperword))
2068 } else if (kdb_getword(&word, addr, bytesperword))
2070 kdb_printf(fmtstr, word);
2072 kdbnearsym(word, &symtab);
2074 memset(&symtab, 0, sizeof(symtab));
2075 if (symtab.sym_name) {
2076 kdb_symbol_print(word, &symtab, 0);
2079 kdb_printf(" %s %s "
2080 kdb_machreg_fmt " " kdb_machreg_fmt " " kdb_machreg_fmt,
2087 addr += bytesperword;
2095 cp = wc.c + 8 - bytesperword;
2100 #define printable_char(c) ({unsigned char __c = c; isprint(__c) ? __c : '.';})
2101 switch (bytesperword) {
2103 *c++ = printable_char(*cp++);
2104 *c++ = printable_char(*cp++);
2105 *c++ = printable_char(*cp++);
2106 *c++ = printable_char(*cp++);
2109 *c++ = printable_char(*cp++);
2110 *c++ = printable_char(*cp++);
2113 *c++ = printable_char(*cp++);
2116 *c++ = printable_char(*cp++);
2120 #undef printable_char
2123 kdb_printf("%*s %s\n", (int)((num-i)*(2*bytesperword + 1)+1), " ", cbuf);
2127 kdb_md(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2129 static kdb_machreg_t last_addr;
2130 static int last_radix, last_bytesperword, last_repeat;
2131 int radix = 16, mdcount = 8, bytesperword = sizeof(kdb_machreg_t), repeat;
2133 char fmtchar, fmtstr[64];
2141 kdbgetintenv("MDCOUNT", &mdcount);
2142 kdbgetintenv("RADIX", &radix);
2143 kdbgetintenv("BYTESPERWORD", &bytesperword);
2145 /* Assume 'md <addr>' and start with environment values */
2146 repeat = mdcount * 16 / bytesperword;
2148 if (strcmp(argv[0], "mdr") == 0) {
2150 return KDB_ARGCOUNT;
2152 } else if (isdigit(argv[0][2])) {
2153 bytesperword = (int)(argv[0][2] - '0');
2154 if (bytesperword == 0) {
2155 bytesperword = last_bytesperword;
2156 if (bytesperword == 0) {
2160 last_bytesperword = bytesperword;
2161 repeat = mdcount * 16 / bytesperword;
2164 else if (argv[0][3] == 'c' && argv[0][4]) {
2166 repeat = simple_strtoul(argv[0]+4, &p, 10);
2167 mdcount = ((repeat * bytesperword) + 15) / 16;
2170 last_repeat = repeat;
2171 } else if (strcmp(argv[0], "md") == 0)
2173 else if (strcmp(argv[0], "mds") == 0)
2175 else if (strcmp(argv[0], "mdp") == 0) {
2179 return KDB_NOTFOUND;
2183 return KDB_ARGCOUNT;
2186 bytesperword = last_bytesperword;
2187 repeat = last_repeat;
2188 mdcount = ((repeat * bytesperword) + 15) / 16;
2193 int diag, nextarg = 1;
2194 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL, regs);
2197 if (argc > nextarg+2)
2198 return KDB_ARGCOUNT;
2200 if (argc >= nextarg) {
2201 diag = kdbgetularg(argv[nextarg], &val);
2203 mdcount = (int) val;
2204 repeat = mdcount * 16 / bytesperword;
2207 if (argc >= nextarg+1) {
2208 diag = kdbgetularg(argv[nextarg+1], &val);
2214 if (strcmp(argv[0], "mdr") == 0) {
2215 return(kdb_mdr(addr, mdcount));
2229 return KDB_BADRADIX;
2234 if (bytesperword > sizeof(kdb_machreg_t))
2235 return KDB_BADWIDTH;
2237 switch (bytesperword) {
2239 sprintf(fmtstr, "%%16.16l%c ", fmtchar);
2242 sprintf(fmtstr, "%%8.8l%c ", fmtchar);
2245 sprintf(fmtstr, "%%4.4l%c ", fmtchar);
2248 sprintf(fmtstr, "%%2.2l%c ", fmtchar);
2251 return KDB_BADWIDTH;
2254 last_repeat = repeat;
2255 last_bytesperword = bytesperword;
2257 if (strcmp(argv[0], "mds") == 0) {
2259 /* Do not save these changes as last_*, they are temporary mds
2262 bytesperword = sizeof(kdb_machreg_t);
2264 kdbgetintenv("NOSECT", &nosect);
2267 /* Round address down modulo BYTESPERWORD */
2269 addr &= ~(bytesperword-1);
2271 while (repeat > 0) {
2273 int n, z, num = (symbolic ? 1 : (16 / bytesperword));
2275 for (a = addr, z = 0; z < repeat; a += bytesperword, ++z) {
2277 if (kdb_getphysword(&word, a, bytesperword)
2280 } else if (kdb_getword(&word, a, bytesperword) || word)
2283 n = min(num, repeat);
2284 kdb_md_line(fmtstr, addr, symbolic, nosect, bytesperword, num, repeat, phys);
2285 addr += bytesperword * n;
2287 z = (z + num - 1) / num;
2289 int s = num * (z-2);
2290 kdb_printf(kdb_machreg_fmt0 "-" kdb_machreg_fmt0 " zero suppressed\n",
2291 addr, addr + bytesperword * s - 1);
2292 addr += bytesperword * s;
2304 * This function implements the 'mm' command.
2306 * mm address-expression new-value
2309 * argc argument count
2310 * argv argument vector
2311 * envp environment vector
2312 * regs registers at time kdb was entered.
2316 * zero for success, a kdb diagnostic if error
2320 * mm works on machine words, mmW works on bytes.
2324 kdb_mm(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2329 unsigned long contents;
2333 if (argv[0][2] && !isdigit(argv[0][2]))
2334 return KDB_NOTFOUND;
2337 return KDB_ARGCOUNT;
2341 if ((diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL, regs)))
2345 return KDB_ARGCOUNT;
2347 if ((diag = kdbgetaddrarg(argc, argv, &nextarg, &contents, NULL, NULL, regs)))
2350 if (nextarg != argc + 1)
2351 return KDB_ARGCOUNT;
2353 width = argv[0][2] ? (argv[0][2] - '0') : (sizeof(kdb_machreg_t));
2354 if ((diag = kdb_putword(addr, contents, width)))
2357 kdb_printf(kdb_machreg_fmt " = " kdb_machreg_fmt "\n", addr, contents);
2365 * This function implements the 'go' command.
2367 * go [address-expression]
2370 * argc argument count
2371 * argv argument vector
2372 * envp environment vector
2373 * regs registers at time kdb was entered.
2377 * KDB_CMD_GO for success, a kdb diagnostic if error
2384 kdb_go(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2392 if (smp_processor_id() != kdb_initial_cpu) {
2393 kdb_printf("go <address> must be issued from the initial cpu, do cpu %d first\n", kdb_initial_cpu);
2394 return KDB_ARGCOUNT;
2397 diag = kdbgetaddrarg(argc, argv, &nextarg,
2398 &addr, &offset, NULL, regs);
2402 kdba_setpc(regs, addr);
2404 return KDB_ARGCOUNT;
2407 if (KDB_FLAG(CATASTROPHIC)) {
2408 kdb_printf("Catastrophic error detected\n");
2409 kdb_printf("kdb_continue_catastrophic=%d, ",
2410 kdb_continue_catastrophic);
2411 if (kdb_continue_catastrophic == 0 && kdb_go_count++ == 0) {
2412 kdb_printf("type go a second time if you really want to continue\n");
2415 if (kdb_continue_catastrophic == 2) {
2417 kdb_printf("forcing reboot\n");
2418 kdb_reboot(0, NULL, NULL, regs);
2420 kdb_printf("attempting to continue\n");
2422 if (smp_processor_id() != kdb_initial_cpu) {
2424 kdb_printf("go was not issued from initial cpu, switching back to cpu %d\n", kdb_initial_cpu);
2425 sprintf(buf, "cpu %d\n", kdb_initial_cpu);
2426 /* Recursive use of kdb_parse, do not use argv after this point */
2428 diag = kdb_parse(buf, regs);
2429 if (diag == KDB_CMD_CPU)
2430 KDB_STATE_SET_CPU(GO_SWITCH, kdb_initial_cpu);
2438 * This function implements the 'rd' command.
2440 * rd display all general registers.
2441 * rd c display all control registers.
2442 * rd d display all debug registers.
2445 * argc argument count
2446 * argv argument vector
2447 * envp environment vector
2448 * regs registers at time kdb was entered.
2452 * zero for success, a kdb diagnostic if error
2459 kdb_rd(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2462 return kdba_dumpregs(regs, NULL, NULL);
2466 return KDB_ARGCOUNT;
2469 return kdba_dumpregs(regs, argv[1], argc==2 ? argv[2]: NULL);
2475 * This function implements the 'rm' (register modify) command.
2477 * rm register-name new-contents
2480 * argc argument count
2481 * argv argument vector
2482 * envp environment vector
2483 * regs registers at time kdb was entered.
2487 * zero for success, a kdb diagnostic if error
2491 * Currently doesn't allow modification of control or
2492 * debug registers, nor does it allow modification
2493 * of model-specific registers (MSR).
2497 kdb_rm(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2501 kdb_machreg_t contents;
2504 return KDB_ARGCOUNT;
2508 * Allow presence or absence of leading '%' symbol.
2511 if (argv[1][0] == '%')
2514 diag = kdbgetularg(argv[2], &contents);
2518 diag = kdba_setregcontents(&argv[1][ind], regs, contents);
2525 #if defined(CONFIG_MAGIC_SYSRQ)
2529 * This function implements the 'sr' (SYSRQ key) command which
2530 * interfaces to the soi-disant MAGIC SYSRQ functionality.
2532 * sr <magic-sysrq-code>
2535 * argc argument count
2536 * argv argument vector
2537 * envp environment vector
2538 * regs registers at time kdb was entered.
2542 * zero for success, a kdb diagnostic if error
2549 kdb_sr(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2551 extern int sysrq_enabled;
2553 return KDB_ARGCOUNT;
2555 if (!sysrq_enabled) {
2556 kdb_printf("Auto activating sysrq\n");
2560 handle_sysrq(*argv[1], regs, 0);
2564 #endif /* CONFIG_MAGIC_SYSRQ */
2569 * This function implements the 'regs' (display exception frame)
2570 * command. This command takes an address and expects to find
2571 * an exception frame at that address, formats and prints it.
2573 * regs address-expression
2576 * argc argument count
2577 * argv argument vector
2578 * envp environment vector
2579 * regs registers at time kdb was entered.
2583 * zero for success, a kdb diagnostic if error
2591 kdb_ef(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2600 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL, regs);
2604 return kdba_dumpregs((struct pt_regs *)addr, NULL, NULL);
2607 return KDB_ARGCOUNT;
2610 #if defined(CONFIG_MODULES)
2611 extern struct list_head *kdb_modules;
2612 extern void free_module(struct module *);
2614 /* modules using other modules */
2617 struct list_head list;
2618 struct module *module_which_uses;
2624 * This function implements the 'lsmod' command. Lists currently
2625 * loaded kernel modules.
2627 * Mostly taken from userland lsmod.
2630 * argc argument count
2631 * argv argument vector
2632 * envp environment vector
2633 * regs registers at time kdb was entered.
2637 * zero for success, a kdb diagnostic if error
2645 kdb_lsmod(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2650 return KDB_ARGCOUNT;
2652 kdb_printf("Module Size modstruct Used by\n");
2653 list_for_each_entry(mod, kdb_modules, list) {
2655 kdb_printf("%-20s%8lu 0x%p ", mod->name,
2656 mod->core_size, (void *)mod);
2657 #ifdef CONFIG_MODULE_UNLOAD
2658 kdb_printf("%4d ", module_refcount(mod));
2660 if (mod->state == MODULE_STATE_GOING)
2661 kdb_printf(" (Unloading)");
2662 else if (mod->state == MODULE_STATE_COMING)
2663 kdb_printf(" (Loading)");
2665 kdb_printf(" (Live)");
2667 #ifdef CONFIG_MODULE_UNLOAD
2669 struct module_use *use;
2671 list_for_each_entry(use, &mod->modules_which_use_me, list)
2672 kdb_printf("%s ", use->module_which_uses->name);
2681 #endif /* CONFIG_MODULES */
2686 * This function implements the 'env' command. Display the current
2687 * environment variables.
2690 * argc argument count
2691 * argv argument vector
2692 * envp environment vector
2693 * regs registers at time kdb was entered.
2697 * zero for success, a kdb diagnostic if error
2704 kdb_env(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2708 for(i=0; i<__nenv; i++) {
2710 kdb_printf("%s\n", __env[i]);
2714 if (KDB_DEBUG(MASK))
2715 kdb_printf("KDBFLAGS=0x%x\n", kdb_flags);
2723 * This function implements the 'dmesg' command to display the contents
2724 * of the syslog buffer.
2726 * dmesg [lines] [adjust]
2729 * argc argument count
2730 * argv argument vector
2731 * envp environment vector
2732 * regs registers at time kdb was entered.
2736 * zero for success, a kdb diagnostic if error
2744 kdb_dmesg(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2746 char *syslog_data[4], *start, *end, c = '\0', *p;
2747 int diag, logging, logsize, lines = 0, adjust = 0, n;
2750 return KDB_ARGCOUNT;
2753 lines = simple_strtol(argv[1], &cp, 0);
2757 adjust = simple_strtoul(argv[2], &cp, 0);
2758 if (*cp || adjust < 0)
2763 /* disable LOGGING if set */
2764 diag = kdbgetintenv("LOGGING", &logging);
2765 if (!diag && logging) {
2766 const char *setargs[] = { "set", "LOGGING", "0" };
2767 kdb_set(2, setargs, envp, regs);
2770 /* syslog_data[0,1] physical start, end+1. syslog_data[2,3] logical start, end+1. */
2771 kdb_syslog_data(syslog_data);
2772 if (syslog_data[2] == syslog_data[3])
2774 logsize = syslog_data[1] - syslog_data[0];
2775 start = syslog_data[2];
2776 end = syslog_data[3];
2777 #define KDB_WRAP(p) (((p - syslog_data[0]) % logsize) + syslog_data[0])
2778 for (n = 0, p = start; p < end; ++p) {
2779 if ((c = *KDB_WRAP(p)) == '\n')
2786 kdb_printf("buffer only contains %d lines, nothing printed\n", n);
2787 else if (adjust - lines >= n)
2788 kdb_printf("buffer only contains %d lines, last %d lines printed\n",
2791 for (; start < end && adjust; ++start) {
2792 if (*KDB_WRAP(start) == '\n')
2798 for (p = start; p < end && lines; ++p) {
2799 if (*KDB_WRAP(p) == '\n')
2803 } else if (lines > 0) {
2804 int skip = n - (adjust + lines);
2806 kdb_printf("buffer only contains %d lines, nothing printed\n", n);
2808 } else if (skip < 0) {
2811 kdb_printf("buffer only contains %d lines, first %d lines printed\n",
2814 for (; start < end && skip; ++start) {
2815 if (*KDB_WRAP(start) == '\n')
2818 for (p = start; p < end && lines; ++p) {
2819 if (*KDB_WRAP(p) == '\n')
2824 /* Do a line at a time (max 200 chars) to reduce protocol overhead */
2826 while (start != end) {
2827 char buf[201], *p = buf;
2828 while (start < end && (c = *KDB_WRAP(start)) && (p - buf) < sizeof(buf)-1) {
2835 kdb_printf("%s", buf);
2846 * This function implements the 'cpu' command.
2851 * argc argument count
2852 * argv argument vector
2853 * envp environment vector
2854 * regs registers at time kdb was entered.
2858 * KDB_CMD_CPU for success, a kdb diagnostic if error
2862 * All cpu's should be spinning in kdb(). However just in case
2863 * a cpu did not take the smp_kdb_stop NMI, check that a cpu
2864 * entered kdb() before passing control to it.
2868 kdb_cpu_status(void)
2870 int i, start_cpu, first_print = 1;
2871 char state, prev_state = '?';
2873 kdb_printf("Currently on cpu %d\n", smp_processor_id());
2874 kdb_printf("Available cpus: ");
2875 for (start_cpu = -1, i = 0; i < NR_CPUS; i++) {
2877 state = 'F'; /* cpu is offline */
2879 struct kdb_running_process *krp = kdb_running_process+i;
2880 if (KDB_STATE_CPU(KDB, i)) {
2881 state = ' '; /* cpu is responding to kdb */
2882 if (kdb_task_state_char(krp->p) == 'I')
2883 state = 'I'; /* running the idle task */
2884 } else if (krp->seqno && krp->p && krp->seqno >= kdb_seqno - 1)
2885 state = '+'; /* some kdb data, but not responding */
2887 state = '*'; /* no kdb data */
2889 if (state != prev_state) {
2890 if (prev_state != '?') {
2894 kdb_printf("%d", start_cpu);
2895 if (start_cpu < i-1)
2896 kdb_printf("-%d", i-1);
2897 if (prev_state != ' ')
2898 kdb_printf("(%c)", prev_state);
2904 /* print the trailing cpus, ignoring them if they are all offline */
2905 if (prev_state != 'F') {
2908 kdb_printf("%d", start_cpu);
2909 if (start_cpu < i-1)
2910 kdb_printf("-%d", i-1);
2911 if (prev_state != ' ')
2912 kdb_printf("(%c)", prev_state);
2918 kdb_cpu(int argc, const char **argv, const char **envp, struct pt_regs *regs)
2920 unsigned long cpunum;
2923 /* ask the other cpus if they are still active */
2924 for (i=0; i<NR_CPUS; i++) {
2926 KDB_STATE_CLEAR_CPU(KDB, i);
2930 /* wait for the other cpus to notice and set state KDB again,
2941 return KDB_ARGCOUNT;
2943 diag = kdbgetularg(argv[1], &cpunum);
2950 if ((cpunum > NR_CPUS)
2951 || !cpu_online(cpunum)
2952 || !KDB_STATE_CPU(KDB, cpunum))
2953 return KDB_BADCPUNUM;
2955 kdb_new_cpu = cpunum;
2958 * Switch to other cpu
2963 /* The user may not realize that ps/bta with no parameters does not print idle
2964 * or sleeping system daemon processes, so tell them how many were suppressed.
2967 kdb_ps_suppressed(void)
2969 int idle = 0, daemon = 0;
2970 unsigned long mask_I = kdb_task_state_string("I"),
2971 mask_M = kdb_task_state_string("M");
2973 const struct task_struct *p, *g;
2974 for (cpu = 0; cpu < NR_CPUS; ++cpu) {
2975 if (!cpu_online(cpu))
2977 p = kdb_cpu_curr(cpu);
2978 if (kdb_task_state(p, mask_I))
2981 kdb_do_each_thread(g, p) {
2982 if (kdb_task_state(p, mask_M))
2984 } kdb_while_each_thread(g, p);
2985 if (idle || daemon) {
2987 kdb_printf("%d idle process%s (state I)%s",
2988 idle, idle == 1 ? "" : "es",
2989 daemon ? " and " : "");
2991 kdb_printf("%d sleeping system daemon (state M) process%s",
2992 daemon, daemon == 1 ? "" : "es");
2993 kdb_printf(" suppressed\n");
3000 * This function implements the 'ps' command which shows
3001 * a list of the active processes.
3003 * ps [DRSTCZEUIMA] All processes, optionally filtered by state
3006 * argc argument count
3007 * argv argument vector
3008 * envp environment vector
3009 * regs registers at time kdb was entered.
3013 * zero for success, a kdb diagnostic if error
3020 kdb_ps1(struct task_struct *p)
3022 kdb_printf("0x%p %8d %8d %d %4d %c 0x%p %c%s\n",
3023 (void *)p, p->pid, p->parent->pid,
3024 kdb_task_has_cpu(p), kdb_process_cpu(p),
3025 kdb_task_state_char(p),
3026 (void *)(&p->thread),
3027 (p == current) ? '*': ' ',
3029 if (kdb_task_has_cpu(p)) {
3030 struct kdb_running_process *krp = kdb_running_process + kdb_process_cpu(p);
3031 if (!krp->seqno || !krp->p)
3032 kdb_printf(" Error: no saved data for this cpu\n");
3034 if (krp->seqno < kdb_seqno - 1)
3035 kdb_printf(" Warning: process state is stale\n");
3037 kdb_printf(" Error: does not match running process table (0x%p)\n", krp->p);
3043 kdb_ps(int argc, const char **argv, const char **envp, struct pt_regs *regs)
3045 struct task_struct *g, *p;
3046 unsigned long mask, cpu;
3049 kdb_ps_suppressed();
3050 kdb_printf("%-*s Pid Parent [*] cpu State %-*s Command\n",
3051 (int)(2*sizeof(void *))+2, "Task Addr",
3052 (int)(2*sizeof(void *))+2, "Thread");
3053 mask = kdb_task_state_string(argc ? argv[1] : NULL);
3054 /* Run the active tasks first */
3055 for (cpu = 0; cpu < NR_CPUS; ++cpu) {
3056 if (!cpu_online(cpu))
3058 p = kdb_cpu_curr(cpu);
3059 if (kdb_task_state(p, mask))
3063 /* Now the real tasks */
3064 kdb_do_each_thread(g, p) {
3065 if (kdb_task_state(p, mask))
3067 } kdb_while_each_thread(g, p);
3075 * This function implements the 'pid' command which switches
3076 * the currently active process.
3081 * argc argument count
3082 * argv argument vector
3083 * envp environment vector
3084 * regs registers at time kdb was entered.
3088 * zero for success, a kdb diagnostic if error
3096 kdb_pid(int argc, const char **argv, const char **envp, struct pt_regs *regs)
3098 struct task_struct *p;
3103 return KDB_ARGCOUNT;
3106 diag = kdbgetularg(argv[1], &val);
3110 p = find_task_by_pid((pid_t)val);
3112 kdb_printf("No task with pid=%d\n", (pid_t)val);
3116 kdb_current_task = p;
3119 kdb_printf("KDB current process is %s(pid=%d)\n", kdb_current_task->comm,
3120 kdb_current_task->pid);
3128 * This function implements the 'll' command which follows a linked
3129 * list and executes an arbitrary command for each element.
3132 * argc argument count
3133 * argv argument vector
3134 * envp environment vector
3135 * regs registers at time kdb was entered.
3139 * zero for success, a kdb diagnostic if error
3146 kdb_ll(int argc, const char **argv, const char **envp, struct pt_regs *regs)
3152 unsigned long linkoffset;
3154 const char *command;
3157 return KDB_ARGCOUNT;
3161 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL, regs);
3165 diag = kdbgetularg(argv[2], &linkoffset);
3170 * Using the starting address as
3171 * the first element in the list, and assuming that
3172 * the list ends with a null pointer.
3176 if (!(command = kdb_strdup(argv[3], GFP_KDB))) {
3177 kdb_printf("%s: cannot duplicate command\n", __FUNCTION__);
3180 /* Recursive use of kdb_parse, do not use argv after this point */
3186 sprintf(buf, "%s " kdb_machreg_fmt "\n", command, va);
3187 diag = kdb_parse(buf, regs);
3191 addr = va + linkoffset;
3192 if (kdb_getword(&va, addr, sizeof(va)))
3203 * This function implements the 'help' and '?' commands.
3206 * argc argument count
3207 * argv argument vector
3208 * envp environment vector
3209 * regs registers at time kdb was entered.
3213 * zero for success, a kdb diagnostic if error
3220 kdb_help(int argc, const char **argv, const char **envp, struct pt_regs *regs)
3224 kdb_printf("%-15.15s %-20.20s %s\n", "Command", "Usage", "Description");
3225 kdb_printf("----------------------------------------------------------\n");
3226 for(kt=kdb_commands; kt->cmd_name; kt++) {
3227 kdb_printf("%-15.15s %-20.20s %s\n", kt->cmd_name,
3228 kt->cmd_usage, kt->cmd_help);
3233 extern int kdb_wake_up_process(struct task_struct * p);
3238 * This function implements the 'kill' commands.
3241 * argc argument count
3242 * argv argument vector
3243 * envp environment vector
3244 * regs registers at time kdb was entered.
3248 * zero for success, a kdb diagnostic if error
3255 kdb_kill(int argc, const char **argv, const char **envp, struct pt_regs *regs)
3259 struct task_struct *p;
3260 struct siginfo info;
3263 return KDB_ARGCOUNT;
3265 sig = simple_strtol(argv[1], &endp, 0);
3269 kdb_printf("Invalid signal parameter.<-signal>\n");
3274 pid = simple_strtol(argv[2], &endp, 0);
3278 kdb_printf("Process ID must be large than 0.\n");
3282 /* Find the process. */
3283 if (!(p = find_task_by_pid(pid))) {
3284 kdb_printf("The specified process isn't found.\n");
3287 p = p->group_leader;
3288 info.si_signo = sig;
3290 info.si_code = SI_USER;
3291 info.si_pid = pid; /* use same capabilities as process being signalled */
3292 info.si_uid = 0; /* kdb has root authority */
3293 kdb_send_sig_info(p, &info, kdb_seqno);
3298 int tm_sec; /* seconds */
3299 int tm_min; /* minutes */
3300 int tm_hour; /* hours */
3301 int tm_mday; /* day of the month */
3302 int tm_mon; /* month */
3303 int tm_year; /* year */
3307 kdb_gmtime(struct timespec *tv, struct kdb_tm *tm)
3309 /* This will work from 1970-2099, 2100 is not a leap year */
3310 static int mon_day[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
3311 memset(tm, 0, sizeof(*tm));
3312 tm->tm_sec = tv->tv_sec % (24 * 60 * 60);
3313 tm->tm_mday = tv->tv_sec / (24 * 60 * 60) + (2 * 365 + 1); /* shift base from 1970 to 1968 */
3314 tm->tm_min = tm->tm_sec / 60 % 60;
3315 tm->tm_hour = tm->tm_sec / 60 / 60;
3316 tm->tm_sec = tm->tm_sec % 60;
3317 tm->tm_year = 68 + 4*(tm->tm_mday / (4*365+1));
3318 tm->tm_mday %= (4*365+1);
3320 while (tm->tm_mday >= mon_day[tm->tm_mon]) {
3321 tm->tm_mday -= mon_day[tm->tm_mon];
3322 if (++tm->tm_mon == 12) {
3332 * Most of this code has been lifted from kernel/timer.c::sys_sysinfo().
3333 * I cannot call that code directly from kdb, it has an unconditional
3334 * cli()/sti() and calls routines that take locks which can stop the debugger.
3338 kdb_sysinfo(struct sysinfo *val)
3340 struct timespec uptime;
3341 do_posix_clock_monotonic_gettime(&uptime);
3342 memset(val, 0, sizeof(*val));
3343 val->uptime = uptime.tv_sec;
3344 val->loads[0] = avenrun[0];
3345 val->loads[1] = avenrun[1];
3346 val->loads[2] = avenrun[2];
3347 val->procs = nr_threads-1;
3349 kdb_si_swapinfo(val);
3357 * This function implements the 'summary' command.
3360 * argc argument count
3361 * argv argument vector
3362 * envp environment vector
3363 * regs registers at time kdb was entered.
3367 * zero for success, a kdb diagnostic if error
3374 kdb_summary(int argc, const char **argv, const char **envp, struct pt_regs *regs)
3376 extern struct timespec xtime;
3377 extern struct timezone sys_tz;
3382 return KDB_ARGCOUNT;
3384 kdb_printf("sysname %s\n", system_utsname.sysname);
3385 kdb_printf("release %s\n", system_utsname.release);
3386 kdb_printf("version %s\n", system_utsname.version);
3387 kdb_printf("machine %s\n", system_utsname.machine);
3388 kdb_printf("nodename %s\n", system_utsname.nodename);
3389 kdb_printf("domainname %s\n", system_utsname.domainname);
3391 kdb_gmtime(&xtime, &tm);
3392 kdb_printf("date %04d-%02d-%02d %02d:%02d:%02d tz_minuteswest %d\n",
3393 1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday,
3394 tm.tm_hour, tm.tm_min, tm.tm_sec,
3395 sys_tz.tz_minuteswest);
3398 kdb_printf("uptime ");
3399 if (val.uptime > (24*60*60)) {
3400 int days = val.uptime / (24*60*60);
3401 val.uptime %= (24*60*60);
3402 kdb_printf("%d day%s ", days, days == 1 ? "" : "s");
3404 kdb_printf("%02ld:%02ld\n", val.uptime/(60*60), (val.uptime/60)%60);
3406 /* lifted from fs/proc/proc_misc.c::loadavg_read_proc() */
3408 #define LOAD_INT(x) ((x) >> FSHIFT)
3409 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
3410 kdb_printf("load avg %ld.%02ld %ld.%02ld %ld.%02ld\n",
3411 LOAD_INT(val.loads[0]), LOAD_FRAC(val.loads[0]),
3412 LOAD_INT(val.loads[1]), LOAD_FRAC(val.loads[1]),
3413 LOAD_INT(val.loads[2]), LOAD_FRAC(val.loads[2]));
3418 kdb_meminfo_read_proc(); /* in fs/proc/proc_misc.c */
3426 * This function implements the 'per_cpu' command.
3429 * argc argument count
3430 * argv argument vector
3431 * envp environment vector
3432 * regs registers at time kdb was entered.
3436 * zero for success, a kdb diagnostic if error
3443 kdb_per_cpu(int argc, const char **argv, const char **envp, struct pt_regs *regs)
3445 char buf[256], fmtstr[64];
3446 kdb_symtab_t symtab;
3447 cpumask_t suppress = CPU_MASK_NONE;
3449 unsigned long addr, val, bytesperword = 0, whichcpu = ~0UL;
3451 if (argc < 1 || argc > 3)
3452 return KDB_ARGCOUNT;
3454 snprintf(buf, sizeof(buf), "per_cpu__%s", argv[1]);
3455 if (!kdbgetsymval(buf, &symtab)) {
3456 kdb_printf("%s is not a per_cpu variable\n", argv[1]);
3459 if (argc >=2 && (diag = kdbgetularg(argv[2], &bytesperword)))
3462 bytesperword = sizeof(kdb_machreg_t);
3463 else if (bytesperword > sizeof(kdb_machreg_t))
3464 return KDB_BADWIDTH;
3465 sprintf(fmtstr, "%%0%dlx ", (int)(2*bytesperword));
3467 if ((diag = kdbgetularg(argv[3], &whichcpu)))
3469 if (!cpu_online(whichcpu)) {
3470 kdb_printf("cpu %ld is not online\n", whichcpu);
3471 return KDB_BADCPUNUM;
3475 /* Most architectures use __per_cpu_offset[cpu], some use
3476 * __per_cpu_offset(cpu), smp has no __per_cpu_offset.
3478 #ifdef __per_cpu_offset
3479 #define KDB_PCU(cpu) __per_cpu_offset(cpu)
3482 #define KDB_PCU(cpu) __per_cpu_offset[cpu]
3484 #define KDB_PCU(cpu) 0
3488 for_each_online_cpu(cpu) {
3489 if (whichcpu != ~0UL && whichcpu != cpu)
3491 addr = symtab.sym_start + KDB_PCU(cpu);
3492 if ((diag = kdb_getword(&val, addr, bytesperword))) {
3493 kdb_printf("%5d " kdb_bfd_vma_fmt0 " - unable to read, diag=%d\n",
3499 cpu_set(cpu, suppress);
3502 #endif /* CONFIG_SMP */
3503 kdb_printf("%5d ", cpu);
3504 kdb_md_line(fmtstr, addr,
3505 bytesperword == sizeof(kdb_machreg_t),
3506 1, bytesperword, 1, 1, 0);
3508 if (cpus_weight(suppress) == 0)
3510 kdb_printf("Zero suppressed cpu(s):");
3511 for (cpu = first_cpu(suppress); cpu < NR_CPUS; cpu = next_cpu(cpu, suppress)) {
3512 kdb_printf(" %d", cpu);
3513 if (cpu == NR_CPUS-1 || next_cpu(cpu, suppress) != cpu + 1)
3515 while (cpu < NR_CPUS && next_cpu(cpu, suppress) == cpu + 1)
3517 kdb_printf("-%d", cpu);
3528 * kdb_register_repeat
3530 * This function is used to register a kernel debugger command.
3534 * func Function to execute the command
3535 * usage A simple usage string showing arguments
3536 * help A simple help string describing command
3537 * repeat Does the command auto repeat on enter?
3541 * zero for success, one if a duplicate command.
3548 #define kdb_command_extend 50 /* arbitrary */
3550 kdb_register_repeat(char *cmd,
3555 kdb_repeat_t repeat)
3561 * Brute force method to determine duplicates
3563 for (i=0, kp=kdb_commands; i<kdb_max_commands; i++, kp++) {
3564 if (kp->cmd_name && (strcmp(kp->cmd_name, cmd)==0)) {
3565 kdb_printf("Duplicate kdb command registered: '%s'\n",
3572 * Insert command into first available location in table
3574 for (i=0, kp=kdb_commands; i<kdb_max_commands; i++, kp++) {
3575 if (kp->cmd_name == NULL) {
3580 if (i >= kdb_max_commands) {
3581 kdbtab_t *new = kmalloc((kdb_max_commands + kdb_command_extend) * sizeof(*new), GFP_KDB);
3583 kdb_printf("Could not allocate new kdb_command table\n");
3587 memcpy(new, kdb_commands, kdb_max_commands * sizeof(*new));
3588 kfree(kdb_commands);
3590 memset(new + kdb_max_commands, 0, kdb_command_extend * sizeof(*new));
3592 kp = kdb_commands + kdb_max_commands;
3593 kdb_max_commands += kdb_command_extend;
3597 kp->cmd_func = func;
3598 kp->cmd_usage = usage;
3599 kp->cmd_help = help;
3601 kp->cmd_minlen = minlen;
3602 kp->cmd_repeat = repeat;
3610 * Compatibility register function for commands that do not need to
3611 * specify a repeat state. Equivalent to kdb_register_repeat with
3616 * func Function to execute the command
3617 * usage A simple usage string showing arguments
3618 * help A simple help string describing command
3622 * zero for success, one if a duplicate command.
3630 kdb_register(char *cmd,
3636 return kdb_register_repeat(cmd, func, usage, help, minlen, KDB_REPEAT_NONE);
3642 * This function is used to unregister a kernel debugger command.
3643 * It is generally called when a module which implements kdb
3644 * commands is unloaded.
3651 * zero for success, one command not registered.
3659 kdb_unregister(char *cmd)
3667 for (i=0, kp=kdb_commands; i<kdb_max_commands; i++, kp++) {
3668 if (kp->cmd_name && (strcmp(kp->cmd_name, cmd)==0)) {
3669 kp->cmd_name = NULL;
3683 * This function is called by the kdb_init function to initialize
3684 * the kdb command table. It must be called prior to any other
3685 * call to kdb_register_repeat.
3706 for(i=0, kp=kdb_commands; i < kdb_max_commands; i++,kp++) {
3707 kp->cmd_name = NULL;
3710 kdb_register_repeat("md", kdb_md, "<vaddr>", "Display Memory Contents, also mdWcN, e.g. md8c1", 1, KDB_REPEAT_NO_ARGS);
3711 kdb_register_repeat("mdr", kdb_md, "<vaddr> <bytes>", "Display Raw Memory", 0, KDB_REPEAT_NO_ARGS);
3712 kdb_register_repeat("mdp", kdb_md, "<paddr> <bytes>", "Display Physical Memory", 0, KDB_REPEAT_NO_ARGS);
3713 kdb_register_repeat("mds", kdb_md, "<vaddr>", "Display Memory Symbolically", 0, KDB_REPEAT_NO_ARGS);
3714 kdb_register_repeat("mm", kdb_mm, "<vaddr> <contents>", "Modify Memory Contents", 0, KDB_REPEAT_NO_ARGS);
3715 kdb_register_repeat("id", kdb_id, "<vaddr>", "Display Instructions", 1, KDB_REPEAT_NO_ARGS);
3716 kdb_register_repeat("go", kdb_go, "[<vaddr>]", "Continue Execution", 1, KDB_REPEAT_NONE);
3717 kdb_register_repeat("rd", kdb_rd, "", "Display Registers", 1, KDB_REPEAT_NONE);
3718 kdb_register_repeat("rm", kdb_rm, "<reg> <contents>", "Modify Registers", 0, KDB_REPEAT_NONE);
3719 kdb_register_repeat("ef", kdb_ef, "<vaddr>", "Display exception frame", 0, KDB_REPEAT_NONE);
3720 kdb_register_repeat("bt", kdb_bt, "[<vaddr>]", "Stack traceback", 1, KDB_REPEAT_NONE);
3721 kdb_register_repeat("btp", kdb_bt, "<pid>", "Display stack for process <pid>", 0, KDB_REPEAT_NONE);
3722 kdb_register_repeat("bta", kdb_bt, "[DRSTCZEUIMA]", "Display stack all processes", 0, KDB_REPEAT_NONE);
3723 kdb_register_repeat("btc", kdb_bt, "", "Backtrace current process on each cpu", 0, KDB_REPEAT_NONE);
3724 kdb_register_repeat("btt", kdb_bt, "<vaddr>", "Backtrace process given its struct task address", 0, KDB_REPEAT_NONE);
3725 kdb_register_repeat("ll", kdb_ll, "<first-element> <linkoffset> <cmd>", "Execute cmd for each element in linked list", 0, KDB_REPEAT_NONE);
3726 kdb_register_repeat("env", kdb_env, "", "Show environment variables", 0, KDB_REPEAT_NONE);
3727 kdb_register_repeat("set", kdb_set, "", "Set environment variables", 0, KDB_REPEAT_NONE);
3728 kdb_register_repeat("help", kdb_help, "", "Display Help Message", 1, KDB_REPEAT_NONE);
3729 kdb_register_repeat("?", kdb_help, "", "Display Help Message", 0, KDB_REPEAT_NONE);
3730 kdb_register_repeat("cpu", kdb_cpu, "<cpunum>","Switch to new cpu", 0, KDB_REPEAT_NONE);
3731 kdb_register_repeat("ps", kdb_ps, "", "Display active task list", 0, KDB_REPEAT_NONE);
3732 kdb_register_repeat("pid", kdb_pid, "<pidnum>", "Switch to another task", 0, KDB_REPEAT_NONE);
3733 kdb_register_repeat("reboot", kdb_reboot, "", "Reboot the machine immediately", 0, KDB_REPEAT_NONE);
3734 #if defined(CONFIG_MODULES)
3735 kdb_register_repeat("lsmod", kdb_lsmod, "", "List loaded kernel modules", 0, KDB_REPEAT_NONE);
3737 #if defined(CONFIG_MAGIC_SYSRQ)
3738 kdb_register_repeat("sr", kdb_sr, "<key>", "Magic SysRq key", 0, KDB_REPEAT_NONE);
3740 kdb_register_repeat("dmesg", kdb_dmesg, "[lines]", "Display syslog buffer", 0, KDB_REPEAT_NONE);
3741 kdb_register_repeat("defcmd", kdb_defcmd, "name \"usage\" \"help\"", "Define a set of commands, down to endefcmd", 0, KDB_REPEAT_NONE);
3742 kdb_register_repeat("kill", kdb_kill, "<-signal> <pid>", "Send a signal to a process", 0, KDB_REPEAT_NONE);
3743 kdb_register_repeat("summary", kdb_summary, "", "Summarize the system", 4, KDB_REPEAT_NONE);
3744 kdb_register_repeat("per_cpu", kdb_per_cpu, "", "Display per_cpu variables", 3, KDB_REPEAT_NONE);
3746 /* Any kdb commands that are not in the base code but are required
3747 * earlier than normal initcall processing.
3749 call = &__kdb_initcall_start;
3750 while (call < &__kdb_initcall_end) {
3759 * This function is called by the kdb_init function to execute any
3760 * commands defined in kdb_cmds.
3763 * Commands in *kdb_cmds[];
3778 for (i = 0; kdb_cmds[i]; ++i) {
3779 if (!defcmd_in_progress)
3780 kdb_printf("kdb_cmd[%d]: %s", i, kdb_cmds[i]);
3781 diag = kdb_parse(kdb_cmds[i], NULL);
3783 kdb_printf("command failed, kdb diag %d\n", diag);
3785 if (defcmd_in_progress) {
3786 kdb_printf("Incomplete 'defcmd' set, forcing endefcmd\n");
3787 kdb_parse("endefcmd", NULL);
3794 * Invoked via the panic_notifier_list.
3805 * When this function is called from panic(), the other cpus have already
3811 kdb_panic(struct notifier_block *self, unsigned long command, void *ptr)
3813 KDB_FLAG_SET(CATASTROPHIC); /* kernel state is dubious now */
3818 static struct notifier_block kdb_block = { kdb_panic, NULL, 0 };
3823 * Initialize the kernel debugger environment.
3838 kdb_initial_cpu = smp_processor_id();
3840 * This must be called before any calls to kdb_printf.
3844 kdb_inittab(); /* Initialize Command Table */
3845 kdb_initbptab(); /* Initialize Breakpoint Table */
3846 kdb_id_init(); /* Initialize Disassembler */
3847 kdba_init(); /* Architecture Dependent Initialization */
3850 * Use printk() to get message in log_buf[];
3852 printk("kdb version %d.%d%s by Keith Owens, Scott Lurndal. "\
3853 "Copyright SGI, All Rights Reserved\n",
3854 KDB_MAJOR_VERSION, KDB_MINOR_VERSION, KDB_TEST_VERSION);
3856 kdb_cmd_init(); /* Preset commands from kdb_cmds */
3857 kdb_initial_cpu = -1; /* Avoid recursion problems */
3858 kdb(KDB_REASON_SILENT, 0, 0); /* Activate any preset breakpoints on boot cpu */
3859 kdb_initial_cpu = smp_processor_id();
3860 notifier_chain_register(&panic_notifier_list, &kdb_block);
3862 #ifdef KDB_HAVE_LONGJMP
3863 kdbjmpbuf = vmalloc(NR_CPUS * sizeof(*kdbjmpbuf));
3865 printk(KERN_ERR "Cannot allocate kdbjmpbuf, no kdb recovery will be possible\n");
3866 #endif /* KDB_HAVE_LONGJMP */
3868 kdb_initial_cpu = -1;
3871 EXPORT_SYMBOL(kdb_register);
3872 EXPORT_SYMBOL(kdb_register_repeat);
3873 EXPORT_SYMBOL(kdb_unregister);
3874 EXPORT_SYMBOL(kdb_getarea_size);
3875 EXPORT_SYMBOL(kdb_putarea_size);
3876 EXPORT_SYMBOL(kdb_getuserarea_size);
3877 EXPORT_SYMBOL(kdb_putuserarea_size);
3878 EXPORT_SYMBOL(kdb_getword);
3879 EXPORT_SYMBOL(kdb_putword);
3880 EXPORT_SYMBOL(kdbgetularg);
3881 EXPORT_SYMBOL(kdbgetenv);
3882 EXPORT_SYMBOL(kdbgetintenv);
3883 EXPORT_SYMBOL(kdbgetaddrarg);
3885 EXPORT_SYMBOL(kdb_on);
3886 EXPORT_SYMBOL(kdb_seqno);
3887 EXPORT_SYMBOL(kdb_initial_cpu);
3888 EXPORT_SYMBOL(kdbgetsymval);
3889 EXPORT_SYMBOL(kdbnearsym);
3890 EXPORT_SYMBOL(kdb_printf);
3891 EXPORT_SYMBOL(kdb_symbol_print);
3892 EXPORT_SYMBOL(kdb_notifier_list);
3893 EXPORT_SYMBOL(kdb_current_task);