1 /* $NetBSD: kvm_proc.c,v 1.32 2000/01/15 19:16:32 chs 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.32 2000/01/15 19:16:32 chs 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 if (nprocs < 0) 472 return (0); 473 #ifdef notdef 474 size = nprocs * sizeof(struct kinfo_proc); 475 (void)realloc(kd->procbase, size); 476 #endif 477 } 478 *cnt = nprocs; 479 return (kd->procbase); 480 } 481 482 void 483 _kvm_freeprocs(kd) 484 kvm_t *kd; 485 { 486 if (kd->procbase) { 487 free(kd->procbase); 488 kd->procbase = 0; 489 } 490 } 491 492 void * 493 _kvm_realloc(kd, p, n) 494 kvm_t *kd; 495 void *p; 496 size_t n; 497 { 498 void *np = (void *)realloc(p, n); 499 500 if (np == 0) 501 _kvm_err(kd, kd->program, "out of memory"); 502 return (np); 503 } 504 505 #ifndef MAX 506 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 507 #endif 508 509 /* 510 * Read in an argument vector from the user address space of process p. 511 * addr if the user-space base address of narg null-terminated contiguous 512 * strings. This is used to read in both the command arguments and 513 * environment strings. Read at most maxcnt characters of strings. 514 */ 515 static char ** 516 kvm_argv(kd, p, addr, narg, maxcnt) 517 kvm_t *kd; 518 const struct proc *p; 519 u_long addr; 520 int narg; 521 int maxcnt; 522 { 523 char *np, *cp, *ep, *ap; 524 u_long oaddr = (u_long)~0L; 525 u_long len; 526 size_t cc; 527 char **argv; 528 529 /* 530 * Check that there aren't an unreasonable number of agruments, 531 * and that the address is in user space. 532 */ 533 if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva) 534 return (0); 535 536 if (kd->argv == 0) { 537 /* 538 * Try to avoid reallocs. 539 */ 540 kd->argc = MAX(narg + 1, 32); 541 kd->argv = (char **)_kvm_malloc(kd, kd->argc * 542 sizeof(*kd->argv)); 543 if (kd->argv == 0) 544 return (0); 545 } else if (narg + 1 > kd->argc) { 546 kd->argc = MAX(2 * kd->argc, narg + 1); 547 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 548 sizeof(*kd->argv)); 549 if (kd->argv == 0) 550 return (0); 551 } 552 if (kd->argspc == 0) { 553 kd->argspc = (char *)_kvm_malloc(kd, (size_t)kd->nbpg); 554 if (kd->argspc == 0) 555 return (0); 556 kd->arglen = kd->nbpg; 557 } 558 if (kd->argbuf == 0) { 559 kd->argbuf = (char *)_kvm_malloc(kd, (size_t)kd->nbpg); 560 if (kd->argbuf == 0) 561 return (0); 562 } 563 cc = sizeof(char *) * narg; 564 if (kvm_uread(kd, p, addr, (void *)kd->argv, cc) != cc) 565 return (0); 566 ap = np = kd->argspc; 567 argv = kd->argv; 568 len = 0; 569 /* 570 * Loop over pages, filling in the argument vector. 571 */ 572 while (argv < kd->argv + narg && *argv != 0) { 573 addr = (u_long)*argv & ~(kd->nbpg - 1); 574 if (addr != oaddr) { 575 if (kvm_uread(kd, p, addr, kd->argbuf, 576 (size_t)kd->nbpg) != kd->nbpg) 577 return (0); 578 oaddr = addr; 579 } 580 addr = (u_long)*argv & (kd->nbpg - 1); 581 cp = kd->argbuf + (size_t)addr; 582 cc = kd->nbpg - (size_t)addr; 583 if (maxcnt > 0 && cc > (size_t)(maxcnt - len)) 584 cc = (size_t)(maxcnt - len); 585 ep = memchr(cp, '\0', cc); 586 if (ep != 0) 587 cc = ep - cp + 1; 588 if (len + cc > kd->arglen) { 589 int off; 590 char **pp; 591 char *op = kd->argspc; 592 593 kd->arglen *= 2; 594 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 595 (size_t)kd->arglen); 596 if (kd->argspc == 0) 597 return (0); 598 /* 599 * Adjust argv pointers in case realloc moved 600 * the string space. 601 */ 602 off = kd->argspc - op; 603 for (pp = kd->argv; pp < argv; pp++) 604 *pp += off; 605 ap += off; 606 np += off; 607 } 608 memcpy(np, cp, cc); 609 np += cc; 610 len += cc; 611 if (ep != 0) { 612 *argv++ = ap; 613 ap = np; 614 } else 615 *argv += cc; 616 if (maxcnt > 0 && len >= maxcnt) { 617 /* 618 * We're stopping prematurely. Terminate the 619 * current string. 620 */ 621 if (ep == 0) { 622 *np = '\0'; 623 *argv++ = ap; 624 } 625 break; 626 } 627 } 628 /* Make sure argv is terminated. */ 629 *argv = 0; 630 return (kd->argv); 631 } 632 633 static void 634 ps_str_a(p, addr, n) 635 struct ps_strings *p; 636 u_long *addr; 637 int *n; 638 { 639 *addr = (u_long)p->ps_argvstr; 640 *n = p->ps_nargvstr; 641 } 642 643 static void 644 ps_str_e(p, addr, n) 645 struct ps_strings *p; 646 u_long *addr; 647 int *n; 648 { 649 *addr = (u_long)p->ps_envstr; 650 *n = p->ps_nenvstr; 651 } 652 653 /* 654 * Determine if the proc indicated by p is still active. 655 * This test is not 100% foolproof in theory, but chances of 656 * being wrong are very low. 657 */ 658 static int 659 proc_verify(kd, kernp, p) 660 kvm_t *kd; 661 u_long kernp; 662 const struct proc *p; 663 { 664 struct proc kernproc; 665 666 /* 667 * Just read in the whole proc. It's not that big relative 668 * to the cost of the read system call. 669 */ 670 if (kvm_read(kd, kernp, (void *)&kernproc, sizeof(kernproc)) != 671 sizeof(kernproc)) 672 return (0); 673 return (p->p_pid == kernproc.p_pid && 674 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB)); 675 } 676 677 static char ** 678 kvm_doargv(kd, kp, nchr, info) 679 kvm_t *kd; 680 const struct kinfo_proc *kp; 681 int nchr; 682 void (*info)(struct ps_strings *, u_long *, int *); 683 { 684 const struct proc *p = &kp->kp_proc; 685 char **ap; 686 u_long addr; 687 int cnt; 688 struct ps_strings arginfo; 689 690 /* 691 * Pointers are stored at the top of the user stack. 692 */ 693 if (p->p_stat == SZOMB) 694 return (0); 695 cnt = kvm_uread(kd, p, kd->usrstack - sizeof(arginfo), 696 (void *)&arginfo, sizeof(arginfo)); 697 if (cnt != sizeof(arginfo)) 698 return (0); 699 700 (*info)(&arginfo, &addr, &cnt); 701 if (cnt == 0) 702 return (0); 703 ap = kvm_argv(kd, p, addr, cnt, nchr); 704 /* 705 * For live kernels, make sure this process didn't go away. 706 */ 707 if (ap != 0 && ISALIVE(kd) && 708 !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p)) 709 ap = 0; 710 return (ap); 711 } 712 713 /* 714 * Get the command args. This code is now machine independent. 715 */ 716 char ** 717 kvm_getargv(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_a)); 723 } 724 725 char ** 726 kvm_getenvv(kd, kp, nchr) 727 kvm_t *kd; 728 const struct kinfo_proc *kp; 729 int nchr; 730 { 731 return (kvm_doargv(kd, kp, nchr, ps_str_e)); 732 } 733 734 /* 735 * Read from user space. The user context is given by p. 736 */ 737 ssize_t 738 kvm_uread(kd, p, uva, buf, len) 739 kvm_t *kd; 740 const struct proc *p; 741 u_long uva; 742 char *buf; 743 size_t len; 744 { 745 char *cp; 746 747 cp = buf; 748 while (len > 0) { 749 size_t cc; 750 char *dp; 751 u_long cnt; 752 753 dp = _kvm_uread(kd, p, uva, &cnt); 754 if (dp == 0) { 755 _kvm_err(kd, 0, "invalid address (%x)", uva); 756 return (0); 757 } 758 cc = (size_t)MIN(cnt, len); 759 memcpy(cp, dp, cc); 760 cp += cc; 761 uva += cc; 762 len -= cc; 763 } 764 return (ssize_t)(cp - buf); 765 } 766