Deleted Added
full compact
db_command.c (78161) db_command.c (79418)
1/*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
25 *
1/*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
25 *
26 * $FreeBSD: head/sys/ddb/db_command.c 78161 2001-06-13 10:58:39Z peter $
26 * $FreeBSD: head/sys/ddb/db_command.c 79418 2001-07-08 04:56:07Z julian $
27 */
28
29/*
30 * Author: David B. Golub, Carnegie Mellon University
31 * Date: 7/90
32 */
33
34/*
35 * Command dispatcher.
36 */
37#include <sys/param.h>
38#include <sys/linker_set.h>
39#include <sys/reboot.h>
40#include <sys/systm.h>
41#include <sys/cons.h>
42
43#include <ddb/ddb.h>
44#include <ddb/db_command.h>
45#include <ddb/db_lex.h>
46#include <ddb/db_output.h>
47
27 */
28
29/*
30 * Author: David B. Golub, Carnegie Mellon University
31 * Date: 7/90
32 */
33
34/*
35 * Command dispatcher.
36 */
37#include <sys/param.h>
38#include <sys/linker_set.h>
39#include <sys/reboot.h>
40#include <sys/systm.h>
41#include <sys/cons.h>
42
43#include <ddb/ddb.h>
44#include <ddb/db_command.h>
45#include <ddb/db_lex.h>
46#include <ddb/db_output.h>
47
48#include
48#include <machine/setjmp.h>
49
50/*
51 * Exported global variables
52 */
53boolean_t db_cmd_loop_done;
54db_addr_t db_dot;
55jmp_buf db_jmpbuf;
56db_addr_t db_last_addr;
57db_addr_t db_prev;
58db_addr_t db_next;
59
60SET_DECLARE(db_cmd_set, struct command);
61SET_DECLARE(db_show_cmd_set, struct command);
62
63static db_cmdfcn_t db_fncall;
64static db_cmdfcn_t db_gdb;
65
66/* XXX this is actually forward-static. */
67extern struct command db_show_cmds[];
68
69/*
70 * if 'ed' style: 'dot' is set at start of last item printed,
71 * and '+' points to next line.
72 * Otherwise: 'dot' points to next item, '..' points to last.
73 */
74static boolean_t db_ed_style = TRUE;
75
76/*
77 * Utility routine - discard tokens through end-of-line.
78 */
79void
80db_skip_to_eol()
81{
82 int t;
83 do {
84 t = db_read_token();
85 } while (t != tEOL);
86}
87
88/*
89 * Results of command search.
90 */
91#define CMD_UNIQUE 0
92#define CMD_FOUND 1
93#define CMD_NONE 2
94#define CMD_AMBIGUOUS 3
95#define CMD_HELP 4
96
97static void db_cmd_list __P((struct command *table,
98 struct command **aux_tablep,
99 struct command **aux_tablep_end));
100static int db_cmd_search __P((char *name, struct command *table,
101 struct command **aux_tablep,
102 struct command **aux_tablep_end,
103 struct command **cmdp));
104static void db_command __P((struct command **last_cmdp,
105 struct command *cmd_table,
106 struct command **aux_cmd_tablep,
107 struct command **aux_cmd_tablep_end));
108
109/*
110 * Search for command prefix.
111 */
112static int
113db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
114 char * name;
115 struct command *table;
116 struct command **aux_tablep;
117 struct command **aux_tablep_end;
118 struct command **cmdp; /* out */
119{
120 struct command *cmd;
121 struct command **aux_cmdp;
122 int result = CMD_NONE;
123
124 for (cmd = table; cmd->name != 0; cmd++) {
125 register char *lp;
126 register char *rp;
127 register int c;
128
129 lp = name;
130 rp = cmd->name;
131 while ((c = *lp) == *rp) {
132 if (c == 0) {
133 /* complete match */
134 *cmdp = cmd;
135 return (CMD_UNIQUE);
136 }
137 lp++;
138 rp++;
139 }
140 if (c == 0) {
141 /* end of name, not end of command -
142 partial match */
143 if (result == CMD_FOUND) {
144 result = CMD_AMBIGUOUS;
145 /* but keep looking for a full match -
146 this lets us match single letters */
147 }
148 else {
149 *cmdp = cmd;
150 result = CMD_FOUND;
151 }
152 }
153 }
154 if (result == CMD_NONE && aux_tablep != 0)
155 /* XXX repeat too much code. */
156 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
157 register char *lp;
158 register char *rp;
159 register int c;
160
161 lp = name;
162 rp = (*aux_cmdp)->name;
163 while ((c = *lp) == *rp) {
164 if (c == 0) {
165 /* complete match */
166 *cmdp = *aux_cmdp;
167 return (CMD_UNIQUE);
168 }
169 lp++;
170 rp++;
171 }
172 if (c == 0) {
173 /* end of name, not end of command -
174 partial match */
175 if (result == CMD_FOUND) {
176 result = CMD_AMBIGUOUS;
177 /* but keep looking for a full match -
178 this lets us match single letters */
179 }
180 else {
181 *cmdp = *aux_cmdp;
182 result = CMD_FOUND;
183 }
184 }
185 }
186 if (result == CMD_NONE) {
187 /* check for 'help' */
188 if (name[0] == 'h' && name[1] == 'e'
189 && name[2] == 'l' && name[3] == 'p')
190 result = CMD_HELP;
191 }
192 return (result);
193}
194
195static void
196db_cmd_list(table, aux_tablep, aux_tablep_end)
197 struct command *table;
198 struct command **aux_tablep;
199 struct command **aux_tablep_end;
200{
201 register struct command *cmd;
202 register struct command **aux_cmdp;
203
204 for (cmd = table; cmd->name != 0; cmd++) {
205 db_printf("%-12s", cmd->name);
206 db_end_line();
207 }
208 if (aux_tablep == 0)
209 return;
210 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
211 db_printf("%-12s", (*aux_cmdp)->name);
212 db_end_line();
213 }
214}
215
216static void
217db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
218 struct command **last_cmdp; /* IN_OUT */
219 struct command *cmd_table;
220 struct command **aux_cmd_tablep;
221 struct command **aux_cmd_tablep_end;
222{
223 struct command *cmd;
224 int t;
225 char modif[TOK_STRING_SIZE];
226 db_expr_t addr, count;
227 boolean_t have_addr = FALSE;
228 int result;
229
230 t = db_read_token();
231 if (t == tEOL) {
232 /* empty line repeats last command, at 'next' */
233 cmd = *last_cmdp;
234 addr = (db_expr_t)db_next;
235 have_addr = FALSE;
236 count = 1;
237 modif[0] = '\0';
238 }
239 else if (t == tEXCL) {
240 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
241 return;
242 }
243 else if (t != tIDENT) {
244 db_printf("?\n");
245 db_flush_lex();
246 return;
247 }
248 else {
249 /*
250 * Search for command
251 */
252 while (cmd_table) {
253 result = db_cmd_search(db_tok_string,
254 cmd_table,
255 aux_cmd_tablep,
256 aux_cmd_tablep_end,
257 &cmd);
258 switch (result) {
259 case CMD_NONE:
260 db_printf("No such command\n");
261 db_flush_lex();
262 return;
263 case CMD_AMBIGUOUS:
264 db_printf("Ambiguous\n");
265 db_flush_lex();
266 return;
267 case CMD_HELP:
268 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
269 db_flush_lex();
270 return;
271 default:
272 break;
273 }
274 if ((cmd_table = cmd->more) != 0) {
275 /* XXX usually no more aux's. */
276 aux_cmd_tablep = 0;
277 if (cmd_table == db_show_cmds)
278 aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
279 aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
280
281 t = db_read_token();
282 if (t != tIDENT) {
283 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
284 db_flush_lex();
285 return;
286 }
287 }
288 }
289
290 if ((cmd->flag & CS_OWN) == 0) {
291 /*
292 * Standard syntax:
293 * command [/modifier] [addr] [,count]
294 */
295 t = db_read_token();
296 if (t == tSLASH) {
297 t = db_read_token();
298 if (t != tIDENT) {
299 db_printf("Bad modifier\n");
300 db_flush_lex();
301 return;
302 }
303 db_strcpy(modif, db_tok_string);
304 }
305 else {
306 db_unread_token(t);
307 modif[0] = '\0';
308 }
309
310 if (db_expression(&addr)) {
311 db_dot = (db_addr_t) addr;
312 db_last_addr = db_dot;
313 have_addr = TRUE;
314 }
315 else {
316 addr = (db_expr_t) db_dot;
317 have_addr = FALSE;
318 }
319 t = db_read_token();
320 if (t == tCOMMA) {
321 if (!db_expression(&count)) {
322 db_printf("Count missing\n");
323 db_flush_lex();
324 return;
325 }
326 }
327 else {
328 db_unread_token(t);
329 count = -1;
330 }
331 if ((cmd->flag & CS_MORE) == 0) {
332 db_skip_to_eol();
333 }
334 }
335 }
336 *last_cmdp = cmd;
337 if (cmd != 0) {
338 /*
339 * Execute the command.
340 */
341 (*cmd->fcn)(addr, have_addr, count, modif);
342
343 if (cmd->flag & CS_SET_DOT) {
344 /*
345 * If command changes dot, set dot to
346 * previous address displayed (if 'ed' style).
347 */
348 if (db_ed_style) {
349 db_dot = db_prev;
350 }
351 else {
352 db_dot = db_next;
353 }
354 }
355 else {
356 /*
357 * If command does not change dot,
358 * set 'next' location to be the same.
359 */
360 db_next = db_dot;
361 }
362 }
363}
364
365/*
366 * 'show' commands
367 */
368
369static struct command db_show_all_cmds[] = {
370#if 0
371 { "threads", db_show_all_threads, 0, 0 },
372#endif
373 { "procs", db_ps, 0, 0 },
374 { (char *)0 }
375};
376
377static struct command db_show_cmds[] = {
378 { "all", 0, 0, db_show_all_cmds },
379 { "registers", db_show_regs, 0, 0 },
380 { "breaks", db_listbreak_cmd, 0, 0 },
381#if 0
382 { "thread", db_show_one_thread, 0, 0 },
383#endif
384#if 0
385 { "port", ipc_port_print, 0, 0 },
386#endif
387 { (char *)0, }
388};
389
390static struct command db_command_table[] = {
391 { "print", db_print_cmd, 0, 0 },
392 { "p", db_print_cmd, 0, 0 },
393 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
394 { "x", db_examine_cmd, CS_SET_DOT, 0 },
395 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
396 { "set", db_set_cmd, CS_OWN, 0 },
397 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
398 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
399 { "delete", db_delete_cmd, 0, 0 },
400 { "d", db_delete_cmd, 0, 0 },
401 { "break", db_breakpoint_cmd, 0, 0 },
402 { "dwatch", db_deletewatch_cmd, 0, 0 },
403 { "watch", db_watchpoint_cmd, CS_MORE,0 },
404 { "step", db_single_step_cmd, 0, 0 },
405 { "s", db_single_step_cmd, 0, 0 },
406 { "continue", db_continue_cmd, 0, 0 },
407 { "c", db_continue_cmd, 0, 0 },
408 { "until", db_trace_until_call_cmd,0, 0 },
409 { "next", db_trace_until_matching_cmd,0, 0 },
410 { "match", db_trace_until_matching_cmd,0, 0 },
411 { "trace", db_stack_trace_cmd, 0, 0 },
412 { "call", db_fncall, CS_OWN, 0 },
413 { "show", 0, 0, db_show_cmds },
414 { "ps", db_ps, 0, 0 },
415 { "gdb", db_gdb, 0, 0 },
416 { (char *)0, }
417};
418
419static struct command *db_last_command = 0;
420
421#if 0
422void
423db_help_cmd()
424{
425 struct command *cmd = db_command_table;
426
427 while (cmd->name != 0) {
428 db_printf("%-12s", cmd->name);
429 db_end_line();
430 cmd++;
431 }
432}
433#endif
434
435/*
436 * At least one non-optional command must be implemented using
437 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
438 */
439DB_COMMAND(panic, db_panic)
440{
441 panic("from debugger");
442}
443
444void
445db_command_loop()
446{
447 /*
448 * Initialize 'prev' and 'next' to dot.
449 */
450 db_prev = db_dot;
451 db_next = db_dot;
452
453 db_cmd_loop_done = 0;
454 while (!db_cmd_loop_done) {
455
456 (void) setjmp(db_jmpbuf);
457 if (db_print_position() != 0)
458 db_printf("\n");
459
460 db_printf("db> ");
461 (void) db_read_line();
462
463 db_command(&db_last_command, db_command_table,
464 SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
465 }
466}
467
468void
469db_error(s)
470 char *s;
471{
472 if (s)
473 db_printf(s);
474 db_flush_lex();
475 longjmp(db_jmpbuf, 1);
476}
477
478
479/*
480 * Call random function:
481 * !expr(arg,arg,arg)
482 */
483static void
484db_fncall(dummy1, dummy2, dummy3, dummy4)
485 db_expr_t dummy1;
486 boolean_t dummy2;
487 db_expr_t dummy3;
488 char * dummy4;
489{
490 db_expr_t fn_addr;
491#define MAXARGS 11 /* XXX only 10 are passed */
492 db_expr_t args[MAXARGS];
493 int nargs = 0;
494 db_expr_t retval;
495 typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t,
496 db_expr_t, db_expr_t, db_expr_t,
497 db_expr_t, db_expr_t, db_expr_t,
498 db_expr_t));
499 fcn_10args_t *func;
500 int t;
501
502 if (!db_expression(&fn_addr)) {
503 db_printf("Bad function\n");
504 db_flush_lex();
505 return;
506 }
507 func = (fcn_10args_t *)fn_addr; /* XXX */
508
509 t = db_read_token();
510 if (t == tLPAREN) {
511 if (db_expression(&args[0])) {
512 nargs++;
513 while ((t = db_read_token()) == tCOMMA) {
514 if (nargs == MAXARGS) {
515 db_printf("Too many arguments\n");
516 db_flush_lex();
517 return;
518 }
519 if (!db_expression(&args[nargs])) {
520 db_printf("Argument missing\n");
521 db_flush_lex();
522 return;
523 }
524 nargs++;
525 }
526 db_unread_token(t);
527 }
528 if (db_read_token() != tRPAREN) {
529 db_printf("?\n");
530 db_flush_lex();
531 return;
532 }
533 }
534 db_skip_to_eol();
535
536 while (nargs < MAXARGS) {
537 args[nargs++] = 0;
538 }
539
540 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
541 args[5], args[6], args[7], args[8], args[9] );
542 db_printf("%#lr\n", (long)retval);
543}
544
545/* Enter GDB remote protocol debugger on the next trap. */
546
547dev_t gdbdev = NODEV;
548cn_getc_t *gdb_getc;
549cn_putc_t *gdb_putc;
550
551static void
552db_gdb (dummy1, dummy2, dummy3, dummy4)
553 db_expr_t dummy1;
554 boolean_t dummy2;
555 db_expr_t dummy3;
556 char * dummy4;
557{
558
559 if (gdbdev == NODEV) {
560 db_printf("No gdb port enabled. Set flag 0x80 on desired port\n");
561 db_printf("in your configuration file (currently sio only).\n");
562 return;
563 }
564 boothowto ^= RB_GDB;
565
566 db_printf("Next trap will enter %s\n",
567 boothowto & RB_GDB ? "GDB remote protocol mode"
568 : "DDB debugger");
569}
49
50/*
51 * Exported global variables
52 */
53boolean_t db_cmd_loop_done;
54db_addr_t db_dot;
55jmp_buf db_jmpbuf;
56db_addr_t db_last_addr;
57db_addr_t db_prev;
58db_addr_t db_next;
59
60SET_DECLARE(db_cmd_set, struct command);
61SET_DECLARE(db_show_cmd_set, struct command);
62
63static db_cmdfcn_t db_fncall;
64static db_cmdfcn_t db_gdb;
65
66/* XXX this is actually forward-static. */
67extern struct command db_show_cmds[];
68
69/*
70 * if 'ed' style: 'dot' is set at start of last item printed,
71 * and '+' points to next line.
72 * Otherwise: 'dot' points to next item, '..' points to last.
73 */
74static boolean_t db_ed_style = TRUE;
75
76/*
77 * Utility routine - discard tokens through end-of-line.
78 */
79void
80db_skip_to_eol()
81{
82 int t;
83 do {
84 t = db_read_token();
85 } while (t != tEOL);
86}
87
88/*
89 * Results of command search.
90 */
91#define CMD_UNIQUE 0
92#define CMD_FOUND 1
93#define CMD_NONE 2
94#define CMD_AMBIGUOUS 3
95#define CMD_HELP 4
96
97static void db_cmd_list __P((struct command *table,
98 struct command **aux_tablep,
99 struct command **aux_tablep_end));
100static int db_cmd_search __P((char *name, struct command *table,
101 struct command **aux_tablep,
102 struct command **aux_tablep_end,
103 struct command **cmdp));
104static void db_command __P((struct command **last_cmdp,
105 struct command *cmd_table,
106 struct command **aux_cmd_tablep,
107 struct command **aux_cmd_tablep_end));
108
109/*
110 * Search for command prefix.
111 */
112static int
113db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
114 char * name;
115 struct command *table;
116 struct command **aux_tablep;
117 struct command **aux_tablep_end;
118 struct command **cmdp; /* out */
119{
120 struct command *cmd;
121 struct command **aux_cmdp;
122 int result = CMD_NONE;
123
124 for (cmd = table; cmd->name != 0; cmd++) {
125 register char *lp;
126 register char *rp;
127 register int c;
128
129 lp = name;
130 rp = cmd->name;
131 while ((c = *lp) == *rp) {
132 if (c == 0) {
133 /* complete match */
134 *cmdp = cmd;
135 return (CMD_UNIQUE);
136 }
137 lp++;
138 rp++;
139 }
140 if (c == 0) {
141 /* end of name, not end of command -
142 partial match */
143 if (result == CMD_FOUND) {
144 result = CMD_AMBIGUOUS;
145 /* but keep looking for a full match -
146 this lets us match single letters */
147 }
148 else {
149 *cmdp = cmd;
150 result = CMD_FOUND;
151 }
152 }
153 }
154 if (result == CMD_NONE && aux_tablep != 0)
155 /* XXX repeat too much code. */
156 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
157 register char *lp;
158 register char *rp;
159 register int c;
160
161 lp = name;
162 rp = (*aux_cmdp)->name;
163 while ((c = *lp) == *rp) {
164 if (c == 0) {
165 /* complete match */
166 *cmdp = *aux_cmdp;
167 return (CMD_UNIQUE);
168 }
169 lp++;
170 rp++;
171 }
172 if (c == 0) {
173 /* end of name, not end of command -
174 partial match */
175 if (result == CMD_FOUND) {
176 result = CMD_AMBIGUOUS;
177 /* but keep looking for a full match -
178 this lets us match single letters */
179 }
180 else {
181 *cmdp = *aux_cmdp;
182 result = CMD_FOUND;
183 }
184 }
185 }
186 if (result == CMD_NONE) {
187 /* check for 'help' */
188 if (name[0] == 'h' && name[1] == 'e'
189 && name[2] == 'l' && name[3] == 'p')
190 result = CMD_HELP;
191 }
192 return (result);
193}
194
195static void
196db_cmd_list(table, aux_tablep, aux_tablep_end)
197 struct command *table;
198 struct command **aux_tablep;
199 struct command **aux_tablep_end;
200{
201 register struct command *cmd;
202 register struct command **aux_cmdp;
203
204 for (cmd = table; cmd->name != 0; cmd++) {
205 db_printf("%-12s", cmd->name);
206 db_end_line();
207 }
208 if (aux_tablep == 0)
209 return;
210 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
211 db_printf("%-12s", (*aux_cmdp)->name);
212 db_end_line();
213 }
214}
215
216static void
217db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
218 struct command **last_cmdp; /* IN_OUT */
219 struct command *cmd_table;
220 struct command **aux_cmd_tablep;
221 struct command **aux_cmd_tablep_end;
222{
223 struct command *cmd;
224 int t;
225 char modif[TOK_STRING_SIZE];
226 db_expr_t addr, count;
227 boolean_t have_addr = FALSE;
228 int result;
229
230 t = db_read_token();
231 if (t == tEOL) {
232 /* empty line repeats last command, at 'next' */
233 cmd = *last_cmdp;
234 addr = (db_expr_t)db_next;
235 have_addr = FALSE;
236 count = 1;
237 modif[0] = '\0';
238 }
239 else if (t == tEXCL) {
240 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
241 return;
242 }
243 else if (t != tIDENT) {
244 db_printf("?\n");
245 db_flush_lex();
246 return;
247 }
248 else {
249 /*
250 * Search for command
251 */
252 while (cmd_table) {
253 result = db_cmd_search(db_tok_string,
254 cmd_table,
255 aux_cmd_tablep,
256 aux_cmd_tablep_end,
257 &cmd);
258 switch (result) {
259 case CMD_NONE:
260 db_printf("No such command\n");
261 db_flush_lex();
262 return;
263 case CMD_AMBIGUOUS:
264 db_printf("Ambiguous\n");
265 db_flush_lex();
266 return;
267 case CMD_HELP:
268 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
269 db_flush_lex();
270 return;
271 default:
272 break;
273 }
274 if ((cmd_table = cmd->more) != 0) {
275 /* XXX usually no more aux's. */
276 aux_cmd_tablep = 0;
277 if (cmd_table == db_show_cmds)
278 aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
279 aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
280
281 t = db_read_token();
282 if (t != tIDENT) {
283 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
284 db_flush_lex();
285 return;
286 }
287 }
288 }
289
290 if ((cmd->flag & CS_OWN) == 0) {
291 /*
292 * Standard syntax:
293 * command [/modifier] [addr] [,count]
294 */
295 t = db_read_token();
296 if (t == tSLASH) {
297 t = db_read_token();
298 if (t != tIDENT) {
299 db_printf("Bad modifier\n");
300 db_flush_lex();
301 return;
302 }
303 db_strcpy(modif, db_tok_string);
304 }
305 else {
306 db_unread_token(t);
307 modif[0] = '\0';
308 }
309
310 if (db_expression(&addr)) {
311 db_dot = (db_addr_t) addr;
312 db_last_addr = db_dot;
313 have_addr = TRUE;
314 }
315 else {
316 addr = (db_expr_t) db_dot;
317 have_addr = FALSE;
318 }
319 t = db_read_token();
320 if (t == tCOMMA) {
321 if (!db_expression(&count)) {
322 db_printf("Count missing\n");
323 db_flush_lex();
324 return;
325 }
326 }
327 else {
328 db_unread_token(t);
329 count = -1;
330 }
331 if ((cmd->flag & CS_MORE) == 0) {
332 db_skip_to_eol();
333 }
334 }
335 }
336 *last_cmdp = cmd;
337 if (cmd != 0) {
338 /*
339 * Execute the command.
340 */
341 (*cmd->fcn)(addr, have_addr, count, modif);
342
343 if (cmd->flag & CS_SET_DOT) {
344 /*
345 * If command changes dot, set dot to
346 * previous address displayed (if 'ed' style).
347 */
348 if (db_ed_style) {
349 db_dot = db_prev;
350 }
351 else {
352 db_dot = db_next;
353 }
354 }
355 else {
356 /*
357 * If command does not change dot,
358 * set 'next' location to be the same.
359 */
360 db_next = db_dot;
361 }
362 }
363}
364
365/*
366 * 'show' commands
367 */
368
369static struct command db_show_all_cmds[] = {
370#if 0
371 { "threads", db_show_all_threads, 0, 0 },
372#endif
373 { "procs", db_ps, 0, 0 },
374 { (char *)0 }
375};
376
377static struct command db_show_cmds[] = {
378 { "all", 0, 0, db_show_all_cmds },
379 { "registers", db_show_regs, 0, 0 },
380 { "breaks", db_listbreak_cmd, 0, 0 },
381#if 0
382 { "thread", db_show_one_thread, 0, 0 },
383#endif
384#if 0
385 { "port", ipc_port_print, 0, 0 },
386#endif
387 { (char *)0, }
388};
389
390static struct command db_command_table[] = {
391 { "print", db_print_cmd, 0, 0 },
392 { "p", db_print_cmd, 0, 0 },
393 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
394 { "x", db_examine_cmd, CS_SET_DOT, 0 },
395 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
396 { "set", db_set_cmd, CS_OWN, 0 },
397 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
398 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
399 { "delete", db_delete_cmd, 0, 0 },
400 { "d", db_delete_cmd, 0, 0 },
401 { "break", db_breakpoint_cmd, 0, 0 },
402 { "dwatch", db_deletewatch_cmd, 0, 0 },
403 { "watch", db_watchpoint_cmd, CS_MORE,0 },
404 { "step", db_single_step_cmd, 0, 0 },
405 { "s", db_single_step_cmd, 0, 0 },
406 { "continue", db_continue_cmd, 0, 0 },
407 { "c", db_continue_cmd, 0, 0 },
408 { "until", db_trace_until_call_cmd,0, 0 },
409 { "next", db_trace_until_matching_cmd,0, 0 },
410 { "match", db_trace_until_matching_cmd,0, 0 },
411 { "trace", db_stack_trace_cmd, 0, 0 },
412 { "call", db_fncall, CS_OWN, 0 },
413 { "show", 0, 0, db_show_cmds },
414 { "ps", db_ps, 0, 0 },
415 { "gdb", db_gdb, 0, 0 },
416 { (char *)0, }
417};
418
419static struct command *db_last_command = 0;
420
421#if 0
422void
423db_help_cmd()
424{
425 struct command *cmd = db_command_table;
426
427 while (cmd->name != 0) {
428 db_printf("%-12s", cmd->name);
429 db_end_line();
430 cmd++;
431 }
432}
433#endif
434
435/*
436 * At least one non-optional command must be implemented using
437 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
438 */
439DB_COMMAND(panic, db_panic)
440{
441 panic("from debugger");
442}
443
444void
445db_command_loop()
446{
447 /*
448 * Initialize 'prev' and 'next' to dot.
449 */
450 db_prev = db_dot;
451 db_next = db_dot;
452
453 db_cmd_loop_done = 0;
454 while (!db_cmd_loop_done) {
455
456 (void) setjmp(db_jmpbuf);
457 if (db_print_position() != 0)
458 db_printf("\n");
459
460 db_printf("db> ");
461 (void) db_read_line();
462
463 db_command(&db_last_command, db_command_table,
464 SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
465 }
466}
467
468void
469db_error(s)
470 char *s;
471{
472 if (s)
473 db_printf(s);
474 db_flush_lex();
475 longjmp(db_jmpbuf, 1);
476}
477
478
479/*
480 * Call random function:
481 * !expr(arg,arg,arg)
482 */
483static void
484db_fncall(dummy1, dummy2, dummy3, dummy4)
485 db_expr_t dummy1;
486 boolean_t dummy2;
487 db_expr_t dummy3;
488 char * dummy4;
489{
490 db_expr_t fn_addr;
491#define MAXARGS 11 /* XXX only 10 are passed */
492 db_expr_t args[MAXARGS];
493 int nargs = 0;
494 db_expr_t retval;
495 typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t,
496 db_expr_t, db_expr_t, db_expr_t,
497 db_expr_t, db_expr_t, db_expr_t,
498 db_expr_t));
499 fcn_10args_t *func;
500 int t;
501
502 if (!db_expression(&fn_addr)) {
503 db_printf("Bad function\n");
504 db_flush_lex();
505 return;
506 }
507 func = (fcn_10args_t *)fn_addr; /* XXX */
508
509 t = db_read_token();
510 if (t == tLPAREN) {
511 if (db_expression(&args[0])) {
512 nargs++;
513 while ((t = db_read_token()) == tCOMMA) {
514 if (nargs == MAXARGS) {
515 db_printf("Too many arguments\n");
516 db_flush_lex();
517 return;
518 }
519 if (!db_expression(&args[nargs])) {
520 db_printf("Argument missing\n");
521 db_flush_lex();
522 return;
523 }
524 nargs++;
525 }
526 db_unread_token(t);
527 }
528 if (db_read_token() != tRPAREN) {
529 db_printf("?\n");
530 db_flush_lex();
531 return;
532 }
533 }
534 db_skip_to_eol();
535
536 while (nargs < MAXARGS) {
537 args[nargs++] = 0;
538 }
539
540 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
541 args[5], args[6], args[7], args[8], args[9] );
542 db_printf("%#lr\n", (long)retval);
543}
544
545/* Enter GDB remote protocol debugger on the next trap. */
546
547dev_t gdbdev = NODEV;
548cn_getc_t *gdb_getc;
549cn_putc_t *gdb_putc;
550
551static void
552db_gdb (dummy1, dummy2, dummy3, dummy4)
553 db_expr_t dummy1;
554 boolean_t dummy2;
555 db_expr_t dummy3;
556 char * dummy4;
557{
558
559 if (gdbdev == NODEV) {
560 db_printf("No gdb port enabled. Set flag 0x80 on desired port\n");
561 db_printf("in your configuration file (currently sio only).\n");
562 return;
563 }
564 boothowto ^= RB_GDB;
565
566 db_printf("Next trap will enter %s\n",
567 boothowto & RB_GDB ? "GDB remote protocol mode"
568 : "DDB debugger");
569}