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