xref: /freebsd/sys/ddb/ddb.h (revision 058ac3e8063366dafa634d9107642e12b038bf09)
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 /*
89  * There are three "command tables":
90  * - One for simple commands; a list of these is displayed
91  *   by typing 'help' at the debugger prompt.
92  * - One for sub-commands of 'show'; to see this type 'show'
93  *   without any arguments.
94  * - The last one for sub-commands of 'show all'; type 'show all'
95  *   without any argument to get a list.
96  */
97 struct db_command;
98 LIST_HEAD(db_command_table, db_command);
99 extern struct db_command_table db_cmd_table;
100 extern struct db_command_table db_show_table;
101 extern struct db_command_table db_show_all_table;
102 extern struct db_command_table db_show_active_table;
103 
104 /*
105  * Type signature for a function implementing a ddb command.
106  */
107 typedef void db_cmdfcn_t(db_expr_t addr, bool have_addr, db_expr_t count,
108 	    char *modif);
109 
110 /*
111  * Command table entry.
112  */
113 struct db_command {
114 	char *name;		/* command name */
115 	db_cmdfcn_t *fcn;	/* function to call */
116 	int flag;
117 #define	CS_OWN		0x1	/* non-standard syntax */
118 #define	CS_MORE		0x2	/* standard syntax, but may have other words
119 				 * at end */
120 #define	CS_SET_DOT	0x100	/* set dot after command */
121 #define	DB_CMD_MEMSAFE	0x1000	/* Command does not allow reads or writes to
122 				 * arbitrary memory. */
123 #define	DB_MAC1		0x10000	/* For MAC policy use */
124 #define	DB_MAC2		0x20000
125 	struct db_command_table *more; /* another level of command */
126 	LIST_ENTRY(db_command) next; /* next entry in the command table */
127 	void *mac_priv;		/* For MAC policy use */
128 };
129 
130 /*
131  * Arrange for the specified ddb command to be defined and
132  * bound to the specified function.  Commands can be defined
133  * in modules in which case they will be available only when
134  * the module is loaded.
135  */
136 #define	_DB_SET(_suffix, _name, _func, list, _flag, _more)	\
137 static struct db_command __CONCAT(_name,_suffix) = {		\
138 	.name	= __STRING(_name),				\
139 	.fcn	= _func,					\
140 	.flag	= _flag,					\
141 	.more	= _more						\
142 };								\
143 static void __CONCAT(__CONCAT(_name,_suffix),_add)(void *arg __unused) \
144     { db_command_register(&list, &__CONCAT(_name,_suffix)); }	\
145 SYSINIT(__CONCAT(_name,_suffix), SI_SUB_KLD, SI_ORDER_ANY,	\
146     __CONCAT(__CONCAT(_name,_suffix),_add), NULL);		\
147 static void __CONCAT(__CONCAT(_name,_suffix),_del)(void *arg __unused) \
148     { db_command_unregister(&list, &__CONCAT(_name,_suffix)); }	\
149 SYSUNINIT(__CONCAT(_name,_suffix), SI_SUB_KLD, SI_ORDER_ANY,	\
150     __CONCAT(__CONCAT(_name,_suffix),_del), NULL);
151 
152 /*
153  * Like _DB_SET but also create the function declaration which
154  * must be followed immediately by the body; e.g.
155  *   _DB_FUNC(_cmd, panic, db_panic, db_cmd_table, 0, NULL)
156  *   {
157  *	...panic implementation...
158  *   }
159  *
160  * This macro is mostly used to define commands placed in one of
161  * the ddb command tables; see DB_COMMAND, etc. below.
162  */
163 #define	_DB_FUNC(_suffix, _name, _func, list, _flag, _more)	\
164 static db_cmdfcn_t _func;					\
165 _DB_SET(_suffix, _name, _func, list, _flag, _more);		\
166 static void							\
167 _func(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
168 
169 /* common idom provided for backwards compatibility */
170 #define	DB_FUNC(_name, _func, list, _flag, _more)		\
171 	_DB_FUNC(_cmd, _name, _func, list, _flag, _more)
172 
173 #define	DB_COMMAND_FLAGS(cmd_name, func_name, flags) \
174 	_DB_FUNC(_cmd, cmd_name, func_name, db_cmd_table, flags, NULL)
175 #define	DB_COMMAND(cmd_name, func_name) \
176 	DB_COMMAND_FLAGS(cmd_name, func_name, 0)
177 #define	DB_ALIAS_FLAGS(alias_name, func_name, flags) \
178 	_DB_SET(_cmd, alias_name, func_name, db_cmd_table, flags, NULL)
179 #define	DB_ALIAS(alias_name, func_name) \
180 	DB_ALIAS_FLAGS(alias_name, func_name, 0)
181 #define	DB_SHOW_COMMAND_FLAGS(cmd_name, func_name, flags) \
182 	_DB_FUNC(_show, cmd_name, func_name, db_show_table, flags, NULL)
183 #define	DB_SHOW_COMMAND(cmd_name, func_name) \
184 	DB_SHOW_COMMAND_FLAGS(cmd_name, func_name, 0)
185 #define	DB_SHOW_ALIAS_FLAGS(alias_name, func_name, flags) \
186 	_DB_SET(_show, alias_name, func_name, db_show_table, flags, NULL)
187 #define	DB_SHOW_ALIAS(alias_name, func_name) \
188 	DB_SHOW_ALIAS_FLAGS(alias_name, func_name, 0)
189 #define	DB_SHOW_ALL_COMMAND(cmd_name, func_name)			\
190 	_DB_FUNC(_show_all, cmd_name, func_name, db_show_all_table,	\
191 	    DB_CMD_MEMSAFE, NULL)
192 #define	DB_SHOW_ALL_ALIAS(alias_name, func_name)			\
193 	_DB_SET(_show_all, alias_name, func_name, db_show_all_table,	\
194 	    DB_CMD_MEMSAFE, NULL)
195 
196 extern db_expr_t db_maxoff;
197 extern int db_indent;
198 extern int db_inst_count;
199 extern int db_load_count;
200 extern int db_store_count;
201 extern volatile int db_pager_quit;
202 extern db_expr_t db_radix;
203 extern db_expr_t db_max_width;
204 extern db_expr_t db_tab_stop_width;
205 extern db_expr_t db_lines_per_page;
206 
207 struct thread;
208 struct vm_map;
209 
210 void		db_check_interrupt(void);
211 void		db_clear_watchpoints(void);
212 db_addr_t	db_disasm(db_addr_t loc, bool altfmt);
213 				/* instruction disassembler */
214 void		db_error(const char *s);
215 int		db_expression(db_expr_t *valuep);
216 int		db_getc(void);
217 int		db_get_variable(db_expr_t *valuep);
218 void		db_iprintf(const char *,...) __printflike(1, 2);
219 struct proc	*db_lookup_proc(db_expr_t addr);
220 struct thread	*db_lookup_thread(db_expr_t addr, bool check_pid);
221 struct vm_map	*db_map_addr(vm_offset_t);
222 bool		db_map_current(struct vm_map *);
223 bool		db_map_equal(struct vm_map *, struct vm_map *);
224 void		db_md_list_watchpoints(void);
225 void		db_print_loc_and_inst(db_addr_t loc);
226 void		db_print_thread(void);
227 int		db_printf(const char *fmt, ...) __printflike(1, 2);
228 int		db_read_bytes(vm_offset_t addr, size_t size, char *data);
229 				/* machine-dependent */
230 int		db_readline(char *lstart, int lsize);
231 void		db_restart_at_pc(bool watchpt);
232 int		db_set_variable(db_expr_t value);
233 void		db_set_watchpoints(void);
234 void		db_skip_to_eol(void);
235 bool		db_stop_at_pc(int type, int code, bool *is_breakpoint,
236 		    bool *is_watchpoint);
237 #define		db_strcpy	strcpy
238 void		db_trace_self(void);
239 int		db_trace_thread(struct thread *, int);
240 bool		db_value_of_name(const char *name, db_expr_t *valuep);
241 bool		db_value_of_name_pcpu(const char *name, db_expr_t *valuep);
242 bool		db_value_of_name_vnet(const char *name, db_expr_t *valuep);
243 int		db_write_bytes(vm_offset_t addr, size_t size, char *data);
244 void		db_command_register(struct db_command_table *,
245 		    struct db_command *);
246 void		db_command_unregister(struct db_command_table *,
247 		    struct db_command *);
248 int		db_fetch_ksymtab(vm_offset_t ksym_start, vm_offset_t ksym_end,
249 		    vm_offset_t relbase);
250 
251 db_cmdfcn_t	db_breakpoint_cmd;
252 db_cmdfcn_t	db_capture_cmd;
253 db_cmdfcn_t	db_continue_cmd;
254 db_cmdfcn_t	db_delete_cmd;
255 db_cmdfcn_t	db_deletehwatch_cmd;
256 db_cmdfcn_t	db_deletewatch_cmd;
257 db_cmdfcn_t	db_examine_cmd;
258 db_cmdfcn_t	db_findstack_cmd;
259 db_cmdfcn_t	db_hwatchpoint_cmd;
260 db_cmdfcn_t	db_listbreak_cmd;
261 db_cmdfcn_t	db_scripts_cmd;
262 db_cmdfcn_t	db_print_cmd;
263 db_cmdfcn_t	db_ps;
264 db_cmdfcn_t	db_run_cmd;
265 db_cmdfcn_t	db_script_cmd;
266 db_cmdfcn_t	db_search_cmd;
267 db_cmdfcn_t	db_set_cmd;
268 db_cmdfcn_t	db_set_thread;
269 db_cmdfcn_t	db_show_regs;
270 db_cmdfcn_t	db_show_threads;
271 db_cmdfcn_t	db_single_step_cmd;
272 db_cmdfcn_t	db_textdump_cmd;
273 db_cmdfcn_t	db_trace_until_call_cmd;
274 db_cmdfcn_t	db_trace_until_matching_cmd;
275 db_cmdfcn_t	db_unscript_cmd;
276 db_cmdfcn_t	db_watchpoint_cmd;
277 db_cmdfcn_t	db_write_cmd;
278 
279 /*
280  * Interface between DDB and the DDB output capture facility.
281  */
282 struct dumperinfo;
283 void	db_capture_dump(struct dumperinfo *di);
284 void	db_capture_enterpager(void);
285 void	db_capture_exitpager(void);
286 void	db_capture_write(char *buffer, u_int buflen);
287 void	db_capture_writech(char ch);
288 
289 /*
290  * Interface between DDB  and the script facility.
291  */
292 void	db_script_kdbenter(const char *eventname);	/* KDB enter event. */
293 
294 /*
295  * Interface between DDB and the textdump facility.
296  *
297  * Text dump blocks are of a fixed size; textdump_block_buffer is a
298  * statically allocated buffer that code interacting with textdumps can use
299  * to prepare and hold a pending block in when calling writenextblock().
300  */
301 #define	TEXTDUMP_BLOCKSIZE	512
302 extern char	textdump_block_buffer[TEXTDUMP_BLOCKSIZE];
303 
304 void	textdump_mkustar(char *block_buffer, const char *filename,
305 	    u_int size);
306 void	textdump_restoreoff(off_t offset);
307 void	textdump_saveoff(off_t *offsetp);
308 int	textdump_writenextblock(struct dumperinfo *di, char *buffer);
309 
310 /*
311  * Interface between the kernel and textdumps.
312  */
313 extern int	textdump_pending;	/* Call textdump_dumpsys() instead. */
314 void	textdump_dumpsys(struct dumperinfo *di);
315 
316 #endif /* !_DDB_DDB_H_ */
317