1 /* $NetBSD: kern_ktrace.c,v 1.108 2006/10/13 16:53:36 dogcow Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.108 2006/10/13 16:53:36 dogcow Exp $"); 36 37 #include "opt_ktrace.h" 38 #include "opt_compat_mach.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/proc.h> 43 #include <sys/file.h> 44 #include <sys/namei.h> 45 #include <sys/vnode.h> 46 #include <sys/kernel.h> 47 #include <sys/kthread.h> 48 #include <sys/ktrace.h> 49 #include <sys/malloc.h> 50 #include <sys/syslog.h> 51 #include <sys/filedesc.h> 52 #include <sys/ioctl.h> 53 #include <sys/callout.h> 54 #include <sys/kauth.h> 55 56 #include <sys/mount.h> 57 #include <sys/sa.h> 58 #include <sys/syscallargs.h> 59 60 #ifdef KTRACE 61 62 /* 63 * XXX: 64 * - need better error reporting? 65 * - p->p_tracep access lock. lock p_lock, lock ktd if !NULL, inc ref. 66 * - userland utility to sort ktrace.out by timestamp. 67 * - keep minimum information in ktrace_entry when rest of alloc failed. 68 * - enlarge ktrace_entry so that small entry won't require additional 69 * alloc? 70 * - per trace control of configurable parameters. 71 */ 72 73 struct ktrace_entry { 74 TAILQ_ENTRY(ktrace_entry) kte_list; 75 struct ktr_header kte_kth; 76 void *kte_buf; 77 }; 78 79 struct ktr_desc { 80 TAILQ_ENTRY(ktr_desc) ktd_list; 81 int ktd_flags; 82 #define KTDF_WAIT 0x0001 83 #define KTDF_DONE 0x0002 84 #define KTDF_BLOCKING 0x0004 85 #define KTDF_INTERACTIVE 0x0008 86 int ktd_error; 87 #define KTDE_ENOMEM 0x0001 88 #define KTDE_ENOSPC 0x0002 89 int ktd_errcnt; 90 int ktd_ref; /* # of reference */ 91 int ktd_qcount; /* # of entry in the queue */ 92 93 /* 94 * Params to control behaviour. 95 */ 96 int ktd_delayqcnt; /* # of entry allowed to delay */ 97 int ktd_wakedelay; /* delay of wakeup in *tick* */ 98 int ktd_intrwakdl; /* ditto, but when interactive */ 99 100 struct file *ktd_fp; /* trace output file */ 101 struct proc *ktd_proc; /* our kernel thread */ 102 TAILQ_HEAD(, ktrace_entry) ktd_queue; 103 struct callout ktd_wakch; /* delayed wakeup */ 104 struct simplelock ktd_slock; 105 }; 106 107 static void ktrinitheader(struct ktr_header *, struct lwp *, int); 108 static void ktrwrite(struct ktr_desc *, struct ktrace_entry *); 109 static int ktrace_common(struct lwp *, int, int, int, struct file *); 110 static int ktrops(struct lwp *, struct proc *, int, int, 111 struct ktr_desc *); 112 static int ktrsetchildren(struct lwp *, struct proc *, int, int, 113 struct ktr_desc *); 114 static int ktrcanset(struct lwp *, struct proc *); 115 static int ktrsamefile(struct file *, struct file *); 116 117 static struct ktr_desc * 118 ktd_lookup(struct file *); 119 static void ktdrel(struct ktr_desc *); 120 static void ktdref(struct ktr_desc *); 121 static void ktraddentry(struct lwp *, struct ktrace_entry *, int); 122 /* Flags for ktraddentry (3rd arg) */ 123 #define KTA_NOWAIT 0x0000 124 #define KTA_WAITOK 0x0001 125 #define KTA_LARGE 0x0002 126 static void ktefree(struct ktrace_entry *); 127 static void ktd_logerrl(struct ktr_desc *, int); 128 static void ktd_logerr(struct proc *, int); 129 static void ktrace_thread(void *); 130 131 /* 132 * Default vaules. 133 */ 134 #define KTD_MAXENTRY 1000 /* XXX: tune */ 135 #define KTD_TIMEOUT 5 /* XXX: tune */ 136 #define KTD_DELAYQCNT 100 /* XXX: tune */ 137 #define KTD_WAKEDELAY 5000 /* XXX: tune */ 138 #define KTD_INTRWAKDL 100 /* XXX: tune */ 139 140 /* 141 * Patchable variables. 142 */ 143 int ktd_maxentry = KTD_MAXENTRY; /* max # of entry in the queue */ 144 int ktd_timeout = KTD_TIMEOUT; /* timeout in seconds */ 145 int ktd_delayqcnt = KTD_DELAYQCNT; /* # of entry allowed to delay */ 146 int ktd_wakedelay = KTD_WAKEDELAY; /* delay of wakeup in *ms* */ 147 int ktd_intrwakdl = KTD_INTRWAKDL; /* ditto, but when interactive */ 148 149 static struct simplelock ktdq_slock = SIMPLELOCK_INITIALIZER; 150 static TAILQ_HEAD(, ktr_desc) ktdq = TAILQ_HEAD_INITIALIZER(ktdq); 151 152 MALLOC_DEFINE(M_KTRACE, "ktrace", "ktrace data buffer"); 153 POOL_INIT(kte_pool, sizeof(struct ktrace_entry), 0, 0, 0, 154 "ktepl", &pool_allocator_nointr); 155 156 static inline void 157 ktd_wakeup(struct ktr_desc *ktd) 158 { 159 160 callout_stop(&ktd->ktd_wakch); 161 wakeup(ktd); 162 } 163 164 static void 165 ktd_logerrl(struct ktr_desc *ktd, int error) 166 { 167 168 ktd->ktd_error |= error; 169 ktd->ktd_errcnt++; 170 } 171 172 static void 173 ktd_logerr(struct proc *p, int error) 174 { 175 struct ktr_desc *ktd = p->p_tracep; 176 177 if (ktd == NULL) 178 return; 179 180 simple_lock(&ktd->ktd_slock); 181 ktd_logerrl(ktd, error); 182 simple_unlock(&ktd->ktd_slock); 183 } 184 185 /* 186 * Release a reference. Called with ktd_slock held. 187 */ 188 void 189 ktdrel(struct ktr_desc *ktd) 190 { 191 192 KDASSERT(ktd->ktd_ref != 0); 193 KASSERT(ktd->ktd_ref > 0); 194 if (--ktd->ktd_ref <= 0) { 195 ktd->ktd_flags |= KTDF_DONE; 196 wakeup(ktd); 197 } 198 simple_unlock(&ktd->ktd_slock); 199 } 200 201 void 202 ktdref(struct ktr_desc *ktd) 203 { 204 205 simple_lock(&ktd->ktd_slock); 206 ktd->ktd_ref++; 207 simple_unlock(&ktd->ktd_slock); 208 } 209 210 struct ktr_desc * 211 ktd_lookup(struct file *fp) 212 { 213 struct ktr_desc *ktd; 214 215 simple_lock(&ktdq_slock); 216 for (ktd = TAILQ_FIRST(&ktdq); ktd != NULL; 217 ktd = TAILQ_NEXT(ktd, ktd_list)) { 218 simple_lock(&ktd->ktd_slock); 219 if (ktrsamefile(ktd->ktd_fp, fp)) { 220 ktd->ktd_ref++; 221 simple_unlock(&ktd->ktd_slock); 222 break; 223 } 224 simple_unlock(&ktd->ktd_slock); 225 } 226 simple_unlock(&ktdq_slock); 227 return (ktd); 228 } 229 230 void 231 ktraddentry(struct lwp *l, struct ktrace_entry *kte, int flags) 232 { 233 struct proc *p = l->l_proc; 234 struct ktr_desc *ktd; 235 #ifdef DEBUG 236 struct timeval t1, t2; 237 #endif 238 239 if (p->p_traceflag & KTRFAC_TRC_EMUL) { 240 /* Add emulation trace before first entry for this process */ 241 p->p_traceflag &= ~KTRFAC_TRC_EMUL; 242 ktremul(l); 243 } 244 245 /* 246 * Tracing may be canceled while we were sleeping waiting for 247 * memory. 248 */ 249 ktd = p->p_tracep; 250 if (ktd == NULL) 251 goto freekte; 252 253 /* 254 * Bump reference count so that the object will remain while 255 * we are here. Note that the trace is controlled by other 256 * process. 257 */ 258 ktdref(ktd); 259 260 simple_lock(&ktd->ktd_slock); 261 if (ktd->ktd_flags & KTDF_DONE) 262 goto relktd; 263 264 if (ktd->ktd_qcount > ktd_maxentry) { 265 ktd_logerrl(ktd, KTDE_ENOSPC); 266 goto relktd; 267 } 268 TAILQ_INSERT_TAIL(&ktd->ktd_queue, kte, kte_list); 269 ktd->ktd_qcount++; 270 if (ktd->ktd_flags & KTDF_BLOCKING) 271 goto skip_sync; 272 273 if (flags & KTA_WAITOK && 274 (/* flags & KTA_LARGE */0 || ktd->ktd_flags & KTDF_WAIT || 275 ktd->ktd_qcount > ktd_maxentry >> 1)) 276 /* 277 * Sync with writer thread since we're requesting rather 278 * big one or many requests are pending. 279 */ 280 do { 281 ktd->ktd_flags |= KTDF_WAIT; 282 ktd_wakeup(ktd); 283 #ifdef DEBUG 284 getmicrouptime(&t1); 285 #endif 286 if (ltsleep(&ktd->ktd_flags, PWAIT, "ktrsync", 287 ktd_timeout * hz, &ktd->ktd_slock) != 0) { 288 ktd->ktd_flags |= KTDF_BLOCKING; 289 /* 290 * Maybe the writer thread is blocking 291 * completely for some reason, but 292 * don't stop target process forever. 293 */ 294 log(LOG_NOTICE, "ktrace timeout\n"); 295 break; 296 } 297 #ifdef DEBUG 298 getmicrouptime(&t2); 299 timersub(&t2, &t1, &t2); 300 if (t2.tv_sec > 0) 301 log(LOG_NOTICE, 302 "ktrace long wait: %ld.%06ld\n", 303 t2.tv_sec, t2.tv_usec); 304 #endif 305 } while (p->p_tracep == ktd && 306 (ktd->ktd_flags & (KTDF_WAIT | KTDF_DONE)) == KTDF_WAIT); 307 else { 308 /* Schedule delayed wakeup */ 309 if (ktd->ktd_qcount > ktd->ktd_delayqcnt) 310 ktd_wakeup(ktd); /* Wakeup now */ 311 else if (!callout_pending(&ktd->ktd_wakch)) 312 callout_reset(&ktd->ktd_wakch, 313 ktd->ktd_flags & KTDF_INTERACTIVE ? 314 ktd->ktd_intrwakdl : ktd->ktd_wakedelay, 315 (void (*)(void *))wakeup, ktd); 316 } 317 318 skip_sync: 319 ktdrel(ktd); 320 return; 321 322 relktd: 323 ktdrel(ktd); 324 325 freekte: 326 ktefree(kte); 327 } 328 329 void 330 ktefree(struct ktrace_entry *kte) 331 { 332 333 if (kte->kte_buf != NULL) 334 free(kte->kte_buf, M_KTRACE); 335 pool_put(&kte_pool, kte); 336 } 337 338 /* 339 * "deep" compare of two files for the purposes of clearing a trace. 340 * Returns true if they're the same open file, or if they point at the 341 * same underlying vnode/socket. 342 */ 343 344 int 345 ktrsamefile(struct file *f1, struct file *f2) 346 { 347 348 return ((f1 == f2) || 349 ((f1 != NULL) && (f2 != NULL) && 350 (f1->f_type == f2->f_type) && 351 (f1->f_data == f2->f_data))); 352 } 353 354 void 355 ktrderef(struct proc *p) 356 { 357 struct ktr_desc *ktd = p->p_tracep; 358 359 p->p_traceflag = 0; 360 if (ktd == NULL) 361 return; 362 p->p_tracep = NULL; 363 364 simple_lock(&ktd->ktd_slock); 365 wakeup(&ktd->ktd_flags); 366 ktdrel(ktd); 367 } 368 369 void 370 ktradref(struct proc *p) 371 { 372 struct ktr_desc *ktd = p->p_tracep; 373 374 ktdref(ktd); 375 } 376 377 void 378 ktrinitheader(struct ktr_header *kth, struct lwp *l, int type) 379 { 380 struct proc *p = l->l_proc; 381 382 (void)memset(kth, 0, sizeof(*kth)); 383 kth->ktr_type = type; 384 kth->ktr_pid = p->p_pid; 385 memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN); 386 387 kth->ktr_version = KTRFAC_VERSION(p->p_traceflag); 388 389 switch (KTRFAC_VERSION(p->p_traceflag)) { 390 case 0: 391 /* This is the original format */ 392 microtime(&kth->ktr_tv); 393 break; 394 case 1: 395 kth->ktr_lid = l->l_lid; 396 nanotime(&kth->ktr_time); 397 break; 398 default: 399 break; 400 } 401 } 402 403 void 404 ktrsyscall(struct lwp *l, register_t code, register_t realcode, 405 const struct sysent *callp, register_t args[]) 406 { 407 struct proc *p = l->l_proc; 408 struct ktrace_entry *kte; 409 struct ktr_header *kth; 410 struct ktr_syscall *ktp; 411 register_t *argp; 412 int argsize; 413 size_t len; 414 u_int i; 415 416 if (callp == NULL) 417 callp = p->p_emul->e_sysent; 418 419 argsize = callp[code].sy_argsize; 420 #ifdef _LP64 421 if (p->p_flag & P_32) 422 argsize = argsize << 1; 423 #endif 424 len = sizeof(struct ktr_syscall) + argsize; 425 426 p->p_traceflag |= KTRFAC_ACTIVE; 427 kte = pool_get(&kte_pool, PR_WAITOK); 428 kth = &kte->kte_kth; 429 ktrinitheader(kth, l, KTR_SYSCALL); 430 431 ktp = malloc(len, M_KTRACE, M_WAITOK); 432 ktp->ktr_code = realcode; 433 ktp->ktr_argsize = argsize; 434 argp = (register_t *)(ktp + 1); 435 for (i = 0; i < (argsize / sizeof(*argp)); i++) 436 *argp++ = args[i]; 437 kth->ktr_len = len; 438 kte->kte_buf = ktp; 439 440 ktraddentry(l, kte, KTA_WAITOK); 441 p->p_traceflag &= ~KTRFAC_ACTIVE; 442 } 443 444 void 445 ktrsysret(struct lwp *l, register_t code, int error, register_t *retval) 446 { 447 struct proc *p = l->l_proc; 448 struct ktrace_entry *kte; 449 struct ktr_header *kth; 450 struct ktr_sysret *ktp; 451 452 p->p_traceflag |= KTRFAC_ACTIVE; 453 kte = pool_get(&kte_pool, PR_WAITOK); 454 kth = &kte->kte_kth; 455 ktrinitheader(kth, l, KTR_SYSRET); 456 457 ktp = malloc(sizeof(struct ktr_sysret), M_KTRACE, M_WAITOK); 458 ktp->ktr_code = code; 459 ktp->ktr_eosys = 0; /* XXX unused */ 460 ktp->ktr_error = error; 461 ktp->ktr_retval = retval ? retval[0] : 0; 462 ktp->ktr_retval_1 = retval ? retval[1] : 0; 463 464 kth->ktr_len = sizeof(struct ktr_sysret); 465 kte->kte_buf = ktp; 466 467 ktraddentry(l, kte, KTA_WAITOK); 468 p->p_traceflag &= ~KTRFAC_ACTIVE; 469 } 470 471 /* 472 * XXX: ndp->ni_pathlen should be passed. 473 */ 474 void 475 ktrnamei(struct lwp *l, char *path) 476 { 477 478 ktrkmem(l, KTR_NAMEI, path, strlen(path)); 479 } 480 481 void 482 ktremul(struct lwp *l) 483 { 484 const char *emul = l->l_proc->p_emul->e_name; 485 486 ktrkmem(l, KTR_EMUL, emul, strlen(emul)); 487 } 488 489 void 490 ktrkmem(struct lwp *l, int type, const void *bf, size_t len) 491 { 492 struct proc *p = l->l_proc; 493 struct ktrace_entry *kte; 494 struct ktr_header *kth; 495 496 p->p_traceflag |= KTRFAC_ACTIVE; 497 kte = pool_get(&kte_pool, PR_WAITOK); 498 kth = &kte->kte_kth; 499 ktrinitheader(kth, l, type); 500 501 kth->ktr_len = len; 502 kte->kte_buf = malloc(len, M_KTRACE, M_WAITOK); 503 memcpy(kte->kte_buf, bf, len); 504 505 ktraddentry(l, kte, KTA_WAITOK); 506 p->p_traceflag &= ~KTRFAC_ACTIVE; 507 } 508 509 void 510 ktrgenio(struct lwp *l, int fd, enum uio_rw rw, struct iovec *iov, 511 int len, int error) 512 { 513 struct proc *p = l->l_proc; 514 struct ktrace_entry *kte; 515 struct ktr_header *kth; 516 struct ktr_genio *ktp; 517 int resid = len, cnt; 518 caddr_t cp; 519 int buflen; 520 521 if (error) 522 return; 523 524 p->p_traceflag |= KTRFAC_ACTIVE; 525 526 next: 527 buflen = min(PAGE_SIZE, resid + sizeof(struct ktr_genio)); 528 529 kte = pool_get(&kte_pool, PR_WAITOK); 530 kth = &kte->kte_kth; 531 ktrinitheader(kth, l, KTR_GENIO); 532 533 ktp = malloc(buflen, M_KTRACE, M_WAITOK); 534 ktp->ktr_fd = fd; 535 ktp->ktr_rw = rw; 536 537 kte->kte_buf = ktp; 538 539 cp = (caddr_t)(ktp + 1); 540 buflen -= sizeof(struct ktr_genio); 541 kth->ktr_len = sizeof(struct ktr_genio); 542 543 while (buflen > 0) { 544 cnt = min(iov->iov_len, buflen); 545 if (copyin(iov->iov_base, cp, cnt) != 0) 546 goto out; 547 kth->ktr_len += cnt; 548 buflen -= cnt; 549 resid -= cnt; 550 iov->iov_len -= cnt; 551 if (iov->iov_len == 0) 552 iov++; 553 else 554 iov->iov_base = (caddr_t)iov->iov_base + cnt; 555 } 556 557 /* 558 * Don't push so many entry at once. It will cause kmem map 559 * shortage. 560 */ 561 ktraddentry(l, kte, KTA_WAITOK | KTA_LARGE); 562 if (resid > 0) { 563 #if 0 /* XXX NJWLWP */ 564 KDASSERT(p->p_cpu != NULL); 565 KDASSERT(p->p_cpu == curcpu()); 566 #endif 567 /* XXX NJWLWP */ 568 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) 569 preempt(1); 570 571 goto next; 572 } 573 574 p->p_traceflag &= ~KTRFAC_ACTIVE; 575 return; 576 577 out: 578 ktefree(kte); 579 p->p_traceflag &= ~KTRFAC_ACTIVE; 580 } 581 582 void 583 ktrpsig(struct lwp *l, int sig, sig_t action, const sigset_t *mask, 584 const ksiginfo_t *ksi) 585 { 586 struct proc *p = l->l_proc; 587 struct ktrace_entry *kte; 588 struct ktr_header *kth; 589 struct { 590 struct ktr_psig kp; 591 siginfo_t si; 592 } *kbuf; 593 594 p->p_traceflag |= KTRFAC_ACTIVE; 595 kte = pool_get(&kte_pool, PR_WAITOK); 596 kth = &kte->kte_kth; 597 ktrinitheader(kth, l, KTR_PSIG); 598 599 kbuf = malloc(sizeof(*kbuf), M_KTRACE, M_WAITOK); 600 kbuf->kp.signo = (char)sig; 601 kbuf->kp.action = action; 602 kbuf->kp.mask = *mask; 603 kte->kte_buf = kbuf; 604 if (ksi) { 605 kbuf->kp.code = KSI_TRAPCODE(ksi); 606 (void)memset(&kbuf->si, 0, sizeof(kbuf->si)); 607 kbuf->si._info = ksi->ksi_info; 608 kth->ktr_len = sizeof(*kbuf); 609 } else { 610 kbuf->kp.code = 0; 611 kth->ktr_len = sizeof(struct ktr_psig); 612 } 613 614 ktraddentry(l, kte, KTA_WAITOK); 615 p->p_traceflag &= ~KTRFAC_ACTIVE; 616 } 617 618 void 619 ktrcsw(struct lwp *l, int out, int user) 620 { 621 struct proc *p = l->l_proc; 622 struct ktrace_entry *kte; 623 struct ktr_header *kth; 624 struct ktr_csw *kc; 625 626 p->p_traceflag |= KTRFAC_ACTIVE; 627 628 /* 629 * We can't sleep if we're already going to sleep (if original 630 * condition is met during sleep, we hang up). 631 */ 632 kte = pool_get(&kte_pool, out ? PR_NOWAIT : PR_WAITOK); 633 if (kte == NULL) { 634 ktd_logerr(p, KTDE_ENOMEM); 635 goto out; 636 } 637 kth = &kte->kte_kth; 638 ktrinitheader(kth, l, KTR_CSW); 639 640 kc = malloc(sizeof(struct ktr_csw), M_KTRACE, 641 out ? M_NOWAIT : M_WAITOK); 642 if (kc == NULL) { 643 ktd_logerr(p, KTDE_ENOMEM); 644 goto free_kte; 645 } 646 kc->out = out; 647 kc->user = user; 648 kth->ktr_len = sizeof(struct ktr_csw); 649 kte->kte_buf = kc; 650 651 ktraddentry(l, kte, out ? KTA_NOWAIT : KTA_WAITOK); 652 p->p_traceflag &= ~KTRFAC_ACTIVE; 653 return; 654 655 free_kte: 656 pool_put(&kte_pool, kte); 657 out: 658 p->p_traceflag &= ~KTRFAC_ACTIVE; 659 } 660 661 void 662 ktruser(struct lwp *l, const char *id, void *addr, size_t len, int ustr) 663 { 664 struct proc *p = l->l_proc; 665 struct ktrace_entry *kte; 666 struct ktr_header *kth; 667 struct ktr_user *ktp; 668 caddr_t user_dta; 669 670 p->p_traceflag |= KTRFAC_ACTIVE; 671 kte = pool_get(&kte_pool, PR_WAITOK); 672 kth = &kte->kte_kth; 673 ktrinitheader(kth, l, KTR_USER); 674 675 ktp = malloc(sizeof(struct ktr_user) + len, M_KTRACE, M_WAITOK); 676 if (ustr) { 677 if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0) 678 ktp->ktr_id[0] = '\0'; 679 } else 680 strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN); 681 ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0'; 682 683 user_dta = (caddr_t)(ktp + 1); 684 if (copyin(addr, (void *)user_dta, len) != 0) 685 len = 0; 686 687 kth->ktr_len = sizeof(struct ktr_user) + len; 688 kte->kte_buf = ktp; 689 690 ktraddentry(l, kte, KTA_WAITOK); 691 p->p_traceflag &= ~KTRFAC_ACTIVE; 692 } 693 694 void 695 ktrmmsg(struct lwp *l, const void *msgh, size_t size) 696 { 697 ktrkmem(l, KTR_MMSG, msgh, size); 698 } 699 700 void 701 ktrmool(struct lwp *l, const void *kaddr, size_t size, const void *uaddr) 702 { 703 struct proc *p = l->l_proc; 704 struct ktrace_entry *kte; 705 struct ktr_header *kth; 706 struct ktr_mool *kp; 707 struct ktr_mool *bf; 708 709 p->p_traceflag |= KTRFAC_ACTIVE; 710 kte = pool_get(&kte_pool, PR_WAITOK); 711 kth = &kte->kte_kth; 712 ktrinitheader(kth, l, KTR_MOOL); 713 714 kp = malloc(size + sizeof(*kp), M_KTRACE, M_WAITOK); 715 kp->uaddr = uaddr; 716 kp->size = size; 717 bf = kp + 1; /* Skip uaddr and size */ 718 (void)memcpy(bf, kaddr, size); 719 720 kth->ktr_len = size + sizeof(*kp); 721 kte->kte_buf = kp; 722 723 ktraddentry(l, kte, KTA_WAITOK); 724 p->p_traceflag &= ~KTRFAC_ACTIVE; 725 } 726 727 void 728 ktrsaupcall(struct lwp *l, int type, int nevent, int nint, void *sas, 729 void *ap) 730 { 731 struct proc *p = l->l_proc; 732 struct ktrace_entry *kte; 733 struct ktr_header *kth; 734 struct ktr_saupcall *ktp; 735 size_t len; 736 struct sa_t **sapp; 737 int i; 738 739 p->p_traceflag |= KTRFAC_ACTIVE; 740 kte = pool_get(&kte_pool, PR_WAITOK); 741 kth = &kte->kte_kth; 742 ktrinitheader(kth, l, KTR_SAUPCALL); 743 744 len = sizeof(struct ktr_saupcall); 745 ktp = malloc(len + sizeof(struct sa_t) * (nevent + nint + 1), M_KTRACE, 746 M_WAITOK); 747 748 ktp->ktr_type = type; 749 ktp->ktr_nevent = nevent; 750 ktp->ktr_nint = nint; 751 ktp->ktr_sas = sas; 752 ktp->ktr_ap = ap; 753 /* 754 * Copy the sa_t's 755 */ 756 sapp = (struct sa_t **) sas; 757 758 for (i = nevent + nint; i >= 0; i--) { 759 if (copyin(*sapp, (char *)ktp + len, sizeof(struct sa_t)) == 0) 760 len += sizeof(struct sa_t); 761 sapp++; 762 } 763 764 kth->ktr_len = len; 765 kte->kte_buf = ktp; 766 767 ktraddentry(l, kte, KTA_WAITOK); 768 p->p_traceflag &= ~KTRFAC_ACTIVE; 769 } 770 771 void 772 ktrmib(l, name, namelen) 773 struct lwp *l; 774 const int *name; 775 u_int namelen; 776 { 777 struct proc *p = l->l_proc; 778 struct ktrace_entry *kte; 779 struct ktr_header *kth; 780 int *namep; 781 size_t size; 782 783 p->p_traceflag |= KTRFAC_ACTIVE; 784 kte = pool_get(&kte_pool, PR_WAITOK); 785 kth = &kte->kte_kth; 786 ktrinitheader(kth, l, KTR_MIB); 787 788 size = namelen * sizeof(*name); 789 namep = malloc(size, M_KTRACE, M_WAITOK); 790 (void)memcpy(namep, name, namelen * sizeof(*name)); 791 792 kth->ktr_len = size; 793 kte->kte_buf = namep; 794 795 ktraddentry(l, kte, KTA_WAITOK); 796 p->p_traceflag &= ~KTRFAC_ACTIVE; 797 } 798 799 /* Interface and common routines */ 800 801 int 802 ktrace_common(struct lwp *curl, int ops, int facs, int pid, struct file *fp) 803 { 804 struct proc *curp; 805 struct proc *p; 806 struct pgrp *pg; 807 struct ktr_desc *ktd = NULL; 808 int ret = 0; 809 int error = 0; 810 int descend; 811 812 curp = curl->l_proc; 813 curp->p_traceflag |= KTRFAC_ACTIVE; 814 descend = ops & KTRFLAG_DESCEND; 815 facs = facs & ~((unsigned) KTRFAC_ROOT); 816 817 switch (KTROP(ops)) { 818 819 case KTROP_CLEARFILE: 820 /* 821 * Clear all uses of the tracefile 822 */ 823 824 ktd = ktd_lookup(fp); 825 if (ktd == NULL) 826 goto done; 827 828 proclist_lock_read(); 829 PROCLIST_FOREACH(p, &allproc) { 830 if (p->p_tracep == ktd) { 831 if (ktrcanset(curl, p)) 832 ktrderef(p); 833 else 834 error = EPERM; 835 } 836 } 837 proclist_unlock_read(); 838 goto done; 839 840 case KTROP_SET: 841 ktd = ktd_lookup(fp); 842 if (ktd == NULL) { 843 ktd = malloc(sizeof(struct ktr_desc), 844 M_KTRACE, M_WAITOK); 845 TAILQ_INIT(&ktd->ktd_queue); 846 simple_lock_init(&ktd->ktd_slock); 847 callout_init(&ktd->ktd_wakch); 848 ktd->ktd_flags = ktd->ktd_qcount = 849 ktd->ktd_error = ktd->ktd_errcnt = 0; 850 ktd->ktd_ref = 1; 851 ktd->ktd_delayqcnt = ktd_delayqcnt; 852 ktd->ktd_wakedelay = mstohz(ktd_wakedelay); 853 ktd->ktd_intrwakdl = mstohz(ktd_intrwakdl); 854 /* 855 * XXX: not correct. needs an way to detect 856 * whether ktruss or ktrace. 857 */ 858 if (fp->f_type == DTYPE_PIPE) 859 ktd->ktd_flags |= KTDF_INTERACTIVE; 860 861 error = kthread_create1(ktrace_thread, ktd, 862 &ktd->ktd_proc, "ktr %p", ktd); 863 if (error != 0) { 864 free(ktd, M_KTRACE); 865 goto done; 866 } 867 868 simple_lock(&fp->f_slock); 869 fp->f_count++; 870 simple_unlock(&fp->f_slock); 871 ktd->ktd_fp = fp; 872 873 simple_lock(&ktdq_slock); 874 TAILQ_INSERT_TAIL(&ktdq, ktd, ktd_list); 875 simple_unlock(&ktdq_slock); 876 } 877 break; 878 879 case KTROP_CLEAR: 880 break; 881 } 882 883 /* 884 * need something to (un)trace (XXX - why is this here?) 885 */ 886 if (!facs) { 887 error = EINVAL; 888 goto done; 889 } 890 891 /* 892 * do it 893 */ 894 if (pid < 0) { 895 /* 896 * by process group 897 */ 898 pg = pg_find(-pid, PFIND_UNLOCK_FAIL); 899 if (pg == NULL) { 900 error = ESRCH; 901 goto done; 902 } 903 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 904 if (descend) 905 ret |= ktrsetchildren(curl, p, ops, facs, ktd); 906 else 907 ret |= ktrops(curl, p, ops, facs, ktd); 908 } 909 910 } else { 911 /* 912 * by pid 913 */ 914 p = p_find(pid, PFIND_UNLOCK_FAIL); 915 if (p == NULL) { 916 error = ESRCH; 917 goto done; 918 } 919 if (descend) 920 ret |= ktrsetchildren(curl, p, ops, facs, ktd); 921 else 922 ret |= ktrops(curl, p, ops, facs, ktd); 923 } 924 proclist_unlock_read(); /* taken by p{g}_find */ 925 if (!ret) 926 error = EPERM; 927 done: 928 if (ktd != NULL) { 929 if (error != 0) { 930 /* 931 * Wakeup the thread so that it can be die if we 932 * can't trace any process. 933 */ 934 ktd_wakeup(ktd); 935 } 936 if (KTROP(ops) == KTROP_SET || KTROP(ops) == KTROP_CLEARFILE) { 937 simple_lock(&ktd->ktd_slock); 938 ktdrel(ktd); 939 } 940 } 941 curp->p_traceflag &= ~KTRFAC_ACTIVE; 942 return (error); 943 } 944 945 /* 946 * fktrace system call 947 */ 948 /* ARGSUSED */ 949 int 950 sys_fktrace(struct lwp *l, void *v, register_t *retval __unused) 951 { 952 struct sys_fktrace_args /* { 953 syscallarg(int) fd; 954 syscallarg(int) ops; 955 syscallarg(int) facs; 956 syscallarg(int) pid; 957 } */ *uap = v; 958 struct file *fp = NULL; 959 struct filedesc *fdp = l->l_proc->p_fd; 960 int error; 961 962 fdp = l->l_proc->p_fd; 963 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) 964 return (EBADF); 965 966 FILE_USE(fp); 967 968 if ((fp->f_flag & FWRITE) == 0) 969 error = EBADF; 970 else 971 error = ktrace_common(l, SCARG(uap, ops), 972 SCARG(uap, facs), SCARG(uap, pid), fp); 973 974 FILE_UNUSE(fp, l); 975 976 return error; 977 } 978 979 /* 980 * ktrace system call 981 */ 982 /* ARGSUSED */ 983 int 984 sys_ktrace(struct lwp *l, void *v, register_t *retval __unused) 985 { 986 struct sys_ktrace_args /* { 987 syscallarg(const char *) fname; 988 syscallarg(int) ops; 989 syscallarg(int) facs; 990 syscallarg(int) pid; 991 } */ *uap = v; 992 struct proc *curp = l->l_proc; 993 struct vnode *vp = NULL; 994 struct file *fp = NULL; 995 struct nameidata nd; 996 int error = 0; 997 int fd; 998 999 curp->p_traceflag |= KTRFAC_ACTIVE; 1000 if (KTROP(SCARG(uap, ops)) != KTROP_CLEAR) { 1001 /* 1002 * an operation which requires a file argument. 1003 */ 1004 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname), 1005 l); 1006 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) { 1007 curp->p_traceflag &= ~KTRFAC_ACTIVE; 1008 return (error); 1009 } 1010 vp = nd.ni_vp; 1011 VOP_UNLOCK(vp, 0); 1012 if (vp->v_type != VREG) { 1013 (void) vn_close(vp, FREAD|FWRITE, l->l_cred, l); 1014 curp->p_traceflag &= ~KTRFAC_ACTIVE; 1015 return (EACCES); 1016 } 1017 /* 1018 * XXX This uses up a file descriptor slot in the 1019 * tracing process for the duration of this syscall. 1020 * This is not expected to be a problem. If 1021 * falloc(NULL, ...) DTRT we could skip that part, but 1022 * that would require changing its interface to allow 1023 * the caller to pass in a ucred.. 1024 * 1025 * This will FILE_USE the fp it returns, if any. 1026 * Keep it in use until we return. 1027 */ 1028 if ((error = falloc(l, &fp, &fd)) != 0) 1029 goto done; 1030 1031 fp->f_flag = FWRITE; 1032 fp->f_type = DTYPE_VNODE; 1033 fp->f_ops = &vnops; 1034 fp->f_data = (caddr_t)vp; 1035 FILE_SET_MATURE(fp); 1036 vp = NULL; 1037 } 1038 error = ktrace_common(l, SCARG(uap, ops), SCARG(uap, facs), 1039 SCARG(uap, pid), fp); 1040 done: 1041 if (vp != NULL) 1042 (void) vn_close(vp, FWRITE, l->l_cred, l); 1043 if (fp != NULL) { 1044 FILE_UNUSE(fp, l); /* release file */ 1045 fdrelease(l, fd); /* release fd table slot */ 1046 } 1047 return (error); 1048 } 1049 1050 int 1051 ktrops(struct lwp *curl, struct proc *p, int ops, int facs, 1052 struct ktr_desc *ktd) 1053 { 1054 int vers = ops & KTRFAC_VER_MASK; 1055 1056 if (!ktrcanset(curl, p)) 1057 return (0); 1058 1059 switch (vers) { 1060 case KTRFACv0: 1061 case KTRFACv1: 1062 break; 1063 default: 1064 return EINVAL; 1065 } 1066 1067 if (KTROP(ops) == KTROP_SET) { 1068 if (p->p_tracep != ktd) { 1069 /* 1070 * if trace file already in use, relinquish 1071 */ 1072 ktrderef(p); 1073 p->p_tracep = ktd; 1074 ktradref(p); 1075 } 1076 p->p_traceflag |= facs; 1077 if (kauth_cred_geteuid(curl->l_cred) == 0) 1078 p->p_traceflag |= KTRFAC_ROOT; 1079 } else { 1080 /* KTROP_CLEAR */ 1081 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) { 1082 /* no more tracing */ 1083 ktrderef(p); 1084 } 1085 } 1086 1087 if (p->p_traceflag) 1088 p->p_traceflag |= vers; 1089 /* 1090 * Emit an emulation record, every time there is a ktrace 1091 * change/attach request. 1092 */ 1093 if (KTRPOINT(p, KTR_EMUL)) 1094 p->p_traceflag |= KTRFAC_TRC_EMUL; 1095 #ifdef __HAVE_SYSCALL_INTERN 1096 (*p->p_emul->e_syscall_intern)(p); 1097 #endif 1098 1099 return (1); 1100 } 1101 1102 int 1103 ktrsetchildren(struct lwp *curl, struct proc *top, int ops, int facs, 1104 struct ktr_desc *ktd) 1105 { 1106 struct proc *p; 1107 int ret = 0; 1108 1109 p = top; 1110 for (;;) { 1111 ret |= ktrops(curl, p, ops, facs, ktd); 1112 /* 1113 * If this process has children, descend to them next, 1114 * otherwise do any siblings, and if done with this level, 1115 * follow back up the tree (but not past top). 1116 */ 1117 if (LIST_FIRST(&p->p_children) != NULL) { 1118 p = LIST_FIRST(&p->p_children); 1119 continue; 1120 } 1121 for (;;) { 1122 if (p == top) 1123 return (ret); 1124 if (LIST_NEXT(p, p_sibling) != NULL) { 1125 p = LIST_NEXT(p, p_sibling); 1126 break; 1127 } 1128 p = p->p_pptr; 1129 } 1130 } 1131 /*NOTREACHED*/ 1132 } 1133 1134 void 1135 ktrwrite(struct ktr_desc *ktd, struct ktrace_entry *kte) 1136 { 1137 struct uio auio; 1138 struct iovec aiov[64], *iov; 1139 struct ktrace_entry *top = kte; 1140 struct ktr_header *kth; 1141 struct file *fp = ktd->ktd_fp; 1142 struct proc *p; 1143 int error; 1144 next: 1145 auio.uio_iov = iov = &aiov[0]; 1146 auio.uio_offset = 0; 1147 auio.uio_rw = UIO_WRITE; 1148 auio.uio_resid = 0; 1149 auio.uio_iovcnt = 0; 1150 UIO_SETUP_SYSSPACE(&auio); 1151 do { 1152 kth = &kte->kte_kth; 1153 1154 if (kth->ktr_version == 0) { 1155 /* 1156 * Convert back to the old format fields 1157 */ 1158 TIMESPEC_TO_TIMEVAL(&kth->ktr_tv, &kth->ktr_time); 1159 kth->ktr_unused = NULL; 1160 } 1161 iov->iov_base = (caddr_t)kth; 1162 iov++->iov_len = sizeof(struct ktr_header); 1163 auio.uio_resid += sizeof(struct ktr_header); 1164 auio.uio_iovcnt++; 1165 if (kth->ktr_len > 0) { 1166 iov->iov_base = kte->kte_buf; 1167 iov++->iov_len = kth->ktr_len; 1168 auio.uio_resid += kth->ktr_len; 1169 auio.uio_iovcnt++; 1170 } 1171 } while ((kte = TAILQ_NEXT(kte, kte_list)) != NULL && 1172 auio.uio_iovcnt < sizeof(aiov) / sizeof(aiov[0]) - 1); 1173 1174 again: 1175 simple_lock(&fp->f_slock); 1176 FILE_USE(fp); 1177 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio, 1178 fp->f_cred, FOF_UPDATE_OFFSET); 1179 FILE_UNUSE(fp, NULL); 1180 switch (error) { 1181 1182 case 0: 1183 if (auio.uio_resid > 0) 1184 goto again; 1185 if (kte != NULL) 1186 goto next; 1187 break; 1188 1189 case EWOULDBLOCK: 1190 preempt(1); 1191 goto again; 1192 1193 default: 1194 /* 1195 * If error encountered, give up tracing on this 1196 * vnode. Don't report EPIPE as this can easily 1197 * happen with fktrace()/ktruss. 1198 */ 1199 #ifndef DEBUG 1200 if (error != EPIPE) 1201 #endif 1202 log(LOG_NOTICE, 1203 "ktrace write failed, errno %d, tracing stopped\n", 1204 error); 1205 proclist_lock_read(); 1206 PROCLIST_FOREACH(p, &allproc) { 1207 if (p->p_tracep == ktd) 1208 ktrderef(p); 1209 } 1210 proclist_unlock_read(); 1211 } 1212 1213 while ((kte = top) != NULL) { 1214 top = TAILQ_NEXT(top, kte_list); 1215 ktefree(kte); 1216 } 1217 } 1218 1219 void 1220 ktrace_thread(void *arg) 1221 { 1222 struct ktr_desc *ktd = arg; 1223 struct file *fp = ktd->ktd_fp; 1224 struct ktrace_entry *kte; 1225 int ktrerr, errcnt; 1226 1227 for (;;) { 1228 simple_lock(&ktd->ktd_slock); 1229 kte = TAILQ_FIRST(&ktd->ktd_queue); 1230 if (kte == NULL) { 1231 if (ktd->ktd_flags & KTDF_WAIT) { 1232 ktd->ktd_flags &= ~(KTDF_WAIT | KTDF_BLOCKING); 1233 wakeup(&ktd->ktd_flags); 1234 } 1235 if (ktd->ktd_ref == 0) 1236 break; 1237 ltsleep(ktd, PWAIT | PNORELOCK, "ktrwait", 0, 1238 &ktd->ktd_slock); 1239 continue; 1240 } 1241 TAILQ_INIT(&ktd->ktd_queue); 1242 ktd->ktd_qcount = 0; 1243 ktrerr = ktd->ktd_error; 1244 errcnt = ktd->ktd_errcnt; 1245 ktd->ktd_error = ktd->ktd_errcnt = 0; 1246 simple_unlock(&ktd->ktd_slock); 1247 1248 if (ktrerr) { 1249 log(LOG_NOTICE, 1250 "ktrace failed, fp %p, error 0x%x, total %d\n", 1251 fp, ktrerr, errcnt); 1252 } 1253 ktrwrite(ktd, kte); 1254 } 1255 simple_unlock(&ktd->ktd_slock); 1256 1257 simple_lock(&ktdq_slock); 1258 TAILQ_REMOVE(&ktdq, ktd, ktd_list); 1259 simple_unlock(&ktdq_slock); 1260 1261 simple_lock(&fp->f_slock); 1262 FILE_USE(fp); 1263 1264 /* 1265 * ktrace file descriptor can't be watched (are not visible to 1266 * userspace), so no kqueue stuff here 1267 * XXX: The above comment is wrong, because the fktrace file 1268 * descriptor is available in userland. 1269 */ 1270 closef(fp, NULL); 1271 1272 callout_stop(&ktd->ktd_wakch); 1273 free(ktd, M_KTRACE); 1274 1275 kthread_exit(0); 1276 } 1277 1278 /* 1279 * Return true if caller has permission to set the ktracing state 1280 * of target. Essentially, the target can't possess any 1281 * more permissions than the caller. KTRFAC_ROOT signifies that 1282 * root previously set the tracing status on the target process, and 1283 * so, only root may further change it. 1284 * 1285 * TODO: check groups. use caller effective gid. 1286 */ 1287 int 1288 ktrcanset(struct lwp *calll, struct proc *targetp) 1289 { 1290 kauth_cred_t caller = calll->l_cred; 1291 kauth_cred_t target = targetp->p_cred; 1292 1293 if ((kauth_cred_geteuid(caller) == kauth_cred_getuid(target) && 1294 kauth_cred_getuid(target) == kauth_cred_getsvuid(target) && 1295 kauth_cred_getgid(caller) == kauth_cred_getgid(target) && /* XXX */ 1296 kauth_cred_getgid(target) == kauth_cred_getsvgid(target) && 1297 (targetp->p_traceflag & KTRFAC_ROOT) == 0 && 1298 (targetp->p_flag & P_SUGID) == 0) || 1299 kauth_cred_geteuid(caller) == 0) 1300 return (1); 1301 1302 return (0); 1303 } 1304 #endif /* KTRACE */ 1305 1306 /* 1307 * Put user defined entry to ktrace records. 1308 */ 1309 int 1310 sys_utrace(struct lwp *l, void *v, register_t *retval __unused) 1311 { 1312 #ifdef KTRACE 1313 struct sys_utrace_args /* { 1314 syscallarg(const char *) label; 1315 syscallarg(void *) addr; 1316 syscallarg(size_t) len; 1317 } */ *uap = v; 1318 struct proc *p = l->l_proc; 1319 1320 if (!KTRPOINT(p, KTR_USER)) 1321 return (0); 1322 1323 if (SCARG(uap, len) > KTR_USER_MAXLEN) 1324 return (EINVAL); 1325 1326 ktruser(l, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1); 1327 1328 return (0); 1329 #else /* !KTRACE */ 1330 do { if (&l) {} } while (/* CONSTCOND */ 0); /* quiet -Wunused */ 1331 do { if (&v) {} } while (/* CONSTCOND */ 0); 1332 return ENOSYS; 1333 #endif /* KTRACE */ 1334 } 1335