db_command.c revision 1.101 1 /* $NetBSD: db_command.c,v 1.101 2007/09/23 19:51:20 martin Exp $ */
2 /*
3 * Mach Operating System
4 * Copyright (c) 1991,1990 Carnegie Mellon University
5 * All Rights Reserved.
6 *
7 * Permission to use, copy, modify and distribute this software and its
8 * documentation is hereby granted, provided that both the copyright
9 * notice and this permission notice appear in all copies of the
10 * software, derivative works or modified versions, and any portions
11 * thereof, and that both notices appear in supporting documentation.
12 *
13 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
14 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
15 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 *
17 * Carnegie Mellon requests users of this software to return to
18 *
19 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
20 * School of Computer Science
21 * Carnegie Mellon University
22 * Pittsburgh PA 15213-3890
23 *
24 * any improvements or extensions that they make and grant Carnegie the
25 * rights to redistribute these changes.
26 */
27 /*
28 * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
29 * All rights reserved.
30 *
31 * This code is derived from software contributed to The NetBSD Foundation
32 * by Adam Hamsik.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the NetBSD
45 * Foundation, Inc. and its contributors.
46 * 4. Neither the name of The NetBSD Foundation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
61 */
62
63 /*
64 * Command dispatcher.
65 */
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.101 2007/09/23 19:51:20 martin Exp $");
69
70 #include "opt_ddb.h"
71 #include "opt_kgdb.h"
72 #include "opt_inet.h"
73 #include "opt_ddbparam.h"
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/reboot.h>
78 #include <sys/device.h>
79 #include <sys/malloc.h>
80 #include <sys/mbuf.h>
81 #include <sys/namei.h>
82 #include <sys/pool.h>
83 #include <sys/proc.h>
84 #include <sys/vnode.h>
85 #include <sys/lockdebug.h>
86 #include <sys/sleepq.h>
87
88 /*include queue macros*/
89 #include <sys/queue.h>
90
91 #include <machine/db_machdep.h> /* type definitions */
92
93 #if defined(_KERNEL_OPT)
94 #include "opt_multiprocessor.h"
95 #endif
96 #ifdef MULTIPROCESSOR
97 #include <machine/cpu.h>
98 #endif
99
100 #include <ddb/db_lex.h>
101 #include <ddb/db_output.h>
102 #include <ddb/db_command.h>
103 #include <ddb/db_break.h>
104 #include <ddb/db_watch.h>
105 #include <ddb/db_run.h>
106 #include <ddb/db_variables.h>
107 #include <ddb/db_interface.h>
108 #include <ddb/db_sym.h>
109 #include <ddb/db_extern.h>
110 #include <ddb/db_command_list.h>
111
112 #include <uvm/uvm_extern.h>
113 #include <uvm/uvm_ddb.h>
114
115 #include "arp.h"
116
117 /*
118 * Results of command search.
119 */
120 #define CMD_UNIQUE 0
121 #define CMD_FOUND 1
122 #define CMD_NONE 2
123 #define CMD_AMBIGUOUS 3
124
125 /*
126 * Exported global variables
127 */
128 bool db_cmd_loop_done;
129 label_t *db_recover;
130 db_addr_t db_dot;
131 db_addr_t db_last_addr;
132 db_addr_t db_prev;
133 db_addr_t db_next;
134
135
136 /*
137 New DDB api for adding and removing commands uses three lists, because
138 we use two types of commands
139 a) standard commands without subcommands -> reboot
140 b) show commands which are subcommands of show command -> show aio_jobs
141 c) if defined machine specific commands
142
143 ddb_add_cmd, ddb_rem_cmd use type (DDB_SHOW_CMD||DDB_BASE_CMD)argument to
144 add them to representativ lists.
145 */
146
147 /*head of base commands list*/
148 static struct db_cmd_tbl_en_head db_base_cmd_list =
149 TAILQ_HEAD_INITIALIZER(db_base_cmd_list);
150
151 /*head of show commands list*/
152 static struct db_cmd_tbl_en_head db_show_cmd_list =
153 TAILQ_HEAD_INITIALIZER(db_show_cmd_list);
154
155 #ifdef DB_MACHINE_COMMANDS
156 /*head of machine commands list*/
157 static struct db_cmd_tbl_en_head db_mach_cmd_list =
158 TAILQ_HEAD_INITIALIZER(db_mach_cmd_list);
159 #endif
160
161 /*
162 * if 'ed' style: 'dot' is set at start of last item printed,
163 * and '+' points to next line.
164 * Otherwise: 'dot' points to next item, '..' points to last.
165 */
166 static bool db_ed_style = true;
167
168 static void db_cmd_list(const struct db_cmd_tbl_en_head *);
169 static int db_cmd_search(const char *, const struct db_command *,
170 const struct db_command **);
171 static void db_command(const struct db_command **);
172 static void db_buf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
173 static void db_event_print_cmd(db_expr_t, bool, db_expr_t, const char *);
174 static void db_fncall(db_expr_t, bool, db_expr_t, const char *);
175 static int db_get_list_type(const char *);
176 static void db_help_print_cmd(db_expr_t, bool, db_expr_t, const char *);
177 static void db_init_commands(void);
178 static void db_lock_print_cmd(db_expr_t, bool, db_expr_t, const char *);
179 static void db_mount_print_cmd(db_expr_t, bool, db_expr_t, const char *);
180 static void db_mbuf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
181 static void db_malloc_print_cmd(db_expr_t, bool, db_expr_t, const char *);
182 static void db_map_print_cmd(db_expr_t, bool, db_expr_t, const char *);
183 static void db_namecache_print_cmd(db_expr_t, bool, db_expr_t,
184 const char *);
185 static void db_object_print_cmd(db_expr_t, bool, db_expr_t, const char *);
186 static void db_page_print_cmd(db_expr_t, bool, db_expr_t, const char *);
187 static void db_show_all_pages(db_expr_t, bool, db_expr_t, const char *);
188 static void db_pool_print_cmd(db_expr_t, bool, db_expr_t, const char *);
189 static void db_reboot_cmd(db_expr_t, bool, db_expr_t, const char *);
190 static void db_sifting_cmd(db_expr_t, bool, db_expr_t, const char *);
191 static void db_stack_trace_cmd(db_expr_t, bool, db_expr_t, const char *);
192 static void db_sync_cmd(db_expr_t, bool, db_expr_t, const char *);
193 static void db_uvmexp_print_cmd(db_expr_t, bool, db_expr_t, const char *);
194 static void db_vnode_print_cmd(db_expr_t, bool, db_expr_t, const char *);
195
196 static const struct db_command db_show_cmds[] = {
197 /*added from all sub cmds*/
198 { DDB_ADD_CMD("callout", db_show_callout,
199 0 ,"List all used callout functions.",NULL,NULL) },
200 { DDB_ADD_CMD("pages", db_show_all_pages,
201 0 ,"List all used memory pages.",NULL,NULL) },
202 { DDB_ADD_CMD("procs", db_show_all_procs,
203 0 ,"List all processes.",NULL,NULL) },
204 { DDB_ADD_CMD("pools", db_show_all_pools,
205 0 ,"Show all poolS",NULL,NULL) },
206 /*added from all sub cmds*/
207 { DDB_ADD_CMD("aio_jobs", db_show_aio_jobs, 0,
208 "Show aio jobs",NULL,NULL) },
209 { DDB_ADD_CMD("all", NULL,
210 CS_COMPAT, NULL,NULL,NULL) },
211 #if defined(INET) && (NARP > 0)
212 { DDB_ADD_CMD("arptab", db_show_arptab, 0,NULL,NULL,NULL) },
213 #endif
214 { DDB_ADD_CMD("breaks", db_listbreak_cmd, 0,
215 "Display all breaks.",NULL,NULL) },
216 { DDB_ADD_CMD("buf", db_buf_print_cmd, 0,
217 "Print the struct buf at address.", "[/f] address",NULL) },
218 { DDB_ADD_CMD("event", db_event_print_cmd, 0,
219 "Print all the non-zero evcnt(9) event counters.", "[/f]",NULL) },
220 { DDB_ADD_CMD("lock", db_lock_print_cmd, 0,NULL,NULL,NULL) },
221 { DDB_ADD_CMD("malloc", db_malloc_print_cmd,0,NULL,NULL,NULL) },
222 { DDB_ADD_CMD("map", db_map_print_cmd, 0,
223 "Print the vm_map at address.", "[/f] address",NULL) },
224 { DDB_ADD_CMD("mount", db_mount_print_cmd, 0,
225 "Print the mount structure at address.", "[/f] address",NULL) },
226 { DDB_ADD_CMD("mbuf", db_mbuf_print_cmd, 0,NULL,NULL,
227 "-c prints all mbuf chains") },
228 { DDB_ADD_CMD("ncache", db_namecache_print_cmd, 0,
229 "Dump the namecache list.", "address",NULL) },
230 { DDB_ADD_CMD("object", db_object_print_cmd, 0,
231 "Print the vm_object at address.", "[/f] address",NULL) },
232 { DDB_ADD_CMD("page", db_page_print_cmd, 0,
233 "Print the vm_page at address.", "[/f] address",NULL) },
234 { DDB_ADD_CMD("pool", db_pool_print_cmd, 0,
235 "Print the pool at address.", "[/clp] address",NULL) },
236 { DDB_ADD_CMD("registers", db_show_regs, 0,
237 "Display the register set.", "[/u]",NULL) },
238 { DDB_ADD_CMD("sched_qs", db_show_sched_qs, 0,
239 "Print the state of the scheduler's run queues.",
240 NULL,NULL) },
241 { DDB_ADD_CMD("uvmexp", db_uvmexp_print_cmd, 0,
242 "Print a selection of UVM counters and statistics.",
243 NULL,NULL) },
244 { DDB_ADD_CMD("vnode", db_vnode_print_cmd, 0,
245 "Print the vnode at address.", "[/f] address",NULL) },
246 { DDB_ADD_CMD("watches", db_listwatch_cmd, 0,
247 "Display all watchpoints.", NULL,NULL) },
248 { DDB_ADD_CMD(NULL, NULL, 0,NULL,NULL,NULL) }
249 };
250
251 /* arch/<arch>/<arch>/db_interface.c */
252 #ifdef DB_MACHINE_COMMANDS
253 extern const struct db_command db_machine_command_table[];
254 #endif
255
256 static const struct db_command db_command_table[] = {
257 { DDB_ADD_CMD("b", db_breakpoint_cmd, 0,
258 "Set a breakpoint at address", "[/u] address[,count].",NULL) },
259 { DDB_ADD_CMD("break", db_breakpoint_cmd, 0,
260 "Set a breakpoint at address", "[/u] address[,count].",NULL) },
261 { DDB_ADD_CMD("bt", db_stack_trace_cmd, 0,
262 "Show backtrace.", "See help trace.",NULL) },
263 { DDB_ADD_CMD("c", db_continue_cmd, 0,
264 "Continue execution.", "[/c]",NULL) },
265 { DDB_ADD_CMD("call", db_fncall, CS_OWN,
266 "Call the function", "address[(expression[,...])]",NULL) },
267 { DDB_ADD_CMD("callout", db_show_callout, 0, NULL,
268 NULL,NULL ) },
269 { DDB_ADD_CMD("continue", db_continue_cmd, 0,
270 "Continue execution.", "[/c]",NULL) },
271 { DDB_ADD_CMD("d", db_delete_cmd, 0,
272 "Delete a breakpoint.", "address | #number",NULL) },
273 { DDB_ADD_CMD("delete", db_delete_cmd, 0,
274 "Delete a breakpoint.", "address | #number",NULL) },
275 { DDB_ADD_CMD("dmesg", db_dmesg, 0,
276 "Show kernel message buffer.", "[count]",NULL) },
277 { DDB_ADD_CMD("dwatch", db_deletewatch_cmd, 0,
278 "Delete the watchpoint.", "address",NULL) },
279 { DDB_ADD_CMD("examine", db_examine_cmd, CS_SET_DOT,
280 "Display the address locations.",
281 "[/modifier] address[,count]",NULL) },
282 { DDB_ADD_CMD("help", db_help_print_cmd, CS_OWN|CS_NOREPEAT,
283 "Display help about commands",
284 "Use other commands as arguments.",NULL) },
285 { DDB_ADD_CMD("kill", db_kill_proc, CS_OWN,
286 "Send a signal to the process","pid[,signal_number]",
287 " pid:\t\t\tthe process id (may need 0t prefix for decimal)\n"
288 " signal_number:\tthe signal to send") },
289 #ifdef KGDB
290 { DDB_ADD_CMD("kgdb", db_kgdb_cmd, 0, NULL,NULL,NULL) },
291 #endif
292 #ifdef DB_MACHINE_COMMANDS
293 { DDB_ADD_CMD("machine",NULL,CS_MACH,
294 "Architecture specific functions.",NULL,NULL) },
295 #endif
296 { DDB_ADD_CMD("match", db_trace_until_matching_cmd,0,
297 "Stop at the matching return instruction.","See help next",NULL) },
298 { DDB_ADD_CMD("next", db_trace_until_matching_cmd,0,
299 "Stop at the matching return instruction.","[/p]",NULL) },
300 { DDB_ADD_CMD("p", db_print_cmd, 0,
301 "Print address according to the format.",
302 "[/axzodurc] address [address ...]",NULL) },
303 { DDB_ADD_CMD("print", db_print_cmd, 0,
304 "Print address according to the format.",
305 "[/axzodurc] address [address ...]",NULL) },
306 { DDB_ADD_CMD("ps", db_show_all_procs, 0,
307 "Print all processes.","See show all procs",NULL) },
308 { DDB_ADD_CMD("reboot", db_reboot_cmd, CS_OWN,
309 "Reboot","0x1 RB_ASKNAME, 0x2 RB_SINGLE, 0x4 RB_NOSYNC, 0x8 RB_HALT,"
310 "0x40 RB_KDB, 0x100 RB_DUMP, 0x808 RB_POWERDOWN",NULL) },
311 { DDB_ADD_CMD("s", db_single_step_cmd, 0,
312 "Single-step count times.","[/p] [,count]",NULL) },
313 { DDB_ADD_CMD("search", db_search_cmd, CS_OWN|CS_SET_DOT,
314 "Search memory from address for value.",
315 "[/bhl] address value [mask] [,count]",NULL) },
316 { DDB_ADD_CMD("set", db_set_cmd, CS_OWN,
317 "Set the named variable","$variable [=] expression",NULL) },
318 { DDB_ADD_CMD("show", NULL ,CS_SHOW,
319 "Show kernel stats.", NULL,NULL) },
320 { DDB_ADD_CMD("sifting", db_sifting_cmd, CS_OWN,
321 "Search the symbol tables ","[/F] string",NULL) },
322 { DDB_ADD_CMD("step", db_single_step_cmd, 0,
323 "Single-step count times.","[/p] [,count]",NULL) },
324 { DDB_ADD_CMD("sync", db_sync_cmd, CS_OWN,
325 "Force a crash dump, and then reboot.",NULL,NULL) },
326 { DDB_ADD_CMD("trace", db_stack_trace_cmd, 0,
327 "Stack trace from frame-address.",
328 "[/u[l]] [frame-address][,count]",NULL) },
329 { DDB_ADD_CMD("until", db_trace_until_call_cmd,0,
330 "Stop at the next call or return instruction.","[/p]",NULL) },
331 { DDB_ADD_CMD("w", db_write_cmd, CS_MORE|CS_SET_DOT,
332 "Set a watchpoint for a region. ","address[,size]",NULL) },
333 { DDB_ADD_CMD("watch", db_watchpoint_cmd, CS_MORE,
334 "Set a watchpoint for a region. ","address[,size]",NULL) },
335 { DDB_ADD_CMD("write", db_write_cmd, CS_MORE|CS_SET_DOT,
336 "Write the expressions at succeeding locations.",
337 "[/bhl] address expression [expression ...]",NULL) },
338 { DDB_ADD_CMD("x", db_examine_cmd, CS_SET_DOT,
339 "Display the address locations.",
340 "[/modifier] address[,count]",NULL) },
341 { DDB_ADD_CMD(NULL, NULL, 0, NULL, NULL, NULL) }
342 };
343
344 static const struct db_command *db_last_command = NULL;
345 #if defined(DDB_COMMANDONENTER)
346 char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = ___STRING(DDB_COMMANDONENTER);
347 #else /* defined(DDB_COMMANDONENTER) */
348 char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = "";
349 #endif /* defined(DDB_COMMANDONENTER) */
350 #define DB_LINE_SEP ';'
351
352 /*
353 * Utility routine - discard tokens through end-of-line.
354 */
355 void
356 db_skip_to_eol(void)
357 {
358 int t;
359
360 do {
361 t = db_read_token();
362 } while (t != tEOL);
363 }
364
365 void
366 db_error(const char *s)
367 {
368
369 if (s)
370 db_printf("%s", s);
371 db_flush_lex();
372 longjmp(db_recover);
373 }
374
375 /*Execute commandlist after ddb start
376 *This function goes through the command list created from commands and ';'
377 */
378
379 static void
380 db_execute_commandlist(const char *cmdlist)
381 {
382 const char *cmd = cmdlist;
383 const struct db_command *dummy = NULL;
384
385 while (*cmd != '\0') {
386 const char *ep = cmd;
387
388 while (*ep != '\0' && *ep != DB_LINE_SEP) {
389 ep++;
390 }
391 db_set_line(cmd, ep);
392 db_command(&dummy);
393 cmd = ep;
394 if (*cmd == DB_LINE_SEP) {
395 cmd++;
396 }
397 }
398 }
399
400 /*Initialize ddb command tables*/
401 static void
402 db_init_commands(void)
403 {
404 static bool done = false;
405
406 if (done) return;
407 done = true;
408
409 /*register command tables*/
410 (void)db_register_tbl(DDB_BASE_CMD,db_command_table);
411
412 #ifdef DB_MACHINE_COMMANDS
413 (void)db_register_tbl(DDB_MACH_CMD,db_machine_command_table);
414 #endif
415 (void)db_register_tbl(DDB_SHOW_CMD,db_show_cmds);
416 }
417
418
419 /*
420 * Add command table to the specified list
421 * Arg:
422 * int type specifies type of command table DDB_SHOW_CMD|DDB_BASE_CMD|DDB_MAC_CMD
423 * *cmd_tbl poiter to static allocated db_command table
424 *
425 *Command table must be NULL terminated array of struct db_command
426 */
427 int
428 db_register_tbl(uint8_t type, const struct db_command *cmd_tbl)
429 {
430 /*XXX what is better count number of elements here or use num argument ?*/
431 uint32_t i=0;
432 struct db_cmd_tbl_en *list_ent;
433 const struct db_command *cmd;
434 struct db_cmd_tbl_en_head *list;
435
436 /*I have to check this because e.g. machine commands can be NULL*/
437 if (cmd_tbl->name != 0) {
438
439 if ((list_ent = malloc(sizeof(struct db_cmd_tbl_en),M_TEMP,M_ZERO))
440 == NULL)
441 return(ENOMEM);
442
443 list_ent->db_cmd=cmd_tbl;
444
445
446 for (cmd = cmd_tbl; cmd->name != 0; cmd++)
447 i++;
448
449 list_ent->db_cmd_num=i;
450
451 switch(type){
452
453 case DDB_BASE_CMD:
454 list=&db_base_cmd_list;
455 break;
456
457 case DDB_SHOW_CMD:
458 list=&db_show_cmd_list;
459 break;
460
461 #ifdef DB_MACHINE_COMMANDS
462 case DDB_MACH_CMD:
463 list=&db_mach_cmd_list;
464 break;
465 #endif
466
467 default:
468 return (ENOENT);
469 break;
470 }
471
472 /*Type specify list*/
473
474 if (TAILQ_EMPTY(list))
475 /*If head is empty we add first table here*/
476 TAILQ_INSERT_HEAD(list,
477 list_ent,db_cmd_next);
478 else
479 /*new commands go to the end*/
480 TAILQ_INSERT_TAIL(list,
481 list_ent,db_cmd_next);
482 }
483 return 0;
484 }
485 /*
486 *Remove command table specified with db_cmd address == cmd_tbl
487 */
488 int
489 db_unregister_tbl(uint8_t type,const struct db_command *cmd_tbl)
490 {
491
492 struct db_cmd_tbl_en *list_ent;
493 struct db_cmd_tbl_en_head *list;
494 int error=ENOENT;
495
496 if (db_base_cmd_list.tqh_first == NULL)
497 return (error);
498
499 /*I go through the list, selected with type and look for
500 cmd_tbl address in list entries.*/
501 switch(type){
502
503 case DDB_BASE_CMD:
504 list=&db_base_cmd_list;
505 break;
506
507 case DDB_SHOW_CMD:
508 list=&db_show_cmd_list;
509 break;
510
511 #ifdef DB_MACHINE_COMMANDS
512 case DDB_MACH_CMD:
513 list=&db_mach_cmd_list;
514 break;
515 #endif
516
517 default:
518 return (EINVAL);
519 break;
520 }
521
522 TAILQ_FOREACH(list_ent,list,db_cmd_next){
523
524 if (list_ent->db_cmd == cmd_tbl){
525 TAILQ_REMOVE(list,
526 list_ent,db_cmd_next);
527
528 free(list_ent,M_TEMP);
529 error=0;
530 }
531 }
532 return (error);
533 }
534
535 /*This function is called from machine trap code.*/
536 void
537 db_command_loop(void)
538 {
539
540 label_t db_jmpbuf;
541 label_t *savejmp;
542
543 /*
544 * Initialize 'prev' and 'next' to dot.
545 */
546 db_prev = db_dot;
547 db_next = db_dot;
548
549 db_cmd_loop_done = 0;
550
551 /*Init default command tables add machine, base,
552 show command tables to the list*/
553 (void) db_init_commands();
554
555 /*save context for return from ddb*/
556 savejmp = db_recover;
557 db_recover = &db_jmpbuf;
558 (void) setjmp(&db_jmpbuf);
559
560 /*Execute default ddb start commands*/
561 db_execute_commandlist(db_cmd_on_enter);
562
563 while (!db_cmd_loop_done) {
564 if (db_print_position() != 0)
565 db_printf("\n");
566 db_output_line = 0;
567
568
569 #ifdef MULTIPROCESSOR
570 db_printf("db{%ld}> ", (long)cpu_number());
571 #else
572 db_printf("db> ");
573 #endif
574 (void) db_read_line();
575
576 db_command(&db_last_command);
577 }
578
579 db_recover = savejmp;
580 }
581
582 /*
583 * Search for command table for command prefix
584 * ret: CMD_UNIQUE -> completly match cmd prefix
585 * CMD_FOUND -> partialy match cmd prefix
586 * CMD_AMBIGIOUS ->more partialy matches
587 * CMD_NONE -> command not found
588 */
589 static int
590 db_cmd_search(const char *name,const struct db_command *table,
591 const struct db_command **cmdp)
592 {
593
594 const struct db_command *cmd;
595 int result = CMD_NONE;
596
597 for (cmd = table; cmd->name != 0; cmd++) {
598 const char *lp;
599 const char *rp;
600 int c;
601
602 lp = name;
603 rp = cmd->name;
604 while ((c = *lp) == *rp) {
605 if (c == 0) {
606 /* complete match */
607 *cmdp = cmd;
608 return (CMD_UNIQUE);
609 }
610 lp++;
611 rp++;
612 }
613 if (c == 0) {
614 /* end of name, not end of command -
615 partial match */
616 if (result == CMD_FOUND) {
617 result = CMD_AMBIGUOUS;
618 /* but keep looking for a full match -
619 this lets us match single letters */
620 } else {
621 *cmdp = cmd;
622 result = CMD_FOUND;
623 }
624 }
625 }
626 return (result);
627 }
628
629 /*
630 *List commands to the console.
631 */
632 static void
633 db_cmd_list(const struct db_cmd_tbl_en_head *list)
634 {
635
636 struct db_cmd_tbl_en *list_ent;
637 const struct db_command *table;
638 int i, j, w, columns, lines, width=0, numcmds=0;
639 const char *p;
640
641 TAILQ_FOREACH(list_ent,list,db_cmd_next){
642 numcmds+=list_ent->db_cmd_num;
643 table=list_ent->db_cmd;
644
645 for (i = 0; i<list_ent->db_cmd_num; i++) {
646 w = strlen(table[i].name);
647 if (w > width)
648 width = w;
649 }
650 }
651
652 width = DB_NEXT_TAB(width);
653
654 columns = db_max_width / width;
655 if (columns == 0)
656 columns = 1;
657
658
659 TAILQ_FOREACH(list_ent,list,db_cmd_next){
660 table=list_ent->db_cmd;
661
662 lines = (list_ent->db_cmd_num + columns - 1) / columns;
663
664 for (i = 0; i < lines; i++) {
665 for (j = 0; j < columns; j++) {
666 p = table[j * lines + i].name;
667 if (p)
668 db_printf("%s", p);
669 if (j * lines + i + lines >= list_ent->db_cmd_num){
670 db_putchar('\n');
671 break;
672 }
673 if (p) {
674 w = strlen(p);
675 while (w < width) {
676 w = DB_NEXT_TAB(w);
677 db_putchar('\t');
678 }
679 }
680 }
681 }
682 }
683 return;
684 }
685
686 /*
687 *Returns type of list for command with name *name.
688 */
689 static int
690 db_get_list_type(const char *name)
691 {
692
693 const struct db_command *cmd;
694 struct db_cmd_tbl_en *list_ent;
695 int error,ret=-1;
696
697 /*I go through base cmd list and search command with name(form cmd line)*/
698 TAILQ_FOREACH(list_ent,&db_base_cmd_list,db_cmd_next){
699 /*cmd_search returns CMD_UNIQUE, CMD_FOUND ...
700 *CMD_UNIQUE when name was completly matched to cmd->name
701 *CMD_FOUND when name was only partially matched to cmd->name
702 *CMD_NONE command not found in a list
703 *CMD_AMBIGIOUS ->more partialy matches
704 */
705
706 error=db_cmd_search(name,list_ent->db_cmd,&cmd);
707 /*because I can't have better result then CMD_UNIQUE I break*/
708 if (error == CMD_UNIQUE){
709 if (cmd->flag == CS_SHOW){
710 ret=DDB_SHOW_CMD;
711 break;
712 }
713
714 if (cmd->flag == CS_MACH){
715 ret=DDB_MACH_CMD;
716 break;
717 } else {
718 ret=DDB_BASE_CMD;
719 break;
720 }
721 /*I have only partially matched name so I continue search*/
722 } else if (error == CMD_FOUND){
723 if (cmd->flag == CS_SHOW)
724 ret=DDB_SHOW_CMD;
725 if (cmd->flag == CS_MACH)
726 ret=DDB_MACH_CMD;
727 else
728 ret=DDB_BASE_CMD;
729 }
730 }
731
732 return(ret);
733 }
734
735 /*
736 *Parse command line and execute apropriate function.
737 */
738 static void
739 db_command(const struct db_command **last_cmdp)
740 {
741 const struct db_command *command;
742 struct db_cmd_tbl_en *list_ent;
743 struct db_cmd_tbl_en_head *list;
744
745 int t;
746 int result;
747
748 char modif[TOK_STRING_SIZE];
749 db_expr_t addr, count;
750 bool have_addr = false;
751
752 static db_expr_t last_count = 0;
753
754 command = NULL; /* XXX gcc */
755
756 t = db_read_token();
757 if ((t == tEOL) || (t == tCOMMA)) {
758 /*
759 * An empty line repeats last command, at 'next'.
760 * Only a count repeats the last command with the new count.
761 */
762 command = *last_cmdp;
763
764 if (!command)
765 return;
766
767 addr = (db_expr_t)db_next;
768 if (t == tCOMMA) {
769 if (!db_expression(&count)) {
770 db_printf("Count missing\n");
771 db_flush_lex();
772 return;
773 }
774 } else
775 count = last_count;
776 have_addr = false;
777 modif[0] = '\0';
778 db_skip_to_eol();
779
780 } else if (t == tEXCL) {
781 db_fncall(0, 0, 0, NULL);
782 return;
783
784 } else if (t != tIDENT) {
785 db_printf("?\n");
786 db_flush_lex();
787 return;
788
789 } else {
790
791 switch(db_get_list_type(db_tok_string)) {
792
793 case DDB_BASE_CMD:
794 list=&db_base_cmd_list;
795 break;
796 case DDB_SHOW_CMD:
797 list=&db_show_cmd_list;
798 /* need to read show subcommand if show command list
799 is used. */
800 t = db_read_token();
801
802 if (t != tIDENT) {
803 /* if only show command is executed, print
804 all subcommands */
805 db_cmd_list(list);
806 db_flush_lex();
807 return;
808 }
809 break;
810 #ifdef DB_MACHINE_COMMANDS
811 case DDB_MACH_CMD:
812 list=&db_mach_cmd_list;
813 /* need to read machine subcommand if
814 machine level 2 command list is used. */
815 t = db_read_token();
816
817 if (t != tIDENT) {
818 /* if only show command is executed, print
819 all subcommands */
820 db_cmd_list(list);
821 db_flush_lex();
822 return;
823 }
824 break;
825 #endif
826 default:
827 db_printf("No such command\n");
828 db_flush_lex();
829 return;
830 }
831
832 COMPAT_RET:
833 TAILQ_FOREACH(list_ent,list,db_cmd_next) {
834 result = db_cmd_search(db_tok_string, list_ent->db_cmd,
835 &command);
836
837 /* after CMD_UNIQUE in cmd_list only a single command
838 name is possible */
839 if (result == CMD_UNIQUE)
840 break;
841
842 }
843
844 /*check compatibility flag*/
845 if (command && command->flag & CS_COMPAT){
846 t = db_read_token();
847 if (t != tIDENT) {
848 db_cmd_list(list);
849 db_flush_lex();
850 return;
851 }
852
853 /* support only level 2 commands here */
854 goto COMPAT_RET;
855 }
856
857 if (!command) {
858 db_printf("No such command\n");
859 db_flush_lex();
860 return;
861 }
862
863 if ((command->flag & CS_OWN) == 0) {
864
865 /*
866 * Standard syntax:
867 * command [/modifier] [addr] [,count]
868 */
869 t = db_read_token(); /* get modifier */
870 if (t == tSLASH) {
871 t = db_read_token();
872 if (t != tIDENT) {
873 db_printf("Bad modifier\n");
874 db_flush_lex();
875 return;
876 }
877 /* save modifier */
878 strlcpy(modif, db_tok_string, sizeof(modif));
879
880 } else {
881 db_unread_token(t);
882 modif[0] = '\0';
883 }
884
885 if (db_expression(&addr)) { /*get address*/
886 db_dot = (db_addr_t) addr;
887 db_last_addr = db_dot;
888 have_addr = true;
889 } else {
890 addr = (db_expr_t) db_dot;
891 have_addr = false;
892 }
893
894 t = db_read_token();
895 if (t == tCOMMA) { /*Get count*/
896 if (!db_expression(&count)) {
897 db_printf("Count missing\n");
898 db_flush_lex();
899 return;
900 }
901 } else {
902 db_unread_token(t);
903 count = -1;
904 }
905 if ((command->flag & CS_MORE) == 0) {
906 db_skip_to_eol();
907 }
908 }
909 }
910
911 if (command->flag & CS_NOREPEAT) {
912 *last_cmdp = NULL;
913 last_count = 0;
914 } else {
915 *last_cmdp = command;
916 last_count = count;
917 }
918
919 if (command != NULL) {
920 /*
921 * Execute the command.
922 */
923 (*command->fcn)(addr, have_addr, count, modif);
924
925 if (command->flag & CS_SET_DOT) {
926 /*
927 * If command changes dot, set dot to
928 * previous address displayed (if 'ed' style).
929 */
930 if (db_ed_style)
931 db_dot = db_prev;
932 else
933 db_dot = db_next;
934 } else {
935 /*
936 * If command does not change dot,
937 * set 'next' location to be the same.
938 */
939 db_next = db_dot;
940 }
941 }
942 }
943
944 /*
945 * Print help for commands
946 */
947 static void
948 db_help_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
949 const char *modif)
950 {
951
952 const struct db_cmd_tbl_en_head *list;
953 const struct db_cmd_tbl_en *list_ent;
954 const struct db_command *help;
955 int t, result;
956
957 t = db_read_token();
958 /* is there another command after the "help"? */
959 if (t == tIDENT){
960
961 switch(db_get_list_type(db_tok_string)){
962
963 case DDB_BASE_CMD:
964 list=&db_base_cmd_list;
965 break;
966 case DDB_SHOW_CMD:
967 list=&db_show_cmd_list;
968 /* read the show subcommand */
969 t = db_read_token();
970
971 if (t != tIDENT) {
972 /* no subcommand, print the list */
973 db_cmd_list(list);
974 db_flush_lex();
975 return;
976 }
977
978 break;
979 #ifdef DB_MACHINE_COMMANDS
980 case DDB_MACH_CMD:
981 list=&db_mach_cmd_list;
982 /* read machine subcommand */
983 t = db_read_token();
984
985 if (t != tIDENT) {
986 /* no subcommand - just print the list */
987 db_cmd_list(list);
988 db_flush_lex();
989 return;
990 }
991 break;
992 #endif
993 default:
994 db_printf("No such command\n");
995 db_flush_lex();
996 return;
997 }
998 COMPAT_RET:
999 TAILQ_FOREACH(list_ent,list,db_cmd_next){
1000 result = db_cmd_search(db_tok_string, list_ent->db_cmd,
1001 &help);
1002 /* after CMD_UNIQUE only a single command
1003 name is possible */
1004 if (result == CMD_UNIQUE)
1005 break;
1006 }
1007 #ifdef DDB_VERBOSE_HELP
1008 /*print help*/
1009
1010 db_printf("Command: %s\n",help->name);
1011
1012 if (help->cmd_descr != NULL)
1013 db_printf(" Description: %s\n",help->cmd_descr);
1014
1015 if (help->cmd_arg != NULL)
1016 db_printf(" Arguments: %s\n",help->cmd_arg);
1017
1018 if (help->cmd_arg_help != NULL)
1019 db_printf(" Arguments description:\n%s\n",
1020 help->cmd_arg_help);
1021
1022 if ((help->cmd_arg == NULL) && (help->cmd_descr == NULL))
1023 db_printf("%s Doesn't have any help message included.\n",
1024 help->name);
1025 #endif
1026 /* check compatibility flag */
1027 /*
1028 * The "show all" command table has been merged with the
1029 * "show" command table - but we want to keep the old UI
1030 * available. So if we find a CS_COMPAT entry, we read
1031 * the next token and try again.
1032 */
1033 if (help->flag == CS_COMPAT){
1034 t = db_read_token();
1035
1036 if (t != tIDENT){
1037 db_cmd_list(list);
1038 db_flush_lex();
1039 return;
1040 }
1041
1042 goto COMPAT_RET;
1043 /* support only level 2 commands here */
1044 } else {
1045 db_skip_to_eol();
1046 }
1047
1048 } else /* t != tIDENT */
1049 /* print base commands */
1050 db_cmd_list(&db_base_cmd_list);
1051
1052 return;
1053 }
1054 /*ARGSUSED*/
1055 static void
1056 db_map_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
1057 const char *modif)
1058 {
1059 bool full = false;
1060
1061 if (modif[0] == 'f')
1062 full = true;
1063
1064 if (have_addr == false)
1065 addr = (db_expr_t)(intptr_t) kernel_map;
1066
1067 uvm_map_printit((struct vm_map *)(intptr_t) addr, full, db_printf);
1068 }
1069
1070 /*ARGSUSED*/
1071 static void
1072 db_malloc_print_cmd(db_expr_t addr, bool have_addr,
1073 db_expr_t count, const char *modif)
1074 {
1075
1076 #ifdef MALLOC_DEBUG
1077 if (!have_addr)
1078 addr = 0;
1079
1080 debug_malloc_printit(db_printf, (vaddr_t) addr);
1081 #else
1082 db_printf("The kernel is not built with the MALLOC_DEBUG option.\n");
1083 #endif /* MALLOC_DEBUG */
1084 }
1085
1086 /*ARGSUSED*/
1087 static void
1088 db_object_print_cmd(db_expr_t addr, bool have_addr,
1089 db_expr_t count, const char *modif)
1090 {
1091 bool full = false;
1092
1093 if (modif[0] == 'f')
1094 full = true;
1095
1096 uvm_object_printit((struct uvm_object *)(intptr_t) addr, full,
1097 db_printf);
1098 }
1099
1100 /*ARGSUSED*/
1101 static void
1102 db_page_print_cmd(db_expr_t addr, bool have_addr,
1103 db_expr_t count, const char *modif)
1104 {
1105 bool full = false;
1106
1107 if (modif[0] == 'f')
1108 full = true;
1109
1110 uvm_page_printit((struct vm_page *)(intptr_t) addr, full, db_printf);
1111 }
1112
1113 /*ARGSUSED*/
1114 static void
1115 db_show_all_pages(db_expr_t addr, bool have_addr,
1116 db_expr_t count, const char *modif)
1117 {
1118
1119 uvm_page_printall(db_printf);
1120 }
1121
1122 /*ARGSUSED*/
1123 static void
1124 db_buf_print_cmd(db_expr_t addr, bool have_addr,
1125 db_expr_t count, const char *modif)
1126 {
1127 bool full = false;
1128
1129 if (modif[0] == 'f')
1130 full = true;
1131
1132 vfs_buf_print((struct buf *)(intptr_t) addr, full, db_printf);
1133 }
1134
1135 /*ARGSUSED*/
1136 static void
1137 db_event_print_cmd(db_expr_t addr, bool have_addr,
1138 db_expr_t count, const char *modif)
1139 {
1140 bool full = false;
1141
1142 if (modif[0] == 'f')
1143 full = true;
1144
1145 event_print(full, db_printf);
1146 }
1147
1148 /*ARGSUSED*/
1149 static void
1150 db_vnode_print_cmd(db_expr_t addr, bool have_addr,
1151 db_expr_t count, const char *modif)
1152 {
1153 bool full = false;
1154
1155 if (modif[0] == 'f')
1156 full = true;
1157
1158 vfs_vnode_print((struct vnode *)(intptr_t) addr, full, db_printf);
1159 }
1160
1161 static void
1162 db_mount_print_cmd(db_expr_t addr, bool have_addr,
1163 db_expr_t count, const char *modif)
1164 {
1165 bool full = false;
1166
1167 if (modif[0] == 'f')
1168 full = true;
1169
1170 vfs_mount_print((struct mount *)(intptr_t) addr, full, db_printf);
1171 }
1172
1173 /*ARGSUSED*/
1174 static void
1175 db_mbuf_print_cmd(db_expr_t addr, bool have_addr,
1176 db_expr_t count, const char *modif)
1177 {
1178
1179 m_print((const struct mbuf *)(intptr_t) addr, modif, db_printf);
1180 }
1181
1182 /*ARGSUSED*/
1183 static void
1184 db_pool_print_cmd(db_expr_t addr, bool have_addr,
1185 db_expr_t count, const char *modif)
1186 {
1187
1188 pool_printit((struct pool *)(intptr_t) addr, modif, db_printf);
1189 }
1190
1191 /*ARGSUSED*/
1192 static void
1193 db_namecache_print_cmd(db_expr_t addr, bool have_addr,
1194 db_expr_t count, const char *modif)
1195 {
1196
1197 namecache_print((struct vnode *)(intptr_t) addr, db_printf);
1198 }
1199
1200 /*ARGSUSED*/
1201 static void
1202 db_uvmexp_print_cmd(db_expr_t addr, bool have_addr,
1203 db_expr_t count, const char *modif)
1204 {
1205
1206 uvmexp_print(db_printf);
1207 }
1208
1209 /*ARGSUSED*/
1210 static void
1211 db_lock_print_cmd(db_expr_t addr, bool have_addr,
1212 db_expr_t count, const char *modif)
1213 {
1214
1215 lockdebug_lock_print((void *)addr, db_printf);
1216 }
1217
1218 /*
1219 * Call random function:
1220 * !expr(arg,arg,arg)
1221 */
1222 /*ARGSUSED*/
1223 static void
1224 db_fncall(db_expr_t addr, bool have_addr,
1225 db_expr_t count, const char *modif)
1226 {
1227 db_expr_t fn_addr;
1228 #define MAXARGS 11
1229 db_expr_t args[MAXARGS];
1230 int nargs = 0;
1231 db_expr_t retval;
1232 db_expr_t (*func)(db_expr_t, ...);
1233 int t;
1234
1235 if (!db_expression(&fn_addr)) {
1236 db_printf("Bad function\n");
1237 db_flush_lex();
1238 return;
1239 }
1240 func = (db_expr_t (*)(db_expr_t, ...))(intptr_t) fn_addr;
1241
1242 t = db_read_token();
1243 if (t == tLPAREN) {
1244 if (db_expression(&args[0])) {
1245 nargs++;
1246 while ((t = db_read_token()) == tCOMMA) {
1247 if (nargs == MAXARGS) {
1248 db_printf("Too many arguments\n");
1249 db_flush_lex();
1250 return;
1251 }
1252 if (!db_expression(&args[nargs])) {
1253 db_printf("Argument missing\n");
1254 db_flush_lex();
1255 return;
1256 }
1257 nargs++;
1258 }
1259 db_unread_token(t);
1260 }
1261 if (db_read_token() != tRPAREN) {
1262 db_printf("?\n");
1263 db_flush_lex();
1264 return;
1265 }
1266 }
1267 db_skip_to_eol();
1268
1269 while (nargs < MAXARGS) {
1270 args[nargs++] = 0;
1271 }
1272
1273 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
1274 args[5], args[6], args[7], args[8], args[9]);
1275 db_printf("%s\n", db_num_to_str(retval));
1276 }
1277
1278 static void
1279 db_reboot_cmd(db_expr_t addr, bool have_addr,
1280 db_expr_t count, const char *modif)
1281 {
1282 db_expr_t bootflags;
1283
1284 /* Flags, default to RB_AUTOBOOT */
1285 if (!db_expression(&bootflags))
1286 bootflags = (db_expr_t)RB_AUTOBOOT;
1287 if (db_read_token() != tEOL) {
1288 db_error("?\n");
1289 /*NOTREACHED*/
1290 }
1291 /*
1292 * We are leaving DDB, never to return upward.
1293 * Clear db_recover so that we can debug faults in functions
1294 * called from cpu_reboot.
1295 */
1296 db_recover = 0;
1297 cpu_reboot((int)bootflags, NULL);
1298 }
1299
1300 static void
1301 db_sifting_cmd(db_expr_t addr, bool have_addr,
1302 db_expr_t count, const char *modif)
1303 {
1304 int mode, t;
1305
1306 t = db_read_token();
1307 if (t == tSLASH) {
1308 t = db_read_token();
1309 if (t != tIDENT) {
1310 bad_modifier:
1311 db_printf("Bad modifier\n");
1312 db_flush_lex();
1313 return;
1314 }
1315 if (!strcmp(db_tok_string, "F"))
1316 mode = 'F';
1317 else
1318 goto bad_modifier;
1319 t = db_read_token();
1320 } else
1321 mode = 0;
1322
1323 if (t == tIDENT)
1324 db_sifting(db_tok_string, mode);
1325 else {
1326 db_printf("Bad argument (non-string)\n");
1327 db_flush_lex();
1328 }
1329 }
1330
1331 static void
1332 db_stack_trace_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
1333 {
1334 register const char *cp = modif;
1335 register char c;
1336 void (*pr)(const char *, ...);
1337
1338 pr = db_printf;
1339 while ((c = *cp++) != 0)
1340 if (c == 'l')
1341 pr = printf;
1342
1343 if (count == -1)
1344 count = 65535;
1345
1346 db_stack_trace_print(addr, have_addr, count, modif, pr);
1347 }
1348
1349 static void
1350 db_sync_cmd(db_expr_t addr, bool have_addr,
1351 db_expr_t count, const char *modif)
1352 {
1353
1354 /*
1355 * We are leaving DDB, never to return upward.
1356 * Clear db_recover so that we can debug faults in functions
1357 * called from cpu_reboot.
1358 */
1359 db_recover = 0;
1360 cpu_reboot(RB_DUMP, NULL);
1361 }
1362