1 /* $OpenBSD: kvm_proc.c,v 1.14 2002/02/17 19:42:25 millert 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.14 2002/02/17 19:42:25 millert 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 <uvm/uvm_extern.h> 105 #include <uvm/uvm_amap.h> 106 #include <machine/vmparam.h> 107 #include <machine/pmap.h> 108 109 #include <sys/sysctl.h> 110 111 #include <limits.h> 112 #include <db.h> 113 #include <paths.h> 114 115 #include "kvm_private.h" 116 117 #define KREAD(kd, addr, obj) \ 118 (kvm_read(kd, addr, (void *)(obj), sizeof(*obj)) != sizeof(*obj)) 119 120 ssize_t kvm_uread(kvm_t *, const struct proc *, u_long, char *, size_t); 121 122 static char **kvm_argv(kvm_t *, const struct proc *, u_long, int, int); 123 static int kvm_deadprocs(kvm_t *, int, int, u_long, u_long, int); 124 static char **kvm_doargv(kvm_t *, const struct kinfo_proc *, int, 125 void (*)(struct ps_strings *, u_long *, int *)); 126 static int kvm_proclist(kvm_t *, int, int, struct proc *, 127 struct kinfo_proc *, int); 128 static int proc_verify(kvm_t *, u_long, const struct proc *); 129 static void ps_str_a(struct ps_strings *, u_long *, int *); 130 static void ps_str_e(struct ps_strings *, u_long *, int *); 131 132 char * 133 _kvm_uread(kd, p, va, cnt) 134 kvm_t *kd; 135 const struct proc *p; 136 u_long va; 137 u_long *cnt; 138 { 139 u_long addr, head; 140 u_long offset; 141 struct vm_map_entry vme; 142 struct vm_amap amap; 143 struct vm_anon *anonp, anon; 144 struct vm_page pg; 145 u_long slot; 146 147 if (kd->swapspc == 0) { 148 kd->swapspc = (char *)_kvm_malloc(kd, kd->nbpg); 149 if (kd->swapspc == 0) 150 return (0); 151 } 152 153 /* 154 * Look through the address map for the memory object 155 * that corresponds to the given virtual address. 156 * The header just has the entire valid range. 157 */ 158 head = (u_long)&p->p_vmspace->vm_map.header; 159 addr = head; 160 while (1) { 161 if (KREAD(kd, addr, &vme)) 162 return (0); 163 164 if (va >= vme.start && va < vme.end && 165 vme.aref.ar_amap != NULL) 166 break; 167 168 addr = (u_long)vme.next; 169 if (addr == head) 170 return (0); 171 } 172 173 /* 174 * we found the map entry, now to find the object... 175 */ 176 if (vme.aref.ar_amap == NULL) 177 return NULL; 178 179 addr = (u_long)vme.aref.ar_amap; 180 if (KREAD(kd, addr, &amap)) 181 return NULL; 182 183 offset = va - vme.start; 184 slot = offset / kd->nbpg + vme.aref.ar_pageoff; 185 /* sanity-check slot number */ 186 if (slot > amap.am_nslot) 187 return NULL; 188 189 addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp); 190 if (KREAD(kd, addr, &anonp)) 191 return NULL; 192 193 addr = (u_long)anonp; 194 if (KREAD(kd, addr, &anon)) 195 return NULL; 196 197 addr = (u_long)anon.u.an_page; 198 if (addr) { 199 if (KREAD(kd, addr, &pg)) 200 return NULL; 201 202 if (_kvm_pread(kd, kd->pmfd, (void *)kd->swapspc, (size_t)kd->nbpg, (off_t)pg.phys_addr) != kd->nbpg) { 203 return NULL; 204 } 205 } else { 206 if (_kvm_pread(kd, kd->swfd, (void *)kd->swapspc, (size_t)kd->nbpg, (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg) { 207 return NULL; 208 } 209 } 210 211 /* Found the page. */ 212 offset %= kd->nbpg; 213 *cnt = kd->nbpg - offset; 214 return (&kd->swapspc[offset]); 215 } 216 217 /* 218 * Read proc's from memory file into buffer bp, which has space to hold 219 * at most maxcnt procs. 220 */ 221 static int 222 kvm_proclist(kd, what, arg, p, bp, maxcnt) 223 kvm_t *kd; 224 int what, arg; 225 struct proc *p; 226 struct kinfo_proc *bp; 227 int maxcnt; 228 { 229 int cnt = 0; 230 struct eproc eproc; 231 struct pgrp pgrp; 232 struct session sess; 233 struct tty tty; 234 struct proc proc; 235 236 for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) { 237 if (KREAD(kd, (u_long)p, &proc)) { 238 _kvm_err(kd, kd->program, "can't read proc at %x", p); 239 return (-1); 240 } 241 if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0) 242 KREAD(kd, (u_long)eproc.e_pcred.pc_ucred, 243 &eproc.e_ucred); 244 245 switch(what) { 246 247 case KERN_PROC_PID: 248 if (proc.p_pid != (pid_t)arg) 249 continue; 250 break; 251 252 case KERN_PROC_UID: 253 if (eproc.e_ucred.cr_uid != (uid_t)arg) 254 continue; 255 break; 256 257 case KERN_PROC_RUID: 258 if (eproc.e_pcred.p_ruid != (uid_t)arg) 259 continue; 260 break; 261 262 case KERN_PROC_ALL: 263 if (proc.p_flag & P_SYSTEM) 264 continue; 265 break; 266 } 267 /* 268 * We're going to add another proc to the set. If this 269 * will overflow the buffer, assume the reason is because 270 * nprocs (or the proc list) is corrupt and declare an error. 271 */ 272 if (cnt >= maxcnt) { 273 _kvm_err(kd, kd->program, "nprocs corrupt"); 274 return (-1); 275 } 276 /* 277 * gather eproc 278 */ 279 eproc.e_paddr = p; 280 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 281 _kvm_err(kd, kd->program, "can't read pgrp at %x", 282 proc.p_pgrp); 283 return (-1); 284 } 285 eproc.e_sess = pgrp.pg_session; 286 eproc.e_pgid = pgrp.pg_id; 287 eproc.e_jobc = pgrp.pg_jobc; 288 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 289 _kvm_err(kd, kd->program, "can't read session at %x", 290 pgrp.pg_session); 291 return (-1); 292 } 293 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 294 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 295 _kvm_err(kd, kd->program, 296 "can't read tty at %x", sess.s_ttyp); 297 return (-1); 298 } 299 eproc.e_tdev = tty.t_dev; 300 eproc.e_tsess = tty.t_session; 301 if (tty.t_pgrp != NULL) { 302 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 303 _kvm_err(kd, kd->program, 304 "can't read tpgrp at &x", 305 tty.t_pgrp); 306 return (-1); 307 } 308 eproc.e_tpgid = pgrp.pg_id; 309 } else 310 eproc.e_tpgid = -1; 311 } else 312 eproc.e_tdev = NODEV; 313 eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0; 314 if (sess.s_leader == p) 315 eproc.e_flag |= EPROC_SLEADER; 316 if (proc.p_wmesg) 317 (void)kvm_read(kd, (u_long)proc.p_wmesg, 318 eproc.e_wmesg, WMESGLEN); 319 320 (void)kvm_read(kd, (u_long)proc.p_vmspace, 321 &eproc.e_vm, sizeof(eproc.e_vm)); 322 323 eproc.e_xsize = eproc.e_xrssize = 0; 324 eproc.e_xccount = eproc.e_xswrss = 0; 325 326 switch (what) { 327 328 case KERN_PROC_PGRP: 329 if (eproc.e_pgid != (pid_t)arg) 330 continue; 331 break; 332 333 case KERN_PROC_TTY: 334 if ((proc.p_flag & P_CONTROLT) == 0 || 335 eproc.e_tdev != (dev_t)arg) 336 continue; 337 break; 338 } 339 bcopy(&proc, &bp->kp_proc, sizeof(proc)); 340 bcopy(&eproc, &bp->kp_eproc, sizeof(eproc)); 341 ++bp; 342 ++cnt; 343 } 344 return (cnt); 345 } 346 347 /* 348 * Build proc info array by reading in proc list from a crash dump. 349 * Return number of procs read. maxcnt is the max we will read. 350 */ 351 static int 352 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt) 353 kvm_t *kd; 354 int what, arg; 355 u_long a_allproc; 356 u_long a_zombproc; 357 int maxcnt; 358 { 359 struct kinfo_proc *bp = kd->procbase; 360 int acnt, zcnt; 361 struct proc *p; 362 363 if (KREAD(kd, a_allproc, &p)) { 364 _kvm_err(kd, kd->program, "cannot read allproc"); 365 return (-1); 366 } 367 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 368 if (acnt < 0) 369 return (acnt); 370 371 if (KREAD(kd, a_zombproc, &p)) { 372 _kvm_err(kd, kd->program, "cannot read zombproc"); 373 return (-1); 374 } 375 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 376 if (zcnt < 0) 377 zcnt = 0; 378 379 return (acnt + zcnt); 380 } 381 382 struct kinfo_proc * 383 kvm_getprocs(kd, op, arg, cnt) 384 kvm_t *kd; 385 int op, arg; 386 int *cnt; 387 { 388 size_t size; 389 int mib[4], st, nprocs; 390 391 if (kd->procbase != 0) { 392 free((void *)kd->procbase); 393 /* 394 * Clear this pointer in case this call fails. Otherwise, 395 * kvm_close() will free it again. 396 */ 397 kd->procbase = 0; 398 } 399 if (ISALIVE(kd)) { 400 size = 0; 401 mib[0] = CTL_KERN; 402 mib[1] = KERN_PROC; 403 mib[2] = op; 404 mib[3] = arg; 405 st = sysctl(mib, 4, NULL, &size, NULL, 0); 406 if (st == -1) { 407 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 408 return (0); 409 } 410 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 411 if (kd->procbase == 0) 412 return (0); 413 st = sysctl(mib, 4, kd->procbase, &size, NULL, 0); 414 if (st == -1) { 415 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 416 return (0); 417 } 418 if (size % sizeof(struct kinfo_proc) != 0) { 419 _kvm_err(kd, kd->program, 420 "proc size mismatch (%d total, %d chunks)", 421 size, sizeof(struct kinfo_proc)); 422 return (0); 423 } 424 nprocs = size / sizeof(struct kinfo_proc); 425 } else { 426 struct nlist nl[4], *p; 427 428 nl[0].n_name = "_nprocs"; 429 nl[1].n_name = "_allproc"; 430 nl[2].n_name = "_zombproc"; 431 nl[3].n_name = 0; 432 433 if (kvm_nlist(kd, nl) != 0) { 434 for (p = nl; p->n_type != 0; ++p) 435 ; 436 _kvm_err(kd, kd->program, 437 "%s: no such symbol", p->n_name); 438 return (0); 439 } 440 if (KREAD(kd, nl[0].n_value, &nprocs)) { 441 _kvm_err(kd, kd->program, "can't read nprocs"); 442 return (0); 443 } 444 size = nprocs * sizeof(struct kinfo_proc); 445 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 446 if (kd->procbase == 0) 447 return (0); 448 449 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 450 nl[2].n_value, nprocs); 451 #ifdef notdef 452 size = nprocs * sizeof(struct kinfo_proc); 453 (void)realloc(kd->procbase, size); 454 #endif 455 } 456 *cnt = nprocs; 457 return (kd->procbase); 458 } 459 460 void 461 _kvm_freeprocs(kd) 462 kvm_t *kd; 463 { 464 if (kd->procbase) { 465 free(kd->procbase); 466 kd->procbase = 0; 467 } 468 } 469 470 void * 471 _kvm_realloc(kd, p, n) 472 kvm_t *kd; 473 void *p; 474 size_t n; 475 { 476 void *np = (void *)realloc(p, n); 477 478 if (np == 0) 479 _kvm_err(kd, kd->program, "out of memory"); 480 return (np); 481 } 482 483 #ifndef MAX 484 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 485 #endif 486 487 /* 488 * Read in an argument vector from the user address space of process p. 489 * addr if the user-space base address of narg null-terminated contiguous 490 * strings. This is used to read in both the command arguments and 491 * environment strings. Read at most maxcnt characters of strings. 492 */ 493 static char ** 494 kvm_argv(kd, p, addr, narg, maxcnt) 495 kvm_t *kd; 496 const struct proc *p; 497 u_long addr; 498 int narg; 499 int maxcnt; 500 { 501 char *np, *cp, *ep, *ap; 502 u_long oaddr = -1; 503 int len, cc; 504 char **argv; 505 506 /* 507 * Check that there aren't an unreasonable number of agruments, 508 * and that the address is in user space. 509 */ 510 if (narg > ARG_MAX || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS) 511 return (0); 512 513 if (kd->argv == 0) { 514 /* 515 * Try to avoid reallocs. 516 */ 517 kd->argc = MAX(narg + 1, 32); 518 kd->argv = (char **)_kvm_malloc(kd, kd->argc * 519 sizeof(*kd->argv)); 520 if (kd->argv == 0) 521 return (0); 522 } else if (narg + 1 > kd->argc) { 523 kd->argc = MAX(2 * kd->argc, narg + 1); 524 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 525 sizeof(*kd->argv)); 526 if (kd->argv == 0) 527 return (0); 528 } 529 if (kd->argspc == 0) { 530 kd->argspc = (char *)_kvm_malloc(kd, kd->nbpg); 531 if (kd->argspc == 0) 532 return (0); 533 kd->arglen = kd->nbpg; 534 } 535 if (kd->argbuf == 0) { 536 kd->argbuf = (char *)_kvm_malloc(kd, kd->nbpg); 537 if (kd->argbuf == 0) 538 return (0); 539 } 540 cc = sizeof(char *) * narg; 541 if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc) 542 return (0); 543 ap = np = kd->argspc; 544 argv = kd->argv; 545 len = 0; 546 /* 547 * Loop over pages, filling in the argument vector. 548 */ 549 while (argv < kd->argv + narg && *argv != 0) { 550 addr = (u_long)*argv & ~(kd->nbpg - 1); 551 if (addr != oaddr) { 552 if (kvm_uread(kd, p, addr, kd->argbuf, kd->nbpg) != 553 kd->nbpg) 554 return (0); 555 oaddr = addr; 556 } 557 addr = (u_long)*argv & (kd->nbpg - 1); 558 cp = kd->argbuf + addr; 559 cc = kd->nbpg - addr; 560 if (maxcnt > 0 && cc > maxcnt - len) 561 cc = maxcnt - len;; 562 ep = memchr(cp, '\0', cc); 563 if (ep != 0) 564 cc = ep - cp + 1; 565 if (len + cc > kd->arglen) { 566 int off; 567 char **pp; 568 char *op = kd->argspc; 569 570 kd->arglen *= 2; 571 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 572 kd->arglen); 573 if (kd->argspc == 0) 574 return (0); 575 /* 576 * Adjust argv pointers in case realloc moved 577 * the string space. 578 */ 579 off = kd->argspc - op; 580 for (pp = kd->argv; pp < argv; pp++) 581 *pp += off; 582 ap += off; 583 np += off; 584 } 585 memcpy(np, cp, cc); 586 np += cc; 587 len += cc; 588 if (ep != 0) { 589 *argv++ = ap; 590 ap = np; 591 } else 592 *argv += cc; 593 if (maxcnt > 0 && len >= maxcnt) { 594 /* 595 * We're stopping prematurely. Terminate the 596 * current string. 597 */ 598 if (ep == 0) { 599 *np = '\0'; 600 *argv++ = ap; 601 } 602 break; 603 } 604 } 605 /* Make sure argv is terminated. */ 606 *argv = 0; 607 return (kd->argv); 608 } 609 610 static void 611 ps_str_a(p, addr, n) 612 struct ps_strings *p; 613 u_long *addr; 614 int *n; 615 { 616 *addr = (u_long)p->ps_argvstr; 617 *n = p->ps_nargvstr; 618 } 619 620 static void 621 ps_str_e(p, addr, n) 622 struct ps_strings *p; 623 u_long *addr; 624 int *n; 625 { 626 *addr = (u_long)p->ps_envstr; 627 *n = p->ps_nenvstr; 628 } 629 630 /* 631 * Determine if the proc indicated by p is still active. 632 * This test is not 100% foolproof in theory, but chances of 633 * being wrong are very low. 634 */ 635 static int 636 proc_verify(kd, kernp, p) 637 kvm_t *kd; 638 u_long kernp; 639 const struct proc *p; 640 { 641 struct proc kernproc; 642 643 /* 644 * Just read in the whole proc. It's not that big relative 645 * to the cost of the read system call. 646 */ 647 if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) != 648 sizeof(kernproc)) 649 return (0); 650 return (p->p_pid == kernproc.p_pid && 651 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB)); 652 } 653 654 static char ** 655 kvm_doargv(kd, kp, nchr, info) 656 kvm_t *kd; 657 const struct kinfo_proc *kp; 658 int nchr; 659 void (*info)(struct ps_strings *, u_long *, int *); 660 { 661 const struct proc *p = &kp->kp_proc; 662 char **ap; 663 u_long addr; 664 int cnt; 665 struct ps_strings arginfo; 666 static struct ps_strings *ps; 667 668 if (ps == NULL) { 669 struct _ps_strings _ps; 670 int mib[2]; 671 size_t len; 672 673 mib[0] = CTL_VM; 674 mib[1] = VM_PSSTRINGS; 675 len = sizeof(_ps); 676 sysctl(mib, 2, &_ps, &len, NULL, 0); 677 ps = (struct ps_strings *)_ps.val; 678 } 679 680 /* 681 * Pointers are stored at the top of the user stack. 682 */ 683 if (p->p_stat == SZOMB || 684 kvm_uread(kd, p, (u_long)ps, (char *)&arginfo, 685 sizeof(arginfo)) != sizeof(arginfo)) 686 return (0); 687 688 (*info)(&arginfo, &addr, &cnt); 689 if (cnt == 0) 690 return (0); 691 ap = kvm_argv(kd, p, addr, cnt, nchr); 692 /* 693 * For live kernels, make sure this process didn't go away. 694 */ 695 if (ap != 0 && ISALIVE(kd) && 696 !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p)) 697 ap = 0; 698 return (ap); 699 } 700 701 /* 702 * Get the command args. This code is now machine independent. 703 */ 704 char ** 705 kvm_getargv(kd, kp, nchr) 706 kvm_t *kd; 707 const struct kinfo_proc *kp; 708 int nchr; 709 { 710 return (kvm_doargv(kd, kp, nchr, ps_str_a)); 711 } 712 713 char ** 714 kvm_getenvv(kd, kp, nchr) 715 kvm_t *kd; 716 const struct kinfo_proc *kp; 717 int nchr; 718 { 719 return (kvm_doargv(kd, kp, nchr, ps_str_e)); 720 } 721 722 /* 723 * Read from user space. The user context is given by p. 724 */ 725 ssize_t 726 kvm_uread(kd, p, uva, buf, len) 727 kvm_t *kd; 728 const struct proc *p; 729 u_long uva; 730 char *buf; 731 size_t len; 732 { 733 char *cp; 734 735 cp = buf; 736 while (len > 0) { 737 int cc; 738 char *dp; 739 u_long cnt; 740 741 dp = _kvm_uread(kd, p, uva, &cnt); 742 if (dp == 0) { 743 _kvm_err(kd, 0, "invalid address (%lx)", uva); 744 return (0); 745 } 746 cc = MIN(cnt, len); 747 bcopy(dp, cp, cc); 748 749 cp += cc; 750 uva += cc; 751 len -= cc; 752 } 753 return (ssize_t)(cp - buf); 754 } 755