db_command.c revision 1.96.2.1 1 1.96.2.1 skrll /* $NetBSD: db_command.c,v 1.96.2.1 2007/09/10 10:54:46 skrll Exp $ */
2 1.12 cgd
3 1.64 simonb /*
4 1.1 cgd * Mach Operating System
5 1.1 cgd * Copyright (c) 1991,1990 Carnegie Mellon University
6 1.1 cgd * All Rights Reserved.
7 1.64 simonb *
8 1.1 cgd * Permission to use, copy, modify and distribute this software and its
9 1.1 cgd * documentation is hereby granted, provided that both the copyright
10 1.1 cgd * notice and this permission notice appear in all copies of the
11 1.1 cgd * software, derivative works or modified versions, and any portions
12 1.1 cgd * thereof, and that both notices appear in supporting documentation.
13 1.64 simonb *
14 1.30 pk * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 1.1 cgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 1.1 cgd * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 1.64 simonb *
18 1.1 cgd * Carnegie Mellon requests users of this software to return to
19 1.64 simonb *
20 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 1.1 cgd * School of Computer Science
22 1.1 cgd * Carnegie Mellon University
23 1.1 cgd * Pittsburgh PA 15213-3890
24 1.64 simonb *
25 1.1 cgd * any improvements or extensions that they make and grant Carnegie the
26 1.1 cgd * rights to redistribute these changes.
27 1.1 cgd */
28 1.25 mrg
29 1.63 lukem /*
30 1.63 lukem * Command dispatcher.
31 1.63 lukem */
32 1.63 lukem
33 1.63 lukem #include <sys/cdefs.h>
34 1.96.2.1 skrll __KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.96.2.1 2007/09/10 10:54:46 skrll Exp $");
35 1.63 lukem
36 1.27 tron #include "opt_ddb.h"
37 1.69 briggs #include "opt_kgdb.h"
38 1.40 jhawk #include "opt_inet.h"
39 1.80 yamt #include "opt_ddbparam.h"
40 1.5 mycroft
41 1.7 mycroft #include <sys/param.h>
42 1.18 christos #include <sys/systm.h>
43 1.23 scottr #include <sys/reboot.h>
44 1.65 simonb #include <sys/device.h>
45 1.65 simonb #include <sys/malloc.h>
46 1.83 yamt #include <sys/mbuf.h>
47 1.65 simonb #include <sys/namei.h>
48 1.65 simonb #include <sys/pool.h>
49 1.7 mycroft #include <sys/proc.h>
50 1.36 chs #include <sys/vnode.h>
51 1.92 ad #include <sys/lockdebug.h>
52 1.92 ad #include <sys/sleepq.h>
53 1.15 gwr
54 1.1 cgd #include <machine/db_machdep.h> /* type definitions */
55 1.1 cgd
56 1.58 mrg #if defined(_KERNEL_OPT)
57 1.34 sommerfe #include "opt_multiprocessor.h"
58 1.34 sommerfe #endif
59 1.34 sommerfe #ifdef MULTIPROCESSOR
60 1.34 sommerfe #include <machine/cpu.h>
61 1.34 sommerfe #endif
62 1.34 sommerfe
63 1.1 cgd #include <ddb/db_lex.h>
64 1.1 cgd #include <ddb/db_output.h>
65 1.10 pk #include <ddb/db_command.h>
66 1.16 christos #include <ddb/db_break.h>
67 1.16 christos #include <ddb/db_watch.h>
68 1.16 christos #include <ddb/db_run.h>
69 1.16 christos #include <ddb/db_variables.h>
70 1.16 christos #include <ddb/db_interface.h>
71 1.16 christos #include <ddb/db_sym.h>
72 1.16 christos #include <ddb/db_extern.h>
73 1.1 cgd
74 1.24 mrg #include <uvm/uvm_extern.h>
75 1.26 jonathan #include <uvm/uvm_ddb.h>
76 1.24 mrg
77 1.46 jhawk #include "arp.h"
78 1.46 jhawk
79 1.1 cgd /*
80 1.64 simonb * Results of command search.
81 1.64 simonb */
82 1.64 simonb #define CMD_UNIQUE 0
83 1.64 simonb #define CMD_FOUND 1
84 1.64 simonb #define CMD_NONE 2
85 1.64 simonb #define CMD_AMBIGUOUS 3
86 1.64 simonb #define CMD_HELP 4
87 1.64 simonb
88 1.64 simonb /*
89 1.1 cgd * Exported global variables
90 1.1 cgd */
91 1.93 thorpej bool db_cmd_loop_done;
92 1.17 gwr label_t *db_recover;
93 1.64 simonb db_addr_t db_dot;
94 1.64 simonb db_addr_t db_last_addr;
95 1.64 simonb db_addr_t db_prev;
96 1.64 simonb db_addr_t db_next;
97 1.1 cgd
98 1.1 cgd /*
99 1.1 cgd * if 'ed' style: 'dot' is set at start of last item printed,
100 1.1 cgd * and '+' points to next line.
101 1.1 cgd * Otherwise: 'dot' points to next item, '..' points to last.
102 1.1 cgd */
103 1.95 thorpej static bool db_ed_style = true;
104 1.64 simonb
105 1.94 matt static void db_buf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
106 1.65 simonb static void db_cmd_list(const struct db_command *);
107 1.64 simonb static int db_cmd_search(const char *, const struct db_command *,
108 1.64 simonb const struct db_command **);
109 1.64 simonb static void db_command(const struct db_command **,
110 1.64 simonb const struct db_command *);
111 1.94 matt static void db_event_print_cmd(db_expr_t, bool, db_expr_t, const char *);
112 1.94 matt static void db_fncall(db_expr_t, bool, db_expr_t, const char *);
113 1.94 matt static void db_malloc_print_cmd(db_expr_t, bool, db_expr_t, const char *);
114 1.94 matt static void db_map_print_cmd(db_expr_t, bool, db_expr_t, const char *);
115 1.94 matt static void db_namecache_print_cmd(db_expr_t, bool, db_expr_t, const char *);
116 1.94 matt static void db_object_print_cmd(db_expr_t, bool, db_expr_t, const char *);
117 1.94 matt static void db_page_print_cmd(db_expr_t, bool, db_expr_t, const char *);
118 1.94 matt static void db_show_all_pages(db_expr_t, bool, db_expr_t, const char *);
119 1.94 matt static void db_pool_print_cmd(db_expr_t, bool, db_expr_t, const char *);
120 1.94 matt static void db_reboot_cmd(db_expr_t, bool, db_expr_t, const char *);
121 1.94 matt static void db_sifting_cmd(db_expr_t, bool, db_expr_t, const char *);
122 1.94 matt static void db_stack_trace_cmd(db_expr_t, bool, db_expr_t, const char *);
123 1.94 matt static void db_sync_cmd(db_expr_t, bool, db_expr_t, const char *);
124 1.94 matt static void db_uvmexp_print_cmd(db_expr_t, bool, db_expr_t, const char *);
125 1.94 matt static void db_vnode_print_cmd(db_expr_t, bool, db_expr_t, const char *);
126 1.94 matt static void db_lock_print_cmd(db_expr_t, bool, db_expr_t, const char *);
127 1.94 matt static void db_mount_print_cmd(db_expr_t, bool, db_expr_t, const char *);
128 1.94 matt static void db_mbuf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
129 1.64 simonb
130 1.64 simonb /*
131 1.64 simonb * 'show' commands
132 1.64 simonb */
133 1.64 simonb
134 1.64 simonb static const struct db_command db_show_all_cmds[] = {
135 1.64 simonb { "callout", db_show_callout, 0, NULL },
136 1.84 bjh21 { "pages", db_show_all_pages, 0, NULL },
137 1.64 simonb { "procs", db_show_all_procs, 0, NULL },
138 1.82 yamt { "pools", db_show_all_pools, 0, NULL },
139 1.64 simonb { NULL, NULL, 0, NULL }
140 1.64 simonb };
141 1.64 simonb
142 1.64 simonb static const struct db_command db_show_cmds[] = {
143 1.96 rmind { "aio_jobs", db_show_aio_jobs, 0, NULL },
144 1.64 simonb { "all", NULL, 0, db_show_all_cmds },
145 1.64 simonb #if defined(INET) && (NARP > 0)
146 1.64 simonb { "arptab", db_show_arptab, 0, NULL },
147 1.64 simonb #endif
148 1.64 simonb { "breaks", db_listbreak_cmd, 0, NULL },
149 1.64 simonb { "buf", db_buf_print_cmd, 0, NULL },
150 1.65 simonb { "event", db_event_print_cmd, 0, NULL },
151 1.92 ad { "lock", db_lock_print_cmd, 0, NULL },
152 1.65 simonb { "malloc", db_malloc_print_cmd, 0, NULL },
153 1.64 simonb { "map", db_map_print_cmd, 0, NULL },
154 1.74 dbj { "mount", db_mount_print_cmd, 0, NULL },
155 1.83 yamt { "mbuf", db_mbuf_print_cmd, 0, NULL },
156 1.96.2.1 skrll { "mqueue", db_show_mqueue_cmd, 0, NULL },
157 1.64 simonb { "ncache", db_namecache_print_cmd, 0, NULL },
158 1.64 simonb { "object", db_object_print_cmd, 0, NULL },
159 1.64 simonb { "page", db_page_print_cmd, 0, NULL },
160 1.64 simonb { "pool", db_pool_print_cmd, 0, NULL },
161 1.64 simonb { "registers", db_show_regs, 0, NULL },
162 1.72 thorpej { "sched_qs", db_show_sched_qs, 0, NULL },
163 1.64 simonb { "uvmexp", db_uvmexp_print_cmd, 0, NULL },
164 1.64 simonb { "vnode", db_vnode_print_cmd, 0, NULL },
165 1.64 simonb { "watches", db_listwatch_cmd, 0, NULL },
166 1.64 simonb { NULL, NULL, 0, NULL }
167 1.64 simonb };
168 1.64 simonb
169 1.77 yamt /* arch/<arch>/<arch>/db_interface.c */
170 1.77 yamt #ifdef DB_MACHINE_COMMANDS
171 1.77 yamt extern const struct db_command db_machine_command_table[];
172 1.77 yamt #endif
173 1.77 yamt
174 1.64 simonb static const struct db_command db_command_table[] = {
175 1.73 chs { "b", db_breakpoint_cmd, 0, NULL },
176 1.64 simonb { "break", db_breakpoint_cmd, 0, NULL },
177 1.68 jmc { "bt", db_stack_trace_cmd, 0, NULL },
178 1.64 simonb { "c", db_continue_cmd, 0, NULL },
179 1.64 simonb { "call", db_fncall, CS_OWN, NULL },
180 1.64 simonb { "callout", db_show_callout, 0, NULL },
181 1.64 simonb { "continue", db_continue_cmd, 0, NULL },
182 1.64 simonb { "d", db_delete_cmd, 0, NULL },
183 1.64 simonb { "delete", db_delete_cmd, 0, NULL },
184 1.64 simonb { "dmesg", db_dmesg, 0, NULL },
185 1.64 simonb { "dwatch", db_deletewatch_cmd, 0, NULL },
186 1.64 simonb { "examine", db_examine_cmd, CS_SET_DOT, NULL },
187 1.64 simonb { "kill", db_kill_proc, CS_OWN, NULL },
188 1.69 briggs #ifdef KGDB
189 1.69 briggs { "kgdb", db_kgdb_cmd, 0, NULL },
190 1.69 briggs #endif
191 1.64 simonb #ifdef DB_MACHINE_COMMANDS
192 1.64 simonb { "machine", NULL, 0, db_machine_command_table },
193 1.64 simonb #endif
194 1.64 simonb { "match", db_trace_until_matching_cmd,0, NULL },
195 1.64 simonb { "next", db_trace_until_matching_cmd,0, NULL },
196 1.64 simonb { "p", db_print_cmd, 0, NULL },
197 1.64 simonb { "print", db_print_cmd, 0, NULL },
198 1.64 simonb { "ps", db_show_all_procs, 0, NULL },
199 1.64 simonb { "reboot", db_reboot_cmd, CS_OWN, NULL },
200 1.64 simonb { "s", db_single_step_cmd, 0, NULL },
201 1.64 simonb { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL },
202 1.64 simonb { "set", db_set_cmd, CS_OWN, NULL },
203 1.64 simonb { "show", NULL, 0, db_show_cmds },
204 1.64 simonb { "sifting", db_sifting_cmd, CS_OWN, NULL },
205 1.64 simonb { "step", db_single_step_cmd, 0, NULL },
206 1.64 simonb { "sync", db_sync_cmd, CS_OWN, NULL },
207 1.64 simonb { "trace", db_stack_trace_cmd, 0, NULL },
208 1.64 simonb { "until", db_trace_until_call_cmd,0, NULL },
209 1.64 simonb { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
210 1.64 simonb { "watch", db_watchpoint_cmd, CS_MORE, NULL },
211 1.64 simonb { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
212 1.64 simonb { "x", db_examine_cmd, CS_SET_DOT, NULL },
213 1.64 simonb { NULL, NULL, 0, NULL }
214 1.64 simonb };
215 1.64 simonb
216 1.64 simonb static const struct db_command *db_last_command = NULL;
217 1.80 yamt #if defined(DDB_COMMANDONENTER)
218 1.81 yamt char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = ___STRING(DDB_COMMANDONENTER);
219 1.81 yamt #else /* defined(DDB_COMMANDONENTER) */
220 1.81 yamt char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = "";
221 1.80 yamt #endif /* defined(DDB_COMMANDONENTER) */
222 1.80 yamt #define DB_LINE_SEP ';'
223 1.1 cgd
224 1.1 cgd /*
225 1.1 cgd * Utility routine - discard tokens through end-of-line.
226 1.1 cgd */
227 1.1 cgd void
228 1.64 simonb db_skip_to_eol(void)
229 1.1 cgd {
230 1.64 simonb int t;
231 1.64 simonb
232 1.1 cgd do {
233 1.64 simonb t = db_read_token();
234 1.1 cgd } while (t != tEOL);
235 1.1 cgd }
236 1.1 cgd
237 1.64 simonb void
238 1.89 uwe db_error(const char *s)
239 1.64 simonb {
240 1.64 simonb
241 1.64 simonb if (s)
242 1.64 simonb db_printf("%s", s);
243 1.64 simonb db_flush_lex();
244 1.64 simonb longjmp(db_recover);
245 1.64 simonb }
246 1.64 simonb
247 1.81 yamt static void
248 1.81 yamt db_execute_commandlist(const char *cmdlist)
249 1.81 yamt {
250 1.81 yamt const char *cmd = cmdlist;
251 1.81 yamt const struct db_command *dummy = NULL;
252 1.81 yamt
253 1.81 yamt while (*cmd != '\0') {
254 1.81 yamt const char *ep = cmd;
255 1.81 yamt
256 1.81 yamt while (*ep != '\0' && *ep != DB_LINE_SEP) {
257 1.81 yamt ep++;
258 1.81 yamt }
259 1.81 yamt db_set_line(cmd, ep);
260 1.81 yamt db_command(&dummy, db_command_table);
261 1.81 yamt cmd = ep;
262 1.81 yamt if (*cmd == DB_LINE_SEP) {
263 1.81 yamt cmd++;
264 1.81 yamt }
265 1.81 yamt }
266 1.81 yamt }
267 1.81 yamt
268 1.64 simonb void
269 1.64 simonb db_command_loop(void)
270 1.64 simonb {
271 1.64 simonb label_t db_jmpbuf;
272 1.64 simonb label_t *savejmp;
273 1.64 simonb
274 1.64 simonb /*
275 1.64 simonb * Initialize 'prev' and 'next' to dot.
276 1.64 simonb */
277 1.64 simonb db_prev = db_dot;
278 1.64 simonb db_next = db_dot;
279 1.64 simonb
280 1.64 simonb db_cmd_loop_done = 0;
281 1.64 simonb
282 1.64 simonb savejmp = db_recover;
283 1.64 simonb db_recover = &db_jmpbuf;
284 1.64 simonb (void) setjmp(&db_jmpbuf);
285 1.64 simonb
286 1.81 yamt db_execute_commandlist(db_cmd_on_enter);
287 1.80 yamt
288 1.64 simonb while (!db_cmd_loop_done) {
289 1.64 simonb if (db_print_position() != 0)
290 1.64 simonb db_printf("\n");
291 1.64 simonb db_output_line = 0;
292 1.64 simonb
293 1.64 simonb
294 1.64 simonb #ifdef MULTIPROCESSOR
295 1.64 simonb db_printf("db{%ld}> ", (long)cpu_number());
296 1.64 simonb #else
297 1.64 simonb db_printf("db> ");
298 1.64 simonb #endif
299 1.64 simonb (void) db_read_line();
300 1.64 simonb
301 1.64 simonb db_command(&db_last_command, db_command_table);
302 1.64 simonb }
303 1.64 simonb
304 1.64 simonb db_recover = savejmp;
305 1.64 simonb }
306 1.1 cgd
307 1.1 cgd /*
308 1.1 cgd * Search for command prefix.
309 1.1 cgd */
310 1.64 simonb static int
311 1.64 simonb db_cmd_search(const char *name, const struct db_command *table,
312 1.64 simonb const struct db_command **cmdp)
313 1.1 cgd {
314 1.53 jdolecek const struct db_command *cmd;
315 1.10 pk int result = CMD_NONE;
316 1.1 cgd
317 1.1 cgd for (cmd = table; cmd->name != 0; cmd++) {
318 1.64 simonb const char *lp;
319 1.64 simonb const char *rp;
320 1.64 simonb int c;
321 1.64 simonb
322 1.64 simonb lp = name;
323 1.64 simonb rp = cmd->name;
324 1.64 simonb while ((c = *lp) == *rp) {
325 1.64 simonb if (c == 0) {
326 1.64 simonb /* complete match */
327 1.64 simonb *cmdp = cmd;
328 1.64 simonb return (CMD_UNIQUE);
329 1.64 simonb }
330 1.64 simonb lp++;
331 1.64 simonb rp++;
332 1.64 simonb }
333 1.1 cgd if (c == 0) {
334 1.64 simonb /* end of name, not end of command -
335 1.64 simonb partial match */
336 1.64 simonb if (result == CMD_FOUND) {
337 1.64 simonb result = CMD_AMBIGUOUS;
338 1.64 simonb /* but keep looking for a full match -
339 1.64 simonb this lets us match single letters */
340 1.64 simonb } else {
341 1.64 simonb *cmdp = cmd;
342 1.64 simonb result = CMD_FOUND;
343 1.64 simonb }
344 1.1 cgd }
345 1.1 cgd }
346 1.1 cgd if (result == CMD_NONE) {
347 1.64 simonb /* check for 'help' */
348 1.1 cgd if (name[0] == 'h' && name[1] == 'e'
349 1.1 cgd && name[2] == 'l' && name[3] == 'p')
350 1.1 cgd result = CMD_HELP;
351 1.1 cgd }
352 1.1 cgd return (result);
353 1.1 cgd }
354 1.1 cgd
355 1.64 simonb static void
356 1.64 simonb db_cmd_list(const struct db_command *table)
357 1.1 cgd {
358 1.67 simonb int i, j, w, columns, lines, width=0, numcmds;
359 1.53 jdolecek const char *p;
360 1.1 cgd
361 1.32 lukem for (numcmds = 0; table[numcmds].name != NULL; numcmds++) {
362 1.32 lukem w = strlen(table[numcmds].name);
363 1.32 lukem if (w > width)
364 1.32 lukem width = w;
365 1.32 lukem }
366 1.32 lukem width = DB_NEXT_TAB(width);
367 1.32 lukem
368 1.32 lukem columns = db_max_width / width;
369 1.32 lukem if (columns == 0)
370 1.32 lukem columns = 1;
371 1.32 lukem lines = (numcmds + columns - 1) / columns;
372 1.32 lukem for (i = 0; i < lines; i++) {
373 1.32 lukem for (j = 0; j < columns; j++) {
374 1.32 lukem p = table[j * lines + i].name;
375 1.32 lukem if (p)
376 1.32 lukem db_printf("%s", p);
377 1.32 lukem if (j * lines + i + lines >= numcmds) {
378 1.32 lukem db_putchar('\n');
379 1.32 lukem break;
380 1.32 lukem }
381 1.85 christos if (p) {
382 1.85 christos w = strlen(p);
383 1.85 christos while (w < width) {
384 1.85 christos w = DB_NEXT_TAB(w);
385 1.85 christos db_putchar('\t');
386 1.85 christos }
387 1.32 lukem }
388 1.32 lukem }
389 1.1 cgd }
390 1.1 cgd }
391 1.1 cgd
392 1.64 simonb static void
393 1.64 simonb db_command(const struct db_command **last_cmdp,
394 1.64 simonb const struct db_command *cmd_table)
395 1.1 cgd {
396 1.53 jdolecek const struct db_command *cmd;
397 1.1 cgd int t;
398 1.1 cgd char modif[TOK_STRING_SIZE];
399 1.1 cgd db_expr_t addr, count;
400 1.95 thorpej bool have_addr = false;
401 1.1 cgd int result;
402 1.87 he static db_expr_t last_count = 0;
403 1.1 cgd
404 1.86 mrg cmd = NULL; /* XXX gcc */
405 1.86 mrg
406 1.1 cgd t = db_read_token();
407 1.38 jhawk if ((t == tEOL) || (t == tCOMMA)) {
408 1.1 cgd /*
409 1.64 simonb * An empty line repeats last command, at 'next'.
410 1.64 simonb * Only a count repeats the last command with the new count.
411 1.64 simonb */
412 1.64 simonb cmd = *last_cmdp;
413 1.64 simonb addr = (db_expr_t)db_next;
414 1.64 simonb if (t == tCOMMA) {
415 1.64 simonb if (!db_expression(&count)) {
416 1.64 simonb db_printf("Count missing\n");
417 1.64 simonb db_flush_lex();
418 1.64 simonb return;
419 1.64 simonb }
420 1.64 simonb } else
421 1.64 simonb count = last_count;
422 1.95 thorpej have_addr = false;
423 1.64 simonb modif[0] = '\0';
424 1.64 simonb db_skip_to_eol();
425 1.64 simonb } else if (t == tEXCL) {
426 1.64 simonb db_fncall(0, 0, 0, NULL);
427 1.64 simonb return;
428 1.64 simonb } else if (t != tIDENT) {
429 1.64 simonb db_printf("?\n");
430 1.64 simonb db_flush_lex();
431 1.64 simonb return;
432 1.64 simonb } else {
433 1.64 simonb /*
434 1.64 simonb * Search for command
435 1.1 cgd */
436 1.64 simonb while (cmd_table) {
437 1.64 simonb result = db_cmd_search(db_tok_string, cmd_table, &cmd);
438 1.64 simonb switch (result) {
439 1.64 simonb case CMD_NONE:
440 1.64 simonb db_printf("No such command\n");
441 1.64 simonb db_flush_lex();
442 1.64 simonb return;
443 1.64 simonb case CMD_AMBIGUOUS:
444 1.64 simonb db_printf("Ambiguous\n");
445 1.64 simonb db_flush_lex();
446 1.64 simonb return;
447 1.64 simonb case CMD_HELP:
448 1.64 simonb db_cmd_list(cmd_table);
449 1.64 simonb db_flush_lex();
450 1.64 simonb return;
451 1.64 simonb default:
452 1.64 simonb break;
453 1.64 simonb }
454 1.64 simonb if ((cmd_table = cmd->more) != 0) {
455 1.64 simonb t = db_read_token();
456 1.64 simonb if (t != tIDENT) {
457 1.64 simonb db_cmd_list(cmd_table);
458 1.64 simonb db_flush_lex();
459 1.64 simonb return;
460 1.64 simonb }
461 1.64 simonb }
462 1.1 cgd }
463 1.1 cgd
464 1.64 simonb if ((cmd->flag & CS_OWN) == 0) {
465 1.64 simonb /*
466 1.64 simonb * Standard syntax:
467 1.64 simonb * command [/modifier] [addr] [,count]
468 1.64 simonb */
469 1.64 simonb t = db_read_token();
470 1.64 simonb if (t == tSLASH) {
471 1.64 simonb t = db_read_token();
472 1.64 simonb if (t != tIDENT) {
473 1.64 simonb db_printf("Bad modifier\n");
474 1.64 simonb db_flush_lex();
475 1.64 simonb return;
476 1.64 simonb }
477 1.71 itojun strlcpy(modif, db_tok_string, sizeof(modif));
478 1.64 simonb } else {
479 1.64 simonb db_unread_token(t);
480 1.64 simonb modif[0] = '\0';
481 1.64 simonb }
482 1.64 simonb
483 1.64 simonb if (db_expression(&addr)) {
484 1.64 simonb db_dot = (db_addr_t) addr;
485 1.64 simonb db_last_addr = db_dot;
486 1.95 thorpej have_addr = true;
487 1.64 simonb } else {
488 1.64 simonb addr = (db_expr_t) db_dot;
489 1.95 thorpej have_addr = false;
490 1.64 simonb }
491 1.64 simonb t = db_read_token();
492 1.64 simonb if (t == tCOMMA) {
493 1.64 simonb if (!db_expression(&count)) {
494 1.64 simonb db_printf("Count missing\n");
495 1.64 simonb db_flush_lex();
496 1.64 simonb return;
497 1.64 simonb }
498 1.64 simonb } else {
499 1.64 simonb db_unread_token(t);
500 1.64 simonb count = -1;
501 1.64 simonb }
502 1.64 simonb if ((cmd->flag & CS_MORE) == 0) {
503 1.64 simonb db_skip_to_eol();
504 1.64 simonb }
505 1.1 cgd }
506 1.1 cgd }
507 1.1 cgd *last_cmdp = cmd;
508 1.38 jhawk last_count = count;
509 1.1 cgd if (cmd != 0) {
510 1.1 cgd /*
511 1.64 simonb * Execute the command.
512 1.1 cgd */
513 1.64 simonb (*cmd->fcn)(addr, have_addr, count, modif);
514 1.64 simonb
515 1.64 simonb if (cmd->flag & CS_SET_DOT) {
516 1.64 simonb /*
517 1.64 simonb * If command changes dot, set dot to
518 1.64 simonb * previous address displayed (if 'ed' style).
519 1.64 simonb */
520 1.64 simonb if (db_ed_style)
521 1.64 simonb db_dot = db_prev;
522 1.64 simonb else
523 1.64 simonb db_dot = db_next;
524 1.64 simonb } else {
525 1.64 simonb /*
526 1.64 simonb * If command does not change dot,
527 1.64 simonb * set 'next' location to be the same.
528 1.64 simonb */
529 1.64 simonb db_next = db_dot;
530 1.1 cgd }
531 1.1 cgd }
532 1.1 cgd }
533 1.1 cgd
534 1.4 brezak /*ARGSUSED*/
535 1.64 simonb static void
536 1.94 matt db_map_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
537 1.90 christos const char *modif)
538 1.64 simonb {
539 1.95 thorpej bool full = false;
540 1.64 simonb
541 1.64 simonb if (modif[0] == 'f')
542 1.95 thorpej full = true;
543 1.60 matt
544 1.95 thorpej if (have_addr == false)
545 1.66 scw addr = (db_expr_t)(intptr_t) kernel_map;
546 1.4 brezak
547 1.66 scw uvm_map_printit((struct vm_map *)(intptr_t) addr, full, db_printf);
548 1.4 brezak }
549 1.4 brezak
550 1.4 brezak /*ARGSUSED*/
551 1.64 simonb static void
552 1.94 matt db_malloc_print_cmd(db_expr_t addr, bool have_addr,
553 1.91 christos db_expr_t count, const char *modif)
554 1.62 thorpej {
555 1.64 simonb
556 1.62 thorpej #ifdef MALLOC_DEBUG
557 1.62 thorpej if (!have_addr)
558 1.62 thorpej addr = 0;
559 1.62 thorpej
560 1.62 thorpej debug_malloc_printit(db_printf, (vaddr_t) addr);
561 1.62 thorpej #else
562 1.62 thorpej db_printf("The kernel is not built with the MALLOC_DEBUG option.\n");
563 1.62 thorpej #endif /* MALLOC_DEBUG */
564 1.62 thorpej }
565 1.62 thorpej
566 1.62 thorpej /*ARGSUSED*/
567 1.64 simonb static void
568 1.94 matt db_object_print_cmd(db_expr_t addr, bool have_addr,
569 1.91 christos db_expr_t count, const char *modif)
570 1.64 simonb {
571 1.95 thorpej bool full = false;
572 1.64 simonb
573 1.64 simonb if (modif[0] == 'f')
574 1.95 thorpej full = true;
575 1.4 brezak
576 1.66 scw uvm_object_printit((struct uvm_object *)(intptr_t) addr, full,
577 1.66 scw db_printf);
578 1.24 mrg }
579 1.24 mrg
580 1.24 mrg /*ARGSUSED*/
581 1.64 simonb static void
582 1.94 matt db_page_print_cmd(db_expr_t addr, bool have_addr,
583 1.91 christos db_expr_t count, const char *modif)
584 1.64 simonb {
585 1.95 thorpej bool full = false;
586 1.64 simonb
587 1.64 simonb if (modif[0] == 'f')
588 1.95 thorpej full = true;
589 1.24 mrg
590 1.66 scw uvm_page_printit((struct vm_page *)(intptr_t) addr, full, db_printf);
591 1.36 chs }
592 1.36 chs
593 1.36 chs /*ARGSUSED*/
594 1.64 simonb static void
595 1.94 matt db_show_all_pages(db_expr_t addr, bool have_addr,
596 1.91 christos db_expr_t count, const char *modif)
597 1.84 bjh21 {
598 1.84 bjh21
599 1.84 bjh21 uvm_page_printall(db_printf);
600 1.84 bjh21 }
601 1.84 bjh21
602 1.84 bjh21 /*ARGSUSED*/
603 1.84 bjh21 static void
604 1.94 matt db_buf_print_cmd(db_expr_t addr, bool have_addr,
605 1.91 christos db_expr_t count, const char *modif)
606 1.36 chs {
607 1.95 thorpej bool full = false;
608 1.64 simonb
609 1.36 chs if (modif[0] == 'f')
610 1.95 thorpej full = true;
611 1.36 chs
612 1.66 scw vfs_buf_print((struct buf *)(intptr_t) addr, full, db_printf);
613 1.65 simonb }
614 1.65 simonb
615 1.65 simonb /*ARGSUSED*/
616 1.65 simonb static void
617 1.94 matt db_event_print_cmd(db_expr_t addr, bool have_addr,
618 1.91 christos db_expr_t count, const char *modif)
619 1.65 simonb {
620 1.95 thorpej bool full = false;
621 1.65 simonb
622 1.65 simonb if (modif[0] == 'f')
623 1.95 thorpej full = true;
624 1.65 simonb
625 1.65 simonb event_print(full, db_printf);
626 1.36 chs }
627 1.36 chs
628 1.36 chs /*ARGSUSED*/
629 1.64 simonb static void
630 1.94 matt db_vnode_print_cmd(db_expr_t addr, bool have_addr,
631 1.91 christos db_expr_t count, const char *modif)
632 1.36 chs {
633 1.95 thorpej bool full = false;
634 1.64 simonb
635 1.36 chs if (modif[0] == 'f')
636 1.95 thorpej full = true;
637 1.36 chs
638 1.66 scw vfs_vnode_print((struct vnode *)(intptr_t) addr, full, db_printf);
639 1.74 dbj }
640 1.74 dbj
641 1.74 dbj static void
642 1.94 matt db_mount_print_cmd(db_expr_t addr, bool have_addr,
643 1.91 christos db_expr_t count, const char *modif)
644 1.74 dbj {
645 1.95 thorpej bool full = false;
646 1.74 dbj
647 1.74 dbj if (modif[0] == 'f')
648 1.95 thorpej full = true;
649 1.74 dbj
650 1.74 dbj vfs_mount_print((struct mount *)(intptr_t) addr, full, db_printf);
651 1.4 brezak }
652 1.4 brezak
653 1.31 thorpej /*ARGSUSED*/
654 1.64 simonb static void
655 1.94 matt db_mbuf_print_cmd(db_expr_t addr, bool have_addr,
656 1.91 christos db_expr_t count, const char *modif)
657 1.83 yamt {
658 1.83 yamt
659 1.83 yamt m_print((const struct mbuf *)(intptr_t) addr, modif, db_printf);
660 1.83 yamt }
661 1.83 yamt
662 1.83 yamt /*ARGSUSED*/
663 1.83 yamt static void
664 1.94 matt db_pool_print_cmd(db_expr_t addr, bool have_addr,
665 1.91 christos db_expr_t count, const char *modif)
666 1.31 thorpej {
667 1.64 simonb
668 1.66 scw pool_printit((struct pool *)(intptr_t) addr, modif, db_printf);
669 1.51 chs }
670 1.31 thorpej
671 1.51 chs /*ARGSUSED*/
672 1.64 simonb static void
673 1.94 matt db_namecache_print_cmd(db_expr_t addr, bool have_addr,
674 1.91 christos db_expr_t count, const char *modif)
675 1.51 chs {
676 1.64 simonb
677 1.66 scw namecache_print((struct vnode *)(intptr_t) addr, db_printf);
678 1.51 chs }
679 1.51 chs
680 1.51 chs /*ARGSUSED*/
681 1.64 simonb static void
682 1.94 matt db_uvmexp_print_cmd(db_expr_t addr, bool have_addr,
683 1.91 christos db_expr_t count, const char *modif)
684 1.51 chs {
685 1.64 simonb
686 1.51 chs uvmexp_print(db_printf);
687 1.31 thorpej }
688 1.31 thorpej
689 1.92 ad /*ARGSUSED*/
690 1.92 ad static void
691 1.94 matt db_lock_print_cmd(db_expr_t addr, bool have_addr,
692 1.92 ad db_expr_t count, const char *modif)
693 1.92 ad {
694 1.92 ad
695 1.92 ad lockdebug_lock_print((void *)addr, db_printf);
696 1.92 ad }
697 1.92 ad
698 1.1 cgd /*
699 1.1 cgd * Call random function:
700 1.1 cgd * !expr(arg,arg,arg)
701 1.1 cgd */
702 1.16 christos /*ARGSUSED*/
703 1.64 simonb static void
704 1.94 matt db_fncall(db_expr_t addr, bool have_addr,
705 1.91 christos db_expr_t count, const char *modif)
706 1.1 cgd {
707 1.1 cgd db_expr_t fn_addr;
708 1.1 cgd #define MAXARGS 11
709 1.1 cgd db_expr_t args[MAXARGS];
710 1.1 cgd int nargs = 0;
711 1.1 cgd db_expr_t retval;
712 1.64 simonb db_expr_t (*func)(db_expr_t, ...);
713 1.1 cgd int t;
714 1.1 cgd
715 1.1 cgd if (!db_expression(&fn_addr)) {
716 1.64 simonb db_printf("Bad function\n");
717 1.64 simonb db_flush_lex();
718 1.64 simonb return;
719 1.1 cgd }
720 1.66 scw func = (db_expr_t (*)(db_expr_t, ...))(intptr_t) fn_addr;
721 1.1 cgd
722 1.1 cgd t = db_read_token();
723 1.1 cgd if (t == tLPAREN) {
724 1.64 simonb if (db_expression(&args[0])) {
725 1.64 simonb nargs++;
726 1.64 simonb while ((t = db_read_token()) == tCOMMA) {
727 1.64 simonb if (nargs == MAXARGS) {
728 1.64 simonb db_printf("Too many arguments\n");
729 1.64 simonb db_flush_lex();
730 1.64 simonb return;
731 1.64 simonb }
732 1.64 simonb if (!db_expression(&args[nargs])) {
733 1.64 simonb db_printf("Argument missing\n");
734 1.64 simonb db_flush_lex();
735 1.64 simonb return;
736 1.64 simonb }
737 1.64 simonb nargs++;
738 1.64 simonb }
739 1.64 simonb db_unread_token(t);
740 1.64 simonb }
741 1.64 simonb if (db_read_token() != tRPAREN) {
742 1.64 simonb db_printf("?\n");
743 1.1 cgd db_flush_lex();
744 1.1 cgd return;
745 1.1 cgd }
746 1.1 cgd }
747 1.1 cgd db_skip_to_eol();
748 1.1 cgd
749 1.1 cgd while (nargs < MAXARGS) {
750 1.64 simonb args[nargs++] = 0;
751 1.1 cgd }
752 1.1 cgd
753 1.1 cgd retval = (*func)(args[0], args[1], args[2], args[3], args[4],
754 1.20 christos args[5], args[6], args[7], args[8], args[9]);
755 1.45 jhawk db_printf("%s\n", db_num_to_str(retval));
756 1.23 scottr }
757 1.23 scottr
758 1.64 simonb static void
759 1.94 matt db_reboot_cmd(db_expr_t addr, bool have_addr,
760 1.91 christos db_expr_t count, const char *modif)
761 1.23 scottr {
762 1.23 scottr db_expr_t bootflags;
763 1.23 scottr
764 1.23 scottr /* Flags, default to RB_AUTOBOOT */
765 1.23 scottr if (!db_expression(&bootflags))
766 1.23 scottr bootflags = (db_expr_t)RB_AUTOBOOT;
767 1.23 scottr if (db_read_token() != tEOL) {
768 1.64 simonb db_error("?\n");
769 1.64 simonb /*NOTREACHED*/
770 1.23 scottr }
771 1.47 sommerfe /*
772 1.47 sommerfe * We are leaving DDB, never to return upward.
773 1.47 sommerfe * Clear db_recover so that we can debug faults in functions
774 1.47 sommerfe * called from cpu_reboot.
775 1.47 sommerfe */
776 1.47 sommerfe db_recover = 0;
777 1.23 scottr cpu_reboot((int)bootflags, NULL);
778 1.41 jhawk }
779 1.41 jhawk
780 1.64 simonb static void
781 1.94 matt db_sifting_cmd(db_expr_t addr, bool have_addr,
782 1.91 christos db_expr_t count, const char *modif)
783 1.41 jhawk {
784 1.41 jhawk int mode, t;
785 1.41 jhawk
786 1.41 jhawk t = db_read_token();
787 1.41 jhawk if (t == tSLASH) {
788 1.41 jhawk t = db_read_token();
789 1.41 jhawk if (t != tIDENT) {
790 1.41 jhawk bad_modifier:
791 1.41 jhawk db_printf("Bad modifier\n");
792 1.41 jhawk db_flush_lex();
793 1.41 jhawk return;
794 1.41 jhawk }
795 1.41 jhawk if (!strcmp(db_tok_string, "F"))
796 1.41 jhawk mode = 'F';
797 1.41 jhawk else
798 1.41 jhawk goto bad_modifier;
799 1.41 jhawk t = db_read_token();
800 1.41 jhawk } else
801 1.41 jhawk mode = 0;
802 1.41 jhawk
803 1.51 chs if (t == tIDENT)
804 1.41 jhawk db_sifting(db_tok_string, mode);
805 1.41 jhawk else {
806 1.41 jhawk db_printf("Bad argument (non-string)\n");
807 1.41 jhawk db_flush_lex();
808 1.41 jhawk }
809 1.43 jhawk }
810 1.43 jhawk
811 1.64 simonb static void
812 1.94 matt db_stack_trace_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
813 1.43 jhawk {
814 1.79 drochner register const char *cp = modif;
815 1.70 atatat register char c;
816 1.70 atatat void (*pr)(const char *, ...);
817 1.70 atatat
818 1.70 atatat pr = db_printf;
819 1.70 atatat while ((c = *cp++) != 0)
820 1.70 atatat if (c == 'l')
821 1.70 atatat pr = printf;
822 1.64 simonb
823 1.43 jhawk if (count == -1)
824 1.43 jhawk count = 65535;
825 1.43 jhawk
826 1.70 atatat db_stack_trace_print(addr, have_addr, count, modif, pr);
827 1.32 lukem }
828 1.32 lukem
829 1.64 simonb static void
830 1.94 matt db_sync_cmd(db_expr_t addr, bool have_addr,
831 1.91 christos db_expr_t count, const char *modif)
832 1.32 lukem {
833 1.64 simonb
834 1.47 sommerfe /*
835 1.47 sommerfe * We are leaving DDB, never to return upward.
836 1.47 sommerfe * Clear db_recover so that we can debug faults in functions
837 1.47 sommerfe * called from cpu_reboot.
838 1.47 sommerfe */
839 1.47 sommerfe db_recover = 0;
840 1.32 lukem cpu_reboot(RB_DUMP, NULL);
841 1.1 cgd }
842