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