1 /*        $NetBSD: db_command.c,v 1.190 2023/11/02 10:31:55 martin Exp $        */
2 
3 /*
4  * Copyright (c) 1996, 1997, 1998, 1999, 2002, 2009, 2019
5  *     The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Adam Hamsik, and by Andrew Doran.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Mach Operating System
35  * Copyright (c) 1991,1990 Carnegie Mellon University
36  * All Rights Reserved.
37  *
38  * Permission to use, copy, modify and distribute this software and its
39  * documentation is hereby granted, provided that both the copyright
40  * notice and this permission notice appear in all copies of the
41  * software, derivative works or modified versions, and any portions
42  * thereof, and that both notices appear in supporting documentation.
43  *
44  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47  *
48  * Carnegie Mellon requests users of this software to return to
49  *
50  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
51  *  School of Computer Science
52  *  Carnegie Mellon University
53  *  Pittsburgh PA 15213-3890
54  *
55  * any improvements or extensions that they make and grant Carnegie the
56  * rights to redistribute these changes.
57  */
58 
59 /*
60  * Command dispatcher.
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.190 2023/11/02 10:31:55 martin Exp $");
65 
66 #ifdef _KERNEL_OPT
67 #include "opt_aio.h"
68 #include "opt_ddb.h"
69 #include "opt_fdt.h"
70 #include "opt_kgdb.h"
71 #include "opt_mqueue.h"
72 #include "opt_inet.h"
73 #include "opt_kernhist.h"
74 #include "opt_ddbparam.h"
75 #include "opt_multiprocessor.h"
76 #endif
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/reboot.h>
81 #include <sys/device.h>
82 #include <sys/eventvar.h>
83 #include <sys/lwp.h>
84 #include <sys/mbuf.h>
85 #include <sys/namei.h>
86 #include <sys/pool.h>
87 #include <sys/proc.h>
88 #include <sys/vnode.h>
89 #include <sys/vmem.h>
90 #include <sys/lockdebug.h>
91 #include <sys/cpu.h>
92 #include <sys/buf.h>
93 #include <sys/module.h>
94 #include <sys/kernhist.h>
95 #include <sys/socketvar.h>
96 #include <sys/queue.h>
97 #include <sys/syncobj.h>
98 
99 #include <dev/cons.h>
100 
101 #include <ddb/ddb.h>
102 
103 #include <uvm/uvm_extern.h>
104 #include <uvm/uvm_ddb.h>
105 
106 #include <net/route.h>
107 
108 #ifdef FDT
109 #include <dev/fdt/fdtvar.h>
110 #include <dev/fdt/fdt_ddb.h>
111 #endif
112 
113 /*
114  * Results of command search.
115  */
116 #define   CMD_EXACT           0
117 #define   CMD_PREFIX                    1
118 #define   CMD_NONE            2
119 #define   CMD_AMBIGUOUS       3
120 
121 /*
122  * Exported global variables
123  */
124 bool                db_cmd_loop_done;
125 label_t             *db_recover;
126 db_addr_t db_dot;
127 db_addr_t db_last_addr;
128 db_addr_t db_prev;
129 db_addr_t db_next;
130 
131 
132 /*
133  * New DDB api for adding and removing commands uses three lists, because
134  * we use two types of commands
135  * a) standard commands without subcommands -> reboot
136  * b) show commands which are subcommands of show command -> show aio_jobs
137  * c) if defined machine specific commands
138  *
139  * ddb_add_cmd, ddb_rem_cmd use type (DDB_SHOW_CMD||DDB_BASE_CMD)argument to
140  * add them to representativ lists.
141  */
142 
143 static const struct db_command db_command_table[];
144 static const struct db_command db_show_cmds[];
145 
146 #ifdef DB_MACHINE_COMMANDS
147 /* arch/<arch>/<arch>/db_interface.c */
148 extern const struct db_command db_machine_command_table[];
149 #endif
150 
151 /* the global queue of all command tables */
152 TAILQ_HEAD(db_cmd_tbl_en_head, db_cmd_tbl_en);
153 
154 /* TAILQ entry used to register command tables */
155 struct db_cmd_tbl_en {
156           const struct db_command *db_cmd;        /* cmd table */
157           TAILQ_ENTRY(db_cmd_tbl_en) db_cmd_next;
158 };
159 
160 /* head of base commands list */
161 static struct db_cmd_tbl_en_head db_base_cmd_list =
162           TAILQ_HEAD_INITIALIZER(db_base_cmd_list);
163 static struct db_cmd_tbl_en db_base_cmd_builtins =
164      { .db_cmd = db_command_table };
165 
166 /* head of show commands list */
167 static struct db_cmd_tbl_en_head db_show_cmd_list =
168           TAILQ_HEAD_INITIALIZER(db_show_cmd_list);
169 static struct db_cmd_tbl_en db_show_cmd_builtins =
170      { .db_cmd = db_show_cmds };
171 
172 /* head of machine commands list */
173 static struct db_cmd_tbl_en_head db_mach_cmd_list =
174           TAILQ_HEAD_INITIALIZER(db_mach_cmd_list);
175 #ifdef DB_MACHINE_COMMANDS
176 static struct db_cmd_tbl_en db_mach_cmd_builtins =
177      { .db_cmd = db_machine_command_table };
178 #endif
179 
180 /*
181  * if 'ed' style: 'dot' is set at start of last item printed,
182  * and '+' points to next line.
183  * Otherwise: 'dot' points to next item, '..' points to last.
184  */
185 static bool          db_ed_style = true;
186 
187 static void         db_init_commands(void);
188 static int          db_register_tbl_entry(uint8_t type,
189     struct db_cmd_tbl_en *list_ent);
190 static void         db_cmd_list(const struct db_cmd_tbl_en_head *);
191 static int          db_cmd_search(const char *, struct db_cmd_tbl_en_head *,
192                                     const struct db_command **);
193 static int          db_cmd_search_table(const char *, const struct db_command *,
194                                             const struct db_command **);
195 static void         db_cmd_search_failed(char *, int);
196 static const struct db_command *db_read_command(void);
197 static void         db_command(const struct db_command **);
198 static void         db_buf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
199 static void         db_event_print_cmd(db_expr_t, bool, db_expr_t, const char *);
200 static void         db_fncall(db_expr_t, bool, db_expr_t, const char *);
201 static void     db_help_print_cmd(db_expr_t, bool, db_expr_t, const char *);
202 static void         db_lock_print_cmd(db_expr_t, bool, db_expr_t, const char *);
203 static void         db_show_all_locks(db_expr_t, bool, db_expr_t, const char *);
204 static void         db_show_all_tstiles(db_expr_t, bool, db_expr_t, const char *);
205 static void         db_show_lockstats(db_expr_t, bool, db_expr_t, const char *);
206 static void         db_show_all_freelists(db_expr_t, bool, db_expr_t, const char *);
207 static void         db_mount_print_cmd(db_expr_t, bool, db_expr_t, const char *);
208 static void         db_show_all_mounts(db_expr_t, bool, db_expr_t, const char *);
209 static void         db_mbuf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
210 static void         db_kqueue_print_cmd(db_expr_t, bool, db_expr_t, const char *);
211 static void         db_map_print_cmd(db_expr_t, bool, db_expr_t, const char *);
212 static void         db_namecache_print_cmd(db_expr_t, bool, db_expr_t,
213                         const char *);
214 static void         db_object_print_cmd(db_expr_t, bool, db_expr_t, const char *);
215 static void         db_page_print_cmd(db_expr_t, bool, db_expr_t, const char *);
216 static void         db_show_all_pages(db_expr_t, bool, db_expr_t, const char *);
217 static void         db_pool_print_cmd(db_expr_t, bool, db_expr_t, const char *);
218 static void         db_reboot_cmd(db_expr_t, bool, db_expr_t, const char *);
219 static void         db_sifting_cmd(db_expr_t, bool, db_expr_t, const char *);
220 static void         db_socket_print_cmd(db_expr_t, bool, db_expr_t, const char *);
221 static void         db_stack_trace_cmd(db_expr_t, bool, db_expr_t, const char *);
222 static void         db_sync_cmd(db_expr_t, bool, db_expr_t, const char *);
223 static void         db_whatis_cmd(db_expr_t, bool, db_expr_t, const char *);
224 static void         db_uvmexp_print_cmd(db_expr_t, bool, db_expr_t, const char *);
225 #ifdef KERNHIST
226 static void         db_kernhist_print_cmd(db_expr_t, bool, db_expr_t, const char *);
227 #endif
228 static void         db_vnode_print_cmd(db_expr_t, bool, db_expr_t, const char *);
229 static void         db_vnode_lock_print_cmd(db_expr_t, bool, db_expr_t,
230                         const char *);
231 static void         db_vmem_print_cmd(db_expr_t, bool, db_expr_t, const char *);
232 #ifdef FDT
233 static void         db_fdt_print_cmd(db_expr_t, bool, db_expr_t, const char *);
234 #endif
235 
236 static const struct db_command db_show_cmds[] = {
237           { DDB_ADD_CMD("all",          NULL,
238               CS_COMPAT, NULL,NULL,NULL) },
239 #ifdef AIO
240           { DDB_ADD_CMD("aio_jobs",     db_show_aio_jobs,   0,
241               "Show aio jobs",NULL,NULL) },
242 #endif
243 #ifdef _KERNEL
244           { DDB_ADD_CMD("breaks",       db_listbreak_cmd,   0,
245               "Display all breaks.",NULL,NULL) },
246 #endif
247           { DDB_ADD_CMD("buf",          db_buf_print_cmd,   0,
248               "Print the struct buf at address.", "[/f] address",NULL) },
249           /* added from all sub cmds */
250           { DDB_ADD_CMD("callout",  db_show_callout,
251               0 ,"List all used callout functions.",NULL,NULL) },
252           { DDB_ADD_CMD("condvar", db_show_condvar,
253               0 ,"Show the contents of a condition variable.",NULL,NULL) },
254           { DDB_ADD_CMD("devices", db_show_all_devices,     0,NULL,NULL,NULL) },
255           { DDB_ADD_CMD("event",        db_event_print_cmd, 0,
256               "Print all the non-zero evcnt(9) event counters.", "[/fitm]",NULL) },
257 #ifdef FDT
258           { DDB_ADD_CMD("fdt", db_fdt_print_cmd, 0,
259               "Show FDT information", NULL, NULL) },
260 #endif
261           { DDB_ADD_CMD("files", db_show_files_cmd,         0,
262               "Print the files open by process at address",
263               "[/f] address", NULL) },
264           { DDB_ADD_CMD("freelists",    db_show_all_freelists,
265               0 ,"Show all freelists", NULL, NULL) },
266 #ifdef KERNHIST
267           { DDB_ADD_CMD("kernhist", db_kernhist_print_cmd, 0,
268               "Print the UVM history logs.",
269               NULL,NULL) },
270 #endif
271           /* added from all sub cmds */
272           { DDB_ADD_CMD("locks",        db_show_all_locks,
273               0 ,"Show all held locks", "[/t]", NULL) },
274           { DDB_ADD_CMD("lock",         db_lock_print_cmd,  0,NULL,NULL,NULL) },
275           { DDB_ADD_CMD("lockstats",
276                                         db_show_lockstats,  0,
277               "Print statistics of locks", NULL, NULL) },
278           { DDB_ADD_CMD("kqueue",       db_kqueue_print_cmd,          0,
279               "Print the kqueue at address.", "[/f] address",NULL) },
280           { DDB_ADD_CMD("map",          db_map_print_cmd,   0,
281               "Print the vm_map at address.", "[/f] address",NULL) },
282           { DDB_ADD_CMD("mbuf",         db_mbuf_print_cmd,  0,NULL,NULL,
283               "-c prints all mbuf chains") },
284           { DDB_ADD_CMD("module", db_show_module_cmd,       0,
285               "Print kernel modules", NULL, NULL) },
286           { DDB_ADD_CMD("mount",        db_mount_print_cmd, 0,
287               "Print the mount structure at address.", "[/f] address",NULL) },
288           { DDB_ADD_CMD("mounts",       db_show_all_mounts, 0,
289               "Print all mount structures.", "[/f]", NULL) },
290 #ifdef MQUEUE
291           { DDB_ADD_CMD("mqueue", db_show_mqueue_cmd,       0,
292               "Print the message queues", NULL, NULL) },
293 #endif
294           { DDB_ADD_CMD("ncache",       db_namecache_print_cmd,       0,
295               "Dump the namecache list.", "address",NULL) },
296           { DDB_ADD_CMD("object",       db_object_print_cmd,          0,
297               "Print the vm_object at address.", "[/f] address",NULL) },
298           { DDB_ADD_CMD("page",         db_page_print_cmd,  0,
299               "Print the vm_page at address.", "[/f] address",NULL) },
300           { DDB_ADD_CMD("pages",        db_show_all_pages,
301               0 ,"List all used memory pages.",NULL,NULL) },
302           { DDB_ADD_CMD("panic",        db_show_panic,      0,
303               "Print the current panic string",NULL,NULL) },
304           { DDB_ADD_CMD("pool",         db_pool_print_cmd,  0,
305               "Print the pool at address.", "[/clp] address",NULL) },
306           /* added from all sub cmds */
307           { DDB_ADD_CMD("pools",        db_show_all_pools,
308               0 ,"Show all pools",NULL,NULL) },
309           { DDB_ADD_CMD("proc",         db_show_proc,
310               0 ,"Print process information.",NULL,NULL) },
311           /* added from all sub cmds */
312           { DDB_ADD_CMD("procs",        db_show_all_procs,
313               0 ,"List all processes.",NULL,NULL) },
314           { DDB_ADD_CMD("registers",    db_show_regs,                 0,
315               "Display the register set.", "[/u]",NULL) },
316 #if defined(INET)
317           { DDB_ADD_CMD("routes",       db_show_routes,               0,NULL,NULL,NULL) },
318 #endif
319           { DDB_ADD_CMD("sched_qs",     db_show_sched_qs,   0,
320               "Print the state of the scheduler's run queues.",
321               NULL,NULL) },
322           { DDB_ADD_CMD("selinfo", db_show_selinfo,
323               0 ,"Show the contents of a selinfo.",NULL,NULL) },
324           { DDB_ADD_CMD("sleepq", db_show_sleepq,
325               0 ,"Show the contents of a sleep queue.",NULL,NULL) },
326           { DDB_ADD_CMD("socket",       db_socket_print_cmd,          0,NULL,NULL,NULL) },
327           { DDB_ADD_CMD("tstiles", db_show_all_tstiles,
328               0, "Show who's holding up tstiles", "[/t]", NULL) },
329           { DDB_ADD_CMD("uvmexp",       db_uvmexp_print_cmd, 0,
330               "Print a selection of UVM counters and statistics.",
331               NULL,NULL) },
332           { DDB_ADD_CMD("vmem", db_vmem_print_cmd,          0,
333               "Print the vmem usage.", "address", NULL) },
334           { DDB_ADD_CMD("vmems", db_show_all_vmems,         0,
335               "Show all vmems.", NULL, NULL) },
336           { DDB_ADD_CMD("vnode",        db_vnode_print_cmd, 0,
337               "Print the vnode at address.", "[/f] address",NULL) },
338           { DDB_ADD_CMD("vnode_lock",   db_vnode_lock_print_cmd,      0,
339               "Print the vnode having that address as v_lock.",
340               "[/f] address",NULL) },
341 #ifdef _KERNEL
342           { DDB_ADD_CMD("watches",      db_listwatch_cmd,   0,
343               "Display all watchpoints.", NULL,NULL) },
344 #endif
345           { DDB_END_CMD },
346 };
347 
348 static const struct db_command db_command_table[] = {
349           { DDB_ADD_CMD("b",            db_breakpoint_cmd,  0,
350               "Set a breakpoint at address", "[/u] address[,count].",NULL) },
351           { DDB_ADD_CMD("break",        db_breakpoint_cmd,  0,
352               "Set a breakpoint at address", "[/u] address[,count].",NULL) },
353           { DDB_ADD_CMD("bt",           db_stack_trace_cmd, 0,
354               "Show backtrace.", "See help trace.",NULL) },
355           { DDB_ADD_CMD("c",            db_continue_cmd,    0,
356               "Continue execution.", "[/c]",NULL) },
357           { DDB_ADD_CMD("call",         db_fncall,                    CS_OWN,
358               "Call the function", "address[(expression[,...])]",NULL) },
359           { DDB_ADD_CMD("callout",      db_show_callout,    0, NULL,
360               NULL,NULL ) },
361           { DDB_ADD_CMD("continue",     db_continue_cmd,    0,
362               "Continue execution.", "[/c]",NULL) },
363           { DDB_ADD_CMD("d",            db_delete_cmd,                0,
364               "Delete a breakpoint.", "address | #number",NULL) },
365           { DDB_ADD_CMD("delete",       db_delete_cmd,                0,
366               "Delete a breakpoint.", "address | #number",NULL) },
367           { DDB_ADD_CMD("dmesg",        db_dmesg,           0,
368               "Show kernel message buffer.", "[count]",NULL) },
369           { DDB_ADD_CMD("dwatch",       db_deletewatch_cmd, 0,
370               "Delete the watchpoint.", "address",NULL) },
371           { DDB_ADD_CMD("examine",      db_examine_cmd,               CS_SET_DOT,
372               "Display the address locations.",
373               "[/modifier] address[,count]",NULL) },
374           { DDB_ADD_CMD("exit",                   db_continue_cmd,    0,
375               "Continue execution.", "[/c]",NULL) },
376           { DDB_ADD_CMD("help",   db_help_print_cmd, CS_OWN|CS_NOREPEAT,
377               "Display help about commands",
378               "Use other commands as arguments.",NULL) },
379           { DDB_ADD_CMD("kill",         db_kill_proc,                 CS_OWN,
380               "Send a signal to the process","pid[,signal_number]",
381               "   pid:\t\t\tthe process id (may need 0t prefix for decimal)\n"
382               "   signal_number:\tthe signal to send") },
383 #ifdef KGDB
384           { DDB_ADD_CMD("kgdb",         db_kgdb_cmd,        0,        NULL,NULL,NULL) },
385 #endif
386           { DDB_ADD_CMD("machine",NULL,CS_MACH,
387               "Architecture specific functions.",NULL,NULL) },
388           { DDB_ADD_CMD("match",        db_trace_until_matching_cmd,0,
389               "Stop at the matching return instruction.","See help next",NULL) },
390           { DDB_ADD_CMD("next",         db_trace_until_matching_cmd,0,
391               "Stop at the matching return instruction.","[/p]",NULL) },
392           { DDB_ADD_CMD("p",            db_print_cmd,                 0,
393               "Print address according to the format.",
394               "[/axzodurc] address [address ...]",NULL) },
395           { DDB_ADD_CMD("print",        db_print_cmd,                 0,
396               "Print address according to the format.",
397               "[/axzodurc] address [address ...]",NULL) },
398           { DDB_ADD_CMD("ps",           db_show_all_procs,  0,
399               "Print all processes.","See show all procs",NULL) },
400           { DDB_ADD_CMD("quit",                   db_continue_cmd,    0,
401               "Continue execution.", "[/c]",NULL) },
402           { DDB_ADD_CMD("reboot",       db_reboot_cmd,                CS_OWN,
403               "Reboot","0x1  RB_ASKNAME, 0x2 RB_SINGLE, 0x4 RB_NOSYNC, 0x8 RB_HALT,"
404               "0x40 RB_KDB, 0x100 RB_DUMP, 0x808 RB_POWERDOWN",NULL) },
405           { DDB_ADD_CMD("s",            db_single_step_cmd, 0,
406               "Single-step count times.","[/p] [,count]",NULL) },
407           { DDB_ADD_CMD("search",       db_search_cmd,                CS_OWN|CS_SET_DOT,
408               "Search memory from address for value.",
409               "[/bhl] address value [mask] [,count]",NULL) },
410           { DDB_ADD_CMD("set",          db_set_cmd,                   CS_OWN,
411               "Set the named variable","$variable [=] expression",NULL) },
412           { DDB_ADD_CMD("show",         NULL, CS_SHOW,
413               "Show kernel stats.", NULL,NULL) },
414           { DDB_ADD_CMD("sifting",      db_sifting_cmd,               CS_OWN,
415               "Search the symbol tables ","[/F] string",NULL) },
416           { DDB_ADD_CMD("step",         db_single_step_cmd, 0,
417               "Single-step count times.","[/p] [,count]",NULL) },
418           { DDB_ADD_CMD("sync",         db_sync_cmd,                  CS_OWN,
419               "Force a crash dump, and then reboot.",NULL,NULL) },
420           { DDB_ADD_CMD("trace",        db_stack_trace_cmd, 0,
421               "Stack trace from frame-address.",
422               "[/u[l]] [frame-address][,count]",NULL) },
423           { DDB_ADD_CMD("until",        db_trace_until_call_cmd,0,
424               "Stop at the next call or return instruction.","[/p]",NULL) },
425           { DDB_ADD_CMD("w",            db_write_cmd,                 CS_MORE|CS_SET_DOT,
426               "Write the expressions at succeeding locations.",
427               "[/bhl] address expression [expression ...]",NULL) },
428           { DDB_ADD_CMD("watch",        db_watchpoint_cmd,  CS_MORE,
429               "Set a watchpoint for a region. ","address[,size]",NULL) },
430           { DDB_ADD_CMD("whatis",       db_whatis_cmd, 0,
431               "Describe what an address is", "address", NULL) },
432           { DDB_ADD_CMD("write",        db_write_cmd,                 CS_MORE|CS_SET_DOT,
433               "Write the expressions at succeeding locations.",
434               "[/bhl] address expression [expression ...]",NULL) },
435           { DDB_ADD_CMD("x",            db_examine_cmd,               CS_SET_DOT,
436               "Display the address locations.",
437               "[/modifier] address[,count]",NULL) },
438           { DDB_END_CMD },
439 };
440 
441 static const struct db_command          *db_last_command = NULL;
442 #if defined(DDB_COMMANDONENTER)
443 char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = ___STRING(DDB_COMMANDONENTER);
444 #else /* defined(DDB_COMMANDONENTER) */
445 char db_cmd_on_enter[DB_LINE_MAXLEN + 1] = "";
446 #endif /* defined(DDB_COMMANDONENTER) */
447 #define   DB_LINE_SEP         ';'
448 
449 /*
450  * Execute commandlist after ddb start
451  * This function goes through the command list created from commands and ';'
452  */
453 static void
db_execute_commandlist(const char * cmdlist)454 db_execute_commandlist(const char *cmdlist)
455 {
456           const char *cmd = cmdlist;
457           const struct db_command       *dummy = NULL;
458 
459           while (*cmd != '\0') {
460                     const char *ep = cmd;
461 
462                     while (*ep != '\0' && *ep != DB_LINE_SEP) {
463                               ep++;
464                     }
465                     db_set_line(cmd, ep);
466                     db_command(&dummy);
467                     cmd = ep;
468                     if (*cmd == DB_LINE_SEP) {
469                               cmd++;
470                     }
471           }
472 }
473 
474 /* Initialize ddb command tables */
475 void
db_init_commands(void)476 db_init_commands(void)
477 {
478           static bool done = false;
479 
480           if (done) return;
481           done = true;
482 
483           /* register command tables */
484           (void)db_register_tbl_entry(DDB_BASE_CMD, &db_base_cmd_builtins);
485 #ifdef DB_MACHINE_COMMANDS
486           (void)db_register_tbl_entry(DDB_MACH_CMD, &db_mach_cmd_builtins);
487 #endif
488           (void)db_register_tbl_entry(DDB_SHOW_CMD, &db_show_cmd_builtins);
489 }
490 
491 
492 /*
493  * Add command table to the specified list
494  * Arg:
495  * int type specifies type of command table DDB_SHOW_CMD|DDB_BASE_CMD|DDB_MAC_CMD
496  * *cmd_tbl pointer to static allocated db_command table
497  *
498  * Command table must be NULL terminated array of struct db_command
499  */
500 int
db_register_tbl(uint8_t type,const struct db_command * cmd_tbl)501 db_register_tbl(uint8_t type, const struct db_command *cmd_tbl)
502 {
503           struct db_cmd_tbl_en *list_ent;
504 
505           /* empty list - ignore */
506           if (cmd_tbl->name == 0)
507                     return 0;
508 
509           /* force builtin commands to be registered first */
510           db_init_commands();
511 
512           /* now create a list entry for this table */
513           list_ent = db_zalloc(sizeof(*list_ent));
514           if (list_ent == NULL)
515                     return ENOMEM;
516           list_ent->db_cmd=cmd_tbl;
517 
518           /* and register it */
519           return db_register_tbl_entry(type, list_ent);
520 }
521 
522 static int
db_register_tbl_entry(uint8_t type,struct db_cmd_tbl_en * list_ent)523 db_register_tbl_entry(uint8_t type, struct db_cmd_tbl_en *list_ent)
524 {
525           struct db_cmd_tbl_en_head *list;
526 
527           switch(type) {
528           case DDB_BASE_CMD:
529                     list = &db_base_cmd_list;
530                     break;
531           case DDB_SHOW_CMD:
532                     list = &db_show_cmd_list;
533                     break;
534           case DDB_MACH_CMD:
535                     list = &db_mach_cmd_list;
536                     break;
537           default:
538                     return ENOENT;
539           }
540 
541           TAILQ_INSERT_TAIL(list, list_ent, db_cmd_next);
542 
543           return 0;
544 }
545 
546 /*
547  * Remove command table specified with db_cmd address == cmd_tbl
548  */
549 int
db_unregister_tbl(uint8_t type,const struct db_command * cmd_tbl)550 db_unregister_tbl(uint8_t type,const struct db_command *cmd_tbl)
551 {
552           struct db_cmd_tbl_en *list_ent;
553           struct db_cmd_tbl_en_head *list;
554 
555           /* find list on which the entry should live */
556           switch (type) {
557           case DDB_BASE_CMD:
558                     list=&db_base_cmd_list;
559                     break;
560           case DDB_SHOW_CMD:
561                     list=&db_show_cmd_list;
562                     break;
563           case DDB_MACH_CMD:
564                     list=&db_mach_cmd_list;
565                     break;
566           default:
567                     return EINVAL;
568           }
569 
570           TAILQ_FOREACH (list_ent, list, db_cmd_next) {
571                     if (list_ent->db_cmd == cmd_tbl){
572                               TAILQ_REMOVE(list,
573                                   list_ent, db_cmd_next);
574                               db_free(list_ent, sizeof(*list_ent));
575                               return 0;
576                     }
577           }
578           return ENOENT;
579 }
580 
581 #ifndef _KERNEL
582 #define   cnpollc(c)          __nothing
583 #endif
584 
585 /*
586  * This function is called via db_trap() or directly from
587  * machine trap code.
588  */
589 void
db_command_loop(void)590 db_command_loop(void)
591 {
592           label_t   db_jmpbuf;
593           label_t   *savejmp;
594 
595           /*
596            * Initialize 'prev' and 'next' to dot.
597            */
598           db_prev = db_dot;
599           db_next = db_dot;
600 
601           db_cmd_loop_done = false;
602 
603           /* Init default command tables add machine, base,
604              show command tables to the list */
605           db_init_commands();
606 
607           /* save context for return from ddb */
608           savejmp = db_recover;
609           db_recover = &db_jmpbuf;
610 
611           /*
612            * Execute default ddb start commands only if this is the
613            * first entry into DDB, in case the start commands fault
614            * and we recurse into here.
615            */
616           if (savejmp == NULL && db_cmd_on_enter[0] != '\0') {
617                     if (setjmp(&db_jmpbuf) == 0)
618                               db_execute_commandlist(db_cmd_on_enter);
619           }
620 
621           (void) setjmp(&db_jmpbuf);
622           while (!db_cmd_loop_done) {
623                     if (db_print_position() != 0)
624                               db_printf("\n");
625                     db_output_line = 0;
626                     cnpollc(1);
627                     (void) db_read_line();
628                     cnpollc(0);
629                     db_command(&db_last_command);
630           }
631 
632           db_recover = savejmp;
633 }
634 
635 /*
636  * Search command table for command prefix
637  */
638 static int
db_cmd_search_table(const char * name,const struct db_command * table,const struct db_command ** cmdp)639 db_cmd_search_table(const char *name,
640                         const struct db_command *table,
641                         const struct db_command **cmdp)
642 {
643 
644           const struct db_command       *cmd;
645           int result;
646 
647           result = CMD_NONE;
648           *cmdp = NULL;
649 
650           for (cmd = table; cmd->name != 0; cmd++) {
651                     const char *lp;
652                     const char *rp;
653 
654                     lp = name;
655                     rp = cmd->name;
656                     while (*lp != '\0' && *lp == *rp) {
657                               rp++;
658                               lp++;
659                     }
660 
661                     if (*lp != '\0') /* mismatch or extra chars in name */
662                               continue;
663 
664                     if (*rp == '\0') { /* exact match */
665                               *cmdp = cmd;
666                               return (CMD_EXACT);
667                     }
668 
669                     /* prefix match: end of name, not end of command */
670                     if (result == CMD_NONE) {
671                               result = CMD_PREFIX;
672                               *cmdp = cmd;
673                     }
674                     else if (result == CMD_PREFIX) {
675                               result = CMD_AMBIGUOUS;
676                               *cmdp = NULL;
677                     }
678           }
679 
680           return (result);
681 }
682 
683 
684 /*
685  * Search list of command tables for command
686  */
687 static int
db_cmd_search(const char * name,struct db_cmd_tbl_en_head * list_head,const struct db_command ** cmdp)688 db_cmd_search(const char *name,
689                 struct db_cmd_tbl_en_head *list_head,
690                 const struct db_command **cmdp)
691 {
692           struct db_cmd_tbl_en *list_ent;
693           const struct db_command *found_command;
694           bool accept_prefix_match;
695           int result;
696 
697           result = CMD_NONE;
698           found_command = NULL;
699           accept_prefix_match = true;
700 
701           TAILQ_FOREACH(list_ent, list_head, db_cmd_next) {
702                     const struct db_command *cmd;
703                     int found;
704 
705                     found = db_cmd_search_table(name, list_ent->db_cmd, &cmd);
706                     if (found == CMD_EXACT) {
707                               result = CMD_EXACT;
708                               found_command = cmd;
709                               break;
710                     }
711 
712                     if (found == CMD_PREFIX) {
713                               if (accept_prefix_match) {
714                                         /*
715                                          * Continue search, but note current result
716                                          * in case we won't find anything else.
717                                          */
718                                         accept_prefix_match = false;
719                                         result = CMD_PREFIX;
720                                         found_command = cmd;
721                               } else {
722                                         /*
723                                          * Watch out for globally ambiguous
724                                          * prefix match that is not locally
725                                          * ambiguous - with one match in one
726                                          * table and another match(es) in
727                                          * another table.
728                                          */
729                                         result = CMD_AMBIGUOUS;
730                                         found_command = NULL;
731                               }
732                     }
733                     else if (found == CMD_AMBIGUOUS) {
734                               accept_prefix_match = false;
735                               result = CMD_AMBIGUOUS;
736                               found_command = NULL;
737                     }
738           }
739 
740           *cmdp = found_command;
741           return result;
742 }
743 
744 static void
db_cmd_search_failed(char * name,int search_result)745 db_cmd_search_failed(char *name, int search_result)
746 {
747           if (search_result == CMD_NONE)
748                     db_printf("No such command: %s\n", name);
749           else
750                     db_printf("Ambiguous command: %s\n", name);
751 }
752 
753 
754 /*
755  * List commands to the console.
756  */
757 static void
db_cmd_list(const struct db_cmd_tbl_en_head * list)758 db_cmd_list(const struct db_cmd_tbl_en_head *list)
759 {
760 
761           struct db_cmd_tbl_en *list_ent;
762           const struct db_command *table;
763           size_t              i, j, w, columns, lines, numcmds, width=0;
764           const char          *p;
765 
766           TAILQ_FOREACH(list_ent,list,db_cmd_next) {
767                     table = list_ent->db_cmd;
768                     for (i = 0; table[i].name != NULL; i++) {
769                               w = strlen(table[i].name);
770                               if (w > width)
771                                         width = w;
772                     }
773           }
774 
775           width = DB_NEXT_TAB(width);
776 
777           columns = db_max_width / width;
778           if (columns == 0)
779                     columns = 1;
780 
781           TAILQ_FOREACH(list_ent,list,db_cmd_next) {
782                     table = list_ent->db_cmd;
783 
784                     for (numcmds = 0; table[numcmds].name != NULL; numcmds++)
785                               ;
786                     lines = (numcmds + columns - 1) / columns;
787 
788                     for (i = 0; i < lines; i++) {
789                               for (j = 0; j < columns; j++) {
790                                         p = table[j * lines + i].name;
791                                         if (p)
792                                                   db_printf("%s", p);
793                                         if (j * lines + i + lines >= numcmds) {
794                                                   db_putchar('\n');
795                                                   break;
796                                         }
797                                         if (p) {
798                                                   w = strlen(p);
799                                                   while (w < width) {
800                                                             w = DB_NEXT_TAB(w);
801                                                             db_putchar('\t');
802                                                   }
803                                         }
804                               }
805                     }
806           }
807           return;
808 }
809 
810 /*
811  * Read complete command with all subcommands, starting with current
812  * db_tok_string. If subcommand is missing, print the list of all
813  * subcommands.  If command/subcommand is not found, print an error
814  * message.  Returns pointer to "leaf" command or NULL.
815  */
816 static const struct db_command *
db_read_command(void)817 db_read_command(void)
818 {
819           const struct db_command *command;
820           struct db_cmd_tbl_en_head *list;
821           int found;
822           int t;
823 
824           list = &db_base_cmd_list;
825           do {
826                     found = db_cmd_search(db_tok_string, list, &command);
827                     if (command == NULL) {
828                               db_cmd_search_failed(db_tok_string, found);
829                               db_flush_lex();
830                               return NULL;
831                     }
832 
833                     if (command->flag == CS_SHOW)
834                               list = &db_show_cmd_list;
835                     else if (command->flag == CS_MACH)
836                               list = &db_mach_cmd_list;
837                     else if (command->flag == CS_COMPAT)
838                               /* same list */;
839                     else
840                               break; /* expect no more subcommands */
841 
842                     t = db_read_token(); /* read subcommand */
843                     if (t != tIDENT) {
844                               /* if none given - just print all of them */
845                               db_cmd_list(list);
846                               db_flush_lex();
847                               return NULL;
848                     }
849           } while (list != NULL);
850 
851           return command;
852 }
853 
854 /*
855  * Parse command line and execute appropriate function.
856  */
857 static void
db_command(const struct db_command ** last_cmdp)858 db_command(const struct db_command **last_cmdp)
859 {
860           static db_expr_t last_count = 0;
861 
862           int t;
863           const struct db_command *command;
864           db_expr_t addr, count;
865           bool have_addr;
866           char modif[TOK_STRING_SIZE];
867 
868           command = NULL;
869           have_addr = false;
870           count = -1;
871 
872           t = db_read_token();
873           if ((t == tEOL) || (t == tCOMMA)) {
874                     /*
875                      * An empty line repeats last command, at 'next'.
876                      * Only a count repeats the last command with the new count.
877                      */
878                     command = *last_cmdp;
879 
880                     if (!command)
881                               return;
882 
883                     addr = (db_expr_t)db_next;
884                     if (t == tCOMMA) {
885                               if (!db_expression(&count)) {
886                                         db_printf("Count missing\n");
887                                         db_flush_lex();
888                                         return;
889                               }
890                     } else
891                               count = last_count;
892                     modif[0] = '\0';
893                     db_skip_to_eol();
894 
895           } else if (t == tEXCL) {
896                     db_fncall(0, 0, 0, NULL);
897                     return;
898 
899           } else if (t != tIDENT) {
900                     db_printf("?\n");
901                     db_flush_lex();
902                     return;
903 
904           } else {
905 
906                     command = db_read_command();
907                     if (command == NULL)
908                               return;
909 
910                     if ((command->flag & CS_OWN) == 0) {
911 
912                               /*
913                                * Standard syntax:
914                                * command [/modifier] [addr] [,count]
915                                */
916                               t = db_read_token(); /* get modifier */
917                               if (t == tSLASH) {
918                                         t = db_read_token();
919                                         if (t != tIDENT) {
920                                                   db_printf("Bad modifier\n");
921                                                   db_flush_lex();
922                                                   return;
923                                         }
924                                         /* save modifier */
925                                         strlcpy(modif, db_tok_string, sizeof(modif));
926 
927                               } else {
928                                         db_unread_token(t);
929                                         modif[0] = '\0';
930                               }
931 
932                               if (db_expression(&addr)) { /*get address*/
933                                         db_dot = (db_addr_t) addr;
934                                         db_last_addr = db_dot;
935                                         have_addr = true;
936                               } else {
937                                         addr = (db_expr_t) db_dot;
938                               }
939 
940                               t = db_read_token();
941                               if (t == tCOMMA) { /*Get count*/
942                                         if (!db_expression(&count)) {
943                                                   db_printf("Count missing\n");
944                                                   db_flush_lex();
945                                                   return;
946                                         }
947                               } else {
948                                         db_unread_token(t);
949                               }
950                               if ((command->flag & CS_MORE) == 0) {
951                                         db_skip_to_eol();
952                               }
953                     }
954           }
955 
956           if (command != NULL && command->flag & CS_NOREPEAT) {
957                     *last_cmdp = NULL;
958                     last_count = 0;
959           } else {
960                     *last_cmdp = command;
961                     last_count = count;
962           }
963 
964 
965           if (command != NULL) {
966                     /*
967                      * Execute the command.
968                      */
969                     if (command->fcn != NULL)
970                               (*command->fcn)(addr, have_addr, count, modif);
971 
972                     if (command->flag & CS_SET_DOT) {
973                               /*
974                                * If command changes dot, set dot to
975                                * previous address displayed (if 'ed' style).
976                                */
977                               if (db_ed_style)
978                                         db_dot = db_prev;
979                               else
980                                         db_dot = db_next;
981                     } else {
982                               /*
983                                * If command does not change dot,
984                                * set 'next' location to be the same.
985                                */
986                               db_next = db_dot;
987                     }
988           }
989 }
990 
991 /*
992  * Print help for commands
993  */
994 static void
db_help_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)995 db_help_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
996 const char *modif)
997 {
998           const struct db_command *command;
999           int t;
1000 
1001           t = db_read_token();
1002 
1003           /* is there another command after the "help"? */
1004           if (t != tIDENT) {
1005                     /* print base commands */
1006                     db_cmd_list(&db_base_cmd_list);
1007                     return;
1008           }
1009 
1010           command = db_read_command();
1011           if (command == NULL)
1012                     return;
1013 
1014 #ifdef DDB_VERBOSE_HELP
1015           db_printf("Command: %s\n", command->name);
1016           if (command->cmd_descr != NULL)
1017                     db_printf(" Description: %s\n", command->cmd_descr);
1018           if (command->cmd_arg != NULL)
1019                     db_printf(" Arguments: %s\n", command->cmd_arg);
1020           if (command->cmd_arg_help != NULL)
1021                     db_printf(" Arguments description:\n%s\n",
1022                                 command->cmd_arg_help);
1023           if ((command->cmd_arg == NULL) && (command->cmd_descr == NULL))
1024                     db_printf(" No help message.\n");
1025 #endif
1026 
1027           db_skip_to_eol();
1028 }
1029 
1030 /*ARGSUSED*/
1031 static void
db_kqueue_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1032 db_kqueue_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
1033     const char *modif)
1034 {
1035 #ifdef _KERNEL
1036           bool full = false;
1037 
1038           if (modif[0] == 'f')
1039                     full = true;
1040 
1041           if (have_addr == false) {
1042                     db_printf("%s: must specify kqueue address\n", __func__);
1043                     return;
1044           }
1045 
1046           kqueue_printit((struct kqueue *)(uintptr_t) addr, full, db_printf);
1047 #else
1048           db_kernelonly();
1049 #endif    /* XXX CRASH(8) */
1050 }
1051 
1052 /*ARGSUSED*/
1053 static void
db_map_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1054 db_map_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
1055     const char *modif)
1056 {
1057 #ifdef _KERNEL
1058           bool full = false;
1059 
1060           if (modif[0] == 'f')
1061                     full = true;
1062 
1063           if (have_addr == false)
1064                     addr = (db_expr_t)(uintptr_t)db_read_ptr("kernel_map");
1065 
1066           uvm_map_printit((struct vm_map *)(uintptr_t) addr, full, db_printf);
1067 #else
1068           db_kernelonly();
1069 #endif    /* XXX CRASH(8) */
1070 }
1071 
1072 /*ARGSUSED*/
1073 static void
db_object_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1074 db_object_print_cmd(db_expr_t addr, bool have_addr,
1075     db_expr_t count, const char *modif)
1076 {
1077 #ifdef _KERNEL /* XXX CRASH(8) */
1078           bool full = false;
1079 
1080           if (modif[0] == 'f')
1081                     full = true;
1082 
1083           uvm_object_printit((struct uvm_object *)(uintptr_t) addr, full,
1084               db_printf);
1085 #else
1086           db_kernelonly();
1087 #endif
1088 }
1089 
1090 /*ARGSUSED*/
1091 static void
db_page_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1092 db_page_print_cmd(db_expr_t addr, bool have_addr,
1093     db_expr_t count, const char *modif)
1094 {
1095 #ifdef _KERNEL /* XXX CRASH(8) */
1096           bool full = false;
1097 
1098           if (modif[0] == 'f')
1099                     full = true;
1100 
1101           uvm_page_printit((struct vm_page *)(uintptr_t) addr, full, db_printf);
1102 #else
1103           db_kernelonly();
1104 #endif
1105 }
1106 
1107 /*ARGSUSED*/
1108 static void
db_show_all_pages(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1109 db_show_all_pages(db_expr_t addr, bool have_addr,
1110     db_expr_t count, const char *modif)
1111 {
1112 
1113 #ifdef _KERNEL /* XXX CRASH(8) */
1114           uvm_page_printall(db_printf);
1115 #else
1116           db_kernelonly();
1117 #endif
1118 }
1119 
1120 /*ARGSUSED*/
1121 static void
db_buf_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1122 db_buf_print_cmd(db_expr_t addr, bool have_addr,
1123     db_expr_t count, const char *modif)
1124 {
1125 #ifdef _KERNEL /* XXX CRASH(8) */
1126           bool full = false;
1127 
1128           if (modif[0] == 'f')
1129                     full = true;
1130 
1131           vfs_buf_print((struct buf *)(uintptr_t) addr, full, db_printf);
1132 #else
1133           db_kernelonly();
1134 #endif
1135 }
1136 
1137 /*ARGSUSED*/
1138 static void
db_event_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1139 db_event_print_cmd(db_expr_t addr, bool have_addr,
1140     db_expr_t count, const char *modif)
1141 {
1142           bool showzero = false;
1143           bool showall = true;
1144           bool showintr = false;
1145           bool showtrap = false;
1146           bool showmisc = false;
1147           struct evcnt ev, *evp;
1148           char buf[80];
1149           int i;
1150 
1151           i = 0;
1152           while (modif[i]) {
1153                     switch (modif[i]) {
1154                     case 'f':
1155                               showzero = true;
1156                               break;
1157                     case 'i':
1158                               showintr = true;
1159                               showall = false;
1160                               break;
1161                     case 't':
1162                               showtrap = true;
1163                               showall = false;
1164                               break;
1165                     case 'm':
1166                               showmisc = true;
1167                               showall = false;
1168                               break;
1169                     }
1170                     i++;
1171           }
1172 
1173           if (showall)
1174                     showmisc = showintr = showtrap = true;
1175 
1176           evp = (struct evcnt *)db_read_ptr("allevents");
1177           while (evp != NULL) {
1178                     db_read_bytes((db_addr_t)evp, sizeof(ev), (char *)&ev);
1179                     evp = ev.ev_list.tqe_next;
1180                     if (ev.ev_count == 0 && !showzero)
1181                               continue;
1182                     if (ev.ev_type == EVCNT_TYPE_INTR && !showintr)
1183                               continue;
1184                     if (ev.ev_type == EVCNT_TYPE_TRAP && !showtrap)
1185                               continue;
1186                     if (ev.ev_type == EVCNT_TYPE_MISC && !showmisc)
1187                               continue;
1188                     db_read_bytes((db_addr_t)ev.ev_group, ev.ev_grouplen + 1, buf);
1189                     db_printf("evcnt type %d: %s ", ev.ev_type, buf);
1190                     db_read_bytes((db_addr_t)ev.ev_name, ev.ev_namelen + 1, buf);
1191                     db_printf("%s = %lld\n", buf, (long long)ev.ev_count);
1192           }
1193 }
1194 
1195 /*ARGSUSED*/
1196 static void
db_vnode_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1197 db_vnode_print_cmd(db_expr_t addr, bool have_addr,
1198     db_expr_t count, const char *modif)
1199 {
1200 #ifdef _KERNEL /* XXX CRASH(8) */
1201           bool full = false;
1202 
1203           if (modif[0] == 'f')
1204                     full = true;
1205 
1206           vfs_vnode_print((struct vnode *)(uintptr_t) addr, full, db_printf);
1207 #else
1208           db_kernelonly();
1209 #endif
1210 }
1211 
1212 /*ARGSUSED*/
1213 static void
db_vnode_lock_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1214 db_vnode_lock_print_cmd(db_expr_t addr, bool have_addr,
1215     db_expr_t count, const char *modif)
1216 {
1217 #ifdef _KERNEL /* XXX CRASH(8) */
1218           bool full = false;
1219 
1220           if (modif[0] == 'f')
1221                     full = true;
1222 
1223           vfs_vnode_lock_print((struct vnode *)(uintptr_t) addr, full, db_printf);
1224 #else
1225           db_kernelonly();
1226 #endif
1227 }
1228 
1229 /*ARGSUSED*/
1230 static void
db_vmem_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1231 db_vmem_print_cmd(db_expr_t addr, bool have_addr,
1232     db_expr_t count, const char *modif)
1233 {
1234 
1235 #ifdef _KERNEL /* XXX CRASH(8) */
1236           vmem_print((uintptr_t) addr, modif, db_printf);
1237 #else
1238           db_kernelonly();
1239 #endif
1240 }
1241 
1242 static void
db_mount_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1243 db_mount_print_cmd(db_expr_t addr, bool have_addr,
1244     db_expr_t count, const char *modif)
1245 {
1246 #ifdef _KERNEL      /* XXX CRASH(8) */
1247           bool full = false;
1248 
1249           if (modif[0] == 'f')
1250                     full = true;
1251 
1252           vfs_mount_print((struct mount *)(uintptr_t) addr, full, db_printf);
1253 #endif
1254 }
1255 
1256 static void
db_show_all_mounts(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1257 db_show_all_mounts(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
1258 {
1259 #ifdef _KERNEL      /* XXX CRASH(8) */
1260           bool full = false;
1261 
1262           if (modif[0] == 'f')
1263                     full = true;
1264 
1265           vfs_mount_print_all(full, db_printf);
1266 #else
1267           db_kernelonly();
1268 #endif
1269 }
1270 
1271 /*ARGSUSED*/
1272 static void
db_mbuf_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1273 db_mbuf_print_cmd(db_expr_t addr, bool have_addr,
1274     db_expr_t count, const char *modif)
1275 {
1276 
1277 #ifdef _KERNEL /* XXX CRASH(8) */
1278           m_print((const struct mbuf *)(uintptr_t) addr, modif, db_printf);
1279 #else
1280           db_kernelonly();
1281 #endif
1282 }
1283 
1284 /*ARGSUSED*/
1285 static void
db_pool_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1286 db_pool_print_cmd(db_expr_t addr, bool have_addr,
1287     db_expr_t count, const char *modif)
1288 {
1289 
1290 #ifdef _KERNEL /* XXX CRASH(8) */
1291           pool_printit((struct pool *)(uintptr_t) addr, modif, db_printf);
1292 #else
1293           db_kernelonly();
1294 #endif
1295 }
1296 
1297 /*ARGSUSED*/
1298 static void
db_namecache_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1299 db_namecache_print_cmd(db_expr_t addr, bool have_addr,
1300     db_expr_t count, const char *modif)
1301 {
1302 
1303 #ifdef _KERNEL /* XXX CRASH(8) */
1304           namecache_print((struct vnode *)(uintptr_t) addr, db_printf);
1305 #else
1306           db_kernelonly();
1307 #endif
1308 }
1309 
1310 /*ARGSUSED*/
1311 static void
db_uvmexp_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1312 db_uvmexp_print_cmd(db_expr_t addr, bool have_addr,
1313     db_expr_t count, const char *modif)
1314 {
1315 
1316 #ifdef _KERNEL      /* XXX CRASH(8) */
1317           uvmexp_print(db_printf);
1318 #else
1319           db_kernelonly();
1320 #endif
1321 }
1322 
1323 /*ARGSUSED */
1324 static void
db_socket_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1325 db_socket_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
1326     const char *modif)
1327 {
1328 
1329 #ifdef _KERNEL      /* XXX CRASH(8) */
1330           socket_print(modif, db_printf);
1331 #else
1332           db_kernelonly();
1333 #endif
1334 }
1335 
1336 #ifdef KERNHIST
1337 /*ARGSUSED*/
1338 static void
db_kernhist_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1339 db_kernhist_print_cmd(db_expr_t addr, bool have_addr,
1340     db_expr_t count, const char *modif)
1341 {
1342 
1343           if (!have_addr)
1344                     addr = 0;
1345 
1346           if (count == -1)
1347                     count = 0;
1348 
1349           kernhist_print((void *)(uintptr_t)addr, count, modif, db_printf);
1350 }
1351 #endif
1352 
1353 /*ARGSUSED*/
1354 static void
db_lock_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1355 db_lock_print_cmd(db_expr_t addr, bool have_addr,
1356     db_expr_t count, const char *modif)
1357 {
1358 
1359           lockdebug_lock_print(have_addr ? (void *)(uintptr_t)addr : NULL,
1360               db_printf);
1361 }
1362 
1363 static void
db_show_all_locks(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1364 db_show_all_locks(db_expr_t addr, bool have_addr,
1365     db_expr_t count, const char *modif)
1366 {
1367 
1368 #ifdef _KERNEL      /* XXX CRASH(8) */
1369           lockdebug_show_all_locks(db_printf, modif);
1370 #else
1371           db_kernelonly();
1372 #endif
1373 }
1374 
1375 static void
db_show_all_tstiles(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1376 db_show_all_tstiles(db_expr_t addr, bool have_addr,
1377     db_expr_t count, const char *modif)
1378 {
1379           struct proc *p;
1380           bool trace = false;
1381 
1382           if (modif[0] == 't')
1383                     trace = true;
1384 
1385           db_printf("%5s %5s %16s %16s %8s %16s\n",
1386               "PID", "LID", "COMMAND", "WAITING-FOR", "TYPE", "WAIT-CHANNEL");
1387           for (p = db_proc_first(); p != NULL; p = db_proc_next(p)) {
1388                     pid_t pid = -1;
1389                     char comm[MAXCOMLEN + 1] = "";
1390                     struct lwp *l = NULL;
1391 
1392                     db_read_bytes((db_addr_t)&p->p_pid, sizeof(pid), (char *)&pid);
1393                     db_read_bytes((db_addr_t)p->p_comm, sizeof(comm), comm);
1394                     db_read_bytes((db_addr_t)&p->p_lwps.lh_first, sizeof(l),
1395                         (char *)&l);
1396                     while (l != NULL) {
1397                               wchan_t wchan = NULL;
1398                               char wchanname[128] = "";
1399                               lwpid_t lid = -1;
1400                               const struct syncobj *sobj = NULL;
1401                               struct lwp *owner = NULL;
1402                               char sobjname[sizeof(sobj->sobj_name)] = "";
1403 
1404                               db_read_bytes((db_addr_t)&l->l_wchan, sizeof(wchan),
1405                                   (char *)&wchan);
1406                               if (wchan == NULL)
1407                                         goto next;
1408                               db_symstr(wchanname, sizeof(wchanname),
1409                                   (db_expr_t)(uintptr_t)wchan, DB_STGY_ANY);
1410 
1411                               db_read_bytes((db_addr_t)&l->l_lid, sizeof(lid),
1412                                   (char *)&lid);
1413                               db_read_bytes((db_addr_t)&l->l_syncobj, sizeof(sobj),
1414                                   (char *)&sobj);
1415                               if (sobj == NULL) {
1416                                         db_printf("%5ld %5ld %16s %16s %8s %16s\n",
1417                                             (long)pid,
1418                                             (long)lid,
1419                                             comm,
1420                                             "(unknown)",
1421                                             "",
1422                                             wchanname);
1423                                         goto next;
1424                               }
1425                               db_read_bytes((db_addr_t)&sobj->sobj_name,
1426                                   sizeof(sobjname), sobjname);
1427 
1428                               owner = db_syncobj_owner(sobj, wchan);
1429 
1430                               db_printf("%5ld %5ld %16s %16lx %8s %16s\n",
1431                                   (long)pid,
1432                                   (long)lid,
1433                                   comm,
1434                                   (long)owner,
1435                                   sobjname,
1436                                   wchanname);
1437 
1438                               if (trace && owner != NULL) {
1439                                         db_stack_trace_print(
1440                                             (db_expr_t)(uintptr_t)owner,
1441                                             /*have_addr*/true, /*count*/-1,
1442                                             /*modif(lwp)*/"a", db_printf);
1443                               }
1444 
1445 next:                         db_read_bytes((db_addr_t)&l->l_sibling.le_next,
1446                                   sizeof(l), (char *)&l);
1447                     }
1448           }
1449 }
1450 
1451 static void
db_show_all_freelists(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1452 db_show_all_freelists(db_expr_t addr, bool have_addr,
1453     db_expr_t count, const char *modif)
1454 {
1455 
1456 #ifdef _KERNEL      /* XXX CRASH(8) */
1457           uvm_page_print_freelists(db_printf);
1458 #else
1459           db_kernelonly();
1460 #endif
1461 }
1462 
1463 static void
db_show_lockstats(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1464 db_show_lockstats(db_expr_t addr, bool have_addr,
1465     db_expr_t count, const char *modif)
1466 {
1467 
1468 #ifdef _KERNEL      /* XXX CRASH(8) */
1469           lockdebug_show_lockstats(db_printf);
1470 #else
1471           db_kernelonly();
1472 #endif
1473 }
1474 
1475 #ifdef FDT
1476 /*ARGSUSED*/
1477 static void
db_fdt_print_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1478 db_fdt_print_cmd(db_expr_t addr, bool have_addr,
1479     db_expr_t count, const char *modif)
1480 {
1481 #ifdef _KERNEL /* XXX CRASH(8) */
1482           bool full = false;
1483 
1484           if (modif[0] == 'f')
1485                     full = true;
1486 
1487           fdt_print(have_addr ? (void *)(uintptr_t)addr : fdtbus_get_data(),
1488               full, db_printf);
1489 #else
1490           db_kernelonly();
1491 #endif
1492 }
1493 #endif
1494 
1495 /*
1496  * Call random function:
1497  * !expr(arg,arg,arg)
1498  */
1499 /*ARGSUSED*/
1500 static void
db_fncall(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1501 db_fncall(db_expr_t addr, bool have_addr,
1502     db_expr_t count, const char *modif)
1503 {
1504 #ifdef _KERNEL
1505           db_expr_t fn_addr;
1506 #define   MAXARGS             11
1507           db_expr_t args[MAXARGS];
1508           int                 nargs = 0;
1509           db_expr_t retval;
1510           db_expr_t (*func)(db_expr_t, ...);
1511           int                 t;
1512 
1513           if (!db_expression(&fn_addr)) {
1514                     db_printf("Bad function\n");
1515                     db_flush_lex();
1516                     return;
1517           }
1518           func = (db_expr_t (*)(db_expr_t, ...))(uintptr_t) fn_addr;
1519 
1520           t = db_read_token();
1521           if (t == tLPAREN) {
1522                     if (db_expression(&args[0])) {
1523                               nargs++;
1524                               while ((t = db_read_token()) == tCOMMA) {
1525                                         if (nargs == MAXARGS) {
1526                                                   db_printf("Too many arguments\n");
1527                                                   db_flush_lex();
1528                                                   return;
1529                                         }
1530                                         if (!db_expression(&args[nargs])) {
1531                                                   db_printf("Argument missing\n");
1532                                                   db_flush_lex();
1533                                                   return;
1534                                         }
1535                                         nargs++;
1536                               }
1537                               db_unread_token(t);
1538                     }
1539                     if (db_read_token() != tRPAREN) {
1540                               db_printf("?\n");
1541                               db_flush_lex();
1542                               return;
1543                     }
1544           }
1545           db_skip_to_eol();
1546 
1547           while (nargs < MAXARGS) {
1548                     args[nargs++] = 0;
1549           }
1550 
1551           retval = (*func)(args[0], args[1], args[2], args[3], args[4],
1552                                args[5], args[6], args[7], args[8], args[9]);
1553           db_printf("%s\n", db_num_to_str(retval));
1554 #else     /* _KERNEL */
1555           db_kernelonly();
1556 #endif    /* _KERNEL */
1557 }
1558 
1559 static void
db_reboot_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1560 db_reboot_cmd(db_expr_t addr, bool have_addr,
1561     db_expr_t count, const char *modif)
1562 {
1563 #ifdef _KERNEL
1564           db_expr_t bootflags;
1565 
1566           /* Flags, default to RB_AUTOBOOT */
1567           if (!db_expression(&bootflags))
1568                     bootflags = (db_expr_t)RB_AUTOBOOT;
1569           if (db_read_token() != tEOL) {
1570                     db_error("?\n");
1571                     /*NOTREACHED*/
1572           }
1573           /*
1574            * We are leaving DDB, never to return upward.
1575            * Clear db_recover so that we can debug faults in functions
1576            * called from cpu_reboot.
1577            */
1578           db_recover = 0;
1579           /* Avoid all mutex errors */
1580           lockdebug_dismiss();
1581           panicstr = "reboot forced via kernel debugger";
1582           /* Make it possible to break into the debugger again */
1583           spl0();
1584           kern_reboot((int)bootflags, NULL);
1585 #else     /* _KERNEL */
1586           db_kernelonly();
1587 #endif    /* _KERNEL */
1588 }
1589 
1590 static void
db_sifting_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1591 db_sifting_cmd(db_expr_t addr, bool have_addr,
1592     db_expr_t count, const char *modif)
1593 {
1594           int       mode, t;
1595 
1596           t = db_read_token();
1597           if (t == tSLASH) {
1598                     t = db_read_token();
1599                     if (t != tIDENT) {
1600                               bad_modifier:
1601                               db_printf("Bad modifier\n");
1602                               db_flush_lex();
1603                               return;
1604                     }
1605                     if (!strcmp(db_tok_string, "F"))
1606                               mode = 'F';
1607                     else
1608                               goto bad_modifier;
1609                     t = db_read_token();
1610           } else
1611                     mode = 0;
1612 
1613           if (t == tIDENT)
1614                     db_sifting(db_tok_string, mode);
1615           else {
1616                     db_printf("Bad argument (non-string)\n");
1617                     db_flush_lex();
1618           }
1619 }
1620 
1621 static void
db_stack_trace_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1622 db_stack_trace_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
1623 {
1624           register const char *cp = modif;
1625           register char c;
1626           void (*pr)(const char *, ...);
1627 
1628           pr = db_printf;
1629           while ((c = *cp++) != 0)
1630                     if (c == 'l')
1631                               pr = (void (*)(const char *, ...))printf;
1632 
1633           if (count == -1)
1634                     count = 65535;
1635 
1636           db_stack_trace_print(addr, have_addr, count, modif, pr);
1637 }
1638 
1639 static void
db_sync_cmd(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)1640 db_sync_cmd(db_expr_t addr, bool have_addr,
1641     db_expr_t count, const char *modif)
1642 {
1643 #ifdef _KERNEL
1644           /*
1645            * We are leaving DDB, never to return upward.
1646            * Clear db_recover so that we can debug faults in functions
1647            * called from cpu_reboot.
1648            */
1649           db_recover = 0;
1650           panicstr = "dump forced via kernel debugger";
1651           kern_reboot(RB_DUMP, NULL);
1652 #else     /* _KERNEL */
1653           db_kernelonly();
1654 #endif    /* _KERNEL */
1655 }
1656 
1657 /*
1658  * Describe what an address is
1659  */
1660 void
db_whatis_cmd(db_expr_t address,bool have_addr,db_expr_t count,const char * modif)1661 db_whatis_cmd(db_expr_t address, bool have_addr,
1662     db_expr_t count, const char *modif)
1663 {
1664           const uintptr_t addr = (uintptr_t)address;
1665 
1666           db_lwp_whatis(addr, db_printf);
1667 #ifdef _KERNEL      /* XXX CRASH(8) */
1668           pool_whatis(addr, db_printf);
1669           vmem_whatis(addr, db_printf);
1670           uvm_whatis(addr, db_printf);
1671           module_whatis(addr, db_printf);
1672 #else
1673           db_kernelonly();
1674 #endif
1675 }
1676