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