1 /* $NetBSD: kvm_proc.c,v 1.29 1999/01/25 03:38:57 mrg 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.29 1999/01/25 03:38:57 mrg 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 #include <vm/swap_pager.h> 108 109 #if defined(UVM) 110 #include <uvm/uvm_extern.h> 111 #include <uvm/uvm_amap.h> 112 #endif 113 114 #include <sys/sysctl.h> 115 116 #include <limits.h> 117 #include <db.h> 118 #include <paths.h> 119 120 #include "kvm_private.h" 121 122 #define KREAD(kd, addr, obj) \ 123 (kvm_read(kd, addr, (void *)(obj), sizeof(*obj)) != sizeof(*obj)) 124 125 char *_kvm_uread __P((kvm_t *, const struct proc *, u_long, u_long *)); 126 #if !defined(UVM) 127 int _kvm_coreinit __P((kvm_t *)); 128 int _kvm_readfromcore __P((kvm_t *, u_long, u_long)); 129 int _kvm_readfrompager __P((kvm_t *, struct vm_object *, u_long)); 130 #endif 131 ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *, 132 size_t)); 133 134 static char **kvm_argv __P((kvm_t *, const struct proc *, u_long, int, 135 int)); 136 static int kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, u_long, 137 int)); 138 static char **kvm_doargv __P((kvm_t *, const struct kinfo_proc *, int, 139 void (*)(struct ps_strings *, u_long *, int *))); 140 static int kvm_proclist __P((kvm_t *, int, int, struct proc *, 141 struct kinfo_proc *, int)); 142 static int proc_verify __P((kvm_t *, u_long, const struct proc *)); 143 static void ps_str_a __P((struct ps_strings *, u_long *, int *)); 144 static void ps_str_e __P((struct ps_strings *, u_long *, int *)); 145 146 char * 147 _kvm_uread(kd, p, va, cnt) 148 kvm_t *kd; 149 const struct proc *p; 150 u_long va; 151 u_long *cnt; 152 { 153 int true = 1; 154 u_long addr, head; 155 u_long offset; 156 struct vm_map_entry vme; 157 #if defined(UVM) 158 struct vm_amap amap; 159 struct vm_anon *anonp, anon; 160 struct vm_page pg; 161 u_long slot; 162 #else 163 struct vm_object vmo; 164 int rv; 165 #endif 166 167 if (kd->swapspc == 0) { 168 kd->swapspc = (char *)_kvm_malloc(kd, (size_t)kd->nbpg); 169 if (kd->swapspc == 0) 170 return (0); 171 } 172 173 /* 174 * Look through the address map for the memory object 175 * that corresponds to the given virtual address. 176 * The header just has the entire valid range. 177 */ 178 head = (u_long)&p->p_vmspace->vm_map.header; 179 addr = head; 180 while (true) { 181 if (KREAD(kd, addr, &vme)) 182 return (0); 183 184 #if defined(UVM) 185 if (va >= vme.start && va < vme.end && 186 vme.aref.ar_amap != NULL) 187 break; 188 189 #else 190 if (va >= vme.start && va < vme.end && 191 vme.object.vm_object != 0) 192 break; 193 #endif 194 195 addr = (u_long)vme.next; 196 if (addr == head) 197 return (0); 198 199 } 200 #if defined(UVM) 201 202 /* 203 * we found the map entry, now to find the object... 204 */ 205 if (vme.aref.ar_amap == NULL) 206 return NULL; 207 208 addr = (u_long)vme.aref.ar_amap; 209 if (KREAD(kd, addr, &amap)) 210 return NULL; 211 212 offset = va - vme.start; 213 slot = offset / kd->nbpg + vme.aref.ar_pageoff; 214 /* sanity-check slot number */ 215 if (slot > amap.am_nslot) 216 return NULL; 217 218 addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp); 219 if (KREAD(kd, addr, &anonp)) 220 return NULL; 221 222 addr = (u_long)anonp; 223 if (KREAD(kd, addr, &anon)) 224 return NULL; 225 226 addr = (u_long)anon.u.an_page; 227 if (addr) { 228 if (KREAD(kd, addr, &pg)) 229 return NULL; 230 231 if (pread(kd->pmfd, (void *)kd->swapspc, (size_t)kd->nbpg, 232 (off_t)pg.phys_addr) != kd->nbpg) 233 return NULL; 234 } 235 else { 236 if (pread(kd->swfd, (void *)kd->swapspc, (size_t)kd->nbpg, 237 (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg) 238 return NULL; 239 } 240 #else 241 /* 242 * We found the right object -- follow shadow links. 243 */ 244 offset = va - vme.start + vme.offset; 245 addr = (u_long)vme.object.vm_object; 246 247 while (1) { 248 /* Try reading the page from core first. */ 249 if ((rv = _kvm_readfromcore(kd, addr, offset))) 250 break; 251 252 if (KREAD(kd, addr, &vmo)) 253 return (0); 254 255 /* If there is a pager here, see if it has the page. */ 256 if (vmo.pager != 0 && 257 (rv = _kvm_readfrompager(kd, &vmo, offset))) 258 break; 259 260 /* Move down the shadow chain. */ 261 addr = (u_long)vmo.shadow; 262 if (addr == 0) 263 return (0); 264 offset += vmo.shadow_offset; 265 } 266 267 if (rv == -1) 268 return (0); 269 #endif 270 271 /* Found the page. */ 272 offset %= kd->nbpg; 273 *cnt = kd->nbpg - offset; 274 return (&kd->swapspc[(size_t)offset]); 275 } 276 277 #if !defined(UVM) 278 279 #define vm_page_hash(kd, object, offset) \ 280 (((u_long)object + (u_long)(offset / kd->nbpg)) & kd->vm_page_hash_mask) 281 282 int 283 _kvm_coreinit(kd) 284 kvm_t *kd; 285 { 286 struct nlist nlist[3]; 287 288 nlist[0].n_name = "_vm_page_buckets"; 289 nlist[1].n_name = "_vm_page_hash_mask"; 290 nlist[2].n_name = 0; 291 if (kvm_nlist(kd, nlist) != 0) 292 return (-1); 293 294 if (KREAD(kd, nlist[0].n_value, &kd->vm_page_buckets) || 295 KREAD(kd, nlist[1].n_value, &kd->vm_page_hash_mask)) 296 return (-1); 297 298 return (0); 299 } 300 301 int 302 _kvm_readfromcore(kd, object, offset) 303 kvm_t *kd; 304 u_long object, offset; 305 { 306 u_long addr; 307 struct pglist bucket; 308 struct vm_page mem; 309 off_t seekpoint; 310 311 if (kd->vm_page_buckets == 0 && 312 _kvm_coreinit(kd)) 313 return (-1); 314 315 addr = (u_long)&kd->vm_page_buckets[vm_page_hash(kd, object, offset)]; 316 if (KREAD(kd, addr, &bucket)) 317 return (-1); 318 319 addr = (u_long)bucket.tqh_first; 320 offset &= ~(kd->nbpg -1); 321 while (1) { 322 if (addr == 0) 323 return (0); 324 325 if (KREAD(kd, addr, &mem)) 326 return (-1); 327 328 if ((u_long)mem.object == object && 329 (u_long)mem.offset == offset) 330 break; 331 332 addr = (u_long)mem.hashq.tqe_next; 333 } 334 335 seekpoint = mem.phys_addr; 336 337 if (pread(kd->pmfd, kd->swapspc, kd->nbpg, seekpoint) != kd->nbpg) 338 return (-1); 339 340 return (1); 341 } 342 343 int 344 _kvm_readfrompager(kd, vmop, offset) 345 kvm_t *kd; 346 struct vm_object *vmop; 347 u_long offset; 348 { 349 u_long addr; 350 struct pager_struct pager; 351 struct swpager swap; 352 int ix; 353 struct swblock swb; 354 off_t seekpoint; 355 356 /* Read in the pager info and make sure it's a swap device. */ 357 addr = (u_long)vmop->pager; 358 if (KREAD(kd, addr, &pager) || pager.pg_type != PG_SWAP) 359 return (-1); 360 361 /* Read in the swap_pager private data. */ 362 addr = (u_long)pager.pg_data; 363 if (KREAD(kd, addr, &swap)) 364 return (-1); 365 366 /* 367 * Calculate the paging offset, and make sure it's within the 368 * bounds of the pager. 369 */ 370 offset += vmop->paging_offset; 371 ix = offset / dbtob(swap.sw_bsize); 372 #if 0 373 if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks) 374 return (-1); 375 #else 376 if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks) { 377 int i; 378 printf("BUG BUG BUG BUG:\n"); 379 printf("object %p offset %lx pgoffset %lx ", 380 vmop, offset - vmop->paging_offset, 381 (u_long)vmop->paging_offset); 382 printf("pager %p swpager %p\n", 383 vmop->pager, pager.pg_data); 384 printf("osize %lx bsize %x blocks %p nblocks %x\n", 385 (u_long)swap.sw_osize, swap.sw_bsize, swap.sw_blocks, 386 swap.sw_nblocks); 387 for (i = 0; i < swap.sw_nblocks; i++) { 388 addr = (u_long)&swap.sw_blocks[i]; 389 if (KREAD(kd, addr, &swb)) 390 return (0); 391 printf("sw_blocks[%d]: block %x mask %x\n", i, 392 swb.swb_block, swb.swb_mask); 393 } 394 return (-1); 395 } 396 #endif 397 398 /* Read in the swap records. */ 399 addr = (u_long)&swap.sw_blocks[ix]; 400 if (KREAD(kd, addr, &swb)) 401 return (-1); 402 403 /* Calculate offset within pager. */ 404 offset %= dbtob(swap.sw_bsize); 405 406 /* Check that the page is actually present. */ 407 if ((swb.swb_mask & (1 << (offset / kd->nbpg))) == 0) 408 return (0); 409 410 if (!ISALIVE(kd)) 411 return (-1); 412 413 /* Calculate the physical address and read the page. */ 414 seekpoint = dbtob(swb.swb_block) + (offset & ~(kd->nbpg -1)); 415 416 if (pread(kd->swfd, kd->swapspc, kd->nbpg, seekpoint) != kd->nbpg) 417 return (-1); 418 419 return (1); 420 } 421 #endif /* !defined(UVM) */ 422 423 /* 424 * Read proc's from memory file into buffer bp, which has space to hold 425 * at most maxcnt procs. 426 */ 427 static int 428 kvm_proclist(kd, what, arg, p, bp, maxcnt) 429 kvm_t *kd; 430 int what, arg; 431 struct proc *p; 432 struct kinfo_proc *bp; 433 int maxcnt; 434 { 435 int cnt = 0; 436 struct eproc eproc; 437 struct pgrp pgrp; 438 struct session sess; 439 struct tty tty; 440 struct proc proc; 441 442 for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) { 443 if (KREAD(kd, (u_long)p, &proc)) { 444 _kvm_err(kd, kd->program, "can't read proc at %x", p); 445 return (-1); 446 } 447 if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0) 448 if (KREAD(kd, (u_long)eproc.e_pcred.pc_ucred, 449 &eproc.e_ucred)) { 450 _kvm_err(kd, kd->program, 451 "can't read proc credentials at %x", p); 452 return -1; 453 } 454 455 switch(what) { 456 457 case KERN_PROC_PID: 458 if (proc.p_pid != (pid_t)arg) 459 continue; 460 break; 461 462 case KERN_PROC_UID: 463 if (eproc.e_ucred.cr_uid != (uid_t)arg) 464 continue; 465 break; 466 467 case KERN_PROC_RUID: 468 if (eproc.e_pcred.p_ruid != (uid_t)arg) 469 continue; 470 break; 471 } 472 /* 473 * We're going to add another proc to the set. If this 474 * will overflow the buffer, assume the reason is because 475 * nprocs (or the proc list) is corrupt and declare an error. 476 */ 477 if (cnt >= maxcnt) { 478 _kvm_err(kd, kd->program, "nprocs corrupt"); 479 return (-1); 480 } 481 /* 482 * gather eproc 483 */ 484 eproc.e_paddr = p; 485 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 486 _kvm_err(kd, kd->program, "can't read pgrp at %x", 487 proc.p_pgrp); 488 return (-1); 489 } 490 eproc.e_sess = pgrp.pg_session; 491 eproc.e_pgid = pgrp.pg_id; 492 eproc.e_jobc = pgrp.pg_jobc; 493 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 494 _kvm_err(kd, kd->program, "can't read session at %x", 495 pgrp.pg_session); 496 return (-1); 497 } 498 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 499 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 500 _kvm_err(kd, kd->program, 501 "can't read tty at %x", sess.s_ttyp); 502 return (-1); 503 } 504 eproc.e_tdev = tty.t_dev; 505 eproc.e_tsess = tty.t_session; 506 if (tty.t_pgrp != NULL) { 507 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 508 _kvm_err(kd, kd->program, 509 "can't read tpgrp at &x", 510 tty.t_pgrp); 511 return (-1); 512 } 513 eproc.e_tpgid = pgrp.pg_id; 514 } else 515 eproc.e_tpgid = -1; 516 } else 517 eproc.e_tdev = NODEV; 518 eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0; 519 if (sess.s_leader == p) 520 eproc.e_flag |= EPROC_SLEADER; 521 if (proc.p_wmesg) 522 (void)kvm_read(kd, (u_long)proc.p_wmesg, 523 eproc.e_wmesg, WMESGLEN); 524 525 (void)kvm_read(kd, (u_long)proc.p_vmspace, 526 (void *)&eproc.e_vm, sizeof(eproc.e_vm)); 527 528 eproc.e_xsize = eproc.e_xrssize = 0; 529 eproc.e_xccount = eproc.e_xswrss = 0; 530 531 switch (what) { 532 533 case KERN_PROC_PGRP: 534 if (eproc.e_pgid != (pid_t)arg) 535 continue; 536 break; 537 538 case KERN_PROC_TTY: 539 if ((proc.p_flag & P_CONTROLT) == 0 || 540 eproc.e_tdev != (dev_t)arg) 541 continue; 542 break; 543 } 544 memcpy(&bp->kp_proc, &proc, sizeof(proc)); 545 memcpy(&bp->kp_eproc, &eproc, sizeof(eproc)); 546 ++bp; 547 ++cnt; 548 } 549 return (cnt); 550 } 551 552 /* 553 * Build proc info array by reading in proc list from a crash dump. 554 * Return number of procs read. maxcnt is the max we will read. 555 */ 556 static int 557 kvm_deadprocs(kd, what, arg, a_allproc, a_deadproc, a_zombproc, maxcnt) 558 kvm_t *kd; 559 int what, arg; 560 u_long a_allproc; 561 u_long a_deadproc; 562 u_long a_zombproc; 563 int maxcnt; 564 { 565 struct kinfo_proc *bp = kd->procbase; 566 int acnt, dcnt, zcnt; 567 struct proc *p; 568 569 if (KREAD(kd, a_allproc, &p)) { 570 _kvm_err(kd, kd->program, "cannot read allproc"); 571 return (-1); 572 } 573 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 574 if (acnt < 0) 575 return (acnt); 576 577 if (KREAD(kd, a_deadproc, &p)) { 578 _kvm_err(kd, kd->program, "cannot read deadproc"); 579 return (-1); 580 } 581 582 dcnt = kvm_proclist(kd, what, arg, p, bp, maxcnt - acnt); 583 if (dcnt < 0) 584 dcnt = 0; 585 586 if (KREAD(kd, a_zombproc, &p)) { 587 _kvm_err(kd, kd->program, "cannot read zombproc"); 588 return (-1); 589 } 590 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, 591 maxcnt - (acnt + dcnt)); 592 if (zcnt < 0) 593 zcnt = 0; 594 595 return (acnt + zcnt); 596 } 597 598 struct kinfo_proc * 599 kvm_getprocs(kd, op, arg, cnt) 600 kvm_t *kd; 601 int op, arg; 602 int *cnt; 603 { 604 size_t size; 605 int mib[4], st, nprocs; 606 607 if (kd->procbase != 0) { 608 free((void *)kd->procbase); 609 /* 610 * Clear this pointer in case this call fails. Otherwise, 611 * kvm_close() will free it again. 612 */ 613 kd->procbase = 0; 614 } 615 if (ISALIVE(kd)) { 616 size = 0; 617 mib[0] = CTL_KERN; 618 mib[1] = KERN_PROC; 619 mib[2] = op; 620 mib[3] = arg; 621 st = sysctl(mib, 4, NULL, &size, NULL, 0); 622 if (st == -1) { 623 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 624 return (0); 625 } 626 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 627 if (kd->procbase == 0) 628 return (0); 629 st = sysctl(mib, 4, kd->procbase, &size, NULL, 0); 630 if (st == -1) { 631 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 632 return (0); 633 } 634 if (size % sizeof(struct kinfo_proc) != 0) { 635 _kvm_err(kd, kd->program, 636 "proc size mismatch (%d total, %d chunks)", 637 size, sizeof(struct kinfo_proc)); 638 return (0); 639 } 640 nprocs = size / sizeof(struct kinfo_proc); 641 } else { 642 struct nlist nl[5], *p; 643 644 nl[0].n_name = "_nprocs"; 645 nl[1].n_name = "_allproc"; 646 nl[2].n_name = "_deadproc"; 647 nl[3].n_name = "_zombproc"; 648 nl[4].n_name = 0; 649 650 if (kvm_nlist(kd, nl) != 0) { 651 for (p = nl; p->n_type != 0; ++p) 652 ; 653 _kvm_err(kd, kd->program, 654 "%s: no such symbol", p->n_name); 655 return (0); 656 } 657 if (KREAD(kd, nl[0].n_value, &nprocs)) { 658 _kvm_err(kd, kd->program, "can't read nprocs"); 659 return (0); 660 } 661 size = nprocs * sizeof(struct kinfo_proc); 662 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 663 if (kd->procbase == 0) 664 return (0); 665 666 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 667 nl[2].n_value, nl[3].n_value, nprocs); 668 #ifdef notdef 669 size = nprocs * sizeof(struct kinfo_proc); 670 (void)realloc(kd->procbase, size); 671 #endif 672 } 673 *cnt = nprocs; 674 return (kd->procbase); 675 } 676 677 void 678 _kvm_freeprocs(kd) 679 kvm_t *kd; 680 { 681 if (kd->procbase) { 682 free(kd->procbase); 683 kd->procbase = 0; 684 } 685 } 686 687 void * 688 _kvm_realloc(kd, p, n) 689 kvm_t *kd; 690 void *p; 691 size_t n; 692 { 693 void *np = (void *)realloc(p, n); 694 695 if (np == 0) 696 _kvm_err(kd, kd->program, "out of memory"); 697 return (np); 698 } 699 700 #ifndef MAX 701 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 702 #endif 703 704 /* 705 * Read in an argument vector from the user address space of process p. 706 * addr if the user-space base address of narg null-terminated contiguous 707 * strings. This is used to read in both the command arguments and 708 * environment strings. Read at most maxcnt characters of strings. 709 */ 710 static char ** 711 kvm_argv(kd, p, addr, narg, maxcnt) 712 kvm_t *kd; 713 const struct proc *p; 714 u_long addr; 715 int narg; 716 int maxcnt; 717 { 718 char *np, *cp, *ep, *ap; 719 u_long oaddr = (u_long)~0L; 720 u_long len; 721 size_t cc; 722 char **argv; 723 724 /* 725 * Check that there aren't an unreasonable number of agruments, 726 * and that the address is in user space. 727 */ 728 if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva) 729 return (0); 730 731 if (kd->argv == 0) { 732 /* 733 * Try to avoid reallocs. 734 */ 735 kd->argc = MAX(narg + 1, 32); 736 kd->argv = (char **)_kvm_malloc(kd, kd->argc * 737 sizeof(*kd->argv)); 738 if (kd->argv == 0) 739 return (0); 740 } else if (narg + 1 > kd->argc) { 741 kd->argc = MAX(2 * kd->argc, narg + 1); 742 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 743 sizeof(*kd->argv)); 744 if (kd->argv == 0) 745 return (0); 746 } 747 if (kd->argspc == 0) { 748 kd->argspc = (char *)_kvm_malloc(kd, (size_t)kd->nbpg); 749 if (kd->argspc == 0) 750 return (0); 751 kd->arglen = kd->nbpg; 752 } 753 if (kd->argbuf == 0) { 754 kd->argbuf = (char *)_kvm_malloc(kd, (size_t)kd->nbpg); 755 if (kd->argbuf == 0) 756 return (0); 757 } 758 cc = sizeof(char *) * narg; 759 if (kvm_uread(kd, p, addr, (void *)kd->argv, cc) != cc) 760 return (0); 761 ap = np = kd->argspc; 762 argv = kd->argv; 763 len = 0; 764 /* 765 * Loop over pages, filling in the argument vector. 766 */ 767 while (argv < kd->argv + narg && *argv != 0) { 768 addr = (u_long)*argv & ~(kd->nbpg - 1); 769 if (addr != oaddr) { 770 if (kvm_uread(kd, p, addr, kd->argbuf, 771 (size_t)kd->nbpg) != kd->nbpg) 772 return (0); 773 oaddr = addr; 774 } 775 addr = (u_long)*argv & (kd->nbpg - 1); 776 cp = kd->argbuf + (size_t)addr; 777 cc = kd->nbpg - (size_t)addr; 778 if (maxcnt > 0 && cc > (size_t)(maxcnt - len)) 779 cc = (size_t)(maxcnt - len); 780 ep = memchr(cp, '\0', cc); 781 if (ep != 0) 782 cc = ep - cp + 1; 783 if (len + cc > kd->arglen) { 784 int off; 785 char **pp; 786 char *op = kd->argspc; 787 788 kd->arglen *= 2; 789 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 790 (size_t)kd->arglen); 791 if (kd->argspc == 0) 792 return (0); 793 /* 794 * Adjust argv pointers in case realloc moved 795 * the string space. 796 */ 797 off = kd->argspc - op; 798 for (pp = kd->argv; pp < argv; pp++) 799 *pp += off; 800 ap += off; 801 np += off; 802 } 803 memcpy(np, cp, cc); 804 np += cc; 805 len += cc; 806 if (ep != 0) { 807 *argv++ = ap; 808 ap = np; 809 } else 810 *argv += cc; 811 if (maxcnt > 0 && len >= maxcnt) { 812 /* 813 * We're stopping prematurely. Terminate the 814 * current string. 815 */ 816 if (ep == 0) { 817 *np = '\0'; 818 *argv++ = ap; 819 } 820 break; 821 } 822 } 823 /* Make sure argv is terminated. */ 824 *argv = 0; 825 return (kd->argv); 826 } 827 828 static void 829 ps_str_a(p, addr, n) 830 struct ps_strings *p; 831 u_long *addr; 832 int *n; 833 { 834 *addr = (u_long)p->ps_argvstr; 835 *n = p->ps_nargvstr; 836 } 837 838 static void 839 ps_str_e(p, addr, n) 840 struct ps_strings *p; 841 u_long *addr; 842 int *n; 843 { 844 *addr = (u_long)p->ps_envstr; 845 *n = p->ps_nenvstr; 846 } 847 848 /* 849 * Determine if the proc indicated by p is still active. 850 * This test is not 100% foolproof in theory, but chances of 851 * being wrong are very low. 852 */ 853 static int 854 proc_verify(kd, kernp, p) 855 kvm_t *kd; 856 u_long kernp; 857 const struct proc *p; 858 { 859 struct proc kernproc; 860 861 /* 862 * Just read in the whole proc. It's not that big relative 863 * to the cost of the read system call. 864 */ 865 if (kvm_read(kd, kernp, (void *)&kernproc, sizeof(kernproc)) != 866 sizeof(kernproc)) 867 return (0); 868 return (p->p_pid == kernproc.p_pid && 869 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB)); 870 } 871 872 static char ** 873 kvm_doargv(kd, kp, nchr, info) 874 kvm_t *kd; 875 const struct kinfo_proc *kp; 876 int nchr; 877 void (*info)(struct ps_strings *, u_long *, int *); 878 { 879 const struct proc *p = &kp->kp_proc; 880 char **ap; 881 u_long addr; 882 int cnt; 883 struct ps_strings arginfo; 884 885 /* 886 * Pointers are stored at the top of the user stack. 887 */ 888 if (p->p_stat == SZOMB) 889 return (0); 890 cnt = kvm_uread(kd, p, kd->usrstack - sizeof(arginfo), 891 (void *)&arginfo, sizeof(arginfo)); 892 if (cnt != sizeof(arginfo)) 893 return (0); 894 895 (*info)(&arginfo, &addr, &cnt); 896 if (cnt == 0) 897 return (0); 898 ap = kvm_argv(kd, p, addr, cnt, nchr); 899 /* 900 * For live kernels, make sure this process didn't go away. 901 */ 902 if (ap != 0 && ISALIVE(kd) && 903 !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p)) 904 ap = 0; 905 return (ap); 906 } 907 908 /* 909 * Get the command args. This code is now machine independent. 910 */ 911 char ** 912 kvm_getargv(kd, kp, nchr) 913 kvm_t *kd; 914 const struct kinfo_proc *kp; 915 int nchr; 916 { 917 return (kvm_doargv(kd, kp, nchr, ps_str_a)); 918 } 919 920 char ** 921 kvm_getenvv(kd, kp, nchr) 922 kvm_t *kd; 923 const struct kinfo_proc *kp; 924 int nchr; 925 { 926 return (kvm_doargv(kd, kp, nchr, ps_str_e)); 927 } 928 929 /* 930 * Read from user space. The user context is given by p. 931 */ 932 ssize_t 933 kvm_uread(kd, p, uva, buf, len) 934 kvm_t *kd; 935 const struct proc *p; 936 u_long uva; 937 char *buf; 938 size_t len; 939 { 940 char *cp; 941 942 cp = buf; 943 while (len > 0) { 944 size_t cc; 945 char *dp; 946 u_long cnt; 947 948 dp = _kvm_uread(kd, p, uva, &cnt); 949 if (dp == 0) { 950 _kvm_err(kd, 0, "invalid address (%x)", uva); 951 return (0); 952 } 953 cc = (size_t)MIN(cnt, len); 954 memcpy(cp, dp, cc); 955 cp += cc; 956 uva += cc; 957 len -= cc; 958 } 959 return (ssize_t)(cp - buf); 960 } 961