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