summaryrefslogtreecommitdiff
path: root/sys/ddb/db_proc.c
blob: 7eb942713d647d51499171d46f1456f1f6588175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*	$NetBSD: db_proc.c,v 1.14 2021/01/11 07:49:04 simonb Exp $	*/

/*-
 * Copyright (c) 2009, 2020 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Andrew Doran.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 1982, 1986, 1989, 1991, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from: kern_proc.c	8.4 (Berkeley) 1/4/94
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: db_proc.c,v 1.14 2021/01/11 07:49:04 simonb Exp $");

#ifndef _KERNEL
#include <stdbool.h>
#endif

#include <sys/param.h>
#include <sys/cpu.h>
#include <sys/proc.h>
#ifdef _KERNEL	/* XXX */
#include <sys/kauth.h>
#endif

#include <ddb/ddb.h>

proc_t *
db_proc_first(void)
{

	return db_read_ptr("allproc");
}

proc_t *
db_proc_next(proc_t *p)
{

	db_read_bytes((db_addr_t)&p->p_list.le_next, sizeof(p), (char *)&p);
	return p;
}

proc_t *
db_proc_find(pid_t pid)
{
	proc_t *p;
	pid_t tp;

	for (p = db_proc_first(); p != NULL; p = db_proc_next(p)) {
		db_read_bytes((db_addr_t)&p->p_pid, sizeof(tp),
		    (char *)&tp);
		if (tp == pid) {
			return p;
		}
	}
	return NULL;
}

static void
db_read_string(const char *src, size_t len, char *dst)
{
	size_t i;

	for (i = 0; i < len; i++) {
		db_read_bytes((db_addr_t)&src[i], 1, &dst[i]);
		if (dst[i] == '\0')
			break;
	}
}

void
db_show_all_procs(db_expr_t addr, bool haddr, db_expr_t count,
		  const char *modif)
{
	static struct pgrp pgrp;
	static proc_t p;
	static lwp_t l;
	const char *mode, *ename;
	proc_t *pp;
	lwp_t *lp;
	char db_nbuf[MAXCOMLEN + 1], wbuf[MAXCOMLEN + 1];
	bool run;
	int cpuno;

	if (modif[0] == 0)
		mode = "l";			/* default == lwp mode */
	else
		mode = strchr("mawln", modif[0]);

	if (mode == NULL || *mode == 'm') {
		db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
		db_printf("\t/a == show process address info\n");
		db_printf("\t/l == show LWP info [default]\n");
		db_printf("\t/n == show normal process info\n");
		db_printf("\t/w == show process wait/emul info\n");
		return;
	}

	switch (*mode) {
	case 'a':
		db_printf("PID   %-16s %18s %18s %18s\n",
		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
		break;
	case 'l':
		db_printf("PID   %4s S %3s %9s %18s %18s %-8s\n",
		    "LID", "CPU", "FLAGS", "STRUCT LWP *", "NAME", "WAIT");
		break;
	case 'n':
		db_printf("PID  %8s %8s %10s S %7s %4s %16s %7s\n",
		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
		break;
	case 'w':
		db_printf("PID  %4s %16s %8s %4s %-12s%s\n",
		    "LID", "COMMAND", "EMUL", "PRI", "WAIT-MSG",
		    "WAIT-CHANNEL");
		break;
	}

	for (pp = db_proc_first(); pp != NULL; pp = db_proc_next(pp)) {
		db_read_bytes((db_addr_t)pp, sizeof(p), (char *)&p);
		if (p.p_stat == 0) {
			continue;
		}
		lp = p.p_lwps.lh_first;
		if (lp != NULL) {
			db_read_bytes((db_addr_t)lp, sizeof(l), (char *)&l);
		}
		db_printf("%-5d", p.p_pid);

		switch (*mode) {
		case 'a':
			db_printf(" %-16.16s %18lx %18lx %18lx\n",
			    p.p_comm, (long)pp,
			    (long)(lp != NULL ? l.l_addr : 0),
			    (long)p.p_vmspace);
			break;
		case 'l':
			 while (lp != NULL) {
			 	if (l.l_name != NULL) {
					db_read_string(l.l_name,
					    MAXCOMLEN, db_nbuf);
					db_nbuf[MAXCOMLEN] = '\0';
				} else {
					strlcpy(db_nbuf, p.p_comm,
					    sizeof(db_nbuf));
				}
				run = (l.l_stat == LSONPROC ||
				    (l.l_pflag & LP_RUNNING) != 0);
				if (l.l_cpu != NULL) {
					db_read_bytes((db_addr_t)
					    &l.l_cpu->ci_data.cpu_index,
					    sizeof(cpuno), (char *)&cpuno);
				} else
					cpuno = -1;
				if (l.l_wchan && l.l_wmesg) {
					db_read_string(l.l_wmesg,
					    sizeof(wbuf), wbuf);
					wbuf[MAXCOMLEN] = '\0';
				} else {
					wbuf[0] = '\0';
				}
				db_printf("%c%4d %d %3d %9x %18lx %18s %-8s\n",
				    (run ? '>' : ' '), l.l_lid,
				    l.l_stat, cpuno, l.l_flag, (long)lp,
				    db_nbuf, wbuf);
				lp = LIST_NEXT((&l), l_sibling);
				if (lp != NULL) {
					db_printf("%-5d", p.p_pid);
					db_read_bytes((db_addr_t)lp, sizeof(l),
					    (char *)&l);
				}
			}
			break;
		case 'n':
			db_read_bytes((db_addr_t)p.p_pgrp, sizeof(pgrp),
			    (char *)&pgrp);
			if (lp != NULL && l.l_wchan && l.l_wmesg) {
				db_read_string(l.l_wmesg,
				    sizeof(wbuf), wbuf);
				wbuf[MAXCOMLEN] = '\0';
			} else {
				wbuf[0] = '\0';
			}
			db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
			    p.p_pptr != NULL ? p.p_pptr->p_pid : -1, pgrp.pg_id,
#ifdef _KERNEL
			    kauth_cred_getuid(p.p_cred),
#else
			    /* XXX CRASH(8) */ 666,
#endif
			    p.p_stat, p.p_flag,
			    p.p_nlwps, p.p_comm,
			    (p.p_nlwps != 1) ? "*" : wbuf);
			break;

		case 'w':
			 while (lp != NULL) {
				if (l.l_wchan && l.l_wmesg) {
					db_read_string(l.l_wmesg,
					    sizeof(wbuf), wbuf);
					wbuf[MAXCOMLEN] = '\0';
				} else {
					wbuf[0] = '\0';
				}
				run = (l.l_stat == LSONPROC ||
				    (l.l_pflag & LP_RUNNING) != 0);
				db_read_bytes((db_addr_t)&p.p_emul->e_name,
				    sizeof(ename), (char *)&ename);

				db_read_string(ename, sizeof(db_nbuf), db_nbuf);
				db_nbuf[MAXCOMLEN] = '\0';

				db_printf(
				    "%c%4d %16s %8s %4d %-12s %-18lx\n",
				    (run ? '>' : ' '), l.l_lid,
				    p.p_comm, db_nbuf,
				    l.l_priority, wbuf, (long)l.l_wchan);
				lp = LIST_NEXT((&l), l_sibling);
				if (lp != NULL) {
					db_printf("%-5d", p.p_pid);
					db_read_bytes((db_addr_t)lp, sizeof(l),
					    (char *)&l);
				}
			}
			break;
		}
	}
}

void
db_show_proc(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
{
	static proc_t p;
	static lwp_t l;
	const char *mode;
	proc_t *pp;
	lwp_t *lp;
	char db_nbuf[MAXCOMLEN + 1], wbuf[MAXCOMLEN + 1];
	bool run;
	int cpuno;

	if (modif[0] == 0)
		mode = "p";			/* default == by pid */
	else
		mode = strchr("ap", modif[0]);

	if (mode == NULL || !haddr) {
		db_printf("usage: show proc [/a] [/p] address|pid\n");
		db_printf("\t/a == argument is an address of any lwp\n");
		db_printf("\t/p == argument is a pid [default]\n");
		return;
	}

	switch (*mode) {
	case 'a':
		lp = (lwp_t *)(uintptr_t)addr;
		db_printf("lwp_t %lx\n", (long)lp);
		db_read_bytes((db_addr_t)lp, sizeof(l), (char *)&l);
		pp = l.l_proc;
		break;
	default:
	case 'p':
		pp = db_proc_find((pid_t)addr);
		lp = NULL;
		break;
	}

	if (pp == NULL) {
		db_printf("bad address\n");
		return;
	}

	db_read_bytes((db_addr_t)pp, sizeof(p), (char *)&p);
	if (lp == NULL)
		lp = p.p_lwps.lh_first;

	db_printf("%s: pid %d proc %lx vmspace/map %lx flags %x\n",
	    p.p_comm, p.p_pid, (long)pp, (long)p.p_vmspace, p.p_flag);

	while (lp != NULL) {
		db_read_bytes((db_addr_t)lp, sizeof(l), (char *)&l);

		run = (l.l_stat == LSONPROC ||
		    (l.l_pflag & LP_RUNNING) != 0);

		db_printf("%slwp %d", (run ? "> " : "  "), l.l_lid);
		if (l.l_name != NULL) {
			db_read_string(l.l_name, MAXCOMLEN, db_nbuf);
			db_nbuf[MAXCOMLEN] = '\0';
			db_printf(" [%s]", db_nbuf);
		}
		db_printf(" %lx pcb %lx\n", (long)lp, (long)l.l_addr);

		if (l.l_cpu != NULL) {
			db_read_bytes((db_addr_t)
			    &l.l_cpu->ci_data.cpu_index,
			    sizeof(cpuno), (char *)&cpuno);
		} else
			cpuno = -1;
		db_printf("    stat %d flags %x cpu %d pri %d ref %d\n",
		    l.l_stat, l.l_flag, cpuno, l.l_priority, l.l_refcnt);

		if (l.l_wchan && l.l_wmesg) {
			db_read_string(l.l_wmesg, MAXCOMLEN, wbuf);
			wbuf[MAXCOMLEN] = '\0';
			db_printf("    wmesg %s wchan %lx\n",
			    wbuf, (long)l.l_wchan);
		}

		lp = LIST_NEXT(&l, l_sibling);
	}
}