xref: /netbsd-src/sys/ddb/db_xxx.c (revision 69b6d498973bb4d7230c2d3c12bd9a032738ec8e)
1 /*	$NetBSD: db_xxx.c,v 1.31 2005/06/01 12:25:27 drochner Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	from: kern_proc.c	8.4 (Berkeley) 1/4/94
32  */
33 
34 /*
35  * Miscellaneous DDB functions that are intimate (xxx) with various
36  * data structures and functions used by the kernel (proc, callout).
37  */
38 
39 #include "opt_kgdb.h"
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.31 2005/06/01 12:25:27 drochner Exp $");
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/proc.h>
48 #include <sys/msgbuf.h>
49 
50 #include <sys/callout.h>
51 #include <sys/signalvar.h>
52 #include <sys/resourcevar.h>
53 
54 #include <machine/db_machdep.h>
55 
56 #include <ddb/db_access.h>
57 #include <ddb/db_command.h>
58 #include <ddb/db_interface.h>
59 #include <ddb/db_lex.h>
60 #include <ddb/db_output.h>
61 #include <ddb/db_sym.h>
62 #include <ddb/db_extern.h>
63 #ifdef KGDB
64 #include <sys/kgdb.h>
65 #endif
66 
67 void
68 db_kill_proc(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
69 {
70 	struct proc *p;
71 	db_expr_t pid, sig;
72 	int t;
73 
74 	/* What pid? */
75 	if (!db_expression(&pid)) {
76 		db_error("pid?\n");
77 		/*NOTREACHED*/
78 	}
79 	/* What sig? */
80 	t = db_read_token();
81 	if (t == tCOMMA) {
82 		if (!db_expression(&sig)) {
83 			db_error("sig?\n");
84 			/*NOTREACHED*/
85 		}
86 	} else {
87 		db_unread_token(t);
88 		sig = 15;
89 	}
90 	if (db_read_token() != tEOL) {
91 		db_error("?\n");
92 		/*NOTREACHED*/
93 	}
94 
95 	p = pfind((pid_t)pid);
96 	if (p == NULL) {
97 		db_error("no such proc\n");
98 		/*NOTREACHED*/
99 	}
100 	psignal(p, (int)sig);
101 }
102 
103 #ifdef KGDB
104 void
105 db_kgdb_cmd(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
106 {
107 	kgdb_active++;
108 	kgdb_trap(db_trap_type, DDB_REGS);
109 	kgdb_active--;
110 }
111 #endif
112 
113 void
114 db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
115 {
116 	int i;
117 
118 	const char *mode;
119 	struct proc *p, *pp, *cp;
120 	struct lwp *l, *cl;
121 	struct timeval tv[2];
122 	const struct proclist_desc *pd;
123 
124 	if (modif[0] == 0)
125 		mode = "n";			/* default == normal mode */
126 	else
127 		mode = strchr("mawln", modif[0]);
128 
129 	if (mode == NULL || *mode == 'm') {
130 		db_printf("usage: show all procs [/a] [/n] [/w]\n");
131 		db_printf("\t/a == show process address info\n");
132 		db_printf("\t/l == show LWP info\n");
133 		db_printf("\t/n == show normal process info [default]\n");
134 		db_printf("\t/w == show process wait/emul info\n");
135 		return;
136 	}
137 
138 	switch (*mode) {
139 	case 'a':
140 		db_printf(" PID       %10s %18s %18s %18s\n",
141 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
142 		break;
143 	case 'l':
144 		db_printf(" PID        %4s S %9s %18s %18s %-12s\n",
145 		    "LID", "FLAGS", "STRUCT LWP *", "UAREA *", "WAIT");
146 		break;
147 	case 'n':
148 		db_printf(" PID       %8s %8s %10s S %7s %4s %16s %7s\n",
149 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
150 		break;
151 	case 'w':
152 		db_printf(" PID       %10s %8s %4s %5s %5s %-12s%s\n",
153 		    "COMMAND", "EMUL", "PRI", "UTIME", "STIME",
154 		    "WAIT-MSG", "WAIT-CHANNEL");
155 		break;
156 	}
157 
158 	/* XXX LOCKING XXX */
159 	pd = proclists;
160 	cp = curproc;
161 	cl = curlwp;
162 	for (pd = proclists; pd->pd_list != NULL; pd++) {
163 		LIST_FOREACH(p, pd->pd_list, p_list) {
164 			pp = p->p_pptr;
165 			if (p->p_stat == 0) {
166 				continue;
167 			}
168 			l = LIST_FIRST(&p->p_lwps);
169 			db_printf("%c%-10d", " >"[cp == p], p->p_pid);
170 
171 			switch (*mode) {
172 
173 			case 'a':
174 				db_printf("%10.10s %18p %18p %18p\n",
175 				    p->p_comm, p,
176 				    l != NULL ? l->l_addr : 0,
177 				    p->p_vmspace);
178 				break;
179 			case 'l':
180 				 while (l != NULL) {
181 					db_printf("%c%4d %d %#9x %18p %18p %s\n",
182 					    " >"[cl == l], l->l_lid,
183 					    l->l_stat, l->l_flag, l,
184 					    l->l_addr,
185 					    (l->l_wchan && l->l_wmesg) ?
186 					    l->l_wmesg : "");
187 
188 					l = LIST_NEXT(l, l_sibling);
189 					if (l)
190 						db_printf("%11s","");
191 				}
192 				break;
193 			case 'n':
194 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
195 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
196 				    p->p_cred->p_ruid, p->p_stat, p->p_flag,
197 				    p->p_nlwps, p->p_comm,
198 				    (p->p_nlwps != 1) ? "*" : (
199 				    (l->l_wchan && l->l_wmesg) ?
200 				    l->l_wmesg : ""));
201 				break;
202 
203 			case 'w':
204 				db_printf("%10s %8s %4d", p->p_comm,
205 				    p->p_emul->e_name,
206 				    (l != NULL) ? l->l_priority : -1);
207 				calcru(p, &tv[0], &tv[1], NULL);
208 				for (i = 0; i < 2; ++i) {
209 					db_printf("%4ld.%1ld",
210 					    (long)tv[i].tv_sec,
211 					    (long)tv[i].tv_usec/100000);
212 				}
213 				if (p->p_nlwps == 1) {
214 				if (l->l_wchan && l->l_wmesg) {
215 					db_printf(" %-12s", l->l_wmesg);
216 					db_printsym(
217 					    (db_expr_t)(intptr_t)l->l_wchan,
218 					    DB_STGY_XTRN, db_printf);
219 				} } else {
220 					db_printf(" * ");
221 				}
222 				db_printf("\n");
223 				break;
224 
225 			}
226 		}
227 	}
228 }
229 
230 void
231 db_dmesg(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
232 {
233 	struct kern_msgbuf *mbp;
234 	db_expr_t print;
235 	int ch, newl, skip, i;
236 	char *p, *bufdata;
237 
238         if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
239 		db_printf("message buffer not available\n");
240 		return;
241 	}
242 
243 	mbp = msgbufp;
244 	bufdata = &mbp->msg_bufc[0];
245 
246 	if (haddr && addr < mbp->msg_bufs)
247 		print = addr;
248 	else
249 		print = mbp->msg_bufs;
250 
251 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
252 	    i < mbp->msg_bufs; i++, p++) {
253 		if (p == bufdata + mbp->msg_bufs)
254 			p = bufdata;
255 		if (i < mbp->msg_bufs - print)
256 			continue;
257 		ch = *p;
258 		/* Skip "\n<.*>" syslog sequences. */
259 		if (skip) {
260 			if (ch == '>')
261 				newl = skip = 0;
262 			continue;
263 		}
264 		if (newl && ch == '<') {
265 			skip = 1;
266 			continue;
267 		}
268 		if (ch == '\0')
269 			continue;
270 		newl = ch == '\n';
271 		db_printf("%c", ch);
272 	}
273 	if (!newl)
274 		db_printf("\n");
275 }
276 
277 #ifdef __HAVE_BIGENDIAN_BITOPS
278 #define	RQMASK(n) (0x80000000 >> (n))
279 #else
280 #define	RQMASK(n) (0x00000001 << (n))
281 #endif
282 
283 void
284 db_show_sched_qs(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
285 {
286 	struct prochd *ph;
287 	struct lwp *l;
288 	int i, first;
289 
290 	for (i = 0; i < RUNQUE_NQS; i++)
291 	{
292 		first = 1;
293 		ph = &sched_qs[i];
294 		for (l = ph->ph_link; l != (struct lwp *)ph; l = l->l_forw) {
295 			if (first) {
296 				db_printf("%c%d",
297 				    (sched_whichqs & RQMASK(i))
298 				    ? ' ' : '!', i);
299 				first = 0;
300 			}
301 			db_printf("\t%d.%d (%s)\n", l->l_proc->p_pid,
302 			    l->l_lid, l->l_proc->p_comm);
303 		}
304 	}
305 }
306