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