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