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