kvm_proc.c revision 103216
1/*- 2 * Copyright (c) 1989, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software developed by the Computer Systems 6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 7 * BG 91-66 and contributed to Berkeley. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * $FreeBSD: head/lib/libkvm/kvm_proc.c 103216 2002-09-11 08:13:56Z julian $ 38 */ 39 40#include <sys/cdefs.h> 41__FBSDID("$FreeBSD: head/lib/libkvm/kvm_proc.c 103216 2002-09-11 08:13:56Z julian $"); 42 43#if defined(LIBC_SCCS) && !defined(lint) 44static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"; 45#endif /* LIBC_SCCS and not lint */ 46 47/* 48 * Proc traversal interface for kvm. ps and w are (probably) the exclusive 49 * users of this code, so we've factored it out into a separate module. 50 * Thus, we keep this grunge out of the other kvm applications (i.e., 51 * most other applications are interested only in open/close/read/nlist). 52 */ 53 54#include <sys/param.h> 55#define _WANT_UCRED /* make ucred.h give us 'struct ucred' */ 56#include <sys/ucred.h> 57#include <sys/user.h> 58#include <sys/proc.h> 59#include <sys/exec.h> 60#include <sys/stat.h> 61#include <sys/ioctl.h> 62#include <sys/tty.h> 63#include <sys/file.h> 64#include <stdio.h> 65#include <stdlib.h> 66#include <unistd.h> 67#include <nlist.h> 68#include <kvm.h> 69 70#include <vm/vm.h> 71#include <vm/vm_param.h> 72#include <vm/swap_pager.h> 73 74#include <sys/sysctl.h> 75 76#include <limits.h> 77#include <memory.h> 78#include <paths.h> 79 80#include "kvm_private.h" 81 82#if used 83static char * 84kvm_readswap(kd, p, va, cnt) 85 kvm_t *kd; 86 const struct proc *p; 87 u_long va; 88 u_long *cnt; 89{ 90#ifdef __FreeBSD__ 91 /* XXX Stubbed out, our vm system is differnet */ 92 _kvm_err(kd, kd->program, "kvm_readswap not implemented"); 93 return(0); 94#endif /* __FreeBSD__ */ 95} 96#endif 97 98#define KREAD(kd, addr, obj) \ 99 (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) 100 101/* 102 * Read proc's from memory file into buffer bp, which has space to hold 103 * at most maxcnt procs. 104 */ 105static int 106kvm_proclist(kd, what, arg, p, bp, maxcnt) 107 kvm_t *kd; 108 int what, arg; 109 struct proc *p; 110 struct kinfo_proc *bp; 111 int maxcnt; 112{ 113 int cnt = 0; 114 struct kinfo_proc kinfo_proc, *kp; 115 struct pgrp pgrp; 116 struct session sess; 117 struct tty tty; 118 struct vmspace vmspace; 119 struct procsig procsig; 120 struct pstats pstats; 121 struct ucred ucred; 122 struct thread mtd; 123 struct proc proc; 124 struct proc pproc; 125 struct timeval tv; 126 127 kp = &kinfo_proc; 128 kp->ki_structsize = sizeof(kinfo_proc); 129 for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) { 130 memset(kp, 0, sizeof *kp); 131 if (KREAD(kd, (u_long)p, &proc)) { 132 _kvm_err(kd, kd->program, "can't read proc at %x", p); 133 return (-1); 134 } 135 if (proc.p_state != PRS_ZOMBIE) { 136 if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads), 137 &mtd)) { 138 _kvm_err(kd, kd->program, 139 "can't read thread at %x", 140 TAILQ_FIRST(&proc.p_threads)); 141 return (-1); 142 } 143 } 144 if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) { 145 kp->ki_ruid = ucred.cr_ruid; 146 kp->ki_svuid = ucred.cr_svuid; 147 kp->ki_rgid = ucred.cr_rgid; 148 kp->ki_svgid = ucred.cr_svgid; 149 kp->ki_ngroups = ucred.cr_ngroups; 150 bcopy(ucred.cr_groups, kp->ki_groups, 151 NGROUPS * sizeof(gid_t)); 152 kp->ki_uid = ucred.cr_uid; 153 } 154 155 switch(what) { 156 157 case KERN_PROC_PID: 158 if (proc.p_pid != (pid_t)arg) 159 continue; 160 break; 161 162 case KERN_PROC_UID: 163 if (kp->ki_uid != (uid_t)arg) 164 continue; 165 break; 166 167 case KERN_PROC_RUID: 168 if (kp->ki_ruid != (uid_t)arg) 169 continue; 170 break; 171 } 172 /* 173 * We're going to add another proc to the set. If this 174 * will overflow the buffer, assume the reason is because 175 * nprocs (or the proc list) is corrupt and declare an error. 176 */ 177 if (cnt >= maxcnt) { 178 _kvm_err(kd, kd->program, "nprocs corrupt"); 179 return (-1); 180 } 181 /* 182 * gather kinfo_proc 183 */ 184 kp->ki_paddr = p; 185 kp->ki_addr = proc.p_uarea; 186 /* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */ 187 kp->ki_args = proc.p_args; 188 kp->ki_tracep = proc.p_tracep; 189 kp->ki_textvp = proc.p_textvp; 190 kp->ki_fd = proc.p_fd; 191 kp->ki_vmspace = proc.p_vmspace; 192 if (proc.p_procsig != NULL) { 193 if (KREAD(kd, (u_long)proc.p_procsig, &procsig)) { 194 _kvm_err(kd, kd->program, 195 "can't read procsig at %x", proc.p_procsig); 196 return (-1); 197 } 198 kp->ki_sigignore = procsig.ps_sigignore; 199 kp->ki_sigcatch = procsig.ps_sigcatch; 200 } 201 if ((proc.p_sflag & PS_INMEM) && proc.p_stats != NULL) { 202 if (KREAD(kd, (u_long)proc.p_stats, &pstats)) { 203 _kvm_err(kd, kd->program, 204 "can't read stats at %x", proc.p_stats); 205 return (-1); 206 } 207 kp->ki_start = pstats.p_start; 208 kp->ki_rusage = pstats.p_ru; 209 kp->ki_childtime.tv_sec = pstats.p_cru.ru_utime.tv_sec + 210 pstats.p_cru.ru_stime.tv_sec; 211 kp->ki_childtime.tv_usec = 212 pstats.p_cru.ru_utime.tv_usec + 213 pstats.p_cru.ru_stime.tv_usec; 214 } 215 if (proc.p_oppid) 216 kp->ki_ppid = proc.p_oppid; 217 else if (proc.p_pptr) { 218 if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { 219 _kvm_err(kd, kd->program, 220 "can't read pproc at %x", proc.p_pptr); 221 return (-1); 222 } 223 kp->ki_ppid = pproc.p_pid; 224 } else 225 kp->ki_ppid = 0; 226 if (proc.p_pgrp == NULL) 227 goto nopgrp; 228 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 229 _kvm_err(kd, kd->program, "can't read pgrp at %x", 230 proc.p_pgrp); 231 return (-1); 232 } 233 kp->ki_pgid = pgrp.pg_id; 234 kp->ki_jobc = pgrp.pg_jobc; 235 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 236 _kvm_err(kd, kd->program, "can't read session at %x", 237 pgrp.pg_session); 238 return (-1); 239 } 240 kp->ki_sid = sess.s_sid; 241 (void)memcpy(kp->ki_login, sess.s_login, 242 sizeof(kp->ki_login)); 243 kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0; 244 if (sess.s_leader == p) 245 kp->ki_kiflag |= KI_SLEADER; 246 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 247 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 248 _kvm_err(kd, kd->program, 249 "can't read tty at %x", sess.s_ttyp); 250 return (-1); 251 } 252 kp->ki_tdev = tty.t_dev; 253 if (tty.t_pgrp != NULL) { 254 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 255 _kvm_err(kd, kd->program, 256 "can't read tpgrp at %x", 257 tty.t_pgrp); 258 return (-1); 259 } 260 kp->ki_tpgid = pgrp.pg_id; 261 } else 262 kp->ki_tpgid = -1; 263 if (tty.t_session != NULL) { 264 if (KREAD(kd, (u_long)tty.t_session, &sess)) { 265 _kvm_err(kd, kd->program, 266 "can't read session at %x", 267 tty.t_session); 268 return (-1); 269 } 270 kp->ki_tsid = sess.s_sid; 271 } 272 } else { 273nopgrp: 274 kp->ki_tdev = NODEV; 275 } 276 if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg) 277 (void)kvm_read(kd, (u_long)mtd.td_wmesg, 278 kp->ki_wmesg, WMESGLEN); 279 280#ifdef sparc 281 (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize, 282 (char *)&kp->ki_rssize, 283 sizeof(kp->ki_rssize)); 284 (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize, 285 (char *)&kp->ki_tsize, 286 3 * sizeof(kp->ki_rssize)); /* XXX */ 287#else 288 (void)kvm_read(kd, (u_long)proc.p_vmspace, 289 (char *)&vmspace, sizeof(vmspace)); 290 kp->ki_size = vmspace.vm_map.size; 291 kp->ki_rssize = vmspace.vm_swrss; /* XXX */ 292 kp->ki_swrss = vmspace.vm_swrss; 293 kp->ki_tsize = vmspace.vm_tsize; 294 kp->ki_dsize = vmspace.vm_dsize; 295 kp->ki_ssize = vmspace.vm_ssize; 296#endif 297 298 switch (what) { 299 300 case KERN_PROC_PGRP: 301 if (kp->ki_pgid != (pid_t)arg) 302 continue; 303 break; 304 305 case KERN_PROC_TTY: 306 if ((proc.p_flag & P_CONTROLT) == 0 || 307 kp->ki_tdev != (dev_t)arg) 308 continue; 309 break; 310 } 311 if (proc.p_comm[0] != 0) { 312 strncpy(kp->ki_comm, proc.p_comm, MAXCOMLEN); 313 kp->ki_comm[MAXCOMLEN] = 0; 314 } 315 if ((proc.p_state != PRS_ZOMBIE) && 316 (mtd.td_blocked != 0)) { 317 kp->ki_kiflag |= KI_MTXBLOCK; 318 if (mtd.td_mtxname) 319 (void)kvm_read(kd, 320 (u_long)mtd.td_mtxname, 321 kp->ki_mtxname, MTXNAMELEN); 322 kp->ki_mtxname[MTXNAMELEN] = 0; 323 } 324 bintime2timeval(&proc.p_runtime, &tv); 325 kp->ki_runtime = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec; 326 kp->ki_pid = proc.p_pid; 327 kp->ki_siglist = proc.p_siglist; 328 kp->ki_sigmask = proc.p_sigmask; 329 kp->ki_xstat = proc.p_xstat; 330 kp->ki_acflag = proc.p_acflag; 331 if (proc.p_state != PRS_ZOMBIE) { 332 kp->ki_pctcpu = proc.p_kse.ke_pctcpu; 333 kp->ki_estcpu = proc.p_ksegrp.kg_estcpu; 334 kp->ki_slptime = proc.p_ksegrp.kg_slptime; 335 kp->ki_swtime = proc.p_swtime; 336 kp->ki_flag = proc.p_flag; 337 kp->ki_sflag = proc.p_sflag; 338 kp->ki_wchan = mtd.td_wchan; 339 kp->ki_traceflag = proc.p_traceflag; 340 if (proc.p_state == PRS_NORMAL) { 341 if (TD_ON_RUNQ(&mtd) || 342 TD_CAN_RUN(&mtd) || 343 TD_IS_RUNNING(&mtd)) { 344 kp->ki_stat = SRUN; 345 } else if (mtd.td_state == TDS_INHIBITED) { 346 if (P_SHOULDSTOP(&proc)) { 347 kp->ki_stat = SSTOP; 348 } else if (TD_IS_SLEEPING(&mtd)) { 349 kp->ki_stat = SSLEEP; 350 } else if (TD_ON_MUTEX(&mtd)) { 351 kp->ki_stat = SMTX; 352 } else { 353 kp->ki_stat = SWAIT; 354 } 355 } 356 } else { 357 kp->ki_stat = SIDL; 358 } 359 kp->ki_pri.pri_class = proc.p_ksegrp.kg_pri_class; 360 kp->ki_pri.pri_user = proc.p_ksegrp.kg_user_pri; 361 kp->ki_pri.pri_level = mtd.td_priority; 362 kp->ki_pri.pri_native = mtd.td_base_pri; 363 kp->ki_nice = proc.p_ksegrp.kg_nice; 364 kp->ki_lock = proc.p_lock; 365 kp->ki_rqindex = proc.p_kse.ke_rqindex; 366 kp->ki_oncpu = proc.p_kse.ke_oncpu; 367 kp->ki_lastcpu = mtd.td_lastcpu; 368 } else { 369 kp->ki_stat = SZOMB; 370 } 371 bcopy(&kinfo_proc, bp, sizeof(kinfo_proc)); 372 ++bp; 373 ++cnt; 374 } 375 return (cnt); 376} 377 378/* 379 * Build proc info array by reading in proc list from a crash dump. 380 * Return number of procs read. maxcnt is the max we will read. 381 */ 382static int 383kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt) 384 kvm_t *kd; 385 int what, arg; 386 u_long a_allproc; 387 u_long a_zombproc; 388 int maxcnt; 389{ 390 struct kinfo_proc *bp = kd->procbase; 391 int acnt, zcnt; 392 struct proc *p; 393 394 if (KREAD(kd, a_allproc, &p)) { 395 _kvm_err(kd, kd->program, "cannot read allproc"); 396 return (-1); 397 } 398 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 399 if (acnt < 0) 400 return (acnt); 401 402 if (KREAD(kd, a_zombproc, &p)) { 403 _kvm_err(kd, kd->program, "cannot read zombproc"); 404 return (-1); 405 } 406 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 407 if (zcnt < 0) 408 zcnt = 0; 409 410 return (acnt + zcnt); 411} 412 413struct kinfo_proc * 414kvm_getprocs(kd, op, arg, cnt) 415 kvm_t *kd; 416 int op, arg; 417 int *cnt; 418{ 419 int mib[4], st, nprocs; 420 size_t size; 421 422 if (kd->procbase != 0) { 423 free((void *)kd->procbase); 424 /* 425 * Clear this pointer in case this call fails. Otherwise, 426 * kvm_close() will free it again. 427 */ 428 kd->procbase = 0; 429 } 430 if (ISALIVE(kd)) { 431 size = 0; 432 mib[0] = CTL_KERN; 433 mib[1] = KERN_PROC; 434 mib[2] = op; 435 mib[3] = arg; 436 st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, NULL, &size, NULL, 0); 437 if (st == -1) { 438 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 439 return (0); 440 } 441 /* 442 * We can't continue with a size of 0 because we pass 443 * it to realloc() (via _kvm_realloc()), and passing 0 444 * to realloc() results in undefined behavior. 445 */ 446 if (size == 0) { 447 /* 448 * XXX: We should probably return an invalid, 449 * but non-NULL, pointer here so any client 450 * program trying to dereference it will 451 * crash. However, _kvm_freeprocs() calls 452 * free() on kd->procbase if it isn't NULL, 453 * and free()'ing a junk pointer isn't good. 454 * Then again, _kvm_freeprocs() isn't used 455 * anywhere . . . 456 */ 457 kd->procbase = _kvm_malloc(kd, 1); 458 goto liveout; 459 } 460 do { 461 size += size / 10; 462 kd->procbase = (struct kinfo_proc *) 463 _kvm_realloc(kd, kd->procbase, size); 464 if (kd->procbase == 0) 465 return (0); 466 st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, 467 kd->procbase, &size, NULL, 0); 468 } while (st == -1 && errno == ENOMEM); 469 if (st == -1) { 470 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 471 return (0); 472 } 473 /* 474 * We have to check the size again because sysctl() 475 * may "round up" oldlenp if oldp is NULL; hence it 476 * might've told us that there was data to get when 477 * there really isn't any. 478 */ 479 if (size > 0 && 480 kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) { 481 _kvm_err(kd, kd->program, 482 "kinfo_proc size mismatch (expected %d, got %d)", 483 sizeof(struct kinfo_proc), 484 kd->procbase->ki_structsize); 485 return (0); 486 } 487liveout: 488 nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize; 489 } else { 490 struct nlist nl[4], *p; 491 492 nl[0].n_name = "_nprocs"; 493 nl[1].n_name = "_allproc"; 494 nl[2].n_name = "_zombproc"; 495 nl[3].n_name = 0; 496 497 if (kvm_nlist(kd, nl) != 0) { 498 for (p = nl; p->n_type != 0; ++p) 499 ; 500 _kvm_err(kd, kd->program, 501 "%s: no such symbol", p->n_name); 502 return (0); 503 } 504 if (KREAD(kd, nl[0].n_value, &nprocs)) { 505 _kvm_err(kd, kd->program, "can't read nprocs"); 506 return (0); 507 } 508 size = nprocs * sizeof(struct kinfo_proc); 509 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 510 if (kd->procbase == 0) 511 return (0); 512 513 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 514 nl[2].n_value, nprocs); 515#ifdef notdef 516 size = nprocs * sizeof(struct kinfo_proc); 517 (void)realloc(kd->procbase, size); 518#endif 519 } 520 *cnt = nprocs; 521 return (kd->procbase); 522} 523 524void 525_kvm_freeprocs(kd) 526 kvm_t *kd; 527{ 528 if (kd->procbase) { 529 free(kd->procbase); 530 kd->procbase = 0; 531 } 532} 533 534void * 535_kvm_realloc(kd, p, n) 536 kvm_t *kd; 537 void *p; 538 size_t n; 539{ 540 void *np = (void *)realloc(p, n); 541 542 if (np == 0) { 543 free(p); 544 _kvm_err(kd, kd->program, "out of memory"); 545 } 546 return (np); 547} 548 549#ifndef MAX 550#define MAX(a, b) ((a) > (b) ? (a) : (b)) 551#endif 552 553/* 554 * Read in an argument vector from the user address space of process kp. 555 * addr if the user-space base address of narg null-terminated contiguous 556 * strings. This is used to read in both the command arguments and 557 * environment strings. Read at most maxcnt characters of strings. 558 */ 559static char ** 560kvm_argv(kd, kp, addr, narg, maxcnt) 561 kvm_t *kd; 562 struct kinfo_proc *kp; 563 u_long addr; 564 int narg; 565 int maxcnt; 566{ 567 char *np, *cp, *ep, *ap; 568 u_long oaddr = -1; 569 int len, cc; 570 char **argv; 571 572 /* 573 * Check that there aren't an unreasonable number of agruments, 574 * and that the address is in user space. 575 */ 576 if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS) 577 return (0); 578 579 /* 580 * kd->argv : work space for fetching the strings from the target 581 * process's space, and is converted for returning to caller 582 */ 583 if (kd->argv == 0) { 584 /* 585 * Try to avoid reallocs. 586 */ 587 kd->argc = MAX(narg + 1, 32); 588 kd->argv = (char **)_kvm_malloc(kd, kd->argc * 589 sizeof(*kd->argv)); 590 if (kd->argv == 0) 591 return (0); 592 } else if (narg + 1 > kd->argc) { 593 kd->argc = MAX(2 * kd->argc, narg + 1); 594 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 595 sizeof(*kd->argv)); 596 if (kd->argv == 0) 597 return (0); 598 } 599 /* 600 * kd->argspc : returned to user, this is where the kd->argv 601 * arrays are left pointing to the collected strings. 602 */ 603 if (kd->argspc == 0) { 604 kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE); 605 if (kd->argspc == 0) 606 return (0); 607 kd->arglen = PAGE_SIZE; 608 } 609 /* 610 * kd->argbuf : used to pull in pages from the target process. 611 * the strings are copied out of here. 612 */ 613 if (kd->argbuf == 0) { 614 kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE); 615 if (kd->argbuf == 0) 616 return (0); 617 } 618 619 /* Pull in the target process'es argv vector */ 620 cc = sizeof(char *) * narg; 621 if (kvm_uread(kd, kp, addr, (char *)kd->argv, cc) != cc) 622 return (0); 623 /* 624 * ap : saved start address of string we're working on in kd->argspc 625 * np : pointer to next place to write in kd->argspc 626 * len: length of data in kd->argspc 627 * argv: pointer to the argv vector that we are hunting around the 628 * target process space for, and converting to addresses in 629 * our address space (kd->argspc). 630 */ 631 ap = np = kd->argspc; 632 argv = kd->argv; 633 len = 0; 634 /* 635 * Loop over pages, filling in the argument vector. 636 * Note that the argv strings could be pointing *anywhere* in 637 * the user address space and are no longer contiguous. 638 * Note that *argv is modified when we are going to fetch a string 639 * that crosses a page boundary. We copy the next part of the string 640 * into to "np" and eventually convert the pointer. 641 */ 642 while (argv < kd->argv + narg && *argv != 0) { 643 644 /* get the address that the current argv string is on */ 645 addr = (u_long)*argv & ~(PAGE_SIZE - 1); 646 647 /* is it the same page as the last one? */ 648 if (addr != oaddr) { 649 if (kvm_uread(kd, kp, addr, kd->argbuf, PAGE_SIZE) != 650 PAGE_SIZE) 651 return (0); 652 oaddr = addr; 653 } 654 655 /* offset within the page... kd->argbuf */ 656 addr = (u_long)*argv & (PAGE_SIZE - 1); 657 658 /* cp = start of string, cc = count of chars in this chunk */ 659 cp = kd->argbuf + addr; 660 cc = PAGE_SIZE - addr; 661 662 /* dont get more than asked for by user process */ 663 if (maxcnt > 0 && cc > maxcnt - len) 664 cc = maxcnt - len; 665 666 /* pointer to end of string if we found it in this page */ 667 ep = memchr(cp, '\0', cc); 668 if (ep != 0) 669 cc = ep - cp + 1; 670 /* 671 * at this point, cc is the count of the chars that we are 672 * going to retrieve this time. we may or may not have found 673 * the end of it. (ep points to the null if the end is known) 674 */ 675 676 /* will we exceed the malloc/realloced buffer? */ 677 if (len + cc > kd->arglen) { 678 int off; 679 char **pp; 680 char *op = kd->argspc; 681 682 kd->arglen *= 2; 683 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 684 kd->arglen); 685 if (kd->argspc == 0) 686 return (0); 687 /* 688 * Adjust argv pointers in case realloc moved 689 * the string space. 690 */ 691 off = kd->argspc - op; 692 for (pp = kd->argv; pp < argv; pp++) 693 *pp += off; 694 ap += off; 695 np += off; 696 } 697 /* np = where to put the next part of the string in kd->argspc*/ 698 /* np is kinda redundant.. could use "kd->argspc + len" */ 699 memcpy(np, cp, cc); 700 np += cc; /* inc counters */ 701 len += cc; 702 703 /* 704 * if end of string found, set the *argv pointer to the 705 * saved beginning of string, and advance. argv points to 706 * somewhere in kd->argv.. This is initially relative 707 * to the target process, but when we close it off, we set 708 * it to point in our address space. 709 */ 710 if (ep != 0) { 711 *argv++ = ap; 712 ap = np; 713 } else { 714 /* update the address relative to the target process */ 715 *argv += cc; 716 } 717 718 if (maxcnt > 0 && len >= maxcnt) { 719 /* 720 * We're stopping prematurely. Terminate the 721 * current string. 722 */ 723 if (ep == 0) { 724 *np = '\0'; 725 *argv++ = ap; 726 } 727 break; 728 } 729 } 730 /* Make sure argv is terminated. */ 731 *argv = 0; 732 return (kd->argv); 733} 734 735static void 736ps_str_a(p, addr, n) 737 struct ps_strings *p; 738 u_long *addr; 739 int *n; 740{ 741 *addr = (u_long)p->ps_argvstr; 742 *n = p->ps_nargvstr; 743} 744 745static void 746ps_str_e(p, addr, n) 747 struct ps_strings *p; 748 u_long *addr; 749 int *n; 750{ 751 *addr = (u_long)p->ps_envstr; 752 *n = p->ps_nenvstr; 753} 754 755/* 756 * Determine if the proc indicated by p is still active. 757 * This test is not 100% foolproof in theory, but chances of 758 * being wrong are very low. 759 */ 760static int 761proc_verify(curkp) 762 struct kinfo_proc *curkp; 763{ 764 struct kinfo_proc newkp; 765 int mib[4]; 766 size_t len; 767 768 mib[0] = CTL_KERN; 769 mib[1] = KERN_PROC; 770 mib[2] = KERN_PROC_PID; 771 mib[3] = curkp->ki_pid; 772 len = sizeof(newkp); 773 if (sysctl(mib, 4, &newkp, &len, NULL, 0) == -1) 774 return (0); 775 return (curkp->ki_pid == newkp.ki_pid && 776 (newkp.ki_stat != SZOMB || curkp->ki_stat == SZOMB)); 777} 778 779static char ** 780kvm_doargv(kd, kp, nchr, info) 781 kvm_t *kd; 782 struct kinfo_proc *kp; 783 int nchr; 784 void (*info)(struct ps_strings *, u_long *, int *); 785{ 786 char **ap; 787 u_long addr; 788 int cnt; 789 static struct ps_strings arginfo; 790 static u_long ps_strings; 791 size_t len; 792 793 if (ps_strings == NULL) { 794 len = sizeof(ps_strings); 795 if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL, 796 0) == -1) 797 ps_strings = PS_STRINGS; 798 } 799 800 /* 801 * Pointers are stored at the top of the user stack. 802 */ 803 if (kp->ki_stat == SZOMB || 804 kvm_uread(kd, kp, ps_strings, (char *)&arginfo, 805 sizeof(arginfo)) != sizeof(arginfo)) 806 return (0); 807 808 (*info)(&arginfo, &addr, &cnt); 809 if (cnt == 0) 810 return (0); 811 ap = kvm_argv(kd, kp, addr, cnt, nchr); 812 /* 813 * For live kernels, make sure this process didn't go away. 814 */ 815 if (ap != 0 && ISALIVE(kd) && !proc_verify(kp)) 816 ap = 0; 817 return (ap); 818} 819 820/* 821 * Get the command args. This code is now machine independent. 822 */ 823char ** 824kvm_getargv(kd, kp, nchr) 825 kvm_t *kd; 826 const struct kinfo_proc *kp; 827 int nchr; 828{ 829 int oid[4]; 830 int i; 831 size_t bufsz; 832 static unsigned long buflen; 833 static char *buf, *p; 834 static char **bufp; 835 static int argc; 836 837 if (!ISALIVE(kd)) { 838 _kvm_err(kd, kd->program, 839 "cannot read user space from dead kernel"); 840 return (0); 841 } 842 843 if (!buflen) { 844 bufsz = sizeof(buflen); 845 i = sysctlbyname("kern.ps_arg_cache_limit", 846 &buflen, &bufsz, NULL, 0); 847 if (i == -1) { 848 buflen = 0; 849 } else { 850 buf = malloc(buflen); 851 if (buf == NULL) 852 buflen = 0; 853 argc = 32; 854 bufp = malloc(sizeof(char *) * argc); 855 } 856 } 857 if (buf != NULL) { 858 oid[0] = CTL_KERN; 859 oid[1] = KERN_PROC; 860 oid[2] = KERN_PROC_ARGS; 861 oid[3] = kp->ki_pid; 862 bufsz = buflen; 863 i = sysctl(oid, 4, buf, &bufsz, 0, 0); 864 if (i == 0 && bufsz > 0) { 865 i = 0; 866 p = buf; 867 do { 868 bufp[i++] = p; 869 p += strlen(p) + 1; 870 if (i >= argc) { 871 argc += argc; 872 bufp = realloc(bufp, 873 sizeof(char *) * argc); 874 } 875 } while (p < buf + bufsz); 876 bufp[i++] = 0; 877 return (bufp); 878 } 879 } 880 if (kp->ki_flag & P_SYSTEM) 881 return (NULL); 882 return (kvm_doargv(kd, kp, nchr, ps_str_a)); 883} 884 885char ** 886kvm_getenvv(kd, kp, nchr) 887 kvm_t *kd; 888 const struct kinfo_proc *kp; 889 int nchr; 890{ 891 return (kvm_doargv(kd, kp, nchr, ps_str_e)); 892} 893 894/* 895 * Read from user space. The user context is given by p. 896 */ 897ssize_t 898kvm_uread(kd, kp, uva, buf, len) 899 kvm_t *kd; 900 struct kinfo_proc *kp; 901 u_long uva; 902 char *buf; 903 size_t len; 904{ 905 char *cp; 906 char procfile[MAXPATHLEN]; 907 ssize_t amount; 908 int fd; 909 910 if (!ISALIVE(kd)) { 911 _kvm_err(kd, kd->program, 912 "cannot read user space from dead kernel"); 913 return (0); 914 } 915 916 sprintf(procfile, "/proc/%d/mem", kp->ki_pid); 917 fd = open(procfile, O_RDONLY, 0); 918 if (fd < 0) { 919 _kvm_err(kd, kd->program, "cannot open %s", procfile); 920 close(fd); 921 return (0); 922 } 923 924 cp = buf; 925 while (len > 0) { 926 errno = 0; 927 if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) { 928 _kvm_err(kd, kd->program, "invalid address (%x) in %s", 929 uva, procfile); 930 break; 931 } 932 amount = read(fd, cp, len); 933 if (amount < 0) { 934 _kvm_syserr(kd, kd->program, "error reading %s", 935 procfile); 936 break; 937 } 938 if (amount == 0) { 939 _kvm_err(kd, kd->program, "EOF reading %s", procfile); 940 break; 941 } 942 cp += amount; 943 uva += amount; 944 len -= amount; 945 } 946 947 close(fd); 948 return ((ssize_t)(cp - buf)); 949} 950