Commit aa9291355e19f804570466756ed7d874cd2e99ff

Authored by Linus Torvalds

Merge tag 'for_linus-3.19-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel/kgdb

Pull kgdb/kdb fixes from Jason Wessel:
 "These have been around since 3.17 and in kgdb-next for the last 9
  weeks and some will go back to -stable.

  Summary of changes:

  Cleanups
   - kdb: Remove unused command flags, repeat flags and KDB_REPEAT_NONE

  Fixes
   - kgdb/kdb: Allow access on a single core, if a CPU round up is
     deemed impossible, which will allow inspection of the now "trashed"
     kernel
   - kdb: Add enable mask for the command groups
   - kdb: access controls to restrict sensitive commands"

* tag 'for_linus-3.19-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel/kgdb:
  kernel/debug/debug_core.c: Logging clean-up
  kgdb: timeout if secondary CPUs ignore the roundup
  kdb: Allow access to sensitive commands to be restricted by default
  kdb: Add enable mask for groups of commands
  kdb: Categorize kdb commands (similar to SysRq categorization)
  kdb: Remove KDB_REPEAT_NONE flag
  kdb: Use KDB_REPEAT_* values as flags
  kdb: Rename kdb_register_repeat() to kdb_register_flags()
  kdb: Rename kdb_repeat_t to kdb_cmdflags_t, cmd_repeat to cmd_flags
  kdb: Remove currently unused kdbtab_t->cmd_flags

Showing 8 changed files Inline Diff

1 #ifndef _KDB_H 1 #ifndef _KDB_H
2 #define _KDB_H 2 #define _KDB_H
3 3
4 /* 4 /*
5 * Kernel Debugger Architecture Independent Global Headers 5 * Kernel Debugger Architecture Independent Global Headers
6 * 6 *
7 * This file is subject to the terms and conditions of the GNU General Public 7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive 8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details. 9 * for more details.
10 * 10 *
11 * Copyright (c) 2000-2007 Silicon Graphics, Inc. All Rights Reserved. 11 * Copyright (c) 2000-2007 Silicon Graphics, Inc. All Rights Reserved.
12 * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com> 12 * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com>
13 * Copyright (C) 2009 Jason Wessel <jason.wessel@windriver.com> 13 * Copyright (C) 2009 Jason Wessel <jason.wessel@windriver.com>
14 */ 14 */
15 15
16 /* Shifted versions of the command enable bits are be used if the command
17 * has no arguments (see kdb_check_flags). This allows commands, such as
18 * go, to have different permissions depending upon whether it is called
19 * with an argument.
20 */
21 #define KDB_ENABLE_NO_ARGS_SHIFT 10
22
16 typedef enum { 23 typedef enum {
17 KDB_REPEAT_NONE = 0, /* Do not repeat this command */ 24 KDB_ENABLE_ALL = (1 << 0), /* Enable everything */
18 KDB_REPEAT_NO_ARGS, /* Repeat the command without arguments */ 25 KDB_ENABLE_MEM_READ = (1 << 1),
19 KDB_REPEAT_WITH_ARGS, /* Repeat the command including its arguments */ 26 KDB_ENABLE_MEM_WRITE = (1 << 2),
20 } kdb_repeat_t; 27 KDB_ENABLE_REG_READ = (1 << 3),
28 KDB_ENABLE_REG_WRITE = (1 << 4),
29 KDB_ENABLE_INSPECT = (1 << 5),
30 KDB_ENABLE_FLOW_CTRL = (1 << 6),
31 KDB_ENABLE_SIGNAL = (1 << 7),
32 KDB_ENABLE_REBOOT = (1 << 8),
33 /* User exposed values stop here, all remaining flags are
34 * exclusively used to describe a commands behaviour.
35 */
21 36
37 KDB_ENABLE_ALWAYS_SAFE = (1 << 9),
38 KDB_ENABLE_MASK = (1 << KDB_ENABLE_NO_ARGS_SHIFT) - 1,
39
40 KDB_ENABLE_ALL_NO_ARGS = KDB_ENABLE_ALL << KDB_ENABLE_NO_ARGS_SHIFT,
41 KDB_ENABLE_MEM_READ_NO_ARGS = KDB_ENABLE_MEM_READ
42 << KDB_ENABLE_NO_ARGS_SHIFT,
43 KDB_ENABLE_MEM_WRITE_NO_ARGS = KDB_ENABLE_MEM_WRITE
44 << KDB_ENABLE_NO_ARGS_SHIFT,
45 KDB_ENABLE_REG_READ_NO_ARGS = KDB_ENABLE_REG_READ
46 << KDB_ENABLE_NO_ARGS_SHIFT,
47 KDB_ENABLE_REG_WRITE_NO_ARGS = KDB_ENABLE_REG_WRITE
48 << KDB_ENABLE_NO_ARGS_SHIFT,
49 KDB_ENABLE_INSPECT_NO_ARGS = KDB_ENABLE_INSPECT
50 << KDB_ENABLE_NO_ARGS_SHIFT,
51 KDB_ENABLE_FLOW_CTRL_NO_ARGS = KDB_ENABLE_FLOW_CTRL
52 << KDB_ENABLE_NO_ARGS_SHIFT,
53 KDB_ENABLE_SIGNAL_NO_ARGS = KDB_ENABLE_SIGNAL
54 << KDB_ENABLE_NO_ARGS_SHIFT,
55 KDB_ENABLE_REBOOT_NO_ARGS = KDB_ENABLE_REBOOT
56 << KDB_ENABLE_NO_ARGS_SHIFT,
57 KDB_ENABLE_ALWAYS_SAFE_NO_ARGS = KDB_ENABLE_ALWAYS_SAFE
58 << KDB_ENABLE_NO_ARGS_SHIFT,
59 KDB_ENABLE_MASK_NO_ARGS = KDB_ENABLE_MASK << KDB_ENABLE_NO_ARGS_SHIFT,
60
61 KDB_REPEAT_NO_ARGS = 0x40000000, /* Repeat the command w/o arguments */
62 KDB_REPEAT_WITH_ARGS = 0x80000000, /* Repeat the command with args */
63 } kdb_cmdflags_t;
64
22 typedef int (*kdb_func_t)(int, const char **); 65 typedef int (*kdb_func_t)(int, const char **);
23 66
24 #ifdef CONFIG_KGDB_KDB 67 #ifdef CONFIG_KGDB_KDB
25 #include <linux/init.h> 68 #include <linux/init.h>
26 #include <linux/sched.h> 69 #include <linux/sched.h>
27 #include <linux/atomic.h> 70 #include <linux/atomic.h>
28 71
29 #define KDB_POLL_FUNC_MAX 5 72 #define KDB_POLL_FUNC_MAX 5
30 extern int kdb_poll_idx; 73 extern int kdb_poll_idx;
31 74
32 /* 75 /*
33 * kdb_initial_cpu is initialized to -1, and is set to the cpu 76 * kdb_initial_cpu is initialized to -1, and is set to the cpu
34 * number whenever the kernel debugger is entered. 77 * number whenever the kernel debugger is entered.
35 */ 78 */
36 extern int kdb_initial_cpu; 79 extern int kdb_initial_cpu;
37 extern atomic_t kdb_event; 80 extern atomic_t kdb_event;
38 81
39 /* Types and messages used for dynamically added kdb shell commands */ 82 /* Types and messages used for dynamically added kdb shell commands */
40 83
41 #define KDB_MAXARGS 16 /* Maximum number of arguments to a function */ 84 #define KDB_MAXARGS 16 /* Maximum number of arguments to a function */
42 85
43 /* KDB return codes from a command or internal kdb function */ 86 /* KDB return codes from a command or internal kdb function */
44 #define KDB_NOTFOUND (-1) 87 #define KDB_NOTFOUND (-1)
45 #define KDB_ARGCOUNT (-2) 88 #define KDB_ARGCOUNT (-2)
46 #define KDB_BADWIDTH (-3) 89 #define KDB_BADWIDTH (-3)
47 #define KDB_BADRADIX (-4) 90 #define KDB_BADRADIX (-4)
48 #define KDB_NOTENV (-5) 91 #define KDB_NOTENV (-5)
49 #define KDB_NOENVVALUE (-6) 92 #define KDB_NOENVVALUE (-6)
50 #define KDB_NOTIMP (-7) 93 #define KDB_NOTIMP (-7)
51 #define KDB_ENVFULL (-8) 94 #define KDB_ENVFULL (-8)
52 #define KDB_ENVBUFFULL (-9) 95 #define KDB_ENVBUFFULL (-9)
53 #define KDB_TOOMANYBPT (-10) 96 #define KDB_TOOMANYBPT (-10)
54 #define KDB_TOOMANYDBREGS (-11) 97 #define KDB_TOOMANYDBREGS (-11)
55 #define KDB_DUPBPT (-12) 98 #define KDB_DUPBPT (-12)
56 #define KDB_BPTNOTFOUND (-13) 99 #define KDB_BPTNOTFOUND (-13)
57 #define KDB_BADMODE (-14) 100 #define KDB_BADMODE (-14)
58 #define KDB_BADINT (-15) 101 #define KDB_BADINT (-15)
59 #define KDB_INVADDRFMT (-16) 102 #define KDB_INVADDRFMT (-16)
60 #define KDB_BADREG (-17) 103 #define KDB_BADREG (-17)
61 #define KDB_BADCPUNUM (-18) 104 #define KDB_BADCPUNUM (-18)
62 #define KDB_BADLENGTH (-19) 105 #define KDB_BADLENGTH (-19)
63 #define KDB_NOBP (-20) 106 #define KDB_NOBP (-20)
64 #define KDB_BADADDR (-21) 107 #define KDB_BADADDR (-21)
108 #define KDB_NOPERM (-22)
65 109
66 /* 110 /*
67 * kdb_diemsg 111 * kdb_diemsg
68 * 112 *
69 * Contains a pointer to the last string supplied to the 113 * Contains a pointer to the last string supplied to the
70 * kernel 'die' panic function. 114 * kernel 'die' panic function.
71 */ 115 */
72 extern const char *kdb_diemsg; 116 extern const char *kdb_diemsg;
73 117
74 #define KDB_FLAG_EARLYKDB (1 << 0) /* set from boot parameter kdb=early */ 118 #define KDB_FLAG_EARLYKDB (1 << 0) /* set from boot parameter kdb=early */
75 #define KDB_FLAG_CATASTROPHIC (1 << 1) /* A catastrophic event has occurred */ 119 #define KDB_FLAG_CATASTROPHIC (1 << 1) /* A catastrophic event has occurred */
76 #define KDB_FLAG_CMD_INTERRUPT (1 << 2) /* Previous command was interrupted */ 120 #define KDB_FLAG_CMD_INTERRUPT (1 << 2) /* Previous command was interrupted */
77 #define KDB_FLAG_NOIPI (1 << 3) /* Do not send IPIs */ 121 #define KDB_FLAG_NOIPI (1 << 3) /* Do not send IPIs */
78 #define KDB_FLAG_NO_CONSOLE (1 << 5) /* No console is available, 122 #define KDB_FLAG_NO_CONSOLE (1 << 5) /* No console is available,
79 * kdb is disabled */ 123 * kdb is disabled */
80 #define KDB_FLAG_NO_VT_CONSOLE (1 << 6) /* No VT console is available, do 124 #define KDB_FLAG_NO_VT_CONSOLE (1 << 6) /* No VT console is available, do
81 * not use keyboard */ 125 * not use keyboard */
82 #define KDB_FLAG_NO_I8042 (1 << 7) /* No i8042 chip is available, do 126 #define KDB_FLAG_NO_I8042 (1 << 7) /* No i8042 chip is available, do
83 * not use keyboard */ 127 * not use keyboard */
84 128
85 extern int kdb_flags; /* Global flags, see kdb_state for per cpu state */ 129 extern int kdb_flags; /* Global flags, see kdb_state for per cpu state */
86 130
87 extern void kdb_save_flags(void); 131 extern void kdb_save_flags(void);
88 extern void kdb_restore_flags(void); 132 extern void kdb_restore_flags(void);
89 133
90 #define KDB_FLAG(flag) (kdb_flags & KDB_FLAG_##flag) 134 #define KDB_FLAG(flag) (kdb_flags & KDB_FLAG_##flag)
91 #define KDB_FLAG_SET(flag) ((void)(kdb_flags |= KDB_FLAG_##flag)) 135 #define KDB_FLAG_SET(flag) ((void)(kdb_flags |= KDB_FLAG_##flag))
92 #define KDB_FLAG_CLEAR(flag) ((void)(kdb_flags &= ~KDB_FLAG_##flag)) 136 #define KDB_FLAG_CLEAR(flag) ((void)(kdb_flags &= ~KDB_FLAG_##flag))
93 137
94 /* 138 /*
95 * External entry point for the kernel debugger. The pt_regs 139 * External entry point for the kernel debugger. The pt_regs
96 * at the time of entry are supplied along with the reason for 140 * at the time of entry are supplied along with the reason for
97 * entry to the kernel debugger. 141 * entry to the kernel debugger.
98 */ 142 */
99 143
100 typedef enum { 144 typedef enum {
101 KDB_REASON_ENTER = 1, /* KDB_ENTER() trap/fault - regs valid */ 145 KDB_REASON_ENTER = 1, /* KDB_ENTER() trap/fault - regs valid */
102 KDB_REASON_ENTER_SLAVE, /* KDB_ENTER_SLAVE() trap/fault - regs valid */ 146 KDB_REASON_ENTER_SLAVE, /* KDB_ENTER_SLAVE() trap/fault - regs valid */
103 KDB_REASON_BREAK, /* Breakpoint inst. - regs valid */ 147 KDB_REASON_BREAK, /* Breakpoint inst. - regs valid */
104 KDB_REASON_DEBUG, /* Debug Fault - regs valid */ 148 KDB_REASON_DEBUG, /* Debug Fault - regs valid */
105 KDB_REASON_OOPS, /* Kernel Oops - regs valid */ 149 KDB_REASON_OOPS, /* Kernel Oops - regs valid */
106 KDB_REASON_SWITCH, /* CPU switch - regs valid*/ 150 KDB_REASON_SWITCH, /* CPU switch - regs valid*/
107 KDB_REASON_KEYBOARD, /* Keyboard entry - regs valid */ 151 KDB_REASON_KEYBOARD, /* Keyboard entry - regs valid */
108 KDB_REASON_NMI, /* Non-maskable interrupt; regs valid */ 152 KDB_REASON_NMI, /* Non-maskable interrupt; regs valid */
109 KDB_REASON_RECURSE, /* Recursive entry to kdb; 153 KDB_REASON_RECURSE, /* Recursive entry to kdb;
110 * regs probably valid */ 154 * regs probably valid */
111 KDB_REASON_SSTEP, /* Single Step trap. - regs valid */ 155 KDB_REASON_SSTEP, /* Single Step trap. - regs valid */
112 KDB_REASON_SYSTEM_NMI, /* In NMI due to SYSTEM cmd; regs valid */ 156 KDB_REASON_SYSTEM_NMI, /* In NMI due to SYSTEM cmd; regs valid */
113 } kdb_reason_t; 157 } kdb_reason_t;
114 158
115 extern int kdb_trap_printk; 159 extern int kdb_trap_printk;
116 extern __printf(1, 0) int vkdb_printf(const char *fmt, va_list args); 160 extern __printf(1, 0) int vkdb_printf(const char *fmt, va_list args);
117 extern __printf(1, 2) int kdb_printf(const char *, ...); 161 extern __printf(1, 2) int kdb_printf(const char *, ...);
118 typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...); 162 typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
119 163
120 extern void kdb_init(int level); 164 extern void kdb_init(int level);
121 165
122 /* Access to kdb specific polling devices */ 166 /* Access to kdb specific polling devices */
123 typedef int (*get_char_func)(void); 167 typedef int (*get_char_func)(void);
124 extern get_char_func kdb_poll_funcs[]; 168 extern get_char_func kdb_poll_funcs[];
125 extern int kdb_get_kbd_char(void); 169 extern int kdb_get_kbd_char(void);
126 170
127 static inline 171 static inline
128 int kdb_process_cpu(const struct task_struct *p) 172 int kdb_process_cpu(const struct task_struct *p)
129 { 173 {
130 unsigned int cpu = task_thread_info(p)->cpu; 174 unsigned int cpu = task_thread_info(p)->cpu;
131 if (cpu > num_possible_cpus()) 175 if (cpu > num_possible_cpus())
132 cpu = 0; 176 cpu = 0;
133 return cpu; 177 return cpu;
134 } 178 }
135 179
136 /* kdb access to register set for stack dumping */ 180 /* kdb access to register set for stack dumping */
137 extern struct pt_regs *kdb_current_regs; 181 extern struct pt_regs *kdb_current_regs;
138 #ifdef CONFIG_KALLSYMS 182 #ifdef CONFIG_KALLSYMS
139 extern const char *kdb_walk_kallsyms(loff_t *pos); 183 extern const char *kdb_walk_kallsyms(loff_t *pos);
140 #else /* ! CONFIG_KALLSYMS */ 184 #else /* ! CONFIG_KALLSYMS */
141 static inline const char *kdb_walk_kallsyms(loff_t *pos) 185 static inline const char *kdb_walk_kallsyms(loff_t *pos)
142 { 186 {
143 return NULL; 187 return NULL;
144 } 188 }
145 #endif /* ! CONFIG_KALLSYMS */ 189 #endif /* ! CONFIG_KALLSYMS */
146 190
147 /* Dynamic kdb shell command registration */ 191 /* Dynamic kdb shell command registration */
148 extern int kdb_register(char *, kdb_func_t, char *, char *, short); 192 extern int kdb_register(char *, kdb_func_t, char *, char *, short);
149 extern int kdb_register_repeat(char *, kdb_func_t, char *, char *, 193 extern int kdb_register_flags(char *, kdb_func_t, char *, char *,
150 short, kdb_repeat_t); 194 short, kdb_cmdflags_t);
151 extern int kdb_unregister(char *); 195 extern int kdb_unregister(char *);
152 #else /* ! CONFIG_KGDB_KDB */ 196 #else /* ! CONFIG_KGDB_KDB */
153 static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; } 197 static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
154 static inline void kdb_init(int level) {} 198 static inline void kdb_init(int level) {}
155 static inline int kdb_register(char *cmd, kdb_func_t func, char *usage, 199 static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
156 char *help, short minlen) { return 0; } 200 char *help, short minlen) { return 0; }
157 static inline int kdb_register_repeat(char *cmd, kdb_func_t func, char *usage, 201 static inline int kdb_register_flags(char *cmd, kdb_func_t func, char *usage,
158 char *help, short minlen, 202 char *help, short minlen,
159 kdb_repeat_t repeat) { return 0; } 203 kdb_cmdflags_t flags) { return 0; }
160 static inline int kdb_unregister(char *cmd) { return 0; } 204 static inline int kdb_unregister(char *cmd) { return 0; }
161 #endif /* CONFIG_KGDB_KDB */ 205 #endif /* CONFIG_KGDB_KDB */
162 enum { 206 enum {
163 KDB_NOT_INITIALIZED, 207 KDB_NOT_INITIALIZED,
164 KDB_INIT_EARLY, 208 KDB_INIT_EARLY,
165 KDB_INIT_FULL, 209 KDB_INIT_FULL,
166 }; 210 };
167 211
168 extern int kdbgetintenv(const char *, int *); 212 extern int kdbgetintenv(const char *, int *);
169 extern int kdb_set(int, const char **); 213 extern int kdb_set(int, const char **);
170 214
171 #endif /* !_KDB_H */ 215 #endif /* !_KDB_H */
172 216
kernel/debug/debug_core.c
1 /* 1 /*
2 * Kernel Debug Core 2 * Kernel Debug Core
3 * 3 *
4 * Maintainer: Jason Wessel <jason.wessel@windriver.com> 4 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
5 * 5 *
6 * Copyright (C) 2000-2001 VERITAS Software Corporation. 6 * Copyright (C) 2000-2001 VERITAS Software Corporation.
7 * Copyright (C) 2002-2004 Timesys Corporation 7 * Copyright (C) 2002-2004 Timesys Corporation
8 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com> 8 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
9 * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz> 9 * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz>
10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org> 10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd. 11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
12 * Copyright (C) 2005-2009 Wind River Systems, Inc. 12 * Copyright (C) 2005-2009 Wind River Systems, Inc.
13 * Copyright (C) 2007 MontaVista Software, Inc. 13 * Copyright (C) 2007 MontaVista Software, Inc.
14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
15 * 15 *
16 * Contributors at various stages not listed above: 16 * Contributors at various stages not listed above:
17 * Jason Wessel ( jason.wessel@windriver.com ) 17 * Jason Wessel ( jason.wessel@windriver.com )
18 * George Anzinger <george@mvista.com> 18 * George Anzinger <george@mvista.com>
19 * Anurekh Saxena (anurekh.saxena@timesys.com) 19 * Anurekh Saxena (anurekh.saxena@timesys.com)
20 * Lake Stevens Instrument Division (Glenn Engel) 20 * Lake Stevens Instrument Division (Glenn Engel)
21 * Jim Kingdon, Cygnus Support. 21 * Jim Kingdon, Cygnus Support.
22 * 22 *
23 * Original KGDB stub: David Grothe <dave@gcom.com>, 23 * Original KGDB stub: David Grothe <dave@gcom.com>,
24 * Tigran Aivazian <tigran@sco.com> 24 * Tigran Aivazian <tigran@sco.com>
25 * 25 *
26 * This file is licensed under the terms of the GNU General Public License 26 * This file is licensed under the terms of the GNU General Public License
27 * version 2. This program is licensed "as is" without any warranty of any 27 * version 2. This program is licensed "as is" without any warranty of any
28 * kind, whether express or implied. 28 * kind, whether express or implied.
29 */ 29 */
30
31 #define pr_fmt(fmt) "KGDB: " fmt
32
30 #include <linux/pid_namespace.h> 33 #include <linux/pid_namespace.h>
31 #include <linux/clocksource.h> 34 #include <linux/clocksource.h>
32 #include <linux/serial_core.h> 35 #include <linux/serial_core.h>
33 #include <linux/interrupt.h> 36 #include <linux/interrupt.h>
34 #include <linux/spinlock.h> 37 #include <linux/spinlock.h>
35 #include <linux/console.h> 38 #include <linux/console.h>
36 #include <linux/threads.h> 39 #include <linux/threads.h>
37 #include <linux/uaccess.h> 40 #include <linux/uaccess.h>
38 #include <linux/kernel.h> 41 #include <linux/kernel.h>
39 #include <linux/module.h> 42 #include <linux/module.h>
40 #include <linux/ptrace.h> 43 #include <linux/ptrace.h>
41 #include <linux/string.h> 44 #include <linux/string.h>
42 #include <linux/delay.h> 45 #include <linux/delay.h>
43 #include <linux/sched.h> 46 #include <linux/sched.h>
44 #include <linux/sysrq.h> 47 #include <linux/sysrq.h>
45 #include <linux/reboot.h> 48 #include <linux/reboot.h>
46 #include <linux/init.h> 49 #include <linux/init.h>
47 #include <linux/kgdb.h> 50 #include <linux/kgdb.h>
48 #include <linux/kdb.h> 51 #include <linux/kdb.h>
49 #include <linux/pid.h> 52 #include <linux/pid.h>
50 #include <linux/smp.h> 53 #include <linux/smp.h>
51 #include <linux/mm.h> 54 #include <linux/mm.h>
52 #include <linux/vmacache.h> 55 #include <linux/vmacache.h>
53 #include <linux/rcupdate.h> 56 #include <linux/rcupdate.h>
54 57
55 #include <asm/cacheflush.h> 58 #include <asm/cacheflush.h>
56 #include <asm/byteorder.h> 59 #include <asm/byteorder.h>
57 #include <linux/atomic.h> 60 #include <linux/atomic.h>
58 61
59 #include "debug_core.h" 62 #include "debug_core.h"
60 63
61 static int kgdb_break_asap; 64 static int kgdb_break_asap;
62 65
63 struct debuggerinfo_struct kgdb_info[NR_CPUS]; 66 struct debuggerinfo_struct kgdb_info[NR_CPUS];
64 67
65 /** 68 /**
66 * kgdb_connected - Is a host GDB connected to us? 69 * kgdb_connected - Is a host GDB connected to us?
67 */ 70 */
68 int kgdb_connected; 71 int kgdb_connected;
69 EXPORT_SYMBOL_GPL(kgdb_connected); 72 EXPORT_SYMBOL_GPL(kgdb_connected);
70 73
71 /* All the KGDB handlers are installed */ 74 /* All the KGDB handlers are installed */
72 int kgdb_io_module_registered; 75 int kgdb_io_module_registered;
73 76
74 /* Guard for recursive entry */ 77 /* Guard for recursive entry */
75 static int exception_level; 78 static int exception_level;
76 79
77 struct kgdb_io *dbg_io_ops; 80 struct kgdb_io *dbg_io_ops;
78 static DEFINE_SPINLOCK(kgdb_registration_lock); 81 static DEFINE_SPINLOCK(kgdb_registration_lock);
79 82
80 /* Action for the reboot notifiter, a global allow kdb to change it */ 83 /* Action for the reboot notifiter, a global allow kdb to change it */
81 static int kgdbreboot; 84 static int kgdbreboot;
82 /* kgdb console driver is loaded */ 85 /* kgdb console driver is loaded */
83 static int kgdb_con_registered; 86 static int kgdb_con_registered;
84 /* determine if kgdb console output should be used */ 87 /* determine if kgdb console output should be used */
85 static int kgdb_use_con; 88 static int kgdb_use_con;
86 /* Flag for alternate operations for early debugging */ 89 /* Flag for alternate operations for early debugging */
87 bool dbg_is_early = true; 90 bool dbg_is_early = true;
88 /* Next cpu to become the master debug core */ 91 /* Next cpu to become the master debug core */
89 int dbg_switch_cpu; 92 int dbg_switch_cpu;
90 93
91 /* Use kdb or gdbserver mode */ 94 /* Use kdb or gdbserver mode */
92 int dbg_kdb_mode = 1; 95 int dbg_kdb_mode = 1;
93 96
94 static int __init opt_kgdb_con(char *str) 97 static int __init opt_kgdb_con(char *str)
95 { 98 {
96 kgdb_use_con = 1; 99 kgdb_use_con = 1;
97 return 0; 100 return 0;
98 } 101 }
99 102
100 early_param("kgdbcon", opt_kgdb_con); 103 early_param("kgdbcon", opt_kgdb_con);
101 104
102 module_param(kgdb_use_con, int, 0644); 105 module_param(kgdb_use_con, int, 0644);
103 module_param(kgdbreboot, int, 0644); 106 module_param(kgdbreboot, int, 0644);
104 107
105 /* 108 /*
106 * Holds information about breakpoints in a kernel. These breakpoints are 109 * Holds information about breakpoints in a kernel. These breakpoints are
107 * added and removed by gdb. 110 * added and removed by gdb.
108 */ 111 */
109 static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = { 112 static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
110 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED } 113 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
111 }; 114 };
112 115
113 /* 116 /*
114 * The CPU# of the active CPU, or -1 if none: 117 * The CPU# of the active CPU, or -1 if none:
115 */ 118 */
116 atomic_t kgdb_active = ATOMIC_INIT(-1); 119 atomic_t kgdb_active = ATOMIC_INIT(-1);
117 EXPORT_SYMBOL_GPL(kgdb_active); 120 EXPORT_SYMBOL_GPL(kgdb_active);
118 static DEFINE_RAW_SPINLOCK(dbg_master_lock); 121 static DEFINE_RAW_SPINLOCK(dbg_master_lock);
119 static DEFINE_RAW_SPINLOCK(dbg_slave_lock); 122 static DEFINE_RAW_SPINLOCK(dbg_slave_lock);
120 123
121 /* 124 /*
122 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early 125 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
123 * bootup code (which might not have percpu set up yet): 126 * bootup code (which might not have percpu set up yet):
124 */ 127 */
125 static atomic_t masters_in_kgdb; 128 static atomic_t masters_in_kgdb;
126 static atomic_t slaves_in_kgdb; 129 static atomic_t slaves_in_kgdb;
127 static atomic_t kgdb_break_tasklet_var; 130 static atomic_t kgdb_break_tasklet_var;
128 atomic_t kgdb_setting_breakpoint; 131 atomic_t kgdb_setting_breakpoint;
129 132
130 struct task_struct *kgdb_usethread; 133 struct task_struct *kgdb_usethread;
131 struct task_struct *kgdb_contthread; 134 struct task_struct *kgdb_contthread;
132 135
133 int kgdb_single_step; 136 int kgdb_single_step;
134 static pid_t kgdb_sstep_pid; 137 static pid_t kgdb_sstep_pid;
135 138
136 /* to keep track of the CPU which is doing the single stepping*/ 139 /* to keep track of the CPU which is doing the single stepping*/
137 atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1); 140 atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
138 141
139 /* 142 /*
140 * If you are debugging a problem where roundup (the collection of 143 * If you are debugging a problem where roundup (the collection of
141 * all other CPUs) is a problem [this should be extremely rare], 144 * all other CPUs) is a problem [this should be extremely rare],
142 * then use the nokgdbroundup option to avoid roundup. In that case 145 * then use the nokgdbroundup option to avoid roundup. In that case
143 * the other CPUs might interfere with your debugging context, so 146 * the other CPUs might interfere with your debugging context, so
144 * use this with care: 147 * use this with care:
145 */ 148 */
146 static int kgdb_do_roundup = 1; 149 static int kgdb_do_roundup = 1;
147 150
148 static int __init opt_nokgdbroundup(char *str) 151 static int __init opt_nokgdbroundup(char *str)
149 { 152 {
150 kgdb_do_roundup = 0; 153 kgdb_do_roundup = 0;
151 154
152 return 0; 155 return 0;
153 } 156 }
154 157
155 early_param("nokgdbroundup", opt_nokgdbroundup); 158 early_param("nokgdbroundup", opt_nokgdbroundup);
156 159
157 /* 160 /*
158 * Finally, some KGDB code :-) 161 * Finally, some KGDB code :-)
159 */ 162 */
160 163
161 /* 164 /*
162 * Weak aliases for breakpoint management, 165 * Weak aliases for breakpoint management,
163 * can be overriden by architectures when needed: 166 * can be overriden by architectures when needed:
164 */ 167 */
165 int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) 168 int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
166 { 169 {
167 int err; 170 int err;
168 171
169 err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr, 172 err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
170 BREAK_INSTR_SIZE); 173 BREAK_INSTR_SIZE);
171 if (err) 174 if (err)
172 return err; 175 return err;
173 err = probe_kernel_write((char *)bpt->bpt_addr, 176 err = probe_kernel_write((char *)bpt->bpt_addr,
174 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); 177 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
175 return err; 178 return err;
176 } 179 }
177 180
178 int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt) 181 int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
179 { 182 {
180 return probe_kernel_write((char *)bpt->bpt_addr, 183 return probe_kernel_write((char *)bpt->bpt_addr,
181 (char *)bpt->saved_instr, BREAK_INSTR_SIZE); 184 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
182 } 185 }
183 186
184 int __weak kgdb_validate_break_address(unsigned long addr) 187 int __weak kgdb_validate_break_address(unsigned long addr)
185 { 188 {
186 struct kgdb_bkpt tmp; 189 struct kgdb_bkpt tmp;
187 int err; 190 int err;
188 /* Validate setting the breakpoint and then removing it. If the 191 /* Validate setting the breakpoint and then removing it. If the
189 * remove fails, the kernel needs to emit a bad message because we 192 * remove fails, the kernel needs to emit a bad message because we
190 * are deep trouble not being able to put things back the way we 193 * are deep trouble not being able to put things back the way we
191 * found them. 194 * found them.
192 */ 195 */
193 tmp.bpt_addr = addr; 196 tmp.bpt_addr = addr;
194 err = kgdb_arch_set_breakpoint(&tmp); 197 err = kgdb_arch_set_breakpoint(&tmp);
195 if (err) 198 if (err)
196 return err; 199 return err;
197 err = kgdb_arch_remove_breakpoint(&tmp); 200 err = kgdb_arch_remove_breakpoint(&tmp);
198 if (err) 201 if (err)
199 printk(KERN_ERR "KGDB: Critical breakpoint error, kernel " 202 pr_err("Critical breakpoint error, kernel memory destroyed at: %lx\n",
200 "memory destroyed at: %lx", addr); 203 addr);
201 return err; 204 return err;
202 } 205 }
203 206
204 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs) 207 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
205 { 208 {
206 return instruction_pointer(regs); 209 return instruction_pointer(regs);
207 } 210 }
208 211
209 int __weak kgdb_arch_init(void) 212 int __weak kgdb_arch_init(void)
210 { 213 {
211 return 0; 214 return 0;
212 } 215 }
213 216
214 int __weak kgdb_skipexception(int exception, struct pt_regs *regs) 217 int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
215 { 218 {
216 return 0; 219 return 0;
217 } 220 }
218 221
219 /* 222 /*
220 * Some architectures need cache flushes when we set/clear a 223 * Some architectures need cache flushes when we set/clear a
221 * breakpoint: 224 * breakpoint:
222 */ 225 */
223 static void kgdb_flush_swbreak_addr(unsigned long addr) 226 static void kgdb_flush_swbreak_addr(unsigned long addr)
224 { 227 {
225 if (!CACHE_FLUSH_IS_SAFE) 228 if (!CACHE_FLUSH_IS_SAFE)
226 return; 229 return;
227 230
228 if (current->mm) { 231 if (current->mm) {
229 int i; 232 int i;
230 233
231 for (i = 0; i < VMACACHE_SIZE; i++) { 234 for (i = 0; i < VMACACHE_SIZE; i++) {
232 if (!current->vmacache[i]) 235 if (!current->vmacache[i])
233 continue; 236 continue;
234 flush_cache_range(current->vmacache[i], 237 flush_cache_range(current->vmacache[i],
235 addr, addr + BREAK_INSTR_SIZE); 238 addr, addr + BREAK_INSTR_SIZE);
236 } 239 }
237 } 240 }
238 241
239 /* Force flush instruction cache if it was outside the mm */ 242 /* Force flush instruction cache if it was outside the mm */
240 flush_icache_range(addr, addr + BREAK_INSTR_SIZE); 243 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
241 } 244 }
242 245
243 /* 246 /*
244 * SW breakpoint management: 247 * SW breakpoint management:
245 */ 248 */
246 int dbg_activate_sw_breakpoints(void) 249 int dbg_activate_sw_breakpoints(void)
247 { 250 {
248 int error; 251 int error;
249 int ret = 0; 252 int ret = 0;
250 int i; 253 int i;
251 254
252 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 255 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
253 if (kgdb_break[i].state != BP_SET) 256 if (kgdb_break[i].state != BP_SET)
254 continue; 257 continue;
255 258
256 error = kgdb_arch_set_breakpoint(&kgdb_break[i]); 259 error = kgdb_arch_set_breakpoint(&kgdb_break[i]);
257 if (error) { 260 if (error) {
258 ret = error; 261 ret = error;
259 printk(KERN_INFO "KGDB: BP install failed: %lx", 262 pr_info("BP install failed: %lx\n",
260 kgdb_break[i].bpt_addr); 263 kgdb_break[i].bpt_addr);
261 continue; 264 continue;
262 } 265 }
263 266
264 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr); 267 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
265 kgdb_break[i].state = BP_ACTIVE; 268 kgdb_break[i].state = BP_ACTIVE;
266 } 269 }
267 return ret; 270 return ret;
268 } 271 }
269 272
270 int dbg_set_sw_break(unsigned long addr) 273 int dbg_set_sw_break(unsigned long addr)
271 { 274 {
272 int err = kgdb_validate_break_address(addr); 275 int err = kgdb_validate_break_address(addr);
273 int breakno = -1; 276 int breakno = -1;
274 int i; 277 int i;
275 278
276 if (err) 279 if (err)
277 return err; 280 return err;
278 281
279 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 282 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
280 if ((kgdb_break[i].state == BP_SET) && 283 if ((kgdb_break[i].state == BP_SET) &&
281 (kgdb_break[i].bpt_addr == addr)) 284 (kgdb_break[i].bpt_addr == addr))
282 return -EEXIST; 285 return -EEXIST;
283 } 286 }
284 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 287 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
285 if (kgdb_break[i].state == BP_REMOVED && 288 if (kgdb_break[i].state == BP_REMOVED &&
286 kgdb_break[i].bpt_addr == addr) { 289 kgdb_break[i].bpt_addr == addr) {
287 breakno = i; 290 breakno = i;
288 break; 291 break;
289 } 292 }
290 } 293 }
291 294
292 if (breakno == -1) { 295 if (breakno == -1) {
293 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 296 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
294 if (kgdb_break[i].state == BP_UNDEFINED) { 297 if (kgdb_break[i].state == BP_UNDEFINED) {
295 breakno = i; 298 breakno = i;
296 break; 299 break;
297 } 300 }
298 } 301 }
299 } 302 }
300 303
301 if (breakno == -1) 304 if (breakno == -1)
302 return -E2BIG; 305 return -E2BIG;
303 306
304 kgdb_break[breakno].state = BP_SET; 307 kgdb_break[breakno].state = BP_SET;
305 kgdb_break[breakno].type = BP_BREAKPOINT; 308 kgdb_break[breakno].type = BP_BREAKPOINT;
306 kgdb_break[breakno].bpt_addr = addr; 309 kgdb_break[breakno].bpt_addr = addr;
307 310
308 return 0; 311 return 0;
309 } 312 }
310 313
311 int dbg_deactivate_sw_breakpoints(void) 314 int dbg_deactivate_sw_breakpoints(void)
312 { 315 {
313 int error; 316 int error;
314 int ret = 0; 317 int ret = 0;
315 int i; 318 int i;
316 319
317 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 320 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
318 if (kgdb_break[i].state != BP_ACTIVE) 321 if (kgdb_break[i].state != BP_ACTIVE)
319 continue; 322 continue;
320 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]); 323 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
321 if (error) { 324 if (error) {
322 printk(KERN_INFO "KGDB: BP remove failed: %lx\n", 325 pr_info("BP remove failed: %lx\n",
323 kgdb_break[i].bpt_addr); 326 kgdb_break[i].bpt_addr);
324 ret = error; 327 ret = error;
325 } 328 }
326 329
327 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr); 330 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
328 kgdb_break[i].state = BP_SET; 331 kgdb_break[i].state = BP_SET;
329 } 332 }
330 return ret; 333 return ret;
331 } 334 }
332 335
333 int dbg_remove_sw_break(unsigned long addr) 336 int dbg_remove_sw_break(unsigned long addr)
334 { 337 {
335 int i; 338 int i;
336 339
337 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 340 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
338 if ((kgdb_break[i].state == BP_SET) && 341 if ((kgdb_break[i].state == BP_SET) &&
339 (kgdb_break[i].bpt_addr == addr)) { 342 (kgdb_break[i].bpt_addr == addr)) {
340 kgdb_break[i].state = BP_REMOVED; 343 kgdb_break[i].state = BP_REMOVED;
341 return 0; 344 return 0;
342 } 345 }
343 } 346 }
344 return -ENOENT; 347 return -ENOENT;
345 } 348 }
346 349
347 int kgdb_isremovedbreak(unsigned long addr) 350 int kgdb_isremovedbreak(unsigned long addr)
348 { 351 {
349 int i; 352 int i;
350 353
351 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 354 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
352 if ((kgdb_break[i].state == BP_REMOVED) && 355 if ((kgdb_break[i].state == BP_REMOVED) &&
353 (kgdb_break[i].bpt_addr == addr)) 356 (kgdb_break[i].bpt_addr == addr))
354 return 1; 357 return 1;
355 } 358 }
356 return 0; 359 return 0;
357 } 360 }
358 361
359 int dbg_remove_all_break(void) 362 int dbg_remove_all_break(void)
360 { 363 {
361 int error; 364 int error;
362 int i; 365 int i;
363 366
364 /* Clear memory breakpoints. */ 367 /* Clear memory breakpoints. */
365 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { 368 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
366 if (kgdb_break[i].state != BP_ACTIVE) 369 if (kgdb_break[i].state != BP_ACTIVE)
367 goto setundefined; 370 goto setundefined;
368 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]); 371 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
369 if (error) 372 if (error)
370 printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n", 373 pr_err("breakpoint remove failed: %lx\n",
371 kgdb_break[i].bpt_addr); 374 kgdb_break[i].bpt_addr);
372 setundefined: 375 setundefined:
373 kgdb_break[i].state = BP_UNDEFINED; 376 kgdb_break[i].state = BP_UNDEFINED;
374 } 377 }
375 378
376 /* Clear hardware breakpoints. */ 379 /* Clear hardware breakpoints. */
377 if (arch_kgdb_ops.remove_all_hw_break) 380 if (arch_kgdb_ops.remove_all_hw_break)
378 arch_kgdb_ops.remove_all_hw_break(); 381 arch_kgdb_ops.remove_all_hw_break();
379 382
380 return 0; 383 return 0;
381 } 384 }
382 385
383 /* 386 /*
384 * Return true if there is a valid kgdb I/O module. Also if no 387 * Return true if there is a valid kgdb I/O module. Also if no
385 * debugger is attached a message can be printed to the console about 388 * debugger is attached a message can be printed to the console about
386 * waiting for the debugger to attach. 389 * waiting for the debugger to attach.
387 * 390 *
388 * The print_wait argument is only to be true when called from inside 391 * The print_wait argument is only to be true when called from inside
389 * the core kgdb_handle_exception, because it will wait for the 392 * the core kgdb_handle_exception, because it will wait for the
390 * debugger to attach. 393 * debugger to attach.
391 */ 394 */
392 static int kgdb_io_ready(int print_wait) 395 static int kgdb_io_ready(int print_wait)
393 { 396 {
394 if (!dbg_io_ops) 397 if (!dbg_io_ops)
395 return 0; 398 return 0;
396 if (kgdb_connected) 399 if (kgdb_connected)
397 return 1; 400 return 1;
398 if (atomic_read(&kgdb_setting_breakpoint)) 401 if (atomic_read(&kgdb_setting_breakpoint))
399 return 1; 402 return 1;
400 if (print_wait) { 403 if (print_wait) {
401 #ifdef CONFIG_KGDB_KDB 404 #ifdef CONFIG_KGDB_KDB
402 if (!dbg_kdb_mode) 405 if (!dbg_kdb_mode)
403 printk(KERN_CRIT "KGDB: waiting... or $3#33 for KDB\n"); 406 pr_crit("waiting... or $3#33 for KDB\n");
404 #else 407 #else
405 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n"); 408 pr_crit("Waiting for remote debugger\n");
406 #endif 409 #endif
407 } 410 }
408 return 1; 411 return 1;
409 } 412 }
410 413
411 static int kgdb_reenter_check(struct kgdb_state *ks) 414 static int kgdb_reenter_check(struct kgdb_state *ks)
412 { 415 {
413 unsigned long addr; 416 unsigned long addr;
414 417
415 if (atomic_read(&kgdb_active) != raw_smp_processor_id()) 418 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
416 return 0; 419 return 0;
417 420
418 /* Panic on recursive debugger calls: */ 421 /* Panic on recursive debugger calls: */
419 exception_level++; 422 exception_level++;
420 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs); 423 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
421 dbg_deactivate_sw_breakpoints(); 424 dbg_deactivate_sw_breakpoints();
422 425
423 /* 426 /*
424 * If the break point removed ok at the place exception 427 * If the break point removed ok at the place exception
425 * occurred, try to recover and print a warning to the end 428 * occurred, try to recover and print a warning to the end
426 * user because the user planted a breakpoint in a place that 429 * user because the user planted a breakpoint in a place that
427 * KGDB needs in order to function. 430 * KGDB needs in order to function.
428 */ 431 */
429 if (dbg_remove_sw_break(addr) == 0) { 432 if (dbg_remove_sw_break(addr) == 0) {
430 exception_level = 0; 433 exception_level = 0;
431 kgdb_skipexception(ks->ex_vector, ks->linux_regs); 434 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
432 dbg_activate_sw_breakpoints(); 435 dbg_activate_sw_breakpoints();
433 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n", 436 pr_crit("re-enter error: breakpoint removed %lx\n", addr);
434 addr);
435 WARN_ON_ONCE(1); 437 WARN_ON_ONCE(1);
436 438
437 return 1; 439 return 1;
438 } 440 }
439 dbg_remove_all_break(); 441 dbg_remove_all_break();
440 kgdb_skipexception(ks->ex_vector, ks->linux_regs); 442 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
441 443
442 if (exception_level > 1) { 444 if (exception_level > 1) {
443 dump_stack(); 445 dump_stack();
444 panic("Recursive entry to debugger"); 446 panic("Recursive entry to debugger");
445 } 447 }
446 448
447 printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n"); 449 pr_crit("re-enter exception: ALL breakpoints killed\n");
448 #ifdef CONFIG_KGDB_KDB 450 #ifdef CONFIG_KGDB_KDB
449 /* Allow kdb to debug itself one level */ 451 /* Allow kdb to debug itself one level */
450 return 0; 452 return 0;
451 #endif 453 #endif
452 dump_stack(); 454 dump_stack();
453 panic("Recursive entry to debugger"); 455 panic("Recursive entry to debugger");
454 456
455 return 1; 457 return 1;
456 } 458 }
457 459
458 static void dbg_touch_watchdogs(void) 460 static void dbg_touch_watchdogs(void)
459 { 461 {
460 touch_softlockup_watchdog_sync(); 462 touch_softlockup_watchdog_sync();
461 clocksource_touch_watchdog(); 463 clocksource_touch_watchdog();
462 rcu_cpu_stall_reset(); 464 rcu_cpu_stall_reset();
463 } 465 }
464 466
465 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs, 467 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
466 int exception_state) 468 int exception_state)
467 { 469 {
468 unsigned long flags; 470 unsigned long flags;
469 int sstep_tries = 100; 471 int sstep_tries = 100;
470 int error; 472 int error;
471 int cpu; 473 int cpu;
472 int trace_on = 0; 474 int trace_on = 0;
473 int online_cpus = num_online_cpus(); 475 int online_cpus = num_online_cpus();
476 u64 time_left;
474 477
475 kgdb_info[ks->cpu].enter_kgdb++; 478 kgdb_info[ks->cpu].enter_kgdb++;
476 kgdb_info[ks->cpu].exception_state |= exception_state; 479 kgdb_info[ks->cpu].exception_state |= exception_state;
477 480
478 if (exception_state == DCPU_WANT_MASTER) 481 if (exception_state == DCPU_WANT_MASTER)
479 atomic_inc(&masters_in_kgdb); 482 atomic_inc(&masters_in_kgdb);
480 else 483 else
481 atomic_inc(&slaves_in_kgdb); 484 atomic_inc(&slaves_in_kgdb);
482 485
483 if (arch_kgdb_ops.disable_hw_break) 486 if (arch_kgdb_ops.disable_hw_break)
484 arch_kgdb_ops.disable_hw_break(regs); 487 arch_kgdb_ops.disable_hw_break(regs);
485 488
486 acquirelock: 489 acquirelock:
487 /* 490 /*
488 * Interrupts will be restored by the 'trap return' code, except when 491 * Interrupts will be restored by the 'trap return' code, except when
489 * single stepping. 492 * single stepping.
490 */ 493 */
491 local_irq_save(flags); 494 local_irq_save(flags);
492 495
493 cpu = ks->cpu; 496 cpu = ks->cpu;
494 kgdb_info[cpu].debuggerinfo = regs; 497 kgdb_info[cpu].debuggerinfo = regs;
495 kgdb_info[cpu].task = current; 498 kgdb_info[cpu].task = current;
496 kgdb_info[cpu].ret_state = 0; 499 kgdb_info[cpu].ret_state = 0;
497 kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT; 500 kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
498 501
499 /* Make sure the above info reaches the primary CPU */ 502 /* Make sure the above info reaches the primary CPU */
500 smp_mb(); 503 smp_mb();
501 504
502 if (exception_level == 1) { 505 if (exception_level == 1) {
503 if (raw_spin_trylock(&dbg_master_lock)) 506 if (raw_spin_trylock(&dbg_master_lock))
504 atomic_xchg(&kgdb_active, cpu); 507 atomic_xchg(&kgdb_active, cpu);
505 goto cpu_master_loop; 508 goto cpu_master_loop;
506 } 509 }
507 510
508 /* 511 /*
509 * CPU will loop if it is a slave or request to become a kgdb 512 * CPU will loop if it is a slave or request to become a kgdb
510 * master cpu and acquire the kgdb_active lock: 513 * master cpu and acquire the kgdb_active lock:
511 */ 514 */
512 while (1) { 515 while (1) {
513 cpu_loop: 516 cpu_loop:
514 if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) { 517 if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) {
515 kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER; 518 kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER;
516 goto cpu_master_loop; 519 goto cpu_master_loop;
517 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) { 520 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
518 if (raw_spin_trylock(&dbg_master_lock)) { 521 if (raw_spin_trylock(&dbg_master_lock)) {
519 atomic_xchg(&kgdb_active, cpu); 522 atomic_xchg(&kgdb_active, cpu);
520 break; 523 break;
521 } 524 }
522 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) { 525 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
523 if (!raw_spin_is_locked(&dbg_slave_lock)) 526 if (!raw_spin_is_locked(&dbg_slave_lock))
524 goto return_normal; 527 goto return_normal;
525 } else { 528 } else {
526 return_normal: 529 return_normal:
527 /* Return to normal operation by executing any 530 /* Return to normal operation by executing any
528 * hw breakpoint fixup. 531 * hw breakpoint fixup.
529 */ 532 */
530 if (arch_kgdb_ops.correct_hw_break) 533 if (arch_kgdb_ops.correct_hw_break)
531 arch_kgdb_ops.correct_hw_break(); 534 arch_kgdb_ops.correct_hw_break();
532 if (trace_on) 535 if (trace_on)
533 tracing_on(); 536 tracing_on();
534 kgdb_info[cpu].exception_state &= 537 kgdb_info[cpu].exception_state &=
535 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE); 538 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
536 kgdb_info[cpu].enter_kgdb--; 539 kgdb_info[cpu].enter_kgdb--;
537 smp_mb__before_atomic(); 540 smp_mb__before_atomic();
538 atomic_dec(&slaves_in_kgdb); 541 atomic_dec(&slaves_in_kgdb);
539 dbg_touch_watchdogs(); 542 dbg_touch_watchdogs();
540 local_irq_restore(flags); 543 local_irq_restore(flags);
541 return 0; 544 return 0;
542 } 545 }
543 cpu_relax(); 546 cpu_relax();
544 } 547 }
545 548
546 /* 549 /*
547 * For single stepping, try to only enter on the processor 550 * For single stepping, try to only enter on the processor
548 * that was single stepping. To guard against a deadlock, the 551 * that was single stepping. To guard against a deadlock, the
549 * kernel will only try for the value of sstep_tries before 552 * kernel will only try for the value of sstep_tries before
550 * giving up and continuing on. 553 * giving up and continuing on.
551 */ 554 */
552 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 && 555 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
553 (kgdb_info[cpu].task && 556 (kgdb_info[cpu].task &&
554 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) { 557 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
555 atomic_set(&kgdb_active, -1); 558 atomic_set(&kgdb_active, -1);
556 raw_spin_unlock(&dbg_master_lock); 559 raw_spin_unlock(&dbg_master_lock);
557 dbg_touch_watchdogs(); 560 dbg_touch_watchdogs();
558 local_irq_restore(flags); 561 local_irq_restore(flags);
559 562
560 goto acquirelock; 563 goto acquirelock;
561 } 564 }
562 565
563 if (!kgdb_io_ready(1)) { 566 if (!kgdb_io_ready(1)) {
564 kgdb_info[cpu].ret_state = 1; 567 kgdb_info[cpu].ret_state = 1;
565 goto kgdb_restore; /* No I/O connection, resume the system */ 568 goto kgdb_restore; /* No I/O connection, resume the system */
566 } 569 }
567 570
568 /* 571 /*
569 * Don't enter if we have hit a removed breakpoint. 572 * Don't enter if we have hit a removed breakpoint.
570 */ 573 */
571 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs)) 574 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
572 goto kgdb_restore; 575 goto kgdb_restore;
573 576
574 /* Call the I/O driver's pre_exception routine */ 577 /* Call the I/O driver's pre_exception routine */
575 if (dbg_io_ops->pre_exception) 578 if (dbg_io_ops->pre_exception)
576 dbg_io_ops->pre_exception(); 579 dbg_io_ops->pre_exception();
577 580
578 /* 581 /*
579 * Get the passive CPU lock which will hold all the non-primary 582 * Get the passive CPU lock which will hold all the non-primary
580 * CPU in a spin state while the debugger is active 583 * CPU in a spin state while the debugger is active
581 */ 584 */
582 if (!kgdb_single_step) 585 if (!kgdb_single_step)
583 raw_spin_lock(&dbg_slave_lock); 586 raw_spin_lock(&dbg_slave_lock);
584 587
585 #ifdef CONFIG_SMP 588 #ifdef CONFIG_SMP
586 /* If send_ready set, slaves are already waiting */ 589 /* If send_ready set, slaves are already waiting */
587 if (ks->send_ready) 590 if (ks->send_ready)
588 atomic_set(ks->send_ready, 1); 591 atomic_set(ks->send_ready, 1);
589 592
590 /* Signal the other CPUs to enter kgdb_wait() */ 593 /* Signal the other CPUs to enter kgdb_wait() */
591 else if ((!kgdb_single_step) && kgdb_do_roundup) 594 else if ((!kgdb_single_step) && kgdb_do_roundup)
592 kgdb_roundup_cpus(flags); 595 kgdb_roundup_cpus(flags);
593 #endif 596 #endif
594 597
595 /* 598 /*
596 * Wait for the other CPUs to be notified and be waiting for us: 599 * Wait for the other CPUs to be notified and be waiting for us:
597 */ 600 */
598 while (kgdb_do_roundup && (atomic_read(&masters_in_kgdb) + 601 time_left = loops_per_jiffy * HZ;
599 atomic_read(&slaves_in_kgdb)) != online_cpus) 602 while (kgdb_do_roundup && --time_left &&
603 (atomic_read(&masters_in_kgdb) + atomic_read(&slaves_in_kgdb)) !=
604 online_cpus)
600 cpu_relax(); 605 cpu_relax();
606 if (!time_left)
607 pr_crit("KGDB: Timed out waiting for secondary CPUs.\n");
601 608
602 /* 609 /*
603 * At this point the primary processor is completely 610 * At this point the primary processor is completely
604 * in the debugger and all secondary CPUs are quiescent 611 * in the debugger and all secondary CPUs are quiescent
605 */ 612 */
606 dbg_deactivate_sw_breakpoints(); 613 dbg_deactivate_sw_breakpoints();
607 kgdb_single_step = 0; 614 kgdb_single_step = 0;
608 kgdb_contthread = current; 615 kgdb_contthread = current;
609 exception_level = 0; 616 exception_level = 0;
610 trace_on = tracing_is_on(); 617 trace_on = tracing_is_on();
611 if (trace_on) 618 if (trace_on)
612 tracing_off(); 619 tracing_off();
613 620
614 while (1) { 621 while (1) {
615 cpu_master_loop: 622 cpu_master_loop:
616 if (dbg_kdb_mode) { 623 if (dbg_kdb_mode) {
617 kgdb_connected = 1; 624 kgdb_connected = 1;
618 error = kdb_stub(ks); 625 error = kdb_stub(ks);
619 if (error == -1) 626 if (error == -1)
620 continue; 627 continue;
621 kgdb_connected = 0; 628 kgdb_connected = 0;
622 } else { 629 } else {
623 error = gdb_serial_stub(ks); 630 error = gdb_serial_stub(ks);
624 } 631 }
625 632
626 if (error == DBG_PASS_EVENT) { 633 if (error == DBG_PASS_EVENT) {
627 dbg_kdb_mode = !dbg_kdb_mode; 634 dbg_kdb_mode = !dbg_kdb_mode;
628 } else if (error == DBG_SWITCH_CPU_EVENT) { 635 } else if (error == DBG_SWITCH_CPU_EVENT) {
629 kgdb_info[dbg_switch_cpu].exception_state |= 636 kgdb_info[dbg_switch_cpu].exception_state |=
630 DCPU_NEXT_MASTER; 637 DCPU_NEXT_MASTER;
631 goto cpu_loop; 638 goto cpu_loop;
632 } else { 639 } else {
633 kgdb_info[cpu].ret_state = error; 640 kgdb_info[cpu].ret_state = error;
634 break; 641 break;
635 } 642 }
636 } 643 }
637 644
638 /* Call the I/O driver's post_exception routine */ 645 /* Call the I/O driver's post_exception routine */
639 if (dbg_io_ops->post_exception) 646 if (dbg_io_ops->post_exception)
640 dbg_io_ops->post_exception(); 647 dbg_io_ops->post_exception();
641 648
642 if (!kgdb_single_step) { 649 if (!kgdb_single_step) {
643 raw_spin_unlock(&dbg_slave_lock); 650 raw_spin_unlock(&dbg_slave_lock);
644 /* Wait till all the CPUs have quit from the debugger. */ 651 /* Wait till all the CPUs have quit from the debugger. */
645 while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb)) 652 while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb))
646 cpu_relax(); 653 cpu_relax();
647 } 654 }
648 655
649 kgdb_restore: 656 kgdb_restore:
650 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) { 657 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
651 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step); 658 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
652 if (kgdb_info[sstep_cpu].task) 659 if (kgdb_info[sstep_cpu].task)
653 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid; 660 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
654 else 661 else
655 kgdb_sstep_pid = 0; 662 kgdb_sstep_pid = 0;
656 } 663 }
657 if (arch_kgdb_ops.correct_hw_break) 664 if (arch_kgdb_ops.correct_hw_break)
658 arch_kgdb_ops.correct_hw_break(); 665 arch_kgdb_ops.correct_hw_break();
659 if (trace_on) 666 if (trace_on)
660 tracing_on(); 667 tracing_on();
661 668
662 kgdb_info[cpu].exception_state &= 669 kgdb_info[cpu].exception_state &=
663 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE); 670 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
664 kgdb_info[cpu].enter_kgdb--; 671 kgdb_info[cpu].enter_kgdb--;
665 smp_mb__before_atomic(); 672 smp_mb__before_atomic();
666 atomic_dec(&masters_in_kgdb); 673 atomic_dec(&masters_in_kgdb);
667 /* Free kgdb_active */ 674 /* Free kgdb_active */
668 atomic_set(&kgdb_active, -1); 675 atomic_set(&kgdb_active, -1);
669 raw_spin_unlock(&dbg_master_lock); 676 raw_spin_unlock(&dbg_master_lock);
670 dbg_touch_watchdogs(); 677 dbg_touch_watchdogs();
671 local_irq_restore(flags); 678 local_irq_restore(flags);
672 679
673 return kgdb_info[cpu].ret_state; 680 return kgdb_info[cpu].ret_state;
674 } 681 }
675 682
676 /* 683 /*
677 * kgdb_handle_exception() - main entry point from a kernel exception 684 * kgdb_handle_exception() - main entry point from a kernel exception
678 * 685 *
679 * Locking hierarchy: 686 * Locking hierarchy:
680 * interface locks, if any (begin_session) 687 * interface locks, if any (begin_session)
681 * kgdb lock (kgdb_active) 688 * kgdb lock (kgdb_active)
682 */ 689 */
683 int 690 int
684 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs) 691 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
685 { 692 {
686 struct kgdb_state kgdb_var; 693 struct kgdb_state kgdb_var;
687 struct kgdb_state *ks = &kgdb_var; 694 struct kgdb_state *ks = &kgdb_var;
688 int ret = 0; 695 int ret = 0;
689 696
690 if (arch_kgdb_ops.enable_nmi) 697 if (arch_kgdb_ops.enable_nmi)
691 arch_kgdb_ops.enable_nmi(0); 698 arch_kgdb_ops.enable_nmi(0);
692 699
693 memset(ks, 0, sizeof(struct kgdb_state)); 700 memset(ks, 0, sizeof(struct kgdb_state));
694 ks->cpu = raw_smp_processor_id(); 701 ks->cpu = raw_smp_processor_id();
695 ks->ex_vector = evector; 702 ks->ex_vector = evector;
696 ks->signo = signo; 703 ks->signo = signo;
697 ks->err_code = ecode; 704 ks->err_code = ecode;
698 ks->linux_regs = regs; 705 ks->linux_regs = regs;
699 706
700 if (kgdb_reenter_check(ks)) 707 if (kgdb_reenter_check(ks))
701 goto out; /* Ouch, double exception ! */ 708 goto out; /* Ouch, double exception ! */
702 if (kgdb_info[ks->cpu].enter_kgdb != 0) 709 if (kgdb_info[ks->cpu].enter_kgdb != 0)
703 goto out; 710 goto out;
704 711
705 ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER); 712 ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
706 out: 713 out:
707 if (arch_kgdb_ops.enable_nmi) 714 if (arch_kgdb_ops.enable_nmi)
708 arch_kgdb_ops.enable_nmi(1); 715 arch_kgdb_ops.enable_nmi(1);
709 return ret; 716 return ret;
710 } 717 }
711 718
712 /* 719 /*
713 * GDB places a breakpoint at this function to know dynamically 720 * GDB places a breakpoint at this function to know dynamically
714 * loaded objects. It's not defined static so that only one instance with this 721 * loaded objects. It's not defined static so that only one instance with this
715 * name exists in the kernel. 722 * name exists in the kernel.
716 */ 723 */
717 724
718 static int module_event(struct notifier_block *self, unsigned long val, 725 static int module_event(struct notifier_block *self, unsigned long val,
719 void *data) 726 void *data)
720 { 727 {
721 return 0; 728 return 0;
722 } 729 }
723 730
724 static struct notifier_block dbg_module_load_nb = { 731 static struct notifier_block dbg_module_load_nb = {
725 .notifier_call = module_event, 732 .notifier_call = module_event,
726 }; 733 };
727 734
728 int kgdb_nmicallback(int cpu, void *regs) 735 int kgdb_nmicallback(int cpu, void *regs)
729 { 736 {
730 #ifdef CONFIG_SMP 737 #ifdef CONFIG_SMP
731 struct kgdb_state kgdb_var; 738 struct kgdb_state kgdb_var;
732 struct kgdb_state *ks = &kgdb_var; 739 struct kgdb_state *ks = &kgdb_var;
733 740
734 memset(ks, 0, sizeof(struct kgdb_state)); 741 memset(ks, 0, sizeof(struct kgdb_state));
735 ks->cpu = cpu; 742 ks->cpu = cpu;
736 ks->linux_regs = regs; 743 ks->linux_regs = regs;
737 744
738 if (kgdb_info[ks->cpu].enter_kgdb == 0 && 745 if (kgdb_info[ks->cpu].enter_kgdb == 0 &&
739 raw_spin_is_locked(&dbg_master_lock)) { 746 raw_spin_is_locked(&dbg_master_lock)) {
740 kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE); 747 kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE);
741 return 0; 748 return 0;
742 } 749 }
743 #endif 750 #endif
744 return 1; 751 return 1;
745 } 752 }
746 753
747 int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code, 754 int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
748 atomic_t *send_ready) 755 atomic_t *send_ready)
749 { 756 {
750 #ifdef CONFIG_SMP 757 #ifdef CONFIG_SMP
751 if (!kgdb_io_ready(0) || !send_ready) 758 if (!kgdb_io_ready(0) || !send_ready)
752 return 1; 759 return 1;
753 760
754 if (kgdb_info[cpu].enter_kgdb == 0) { 761 if (kgdb_info[cpu].enter_kgdb == 0) {
755 struct kgdb_state kgdb_var; 762 struct kgdb_state kgdb_var;
756 struct kgdb_state *ks = &kgdb_var; 763 struct kgdb_state *ks = &kgdb_var;
757 764
758 memset(ks, 0, sizeof(struct kgdb_state)); 765 memset(ks, 0, sizeof(struct kgdb_state));
759 ks->cpu = cpu; 766 ks->cpu = cpu;
760 ks->ex_vector = trapnr; 767 ks->ex_vector = trapnr;
761 ks->signo = SIGTRAP; 768 ks->signo = SIGTRAP;
762 ks->err_code = err_code; 769 ks->err_code = err_code;
763 ks->linux_regs = regs; 770 ks->linux_regs = regs;
764 ks->send_ready = send_ready; 771 ks->send_ready = send_ready;
765 kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER); 772 kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
766 return 0; 773 return 0;
767 } 774 }
768 #endif 775 #endif
769 return 1; 776 return 1;
770 } 777 }
771 778
772 static void kgdb_console_write(struct console *co, const char *s, 779 static void kgdb_console_write(struct console *co, const char *s,
773 unsigned count) 780 unsigned count)
774 { 781 {
775 unsigned long flags; 782 unsigned long flags;
776 783
777 /* If we're debugging, or KGDB has not connected, don't try 784 /* If we're debugging, or KGDB has not connected, don't try
778 * and print. */ 785 * and print. */
779 if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode) 786 if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
780 return; 787 return;
781 788
782 local_irq_save(flags); 789 local_irq_save(flags);
783 gdbstub_msg_write(s, count); 790 gdbstub_msg_write(s, count);
784 local_irq_restore(flags); 791 local_irq_restore(flags);
785 } 792 }
786 793
787 static struct console kgdbcons = { 794 static struct console kgdbcons = {
788 .name = "kgdb", 795 .name = "kgdb",
789 .write = kgdb_console_write, 796 .write = kgdb_console_write,
790 .flags = CON_PRINTBUFFER | CON_ENABLED, 797 .flags = CON_PRINTBUFFER | CON_ENABLED,
791 .index = -1, 798 .index = -1,
792 }; 799 };
793 800
794 #ifdef CONFIG_MAGIC_SYSRQ 801 #ifdef CONFIG_MAGIC_SYSRQ
795 static void sysrq_handle_dbg(int key) 802 static void sysrq_handle_dbg(int key)
796 { 803 {
797 if (!dbg_io_ops) { 804 if (!dbg_io_ops) {
798 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n"); 805 pr_crit("ERROR: No KGDB I/O module available\n");
799 return; 806 return;
800 } 807 }
801 if (!kgdb_connected) { 808 if (!kgdb_connected) {
802 #ifdef CONFIG_KGDB_KDB 809 #ifdef CONFIG_KGDB_KDB
803 if (!dbg_kdb_mode) 810 if (!dbg_kdb_mode)
804 printk(KERN_CRIT "KGDB or $3#33 for KDB\n"); 811 pr_crit("KGDB or $3#33 for KDB\n");
805 #else 812 #else
806 printk(KERN_CRIT "Entering KGDB\n"); 813 pr_crit("Entering KGDB\n");
807 #endif 814 #endif
808 } 815 }
809 816
810 kgdb_breakpoint(); 817 kgdb_breakpoint();
811 } 818 }
812 819
813 static struct sysrq_key_op sysrq_dbg_op = { 820 static struct sysrq_key_op sysrq_dbg_op = {
814 .handler = sysrq_handle_dbg, 821 .handler = sysrq_handle_dbg,
815 .help_msg = "debug(g)", 822 .help_msg = "debug(g)",
816 .action_msg = "DEBUG", 823 .action_msg = "DEBUG",
817 }; 824 };
818 #endif 825 #endif
819 826
820 static int kgdb_panic_event(struct notifier_block *self, 827 static int kgdb_panic_event(struct notifier_block *self,
821 unsigned long val, 828 unsigned long val,
822 void *data) 829 void *data)
823 { 830 {
824 if (dbg_kdb_mode) 831 if (dbg_kdb_mode)
825 kdb_printf("PANIC: %s\n", (char *)data); 832 kdb_printf("PANIC: %s\n", (char *)data);
826 kgdb_breakpoint(); 833 kgdb_breakpoint();
827 return NOTIFY_DONE; 834 return NOTIFY_DONE;
828 } 835 }
829 836
830 static struct notifier_block kgdb_panic_event_nb = { 837 static struct notifier_block kgdb_panic_event_nb = {
831 .notifier_call = kgdb_panic_event, 838 .notifier_call = kgdb_panic_event,
832 .priority = INT_MAX, 839 .priority = INT_MAX,
833 }; 840 };
834 841
835 void __weak kgdb_arch_late(void) 842 void __weak kgdb_arch_late(void)
836 { 843 {
837 } 844 }
838 845
839 void __init dbg_late_init(void) 846 void __init dbg_late_init(void)
840 { 847 {
841 dbg_is_early = false; 848 dbg_is_early = false;
842 if (kgdb_io_module_registered) 849 if (kgdb_io_module_registered)
843 kgdb_arch_late(); 850 kgdb_arch_late();
844 kdb_init(KDB_INIT_FULL); 851 kdb_init(KDB_INIT_FULL);
845 } 852 }
846 853
847 static int 854 static int
848 dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x) 855 dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
849 { 856 {
850 /* 857 /*
851 * Take the following action on reboot notify depending on value: 858 * Take the following action on reboot notify depending on value:
852 * 1 == Enter debugger 859 * 1 == Enter debugger
853 * 0 == [the default] detatch debug client 860 * 0 == [the default] detatch debug client
854 * -1 == Do nothing... and use this until the board resets 861 * -1 == Do nothing... and use this until the board resets
855 */ 862 */
856 switch (kgdbreboot) { 863 switch (kgdbreboot) {
857 case 1: 864 case 1:
858 kgdb_breakpoint(); 865 kgdb_breakpoint();
859 case -1: 866 case -1:
860 goto done; 867 goto done;
861 } 868 }
862 if (!dbg_kdb_mode) 869 if (!dbg_kdb_mode)
863 gdbstub_exit(code); 870 gdbstub_exit(code);
864 done: 871 done:
865 return NOTIFY_DONE; 872 return NOTIFY_DONE;
866 } 873 }
867 874
868 static struct notifier_block dbg_reboot_notifier = { 875 static struct notifier_block dbg_reboot_notifier = {
869 .notifier_call = dbg_notify_reboot, 876 .notifier_call = dbg_notify_reboot,
870 .next = NULL, 877 .next = NULL,
871 .priority = INT_MAX, 878 .priority = INT_MAX,
872 }; 879 };
873 880
874 static void kgdb_register_callbacks(void) 881 static void kgdb_register_callbacks(void)
875 { 882 {
876 if (!kgdb_io_module_registered) { 883 if (!kgdb_io_module_registered) {
877 kgdb_io_module_registered = 1; 884 kgdb_io_module_registered = 1;
878 kgdb_arch_init(); 885 kgdb_arch_init();
879 if (!dbg_is_early) 886 if (!dbg_is_early)
880 kgdb_arch_late(); 887 kgdb_arch_late();
881 register_module_notifier(&dbg_module_load_nb); 888 register_module_notifier(&dbg_module_load_nb);
882 register_reboot_notifier(&dbg_reboot_notifier); 889 register_reboot_notifier(&dbg_reboot_notifier);
883 atomic_notifier_chain_register(&panic_notifier_list, 890 atomic_notifier_chain_register(&panic_notifier_list,
884 &kgdb_panic_event_nb); 891 &kgdb_panic_event_nb);
885 #ifdef CONFIG_MAGIC_SYSRQ 892 #ifdef CONFIG_MAGIC_SYSRQ
886 register_sysrq_key('g', &sysrq_dbg_op); 893 register_sysrq_key('g', &sysrq_dbg_op);
887 #endif 894 #endif
888 if (kgdb_use_con && !kgdb_con_registered) { 895 if (kgdb_use_con && !kgdb_con_registered) {
889 register_console(&kgdbcons); 896 register_console(&kgdbcons);
890 kgdb_con_registered = 1; 897 kgdb_con_registered = 1;
891 } 898 }
892 } 899 }
893 } 900 }
894 901
895 static void kgdb_unregister_callbacks(void) 902 static void kgdb_unregister_callbacks(void)
896 { 903 {
897 /* 904 /*
898 * When this routine is called KGDB should unregister from the 905 * When this routine is called KGDB should unregister from the
899 * panic handler and clean up, making sure it is not handling any 906 * panic handler and clean up, making sure it is not handling any
900 * break exceptions at the time. 907 * break exceptions at the time.
901 */ 908 */
902 if (kgdb_io_module_registered) { 909 if (kgdb_io_module_registered) {
903 kgdb_io_module_registered = 0; 910 kgdb_io_module_registered = 0;
904 unregister_reboot_notifier(&dbg_reboot_notifier); 911 unregister_reboot_notifier(&dbg_reboot_notifier);
905 unregister_module_notifier(&dbg_module_load_nb); 912 unregister_module_notifier(&dbg_module_load_nb);
906 atomic_notifier_chain_unregister(&panic_notifier_list, 913 atomic_notifier_chain_unregister(&panic_notifier_list,
907 &kgdb_panic_event_nb); 914 &kgdb_panic_event_nb);
908 kgdb_arch_exit(); 915 kgdb_arch_exit();
909 #ifdef CONFIG_MAGIC_SYSRQ 916 #ifdef CONFIG_MAGIC_SYSRQ
910 unregister_sysrq_key('g', &sysrq_dbg_op); 917 unregister_sysrq_key('g', &sysrq_dbg_op);
911 #endif 918 #endif
912 if (kgdb_con_registered) { 919 if (kgdb_con_registered) {
913 unregister_console(&kgdbcons); 920 unregister_console(&kgdbcons);
914 kgdb_con_registered = 0; 921 kgdb_con_registered = 0;
915 } 922 }
916 } 923 }
917 } 924 }
918 925
919 /* 926 /*
920 * There are times a tasklet needs to be used vs a compiled in 927 * There are times a tasklet needs to be used vs a compiled in
921 * break point so as to cause an exception outside a kgdb I/O module, 928 * break point so as to cause an exception outside a kgdb I/O module,
922 * such as is the case with kgdboe, where calling a breakpoint in the 929 * such as is the case with kgdboe, where calling a breakpoint in the
923 * I/O driver itself would be fatal. 930 * I/O driver itself would be fatal.
924 */ 931 */
925 static void kgdb_tasklet_bpt(unsigned long ing) 932 static void kgdb_tasklet_bpt(unsigned long ing)
926 { 933 {
927 kgdb_breakpoint(); 934 kgdb_breakpoint();
928 atomic_set(&kgdb_break_tasklet_var, 0); 935 atomic_set(&kgdb_break_tasklet_var, 0);
929 } 936 }
930 937
931 static DECLARE_TASKLET(kgdb_tasklet_breakpoint, kgdb_tasklet_bpt, 0); 938 static DECLARE_TASKLET(kgdb_tasklet_breakpoint, kgdb_tasklet_bpt, 0);
932 939
933 void kgdb_schedule_breakpoint(void) 940 void kgdb_schedule_breakpoint(void)
934 { 941 {
935 if (atomic_read(&kgdb_break_tasklet_var) || 942 if (atomic_read(&kgdb_break_tasklet_var) ||
936 atomic_read(&kgdb_active) != -1 || 943 atomic_read(&kgdb_active) != -1 ||
937 atomic_read(&kgdb_setting_breakpoint)) 944 atomic_read(&kgdb_setting_breakpoint))
938 return; 945 return;
939 atomic_inc(&kgdb_break_tasklet_var); 946 atomic_inc(&kgdb_break_tasklet_var);
940 tasklet_schedule(&kgdb_tasklet_breakpoint); 947 tasklet_schedule(&kgdb_tasklet_breakpoint);
941 } 948 }
942 EXPORT_SYMBOL_GPL(kgdb_schedule_breakpoint); 949 EXPORT_SYMBOL_GPL(kgdb_schedule_breakpoint);
943 950
944 static void kgdb_initial_breakpoint(void) 951 static void kgdb_initial_breakpoint(void)
945 { 952 {
946 kgdb_break_asap = 0; 953 kgdb_break_asap = 0;
947 954
948 printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n"); 955 pr_crit("Waiting for connection from remote gdb...\n");
949 kgdb_breakpoint(); 956 kgdb_breakpoint();
950 } 957 }
951 958
952 /** 959 /**
953 * kgdb_register_io_module - register KGDB IO module 960 * kgdb_register_io_module - register KGDB IO module
954 * @new_dbg_io_ops: the io ops vector 961 * @new_dbg_io_ops: the io ops vector
955 * 962 *
956 * Register it with the KGDB core. 963 * Register it with the KGDB core.
957 */ 964 */
958 int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops) 965 int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
959 { 966 {
960 int err; 967 int err;
961 968
962 spin_lock(&kgdb_registration_lock); 969 spin_lock(&kgdb_registration_lock);
963 970
964 if (dbg_io_ops) { 971 if (dbg_io_ops) {
965 spin_unlock(&kgdb_registration_lock); 972 spin_unlock(&kgdb_registration_lock);
966 973
967 printk(KERN_ERR "kgdb: Another I/O driver is already " 974 pr_err("Another I/O driver is already registered with KGDB\n");
968 "registered with KGDB.\n");
969 return -EBUSY; 975 return -EBUSY;
970 } 976 }
971 977
972 if (new_dbg_io_ops->init) { 978 if (new_dbg_io_ops->init) {
973 err = new_dbg_io_ops->init(); 979 err = new_dbg_io_ops->init();
974 if (err) { 980 if (err) {
975 spin_unlock(&kgdb_registration_lock); 981 spin_unlock(&kgdb_registration_lock);
976 return err; 982 return err;
977 } 983 }
978 } 984 }
979 985
980 dbg_io_ops = new_dbg_io_ops; 986 dbg_io_ops = new_dbg_io_ops;
981 987
982 spin_unlock(&kgdb_registration_lock); 988 spin_unlock(&kgdb_registration_lock);
983 989
984 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n", 990 pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
985 new_dbg_io_ops->name);
986 991
987 /* Arm KGDB now. */ 992 /* Arm KGDB now. */
988 kgdb_register_callbacks(); 993 kgdb_register_callbacks();
989 994
990 if (kgdb_break_asap) 995 if (kgdb_break_asap)
991 kgdb_initial_breakpoint(); 996 kgdb_initial_breakpoint();
992 997
993 return 0; 998 return 0;
994 } 999 }
995 EXPORT_SYMBOL_GPL(kgdb_register_io_module); 1000 EXPORT_SYMBOL_GPL(kgdb_register_io_module);
996 1001
997 /** 1002 /**
998 * kkgdb_unregister_io_module - unregister KGDB IO module 1003 * kkgdb_unregister_io_module - unregister KGDB IO module
999 * @old_dbg_io_ops: the io ops vector 1004 * @old_dbg_io_ops: the io ops vector
1000 * 1005 *
1001 * Unregister it with the KGDB core. 1006 * Unregister it with the KGDB core.
1002 */ 1007 */
1003 void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops) 1008 void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
1004 { 1009 {
1005 BUG_ON(kgdb_connected); 1010 BUG_ON(kgdb_connected);
1006 1011
1007 /* 1012 /*
1008 * KGDB is no longer able to communicate out, so 1013 * KGDB is no longer able to communicate out, so
1009 * unregister our callbacks and reset state. 1014 * unregister our callbacks and reset state.
1010 */ 1015 */
1011 kgdb_unregister_callbacks(); 1016 kgdb_unregister_callbacks();
1012 1017
1013 spin_lock(&kgdb_registration_lock); 1018 spin_lock(&kgdb_registration_lock);
1014 1019
1015 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops); 1020 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
1016 dbg_io_ops = NULL; 1021 dbg_io_ops = NULL;
1017 1022
1018 spin_unlock(&kgdb_registration_lock); 1023 spin_unlock(&kgdb_registration_lock);
1019 1024
1020 printk(KERN_INFO 1025 pr_info("Unregistered I/O driver %s, debugger disabled\n",
1021 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1022 old_dbg_io_ops->name); 1026 old_dbg_io_ops->name);
1023 } 1027 }
1024 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module); 1028 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1025 1029
1026 int dbg_io_get_char(void) 1030 int dbg_io_get_char(void)
1027 { 1031 {
1028 int ret = dbg_io_ops->read_char(); 1032 int ret = dbg_io_ops->read_char();
1029 if (ret == NO_POLL_CHAR) 1033 if (ret == NO_POLL_CHAR)
1030 return -1; 1034 return -1;
1031 if (!dbg_kdb_mode) 1035 if (!dbg_kdb_mode)
1032 return ret; 1036 return ret;
1033 if (ret == 127) 1037 if (ret == 127)
1034 return 8; 1038 return 8;
1035 return ret; 1039 return ret;
1036 } 1040 }
1037 1041
1038 /** 1042 /**
1039 * kgdb_breakpoint - generate breakpoint exception 1043 * kgdb_breakpoint - generate breakpoint exception
1040 * 1044 *
1041 * This function will generate a breakpoint exception. It is used at the 1045 * This function will generate a breakpoint exception. It is used at the
1042 * beginning of a program to sync up with a debugger and can be used 1046 * beginning of a program to sync up with a debugger and can be used
1043 * otherwise as a quick means to stop program execution and "break" into 1047 * otherwise as a quick means to stop program execution and "break" into
1044 * the debugger. 1048 * the debugger.
1045 */ 1049 */
1046 noinline void kgdb_breakpoint(void) 1050 noinline void kgdb_breakpoint(void)
1047 { 1051 {
1048 atomic_inc(&kgdb_setting_breakpoint); 1052 atomic_inc(&kgdb_setting_breakpoint);
1049 wmb(); /* Sync point before breakpoint */ 1053 wmb(); /* Sync point before breakpoint */
1050 arch_kgdb_breakpoint(); 1054 arch_kgdb_breakpoint();
1051 wmb(); /* Sync point after breakpoint */ 1055 wmb(); /* Sync point after breakpoint */
1052 atomic_dec(&kgdb_setting_breakpoint); 1056 atomic_dec(&kgdb_setting_breakpoint);
1053 } 1057 }
1054 EXPORT_SYMBOL_GPL(kgdb_breakpoint); 1058 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1055 1059
1056 static int __init opt_kgdb_wait(char *str) 1060 static int __init opt_kgdb_wait(char *str)
1057 { 1061 {
1058 kgdb_break_asap = 1; 1062 kgdb_break_asap = 1;
1059 1063
1060 kdb_init(KDB_INIT_EARLY); 1064 kdb_init(KDB_INIT_EARLY);
1061 if (kgdb_io_module_registered) 1065 if (kgdb_io_module_registered)
1062 kgdb_initial_breakpoint(); 1066 kgdb_initial_breakpoint();
1063 1067
1064 return 0; 1068 return 0;
kernel/debug/kdb/kdb_bp.c
1 /* 1 /*
2 * Kernel Debugger Architecture Independent Breakpoint Handler 2 * Kernel Debugger Architecture Independent Breakpoint Handler
3 * 3 *
4 * This file is subject to the terms and conditions of the GNU General Public 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 5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details. 6 * for more details.
7 * 7 *
8 * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved. 8 * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
9 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. 9 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
10 */ 10 */
11 11
12 #include <linux/string.h> 12 #include <linux/string.h>
13 #include <linux/kernel.h> 13 #include <linux/kernel.h>
14 #include <linux/init.h> 14 #include <linux/init.h>
15 #include <linux/kdb.h> 15 #include <linux/kdb.h>
16 #include <linux/kgdb.h> 16 #include <linux/kgdb.h>
17 #include <linux/smp.h> 17 #include <linux/smp.h>
18 #include <linux/sched.h> 18 #include <linux/sched.h>
19 #include <linux/interrupt.h> 19 #include <linux/interrupt.h>
20 #include "kdb_private.h" 20 #include "kdb_private.h"
21 21
22 /* 22 /*
23 * Table of kdb_breakpoints 23 * Table of kdb_breakpoints
24 */ 24 */
25 kdb_bp_t kdb_breakpoints[KDB_MAXBPT]; 25 kdb_bp_t kdb_breakpoints[KDB_MAXBPT];
26 26
27 static void kdb_setsinglestep(struct pt_regs *regs) 27 static void kdb_setsinglestep(struct pt_regs *regs)
28 { 28 {
29 KDB_STATE_SET(DOING_SS); 29 KDB_STATE_SET(DOING_SS);
30 } 30 }
31 31
32 static char *kdb_rwtypes[] = { 32 static char *kdb_rwtypes[] = {
33 "Instruction(i)", 33 "Instruction(i)",
34 "Instruction(Register)", 34 "Instruction(Register)",
35 "Data Write", 35 "Data Write",
36 "I/O", 36 "I/O",
37 "Data Access" 37 "Data Access"
38 }; 38 };
39 39
40 static char *kdb_bptype(kdb_bp_t *bp) 40 static char *kdb_bptype(kdb_bp_t *bp)
41 { 41 {
42 if (bp->bp_type < 0 || bp->bp_type > 4) 42 if (bp->bp_type < 0 || bp->bp_type > 4)
43 return ""; 43 return "";
44 44
45 return kdb_rwtypes[bp->bp_type]; 45 return kdb_rwtypes[bp->bp_type];
46 } 46 }
47 47
48 static int kdb_parsebp(int argc, const char **argv, int *nextargp, kdb_bp_t *bp) 48 static int kdb_parsebp(int argc, const char **argv, int *nextargp, kdb_bp_t *bp)
49 { 49 {
50 int nextarg = *nextargp; 50 int nextarg = *nextargp;
51 int diag; 51 int diag;
52 52
53 bp->bph_length = 1; 53 bp->bph_length = 1;
54 if ((argc + 1) != nextarg) { 54 if ((argc + 1) != nextarg) {
55 if (strncasecmp(argv[nextarg], "datar", sizeof("datar")) == 0) 55 if (strncasecmp(argv[nextarg], "datar", sizeof("datar")) == 0)
56 bp->bp_type = BP_ACCESS_WATCHPOINT; 56 bp->bp_type = BP_ACCESS_WATCHPOINT;
57 else if (strncasecmp(argv[nextarg], "dataw", sizeof("dataw")) == 0) 57 else if (strncasecmp(argv[nextarg], "dataw", sizeof("dataw")) == 0)
58 bp->bp_type = BP_WRITE_WATCHPOINT; 58 bp->bp_type = BP_WRITE_WATCHPOINT;
59 else if (strncasecmp(argv[nextarg], "inst", sizeof("inst")) == 0) 59 else if (strncasecmp(argv[nextarg], "inst", sizeof("inst")) == 0)
60 bp->bp_type = BP_HARDWARE_BREAKPOINT; 60 bp->bp_type = BP_HARDWARE_BREAKPOINT;
61 else 61 else
62 return KDB_ARGCOUNT; 62 return KDB_ARGCOUNT;
63 63
64 bp->bph_length = 1; 64 bp->bph_length = 1;
65 65
66 nextarg++; 66 nextarg++;
67 67
68 if ((argc + 1) != nextarg) { 68 if ((argc + 1) != nextarg) {
69 unsigned long len; 69 unsigned long len;
70 70
71 diag = kdbgetularg((char *)argv[nextarg], 71 diag = kdbgetularg((char *)argv[nextarg],
72 &len); 72 &len);
73 if (diag) 73 if (diag)
74 return diag; 74 return diag;
75 75
76 76
77 if (len > 8) 77 if (len > 8)
78 return KDB_BADLENGTH; 78 return KDB_BADLENGTH;
79 79
80 bp->bph_length = len; 80 bp->bph_length = len;
81 nextarg++; 81 nextarg++;
82 } 82 }
83 83
84 if ((argc + 1) != nextarg) 84 if ((argc + 1) != nextarg)
85 return KDB_ARGCOUNT; 85 return KDB_ARGCOUNT;
86 } 86 }
87 87
88 *nextargp = nextarg; 88 *nextargp = nextarg;
89 return 0; 89 return 0;
90 } 90 }
91 91
92 static int _kdb_bp_remove(kdb_bp_t *bp) 92 static int _kdb_bp_remove(kdb_bp_t *bp)
93 { 93 {
94 int ret = 1; 94 int ret = 1;
95 if (!bp->bp_installed) 95 if (!bp->bp_installed)
96 return ret; 96 return ret;
97 if (!bp->bp_type) 97 if (!bp->bp_type)
98 ret = dbg_remove_sw_break(bp->bp_addr); 98 ret = dbg_remove_sw_break(bp->bp_addr);
99 else 99 else
100 ret = arch_kgdb_ops.remove_hw_breakpoint(bp->bp_addr, 100 ret = arch_kgdb_ops.remove_hw_breakpoint(bp->bp_addr,
101 bp->bph_length, 101 bp->bph_length,
102 bp->bp_type); 102 bp->bp_type);
103 if (ret == 0) 103 if (ret == 0)
104 bp->bp_installed = 0; 104 bp->bp_installed = 0;
105 return ret; 105 return ret;
106 } 106 }
107 107
108 static void kdb_handle_bp(struct pt_regs *regs, kdb_bp_t *bp) 108 static void kdb_handle_bp(struct pt_regs *regs, kdb_bp_t *bp)
109 { 109 {
110 if (KDB_DEBUG(BP)) 110 if (KDB_DEBUG(BP))
111 kdb_printf("regs->ip = 0x%lx\n", instruction_pointer(regs)); 111 kdb_printf("regs->ip = 0x%lx\n", instruction_pointer(regs));
112 112
113 /* 113 /*
114 * Setup single step 114 * Setup single step
115 */ 115 */
116 kdb_setsinglestep(regs); 116 kdb_setsinglestep(regs);
117 117
118 /* 118 /*
119 * Reset delay attribute 119 * Reset delay attribute
120 */ 120 */
121 bp->bp_delay = 0; 121 bp->bp_delay = 0;
122 bp->bp_delayed = 1; 122 bp->bp_delayed = 1;
123 } 123 }
124 124
125 static int _kdb_bp_install(struct pt_regs *regs, kdb_bp_t *bp) 125 static int _kdb_bp_install(struct pt_regs *regs, kdb_bp_t *bp)
126 { 126 {
127 int ret; 127 int ret;
128 /* 128 /*
129 * Install the breakpoint, if it is not already installed. 129 * Install the breakpoint, if it is not already installed.
130 */ 130 */
131 131
132 if (KDB_DEBUG(BP)) 132 if (KDB_DEBUG(BP))
133 kdb_printf("%s: bp_installed %d\n", 133 kdb_printf("%s: bp_installed %d\n",
134 __func__, bp->bp_installed); 134 __func__, bp->bp_installed);
135 if (!KDB_STATE(SSBPT)) 135 if (!KDB_STATE(SSBPT))
136 bp->bp_delay = 0; 136 bp->bp_delay = 0;
137 if (bp->bp_installed) 137 if (bp->bp_installed)
138 return 1; 138 return 1;
139 if (bp->bp_delay || (bp->bp_delayed && KDB_STATE(DOING_SS))) { 139 if (bp->bp_delay || (bp->bp_delayed && KDB_STATE(DOING_SS))) {
140 if (KDB_DEBUG(BP)) 140 if (KDB_DEBUG(BP))
141 kdb_printf("%s: delayed bp\n", __func__); 141 kdb_printf("%s: delayed bp\n", __func__);
142 kdb_handle_bp(regs, bp); 142 kdb_handle_bp(regs, bp);
143 return 0; 143 return 0;
144 } 144 }
145 if (!bp->bp_type) 145 if (!bp->bp_type)
146 ret = dbg_set_sw_break(bp->bp_addr); 146 ret = dbg_set_sw_break(bp->bp_addr);
147 else 147 else
148 ret = arch_kgdb_ops.set_hw_breakpoint(bp->bp_addr, 148 ret = arch_kgdb_ops.set_hw_breakpoint(bp->bp_addr,
149 bp->bph_length, 149 bp->bph_length,
150 bp->bp_type); 150 bp->bp_type);
151 if (ret == 0) { 151 if (ret == 0) {
152 bp->bp_installed = 1; 152 bp->bp_installed = 1;
153 } else { 153 } else {
154 kdb_printf("%s: failed to set breakpoint at 0x%lx\n", 154 kdb_printf("%s: failed to set breakpoint at 0x%lx\n",
155 __func__, bp->bp_addr); 155 __func__, bp->bp_addr);
156 #ifdef CONFIG_DEBUG_RODATA 156 #ifdef CONFIG_DEBUG_RODATA
157 if (!bp->bp_type) { 157 if (!bp->bp_type) {
158 kdb_printf("Software breakpoints are unavailable.\n" 158 kdb_printf("Software breakpoints are unavailable.\n"
159 " Change the kernel CONFIG_DEBUG_RODATA=n\n" 159 " Change the kernel CONFIG_DEBUG_RODATA=n\n"
160 " OR use hw breaks: help bph\n"); 160 " OR use hw breaks: help bph\n");
161 } 161 }
162 #endif 162 #endif
163 return 1; 163 return 1;
164 } 164 }
165 return 0; 165 return 0;
166 } 166 }
167 167
168 /* 168 /*
169 * kdb_bp_install 169 * kdb_bp_install
170 * 170 *
171 * Install kdb_breakpoints prior to returning from the 171 * Install kdb_breakpoints prior to returning from the
172 * kernel debugger. This allows the kdb_breakpoints to be set 172 * kernel debugger. This allows the kdb_breakpoints to be set
173 * upon functions that are used internally by kdb, such as 173 * upon functions that are used internally by kdb, such as
174 * printk(). This function is only called once per kdb session. 174 * printk(). This function is only called once per kdb session.
175 */ 175 */
176 void kdb_bp_install(struct pt_regs *regs) 176 void kdb_bp_install(struct pt_regs *regs)
177 { 177 {
178 int i; 178 int i;
179 179
180 for (i = 0; i < KDB_MAXBPT; i++) { 180 for (i = 0; i < KDB_MAXBPT; i++) {
181 kdb_bp_t *bp = &kdb_breakpoints[i]; 181 kdb_bp_t *bp = &kdb_breakpoints[i];
182 182
183 if (KDB_DEBUG(BP)) { 183 if (KDB_DEBUG(BP)) {
184 kdb_printf("%s: bp %d bp_enabled %d\n", 184 kdb_printf("%s: bp %d bp_enabled %d\n",
185 __func__, i, bp->bp_enabled); 185 __func__, i, bp->bp_enabled);
186 } 186 }
187 if (bp->bp_enabled) 187 if (bp->bp_enabled)
188 _kdb_bp_install(regs, bp); 188 _kdb_bp_install(regs, bp);
189 } 189 }
190 } 190 }
191 191
192 /* 192 /*
193 * kdb_bp_remove 193 * kdb_bp_remove
194 * 194 *
195 * Remove kdb_breakpoints upon entry to the kernel debugger. 195 * Remove kdb_breakpoints upon entry to the kernel debugger.
196 * 196 *
197 * Parameters: 197 * Parameters:
198 * None. 198 * None.
199 * Outputs: 199 * Outputs:
200 * None. 200 * None.
201 * Returns: 201 * Returns:
202 * None. 202 * None.
203 * Locking: 203 * Locking:
204 * None. 204 * None.
205 * Remarks: 205 * Remarks:
206 */ 206 */
207 void kdb_bp_remove(void) 207 void kdb_bp_remove(void)
208 { 208 {
209 int i; 209 int i;
210 210
211 for (i = KDB_MAXBPT - 1; i >= 0; i--) { 211 for (i = KDB_MAXBPT - 1; i >= 0; i--) {
212 kdb_bp_t *bp = &kdb_breakpoints[i]; 212 kdb_bp_t *bp = &kdb_breakpoints[i];
213 213
214 if (KDB_DEBUG(BP)) { 214 if (KDB_DEBUG(BP)) {
215 kdb_printf("%s: bp %d bp_enabled %d\n", 215 kdb_printf("%s: bp %d bp_enabled %d\n",
216 __func__, i, bp->bp_enabled); 216 __func__, i, bp->bp_enabled);
217 } 217 }
218 if (bp->bp_enabled) 218 if (bp->bp_enabled)
219 _kdb_bp_remove(bp); 219 _kdb_bp_remove(bp);
220 } 220 }
221 } 221 }
222 222
223 223
224 /* 224 /*
225 * kdb_printbp 225 * kdb_printbp
226 * 226 *
227 * Internal function to format and print a breakpoint entry. 227 * Internal function to format and print a breakpoint entry.
228 * 228 *
229 * Parameters: 229 * Parameters:
230 * None. 230 * None.
231 * Outputs: 231 * Outputs:
232 * None. 232 * None.
233 * Returns: 233 * Returns:
234 * None. 234 * None.
235 * Locking: 235 * Locking:
236 * None. 236 * None.
237 * Remarks: 237 * Remarks:
238 */ 238 */
239 239
240 static void kdb_printbp(kdb_bp_t *bp, int i) 240 static void kdb_printbp(kdb_bp_t *bp, int i)
241 { 241 {
242 kdb_printf("%s ", kdb_bptype(bp)); 242 kdb_printf("%s ", kdb_bptype(bp));
243 kdb_printf("BP #%d at ", i); 243 kdb_printf("BP #%d at ", i);
244 kdb_symbol_print(bp->bp_addr, NULL, KDB_SP_DEFAULT); 244 kdb_symbol_print(bp->bp_addr, NULL, KDB_SP_DEFAULT);
245 245
246 if (bp->bp_enabled) 246 if (bp->bp_enabled)
247 kdb_printf("\n is enabled"); 247 kdb_printf("\n is enabled");
248 else 248 else
249 kdb_printf("\n is disabled"); 249 kdb_printf("\n is disabled");
250 250
251 kdb_printf("\taddr at %016lx, hardtype=%d installed=%d\n", 251 kdb_printf("\taddr at %016lx, hardtype=%d installed=%d\n",
252 bp->bp_addr, bp->bp_type, bp->bp_installed); 252 bp->bp_addr, bp->bp_type, bp->bp_installed);
253 253
254 kdb_printf("\n"); 254 kdb_printf("\n");
255 } 255 }
256 256
257 /* 257 /*
258 * kdb_bp 258 * kdb_bp
259 * 259 *
260 * Handle the bp commands. 260 * Handle the bp commands.
261 * 261 *
262 * [bp|bph] <addr-expression> [DATAR|DATAW] 262 * [bp|bph] <addr-expression> [DATAR|DATAW]
263 * 263 *
264 * Parameters: 264 * Parameters:
265 * argc Count of arguments in argv 265 * argc Count of arguments in argv
266 * argv Space delimited command line arguments 266 * argv Space delimited command line arguments
267 * Outputs: 267 * Outputs:
268 * None. 268 * None.
269 * Returns: 269 * Returns:
270 * Zero for success, a kdb diagnostic if failure. 270 * Zero for success, a kdb diagnostic if failure.
271 * Locking: 271 * Locking:
272 * None. 272 * None.
273 * Remarks: 273 * Remarks:
274 * 274 *
275 * bp Set breakpoint on all cpus. Only use hardware assist if need. 275 * bp Set breakpoint on all cpus. Only use hardware assist if need.
276 * bph Set breakpoint on all cpus. Force hardware register 276 * bph Set breakpoint on all cpus. Force hardware register
277 */ 277 */
278 278
279 static int kdb_bp(int argc, const char **argv) 279 static int kdb_bp(int argc, const char **argv)
280 { 280 {
281 int i, bpno; 281 int i, bpno;
282 kdb_bp_t *bp, *bp_check; 282 kdb_bp_t *bp, *bp_check;
283 int diag; 283 int diag;
284 char *symname = NULL; 284 char *symname = NULL;
285 long offset = 0ul; 285 long offset = 0ul;
286 int nextarg; 286 int nextarg;
287 kdb_bp_t template = {0}; 287 kdb_bp_t template = {0};
288 288
289 if (argc == 0) { 289 if (argc == 0) {
290 /* 290 /*
291 * Display breakpoint table 291 * Display breakpoint table
292 */ 292 */
293 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; 293 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT;
294 bpno++, bp++) { 294 bpno++, bp++) {
295 if (bp->bp_free) 295 if (bp->bp_free)
296 continue; 296 continue;
297 kdb_printbp(bp, bpno); 297 kdb_printbp(bp, bpno);
298 } 298 }
299 299
300 return 0; 300 return 0;
301 } 301 }
302 302
303 nextarg = 1; 303 nextarg = 1;
304 diag = kdbgetaddrarg(argc, argv, &nextarg, &template.bp_addr, 304 diag = kdbgetaddrarg(argc, argv, &nextarg, &template.bp_addr,
305 &offset, &symname); 305 &offset, &symname);
306 if (diag) 306 if (diag)
307 return diag; 307 return diag;
308 if (!template.bp_addr) 308 if (!template.bp_addr)
309 return KDB_BADINT; 309 return KDB_BADINT;
310 310
311 /* 311 /*
312 * Find an empty bp structure to allocate 312 * Find an empty bp structure to allocate
313 */ 313 */
314 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) { 314 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) {
315 if (bp->bp_free) 315 if (bp->bp_free)
316 break; 316 break;
317 } 317 }
318 318
319 if (bpno == KDB_MAXBPT) 319 if (bpno == KDB_MAXBPT)
320 return KDB_TOOMANYBPT; 320 return KDB_TOOMANYBPT;
321 321
322 if (strcmp(argv[0], "bph") == 0) { 322 if (strcmp(argv[0], "bph") == 0) {
323 template.bp_type = BP_HARDWARE_BREAKPOINT; 323 template.bp_type = BP_HARDWARE_BREAKPOINT;
324 diag = kdb_parsebp(argc, argv, &nextarg, &template); 324 diag = kdb_parsebp(argc, argv, &nextarg, &template);
325 if (diag) 325 if (diag)
326 return diag; 326 return diag;
327 } else { 327 } else {
328 template.bp_type = BP_BREAKPOINT; 328 template.bp_type = BP_BREAKPOINT;
329 } 329 }
330 330
331 /* 331 /*
332 * Check for clashing breakpoints. 332 * Check for clashing breakpoints.
333 * 333 *
334 * Note, in this design we can't have hardware breakpoints 334 * Note, in this design we can't have hardware breakpoints
335 * enabled for both read and write on the same address. 335 * enabled for both read and write on the same address.
336 */ 336 */
337 for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT; 337 for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT;
338 i++, bp_check++) { 338 i++, bp_check++) {
339 if (!bp_check->bp_free && 339 if (!bp_check->bp_free &&
340 bp_check->bp_addr == template.bp_addr) { 340 bp_check->bp_addr == template.bp_addr) {
341 kdb_printf("You already have a breakpoint at " 341 kdb_printf("You already have a breakpoint at "
342 kdb_bfd_vma_fmt0 "\n", template.bp_addr); 342 kdb_bfd_vma_fmt0 "\n", template.bp_addr);
343 return KDB_DUPBPT; 343 return KDB_DUPBPT;
344 } 344 }
345 } 345 }
346 346
347 template.bp_enabled = 1; 347 template.bp_enabled = 1;
348 348
349 /* 349 /*
350 * Actually allocate the breakpoint found earlier 350 * Actually allocate the breakpoint found earlier
351 */ 351 */
352 *bp = template; 352 *bp = template;
353 bp->bp_free = 0; 353 bp->bp_free = 0;
354 354
355 kdb_printbp(bp, bpno); 355 kdb_printbp(bp, bpno);
356 356
357 return 0; 357 return 0;
358 } 358 }
359 359
360 /* 360 /*
361 * kdb_bc 361 * kdb_bc
362 * 362 *
363 * Handles the 'bc', 'be', and 'bd' commands 363 * Handles the 'bc', 'be', and 'bd' commands
364 * 364 *
365 * [bd|bc|be] <breakpoint-number> 365 * [bd|bc|be] <breakpoint-number>
366 * [bd|bc|be] * 366 * [bd|bc|be] *
367 * 367 *
368 * Parameters: 368 * Parameters:
369 * argc Count of arguments in argv 369 * argc Count of arguments in argv
370 * argv Space delimited command line arguments 370 * argv Space delimited command line arguments
371 * Outputs: 371 * Outputs:
372 * None. 372 * None.
373 * Returns: 373 * Returns:
374 * Zero for success, a kdb diagnostic for failure 374 * Zero for success, a kdb diagnostic for failure
375 * Locking: 375 * Locking:
376 * None. 376 * None.
377 * Remarks: 377 * Remarks:
378 */ 378 */
379 static int kdb_bc(int argc, const char **argv) 379 static int kdb_bc(int argc, const char **argv)
380 { 380 {
381 unsigned long addr; 381 unsigned long addr;
382 kdb_bp_t *bp = NULL; 382 kdb_bp_t *bp = NULL;
383 int lowbp = KDB_MAXBPT; 383 int lowbp = KDB_MAXBPT;
384 int highbp = 0; 384 int highbp = 0;
385 int done = 0; 385 int done = 0;
386 int i; 386 int i;
387 int diag = 0; 387 int diag = 0;
388 388
389 int cmd; /* KDBCMD_B? */ 389 int cmd; /* KDBCMD_B? */
390 #define KDBCMD_BC 0 390 #define KDBCMD_BC 0
391 #define KDBCMD_BE 1 391 #define KDBCMD_BE 1
392 #define KDBCMD_BD 2 392 #define KDBCMD_BD 2
393 393
394 if (strcmp(argv[0], "be") == 0) 394 if (strcmp(argv[0], "be") == 0)
395 cmd = KDBCMD_BE; 395 cmd = KDBCMD_BE;
396 else if (strcmp(argv[0], "bd") == 0) 396 else if (strcmp(argv[0], "bd") == 0)
397 cmd = KDBCMD_BD; 397 cmd = KDBCMD_BD;
398 else 398 else
399 cmd = KDBCMD_BC; 399 cmd = KDBCMD_BC;
400 400
401 if (argc != 1) 401 if (argc != 1)
402 return KDB_ARGCOUNT; 402 return KDB_ARGCOUNT;
403 403
404 if (strcmp(argv[1], "*") == 0) { 404 if (strcmp(argv[1], "*") == 0) {
405 lowbp = 0; 405 lowbp = 0;
406 highbp = KDB_MAXBPT; 406 highbp = KDB_MAXBPT;
407 } else { 407 } else {
408 diag = kdbgetularg(argv[1], &addr); 408 diag = kdbgetularg(argv[1], &addr);
409 if (diag) 409 if (diag)
410 return diag; 410 return diag;
411 411
412 /* 412 /*
413 * For addresses less than the maximum breakpoint number, 413 * For addresses less than the maximum breakpoint number,
414 * assume that the breakpoint number is desired. 414 * assume that the breakpoint number is desired.
415 */ 415 */
416 if (addr < KDB_MAXBPT) { 416 if (addr < KDB_MAXBPT) {
417 bp = &kdb_breakpoints[addr]; 417 bp = &kdb_breakpoints[addr];
418 lowbp = highbp = addr; 418 lowbp = highbp = addr;
419 highbp++; 419 highbp++;
420 } else { 420 } else {
421 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; 421 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT;
422 i++, bp++) { 422 i++, bp++) {
423 if (bp->bp_addr == addr) { 423 if (bp->bp_addr == addr) {
424 lowbp = highbp = i; 424 lowbp = highbp = i;
425 highbp++; 425 highbp++;
426 break; 426 break;
427 } 427 }
428 } 428 }
429 } 429 }
430 } 430 }
431 431
432 /* 432 /*
433 * Now operate on the set of breakpoints matching the input 433 * Now operate on the set of breakpoints matching the input
434 * criteria (either '*' for all, or an individual breakpoint). 434 * criteria (either '*' for all, or an individual breakpoint).
435 */ 435 */
436 for (bp = &kdb_breakpoints[lowbp], i = lowbp; 436 for (bp = &kdb_breakpoints[lowbp], i = lowbp;
437 i < highbp; 437 i < highbp;
438 i++, bp++) { 438 i++, bp++) {
439 if (bp->bp_free) 439 if (bp->bp_free)
440 continue; 440 continue;
441 441
442 done++; 442 done++;
443 443
444 switch (cmd) { 444 switch (cmd) {
445 case KDBCMD_BC: 445 case KDBCMD_BC:
446 bp->bp_enabled = 0; 446 bp->bp_enabled = 0;
447 447
448 kdb_printf("Breakpoint %d at " 448 kdb_printf("Breakpoint %d at "
449 kdb_bfd_vma_fmt " cleared\n", 449 kdb_bfd_vma_fmt " cleared\n",
450 i, bp->bp_addr); 450 i, bp->bp_addr);
451 451
452 bp->bp_addr = 0; 452 bp->bp_addr = 0;
453 bp->bp_free = 1; 453 bp->bp_free = 1;
454 454
455 break; 455 break;
456 case KDBCMD_BE: 456 case KDBCMD_BE:
457 bp->bp_enabled = 1; 457 bp->bp_enabled = 1;
458 458
459 kdb_printf("Breakpoint %d at " 459 kdb_printf("Breakpoint %d at "
460 kdb_bfd_vma_fmt " enabled", 460 kdb_bfd_vma_fmt " enabled",
461 i, bp->bp_addr); 461 i, bp->bp_addr);
462 462
463 kdb_printf("\n"); 463 kdb_printf("\n");
464 break; 464 break;
465 case KDBCMD_BD: 465 case KDBCMD_BD:
466 if (!bp->bp_enabled) 466 if (!bp->bp_enabled)
467 break; 467 break;
468 468
469 bp->bp_enabled = 0; 469 bp->bp_enabled = 0;
470 470
471 kdb_printf("Breakpoint %d at " 471 kdb_printf("Breakpoint %d at "
472 kdb_bfd_vma_fmt " disabled\n", 472 kdb_bfd_vma_fmt " disabled\n",
473 i, bp->bp_addr); 473 i, bp->bp_addr);
474 474
475 break; 475 break;
476 } 476 }
477 if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) { 477 if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
478 bp->bp_delay = 0; 478 bp->bp_delay = 0;
479 KDB_STATE_CLEAR(SSBPT); 479 KDB_STATE_CLEAR(SSBPT);
480 } 480 }
481 } 481 }
482 482
483 return (!done) ? KDB_BPTNOTFOUND : 0; 483 return (!done) ? KDB_BPTNOTFOUND : 0;
484 } 484 }
485 485
486 /* 486 /*
487 * kdb_ss 487 * kdb_ss
488 * 488 *
489 * Process the 'ss' (Single Step) command. 489 * Process the 'ss' (Single Step) command.
490 * 490 *
491 * ss 491 * ss
492 * 492 *
493 * Parameters: 493 * Parameters:
494 * argc Argument count 494 * argc Argument count
495 * argv Argument vector 495 * argv Argument vector
496 * Outputs: 496 * Outputs:
497 * None. 497 * None.
498 * Returns: 498 * Returns:
499 * KDB_CMD_SS for success, a kdb error if failure. 499 * KDB_CMD_SS for success, a kdb error if failure.
500 * Locking: 500 * Locking:
501 * None. 501 * None.
502 * Remarks: 502 * Remarks:
503 * 503 *
504 * Set the arch specific option to trigger a debug trap after the next 504 * Set the arch specific option to trigger a debug trap after the next
505 * instruction. 505 * instruction.
506 */ 506 */
507 507
508 static int kdb_ss(int argc, const char **argv) 508 static int kdb_ss(int argc, const char **argv)
509 { 509 {
510 if (argc != 0) 510 if (argc != 0)
511 return KDB_ARGCOUNT; 511 return KDB_ARGCOUNT;
512 /* 512 /*
513 * Set trace flag and go. 513 * Set trace flag and go.
514 */ 514 */
515 KDB_STATE_SET(DOING_SS); 515 KDB_STATE_SET(DOING_SS);
516 return KDB_CMD_SS; 516 return KDB_CMD_SS;
517 } 517 }
518 518
519 /* Initialize the breakpoint table and register breakpoint commands. */ 519 /* Initialize the breakpoint table and register breakpoint commands. */
520 520
521 void __init kdb_initbptab(void) 521 void __init kdb_initbptab(void)
522 { 522 {
523 int i; 523 int i;
524 kdb_bp_t *bp; 524 kdb_bp_t *bp;
525 525
526 /* 526 /*
527 * First time initialization. 527 * First time initialization.
528 */ 528 */
529 memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints)); 529 memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
530 530
531 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) 531 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++)
532 bp->bp_free = 1; 532 bp->bp_free = 1;
533 533
534 kdb_register_repeat("bp", kdb_bp, "[<vaddr>]", 534 kdb_register_flags("bp", kdb_bp, "[<vaddr>]",
535 "Set/Display breakpoints", 0, KDB_REPEAT_NO_ARGS); 535 "Set/Display breakpoints", 0,
536 kdb_register_repeat("bl", kdb_bp, "[<vaddr>]", 536 KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
537 "Display breakpoints", 0, KDB_REPEAT_NO_ARGS); 537 kdb_register_flags("bl", kdb_bp, "[<vaddr>]",
538 "Display breakpoints", 0,
539 KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
538 if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT) 540 if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)
539 kdb_register_repeat("bph", kdb_bp, "[<vaddr>]", 541 kdb_register_flags("bph", kdb_bp, "[<vaddr>]",
540 "[datar [length]|dataw [length]] Set hw brk", 0, KDB_REPEAT_NO_ARGS); 542 "[datar [length]|dataw [length]] Set hw brk", 0,
541 kdb_register_repeat("bc", kdb_bc, "<bpnum>", 543 KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
542 "Clear Breakpoint", 0, KDB_REPEAT_NONE); 544 kdb_register_flags("bc", kdb_bc, "<bpnum>",
543 kdb_register_repeat("be", kdb_bc, "<bpnum>", 545 "Clear Breakpoint", 0,
544 "Enable Breakpoint", 0, KDB_REPEAT_NONE); 546 KDB_ENABLE_FLOW_CTRL);
545 kdb_register_repeat("bd", kdb_bc, "<bpnum>", 547 kdb_register_flags("be", kdb_bc, "<bpnum>",
546 "Disable Breakpoint", 0, KDB_REPEAT_NONE); 548 "Enable Breakpoint", 0,
549 KDB_ENABLE_FLOW_CTRL);
550 kdb_register_flags("bd", kdb_bc, "<bpnum>",
551 "Disable Breakpoint", 0,
552 KDB_ENABLE_FLOW_CTRL);
547 553
548 kdb_register_repeat("ss", kdb_ss, "", 554 kdb_register_flags("ss", kdb_ss, "",
549 "Single Step", 1, KDB_REPEAT_NO_ARGS); 555 "Single Step", 1,
556 KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
550 /* 557 /*
551 * Architecture dependent initialization. 558 * Architecture dependent initialization.
552 */ 559 */
553 } 560 }
554 561
kernel/debug/kdb/kdb_debugger.c
1 /* 1 /*
2 * Created by: Jason Wessel <jason.wessel@windriver.com> 2 * Created by: Jason Wessel <jason.wessel@windriver.com>
3 * 3 *
4 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. 4 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
5 * 5 *
6 * This file is licensed under the terms of the GNU General Public 6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any 7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied. 8 * warranty of any kind, whether express or implied.
9 */ 9 */
10 10
11 #include <linux/kgdb.h> 11 #include <linux/kgdb.h>
12 #include <linux/kdb.h> 12 #include <linux/kdb.h>
13 #include <linux/kdebug.h> 13 #include <linux/kdebug.h>
14 #include <linux/export.h> 14 #include <linux/export.h>
15 #include <linux/hardirq.h> 15 #include <linux/hardirq.h>
16 #include "kdb_private.h" 16 #include "kdb_private.h"
17 #include "../debug_core.h" 17 #include "../debug_core.h"
18 18
19 /* 19 /*
20 * KDB interface to KGDB internals 20 * KDB interface to KGDB internals
21 */ 21 */
22 get_char_func kdb_poll_funcs[] = { 22 get_char_func kdb_poll_funcs[] = {
23 dbg_io_get_char, 23 dbg_io_get_char,
24 NULL, 24 NULL,
25 NULL, 25 NULL,
26 NULL, 26 NULL,
27 NULL, 27 NULL,
28 NULL, 28 NULL,
29 }; 29 };
30 EXPORT_SYMBOL_GPL(kdb_poll_funcs); 30 EXPORT_SYMBOL_GPL(kdb_poll_funcs);
31 31
32 int kdb_poll_idx = 1; 32 int kdb_poll_idx = 1;
33 EXPORT_SYMBOL_GPL(kdb_poll_idx); 33 EXPORT_SYMBOL_GPL(kdb_poll_idx);
34 34
35 static struct kgdb_state *kdb_ks; 35 static struct kgdb_state *kdb_ks;
36 36
37 int kdb_common_init_state(struct kgdb_state *ks) 37 int kdb_common_init_state(struct kgdb_state *ks)
38 { 38 {
39 kdb_initial_cpu = atomic_read(&kgdb_active); 39 kdb_initial_cpu = atomic_read(&kgdb_active);
40 kdb_current_task = kgdb_info[ks->cpu].task; 40 kdb_current_task = kgdb_info[ks->cpu].task;
41 kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo; 41 kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo;
42 return 0; 42 return 0;
43 } 43 }
44 44
45 int kdb_common_deinit_state(void) 45 int kdb_common_deinit_state(void)
46 { 46 {
47 kdb_initial_cpu = -1; 47 kdb_initial_cpu = -1;
48 kdb_current_task = NULL; 48 kdb_current_task = NULL;
49 kdb_current_regs = NULL; 49 kdb_current_regs = NULL;
50 return 0; 50 return 0;
51 } 51 }
52 52
53 int kdb_stub(struct kgdb_state *ks) 53 int kdb_stub(struct kgdb_state *ks)
54 { 54 {
55 int error = 0; 55 int error = 0;
56 kdb_bp_t *bp; 56 kdb_bp_t *bp;
57 unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs); 57 unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
58 kdb_reason_t reason = KDB_REASON_OOPS; 58 kdb_reason_t reason = KDB_REASON_OOPS;
59 kdb_dbtrap_t db_result = KDB_DB_NOBPT; 59 kdb_dbtrap_t db_result = KDB_DB_NOBPT;
60 int i; 60 int i;
61 61
62 kdb_ks = ks; 62 kdb_ks = ks;
63 if (KDB_STATE(REENTRY)) { 63 if (KDB_STATE(REENTRY)) {
64 reason = KDB_REASON_SWITCH; 64 reason = KDB_REASON_SWITCH;
65 KDB_STATE_CLEAR(REENTRY); 65 KDB_STATE_CLEAR(REENTRY);
66 addr = instruction_pointer(ks->linux_regs); 66 addr = instruction_pointer(ks->linux_regs);
67 } 67 }
68 ks->pass_exception = 0; 68 ks->pass_exception = 0;
69 if (atomic_read(&kgdb_setting_breakpoint)) 69 if (atomic_read(&kgdb_setting_breakpoint))
70 reason = KDB_REASON_KEYBOARD; 70 reason = KDB_REASON_KEYBOARD;
71 71
72 if (ks->err_code == KDB_REASON_SYSTEM_NMI && ks->signo == SIGTRAP) 72 if (ks->err_code == KDB_REASON_SYSTEM_NMI && ks->signo == SIGTRAP)
73 reason = KDB_REASON_SYSTEM_NMI; 73 reason = KDB_REASON_SYSTEM_NMI;
74 74
75 else if (in_nmi()) 75 else if (in_nmi())
76 reason = KDB_REASON_NMI; 76 reason = KDB_REASON_NMI;
77 77
78 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) { 78 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
79 if ((bp->bp_enabled) && (bp->bp_addr == addr)) { 79 if ((bp->bp_enabled) && (bp->bp_addr == addr)) {
80 reason = KDB_REASON_BREAK; 80 reason = KDB_REASON_BREAK;
81 db_result = KDB_DB_BPT; 81 db_result = KDB_DB_BPT;
82 if (addr != instruction_pointer(ks->linux_regs)) 82 if (addr != instruction_pointer(ks->linux_regs))
83 kgdb_arch_set_pc(ks->linux_regs, addr); 83 kgdb_arch_set_pc(ks->linux_regs, addr);
84 break; 84 break;
85 } 85 }
86 } 86 }
87 if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) { 87 if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) {
88 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) { 88 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
89 if (bp->bp_free) 89 if (bp->bp_free)
90 continue; 90 continue;
91 if (bp->bp_addr == addr) { 91 if (bp->bp_addr == addr) {
92 bp->bp_delay = 1; 92 bp->bp_delay = 1;
93 bp->bp_delayed = 1; 93 bp->bp_delayed = 1;
94 /* 94 /*
95 * SSBPT is set when the kernel debugger must single step a 95 * SSBPT is set when the kernel debugger must single step a
96 * task in order to re-establish an instruction breakpoint 96 * task in order to re-establish an instruction breakpoint
97 * which uses the instruction replacement mechanism. It is 97 * which uses the instruction replacement mechanism. It is
98 * cleared by any action that removes the need to single-step 98 * cleared by any action that removes the need to single-step
99 * the breakpoint. 99 * the breakpoint.
100 */ 100 */
101 reason = KDB_REASON_BREAK; 101 reason = KDB_REASON_BREAK;
102 db_result = KDB_DB_BPT; 102 db_result = KDB_DB_BPT;
103 KDB_STATE_SET(SSBPT); 103 KDB_STATE_SET(SSBPT);
104 break; 104 break;
105 } 105 }
106 } 106 }
107 } 107 }
108 108
109 if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 && 109 if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 &&
110 ks->signo == SIGTRAP) { 110 ks->signo == SIGTRAP) {
111 reason = KDB_REASON_SSTEP; 111 reason = KDB_REASON_SSTEP;
112 db_result = KDB_DB_BPT; 112 db_result = KDB_DB_BPT;
113 } 113 }
114 /* Set initial kdb state variables */ 114 /* Set initial kdb state variables */
115 KDB_STATE_CLEAR(KGDB_TRANS); 115 KDB_STATE_CLEAR(KGDB_TRANS);
116 kdb_common_init_state(ks); 116 kdb_common_init_state(ks);
117 /* Remove any breakpoints as needed by kdb and clear single step */ 117 /* Remove any breakpoints as needed by kdb and clear single step */
118 kdb_bp_remove(); 118 kdb_bp_remove();
119 KDB_STATE_CLEAR(DOING_SS); 119 KDB_STATE_CLEAR(DOING_SS);
120 KDB_STATE_SET(PAGER); 120 KDB_STATE_SET(PAGER);
121 /* zero out any offline cpu data */ 121 /* zero out any offline cpu data */
122 for_each_present_cpu(i) { 122 for_each_present_cpu(i) {
123 if (!cpu_online(i)) { 123 if (!cpu_online(i)) {
124 kgdb_info[i].debuggerinfo = NULL; 124 kgdb_info[i].debuggerinfo = NULL;
125 kgdb_info[i].task = NULL; 125 kgdb_info[i].task = NULL;
126 } 126 }
127 } 127 }
128 if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) { 128 if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) {
129 ks->pass_exception = 1; 129 ks->pass_exception = 1;
130 KDB_FLAG_SET(CATASTROPHIC); 130 KDB_FLAG_SET(CATASTROPHIC);
131 } 131 }
132 /* set CATASTROPHIC if the system contains unresponsive processors */
133 for_each_online_cpu(i)
134 if (!kgdb_info[i].enter_kgdb)
135 KDB_FLAG_SET(CATASTROPHIC);
132 if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) { 136 if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) {
133 KDB_STATE_CLEAR(SSBPT); 137 KDB_STATE_CLEAR(SSBPT);
134 KDB_STATE_CLEAR(DOING_SS); 138 KDB_STATE_CLEAR(DOING_SS);
135 } else { 139 } else {
136 /* Start kdb main loop */ 140 /* Start kdb main loop */
137 error = kdb_main_loop(KDB_REASON_ENTER, reason, 141 error = kdb_main_loop(KDB_REASON_ENTER, reason,
138 ks->err_code, db_result, ks->linux_regs); 142 ks->err_code, db_result, ks->linux_regs);
139 } 143 }
140 /* 144 /*
141 * Upon exit from the kdb main loop setup break points and restart 145 * Upon exit from the kdb main loop setup break points and restart
142 * the system based on the requested continue state 146 * the system based on the requested continue state
143 */ 147 */
144 kdb_common_deinit_state(); 148 kdb_common_deinit_state();
145 KDB_STATE_CLEAR(PAGER); 149 KDB_STATE_CLEAR(PAGER);
146 kdbnearsym_cleanup(); 150 kdbnearsym_cleanup();
147 if (error == KDB_CMD_KGDB) { 151 if (error == KDB_CMD_KGDB) {
148 if (KDB_STATE(DOING_KGDB)) 152 if (KDB_STATE(DOING_KGDB))
149 KDB_STATE_CLEAR(DOING_KGDB); 153 KDB_STATE_CLEAR(DOING_KGDB);
150 return DBG_PASS_EVENT; 154 return DBG_PASS_EVENT;
151 } 155 }
152 kdb_bp_install(ks->linux_regs); 156 kdb_bp_install(ks->linux_regs);
153 dbg_activate_sw_breakpoints(); 157 dbg_activate_sw_breakpoints();
154 /* Set the exit state to a single step or a continue */ 158 /* Set the exit state to a single step or a continue */
155 if (KDB_STATE(DOING_SS)) 159 if (KDB_STATE(DOING_SS))
156 gdbstub_state(ks, "s"); 160 gdbstub_state(ks, "s");
157 else 161 else
158 gdbstub_state(ks, "c"); 162 gdbstub_state(ks, "c");
159 163
160 KDB_FLAG_CLEAR(CATASTROPHIC); 164 KDB_FLAG_CLEAR(CATASTROPHIC);
161 165
162 /* Invoke arch specific exception handling prior to system resume */ 166 /* Invoke arch specific exception handling prior to system resume */
163 kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e"); 167 kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e");
164 if (ks->pass_exception) 168 if (ks->pass_exception)
165 kgdb_info[ks->cpu].ret_state = 1; 169 kgdb_info[ks->cpu].ret_state = 1;
166 if (error == KDB_CMD_CPU) { 170 if (error == KDB_CMD_CPU) {
167 KDB_STATE_SET(REENTRY); 171 KDB_STATE_SET(REENTRY);
168 /* 172 /*
169 * Force clear the single step bit because kdb emulates this 173 * Force clear the single step bit because kdb emulates this
170 * differently vs the gdbstub 174 * differently vs the gdbstub
171 */ 175 */
172 kgdb_single_step = 0; 176 kgdb_single_step = 0;
173 dbg_deactivate_sw_breakpoints(); 177 dbg_deactivate_sw_breakpoints();
174 return DBG_SWITCH_CPU_EVENT; 178 return DBG_SWITCH_CPU_EVENT;
175 } 179 }
176 return kgdb_info[ks->cpu].ret_state; 180 return kgdb_info[ks->cpu].ret_state;
177 } 181 }
178 182
179 void kdb_gdb_state_pass(char *buf) 183 void kdb_gdb_state_pass(char *buf)
180 { 184 {
181 gdbstub_state(kdb_ks, buf); 185 gdbstub_state(kdb_ks, buf);
182 } 186 }
183 187
kernel/debug/kdb/kdb_main.c
1 /* 1 /*
2 * Kernel Debugger Architecture Independent Main Code 2 * Kernel Debugger Architecture Independent Main Code
3 * 3 *
4 * This file is subject to the terms and conditions of the GNU General Public 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 5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details. 6 * for more details.
7 * 7 *
8 * Copyright (C) 1999-2004 Silicon Graphics, Inc. All Rights Reserved. 8 * Copyright (C) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
9 * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com> 9 * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com>
10 * Xscale (R) modifications copyright (C) 2003 Intel Corporation. 10 * Xscale (R) modifications copyright (C) 2003 Intel Corporation.
11 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. 11 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
12 */ 12 */
13 13
14 #include <linux/ctype.h> 14 #include <linux/ctype.h>
15 #include <linux/types.h>
15 #include <linux/string.h> 16 #include <linux/string.h>
16 #include <linux/kernel.h> 17 #include <linux/kernel.h>
17 #include <linux/kmsg_dump.h> 18 #include <linux/kmsg_dump.h>
18 #include <linux/reboot.h> 19 #include <linux/reboot.h>
19 #include <linux/sched.h> 20 #include <linux/sched.h>
20 #include <linux/sysrq.h> 21 #include <linux/sysrq.h>
21 #include <linux/smp.h> 22 #include <linux/smp.h>
22 #include <linux/utsname.h> 23 #include <linux/utsname.h>
23 #include <linux/vmalloc.h> 24 #include <linux/vmalloc.h>
24 #include <linux/atomic.h> 25 #include <linux/atomic.h>
25 #include <linux/module.h> 26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
26 #include <linux/mm.h> 28 #include <linux/mm.h>
27 #include <linux/init.h> 29 #include <linux/init.h>
28 #include <linux/kallsyms.h> 30 #include <linux/kallsyms.h>
29 #include <linux/kgdb.h> 31 #include <linux/kgdb.h>
30 #include <linux/kdb.h> 32 #include <linux/kdb.h>
31 #include <linux/notifier.h> 33 #include <linux/notifier.h>
32 #include <linux/interrupt.h> 34 #include <linux/interrupt.h>
33 #include <linux/delay.h> 35 #include <linux/delay.h>
34 #include <linux/nmi.h> 36 #include <linux/nmi.h>
35 #include <linux/time.h> 37 #include <linux/time.h>
36 #include <linux/ptrace.h> 38 #include <linux/ptrace.h>
37 #include <linux/sysctl.h> 39 #include <linux/sysctl.h>
38 #include <linux/cpu.h> 40 #include <linux/cpu.h>
39 #include <linux/kdebug.h> 41 #include <linux/kdebug.h>
40 #include <linux/proc_fs.h> 42 #include <linux/proc_fs.h>
41 #include <linux/uaccess.h> 43 #include <linux/uaccess.h>
42 #include <linux/slab.h> 44 #include <linux/slab.h>
43 #include "kdb_private.h" 45 #include "kdb_private.h"
44 46
47 #undef MODULE_PARAM_PREFIX
48 #define MODULE_PARAM_PREFIX "kdb."
49
50 static int kdb_cmd_enabled = CONFIG_KDB_DEFAULT_ENABLE;
51 module_param_named(cmd_enable, kdb_cmd_enabled, int, 0600);
52
45 #define GREP_LEN 256 53 #define GREP_LEN 256
46 char kdb_grep_string[GREP_LEN]; 54 char kdb_grep_string[GREP_LEN];
47 int kdb_grepping_flag; 55 int kdb_grepping_flag;
48 EXPORT_SYMBOL(kdb_grepping_flag); 56 EXPORT_SYMBOL(kdb_grepping_flag);
49 int kdb_grep_leading; 57 int kdb_grep_leading;
50 int kdb_grep_trailing; 58 int kdb_grep_trailing;
51 59
52 /* 60 /*
53 * Kernel debugger state flags 61 * Kernel debugger state flags
54 */ 62 */
55 int kdb_flags; 63 int kdb_flags;
56 atomic_t kdb_event; 64 atomic_t kdb_event;
57 65
58 /* 66 /*
59 * kdb_lock protects updates to kdb_initial_cpu. Used to 67 * kdb_lock protects updates to kdb_initial_cpu. Used to
60 * single thread processors through the kernel debugger. 68 * single thread processors through the kernel debugger.
61 */ 69 */
62 int kdb_initial_cpu = -1; /* cpu number that owns kdb */ 70 int kdb_initial_cpu = -1; /* cpu number that owns kdb */
63 int kdb_nextline = 1; 71 int kdb_nextline = 1;
64 int kdb_state; /* General KDB state */ 72 int kdb_state; /* General KDB state */
65 73
66 struct task_struct *kdb_current_task; 74 struct task_struct *kdb_current_task;
67 EXPORT_SYMBOL(kdb_current_task); 75 EXPORT_SYMBOL(kdb_current_task);
68 struct pt_regs *kdb_current_regs; 76 struct pt_regs *kdb_current_regs;
69 77
70 const char *kdb_diemsg; 78 const char *kdb_diemsg;
71 static int kdb_go_count; 79 static int kdb_go_count;
72 #ifdef CONFIG_KDB_CONTINUE_CATASTROPHIC 80 #ifdef CONFIG_KDB_CONTINUE_CATASTROPHIC
73 static unsigned int kdb_continue_catastrophic = 81 static unsigned int kdb_continue_catastrophic =
74 CONFIG_KDB_CONTINUE_CATASTROPHIC; 82 CONFIG_KDB_CONTINUE_CATASTROPHIC;
75 #else 83 #else
76 static unsigned int kdb_continue_catastrophic; 84 static unsigned int kdb_continue_catastrophic;
77 #endif 85 #endif
78 86
79 /* kdb_commands describes the available commands. */ 87 /* kdb_commands describes the available commands. */
80 static kdbtab_t *kdb_commands; 88 static kdbtab_t *kdb_commands;
81 #define KDB_BASE_CMD_MAX 50 89 #define KDB_BASE_CMD_MAX 50
82 static int kdb_max_commands = KDB_BASE_CMD_MAX; 90 static int kdb_max_commands = KDB_BASE_CMD_MAX;
83 static kdbtab_t kdb_base_commands[KDB_BASE_CMD_MAX]; 91 static kdbtab_t kdb_base_commands[KDB_BASE_CMD_MAX];
84 #define for_each_kdbcmd(cmd, num) \ 92 #define for_each_kdbcmd(cmd, num) \
85 for ((cmd) = kdb_base_commands, (num) = 0; \ 93 for ((cmd) = kdb_base_commands, (num) = 0; \
86 num < kdb_max_commands; \ 94 num < kdb_max_commands; \
87 num++, num == KDB_BASE_CMD_MAX ? cmd = kdb_commands : cmd++) 95 num++, num == KDB_BASE_CMD_MAX ? cmd = kdb_commands : cmd++)
88 96
89 typedef struct _kdbmsg { 97 typedef struct _kdbmsg {
90 int km_diag; /* kdb diagnostic */ 98 int km_diag; /* kdb diagnostic */
91 char *km_msg; /* Corresponding message text */ 99 char *km_msg; /* Corresponding message text */
92 } kdbmsg_t; 100 } kdbmsg_t;
93 101
94 #define KDBMSG(msgnum, text) \ 102 #define KDBMSG(msgnum, text) \
95 { KDB_##msgnum, text } 103 { KDB_##msgnum, text }
96 104
97 static kdbmsg_t kdbmsgs[] = { 105 static kdbmsg_t kdbmsgs[] = {
98 KDBMSG(NOTFOUND, "Command Not Found"), 106 KDBMSG(NOTFOUND, "Command Not Found"),
99 KDBMSG(ARGCOUNT, "Improper argument count, see usage."), 107 KDBMSG(ARGCOUNT, "Improper argument count, see usage."),
100 KDBMSG(BADWIDTH, "Illegal value for BYTESPERWORD use 1, 2, 4 or 8, " 108 KDBMSG(BADWIDTH, "Illegal value for BYTESPERWORD use 1, 2, 4 or 8, "
101 "8 is only allowed on 64 bit systems"), 109 "8 is only allowed on 64 bit systems"),
102 KDBMSG(BADRADIX, "Illegal value for RADIX use 8, 10 or 16"), 110 KDBMSG(BADRADIX, "Illegal value for RADIX use 8, 10 or 16"),
103 KDBMSG(NOTENV, "Cannot find environment variable"), 111 KDBMSG(NOTENV, "Cannot find environment variable"),
104 KDBMSG(NOENVVALUE, "Environment variable should have value"), 112 KDBMSG(NOENVVALUE, "Environment variable should have value"),
105 KDBMSG(NOTIMP, "Command not implemented"), 113 KDBMSG(NOTIMP, "Command not implemented"),
106 KDBMSG(ENVFULL, "Environment full"), 114 KDBMSG(ENVFULL, "Environment full"),
107 KDBMSG(ENVBUFFULL, "Environment buffer full"), 115 KDBMSG(ENVBUFFULL, "Environment buffer full"),
108 KDBMSG(TOOMANYBPT, "Too many breakpoints defined"), 116 KDBMSG(TOOMANYBPT, "Too many breakpoints defined"),
109 #ifdef CONFIG_CPU_XSCALE 117 #ifdef CONFIG_CPU_XSCALE
110 KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"), 118 KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"),
111 #else 119 #else
112 KDBMSG(TOOMANYDBREGS, "More breakpoints than db registers defined"), 120 KDBMSG(TOOMANYDBREGS, "More breakpoints than db registers defined"),
113 #endif 121 #endif
114 KDBMSG(DUPBPT, "Duplicate breakpoint address"), 122 KDBMSG(DUPBPT, "Duplicate breakpoint address"),
115 KDBMSG(BPTNOTFOUND, "Breakpoint not found"), 123 KDBMSG(BPTNOTFOUND, "Breakpoint not found"),
116 KDBMSG(BADMODE, "Invalid IDMODE"), 124 KDBMSG(BADMODE, "Invalid IDMODE"),
117 KDBMSG(BADINT, "Illegal numeric value"), 125 KDBMSG(BADINT, "Illegal numeric value"),
118 KDBMSG(INVADDRFMT, "Invalid symbolic address format"), 126 KDBMSG(INVADDRFMT, "Invalid symbolic address format"),
119 KDBMSG(BADREG, "Invalid register name"), 127 KDBMSG(BADREG, "Invalid register name"),
120 KDBMSG(BADCPUNUM, "Invalid cpu number"), 128 KDBMSG(BADCPUNUM, "Invalid cpu number"),
121 KDBMSG(BADLENGTH, "Invalid length field"), 129 KDBMSG(BADLENGTH, "Invalid length field"),
122 KDBMSG(NOBP, "No Breakpoint exists"), 130 KDBMSG(NOBP, "No Breakpoint exists"),
123 KDBMSG(BADADDR, "Invalid address"), 131 KDBMSG(BADADDR, "Invalid address"),
132 KDBMSG(NOPERM, "Permission denied"),
124 }; 133 };
125 #undef KDBMSG 134 #undef KDBMSG
126 135
127 static const int __nkdb_err = ARRAY_SIZE(kdbmsgs); 136 static const int __nkdb_err = ARRAY_SIZE(kdbmsgs);
128 137
129 138
130 /* 139 /*
131 * Initial environment. This is all kept static and local to 140 * Initial environment. This is all kept static and local to
132 * this file. We don't want to rely on the memory allocation 141 * 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 142 * mechanisms in the kernel, so we use a very limited allocate-only
134 * heap for new and altered environment variables. The entire 143 * heap for new and altered environment variables. The entire
135 * environment is limited to a fixed number of entries (add more 144 * 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 145 * to __env[] if required) and a fixed amount of heap (add more to
137 * KDB_ENVBUFSIZE if required). 146 * KDB_ENVBUFSIZE if required).
138 */ 147 */
139 148
140 static char *__env[] = { 149 static char *__env[] = {
141 #if defined(CONFIG_SMP) 150 #if defined(CONFIG_SMP)
142 "PROMPT=[%d]kdb> ", 151 "PROMPT=[%d]kdb> ",
143 #else 152 #else
144 "PROMPT=kdb> ", 153 "PROMPT=kdb> ",
145 #endif 154 #endif
146 "MOREPROMPT=more> ", 155 "MOREPROMPT=more> ",
147 "RADIX=16", 156 "RADIX=16",
148 "MDCOUNT=8", /* lines of md output */ 157 "MDCOUNT=8", /* lines of md output */
149 KDB_PLATFORM_ENV, 158 KDB_PLATFORM_ENV,
150 "DTABCOUNT=30", 159 "DTABCOUNT=30",
151 "NOSECT=1", 160 "NOSECT=1",
152 (char *)0, 161 (char *)0,
153 (char *)0, 162 (char *)0,
154 (char *)0, 163 (char *)0,
155 (char *)0, 164 (char *)0,
156 (char *)0, 165 (char *)0,
157 (char *)0, 166 (char *)0,
158 (char *)0, 167 (char *)0,
159 (char *)0, 168 (char *)0,
160 (char *)0, 169 (char *)0,
161 (char *)0, 170 (char *)0,
162 (char *)0, 171 (char *)0,
163 (char *)0, 172 (char *)0,
164 (char *)0, 173 (char *)0,
165 (char *)0, 174 (char *)0,
166 (char *)0, 175 (char *)0,
167 (char *)0, 176 (char *)0,
168 (char *)0, 177 (char *)0,
169 (char *)0, 178 (char *)0,
170 (char *)0, 179 (char *)0,
171 (char *)0, 180 (char *)0,
172 (char *)0, 181 (char *)0,
173 (char *)0, 182 (char *)0,
174 (char *)0, 183 (char *)0,
175 (char *)0, 184 (char *)0,
176 }; 185 };
177 186
178 static const int __nenv = ARRAY_SIZE(__env); 187 static const int __nenv = ARRAY_SIZE(__env);
179 188
180 struct task_struct *kdb_curr_task(int cpu) 189 struct task_struct *kdb_curr_task(int cpu)
181 { 190 {
182 struct task_struct *p = curr_task(cpu); 191 struct task_struct *p = curr_task(cpu);
183 #ifdef _TIF_MCA_INIT 192 #ifdef _TIF_MCA_INIT
184 if ((task_thread_info(p)->flags & _TIF_MCA_INIT) && KDB_TSK(cpu)) 193 if ((task_thread_info(p)->flags & _TIF_MCA_INIT) && KDB_TSK(cpu))
185 p = krp->p; 194 p = krp->p;
186 #endif 195 #endif
187 return p; 196 return p;
188 } 197 }
189 198
190 /* 199 /*
200 * Check whether the flags of the current command and the permissions
201 * of the kdb console has allow a command to be run.
202 */
203 static inline bool kdb_check_flags(kdb_cmdflags_t flags, int permissions,
204 bool no_args)
205 {
206 /* permissions comes from userspace so needs massaging slightly */
207 permissions &= KDB_ENABLE_MASK;
208 permissions |= KDB_ENABLE_ALWAYS_SAFE;
209
210 /* some commands change group when launched with no arguments */
211 if (no_args)
212 permissions |= permissions << KDB_ENABLE_NO_ARGS_SHIFT;
213
214 flags |= KDB_ENABLE_ALL;
215
216 return permissions & flags;
217 }
218
219 /*
191 * kdbgetenv - This function will return the character string value of 220 * kdbgetenv - This function will return the character string value of
192 * an environment variable. 221 * an environment variable.
193 * Parameters: 222 * Parameters:
194 * match A character string representing an environment variable. 223 * match A character string representing an environment variable.
195 * Returns: 224 * Returns:
196 * NULL No environment variable matches 'match' 225 * NULL No environment variable matches 'match'
197 * char* Pointer to string value of environment variable. 226 * char* Pointer to string value of environment variable.
198 */ 227 */
199 char *kdbgetenv(const char *match) 228 char *kdbgetenv(const char *match)
200 { 229 {
201 char **ep = __env; 230 char **ep = __env;
202 int matchlen = strlen(match); 231 int matchlen = strlen(match);
203 int i; 232 int i;
204 233
205 for (i = 0; i < __nenv; i++) { 234 for (i = 0; i < __nenv; i++) {
206 char *e = *ep++; 235 char *e = *ep++;
207 236
208 if (!e) 237 if (!e)
209 continue; 238 continue;
210 239
211 if ((strncmp(match, e, matchlen) == 0) 240 if ((strncmp(match, e, matchlen) == 0)
212 && ((e[matchlen] == '\0') 241 && ((e[matchlen] == '\0')
213 || (e[matchlen] == '='))) { 242 || (e[matchlen] == '='))) {
214 char *cp = strchr(e, '='); 243 char *cp = strchr(e, '=');
215 return cp ? ++cp : ""; 244 return cp ? ++cp : "";
216 } 245 }
217 } 246 }
218 return NULL; 247 return NULL;
219 } 248 }
220 249
221 /* 250 /*
222 * kdballocenv - This function is used to allocate bytes for 251 * kdballocenv - This function is used to allocate bytes for
223 * environment entries. 252 * environment entries.
224 * Parameters: 253 * Parameters:
225 * match A character string representing a numeric value 254 * match A character string representing a numeric value
226 * Outputs: 255 * Outputs:
227 * *value the unsigned long representation of the env variable 'match' 256 * *value the unsigned long representation of the env variable 'match'
228 * Returns: 257 * Returns:
229 * Zero on success, a kdb diagnostic on failure. 258 * Zero on success, a kdb diagnostic on failure.
230 * Remarks: 259 * Remarks:
231 * We use a static environment buffer (envbuffer) to hold the values 260 * We use a static environment buffer (envbuffer) to hold the values
232 * of dynamically generated environment variables (see kdb_set). Buffer 261 * of dynamically generated environment variables (see kdb_set). Buffer
233 * space once allocated is never free'd, so over time, the amount of space 262 * 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 263 * (currently 512 bytes) will be exhausted if env variables are changed
235 * frequently. 264 * frequently.
236 */ 265 */
237 static char *kdballocenv(size_t bytes) 266 static char *kdballocenv(size_t bytes)
238 { 267 {
239 #define KDB_ENVBUFSIZE 512 268 #define KDB_ENVBUFSIZE 512
240 static char envbuffer[KDB_ENVBUFSIZE]; 269 static char envbuffer[KDB_ENVBUFSIZE];
241 static int envbufsize; 270 static int envbufsize;
242 char *ep = NULL; 271 char *ep = NULL;
243 272
244 if ((KDB_ENVBUFSIZE - envbufsize) >= bytes) { 273 if ((KDB_ENVBUFSIZE - envbufsize) >= bytes) {
245 ep = &envbuffer[envbufsize]; 274 ep = &envbuffer[envbufsize];
246 envbufsize += bytes; 275 envbufsize += bytes;
247 } 276 }
248 return ep; 277 return ep;
249 } 278 }
250 279
251 /* 280 /*
252 * kdbgetulenv - This function will return the value of an unsigned 281 * kdbgetulenv - This function will return the value of an unsigned
253 * long-valued environment variable. 282 * long-valued environment variable.
254 * Parameters: 283 * Parameters:
255 * match A character string representing a numeric value 284 * match A character string representing a numeric value
256 * Outputs: 285 * Outputs:
257 * *value the unsigned long represntation of the env variable 'match' 286 * *value the unsigned long represntation of the env variable 'match'
258 * Returns: 287 * Returns:
259 * Zero on success, a kdb diagnostic on failure. 288 * Zero on success, a kdb diagnostic on failure.
260 */ 289 */
261 static int kdbgetulenv(const char *match, unsigned long *value) 290 static int kdbgetulenv(const char *match, unsigned long *value)
262 { 291 {
263 char *ep; 292 char *ep;
264 293
265 ep = kdbgetenv(match); 294 ep = kdbgetenv(match);
266 if (!ep) 295 if (!ep)
267 return KDB_NOTENV; 296 return KDB_NOTENV;
268 if (strlen(ep) == 0) 297 if (strlen(ep) == 0)
269 return KDB_NOENVVALUE; 298 return KDB_NOENVVALUE;
270 299
271 *value = simple_strtoul(ep, NULL, 0); 300 *value = simple_strtoul(ep, NULL, 0);
272 301
273 return 0; 302 return 0;
274 } 303 }
275 304
276 /* 305 /*
277 * kdbgetintenv - This function will return the value of an 306 * kdbgetintenv - This function will return the value of an
278 * integer-valued environment variable. 307 * integer-valued environment variable.
279 * Parameters: 308 * Parameters:
280 * match A character string representing an integer-valued env variable 309 * match A character string representing an integer-valued env variable
281 * Outputs: 310 * Outputs:
282 * *value the integer representation of the environment variable 'match' 311 * *value the integer representation of the environment variable 'match'
283 * Returns: 312 * Returns:
284 * Zero on success, a kdb diagnostic on failure. 313 * Zero on success, a kdb diagnostic on failure.
285 */ 314 */
286 int kdbgetintenv(const char *match, int *value) 315 int kdbgetintenv(const char *match, int *value)
287 { 316 {
288 unsigned long val; 317 unsigned long val;
289 int diag; 318 int diag;
290 319
291 diag = kdbgetulenv(match, &val); 320 diag = kdbgetulenv(match, &val);
292 if (!diag) 321 if (!diag)
293 *value = (int) val; 322 *value = (int) val;
294 return diag; 323 return diag;
295 } 324 }
296 325
297 /* 326 /*
298 * kdbgetularg - This function will convert a numeric string into an 327 * kdbgetularg - This function will convert a numeric string into an
299 * unsigned long value. 328 * unsigned long value.
300 * Parameters: 329 * Parameters:
301 * arg A character string representing a numeric value 330 * arg A character string representing a numeric value
302 * Outputs: 331 * Outputs:
303 * *value the unsigned long represntation of arg. 332 * *value the unsigned long represntation of arg.
304 * Returns: 333 * Returns:
305 * Zero on success, a kdb diagnostic on failure. 334 * Zero on success, a kdb diagnostic on failure.
306 */ 335 */
307 int kdbgetularg(const char *arg, unsigned long *value) 336 int kdbgetularg(const char *arg, unsigned long *value)
308 { 337 {
309 char *endp; 338 char *endp;
310 unsigned long val; 339 unsigned long val;
311 340
312 val = simple_strtoul(arg, &endp, 0); 341 val = simple_strtoul(arg, &endp, 0);
313 342
314 if (endp == arg) { 343 if (endp == arg) {
315 /* 344 /*
316 * Also try base 16, for us folks too lazy to type the 345 * Also try base 16, for us folks too lazy to type the
317 * leading 0x... 346 * leading 0x...
318 */ 347 */
319 val = simple_strtoul(arg, &endp, 16); 348 val = simple_strtoul(arg, &endp, 16);
320 if (endp == arg) 349 if (endp == arg)
321 return KDB_BADINT; 350 return KDB_BADINT;
322 } 351 }
323 352
324 *value = val; 353 *value = val;
325 354
326 return 0; 355 return 0;
327 } 356 }
328 357
329 int kdbgetu64arg(const char *arg, u64 *value) 358 int kdbgetu64arg(const char *arg, u64 *value)
330 { 359 {
331 char *endp; 360 char *endp;
332 u64 val; 361 u64 val;
333 362
334 val = simple_strtoull(arg, &endp, 0); 363 val = simple_strtoull(arg, &endp, 0);
335 364
336 if (endp == arg) { 365 if (endp == arg) {
337 366
338 val = simple_strtoull(arg, &endp, 16); 367 val = simple_strtoull(arg, &endp, 16);
339 if (endp == arg) 368 if (endp == arg)
340 return KDB_BADINT; 369 return KDB_BADINT;
341 } 370 }
342 371
343 *value = val; 372 *value = val;
344 373
345 return 0; 374 return 0;
346 } 375 }
347 376
348 /* 377 /*
349 * kdb_set - This function implements the 'set' command. Alter an 378 * kdb_set - This function implements the 'set' command. Alter an
350 * existing environment variable or create a new one. 379 * existing environment variable or create a new one.
351 */ 380 */
352 int kdb_set(int argc, const char **argv) 381 int kdb_set(int argc, const char **argv)
353 { 382 {
354 int i; 383 int i;
355 char *ep; 384 char *ep;
356 size_t varlen, vallen; 385 size_t varlen, vallen;
357 386
358 /* 387 /*
359 * we can be invoked two ways: 388 * we can be invoked two ways:
360 * set var=value argv[1]="var", argv[2]="value" 389 * set var=value argv[1]="var", argv[2]="value"
361 * set var = value argv[1]="var", argv[2]="=", argv[3]="value" 390 * set var = value argv[1]="var", argv[2]="=", argv[3]="value"
362 * - if the latter, shift 'em down. 391 * - if the latter, shift 'em down.
363 */ 392 */
364 if (argc == 3) { 393 if (argc == 3) {
365 argv[2] = argv[3]; 394 argv[2] = argv[3];
366 argc--; 395 argc--;
367 } 396 }
368 397
369 if (argc != 2) 398 if (argc != 2)
370 return KDB_ARGCOUNT; 399 return KDB_ARGCOUNT;
371 400
372 /* 401 /*
373 * Check for internal variables 402 * Check for internal variables
374 */ 403 */
375 if (strcmp(argv[1], "KDBDEBUG") == 0) { 404 if (strcmp(argv[1], "KDBDEBUG") == 0) {
376 unsigned int debugflags; 405 unsigned int debugflags;
377 char *cp; 406 char *cp;
378 407
379 debugflags = simple_strtoul(argv[2], &cp, 0); 408 debugflags = simple_strtoul(argv[2], &cp, 0);
380 if (cp == argv[2] || debugflags & ~KDB_DEBUG_FLAG_MASK) { 409 if (cp == argv[2] || debugflags & ~KDB_DEBUG_FLAG_MASK) {
381 kdb_printf("kdb: illegal debug flags '%s'\n", 410 kdb_printf("kdb: illegal debug flags '%s'\n",
382 argv[2]); 411 argv[2]);
383 return 0; 412 return 0;
384 } 413 }
385 kdb_flags = (kdb_flags & 414 kdb_flags = (kdb_flags &
386 ~(KDB_DEBUG_FLAG_MASK << KDB_DEBUG_FLAG_SHIFT)) 415 ~(KDB_DEBUG_FLAG_MASK << KDB_DEBUG_FLAG_SHIFT))
387 | (debugflags << KDB_DEBUG_FLAG_SHIFT); 416 | (debugflags << KDB_DEBUG_FLAG_SHIFT);
388 417
389 return 0; 418 return 0;
390 } 419 }
391 420
392 /* 421 /*
393 * Tokenizer squashed the '=' sign. argv[1] is variable 422 * Tokenizer squashed the '=' sign. argv[1] is variable
394 * name, argv[2] = value. 423 * name, argv[2] = value.
395 */ 424 */
396 varlen = strlen(argv[1]); 425 varlen = strlen(argv[1]);
397 vallen = strlen(argv[2]); 426 vallen = strlen(argv[2]);
398 ep = kdballocenv(varlen + vallen + 2); 427 ep = kdballocenv(varlen + vallen + 2);
399 if (ep == (char *)0) 428 if (ep == (char *)0)
400 return KDB_ENVBUFFULL; 429 return KDB_ENVBUFFULL;
401 430
402 sprintf(ep, "%s=%s", argv[1], argv[2]); 431 sprintf(ep, "%s=%s", argv[1], argv[2]);
403 432
404 ep[varlen+vallen+1] = '\0'; 433 ep[varlen+vallen+1] = '\0';
405 434
406 for (i = 0; i < __nenv; i++) { 435 for (i = 0; i < __nenv; i++) {
407 if (__env[i] 436 if (__env[i]
408 && ((strncmp(__env[i], argv[1], varlen) == 0) 437 && ((strncmp(__env[i], argv[1], varlen) == 0)
409 && ((__env[i][varlen] == '\0') 438 && ((__env[i][varlen] == '\0')
410 || (__env[i][varlen] == '=')))) { 439 || (__env[i][varlen] == '=')))) {
411 __env[i] = ep; 440 __env[i] = ep;
412 return 0; 441 return 0;
413 } 442 }
414 } 443 }
415 444
416 /* 445 /*
417 * Wasn't existing variable. Fit into slot. 446 * Wasn't existing variable. Fit into slot.
418 */ 447 */
419 for (i = 0; i < __nenv-1; i++) { 448 for (i = 0; i < __nenv-1; i++) {
420 if (__env[i] == (char *)0) { 449 if (__env[i] == (char *)0) {
421 __env[i] = ep; 450 __env[i] = ep;
422 return 0; 451 return 0;
423 } 452 }
424 } 453 }
425 454
426 return KDB_ENVFULL; 455 return KDB_ENVFULL;
427 } 456 }
428 457
429 static int kdb_check_regs(void) 458 static int kdb_check_regs(void)
430 { 459 {
431 if (!kdb_current_regs) { 460 if (!kdb_current_regs) {
432 kdb_printf("No current kdb registers." 461 kdb_printf("No current kdb registers."
433 " You may need to select another task\n"); 462 " You may need to select another task\n");
434 return KDB_BADREG; 463 return KDB_BADREG;
435 } 464 }
436 return 0; 465 return 0;
437 } 466 }
438 467
439 /* 468 /*
440 * kdbgetaddrarg - This function is responsible for parsing an 469 * kdbgetaddrarg - This function is responsible for parsing an
441 * address-expression and returning the value of the expression, 470 * address-expression and returning the value of the expression,
442 * symbol name, and offset to the caller. 471 * symbol name, and offset to the caller.
443 * 472 *
444 * The argument may consist of a numeric value (decimal or 473 * The argument may consist of a numeric value (decimal or
445 * hexidecimal), a symbol name, a register name (preceded by the 474 * hexidecimal), a symbol name, a register name (preceded by the
446 * percent sign), an environment variable with a numeric value 475 * percent sign), an environment variable with a numeric value
447 * (preceded by a dollar sign) or a simple arithmetic expression 476 * (preceded by a dollar sign) or a simple arithmetic expression
448 * consisting of a symbol name, +/-, and a numeric constant value 477 * consisting of a symbol name, +/-, and a numeric constant value
449 * (offset). 478 * (offset).
450 * Parameters: 479 * Parameters:
451 * argc - count of arguments in argv 480 * argc - count of arguments in argv
452 * argv - argument vector 481 * argv - argument vector
453 * *nextarg - index to next unparsed argument in argv[] 482 * *nextarg - index to next unparsed argument in argv[]
454 * regs - Register state at time of KDB entry 483 * regs - Register state at time of KDB entry
455 * Outputs: 484 * Outputs:
456 * *value - receives the value of the address-expression 485 * *value - receives the value of the address-expression
457 * *offset - receives the offset specified, if any 486 * *offset - receives the offset specified, if any
458 * *name - receives the symbol name, if any 487 * *name - receives the symbol name, if any
459 * *nextarg - index to next unparsed argument in argv[] 488 * *nextarg - index to next unparsed argument in argv[]
460 * Returns: 489 * Returns:
461 * zero is returned on success, a kdb diagnostic code is 490 * zero is returned on success, a kdb diagnostic code is
462 * returned on error. 491 * returned on error.
463 */ 492 */
464 int kdbgetaddrarg(int argc, const char **argv, int *nextarg, 493 int kdbgetaddrarg(int argc, const char **argv, int *nextarg,
465 unsigned long *value, long *offset, 494 unsigned long *value, long *offset,
466 char **name) 495 char **name)
467 { 496 {
468 unsigned long addr; 497 unsigned long addr;
469 unsigned long off = 0; 498 unsigned long off = 0;
470 int positive; 499 int positive;
471 int diag; 500 int diag;
472 int found = 0; 501 int found = 0;
473 char *symname; 502 char *symname;
474 char symbol = '\0'; 503 char symbol = '\0';
475 char *cp; 504 char *cp;
476 kdb_symtab_t symtab; 505 kdb_symtab_t symtab;
477 506
478 /* 507 /*
508 * If the enable flags prohibit both arbitrary memory access
509 * and flow control then there are no reasonable grounds to
510 * provide symbol lookup.
511 */
512 if (!kdb_check_flags(KDB_ENABLE_MEM_READ | KDB_ENABLE_FLOW_CTRL,
513 kdb_cmd_enabled, false))
514 return KDB_NOPERM;
515
516 /*
479 * Process arguments which follow the following syntax: 517 * Process arguments which follow the following syntax:
480 * 518 *
481 * symbol | numeric-address [+/- numeric-offset] 519 * symbol | numeric-address [+/- numeric-offset]
482 * %register 520 * %register
483 * $environment-variable 521 * $environment-variable
484 */ 522 */
485 523
486 if (*nextarg > argc) 524 if (*nextarg > argc)
487 return KDB_ARGCOUNT; 525 return KDB_ARGCOUNT;
488 526
489 symname = (char *)argv[*nextarg]; 527 symname = (char *)argv[*nextarg];
490 528
491 /* 529 /*
492 * If there is no whitespace between the symbol 530 * If there is no whitespace between the symbol
493 * or address and the '+' or '-' symbols, we 531 * or address and the '+' or '-' symbols, we
494 * remember the character and replace it with a 532 * remember the character and replace it with a
495 * null so the symbol/value can be properly parsed 533 * null so the symbol/value can be properly parsed
496 */ 534 */
497 cp = strpbrk(symname, "+-"); 535 cp = strpbrk(symname, "+-");
498 if (cp != NULL) { 536 if (cp != NULL) {
499 symbol = *cp; 537 symbol = *cp;
500 *cp++ = '\0'; 538 *cp++ = '\0';
501 } 539 }
502 540
503 if (symname[0] == '$') { 541 if (symname[0] == '$') {
504 diag = kdbgetulenv(&symname[1], &addr); 542 diag = kdbgetulenv(&symname[1], &addr);
505 if (diag) 543 if (diag)
506 return diag; 544 return diag;
507 } else if (symname[0] == '%') { 545 } else if (symname[0] == '%') {
508 diag = kdb_check_regs(); 546 diag = kdb_check_regs();
509 if (diag) 547 if (diag)
510 return diag; 548 return diag;
511 /* Implement register values with % at a later time as it is 549 /* Implement register values with % at a later time as it is
512 * arch optional. 550 * arch optional.
513 */ 551 */
514 return KDB_NOTIMP; 552 return KDB_NOTIMP;
515 } else { 553 } else {
516 found = kdbgetsymval(symname, &symtab); 554 found = kdbgetsymval(symname, &symtab);
517 if (found) { 555 if (found) {
518 addr = symtab.sym_start; 556 addr = symtab.sym_start;
519 } else { 557 } else {
520 diag = kdbgetularg(argv[*nextarg], &addr); 558 diag = kdbgetularg(argv[*nextarg], &addr);
521 if (diag) 559 if (diag)
522 return diag; 560 return diag;
523 } 561 }
524 } 562 }
525 563
526 if (!found) 564 if (!found)
527 found = kdbnearsym(addr, &symtab); 565 found = kdbnearsym(addr, &symtab);
528 566
529 (*nextarg)++; 567 (*nextarg)++;
530 568
531 if (name) 569 if (name)
532 *name = symname; 570 *name = symname;
533 if (value) 571 if (value)
534 *value = addr; 572 *value = addr;
535 if (offset && name && *name) 573 if (offset && name && *name)
536 *offset = addr - symtab.sym_start; 574 *offset = addr - symtab.sym_start;
537 575
538 if ((*nextarg > argc) 576 if ((*nextarg > argc)
539 && (symbol == '\0')) 577 && (symbol == '\0'))
540 return 0; 578 return 0;
541 579
542 /* 580 /*
543 * check for +/- and offset 581 * check for +/- and offset
544 */ 582 */
545 583
546 if (symbol == '\0') { 584 if (symbol == '\0') {
547 if ((argv[*nextarg][0] != '+') 585 if ((argv[*nextarg][0] != '+')
548 && (argv[*nextarg][0] != '-')) { 586 && (argv[*nextarg][0] != '-')) {
549 /* 587 /*
550 * Not our argument. Return. 588 * Not our argument. Return.
551 */ 589 */
552 return 0; 590 return 0;
553 } else { 591 } else {
554 positive = (argv[*nextarg][0] == '+'); 592 positive = (argv[*nextarg][0] == '+');
555 (*nextarg)++; 593 (*nextarg)++;
556 } 594 }
557 } else 595 } else
558 positive = (symbol == '+'); 596 positive = (symbol == '+');
559 597
560 /* 598 /*
561 * Now there must be an offset! 599 * Now there must be an offset!
562 */ 600 */
563 if ((*nextarg > argc) 601 if ((*nextarg > argc)
564 && (symbol == '\0')) { 602 && (symbol == '\0')) {
565 return KDB_INVADDRFMT; 603 return KDB_INVADDRFMT;
566 } 604 }
567 605
568 if (!symbol) { 606 if (!symbol) {
569 cp = (char *)argv[*nextarg]; 607 cp = (char *)argv[*nextarg];
570 (*nextarg)++; 608 (*nextarg)++;
571 } 609 }
572 610
573 diag = kdbgetularg(cp, &off); 611 diag = kdbgetularg(cp, &off);
574 if (diag) 612 if (diag)
575 return diag; 613 return diag;
576 614
577 if (!positive) 615 if (!positive)
578 off = -off; 616 off = -off;
579 617
580 if (offset) 618 if (offset)
581 *offset += off; 619 *offset += off;
582 620
583 if (value) 621 if (value)
584 *value += off; 622 *value += off;
585 623
586 return 0; 624 return 0;
587 } 625 }
588 626
589 static void kdb_cmderror(int diag) 627 static void kdb_cmderror(int diag)
590 { 628 {
591 int i; 629 int i;
592 630
593 if (diag >= 0) { 631 if (diag >= 0) {
594 kdb_printf("no error detected (diagnostic is %d)\n", diag); 632 kdb_printf("no error detected (diagnostic is %d)\n", diag);
595 return; 633 return;
596 } 634 }
597 635
598 for (i = 0; i < __nkdb_err; i++) { 636 for (i = 0; i < __nkdb_err; i++) {
599 if (kdbmsgs[i].km_diag == diag) { 637 if (kdbmsgs[i].km_diag == diag) {
600 kdb_printf("diag: %d: %s\n", diag, kdbmsgs[i].km_msg); 638 kdb_printf("diag: %d: %s\n", diag, kdbmsgs[i].km_msg);
601 return; 639 return;
602 } 640 }
603 } 641 }
604 642
605 kdb_printf("Unknown diag %d\n", -diag); 643 kdb_printf("Unknown diag %d\n", -diag);
606 } 644 }
607 645
608 /* 646 /*
609 * kdb_defcmd, kdb_defcmd2 - This function implements the 'defcmd' 647 * kdb_defcmd, kdb_defcmd2 - This function implements the 'defcmd'
610 * command which defines one command as a set of other commands, 648 * command which defines one command as a set of other commands,
611 * terminated by endefcmd. kdb_defcmd processes the initial 649 * terminated by endefcmd. kdb_defcmd processes the initial
612 * 'defcmd' command, kdb_defcmd2 is invoked from kdb_parse for 650 * 'defcmd' command, kdb_defcmd2 is invoked from kdb_parse for
613 * the following commands until 'endefcmd'. 651 * the following commands until 'endefcmd'.
614 * Inputs: 652 * Inputs:
615 * argc argument count 653 * argc argument count
616 * argv argument vector 654 * argv argument vector
617 * Returns: 655 * Returns:
618 * zero for success, a kdb diagnostic if error 656 * zero for success, a kdb diagnostic if error
619 */ 657 */
620 struct defcmd_set { 658 struct defcmd_set {
621 int count; 659 int count;
622 int usable; 660 int usable;
623 char *name; 661 char *name;
624 char *usage; 662 char *usage;
625 char *help; 663 char *help;
626 char **command; 664 char **command;
627 }; 665 };
628 static struct defcmd_set *defcmd_set; 666 static struct defcmd_set *defcmd_set;
629 static int defcmd_set_count; 667 static int defcmd_set_count;
630 static int defcmd_in_progress; 668 static int defcmd_in_progress;
631 669
632 /* Forward references */ 670 /* Forward references */
633 static int kdb_exec_defcmd(int argc, const char **argv); 671 static int kdb_exec_defcmd(int argc, const char **argv);
634 672
635 static int kdb_defcmd2(const char *cmdstr, const char *argv0) 673 static int kdb_defcmd2(const char *cmdstr, const char *argv0)
636 { 674 {
637 struct defcmd_set *s = defcmd_set + defcmd_set_count - 1; 675 struct defcmd_set *s = defcmd_set + defcmd_set_count - 1;
638 char **save_command = s->command; 676 char **save_command = s->command;
639 if (strcmp(argv0, "endefcmd") == 0) { 677 if (strcmp(argv0, "endefcmd") == 0) {
640 defcmd_in_progress = 0; 678 defcmd_in_progress = 0;
641 if (!s->count) 679 if (!s->count)
642 s->usable = 0; 680 s->usable = 0;
643 if (s->usable) 681 if (s->usable)
644 kdb_register(s->name, kdb_exec_defcmd, 682 /* macros are always safe because when executed each
645 s->usage, s->help, 0); 683 * internal command re-enters kdb_parse() and is
684 * safety checked individually.
685 */
686 kdb_register_flags(s->name, kdb_exec_defcmd, s->usage,
687 s->help, 0,
688 KDB_ENABLE_ALWAYS_SAFE);
646 return 0; 689 return 0;
647 } 690 }
648 if (!s->usable) 691 if (!s->usable)
649 return KDB_NOTIMP; 692 return KDB_NOTIMP;
650 s->command = kzalloc((s->count + 1) * sizeof(*(s->command)), GFP_KDB); 693 s->command = kzalloc((s->count + 1) * sizeof(*(s->command)), GFP_KDB);
651 if (!s->command) { 694 if (!s->command) {
652 kdb_printf("Could not allocate new kdb_defcmd table for %s\n", 695 kdb_printf("Could not allocate new kdb_defcmd table for %s\n",
653 cmdstr); 696 cmdstr);
654 s->usable = 0; 697 s->usable = 0;
655 return KDB_NOTIMP; 698 return KDB_NOTIMP;
656 } 699 }
657 memcpy(s->command, save_command, s->count * sizeof(*(s->command))); 700 memcpy(s->command, save_command, s->count * sizeof(*(s->command)));
658 s->command[s->count++] = kdb_strdup(cmdstr, GFP_KDB); 701 s->command[s->count++] = kdb_strdup(cmdstr, GFP_KDB);
659 kfree(save_command); 702 kfree(save_command);
660 return 0; 703 return 0;
661 } 704 }
662 705
663 static int kdb_defcmd(int argc, const char **argv) 706 static int kdb_defcmd(int argc, const char **argv)
664 { 707 {
665 struct defcmd_set *save_defcmd_set = defcmd_set, *s; 708 struct defcmd_set *save_defcmd_set = defcmd_set, *s;
666 if (defcmd_in_progress) { 709 if (defcmd_in_progress) {
667 kdb_printf("kdb: nested defcmd detected, assuming missing " 710 kdb_printf("kdb: nested defcmd detected, assuming missing "
668 "endefcmd\n"); 711 "endefcmd\n");
669 kdb_defcmd2("endefcmd", "endefcmd"); 712 kdb_defcmd2("endefcmd", "endefcmd");
670 } 713 }
671 if (argc == 0) { 714 if (argc == 0) {
672 int i; 715 int i;
673 for (s = defcmd_set; s < defcmd_set + defcmd_set_count; ++s) { 716 for (s = defcmd_set; s < defcmd_set + defcmd_set_count; ++s) {
674 kdb_printf("defcmd %s \"%s\" \"%s\"\n", s->name, 717 kdb_printf("defcmd %s \"%s\" \"%s\"\n", s->name,
675 s->usage, s->help); 718 s->usage, s->help);
676 for (i = 0; i < s->count; ++i) 719 for (i = 0; i < s->count; ++i)
677 kdb_printf("%s", s->command[i]); 720 kdb_printf("%s", s->command[i]);
678 kdb_printf("endefcmd\n"); 721 kdb_printf("endefcmd\n");
679 } 722 }
680 return 0; 723 return 0;
681 } 724 }
682 if (argc != 3) 725 if (argc != 3)
683 return KDB_ARGCOUNT; 726 return KDB_ARGCOUNT;
684 if (in_dbg_master()) { 727 if (in_dbg_master()) {
685 kdb_printf("Command only available during kdb_init()\n"); 728 kdb_printf("Command only available during kdb_init()\n");
686 return KDB_NOTIMP; 729 return KDB_NOTIMP;
687 } 730 }
688 defcmd_set = kmalloc((defcmd_set_count + 1) * sizeof(*defcmd_set), 731 defcmd_set = kmalloc((defcmd_set_count + 1) * sizeof(*defcmd_set),
689 GFP_KDB); 732 GFP_KDB);
690 if (!defcmd_set) 733 if (!defcmd_set)
691 goto fail_defcmd; 734 goto fail_defcmd;
692 memcpy(defcmd_set, save_defcmd_set, 735 memcpy(defcmd_set, save_defcmd_set,
693 defcmd_set_count * sizeof(*defcmd_set)); 736 defcmd_set_count * sizeof(*defcmd_set));
694 s = defcmd_set + defcmd_set_count; 737 s = defcmd_set + defcmd_set_count;
695 memset(s, 0, sizeof(*s)); 738 memset(s, 0, sizeof(*s));
696 s->usable = 1; 739 s->usable = 1;
697 s->name = kdb_strdup(argv[1], GFP_KDB); 740 s->name = kdb_strdup(argv[1], GFP_KDB);
698 if (!s->name) 741 if (!s->name)
699 goto fail_name; 742 goto fail_name;
700 s->usage = kdb_strdup(argv[2], GFP_KDB); 743 s->usage = kdb_strdup(argv[2], GFP_KDB);
701 if (!s->usage) 744 if (!s->usage)
702 goto fail_usage; 745 goto fail_usage;
703 s->help = kdb_strdup(argv[3], GFP_KDB); 746 s->help = kdb_strdup(argv[3], GFP_KDB);
704 if (!s->help) 747 if (!s->help)
705 goto fail_help; 748 goto fail_help;
706 if (s->usage[0] == '"') { 749 if (s->usage[0] == '"') {
707 strcpy(s->usage, argv[2]+1); 750 strcpy(s->usage, argv[2]+1);
708 s->usage[strlen(s->usage)-1] = '\0'; 751 s->usage[strlen(s->usage)-1] = '\0';
709 } 752 }
710 if (s->help[0] == '"') { 753 if (s->help[0] == '"') {
711 strcpy(s->help, argv[3]+1); 754 strcpy(s->help, argv[3]+1);
712 s->help[strlen(s->help)-1] = '\0'; 755 s->help[strlen(s->help)-1] = '\0';
713 } 756 }
714 ++defcmd_set_count; 757 ++defcmd_set_count;
715 defcmd_in_progress = 1; 758 defcmd_in_progress = 1;
716 kfree(save_defcmd_set); 759 kfree(save_defcmd_set);
717 return 0; 760 return 0;
718 fail_help: 761 fail_help:
719 kfree(s->usage); 762 kfree(s->usage);
720 fail_usage: 763 fail_usage:
721 kfree(s->name); 764 kfree(s->name);
722 fail_name: 765 fail_name:
723 kfree(defcmd_set); 766 kfree(defcmd_set);
724 fail_defcmd: 767 fail_defcmd:
725 kdb_printf("Could not allocate new defcmd_set entry for %s\n", argv[1]); 768 kdb_printf("Could not allocate new defcmd_set entry for %s\n", argv[1]);
726 defcmd_set = save_defcmd_set; 769 defcmd_set = save_defcmd_set;
727 return KDB_NOTIMP; 770 return KDB_NOTIMP;
728 } 771 }
729 772
730 /* 773 /*
731 * kdb_exec_defcmd - Execute the set of commands associated with this 774 * kdb_exec_defcmd - Execute the set of commands associated with this
732 * defcmd name. 775 * defcmd name.
733 * Inputs: 776 * Inputs:
734 * argc argument count 777 * argc argument count
735 * argv argument vector 778 * argv argument vector
736 * Returns: 779 * Returns:
737 * zero for success, a kdb diagnostic if error 780 * zero for success, a kdb diagnostic if error
738 */ 781 */
739 static int kdb_exec_defcmd(int argc, const char **argv) 782 static int kdb_exec_defcmd(int argc, const char **argv)
740 { 783 {
741 int i, ret; 784 int i, ret;
742 struct defcmd_set *s; 785 struct defcmd_set *s;
743 if (argc != 0) 786 if (argc != 0)
744 return KDB_ARGCOUNT; 787 return KDB_ARGCOUNT;
745 for (s = defcmd_set, i = 0; i < defcmd_set_count; ++i, ++s) { 788 for (s = defcmd_set, i = 0; i < defcmd_set_count; ++i, ++s) {
746 if (strcmp(s->name, argv[0]) == 0) 789 if (strcmp(s->name, argv[0]) == 0)
747 break; 790 break;
748 } 791 }
749 if (i == defcmd_set_count) { 792 if (i == defcmd_set_count) {
750 kdb_printf("kdb_exec_defcmd: could not find commands for %s\n", 793 kdb_printf("kdb_exec_defcmd: could not find commands for %s\n",
751 argv[0]); 794 argv[0]);
752 return KDB_NOTIMP; 795 return KDB_NOTIMP;
753 } 796 }
754 for (i = 0; i < s->count; ++i) { 797 for (i = 0; i < s->count; ++i) {
755 /* Recursive use of kdb_parse, do not use argv after 798 /* Recursive use of kdb_parse, do not use argv after
756 * this point */ 799 * this point */
757 argv = NULL; 800 argv = NULL;
758 kdb_printf("[%s]kdb> %s\n", s->name, s->command[i]); 801 kdb_printf("[%s]kdb> %s\n", s->name, s->command[i]);
759 ret = kdb_parse(s->command[i]); 802 ret = kdb_parse(s->command[i]);
760 if (ret) 803 if (ret)
761 return ret; 804 return ret;
762 } 805 }
763 return 0; 806 return 0;
764 } 807 }
765 808
766 /* Command history */ 809 /* Command history */
767 #define KDB_CMD_HISTORY_COUNT 32 810 #define KDB_CMD_HISTORY_COUNT 32
768 #define CMD_BUFLEN 200 /* kdb_printf: max printline 811 #define CMD_BUFLEN 200 /* kdb_printf: max printline
769 * size == 256 */ 812 * size == 256 */
770 static unsigned int cmd_head, cmd_tail; 813 static unsigned int cmd_head, cmd_tail;
771 static unsigned int cmdptr; 814 static unsigned int cmdptr;
772 static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN]; 815 static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN];
773 static char cmd_cur[CMD_BUFLEN]; 816 static char cmd_cur[CMD_BUFLEN];
774 817
775 /* 818 /*
776 * The "str" argument may point to something like | grep xyz 819 * The "str" argument may point to something like | grep xyz
777 */ 820 */
778 static void parse_grep(const char *str) 821 static void parse_grep(const char *str)
779 { 822 {
780 int len; 823 int len;
781 char *cp = (char *)str, *cp2; 824 char *cp = (char *)str, *cp2;
782 825
783 /* sanity check: we should have been called with the \ first */ 826 /* sanity check: we should have been called with the \ first */
784 if (*cp != '|') 827 if (*cp != '|')
785 return; 828 return;
786 cp++; 829 cp++;
787 while (isspace(*cp)) 830 while (isspace(*cp))
788 cp++; 831 cp++;
789 if (strncmp(cp, "grep ", 5)) { 832 if (strncmp(cp, "grep ", 5)) {
790 kdb_printf("invalid 'pipe', see grephelp\n"); 833 kdb_printf("invalid 'pipe', see grephelp\n");
791 return; 834 return;
792 } 835 }
793 cp += 5; 836 cp += 5;
794 while (isspace(*cp)) 837 while (isspace(*cp))
795 cp++; 838 cp++;
796 cp2 = strchr(cp, '\n'); 839 cp2 = strchr(cp, '\n');
797 if (cp2) 840 if (cp2)
798 *cp2 = '\0'; /* remove the trailing newline */ 841 *cp2 = '\0'; /* remove the trailing newline */
799 len = strlen(cp); 842 len = strlen(cp);
800 if (len == 0) { 843 if (len == 0) {
801 kdb_printf("invalid 'pipe', see grephelp\n"); 844 kdb_printf("invalid 'pipe', see grephelp\n");
802 return; 845 return;
803 } 846 }
804 /* now cp points to a nonzero length search string */ 847 /* now cp points to a nonzero length search string */
805 if (*cp == '"') { 848 if (*cp == '"') {
806 /* allow it be "x y z" by removing the "'s - there must 849 /* allow it be "x y z" by removing the "'s - there must
807 be two of them */ 850 be two of them */
808 cp++; 851 cp++;
809 cp2 = strchr(cp, '"'); 852 cp2 = strchr(cp, '"');
810 if (!cp2) { 853 if (!cp2) {
811 kdb_printf("invalid quoted string, see grephelp\n"); 854 kdb_printf("invalid quoted string, see grephelp\n");
812 return; 855 return;
813 } 856 }
814 *cp2 = '\0'; /* end the string where the 2nd " was */ 857 *cp2 = '\0'; /* end the string where the 2nd " was */
815 } 858 }
816 kdb_grep_leading = 0; 859 kdb_grep_leading = 0;
817 if (*cp == '^') { 860 if (*cp == '^') {
818 kdb_grep_leading = 1; 861 kdb_grep_leading = 1;
819 cp++; 862 cp++;
820 } 863 }
821 len = strlen(cp); 864 len = strlen(cp);
822 kdb_grep_trailing = 0; 865 kdb_grep_trailing = 0;
823 if (*(cp+len-1) == '$') { 866 if (*(cp+len-1) == '$') {
824 kdb_grep_trailing = 1; 867 kdb_grep_trailing = 1;
825 *(cp+len-1) = '\0'; 868 *(cp+len-1) = '\0';
826 } 869 }
827 len = strlen(cp); 870 len = strlen(cp);
828 if (!len) 871 if (!len)
829 return; 872 return;
830 if (len >= GREP_LEN) { 873 if (len >= GREP_LEN) {
831 kdb_printf("search string too long\n"); 874 kdb_printf("search string too long\n");
832 return; 875 return;
833 } 876 }
834 strcpy(kdb_grep_string, cp); 877 strcpy(kdb_grep_string, cp);
835 kdb_grepping_flag++; 878 kdb_grepping_flag++;
836 return; 879 return;
837 } 880 }
838 881
839 /* 882 /*
840 * kdb_parse - Parse the command line, search the command table for a 883 * kdb_parse - Parse the command line, search the command table for a
841 * matching command and invoke the command function. This 884 * matching command and invoke the command function. This
842 * function may be called recursively, if it is, the second call 885 * function may be called recursively, if it is, the second call
843 * will overwrite argv and cbuf. It is the caller's 886 * will overwrite argv and cbuf. It is the caller's
844 * responsibility to save their argv if they recursively call 887 * responsibility to save their argv if they recursively call
845 * kdb_parse(). 888 * kdb_parse().
846 * Parameters: 889 * Parameters:
847 * cmdstr The input command line to be parsed. 890 * cmdstr The input command line to be parsed.
848 * regs The registers at the time kdb was entered. 891 * regs The registers at the time kdb was entered.
849 * Returns: 892 * Returns:
850 * Zero for success, a kdb diagnostic if failure. 893 * Zero for success, a kdb diagnostic if failure.
851 * Remarks: 894 * Remarks:
852 * Limited to 20 tokens. 895 * Limited to 20 tokens.
853 * 896 *
854 * Real rudimentary tokenization. Basically only whitespace 897 * Real rudimentary tokenization. Basically only whitespace
855 * is considered a token delimeter (but special consideration 898 * is considered a token delimeter (but special consideration
856 * is taken of the '=' sign as used by the 'set' command). 899 * is taken of the '=' sign as used by the 'set' command).
857 * 900 *
858 * The algorithm used to tokenize the input string relies on 901 * The algorithm used to tokenize the input string relies on
859 * there being at least one whitespace (or otherwise useless) 902 * there being at least one whitespace (or otherwise useless)
860 * character between tokens as the character immediately following 903 * character between tokens as the character immediately following
861 * the token is altered in-place to a null-byte to terminate the 904 * the token is altered in-place to a null-byte to terminate the
862 * token string. 905 * token string.
863 */ 906 */
864 907
865 #define MAXARGC 20 908 #define MAXARGC 20
866 909
867 int kdb_parse(const char *cmdstr) 910 int kdb_parse(const char *cmdstr)
868 { 911 {
869 static char *argv[MAXARGC]; 912 static char *argv[MAXARGC];
870 static int argc; 913 static int argc;
871 static char cbuf[CMD_BUFLEN+2]; 914 static char cbuf[CMD_BUFLEN+2];
872 char *cp; 915 char *cp;
873 char *cpp, quoted; 916 char *cpp, quoted;
874 kdbtab_t *tp; 917 kdbtab_t *tp;
875 int i, escaped, ignore_errors = 0, check_grep; 918 int i, escaped, ignore_errors = 0, check_grep;
876 919
877 /* 920 /*
878 * First tokenize the command string. 921 * First tokenize the command string.
879 */ 922 */
880 cp = (char *)cmdstr; 923 cp = (char *)cmdstr;
881 kdb_grepping_flag = check_grep = 0; 924 kdb_grepping_flag = check_grep = 0;
882 925
883 if (KDB_FLAG(CMD_INTERRUPT)) { 926 if (KDB_FLAG(CMD_INTERRUPT)) {
884 /* Previous command was interrupted, newline must not 927 /* Previous command was interrupted, newline must not
885 * repeat the command */ 928 * repeat the command */
886 KDB_FLAG_CLEAR(CMD_INTERRUPT); 929 KDB_FLAG_CLEAR(CMD_INTERRUPT);
887 KDB_STATE_SET(PAGER); 930 KDB_STATE_SET(PAGER);
888 argc = 0; /* no repeat */ 931 argc = 0; /* no repeat */
889 } 932 }
890 933
891 if (*cp != '\n' && *cp != '\0') { 934 if (*cp != '\n' && *cp != '\0') {
892 argc = 0; 935 argc = 0;
893 cpp = cbuf; 936 cpp = cbuf;
894 while (*cp) { 937 while (*cp) {
895 /* skip whitespace */ 938 /* skip whitespace */
896 while (isspace(*cp)) 939 while (isspace(*cp))
897 cp++; 940 cp++;
898 if ((*cp == '\0') || (*cp == '\n') || 941 if ((*cp == '\0') || (*cp == '\n') ||
899 (*cp == '#' && !defcmd_in_progress)) 942 (*cp == '#' && !defcmd_in_progress))
900 break; 943 break;
901 /* special case: check for | grep pattern */ 944 /* special case: check for | grep pattern */
902 if (*cp == '|') { 945 if (*cp == '|') {
903 check_grep++; 946 check_grep++;
904 break; 947 break;
905 } 948 }
906 if (cpp >= cbuf + CMD_BUFLEN) { 949 if (cpp >= cbuf + CMD_BUFLEN) {
907 kdb_printf("kdb_parse: command buffer " 950 kdb_printf("kdb_parse: command buffer "
908 "overflow, command ignored\n%s\n", 951 "overflow, command ignored\n%s\n",
909 cmdstr); 952 cmdstr);
910 return KDB_NOTFOUND; 953 return KDB_NOTFOUND;
911 } 954 }
912 if (argc >= MAXARGC - 1) { 955 if (argc >= MAXARGC - 1) {
913 kdb_printf("kdb_parse: too many arguments, " 956 kdb_printf("kdb_parse: too many arguments, "
914 "command ignored\n%s\n", cmdstr); 957 "command ignored\n%s\n", cmdstr);
915 return KDB_NOTFOUND; 958 return KDB_NOTFOUND;
916 } 959 }
917 argv[argc++] = cpp; 960 argv[argc++] = cpp;
918 escaped = 0; 961 escaped = 0;
919 quoted = '\0'; 962 quoted = '\0';
920 /* Copy to next unquoted and unescaped 963 /* Copy to next unquoted and unescaped
921 * whitespace or '=' */ 964 * whitespace or '=' */
922 while (*cp && *cp != '\n' && 965 while (*cp && *cp != '\n' &&
923 (escaped || quoted || !isspace(*cp))) { 966 (escaped || quoted || !isspace(*cp))) {
924 if (cpp >= cbuf + CMD_BUFLEN) 967 if (cpp >= cbuf + CMD_BUFLEN)
925 break; 968 break;
926 if (escaped) { 969 if (escaped) {
927 escaped = 0; 970 escaped = 0;
928 *cpp++ = *cp++; 971 *cpp++ = *cp++;
929 continue; 972 continue;
930 } 973 }
931 if (*cp == '\\') { 974 if (*cp == '\\') {
932 escaped = 1; 975 escaped = 1;
933 ++cp; 976 ++cp;
934 continue; 977 continue;
935 } 978 }
936 if (*cp == quoted) 979 if (*cp == quoted)
937 quoted = '\0'; 980 quoted = '\0';
938 else if (*cp == '\'' || *cp == '"') 981 else if (*cp == '\'' || *cp == '"')
939 quoted = *cp; 982 quoted = *cp;
940 *cpp = *cp++; 983 *cpp = *cp++;
941 if (*cpp == '=' && !quoted) 984 if (*cpp == '=' && !quoted)
942 break; 985 break;
943 ++cpp; 986 ++cpp;
944 } 987 }
945 *cpp++ = '\0'; /* Squash a ws or '=' character */ 988 *cpp++ = '\0'; /* Squash a ws or '=' character */
946 } 989 }
947 } 990 }
948 if (!argc) 991 if (!argc)
949 return 0; 992 return 0;
950 if (check_grep) 993 if (check_grep)
951 parse_grep(cp); 994 parse_grep(cp);
952 if (defcmd_in_progress) { 995 if (defcmd_in_progress) {
953 int result = kdb_defcmd2(cmdstr, argv[0]); 996 int result = kdb_defcmd2(cmdstr, argv[0]);
954 if (!defcmd_in_progress) { 997 if (!defcmd_in_progress) {
955 argc = 0; /* avoid repeat on endefcmd */ 998 argc = 0; /* avoid repeat on endefcmd */
956 *(argv[0]) = '\0'; 999 *(argv[0]) = '\0';
957 } 1000 }
958 return result; 1001 return result;
959 } 1002 }
960 if (argv[0][0] == '-' && argv[0][1] && 1003 if (argv[0][0] == '-' && argv[0][1] &&
961 (argv[0][1] < '0' || argv[0][1] > '9')) { 1004 (argv[0][1] < '0' || argv[0][1] > '9')) {
962 ignore_errors = 1; 1005 ignore_errors = 1;
963 ++argv[0]; 1006 ++argv[0];
964 } 1007 }
965 1008
966 for_each_kdbcmd(tp, i) { 1009 for_each_kdbcmd(tp, i) {
967 if (tp->cmd_name) { 1010 if (tp->cmd_name) {
968 /* 1011 /*
969 * If this command is allowed to be abbreviated, 1012 * If this command is allowed to be abbreviated,
970 * check to see if this is it. 1013 * check to see if this is it.
971 */ 1014 */
972 1015
973 if (tp->cmd_minlen 1016 if (tp->cmd_minlen
974 && (strlen(argv[0]) <= tp->cmd_minlen)) { 1017 && (strlen(argv[0]) <= tp->cmd_minlen)) {
975 if (strncmp(argv[0], 1018 if (strncmp(argv[0],
976 tp->cmd_name, 1019 tp->cmd_name,
977 tp->cmd_minlen) == 0) { 1020 tp->cmd_minlen) == 0) {
978 break; 1021 break;
979 } 1022 }
980 } 1023 }
981 1024
982 if (strcmp(argv[0], tp->cmd_name) == 0) 1025 if (strcmp(argv[0], tp->cmd_name) == 0)
983 break; 1026 break;
984 } 1027 }
985 } 1028 }
986 1029
987 /* 1030 /*
988 * If we don't find a command by this name, see if the first 1031 * 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. 1032 * few characters of this match any of the known commands.
990 * e.g., md1c20 should match md. 1033 * e.g., md1c20 should match md.
991 */ 1034 */
992 if (i == kdb_max_commands) { 1035 if (i == kdb_max_commands) {
993 for_each_kdbcmd(tp, i) { 1036 for_each_kdbcmd(tp, i) {
994 if (tp->cmd_name) { 1037 if (tp->cmd_name) {
995 if (strncmp(argv[0], 1038 if (strncmp(argv[0],
996 tp->cmd_name, 1039 tp->cmd_name,
997 strlen(tp->cmd_name)) == 0) { 1040 strlen(tp->cmd_name)) == 0) {
998 break; 1041 break;
999 } 1042 }
1000 } 1043 }
1001 } 1044 }
1002 } 1045 }
1003 1046
1004 if (i < kdb_max_commands) { 1047 if (i < kdb_max_commands) {
1005 int result; 1048 int result;
1049
1050 if (!kdb_check_flags(tp->cmd_flags, kdb_cmd_enabled, argc <= 1))
1051 return KDB_NOPERM;
1052
1006 KDB_STATE_SET(CMD); 1053 KDB_STATE_SET(CMD);
1007 result = (*tp->cmd_func)(argc-1, (const char **)argv); 1054 result = (*tp->cmd_func)(argc-1, (const char **)argv);
1008 if (result && ignore_errors && result > KDB_CMD_GO) 1055 if (result && ignore_errors && result > KDB_CMD_GO)
1009 result = 0; 1056 result = 0;
1010 KDB_STATE_CLEAR(CMD); 1057 KDB_STATE_CLEAR(CMD);
1011 switch (tp->cmd_repeat) { 1058
1012 case KDB_REPEAT_NONE: 1059 if (tp->cmd_flags & KDB_REPEAT_WITH_ARGS)
1013 argc = 0; 1060 return result;
1014 if (argv[0]) 1061
1015 *(argv[0]) = '\0'; 1062 argc = tp->cmd_flags & KDB_REPEAT_NO_ARGS ? 1 : 0;
1016 break; 1063 if (argv[argc])
1017 case KDB_REPEAT_NO_ARGS: 1064 *(argv[argc]) = '\0';
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; 1065 return result;
1026 } 1066 }
1027 1067
1028 /* 1068 /*
1029 * If the input with which we were presented does not 1069 * If the input with which we were presented does not
1030 * map to an existing command, attempt to parse it as an 1070 * map to an existing command, attempt to parse it as an
1031 * address argument and display the result. Useful for 1071 * address argument and display the result. Useful for
1032 * obtaining the address of a variable, or the nearest symbol 1072 * obtaining the address of a variable, or the nearest symbol
1033 * to an address contained in a register. 1073 * to an address contained in a register.
1034 */ 1074 */
1035 { 1075 {
1036 unsigned long value; 1076 unsigned long value;
1037 char *name = NULL; 1077 char *name = NULL;
1038 long offset; 1078 long offset;
1039 int nextarg = 0; 1079 int nextarg = 0;
1040 1080
1041 if (kdbgetaddrarg(0, (const char **)argv, &nextarg, 1081 if (kdbgetaddrarg(0, (const char **)argv, &nextarg,
1042 &value, &offset, &name)) { 1082 &value, &offset, &name)) {
1043 return KDB_NOTFOUND; 1083 return KDB_NOTFOUND;
1044 } 1084 }
1045 1085
1046 kdb_printf("%s = ", argv[0]); 1086 kdb_printf("%s = ", argv[0]);
1047 kdb_symbol_print(value, NULL, KDB_SP_DEFAULT); 1087 kdb_symbol_print(value, NULL, KDB_SP_DEFAULT);
1048 kdb_printf("\n"); 1088 kdb_printf("\n");
1049 return 0; 1089 return 0;
1050 } 1090 }
1051 } 1091 }
1052 1092
1053 1093
1054 static int handle_ctrl_cmd(char *cmd) 1094 static int handle_ctrl_cmd(char *cmd)
1055 { 1095 {
1056 #define CTRL_P 16 1096 #define CTRL_P 16
1057 #define CTRL_N 14 1097 #define CTRL_N 14
1058 1098
1059 /* initial situation */ 1099 /* initial situation */
1060 if (cmd_head == cmd_tail) 1100 if (cmd_head == cmd_tail)
1061 return 0; 1101 return 0;
1062 switch (*cmd) { 1102 switch (*cmd) {
1063 case CTRL_P: 1103 case CTRL_P:
1064 if (cmdptr != cmd_tail) 1104 if (cmdptr != cmd_tail)
1065 cmdptr = (cmdptr-1) % KDB_CMD_HISTORY_COUNT; 1105 cmdptr = (cmdptr-1) % KDB_CMD_HISTORY_COUNT;
1066 strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN); 1106 strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
1067 return 1; 1107 return 1;
1068 case CTRL_N: 1108 case CTRL_N:
1069 if (cmdptr != cmd_head) 1109 if (cmdptr != cmd_head)
1070 cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT; 1110 cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT;
1071 strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN); 1111 strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
1072 return 1; 1112 return 1;
1073 } 1113 }
1074 return 0; 1114 return 0;
1075 } 1115 }
1076 1116
1077 /* 1117 /*
1078 * kdb_reboot - This function implements the 'reboot' command. Reboot 1118 * kdb_reboot - This function implements the 'reboot' command. Reboot
1079 * the system immediately, or loop for ever on failure. 1119 * the system immediately, or loop for ever on failure.
1080 */ 1120 */
1081 static int kdb_reboot(int argc, const char **argv) 1121 static int kdb_reboot(int argc, const char **argv)
1082 { 1122 {
1083 emergency_restart(); 1123 emergency_restart();
1084 kdb_printf("Hmm, kdb_reboot did not reboot, spinning here\n"); 1124 kdb_printf("Hmm, kdb_reboot did not reboot, spinning here\n");
1085 while (1) 1125 while (1)
1086 cpu_relax(); 1126 cpu_relax();
1087 /* NOTREACHED */ 1127 /* NOTREACHED */
1088 return 0; 1128 return 0;
1089 } 1129 }
1090 1130
1091 static void kdb_dumpregs(struct pt_regs *regs) 1131 static void kdb_dumpregs(struct pt_regs *regs)
1092 { 1132 {
1093 int old_lvl = console_loglevel; 1133 int old_lvl = console_loglevel;
1094 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH; 1134 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
1095 kdb_trap_printk++; 1135 kdb_trap_printk++;
1096 show_regs(regs); 1136 show_regs(regs);
1097 kdb_trap_printk--; 1137 kdb_trap_printk--;
1098 kdb_printf("\n"); 1138 kdb_printf("\n");
1099 console_loglevel = old_lvl; 1139 console_loglevel = old_lvl;
1100 } 1140 }
1101 1141
1102 void kdb_set_current_task(struct task_struct *p) 1142 void kdb_set_current_task(struct task_struct *p)
1103 { 1143 {
1104 kdb_current_task = p; 1144 kdb_current_task = p;
1105 1145
1106 if (kdb_task_has_cpu(p)) { 1146 if (kdb_task_has_cpu(p)) {
1107 kdb_current_regs = KDB_TSKREGS(kdb_process_cpu(p)); 1147 kdb_current_regs = KDB_TSKREGS(kdb_process_cpu(p));
1108 return; 1148 return;
1109 } 1149 }
1110 kdb_current_regs = NULL; 1150 kdb_current_regs = NULL;
1111 } 1151 }
1112 1152
1113 /* 1153 /*
1114 * kdb_local - The main code for kdb. This routine is invoked on a 1154 * kdb_local - The main code for kdb. This routine is invoked on a
1115 * specific processor, it is not global. The main kdb() routine 1155 * specific processor, it is not global. The main kdb() routine
1116 * ensures that only one processor at a time is in this routine. 1156 * 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 1157 * This code is called with the real reason code on the first
1118 * entry to a kdb session, thereafter it is called with reason 1158 * entry to a kdb session, thereafter it is called with reason
1119 * SWITCH, even if the user goes back to the original cpu. 1159 * SWITCH, even if the user goes back to the original cpu.
1120 * Inputs: 1160 * Inputs:
1121 * reason The reason KDB was invoked 1161 * reason The reason KDB was invoked
1122 * error The hardware-defined error code 1162 * error The hardware-defined error code
1123 * regs The exception frame at time of fault/breakpoint. 1163 * regs The exception frame at time of fault/breakpoint.
1124 * db_result Result code from the break or debug point. 1164 * db_result Result code from the break or debug point.
1125 * Returns: 1165 * Returns:
1126 * 0 KDB was invoked for an event which it wasn't responsible 1166 * 0 KDB was invoked for an event which it wasn't responsible
1127 * 1 KDB handled the event for which it was invoked. 1167 * 1 KDB handled the event for which it was invoked.
1128 * KDB_CMD_GO User typed 'go'. 1168 * KDB_CMD_GO User typed 'go'.
1129 * KDB_CMD_CPU User switched to another cpu. 1169 * KDB_CMD_CPU User switched to another cpu.
1130 * KDB_CMD_SS Single step. 1170 * KDB_CMD_SS Single step.
1131 */ 1171 */
1132 static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs, 1172 static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
1133 kdb_dbtrap_t db_result) 1173 kdb_dbtrap_t db_result)
1134 { 1174 {
1135 char *cmdbuf; 1175 char *cmdbuf;
1136 int diag; 1176 int diag;
1137 struct task_struct *kdb_current = 1177 struct task_struct *kdb_current =
1138 kdb_curr_task(raw_smp_processor_id()); 1178 kdb_curr_task(raw_smp_processor_id());
1139 1179
1140 KDB_DEBUG_STATE("kdb_local 1", reason); 1180 KDB_DEBUG_STATE("kdb_local 1", reason);
1141 kdb_go_count = 0; 1181 kdb_go_count = 0;
1142 if (reason == KDB_REASON_DEBUG) { 1182 if (reason == KDB_REASON_DEBUG) {
1143 /* special case below */ 1183 /* special case below */
1144 } else { 1184 } else {
1145 kdb_printf("\nEntering kdb (current=0x%p, pid %d) ", 1185 kdb_printf("\nEntering kdb (current=0x%p, pid %d) ",
1146 kdb_current, kdb_current ? kdb_current->pid : 0); 1186 kdb_current, kdb_current ? kdb_current->pid : 0);
1147 #if defined(CONFIG_SMP) 1187 #if defined(CONFIG_SMP)
1148 kdb_printf("on processor %d ", raw_smp_processor_id()); 1188 kdb_printf("on processor %d ", raw_smp_processor_id());
1149 #endif 1189 #endif
1150 } 1190 }
1151 1191
1152 switch (reason) { 1192 switch (reason) {
1153 case KDB_REASON_DEBUG: 1193 case KDB_REASON_DEBUG:
1154 { 1194 {
1155 /* 1195 /*
1156 * If re-entering kdb after a single step 1196 * If re-entering kdb after a single step
1157 * command, don't print the message. 1197 * command, don't print the message.
1158 */ 1198 */
1159 switch (db_result) { 1199 switch (db_result) {
1160 case KDB_DB_BPT: 1200 case KDB_DB_BPT:
1161 kdb_printf("\nEntering kdb (0x%p, pid %d) ", 1201 kdb_printf("\nEntering kdb (0x%p, pid %d) ",
1162 kdb_current, kdb_current->pid); 1202 kdb_current, kdb_current->pid);
1163 #if defined(CONFIG_SMP) 1203 #if defined(CONFIG_SMP)
1164 kdb_printf("on processor %d ", raw_smp_processor_id()); 1204 kdb_printf("on processor %d ", raw_smp_processor_id());
1165 #endif 1205 #endif
1166 kdb_printf("due to Debug @ " kdb_machreg_fmt "\n", 1206 kdb_printf("due to Debug @ " kdb_machreg_fmt "\n",
1167 instruction_pointer(regs)); 1207 instruction_pointer(regs));
1168 break; 1208 break;
1169 case KDB_DB_SS: 1209 case KDB_DB_SS:
1170 break; 1210 break;
1171 case KDB_DB_SSBPT: 1211 case KDB_DB_SSBPT:
1172 KDB_DEBUG_STATE("kdb_local 4", reason); 1212 KDB_DEBUG_STATE("kdb_local 4", reason);
1173 return 1; /* kdba_db_trap did the work */ 1213 return 1; /* kdba_db_trap did the work */
1174 default: 1214 default:
1175 kdb_printf("kdb: Bad result from kdba_db_trap: %d\n", 1215 kdb_printf("kdb: Bad result from kdba_db_trap: %d\n",
1176 db_result); 1216 db_result);
1177 break; 1217 break;
1178 } 1218 }
1179 1219
1180 } 1220 }
1181 break; 1221 break;
1182 case KDB_REASON_ENTER: 1222 case KDB_REASON_ENTER:
1183 if (KDB_STATE(KEYBOARD)) 1223 if (KDB_STATE(KEYBOARD))
1184 kdb_printf("due to Keyboard Entry\n"); 1224 kdb_printf("due to Keyboard Entry\n");
1185 else 1225 else
1186 kdb_printf("due to KDB_ENTER()\n"); 1226 kdb_printf("due to KDB_ENTER()\n");
1187 break; 1227 break;
1188 case KDB_REASON_KEYBOARD: 1228 case KDB_REASON_KEYBOARD:
1189 KDB_STATE_SET(KEYBOARD); 1229 KDB_STATE_SET(KEYBOARD);
1190 kdb_printf("due to Keyboard Entry\n"); 1230 kdb_printf("due to Keyboard Entry\n");
1191 break; 1231 break;
1192 case KDB_REASON_ENTER_SLAVE: 1232 case KDB_REASON_ENTER_SLAVE:
1193 /* drop through, slaves only get released via cpu switch */ 1233 /* drop through, slaves only get released via cpu switch */
1194 case KDB_REASON_SWITCH: 1234 case KDB_REASON_SWITCH:
1195 kdb_printf("due to cpu switch\n"); 1235 kdb_printf("due to cpu switch\n");
1196 break; 1236 break;
1197 case KDB_REASON_OOPS: 1237 case KDB_REASON_OOPS:
1198 kdb_printf("Oops: %s\n", kdb_diemsg); 1238 kdb_printf("Oops: %s\n", kdb_diemsg);
1199 kdb_printf("due to oops @ " kdb_machreg_fmt "\n", 1239 kdb_printf("due to oops @ " kdb_machreg_fmt "\n",
1200 instruction_pointer(regs)); 1240 instruction_pointer(regs));
1201 kdb_dumpregs(regs); 1241 kdb_dumpregs(regs);
1202 break; 1242 break;
1203 case KDB_REASON_SYSTEM_NMI: 1243 case KDB_REASON_SYSTEM_NMI:
1204 kdb_printf("due to System NonMaskable Interrupt\n"); 1244 kdb_printf("due to System NonMaskable Interrupt\n");
1205 break; 1245 break;
1206 case KDB_REASON_NMI: 1246 case KDB_REASON_NMI:
1207 kdb_printf("due to NonMaskable Interrupt @ " 1247 kdb_printf("due to NonMaskable Interrupt @ "
1208 kdb_machreg_fmt "\n", 1248 kdb_machreg_fmt "\n",
1209 instruction_pointer(regs)); 1249 instruction_pointer(regs));
1210 kdb_dumpregs(regs); 1250 kdb_dumpregs(regs);
1211 break; 1251 break;
1212 case KDB_REASON_SSTEP: 1252 case KDB_REASON_SSTEP:
1213 case KDB_REASON_BREAK: 1253 case KDB_REASON_BREAK:
1214 kdb_printf("due to %s @ " kdb_machreg_fmt "\n", 1254 kdb_printf("due to %s @ " kdb_machreg_fmt "\n",
1215 reason == KDB_REASON_BREAK ? 1255 reason == KDB_REASON_BREAK ?
1216 "Breakpoint" : "SS trap", instruction_pointer(regs)); 1256 "Breakpoint" : "SS trap", instruction_pointer(regs));
1217 /* 1257 /*
1218 * Determine if this breakpoint is one that we 1258 * Determine if this breakpoint is one that we
1219 * are interested in. 1259 * are interested in.
1220 */ 1260 */
1221 if (db_result != KDB_DB_BPT) { 1261 if (db_result != KDB_DB_BPT) {
1222 kdb_printf("kdb: error return from kdba_bp_trap: %d\n", 1262 kdb_printf("kdb: error return from kdba_bp_trap: %d\n",
1223 db_result); 1263 db_result);
1224 KDB_DEBUG_STATE("kdb_local 6", reason); 1264 KDB_DEBUG_STATE("kdb_local 6", reason);
1225 return 0; /* Not for us, dismiss it */ 1265 return 0; /* Not for us, dismiss it */
1226 } 1266 }
1227 break; 1267 break;
1228 case KDB_REASON_RECURSE: 1268 case KDB_REASON_RECURSE:
1229 kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n", 1269 kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n",
1230 instruction_pointer(regs)); 1270 instruction_pointer(regs));
1231 break; 1271 break;
1232 default: 1272 default:
1233 kdb_printf("kdb: unexpected reason code: %d\n", reason); 1273 kdb_printf("kdb: unexpected reason code: %d\n", reason);
1234 KDB_DEBUG_STATE("kdb_local 8", reason); 1274 KDB_DEBUG_STATE("kdb_local 8", reason);
1235 return 0; /* Not for us, dismiss it */ 1275 return 0; /* Not for us, dismiss it */
1236 } 1276 }
1237 1277
1238 while (1) { 1278 while (1) {
1239 /* 1279 /*
1240 * Initialize pager context. 1280 * Initialize pager context.
1241 */ 1281 */
1242 kdb_nextline = 1; 1282 kdb_nextline = 1;
1243 KDB_STATE_CLEAR(SUPPRESS); 1283 KDB_STATE_CLEAR(SUPPRESS);
1244 1284
1245 cmdbuf = cmd_cur; 1285 cmdbuf = cmd_cur;
1246 *cmdbuf = '\0'; 1286 *cmdbuf = '\0';
1247 *(cmd_hist[cmd_head]) = '\0'; 1287 *(cmd_hist[cmd_head]) = '\0';
1248 1288
1249 do_full_getstr: 1289 do_full_getstr:
1250 #if defined(CONFIG_SMP) 1290 #if defined(CONFIG_SMP)
1251 snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"), 1291 snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
1252 raw_smp_processor_id()); 1292 raw_smp_processor_id());
1253 #else 1293 #else
1254 snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT")); 1294 snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"));
1255 #endif 1295 #endif
1256 if (defcmd_in_progress) 1296 if (defcmd_in_progress)
1257 strncat(kdb_prompt_str, "[defcmd]", CMD_BUFLEN); 1297 strncat(kdb_prompt_str, "[defcmd]", CMD_BUFLEN);
1258 1298
1259 /* 1299 /*
1260 * Fetch command from keyboard 1300 * Fetch command from keyboard
1261 */ 1301 */
1262 cmdbuf = kdb_getstr(cmdbuf, CMD_BUFLEN, kdb_prompt_str); 1302 cmdbuf = kdb_getstr(cmdbuf, CMD_BUFLEN, kdb_prompt_str);
1263 if (*cmdbuf != '\n') { 1303 if (*cmdbuf != '\n') {
1264 if (*cmdbuf < 32) { 1304 if (*cmdbuf < 32) {
1265 if (cmdptr == cmd_head) { 1305 if (cmdptr == cmd_head) {
1266 strncpy(cmd_hist[cmd_head], cmd_cur, 1306 strncpy(cmd_hist[cmd_head], cmd_cur,
1267 CMD_BUFLEN); 1307 CMD_BUFLEN);
1268 *(cmd_hist[cmd_head] + 1308 *(cmd_hist[cmd_head] +
1269 strlen(cmd_hist[cmd_head])-1) = '\0'; 1309 strlen(cmd_hist[cmd_head])-1) = '\0';
1270 } 1310 }
1271 if (!handle_ctrl_cmd(cmdbuf)) 1311 if (!handle_ctrl_cmd(cmdbuf))
1272 *(cmd_cur+strlen(cmd_cur)-1) = '\0'; 1312 *(cmd_cur+strlen(cmd_cur)-1) = '\0';
1273 cmdbuf = cmd_cur; 1313 cmdbuf = cmd_cur;
1274 goto do_full_getstr; 1314 goto do_full_getstr;
1275 } else { 1315 } else {
1276 strncpy(cmd_hist[cmd_head], cmd_cur, 1316 strncpy(cmd_hist[cmd_head], cmd_cur,
1277 CMD_BUFLEN); 1317 CMD_BUFLEN);
1278 } 1318 }
1279 1319
1280 cmd_head = (cmd_head+1) % KDB_CMD_HISTORY_COUNT; 1320 cmd_head = (cmd_head+1) % KDB_CMD_HISTORY_COUNT;
1281 if (cmd_head == cmd_tail) 1321 if (cmd_head == cmd_tail)
1282 cmd_tail = (cmd_tail+1) % KDB_CMD_HISTORY_COUNT; 1322 cmd_tail = (cmd_tail+1) % KDB_CMD_HISTORY_COUNT;
1283 } 1323 }
1284 1324
1285 cmdptr = cmd_head; 1325 cmdptr = cmd_head;
1286 diag = kdb_parse(cmdbuf); 1326 diag = kdb_parse(cmdbuf);
1287 if (diag == KDB_NOTFOUND) { 1327 if (diag == KDB_NOTFOUND) {
1288 kdb_printf("Unknown kdb command: '%s'\n", cmdbuf); 1328 kdb_printf("Unknown kdb command: '%s'\n", cmdbuf);
1289 diag = 0; 1329 diag = 0;
1290 } 1330 }
1291 if (diag == KDB_CMD_GO 1331 if (diag == KDB_CMD_GO
1292 || diag == KDB_CMD_CPU 1332 || diag == KDB_CMD_CPU
1293 || diag == KDB_CMD_SS 1333 || diag == KDB_CMD_SS
1294 || diag == KDB_CMD_KGDB) 1334 || diag == KDB_CMD_KGDB)
1295 break; 1335 break;
1296 1336
1297 if (diag) 1337 if (diag)
1298 kdb_cmderror(diag); 1338 kdb_cmderror(diag);
1299 } 1339 }
1300 KDB_DEBUG_STATE("kdb_local 9", diag); 1340 KDB_DEBUG_STATE("kdb_local 9", diag);
1301 return diag; 1341 return diag;
1302 } 1342 }
1303 1343
1304 1344
1305 /* 1345 /*
1306 * kdb_print_state - Print the state data for the current processor 1346 * kdb_print_state - Print the state data for the current processor
1307 * for debugging. 1347 * for debugging.
1308 * Inputs: 1348 * Inputs:
1309 * text Identifies the debug point 1349 * text Identifies the debug point
1310 * value Any integer value to be printed, e.g. reason code. 1350 * value Any integer value to be printed, e.g. reason code.
1311 */ 1351 */
1312 void kdb_print_state(const char *text, int value) 1352 void kdb_print_state(const char *text, int value)
1313 { 1353 {
1314 kdb_printf("state: %s cpu %d value %d initial %d state %x\n", 1354 kdb_printf("state: %s cpu %d value %d initial %d state %x\n",
1315 text, raw_smp_processor_id(), value, kdb_initial_cpu, 1355 text, raw_smp_processor_id(), value, kdb_initial_cpu,
1316 kdb_state); 1356 kdb_state);
1317 } 1357 }
1318 1358
1319 /* 1359 /*
1320 * kdb_main_loop - After initial setup and assignment of the 1360 * kdb_main_loop - After initial setup and assignment of the
1321 * controlling cpu, all cpus are in this loop. One cpu is in 1361 * controlling cpu, all cpus are in this loop. One cpu is in
1322 * control and will issue the kdb prompt, the others will spin 1362 * control and will issue the kdb prompt, the others will spin
1323 * until 'go' or cpu switch. 1363 * until 'go' or cpu switch.
1324 * 1364 *
1325 * To get a consistent view of the kernel stacks for all 1365 * To get a consistent view of the kernel stacks for all
1326 * processes, this routine is invoked from the main kdb code via 1366 * processes, this routine is invoked from the main kdb code via
1327 * an architecture specific routine. kdba_main_loop is 1367 * an architecture specific routine. kdba_main_loop is
1328 * responsible for making the kernel stacks consistent for all 1368 * responsible for making the kernel stacks consistent for all
1329 * processes, there should be no difference between a blocked 1369 * processes, there should be no difference between a blocked
1330 * process and a running process as far as kdb is concerned. 1370 * process and a running process as far as kdb is concerned.
1331 * Inputs: 1371 * Inputs:
1332 * reason The reason KDB was invoked 1372 * reason The reason KDB was invoked
1333 * error The hardware-defined error code 1373 * error The hardware-defined error code
1334 * reason2 kdb's current reason code. 1374 * reason2 kdb's current reason code.
1335 * Initially error but can change 1375 * Initially error but can change
1336 * according to kdb state. 1376 * according to kdb state.
1337 * db_result Result code from break or debug point. 1377 * db_result Result code from break or debug point.
1338 * regs The exception frame at time of fault/breakpoint. 1378 * regs The exception frame at time of fault/breakpoint.
1339 * should always be valid. 1379 * should always be valid.
1340 * Returns: 1380 * Returns:
1341 * 0 KDB was invoked for an event which it wasn't responsible 1381 * 0 KDB was invoked for an event which it wasn't responsible
1342 * 1 KDB handled the event for which it was invoked. 1382 * 1 KDB handled the event for which it was invoked.
1343 */ 1383 */
1344 int kdb_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error, 1384 int kdb_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
1345 kdb_dbtrap_t db_result, struct pt_regs *regs) 1385 kdb_dbtrap_t db_result, struct pt_regs *regs)
1346 { 1386 {
1347 int result = 1; 1387 int result = 1;
1348 /* Stay in kdb() until 'go', 'ss[b]' or an error */ 1388 /* Stay in kdb() until 'go', 'ss[b]' or an error */
1349 while (1) { 1389 while (1) {
1350 /* 1390 /*
1351 * All processors except the one that is in control 1391 * All processors except the one that is in control
1352 * will spin here. 1392 * will spin here.
1353 */ 1393 */
1354 KDB_DEBUG_STATE("kdb_main_loop 1", reason); 1394 KDB_DEBUG_STATE("kdb_main_loop 1", reason);
1355 while (KDB_STATE(HOLD_CPU)) { 1395 while (KDB_STATE(HOLD_CPU)) {
1356 /* state KDB is turned off by kdb_cpu to see if the 1396 /* state KDB is turned off by kdb_cpu to see if the
1357 * other cpus are still live, each cpu in this loop 1397 * other cpus are still live, each cpu in this loop
1358 * turns it back on. 1398 * turns it back on.
1359 */ 1399 */
1360 if (!KDB_STATE(KDB)) 1400 if (!KDB_STATE(KDB))
1361 KDB_STATE_SET(KDB); 1401 KDB_STATE_SET(KDB);
1362 } 1402 }
1363 1403
1364 KDB_STATE_CLEAR(SUPPRESS); 1404 KDB_STATE_CLEAR(SUPPRESS);
1365 KDB_DEBUG_STATE("kdb_main_loop 2", reason); 1405 KDB_DEBUG_STATE("kdb_main_loop 2", reason);
1366 if (KDB_STATE(LEAVING)) 1406 if (KDB_STATE(LEAVING))
1367 break; /* Another cpu said 'go' */ 1407 break; /* Another cpu said 'go' */
1368 /* Still using kdb, this processor is in control */ 1408 /* Still using kdb, this processor is in control */
1369 result = kdb_local(reason2, error, regs, db_result); 1409 result = kdb_local(reason2, error, regs, db_result);
1370 KDB_DEBUG_STATE("kdb_main_loop 3", result); 1410 KDB_DEBUG_STATE("kdb_main_loop 3", result);
1371 1411
1372 if (result == KDB_CMD_CPU) 1412 if (result == KDB_CMD_CPU)
1373 break; 1413 break;
1374 1414
1375 if (result == KDB_CMD_SS) { 1415 if (result == KDB_CMD_SS) {
1376 KDB_STATE_SET(DOING_SS); 1416 KDB_STATE_SET(DOING_SS);
1377 break; 1417 break;
1378 } 1418 }
1379 1419
1380 if (result == KDB_CMD_KGDB) { 1420 if (result == KDB_CMD_KGDB) {
1381 if (!KDB_STATE(DOING_KGDB)) 1421 if (!KDB_STATE(DOING_KGDB))
1382 kdb_printf("Entering please attach debugger " 1422 kdb_printf("Entering please attach debugger "
1383 "or use $D#44+ or $3#33\n"); 1423 "or use $D#44+ or $3#33\n");
1384 break; 1424 break;
1385 } 1425 }
1386 if (result && result != 1 && result != KDB_CMD_GO) 1426 if (result && result != 1 && result != KDB_CMD_GO)
1387 kdb_printf("\nUnexpected kdb_local return code %d\n", 1427 kdb_printf("\nUnexpected kdb_local return code %d\n",
1388 result); 1428 result);
1389 KDB_DEBUG_STATE("kdb_main_loop 4", reason); 1429 KDB_DEBUG_STATE("kdb_main_loop 4", reason);
1390 break; 1430 break;
1391 } 1431 }
1392 if (KDB_STATE(DOING_SS)) 1432 if (KDB_STATE(DOING_SS))
1393 KDB_STATE_CLEAR(SSBPT); 1433 KDB_STATE_CLEAR(SSBPT);
1394 1434
1395 /* Clean up any keyboard devices before leaving */ 1435 /* Clean up any keyboard devices before leaving */
1396 kdb_kbd_cleanup_state(); 1436 kdb_kbd_cleanup_state();
1397 1437
1398 return result; 1438 return result;
1399 } 1439 }
1400 1440
1401 /* 1441 /*
1402 * kdb_mdr - This function implements the guts of the 'mdr', memory 1442 * kdb_mdr - This function implements the guts of the 'mdr', memory
1403 * read command. 1443 * read command.
1404 * mdr <addr arg>,<byte count> 1444 * mdr <addr arg>,<byte count>
1405 * Inputs: 1445 * Inputs:
1406 * addr Start address 1446 * addr Start address
1407 * count Number of bytes 1447 * count Number of bytes
1408 * Returns: 1448 * Returns:
1409 * Always 0. Any errors are detected and printed by kdb_getarea. 1449 * Always 0. Any errors are detected and printed by kdb_getarea.
1410 */ 1450 */
1411 static int kdb_mdr(unsigned long addr, unsigned int count) 1451 static int kdb_mdr(unsigned long addr, unsigned int count)
1412 { 1452 {
1413 unsigned char c; 1453 unsigned char c;
1414 while (count--) { 1454 while (count--) {
1415 if (kdb_getarea(c, addr)) 1455 if (kdb_getarea(c, addr))
1416 return 0; 1456 return 0;
1417 kdb_printf("%02x", c); 1457 kdb_printf("%02x", c);
1418 addr++; 1458 addr++;
1419 } 1459 }
1420 kdb_printf("\n"); 1460 kdb_printf("\n");
1421 return 0; 1461 return 0;
1422 } 1462 }
1423 1463
1424 /* 1464 /*
1425 * kdb_md - This function implements the 'md', 'md1', 'md2', 'md4', 1465 * kdb_md - This function implements the 'md', 'md1', 'md2', 'md4',
1426 * 'md8' 'mdr' and 'mds' commands. 1466 * 'md8' 'mdr' and 'mds' commands.
1427 * 1467 *
1428 * md|mds [<addr arg> [<line count> [<radix>]]] 1468 * md|mds [<addr arg> [<line count> [<radix>]]]
1429 * mdWcN [<addr arg> [<line count> [<radix>]]] 1469 * mdWcN [<addr arg> [<line count> [<radix>]]]
1430 * where W = is the width (1, 2, 4 or 8) and N is the count. 1470 * where W = is the width (1, 2, 4 or 8) and N is the count.
1431 * for eg., md1c20 reads 20 bytes, 1 at a time. 1471 * for eg., md1c20 reads 20 bytes, 1 at a time.
1432 * mdr <addr arg>,<byte count> 1472 * mdr <addr arg>,<byte count>
1433 */ 1473 */
1434 static void kdb_md_line(const char *fmtstr, unsigned long addr, 1474 static void kdb_md_line(const char *fmtstr, unsigned long addr,
1435 int symbolic, int nosect, int bytesperword, 1475 int symbolic, int nosect, int bytesperword,
1436 int num, int repeat, int phys) 1476 int num, int repeat, int phys)
1437 { 1477 {
1438 /* print just one line of data */ 1478 /* print just one line of data */
1439 kdb_symtab_t symtab; 1479 kdb_symtab_t symtab;
1440 char cbuf[32]; 1480 char cbuf[32];
1441 char *c = cbuf; 1481 char *c = cbuf;
1442 int i; 1482 int i;
1443 unsigned long word; 1483 unsigned long word;
1444 1484
1445 memset(cbuf, '\0', sizeof(cbuf)); 1485 memset(cbuf, '\0', sizeof(cbuf));
1446 if (phys) 1486 if (phys)
1447 kdb_printf("phys " kdb_machreg_fmt0 " ", addr); 1487 kdb_printf("phys " kdb_machreg_fmt0 " ", addr);
1448 else 1488 else
1449 kdb_printf(kdb_machreg_fmt0 " ", addr); 1489 kdb_printf(kdb_machreg_fmt0 " ", addr);
1450 1490
1451 for (i = 0; i < num && repeat--; i++) { 1491 for (i = 0; i < num && repeat--; i++) {
1452 if (phys) { 1492 if (phys) {
1453 if (kdb_getphysword(&word, addr, bytesperword)) 1493 if (kdb_getphysword(&word, addr, bytesperword))
1454 break; 1494 break;
1455 } else if (kdb_getword(&word, addr, bytesperword)) 1495 } else if (kdb_getword(&word, addr, bytesperword))
1456 break; 1496 break;
1457 kdb_printf(fmtstr, word); 1497 kdb_printf(fmtstr, word);
1458 if (symbolic) 1498 if (symbolic)
1459 kdbnearsym(word, &symtab); 1499 kdbnearsym(word, &symtab);
1460 else 1500 else
1461 memset(&symtab, 0, sizeof(symtab)); 1501 memset(&symtab, 0, sizeof(symtab));
1462 if (symtab.sym_name) { 1502 if (symtab.sym_name) {
1463 kdb_symbol_print(word, &symtab, 0); 1503 kdb_symbol_print(word, &symtab, 0);
1464 if (!nosect) { 1504 if (!nosect) {
1465 kdb_printf("\n"); 1505 kdb_printf("\n");
1466 kdb_printf(" %s %s " 1506 kdb_printf(" %s %s "
1467 kdb_machreg_fmt " " 1507 kdb_machreg_fmt " "
1468 kdb_machreg_fmt " " 1508 kdb_machreg_fmt " "
1469 kdb_machreg_fmt, symtab.mod_name, 1509 kdb_machreg_fmt, symtab.mod_name,
1470 symtab.sec_name, symtab.sec_start, 1510 symtab.sec_name, symtab.sec_start,
1471 symtab.sym_start, symtab.sym_end); 1511 symtab.sym_start, symtab.sym_end);
1472 } 1512 }
1473 addr += bytesperword; 1513 addr += bytesperword;
1474 } else { 1514 } else {
1475 union { 1515 union {
1476 u64 word; 1516 u64 word;
1477 unsigned char c[8]; 1517 unsigned char c[8];
1478 } wc; 1518 } wc;
1479 unsigned char *cp; 1519 unsigned char *cp;
1480 #ifdef __BIG_ENDIAN 1520 #ifdef __BIG_ENDIAN
1481 cp = wc.c + 8 - bytesperword; 1521 cp = wc.c + 8 - bytesperword;
1482 #else 1522 #else
1483 cp = wc.c; 1523 cp = wc.c;
1484 #endif 1524 #endif
1485 wc.word = word; 1525 wc.word = word;
1486 #define printable_char(c) \ 1526 #define printable_char(c) \
1487 ({unsigned char __c = c; isascii(__c) && isprint(__c) ? __c : '.'; }) 1527 ({unsigned char __c = c; isascii(__c) && isprint(__c) ? __c : '.'; })
1488 switch (bytesperword) { 1528 switch (bytesperword) {
1489 case 8: 1529 case 8:
1490 *c++ = printable_char(*cp++); 1530 *c++ = printable_char(*cp++);
1491 *c++ = printable_char(*cp++); 1531 *c++ = printable_char(*cp++);
1492 *c++ = printable_char(*cp++); 1532 *c++ = printable_char(*cp++);
1493 *c++ = printable_char(*cp++); 1533 *c++ = printable_char(*cp++);
1494 addr += 4; 1534 addr += 4;
1495 case 4: 1535 case 4:
1496 *c++ = printable_char(*cp++); 1536 *c++ = printable_char(*cp++);
1497 *c++ = printable_char(*cp++); 1537 *c++ = printable_char(*cp++);
1498 addr += 2; 1538 addr += 2;
1499 case 2: 1539 case 2:
1500 *c++ = printable_char(*cp++); 1540 *c++ = printable_char(*cp++);
1501 addr++; 1541 addr++;
1502 case 1: 1542 case 1:
1503 *c++ = printable_char(*cp++); 1543 *c++ = printable_char(*cp++);
1504 addr++; 1544 addr++;
1505 break; 1545 break;
1506 } 1546 }
1507 #undef printable_char 1547 #undef printable_char
1508 } 1548 }
1509 } 1549 }
1510 kdb_printf("%*s %s\n", (int)((num-i)*(2*bytesperword + 1)+1), 1550 kdb_printf("%*s %s\n", (int)((num-i)*(2*bytesperword + 1)+1),
1511 " ", cbuf); 1551 " ", cbuf);
1512 } 1552 }
1513 1553
1514 static int kdb_md(int argc, const char **argv) 1554 static int kdb_md(int argc, const char **argv)
1515 { 1555 {
1516 static unsigned long last_addr; 1556 static unsigned long last_addr;
1517 static int last_radix, last_bytesperword, last_repeat; 1557 static int last_radix, last_bytesperword, last_repeat;
1518 int radix = 16, mdcount = 8, bytesperword = KDB_WORD_SIZE, repeat; 1558 int radix = 16, mdcount = 8, bytesperword = KDB_WORD_SIZE, repeat;
1519 int nosect = 0; 1559 int nosect = 0;
1520 char fmtchar, fmtstr[64]; 1560 char fmtchar, fmtstr[64];
1521 unsigned long addr; 1561 unsigned long addr;
1522 unsigned long word; 1562 unsigned long word;
1523 long offset = 0; 1563 long offset = 0;
1524 int symbolic = 0; 1564 int symbolic = 0;
1525 int valid = 0; 1565 int valid = 0;
1526 int phys = 0; 1566 int phys = 0;
1527 1567
1528 kdbgetintenv("MDCOUNT", &mdcount); 1568 kdbgetintenv("MDCOUNT", &mdcount);
1529 kdbgetintenv("RADIX", &radix); 1569 kdbgetintenv("RADIX", &radix);
1530 kdbgetintenv("BYTESPERWORD", &bytesperword); 1570 kdbgetintenv("BYTESPERWORD", &bytesperword);
1531 1571
1532 /* Assume 'md <addr>' and start with environment values */ 1572 /* Assume 'md <addr>' and start with environment values */
1533 repeat = mdcount * 16 / bytesperword; 1573 repeat = mdcount * 16 / bytesperword;
1534 1574
1535 if (strcmp(argv[0], "mdr") == 0) { 1575 if (strcmp(argv[0], "mdr") == 0) {
1536 if (argc != 2) 1576 if (argc != 2)
1537 return KDB_ARGCOUNT; 1577 return KDB_ARGCOUNT;
1538 valid = 1; 1578 valid = 1;
1539 } else if (isdigit(argv[0][2])) { 1579 } else if (isdigit(argv[0][2])) {
1540 bytesperword = (int)(argv[0][2] - '0'); 1580 bytesperword = (int)(argv[0][2] - '0');
1541 if (bytesperword == 0) { 1581 if (bytesperword == 0) {
1542 bytesperword = last_bytesperword; 1582 bytesperword = last_bytesperword;
1543 if (bytesperword == 0) 1583 if (bytesperword == 0)
1544 bytesperword = 4; 1584 bytesperword = 4;
1545 } 1585 }
1546 last_bytesperword = bytesperword; 1586 last_bytesperword = bytesperword;
1547 repeat = mdcount * 16 / bytesperword; 1587 repeat = mdcount * 16 / bytesperword;
1548 if (!argv[0][3]) 1588 if (!argv[0][3])
1549 valid = 1; 1589 valid = 1;
1550 else if (argv[0][3] == 'c' && argv[0][4]) { 1590 else if (argv[0][3] == 'c' && argv[0][4]) {
1551 char *p; 1591 char *p;
1552 repeat = simple_strtoul(argv[0] + 4, &p, 10); 1592 repeat = simple_strtoul(argv[0] + 4, &p, 10);
1553 mdcount = ((repeat * bytesperword) + 15) / 16; 1593 mdcount = ((repeat * bytesperword) + 15) / 16;
1554 valid = !*p; 1594 valid = !*p;
1555 } 1595 }
1556 last_repeat = repeat; 1596 last_repeat = repeat;
1557 } else if (strcmp(argv[0], "md") == 0) 1597 } else if (strcmp(argv[0], "md") == 0)
1558 valid = 1; 1598 valid = 1;
1559 else if (strcmp(argv[0], "mds") == 0) 1599 else if (strcmp(argv[0], "mds") == 0)
1560 valid = 1; 1600 valid = 1;
1561 else if (strcmp(argv[0], "mdp") == 0) { 1601 else if (strcmp(argv[0], "mdp") == 0) {
1562 phys = valid = 1; 1602 phys = valid = 1;
1563 } 1603 }
1564 if (!valid) 1604 if (!valid)
1565 return KDB_NOTFOUND; 1605 return KDB_NOTFOUND;
1566 1606
1567 if (argc == 0) { 1607 if (argc == 0) {
1568 if (last_addr == 0) 1608 if (last_addr == 0)
1569 return KDB_ARGCOUNT; 1609 return KDB_ARGCOUNT;
1570 addr = last_addr; 1610 addr = last_addr;
1571 radix = last_radix; 1611 radix = last_radix;
1572 bytesperword = last_bytesperword; 1612 bytesperword = last_bytesperword;
1573 repeat = last_repeat; 1613 repeat = last_repeat;
1574 mdcount = ((repeat * bytesperword) + 15) / 16; 1614 mdcount = ((repeat * bytesperword) + 15) / 16;
1575 } 1615 }
1576 1616
1577 if (argc) { 1617 if (argc) {
1578 unsigned long val; 1618 unsigned long val;
1579 int diag, nextarg = 1; 1619 int diag, nextarg = 1;
1580 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, 1620 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
1581 &offset, NULL); 1621 &offset, NULL);
1582 if (diag) 1622 if (diag)
1583 return diag; 1623 return diag;
1584 if (argc > nextarg+2) 1624 if (argc > nextarg+2)
1585 return KDB_ARGCOUNT; 1625 return KDB_ARGCOUNT;
1586 1626
1587 if (argc >= nextarg) { 1627 if (argc >= nextarg) {
1588 diag = kdbgetularg(argv[nextarg], &val); 1628 diag = kdbgetularg(argv[nextarg], &val);
1589 if (!diag) { 1629 if (!diag) {
1590 mdcount = (int) val; 1630 mdcount = (int) val;
1591 repeat = mdcount * 16 / bytesperword; 1631 repeat = mdcount * 16 / bytesperword;
1592 } 1632 }
1593 } 1633 }
1594 if (argc >= nextarg+1) { 1634 if (argc >= nextarg+1) {
1595 diag = kdbgetularg(argv[nextarg+1], &val); 1635 diag = kdbgetularg(argv[nextarg+1], &val);
1596 if (!diag) 1636 if (!diag)
1597 radix = (int) val; 1637 radix = (int) val;
1598 } 1638 }
1599 } 1639 }
1600 1640
1601 if (strcmp(argv[0], "mdr") == 0) 1641 if (strcmp(argv[0], "mdr") == 0)
1602 return kdb_mdr(addr, mdcount); 1642 return kdb_mdr(addr, mdcount);
1603 1643
1604 switch (radix) { 1644 switch (radix) {
1605 case 10: 1645 case 10:
1606 fmtchar = 'd'; 1646 fmtchar = 'd';
1607 break; 1647 break;
1608 case 16: 1648 case 16:
1609 fmtchar = 'x'; 1649 fmtchar = 'x';
1610 break; 1650 break;
1611 case 8: 1651 case 8:
1612 fmtchar = 'o'; 1652 fmtchar = 'o';
1613 break; 1653 break;
1614 default: 1654 default:
1615 return KDB_BADRADIX; 1655 return KDB_BADRADIX;
1616 } 1656 }
1617 1657
1618 last_radix = radix; 1658 last_radix = radix;
1619 1659
1620 if (bytesperword > KDB_WORD_SIZE) 1660 if (bytesperword > KDB_WORD_SIZE)
1621 return KDB_BADWIDTH; 1661 return KDB_BADWIDTH;
1622 1662
1623 switch (bytesperword) { 1663 switch (bytesperword) {
1624 case 8: 1664 case 8:
1625 sprintf(fmtstr, "%%16.16l%c ", fmtchar); 1665 sprintf(fmtstr, "%%16.16l%c ", fmtchar);
1626 break; 1666 break;
1627 case 4: 1667 case 4:
1628 sprintf(fmtstr, "%%8.8l%c ", fmtchar); 1668 sprintf(fmtstr, "%%8.8l%c ", fmtchar);
1629 break; 1669 break;
1630 case 2: 1670 case 2:
1631 sprintf(fmtstr, "%%4.4l%c ", fmtchar); 1671 sprintf(fmtstr, "%%4.4l%c ", fmtchar);
1632 break; 1672 break;
1633 case 1: 1673 case 1:
1634 sprintf(fmtstr, "%%2.2l%c ", fmtchar); 1674 sprintf(fmtstr, "%%2.2l%c ", fmtchar);
1635 break; 1675 break;
1636 default: 1676 default:
1637 return KDB_BADWIDTH; 1677 return KDB_BADWIDTH;
1638 } 1678 }
1639 1679
1640 last_repeat = repeat; 1680 last_repeat = repeat;
1641 last_bytesperword = bytesperword; 1681 last_bytesperword = bytesperword;
1642 1682
1643 if (strcmp(argv[0], "mds") == 0) { 1683 if (strcmp(argv[0], "mds") == 0) {
1644 symbolic = 1; 1684 symbolic = 1;
1645 /* Do not save these changes as last_*, they are temporary mds 1685 /* Do not save these changes as last_*, they are temporary mds
1646 * overrides. 1686 * overrides.
1647 */ 1687 */
1648 bytesperword = KDB_WORD_SIZE; 1688 bytesperword = KDB_WORD_SIZE;
1649 repeat = mdcount; 1689 repeat = mdcount;
1650 kdbgetintenv("NOSECT", &nosect); 1690 kdbgetintenv("NOSECT", &nosect);
1651 } 1691 }
1652 1692
1653 /* Round address down modulo BYTESPERWORD */ 1693 /* Round address down modulo BYTESPERWORD */
1654 1694
1655 addr &= ~(bytesperword-1); 1695 addr &= ~(bytesperword-1);
1656 1696
1657 while (repeat > 0) { 1697 while (repeat > 0) {
1658 unsigned long a; 1698 unsigned long a;
1659 int n, z, num = (symbolic ? 1 : (16 / bytesperword)); 1699 int n, z, num = (symbolic ? 1 : (16 / bytesperword));
1660 1700
1661 if (KDB_FLAG(CMD_INTERRUPT)) 1701 if (KDB_FLAG(CMD_INTERRUPT))
1662 return 0; 1702 return 0;
1663 for (a = addr, z = 0; z < repeat; a += bytesperword, ++z) { 1703 for (a = addr, z = 0; z < repeat; a += bytesperword, ++z) {
1664 if (phys) { 1704 if (phys) {
1665 if (kdb_getphysword(&word, a, bytesperword) 1705 if (kdb_getphysword(&word, a, bytesperword)
1666 || word) 1706 || word)
1667 break; 1707 break;
1668 } else if (kdb_getword(&word, a, bytesperword) || word) 1708 } else if (kdb_getword(&word, a, bytesperword) || word)
1669 break; 1709 break;
1670 } 1710 }
1671 n = min(num, repeat); 1711 n = min(num, repeat);
1672 kdb_md_line(fmtstr, addr, symbolic, nosect, bytesperword, 1712 kdb_md_line(fmtstr, addr, symbolic, nosect, bytesperword,
1673 num, repeat, phys); 1713 num, repeat, phys);
1674 addr += bytesperword * n; 1714 addr += bytesperword * n;
1675 repeat -= n; 1715 repeat -= n;
1676 z = (z + num - 1) / num; 1716 z = (z + num - 1) / num;
1677 if (z > 2) { 1717 if (z > 2) {
1678 int s = num * (z-2); 1718 int s = num * (z-2);
1679 kdb_printf(kdb_machreg_fmt0 "-" kdb_machreg_fmt0 1719 kdb_printf(kdb_machreg_fmt0 "-" kdb_machreg_fmt0
1680 " zero suppressed\n", 1720 " zero suppressed\n",
1681 addr, addr + bytesperword * s - 1); 1721 addr, addr + bytesperword * s - 1);
1682 addr += bytesperword * s; 1722 addr += bytesperword * s;
1683 repeat -= s; 1723 repeat -= s;
1684 } 1724 }
1685 } 1725 }
1686 last_addr = addr; 1726 last_addr = addr;
1687 1727
1688 return 0; 1728 return 0;
1689 } 1729 }
1690 1730
1691 /* 1731 /*
1692 * kdb_mm - This function implements the 'mm' command. 1732 * kdb_mm - This function implements the 'mm' command.
1693 * mm address-expression new-value 1733 * mm address-expression new-value
1694 * Remarks: 1734 * Remarks:
1695 * mm works on machine words, mmW works on bytes. 1735 * mm works on machine words, mmW works on bytes.
1696 */ 1736 */
1697 static int kdb_mm(int argc, const char **argv) 1737 static int kdb_mm(int argc, const char **argv)
1698 { 1738 {
1699 int diag; 1739 int diag;
1700 unsigned long addr; 1740 unsigned long addr;
1701 long offset = 0; 1741 long offset = 0;
1702 unsigned long contents; 1742 unsigned long contents;
1703 int nextarg; 1743 int nextarg;
1704 int width; 1744 int width;
1705 1745
1706 if (argv[0][2] && !isdigit(argv[0][2])) 1746 if (argv[0][2] && !isdigit(argv[0][2]))
1707 return KDB_NOTFOUND; 1747 return KDB_NOTFOUND;
1708 1748
1709 if (argc < 2) 1749 if (argc < 2)
1710 return KDB_ARGCOUNT; 1750 return KDB_ARGCOUNT;
1711 1751
1712 nextarg = 1; 1752 nextarg = 1;
1713 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL); 1753 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
1714 if (diag) 1754 if (diag)
1715 return diag; 1755 return diag;
1716 1756
1717 if (nextarg > argc) 1757 if (nextarg > argc)
1718 return KDB_ARGCOUNT; 1758 return KDB_ARGCOUNT;
1719 diag = kdbgetaddrarg(argc, argv, &nextarg, &contents, NULL, NULL); 1759 diag = kdbgetaddrarg(argc, argv, &nextarg, &contents, NULL, NULL);
1720 if (diag) 1760 if (diag)
1721 return diag; 1761 return diag;
1722 1762
1723 if (nextarg != argc + 1) 1763 if (nextarg != argc + 1)
1724 return KDB_ARGCOUNT; 1764 return KDB_ARGCOUNT;
1725 1765
1726 width = argv[0][2] ? (argv[0][2] - '0') : (KDB_WORD_SIZE); 1766 width = argv[0][2] ? (argv[0][2] - '0') : (KDB_WORD_SIZE);
1727 diag = kdb_putword(addr, contents, width); 1767 diag = kdb_putword(addr, contents, width);
1728 if (diag) 1768 if (diag)
1729 return diag; 1769 return diag;
1730 1770
1731 kdb_printf(kdb_machreg_fmt " = " kdb_machreg_fmt "\n", addr, contents); 1771 kdb_printf(kdb_machreg_fmt " = " kdb_machreg_fmt "\n", addr, contents);
1732 1772
1733 return 0; 1773 return 0;
1734 } 1774 }
1735 1775
1736 /* 1776 /*
1737 * kdb_go - This function implements the 'go' command. 1777 * kdb_go - This function implements the 'go' command.
1738 * go [address-expression] 1778 * go [address-expression]
1739 */ 1779 */
1740 static int kdb_go(int argc, const char **argv) 1780 static int kdb_go(int argc, const char **argv)
1741 { 1781 {
1742 unsigned long addr; 1782 unsigned long addr;
1743 int diag; 1783 int diag;
1744 int nextarg; 1784 int nextarg;
1745 long offset; 1785 long offset;
1746 1786
1747 if (raw_smp_processor_id() != kdb_initial_cpu) { 1787 if (raw_smp_processor_id() != kdb_initial_cpu) {
1748 kdb_printf("go must execute on the entry cpu, " 1788 kdb_printf("go must execute on the entry cpu, "
1749 "please use \"cpu %d\" and then execute go\n", 1789 "please use \"cpu %d\" and then execute go\n",
1750 kdb_initial_cpu); 1790 kdb_initial_cpu);
1751 return KDB_BADCPUNUM; 1791 return KDB_BADCPUNUM;
1752 } 1792 }
1753 if (argc == 1) { 1793 if (argc == 1) {
1754 nextarg = 1; 1794 nextarg = 1;
1755 diag = kdbgetaddrarg(argc, argv, &nextarg, 1795 diag = kdbgetaddrarg(argc, argv, &nextarg,
1756 &addr, &offset, NULL); 1796 &addr, &offset, NULL);
1757 if (diag) 1797 if (diag)
1758 return diag; 1798 return diag;
1759 } else if (argc) { 1799 } else if (argc) {
1760 return KDB_ARGCOUNT; 1800 return KDB_ARGCOUNT;
1761 } 1801 }
1762 1802
1763 diag = KDB_CMD_GO; 1803 diag = KDB_CMD_GO;
1764 if (KDB_FLAG(CATASTROPHIC)) { 1804 if (KDB_FLAG(CATASTROPHIC)) {
1765 kdb_printf("Catastrophic error detected\n"); 1805 kdb_printf("Catastrophic error detected\n");
1766 kdb_printf("kdb_continue_catastrophic=%d, ", 1806 kdb_printf("kdb_continue_catastrophic=%d, ",
1767 kdb_continue_catastrophic); 1807 kdb_continue_catastrophic);
1768 if (kdb_continue_catastrophic == 0 && kdb_go_count++ == 0) { 1808 if (kdb_continue_catastrophic == 0 && kdb_go_count++ == 0) {
1769 kdb_printf("type go a second time if you really want " 1809 kdb_printf("type go a second time if you really want "
1770 "to continue\n"); 1810 "to continue\n");
1771 return 0; 1811 return 0;
1772 } 1812 }
1773 if (kdb_continue_catastrophic == 2) { 1813 if (kdb_continue_catastrophic == 2) {
1774 kdb_printf("forcing reboot\n"); 1814 kdb_printf("forcing reboot\n");
1775 kdb_reboot(0, NULL); 1815 kdb_reboot(0, NULL);
1776 } 1816 }
1777 kdb_printf("attempting to continue\n"); 1817 kdb_printf("attempting to continue\n");
1778 } 1818 }
1779 return diag; 1819 return diag;
1780 } 1820 }
1781 1821
1782 /* 1822 /*
1783 * kdb_rd - This function implements the 'rd' command. 1823 * kdb_rd - This function implements the 'rd' command.
1784 */ 1824 */
1785 static int kdb_rd(int argc, const char **argv) 1825 static int kdb_rd(int argc, const char **argv)
1786 { 1826 {
1787 int len = kdb_check_regs(); 1827 int len = kdb_check_regs();
1788 #if DBG_MAX_REG_NUM > 0 1828 #if DBG_MAX_REG_NUM > 0
1789 int i; 1829 int i;
1790 char *rname; 1830 char *rname;
1791 int rsize; 1831 int rsize;
1792 u64 reg64; 1832 u64 reg64;
1793 u32 reg32; 1833 u32 reg32;
1794 u16 reg16; 1834 u16 reg16;
1795 u8 reg8; 1835 u8 reg8;
1796 1836
1797 if (len) 1837 if (len)
1798 return len; 1838 return len;
1799 1839
1800 for (i = 0; i < DBG_MAX_REG_NUM; i++) { 1840 for (i = 0; i < DBG_MAX_REG_NUM; i++) {
1801 rsize = dbg_reg_def[i].size * 2; 1841 rsize = dbg_reg_def[i].size * 2;
1802 if (rsize > 16) 1842 if (rsize > 16)
1803 rsize = 2; 1843 rsize = 2;
1804 if (len + strlen(dbg_reg_def[i].name) + 4 + rsize > 80) { 1844 if (len + strlen(dbg_reg_def[i].name) + 4 + rsize > 80) {
1805 len = 0; 1845 len = 0;
1806 kdb_printf("\n"); 1846 kdb_printf("\n");
1807 } 1847 }
1808 if (len) 1848 if (len)
1809 len += kdb_printf(" "); 1849 len += kdb_printf(" ");
1810 switch(dbg_reg_def[i].size * 8) { 1850 switch(dbg_reg_def[i].size * 8) {
1811 case 8: 1851 case 8:
1812 rname = dbg_get_reg(i, &reg8, kdb_current_regs); 1852 rname = dbg_get_reg(i, &reg8, kdb_current_regs);
1813 if (!rname) 1853 if (!rname)
1814 break; 1854 break;
1815 len += kdb_printf("%s: %02x", rname, reg8); 1855 len += kdb_printf("%s: %02x", rname, reg8);
1816 break; 1856 break;
1817 case 16: 1857 case 16:
1818 rname = dbg_get_reg(i, &reg16, kdb_current_regs); 1858 rname = dbg_get_reg(i, &reg16, kdb_current_regs);
1819 if (!rname) 1859 if (!rname)
1820 break; 1860 break;
1821 len += kdb_printf("%s: %04x", rname, reg16); 1861 len += kdb_printf("%s: %04x", rname, reg16);
1822 break; 1862 break;
1823 case 32: 1863 case 32:
1824 rname = dbg_get_reg(i, &reg32, kdb_current_regs); 1864 rname = dbg_get_reg(i, &reg32, kdb_current_regs);
1825 if (!rname) 1865 if (!rname)
1826 break; 1866 break;
1827 len += kdb_printf("%s: %08x", rname, reg32); 1867 len += kdb_printf("%s: %08x", rname, reg32);
1828 break; 1868 break;
1829 case 64: 1869 case 64:
1830 rname = dbg_get_reg(i, &reg64, kdb_current_regs); 1870 rname = dbg_get_reg(i, &reg64, kdb_current_regs);
1831 if (!rname) 1871 if (!rname)
1832 break; 1872 break;
1833 len += kdb_printf("%s: %016llx", rname, reg64); 1873 len += kdb_printf("%s: %016llx", rname, reg64);
1834 break; 1874 break;
1835 default: 1875 default:
1836 len += kdb_printf("%s: ??", dbg_reg_def[i].name); 1876 len += kdb_printf("%s: ??", dbg_reg_def[i].name);
1837 } 1877 }
1838 } 1878 }
1839 kdb_printf("\n"); 1879 kdb_printf("\n");
1840 #else 1880 #else
1841 if (len) 1881 if (len)
1842 return len; 1882 return len;
1843 1883
1844 kdb_dumpregs(kdb_current_regs); 1884 kdb_dumpregs(kdb_current_regs);
1845 #endif 1885 #endif
1846 return 0; 1886 return 0;
1847 } 1887 }
1848 1888
1849 /* 1889 /*
1850 * kdb_rm - This function implements the 'rm' (register modify) command. 1890 * kdb_rm - This function implements the 'rm' (register modify) command.
1851 * rm register-name new-contents 1891 * rm register-name new-contents
1852 * Remarks: 1892 * Remarks:
1853 * Allows register modification with the same restrictions as gdb 1893 * Allows register modification with the same restrictions as gdb
1854 */ 1894 */
1855 static int kdb_rm(int argc, const char **argv) 1895 static int kdb_rm(int argc, const char **argv)
1856 { 1896 {
1857 #if DBG_MAX_REG_NUM > 0 1897 #if DBG_MAX_REG_NUM > 0
1858 int diag; 1898 int diag;
1859 const char *rname; 1899 const char *rname;
1860 int i; 1900 int i;
1861 u64 reg64; 1901 u64 reg64;
1862 u32 reg32; 1902 u32 reg32;
1863 u16 reg16; 1903 u16 reg16;
1864 u8 reg8; 1904 u8 reg8;
1865 1905
1866 if (argc != 2) 1906 if (argc != 2)
1867 return KDB_ARGCOUNT; 1907 return KDB_ARGCOUNT;
1868 /* 1908 /*
1869 * Allow presence or absence of leading '%' symbol. 1909 * Allow presence or absence of leading '%' symbol.
1870 */ 1910 */
1871 rname = argv[1]; 1911 rname = argv[1];
1872 if (*rname == '%') 1912 if (*rname == '%')
1873 rname++; 1913 rname++;
1874 1914
1875 diag = kdbgetu64arg(argv[2], &reg64); 1915 diag = kdbgetu64arg(argv[2], &reg64);
1876 if (diag) 1916 if (diag)
1877 return diag; 1917 return diag;
1878 1918
1879 diag = kdb_check_regs(); 1919 diag = kdb_check_regs();
1880 if (diag) 1920 if (diag)
1881 return diag; 1921 return diag;
1882 1922
1883 diag = KDB_BADREG; 1923 diag = KDB_BADREG;
1884 for (i = 0; i < DBG_MAX_REG_NUM; i++) { 1924 for (i = 0; i < DBG_MAX_REG_NUM; i++) {
1885 if (strcmp(rname, dbg_reg_def[i].name) == 0) { 1925 if (strcmp(rname, dbg_reg_def[i].name) == 0) {
1886 diag = 0; 1926 diag = 0;
1887 break; 1927 break;
1888 } 1928 }
1889 } 1929 }
1890 if (!diag) { 1930 if (!diag) {
1891 switch(dbg_reg_def[i].size * 8) { 1931 switch(dbg_reg_def[i].size * 8) {
1892 case 8: 1932 case 8:
1893 reg8 = reg64; 1933 reg8 = reg64;
1894 dbg_set_reg(i, &reg8, kdb_current_regs); 1934 dbg_set_reg(i, &reg8, kdb_current_regs);
1895 break; 1935 break;
1896 case 16: 1936 case 16:
1897 reg16 = reg64; 1937 reg16 = reg64;
1898 dbg_set_reg(i, &reg16, kdb_current_regs); 1938 dbg_set_reg(i, &reg16, kdb_current_regs);
1899 break; 1939 break;
1900 case 32: 1940 case 32:
1901 reg32 = reg64; 1941 reg32 = reg64;
1902 dbg_set_reg(i, &reg32, kdb_current_regs); 1942 dbg_set_reg(i, &reg32, kdb_current_regs);
1903 break; 1943 break;
1904 case 64: 1944 case 64:
1905 dbg_set_reg(i, &reg64, kdb_current_regs); 1945 dbg_set_reg(i, &reg64, kdb_current_regs);
1906 break; 1946 break;
1907 } 1947 }
1908 } 1948 }
1909 return diag; 1949 return diag;
1910 #else 1950 #else
1911 kdb_printf("ERROR: Register set currently not implemented\n"); 1951 kdb_printf("ERROR: Register set currently not implemented\n");
1912 return 0; 1952 return 0;
1913 #endif 1953 #endif
1914 } 1954 }
1915 1955
1916 #if defined(CONFIG_MAGIC_SYSRQ) 1956 #if defined(CONFIG_MAGIC_SYSRQ)
1917 /* 1957 /*
1918 * kdb_sr - This function implements the 'sr' (SYSRQ key) command 1958 * kdb_sr - This function implements the 'sr' (SYSRQ key) command
1919 * which interfaces to the soi-disant MAGIC SYSRQ functionality. 1959 * which interfaces to the soi-disant MAGIC SYSRQ functionality.
1920 * sr <magic-sysrq-code> 1960 * sr <magic-sysrq-code>
1921 */ 1961 */
1922 static int kdb_sr(int argc, const char **argv) 1962 static int kdb_sr(int argc, const char **argv)
1923 { 1963 {
1964 bool check_mask =
1965 !kdb_check_flags(KDB_ENABLE_ALL, kdb_cmd_enabled, false);
1966
1924 if (argc != 1) 1967 if (argc != 1)
1925 return KDB_ARGCOUNT; 1968 return KDB_ARGCOUNT;
1969
1926 kdb_trap_printk++; 1970 kdb_trap_printk++;
1927 __handle_sysrq(*argv[1], false); 1971 __handle_sysrq(*argv[1], check_mask);
1928 kdb_trap_printk--; 1972 kdb_trap_printk--;
1929 1973
1930 return 0; 1974 return 0;
1931 } 1975 }
1932 #endif /* CONFIG_MAGIC_SYSRQ */ 1976 #endif /* CONFIG_MAGIC_SYSRQ */
1933 1977
1934 /* 1978 /*
1935 * kdb_ef - This function implements the 'regs' (display exception 1979 * kdb_ef - This function implements the 'regs' (display exception
1936 * frame) command. This command takes an address and expects to 1980 * frame) command. This command takes an address and expects to
1937 * find an exception frame at that address, formats and prints 1981 * find an exception frame at that address, formats and prints
1938 * it. 1982 * it.
1939 * regs address-expression 1983 * regs address-expression
1940 * Remarks: 1984 * Remarks:
1941 * Not done yet. 1985 * Not done yet.
1942 */ 1986 */
1943 static int kdb_ef(int argc, const char **argv) 1987 static int kdb_ef(int argc, const char **argv)
1944 { 1988 {
1945 int diag; 1989 int diag;
1946 unsigned long addr; 1990 unsigned long addr;
1947 long offset; 1991 long offset;
1948 int nextarg; 1992 int nextarg;
1949 1993
1950 if (argc != 1) 1994 if (argc != 1)
1951 return KDB_ARGCOUNT; 1995 return KDB_ARGCOUNT;
1952 1996
1953 nextarg = 1; 1997 nextarg = 1;
1954 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL); 1998 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
1955 if (diag) 1999 if (diag)
1956 return diag; 2000 return diag;
1957 show_regs((struct pt_regs *)addr); 2001 show_regs((struct pt_regs *)addr);
1958 return 0; 2002 return 0;
1959 } 2003 }
1960 2004
1961 #if defined(CONFIG_MODULES) 2005 #if defined(CONFIG_MODULES)
1962 /* 2006 /*
1963 * kdb_lsmod - This function implements the 'lsmod' command. Lists 2007 * kdb_lsmod - This function implements the 'lsmod' command. Lists
1964 * currently loaded kernel modules. 2008 * currently loaded kernel modules.
1965 * Mostly taken from userland lsmod. 2009 * Mostly taken from userland lsmod.
1966 */ 2010 */
1967 static int kdb_lsmod(int argc, const char **argv) 2011 static int kdb_lsmod(int argc, const char **argv)
1968 { 2012 {
1969 struct module *mod; 2013 struct module *mod;
1970 2014
1971 if (argc != 0) 2015 if (argc != 0)
1972 return KDB_ARGCOUNT; 2016 return KDB_ARGCOUNT;
1973 2017
1974 kdb_printf("Module Size modstruct Used by\n"); 2018 kdb_printf("Module Size modstruct Used by\n");
1975 list_for_each_entry(mod, kdb_modules, list) { 2019 list_for_each_entry(mod, kdb_modules, list) {
1976 if (mod->state == MODULE_STATE_UNFORMED) 2020 if (mod->state == MODULE_STATE_UNFORMED)
1977 continue; 2021 continue;
1978 2022
1979 kdb_printf("%-20s%8u 0x%p ", mod->name, 2023 kdb_printf("%-20s%8u 0x%p ", mod->name,
1980 mod->core_size, (void *)mod); 2024 mod->core_size, (void *)mod);
1981 #ifdef CONFIG_MODULE_UNLOAD 2025 #ifdef CONFIG_MODULE_UNLOAD
1982 kdb_printf("%4ld ", module_refcount(mod)); 2026 kdb_printf("%4ld ", module_refcount(mod));
1983 #endif 2027 #endif
1984 if (mod->state == MODULE_STATE_GOING) 2028 if (mod->state == MODULE_STATE_GOING)
1985 kdb_printf(" (Unloading)"); 2029 kdb_printf(" (Unloading)");
1986 else if (mod->state == MODULE_STATE_COMING) 2030 else if (mod->state == MODULE_STATE_COMING)
1987 kdb_printf(" (Loading)"); 2031 kdb_printf(" (Loading)");
1988 else 2032 else
1989 kdb_printf(" (Live)"); 2033 kdb_printf(" (Live)");
1990 kdb_printf(" 0x%p", mod->module_core); 2034 kdb_printf(" 0x%p", mod->module_core);
1991 2035
1992 #ifdef CONFIG_MODULE_UNLOAD 2036 #ifdef CONFIG_MODULE_UNLOAD
1993 { 2037 {
1994 struct module_use *use; 2038 struct module_use *use;
1995 kdb_printf(" [ "); 2039 kdb_printf(" [ ");
1996 list_for_each_entry(use, &mod->source_list, 2040 list_for_each_entry(use, &mod->source_list,
1997 source_list) 2041 source_list)
1998 kdb_printf("%s ", use->target->name); 2042 kdb_printf("%s ", use->target->name);
1999 kdb_printf("]\n"); 2043 kdb_printf("]\n");
2000 } 2044 }
2001 #endif 2045 #endif
2002 } 2046 }
2003 2047
2004 return 0; 2048 return 0;
2005 } 2049 }
2006 2050
2007 #endif /* CONFIG_MODULES */ 2051 #endif /* CONFIG_MODULES */
2008 2052
2009 /* 2053 /*
2010 * kdb_env - This function implements the 'env' command. Display the 2054 * kdb_env - This function implements the 'env' command. Display the
2011 * current environment variables. 2055 * current environment variables.
2012 */ 2056 */
2013 2057
2014 static int kdb_env(int argc, const char **argv) 2058 static int kdb_env(int argc, const char **argv)
2015 { 2059 {
2016 int i; 2060 int i;
2017 2061
2018 for (i = 0; i < __nenv; i++) { 2062 for (i = 0; i < __nenv; i++) {
2019 if (__env[i]) 2063 if (__env[i])
2020 kdb_printf("%s\n", __env[i]); 2064 kdb_printf("%s\n", __env[i]);
2021 } 2065 }
2022 2066
2023 if (KDB_DEBUG(MASK)) 2067 if (KDB_DEBUG(MASK))
2024 kdb_printf("KDBFLAGS=0x%x\n", kdb_flags); 2068 kdb_printf("KDBFLAGS=0x%x\n", kdb_flags);
2025 2069
2026 return 0; 2070 return 0;
2027 } 2071 }
2028 2072
2029 #ifdef CONFIG_PRINTK 2073 #ifdef CONFIG_PRINTK
2030 /* 2074 /*
2031 * kdb_dmesg - This function implements the 'dmesg' command to display 2075 * kdb_dmesg - This function implements the 'dmesg' command to display
2032 * the contents of the syslog buffer. 2076 * the contents of the syslog buffer.
2033 * dmesg [lines] [adjust] 2077 * dmesg [lines] [adjust]
2034 */ 2078 */
2035 static int kdb_dmesg(int argc, const char **argv) 2079 static int kdb_dmesg(int argc, const char **argv)
2036 { 2080 {
2037 int diag; 2081 int diag;
2038 int logging; 2082 int logging;
2039 int lines = 0; 2083 int lines = 0;
2040 int adjust = 0; 2084 int adjust = 0;
2041 int n = 0; 2085 int n = 0;
2042 int skip = 0; 2086 int skip = 0;
2043 struct kmsg_dumper dumper = { .active = 1 }; 2087 struct kmsg_dumper dumper = { .active = 1 };
2044 size_t len; 2088 size_t len;
2045 char buf[201]; 2089 char buf[201];
2046 2090
2047 if (argc > 2) 2091 if (argc > 2)
2048 return KDB_ARGCOUNT; 2092 return KDB_ARGCOUNT;
2049 if (argc) { 2093 if (argc) {
2050 char *cp; 2094 char *cp;
2051 lines = simple_strtol(argv[1], &cp, 0); 2095 lines = simple_strtol(argv[1], &cp, 0);
2052 if (*cp) 2096 if (*cp)
2053 lines = 0; 2097 lines = 0;
2054 if (argc > 1) { 2098 if (argc > 1) {
2055 adjust = simple_strtoul(argv[2], &cp, 0); 2099 adjust = simple_strtoul(argv[2], &cp, 0);
2056 if (*cp || adjust < 0) 2100 if (*cp || adjust < 0)
2057 adjust = 0; 2101 adjust = 0;
2058 } 2102 }
2059 } 2103 }
2060 2104
2061 /* disable LOGGING if set */ 2105 /* disable LOGGING if set */
2062 diag = kdbgetintenv("LOGGING", &logging); 2106 diag = kdbgetintenv("LOGGING", &logging);
2063 if (!diag && logging) { 2107 if (!diag && logging) {
2064 const char *setargs[] = { "set", "LOGGING", "0" }; 2108 const char *setargs[] = { "set", "LOGGING", "0" };
2065 kdb_set(2, setargs); 2109 kdb_set(2, setargs);
2066 } 2110 }
2067 2111
2068 kmsg_dump_rewind_nolock(&dumper); 2112 kmsg_dump_rewind_nolock(&dumper);
2069 while (kmsg_dump_get_line_nolock(&dumper, 1, NULL, 0, NULL)) 2113 while (kmsg_dump_get_line_nolock(&dumper, 1, NULL, 0, NULL))
2070 n++; 2114 n++;
2071 2115
2072 if (lines < 0) { 2116 if (lines < 0) {
2073 if (adjust >= n) 2117 if (adjust >= n)
2074 kdb_printf("buffer only contains %d lines, nothing " 2118 kdb_printf("buffer only contains %d lines, nothing "
2075 "printed\n", n); 2119 "printed\n", n);
2076 else if (adjust - lines >= n) 2120 else if (adjust - lines >= n)
2077 kdb_printf("buffer only contains %d lines, last %d " 2121 kdb_printf("buffer only contains %d lines, last %d "
2078 "lines printed\n", n, n - adjust); 2122 "lines printed\n", n, n - adjust);
2079 skip = adjust; 2123 skip = adjust;
2080 lines = abs(lines); 2124 lines = abs(lines);
2081 } else if (lines > 0) { 2125 } else if (lines > 0) {
2082 skip = n - lines - adjust; 2126 skip = n - lines - adjust;
2083 lines = abs(lines); 2127 lines = abs(lines);
2084 if (adjust >= n) { 2128 if (adjust >= n) {
2085 kdb_printf("buffer only contains %d lines, " 2129 kdb_printf("buffer only contains %d lines, "
2086 "nothing printed\n", n); 2130 "nothing printed\n", n);
2087 skip = n; 2131 skip = n;
2088 } else if (skip < 0) { 2132 } else if (skip < 0) {
2089 lines += skip; 2133 lines += skip;
2090 skip = 0; 2134 skip = 0;
2091 kdb_printf("buffer only contains %d lines, first " 2135 kdb_printf("buffer only contains %d lines, first "
2092 "%d lines printed\n", n, lines); 2136 "%d lines printed\n", n, lines);
2093 } 2137 }
2094 } else { 2138 } else {
2095 lines = n; 2139 lines = n;
2096 } 2140 }
2097 2141
2098 if (skip >= n || skip < 0) 2142 if (skip >= n || skip < 0)
2099 return 0; 2143 return 0;
2100 2144
2101 kmsg_dump_rewind_nolock(&dumper); 2145 kmsg_dump_rewind_nolock(&dumper);
2102 while (kmsg_dump_get_line_nolock(&dumper, 1, buf, sizeof(buf), &len)) { 2146 while (kmsg_dump_get_line_nolock(&dumper, 1, buf, sizeof(buf), &len)) {
2103 if (skip) { 2147 if (skip) {
2104 skip--; 2148 skip--;
2105 continue; 2149 continue;
2106 } 2150 }
2107 if (!lines--) 2151 if (!lines--)
2108 break; 2152 break;
2109 if (KDB_FLAG(CMD_INTERRUPT)) 2153 if (KDB_FLAG(CMD_INTERRUPT))
2110 return 0; 2154 return 0;
2111 2155
2112 kdb_printf("%.*s\n", (int)len - 1, buf); 2156 kdb_printf("%.*s\n", (int)len - 1, buf);
2113 } 2157 }
2114 2158
2115 return 0; 2159 return 0;
2116 } 2160 }
2117 #endif /* CONFIG_PRINTK */ 2161 #endif /* CONFIG_PRINTK */
2118 2162
2119 /* Make sure we balance enable/disable calls, must disable first. */ 2163 /* Make sure we balance enable/disable calls, must disable first. */
2120 static atomic_t kdb_nmi_disabled; 2164 static atomic_t kdb_nmi_disabled;
2121 2165
2122 static int kdb_disable_nmi(int argc, const char *argv[]) 2166 static int kdb_disable_nmi(int argc, const char *argv[])
2123 { 2167 {
2124 if (atomic_read(&kdb_nmi_disabled)) 2168 if (atomic_read(&kdb_nmi_disabled))
2125 return 0; 2169 return 0;
2126 atomic_set(&kdb_nmi_disabled, 1); 2170 atomic_set(&kdb_nmi_disabled, 1);
2127 arch_kgdb_ops.enable_nmi(0); 2171 arch_kgdb_ops.enable_nmi(0);
2128 return 0; 2172 return 0;
2129 } 2173 }
2130 2174
2131 static int kdb_param_enable_nmi(const char *val, const struct kernel_param *kp) 2175 static int kdb_param_enable_nmi(const char *val, const struct kernel_param *kp)
2132 { 2176 {
2133 if (!atomic_add_unless(&kdb_nmi_disabled, -1, 0)) 2177 if (!atomic_add_unless(&kdb_nmi_disabled, -1, 0))
2134 return -EINVAL; 2178 return -EINVAL;
2135 arch_kgdb_ops.enable_nmi(1); 2179 arch_kgdb_ops.enable_nmi(1);
2136 return 0; 2180 return 0;
2137 } 2181 }
2138 2182
2139 static const struct kernel_param_ops kdb_param_ops_enable_nmi = { 2183 static const struct kernel_param_ops kdb_param_ops_enable_nmi = {
2140 .set = kdb_param_enable_nmi, 2184 .set = kdb_param_enable_nmi,
2141 }; 2185 };
2142 module_param_cb(enable_nmi, &kdb_param_ops_enable_nmi, NULL, 0600); 2186 module_param_cb(enable_nmi, &kdb_param_ops_enable_nmi, NULL, 0600);
2143 2187
2144 /* 2188 /*
2145 * kdb_cpu - This function implements the 'cpu' command. 2189 * kdb_cpu - This function implements the 'cpu' command.
2146 * cpu [<cpunum>] 2190 * cpu [<cpunum>]
2147 * Returns: 2191 * Returns:
2148 * KDB_CMD_CPU for success, a kdb diagnostic if error 2192 * KDB_CMD_CPU for success, a kdb diagnostic if error
2149 */ 2193 */
2150 static void kdb_cpu_status(void) 2194 static void kdb_cpu_status(void)
2151 { 2195 {
2152 int i, start_cpu, first_print = 1; 2196 int i, start_cpu, first_print = 1;
2153 char state, prev_state = '?'; 2197 char state, prev_state = '?';
2154 2198
2155 kdb_printf("Currently on cpu %d\n", raw_smp_processor_id()); 2199 kdb_printf("Currently on cpu %d\n", raw_smp_processor_id());
2156 kdb_printf("Available cpus: "); 2200 kdb_printf("Available cpus: ");
2157 for (start_cpu = -1, i = 0; i < NR_CPUS; i++) { 2201 for (start_cpu = -1, i = 0; i < NR_CPUS; i++) {
2158 if (!cpu_online(i)) { 2202 if (!cpu_online(i)) {
2159 state = 'F'; /* cpu is offline */ 2203 state = 'F'; /* cpu is offline */
2204 } else if (!kgdb_info[i].enter_kgdb) {
2205 state = 'D'; /* cpu is online but unresponsive */
2160 } else { 2206 } else {
2161 state = ' '; /* cpu is responding to kdb */ 2207 state = ' '; /* cpu is responding to kdb */
2162 if (kdb_task_state_char(KDB_TSK(i)) == 'I') 2208 if (kdb_task_state_char(KDB_TSK(i)) == 'I')
2163 state = 'I'; /* idle task */ 2209 state = 'I'; /* idle task */
2164 } 2210 }
2165 if (state != prev_state) { 2211 if (state != prev_state) {
2166 if (prev_state != '?') { 2212 if (prev_state != '?') {
2167 if (!first_print) 2213 if (!first_print)
2168 kdb_printf(", "); 2214 kdb_printf(", ");
2169 first_print = 0; 2215 first_print = 0;
2170 kdb_printf("%d", start_cpu); 2216 kdb_printf("%d", start_cpu);
2171 if (start_cpu < i-1) 2217 if (start_cpu < i-1)
2172 kdb_printf("-%d", i-1); 2218 kdb_printf("-%d", i-1);
2173 if (prev_state != ' ') 2219 if (prev_state != ' ')
2174 kdb_printf("(%c)", prev_state); 2220 kdb_printf("(%c)", prev_state);
2175 } 2221 }
2176 prev_state = state; 2222 prev_state = state;
2177 start_cpu = i; 2223 start_cpu = i;
2178 } 2224 }
2179 } 2225 }
2180 /* print the trailing cpus, ignoring them if they are all offline */ 2226 /* print the trailing cpus, ignoring them if they are all offline */
2181 if (prev_state != 'F') { 2227 if (prev_state != 'F') {
2182 if (!first_print) 2228 if (!first_print)
2183 kdb_printf(", "); 2229 kdb_printf(", ");
2184 kdb_printf("%d", start_cpu); 2230 kdb_printf("%d", start_cpu);
2185 if (start_cpu < i-1) 2231 if (start_cpu < i-1)
2186 kdb_printf("-%d", i-1); 2232 kdb_printf("-%d", i-1);
2187 if (prev_state != ' ') 2233 if (prev_state != ' ')
2188 kdb_printf("(%c)", prev_state); 2234 kdb_printf("(%c)", prev_state);
2189 } 2235 }
2190 kdb_printf("\n"); 2236 kdb_printf("\n");
2191 } 2237 }
2192 2238
2193 static int kdb_cpu(int argc, const char **argv) 2239 static int kdb_cpu(int argc, const char **argv)
2194 { 2240 {
2195 unsigned long cpunum; 2241 unsigned long cpunum;
2196 int diag; 2242 int diag;
2197 2243
2198 if (argc == 0) { 2244 if (argc == 0) {
2199 kdb_cpu_status(); 2245 kdb_cpu_status();
2200 return 0; 2246 return 0;
2201 } 2247 }
2202 2248
2203 if (argc != 1) 2249 if (argc != 1)
2204 return KDB_ARGCOUNT; 2250 return KDB_ARGCOUNT;
2205 2251
2206 diag = kdbgetularg(argv[1], &cpunum); 2252 diag = kdbgetularg(argv[1], &cpunum);
2207 if (diag) 2253 if (diag)
2208 return diag; 2254 return diag;
2209 2255
2210 /* 2256 /*
2211 * Validate cpunum 2257 * Validate cpunum
2212 */ 2258 */
2213 if ((cpunum > NR_CPUS) || !cpu_online(cpunum)) 2259 if ((cpunum > NR_CPUS) || !kgdb_info[cpunum].enter_kgdb)
2214 return KDB_BADCPUNUM; 2260 return KDB_BADCPUNUM;
2215 2261
2216 dbg_switch_cpu = cpunum; 2262 dbg_switch_cpu = cpunum;
2217 2263
2218 /* 2264 /*
2219 * Switch to other cpu 2265 * Switch to other cpu
2220 */ 2266 */
2221 return KDB_CMD_CPU; 2267 return KDB_CMD_CPU;
2222 } 2268 }
2223 2269
2224 /* The user may not realize that ps/bta with no parameters does not print idle 2270 /* The user may not realize that ps/bta with no parameters does not print idle
2225 * or sleeping system daemon processes, so tell them how many were suppressed. 2271 * or sleeping system daemon processes, so tell them how many were suppressed.
2226 */ 2272 */
2227 void kdb_ps_suppressed(void) 2273 void kdb_ps_suppressed(void)
2228 { 2274 {
2229 int idle = 0, daemon = 0; 2275 int idle = 0, daemon = 0;
2230 unsigned long mask_I = kdb_task_state_string("I"), 2276 unsigned long mask_I = kdb_task_state_string("I"),
2231 mask_M = kdb_task_state_string("M"); 2277 mask_M = kdb_task_state_string("M");
2232 unsigned long cpu; 2278 unsigned long cpu;
2233 const struct task_struct *p, *g; 2279 const struct task_struct *p, *g;
2234 for_each_online_cpu(cpu) { 2280 for_each_online_cpu(cpu) {
2235 p = kdb_curr_task(cpu); 2281 p = kdb_curr_task(cpu);
2236 if (kdb_task_state(p, mask_I)) 2282 if (kdb_task_state(p, mask_I))
2237 ++idle; 2283 ++idle;
2238 } 2284 }
2239 kdb_do_each_thread(g, p) { 2285 kdb_do_each_thread(g, p) {
2240 if (kdb_task_state(p, mask_M)) 2286 if (kdb_task_state(p, mask_M))
2241 ++daemon; 2287 ++daemon;
2242 } kdb_while_each_thread(g, p); 2288 } kdb_while_each_thread(g, p);
2243 if (idle || daemon) { 2289 if (idle || daemon) {
2244 if (idle) 2290 if (idle)
2245 kdb_printf("%d idle process%s (state I)%s\n", 2291 kdb_printf("%d idle process%s (state I)%s\n",
2246 idle, idle == 1 ? "" : "es", 2292 idle, idle == 1 ? "" : "es",
2247 daemon ? " and " : ""); 2293 daemon ? " and " : "");
2248 if (daemon) 2294 if (daemon)
2249 kdb_printf("%d sleeping system daemon (state M) " 2295 kdb_printf("%d sleeping system daemon (state M) "
2250 "process%s", daemon, 2296 "process%s", daemon,
2251 daemon == 1 ? "" : "es"); 2297 daemon == 1 ? "" : "es");
2252 kdb_printf(" suppressed,\nuse 'ps A' to see all.\n"); 2298 kdb_printf(" suppressed,\nuse 'ps A' to see all.\n");
2253 } 2299 }
2254 } 2300 }
2255 2301
2256 /* 2302 /*
2257 * kdb_ps - This function implements the 'ps' command which shows a 2303 * kdb_ps - This function implements the 'ps' command which shows a
2258 * list of the active processes. 2304 * list of the active processes.
2259 * ps [DRSTCZEUIMA] All processes, optionally filtered by state 2305 * ps [DRSTCZEUIMA] All processes, optionally filtered by state
2260 */ 2306 */
2261 void kdb_ps1(const struct task_struct *p) 2307 void kdb_ps1(const struct task_struct *p)
2262 { 2308 {
2263 int cpu; 2309 int cpu;
2264 unsigned long tmp; 2310 unsigned long tmp;
2265 2311
2266 if (!p || probe_kernel_read(&tmp, (char *)p, sizeof(unsigned long))) 2312 if (!p || probe_kernel_read(&tmp, (char *)p, sizeof(unsigned long)))
2267 return; 2313 return;
2268 2314
2269 cpu = kdb_process_cpu(p); 2315 cpu = kdb_process_cpu(p);
2270 kdb_printf("0x%p %8d %8d %d %4d %c 0x%p %c%s\n", 2316 kdb_printf("0x%p %8d %8d %d %4d %c 0x%p %c%s\n",
2271 (void *)p, p->pid, p->parent->pid, 2317 (void *)p, p->pid, p->parent->pid,
2272 kdb_task_has_cpu(p), kdb_process_cpu(p), 2318 kdb_task_has_cpu(p), kdb_process_cpu(p),
2273 kdb_task_state_char(p), 2319 kdb_task_state_char(p),
2274 (void *)(&p->thread), 2320 (void *)(&p->thread),
2275 p == kdb_curr_task(raw_smp_processor_id()) ? '*' : ' ', 2321 p == kdb_curr_task(raw_smp_processor_id()) ? '*' : ' ',
2276 p->comm); 2322 p->comm);
2277 if (kdb_task_has_cpu(p)) { 2323 if (kdb_task_has_cpu(p)) {
2278 if (!KDB_TSK(cpu)) { 2324 if (!KDB_TSK(cpu)) {
2279 kdb_printf(" Error: no saved data for this cpu\n"); 2325 kdb_printf(" Error: no saved data for this cpu\n");
2280 } else { 2326 } else {
2281 if (KDB_TSK(cpu) != p) 2327 if (KDB_TSK(cpu) != p)
2282 kdb_printf(" Error: does not match running " 2328 kdb_printf(" Error: does not match running "
2283 "process table (0x%p)\n", KDB_TSK(cpu)); 2329 "process table (0x%p)\n", KDB_TSK(cpu));
2284 } 2330 }
2285 } 2331 }
2286 } 2332 }
2287 2333
2288 static int kdb_ps(int argc, const char **argv) 2334 static int kdb_ps(int argc, const char **argv)
2289 { 2335 {
2290 struct task_struct *g, *p; 2336 struct task_struct *g, *p;
2291 unsigned long mask, cpu; 2337 unsigned long mask, cpu;
2292 2338
2293 if (argc == 0) 2339 if (argc == 0)
2294 kdb_ps_suppressed(); 2340 kdb_ps_suppressed();
2295 kdb_printf("%-*s Pid Parent [*] cpu State %-*s Command\n", 2341 kdb_printf("%-*s Pid Parent [*] cpu State %-*s Command\n",
2296 (int)(2*sizeof(void *))+2, "Task Addr", 2342 (int)(2*sizeof(void *))+2, "Task Addr",
2297 (int)(2*sizeof(void *))+2, "Thread"); 2343 (int)(2*sizeof(void *))+2, "Thread");
2298 mask = kdb_task_state_string(argc ? argv[1] : NULL); 2344 mask = kdb_task_state_string(argc ? argv[1] : NULL);
2299 /* Run the active tasks first */ 2345 /* Run the active tasks first */
2300 for_each_online_cpu(cpu) { 2346 for_each_online_cpu(cpu) {
2301 if (KDB_FLAG(CMD_INTERRUPT)) 2347 if (KDB_FLAG(CMD_INTERRUPT))
2302 return 0; 2348 return 0;
2303 p = kdb_curr_task(cpu); 2349 p = kdb_curr_task(cpu);
2304 if (kdb_task_state(p, mask)) 2350 if (kdb_task_state(p, mask))
2305 kdb_ps1(p); 2351 kdb_ps1(p);
2306 } 2352 }
2307 kdb_printf("\n"); 2353 kdb_printf("\n");
2308 /* Now the real tasks */ 2354 /* Now the real tasks */
2309 kdb_do_each_thread(g, p) { 2355 kdb_do_each_thread(g, p) {
2310 if (KDB_FLAG(CMD_INTERRUPT)) 2356 if (KDB_FLAG(CMD_INTERRUPT))
2311 return 0; 2357 return 0;
2312 if (kdb_task_state(p, mask)) 2358 if (kdb_task_state(p, mask))
2313 kdb_ps1(p); 2359 kdb_ps1(p);
2314 } kdb_while_each_thread(g, p); 2360 } kdb_while_each_thread(g, p);
2315 2361
2316 return 0; 2362 return 0;
2317 } 2363 }
2318 2364
2319 /* 2365 /*
2320 * kdb_pid - This function implements the 'pid' command which switches 2366 * kdb_pid - This function implements the 'pid' command which switches
2321 * the currently active process. 2367 * the currently active process.
2322 * pid [<pid> | R] 2368 * pid [<pid> | R]
2323 */ 2369 */
2324 static int kdb_pid(int argc, const char **argv) 2370 static int kdb_pid(int argc, const char **argv)
2325 { 2371 {
2326 struct task_struct *p; 2372 struct task_struct *p;
2327 unsigned long val; 2373 unsigned long val;
2328 int diag; 2374 int diag;
2329 2375
2330 if (argc > 1) 2376 if (argc > 1)
2331 return KDB_ARGCOUNT; 2377 return KDB_ARGCOUNT;
2332 2378
2333 if (argc) { 2379 if (argc) {
2334 if (strcmp(argv[1], "R") == 0) { 2380 if (strcmp(argv[1], "R") == 0) {
2335 p = KDB_TSK(kdb_initial_cpu); 2381 p = KDB_TSK(kdb_initial_cpu);
2336 } else { 2382 } else {
2337 diag = kdbgetularg(argv[1], &val); 2383 diag = kdbgetularg(argv[1], &val);
2338 if (diag) 2384 if (diag)
2339 return KDB_BADINT; 2385 return KDB_BADINT;
2340 2386
2341 p = find_task_by_pid_ns((pid_t)val, &init_pid_ns); 2387 p = find_task_by_pid_ns((pid_t)val, &init_pid_ns);
2342 if (!p) { 2388 if (!p) {
2343 kdb_printf("No task with pid=%d\n", (pid_t)val); 2389 kdb_printf("No task with pid=%d\n", (pid_t)val);
2344 return 0; 2390 return 0;
2345 } 2391 }
2346 } 2392 }
2347 kdb_set_current_task(p); 2393 kdb_set_current_task(p);
2348 } 2394 }
2349 kdb_printf("KDB current process is %s(pid=%d)\n", 2395 kdb_printf("KDB current process is %s(pid=%d)\n",
2350 kdb_current_task->comm, 2396 kdb_current_task->comm,
2351 kdb_current_task->pid); 2397 kdb_current_task->pid);
2352 2398
2353 return 0; 2399 return 0;
2354 } 2400 }
2355 2401
2356 static int kdb_kgdb(int argc, const char **argv) 2402 static int kdb_kgdb(int argc, const char **argv)
2357 { 2403 {
2358 return KDB_CMD_KGDB; 2404 return KDB_CMD_KGDB;
2359 } 2405 }
2360 2406
2361 /* 2407 /*
2362 * kdb_help - This function implements the 'help' and '?' commands. 2408 * kdb_help - This function implements the 'help' and '?' commands.
2363 */ 2409 */
2364 static int kdb_help(int argc, const char **argv) 2410 static int kdb_help(int argc, const char **argv)
2365 { 2411 {
2366 kdbtab_t *kt; 2412 kdbtab_t *kt;
2367 int i; 2413 int i;
2368 2414
2369 kdb_printf("%-15.15s %-20.20s %s\n", "Command", "Usage", "Description"); 2415 kdb_printf("%-15.15s %-20.20s %s\n", "Command", "Usage", "Description");
2370 kdb_printf("-----------------------------" 2416 kdb_printf("-----------------------------"
2371 "-----------------------------\n"); 2417 "-----------------------------\n");
2372 for_each_kdbcmd(kt, i) { 2418 for_each_kdbcmd(kt, i) {
2373 char *space = ""; 2419 char *space = "";
2374 if (KDB_FLAG(CMD_INTERRUPT)) 2420 if (KDB_FLAG(CMD_INTERRUPT))
2375 return 0; 2421 return 0;
2376 if (!kt->cmd_name) 2422 if (!kt->cmd_name)
2377 continue; 2423 continue;
2424 if (!kdb_check_flags(kt->cmd_flags, kdb_cmd_enabled, true))
2425 continue;
2378 if (strlen(kt->cmd_usage) > 20) 2426 if (strlen(kt->cmd_usage) > 20)
2379 space = "\n "; 2427 space = "\n ";
2380 kdb_printf("%-15.15s %-20s%s%s\n", kt->cmd_name, 2428 kdb_printf("%-15.15s %-20s%s%s\n", kt->cmd_name,
2381 kt->cmd_usage, space, kt->cmd_help); 2429 kt->cmd_usage, space, kt->cmd_help);
2382 } 2430 }
2383 return 0; 2431 return 0;
2384 } 2432 }
2385 2433
2386 /* 2434 /*
2387 * kdb_kill - This function implements the 'kill' commands. 2435 * kdb_kill - This function implements the 'kill' commands.
2388 */ 2436 */
2389 static int kdb_kill(int argc, const char **argv) 2437 static int kdb_kill(int argc, const char **argv)
2390 { 2438 {
2391 long sig, pid; 2439 long sig, pid;
2392 char *endp; 2440 char *endp;
2393 struct task_struct *p; 2441 struct task_struct *p;
2394 struct siginfo info; 2442 struct siginfo info;
2395 2443
2396 if (argc != 2) 2444 if (argc != 2)
2397 return KDB_ARGCOUNT; 2445 return KDB_ARGCOUNT;
2398 2446
2399 sig = simple_strtol(argv[1], &endp, 0); 2447 sig = simple_strtol(argv[1], &endp, 0);
2400 if (*endp) 2448 if (*endp)
2401 return KDB_BADINT; 2449 return KDB_BADINT;
2402 if (sig >= 0) { 2450 if (sig >= 0) {
2403 kdb_printf("Invalid signal parameter.<-signal>\n"); 2451 kdb_printf("Invalid signal parameter.<-signal>\n");
2404 return 0; 2452 return 0;
2405 } 2453 }
2406 sig = -sig; 2454 sig = -sig;
2407 2455
2408 pid = simple_strtol(argv[2], &endp, 0); 2456 pid = simple_strtol(argv[2], &endp, 0);
2409 if (*endp) 2457 if (*endp)
2410 return KDB_BADINT; 2458 return KDB_BADINT;
2411 if (pid <= 0) { 2459 if (pid <= 0) {
2412 kdb_printf("Process ID must be large than 0.\n"); 2460 kdb_printf("Process ID must be large than 0.\n");
2413 return 0; 2461 return 0;
2414 } 2462 }
2415 2463
2416 /* Find the process. */ 2464 /* Find the process. */
2417 p = find_task_by_pid_ns(pid, &init_pid_ns); 2465 p = find_task_by_pid_ns(pid, &init_pid_ns);
2418 if (!p) { 2466 if (!p) {
2419 kdb_printf("The specified process isn't found.\n"); 2467 kdb_printf("The specified process isn't found.\n");
2420 return 0; 2468 return 0;
2421 } 2469 }
2422 p = p->group_leader; 2470 p = p->group_leader;
2423 info.si_signo = sig; 2471 info.si_signo = sig;
2424 info.si_errno = 0; 2472 info.si_errno = 0;
2425 info.si_code = SI_USER; 2473 info.si_code = SI_USER;
2426 info.si_pid = pid; /* same capabilities as process being signalled */ 2474 info.si_pid = pid; /* same capabilities as process being signalled */
2427 info.si_uid = 0; /* kdb has root authority */ 2475 info.si_uid = 0; /* kdb has root authority */
2428 kdb_send_sig_info(p, &info); 2476 kdb_send_sig_info(p, &info);
2429 return 0; 2477 return 0;
2430 } 2478 }
2431 2479
2432 struct kdb_tm { 2480 struct kdb_tm {
2433 int tm_sec; /* seconds */ 2481 int tm_sec; /* seconds */
2434 int tm_min; /* minutes */ 2482 int tm_min; /* minutes */
2435 int tm_hour; /* hours */ 2483 int tm_hour; /* hours */
2436 int tm_mday; /* day of the month */ 2484 int tm_mday; /* day of the month */
2437 int tm_mon; /* month */ 2485 int tm_mon; /* month */
2438 int tm_year; /* year */ 2486 int tm_year; /* year */
2439 }; 2487 };
2440 2488
2441 static void kdb_gmtime(struct timespec *tv, struct kdb_tm *tm) 2489 static void kdb_gmtime(struct timespec *tv, struct kdb_tm *tm)
2442 { 2490 {
2443 /* This will work from 1970-2099, 2100 is not a leap year */ 2491 /* This will work from 1970-2099, 2100 is not a leap year */
2444 static int mon_day[] = { 31, 29, 31, 30, 31, 30, 31, 2492 static int mon_day[] = { 31, 29, 31, 30, 31, 30, 31,
2445 31, 30, 31, 30, 31 }; 2493 31, 30, 31, 30, 31 };
2446 memset(tm, 0, sizeof(*tm)); 2494 memset(tm, 0, sizeof(*tm));
2447 tm->tm_sec = tv->tv_sec % (24 * 60 * 60); 2495 tm->tm_sec = tv->tv_sec % (24 * 60 * 60);
2448 tm->tm_mday = tv->tv_sec / (24 * 60 * 60) + 2496 tm->tm_mday = tv->tv_sec / (24 * 60 * 60) +
2449 (2 * 365 + 1); /* shift base from 1970 to 1968 */ 2497 (2 * 365 + 1); /* shift base from 1970 to 1968 */
2450 tm->tm_min = tm->tm_sec / 60 % 60; 2498 tm->tm_min = tm->tm_sec / 60 % 60;
2451 tm->tm_hour = tm->tm_sec / 60 / 60; 2499 tm->tm_hour = tm->tm_sec / 60 / 60;
2452 tm->tm_sec = tm->tm_sec % 60; 2500 tm->tm_sec = tm->tm_sec % 60;
2453 tm->tm_year = 68 + 4*(tm->tm_mday / (4*365+1)); 2501 tm->tm_year = 68 + 4*(tm->tm_mday / (4*365+1));
2454 tm->tm_mday %= (4*365+1); 2502 tm->tm_mday %= (4*365+1);
2455 mon_day[1] = 29; 2503 mon_day[1] = 29;
2456 while (tm->tm_mday >= mon_day[tm->tm_mon]) { 2504 while (tm->tm_mday >= mon_day[tm->tm_mon]) {
2457 tm->tm_mday -= mon_day[tm->tm_mon]; 2505 tm->tm_mday -= mon_day[tm->tm_mon];
2458 if (++tm->tm_mon == 12) { 2506 if (++tm->tm_mon == 12) {
2459 tm->tm_mon = 0; 2507 tm->tm_mon = 0;
2460 ++tm->tm_year; 2508 ++tm->tm_year;
2461 mon_day[1] = 28; 2509 mon_day[1] = 28;
2462 } 2510 }
2463 } 2511 }
2464 ++tm->tm_mday; 2512 ++tm->tm_mday;
2465 } 2513 }
2466 2514
2467 /* 2515 /*
2468 * Most of this code has been lifted from kernel/timer.c::sys_sysinfo(). 2516 * Most of this code has been lifted from kernel/timer.c::sys_sysinfo().
2469 * I cannot call that code directly from kdb, it has an unconditional 2517 * I cannot call that code directly from kdb, it has an unconditional
2470 * cli()/sti() and calls routines that take locks which can stop the debugger. 2518 * cli()/sti() and calls routines that take locks which can stop the debugger.
2471 */ 2519 */
2472 static void kdb_sysinfo(struct sysinfo *val) 2520 static void kdb_sysinfo(struct sysinfo *val)
2473 { 2521 {
2474 struct timespec uptime; 2522 struct timespec uptime;
2475 ktime_get_ts(&uptime); 2523 ktime_get_ts(&uptime);
2476 memset(val, 0, sizeof(*val)); 2524 memset(val, 0, sizeof(*val));
2477 val->uptime = uptime.tv_sec; 2525 val->uptime = uptime.tv_sec;
2478 val->loads[0] = avenrun[0]; 2526 val->loads[0] = avenrun[0];
2479 val->loads[1] = avenrun[1]; 2527 val->loads[1] = avenrun[1];
2480 val->loads[2] = avenrun[2]; 2528 val->loads[2] = avenrun[2];
2481 val->procs = nr_threads-1; 2529 val->procs = nr_threads-1;
2482 si_meminfo(val); 2530 si_meminfo(val);
2483 2531
2484 return; 2532 return;
2485 } 2533 }
2486 2534
2487 /* 2535 /*
2488 * kdb_summary - This function implements the 'summary' command. 2536 * kdb_summary - This function implements the 'summary' command.
2489 */ 2537 */
2490 static int kdb_summary(int argc, const char **argv) 2538 static int kdb_summary(int argc, const char **argv)
2491 { 2539 {
2492 struct timespec now; 2540 struct timespec now;
2493 struct kdb_tm tm; 2541 struct kdb_tm tm;
2494 struct sysinfo val; 2542 struct sysinfo val;
2495 2543
2496 if (argc) 2544 if (argc)
2497 return KDB_ARGCOUNT; 2545 return KDB_ARGCOUNT;
2498 2546
2499 kdb_printf("sysname %s\n", init_uts_ns.name.sysname); 2547 kdb_printf("sysname %s\n", init_uts_ns.name.sysname);
2500 kdb_printf("release %s\n", init_uts_ns.name.release); 2548 kdb_printf("release %s\n", init_uts_ns.name.release);
2501 kdb_printf("version %s\n", init_uts_ns.name.version); 2549 kdb_printf("version %s\n", init_uts_ns.name.version);
2502 kdb_printf("machine %s\n", init_uts_ns.name.machine); 2550 kdb_printf("machine %s\n", init_uts_ns.name.machine);
2503 kdb_printf("nodename %s\n", init_uts_ns.name.nodename); 2551 kdb_printf("nodename %s\n", init_uts_ns.name.nodename);
2504 kdb_printf("domainname %s\n", init_uts_ns.name.domainname); 2552 kdb_printf("domainname %s\n", init_uts_ns.name.domainname);
2505 kdb_printf("ccversion %s\n", __stringify(CCVERSION)); 2553 kdb_printf("ccversion %s\n", __stringify(CCVERSION));
2506 2554
2507 now = __current_kernel_time(); 2555 now = __current_kernel_time();
2508 kdb_gmtime(&now, &tm); 2556 kdb_gmtime(&now, &tm);
2509 kdb_printf("date %04d-%02d-%02d %02d:%02d:%02d " 2557 kdb_printf("date %04d-%02d-%02d %02d:%02d:%02d "
2510 "tz_minuteswest %d\n", 2558 "tz_minuteswest %d\n",
2511 1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday, 2559 1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday,
2512 tm.tm_hour, tm.tm_min, tm.tm_sec, 2560 tm.tm_hour, tm.tm_min, tm.tm_sec,
2513 sys_tz.tz_minuteswest); 2561 sys_tz.tz_minuteswest);
2514 2562
2515 kdb_sysinfo(&val); 2563 kdb_sysinfo(&val);
2516 kdb_printf("uptime "); 2564 kdb_printf("uptime ");
2517 if (val.uptime > (24*60*60)) { 2565 if (val.uptime > (24*60*60)) {
2518 int days = val.uptime / (24*60*60); 2566 int days = val.uptime / (24*60*60);
2519 val.uptime %= (24*60*60); 2567 val.uptime %= (24*60*60);
2520 kdb_printf("%d day%s ", days, days == 1 ? "" : "s"); 2568 kdb_printf("%d day%s ", days, days == 1 ? "" : "s");
2521 } 2569 }
2522 kdb_printf("%02ld:%02ld\n", val.uptime/(60*60), (val.uptime/60)%60); 2570 kdb_printf("%02ld:%02ld\n", val.uptime/(60*60), (val.uptime/60)%60);
2523 2571
2524 /* lifted from fs/proc/proc_misc.c::loadavg_read_proc() */ 2572 /* lifted from fs/proc/proc_misc.c::loadavg_read_proc() */
2525 2573
2526 #define LOAD_INT(x) ((x) >> FSHIFT) 2574 #define LOAD_INT(x) ((x) >> FSHIFT)
2527 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) 2575 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
2528 kdb_printf("load avg %ld.%02ld %ld.%02ld %ld.%02ld\n", 2576 kdb_printf("load avg %ld.%02ld %ld.%02ld %ld.%02ld\n",
2529 LOAD_INT(val.loads[0]), LOAD_FRAC(val.loads[0]), 2577 LOAD_INT(val.loads[0]), LOAD_FRAC(val.loads[0]),
2530 LOAD_INT(val.loads[1]), LOAD_FRAC(val.loads[1]), 2578 LOAD_INT(val.loads[1]), LOAD_FRAC(val.loads[1]),
2531 LOAD_INT(val.loads[2]), LOAD_FRAC(val.loads[2])); 2579 LOAD_INT(val.loads[2]), LOAD_FRAC(val.loads[2]));
2532 #undef LOAD_INT 2580 #undef LOAD_INT
2533 #undef LOAD_FRAC 2581 #undef LOAD_FRAC
2534 /* Display in kilobytes */ 2582 /* Display in kilobytes */
2535 #define K(x) ((x) << (PAGE_SHIFT - 10)) 2583 #define K(x) ((x) << (PAGE_SHIFT - 10))
2536 kdb_printf("\nMemTotal: %8lu kB\nMemFree: %8lu kB\n" 2584 kdb_printf("\nMemTotal: %8lu kB\nMemFree: %8lu kB\n"
2537 "Buffers: %8lu kB\n", 2585 "Buffers: %8lu kB\n",
2538 val.totalram, val.freeram, val.bufferram); 2586 val.totalram, val.freeram, val.bufferram);
2539 return 0; 2587 return 0;
2540 } 2588 }
2541 2589
2542 /* 2590 /*
2543 * kdb_per_cpu - This function implements the 'per_cpu' command. 2591 * kdb_per_cpu - This function implements the 'per_cpu' command.
2544 */ 2592 */
2545 static int kdb_per_cpu(int argc, const char **argv) 2593 static int kdb_per_cpu(int argc, const char **argv)
2546 { 2594 {
2547 char fmtstr[64]; 2595 char fmtstr[64];
2548 int cpu, diag, nextarg = 1; 2596 int cpu, diag, nextarg = 1;
2549 unsigned long addr, symaddr, val, bytesperword = 0, whichcpu = ~0UL; 2597 unsigned long addr, symaddr, val, bytesperword = 0, whichcpu = ~0UL;
2550 2598
2551 if (argc < 1 || argc > 3) 2599 if (argc < 1 || argc > 3)
2552 return KDB_ARGCOUNT; 2600 return KDB_ARGCOUNT;
2553 2601
2554 diag = kdbgetaddrarg(argc, argv, &nextarg, &symaddr, NULL, NULL); 2602 diag = kdbgetaddrarg(argc, argv, &nextarg, &symaddr, NULL, NULL);
2555 if (diag) 2603 if (diag)
2556 return diag; 2604 return diag;
2557 2605
2558 if (argc >= 2) { 2606 if (argc >= 2) {
2559 diag = kdbgetularg(argv[2], &bytesperword); 2607 diag = kdbgetularg(argv[2], &bytesperword);
2560 if (diag) 2608 if (diag)
2561 return diag; 2609 return diag;
2562 } 2610 }
2563 if (!bytesperword) 2611 if (!bytesperword)
2564 bytesperword = KDB_WORD_SIZE; 2612 bytesperword = KDB_WORD_SIZE;
2565 else if (bytesperword > KDB_WORD_SIZE) 2613 else if (bytesperword > KDB_WORD_SIZE)
2566 return KDB_BADWIDTH; 2614 return KDB_BADWIDTH;
2567 sprintf(fmtstr, "%%0%dlx ", (int)(2*bytesperword)); 2615 sprintf(fmtstr, "%%0%dlx ", (int)(2*bytesperword));
2568 if (argc >= 3) { 2616 if (argc >= 3) {
2569 diag = kdbgetularg(argv[3], &whichcpu); 2617 diag = kdbgetularg(argv[3], &whichcpu);
2570 if (diag) 2618 if (diag)
2571 return diag; 2619 return diag;
2572 if (!cpu_online(whichcpu)) { 2620 if (!cpu_online(whichcpu)) {
2573 kdb_printf("cpu %ld is not online\n", whichcpu); 2621 kdb_printf("cpu %ld is not online\n", whichcpu);
2574 return KDB_BADCPUNUM; 2622 return KDB_BADCPUNUM;
2575 } 2623 }
2576 } 2624 }
2577 2625
2578 /* Most architectures use __per_cpu_offset[cpu], some use 2626 /* Most architectures use __per_cpu_offset[cpu], some use
2579 * __per_cpu_offset(cpu), smp has no __per_cpu_offset. 2627 * __per_cpu_offset(cpu), smp has no __per_cpu_offset.
2580 */ 2628 */
2581 #ifdef __per_cpu_offset 2629 #ifdef __per_cpu_offset
2582 #define KDB_PCU(cpu) __per_cpu_offset(cpu) 2630 #define KDB_PCU(cpu) __per_cpu_offset(cpu)
2583 #else 2631 #else
2584 #ifdef CONFIG_SMP 2632 #ifdef CONFIG_SMP
2585 #define KDB_PCU(cpu) __per_cpu_offset[cpu] 2633 #define KDB_PCU(cpu) __per_cpu_offset[cpu]
2586 #else 2634 #else
2587 #define KDB_PCU(cpu) 0 2635 #define KDB_PCU(cpu) 0
2588 #endif 2636 #endif
2589 #endif 2637 #endif
2590 for_each_online_cpu(cpu) { 2638 for_each_online_cpu(cpu) {
2591 if (KDB_FLAG(CMD_INTERRUPT)) 2639 if (KDB_FLAG(CMD_INTERRUPT))
2592 return 0; 2640 return 0;
2593 2641
2594 if (whichcpu != ~0UL && whichcpu != cpu) 2642 if (whichcpu != ~0UL && whichcpu != cpu)
2595 continue; 2643 continue;
2596 addr = symaddr + KDB_PCU(cpu); 2644 addr = symaddr + KDB_PCU(cpu);
2597 diag = kdb_getword(&val, addr, bytesperword); 2645 diag = kdb_getword(&val, addr, bytesperword);
2598 if (diag) { 2646 if (diag) {
2599 kdb_printf("%5d " kdb_bfd_vma_fmt0 " - unable to " 2647 kdb_printf("%5d " kdb_bfd_vma_fmt0 " - unable to "
2600 "read, diag=%d\n", cpu, addr, diag); 2648 "read, diag=%d\n", cpu, addr, diag);
2601 continue; 2649 continue;
2602 } 2650 }
2603 kdb_printf("%5d ", cpu); 2651 kdb_printf("%5d ", cpu);
2604 kdb_md_line(fmtstr, addr, 2652 kdb_md_line(fmtstr, addr,
2605 bytesperword == KDB_WORD_SIZE, 2653 bytesperword == KDB_WORD_SIZE,
2606 1, bytesperword, 1, 1, 0); 2654 1, bytesperword, 1, 1, 0);
2607 } 2655 }
2608 #undef KDB_PCU 2656 #undef KDB_PCU
2609 return 0; 2657 return 0;
2610 } 2658 }
2611 2659
2612 /* 2660 /*
2613 * display help for the use of cmd | grep pattern 2661 * display help for the use of cmd | grep pattern
2614 */ 2662 */
2615 static int kdb_grep_help(int argc, const char **argv) 2663 static int kdb_grep_help(int argc, const char **argv)
2616 { 2664 {
2617 kdb_printf("Usage of cmd args | grep pattern:\n"); 2665 kdb_printf("Usage of cmd args | grep pattern:\n");
2618 kdb_printf(" Any command's output may be filtered through an "); 2666 kdb_printf(" Any command's output may be filtered through an ");
2619 kdb_printf("emulated 'pipe'.\n"); 2667 kdb_printf("emulated 'pipe'.\n");
2620 kdb_printf(" 'grep' is just a key word.\n"); 2668 kdb_printf(" 'grep' is just a key word.\n");
2621 kdb_printf(" The pattern may include a very limited set of " 2669 kdb_printf(" The pattern may include a very limited set of "
2622 "metacharacters:\n"); 2670 "metacharacters:\n");
2623 kdb_printf(" pattern or ^pattern or pattern$ or ^pattern$\n"); 2671 kdb_printf(" pattern or ^pattern or pattern$ or ^pattern$\n");
2624 kdb_printf(" And if there are spaces in the pattern, you may " 2672 kdb_printf(" And if there are spaces in the pattern, you may "
2625 "quote it:\n"); 2673 "quote it:\n");
2626 kdb_printf(" \"pat tern\" or \"^pat tern\" or \"pat tern$\"" 2674 kdb_printf(" \"pat tern\" or \"^pat tern\" or \"pat tern$\""
2627 " or \"^pat tern$\"\n"); 2675 " or \"^pat tern$\"\n");
2628 return 0; 2676 return 0;
2629 } 2677 }
2630 2678
2631 /* 2679 /*
2632 * kdb_register_repeat - This function is used to register a kernel 2680 * kdb_register_flags - This function is used to register a kernel
2633 * debugger command. 2681 * debugger command.
2634 * Inputs: 2682 * Inputs:
2635 * cmd Command name 2683 * cmd Command name
2636 * func Function to execute the command 2684 * func Function to execute the command
2637 * usage A simple usage string showing arguments 2685 * usage A simple usage string showing arguments
2638 * help A simple help string describing command 2686 * help A simple help string describing command
2639 * repeat Does the command auto repeat on enter? 2687 * repeat Does the command auto repeat on enter?
2640 * Returns: 2688 * Returns:
2641 * zero for success, one if a duplicate command. 2689 * zero for success, one if a duplicate command.
2642 */ 2690 */
2643 #define kdb_command_extend 50 /* arbitrary */ 2691 #define kdb_command_extend 50 /* arbitrary */
2644 int kdb_register_repeat(char *cmd, 2692 int kdb_register_flags(char *cmd,
2645 kdb_func_t func, 2693 kdb_func_t func,
2646 char *usage, 2694 char *usage,
2647 char *help, 2695 char *help,
2648 short minlen, 2696 short minlen,
2649 kdb_repeat_t repeat) 2697 kdb_cmdflags_t flags)
2650 { 2698 {
2651 int i; 2699 int i;
2652 kdbtab_t *kp; 2700 kdbtab_t *kp;
2653 2701
2654 /* 2702 /*
2655 * Brute force method to determine duplicates 2703 * Brute force method to determine duplicates
2656 */ 2704 */
2657 for_each_kdbcmd(kp, i) { 2705 for_each_kdbcmd(kp, i) {
2658 if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) { 2706 if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) {
2659 kdb_printf("Duplicate kdb command registered: " 2707 kdb_printf("Duplicate kdb command registered: "
2660 "%s, func %p help %s\n", cmd, func, help); 2708 "%s, func %p help %s\n", cmd, func, help);
2661 return 1; 2709 return 1;
2662 } 2710 }
2663 } 2711 }
2664 2712
2665 /* 2713 /*
2666 * Insert command into first available location in table 2714 * Insert command into first available location in table
2667 */ 2715 */
2668 for_each_kdbcmd(kp, i) { 2716 for_each_kdbcmd(kp, i) {
2669 if (kp->cmd_name == NULL) 2717 if (kp->cmd_name == NULL)
2670 break; 2718 break;
2671 } 2719 }
2672 2720
2673 if (i >= kdb_max_commands) { 2721 if (i >= kdb_max_commands) {
2674 kdbtab_t *new = kmalloc((kdb_max_commands - KDB_BASE_CMD_MAX + 2722 kdbtab_t *new = kmalloc((kdb_max_commands - KDB_BASE_CMD_MAX +
2675 kdb_command_extend) * sizeof(*new), GFP_KDB); 2723 kdb_command_extend) * sizeof(*new), GFP_KDB);
2676 if (!new) { 2724 if (!new) {
2677 kdb_printf("Could not allocate new kdb_command " 2725 kdb_printf("Could not allocate new kdb_command "
2678 "table\n"); 2726 "table\n");
2679 return 1; 2727 return 1;
2680 } 2728 }
2681 if (kdb_commands) { 2729 if (kdb_commands) {
2682 memcpy(new, kdb_commands, 2730 memcpy(new, kdb_commands,
2683 (kdb_max_commands - KDB_BASE_CMD_MAX) * sizeof(*new)); 2731 (kdb_max_commands - KDB_BASE_CMD_MAX) * sizeof(*new));
2684 kfree(kdb_commands); 2732 kfree(kdb_commands);
2685 } 2733 }
2686 memset(new + kdb_max_commands - KDB_BASE_CMD_MAX, 0, 2734 memset(new + kdb_max_commands - KDB_BASE_CMD_MAX, 0,
2687 kdb_command_extend * sizeof(*new)); 2735 kdb_command_extend * sizeof(*new));
2688 kdb_commands = new; 2736 kdb_commands = new;
2689 kp = kdb_commands + kdb_max_commands - KDB_BASE_CMD_MAX; 2737 kp = kdb_commands + kdb_max_commands - KDB_BASE_CMD_MAX;
2690 kdb_max_commands += kdb_command_extend; 2738 kdb_max_commands += kdb_command_extend;
2691 } 2739 }
2692 2740
2693 kp->cmd_name = cmd; 2741 kp->cmd_name = cmd;
2694 kp->cmd_func = func; 2742 kp->cmd_func = func;
2695 kp->cmd_usage = usage; 2743 kp->cmd_usage = usage;
2696 kp->cmd_help = help; 2744 kp->cmd_help = help;
2697 kp->cmd_flags = 0;
2698 kp->cmd_minlen = minlen; 2745 kp->cmd_minlen = minlen;
2699 kp->cmd_repeat = repeat; 2746 kp->cmd_flags = flags;
2700 2747
2701 return 0; 2748 return 0;
2702 } 2749 }
2703 EXPORT_SYMBOL_GPL(kdb_register_repeat); 2750 EXPORT_SYMBOL_GPL(kdb_register_flags);
2704 2751
2705 2752
2706 /* 2753 /*
2707 * kdb_register - Compatibility register function for commands that do 2754 * kdb_register - Compatibility register function for commands that do
2708 * not need to specify a repeat state. Equivalent to 2755 * not need to specify a repeat state. Equivalent to
2709 * kdb_register_repeat with KDB_REPEAT_NONE. 2756 * kdb_register_flags with flags set to 0.
2710 * Inputs: 2757 * Inputs:
2711 * cmd Command name 2758 * cmd Command name
2712 * func Function to execute the command 2759 * func Function to execute the command
2713 * usage A simple usage string showing arguments 2760 * usage A simple usage string showing arguments
2714 * help A simple help string describing command 2761 * help A simple help string describing command
2715 * Returns: 2762 * Returns:
2716 * zero for success, one if a duplicate command. 2763 * zero for success, one if a duplicate command.
2717 */ 2764 */
2718 int kdb_register(char *cmd, 2765 int kdb_register(char *cmd,
2719 kdb_func_t func, 2766 kdb_func_t func,
2720 char *usage, 2767 char *usage,
2721 char *help, 2768 char *help,
2722 short minlen) 2769 short minlen)
2723 { 2770 {
2724 return kdb_register_repeat(cmd, func, usage, help, minlen, 2771 return kdb_register_flags(cmd, func, usage, help, minlen, 0);
2725 KDB_REPEAT_NONE);
2726 } 2772 }
2727 EXPORT_SYMBOL_GPL(kdb_register); 2773 EXPORT_SYMBOL_GPL(kdb_register);
2728 2774
2729 /* 2775 /*
2730 * kdb_unregister - This function is used to unregister a kernel 2776 * kdb_unregister - This function is used to unregister a kernel
2731 * debugger command. It is generally called when a module which 2777 * debugger command. It is generally called when a module which
2732 * implements kdb commands is unloaded. 2778 * implements kdb commands is unloaded.
2733 * Inputs: 2779 * Inputs:
2734 * cmd Command name 2780 * cmd Command name
2735 * Returns: 2781 * Returns:
2736 * zero for success, one command not registered. 2782 * zero for success, one command not registered.
2737 */ 2783 */
2738 int kdb_unregister(char *cmd) 2784 int kdb_unregister(char *cmd)
2739 { 2785 {
2740 int i; 2786 int i;
2741 kdbtab_t *kp; 2787 kdbtab_t *kp;
2742 2788
2743 /* 2789 /*
2744 * find the command. 2790 * find the command.
2745 */ 2791 */
2746 for_each_kdbcmd(kp, i) { 2792 for_each_kdbcmd(kp, i) {
2747 if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) { 2793 if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) {
2748 kp->cmd_name = NULL; 2794 kp->cmd_name = NULL;
2749 return 0; 2795 return 0;
2750 } 2796 }
2751 } 2797 }
2752 2798
2753 /* Couldn't find it. */ 2799 /* Couldn't find it. */
2754 return 1; 2800 return 1;
2755 } 2801 }
2756 EXPORT_SYMBOL_GPL(kdb_unregister); 2802 EXPORT_SYMBOL_GPL(kdb_unregister);
2757 2803
2758 /* Initialize the kdb command table. */ 2804 /* Initialize the kdb command table. */
2759 static void __init kdb_inittab(void) 2805 static void __init kdb_inittab(void)
2760 { 2806 {
2761 int i; 2807 int i;
2762 kdbtab_t *kp; 2808 kdbtab_t *kp;
2763 2809
2764 for_each_kdbcmd(kp, i) 2810 for_each_kdbcmd(kp, i)
2765 kp->cmd_name = NULL; 2811 kp->cmd_name = NULL;
2766 2812
2767 kdb_register_repeat("md", kdb_md, "<vaddr>", 2813 kdb_register_flags("md", kdb_md, "<vaddr>",
2768 "Display Memory Contents, also mdWcN, e.g. md8c1", 1, 2814 "Display Memory Contents, also mdWcN, e.g. md8c1", 1,
2769 KDB_REPEAT_NO_ARGS); 2815 KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS);
2770 kdb_register_repeat("mdr", kdb_md, "<vaddr> <bytes>", 2816 kdb_register_flags("mdr", kdb_md, "<vaddr> <bytes>",
2771 "Display Raw Memory", 0, KDB_REPEAT_NO_ARGS); 2817 "Display Raw Memory", 0,
2772 kdb_register_repeat("mdp", kdb_md, "<paddr> <bytes>", 2818 KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS);
2773 "Display Physical Memory", 0, KDB_REPEAT_NO_ARGS); 2819 kdb_register_flags("mdp", kdb_md, "<paddr> <bytes>",
2774 kdb_register_repeat("mds", kdb_md, "<vaddr>", 2820 "Display Physical Memory", 0,
2775 "Display Memory Symbolically", 0, KDB_REPEAT_NO_ARGS); 2821 KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS);
2776 kdb_register_repeat("mm", kdb_mm, "<vaddr> <contents>", 2822 kdb_register_flags("mds", kdb_md, "<vaddr>",
2777 "Modify Memory Contents", 0, KDB_REPEAT_NO_ARGS); 2823 "Display Memory Symbolically", 0,
2778 kdb_register_repeat("go", kdb_go, "[<vaddr>]", 2824 KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS);
2779 "Continue Execution", 1, KDB_REPEAT_NONE); 2825 kdb_register_flags("mm", kdb_mm, "<vaddr> <contents>",
2780 kdb_register_repeat("rd", kdb_rd, "", 2826 "Modify Memory Contents", 0,
2781 "Display Registers", 0, KDB_REPEAT_NONE); 2827 KDB_ENABLE_MEM_WRITE | KDB_REPEAT_NO_ARGS);
2782 kdb_register_repeat("rm", kdb_rm, "<reg> <contents>", 2828 kdb_register_flags("go", kdb_go, "[<vaddr>]",
2783 "Modify Registers", 0, KDB_REPEAT_NONE); 2829 "Continue Execution", 1,
2784 kdb_register_repeat("ef", kdb_ef, "<vaddr>", 2830 KDB_ENABLE_REG_WRITE | KDB_ENABLE_ALWAYS_SAFE_NO_ARGS);
2785 "Display exception frame", 0, KDB_REPEAT_NONE); 2831 kdb_register_flags("rd", kdb_rd, "",
2786 kdb_register_repeat("bt", kdb_bt, "[<vaddr>]", 2832 "Display Registers", 0,
2787 "Stack traceback", 1, KDB_REPEAT_NONE); 2833 KDB_ENABLE_REG_READ);
2788 kdb_register_repeat("btp", kdb_bt, "<pid>", 2834 kdb_register_flags("rm", kdb_rm, "<reg> <contents>",
2789 "Display stack for process <pid>", 0, KDB_REPEAT_NONE); 2835 "Modify Registers", 0,
2790 kdb_register_repeat("bta", kdb_bt, "[D|R|S|T|C|Z|E|U|I|M|A]", 2836 KDB_ENABLE_REG_WRITE);
2791 "Backtrace all processes matching state flag", 0, KDB_REPEAT_NONE); 2837 kdb_register_flags("ef", kdb_ef, "<vaddr>",
2792 kdb_register_repeat("btc", kdb_bt, "", 2838 "Display exception frame", 0,
2793 "Backtrace current process on each cpu", 0, KDB_REPEAT_NONE); 2839 KDB_ENABLE_MEM_READ);
2794 kdb_register_repeat("btt", kdb_bt, "<vaddr>", 2840 kdb_register_flags("bt", kdb_bt, "[<vaddr>]",
2841 "Stack traceback", 1,
2842 KDB_ENABLE_MEM_READ | KDB_ENABLE_INSPECT_NO_ARGS);
2843 kdb_register_flags("btp", kdb_bt, "<pid>",
2844 "Display stack for process <pid>", 0,
2845 KDB_ENABLE_INSPECT);
2846 kdb_register_flags("bta", kdb_bt, "[D|R|S|T|C|Z|E|U|I|M|A]",
2847 "Backtrace all processes matching state flag", 0,
2848 KDB_ENABLE_INSPECT);
2849 kdb_register_flags("btc", kdb_bt, "",
2850 "Backtrace current process on each cpu", 0,
2851 KDB_ENABLE_INSPECT);
2852 kdb_register_flags("btt", kdb_bt, "<vaddr>",
2795 "Backtrace process given its struct task address", 0, 2853 "Backtrace process given its struct task address", 0,
2796 KDB_REPEAT_NONE); 2854 KDB_ENABLE_MEM_READ | KDB_ENABLE_INSPECT_NO_ARGS);
2797 kdb_register_repeat("env", kdb_env, "", 2855 kdb_register_flags("env", kdb_env, "",
2798 "Show environment variables", 0, KDB_REPEAT_NONE); 2856 "Show environment variables", 0,
2799 kdb_register_repeat("set", kdb_set, "", 2857 KDB_ENABLE_ALWAYS_SAFE);
2800 "Set environment variables", 0, KDB_REPEAT_NONE); 2858 kdb_register_flags("set", kdb_set, "",
2801 kdb_register_repeat("help", kdb_help, "", 2859 "Set environment variables", 0,
2802 "Display Help Message", 1, KDB_REPEAT_NONE); 2860 KDB_ENABLE_ALWAYS_SAFE);
2803 kdb_register_repeat("?", kdb_help, "", 2861 kdb_register_flags("help", kdb_help, "",
2804 "Display Help Message", 0, KDB_REPEAT_NONE); 2862 "Display Help Message", 1,
2805 kdb_register_repeat("cpu", kdb_cpu, "<cpunum>", 2863 KDB_ENABLE_ALWAYS_SAFE);
2806 "Switch to new cpu", 0, KDB_REPEAT_NONE); 2864 kdb_register_flags("?", kdb_help, "",
2807 kdb_register_repeat("kgdb", kdb_kgdb, "", 2865 "Display Help Message", 0,
2808 "Enter kgdb mode", 0, KDB_REPEAT_NONE); 2866 KDB_ENABLE_ALWAYS_SAFE);
2809 kdb_register_repeat("ps", kdb_ps, "[<flags>|A]", 2867 kdb_register_flags("cpu", kdb_cpu, "<cpunum>",
2810 "Display active task list", 0, KDB_REPEAT_NONE); 2868 "Switch to new cpu", 0,
2811 kdb_register_repeat("pid", kdb_pid, "<pidnum>", 2869 KDB_ENABLE_ALWAYS_SAFE_NO_ARGS);
2812 "Switch to another task", 0, KDB_REPEAT_NONE); 2870 kdb_register_flags("kgdb", kdb_kgdb, "",
2813 kdb_register_repeat("reboot", kdb_reboot, "", 2871 "Enter kgdb mode", 0, 0);
2814 "Reboot the machine immediately", 0, KDB_REPEAT_NONE); 2872 kdb_register_flags("ps", kdb_ps, "[<flags>|A]",
2873 "Display active task list", 0,
2874 KDB_ENABLE_INSPECT);
2875 kdb_register_flags("pid", kdb_pid, "<pidnum>",
2876 "Switch to another task", 0,
2877 KDB_ENABLE_INSPECT);
2878 kdb_register_flags("reboot", kdb_reboot, "",
2879 "Reboot the machine immediately", 0,
2880 KDB_ENABLE_REBOOT);
2815 #if defined(CONFIG_MODULES) 2881 #if defined(CONFIG_MODULES)
2816 kdb_register_repeat("lsmod", kdb_lsmod, "", 2882 kdb_register_flags("lsmod", kdb_lsmod, "",
2817 "List loaded kernel modules", 0, KDB_REPEAT_NONE); 2883 "List loaded kernel modules", 0,
2884 KDB_ENABLE_INSPECT);
2818 #endif 2885 #endif
2819 #if defined(CONFIG_MAGIC_SYSRQ) 2886 #if defined(CONFIG_MAGIC_SYSRQ)
2820 kdb_register_repeat("sr", kdb_sr, "<key>", 2887 kdb_register_flags("sr", kdb_sr, "<key>",
2821 "Magic SysRq key", 0, KDB_REPEAT_NONE); 2888 "Magic SysRq key", 0,
2889 KDB_ENABLE_ALWAYS_SAFE);
2822 #endif 2890 #endif
2823 #if defined(CONFIG_PRINTK) 2891 #if defined(CONFIG_PRINTK)
2824 kdb_register_repeat("dmesg", kdb_dmesg, "[lines]", 2892 kdb_register_flags("dmesg", kdb_dmesg, "[lines]",
2825 "Display syslog buffer", 0, KDB_REPEAT_NONE); 2893 "Display syslog buffer", 0,
2894 KDB_ENABLE_ALWAYS_SAFE);
2826 #endif 2895 #endif
2827 if (arch_kgdb_ops.enable_nmi) { 2896 if (arch_kgdb_ops.enable_nmi) {
2828 kdb_register_repeat("disable_nmi", kdb_disable_nmi, "", 2897 kdb_register_flags("disable_nmi", kdb_disable_nmi, "",
2829 "Disable NMI entry to KDB", 0, KDB_REPEAT_NONE); 2898 "Disable NMI entry to KDB", 0,
2899 KDB_ENABLE_ALWAYS_SAFE);
2830 } 2900 }
2831 kdb_register_repeat("defcmd", kdb_defcmd, "name \"usage\" \"help\"", 2901 kdb_register_flags("defcmd", kdb_defcmd, "name \"usage\" \"help\"",
2832 "Define a set of commands, down to endefcmd", 0, KDB_REPEAT_NONE); 2902 "Define a set of commands, down to endefcmd", 0,
2833 kdb_register_repeat("kill", kdb_kill, "<-signal> <pid>", 2903 KDB_ENABLE_ALWAYS_SAFE);
2834 "Send a signal to a process", 0, KDB_REPEAT_NONE); 2904 kdb_register_flags("kill", kdb_kill, "<-signal> <pid>",
2835 kdb_register_repeat("summary", kdb_summary, "", 2905 "Send a signal to a process", 0,
2836 "Summarize the system", 4, KDB_REPEAT_NONE); 2906 KDB_ENABLE_SIGNAL);
2837 kdb_register_repeat("per_cpu", kdb_per_cpu, "<sym> [<bytes>] [<cpu>]", 2907 kdb_register_flags("summary", kdb_summary, "",
2838 "Display per_cpu variables", 3, KDB_REPEAT_NONE); 2908 "Summarize the system", 4,
2839 kdb_register_repeat("grephelp", kdb_grep_help, "", 2909 KDB_ENABLE_ALWAYS_SAFE);
2840 "Display help on | grep", 0, KDB_REPEAT_NONE); 2910 kdb_register_flags("per_cpu", kdb_per_cpu, "<sym> [<bytes>] [<cpu>]",
2911 "Display per_cpu variables", 3,
2912 KDB_ENABLE_MEM_READ);
2913 kdb_register_flags("grephelp", kdb_grep_help, "",
2914 "Display help on | grep", 0,
2915 KDB_ENABLE_ALWAYS_SAFE);
2841 } 2916 }
2842 2917
2843 /* Execute any commands defined in kdb_cmds. */ 2918 /* Execute any commands defined in kdb_cmds. */
2844 static void __init kdb_cmd_init(void) 2919 static void __init kdb_cmd_init(void)
2845 { 2920 {
2846 int i, diag; 2921 int i, diag;
2847 for (i = 0; kdb_cmds[i]; ++i) { 2922 for (i = 0; kdb_cmds[i]; ++i) {
2848 diag = kdb_parse(kdb_cmds[i]); 2923 diag = kdb_parse(kdb_cmds[i]);
2849 if (diag) 2924 if (diag)
2850 kdb_printf("kdb command %s failed, kdb diag %d\n", 2925 kdb_printf("kdb command %s failed, kdb diag %d\n",
2851 kdb_cmds[i], diag); 2926 kdb_cmds[i], diag);
2852 } 2927 }
2853 if (defcmd_in_progress) { 2928 if (defcmd_in_progress) {
2854 kdb_printf("Incomplete 'defcmd' set, forcing endefcmd\n"); 2929 kdb_printf("Incomplete 'defcmd' set, forcing endefcmd\n");
2855 kdb_parse("endefcmd"); 2930 kdb_parse("endefcmd");
2856 } 2931 }
2857 } 2932 }
2858 2933
2859 /* Initialize kdb_printf, breakpoint tables and kdb state */ 2934 /* Initialize kdb_printf, breakpoint tables and kdb state */
2860 void __init kdb_init(int lvl) 2935 void __init kdb_init(int lvl)
2861 { 2936 {
2862 static int kdb_init_lvl = KDB_NOT_INITIALIZED; 2937 static int kdb_init_lvl = KDB_NOT_INITIALIZED;
2863 int i; 2938 int i;
2864 2939
2865 if (kdb_init_lvl == KDB_INIT_FULL || lvl <= kdb_init_lvl) 2940 if (kdb_init_lvl == KDB_INIT_FULL || lvl <= kdb_init_lvl)
2866 return; 2941 return;
2867 for (i = kdb_init_lvl; i < lvl; i++) { 2942 for (i = kdb_init_lvl; i < lvl; i++) {
2868 switch (i) { 2943 switch (i) {
2869 case KDB_NOT_INITIALIZED: 2944 case KDB_NOT_INITIALIZED:
2870 kdb_inittab(); /* Initialize Command Table */ 2945 kdb_inittab(); /* Initialize Command Table */
2871 kdb_initbptab(); /* Initialize Breakpoints */ 2946 kdb_initbptab(); /* Initialize Breakpoints */
kernel/debug/kdb/kdb_private.h
1 #ifndef _KDBPRIVATE_H 1 #ifndef _KDBPRIVATE_H
2 #define _KDBPRIVATE_H 2 #define _KDBPRIVATE_H
3 3
4 /* 4 /*
5 * Kernel Debugger Architecture Independent Private Headers 5 * Kernel Debugger Architecture Independent Private Headers
6 * 6 *
7 * This file is subject to the terms and conditions of the GNU General Public 7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive 8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details. 9 * for more details.
10 * 10 *
11 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved. 11 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
12 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. 12 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
13 */ 13 */
14 14
15 #include <linux/kgdb.h> 15 #include <linux/kgdb.h>
16 #include "../debug_core.h" 16 #include "../debug_core.h"
17 17
18 /* Kernel Debugger Command codes. Must not overlap with error codes. */ 18 /* Kernel Debugger Command codes. Must not overlap with error codes. */
19 #define KDB_CMD_GO (-1001) 19 #define KDB_CMD_GO (-1001)
20 #define KDB_CMD_CPU (-1002) 20 #define KDB_CMD_CPU (-1002)
21 #define KDB_CMD_SS (-1003) 21 #define KDB_CMD_SS (-1003)
22 #define KDB_CMD_KGDB (-1005) 22 #define KDB_CMD_KGDB (-1005)
23 23
24 /* Internal debug flags */ 24 /* Internal debug flags */
25 #define KDB_DEBUG_FLAG_BP 0x0002 /* Breakpoint subsystem debug */ 25 #define KDB_DEBUG_FLAG_BP 0x0002 /* Breakpoint subsystem debug */
26 #define KDB_DEBUG_FLAG_BB_SUMM 0x0004 /* Basic block analysis, summary only */ 26 #define KDB_DEBUG_FLAG_BB_SUMM 0x0004 /* Basic block analysis, summary only */
27 #define KDB_DEBUG_FLAG_AR 0x0008 /* Activation record, generic */ 27 #define KDB_DEBUG_FLAG_AR 0x0008 /* Activation record, generic */
28 #define KDB_DEBUG_FLAG_ARA 0x0010 /* Activation record, arch specific */ 28 #define KDB_DEBUG_FLAG_ARA 0x0010 /* Activation record, arch specific */
29 #define KDB_DEBUG_FLAG_BB 0x0020 /* All basic block analysis */ 29 #define KDB_DEBUG_FLAG_BB 0x0020 /* All basic block analysis */
30 #define KDB_DEBUG_FLAG_STATE 0x0040 /* State flags */ 30 #define KDB_DEBUG_FLAG_STATE 0x0040 /* State flags */
31 #define KDB_DEBUG_FLAG_MASK 0xffff /* All debug flags */ 31 #define KDB_DEBUG_FLAG_MASK 0xffff /* All debug flags */
32 #define KDB_DEBUG_FLAG_SHIFT 16 /* Shift factor for dbflags */ 32 #define KDB_DEBUG_FLAG_SHIFT 16 /* Shift factor for dbflags */
33 33
34 #define KDB_DEBUG(flag) (kdb_flags & \ 34 #define KDB_DEBUG(flag) (kdb_flags & \
35 (KDB_DEBUG_FLAG_##flag << KDB_DEBUG_FLAG_SHIFT)) 35 (KDB_DEBUG_FLAG_##flag << KDB_DEBUG_FLAG_SHIFT))
36 #define KDB_DEBUG_STATE(text, value) if (KDB_DEBUG(STATE)) \ 36 #define KDB_DEBUG_STATE(text, value) if (KDB_DEBUG(STATE)) \
37 kdb_print_state(text, value) 37 kdb_print_state(text, value)
38 38
39 #if BITS_PER_LONG == 32 39 #if BITS_PER_LONG == 32
40 40
41 #define KDB_PLATFORM_ENV "BYTESPERWORD=4" 41 #define KDB_PLATFORM_ENV "BYTESPERWORD=4"
42 42
43 #define kdb_machreg_fmt "0x%lx" 43 #define kdb_machreg_fmt "0x%lx"
44 #define kdb_machreg_fmt0 "0x%08lx" 44 #define kdb_machreg_fmt0 "0x%08lx"
45 #define kdb_bfd_vma_fmt "0x%lx" 45 #define kdb_bfd_vma_fmt "0x%lx"
46 #define kdb_bfd_vma_fmt0 "0x%08lx" 46 #define kdb_bfd_vma_fmt0 "0x%08lx"
47 #define kdb_elfw_addr_fmt "0x%x" 47 #define kdb_elfw_addr_fmt "0x%x"
48 #define kdb_elfw_addr_fmt0 "0x%08x" 48 #define kdb_elfw_addr_fmt0 "0x%08x"
49 #define kdb_f_count_fmt "%d" 49 #define kdb_f_count_fmt "%d"
50 50
51 #elif BITS_PER_LONG == 64 51 #elif BITS_PER_LONG == 64
52 52
53 #define KDB_PLATFORM_ENV "BYTESPERWORD=8" 53 #define KDB_PLATFORM_ENV "BYTESPERWORD=8"
54 54
55 #define kdb_machreg_fmt "0x%lx" 55 #define kdb_machreg_fmt "0x%lx"
56 #define kdb_machreg_fmt0 "0x%016lx" 56 #define kdb_machreg_fmt0 "0x%016lx"
57 #define kdb_bfd_vma_fmt "0x%lx" 57 #define kdb_bfd_vma_fmt "0x%lx"
58 #define kdb_bfd_vma_fmt0 "0x%016lx" 58 #define kdb_bfd_vma_fmt0 "0x%016lx"
59 #define kdb_elfw_addr_fmt "0x%x" 59 #define kdb_elfw_addr_fmt "0x%x"
60 #define kdb_elfw_addr_fmt0 "0x%016x" 60 #define kdb_elfw_addr_fmt0 "0x%016x"
61 #define kdb_f_count_fmt "%ld" 61 #define kdb_f_count_fmt "%ld"
62 62
63 #endif 63 #endif
64 64
65 /* 65 /*
66 * KDB_MAXBPT describes the total number of breakpoints 66 * KDB_MAXBPT describes the total number of breakpoints
67 * supported by this architecure. 67 * supported by this architecure.
68 */ 68 */
69 #define KDB_MAXBPT 16 69 #define KDB_MAXBPT 16
70 70
71 /* Symbol table format returned by kallsyms. */ 71 /* Symbol table format returned by kallsyms. */
72 typedef struct __ksymtab { 72 typedef struct __ksymtab {
73 unsigned long value; /* Address of symbol */ 73 unsigned long value; /* Address of symbol */
74 const char *mod_name; /* Module containing symbol or 74 const char *mod_name; /* Module containing symbol or
75 * "kernel" */ 75 * "kernel" */
76 unsigned long mod_start; 76 unsigned long mod_start;
77 unsigned long mod_end; 77 unsigned long mod_end;
78 const char *sec_name; /* Section containing symbol */ 78 const char *sec_name; /* Section containing symbol */
79 unsigned long sec_start; 79 unsigned long sec_start;
80 unsigned long sec_end; 80 unsigned long sec_end;
81 const char *sym_name; /* Full symbol name, including 81 const char *sym_name; /* Full symbol name, including
82 * any version */ 82 * any version */
83 unsigned long sym_start; 83 unsigned long sym_start;
84 unsigned long sym_end; 84 unsigned long sym_end;
85 } kdb_symtab_t; 85 } kdb_symtab_t;
86 extern int kallsyms_symbol_next(char *prefix_name, int flag); 86 extern int kallsyms_symbol_next(char *prefix_name, int flag);
87 extern int kallsyms_symbol_complete(char *prefix_name, int max_len); 87 extern int kallsyms_symbol_complete(char *prefix_name, int max_len);
88 88
89 /* Exported Symbols for kernel loadable modules to use. */ 89 /* Exported Symbols for kernel loadable modules to use. */
90 extern int kdb_getarea_size(void *, unsigned long, size_t); 90 extern int kdb_getarea_size(void *, unsigned long, size_t);
91 extern int kdb_putarea_size(unsigned long, void *, size_t); 91 extern int kdb_putarea_size(unsigned long, void *, size_t);
92 92
93 /* 93 /*
94 * Like get_user and put_user, kdb_getarea and kdb_putarea take variable 94 * Like get_user and put_user, kdb_getarea and kdb_putarea take variable
95 * names, not pointers. The underlying *_size functions take pointers. 95 * names, not pointers. The underlying *_size functions take pointers.
96 */ 96 */
97 #define kdb_getarea(x, addr) kdb_getarea_size(&(x), addr, sizeof((x))) 97 #define kdb_getarea(x, addr) kdb_getarea_size(&(x), addr, sizeof((x)))
98 #define kdb_putarea(addr, x) kdb_putarea_size(addr, &(x), sizeof((x))) 98 #define kdb_putarea(addr, x) kdb_putarea_size(addr, &(x), sizeof((x)))
99 99
100 extern int kdb_getphysword(unsigned long *word, 100 extern int kdb_getphysword(unsigned long *word,
101 unsigned long addr, size_t size); 101 unsigned long addr, size_t size);
102 extern int kdb_getword(unsigned long *, unsigned long, size_t); 102 extern int kdb_getword(unsigned long *, unsigned long, size_t);
103 extern int kdb_putword(unsigned long, unsigned long, size_t); 103 extern int kdb_putword(unsigned long, unsigned long, size_t);
104 104
105 extern int kdbgetularg(const char *, unsigned long *); 105 extern int kdbgetularg(const char *, unsigned long *);
106 extern int kdbgetu64arg(const char *, u64 *); 106 extern int kdbgetu64arg(const char *, u64 *);
107 extern char *kdbgetenv(const char *); 107 extern char *kdbgetenv(const char *);
108 extern int kdbgetaddrarg(int, const char **, int*, unsigned long *, 108 extern int kdbgetaddrarg(int, const char **, int*, unsigned long *,
109 long *, char **); 109 long *, char **);
110 extern int kdbgetsymval(const char *, kdb_symtab_t *); 110 extern int kdbgetsymval(const char *, kdb_symtab_t *);
111 extern int kdbnearsym(unsigned long, kdb_symtab_t *); 111 extern int kdbnearsym(unsigned long, kdb_symtab_t *);
112 extern void kdbnearsym_cleanup(void); 112 extern void kdbnearsym_cleanup(void);
113 extern char *kdb_strdup(const char *str, gfp_t type); 113 extern char *kdb_strdup(const char *str, gfp_t type);
114 extern void kdb_symbol_print(unsigned long, const kdb_symtab_t *, unsigned int); 114 extern void kdb_symbol_print(unsigned long, const kdb_symtab_t *, unsigned int);
115 115
116 /* Routine for debugging the debugger state. */ 116 /* Routine for debugging the debugger state. */
117 extern void kdb_print_state(const char *, int); 117 extern void kdb_print_state(const char *, int);
118 118
119 extern int kdb_state; 119 extern int kdb_state;
120 #define KDB_STATE_KDB 0x00000001 /* Cpu is inside kdb */ 120 #define KDB_STATE_KDB 0x00000001 /* Cpu is inside kdb */
121 #define KDB_STATE_LEAVING 0x00000002 /* Cpu is leaving kdb */ 121 #define KDB_STATE_LEAVING 0x00000002 /* Cpu is leaving kdb */
122 #define KDB_STATE_CMD 0x00000004 /* Running a kdb command */ 122 #define KDB_STATE_CMD 0x00000004 /* Running a kdb command */
123 #define KDB_STATE_KDB_CONTROL 0x00000008 /* This cpu is under 123 #define KDB_STATE_KDB_CONTROL 0x00000008 /* This cpu is under
124 * kdb control */ 124 * kdb control */
125 #define KDB_STATE_HOLD_CPU 0x00000010 /* Hold this cpu inside kdb */ 125 #define KDB_STATE_HOLD_CPU 0x00000010 /* Hold this cpu inside kdb */
126 #define KDB_STATE_DOING_SS 0x00000020 /* Doing ss command */ 126 #define KDB_STATE_DOING_SS 0x00000020 /* Doing ss command */
127 #define KDB_STATE_SSBPT 0x00000080 /* Install breakpoint 127 #define KDB_STATE_SSBPT 0x00000080 /* Install breakpoint
128 * after one ss, independent of 128 * after one ss, independent of
129 * DOING_SS */ 129 * DOING_SS */
130 #define KDB_STATE_REENTRY 0x00000100 /* Valid re-entry into kdb */ 130 #define KDB_STATE_REENTRY 0x00000100 /* Valid re-entry into kdb */
131 #define KDB_STATE_SUPPRESS 0x00000200 /* Suppress error messages */ 131 #define KDB_STATE_SUPPRESS 0x00000200 /* Suppress error messages */
132 #define KDB_STATE_PAGER 0x00000400 /* pager is available */ 132 #define KDB_STATE_PAGER 0x00000400 /* pager is available */
133 #define KDB_STATE_GO_SWITCH 0x00000800 /* go is switching 133 #define KDB_STATE_GO_SWITCH 0x00000800 /* go is switching
134 * back to initial cpu */ 134 * back to initial cpu */
135 #define KDB_STATE_PRINTF_LOCK 0x00001000 /* Holds kdb_printf lock */ 135 #define KDB_STATE_PRINTF_LOCK 0x00001000 /* Holds kdb_printf lock */
136 #define KDB_STATE_WAIT_IPI 0x00002000 /* Waiting for kdb_ipi() NMI */ 136 #define KDB_STATE_WAIT_IPI 0x00002000 /* Waiting for kdb_ipi() NMI */
137 #define KDB_STATE_RECURSE 0x00004000 /* Recursive entry to kdb */ 137 #define KDB_STATE_RECURSE 0x00004000 /* Recursive entry to kdb */
138 #define KDB_STATE_IP_ADJUSTED 0x00008000 /* Restart IP has been 138 #define KDB_STATE_IP_ADJUSTED 0x00008000 /* Restart IP has been
139 * adjusted */ 139 * adjusted */
140 #define KDB_STATE_GO1 0x00010000 /* go only releases one cpu */ 140 #define KDB_STATE_GO1 0x00010000 /* go only releases one cpu */
141 #define KDB_STATE_KEYBOARD 0x00020000 /* kdb entered via 141 #define KDB_STATE_KEYBOARD 0x00020000 /* kdb entered via
142 * keyboard on this cpu */ 142 * keyboard on this cpu */
143 #define KDB_STATE_KEXEC 0x00040000 /* kexec issued */ 143 #define KDB_STATE_KEXEC 0x00040000 /* kexec issued */
144 #define KDB_STATE_DOING_KGDB 0x00080000 /* kgdb enter now issued */ 144 #define KDB_STATE_DOING_KGDB 0x00080000 /* kgdb enter now issued */
145 #define KDB_STATE_KGDB_TRANS 0x00200000 /* Transition to kgdb */ 145 #define KDB_STATE_KGDB_TRANS 0x00200000 /* Transition to kgdb */
146 #define KDB_STATE_ARCH 0xff000000 /* Reserved for arch 146 #define KDB_STATE_ARCH 0xff000000 /* Reserved for arch
147 * specific use */ 147 * specific use */
148 148
149 #define KDB_STATE(flag) (kdb_state & KDB_STATE_##flag) 149 #define KDB_STATE(flag) (kdb_state & KDB_STATE_##flag)
150 #define KDB_STATE_SET(flag) ((void)(kdb_state |= KDB_STATE_##flag)) 150 #define KDB_STATE_SET(flag) ((void)(kdb_state |= KDB_STATE_##flag))
151 #define KDB_STATE_CLEAR(flag) ((void)(kdb_state &= ~KDB_STATE_##flag)) 151 #define KDB_STATE_CLEAR(flag) ((void)(kdb_state &= ~KDB_STATE_##flag))
152 152
153 extern int kdb_nextline; /* Current number of lines displayed */ 153 extern int kdb_nextline; /* Current number of lines displayed */
154 154
155 typedef struct _kdb_bp { 155 typedef struct _kdb_bp {
156 unsigned long bp_addr; /* Address breakpoint is present at */ 156 unsigned long bp_addr; /* Address breakpoint is present at */
157 unsigned int bp_free:1; /* This entry is available */ 157 unsigned int bp_free:1; /* This entry is available */
158 unsigned int bp_enabled:1; /* Breakpoint is active in register */ 158 unsigned int bp_enabled:1; /* Breakpoint is active in register */
159 unsigned int bp_type:4; /* Uses hardware register */ 159 unsigned int bp_type:4; /* Uses hardware register */
160 unsigned int bp_installed:1; /* Breakpoint is installed */ 160 unsigned int bp_installed:1; /* Breakpoint is installed */
161 unsigned int bp_delay:1; /* Do delayed bp handling */ 161 unsigned int bp_delay:1; /* Do delayed bp handling */
162 unsigned int bp_delayed:1; /* Delayed breakpoint */ 162 unsigned int bp_delayed:1; /* Delayed breakpoint */
163 unsigned int bph_length; /* HW break length */ 163 unsigned int bph_length; /* HW break length */
164 } kdb_bp_t; 164 } kdb_bp_t;
165 165
166 #ifdef CONFIG_KGDB_KDB 166 #ifdef CONFIG_KGDB_KDB
167 extern kdb_bp_t kdb_breakpoints[/* KDB_MAXBPT */]; 167 extern kdb_bp_t kdb_breakpoints[/* KDB_MAXBPT */];
168 168
169 /* The KDB shell command table */ 169 /* The KDB shell command table */
170 typedef struct _kdbtab { 170 typedef struct _kdbtab {
171 char *cmd_name; /* Command name */ 171 char *cmd_name; /* Command name */
172 kdb_func_t cmd_func; /* Function to execute command */ 172 kdb_func_t cmd_func; /* Function to execute command */
173 char *cmd_usage; /* Usage String for this command */ 173 char *cmd_usage; /* Usage String for this command */
174 char *cmd_help; /* Help message for this command */ 174 char *cmd_help; /* Help message for this command */
175 short cmd_flags; /* Parsing flags */
176 short cmd_minlen; /* Minimum legal # command 175 short cmd_minlen; /* Minimum legal # command
177 * chars required */ 176 * chars required */
178 kdb_repeat_t cmd_repeat; /* Does command auto repeat on enter? */ 177 kdb_cmdflags_t cmd_flags; /* Command behaviour flags */
179 } kdbtab_t; 178 } kdbtab_t;
180 179
181 extern int kdb_bt(int, const char **); /* KDB display back trace */ 180 extern int kdb_bt(int, const char **); /* KDB display back trace */
182 181
183 /* KDB breakpoint management functions */ 182 /* KDB breakpoint management functions */
184 extern void kdb_initbptab(void); 183 extern void kdb_initbptab(void);
185 extern void kdb_bp_install(struct pt_regs *); 184 extern void kdb_bp_install(struct pt_regs *);
186 extern void kdb_bp_remove(void); 185 extern void kdb_bp_remove(void);
187 186
188 typedef enum { 187 typedef enum {
189 KDB_DB_BPT, /* Breakpoint */ 188 KDB_DB_BPT, /* Breakpoint */
190 KDB_DB_SS, /* Single-step trap */ 189 KDB_DB_SS, /* Single-step trap */
191 KDB_DB_SSBPT, /* Single step over breakpoint */ 190 KDB_DB_SSBPT, /* Single step over breakpoint */
192 KDB_DB_NOBPT /* Spurious breakpoint */ 191 KDB_DB_NOBPT /* Spurious breakpoint */
193 } kdb_dbtrap_t; 192 } kdb_dbtrap_t;
194 193
195 extern int kdb_main_loop(kdb_reason_t, kdb_reason_t, 194 extern int kdb_main_loop(kdb_reason_t, kdb_reason_t,
196 int, kdb_dbtrap_t, struct pt_regs *); 195 int, kdb_dbtrap_t, struct pt_regs *);
197 196
198 /* Miscellaneous functions and data areas */ 197 /* Miscellaneous functions and data areas */
199 extern int kdb_grepping_flag; 198 extern int kdb_grepping_flag;
200 extern char kdb_grep_string[]; 199 extern char kdb_grep_string[];
201 extern int kdb_grep_leading; 200 extern int kdb_grep_leading;
202 extern int kdb_grep_trailing; 201 extern int kdb_grep_trailing;
203 extern char *kdb_cmds[]; 202 extern char *kdb_cmds[];
204 extern unsigned long kdb_task_state_string(const char *); 203 extern unsigned long kdb_task_state_string(const char *);
205 extern char kdb_task_state_char (const struct task_struct *); 204 extern char kdb_task_state_char (const struct task_struct *);
206 extern unsigned long kdb_task_state(const struct task_struct *p, 205 extern unsigned long kdb_task_state(const struct task_struct *p,
207 unsigned long mask); 206 unsigned long mask);
208 extern void kdb_ps_suppressed(void); 207 extern void kdb_ps_suppressed(void);
209 extern void kdb_ps1(const struct task_struct *p); 208 extern void kdb_ps1(const struct task_struct *p);
210 extern void kdb_print_nameval(const char *name, unsigned long val); 209 extern void kdb_print_nameval(const char *name, unsigned long val);
211 extern void kdb_send_sig_info(struct task_struct *p, struct siginfo *info); 210 extern void kdb_send_sig_info(struct task_struct *p, struct siginfo *info);
212 extern void kdb_meminfo_proc_show(void); 211 extern void kdb_meminfo_proc_show(void);
213 extern char *kdb_getstr(char *, size_t, char *); 212 extern char *kdb_getstr(char *, size_t, char *);
214 extern void kdb_gdb_state_pass(char *buf); 213 extern void kdb_gdb_state_pass(char *buf);
215 214
216 /* Defines for kdb_symbol_print */ 215 /* Defines for kdb_symbol_print */
217 #define KDB_SP_SPACEB 0x0001 /* Space before string */ 216 #define KDB_SP_SPACEB 0x0001 /* Space before string */
218 #define KDB_SP_SPACEA 0x0002 /* Space after string */ 217 #define KDB_SP_SPACEA 0x0002 /* Space after string */
219 #define KDB_SP_PAREN 0x0004 /* Parenthesis around string */ 218 #define KDB_SP_PAREN 0x0004 /* Parenthesis around string */
220 #define KDB_SP_VALUE 0x0008 /* Print the value of the address */ 219 #define KDB_SP_VALUE 0x0008 /* Print the value of the address */
221 #define KDB_SP_SYMSIZE 0x0010 /* Print the size of the symbol */ 220 #define KDB_SP_SYMSIZE 0x0010 /* Print the size of the symbol */
222 #define KDB_SP_NEWLINE 0x0020 /* Newline after string */ 221 #define KDB_SP_NEWLINE 0x0020 /* Newline after string */
223 #define KDB_SP_DEFAULT (KDB_SP_VALUE|KDB_SP_PAREN) 222 #define KDB_SP_DEFAULT (KDB_SP_VALUE|KDB_SP_PAREN)
224 223
225 #define KDB_TSK(cpu) kgdb_info[cpu].task 224 #define KDB_TSK(cpu) kgdb_info[cpu].task
226 #define KDB_TSKREGS(cpu) kgdb_info[cpu].debuggerinfo 225 #define KDB_TSKREGS(cpu) kgdb_info[cpu].debuggerinfo
227 226
228 extern struct task_struct *kdb_curr_task(int); 227 extern struct task_struct *kdb_curr_task(int);
229 228
230 #define kdb_task_has_cpu(p) (task_curr(p)) 229 #define kdb_task_has_cpu(p) (task_curr(p))
231 230
232 /* Simplify coexistence with NPTL */ 231 /* Simplify coexistence with NPTL */
233 #define kdb_do_each_thread(g, p) do_each_thread(g, p) 232 #define kdb_do_each_thread(g, p) do_each_thread(g, p)
234 #define kdb_while_each_thread(g, p) while_each_thread(g, p) 233 #define kdb_while_each_thread(g, p) while_each_thread(g, p)
235 234
236 #define GFP_KDB (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL) 235 #define GFP_KDB (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL)
237 236
238 extern void *debug_kmalloc(size_t size, gfp_t flags); 237 extern void *debug_kmalloc(size_t size, gfp_t flags);
239 extern void debug_kfree(void *); 238 extern void debug_kfree(void *);
240 extern void debug_kusage(void); 239 extern void debug_kusage(void);
241 240
242 extern void kdb_set_current_task(struct task_struct *); 241 extern void kdb_set_current_task(struct task_struct *);
243 extern struct task_struct *kdb_current_task; 242 extern struct task_struct *kdb_current_task;
244 243
245 #ifdef CONFIG_KDB_KEYBOARD 244 #ifdef CONFIG_KDB_KEYBOARD
246 extern void kdb_kbd_cleanup_state(void); 245 extern void kdb_kbd_cleanup_state(void);
247 #else /* ! CONFIG_KDB_KEYBOARD */ 246 #else /* ! CONFIG_KDB_KEYBOARD */
248 #define kdb_kbd_cleanup_state() 247 #define kdb_kbd_cleanup_state()
249 #endif /* ! CONFIG_KDB_KEYBOARD */ 248 #endif /* ! CONFIG_KDB_KEYBOARD */
250 249
251 #ifdef CONFIG_MODULES 250 #ifdef CONFIG_MODULES
252 extern struct list_head *kdb_modules; 251 extern struct list_head *kdb_modules;
253 #endif /* CONFIG_MODULES */ 252 #endif /* CONFIG_MODULES */
254 253
255 extern char kdb_prompt_str[]; 254 extern char kdb_prompt_str[];
256 255
257 #define KDB_WORD_SIZE ((int)sizeof(unsigned long)) 256 #define KDB_WORD_SIZE ((int)sizeof(unsigned long))
258 257
259 #endif /* CONFIG_KGDB_KDB */ 258 #endif /* CONFIG_KGDB_KDB */
260 #endif /* !_KDBPRIVATE_H */ 259 #endif /* !_KDBPRIVATE_H */
261 260
kernel/trace/trace_kdb.c
1 /* 1 /*
2 * kdb helper for dumping the ftrace buffer 2 * kdb helper for dumping the ftrace buffer
3 * 3 *
4 * Copyright (C) 2010 Jason Wessel <jason.wessel@windriver.com> 4 * Copyright (C) 2010 Jason Wessel <jason.wessel@windriver.com>
5 * 5 *
6 * ftrace_dump_buf based on ftrace_dump: 6 * ftrace_dump_buf based on ftrace_dump:
7 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> 7 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
8 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> 8 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
9 * 9 *
10 */ 10 */
11 #include <linux/init.h> 11 #include <linux/init.h>
12 #include <linux/kgdb.h> 12 #include <linux/kgdb.h>
13 #include <linux/kdb.h> 13 #include <linux/kdb.h>
14 #include <linux/ftrace.h> 14 #include <linux/ftrace.h>
15 15
16 #include "trace.h" 16 #include "trace.h"
17 #include "trace_output.h" 17 #include "trace_output.h"
18 18
19 static void ftrace_dump_buf(int skip_lines, long cpu_file) 19 static void ftrace_dump_buf(int skip_lines, long cpu_file)
20 { 20 {
21 /* use static because iter can be a bit big for the stack */ 21 /* use static because iter can be a bit big for the stack */
22 static struct trace_iterator iter; 22 static struct trace_iterator iter;
23 static struct ring_buffer_iter *buffer_iter[CONFIG_NR_CPUS]; 23 static struct ring_buffer_iter *buffer_iter[CONFIG_NR_CPUS];
24 unsigned int old_userobj; 24 unsigned int old_userobj;
25 int cnt = 0, cpu; 25 int cnt = 0, cpu;
26 26
27 trace_init_global_iter(&iter); 27 trace_init_global_iter(&iter);
28 iter.buffer_iter = buffer_iter; 28 iter.buffer_iter = buffer_iter;
29 29
30 for_each_tracing_cpu(cpu) { 30 for_each_tracing_cpu(cpu) {
31 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled); 31 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
32 } 32 }
33 33
34 old_userobj = trace_flags; 34 old_userobj = trace_flags;
35 35
36 /* don't look at user memory in panic mode */ 36 /* don't look at user memory in panic mode */
37 trace_flags &= ~TRACE_ITER_SYM_USEROBJ; 37 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
38 38
39 kdb_printf("Dumping ftrace buffer:\n"); 39 kdb_printf("Dumping ftrace buffer:\n");
40 40
41 /* reset all but tr, trace, and overruns */ 41 /* reset all but tr, trace, and overruns */
42 memset(&iter.seq, 0, 42 memset(&iter.seq, 0,
43 sizeof(struct trace_iterator) - 43 sizeof(struct trace_iterator) -
44 offsetof(struct trace_iterator, seq)); 44 offsetof(struct trace_iterator, seq));
45 iter.iter_flags |= TRACE_FILE_LAT_FMT; 45 iter.iter_flags |= TRACE_FILE_LAT_FMT;
46 iter.pos = -1; 46 iter.pos = -1;
47 47
48 if (cpu_file == RING_BUFFER_ALL_CPUS) { 48 if (cpu_file == RING_BUFFER_ALL_CPUS) {
49 for_each_tracing_cpu(cpu) { 49 for_each_tracing_cpu(cpu) {
50 iter.buffer_iter[cpu] = 50 iter.buffer_iter[cpu] =
51 ring_buffer_read_prepare(iter.trace_buffer->buffer, cpu); 51 ring_buffer_read_prepare(iter.trace_buffer->buffer, cpu);
52 ring_buffer_read_start(iter.buffer_iter[cpu]); 52 ring_buffer_read_start(iter.buffer_iter[cpu]);
53 tracing_iter_reset(&iter, cpu); 53 tracing_iter_reset(&iter, cpu);
54 } 54 }
55 } else { 55 } else {
56 iter.cpu_file = cpu_file; 56 iter.cpu_file = cpu_file;
57 iter.buffer_iter[cpu_file] = 57 iter.buffer_iter[cpu_file] =
58 ring_buffer_read_prepare(iter.trace_buffer->buffer, cpu_file); 58 ring_buffer_read_prepare(iter.trace_buffer->buffer, cpu_file);
59 ring_buffer_read_start(iter.buffer_iter[cpu_file]); 59 ring_buffer_read_start(iter.buffer_iter[cpu_file]);
60 tracing_iter_reset(&iter, cpu_file); 60 tracing_iter_reset(&iter, cpu_file);
61 } 61 }
62 62
63 while (trace_find_next_entry_inc(&iter)) { 63 while (trace_find_next_entry_inc(&iter)) {
64 if (!cnt) 64 if (!cnt)
65 kdb_printf("---------------------------------\n"); 65 kdb_printf("---------------------------------\n");
66 cnt++; 66 cnt++;
67 67
68 if (!skip_lines) { 68 if (!skip_lines) {
69 print_trace_line(&iter); 69 print_trace_line(&iter);
70 trace_printk_seq(&iter.seq); 70 trace_printk_seq(&iter.seq);
71 } else { 71 } else {
72 skip_lines--; 72 skip_lines--;
73 } 73 }
74 74
75 if (KDB_FLAG(CMD_INTERRUPT)) 75 if (KDB_FLAG(CMD_INTERRUPT))
76 goto out; 76 goto out;
77 } 77 }
78 78
79 if (!cnt) 79 if (!cnt)
80 kdb_printf(" (ftrace buffer empty)\n"); 80 kdb_printf(" (ftrace buffer empty)\n");
81 else 81 else
82 kdb_printf("---------------------------------\n"); 82 kdb_printf("---------------------------------\n");
83 83
84 out: 84 out:
85 trace_flags = old_userobj; 85 trace_flags = old_userobj;
86 86
87 for_each_tracing_cpu(cpu) { 87 for_each_tracing_cpu(cpu) {
88 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled); 88 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
89 } 89 }
90 90
91 for_each_tracing_cpu(cpu) { 91 for_each_tracing_cpu(cpu) {
92 if (iter.buffer_iter[cpu]) { 92 if (iter.buffer_iter[cpu]) {
93 ring_buffer_read_finish(iter.buffer_iter[cpu]); 93 ring_buffer_read_finish(iter.buffer_iter[cpu]);
94 iter.buffer_iter[cpu] = NULL; 94 iter.buffer_iter[cpu] = NULL;
95 } 95 }
96 } 96 }
97 } 97 }
98 98
99 /* 99 /*
100 * kdb_ftdump - Dump the ftrace log buffer 100 * kdb_ftdump - Dump the ftrace log buffer
101 */ 101 */
102 static int kdb_ftdump(int argc, const char **argv) 102 static int kdb_ftdump(int argc, const char **argv)
103 { 103 {
104 int skip_lines = 0; 104 int skip_lines = 0;
105 long cpu_file; 105 long cpu_file;
106 char *cp; 106 char *cp;
107 107
108 if (argc > 2) 108 if (argc > 2)
109 return KDB_ARGCOUNT; 109 return KDB_ARGCOUNT;
110 110
111 if (argc) { 111 if (argc) {
112 skip_lines = simple_strtol(argv[1], &cp, 0); 112 skip_lines = simple_strtol(argv[1], &cp, 0);
113 if (*cp) 113 if (*cp)
114 skip_lines = 0; 114 skip_lines = 0;
115 } 115 }
116 116
117 if (argc == 2) { 117 if (argc == 2) {
118 cpu_file = simple_strtol(argv[2], &cp, 0); 118 cpu_file = simple_strtol(argv[2], &cp, 0);
119 if (*cp || cpu_file >= NR_CPUS || cpu_file < 0 || 119 if (*cp || cpu_file >= NR_CPUS || cpu_file < 0 ||
120 !cpu_online(cpu_file)) 120 !cpu_online(cpu_file))
121 return KDB_BADINT; 121 return KDB_BADINT;
122 } else { 122 } else {
123 cpu_file = RING_BUFFER_ALL_CPUS; 123 cpu_file = RING_BUFFER_ALL_CPUS;
124 } 124 }
125 125
126 kdb_trap_printk++; 126 kdb_trap_printk++;
127 ftrace_dump_buf(skip_lines, cpu_file); 127 ftrace_dump_buf(skip_lines, cpu_file);
128 kdb_trap_printk--; 128 kdb_trap_printk--;
129 129
130 return 0; 130 return 0;
131 } 131 }
132 132
133 static __init int kdb_ftrace_register(void) 133 static __init int kdb_ftrace_register(void)
134 { 134 {
135 kdb_register_repeat("ftdump", kdb_ftdump, "[skip_#lines] [cpu]", 135 kdb_register_flags("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
136 "Dump ftrace log", 0, KDB_REPEAT_NONE); 136 "Dump ftrace log", 0, KDB_ENABLE_ALWAYS_SAFE);
137 return 0; 137 return 0;
138 } 138 }
139 139
140 late_initcall(kdb_ftrace_register); 140 late_initcall(kdb_ftrace_register);
141 141
1 1
2 config HAVE_ARCH_KGDB 2 config HAVE_ARCH_KGDB
3 bool 3 bool
4 4
5 menuconfig KGDB 5 menuconfig KGDB
6 bool "KGDB: kernel debugger" 6 bool "KGDB: kernel debugger"
7 depends on HAVE_ARCH_KGDB 7 depends on HAVE_ARCH_KGDB
8 depends on DEBUG_KERNEL 8 depends on DEBUG_KERNEL
9 help 9 help
10 If you say Y here, it will be possible to remotely debug the 10 If you say Y here, it will be possible to remotely debug the
11 kernel using gdb. It is recommended but not required, that 11 kernel using gdb. It is recommended but not required, that
12 you also turn on the kernel config option 12 you also turn on the kernel config option
13 CONFIG_FRAME_POINTER to aid in producing more reliable stack 13 CONFIG_FRAME_POINTER to aid in producing more reliable stack
14 backtraces in the external debugger. Documentation of 14 backtraces in the external debugger. Documentation of
15 kernel debugger is available at http://kgdb.sourceforge.net 15 kernel debugger is available at http://kgdb.sourceforge.net
16 as well as in DocBook form in Documentation/DocBook/. If 16 as well as in DocBook form in Documentation/DocBook/. If
17 unsure, say N. 17 unsure, say N.
18 18
19 if KGDB 19 if KGDB
20 20
21 config KGDB_SERIAL_CONSOLE 21 config KGDB_SERIAL_CONSOLE
22 tristate "KGDB: use kgdb over the serial console" 22 tristate "KGDB: use kgdb over the serial console"
23 select CONSOLE_POLL 23 select CONSOLE_POLL
24 select MAGIC_SYSRQ 24 select MAGIC_SYSRQ
25 depends on TTY 25 depends on TTY
26 default y 26 default y
27 help 27 help
28 Share a serial console with kgdb. Sysrq-g must be used 28 Share a serial console with kgdb. Sysrq-g must be used
29 to break in initially. 29 to break in initially.
30 30
31 config KGDB_TESTS 31 config KGDB_TESTS
32 bool "KGDB: internal test suite" 32 bool "KGDB: internal test suite"
33 default n 33 default n
34 help 34 help
35 This is a kgdb I/O module specifically designed to test 35 This is a kgdb I/O module specifically designed to test
36 kgdb's internal functions. This kgdb I/O module is 36 kgdb's internal functions. This kgdb I/O module is
37 intended to for the development of new kgdb stubs 37 intended to for the development of new kgdb stubs
38 as well as regression testing the kgdb internals. 38 as well as regression testing the kgdb internals.
39 See the drivers/misc/kgdbts.c for the details about 39 See the drivers/misc/kgdbts.c for the details about
40 the tests. The most basic of this I/O module is to boot 40 the tests. The most basic of this I/O module is to boot
41 a kernel boot arguments "kgdbwait kgdbts=V1F100" 41 a kernel boot arguments "kgdbwait kgdbts=V1F100"
42 42
43 config KGDB_TESTS_ON_BOOT 43 config KGDB_TESTS_ON_BOOT
44 bool "KGDB: Run tests on boot" 44 bool "KGDB: Run tests on boot"
45 depends on KGDB_TESTS 45 depends on KGDB_TESTS
46 default n 46 default n
47 help 47 help
48 Run the kgdb tests on boot up automatically without the need 48 Run the kgdb tests on boot up automatically without the need
49 to pass in a kernel parameter 49 to pass in a kernel parameter
50 50
51 config KGDB_TESTS_BOOT_STRING 51 config KGDB_TESTS_BOOT_STRING
52 string "KGDB: which internal kgdb tests to run" 52 string "KGDB: which internal kgdb tests to run"
53 depends on KGDB_TESTS_ON_BOOT 53 depends on KGDB_TESTS_ON_BOOT
54 default "V1F100" 54 default "V1F100"
55 help 55 help
56 This is the command string to send the kgdb test suite on 56 This is the command string to send the kgdb test suite on
57 boot. See the drivers/misc/kgdbts.c for detailed 57 boot. See the drivers/misc/kgdbts.c for detailed
58 information about other strings you could use beyond the 58 information about other strings you could use beyond the
59 default of V1F100. 59 default of V1F100.
60 60
61 config KGDB_LOW_LEVEL_TRAP 61 config KGDB_LOW_LEVEL_TRAP
62 bool "KGDB: Allow debugging with traps in notifiers" 62 bool "KGDB: Allow debugging with traps in notifiers"
63 depends on X86 || MIPS 63 depends on X86 || MIPS
64 default n 64 default n
65 help 65 help
66 This will add an extra call back to kgdb for the breakpoint 66 This will add an extra call back to kgdb for the breakpoint
67 exception handler which will allow kgdb to step through a 67 exception handler which will allow kgdb to step through a
68 notify handler. 68 notify handler.
69 69
70 config KGDB_KDB 70 config KGDB_KDB
71 bool "KGDB_KDB: include kdb frontend for kgdb" 71 bool "KGDB_KDB: include kdb frontend for kgdb"
72 default n 72 default n
73 help 73 help
74 KDB frontend for kernel 74 KDB frontend for kernel
75 75
76 config KDB_DEFAULT_ENABLE
77 hex "KDB: Select kdb command functions to be enabled by default"
78 depends on KGDB_KDB
79 default 0x1
80 help
81 Specifiers which kdb commands are enabled by default. This may
82 be set to 1 or 0 to enable all commands or disable almost all
83 commands.
84
85 Alternatively the following bitmask applies:
86
87 0x0002 - allow arbitrary reads from memory and symbol lookup
88 0x0004 - allow arbitrary writes to memory
89 0x0008 - allow current register state to be inspected
90 0x0010 - allow current register state to be modified
91 0x0020 - allow passive inspection (backtrace, process list, lsmod)
92 0x0040 - allow flow control management (breakpoint, single step)
93 0x0080 - enable signalling of processes
94 0x0100 - allow machine to be rebooted
95
96 The config option merely sets the default at boot time. Both
97 issuing 'echo X > /sys/module/kdb/parameters/cmd_enable' or
98 setting with kdb.cmd_enable=X kernel command line option will
99 override the default settings.
100
76 config KDB_KEYBOARD 101 config KDB_KEYBOARD
77 bool "KGDB_KDB: keyboard as input device" 102 bool "KGDB_KDB: keyboard as input device"
78 depends on VT && KGDB_KDB 103 depends on VT && KGDB_KDB
79 default n 104 default n
80 help 105 help
81 KDB can use a PS/2 type keyboard for an input device 106 KDB can use a PS/2 type keyboard for an input device
82 107
83 config KDB_CONTINUE_CATASTROPHIC 108 config KDB_CONTINUE_CATASTROPHIC
84 int "KDB: continue after catastrophic errors" 109 int "KDB: continue after catastrophic errors"
85 depends on KGDB_KDB 110 depends on KGDB_KDB
86 default "0" 111 default "0"
87 help 112 help
88 This integer controls the behaviour of kdb when the kernel gets a 113 This integer controls the behaviour of kdb when the kernel gets a
89 catastrophic error, i.e. for a panic or oops. 114 catastrophic error, i.e. for a panic or oops.
90 When KDB is active and a catastrophic error occurs, nothing extra 115 When KDB is active and a catastrophic error occurs, nothing extra
91 will happen until you type 'go'. 116 will happen until you type 'go'.
92 CONFIG_KDB_CONTINUE_CATASTROPHIC == 0 (default). The first time 117 CONFIG_KDB_CONTINUE_CATASTROPHIC == 0 (default). The first time
93 you type 'go', you will be warned by kdb. The secend time you type 118 you type 'go', you will be warned by kdb. The secend time you type
94 'go', KDB tries to continue. No guarantees that the 119 'go', KDB tries to continue. No guarantees that the
95 kernel is still usable in this situation. 120 kernel is still usable in this situation.
96 CONFIG_KDB_CONTINUE_CATASTROPHIC == 1. KDB tries to continue. 121 CONFIG_KDB_CONTINUE_CATASTROPHIC == 1. KDB tries to continue.
97 No guarantees that the kernel is still usable in this situation. 122 No guarantees that the kernel is still usable in this situation.
98 CONFIG_KDB_CONTINUE_CATASTROPHIC == 2. KDB forces a reboot. 123 CONFIG_KDB_CONTINUE_CATASTROPHIC == 2. KDB forces a reboot.
99 If you are not sure, say 0. 124 If you are not sure, say 0.
100 125
101 endif # KGDB 126 endif # KGDB
102 127