db_xxx.c revision 1.79 1 1.79 martin /* $NetBSD: db_xxx.c,v 1.79 2023/10/15 10:40:52 martin Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 1.1 gwr * The Regents of the University of California. All rights reserved.
6 1.1 gwr *
7 1.1 gwr * Redistribution and use in source and binary forms, with or without
8 1.1 gwr * modification, are permitted provided that the following conditions
9 1.1 gwr * are met:
10 1.1 gwr * 1. Redistributions of source code must retain the above copyright
11 1.1 gwr * notice, this list of conditions and the following disclaimer.
12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gwr * notice, this list of conditions and the following disclaimer in the
14 1.1 gwr * documentation and/or other materials provided with the distribution.
15 1.26 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 gwr * may be used to endorse or promote products derived from this software
17 1.1 gwr * without specific prior written permission.
18 1.1 gwr *
19 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 gwr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 gwr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 gwr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 gwr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 gwr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 gwr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 gwr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 gwr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 gwr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 gwr * SUCH DAMAGE.
30 1.1 gwr *
31 1.1 gwr * from: kern_proc.c 8.4 (Berkeley) 1/4/94
32 1.1 gwr */
33 1.1 gwr
34 1.1 gwr /*
35 1.1 gwr * Miscellaneous DDB functions that are intimate (xxx) with various
36 1.1 gwr * data structures and functions used by the kernel (proc, callout).
37 1.1 gwr */
38 1.15 lukem
39 1.46 dsl #include <sys/cdefs.h>
40 1.79 martin __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.79 2023/10/15 10:40:52 martin Exp $");
41 1.46 dsl
42 1.57 ad #ifdef _KERNEL_OPT
43 1.21 briggs #include "opt_kgdb.h"
44 1.53 ad #include "opt_aio.h"
45 1.62 rmind #include "opt_mqueue.h"
46 1.57 ad #endif
47 1.57 ad
48 1.59 mrg #ifndef _KERNEL
49 1.59 mrg #include <stdbool.h>
50 1.59 mrg #endif
51 1.59 mrg
52 1.1 gwr #include <sys/param.h>
53 1.72 riastrad #include <sys/atomic.h>
54 1.1 gwr #include <sys/systm.h>
55 1.1 gwr #include <sys/kernel.h>
56 1.1 gwr #include <sys/proc.h>
57 1.12 atatat #include <sys/msgbuf.h>
58 1.1 gwr #include <sys/callout.h>
59 1.49 blymn #include <sys/file.h>
60 1.49 blymn #include <sys/filedesc.h>
61 1.49 blymn #include <sys/lockdebug.h>
62 1.1 gwr #include <sys/signalvar.h>
63 1.3 ross #include <sys/resourcevar.h>
64 1.34 he #include <sys/pool.h>
65 1.57 ad #include <sys/uio.h>
66 1.38 elad #include <sys/kauth.h>
67 1.47 rmind #include <sys/mqueue.h>
68 1.49 blymn #include <sys/vnode.h>
69 1.54 ad #include <sys/module.h>
70 1.55 ad #include <sys/cpu.h>
71 1.56 yamt #include <sys/vmem.h>
72 1.76 ad #include <sys/condvar.h>
73 1.76 ad #include <sys/sleepq.h>
74 1.76 ad #include <sys/selinfo.h>
75 1.78 ad #include <sys/pipe.h>
76 1.1 gwr
77 1.57 ad #include <ddb/ddb.h>
78 1.58 mrg #include <ddb/db_user.h>
79 1.1 gwr
80 1.21 briggs #ifdef KGDB
81 1.21 briggs #include <sys/kgdb.h>
82 1.21 briggs #endif
83 1.1 gwr
84 1.1 gwr void
85 1.42 matt db_kill_proc(db_expr_t addr, bool haddr,
86 1.40 christos db_expr_t count, const char *modif)
87 1.1 gwr {
88 1.63 christos #ifdef _KERNEL /* XXX CRASH(8) */
89 1.63 christos struct proc *p;
90 1.63 christos ksiginfo_t ksi;
91 1.63 christos db_expr_t pid, sig;
92 1.63 christos int t;
93 1.63 christos
94 1.63 christos /* What pid? */
95 1.63 christos if (!db_expression(&pid)) {
96 1.63 christos db_error("pid?\n");
97 1.63 christos /*NOTREACHED*/
98 1.63 christos }
99 1.63 christos /* What sig? */
100 1.63 christos t = db_read_token();
101 1.63 christos if (t == tCOMMA) {
102 1.63 christos if (!db_expression(&sig)) {
103 1.63 christos db_error("sig?\n");
104 1.63 christos /*NOTREACHED*/
105 1.63 christos }
106 1.63 christos } else {
107 1.63 christos db_unread_token(t);
108 1.63 christos sig = 15;
109 1.63 christos }
110 1.63 christos if (db_read_token() != tEOL) {
111 1.63 christos db_error("?\n");
112 1.63 christos /*NOTREACHED*/
113 1.63 christos }
114 1.65 christos /* We might stop when the mutex is held or when not */
115 1.75 ad t = mutex_tryenter(&proc_lock);
116 1.66 christos #ifdef DIAGNOSTIC
117 1.66 christos if (!t) {
118 1.66 christos db_error("could not acquire proc_lock mutex\n");
119 1.66 christos /*NOTREACHED*/
120 1.66 christos }
121 1.66 christos #endif
122 1.63 christos p = proc_find((pid_t)pid);
123 1.63 christos if (p == NULL) {
124 1.66 christos if (t)
125 1.75 ad mutex_exit(&proc_lock);
126 1.66 christos db_error("no such proc\n");
127 1.66 christos /*NOTREACHED*/
128 1.63 christos }
129 1.63 christos KSI_INIT(&ksi);
130 1.63 christos ksi.ksi_signo = sig;
131 1.63 christos ksi.ksi_code = SI_USER;
132 1.63 christos ksi.ksi_pid = 0;
133 1.63 christos ksi.ksi_uid = 0;
134 1.66 christos mutex_enter(p->p_lock);
135 1.63 christos kpsignal2(p, &ksi);
136 1.66 christos mutex_exit(p->p_lock);
137 1.66 christos if (t)
138 1.75 ad mutex_exit(&proc_lock);
139 1.63 christos #else
140 1.74 christos db_kernelonly();
141 1.63 christos #endif
142 1.1 gwr }
143 1.21 briggs
144 1.21 briggs #ifdef KGDB
145 1.21 briggs void
146 1.42 matt db_kgdb_cmd(db_expr_t addr, bool haddr,
147 1.40 christos db_expr_t count, const char *modif)
148 1.21 briggs {
149 1.21 briggs kgdb_active++;
150 1.21 briggs kgdb_trap(db_trap_type, DDB_REGS);
151 1.21 briggs kgdb_active--;
152 1.21 briggs }
153 1.21 briggs #endif
154 1.1 gwr
155 1.1 gwr void
156 1.49 blymn db_show_files_cmd(db_expr_t addr, bool haddr,
157 1.49 blymn db_expr_t count, const char *modif)
158 1.49 blymn {
159 1.57 ad #ifdef _KERNEL /* XXX CRASH(8) */
160 1.49 blymn struct proc *p;
161 1.49 blymn int i;
162 1.50 blymn filedesc_t *fdp;
163 1.50 blymn fdfile_t *ff;
164 1.50 blymn file_t *fp;
165 1.70 matt struct vnode *vp;
166 1.49 blymn bool full = false;
167 1.61 ad fdtab_t *dt;
168 1.49 blymn
169 1.71 ozaki if (!haddr) {
170 1.71 ozaki db_printf("usage: show files address\n");
171 1.71 ozaki db_printf("\taddress == an address of a proc structure\n");
172 1.71 ozaki return;
173 1.71 ozaki }
174 1.71 ozaki
175 1.49 blymn if (modif[0] == 'f')
176 1.49 blymn full = true;
177 1.49 blymn
178 1.52 rmind p = (struct proc *) (uintptr_t) addr;
179 1.49 blymn
180 1.50 blymn fdp = p->p_fd;
181 1.72 riastrad dt = atomic_load_consume(&fdp->fd_dt);
182 1.61 ad for (i = 0; i < dt->dt_nfiles; i++) {
183 1.61 ad if ((ff = dt->dt_ff[i]) == NULL)
184 1.49 blymn continue;
185 1.49 blymn
186 1.73 riastrad fp = atomic_load_consume(&ff->ff_file);
187 1.49 blymn
188 1.49 blymn /* Only look at vnodes... */
189 1.70 matt if (fp != NULL && fp->f_type == DTYPE_VNODE
190 1.70 matt && fp->f_vnode != NULL) {
191 1.70 matt vp = fp->f_vnode;
192 1.70 matt vfs_vnode_print(vp, full, db_printf);
193 1.49 blymn
194 1.49 blymn #ifdef LOCKDEBUG
195 1.70 matt db_printf("\nv_uobj.vmobjlock lock details:\n");
196 1.70 matt lockdebug_lock_print(vp->v_uobj.vmobjlock, db_printf);
197 1.70 matt db_printf("\n");
198 1.49 blymn #endif
199 1.49 blymn }
200 1.49 blymn }
201 1.57 ad #endif
202 1.49 blymn }
203 1.49 blymn
204 1.53 ad #ifdef AIO
205 1.49 blymn void
206 1.43 rmind db_show_aio_jobs(db_expr_t addr, bool haddr,
207 1.43 rmind db_expr_t count, const char *modif)
208 1.43 rmind {
209 1.57 ad
210 1.43 rmind aio_print_jobs(db_printf);
211 1.43 rmind }
212 1.53 ad #endif
213 1.43 rmind
214 1.62 rmind #ifdef MQUEUE
215 1.43 rmind void
216 1.47 rmind db_show_mqueue_cmd(db_expr_t addr, bool haddr,
217 1.47 rmind db_expr_t count, const char *modif)
218 1.47 rmind {
219 1.57 ad
220 1.57 ad #ifdef _KERNEL /* XXX CRASH(8) */
221 1.47 rmind mqueue_print_list(db_printf);
222 1.57 ad #endif
223 1.47 rmind }
224 1.62 rmind #endif
225 1.47 rmind
226 1.47 rmind void
227 1.54 ad db_show_module_cmd(db_expr_t addr, bool haddr,
228 1.54 ad db_expr_t count, const char *modif)
229 1.54 ad {
230 1.57 ad
231 1.57 ad #ifdef _KERNEL /* XXX CRASH(8) */
232 1.54 ad module_print_list(db_printf);
233 1.57 ad #endif
234 1.12 atatat }
235 1.12 atatat
236 1.12 atatat void
237 1.42 matt db_show_all_pools(db_expr_t addr, bool haddr,
238 1.40 christos db_expr_t count, const char *modif)
239 1.33 yamt {
240 1.33 yamt
241 1.57 ad #ifdef _KERNEL /* XXX CRASH(8) */
242 1.33 yamt pool_printall(modif, db_printf);
243 1.57 ad #endif
244 1.33 yamt }
245 1.33 yamt
246 1.33 yamt void
247 1.56 yamt db_show_all_vmems(db_expr_t addr, bool have_addr,
248 1.56 yamt db_expr_t count, const char *modif)
249 1.56 yamt {
250 1.56 yamt
251 1.57 ad #ifdef _KERNEL /* XXX CRASH(8) */
252 1.56 yamt vmem_printall(modif, db_printf);
253 1.57 ad #endif
254 1.56 yamt }
255 1.56 yamt
256 1.56 yamt void
257 1.60 ad db_dmesg(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
258 1.12 atatat {
259 1.60 ad struct kern_msgbuf mb, *mbp;
260 1.25 simonb db_expr_t print;
261 1.60 ad int newl, skip, i;
262 1.60 ad char *p, *bufdata, ch;
263 1.22 atatat
264 1.60 ad if (!db_read_int("msgbufenabled")) {
265 1.60 ad db_printf("message buffer not available\n");
266 1.60 ad return;
267 1.60 ad }
268 1.60 ad mbp = (struct kern_msgbuf *)db_read_ptr("msgbufp");
269 1.60 ad db_read_bytes((db_addr_t)mbp, sizeof(mb), (char *)&mb);
270 1.60 ad if (mb.msg_magic != MSG_MAGIC) {
271 1.22 atatat db_printf("message buffer not available\n");
272 1.22 atatat return;
273 1.22 atatat }
274 1.12 atatat
275 1.12 atatat bufdata = &mbp->msg_bufc[0];
276 1.12 atatat
277 1.60 ad if (haddr && addr < mb.msg_bufs)
278 1.25 simonb print = addr;
279 1.25 simonb else
280 1.60 ad print = mb.msg_bufs;
281 1.25 simonb
282 1.60 ad for (newl = skip = i = 0, p = bufdata + mb.msg_bufx;
283 1.60 ad i < mb.msg_bufs; i++, p++) {
284 1.60 ad if (p == bufdata + mb.msg_bufs)
285 1.12 atatat p = bufdata;
286 1.60 ad if (i < mb.msg_bufs - print) {
287 1.25 simonb continue;
288 1.60 ad }
289 1.60 ad db_read_bytes((db_addr_t)p, sizeof(ch), &ch);
290 1.12 atatat /* Skip "\n<.*>" syslog sequences. */
291 1.12 atatat if (skip) {
292 1.12 atatat if (ch == '>')
293 1.12 atatat newl = skip = 0;
294 1.12 atatat continue;
295 1.12 atatat }
296 1.12 atatat if (newl && ch == '<') {
297 1.12 atatat skip = 1;
298 1.12 atatat continue;
299 1.12 atatat }
300 1.12 atatat if (ch == '\0')
301 1.12 atatat continue;
302 1.12 atatat newl = ch == '\n';
303 1.12 atatat db_printf("%c", ch);
304 1.12 atatat }
305 1.12 atatat if (!newl)
306 1.12 atatat db_printf("\n");
307 1.28 thorpej }
308 1.28 thorpej
309 1.28 thorpej void
310 1.42 matt db_show_sched_qs(db_expr_t addr, bool haddr,
311 1.40 christos db_expr_t count, const char *modif)
312 1.28 thorpej {
313 1.44 yamt
314 1.57 ad #ifdef _KERNEL /* XXX CRASH(8) */
315 1.44 yamt sched_print_runqueue(db_printf);
316 1.57 ad #endif
317 1.1 gwr }
318 1.67 christos
319 1.67 christos void
320 1.67 christos db_show_panic(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
321 1.67 christos {
322 1.67 christos #ifdef _KERNEL /* XXX CRASH(8) */
323 1.67 christos int s;
324 1.67 christos
325 1.67 christos s = splhigh();
326 1.67 christos
327 1.67 christos db_printf("Panic string: %s\n", panicstr);
328 1.67 christos
329 1.67 christos (void)splx(s);
330 1.67 christos #endif
331 1.67 christos }
332 1.76 ad
333 1.76 ad void
334 1.76 ad db_show_condvar(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
335 1.76 ad {
336 1.77 martin kcondvar_t *cv = (kcondvar_t *)(uintptr_t)addr;
337 1.76 ad char buf[9], *wmesg;
338 1.76 ad
339 1.76 ad /* XXX messing with kcondvar_t guts without defs */
340 1.76 ad db_read_bytes((db_addr_t)&cv->cv_opaque[1], sizeof(wmesg),
341 1.76 ad (char *)&wmesg);
342 1.76 ad db_read_bytes((db_addr_t)wmesg, sizeof(buf) - 1, buf);
343 1.76 ad buf[sizeof(buf) - 1] = '\0';
344 1.76 ad db_printf("wmesg=%s ", buf);
345 1.76 ad db_show_sleepq((db_addr_t)&cv->cv_opaque[0], false, 0, modif);
346 1.76 ad }
347 1.76 ad
348 1.76 ad void
349 1.76 ad db_show_sleepq(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
350 1.76 ad {
351 1.76 ad sleepq_t sq;
352 1.76 ad lwp_t *lp;
353 1.76 ad
354 1.76 ad db_read_bytes(addr, sizeof(lp), (char *)&sq);
355 1.76 ad db_printf("sleepq=");
356 1.76 ad if ((lp = LIST_FIRST(&sq)) == NULL) {
357 1.76 ad db_printf("<empty>");
358 1.76 ad }
359 1.76 ad while (lp != NULL) {
360 1.76 ad db_printf("%p", lp);
361 1.76 ad db_read_bytes((db_addr_t)&lp->l_sleepchain.le_next, sizeof(lp),
362 1.76 ad (char *)&lp);
363 1.76 ad if (lp != NULL)
364 1.76 ad db_printf(",");
365 1.76 ad }
366 1.76 ad db_printf("\n");
367 1.76 ad }
368 1.76 ad
369 1.76 ad void
370 1.78 ad db_show_pipe(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
371 1.78 ad {
372 1.79 martin struct pipe pipe, *ppipe = (struct pipe *)(uintptr_t)addr;
373 1.78 ad
374 1.78 ad db_read_bytes(addr, sizeof(pipe), (char *)&pipe);
375 1.78 ad
376 1.78 ad db_printf("pipe_lock\t\t%p\n", pipe.pipe_lock);
377 1.78 ad
378 1.78 ad db_printf("pipe_read\t\t");
379 1.78 ad db_show_condvar((db_addr_t)&ppipe->pipe_read, false, 0, modif);
380 1.78 ad
381 1.78 ad db_printf("pipe_write\t\t");
382 1.78 ad db_show_condvar((db_addr_t)&ppipe->pipe_write, false, 0, modif);
383 1.78 ad
384 1.78 ad db_printf("pipe_busy\t\t");
385 1.78 ad db_show_condvar((db_addr_t)&ppipe->pipe_busy, false, 0, modif);
386 1.78 ad
387 1.78 ad db_printf("pipe_buffer.cnt\t\t%ld\n", (long)pipe.pipe_buffer.cnt);
388 1.78 ad db_printf("pipe_buffer.in\t\t%d\n", pipe.pipe_buffer.in);
389 1.78 ad db_printf("pipe_buffer.out\t\t%d\n", pipe.pipe_buffer.out);
390 1.78 ad db_printf("pipe_buffer.size\t%ld\n", (long)pipe.pipe_buffer.size);
391 1.78 ad db_printf("pipe_buffer.buffer\t%p\n", pipe.pipe_buffer.buffer);
392 1.78 ad
393 1.78 ad db_printf("pipe_wrsel\t\t");
394 1.78 ad db_show_selinfo((db_addr_t)&ppipe->pipe_wrsel, false, 0, modif);
395 1.78 ad db_printf("pipe_rdsel\t\t");
396 1.78 ad db_show_selinfo((db_addr_t)&ppipe->pipe_rdsel, false, 0, modif);
397 1.78 ad
398 1.78 ad db_printf("pipe_atime\t\t");
399 1.78 ad db_print_timespec(&pipe.pipe_atime);
400 1.78 ad
401 1.78 ad db_printf("\npipe_mtime\t\t");
402 1.78 ad db_print_timespec(&pipe.pipe_mtime);
403 1.78 ad
404 1.78 ad db_printf("\npipe_btime\t\t");
405 1.78 ad db_print_timespec(&pipe.pipe_btime);
406 1.78 ad
407 1.78 ad db_printf("\npipe_kmem\t\t%lx\n", (long)pipe.pipe_kmem);
408 1.78 ad db_printf("pipe_owner\t\t%p\n", pipe.pipe_owner);
409 1.78 ad db_printf("pipe_wrpgid\t\t%d\n", pipe.pipe_wrpgid);
410 1.78 ad db_printf("pipe_rdpgid\t\t%d\n", pipe.pipe_rdpgid);
411 1.78 ad db_printf("pipe_state\t\t%#08x\n", pipe.pipe_state);
412 1.78 ad }
413 1.78 ad
414 1.78 ad void
415 1.76 ad db_show_selinfo(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
416 1.76 ad {
417 1.76 ad struct selinfo sel;
418 1.76 ad
419 1.76 ad db_read_bytes(addr, sizeof(sel), (char *)&sel);
420 1.76 ad
421 1.76 ad db_printf("collision=%llx klist=%p cluster=%p lwp=%p fdinfo=%lx "
422 1.76 ad "sel_chain=%p\n", (long long)sel.sel_collision,
423 1.76 ad SLIST_FIRST(&sel.sel_klist), sel.sel_cluster, sel.sel_lwp,
424 1.76 ad (long)sel.sel_fdinfo, sel.sel_chain.sle_next);
425 1.76 ad }
426