1 /* $OpenBSD: kvm_proc.c,v 1.8 2001/06/27 06:16:45 art Exp $ */ 2 /* $NetBSD: kvm_proc.c,v 1.30 1999/03/24 05:50:50 mrg Exp $ */ 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 /*- 39 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved. 40 * Copyright (c) 1989, 1992, 1993 41 * The Regents of the University of California. All rights reserved. 42 * 43 * This code is derived from software developed by the Computer Systems 44 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 45 * BG 91-66 and contributed to Berkeley. 46 * 47 * Redistribution and use in source and binary forms, with or without 48 * modification, are permitted provided that the following conditions 49 * are met: 50 * 1. Redistributions of source code must retain the above copyright 51 * notice, this list of conditions and the following disclaimer. 52 * 2. Redistributions in binary form must reproduce the above copyright 53 * notice, this list of conditions and the following disclaimer in the 54 * documentation and/or other materials provided with the distribution. 55 * 3. All advertising materials mentioning features or use of this software 56 * must display the following acknowledgement: 57 * This product includes software developed by the University of 58 * California, Berkeley and its contributors. 59 * 4. Neither the name of the University nor the names of its contributors 60 * may be used to endorse or promote products derived from this software 61 * without specific prior written permission. 62 * 63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 73 * SUCH DAMAGE. 74 */ 75 76 #if defined(LIBC_SCCS) && !defined(lint) 77 #if 0 78 static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"; 79 #else 80 static char *rcsid = "$OpenBSD: kvm_proc.c,v 1.8 2001/06/27 06:16:45 art Exp $"; 81 #endif 82 #endif /* LIBC_SCCS and not lint */ 83 84 /* 85 * Proc traversal interface for kvm. ps and w are (probably) the exclusive 86 * users of this code, so we've factored it out into a separate module. 87 * Thus, we keep this grunge out of the other kvm applications (i.e., 88 * most other applications are interested only in open/close/read/nlist). 89 */ 90 91 #include <sys/param.h> 92 #include <sys/user.h> 93 #include <sys/proc.h> 94 #include <sys/exec.h> 95 #include <sys/stat.h> 96 #include <sys/ioctl.h> 97 #include <sys/tty.h> 98 #include <stdlib.h> 99 #include <string.h> 100 #include <unistd.h> 101 #include <nlist.h> 102 #include <kvm.h> 103 104 #include <vm/vm.h> 105 #include <vm/vm_param.h> 106 107 #include <uvm/uvm_extern.h> 108 #include <uvm/uvm_amap.h> 109 110 #include <sys/sysctl.h> 111 112 #include <limits.h> 113 #include <db.h> 114 #include <paths.h> 115 116 #include "kvm_private.h" 117 118 #define KREAD(kd, addr, obj) \ 119 (kvm_read(kd, addr, (void *)(obj), sizeof(*obj)) != sizeof(*obj)) 120 121 ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *, 122 size_t)); 123 124 static char **kvm_argv __P((kvm_t *, const struct proc *, u_long, int, 125 int)); 126 static int kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, int)); 127 static char **kvm_doargv __P((kvm_t *, const struct kinfo_proc *, int, 128 void (*)(struct ps_strings *, u_long *, int *))); 129 static int kvm_proclist __P((kvm_t *, int, int, struct proc *, 130 struct kinfo_proc *, int)); 131 static int proc_verify __P((kvm_t *, u_long, const struct proc *)); 132 static void ps_str_a __P((struct ps_strings *, u_long *, int *)); 133 static void ps_str_e __P((struct ps_strings *, u_long *, int *)); 134 135 char * 136 _kvm_uread(kd, p, va, cnt) 137 kvm_t *kd; 138 const struct proc *p; 139 u_long va; 140 u_long *cnt; 141 { 142 u_long addr, head; 143 u_long offset; 144 struct vm_map_entry vme; 145 struct vm_amap amap; 146 struct vm_anon *anonp, anon; 147 struct vm_page pg; 148 u_long slot; 149 150 if (kd->swapspc == 0) { 151 kd->swapspc = (char *)_kvm_malloc(kd, kd->nbpg); 152 if (kd->swapspc == 0) 153 return (0); 154 } 155 156 /* 157 * Look through the address map for the memory object 158 * that corresponds to the given virtual address. 159 * The header just has the entire valid range. 160 */ 161 head = (u_long)&p->p_vmspace->vm_map.header; 162 addr = head; 163 while (1) { 164 if (KREAD(kd, addr, &vme)) 165 return (0); 166 167 if (va >= vme.start && va < vme.end && 168 vme.aref.ar_amap != NULL) 169 break; 170 171 addr = (u_long)vme.next; 172 if (addr == head) 173 return (0); 174 } 175 176 /* 177 * we found the map entry, now to find the object... 178 */ 179 if (vme.aref.ar_amap == NULL) 180 return NULL; 181 182 addr = (u_long)vme.aref.ar_amap; 183 if (KREAD(kd, addr, &amap)) 184 return NULL; 185 186 offset = va - vme.start; 187 slot = offset / kd->nbpg + vme.aref.ar_pageoff; 188 /* sanity-check slot number */ 189 if (slot > amap.am_nslot) 190 return NULL; 191 192 addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp); 193 if (KREAD(kd, addr, &anonp)) 194 return NULL; 195 196 addr = (u_long)anonp; 197 if (KREAD(kd, addr, &anon)) 198 return NULL; 199 200 addr = (u_long)anon.u.an_page; 201 if (addr) { 202 if (KREAD(kd, addr, &pg)) 203 return NULL; 204 205 if (_kvm_pread(kd, kd->pmfd, (void *)kd->swapspc, (size_t)kd->nbpg, (off_t)pg.phys_addr) != kd->nbpg) { 206 return NULL; 207 } 208 } else { 209 if (_kvm_pread(kd, kd->swfd, (void *)kd->swapspc, (size_t)kd->nbpg, (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg) { 210 return NULL; 211 } 212 } 213 214 /* Found the page. */ 215 offset %= kd->nbpg; 216 *cnt = kd->nbpg - offset; 217 return (&kd->swapspc[offset]); 218 } 219 220 /* 221 * Read proc's from memory file into buffer bp, which has space to hold 222 * at most maxcnt procs. 223 */ 224 static int 225 kvm_proclist(kd, what, arg, p, bp, maxcnt) 226 kvm_t *kd; 227 int what, arg; 228 struct proc *p; 229 struct kinfo_proc *bp; 230 int maxcnt; 231 { 232 int cnt = 0; 233 struct eproc eproc; 234 struct pgrp pgrp; 235 struct session sess; 236 struct tty tty; 237 struct proc proc; 238 239 for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) { 240 if (KREAD(kd, (u_long)p, &proc)) { 241 _kvm_err(kd, kd->program, "can't read proc at %x", p); 242 return (-1); 243 } 244 if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0) 245 KREAD(kd, (u_long)eproc.e_pcred.pc_ucred, 246 &eproc.e_ucred); 247 248 switch(what) { 249 250 case KERN_PROC_PID: 251 if (proc.p_pid != (pid_t)arg) 252 continue; 253 break; 254 255 case KERN_PROC_UID: 256 if (eproc.e_ucred.cr_uid != (uid_t)arg) 257 continue; 258 break; 259 260 case KERN_PROC_RUID: 261 if (eproc.e_pcred.p_ruid != (uid_t)arg) 262 continue; 263 break; 264 265 case KERN_PROC_ALL: 266 if (proc.p_flag & P_SYSTEM) 267 continue; 268 break; 269 } 270 /* 271 * We're going to add another proc to the set. If this 272 * will overflow the buffer, assume the reason is because 273 * nprocs (or the proc list) is corrupt and declare an error. 274 */ 275 if (cnt >= maxcnt) { 276 _kvm_err(kd, kd->program, "nprocs corrupt"); 277 return (-1); 278 } 279 /* 280 * gather eproc 281 */ 282 eproc.e_paddr = p; 283 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 284 _kvm_err(kd, kd->program, "can't read pgrp at %x", 285 proc.p_pgrp); 286 return (-1); 287 } 288 eproc.e_sess = pgrp.pg_session; 289 eproc.e_pgid = pgrp.pg_id; 290 eproc.e_jobc = pgrp.pg_jobc; 291 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 292 _kvm_err(kd, kd->program, "can't read session at %x", 293 pgrp.pg_session); 294 return (-1); 295 } 296 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 297 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 298 _kvm_err(kd, kd->program, 299 "can't read tty at %x", sess.s_ttyp); 300 return (-1); 301 } 302 eproc.e_tdev = tty.t_dev; 303 eproc.e_tsess = tty.t_session; 304 if (tty.t_pgrp != NULL) { 305 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 306 _kvm_err(kd, kd->program, 307 "can't read tpgrp at &x", 308 tty.t_pgrp); 309 return (-1); 310 } 311 eproc.e_tpgid = pgrp.pg_id; 312 } else 313 eproc.e_tpgid = -1; 314 } else 315 eproc.e_tdev = NODEV; 316 eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0; 317 if (sess.s_leader == p) 318 eproc.e_flag |= EPROC_SLEADER; 319 if (proc.p_wmesg) 320 (void)kvm_read(kd, (u_long)proc.p_wmesg, 321 eproc.e_wmesg, WMESGLEN); 322 323 (void)kvm_read(kd, (u_long)proc.p_vmspace, 324 &eproc.e_vm, sizeof(eproc.e_vm)); 325 326 eproc.e_xsize = eproc.e_xrssize = 0; 327 eproc.e_xccount = eproc.e_xswrss = 0; 328 329 switch (what) { 330 331 case KERN_PROC_PGRP: 332 if (eproc.e_pgid != (pid_t)arg) 333 continue; 334 break; 335 336 case KERN_PROC_TTY: 337 if ((proc.p_flag & P_CONTROLT) == 0 || 338 eproc.e_tdev != (dev_t)arg) 339 continue; 340 break; 341 } 342 bcopy(&proc, &bp->kp_proc, sizeof(proc)); 343 bcopy(&eproc, &bp->kp_eproc, sizeof(eproc)); 344 ++bp; 345 ++cnt; 346 } 347 return (cnt); 348 } 349 350 /* 351 * Build proc info array by reading in proc list from a crash dump. 352 * Return number of procs read. maxcnt is the max we will read. 353 */ 354 static int 355 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt) 356 kvm_t *kd; 357 int what, arg; 358 u_long a_allproc; 359 u_long a_zombproc; 360 int maxcnt; 361 { 362 struct kinfo_proc *bp = kd->procbase; 363 int acnt, zcnt; 364 struct proc *p; 365 366 if (KREAD(kd, a_allproc, &p)) { 367 _kvm_err(kd, kd->program, "cannot read allproc"); 368 return (-1); 369 } 370 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 371 if (acnt < 0) 372 return (acnt); 373 374 if (KREAD(kd, a_zombproc, &p)) { 375 _kvm_err(kd, kd->program, "cannot read zombproc"); 376 return (-1); 377 } 378 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 379 if (zcnt < 0) 380 zcnt = 0; 381 382 return (acnt + zcnt); 383 } 384 385 struct kinfo_proc * 386 kvm_getprocs(kd, op, arg, cnt) 387 kvm_t *kd; 388 int op, arg; 389 int *cnt; 390 { 391 size_t size; 392 int mib[4], st, nprocs; 393 394 if (kd->procbase != 0) { 395 free((void *)kd->procbase); 396 /* 397 * Clear this pointer in case this call fails. Otherwise, 398 * kvm_close() will free it again. 399 */ 400 kd->procbase = 0; 401 } 402 if (ISALIVE(kd)) { 403 size = 0; 404 mib[0] = CTL_KERN; 405 mib[1] = KERN_PROC; 406 mib[2] = op; 407 mib[3] = arg; 408 st = sysctl(mib, 4, NULL, &size, NULL, 0); 409 if (st == -1) { 410 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 411 return (0); 412 } 413 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 414 if (kd->procbase == 0) 415 return (0); 416 st = sysctl(mib, 4, kd->procbase, &size, NULL, 0); 417 if (st == -1) { 418 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 419 return (0); 420 } 421 if (size % sizeof(struct kinfo_proc) != 0) { 422 _kvm_err(kd, kd->program, 423 "proc size mismatch (%d total, %d chunks)", 424 size, sizeof(struct kinfo_proc)); 425 return (0); 426 } 427 nprocs = size / sizeof(struct kinfo_proc); 428 } else { 429 struct nlist nl[4], *p; 430 431 nl[0].n_name = "_nprocs"; 432 nl[1].n_name = "_allproc"; 433 nl[2].n_name = "_zombproc"; 434 nl[3].n_name = 0; 435 436 if (kvm_nlist(kd, nl) != 0) { 437 for (p = nl; p->n_type != 0; ++p) 438 ; 439 _kvm_err(kd, kd->program, 440 "%s: no such symbol", p->n_name); 441 return (0); 442 } 443 if (KREAD(kd, nl[0].n_value, &nprocs)) { 444 _kvm_err(kd, kd->program, "can't read nprocs"); 445 return (0); 446 } 447 size = nprocs * sizeof(struct kinfo_proc); 448 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 449 if (kd->procbase == 0) 450 return (0); 451 452 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 453 nl[2].n_value, nprocs); 454 #ifdef notdef 455 size = nprocs * sizeof(struct kinfo_proc); 456 (void)realloc(kd->procbase, size); 457 #endif 458 } 459 *cnt = nprocs; 460 return (kd->procbase); 461 } 462 463 void 464 _kvm_freeprocs(kd) 465 kvm_t *kd; 466 { 467 if (kd->procbase) { 468 free(kd->procbase); 469 kd->procbase = 0; 470 } 471 } 472 473 void * 474 _kvm_realloc(kd, p, n) 475 kvm_t *kd; 476 void *p; 477 size_t n; 478 { 479 void *np = (void *)realloc(p, n); 480 481 if (np == 0) 482 _kvm_err(kd, kd->program, "out of memory"); 483 return (np); 484 } 485 486 #ifndef MAX 487 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 488 #endif 489 490 /* 491 * Read in an argument vector from the user address space of process p. 492 * addr if the user-space base address of narg null-terminated contiguous 493 * strings. This is used to read in both the command arguments and 494 * environment strings. Read at most maxcnt characters of strings. 495 */ 496 static char ** 497 kvm_argv(kd, p, addr, narg, maxcnt) 498 kvm_t *kd; 499 const struct proc *p; 500 u_long addr; 501 int narg; 502 int maxcnt; 503 { 504 char *np, *cp, *ep, *ap; 505 u_long oaddr = -1; 506 int len, cc; 507 char **argv; 508 509 /* 510 * Check that there aren't an unreasonable number of agruments, 511 * and that the address is in user space. 512 */ 513 if (narg > ARG_MAX || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS) 514 return (0); 515 516 if (kd->argv == 0) { 517 /* 518 * Try to avoid reallocs. 519 */ 520 kd->argc = MAX(narg + 1, 32); 521 kd->argv = (char **)_kvm_malloc(kd, kd->argc * 522 sizeof(*kd->argv)); 523 if (kd->argv == 0) 524 return (0); 525 } else if (narg + 1 > kd->argc) { 526 kd->argc = MAX(2 * kd->argc, narg + 1); 527 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 528 sizeof(*kd->argv)); 529 if (kd->argv == 0) 530 return (0); 531 } 532 if (kd->argspc == 0) { 533 kd->argspc = (char *)_kvm_malloc(kd, kd->nbpg); 534 if (kd->argspc == 0) 535 return (0); 536 kd->arglen = kd->nbpg; 537 } 538 if (kd->argbuf == 0) { 539 kd->argbuf = (char *)_kvm_malloc(kd, kd->nbpg); 540 if (kd->argbuf == 0) 541 return (0); 542 } 543 cc = sizeof(char *) * narg; 544 if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc) 545 return (0); 546 ap = np = kd->argspc; 547 argv = kd->argv; 548 len = 0; 549 /* 550 * Loop over pages, filling in the argument vector. 551 */ 552 while (argv < kd->argv + narg && *argv != 0) { 553 addr = (u_long)*argv & ~(kd->nbpg - 1); 554 if (addr != oaddr) { 555 if (kvm_uread(kd, p, addr, kd->argbuf, kd->nbpg) != 556 kd->nbpg) 557 return (0); 558 oaddr = addr; 559 } 560 addr = (u_long)*argv & (kd->nbpg - 1); 561 cp = kd->argbuf + addr; 562 cc = kd->nbpg - addr; 563 if (maxcnt > 0 && cc > maxcnt - len) 564 cc = maxcnt - len;; 565 ep = memchr(cp, '\0', cc); 566 if (ep != 0) 567 cc = ep - cp + 1; 568 if (len + cc > kd->arglen) { 569 int off; 570 char **pp; 571 char *op = kd->argspc; 572 573 kd->arglen *= 2; 574 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 575 kd->arglen); 576 if (kd->argspc == 0) 577 return (0); 578 /* 579 * Adjust argv pointers in case realloc moved 580 * the string space. 581 */ 582 off = kd->argspc - op; 583 for (pp = kd->argv; pp < argv; pp++) 584 *pp += off; 585 ap += off; 586 np += off; 587 } 588 memcpy(np, cp, cc); 589 np += cc; 590 len += cc; 591 if (ep != 0) { 592 *argv++ = ap; 593 ap = np; 594 } else 595 *argv += cc; 596 if (maxcnt > 0 && len >= maxcnt) { 597 /* 598 * We're stopping prematurely. Terminate the 599 * current string. 600 */ 601 if (ep == 0) { 602 *np = '\0'; 603 *argv++ = ap; 604 } 605 break; 606 } 607 } 608 /* Make sure argv is terminated. */ 609 *argv = 0; 610 return (kd->argv); 611 } 612 613 static void 614 ps_str_a(p, addr, n) 615 struct ps_strings *p; 616 u_long *addr; 617 int *n; 618 { 619 *addr = (u_long)p->ps_argvstr; 620 *n = p->ps_nargvstr; 621 } 622 623 static void 624 ps_str_e(p, addr, n) 625 struct ps_strings *p; 626 u_long *addr; 627 int *n; 628 { 629 *addr = (u_long)p->ps_envstr; 630 *n = p->ps_nenvstr; 631 } 632 633 /* 634 * Determine if the proc indicated by p is still active. 635 * This test is not 100% foolproof in theory, but chances of 636 * being wrong are very low. 637 */ 638 static int 639 proc_verify(kd, kernp, p) 640 kvm_t *kd; 641 u_long kernp; 642 const struct proc *p; 643 { 644 struct proc kernproc; 645 646 /* 647 * Just read in the whole proc. It's not that big relative 648 * to the cost of the read system call. 649 */ 650 if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) != 651 sizeof(kernproc)) 652 return (0); 653 return (p->p_pid == kernproc.p_pid && 654 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB)); 655 } 656 657 static char ** 658 kvm_doargv(kd, kp, nchr, info) 659 kvm_t *kd; 660 const struct kinfo_proc *kp; 661 int nchr; 662 void (*info)(struct ps_strings *, u_long *, int *); 663 { 664 const struct proc *p = &kp->kp_proc; 665 char **ap; 666 u_long addr; 667 int cnt; 668 struct ps_strings arginfo; 669 static struct ps_strings *ps; 670 671 if (ps == NULL) { 672 struct _ps_strings _ps; 673 int mib[2]; 674 size_t len; 675 676 mib[0] = CTL_VM; 677 mib[1] = VM_PSSTRINGS; 678 len = sizeof(_ps); 679 sysctl(mib, 2, &_ps, &len, NULL, 0); 680 ps = (struct ps_strings *)_ps.val; 681 } 682 683 /* 684 * Pointers are stored at the top of the user stack. 685 */ 686 if (p->p_stat == SZOMB || 687 kvm_uread(kd, p, (u_long)ps, (char *)&arginfo, 688 sizeof(arginfo)) != sizeof(arginfo)) 689 return (0); 690 691 (*info)(&arginfo, &addr, &cnt); 692 if (cnt == 0) 693 return (0); 694 ap = kvm_argv(kd, p, addr, cnt, nchr); 695 /* 696 * For live kernels, make sure this process didn't go away. 697 */ 698 if (ap != 0 && ISALIVE(kd) && 699 !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p)) 700 ap = 0; 701 return (ap); 702 } 703 704 /* 705 * Get the command args. This code is now machine independent. 706 */ 707 char ** 708 kvm_getargv(kd, kp, nchr) 709 kvm_t *kd; 710 const struct kinfo_proc *kp; 711 int nchr; 712 { 713 return (kvm_doargv(kd, kp, nchr, ps_str_a)); 714 } 715 716 char ** 717 kvm_getenvv(kd, kp, nchr) 718 kvm_t *kd; 719 const struct kinfo_proc *kp; 720 int nchr; 721 { 722 return (kvm_doargv(kd, kp, nchr, ps_str_e)); 723 } 724 725 /* 726 * Read from user space. The user context is given by p. 727 */ 728 ssize_t 729 kvm_uread(kd, p, uva, buf, len) 730 kvm_t *kd; 731 const struct proc *p; 732 u_long uva; 733 char *buf; 734 size_t len; 735 { 736 char *cp; 737 738 cp = buf; 739 while (len > 0) { 740 int cc; 741 char *dp; 742 u_long cnt; 743 744 dp = _kvm_uread(kd, p, uva, &cnt); 745 if (dp == 0) { 746 _kvm_err(kd, 0, "invalid address (%lx)", uva); 747 return (0); 748 } 749 cc = MIN(cnt, len); 750 bcopy(dp, cp, cc); 751 752 cp += cc; 753 uva += cc; 754 len -= cc; 755 } 756 return (ssize_t)(cp - buf); 757 } 758