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