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