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