1 /* $NetBSD: kvm_proc.c,v 1.61 2006/02/16 20:48:42 christos 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. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 */ 71 72 #include <sys/cdefs.h> 73 #if defined(LIBC_SCCS) && !defined(lint) 74 #if 0 75 static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"; 76 #else 77 __RCSID("$NetBSD: kvm_proc.c,v 1.61 2006/02/16 20:48:42 christos Exp $"); 78 #endif 79 #endif /* LIBC_SCCS and not lint */ 80 81 /* 82 * Proc traversal interface for kvm. ps and w are (probably) the exclusive 83 * users of this code, so we've factored it out into a separate module. 84 * Thus, we keep this grunge out of the other kvm applications (i.e., 85 * most other applications are interested only in open/close/read/nlist). 86 */ 87 88 #include <sys/param.h> 89 #include <sys/user.h> 90 #include <sys/lwp.h> 91 #include <sys/proc.h> 92 #include <sys/exec.h> 93 #include <sys/stat.h> 94 #include <sys/ioctl.h> 95 #include <sys/tty.h> 96 #include <stdlib.h> 97 #include <stddef.h> 98 #include <string.h> 99 #include <unistd.h> 100 #include <nlist.h> 101 #include <kvm.h> 102 103 #include <uvm/uvm_extern.h> 104 #include <uvm/uvm_amap.h> 105 106 #include <sys/sysctl.h> 107 108 #include <limits.h> 109 #include <db.h> 110 #include <paths.h> 111 112 #include "kvm_private.h" 113 114 /* 115 * Common info from kinfo_proc and kinfo_proc2 used by helper routines. 116 */ 117 struct miniproc { 118 struct vmspace *p_vmspace; 119 char p_stat; 120 struct proc *p_paddr; 121 pid_t p_pid; 122 }; 123 124 /* 125 * Convert from struct proc and kinfo_proc{,2} to miniproc. 126 */ 127 #define PTOMINI(kp, p) \ 128 do { \ 129 (p)->p_stat = (kp)->p_stat; \ 130 (p)->p_pid = (kp)->p_pid; \ 131 (p)->p_paddr = NULL; \ 132 (p)->p_vmspace = (kp)->p_vmspace; \ 133 } while (/*CONSTCOND*/0); 134 135 #define KPTOMINI(kp, p) \ 136 do { \ 137 (p)->p_stat = (kp)->kp_proc.p_stat; \ 138 (p)->p_pid = (kp)->kp_proc.p_pid; \ 139 (p)->p_paddr = (kp)->kp_eproc.e_paddr; \ 140 (p)->p_vmspace = (kp)->kp_proc.p_vmspace; \ 141 } while (/*CONSTCOND*/0); 142 143 #define KP2TOMINI(kp, p) \ 144 do { \ 145 (p)->p_stat = (kp)->p_stat; \ 146 (p)->p_pid = (kp)->p_pid; \ 147 (p)->p_paddr = (void *)(long)(kp)->p_paddr; \ 148 (p)->p_vmspace = (void *)(long)(kp)->p_vmspace; \ 149 } while (/*CONSTCOND*/0); 150 151 152 #define KREAD(kd, addr, obj) \ 153 (kvm_read(kd, addr, (obj), sizeof(*obj)) != sizeof(*obj)) 154 155 /* XXX: What uses these two functions? */ 156 char *_kvm_uread __P((kvm_t *, const struct proc *, u_long, 157 u_long *)); 158 ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *, 159 size_t)); 160 161 static char *_kvm_ureadm __P((kvm_t *, const struct miniproc *, u_long, 162 u_long *)); 163 static ssize_t kvm_ureadm __P((kvm_t *, const struct miniproc *, u_long, 164 char *, size_t)); 165 166 static char **kvm_argv __P((kvm_t *, const struct miniproc *, u_long, int, 167 int)); 168 static int kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, int)); 169 static char **kvm_doargv __P((kvm_t *, const struct miniproc *, int, 170 void (*)(struct ps_strings *, u_long *, int *))); 171 static char **kvm_doargv2 __P((kvm_t *, pid_t, int, int)); 172 static int kvm_proclist __P((kvm_t *, int, int, struct proc *, 173 struct kinfo_proc *, int)); 174 static int proc_verify __P((kvm_t *, u_long, const struct miniproc *)); 175 static void ps_str_a __P((struct ps_strings *, u_long *, int *)); 176 static void ps_str_e __P((struct ps_strings *, u_long *, int *)); 177 178 179 static char * 180 _kvm_ureadm(kd, p, va, cnt) 181 kvm_t *kd; 182 const struct miniproc *p; 183 u_long va; 184 u_long *cnt; 185 { 186 int true = 1; 187 u_long addr, head; 188 u_long offset; 189 struct vm_map_entry vme; 190 struct vm_amap amap; 191 struct vm_anon *anonp, anon; 192 struct vm_page pg; 193 u_long slot; 194 195 if (kd->swapspc == NULL) { 196 kd->swapspc = _kvm_malloc(kd, (size_t)kd->nbpg); 197 if (kd->swapspc == NULL) 198 return (NULL); 199 } 200 201 /* 202 * Look through the address map for the memory object 203 * that corresponds to the given virtual address. 204 * The header just has the entire valid range. 205 */ 206 head = (u_long)&p->p_vmspace->vm_map.header; 207 addr = head; 208 while (true) { 209 if (KREAD(kd, addr, &vme)) 210 return (NULL); 211 212 if (va >= vme.start && va < vme.end && 213 vme.aref.ar_amap != NULL) 214 break; 215 216 addr = (u_long)vme.next; 217 if (addr == head) 218 return (NULL); 219 } 220 221 /* 222 * we found the map entry, now to find the object... 223 */ 224 if (vme.aref.ar_amap == NULL) 225 return (NULL); 226 227 addr = (u_long)vme.aref.ar_amap; 228 if (KREAD(kd, addr, &amap)) 229 return (NULL); 230 231 offset = va - vme.start; 232 slot = offset / kd->nbpg + vme.aref.ar_pageoff; 233 /* sanity-check slot number */ 234 if (slot > amap.am_nslot) 235 return (NULL); 236 237 addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp); 238 if (KREAD(kd, addr, &anonp)) 239 return (NULL); 240 241 addr = (u_long)anonp; 242 if (KREAD(kd, addr, &anon)) 243 return (NULL); 244 245 addr = (u_long)anon.an_page; 246 if (addr) { 247 if (KREAD(kd, addr, &pg)) 248 return (NULL); 249 250 if (pread(kd->pmfd, kd->swapspc, (size_t)kd->nbpg, 251 (off_t)pg.phys_addr) != kd->nbpg) 252 return (NULL); 253 } else { 254 if (kd->swfd < 0 || 255 pread(kd->swfd, kd->swapspc, (size_t)kd->nbpg, 256 (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg) 257 return (NULL); 258 } 259 260 /* Found the page. */ 261 offset %= kd->nbpg; 262 *cnt = kd->nbpg - offset; 263 return (&kd->swapspc[(size_t)offset]); 264 } 265 266 char * 267 _kvm_uread(kd, p, va, cnt) 268 kvm_t *kd; 269 const struct proc *p; 270 u_long va; 271 u_long *cnt; 272 { 273 struct miniproc mp; 274 275 PTOMINI(p, &mp); 276 return (_kvm_ureadm(kd, &mp, va, cnt)); 277 } 278 279 /* 280 * Read proc's from memory file into buffer bp, which has space to hold 281 * at most maxcnt procs. 282 */ 283 static int 284 kvm_proclist(kd, what, arg, p, bp, maxcnt) 285 kvm_t *kd; 286 int what, arg; 287 struct proc *p; 288 struct kinfo_proc *bp; 289 int maxcnt; 290 { 291 int cnt = 0; 292 int nlwps; 293 struct kinfo_lwp *kl; 294 struct eproc eproc; 295 struct pgrp pgrp; 296 struct session sess; 297 struct tty tty; 298 struct proc proc; 299 300 for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) { 301 if (KREAD(kd, (u_long)p, &proc)) { 302 _kvm_err(kd, kd->program, "can't read proc at %p", p); 303 return (-1); 304 } 305 if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0) 306 if (KREAD(kd, (u_long)eproc.e_pcred.pc_ucred, 307 &eproc.e_ucred)) { 308 _kvm_err(kd, kd->program, 309 "can't read proc credentials at %p", p); 310 return (-1); 311 } 312 313 switch (what) { 314 315 case KERN_PROC_PID: 316 if (proc.p_pid != (pid_t)arg) 317 continue; 318 break; 319 320 case KERN_PROC_UID: 321 if (eproc.e_ucred.cr_uid != (uid_t)arg) 322 continue; 323 break; 324 325 case KERN_PROC_RUID: 326 if (eproc.e_pcred.p_ruid != (uid_t)arg) 327 continue; 328 break; 329 } 330 /* 331 * We're going to add another proc to the set. If this 332 * will overflow the buffer, assume the reason is because 333 * nprocs (or the proc list) is corrupt and declare an error. 334 */ 335 if (cnt >= maxcnt) { 336 _kvm_err(kd, kd->program, "nprocs corrupt"); 337 return (-1); 338 } 339 /* 340 * gather eproc 341 */ 342 eproc.e_paddr = p; 343 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 344 _kvm_err(kd, kd->program, "can't read pgrp at %p", 345 proc.p_pgrp); 346 return (-1); 347 } 348 eproc.e_sess = pgrp.pg_session; 349 eproc.e_pgid = pgrp.pg_id; 350 eproc.e_jobc = pgrp.pg_jobc; 351 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 352 _kvm_err(kd, kd->program, "can't read session at %p", 353 pgrp.pg_session); 354 return (-1); 355 } 356 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 357 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 358 _kvm_err(kd, kd->program, 359 "can't read tty at %p", sess.s_ttyp); 360 return (-1); 361 } 362 eproc.e_tdev = tty.t_dev; 363 eproc.e_tsess = tty.t_session; 364 if (tty.t_pgrp != NULL) { 365 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 366 _kvm_err(kd, kd->program, 367 "can't read tpgrp at %p", 368 tty.t_pgrp); 369 return (-1); 370 } 371 eproc.e_tpgid = pgrp.pg_id; 372 } else 373 eproc.e_tpgid = -1; 374 } else 375 eproc.e_tdev = NODEV; 376 eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0; 377 eproc.e_sid = sess.s_sid; 378 if (sess.s_leader == p) 379 eproc.e_flag |= EPROC_SLEADER; 380 /* 381 * Fill in the old-style proc.p_wmesg by copying the wmesg 382 * from the first available LWP. 383 */ 384 kl = kvm_getlwps(kd, proc.p_pid, 385 (u_long)PTRTOUINT64(eproc.e_paddr), 386 sizeof(struct kinfo_lwp), &nlwps); 387 if (kl) { 388 if (nlwps > 0) { 389 strcpy(eproc.e_wmesg, kl[0].l_wmesg); 390 } 391 } 392 (void)kvm_read(kd, (u_long)proc.p_vmspace, &eproc.e_vm, 393 sizeof(eproc.e_vm)); 394 395 eproc.e_xsize = eproc.e_xrssize = 0; 396 eproc.e_xccount = eproc.e_xswrss = 0; 397 398 switch (what) { 399 400 case KERN_PROC_PGRP: 401 if (eproc.e_pgid != (pid_t)arg) 402 continue; 403 break; 404 405 case KERN_PROC_TTY: 406 if ((proc.p_flag & P_CONTROLT) == 0 || 407 eproc.e_tdev != (dev_t)arg) 408 continue; 409 break; 410 } 411 memcpy(&bp->kp_proc, &proc, sizeof(proc)); 412 memcpy(&bp->kp_eproc, &eproc, sizeof(eproc)); 413 ++bp; 414 ++cnt; 415 } 416 return (cnt); 417 } 418 419 /* 420 * Build proc info array by reading in proc list from a crash dump. 421 * Return number of procs read. maxcnt is the max we will read. 422 */ 423 static int 424 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt) 425 kvm_t *kd; 426 int what, arg; 427 u_long a_allproc; 428 u_long a_zombproc; 429 int maxcnt; 430 { 431 struct kinfo_proc *bp = kd->procbase; 432 int acnt, zcnt; 433 struct proc *p; 434 435 if (KREAD(kd, a_allproc, &p)) { 436 _kvm_err(kd, kd->program, "cannot read allproc"); 437 return (-1); 438 } 439 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 440 if (acnt < 0) 441 return (acnt); 442 443 if (KREAD(kd, a_zombproc, &p)) { 444 _kvm_err(kd, kd->program, "cannot read zombproc"); 445 return (-1); 446 } 447 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, 448 maxcnt - acnt); 449 if (zcnt < 0) 450 zcnt = 0; 451 452 return (acnt + zcnt); 453 } 454 455 struct kinfo_proc2 * 456 kvm_getproc2(kd, op, arg, esize, cnt) 457 kvm_t *kd; 458 int op, arg; 459 size_t esize; 460 int *cnt; 461 { 462 size_t size; 463 int mib[6], st, nprocs; 464 struct pstats pstats; 465 466 if (ISSYSCTL(kd)) { 467 size = 0; 468 mib[0] = CTL_KERN; 469 mib[1] = KERN_PROC2; 470 mib[2] = op; 471 mib[3] = arg; 472 mib[4] = (int)esize; 473 mib[5] = 0; 474 st = sysctl(mib, 6, NULL, &size, NULL, (size_t)0); 475 if (st == -1) { 476 _kvm_syserr(kd, kd->program, "kvm_getproc2"); 477 return (NULL); 478 } 479 480 mib[5] = (int) (size / esize); 481 KVM_ALLOC(kd, procbase2, size); 482 st = sysctl(mib, 6, kd->procbase2, &size, NULL, (size_t)0); 483 if (st == -1) { 484 _kvm_syserr(kd, kd->program, "kvm_getproc2"); 485 return (NULL); 486 } 487 nprocs = (int) (size / esize); 488 } else { 489 char *kp2c; 490 struct kinfo_proc *kp; 491 struct kinfo_proc2 kp2, *kp2p; 492 struct kinfo_lwp *kl; 493 int i, nlwps; 494 495 kp = kvm_getprocs(kd, op, arg, &nprocs); 496 if (kp == NULL) 497 return (NULL); 498 499 size = nprocs * esize; 500 KVM_ALLOC(kd, procbase2, size); 501 kp2c = (char *)(void *)kd->procbase2; 502 kp2p = &kp2; 503 for (i = 0; i < nprocs; i++, kp++) { 504 kl = kvm_getlwps(kd, kp->kp_proc.p_pid, 505 (u_long)PTRTOUINT64(kp->kp_eproc.e_paddr), 506 sizeof(struct kinfo_lwp), &nlwps); 507 /* We use kl[0] as the "representative" LWP */ 508 memset(kp2p, 0, sizeof(kp2)); 509 kp2p->p_forw = kl[0].l_forw; 510 kp2p->p_back = kl[0].l_back; 511 kp2p->p_paddr = PTRTOUINT64(kp->kp_eproc.e_paddr); 512 kp2p->p_addr = kl[0].l_addr; 513 kp2p->p_fd = PTRTOUINT64(kp->kp_proc.p_fd); 514 kp2p->p_cwdi = PTRTOUINT64(kp->kp_proc.p_cwdi); 515 kp2p->p_stats = PTRTOUINT64(kp->kp_proc.p_stats); 516 kp2p->p_limit = PTRTOUINT64(kp->kp_proc.p_limit); 517 kp2p->p_vmspace = PTRTOUINT64(kp->kp_proc.p_vmspace); 518 kp2p->p_sigacts = PTRTOUINT64(kp->kp_proc.p_sigacts); 519 kp2p->p_sess = PTRTOUINT64(kp->kp_eproc.e_sess); 520 kp2p->p_tsess = 0; 521 kp2p->p_ru = PTRTOUINT64(kp->kp_proc.p_ru); 522 523 kp2p->p_eflag = 0; 524 kp2p->p_exitsig = kp->kp_proc.p_exitsig; 525 kp2p->p_flag = kp->kp_proc.p_flag; 526 527 kp2p->p_pid = kp->kp_proc.p_pid; 528 529 kp2p->p_ppid = kp->kp_eproc.e_ppid; 530 kp2p->p_sid = kp->kp_eproc.e_sid; 531 kp2p->p__pgid = kp->kp_eproc.e_pgid; 532 533 kp2p->p_tpgid = -1 /* XXX NO_PGID! */; 534 535 kp2p->p_uid = kp->kp_eproc.e_ucred.cr_uid; 536 kp2p->p_ruid = kp->kp_eproc.e_pcred.p_ruid; 537 kp2p->p_svuid = kp->kp_eproc.e_pcred.p_svuid; 538 kp2p->p_gid = kp->kp_eproc.e_ucred.cr_gid; 539 kp2p->p_rgid = kp->kp_eproc.e_pcred.p_rgid; 540 kp2p->p_svgid = kp->kp_eproc.e_pcred.p_svgid; 541 542 /*CONSTCOND*/ 543 memcpy(kp2p->p_groups, kp->kp_eproc.e_ucred.cr_groups, 544 MIN(sizeof(kp2p->p_groups), 545 sizeof(kp->kp_eproc.e_ucred.cr_groups))); 546 kp2p->p_ngroups = kp->kp_eproc.e_ucred.cr_ngroups; 547 548 kp2p->p_jobc = kp->kp_eproc.e_jobc; 549 kp2p->p_tdev = kp->kp_eproc.e_tdev; 550 kp2p->p_tpgid = kp->kp_eproc.e_tpgid; 551 kp2p->p_tsess = PTRTOUINT64(kp->kp_eproc.e_tsess); 552 553 kp2p->p_estcpu = kp->kp_proc.p_estcpu; 554 kp2p->p_rtime_sec = kp->kp_proc.p_estcpu; 555 kp2p->p_rtime_usec = kp->kp_proc.p_estcpu; 556 kp2p->p_cpticks = kp->kp_proc.p_cpticks; 557 kp2p->p_pctcpu = kp->kp_proc.p_pctcpu; 558 kp2p->p_swtime = kl[0].l_swtime; 559 kp2p->p_slptime = kl[0].l_slptime; 560 #if 0 /* XXX thorpej */ 561 kp2p->p_schedflags = kp->kp_proc.p_schedflags; 562 #else 563 kp2p->p_schedflags = 0; 564 #endif 565 566 kp2p->p_uticks = kp->kp_proc.p_uticks; 567 kp2p->p_sticks = kp->kp_proc.p_sticks; 568 kp2p->p_iticks = kp->kp_proc.p_iticks; 569 570 kp2p->p_tracep = PTRTOUINT64(kp->kp_proc.p_tracep); 571 kp2p->p_traceflag = kp->kp_proc.p_traceflag; 572 573 kp2p->p_holdcnt = kl[0].l_holdcnt; 574 575 memcpy(&kp2p->p_siglist, 576 &kp->kp_proc.p_sigctx.ps_siglist, 577 sizeof(ki_sigset_t)); 578 memcpy(&kp2p->p_sigmask, 579 &kp->kp_proc.p_sigctx.ps_sigmask, 580 sizeof(ki_sigset_t)); 581 memcpy(&kp2p->p_sigignore, 582 &kp->kp_proc.p_sigctx.ps_sigignore, 583 sizeof(ki_sigset_t)); 584 memcpy(&kp2p->p_sigcatch, 585 &kp->kp_proc.p_sigctx.ps_sigcatch, 586 sizeof(ki_sigset_t)); 587 588 kp2p->p_stat = kp->kp_proc.p_stat; 589 kp2p->p_priority = kl[0].l_priority; 590 kp2p->p_usrpri = kl[0].l_usrpri; 591 kp2p->p_nice = kp->kp_proc.p_nice; 592 593 kp2p->p_xstat = kp->kp_proc.p_xstat; 594 kp2p->p_acflag = kp->kp_proc.p_acflag; 595 596 /*CONSTCOND*/ 597 strncpy(kp2p->p_comm, kp->kp_proc.p_comm, 598 MIN(sizeof(kp2p->p_comm), 599 sizeof(kp->kp_proc.p_comm))); 600 601 strncpy(kp2p->p_wmesg, kp->kp_eproc.e_wmesg, 602 sizeof(kp2p->p_wmesg)); 603 kp2p->p_wchan = kl[0].l_wchan; 604 strncpy(kp2p->p_login, kp->kp_eproc.e_login, 605 sizeof(kp2p->p_login)); 606 607 kp2p->p_vm_rssize = kp->kp_eproc.e_xrssize; 608 kp2p->p_vm_tsize = kp->kp_eproc.e_vm.vm_tsize; 609 kp2p->p_vm_dsize = kp->kp_eproc.e_vm.vm_dsize; 610 kp2p->p_vm_ssize = kp->kp_eproc.e_vm.vm_ssize; 611 612 kp2p->p_eflag = (int32_t)kp->kp_eproc.e_flag; 613 614 kp2p->p_realflag = kp->kp_proc.p_flag; 615 kp2p->p_nlwps = kp->kp_proc.p_nlwps; 616 kp2p->p_nrlwps = kp->kp_proc.p_nrlwps; 617 kp2p->p_realstat = kp->kp_proc.p_stat; 618 619 if (P_ZOMBIE(&kp->kp_proc) || 620 kp->kp_proc.p_stats == NULL || 621 KREAD(kd, (u_long)kp->kp_proc.p_stats, &pstats)) { 622 kp2p->p_uvalid = 0; 623 } else { 624 kp2p->p_uvalid = 1; 625 626 kp2p->p_ustart_sec = (u_int32_t) 627 pstats.p_start.tv_sec; 628 kp2p->p_ustart_usec = (u_int32_t) 629 pstats.p_start.tv_usec; 630 631 kp2p->p_uutime_sec = (u_int32_t) 632 pstats.p_ru.ru_utime.tv_sec; 633 kp2p->p_uutime_usec = (u_int32_t) 634 pstats.p_ru.ru_utime.tv_usec; 635 kp2p->p_ustime_sec = (u_int32_t) 636 pstats.p_ru.ru_stime.tv_sec; 637 kp2p->p_ustime_usec = (u_int32_t) 638 pstats.p_ru.ru_stime.tv_usec; 639 640 kp2p->p_uru_maxrss = pstats.p_ru.ru_maxrss; 641 kp2p->p_uru_ixrss = pstats.p_ru.ru_ixrss; 642 kp2p->p_uru_idrss = pstats.p_ru.ru_idrss; 643 kp2p->p_uru_isrss = pstats.p_ru.ru_isrss; 644 kp2p->p_uru_minflt = pstats.p_ru.ru_minflt; 645 kp2p->p_uru_majflt = pstats.p_ru.ru_majflt; 646 kp2p->p_uru_nswap = pstats.p_ru.ru_nswap; 647 kp2p->p_uru_inblock = pstats.p_ru.ru_inblock; 648 kp2p->p_uru_oublock = pstats.p_ru.ru_oublock; 649 kp2p->p_uru_msgsnd = pstats.p_ru.ru_msgsnd; 650 kp2p->p_uru_msgrcv = pstats.p_ru.ru_msgrcv; 651 kp2p->p_uru_nsignals = pstats.p_ru.ru_nsignals; 652 kp2p->p_uru_nvcsw = pstats.p_ru.ru_nvcsw; 653 kp2p->p_uru_nivcsw = pstats.p_ru.ru_nivcsw; 654 655 kp2p->p_uctime_sec = (u_int32_t) 656 (pstats.p_cru.ru_utime.tv_sec + 657 pstats.p_cru.ru_stime.tv_sec); 658 kp2p->p_uctime_usec = (u_int32_t) 659 (pstats.p_cru.ru_utime.tv_usec + 660 pstats.p_cru.ru_stime.tv_usec); 661 } 662 663 memcpy(kp2c, &kp2, esize); 664 kp2c += esize; 665 } 666 } 667 *cnt = nprocs; 668 return (kd->procbase2); 669 } 670 671 struct kinfo_lwp * 672 kvm_getlwps(kd, pid, paddr, esize, cnt) 673 kvm_t *kd; 674 int pid; 675 u_long paddr; 676 size_t esize; 677 int *cnt; 678 { 679 size_t size; 680 int mib[5], nlwps; 681 ssize_t st; 682 struct kinfo_lwp *kl; 683 684 if (ISSYSCTL(kd)) { 685 size = 0; 686 mib[0] = CTL_KERN; 687 mib[1] = KERN_LWP; 688 mib[2] = pid; 689 mib[3] = (int)esize; 690 mib[4] = 0; 691 st = sysctl(mib, 5, NULL, &size, NULL, (size_t)0); 692 if (st == -1) { 693 _kvm_syserr(kd, kd->program, "kvm_getlwps"); 694 return (NULL); 695 } 696 697 mib[4] = (int) (size / esize); 698 KVM_ALLOC(kd, lwpbase, size); 699 st = sysctl(mib, 5, kd->lwpbase, &size, NULL, (size_t)0); 700 if (st == -1) { 701 _kvm_syserr(kd, kd->program, "kvm_getlwps"); 702 return (NULL); 703 } 704 nlwps = (int) (size / esize); 705 } else { 706 /* grovel through the memory image */ 707 struct proc p; 708 struct lwp l; 709 u_long laddr; 710 int i; 711 712 st = kvm_read(kd, paddr, &p, sizeof(p)); 713 if (st == -1) { 714 _kvm_syserr(kd, kd->program, "kvm_getlwps"); 715 return (NULL); 716 } 717 718 nlwps = p.p_nlwps; 719 size = nlwps * sizeof(*kd->lwpbase); 720 KVM_ALLOC(kd, lwpbase, size); 721 laddr = (u_long)PTRTOUINT64(p.p_lwps.lh_first); 722 for (i = 0; (i < nlwps) && (laddr != 0); i++) { 723 st = kvm_read(kd, laddr, &l, sizeof(l)); 724 if (st == -1) { 725 _kvm_syserr(kd, kd->program, "kvm_getlwps"); 726 return (NULL); 727 } 728 kl = &kd->lwpbase[i]; 729 kl->l_laddr = laddr; 730 kl->l_forw = PTRTOUINT64(l.l_forw); 731 kl->l_back = PTRTOUINT64(l.l_back); 732 kl->l_addr = PTRTOUINT64(l.l_addr); 733 kl->l_lid = l.l_lid; 734 kl->l_flag = l.l_flag; 735 kl->l_swtime = l.l_swtime; 736 kl->l_slptime = l.l_slptime; 737 kl->l_schedflags = 0; /* XXX */ 738 kl->l_holdcnt = l.l_holdcnt; 739 kl->l_priority = l.l_priority; 740 kl->l_usrpri = l.l_usrpri; 741 kl->l_stat = l.l_stat; 742 kl->l_wchan = PTRTOUINT64(l.l_wchan); 743 if (l.l_wmesg) 744 (void)kvm_read(kd, (u_long)l.l_wmesg, 745 kl->l_wmesg, (size_t)WMESGLEN); 746 kl->l_cpuid = KI_NOCPU; 747 laddr = (u_long)PTRTOUINT64(l.l_sibling.le_next); 748 } 749 } 750 751 *cnt = nlwps; 752 return (kd->lwpbase); 753 } 754 755 struct kinfo_proc * 756 kvm_getprocs(kd, op, arg, cnt) 757 kvm_t *kd; 758 int op, arg; 759 int *cnt; 760 { 761 size_t size; 762 int mib[4], st, nprocs; 763 764 if (ISKMEM(kd)) { 765 size = 0; 766 mib[0] = CTL_KERN; 767 mib[1] = KERN_PROC; 768 mib[2] = op; 769 mib[3] = arg; 770 st = sysctl(mib, 4, NULL, &size, NULL, (size_t)0); 771 if (st == -1) { 772 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 773 return (NULL); 774 } 775 KVM_ALLOC(kd, procbase, size); 776 st = sysctl(mib, 4, kd->procbase, &size, NULL, (size_t)0); 777 if (st == -1) { 778 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 779 return (NULL); 780 } 781 if (size % sizeof(struct kinfo_proc) != 0) { 782 _kvm_err(kd, kd->program, 783 "proc size mismatch (%lu total, %lu chunks)", 784 (u_long)size, (u_long)sizeof(struct kinfo_proc)); 785 return (NULL); 786 } 787 nprocs = (int) (size / sizeof(struct kinfo_proc)); 788 } else if (ISSYSCTL(kd)) { 789 _kvm_err(kd, kd->program, "kvm_open called with KVM_NO_FILES, " 790 "can't use kvm_getprocs"); 791 return (NULL); 792 } else { 793 struct nlist nl[4], *p; 794 795 (void)memset(nl, 0, sizeof(nl)); 796 nl[0].n_name = "_nprocs"; 797 nl[1].n_name = "_allproc"; 798 nl[2].n_name = "_zombproc"; 799 nl[3].n_name = NULL; 800 801 if (kvm_nlist(kd, nl) != 0) { 802 for (p = nl; p->n_type != 0; ++p) 803 continue; 804 _kvm_err(kd, kd->program, 805 "%s: no such symbol", p->n_name); 806 return (NULL); 807 } 808 if (KREAD(kd, nl[0].n_value, &nprocs)) { 809 _kvm_err(kd, kd->program, "can't read nprocs"); 810 return (NULL); 811 } 812 size = nprocs * sizeof(*kd->procbase); 813 KVM_ALLOC(kd, procbase, size); 814 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 815 nl[2].n_value, nprocs); 816 if (nprocs < 0) 817 return (NULL); 818 #ifdef notdef 819 size = nprocs * sizeof(struct kinfo_proc); 820 (void)realloc(kd->procbase, size); 821 #endif 822 } 823 *cnt = nprocs; 824 return (kd->procbase); 825 } 826 827 void * 828 _kvm_realloc(kd, p, n) 829 kvm_t *kd; 830 void *p; 831 size_t n; 832 { 833 void *np = realloc(p, n); 834 835 if (np == NULL) 836 _kvm_err(kd, kd->program, "out of memory"); 837 return (np); 838 } 839 840 /* 841 * Read in an argument vector from the user address space of process p. 842 * addr if the user-space base address of narg null-terminated contiguous 843 * strings. This is used to read in both the command arguments and 844 * environment strings. Read at most maxcnt characters of strings. 845 */ 846 static char ** 847 kvm_argv(kd, p, addr, narg, maxcnt) 848 kvm_t *kd; 849 const struct miniproc *p; 850 u_long addr; 851 int narg; 852 int maxcnt; 853 { 854 char *np, *cp, *ep, *ap; 855 u_long oaddr = (u_long)~0L; 856 u_long len; 857 size_t cc; 858 char **argv; 859 860 /* 861 * Check that there aren't an unreasonable number of arguments, 862 * and that the address is in user space. 863 */ 864 if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva) 865 return (NULL); 866 867 if (kd->argv == NULL) { 868 /* 869 * Try to avoid reallocs. 870 */ 871 kd->argc = MAX(narg + 1, 32); 872 kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv)); 873 if (kd->argv == NULL) 874 return (NULL); 875 } else if (narg + 1 > kd->argc) { 876 kd->argc = MAX(2 * kd->argc, narg + 1); 877 kd->argv = _kvm_realloc(kd, kd->argv, kd->argc * 878 sizeof(*kd->argv)); 879 if (kd->argv == NULL) 880 return (NULL); 881 } 882 if (kd->argspc == NULL) { 883 kd->argspc = _kvm_malloc(kd, (size_t)kd->nbpg); 884 if (kd->argspc == NULL) 885 return (NULL); 886 kd->argspc_len = kd->nbpg; 887 } 888 if (kd->argbuf == NULL) { 889 kd->argbuf = _kvm_malloc(kd, (size_t)kd->nbpg); 890 if (kd->argbuf == NULL) 891 return (NULL); 892 } 893 cc = sizeof(char *) * narg; 894 if (kvm_ureadm(kd, p, addr, (void *)kd->argv, cc) != cc) 895 return (NULL); 896 ap = np = kd->argspc; 897 argv = kd->argv; 898 len = 0; 899 /* 900 * Loop over pages, filling in the argument vector. 901 */ 902 while (argv < kd->argv + narg && *argv != NULL) { 903 addr = (u_long)*argv & ~(kd->nbpg - 1); 904 if (addr != oaddr) { 905 if (kvm_ureadm(kd, p, addr, kd->argbuf, 906 (size_t)kd->nbpg) != kd->nbpg) 907 return (NULL); 908 oaddr = addr; 909 } 910 addr = (u_long)*argv & (kd->nbpg - 1); 911 cp = kd->argbuf + (size_t)addr; 912 cc = kd->nbpg - (size_t)addr; 913 if (maxcnt > 0 && cc > (size_t)(maxcnt - len)) 914 cc = (size_t)(maxcnt - len); 915 ep = memchr(cp, '\0', cc); 916 if (ep != NULL) 917 cc = ep - cp + 1; 918 if (len + cc > kd->argspc_len) { 919 ptrdiff_t off; 920 char **pp; 921 char *op = kd->argspc; 922 923 kd->argspc_len *= 2; 924 kd->argspc = _kvm_realloc(kd, kd->argspc, 925 kd->argspc_len); 926 if (kd->argspc == NULL) 927 return (NULL); 928 /* 929 * Adjust argv pointers in case realloc moved 930 * the string space. 931 */ 932 off = kd->argspc - op; 933 for (pp = kd->argv; pp < argv; pp++) 934 *pp += off; 935 ap += off; 936 np += off; 937 } 938 memcpy(np, cp, cc); 939 np += cc; 940 len += cc; 941 if (ep != NULL) { 942 *argv++ = ap; 943 ap = np; 944 } else 945 *argv += cc; 946 if (maxcnt > 0 && len >= maxcnt) { 947 /* 948 * We're stopping prematurely. Terminate the 949 * current string. 950 */ 951 if (ep == NULL) { 952 *np = '\0'; 953 *argv++ = ap; 954 } 955 break; 956 } 957 } 958 /* Make sure argv is terminated. */ 959 *argv = NULL; 960 return (kd->argv); 961 } 962 963 static void 964 ps_str_a(p, addr, n) 965 struct ps_strings *p; 966 u_long *addr; 967 int *n; 968 { 969 970 *addr = (u_long)p->ps_argvstr; 971 *n = p->ps_nargvstr; 972 } 973 974 static void 975 ps_str_e(p, addr, n) 976 struct ps_strings *p; 977 u_long *addr; 978 int *n; 979 { 980 981 *addr = (u_long)p->ps_envstr; 982 *n = p->ps_nenvstr; 983 } 984 985 /* 986 * Determine if the proc indicated by p is still active. 987 * This test is not 100% foolproof in theory, but chances of 988 * being wrong are very low. 989 */ 990 static int 991 proc_verify(kd, kernp, p) 992 kvm_t *kd; 993 u_long kernp; 994 const struct miniproc *p; 995 { 996 struct proc kernproc; 997 998 /* 999 * Just read in the whole proc. It's not that big relative 1000 * to the cost of the read system call. 1001 */ 1002 if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) != 1003 sizeof(kernproc)) 1004 return (0); 1005 return (p->p_pid == kernproc.p_pid && 1006 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB)); 1007 } 1008 1009 static char ** 1010 kvm_doargv(kd, p, nchr, info) 1011 kvm_t *kd; 1012 const struct miniproc *p; 1013 int nchr; 1014 void (*info)(struct ps_strings *, u_long *, int *); 1015 { 1016 char **ap; 1017 u_long addr; 1018 int cnt; 1019 struct ps_strings arginfo; 1020 1021 /* 1022 * Pointers are stored at the top of the user stack. 1023 */ 1024 if (p->p_stat == SZOMB) 1025 return (NULL); 1026 cnt = (int)kvm_ureadm(kd, p, kd->usrstack - sizeof(arginfo), 1027 (void *)&arginfo, sizeof(arginfo)); 1028 if (cnt != sizeof(arginfo)) 1029 return (NULL); 1030 1031 (*info)(&arginfo, &addr, &cnt); 1032 if (cnt == 0) 1033 return (NULL); 1034 ap = kvm_argv(kd, p, addr, cnt, nchr); 1035 /* 1036 * For live kernels, make sure this process didn't go away. 1037 */ 1038 if (ap != NULL && ISALIVE(kd) && 1039 !proc_verify(kd, (u_long)p->p_paddr, p)) 1040 ap = NULL; 1041 return (ap); 1042 } 1043 1044 /* 1045 * Get the command args. This code is now machine independent. 1046 */ 1047 char ** 1048 kvm_getargv(kd, kp, nchr) 1049 kvm_t *kd; 1050 const struct kinfo_proc *kp; 1051 int nchr; 1052 { 1053 struct miniproc p; 1054 1055 KPTOMINI(kp, &p); 1056 return (kvm_doargv(kd, &p, nchr, ps_str_a)); 1057 } 1058 1059 char ** 1060 kvm_getenvv(kd, kp, nchr) 1061 kvm_t *kd; 1062 const struct kinfo_proc *kp; 1063 int nchr; 1064 { 1065 struct miniproc p; 1066 1067 KPTOMINI(kp, &p); 1068 return (kvm_doargv(kd, &p, nchr, ps_str_e)); 1069 } 1070 1071 static char ** 1072 kvm_doargv2(kd, pid, type, nchr) 1073 kvm_t *kd; 1074 pid_t pid; 1075 int type; 1076 int nchr; 1077 { 1078 size_t bufs; 1079 int narg, mib[4]; 1080 size_t newargspc_len; 1081 char **ap, *bp, *endp; 1082 1083 /* 1084 * Check that there aren't an unreasonable number of arguments. 1085 */ 1086 if (nchr > ARG_MAX) 1087 return (NULL); 1088 1089 if (nchr == 0) 1090 nchr = ARG_MAX; 1091 1092 /* Get number of strings in argv */ 1093 mib[0] = CTL_KERN; 1094 mib[1] = KERN_PROC_ARGS; 1095 mib[2] = pid; 1096 mib[3] = type == KERN_PROC_ARGV ? KERN_PROC_NARGV : KERN_PROC_NENV; 1097 bufs = sizeof(narg); 1098 if (sysctl(mib, 4, &narg, &bufs, NULL, (size_t)0) == -1) 1099 return (NULL); 1100 1101 if (kd->argv == NULL) { 1102 /* 1103 * Try to avoid reallocs. 1104 */ 1105 kd->argc = MAX(narg + 1, 32); 1106 kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv)); 1107 if (kd->argv == NULL) 1108 return (NULL); 1109 } else if (narg + 1 > kd->argc) { 1110 kd->argc = MAX(2 * kd->argc, narg + 1); 1111 kd->argv = _kvm_realloc(kd, kd->argv, kd->argc * 1112 sizeof(*kd->argv)); 1113 if (kd->argv == NULL) 1114 return (NULL); 1115 } 1116 1117 newargspc_len = MIN(nchr, ARG_MAX); 1118 KVM_ALLOC(kd, argspc, newargspc_len); 1119 memset(kd->argspc, 0, (size_t)kd->argspc_len); /* XXX necessary? */ 1120 1121 mib[0] = CTL_KERN; 1122 mib[1] = KERN_PROC_ARGS; 1123 mib[2] = pid; 1124 mib[3] = type; 1125 bufs = kd->argspc_len; 1126 if (sysctl(mib, 4, kd->argspc, &bufs, NULL, (size_t)0) == -1) 1127 return (NULL); 1128 1129 bp = kd->argspc; 1130 bp[kd->argspc_len-1] = '\0'; /* make sure the string ends with nul */ 1131 ap = kd->argv; 1132 endp = bp + MIN(nchr, bufs); 1133 1134 while (bp < endp) { 1135 *ap++ = bp; 1136 /* 1137 * XXX: don't need following anymore, or stick check 1138 * for max argc in above while loop? 1139 */ 1140 if (ap >= kd->argv + kd->argc) { 1141 kd->argc *= 2; 1142 kd->argv = _kvm_realloc(kd, kd->argv, 1143 kd->argc * sizeof(*kd->argv)); 1144 ap = kd->argv; 1145 } 1146 bp += strlen(bp) + 1; 1147 } 1148 *ap = NULL; 1149 1150 return (kd->argv); 1151 } 1152 1153 char ** 1154 kvm_getargv2(kd, kp, nchr) 1155 kvm_t *kd; 1156 const struct kinfo_proc2 *kp; 1157 int nchr; 1158 { 1159 1160 return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ARGV, nchr)); 1161 } 1162 1163 char ** 1164 kvm_getenvv2(kd, kp, nchr) 1165 kvm_t *kd; 1166 const struct kinfo_proc2 *kp; 1167 int nchr; 1168 { 1169 1170 return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ENV, nchr)); 1171 } 1172 1173 /* 1174 * Read from user space. The user context is given by p. 1175 */ 1176 static ssize_t 1177 kvm_ureadm(kd, p, uva, buf, len) 1178 kvm_t *kd; 1179 const struct miniproc *p; 1180 u_long uva; 1181 char *buf; 1182 size_t len; 1183 { 1184 char *cp; 1185 1186 cp = buf; 1187 while (len > 0) { 1188 size_t cc; 1189 char *dp; 1190 u_long cnt; 1191 1192 dp = _kvm_ureadm(kd, p, uva, &cnt); 1193 if (dp == NULL) { 1194 _kvm_err(kd, 0, "invalid address (%lx)", uva); 1195 return (0); 1196 } 1197 cc = (size_t)MIN(cnt, len); 1198 memcpy(cp, dp, cc); 1199 cp += cc; 1200 uva += cc; 1201 len -= cc; 1202 } 1203 return (ssize_t)(cp - buf); 1204 } 1205 1206 ssize_t 1207 kvm_uread(kd, p, uva, buf, len) 1208 kvm_t *kd; 1209 const struct proc *p; 1210 u_long uva; 1211 char *buf; 1212 size_t len; 1213 { 1214 struct miniproc mp; 1215 1216 PTOMINI(p, &mp); 1217 return (kvm_ureadm(kd, &mp, uva, buf, len)); 1218 } 1219