1 /* $NetBSD: kern_ktrace.c,v 1.104 2006/06/07 22:33:39 kardel 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.104 2006/06/07 22:33:39 kardel 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 proc *, int, int, int, struct file *); 110 static int ktrops(struct proc *, struct proc *, int, int, 111 struct ktr_desc *); 112 static int ktrsetchildren(struct proc *, struct proc *, int, int, 113 struct ktr_desc *); 114 static int ktrcanset(struct proc *, 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 /* Interface and common routines */ 772 773 int 774 ktrace_common(struct proc *curp, int ops, int facs, int pid, struct file *fp) 775 { 776 struct proc *p; 777 struct pgrp *pg; 778 struct ktr_desc *ktd = NULL; 779 int ret = 0; 780 int error = 0; 781 int descend; 782 783 curp->p_traceflag |= KTRFAC_ACTIVE; 784 descend = ops & KTRFLAG_DESCEND; 785 facs = facs & ~((unsigned) KTRFAC_ROOT); 786 787 switch (KTROP(ops)) { 788 789 case KTROP_CLEARFILE: 790 /* 791 * Clear all uses of the tracefile 792 */ 793 794 ktd = ktd_lookup(fp); 795 if (ktd == NULL) 796 goto done; 797 798 proclist_lock_read(); 799 PROCLIST_FOREACH(p, &allproc) { 800 if (p->p_tracep == ktd) { 801 if (ktrcanset(curp, p)) 802 ktrderef(p); 803 else 804 error = EPERM; 805 } 806 } 807 proclist_unlock_read(); 808 goto done; 809 810 case KTROP_SET: 811 ktd = ktd_lookup(fp); 812 if (ktd == NULL) { 813 ktd = malloc(sizeof(struct ktr_desc), 814 M_KTRACE, M_WAITOK); 815 TAILQ_INIT(&ktd->ktd_queue); 816 simple_lock_init(&ktd->ktd_slock); 817 callout_init(&ktd->ktd_wakch); 818 ktd->ktd_flags = ktd->ktd_qcount = 819 ktd->ktd_error = ktd->ktd_errcnt = 0; 820 ktd->ktd_ref = 1; 821 ktd->ktd_delayqcnt = ktd_delayqcnt; 822 ktd->ktd_wakedelay = mstohz(ktd_wakedelay); 823 ktd->ktd_intrwakdl = mstohz(ktd_intrwakdl); 824 /* 825 * XXX: not correct. needs an way to detect 826 * whether ktruss or ktrace. 827 */ 828 if (fp->f_type == DTYPE_PIPE) 829 ktd->ktd_flags |= KTDF_INTERACTIVE; 830 831 error = kthread_create1(ktrace_thread, ktd, 832 &ktd->ktd_proc, "ktr %p", ktd); 833 if (error != 0) { 834 free(ktd, M_KTRACE); 835 goto done; 836 } 837 838 simple_lock(&fp->f_slock); 839 fp->f_count++; 840 simple_unlock(&fp->f_slock); 841 ktd->ktd_fp = fp; 842 843 simple_lock(&ktdq_slock); 844 TAILQ_INSERT_TAIL(&ktdq, ktd, ktd_list); 845 simple_unlock(&ktdq_slock); 846 } 847 break; 848 849 case KTROP_CLEAR: 850 break; 851 } 852 853 /* 854 * need something to (un)trace (XXX - why is this here?) 855 */ 856 if (!facs) { 857 error = EINVAL; 858 goto done; 859 } 860 861 /* 862 * do it 863 */ 864 if (pid < 0) { 865 /* 866 * by process group 867 */ 868 pg = pg_find(-pid, PFIND_UNLOCK_FAIL); 869 if (pg == NULL) { 870 error = ESRCH; 871 goto done; 872 } 873 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 874 if (descend) 875 ret |= ktrsetchildren(curp, p, ops, facs, ktd); 876 else 877 ret |= ktrops(curp, p, ops, facs, ktd); 878 } 879 880 } else { 881 /* 882 * by pid 883 */ 884 p = p_find(pid, PFIND_UNLOCK_FAIL); 885 if (p == NULL) { 886 error = ESRCH; 887 goto done; 888 } 889 if (descend) 890 ret |= ktrsetchildren(curp, p, ops, facs, ktd); 891 else 892 ret |= ktrops(curp, p, ops, facs, ktd); 893 } 894 proclist_unlock_read(); /* taken by p{g}_find */ 895 if (!ret) 896 error = EPERM; 897 done: 898 if (ktd != NULL) { 899 if (error != 0) { 900 /* 901 * Wakeup the thread so that it can be die if we 902 * can't trace any process. 903 */ 904 ktd_wakeup(ktd); 905 } 906 if (KTROP(ops) == KTROP_SET || KTROP(ops) == KTROP_CLEARFILE) { 907 simple_lock(&ktd->ktd_slock); 908 ktdrel(ktd); 909 } 910 } 911 curp->p_traceflag &= ~KTRFAC_ACTIVE; 912 return (error); 913 } 914 915 /* 916 * fktrace system call 917 */ 918 /* ARGSUSED */ 919 int 920 sys_fktrace(struct lwp *l, void *v, register_t *retval) 921 { 922 struct sys_fktrace_args /* { 923 syscallarg(int) fd; 924 syscallarg(int) ops; 925 syscallarg(int) facs; 926 syscallarg(int) pid; 927 } */ *uap = v; 928 struct proc *curp; 929 struct file *fp = NULL; 930 struct filedesc *fdp = l->l_proc->p_fd; 931 int error; 932 933 curp = l->l_proc; 934 fdp = curp->p_fd; 935 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) 936 return (EBADF); 937 938 FILE_USE(fp); 939 940 if ((fp->f_flag & FWRITE) == 0) 941 error = EBADF; 942 else 943 error = ktrace_common(curp, SCARG(uap, ops), 944 SCARG(uap, facs), SCARG(uap, pid), fp); 945 946 FILE_UNUSE(fp, l); 947 948 return error; 949 } 950 951 /* 952 * ktrace system call 953 */ 954 /* ARGSUSED */ 955 int 956 sys_ktrace(struct lwp *l, void *v, register_t *retval) 957 { 958 struct sys_ktrace_args /* { 959 syscallarg(const char *) fname; 960 syscallarg(int) ops; 961 syscallarg(int) facs; 962 syscallarg(int) pid; 963 } */ *uap = v; 964 struct proc *curp = l->l_proc; 965 struct vnode *vp = NULL; 966 struct file *fp = NULL; 967 struct nameidata nd; 968 int error = 0; 969 int fd; 970 971 curp->p_traceflag |= KTRFAC_ACTIVE; 972 if (KTROP(SCARG(uap, ops)) != KTROP_CLEAR) { 973 /* 974 * an operation which requires a file argument. 975 */ 976 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname), 977 l); 978 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) { 979 curp->p_traceflag &= ~KTRFAC_ACTIVE; 980 return (error); 981 } 982 vp = nd.ni_vp; 983 VOP_UNLOCK(vp, 0); 984 if (vp->v_type != VREG) { 985 (void) vn_close(vp, FREAD|FWRITE, curp->p_cred, l); 986 curp->p_traceflag &= ~KTRFAC_ACTIVE; 987 return (EACCES); 988 } 989 /* 990 * XXX This uses up a file descriptor slot in the 991 * tracing process for the duration of this syscall. 992 * This is not expected to be a problem. If 993 * falloc(NULL, ...) DTRT we could skip that part, but 994 * that would require changing its interface to allow 995 * the caller to pass in a ucred.. 996 * 997 * This will FILE_USE the fp it returns, if any. 998 * Keep it in use until we return. 999 */ 1000 if ((error = falloc(curp, &fp, &fd)) != 0) 1001 goto done; 1002 1003 fp->f_flag = FWRITE; 1004 fp->f_type = DTYPE_VNODE; 1005 fp->f_ops = &vnops; 1006 fp->f_data = (caddr_t)vp; 1007 FILE_SET_MATURE(fp); 1008 vp = NULL; 1009 } 1010 error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs), 1011 SCARG(uap, pid), fp); 1012 done: 1013 if (vp != NULL) 1014 (void) vn_close(vp, FWRITE, curp->p_cred, l); 1015 if (fp != NULL) { 1016 FILE_UNUSE(fp, l); /* release file */ 1017 fdrelease(l, fd); /* release fd table slot */ 1018 } 1019 return (error); 1020 } 1021 1022 int 1023 ktrops(struct proc *curp, struct proc *p, int ops, int facs, 1024 struct ktr_desc *ktd) 1025 { 1026 1027 int vers = ops & KTRFAC_VER_MASK; 1028 1029 if (!ktrcanset(curp, p)) 1030 return (0); 1031 1032 switch (vers) { 1033 case KTRFACv0: 1034 case KTRFACv1: 1035 break; 1036 default: 1037 return EINVAL; 1038 } 1039 1040 if (KTROP(ops) == KTROP_SET) { 1041 if (p->p_tracep != ktd) { 1042 /* 1043 * if trace file already in use, relinquish 1044 */ 1045 ktrderef(p); 1046 p->p_tracep = ktd; 1047 ktradref(p); 1048 } 1049 p->p_traceflag |= facs; 1050 if (kauth_cred_geteuid(curp->p_cred) == 0) 1051 p->p_traceflag |= KTRFAC_ROOT; 1052 } else { 1053 /* KTROP_CLEAR */ 1054 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) { 1055 /* no more tracing */ 1056 ktrderef(p); 1057 } 1058 } 1059 1060 if (p->p_traceflag) 1061 p->p_traceflag |= vers; 1062 /* 1063 * Emit an emulation record, every time there is a ktrace 1064 * change/attach request. 1065 */ 1066 if (KTRPOINT(p, KTR_EMUL)) 1067 p->p_traceflag |= KTRFAC_TRC_EMUL; 1068 #ifdef __HAVE_SYSCALL_INTERN 1069 (*p->p_emul->e_syscall_intern)(p); 1070 #endif 1071 1072 return (1); 1073 } 1074 1075 int 1076 ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs, 1077 struct ktr_desc *ktd) 1078 { 1079 struct proc *p; 1080 int ret = 0; 1081 1082 p = top; 1083 for (;;) { 1084 ret |= ktrops(curp, p, ops, facs, ktd); 1085 /* 1086 * If this process has children, descend to them next, 1087 * otherwise do any siblings, and if done with this level, 1088 * follow back up the tree (but not past top). 1089 */ 1090 if (LIST_FIRST(&p->p_children) != NULL) { 1091 p = LIST_FIRST(&p->p_children); 1092 continue; 1093 } 1094 for (;;) { 1095 if (p == top) 1096 return (ret); 1097 if (LIST_NEXT(p, p_sibling) != NULL) { 1098 p = LIST_NEXT(p, p_sibling); 1099 break; 1100 } 1101 p = p->p_pptr; 1102 } 1103 } 1104 /*NOTREACHED*/ 1105 } 1106 1107 void 1108 ktrwrite(struct ktr_desc *ktd, struct ktrace_entry *kte) 1109 { 1110 struct uio auio; 1111 struct iovec aiov[64], *iov; 1112 struct ktrace_entry *top = kte; 1113 struct ktr_header *kth; 1114 struct file *fp = ktd->ktd_fp; 1115 struct proc *p; 1116 int error; 1117 next: 1118 auio.uio_iov = iov = &aiov[0]; 1119 auio.uio_offset = 0; 1120 auio.uio_rw = UIO_WRITE; 1121 auio.uio_resid = 0; 1122 auio.uio_iovcnt = 0; 1123 UIO_SETUP_SYSSPACE(&auio); 1124 do { 1125 kth = &kte->kte_kth; 1126 1127 if (kth->ktr_version == 0) { 1128 /* 1129 * Convert back to the old format fields 1130 */ 1131 TIMESPEC_TO_TIMEVAL(&kth->ktr_tv, &kth->ktr_time); 1132 kth->ktr_unused = NULL; 1133 } 1134 iov->iov_base = (caddr_t)kth; 1135 iov++->iov_len = sizeof(struct ktr_header); 1136 auio.uio_resid += sizeof(struct ktr_header); 1137 auio.uio_iovcnt++; 1138 if (kth->ktr_len > 0) { 1139 iov->iov_base = kte->kte_buf; 1140 iov++->iov_len = kth->ktr_len; 1141 auio.uio_resid += kth->ktr_len; 1142 auio.uio_iovcnt++; 1143 } 1144 } while ((kte = TAILQ_NEXT(kte, kte_list)) != NULL && 1145 auio.uio_iovcnt < sizeof(aiov) / sizeof(aiov[0]) - 1); 1146 1147 again: 1148 simple_lock(&fp->f_slock); 1149 FILE_USE(fp); 1150 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio, 1151 fp->f_cred, FOF_UPDATE_OFFSET); 1152 FILE_UNUSE(fp, NULL); 1153 switch (error) { 1154 1155 case 0: 1156 if (auio.uio_resid > 0) 1157 goto again; 1158 if (kte != NULL) 1159 goto next; 1160 break; 1161 1162 case EWOULDBLOCK: 1163 preempt(1); 1164 goto again; 1165 1166 default: 1167 /* 1168 * If error encountered, give up tracing on this 1169 * vnode. Don't report EPIPE as this can easily 1170 * happen with fktrace()/ktruss. 1171 */ 1172 #ifndef DEBUG 1173 if (error != EPIPE) 1174 #endif 1175 log(LOG_NOTICE, 1176 "ktrace write failed, errno %d, tracing stopped\n", 1177 error); 1178 proclist_lock_read(); 1179 PROCLIST_FOREACH(p, &allproc) { 1180 if (p->p_tracep == ktd) 1181 ktrderef(p); 1182 } 1183 proclist_unlock_read(); 1184 } 1185 1186 while ((kte = top) != NULL) { 1187 top = TAILQ_NEXT(top, kte_list); 1188 ktefree(kte); 1189 } 1190 } 1191 1192 void 1193 ktrace_thread(void *arg) 1194 { 1195 struct ktr_desc *ktd = arg; 1196 struct file *fp = ktd->ktd_fp; 1197 struct ktrace_entry *kte; 1198 int ktrerr, errcnt; 1199 1200 for (;;) { 1201 simple_lock(&ktd->ktd_slock); 1202 kte = TAILQ_FIRST(&ktd->ktd_queue); 1203 if (kte == NULL) { 1204 if (ktd->ktd_flags & KTDF_WAIT) { 1205 ktd->ktd_flags &= ~(KTDF_WAIT | KTDF_BLOCKING); 1206 wakeup(&ktd->ktd_flags); 1207 } 1208 if (ktd->ktd_ref == 0) 1209 break; 1210 ltsleep(ktd, PWAIT | PNORELOCK, "ktrwait", 0, 1211 &ktd->ktd_slock); 1212 continue; 1213 } 1214 TAILQ_INIT(&ktd->ktd_queue); 1215 ktd->ktd_qcount = 0; 1216 ktrerr = ktd->ktd_error; 1217 errcnt = ktd->ktd_errcnt; 1218 ktd->ktd_error = ktd->ktd_errcnt = 0; 1219 simple_unlock(&ktd->ktd_slock); 1220 1221 if (ktrerr) { 1222 log(LOG_NOTICE, 1223 "ktrace failed, fp %p, error 0x%x, total %d\n", 1224 fp, ktrerr, errcnt); 1225 } 1226 ktrwrite(ktd, kte); 1227 } 1228 simple_unlock(&ktd->ktd_slock); 1229 1230 simple_lock(&ktdq_slock); 1231 TAILQ_REMOVE(&ktdq, ktd, ktd_list); 1232 simple_unlock(&ktdq_slock); 1233 1234 simple_lock(&fp->f_slock); 1235 FILE_USE(fp); 1236 1237 /* 1238 * ktrace file descriptor can't be watched (are not visible to 1239 * userspace), so no kqueue stuff here 1240 * XXX: The above comment is wrong, because the fktrace file 1241 * descriptor is available in userland. 1242 */ 1243 closef(fp, NULL); 1244 1245 callout_stop(&ktd->ktd_wakch); 1246 free(ktd, M_KTRACE); 1247 1248 kthread_exit(0); 1249 } 1250 1251 /* 1252 * Return true if caller has permission to set the ktracing state 1253 * of target. Essentially, the target can't possess any 1254 * more permissions than the caller. KTRFAC_ROOT signifies that 1255 * root previously set the tracing status on the target process, and 1256 * so, only root may further change it. 1257 * 1258 * TODO: check groups. use caller effective gid. 1259 */ 1260 int 1261 ktrcanset(struct proc *callp, struct proc *targetp) 1262 { 1263 kauth_cred_t caller = callp->p_cred; 1264 kauth_cred_t target = targetp->p_cred; 1265 1266 if ((kauth_cred_geteuid(caller) == kauth_cred_getuid(target) && 1267 kauth_cred_getuid(target) == kauth_cred_getsvuid(target) && 1268 kauth_cred_getgid(caller) == kauth_cred_getgid(target) && /* XXX */ 1269 kauth_cred_getgid(target) == kauth_cred_getsvgid(target) && 1270 (targetp->p_traceflag & KTRFAC_ROOT) == 0 && 1271 (targetp->p_flag & P_SUGID) == 0) || 1272 kauth_cred_geteuid(caller) == 0) 1273 return (1); 1274 1275 return (0); 1276 } 1277 #endif /* KTRACE */ 1278 1279 /* 1280 * Put user defined entry to ktrace records. 1281 */ 1282 int 1283 sys_utrace(struct lwp *l, void *v, register_t *retval) 1284 { 1285 #ifdef KTRACE 1286 struct sys_utrace_args /* { 1287 syscallarg(const char *) label; 1288 syscallarg(void *) addr; 1289 syscallarg(size_t) len; 1290 } */ *uap = v; 1291 struct proc *p = l->l_proc; 1292 1293 if (!KTRPOINT(p, KTR_USER)) 1294 return (0); 1295 1296 if (SCARG(uap, len) > KTR_USER_MAXLEN) 1297 return (EINVAL); 1298 1299 ktruser(l, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1); 1300 1301 return (0); 1302 #else /* !KTRACE */ 1303 return ENOSYS; 1304 #endif /* KTRACE */ 1305 } 1306