1 /** $MirOS: src/lib/libkvm/kvm_proc.c,v 1.5 2007/08/08 06:44:01 tg Exp $ */
2 /* $OpenBSD: kvm_proc.c,v 1.26 2004/06/24 21:06:47 millert Exp $ */
3 /* $NetBSD: kvm_proc.c,v 1.30 1999/03/24 05:50:50 mrg Exp $ */
4 /*-
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Charles M. Hannum.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39 /*-
40 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
41 * Copyright (c) 1989, 1992, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * This code is derived from software developed by the Computer Systems
45 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
46 * BG 91-66 and contributed to Berkeley.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 */
72
73 #if defined(LIBC_SCCS) && !defined(lint)
74 #if 0
75 static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93";
76 #else
77 static char *rcsid = "$OpenBSD: kvm_proc.c,v 1.26 2004/06/24 21:06:47 millert Exp $";
78 #endif
79 #endif /* LIBC_SCCS and not lint */
80
81 /*
82 * Proc traversal interface for kvm. ps and w are (probably) the exclusive
83 * users of this code, so we've factored it out into a separate module.
84 * Thus, we keep this grunge out of the other kvm applications (i.e.,
85 * most other applications are interested only in open/close/read/nlist).
86 */
87
88 #include <sys/param.h>
89 #include <sys/user.h>
90 #include <sys/proc.h>
91 #include <sys/exec.h>
92 #include <sys/stat.h>
93 #include <sys/ioctl.h>
94 #include <sys/tty.h>
95 #include <stdlib.h>
96 #include <string.h>
97 #include <unistd.h>
98 #include <nlist.h>
99 #include <kvm.h>
100
101 #include <uvm/uvm_extern.h>
102 #include <uvm/uvm_amap.h>
103 #include <machine/vmparam.h>
104 #include <machine/pmap.h>
105
106 #include <sys/sysctl.h>
107
108 #include <limits.h>
109 #include <db.h>
110 #include <paths.h>
111
112 #include "kvm_private.h"
113
114 /*
115 * Common info from kinfo_proc and kinfo_proc2 used by helper routines.
116 */
117 struct miniproc {
118 struct vmspace *p_vmspace;
119 char p_stat;
120 struct proc *p_paddr;
121 pid_t p_pid;
122 };
123
124 /*
125 * Convert from struct proc and kinfo_proc{,2} to miniproc.
126 */
127 #define PTOMINI(kp, p) \
128 do { \
129 (p)->p_stat = (kp)->p_stat; \
130 (p)->p_pid = (kp)->p_pid; \
131 (p)->p_paddr = NULL; \
132 (p)->p_vmspace = (kp)->p_vmspace; \
133 } while (/*CONSTCOND*/0);
134
135 #define KPTOMINI(kp, p) \
136 do { \
137 (p)->p_stat = (kp)->kp_proc.p_stat; \
138 (p)->p_pid = (kp)->kp_proc.p_pid; \
139 (p)->p_paddr = (kp)->kp_eproc.e_paddr; \
140 (p)->p_vmspace = (kp)->kp_proc.p_vmspace; \
141 } while (/*CONSTCOND*/0);
142
143 #define KP2TOMINI(kp, p) \
144 do { \
145 (p)->p_stat = (kp)->p_stat; \
146 (p)->p_pid = (kp)->p_pid; \
147 (p)->p_paddr = (void *)(long)(kp)->p_paddr; \
148 (p)->p_vmspace = (void *)(long)(kp)->p_vmspace; \
149 } while (/*CONSTCOND*/0);
150
151
152 #define PTRTOINT64(foo) ((u_int64_t)(u_long)(foo))
153
154 #define KREAD(kd, addr, obj) \
155 (kvm_read(kd, addr, (void *)(obj), sizeof(*obj)) != sizeof(*obj))
156
157 ssize_t kvm_uread(kvm_t *, const struct proc *, u_long, char *, size_t);
158
159 static char *_kvm_ureadm(kvm_t *, const struct miniproc *, u_long, u_long *);
160 static ssize_t kvm_ureadm(kvm_t *, const struct miniproc *, u_long, char *, size_t);
161
162 static char **kvm_argv(kvm_t *, const struct miniproc *, u_long, int, int);
163
164 static int kvm_deadprocs(kvm_t *, int, int, u_long, u_long, int);
165 static char **kvm_doargv(kvm_t *, const struct miniproc *, int,
166 void (*)(struct ps_strings *, u_long *, int *));
167 static int kvm_proclist(kvm_t *, int, int, struct proc *,
168 struct kinfo_proc *, int);
169 static int proc_verify(kvm_t *, const struct miniproc *);
170 static void ps_str_a(struct ps_strings *, u_long *, int *);
171 static void ps_str_e(struct ps_strings *, u_long *, int *);
172
173 static char *
_kvm_ureadm(kvm_t * kd,const struct miniproc * p,u_long va,u_long * cnt)174 _kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long va, u_long *cnt)
175 {
176 u_long addr, head, offset, slot;
177 struct vm_anon *anonp, anon;
178 struct vm_map_entry vme;
179 struct vm_amap amap;
180 struct vm_page pg;
181
182 if (kd->swapspc == 0) {
183 kd->swapspc = (char *)_kvm_malloc(kd, kd->nbpg);
184 if (kd->swapspc == 0)
185 return (0);
186 }
187
188 /*
189 * Look through the address map for the memory object
190 * that corresponds to the given virtual address.
191 * The header just has the entire valid range.
192 */
193 head = (u_long)&p->p_vmspace->vm_map.header;
194 addr = head;
195 while (1) {
196 if (KREAD(kd, addr, &vme))
197 return (0);
198
199 if (va >= vme.start && va < vme.end &&
200 vme.aref.ar_amap != NULL)
201 break;
202
203 addr = (u_long)vme.next;
204 if (addr == head)
205 return (0);
206 }
207
208 /*
209 * we found the map entry, now to find the object...
210 */
211 if (vme.aref.ar_amap == NULL)
212 return (NULL);
213
214 addr = (u_long)vme.aref.ar_amap;
215 if (KREAD(kd, addr, &amap))
216 return (NULL);
217
218 offset = va - vme.start;
219 slot = offset / kd->nbpg + vme.aref.ar_pageoff;
220 /* sanity-check slot number */
221 if (slot > amap.am_nslot)
222 return (NULL);
223
224 addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
225 if (KREAD(kd, addr, &anonp))
226 return (NULL);
227
228 addr = (u_long)anonp;
229 if (KREAD(kd, addr, &anon))
230 return (NULL);
231
232 addr = (u_long)anon.u.an_page;
233 if (addr) {
234 if (KREAD(kd, addr, &pg))
235 return (NULL);
236
237 if (_kvm_pread(kd, kd->pmfd, (void *)kd->swapspc,
238 (size_t)kd->nbpg, (off_t)pg.phys_addr) != kd->nbpg)
239 return (NULL);
240 } else {
241 if (_kvm_pread(kd, kd->swfd, (void *)kd->swapspc,
242 (size_t)kd->nbpg,
243 (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
244 return (NULL);
245 }
246
247 /* Found the page. */
248 offset %= kd->nbpg;
249 *cnt = kd->nbpg - offset;
250 return (&kd->swapspc[offset]);
251 }
252
253 char *
_kvm_uread(kvm_t * kd,const struct proc * p,u_long va,u_long * cnt)254 _kvm_uread(kvm_t *kd, const struct proc *p, u_long va, u_long *cnt)
255 {
256 struct miniproc mp;
257
258 PTOMINI(p, &mp);
259 return (_kvm_ureadm(kd, &mp, va, cnt));
260 }
261
262 /*
263 * Read proc's from memory file into buffer bp, which has space to hold
264 * at most maxcnt procs.
265 */
266 static int
kvm_proclist(kvm_t * kd,int what,int arg,struct proc * p,struct kinfo_proc * bp,int maxcnt)267 kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
268 struct kinfo_proc *bp, int maxcnt)
269 {
270 struct session sess;
271 struct eproc eproc;
272 struct proc proc;
273 struct pgrp pgrp;
274 struct tty tty;
275 int cnt = 0;
276
277 for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
278 if (KREAD(kd, (u_long)p, &proc)) {
279 _kvm_err(kd, kd->program, "can't read proc at %p", p);
280 return (-1);
281 }
282 if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
283 KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
284 &eproc.e_ucred);
285
286 switch (what) {
287 case KERN_PROC_PID:
288 if (proc.p_pid != (pid_t)arg)
289 continue;
290 break;
291
292 case KERN_PROC_UID:
293 if (eproc.e_ucred.cr_uid != (uid_t)arg)
294 continue;
295 break;
296
297 case KERN_PROC_RUID:
298 if (eproc.e_pcred.p_ruid != (uid_t)arg)
299 continue;
300 break;
301
302 case KERN_PROC_ALL:
303 if (proc.p_flag & P_SYSTEM)
304 continue;
305 break;
306 }
307 /*
308 * We're going to add another proc to the set. If this
309 * will overflow the buffer, assume the reason is because
310 * nprocs (or the proc list) is corrupt and declare an error.
311 */
312 if (cnt >= maxcnt) {
313 _kvm_err(kd, kd->program, "nprocs corrupt");
314 return (-1);
315 }
316 /*
317 * gather eproc
318 */
319 eproc.e_paddr = p;
320 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
321 _kvm_err(kd, kd->program, "can't read pgrp at %p",
322 proc.p_pgrp);
323 return (-1);
324 }
325 eproc.e_sess = pgrp.pg_session;
326 eproc.e_pgid = pgrp.pg_id;
327 eproc.e_jobc = pgrp.pg_jobc;
328 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
329 _kvm_err(kd, kd->program, "can't read session at %p",
330 pgrp.pg_session);
331 return (-1);
332 }
333 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
334 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
335 _kvm_err(kd, kd->program,
336 "can't read tty at %p", sess.s_ttyp);
337 return (-1);
338 }
339 eproc.e_tdev = tty.t_dev;
340 eproc.e_tsess = tty.t_session;
341 if (tty.t_pgrp != NULL) {
342 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
343 _kvm_err(kd, kd->program,
344 "can't read tpgrp at %p",
345 tty.t_pgrp);
346 return (-1);
347 }
348 eproc.e_tpgid = pgrp.pg_id;
349 } else
350 eproc.e_tpgid = -1;
351 } else
352 eproc.e_tdev = NODEV;
353 eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
354 if (sess.s_leader == p)
355 eproc.e_flag |= EPROC_SLEADER;
356 if (proc.p_wmesg)
357 (void)kvm_read(kd, (u_long)proc.p_wmesg,
358 eproc.e_wmesg, WMESGLEN);
359
360 (void)kvm_read(kd, (u_long)proc.p_vmspace,
361 &eproc.e_vm, sizeof(eproc.e_vm));
362
363 eproc.e_xsize = eproc.e_xrssize = 0;
364 eproc.e_xccount = eproc.e_xswrss = 0;
365
366 switch (what) {
367 case KERN_PROC_PGRP:
368 if (eproc.e_pgid != (pid_t)arg)
369 continue;
370 break;
371
372 case KERN_PROC_TTY:
373 if ((proc.p_flag & P_CONTROLT) == 0 ||
374 eproc.e_tdev != (dev_t)arg)
375 continue;
376 break;
377 }
378 memmove(&bp->kp_proc, &proc, sizeof(proc));
379 memmove(&bp->kp_eproc, &eproc, sizeof(eproc));
380 ++bp;
381 ++cnt;
382 }
383 return (cnt);
384 }
385
386 /*
387 * Build proc info array by reading in proc list from a crash dump.
388 * Return number of procs read. maxcnt is the max we will read.
389 */
390 static int
kvm_deadprocs(kvm_t * kd,int what,int arg,u_long a_allproc,u_long a_zombproc,int maxcnt)391 kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
392 u_long a_zombproc, int maxcnt)
393 {
394 struct kinfo_proc *bp = kd->procbase;
395 struct proc *p;
396 int acnt, zcnt;
397
398 if (KREAD(kd, a_allproc, &p)) {
399 _kvm_err(kd, kd->program, "cannot read allproc");
400 return (-1);
401 }
402 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
403 if (acnt < 0)
404 return (acnt);
405
406 if (KREAD(kd, a_zombproc, &p)) {
407 _kvm_err(kd, kd->program, "cannot read zombproc");
408 return (-1);
409 }
410 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
411 if (zcnt < 0)
412 zcnt = 0;
413
414 return (acnt + zcnt);
415 }
416
417 struct kinfo_proc2 *
kvm_getproc2(kvm_t * kd,int op,int arg,size_t esize,int * cnt)418 kvm_getproc2(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
419 {
420 int mib[6], st, nprocs;
421 struct user user;
422 size_t size;
423
424 if (esize < 0)
425 return (NULL);
426
427 if (kd->procbase2 != NULL) {
428 free(kd->procbase2);
429 /*
430 * Clear this pointer in case this call fails. Otherwise,
431 * kvm_close() will free it again.
432 */
433 kd->procbase2 = 0;
434 }
435
436 if (ISALIVE(kd)) {
437 size = 0;
438 mib[0] = CTL_KERN;
439 mib[1] = KERN_PROC2;
440 mib[2] = op;
441 mib[3] = arg;
442 mib[4] = esize;
443 mib[5] = 0;
444 st = sysctl(mib, 6, NULL, &size, NULL, 0);
445 if (st == -1) {
446 _kvm_syserr(kd, kd->program, "kvm_getproc2");
447 return (NULL);
448 }
449
450 mib[5] = size / esize;
451 kd->procbase2 = (struct kinfo_proc2 *)_kvm_malloc(kd, size);
452 if (kd->procbase2 == 0)
453 return (NULL);
454 st = sysctl(mib, 6, kd->procbase2, &size, NULL, 0);
455 if (st == -1) {
456 _kvm_syserr(kd, kd->program, "kvm_getproc2");
457 return (NULL);
458 }
459 nprocs = size / esize;
460 } else {
461 struct kinfo_proc2 kp2, *kp2p;
462 struct kinfo_proc *kp;
463 char *kp2c;
464 int i;
465
466 kp = kvm_getprocs(kd, op, arg, &nprocs);
467 if (kp == NULL)
468 return (NULL);
469
470 kd->procbase2 = _kvm_malloc(kd, nprocs * esize);
471 kp2c = (char *)kd->procbase2;
472 kp2p = &kp2;
473 for (i = 0; i < nprocs; i++, kp++) {
474 memset(kp2p, 0, sizeof(kp2));
475 kp2p->p_forw = PTRTOINT64(kp->kp_proc.p_forw);
476 kp2p->p_back = PTRTOINT64(kp->kp_proc.p_back);
477 kp2p->p_paddr = PTRTOINT64(kp->kp_eproc.e_paddr);
478
479 kp2p->p_addr = PTRTOINT64(kp->kp_proc.p_addr);
480 kp2p->p_fd = PTRTOINT64(kp->kp_proc.p_fd);
481 kp2p->p_stats = PTRTOINT64(kp->kp_proc.p_stats);
482 kp2p->p_limit = PTRTOINT64(kp->kp_proc.p_limit);
483 kp2p->p_vmspace = PTRTOINT64(kp->kp_proc.p_vmspace);
484 kp2p->p_sigacts = PTRTOINT64(kp->kp_proc.p_sigacts);
485 kp2p->p_sess = PTRTOINT64(kp->kp_eproc.e_sess);
486 kp2p->p_tsess = 0;
487 kp2p->p_ru = PTRTOINT64(kp->kp_proc.p_ru);
488
489 kp2p->p_eflag = 0;
490 kp2p->p_exitsig = kp->kp_proc.p_exitsig;
491 kp2p->p_flag = kp->kp_proc.p_flag;
492
493 kp2p->p_pid = kp->kp_proc.p_pid;
494
495 kp2p->p_ppid = kp->kp_eproc.e_ppid;
496 #if 0
497 kp2p->p_sid = kp->kp_eproc.e_sid;
498 #else
499 kp2p->p_sid = -1; /* XXX */
500 #endif
501 kp2p->p__pgid = kp->kp_eproc.e_pgid;
502
503 kp2p->p_tpgid = -1;
504
505 kp2p->p_uid = kp->kp_eproc.e_ucred.cr_uid;
506 kp2p->p_ruid = kp->kp_eproc.e_pcred.p_ruid;
507 kp2p->p_gid = kp->kp_eproc.e_ucred.cr_gid;
508 kp2p->p_rgid = kp->kp_eproc.e_pcred.p_rgid;
509
510 memcpy(kp2p->p_groups, kp->kp_eproc.e_ucred.cr_groups,
511 MIN(sizeof(kp2p->p_groups),
512 sizeof(kp->kp_eproc.e_ucred.cr_groups)));
513 kp2p->p_ngroups = kp->kp_eproc.e_ucred.cr_ngroups;
514
515 kp2p->p_jobc = kp->kp_eproc.e_jobc;
516 kp2p->p_tdev = kp->kp_eproc.e_tdev;
517 kp2p->p_tpgid = kp->kp_eproc.e_tpgid;
518 kp2p->p_tsess = PTRTOINT64(kp->kp_eproc.e_tsess);
519
520 kp2p->p_estcpu = kp->kp_proc.p_estcpu;
521 kp2p->p_rtime_sec = kp->kp_proc.p_estcpu;
522 kp2p->p_rtime_usec = kp->kp_proc.p_estcpu;
523 kp2p->p_cpticks = kp->kp_proc.p_cpticks;
524 kp2p->p_pctcpu = kp->kp_proc.p_pctcpu;
525 kp2p->p_swtime = kp->kp_proc.p_swtime;
526 kp2p->p_slptime = kp->kp_proc.p_slptime;
527 kp2p->p_schedflags = 0;
528
529 kp2p->p_uticks = kp->kp_proc.p_uticks;
530 kp2p->p_sticks = kp->kp_proc.p_sticks;
531 kp2p->p_iticks = kp->kp_proc.p_iticks;
532
533 kp2p->p_tracep = PTRTOINT64(kp->kp_proc.p_tracep);
534 kp2p->p_traceflag = kp->kp_proc.p_traceflag;
535
536 kp2p->p_holdcnt = kp->kp_proc.p_holdcnt;
537
538 kp2p->p_siglist = kp->kp_proc.p_siglist;
539 kp2p->p_sigmask = kp->kp_proc.p_sigmask;
540 kp2p->p_sigignore = kp->kp_proc.p_sigignore;
541 kp2p->p_sigcatch = kp->kp_proc.p_sigcatch;
542
543 kp2p->p_stat = kp->kp_proc.p_stat;
544 kp2p->p_priority = kp->kp_proc.p_priority;
545 kp2p->p_usrpri = kp->kp_proc.p_usrpri;
546 kp2p->p_nice = kp->kp_proc.p_nice;
547
548 kp2p->p_xstat = kp->kp_proc.p_xstat;
549 kp2p->p_acflag = kp->kp_proc.p_acflag;
550
551 strncpy(kp2p->p_comm, kp->kp_proc.p_comm,
552 MIN(sizeof(kp2p->p_comm), sizeof(kp->kp_proc.p_comm)));
553
554 strncpy(kp2p->p_wmesg, kp->kp_eproc.e_wmesg,
555 sizeof(kp2p->p_wmesg));
556 kp2p->p_wchan = PTRTOINT64(kp->kp_proc.p_wchan);
557
558 strncpy(kp2p->p_login, kp->kp_eproc.e_login,
559 sizeof(kp2p->p_login));
560
561 kp2p->p_vm_rssize = kp->kp_eproc.e_xrssize;
562 kp2p->p_vm_tsize = kp->kp_eproc.e_vm.vm_tsize;
563 kp2p->p_vm_dsize = kp->kp_eproc.e_vm.vm_dsize;
564 kp2p->p_vm_ssize = kp->kp_eproc.e_vm.vm_ssize;
565
566 kp2p->p_eflag = kp->kp_eproc.e_flag;
567
568 if (P_ZOMBIE(&kp->kp_proc) || kp->kp_proc.p_addr == NULL ||
569 KREAD(kd, (u_long)kp->kp_proc.p_addr, &user)) {
570 kp2p->p_uvalid = 0;
571 } else {
572 kp2p->p_uvalid = 1;
573
574 kp2p->p_ustart_sec = user.u_stats.p_start.tv_sec;
575 kp2p->p_ustart_usec = user.u_stats.p_start.tv_usec;
576
577 kp2p->p_uutime_sec = user.u_stats.p_ru.ru_utime.tv_sec;
578 kp2p->p_uutime_usec = user.u_stats.p_ru.ru_utime.tv_usec;
579 kp2p->p_ustime_sec = user.u_stats.p_ru.ru_stime.tv_sec;
580 kp2p->p_ustime_usec = user.u_stats.p_ru.ru_stime.tv_usec;
581
582 kp2p->p_uru_maxrss = user.u_stats.p_ru.ru_maxrss;
583 kp2p->p_uru_ixrss = user.u_stats.p_ru.ru_ixrss;
584 kp2p->p_uru_idrss = user.u_stats.p_ru.ru_idrss;
585 kp2p->p_uru_isrss = user.u_stats.p_ru.ru_isrss;
586 kp2p->p_uru_minflt = user.u_stats.p_ru.ru_minflt;
587 kp2p->p_uru_majflt = user.u_stats.p_ru.ru_majflt;
588 kp2p->p_uru_nswap = user.u_stats.p_ru.ru_nswap;
589 kp2p->p_uru_inblock = user.u_stats.p_ru.ru_inblock;
590 kp2p->p_uru_oublock = user.u_stats.p_ru.ru_oublock;
591 kp2p->p_uru_msgsnd = user.u_stats.p_ru.ru_msgsnd;
592 kp2p->p_uru_msgrcv = user.u_stats.p_ru.ru_msgrcv;
593 kp2p->p_uru_nsignals = user.u_stats.p_ru.ru_nsignals;
594 kp2p->p_uru_nvcsw = user.u_stats.p_ru.ru_nvcsw;
595 kp2p->p_uru_nivcsw = user.u_stats.p_ru.ru_nivcsw;
596
597 kp2p->p_uctime_sec =
598 user.u_stats.p_cru.ru_utime.tv_sec +
599 user.u_stats.p_cru.ru_stime.tv_sec;
600 kp2p->p_uctime_usec =
601 user.u_stats.p_cru.ru_utime.tv_usec +
602 user.u_stats.p_cru.ru_stime.tv_usec;
603 }
604
605 memcpy(kp2c, &kp2, esize);
606 kp2c += esize;
607 }
608
609 free(kd->procbase);
610 }
611 *cnt = nprocs;
612 return (kd->procbase2);
613 }
614
615 struct kinfo_proc *
kvm_getprocs(kvm_t * kd,int op,int arg,int * cnt)616 kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
617 {
618 int mib[4], st, nprocs;
619 size_t size;
620
621 if (kd->procbase != 0) {
622 free((void *)kd->procbase);
623 /*
624 * Clear this pointer in case this call fails. Otherwise,
625 * kvm_close() will free it again.
626 */
627 kd->procbase = 0;
628 }
629 if (ISALIVE(kd)) {
630 size = 0;
631 mib[0] = CTL_KERN;
632 mib[1] = KERN_PROC;
633 mib[2] = op;
634 mib[3] = arg;
635 st = sysctl(mib, 4, NULL, &size, NULL, 0);
636 if (st == -1) {
637 _kvm_syserr(kd, kd->program, "kvm_getprocs");
638 return (0);
639 }
640 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
641 if (kd->procbase == 0)
642 return (0);
643 st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
644 if (st == -1) {
645 _kvm_syserr(kd, kd->program, "kvm_getprocs");
646 return (0);
647 }
648 if (size % sizeof(struct kinfo_proc) != 0) {
649 _kvm_err(kd, kd->program,
650 "proc size mismatch (%zu total, %zu chunks)",
651 size, sizeof(struct kinfo_proc));
652 return (0);
653 }
654 nprocs = size / sizeof(struct kinfo_proc);
655 } else {
656 struct nlist nl[4], *p;
657
658 memset(nl, 0, sizeof(nl));
659 nl[0].n_name = "_nprocs";
660 nl[1].n_name = "_allproc";
661 nl[2].n_name = "_zombproc";
662 nl[3].n_name = NULL;
663
664 if (kvm_nlist(kd, nl) != 0) {
665 for (p = nl; p->n_type != 0; ++p)
666 ;
667 _kvm_err(kd, kd->program,
668 "%s: no such symbol", p->n_name);
669 return (0);
670 }
671 if (KREAD(kd, nl[0].n_value, &nprocs)) {
672 _kvm_err(kd, kd->program, "can't read nprocs");
673 return (0);
674 }
675 size = nprocs * sizeof(struct kinfo_proc);
676 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
677 if (kd->procbase == 0)
678 return (0);
679
680 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
681 nl[2].n_value, nprocs);
682 #ifdef notdef
683 size = nprocs * sizeof(struct kinfo_proc);
684 (void)realloc(kd->procbase, size);
685 #endif
686 }
687 *cnt = nprocs;
688 return (kd->procbase);
689 }
690
691 void
_kvm_freeprocs(kvm_t * kd)692 _kvm_freeprocs(kvm_t *kd)
693 {
694 if (kd->procbase) {
695 free(kd->procbase);
696 kd->procbase = 0;
697 }
698 }
699
700 void *
_kvm_realloc(kvm_t * kd,void * p,size_t n)701 _kvm_realloc(kvm_t *kd, void *p, size_t n)
702 {
703 void *np = (void *)realloc(p, n);
704
705 if (np == 0)
706 _kvm_err(kd, kd->program, "out of memory");
707 return (np);
708 }
709
710 /*
711 * Read in an argument vector from the user address space of process p.
712 * addr if the user-space base address of narg null-terminated contiguous
713 * strings. This is used to read in both the command arguments and
714 * environment strings. Read at most maxcnt characters of strings.
715 */
716 static char **
kvm_argv(kvm_t * kd,const struct miniproc * p,u_long addr,int narg,int maxcnt)717 kvm_argv(kvm_t *kd, const struct miniproc *p, u_long addr, int narg,
718 int maxcnt)
719 {
720 char *np, *cp, *ep, *ap, **argv;
721 u_long oaddr = -1;
722 int len, cc;
723
724 /*
725 * Check that there aren't an unreasonable number of agruments,
726 * and that the address is in user space.
727 */
728 if (narg > ARG_MAX || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
729 return (0);
730
731 if (kd->argv == 0) {
732 /*
733 * Try to avoid reallocs.
734 */
735 kd->argc = MAX(narg + 1, 32);
736 kd->argv = (char **)_kvm_malloc(kd, kd->argc *
737 sizeof(*kd->argv));
738 if (kd->argv == 0)
739 return (0);
740 } else if (narg + 1 > kd->argc) {
741 kd->argc = MAX(2 * kd->argc, narg + 1);
742 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
743 sizeof(*kd->argv));
744 if (kd->argv == 0)
745 return (0);
746 }
747 if (kd->argspc == 0) {
748 kd->argspc = (char *)_kvm_malloc(kd, kd->nbpg);
749 if (kd->argspc == 0)
750 return (0);
751 kd->arglen = kd->nbpg;
752 }
753 if (kd->argbuf == 0) {
754 kd->argbuf = (char *)_kvm_malloc(kd, kd->nbpg);
755 if (kd->argbuf == 0)
756 return (0);
757 }
758 cc = sizeof(char *) * narg;
759 if (kvm_ureadm(kd, p, addr, (char *)kd->argv, cc) != cc)
760 return (0);
761 ap = np = kd->argspc;
762 argv = kd->argv;
763 len = 0;
764
765 /*
766 * Loop over pages, filling in the argument vector.
767 */
768 while (argv < kd->argv + narg && *argv != 0) {
769 addr = (u_long)*argv & ~(kd->nbpg - 1);
770 if (addr != oaddr) {
771 if (kvm_ureadm(kd, p, addr, kd->argbuf, kd->nbpg) !=
772 kd->nbpg)
773 return (0);
774 oaddr = addr;
775 }
776 addr = (u_long)*argv & (kd->nbpg - 1);
777 cp = kd->argbuf + addr;
778 cc = kd->nbpg - addr;
779 if (maxcnt > 0 && cc > maxcnt - len)
780 cc = maxcnt - len;
781 ep = memchr(cp, '\0', cc);
782 if (ep != 0)
783 cc = ep - cp + 1;
784 if (len + cc > kd->arglen) {
785 int off;
786 char **pp;
787 char *op = kd->argspc;
788
789 kd->arglen *= 2;
790 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
791 kd->arglen);
792 if (kd->argspc == 0)
793 return (0);
794 /*
795 * Adjust argv pointers in case realloc moved
796 * the string space.
797 */
798 off = kd->argspc - op;
799 for (pp = kd->argv; pp < argv; pp++)
800 *pp += off;
801 ap += off;
802 np += off;
803 }
804 memcpy(np, cp, cc);
805 np += cc;
806 len += cc;
807 if (ep != 0) {
808 *argv++ = ap;
809 ap = np;
810 } else
811 *argv += cc;
812 if (maxcnt > 0 && len >= maxcnt) {
813 /*
814 * We're stopping prematurely. Terminate the
815 * current string.
816 */
817 if (ep == 0) {
818 *np = '\0';
819 *argv++ = ap;
820 }
821 break;
822 }
823 }
824 /* Make sure argv is terminated. */
825 *argv = 0;
826 return (kd->argv);
827 }
828
829 static void
ps_str_a(struct ps_strings * p,u_long * addr,int * n)830 ps_str_a(struct ps_strings *p, u_long *addr, int *n)
831 {
832 *addr = (u_long)p->ps_argvstr;
833 *n = p->ps_nargvstr;
834 }
835
836 static void
ps_str_e(struct ps_strings * p,u_long * addr,int * n)837 ps_str_e(struct ps_strings *p, u_long *addr, int *n)
838 {
839 *addr = (u_long)p->ps_envstr;
840 *n = p->ps_nenvstr;
841 }
842
843 /*
844 * Determine if the proc indicated by p is still active.
845 * This test is not 100% foolproof in theory, but chances of
846 * being wrong are very low.
847 */
848 static int
proc_verify(kvm_t * kd,const struct miniproc * p)849 proc_verify(kvm_t *kd, const struct miniproc *p)
850 {
851 struct proc kernproc;
852
853 /*
854 * Just read in the whole proc. It's not that big relative
855 * to the cost of the read system call.
856 */
857 if (kvm_read(kd, (u_long)p->p_paddr, &kernproc, sizeof(kernproc)) !=
858 sizeof(kernproc))
859 return (0);
860 return (p->p_pid == kernproc.p_pid &&
861 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
862 }
863
864 static char **
kvm_doargv(kvm_t * kd,const struct miniproc * p,int nchr,void (* info)(struct ps_strings *,u_long *,int *))865 kvm_doargv(kvm_t *kd, const struct miniproc *p, int nchr,
866 void (*info)(struct ps_strings *, u_long *, int *))
867 {
868 static struct ps_strings *ps;
869 struct ps_strings arginfo;
870 u_long addr;
871 char **ap;
872 int cnt;
873
874 if (ps == NULL) {
875 struct _ps_strings _ps;
876 int mib[2];
877 size_t len;
878
879 mib[0] = CTL_VM;
880 mib[1] = VM_PSSTRINGS;
881 len = sizeof(_ps);
882 sysctl(mib, 2, &_ps, &len, NULL, 0);
883 ps = (struct ps_strings *)_ps.val;
884 }
885
886 /*
887 * Pointers are stored at the top of the user stack.
888 */
889 if (p->p_stat == SZOMB ||
890 kvm_ureadm(kd, p, (u_long)ps, (char *)&arginfo,
891 sizeof(arginfo)) != sizeof(arginfo))
892 return (0);
893
894 (*info)(&arginfo, &addr, &cnt);
895 if (cnt == 0)
896 return (0);
897 ap = kvm_argv(kd, p, addr, cnt, nchr);
898 /*
899 * For live kernels, make sure this process didn't go away.
900 */
901 if (ap != 0 && ISALIVE(kd) && !proc_verify(kd, p))
902 ap = 0;
903 return (ap);
904 }
905
906 static char **
kvm_arg_sysctl(kvm_t * kd,pid_t pid,int nchr,int env)907 kvm_arg_sysctl(kvm_t *kd, pid_t pid, int nchr, int env)
908 {
909 size_t len, orglen;
910 int mib[4], ret;
911 char *buf;
912
913 orglen = env ? kd->nbpg : 8 * kd->nbpg; /* XXX - should be ARG_MAX */
914 if (kd->argbuf == NULL &&
915 (kd->argbuf = _kvm_malloc(kd, orglen)) == NULL)
916 return (NULL);
917
918 again:
919 mib[0] = CTL_KERN;
920 mib[1] = KERN_PROC_ARGS;
921 mib[2] = (int)pid;
922 mib[3] = env ? KERN_PROC_ENV : KERN_PROC_ARGV;
923
924 len = orglen;
925 ret = (sysctl(mib, 4, kd->argbuf, &len, NULL, 0) < 0);
926 if (ret && errno == ENOMEM) {
927 orglen *= 2;
928 buf = _kvm_realloc(kd, kd->argbuf, orglen);
929 if (buf == NULL)
930 return (NULL);
931 kd->argbuf = buf;
932 goto again;
933 }
934
935 if (ret) {
936 free(kd->argbuf);
937 kd->argbuf = NULL;
938 _kvm_syserr(kd, kd->program, "kvm_arg_sysctl");
939 return (NULL);
940 }
941 #if 0
942 for (argv = (char **)kd->argbuf; *argv != NULL; argv++)
943 if (strlen(*argv) > nchr)
944 *argv[nchr] = '\0';
945 #endif
946
947 return (char **)(kd->argbuf);
948 }
949
950 /*
951 * Get the command args. This code is now machine independent.
952 */
953 char **
kvm_getargv(kvm_t * kd,const struct kinfo_proc * kp,int nchr)954 kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
955 {
956 struct miniproc p;
957
958 if (ISALIVE(kd))
959 return (kvm_arg_sysctl(kd, kp->kp_proc.p_pid, nchr, 0));
960 KPTOMINI(kp, &p);
961 return (kvm_doargv(kd, &p, nchr, ps_str_a));
962 }
963
964 char **
kvm_getenvv(kvm_t * kd,const struct kinfo_proc * kp,int nchr)965 kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
966 {
967 struct miniproc p;
968
969 if (ISALIVE(kd))
970 return (kvm_arg_sysctl(kd, kp->kp_proc.p_pid, nchr, 1));
971 KPTOMINI(kp, &p);
972 return (kvm_doargv(kd, &p, nchr, ps_str_e));
973 }
974
975 char **
kvm_getargv2(kvm_t * kd,const struct kinfo_proc2 * kp,int nchr)976 kvm_getargv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr)
977 {
978 struct miniproc p;
979
980 if (ISALIVE(kd))
981 return (kvm_arg_sysctl(kd, kp->p_pid, nchr, 0));
982 KP2TOMINI(kp, &p);
983 return (kvm_doargv(kd, &p, nchr, ps_str_a));
984 }
985
986 char **
kvm_getenvv2(kvm_t * kd,const struct kinfo_proc2 * kp,int nchr)987 kvm_getenvv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr)
988 {
989 struct miniproc p;
990
991 if (ISALIVE(kd))
992 return (kvm_arg_sysctl(kd, kp->p_pid, nchr, 1));
993 KP2TOMINI(kp, &p);
994 return (kvm_doargv(kd, &p, nchr, ps_str_e));
995 }
996
997 /*
998 * Read from user space. The user context is given by p.
999 */
1000 static ssize_t
kvm_ureadm(kvm_t * kd,const struct miniproc * p,u_long uva,char * buf,size_t len)1001 kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long uva, char *buf,
1002 size_t len)
1003 {
1004 char *cp = buf;
1005
1006 while (len > 0) {
1007 u_long cnt;
1008 size_t cc;
1009 char *dp;
1010
1011 dp = _kvm_ureadm(kd, p, uva, &cnt);
1012 if (dp == 0) {
1013 _kvm_err(kd, 0, "invalid address (%lx)", uva);
1014 return (0);
1015 }
1016 cc = (size_t)MIN(cnt, len);
1017 memmove(cp, dp, cc);
1018
1019 cp += cc;
1020 uva += cc;
1021 len -= cc;
1022 }
1023 return (ssize_t)(cp - buf);
1024 }
1025
1026 ssize_t
kvm_uread(kvm_t * kd,const struct proc * p,u_long uva,char * buf,size_t len)1027 kvm_uread(kvm_t *kd, const struct proc *p, u_long uva, char *buf,
1028 size_t len)
1029 {
1030 struct miniproc mp;
1031
1032 PTOMINI(p, &mp);
1033 return (kvm_ureadm(kd, &mp, uva, buf, len));
1034 }
1035