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