db_xxx.c revision 1.43 1 /* $NetBSD: db_xxx.c,v 1.43 2007/04/30 14:44:30 rmind 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.43 2007/04/30 14:44:30 rmind 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 #include <sys/pool.h>
54 #include <sys/kauth.h>
55
56 #include <machine/db_machdep.h>
57
58 #include <ddb/db_access.h>
59 #include <ddb/db_command.h>
60 #include <ddb/db_interface.h>
61 #include <ddb/db_lex.h>
62 #include <ddb/db_output.h>
63 #include <ddb/db_sym.h>
64 #include <ddb/db_extern.h>
65 #ifdef KGDB
66 #include <sys/kgdb.h>
67 #endif
68
69 void
70 db_kill_proc(db_expr_t addr, bool haddr,
71 db_expr_t count, const char *modif)
72 {
73 struct proc *p;
74 db_expr_t pid, sig;
75 int t;
76
77 /* What pid? */
78 if (!db_expression(&pid)) {
79 db_error("pid?\n");
80 /*NOTREACHED*/
81 }
82 /* What sig? */
83 t = db_read_token();
84 if (t == tCOMMA) {
85 if (!db_expression(&sig)) {
86 db_error("sig?\n");
87 /*NOTREACHED*/
88 }
89 } else {
90 db_unread_token(t);
91 sig = 15;
92 }
93 if (db_read_token() != tEOL) {
94 db_error("?\n");
95 /*NOTREACHED*/
96 }
97
98 p = pfind((pid_t)pid);
99 if (p == NULL) {
100 db_error("no such proc\n");
101 /*NOTREACHED*/
102 }
103 psignal(p, (int)sig);
104 }
105
106 #ifdef KGDB
107 void
108 db_kgdb_cmd(db_expr_t addr, bool haddr,
109 db_expr_t count, const char *modif)
110 {
111 kgdb_active++;
112 kgdb_trap(db_trap_type, DDB_REGS);
113 kgdb_active--;
114 }
115 #endif
116
117 void
118 db_show_aio_jobs(db_expr_t addr, bool haddr,
119 db_expr_t count, const char *modif)
120 {
121 aio_print_jobs(db_printf);
122 }
123
124 void
125 db_show_all_procs(db_expr_t addr, bool haddr,
126 db_expr_t count, const char *modif)
127 {
128 int i;
129
130 const char *mode;
131 struct proc *p, *pp, *cp;
132 struct lwp *l, *cl;
133 struct timeval tv[2];
134 const struct proclist_desc *pd;
135
136 if (modif[0] == 0)
137 mode = "n"; /* default == normal mode */
138 else
139 mode = strchr("mawln", modif[0]);
140
141 if (mode == NULL || *mode == 'm') {
142 db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
143 db_printf("\t/a == show process address info\n");
144 db_printf("\t/l == show LWP info\n");
145 db_printf("\t/n == show normal process info [default]\n");
146 db_printf("\t/w == show process wait/emul info\n");
147 return;
148 }
149
150 switch (*mode) {
151 case 'a':
152 db_printf(" PID %10s %18s %18s %18s\n",
153 "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
154 break;
155 case 'l':
156 db_printf(" PID %4s S %9s %18s %18s %-12s\n",
157 "LID", "FLAGS", "STRUCT LWP *", "UAREA *", "WAIT");
158 break;
159 case 'n':
160 db_printf(" PID %8s %8s %10s S %7s %4s %16s %7s\n",
161 "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
162 break;
163 case 'w':
164 db_printf(" PID %10s %8s %4s %5s %5s %-12s%s\n",
165 "COMMAND", "EMUL", "PRI", "UTIME", "STIME",
166 "WAIT-MSG", "WAIT-CHANNEL");
167 break;
168 }
169
170 /* XXX LOCKING XXX */
171 pd = proclists;
172 cp = curproc;
173 cl = curlwp;
174 for (pd = proclists; pd->pd_list != NULL; pd++) {
175 LIST_FOREACH(p, pd->pd_list, p_list) {
176 pp = p->p_pptr;
177 if (p->p_stat == 0) {
178 continue;
179 }
180 l = LIST_FIRST(&p->p_lwps);
181 db_printf("%c%-10d", (cp == p ? '>' : ' '), p->p_pid);
182
183 switch (*mode) {
184
185 case 'a':
186 db_printf("%10.10s %18p %18p %18p\n",
187 p->p_comm, p,
188 l != NULL ? l->l_addr : 0,
189 p->p_vmspace);
190 break;
191 case 'l':
192 while (l != NULL) {
193 db_printf("%c%4d %d %#9x %18p %18p %s\n",
194 (cl == l ? '>' : ' '), l->l_lid,
195 l->l_stat, l->l_flag, l,
196 l->l_addr,
197 (l->l_wchan && l->l_wmesg) ?
198 l->l_wmesg : "");
199
200 l = LIST_NEXT(l, l_sibling);
201 if (l)
202 db_printf("%11s","");
203 }
204 break;
205 case 'n':
206 db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
207 pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
208 kauth_cred_getuid(p->p_cred), p->p_stat, p->p_flag,
209 p->p_nlwps, p->p_comm,
210 (p->p_nlwps != 1) ? "*" : (
211 (l->l_wchan && l->l_wmesg) ?
212 l->l_wmesg : ""));
213 break;
214
215 case 'w':
216 db_printf("%10s %8s %4d", p->p_comm,
217 p->p_emul->e_name,
218 (l != NULL) ? l->l_priority : -1);
219 calcru(p, &tv[0], &tv[1], NULL, NULL);
220 for (i = 0; i < 2; ++i) {
221 db_printf("%4ld.%1ld",
222 (long)tv[i].tv_sec,
223 (long)tv[i].tv_usec/100000);
224 }
225 if (p->p_nlwps == 1) {
226 if (l->l_wchan && l->l_wmesg) {
227 db_printf(" %-12s", l->l_wmesg);
228 db_printsym(
229 (db_expr_t)(intptr_t)l->l_wchan,
230 DB_STGY_XTRN, db_printf);
231 } } else {
232 db_printf(" * ");
233 }
234 db_printf("\n");
235 break;
236
237 }
238 }
239 }
240 }
241
242 void
243 db_show_all_pools(db_expr_t addr, bool haddr,
244 db_expr_t count, const char *modif)
245 {
246
247 pool_printall(modif, db_printf);
248 }
249
250 void
251 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count,
252 const char *modif)
253 {
254 struct kern_msgbuf *mbp;
255 db_expr_t print;
256 int ch, newl, skip, i;
257 char *p, *bufdata;
258
259 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
260 db_printf("message buffer not available\n");
261 return;
262 }
263
264 mbp = msgbufp;
265 bufdata = &mbp->msg_bufc[0];
266
267 if (haddr && addr < mbp->msg_bufs)
268 print = addr;
269 else
270 print = mbp->msg_bufs;
271
272 for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
273 i < mbp->msg_bufs; i++, p++) {
274 if (p == bufdata + mbp->msg_bufs)
275 p = bufdata;
276 if (i < mbp->msg_bufs - print)
277 continue;
278 ch = *p;
279 /* Skip "\n<.*>" syslog sequences. */
280 if (skip) {
281 if (ch == '>')
282 newl = skip = 0;
283 continue;
284 }
285 if (newl && ch == '<') {
286 skip = 1;
287 continue;
288 }
289 if (ch == '\0')
290 continue;
291 newl = ch == '\n';
292 db_printf("%c", ch);
293 }
294 if (!newl)
295 db_printf("\n");
296 }
297
298 #ifdef __HAVE_BIGENDIAN_BITOPS
299 #define RQMASK(n) (0x80000000 >> (n))
300 #else
301 #define RQMASK(n) (0x00000001 << (n))
302 #endif
303
304 void
305 db_show_sched_qs(db_expr_t addr, bool haddr,
306 db_expr_t count, const char *modif)
307 {
308 struct prochd *ph;
309 struct lwp *l;
310 int i, first;
311
312 for (i = 0; i < RUNQUE_NQS; i++)
313 {
314 first = 1;
315 ph = &sched_qs[i];
316 for (l = ph->ph_link; l != (void *)ph; l = l->l_forw) {
317 if (first) {
318 db_printf("%c%d",
319 (sched_whichqs & RQMASK(i))
320 ? ' ' : '!', i);
321 first = 0;
322 }
323 db_printf("\t%d.%d (%s) pri=%d usrpri=%d\n",
324 l->l_proc->p_pid,
325 l->l_lid, l->l_proc->p_comm,
326 (int)l->l_priority, (int)l->l_usrpri);
327 }
328 }
329 }
330