xref: /linux/include/linux/kdb.h (revision d295082ea672e7277388e1cf7c5af73788cf8029)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _KDB_H
3 #define _KDB_H
4 
5 /*
6  * Kernel Debugger Architecture Independent Global Headers
7  *
8  * Copyright (c) 2000-2007 Silicon Graphics, Inc.  All Rights Reserved.
9  * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com>
10  * Copyright (C) 2009 Jason Wessel <jason.wessel@windriver.com>
11  */
12 
13 #include <linux/list.h>
14 #include <linux/smp.h>
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 
23 typedef enum {
24 	KDB_ENABLE_ALL = (1 << 0), /* Enable everything */
25 	KDB_ENABLE_MEM_READ = (1 << 1),
26 	KDB_ENABLE_MEM_WRITE = (1 << 2),
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 	 */
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 
65 typedef int (*kdb_func_t)(int, const char **);
66 
67 /* The KDB shell command table */
68 typedef struct _kdbtab {
69 	char    *name;			/* Command name */
70 	kdb_func_t func;		/* Function to execute command */
71 	char    *usage;			/* Usage String for this command */
72 	char    *help;			/* Help message for this command */
73 	short    minlen;		/* Minimum legal # cmd chars required */
74 	kdb_cmdflags_t flags;		/* Command behaviour flags */
75 	struct list_head list_node;	/* Command list */
76 } kdbtab_t;
77 
78 #ifdef	CONFIG_KGDB_KDB
79 #include <linux/init.h>
80 #include <linux/sched.h>
81 #include <linux/atomic.h>
82 
83 #define KDB_POLL_FUNC_MAX	5
84 extern int kdb_poll_idx;
85 
86 /*
87  * kdb_initial_cpu is initialized to -1, and is set to the cpu
88  * number whenever the kernel debugger is entered.
89  */
90 extern int kdb_initial_cpu;
91 
92 /* Types and messages used for dynamically added kdb shell commands */
93 
94 #define KDB_MAXARGS    16 /* Maximum number of arguments to a function  */
95 
96 /* KDB return codes from a command or internal kdb function */
97 #define KDB_NOTFOUND	(-1)
98 #define KDB_ARGCOUNT	(-2)
99 #define KDB_BADWIDTH	(-3)
100 #define KDB_BADRADIX	(-4)
101 #define KDB_NOTENV	(-5)
102 #define KDB_NOENVVALUE	(-6)
103 #define KDB_NOTIMP	(-7)
104 #define KDB_ENVFULL	(-8)
105 #define KDB_KMALLOCFAILED	(-9)
106 #define KDB_TOOMANYBPT	(-10)
107 #define KDB_TOOMANYDBREGS (-11)
108 #define KDB_DUPBPT	(-12)
109 #define KDB_BPTNOTFOUND	(-13)
110 #define KDB_BADMODE	(-14)
111 #define KDB_BADINT	(-15)
112 #define KDB_INVADDRFMT  (-16)
113 #define KDB_BADREG      (-17)
114 #define KDB_BADCPUNUM   (-18)
115 #define KDB_BADLENGTH	(-19)
116 #define KDB_NOBP	(-20)
117 #define KDB_BADADDR	(-21)
118 #define KDB_NOPERM	(-22)
119 
120 /*
121  * kdb_diemsg
122  *
123  *	Contains a pointer to the last string supplied to the
124  *	kernel 'die' panic function.
125  */
126 extern const char *kdb_diemsg;
127 
128 #define KDB_FLAG_EARLYKDB	(1 << 0) /* set from boot parameter kdb=early */
129 #define KDB_FLAG_CATASTROPHIC	(1 << 1) /* A catastrophic event has occurred */
130 #define KDB_FLAG_CMD_INTERRUPT	(1 << 2) /* Previous command was interrupted */
131 #define KDB_FLAG_NOIPI		(1 << 3) /* Do not send IPIs */
132 #define KDB_FLAG_NO_CONSOLE	(1 << 5) /* No console is available,
133 					  * kdb is disabled */
134 #define KDB_FLAG_NO_VT_CONSOLE	(1 << 6) /* No VT console is available, do
135 					  * not use keyboard */
136 #define KDB_FLAG_NO_I8042	(1 << 7) /* No i8042 chip is available, do
137 					  * not use keyboard */
138 
139 extern unsigned int kdb_flags;	/* Global flags, see kdb_state for per cpu state */
140 
141 #define KDB_FLAG(flag)		(kdb_flags & KDB_FLAG_##flag)
142 #define KDB_FLAG_SET(flag)	((void)(kdb_flags |= KDB_FLAG_##flag))
143 #define KDB_FLAG_CLEAR(flag)	((void)(kdb_flags &= ~KDB_FLAG_##flag))
144 
145 /*
146  * External entry point for the kernel debugger.  The pt_regs
147  * at the time of entry are supplied along with the reason for
148  * entry to the kernel debugger.
149  */
150 
151 typedef enum {
152 	KDB_REASON_ENTER = 1,	/* KDB_ENTER() trap/fault - regs valid */
153 	KDB_REASON_ENTER_SLAVE,	/* KDB_ENTER_SLAVE() trap/fault - regs valid */
154 	KDB_REASON_BREAK,	/* Breakpoint inst. - regs valid */
155 	KDB_REASON_DEBUG,	/* Debug Fault - regs valid */
156 	KDB_REASON_OOPS,	/* Kernel Oops - regs valid */
157 	KDB_REASON_SWITCH,	/* CPU switch - regs valid*/
158 	KDB_REASON_KEYBOARD,	/* Keyboard entry - regs valid */
159 	KDB_REASON_NMI,		/* Non-maskable interrupt; regs valid */
160 	KDB_REASON_RECURSE,	/* Recursive entry to kdb;
161 				 * regs probably valid */
162 	KDB_REASON_SSTEP,	/* Single Step trap. - regs valid */
163 	KDB_REASON_SYSTEM_NMI,	/* In NMI due to SYSTEM cmd; regs valid */
164 } kdb_reason_t;
165 
166 enum kdb_msgsrc {
167 	KDB_MSGSRC_INTERNAL, /* direct call to kdb_printf() */
168 	KDB_MSGSRC_PRINTK, /* trapped from printk() */
169 };
170 
171 extern int kdb_trap_printk;
172 extern int kdb_printf_cpu;
173 extern __printf(2, 0) int vkdb_printf(enum kdb_msgsrc src, const char *fmt,
174 				      va_list args);
175 extern __printf(1, 2) int kdb_printf(const char *, ...);
176 typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
177 
178 extern void kdb_init(int level);
179 
180 /* Access to kdb specific polling devices */
181 typedef int (*get_char_func)(void);
182 extern get_char_func kdb_poll_funcs[];
183 extern int kdb_get_kbd_char(void);
184 
185 static inline
kdb_process_cpu(const struct task_struct * p)186 int kdb_process_cpu(const struct task_struct *p)
187 {
188 	unsigned int cpu = task_cpu(p);
189 	if (cpu > num_possible_cpus())
190 		cpu = 0;
191 	return cpu;
192 }
193 
194 extern void kdb_send_sig(struct task_struct *p, int sig);
195 
196 #ifdef CONFIG_KALLSYMS
197 extern const char *kdb_walk_kallsyms(loff_t *pos);
198 #else /* ! CONFIG_KALLSYMS */
kdb_walk_kallsyms(loff_t * pos)199 static inline const char *kdb_walk_kallsyms(loff_t *pos)
200 {
201 	return NULL;
202 }
203 #endif /* ! CONFIG_KALLSYMS */
204 
205 /* Dynamic kdb shell command registration */
206 extern int kdb_register(kdbtab_t *cmd);
207 extern void kdb_unregister(kdbtab_t *cmd);
208 
209 /* Return true when KDB as locked for printing a message on this CPU. */
210 static inline
kdb_printf_on_this_cpu(void)211 bool kdb_printf_on_this_cpu(void)
212 {
213 	/*
214 	 * We can use raw_smp_processor_id() here because the task could
215 	 * not get migrated when KDB has locked for printing on this CPU.
216 	 */
217 	return unlikely(READ_ONCE(kdb_printf_cpu) == raw_smp_processor_id());
218 }
219 
220 #else /* ! CONFIG_KGDB_KDB */
kdb_printf(const char * fmt,...)221 static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
kdb_init(int level)222 static inline void kdb_init(int level) {}
kdb_register(kdbtab_t * cmd)223 static inline int kdb_register(kdbtab_t *cmd) { return 0; }
kdb_unregister(kdbtab_t * cmd)224 static inline void kdb_unregister(kdbtab_t *cmd) {}
225 
kdb_printf_on_this_cpu(void)226 static inline bool kdb_printf_on_this_cpu(void) { return false; }
227 
228 #endif	/* CONFIG_KGDB_KDB */
229 enum {
230 	KDB_NOT_INITIALIZED,
231 	KDB_INIT_EARLY,
232 	KDB_INIT_FULL,
233 };
234 
235 extern int kdbgetintenv(const char *, int *);
236 extern int kdb_set(int, const char **);
237 int kdb_lsmod(int argc, const char **argv);
238 
239 #endif	/* !_KDB_H */
240