1 /* $OpenBSD: kvm_proc.c,v 1.19 2003/11/17 20:25:18 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.19 2003/11/17 20:25:18 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 memset(nl, 0, sizeof(nl)); 425 nl[0].n_name = "_nprocs"; 426 nl[1].n_name = "_allproc"; 427 nl[2].n_name = "_zombproc"; 428 nl[3].n_name = NULL; 429 430 if (kvm_nlist(kd, nl) != 0) { 431 for (p = nl; p->n_type != 0; ++p) 432 ; 433 _kvm_err(kd, kd->program, 434 "%s: no such symbol", p->n_name); 435 return (0); 436 } 437 if (KREAD(kd, nl[0].n_value, &nprocs)) { 438 _kvm_err(kd, kd->program, "can't read nprocs"); 439 return (0); 440 } 441 size = nprocs * sizeof(struct kinfo_proc); 442 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 443 if (kd->procbase == 0) 444 return (0); 445 446 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 447 nl[2].n_value, nprocs); 448 #ifdef notdef 449 size = nprocs * sizeof(struct kinfo_proc); 450 (void)realloc(kd->procbase, size); 451 #endif 452 } 453 *cnt = nprocs; 454 return (kd->procbase); 455 } 456 457 void 458 _kvm_freeprocs(kd) 459 kvm_t *kd; 460 { 461 if (kd->procbase) { 462 free(kd->procbase); 463 kd->procbase = 0; 464 } 465 } 466 467 void * 468 _kvm_realloc(kd, p, n) 469 kvm_t *kd; 470 void *p; 471 size_t n; 472 { 473 void *np = (void *)realloc(p, n); 474 475 if (np == 0) 476 _kvm_err(kd, kd->program, "out of memory"); 477 return (np); 478 } 479 480 #ifndef MAX 481 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 482 #endif 483 484 /* 485 * Read in an argument vector from the user address space of process p. 486 * addr if the user-space base address of narg null-terminated contiguous 487 * strings. This is used to read in both the command arguments and 488 * environment strings. Read at most maxcnt characters of strings. 489 */ 490 static char ** 491 kvm_argv(kd, p, addr, narg, maxcnt) 492 kvm_t *kd; 493 const struct proc *p; 494 u_long addr; 495 int narg; 496 int maxcnt; 497 { 498 char *np, *cp, *ep, *ap; 499 u_long oaddr = -1; 500 int len, cc; 501 char **argv; 502 503 /* 504 * Check that there aren't an unreasonable number of agruments, 505 * and that the address is in user space. 506 */ 507 if (narg > ARG_MAX || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS) 508 return (0); 509 510 if (kd->argv == 0) { 511 /* 512 * Try to avoid reallocs. 513 */ 514 kd->argc = MAX(narg + 1, 32); 515 kd->argv = (char **)_kvm_malloc(kd, kd->argc * 516 sizeof(*kd->argv)); 517 if (kd->argv == 0) 518 return (0); 519 } else if (narg + 1 > kd->argc) { 520 kd->argc = MAX(2 * kd->argc, narg + 1); 521 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 522 sizeof(*kd->argv)); 523 if (kd->argv == 0) 524 return (0); 525 } 526 if (kd->argspc == 0) { 527 kd->argspc = (char *)_kvm_malloc(kd, kd->nbpg); 528 if (kd->argspc == 0) 529 return (0); 530 kd->arglen = kd->nbpg; 531 } 532 if (kd->argbuf == 0) { 533 kd->argbuf = (char *)_kvm_malloc(kd, kd->nbpg); 534 if (kd->argbuf == 0) 535 return (0); 536 } 537 cc = sizeof(char *) * narg; 538 if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc) 539 return (0); 540 ap = np = kd->argspc; 541 argv = kd->argv; 542 len = 0; 543 /* 544 * Loop over pages, filling in the argument vector. 545 */ 546 while (argv < kd->argv + narg && *argv != 0) { 547 addr = (u_long)*argv & ~(kd->nbpg - 1); 548 if (addr != oaddr) { 549 if (kvm_uread(kd, p, addr, kd->argbuf, kd->nbpg) != 550 kd->nbpg) 551 return (0); 552 oaddr = addr; 553 } 554 addr = (u_long)*argv & (kd->nbpg - 1); 555 cp = kd->argbuf + addr; 556 cc = kd->nbpg - addr; 557 if (maxcnt > 0 && cc > maxcnt - len) 558 cc = maxcnt - len; 559 ep = memchr(cp, '\0', cc); 560 if (ep != 0) 561 cc = ep - cp + 1; 562 if (len + cc > kd->arglen) { 563 int off; 564 char **pp; 565 char *op = kd->argspc; 566 567 kd->arglen *= 2; 568 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 569 kd->arglen); 570 if (kd->argspc == 0) 571 return (0); 572 /* 573 * Adjust argv pointers in case realloc moved 574 * the string space. 575 */ 576 off = kd->argspc - op; 577 for (pp = kd->argv; pp < argv; pp++) 578 *pp += off; 579 ap += off; 580 np += off; 581 } 582 memcpy(np, cp, cc); 583 np += cc; 584 len += cc; 585 if (ep != 0) { 586 *argv++ = ap; 587 ap = np; 588 } else 589 *argv += cc; 590 if (maxcnt > 0 && len >= maxcnt) { 591 /* 592 * We're stopping prematurely. Terminate the 593 * current string. 594 */ 595 if (ep == 0) { 596 *np = '\0'; 597 *argv++ = ap; 598 } 599 break; 600 } 601 } 602 /* Make sure argv is terminated. */ 603 *argv = 0; 604 return (kd->argv); 605 } 606 607 static void 608 ps_str_a(p, addr, n) 609 struct ps_strings *p; 610 u_long *addr; 611 int *n; 612 { 613 *addr = (u_long)p->ps_argvstr; 614 *n = p->ps_nargvstr; 615 } 616 617 static void 618 ps_str_e(p, addr, n) 619 struct ps_strings *p; 620 u_long *addr; 621 int *n; 622 { 623 *addr = (u_long)p->ps_envstr; 624 *n = p->ps_nenvstr; 625 } 626 627 /* 628 * Determine if the proc indicated by p is still active. 629 * This test is not 100% foolproof in theory, but chances of 630 * being wrong are very low. 631 */ 632 static int 633 proc_verify(kd, kernp, p) 634 kvm_t *kd; 635 u_long kernp; 636 const struct proc *p; 637 { 638 struct proc kernproc; 639 640 /* 641 * Just read in the whole proc. It's not that big relative 642 * to the cost of the read system call. 643 */ 644 if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) != 645 sizeof(kernproc)) 646 return (0); 647 return (p->p_pid == kernproc.p_pid && 648 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB)); 649 } 650 651 static char ** 652 kvm_doargv(kd, kp, nchr, info) 653 kvm_t *kd; 654 const struct kinfo_proc *kp; 655 int nchr; 656 void (*info)(struct ps_strings *, u_long *, int *); 657 { 658 const struct proc *p = &kp->kp_proc; 659 char **ap; 660 u_long addr; 661 int cnt; 662 struct ps_strings arginfo; 663 static struct ps_strings *ps; 664 665 if (ps == NULL) { 666 struct _ps_strings _ps; 667 int mib[2]; 668 size_t len; 669 670 mib[0] = CTL_VM; 671 mib[1] = VM_PSSTRINGS; 672 len = sizeof(_ps); 673 sysctl(mib, 2, &_ps, &len, NULL, 0); 674 ps = (struct ps_strings *)_ps.val; 675 } 676 677 /* 678 * Pointers are stored at the top of the user stack. 679 */ 680 if (p->p_stat == SZOMB || 681 kvm_uread(kd, p, (u_long)ps, (char *)&arginfo, 682 sizeof(arginfo)) != sizeof(arginfo)) 683 return (0); 684 685 (*info)(&arginfo, &addr, &cnt); 686 if (cnt == 0) 687 return (0); 688 ap = kvm_argv(kd, p, addr, cnt, nchr); 689 /* 690 * For live kernels, make sure this process didn't go away. 691 */ 692 if (ap != 0 && ISALIVE(kd) && 693 !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p)) 694 ap = 0; 695 return (ap); 696 } 697 698 static char ** 699 kvm_arg_sysctl(kvm_t *kd, const struct kinfo_proc *kp, int nchr, int env) 700 { 701 int mib[4]; 702 size_t len, orglen; 703 int ret; 704 char **argv, *arg; 705 char *buf; 706 707 orglen = kd->nbpg; 708 if (kd->argbuf == NULL && 709 (kd->argbuf = _kvm_malloc(kd, orglen)) == NULL) 710 return (NULL); 711 712 again: 713 mib[0] = CTL_KERN; 714 mib[1] = KERN_PROC_ARGS; 715 mib[2] = (int)kp->kp_proc.p_pid; 716 mib[3] = env ? KERN_PROC_ENV : KERN_PROC_ARGV; 717 718 len = orglen; 719 ret = (sysctl(mib, 4, kd->argbuf, &len, NULL, 0) < 0); 720 if (ret && errno == ENOMEM) { 721 orglen += kd->nbpg; 722 buf = _kvm_realloc(kd, kd->argbuf, orglen); 723 if (buf == NULL) 724 return (NULL); 725 kd->argbuf = buf; 726 goto again; 727 } 728 729 if (ret) { 730 free(kd->argbuf); 731 kd->argbuf = NULL; 732 _kvm_syserr(kd, kd->program, "kvm_arg_sysctl"); 733 return (NULL); 734 } 735 #if 0 736 for (argv = (char **)kd->argbuf; *argv != NULL; argv++) 737 if (strlen(*argv) > nchr) 738 *argv[nchr] = '\0'; 739 #endif 740 741 return (char **)(kd->argbuf); 742 } 743 744 /* 745 * Get the command args. This code is now machine independent. 746 */ 747 char ** 748 kvm_getargv(kd, kp, nchr) 749 kvm_t *kd; 750 const struct kinfo_proc *kp; 751 int nchr; 752 { 753 if (ISALIVE(kd)) 754 return (kvm_arg_sysctl(kd, kp, nchr, 0)); 755 return (kvm_doargv(kd, kp, nchr, ps_str_a)); 756 } 757 758 char ** 759 kvm_getenvv(kd, kp, nchr) 760 kvm_t *kd; 761 const struct kinfo_proc *kp; 762 int nchr; 763 { 764 if (ISALIVE(kd)) 765 return (kvm_arg_sysctl(kd, kp, nchr, 1)); 766 return (kvm_doargv(kd, kp, nchr, ps_str_e)); 767 } 768 769 /* 770 * Read from user space. The user context is given by p. 771 */ 772 ssize_t 773 kvm_uread(kd, p, uva, buf, len) 774 kvm_t *kd; 775 const struct proc *p; 776 u_long uva; 777 char *buf; 778 size_t len; 779 { 780 char *cp; 781 782 cp = buf; 783 while (len > 0) { 784 int cc; 785 char *dp; 786 u_long cnt; 787 788 dp = _kvm_uread(kd, p, uva, &cnt); 789 if (dp == 0) { 790 _kvm_err(kd, 0, "invalid address (%lx)", uva); 791 return (0); 792 } 793 cc = MIN(cnt, len); 794 bcopy(dp, cp, cc); 795 796 cp += cc; 797 uva += cc; 798 len -= cc; 799 } 800 return (ssize_t)(cp - buf); 801 } 802