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