kvm_proc.c revision 12885
1106184Smarcel/*- 2139601Smarcel * Copyright (c) 1989, 1992, 1993 3106184Smarcel * The Regents of the University of California. All rights reserved. 4106184Smarcel * 5106184Smarcel * This code is derived from software developed by the Computer Systems 6106184Smarcel * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 7106184Smarcel * BG 91-66 and contributed to Berkeley. 8106184Smarcel * 9106184Smarcel * Redistribution and use in source and binary forms, with or without 10106184Smarcel * modification, are permitted provided that the following conditions 11106184Smarcel * are met: 12106184Smarcel * 1. Redistributions of source code must retain the above copyright 13106184Smarcel * notice, this list of conditions and the following disclaimer. 14106184Smarcel * 2. Redistributions in binary form must reproduce the above copyright 15106184Smarcel * notice, this list of conditions and the following disclaimer in the 16106184Smarcel * documentation and/or other materials provided with the distribution. 17106184Smarcel * 3. All advertising materials mentioning features or use of this software 18106184Smarcel * must display the following acknowledgement: 19106184Smarcel * This product includes software developed by the University of 20106184Smarcel * California, Berkeley and its contributors. 21106184Smarcel * 4. Neither the name of the University nor the names of its contributors 22106184Smarcel * may be used to endorse or promote products derived from this software 23106184Smarcel * without specific prior written permission. 24106184Smarcel * 25106184Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26106184Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27106184Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28106184Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29106184Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30106184Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31106184Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32106184Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33106184Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34106184Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35106184Smarcel * SUCH DAMAGE. 36106184Smarcel */ 37106184Smarcel 38106184Smarcel#if defined(LIBC_SCCS) && !defined(lint) 39139601Smarcelstatic char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"; 40106184Smarcel#endif /* LIBC_SCCS and not lint */ 41106184Smarcel 42106184Smarcel/* 43106184Smarcel * Proc traversal interface for kvm. ps and w are (probably) the exclusive 44106184Smarcel * users of this code, so we've factored it out into a separate module. 45106184Smarcel * Thus, we keep this grunge out of the other kvm applications (i.e., 46106184Smarcel * most other applications are interested only in open/close/read/nlist). 47106184Smarcel */ 48106184Smarcel 49106184Smarcel#include <sys/param.h> 50106184Smarcel#include <sys/user.h> 51106184Smarcel#include <sys/proc.h> 52106184Smarcel#include <sys/exec.h> 53106184Smarcel#include <sys/stat.h> 54106184Smarcel#include <sys/ioctl.h> 55106184Smarcel#include <sys/tty.h> 56#include <sys/file.h> 57#include <unistd.h> 58#include <nlist.h> 59#include <kvm.h> 60 61#include <vm/vm.h> 62#include <vm/vm_param.h> 63#include <vm/swap_pager.h> 64 65#include <sys/sysctl.h> 66 67#include <limits.h> 68#include <memory.h> 69#include <db.h> 70#include <paths.h> 71 72#include "kvm_private.h" 73 74static char * 75kvm_readswap(kd, p, va, cnt) 76 kvm_t *kd; 77 const struct proc *p; 78 u_long va; 79 u_long *cnt; 80{ 81#ifdef __FreeBSD__ 82 /* XXX Stubbed out, our vm system is differnet */ 83 _kvm_err(kd, kd->program, "kvm_readswap not implemented"); 84 return(0); 85#endif /* __FreeBSD__ */ 86} 87 88#define KREAD(kd, addr, obj) \ 89 (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) 90 91/* 92 * Read proc's from memory file into buffer bp, which has space to hold 93 * at most maxcnt procs. 94 */ 95static int 96kvm_proclist(kd, what, arg, p, bp, maxcnt) 97 kvm_t *kd; 98 int what, arg; 99 struct proc *p; 100 struct kinfo_proc *bp; 101 int maxcnt; 102{ 103 register int cnt = 0; 104 struct eproc eproc; 105 struct pgrp pgrp; 106 struct session sess; 107 struct tty tty; 108 struct proc proc; 109 110 for (; cnt < maxcnt && p != NULL; p = proc.p_next) { 111 if (KREAD(kd, (u_long)p, &proc)) { 112 _kvm_err(kd, kd->program, "can't read proc at %x", p); 113 return (-1); 114 } 115 if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0) 116 KREAD(kd, (u_long)eproc.e_pcred.pc_ucred, 117 &eproc.e_ucred); 118 119 switch(what) { 120 121 case KERN_PROC_PID: 122 if (proc.p_pid != (pid_t)arg) 123 continue; 124 break; 125 126 case KERN_PROC_UID: 127 if (eproc.e_ucred.cr_uid != (uid_t)arg) 128 continue; 129 break; 130 131 case KERN_PROC_RUID: 132 if (eproc.e_pcred.p_ruid != (uid_t)arg) 133 continue; 134 break; 135 } 136 /* 137 * We're going to add another proc to the set. If this 138 * will overflow the buffer, assume the reason is because 139 * nprocs (or the proc list) is corrupt and declare an error. 140 */ 141 if (cnt >= maxcnt) { 142 _kvm_err(kd, kd->program, "nprocs corrupt"); 143 return (-1); 144 } 145 /* 146 * gather eproc 147 */ 148 eproc.e_paddr = p; 149 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 150 _kvm_err(kd, kd->program, "can't read pgrp at %x", 151 proc.p_pgrp); 152 return (-1); 153 } 154 eproc.e_sess = pgrp.pg_session; 155 eproc.e_pgid = pgrp.pg_id; 156 eproc.e_jobc = pgrp.pg_jobc; 157 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 158 _kvm_err(kd, kd->program, "can't read session at %x", 159 pgrp.pg_session); 160 return (-1); 161 } 162 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 163 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 164 _kvm_err(kd, kd->program, 165 "can't read tty at %x", sess.s_ttyp); 166 return (-1); 167 } 168 eproc.e_tdev = tty.t_dev; 169 eproc.e_tsess = tty.t_session; 170 if (tty.t_pgrp != NULL) { 171 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 172 _kvm_err(kd, kd->program, 173 "can't read tpgrp at &x", 174 tty.t_pgrp); 175 return (-1); 176 } 177 eproc.e_tpgid = pgrp.pg_id; 178 } else 179 eproc.e_tpgid = -1; 180 } else 181 eproc.e_tdev = NODEV; 182 eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0; 183 if (sess.s_leader == p) 184 eproc.e_flag |= EPROC_SLEADER; 185 if (proc.p_wmesg) 186 (void)kvm_read(kd, (u_long)proc.p_wmesg, 187 eproc.e_wmesg, WMESGLEN); 188 189#ifdef sparc 190 (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize, 191 (char *)&eproc.e_vm.vm_rssize, 192 sizeof(eproc.e_vm.vm_rssize)); 193 (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize, 194 (char *)&eproc.e_vm.vm_tsize, 195 3 * sizeof(eproc.e_vm.vm_rssize)); /* XXX */ 196#else 197 (void)kvm_read(kd, (u_long)proc.p_vmspace, 198 (char *)&eproc.e_vm, sizeof(eproc.e_vm)); 199#endif 200 eproc.e_xsize = eproc.e_xrssize = 0; 201 eproc.e_xccount = eproc.e_xswrss = 0; 202 203 switch (what) { 204 205 case KERN_PROC_PGRP: 206 if (eproc.e_pgid != (pid_t)arg) 207 continue; 208 break; 209 210 case KERN_PROC_TTY: 211 if ((proc.p_flag & P_CONTROLT) == 0 || 212 eproc.e_tdev != (dev_t)arg) 213 continue; 214 break; 215 } 216 bcopy(&proc, &bp->kp_proc, sizeof(proc)); 217 bcopy(&eproc, &bp->kp_eproc, sizeof(eproc)); 218 ++bp; 219 ++cnt; 220 } 221 return (cnt); 222} 223 224/* 225 * Build proc info array by reading in proc list from a crash dump. 226 * Return number of procs read. maxcnt is the max we will read. 227 */ 228static int 229kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt) 230 kvm_t *kd; 231 int what, arg; 232 u_long a_allproc; 233 u_long a_zombproc; 234 int maxcnt; 235{ 236 register struct kinfo_proc *bp = kd->procbase; 237 register int acnt, zcnt; 238 struct proc *p; 239 240 if (KREAD(kd, a_allproc, &p)) { 241 _kvm_err(kd, kd->program, "cannot read allproc"); 242 return (-1); 243 } 244 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 245 if (acnt < 0) 246 return (acnt); 247 248 if (KREAD(kd, a_zombproc, &p)) { 249 _kvm_err(kd, kd->program, "cannot read zombproc"); 250 return (-1); 251 } 252 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 253 if (zcnt < 0) 254 zcnt = 0; 255 256 return (acnt + zcnt); 257} 258 259struct kinfo_proc * 260kvm_getprocs(kd, op, arg, cnt) 261 kvm_t *kd; 262 int op, arg; 263 int *cnt; 264{ 265 int mib[4], size, st, nprocs; 266 267 if (kd->procbase != 0) { 268 free((void *)kd->procbase); 269 /* 270 * Clear this pointer in case this call fails. Otherwise, 271 * kvm_close() will free it again. 272 */ 273 kd->procbase = 0; 274 } 275 if (ISALIVE(kd)) { 276 size = 0; 277 mib[0] = CTL_KERN; 278 mib[1] = KERN_PROC; 279 mib[2] = op; 280 mib[3] = arg; 281 st = sysctl(mib, 4, NULL, &size, NULL, 0); 282 if (st == -1) { 283 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 284 return (0); 285 } 286 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 287 if (kd->procbase == 0) 288 return (0); 289 st = sysctl(mib, 4, kd->procbase, &size, NULL, 0); 290 if (st == -1) { 291 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 292 return (0); 293 } 294 if (size % sizeof(struct kinfo_proc) != 0) { 295 _kvm_err(kd, kd->program, 296 "proc size mismatch (%d total, %d chunks)", 297 size, sizeof(struct kinfo_proc)); 298 return (0); 299 } 300 nprocs = size / sizeof(struct kinfo_proc); 301 } else { 302 struct nlist nl[4], *p; 303 304 nl[0].n_name = "_nprocs"; 305 nl[1].n_name = "_allproc"; 306 nl[2].n_name = "_zombproc"; 307 nl[3].n_name = 0; 308 309 if (kvm_nlist(kd, nl) != 0) { 310 for (p = nl; p->n_type != 0; ++p) 311 ; 312 _kvm_err(kd, kd->program, 313 "%s: no such symbol", p->n_name); 314 return (0); 315 } 316 if (KREAD(kd, nl[0].n_value, &nprocs)) { 317 _kvm_err(kd, kd->program, "can't read nprocs"); 318 return (0); 319 } 320 size = nprocs * sizeof(struct kinfo_proc); 321 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 322 if (kd->procbase == 0) 323 return (0); 324 325 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 326 nl[2].n_value, nprocs); 327#ifdef notdef 328 size = nprocs * sizeof(struct kinfo_proc); 329 (void)realloc(kd->procbase, size); 330#endif 331 } 332 *cnt = nprocs; 333 return (kd->procbase); 334} 335 336void 337_kvm_freeprocs(kd) 338 kvm_t *kd; 339{ 340 if (kd->procbase) { 341 free(kd->procbase); 342 kd->procbase = 0; 343 } 344} 345 346void * 347_kvm_realloc(kd, p, n) 348 kvm_t *kd; 349 void *p; 350 size_t n; 351{ 352 void *np = (void *)realloc(p, n); 353 354 if (np == 0) 355 _kvm_err(kd, kd->program, "out of memory"); 356 return (np); 357} 358 359#ifndef MAX 360#define MAX(a, b) ((a) > (b) ? (a) : (b)) 361#endif 362 363/* 364 * Read in an argument vector from the user address space of process p. 365 * addr if the user-space base address of narg null-terminated contiguous 366 * strings. This is used to read in both the command arguments and 367 * environment strings. Read at most maxcnt characters of strings. 368 */ 369static char ** 370kvm_argv(kd, p, addr, narg, maxcnt) 371 kvm_t *kd; 372 const struct proc *p; 373 register u_long addr; 374 register int narg; 375 register int maxcnt; 376{ 377 register char *np, *cp, *ep, *ap; 378 register u_long oaddr = -1; 379 register int len, cc; 380 register char **argv; 381 382 /* 383 * Check that there aren't an unreasonable number of agruments, 384 * and that the address is in user space. 385 */ 386 if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS) 387 return (0); 388 389 /* 390 * kd->argv : work space for fetching the strings from the target 391 * process's space, and is converted for returning to caller 392 */ 393 if (kd->argv == 0) { 394 /* 395 * Try to avoid reallocs. 396 */ 397 kd->argc = MAX(narg + 1, 32); 398 kd->argv = (char **)_kvm_malloc(kd, kd->argc * 399 sizeof(*kd->argv)); 400 if (kd->argv == 0) 401 return (0); 402 } else if (narg + 1 > kd->argc) { 403 kd->argc = MAX(2 * kd->argc, narg + 1); 404 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 405 sizeof(*kd->argv)); 406 if (kd->argv == 0) 407 return (0); 408 } 409 /* 410 * kd->argspc : returned to user, this is where the kd->argv 411 * arrays are left pointing to the collected strings. 412 */ 413 if (kd->argspc == 0) { 414 kd->argspc = (char *)_kvm_malloc(kd, NBPG); 415 if (kd->argspc == 0) 416 return (0); 417 kd->arglen = NBPG; 418 } 419 /* 420 * kd->argbuf : used to pull in pages from the target process. 421 * the strings are copied out of here. 422 */ 423 if (kd->argbuf == 0) { 424 kd->argbuf = (char *)_kvm_malloc(kd, NBPG); 425 if (kd->argbuf == 0) 426 return (0); 427 } 428 429 /* Pull in the target process'es argv vector */ 430 cc = sizeof(char *) * narg; 431 if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc) 432 return (0); 433 /* 434 * ap : saved start address of string we're working on in kd->argspc 435 * np : pointer to next place to write in kd->argspc 436 * len: length of data in kd->argspc 437 * argv: pointer to the argv vector that we are hunting around the 438 * target process space for, and converting to addresses in 439 * our address space (kd->argspc). 440 */ 441 ap = np = kd->argspc; 442 argv = kd->argv; 443 len = 0; 444 /* 445 * Loop over pages, filling in the argument vector. 446 * Note that the argv strings could be pointing *anywhere* in 447 * the user address space and are no longer contiguous. 448 * Note that *argv is modified when we are going to fetch a string 449 * that crosses a page boundary. We copy the next part of the string 450 * into to "np" and eventually convert the pointer. 451 */ 452 while (argv < kd->argv + narg && *argv != 0) { 453 454 /* get the address that the current argv string is on */ 455 addr = (u_long)*argv & ~(NBPG - 1); 456 457 /* is it the same page as the last one? */ 458 if (addr != oaddr) { 459 if (kvm_uread(kd, p, addr, kd->argbuf, NBPG) != 460 NBPG) 461 return (0); 462 oaddr = addr; 463 } 464 465 /* offset within the page... kd->argbuf */ 466 addr = (u_long)*argv & (NBPG - 1); 467 468 /* cp = start of string, cc = count of chars in this chunk */ 469 cp = kd->argbuf + addr; 470 cc = NBPG - addr; 471 472 /* dont get more than asked for by user process */ 473 if (maxcnt > 0 && cc > maxcnt - len) 474 cc = maxcnt - len; 475 476 /* pointer to end of string if we found it in this page */ 477 ep = memchr(cp, '\0', cc); 478 if (ep != 0) 479 cc = ep - cp + 1; 480 /* 481 * at this point, cc is the count of the chars that we are 482 * going to retrieve this time. we may or may not have found 483 * the end of it. (ep points to the null if the end is known) 484 */ 485 486 /* will we exceed the malloc/realloced buffer? */ 487 if (len + cc > kd->arglen) { 488 register int off; 489 register char **pp; 490 register char *op = kd->argspc; 491 492 kd->arglen *= 2; 493 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 494 kd->arglen); 495 if (kd->argspc == 0) 496 return (0); 497 /* 498 * Adjust argv pointers in case realloc moved 499 * the string space. 500 */ 501 off = kd->argspc - op; 502 for (pp = kd->argv; pp < argv; pp++) 503 *pp += off; 504 ap += off; 505 np += off; 506 } 507 /* np = where to put the next part of the string in kd->argspc*/ 508 /* np is kinda redundant.. could use "kd->argspc + len" */ 509 memcpy(np, cp, cc); 510 np += cc; /* inc counters */ 511 len += cc; 512 513 /* 514 * if end of string found, set the *argv pointer to the 515 * saved beginning of string, and advance. argv points to 516 * somewhere in kd->argv.. This is initially relative 517 * to the target process, but when we close it off, we set 518 * it to point in our address space. 519 */ 520 if (ep != 0) { 521 *argv++ = ap; 522 ap = np; 523 } else { 524 /* update the address relative to the target process */ 525 *argv += cc; 526 } 527 528 if (maxcnt > 0 && len >= maxcnt) { 529 /* 530 * We're stopping prematurely. Terminate the 531 * current string. 532 */ 533 if (ep == 0) { 534 *np = '\0'; 535 *argv++ = ap; 536 } 537 break; 538 } 539 } 540 /* Make sure argv is terminated. */ 541 *argv = 0; 542 return (kd->argv); 543} 544 545static void 546ps_str_a(p, addr, n) 547 struct ps_strings *p; 548 u_long *addr; 549 int *n; 550{ 551 *addr = (u_long)p->ps_argvstr; 552 *n = p->ps_nargvstr; 553} 554 555static void 556ps_str_e(p, addr, n) 557 struct ps_strings *p; 558 u_long *addr; 559 int *n; 560{ 561 *addr = (u_long)p->ps_envstr; 562 *n = p->ps_nenvstr; 563} 564 565/* 566 * Determine if the proc indicated by p is still active. 567 * This test is not 100% foolproof in theory, but chances of 568 * being wrong are very low. 569 */ 570static int 571proc_verify(kd, kernp, p) 572 kvm_t *kd; 573 u_long kernp; 574 const struct proc *p; 575{ 576 struct proc kernproc; 577 578 /* 579 * Just read in the whole proc. It's not that big relative 580 * to the cost of the read system call. 581 */ 582 if (kvm_read(kd, kernp, (char *)&kernproc, sizeof(kernproc)) != 583 sizeof(kernproc)) 584 return (0); 585 return (p->p_pid == kernproc.p_pid && 586 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB)); 587} 588 589static char ** 590kvm_doargv(kd, kp, nchr, info) 591 kvm_t *kd; 592 const struct kinfo_proc *kp; 593 int nchr; 594 void (*info)(struct ps_strings *, u_long *, int *); 595{ 596 register const struct proc *p = &kp->kp_proc; 597 register char **ap; 598 u_long addr; 599 int cnt; 600 struct ps_strings arginfo; 601 602 /* 603 * Pointers are stored at the top of the user stack. 604 */ 605 if (p->p_stat == SZOMB || 606 kvm_uread(kd, p, USRSTACK - sizeof(arginfo), (char *)&arginfo, 607 sizeof(arginfo)) != sizeof(arginfo)) 608 return (0); 609 610 (*info)(&arginfo, &addr, &cnt); 611 if (cnt == 0) 612 return (0); 613 ap = kvm_argv(kd, p, addr, cnt, nchr); 614 /* 615 * For live kernels, make sure this process didn't go away. 616 */ 617 if (ap != 0 && ISALIVE(kd) && 618 !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p)) 619 ap = 0; 620 return (ap); 621} 622 623/* 624 * Get the command args. This code is now machine independent. 625 */ 626char ** 627kvm_getargv(kd, kp, nchr) 628 kvm_t *kd; 629 const struct kinfo_proc *kp; 630 int nchr; 631{ 632 return (kvm_doargv(kd, kp, nchr, ps_str_a)); 633} 634 635char ** 636kvm_getenvv(kd, kp, nchr) 637 kvm_t *kd; 638 const struct kinfo_proc *kp; 639 int nchr; 640{ 641 return (kvm_doargv(kd, kp, nchr, ps_str_e)); 642} 643 644/* 645 * Read from user space. The user context is given by p. 646 */ 647ssize_t 648kvm_uread(kd, p, uva, buf, len) 649 kvm_t *kd; 650 register struct proc *p; 651 register u_long uva; 652 register char *buf; 653 register size_t len; 654{ 655 register char *cp; 656 char procfile[MAXPATHLEN]; 657 ssize_t amount; 658 int fd; 659 660 cp = buf; 661 662 sprintf(procfile, "/proc/%d/mem", p->p_pid); 663 fd = open(procfile, O_RDONLY, 0); 664 665 if (fd < 0) { 666 _kvm_err(kd, kd->program, "cannot open %s", procfile); 667 close(fd); 668 return (0); 669 } 670 671 672 while (len > 0) { 673 if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) { 674 _kvm_err(kd, kd->program, "invalid address (%x) in %s", uva, procfile); 675 break; 676 } 677 amount = read(fd, cp, len); 678 if (amount < 0) { 679 _kvm_err(kd, kd->program, "error reading %s", procfile); 680 break; 681 } 682 cp += amount; 683 uva += amount; 684 len -= amount; 685 } 686 687 close(fd); 688 return (ssize_t)(cp - buf); 689} 690