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