xref: /freebsd/sys/ddb/ddb.h (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1993, Garrett A. Wollman.
5  * Copyright (c) 1993, University of Vermont and State Agricultural College.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 
35 /*
36  * Necessary declarations for the `ddb' kernel debugger.
37  */
38 
39 #ifndef _DDB_DDB_H_
40 #define	_DDB_DDB_H_
41 
42 #ifdef SYSCTL_DECL
43 SYSCTL_DECL(_debug_ddb);
44 #endif
45 
46 #include <machine/db_machdep.h>		/* type definitions */
47 
48 #include <sys/queue.h>			/* LIST_* */
49 #include <sys/kernel.h>			/* SYSINIT */
50 
51 #ifndef DB_MAXARGS
52 #define	DB_MAXARGS	10
53 #endif
54 
55 #ifndef DB_MAXLINE
56 #define	DB_MAXLINE	120
57 #endif
58 
59 #ifndef DB_MAXSCRIPTS
60 #define	DB_MAXSCRIPTS	8
61 #endif
62 
63 #ifndef DB_MAXSCRIPTNAME
64 #define	DB_MAXSCRIPTNAME	32
65 #endif
66 
67 #ifndef DB_MAXSCRIPTLEN
68 #define	DB_MAXSCRIPTLEN	128
69 #endif
70 
71 #ifndef DB_MAXSCRIPTRECURSION
72 #define	DB_MAXSCRIPTRECURSION	3
73 #endif
74 
75 #ifndef DB_CALL
76 #define	DB_CALL	db_fncall_generic
77 #else
78 int	DB_CALL(db_expr_t, db_expr_t *, int, db_expr_t[]);
79 #endif
80 
81 /*
82  * Extern variables to set the address and size of the symtab and strtab.
83  * Most users should use db_fetch_symtab in order to set them from the
84  * boot loader provided values.
85  */
86 extern vm_offset_t ksymtab, kstrtab, ksymtab_size, ksymtab_relbase;
87 
88 /* Command tables contain a list of commands. */
89 struct db_command;
90 LIST_HEAD(db_command_table, db_command);
91 
92 #define	_DB_TABLE_NAME(table)	db_##table##_table
93 
94 #define	DB_DEFINE_TABLE(parent, name, table)				\
95 	struct db_command_table _DB_TABLE_NAME(table) =			\
96 	    LIST_HEAD_INITIALIZER(_DB_TABLE_NAME(table));		\
97 	_DB_SET(parent, name, NULL, 0, &_DB_TABLE_NAME(table))
98 
99 #define	DB_DECLARE_TABLE(table)						\
100 	extern struct db_command_table _DB_TABLE_NAME(table)
101 
102 /*
103  * Builtin command tables:
104  * - cmd: Top-level command table; a list of these is displayed
105  *   by typing 'help' at the debugger prompt.
106  * - show: Sub-commands of 'show'
107  * - show_all: Sub-commands of 'show all'
108  * - show_active: Sub-commands of 'show active'
109  */
110 DB_DECLARE_TABLE(cmd);
111 DB_DECLARE_TABLE(show);
112 DB_DECLARE_TABLE(show_all);
113 DB_DECLARE_TABLE(show_active);
114 
115 /*
116  * Type signature for a function implementing a ddb command.
117  */
118 typedef void db_cmdfcn_t(db_expr_t addr, bool have_addr, db_expr_t count,
119 	    char *modif);
120 
121 /*
122  * Command table entry.
123  */
124 struct db_command {
125 	char *name;		/* command name */
126 	db_cmdfcn_t *fcn;	/* function to call */
127 	int flag;
128 #define	CS_OWN		0x1	/* non-standard syntax */
129 #define	CS_MORE		0x2	/* standard syntax, but may have other words
130 				 * at end */
131 #define	CS_SET_DOT	0x100	/* set dot after command */
132 #define	DB_CMD_MEMSAFE	0x1000	/* Command does not allow reads or writes to
133 				 * arbitrary memory. */
134 #define	DB_MAC1		0x10000	/* For MAC policy use */
135 #define	DB_MAC2		0x20000
136 	struct db_command_table *more; /* another level of command */
137 	LIST_ENTRY(db_command) next; /* next entry in the command table */
138 	void *mac_priv;		/* For MAC policy use */
139 };
140 
141 /*
142  * Arrange for the specified ddb command to be defined and
143  * bound to the specified function.  Commands can be defined
144  * in modules in which case they will be available only when
145  * the module is loaded.
146  */
147 #define	_DB_SET(_table, _name, _func, _flag, _more)		\
148 static struct db_command db_##_table##_##_name##_cmd = {	\
149 	.name	= __STRING(_name),				\
150 	.fcn	= _func,					\
151 	.flag	= _flag,					\
152 	.more	= _more						\
153 };								\
154 								\
155 static void							\
156 db##_table##_##_name##_add(void *arg __unused)			\
157 {								\
158 	db_command_register(&_DB_TABLE_NAME(_table),		\
159 	    &db_##_table##_##_name##_cmd);			\
160 }								\
161 SYSINIT(db_##_table##_##_name, SI_SUB_KLD, SI_ORDER_ANY,	\
162     db##_table##_##_name##_add, NULL);				\
163 								\
164 static void							\
165 db##_table##_##_name##_del(void *arg __unused)			\
166 {								\
167 	db_command_unregister(&_DB_TABLE_NAME(_table),		\
168 	    &db_##_table##_##_name##_cmd);			\
169 }								\
170 SYSUNINIT(db_##_table##_##_name, SI_SUB_KLD, SI_ORDER_ANY,	\
171     db##_table##_##_name##_del, NULL)
172 
173 /*
174  * Like _DB_SET but also create the function declaration which
175  * must be followed immediately by the body; e.g.
176  *   DB_TABLE_COMMAND_FLAGS(_cmd, panic, db_panic, 0)
177  *   {
178  *	...panic implementation...
179  *   }
180  *
181  * This macro is mostly used to define commands placed in one of
182  * the ddb command tables; see DB_COMMAND, etc. below.
183  */
184 #define	DB_TABLE_COMMAND_FLAGS(_table, _name, _func, _flag)	\
185 static db_cmdfcn_t _func;					\
186 _DB_SET(_table, _name, _func, _flag, NULL);			\
187 static void							\
188 _func(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
189 
190 #define	DB_TABLE_COMMAND(_table, _name, _func)			\
191 	DB_TABLE_COMMAND_FLAGS(_table, _name, _func, 0)
192 
193 /* Wrappers around _DB_SET used for aliases. */
194 #define	DB_TABLE_ALIAS_FLAGS(_table, _name, _func, _flag)	\
195 	_DB_SET(_table, _name, _func, _flag, NULL)
196 #define	DB_TABLE_ALIAS(_table, _name, _func)			\
197 	DB_TABLE_ALIAS_FLAGS(_table, _name, _func, 0)
198 
199 #define	DB_COMMAND_FLAGS(cmd_name, func_name, flags) \
200 	DB_TABLE_COMMAND_FLAGS(cmd, cmd_name, func_name, flags)
201 #define	DB_COMMAND(cmd_name, func_name) \
202 	DB_COMMAND_FLAGS(cmd_name, func_name, 0)
203 #define	DB_ALIAS_FLAGS(alias_name, func_name, flags) \
204 	DB_TABLE_ALIAS_FLAGS(cmd, alias_name, func_name, flags)
205 #define	DB_ALIAS(alias_name, func_name) \
206 	DB_ALIAS_FLAGS(alias_name, func_name, 0)
207 #define	DB_SHOW_COMMAND_FLAGS(cmd_name, func_name, flags) \
208 	DB_TABLE_COMMAND_FLAGS(show, cmd_name, func_name, flags)
209 #define	DB_SHOW_COMMAND(cmd_name, func_name) \
210 	DB_SHOW_COMMAND_FLAGS(cmd_name, func_name, 0)
211 #define	DB_SHOW_ALIAS_FLAGS(alias_name, func_name, flags) \
212 	DB_TABLE_ALIAS_FLAGS(show, alias_name, func_name, flags)
213 #define	DB_SHOW_ALIAS(alias_name, func_name) \
214 	DB_SHOW_ALIAS_FLAGS(alias_name, func_name, 0)
215 #define	DB_SHOW_ALL_COMMAND(cmd_name, func_name)			\
216 	DB_TABLE_COMMAND_FLAGS(show_all, cmd_name, func_name, DB_CMD_MEMSAFE)
217 #define	DB_SHOW_ALL_ALIAS(alias_name, func_name)			\
218 	DB_TABLE_ALIAS_FLAGS(show_all, alias_name, func_name, DB_CMD_MEMSAFE)
219 
220 extern db_expr_t db_maxoff;
221 extern int db_indent;
222 extern int db_inst_count;
223 extern int db_load_count;
224 extern int db_store_count;
225 extern volatile int db_pager_quit;
226 extern db_expr_t db_radix;
227 extern db_expr_t db_max_width;
228 extern db_expr_t db_tab_stop_width;
229 extern db_expr_t db_lines_per_page;
230 
231 struct thread;
232 struct vm_map;
233 
234 void		db_check_interrupt(void);
235 void		db_clear_watchpoints(void);
236 db_addr_t	db_disasm(db_addr_t loc, bool altfmt);
237 				/* instruction disassembler */
238 void		db_error(const char *s);
239 int		db_expression(db_expr_t *valuep);
240 int		db_getc(void);
241 int		db_get_variable(db_expr_t *valuep);
242 void		db_iprintf(const char *,...) __printflike(1, 2);
243 struct proc	*db_lookup_proc(db_expr_t addr);
244 struct thread	*db_lookup_thread(db_expr_t addr, bool check_pid);
245 struct vm_map	*db_map_addr(vm_offset_t);
246 bool		db_map_current(struct vm_map *);
247 bool		db_map_equal(struct vm_map *, struct vm_map *);
248 void		db_md_list_watchpoints(void);
249 void		db_print_loc_and_inst(db_addr_t loc);
250 void		db_print_thread(void);
251 int		db_printf(const char *fmt, ...) __printflike(1, 2);
252 int		db_read_bytes(vm_offset_t addr, size_t size, char *data);
253 				/* machine-dependent */
254 int		db_readline(char *lstart, int lsize);
255 void		db_restart_at_pc(bool watchpt);
256 int		db_set_variable(db_expr_t value);
257 void		db_set_watchpoints(void);
258 void		db_skip_to_eol(void);
259 bool		db_stop_at_pc(int type, int code, bool *is_breakpoint,
260 		    bool *is_watchpoint);
261 #define		db_strcpy	strcpy
262 void		db_trace_self(void);
263 int		db_trace_thread(struct thread *, int);
264 bool		db_value_of_name(const char *name, db_expr_t *valuep);
265 bool		db_value_of_name_pcpu(const char *name, db_expr_t *valuep);
266 bool		db_value_of_name_vnet(const char *name, db_expr_t *valuep);
267 int		db_write_bytes(vm_offset_t addr, size_t size, char *data);
268 void		db_command_register(struct db_command_table *,
269 		    struct db_command *);
270 void		db_command_unregister(struct db_command_table *,
271 		    struct db_command *);
272 int		db_fetch_ksymtab(vm_offset_t ksym_start, vm_offset_t ksym_end,
273 		    vm_offset_t relbase);
274 
275 db_cmdfcn_t	db_breakpoint_cmd;
276 db_cmdfcn_t	db_capture_cmd;
277 db_cmdfcn_t	db_continue_cmd;
278 db_cmdfcn_t	db_delete_cmd;
279 db_cmdfcn_t	db_deletehwatch_cmd;
280 db_cmdfcn_t	db_deletewatch_cmd;
281 db_cmdfcn_t	db_examine_cmd;
282 db_cmdfcn_t	db_findstack_cmd;
283 db_cmdfcn_t	db_hwatchpoint_cmd;
284 db_cmdfcn_t	db_listbreak_cmd;
285 db_cmdfcn_t	db_scripts_cmd;
286 db_cmdfcn_t	db_print_cmd;
287 db_cmdfcn_t	db_ps;
288 db_cmdfcn_t	db_run_cmd;
289 db_cmdfcn_t	db_script_cmd;
290 db_cmdfcn_t	db_search_cmd;
291 db_cmdfcn_t	db_set_cmd;
292 db_cmdfcn_t	db_set_thread;
293 db_cmdfcn_t	db_show_regs;
294 db_cmdfcn_t	db_show_threads;
295 db_cmdfcn_t	db_single_step_cmd;
296 db_cmdfcn_t	db_textdump_cmd;
297 db_cmdfcn_t	db_trace_until_call_cmd;
298 db_cmdfcn_t	db_trace_until_matching_cmd;
299 db_cmdfcn_t	db_unscript_cmd;
300 db_cmdfcn_t	db_watchpoint_cmd;
301 db_cmdfcn_t	db_write_cmd;
302 
303 /*
304  * Interface between DDB and the DDB output capture facility.
305  */
306 struct dumperinfo;
307 void	db_capture_dump(struct dumperinfo *di);
308 void	db_capture_enterpager(void);
309 void	db_capture_exitpager(void);
310 void	db_capture_write(char *buffer, u_int buflen);
311 void	db_capture_writech(char ch);
312 
313 /*
314  * Interface between DDB  and the script facility.
315  */
316 void	db_script_kdbenter(const char *eventname);	/* KDB enter event. */
317 
318 /*
319  * Interface between DDB and the textdump facility.
320  *
321  * Text dump blocks are of a fixed size; textdump_block_buffer is a
322  * statically allocated buffer that code interacting with textdumps can use
323  * to prepare and hold a pending block in when calling writenextblock().
324  */
325 #define	TEXTDUMP_BLOCKSIZE	512
326 extern char	textdump_block_buffer[TEXTDUMP_BLOCKSIZE];
327 
328 void	textdump_mkustar(char *block_buffer, const char *filename,
329 	    u_int size);
330 void	textdump_restoreoff(off_t offset);
331 void	textdump_saveoff(off_t *offsetp);
332 int	textdump_writenextblock(struct dumperinfo *di, char *buffer);
333 
334 /*
335  * Interface between the kernel and textdumps.
336  */
337 extern int	textdump_pending;	/* Call textdump_dumpsys() instead. */
338 void	textdump_dumpsys(struct dumperinfo *di);
339 
340 #endif /* !_DDB_DDB_H_ */
341