db_proc.c revision 1.1 1 /* $NetBSD: db_proc.c,v 1.1 2009/03/07 22:02:17 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1989, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * from: kern_proc.c 8.4 (Berkeley) 1/4/94
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: db_proc.c,v 1.1 2009/03/07 22:02:17 ad Exp $");
65
66 #include <ddb/ddb.h>
67
68 #include <sys/param.h>
69 #include <sys/cpu.h>
70 #include <sys/proc.h>
71 #include <sys/kauth.h>
72
73 proc_t *
74 db_proc_first(void)
75 {
76
77 return db_read_ptr("allproc");
78 }
79
80 proc_t *
81 db_proc_next(proc_t *p)
82 {
83
84 db_read_bytes((db_addr_t)&p->p_list.le_next, sizeof(p), (char *)&p);
85 return p;
86 }
87
88 proc_t *
89 db_proc_find(pid_t pid)
90 {
91 proc_t *p;
92 pid_t tp;
93
94 for (p = db_proc_first(); p != NULL; p = db_proc_next(p)) {
95 db_read_bytes((db_addr_t)&p->p_pid, sizeof(tp),
96 (char *)&tp);
97 if (tp == pid) {
98 return p;
99 }
100 }
101 return NULL;
102 }
103
104 void
105 db_show_all_procs(db_expr_t addr, bool haddr, db_expr_t count,
106 const char *modif)
107 {
108 static struct pgrp pgrp;
109 static proc_t p;
110 static lwp_t l;
111 const char *mode, *ename;
112 proc_t *pp;
113 lwp_t *lp;
114 char db_nbuf[MAXCOMLEN + 1], wbuf[MAXCOMLEN + 1];
115 bool run;
116 int cpuno;
117
118 if (modif[0] == 0)
119 mode = "l"; /* default == lwp mode */
120 else
121 mode = strchr("mawln", modif[0]);
122
123 if (mode == NULL || *mode == 'm') {
124 db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
125 db_printf("\t/a == show process address info\n");
126 db_printf("\t/l == show LWP info\n");
127 db_printf("\t/n == show normal process info [default]\n");
128 db_printf("\t/w == show process wait/emul info\n");
129 return;
130 }
131
132 switch (*mode) {
133 case 'a':
134 db_printf("PID %10s %18s %18s %18s\n",
135 "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
136 break;
137 case 'l':
138 db_printf("PID %4s S %3s %9s %18s %18s %-8s\n",
139 "LID", "CPU", "FLAGS", "STRUCT LWP *", "NAME", "WAIT");
140 break;
141 case 'n':
142 db_printf("PID %8s %8s %10s S %7s %4s %16s %7s\n",
143 "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
144 break;
145 case 'w':
146 db_printf("PID %4s %16s %8s %4s %-12s%s\n",
147 "LID", "COMMAND", "EMUL", "PRI", "WAIT-MSG",
148 "WAIT-CHANNEL");
149 break;
150 }
151
152 for (pp = db_proc_first(); pp != NULL; pp = db_proc_next(pp)) {
153 db_read_bytes((db_addr_t)pp, sizeof(p), (char *)&p);
154 if (p.p_stat == 0) {
155 continue;
156 }
157 lp = p.p_lwps.lh_first;
158 if (lp != NULL) {
159 db_read_bytes((db_addr_t)lp, sizeof(l), (char *)&l);
160 }
161 db_printf("%-5d", p.p_pid);
162
163 switch (*mode) {
164 case 'a':
165 db_printf("%10.10s %18lx %18lx %18lx\n",
166 p.p_comm, (long)pp,
167 (long)(lp != NULL ? l.l_addr : 0),
168 (long)p.p_vmspace);
169 break;
170 case 'l':
171 while (lp != NULL) {
172 if (l.l_name != NULL) {
173 db_read_bytes((db_addr_t)l.l_name,
174 MAXCOMLEN, db_nbuf);
175 } else {
176 strlcpy(db_nbuf, p.p_comm,
177 sizeof(db_nbuf));
178 }
179 run = (l.l_stat == LSONPROC ||
180 (l.l_pflag & LP_RUNNING) != 0);
181 if (l.l_cpu != NULL) {
182 db_read_bytes((db_addr_t)
183 &l.l_cpu->ci_data.cpu_index,
184 sizeof(cpuno), (char *)&cpuno);
185 } else
186 cpuno = -1;
187 if (l.l_wchan && l.l_wmesg) {
188 db_read_bytes((db_addr_t)l.l_wmesg,
189 sizeof(wbuf), (char *)wbuf);
190 } else {
191 wbuf[0] = '\0';
192 }
193 db_printf("%c%4d %d %3d %9x %18lx %18s %-8s\n",
194 (run ? '>' : ' '), l.l_lid,
195 l.l_stat, cpuno, l.l_flag, (long)lp,
196 db_nbuf, wbuf);
197 lp = LIST_NEXT((&l), l_sibling);
198 if (lp != NULL) {
199 db_printf("%-5d", p.p_pid);
200 db_read_bytes((db_addr_t)lp, sizeof(l),
201 (char *)&l);
202 }
203 }
204 break;
205 case 'n':
206 db_read_bytes((db_addr_t)p.p_pgrp, sizeof(pgrp),
207 (char *)&pgrp);
208 if (lp != NULL && l.l_wchan && l.l_wmesg) {
209 db_read_bytes((db_addr_t)l.l_wmesg,
210 sizeof(wbuf), (char *)wbuf);
211 } else {
212 wbuf[0] = '\0';
213 }
214 db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
215 p.p_pptr != NULL ? p.p_pid : -1, pgrp.pg_id,
216 #ifdef _KERNEL
217 kauth_cred_getuid(p.p_cred),
218 #else
219 /* XXX CRASH(8) */ 666,
220 #endif
221 p.p_stat, p.p_flag,
222 p.p_nlwps, p.p_comm,
223 (p.p_nlwps != 1) ? "*" : wbuf);
224 break;
225
226 case 'w':
227 while (lp != NULL) {
228 if (l.l_wchan && l.l_wmesg) {
229 db_read_bytes((db_addr_t)l.l_wmesg,
230 sizeof(wbuf), (char *)wbuf);
231 } else {
232 wbuf[0] = '\0';
233 }
234 db_read_bytes((db_addr_t)&p.p_emul->e_name,
235 sizeof(ename), (char *)&ename);
236 db_read_bytes((db_addr_t)ename,
237 sizeof(db_nbuf), db_nbuf);
238 db_printf(
239 "%4d %16s %8s %4d %-12s %-18lx\n",
240 l.l_lid, p.p_comm, db_nbuf,
241 l.l_priority, wbuf, (long)l.l_wchan);
242 lp = LIST_NEXT((&l), l_sibling);
243 if (lp != NULL) {
244 db_printf("%-5d", p.p_pid);
245 db_read_bytes((db_addr_t)lp, sizeof(l),
246 (char *)&l);
247 }
248 }
249 break;
250 }
251 }
252 }
253
254