kernel - Fix atomic op comparison
[dragonfly.git] / sys / ddb / db_command.c
blob9006e70cd61e4b044401ac3b663654cf39041ccc
1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
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.
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.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
26 * $FreeBSD: src/sys/ddb/db_command.c,v 1.34.2.2 2001/07/29 22:48:36 kris Exp $
30 * Author: David B. Golub, Carnegie Mellon University
31 * Date: 7/90
35 * Command dispatcher.
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>
43 #include <ddb/ddb.h>
44 #include <ddb/db_command.h>
45 #include <ddb/db_lex.h>
46 #include <ddb/db_output.h>
48 #include <machine/md_var.h> /* needed for db_reset() */
49 #include <machine/setjmp.h>
52 * Exported global variables
54 boolean_t db_cmd_loop_done;
55 db_addr_t db_dot;
56 jmp_buf db_jmpbuf;
57 db_addr_t db_last_addr;
58 db_addr_t db_prev;
59 db_addr_t db_next;
61 SET_DECLARE(db_cmd_set, struct command);
62 SET_DECLARE(db_show_cmd_set, struct command);
64 static db_cmdfcn_t db_fncall;
65 static db_cmdfcn_t db_gdb;
66 static db_cmdfcn_t db_reset;
69 * 'show' commands
72 static struct command db_show_all_cmds[] = {
73 #if 0
74 { "threads", db_show_all_threads, 0, NULL },
75 #endif
76 { "procs", db_ps, 0, NULL },
77 { NULL }
80 static struct command db_show_cmds[] = {
81 { "all", 0, 0, db_show_all_cmds },
82 { "registers", db_show_regs, 0, NULL },
83 { "breaks", db_listbreak_cmd, 0, NULL },
84 #if 0
85 { "thread", db_show_one_thread, 0, NULL },
86 #endif
87 #if 0
88 { "port", ipc_port_print, 0, NULL },
89 #endif
90 { NULL, }
93 static struct command db_command_table[] = {
94 { "print", db_print_cmd, 0, NULL },
95 { "p", db_print_cmd, 0, NULL },
96 { "examine", db_examine_cmd, CS_SET_DOT, NULL },
97 { "x", db_examine_cmd, CS_SET_DOT, NULL },
98 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL },
99 { "set", db_set_cmd, CS_OWN, NULL },
100 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
101 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL },
102 { "delete", db_delete_cmd, 0, NULL },
103 { "d", db_delete_cmd, 0, NULL },
104 { "break", db_breakpoint_cmd, 0, NULL },
105 { "dwatch", db_deletewatch_cmd, 0, NULL },
106 { "watch", db_watchpoint_cmd, CS_MORE,NULL },
107 { "dhwatch", db_deletehwatch_cmd, 0, NULL },
108 { "hwatch", db_hwatchpoint_cmd, 0, NULL },
109 { "step", db_single_step_cmd, 0, NULL },
110 { "s", db_single_step_cmd, 0, NULL },
111 { "continue", db_continue_cmd, 0, NULL },
112 { "c", db_continue_cmd, 0, NULL },
113 { "until", db_trace_until_call_cmd,0, NULL },
114 { "next", db_trace_until_matching_cmd,0, NULL },
115 { "match", db_trace_until_matching_cmd,0, NULL },
116 { "trace", db_stack_trace_cmd, 0, NULL },
117 { "where", db_stack_trace_cmd, 0, NULL },
118 { "call", db_fncall, CS_OWN, NULL },
119 { "show", 0, 0, db_show_cmds },
120 { "ps", db_ps, 0, NULL },
121 { "gdb", db_gdb, 0, NULL },
122 { "reset", db_reset, 0, NULL },
123 { NULL, }
126 static struct command *db_last_command = NULL;
129 * if 'ed' style: 'dot' is set at start of last item printed,
130 * and '+' points to next line.
131 * Otherwise: 'dot' points to next item, '..' points to last.
133 static boolean_t db_ed_style = TRUE;
136 * Utility routine - discard tokens through end-of-line.
138 void
139 db_skip_to_eol(void)
141 int t;
142 do {
143 t = db_read_token();
144 } while (t != tEOL);
148 * Results of command search.
150 #define CMD_UNIQUE 0
151 #define CMD_FOUND 1
152 #define CMD_NONE 2
153 #define CMD_AMBIGUOUS 3
154 #define CMD_HELP 4
156 static void db_cmd_list (struct command *table,
157 struct command **aux_tablep,
158 struct command **aux_tablep_end);
159 static int db_cmd_search (char *name, struct command *table,
160 struct command **aux_tablep,
161 struct command **aux_tablep_end,
162 struct command **cmdp);
163 static void db_command (struct command **last_cmdp,
164 struct command *cmd_table,
165 struct command **aux_cmd_tablep,
166 struct command **aux_cmd_tablep_end);
169 * Search for command prefix.
171 * Parameters:
172 * cmdp: out
174 static int
175 db_cmd_search(char *name, struct command *table, struct command **aux_tablep,
176 struct command **aux_tablep_end, struct command **cmdp)
178 struct command *cmd;
179 struct command **aux_cmdp;
180 int result = CMD_NONE;
182 for (cmd = table; cmd->name != 0; cmd++) {
183 char *lp;
184 char *rp;
185 int c;
187 lp = name;
188 rp = cmd->name;
189 while ((c = *lp) == *rp) {
190 if (c == 0) {
191 /* complete match */
192 *cmdp = cmd;
193 return (CMD_UNIQUE);
195 lp++;
196 rp++;
198 if (c == 0) {
199 /* end of name, not end of command -
200 partial match */
201 if (result == CMD_FOUND) {
202 result = CMD_AMBIGUOUS;
203 /* but keep looking for a full match -
204 this lets us match single letters */
206 else {
207 *cmdp = cmd;
208 result = CMD_FOUND;
212 if (result == CMD_NONE && aux_tablep != NULL)
213 /* XXX repeat too much code. */
214 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
215 char *lp;
216 char *rp;
217 int c;
219 lp = name;
220 rp = (*aux_cmdp)->name;
221 while ((c = *lp) == *rp) {
222 if (c == 0) {
223 /* complete match */
224 *cmdp = *aux_cmdp;
225 return (CMD_UNIQUE);
227 lp++;
228 rp++;
230 if (c == 0) {
231 /* end of name, not end of command -
232 partial match */
233 if (result == CMD_FOUND) {
234 result = CMD_AMBIGUOUS;
235 /* but keep looking for a full match -
236 this lets us match single letters */
238 else {
239 *cmdp = *aux_cmdp;
240 result = CMD_FOUND;
244 if (result == CMD_NONE) {
245 /* check for 'help' */
246 if (name[0] == 'h' && name[1] == 'e'
247 && name[2] == 'l' && name[3] == 'p')
248 result = CMD_HELP;
250 return (result);
253 static void
254 db_cmd_list(struct command *table, struct command **aux_tablep,
255 struct command **aux_tablep_end)
257 struct command *cmd;
258 struct command **aux_cmdp;
260 for (cmd = table; cmd->name != 0; cmd++) {
261 db_printf("%-12s", cmd->name);
262 db_end_line(12);
264 if (aux_tablep == NULL)
265 return;
266 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
267 db_printf("%-12s", (*aux_cmdp)->name);
268 db_end_line(12);
273 * Parameters:
274 * last_cmdp: IN_OUT
276 static void
277 db_command(struct command **last_cmdp, struct command *cmd_table,
278 struct command **aux_cmd_tablep, struct command **aux_cmd_tablep_end)
280 struct command *cmd;
281 int t;
282 char modif[TOK_STRING_SIZE];
283 db_expr_t addr, count;
284 boolean_t have_addr = FALSE;
285 int result;
287 cmd = NULL;
288 t = db_read_token();
289 if (t == tEOL) {
290 /* empty line repeats last command, at 'next' */
291 cmd = *last_cmdp;
292 addr = (db_expr_t)db_next;
293 have_addr = FALSE;
294 count = 1;
295 modif[0] = '\0';
297 else if (t == tEXCL) {
298 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, NULL);
299 return;
301 else if (t != tIDENT) {
302 db_printf("?\n");
303 db_flush_lex();
304 return;
306 else {
308 * Search for command
310 while (cmd_table) {
311 result = db_cmd_search(db_tok_string,
312 cmd_table,
313 aux_cmd_tablep,
314 aux_cmd_tablep_end,
315 &cmd);
316 switch (result) {
317 case CMD_NONE:
318 db_printf("No such command\n");
319 db_flush_lex();
320 return;
321 case CMD_AMBIGUOUS:
322 db_printf("Ambiguous\n");
323 db_flush_lex();
324 return;
325 case CMD_HELP:
326 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
327 db_flush_lex();
328 return;
329 default:
330 break;
332 if ((cmd_table = cmd->more) != NULL) {
333 /* XXX usually no more aux's. */
334 aux_cmd_tablep = NULL;
335 if (cmd_table == db_show_cmds) {
336 aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
337 aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
340 t = db_read_token();
341 if (t != tIDENT) {
342 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
343 db_flush_lex();
344 return;
349 if ((cmd->flag & CS_OWN) == 0) {
351 * Standard syntax:
352 * command [/modifier] [addr] [,count]
354 t = db_read_token();
355 if (t == tSLASH) {
356 t = db_read_token();
357 if (t != tIDENT && t != tNUMBER) {
358 db_printf("Bad modifier\n");
359 db_flush_lex();
360 return;
362 db_strcpy(modif, db_tok_string);
364 else {
365 db_unread_token(t);
366 modif[0] = '\0';
369 if (db_expression(&addr)) {
370 db_dot = (db_addr_t) addr;
371 db_last_addr = db_dot;
372 have_addr = TRUE;
374 else {
375 addr = (db_expr_t) db_dot;
376 have_addr = FALSE;
378 t = db_read_token();
379 if (t == tCOMMA) {
380 if (!db_expression(&count)) {
381 db_printf("Count missing\n");
382 db_flush_lex();
383 return;
386 else {
387 db_unread_token(t);
388 count = -1;
390 if ((cmd->flag & CS_MORE) == 0) {
391 db_skip_to_eol();
395 *last_cmdp = cmd;
396 if (cmd != NULL) {
398 * Execute the command.
400 (*cmd->fcn)(addr, have_addr, count, modif);
402 if (cmd->flag & CS_SET_DOT) {
404 * If command changes dot, set dot to
405 * previous address displayed (if 'ed' style).
407 if (db_ed_style) {
408 db_dot = db_prev;
410 else {
411 db_dot = db_next;
414 else {
416 * If command does not change dot,
417 * set 'next' location to be the same.
419 db_next = db_dot;
424 #if 0
425 void
426 db_help_cmd(void)
428 struct command *cmd = db_command_table;
430 while (cmd->name != 0) {
431 db_printf("%-12s", cmd->name);
432 db_end_line(12);
433 cmd++;
436 #endif
439 * At least one non-optional command must be implemented using
440 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
442 DB_COMMAND(panic, db_panic)
444 panic("from debugger");
447 void
448 db_command_loop(void)
451 * Initialize 'prev' and 'next' to dot.
453 cnpoll(TRUE);
454 db_prev = db_dot;
455 db_next = db_dot;
457 db_cmd_loop_done = 0;
458 while (!db_cmd_loop_done) {
460 setjmp(db_jmpbuf);
461 if (db_print_position() != 0)
462 db_printf("\n");
464 db_printf("db> ");
465 db_read_line();
467 db_command(&db_last_command, db_command_table,
468 SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
470 cnpoll(FALSE);
473 void
474 db_error(char *s)
476 if (s)
477 db_printf("%s", s);
478 db_flush_lex();
479 longjmp(db_jmpbuf, 1);
484 * Call random function:
485 * !expr(arg,arg,arg)
487 typedef union {
488 db_expr_t (*a0)(void);
489 db_expr_t (*a1)(db_expr_t);
490 db_expr_t (*a2)(db_expr_t, db_expr_t);
491 db_expr_t (*a3)(db_expr_t, db_expr_t, db_expr_t);
492 db_expr_t (*a4)(db_expr_t, db_expr_t, db_expr_t, db_expr_t);
493 db_expr_t (*a5)(db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
494 db_expr_t (*a6)(db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t,
495 db_expr_t);
496 db_expr_t (*a7)(db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t,
497 db_expr_t, db_expr_t);
498 db_expr_t (*a8)(db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t,
499 db_expr_t, db_expr_t, db_expr_t);
500 db_expr_t (*a9)(db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t,
501 db_expr_t, db_expr_t, db_expr_t, db_expr_t);
502 db_expr_t (*a10)(db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t,
503 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
504 } fcn_multiargs_t;
507 static void
508 db_fncall(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
510 db_expr_t fn_addr;
511 #define MAXARGS 11 /* XXX only 10 are passed */
512 db_expr_t args[MAXARGS];
513 int nargs = 0;
514 db_expr_t retval;
515 fcn_multiargs_t func;
516 int t;
518 if (!db_expression(&fn_addr)) {
519 db_printf("Bad function\n");
520 db_flush_lex();
521 return;
523 func.a0 = (void *)fn_addr;
525 t = db_read_token();
526 if (t == tLPAREN) {
527 if (db_expression(&args[0])) {
528 nargs++;
529 while ((t = db_read_token()) == tCOMMA) {
530 if (nargs == MAXARGS) {
531 db_printf("Too many arguments\n");
532 db_flush_lex();
533 return;
535 if (!db_expression(&args[nargs])) {
536 db_printf("Argument missing\n");
537 db_flush_lex();
538 return;
540 nargs++;
542 db_unread_token(t);
544 if (db_read_token() != tRPAREN) {
545 db_printf("?\n");
546 db_flush_lex();
547 return;
550 db_skip_to_eol();
553 * This is mainly so kgdb doesn't get confused if ddb> call is
554 * in its backtrace.
556 switch(nargs) {
557 case 0:
558 retval = (*func.a0)();
559 break;
560 case 1:
561 retval = (*func.a1)(args[0]);
562 break;
563 case 2:
564 retval = (*func.a2)(args[0], args[1]);
565 break;
566 case 3:
567 retval = (*func.a3)(args[0], args[1], args[2]);
568 break;
569 case 4:
570 retval = (*func.a4)(args[0], args[1], args[2], args[3]);
571 break;
572 case 5:
573 retval = (*func.a5)(args[0], args[1], args[2], args[3], args[4]);
574 break;
575 case 6:
576 retval = (*func.a6)(args[0], args[1], args[2], args[3], args[4],
577 args[5]);
578 break;
579 case 7:
580 retval = (*func.a7)(args[0], args[1], args[2], args[3], args[4],
581 args[5], args[6]);
582 break;
583 case 8:
584 retval = (*func.a8)(args[0], args[1], args[2], args[3], args[4],
585 args[5], args[6], args[7]);
586 break;
587 case 9:
588 retval = (*func.a9)(args[0], args[1], args[2], args[3], args[4],
589 args[5], args[6], args[7], args[8]);
590 break;
591 case 10:
592 retval = (*func.a10)(args[0], args[1], args[2], args[3], args[4],
593 args[5], args[6], args[7], args[8], args[9]);
594 break;
595 default:
596 db_printf("too many arguments\n");
597 retval = 0;
598 break;
600 db_printf("%#lr\n", (long)retval);
603 /* Enter GDB remote protocol debugger on the next trap. */
605 static void
606 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
608 if (gdb_tab == NULL) {
609 db_printf("No gdb port enabled. Set flag 0x80 on desired port\n");
610 db_printf("in your configuration file (currently sio only).\n");
611 return;
613 boothowto ^= RB_GDB;
615 db_printf("Next trap will enter %s\n",
616 boothowto & RB_GDB ? "GDB remote protocol mode"
617 : "DDB debugger");
620 static void
621 db_reset(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char * dummy4)
623 cpu_reset();