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