db_xxx.c revision 1.23 1 /* $NetBSD: db_xxx.c,v 1.23 2003/06/28 14:21:29 darrenr 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.23 2003/06/28 14:21:29 darrenr 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 = proc_representative_lwp(p);
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 l = LIST_FIRST(&p->p_lwps);
184 while (l != NULL) {
185 db_printf("%c%4d %d %#7x %18p %18p %s\n",
186 " >"[cl == l], l->l_lid,
187 l->l_stat, l->l_flag, l,
188 l->l_addr,
189 (l->l_wchan && l->l_wmesg) ?
190 l->l_wmesg : "");
191
192 l = LIST_NEXT(l, l_sibling);
193 if (l)
194 db_printf("%11s","");
195 }
196 break;
197 case 'n':
198 db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
199 pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
200 p->p_cred->p_ruid, p->p_stat, p->p_flag,
201 p->p_nlwps, p->p_comm,
202 (p->p_nlwps != 1) ? "*" : (
203 (l->l_wchan && l->l_wmesg) ?
204 l->l_wmesg : ""));
205 break;
206
207 case 'w':
208 db_printf("%10s %8s %4d", p->p_comm,
209 p->p_emul->e_name,
210 (l != NULL) ? l->l_priority : -1);
211 calcru(p, &tv[0], &tv[1], NULL);
212 for (i = 0; i < 2; ++i) {
213 db_printf("%4ld.%1ld",
214 (long)tv[i].tv_sec,
215 (long)tv[i].tv_usec/100000);
216 }
217 if (p->p_nlwps == 1) {
218 if (l->l_wchan && l->l_wmesg) {
219 db_printf(" %-12s", l->l_wmesg);
220 db_printsym(
221 (db_expr_t)(intptr_t)l->l_wchan,
222 DB_STGY_XTRN, db_printf);
223 } } else {
224 db_printf(" * ");
225 }
226 db_printf("\n");
227 break;
228
229 }
230 }
231 }
232 }
233
234 void
235 db_dmesg(db_expr_t addr, int haddr, db_expr_t count, char *modif)
236 {
237 struct kern_msgbuf *mbp;
238 int ch, newl, skip, i;
239 char *p, *bufdata;
240
241 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
242 db_printf("message buffer not available\n");
243 return;
244 }
245
246 mbp = msgbufp;
247 bufdata = &mbp->msg_bufc[0];
248
249 for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
250 i < mbp->msg_bufs; i++, p++) {
251 if (p == bufdata + mbp->msg_bufs)
252 p = bufdata;
253 ch = *p;
254 /* Skip "\n<.*>" syslog sequences. */
255 if (skip) {
256 if (ch == '>')
257 newl = skip = 0;
258 continue;
259 }
260 if (newl && ch == '<') {
261 skip = 1;
262 continue;
263 }
264 if (ch == '\0')
265 continue;
266 newl = ch == '\n';
267 db_printf("%c", ch);
268 }
269 if (!newl)
270 db_printf("\n");
271 }
272