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