12b3c42a9SJohn Baldwin /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 38a36da99SPedro F. Giffuni * 42b3c42a9SJohn Baldwin * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org> 562ae6c89SJason Evans * 662ae6c89SJason Evans * Redistribution and use in source and binary forms, with or without 762ae6c89SJason Evans * modification, are permitted provided that the following conditions 862ae6c89SJason Evans * are met: 962ae6c89SJason Evans * 1. Redistributions of source code must retain the above copyright 1062ae6c89SJason Evans * notice, this list of conditions and the following disclaimer. 1162ae6c89SJason Evans * 2. Redistributions in binary form must reproduce the above copyright 1262ae6c89SJason Evans * notice, this list of conditions and the following disclaimer in the 1362ae6c89SJason Evans * documentation and/or other materials provided with the distribution. 1462ae6c89SJason Evans * 152b3c42a9SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1662ae6c89SJason Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1762ae6c89SJason Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 182b3c42a9SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 192b3c42a9SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 202b3c42a9SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 212b3c42a9SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 222b3c42a9SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 232b3c42a9SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 242b3c42a9SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 252b3c42a9SJohn Baldwin * SUCH DAMAGE. 2662ae6c89SJason Evans */ 2762ae6c89SJason Evans 2862ae6c89SJason Evans /* 29d8f03321SJohn Baldwin * This module holds the global variables used by KTR and the ktr_tracepoint() 30d8f03321SJohn Baldwin * function that does the actual tracing. 3162ae6c89SJason Evans */ 3262ae6c89SJason Evans 33677b542eSDavid E. O'Brien #include <sys/cdefs.h> 34de362218SJohn Baldwin #include "opt_ddb.h" 35d8f03321SJohn Baldwin #include "opt_ktr.h" 36abee588bSJeff Roberson #include "opt_alq.h" 37d8f03321SJohn Baldwin 38de362218SJohn Baldwin #include <sys/param.h> 39e3709597SAttilio Rao #include <sys/queue.h> 40abee588bSJeff Roberson #include <sys/alq.h> 41de362218SJohn Baldwin #include <sys/cons.h> 42e3709597SAttilio Rao #include <sys/cpuset.h> 43054b57a7SMarcel Moolenaar #include <sys/kdb.h> 44a0a7328bSJohn Baldwin #include <sys/kernel.h> 4562ae6c89SJason Evans #include <sys/ktr.h> 46d8f03321SJohn Baldwin #include <sys/libkern.h> 4728d91af3SJeff Roberson #include <sys/lock.h> 4828d91af3SJeff Roberson #include <sys/malloc.h> 4928d91af3SJeff Roberson #include <sys/mutex.h> 5069e94957SJohn Baldwin #include <sys/proc.h> 5128d91af3SJeff Roberson #include <sys/smp.h> 5262ae6c89SJason Evans #include <sys/sysctl.h> 53d8f03321SJohn Baldwin #include <sys/systm.h> 541715f07dSJohn Baldwin #include <sys/time.h> 5560a57b73SJake Burkholder 5660a57b73SJake Burkholder #include <machine/cpu.h> 57d8f03321SJohn Baldwin 5819e9205aSJohn Baldwin #ifdef DDB 59de362218SJohn Baldwin #include <ddb/ddb.h> 6019e9205aSJohn Baldwin #include <ddb/db_output.h> 6119e9205aSJohn Baldwin #endif 62de362218SJohn Baldwin 6336b7dde4SAndriy Gapon #ifndef KTR_BOOT_ENTRIES 6436b7dde4SAndriy Gapon #define KTR_BOOT_ENTRIES 1024 6536b7dde4SAndriy Gapon #endif 6636b7dde4SAndriy Gapon 671715f07dSJohn Baldwin #ifndef KTR_ENTRIES 681715f07dSJohn Baldwin #define KTR_ENTRIES 1024 691715f07dSJohn Baldwin #endif 701715f07dSJohn Baldwin 7128d91af3SJeff Roberson /* Limit the allocations to something manageable. */ 7228d91af3SJeff Roberson #define KTR_ENTRIES_MAX (8 * 1024 * 1024) 7328d91af3SJeff Roberson 74d8f03321SJohn Baldwin #ifndef KTR_MASK 754e55157fSAttilio Rao #define KTR_MASK (0) 76d8f03321SJohn Baldwin #endif 77d8f03321SJohn Baldwin 78d4a2ab8cSAttilio Rao #ifndef KTR_CPUMASK 79d4a2ab8cSAttilio Rao #define KTR_CPUMASK CPUSET_FSET 80d4a2ab8cSAttilio Rao #endif 81d4a2ab8cSAttilio Rao 8260a57b73SJake Burkholder #ifndef KTR_TIME 8360a57b73SJake Burkholder #define KTR_TIME get_cyclecount() 8460a57b73SJake Burkholder #endif 8560a57b73SJake Burkholder 8660a57b73SJake Burkholder #ifndef KTR_CPU 87ef73ae4bSJake Burkholder #define KTR_CPU PCPU_GET(cpuid) 88a0a7328bSJohn Baldwin #endif 89a0a7328bSJohn Baldwin 9028d91af3SJeff Roberson static MALLOC_DEFINE(M_KTR, "KTR", "KTR"); 9128d91af3SJeff Roberson 92de5b1952SAlexander Leidinger FEATURE(ktr, "Kernel support for KTR kernel tracing facility"); 93de5b1952SAlexander Leidinger 9428d91af3SJeff Roberson volatile int ktr_idx = 0; 9521119d06SJohn Baldwin uint64_t ktr_mask = KTR_MASK; 9621119d06SJohn Baldwin uint64_t ktr_compile = KTR_COMPILE; 9736b7dde4SAndriy Gapon int ktr_entries = KTR_BOOT_ENTRIES; 9828d91af3SJeff Roberson int ktr_version = KTR_VERSION; 9936b7dde4SAndriy Gapon struct ktr_entry ktr_buf_init[KTR_BOOT_ENTRIES]; 10028d91af3SJeff Roberson struct ktr_entry *ktr_buf = ktr_buf_init; 10128d91af3SJeff Roberson cpuset_t ktr_cpumask = CPUSET_T_INITIALIZER(KTR_CPUMASK); 10228d91af3SJeff Roberson 1037029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1047029da5cSPawel Biernacki "KTR options"); 1053da1cf1eSHans Petter Selasky 1061b25979bSJohn Baldwin SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, 1071b25979bSJohn Baldwin &ktr_version, 0, "Version of the KTR interface"); 10860a57b73SJake Burkholder 10921119d06SJohn Baldwin SYSCTL_UQUAD(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD, 11028d91af3SJeff Roberson &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel"); 111e3709597SAttilio Rao 112e3709597SAttilio Rao static int 113e3709597SAttilio Rao sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS) 114e3709597SAttilio Rao { 115e3709597SAttilio Rao char lktr_cpumask_str[CPUSETBUFSIZ]; 116e3709597SAttilio Rao cpuset_t imask; 117e3709597SAttilio Rao int error; 118e3709597SAttilio Rao 119e3709597SAttilio Rao cpusetobj_strprint(lktr_cpumask_str, &ktr_cpumask); 120e3709597SAttilio Rao error = sysctl_handle_string(oidp, lktr_cpumask_str, 121e3709597SAttilio Rao sizeof(lktr_cpumask_str), req); 122e3709597SAttilio Rao if (error != 0 || req->newptr == NULL) 123e3709597SAttilio Rao return (error); 124e3709597SAttilio Rao if (cpusetobj_strscan(&imask, lktr_cpumask_str) == -1) 125e3709597SAttilio Rao return (EINVAL); 126e3709597SAttilio Rao CPU_COPY(&imask, &ktr_cpumask); 127e3709597SAttilio Rao 128e3709597SAttilio Rao return (error); 129e3709597SAttilio Rao } 130e3709597SAttilio Rao SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask, 131af3b2549SHans Petter Selasky CTLFLAG_RWTUN | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0, 132e3709597SAttilio Rao sysctl_debug_ktr_cpumask, "S", 133e3709597SAttilio Rao "Bitmask of CPUs on which KTR logging is enabled"); 134e3709597SAttilio Rao 135bef4bf1aSJohn Baldwin static int 136bef4bf1aSJohn Baldwin sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS) 137bef4bf1aSJohn Baldwin { 138bef4bf1aSJohn Baldwin int clear, error; 139bef4bf1aSJohn Baldwin 140bef4bf1aSJohn Baldwin clear = 0; 141bef4bf1aSJohn Baldwin error = sysctl_handle_int(oidp, &clear, 0, req); 142bef4bf1aSJohn Baldwin if (error || !req->newptr) 143bef4bf1aSJohn Baldwin return (error); 144bef4bf1aSJohn Baldwin 145bef4bf1aSJohn Baldwin if (clear) { 14628d91af3SJeff Roberson bzero(ktr_buf, sizeof(*ktr_buf) * ktr_entries); 147bef4bf1aSJohn Baldwin ktr_idx = 0; 148bef4bf1aSJohn Baldwin } 149bef4bf1aSJohn Baldwin 150bef4bf1aSJohn Baldwin return (error); 151bef4bf1aSJohn Baldwin } 1527029da5cSPawel Biernacki SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, 1537029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0, 1547029da5cSPawel Biernacki sysctl_debug_ktr_clear, "I", 1557029da5cSPawel Biernacki "Clear KTR Buffer"); 156bef4bf1aSJohn Baldwin 15728d91af3SJeff Roberson /* 15828d91af3SJeff Roberson * This is a sysctl proc so that it is serialized as !MPSAFE along with 15928d91af3SJeff Roberson * the other ktr sysctl procs. 16028d91af3SJeff Roberson */ 16128d91af3SJeff Roberson static int 16228d91af3SJeff Roberson sysctl_debug_ktr_mask(SYSCTL_HANDLER_ARGS) 16328d91af3SJeff Roberson { 16421119d06SJohn Baldwin uint64_t mask; 16521119d06SJohn Baldwin int error; 16628d91af3SJeff Roberson 16728d91af3SJeff Roberson mask = ktr_mask; 16821119d06SJohn Baldwin error = sysctl_handle_64(oidp, &mask, 0, req); 16928d91af3SJeff Roberson if (error || !req->newptr) 17028d91af3SJeff Roberson return (error); 17128d91af3SJeff Roberson ktr_mask = mask; 17228d91af3SJeff Roberson return (error); 17328d91af3SJeff Roberson } 17428d91af3SJeff Roberson 1757029da5cSPawel Biernacki SYSCTL_PROC(_debug_ktr, OID_AUTO, mask, 1767029da5cSPawel Biernacki CTLTYPE_U64 | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 1777029da5cSPawel Biernacki 0, 0, sysctl_debug_ktr_mask, "QU", 17828d91af3SJeff Roberson "Bitmask of KTR event classes for which logging is enabled"); 17928d91af3SJeff Roberson 180c8199bc9SAndriy Gapon #if KTR_ENTRIES > KTR_BOOT_ENTRIES 18136b7dde4SAndriy Gapon /* 18236b7dde4SAndriy Gapon * A simplified version of sysctl_debug_ktr_entries. 18336b7dde4SAndriy Gapon * No need to care about SMP, scheduling, etc. 18436b7dde4SAndriy Gapon */ 18536b7dde4SAndriy Gapon static void 18636b7dde4SAndriy Gapon ktr_entries_initializer(void *dummy __unused) 18736b7dde4SAndriy Gapon { 18821119d06SJohn Baldwin uint64_t mask; 18936b7dde4SAndriy Gapon 19036b7dde4SAndriy Gapon /* Temporarily disable ktr in case malloc() is being traced. */ 19136b7dde4SAndriy Gapon mask = ktr_mask; 19236b7dde4SAndriy Gapon ktr_mask = 0; 19336b7dde4SAndriy Gapon ktr_buf = malloc(sizeof(*ktr_buf) * KTR_ENTRIES, M_KTR, 19436b7dde4SAndriy Gapon M_WAITOK | M_ZERO); 195c43b08dcSAndriy Gapon memcpy(ktr_buf, ktr_buf_init + ktr_idx, 196c43b08dcSAndriy Gapon (KTR_BOOT_ENTRIES - ktr_idx) * sizeof(*ktr_buf)); 197d6543c67SNeel Natu if (ktr_idx != 0) { 198c43b08dcSAndriy Gapon memcpy(ktr_buf + KTR_BOOT_ENTRIES - ktr_idx, ktr_buf_init, 199c43b08dcSAndriy Gapon ktr_idx * sizeof(*ktr_buf)); 200d6543c67SNeel Natu ktr_idx = KTR_BOOT_ENTRIES; 201d6543c67SNeel Natu } 20236b7dde4SAndriy Gapon ktr_entries = KTR_ENTRIES; 20336b7dde4SAndriy Gapon ktr_mask = mask; 20436b7dde4SAndriy Gapon } 20536b7dde4SAndriy Gapon SYSINIT(ktr_entries_initializer, SI_SUB_KMEM, SI_ORDER_ANY, 20636b7dde4SAndriy Gapon ktr_entries_initializer, NULL); 20736b7dde4SAndriy Gapon #endif 20836b7dde4SAndriy Gapon 20928d91af3SJeff Roberson static int 21028d91af3SJeff Roberson sysctl_debug_ktr_entries(SYSCTL_HANDLER_ARGS) 21128d91af3SJeff Roberson { 21221119d06SJohn Baldwin uint64_t mask; 21321119d06SJohn Baldwin int entries, error; 21428d91af3SJeff Roberson struct ktr_entry *buf, *oldbuf; 21528d91af3SJeff Roberson 21628d91af3SJeff Roberson entries = ktr_entries; 21728d91af3SJeff Roberson error = sysctl_handle_int(oidp, &entries, 0, req); 21828d91af3SJeff Roberson if (error || !req->newptr) 21928d91af3SJeff Roberson return (error); 22028d91af3SJeff Roberson if (entries > KTR_ENTRIES_MAX) 22128d91af3SJeff Roberson return (ERANGE); 22228d91af3SJeff Roberson /* Disable ktr temporarily. */ 22328d91af3SJeff Roberson mask = ktr_mask; 22421119d06SJohn Baldwin ktr_mask = 0; 22528d91af3SJeff Roberson /* Wait for threads to go idle. */ 22628d91af3SJeff Roberson if ((error = quiesce_all_cpus("ktrent", PCATCH)) != 0) { 22728d91af3SJeff Roberson ktr_mask = mask; 22828d91af3SJeff Roberson return (error); 22928d91af3SJeff Roberson } 23028d91af3SJeff Roberson if (ktr_buf != ktr_buf_init) 23128d91af3SJeff Roberson oldbuf = ktr_buf; 23228d91af3SJeff Roberson else 23328d91af3SJeff Roberson oldbuf = NULL; 23428d91af3SJeff Roberson /* Allocate a new buffer. */ 23528d91af3SJeff Roberson buf = malloc(sizeof(*buf) * entries, M_KTR, M_WAITOK | M_ZERO); 23628d91af3SJeff Roberson /* Install the new buffer and restart ktr. */ 23728d91af3SJeff Roberson ktr_buf = buf; 23828d91af3SJeff Roberson ktr_entries = entries; 23928d91af3SJeff Roberson ktr_idx = 0; 24021119d06SJohn Baldwin ktr_mask = mask; 24128d91af3SJeff Roberson if (oldbuf != NULL) 24228d91af3SJeff Roberson free(oldbuf, M_KTR); 24328d91af3SJeff Roberson 24428d91af3SJeff Roberson return (error); 24528d91af3SJeff Roberson } 24628d91af3SJeff Roberson 2477029da5cSPawel Biernacki SYSCTL_PROC(_debug_ktr, OID_AUTO, entries, 2487029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 2497029da5cSPawel Biernacki 0, 0, sysctl_debug_ktr_entries, "I", 2507029da5cSPawel Biernacki "Number of entries in the KTR buffer"); 25128d91af3SJeff Roberson 25260a57b73SJake Burkholder #ifdef KTR_VERBOSE 25360a57b73SJake Burkholder int ktr_verbose = KTR_VERBOSE; 25409786698SPeter Wemm TUNABLE_INT("debug.ktr.verbose", &ktr_verbose); 255a0a7328bSJohn Baldwin SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, ""); 256d8f03321SJohn Baldwin #endif 25760a57b73SJake Burkholder 258abee588bSJeff Roberson #ifdef KTR_ALQ 259abee588bSJeff Roberson struct alq *ktr_alq; 260abee588bSJeff Roberson char ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out"; 261abee588bSJeff Roberson int ktr_alq_cnt = 0; 262abee588bSJeff Roberson int ktr_alq_depth = KTR_ENTRIES; 263abee588bSJeff Roberson int ktr_alq_enabled = 0; 264abee588bSJeff Roberson int ktr_alq_failed = 0; 265abee588bSJeff Roberson int ktr_alq_max = 0; 266abee588bSJeff Roberson 267abee588bSJeff Roberson SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0, 268abee588bSJeff Roberson "Maximum number of entries to write"); 269abee588bSJeff Roberson SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0, 270abee588bSJeff Roberson "Current number of written entries"); 271abee588bSJeff Roberson SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0, 272abee588bSJeff Roberson "Number of times we overran the buffer"); 273abee588bSJeff Roberson SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0, 274abee588bSJeff Roberson "Number of items in the write buffer"); 275abee588bSJeff Roberson SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file, 276abee588bSJeff Roberson sizeof(ktr_alq_file), "KTR logging file"); 277abee588bSJeff Roberson 278abee588bSJeff Roberson static int 279abee588bSJeff Roberson sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS) 280abee588bSJeff Roberson { 281abee588bSJeff Roberson int error; 282abee588bSJeff Roberson int enable; 283abee588bSJeff Roberson 284abee588bSJeff Roberson enable = ktr_alq_enabled; 285abee588bSJeff Roberson 286abee588bSJeff Roberson error = sysctl_handle_int(oidp, &enable, 0, req); 287abee588bSJeff Roberson if (error || !req->newptr) 288abee588bSJeff Roberson return (error); 289abee588bSJeff Roberson 290abee588bSJeff Roberson if (enable) { 291abee588bSJeff Roberson if (ktr_alq_enabled) 292abee588bSJeff Roberson return (0); 293abee588bSJeff Roberson error = alq_open(&ktr_alq, (const char *)ktr_alq_file, 294e551d452SRobert Watson req->td->td_ucred, ALQ_DEFAULT_CMODE, 295e551d452SRobert Watson sizeof(struct ktr_entry), ktr_alq_depth); 296abee588bSJeff Roberson if (error == 0) { 297abee588bSJeff Roberson ktr_alq_cnt = 0; 298abee588bSJeff Roberson ktr_alq_failed = 0; 299abee588bSJeff Roberson ktr_alq_enabled = 1; 300abee588bSJeff Roberson } 301abee588bSJeff Roberson } else { 302abee588bSJeff Roberson if (ktr_alq_enabled == 0) 303abee588bSJeff Roberson return (0); 304abee588bSJeff Roberson ktr_alq_enabled = 0; 305abee588bSJeff Roberson alq_close(ktr_alq); 306abee588bSJeff Roberson ktr_alq = NULL; 307abee588bSJeff Roberson } 308abee588bSJeff Roberson 309abee588bSJeff Roberson return (error); 310abee588bSJeff Roberson } 311abee588bSJeff Roberson SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable, 3127029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0, 3137029da5cSPawel Biernacki sysctl_debug_ktr_alq_enable, "I", 3147029da5cSPawel Biernacki "Enable KTR logging"); 315abee588bSJeff Roberson #endif 316abee588bSJeff Roberson 31760a57b73SJake Burkholder void 31821119d06SJohn Baldwin ktr_tracepoint(uint64_t mask, const char *file, int line, const char *format, 31960a57b73SJake Burkholder u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5, 32060a57b73SJake Burkholder u_long arg6) 321d8f03321SJohn Baldwin { 322d8f03321SJohn Baldwin struct ktr_entry *entry; 323abee588bSJeff Roberson #ifdef KTR_ALQ 324abee588bSJeff Roberson struct ale *ale = NULL; 325abee588bSJeff Roberson #endif 32694a04272SJulian Elischer int newindex, saveindex; 327abee588bSJeff Roberson #if defined(KTR_VERBOSE) || defined(KTR_ALQ) 32869e94957SJohn Baldwin struct thread *td; 329d8f03321SJohn Baldwin #endif 33060a57b73SJake Burkholder int cpu; 331d8f03321SJohn Baldwin 332879e0604SMateusz Guzik if (KERNEL_PANICKED() || kdb_active) 333d664747bSJohn Baldwin return; 33428d91af3SJeff Roberson if ((ktr_mask & mask) == 0 || ktr_buf == NULL) 335d8f03321SJohn Baldwin return; 336c29824dbSJohn Baldwin cpu = KTR_CPU; 337e3709597SAttilio Rao if (!CPU_ISSET(cpu, &ktr_cpumask)) 3381715f07dSJohn Baldwin return; 339abee588bSJeff Roberson #if defined(KTR_VERBOSE) || defined(KTR_ALQ) 34060a57b73SJake Burkholder td = curthread; 3415e26dcb5SJohn Baldwin if (td->td_pflags & TDP_INKTR) 34260a57b73SJake Burkholder return; 3435e26dcb5SJohn Baldwin td->td_pflags |= TDP_INKTR; 34460a57b73SJake Burkholder #endif 345abee588bSJeff Roberson #ifdef KTR_ALQ 34694a04272SJulian Elischer if (ktr_alq_enabled) { 34794a04272SJulian Elischer if (td->td_critnest == 0 && 348305bb04eSXin LI (TD_IS_IDLETHREAD(td)) == 0 && 349a414302fSJeff Roberson td != ald_thread) { 350abee588bSJeff Roberson if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max) 351abee588bSJeff Roberson goto done; 352abee588bSJeff Roberson if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) { 353abee588bSJeff Roberson ktr_alq_failed++; 354abee588bSJeff Roberson goto done; 355abee588bSJeff Roberson } 356abee588bSJeff Roberson ktr_alq_cnt++; 357abee588bSJeff Roberson entry = (struct ktr_entry *)ale->ae_data; 35894a04272SJulian Elischer } else { 359abee588bSJeff Roberson goto done; 36094a04272SJulian Elischer } 36194a04272SJulian Elischer } else 36294a04272SJulian Elischer #endif 36394a04272SJulian Elischer { 364d8f03321SJohn Baldwin do { 365d8f03321SJohn Baldwin saveindex = ktr_idx; 36628d91af3SJeff Roberson newindex = (saveindex + 1) % ktr_entries; 367d8f03321SJohn Baldwin } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0); 368d8f03321SJohn Baldwin entry = &ktr_buf[saveindex]; 36994a04272SJulian Elischer } 37060a57b73SJake Burkholder entry->ktr_timestamp = KTR_TIME; 371c29824dbSJohn Baldwin entry->ktr_cpu = cpu; 37237ee2d8dSJeff Roberson entry->ktr_thread = curthread; 3734c6ffc94SJohn Baldwin if (file != NULL) 3744c6ffc94SJohn Baldwin while (strncmp(file, "../", 3) == 0) 3754c6ffc94SJohn Baldwin file += 3; 37660a57b73SJake Burkholder entry->ktr_file = file; 377d8f03321SJohn Baldwin entry->ktr_line = line; 37860a57b73SJake Burkholder #ifdef KTR_VERBOSE 379d8f03321SJohn Baldwin if (ktr_verbose) { 38022f1b342SJohn Baldwin #ifdef SMP 38160a57b73SJake Burkholder printf("cpu%d ", cpu); 38222f1b342SJohn Baldwin #endif 38360a57b73SJake Burkholder if (ktr_verbose > 1) { 38460a57b73SJake Burkholder printf("%s.%d\t", entry->ktr_file, 38560a57b73SJake Burkholder entry->ktr_line); 386d8f03321SJohn Baldwin } 38760a57b73SJake Burkholder printf(format, arg1, arg2, arg3, arg4, arg5, arg6); 38860a57b73SJake Burkholder printf("\n"); 38960a57b73SJake Burkholder } 390d8f03321SJohn Baldwin #endif 39160a57b73SJake Burkholder entry->ktr_desc = format; 39260a57b73SJake Burkholder entry->ktr_parms[0] = arg1; 39360a57b73SJake Burkholder entry->ktr_parms[1] = arg2; 39460a57b73SJake Burkholder entry->ktr_parms[2] = arg3; 39560a57b73SJake Burkholder entry->ktr_parms[3] = arg4; 39660a57b73SJake Burkholder entry->ktr_parms[4] = arg5; 39760a57b73SJake Burkholder entry->ktr_parms[5] = arg6; 398abee588bSJeff Roberson #ifdef KTR_ALQ 39994a04272SJulian Elischer if (ktr_alq_enabled && ale) 400abee588bSJeff Roberson alq_post(ktr_alq, ale); 401abee588bSJeff Roberson done: 402abee588bSJeff Roberson #endif 403abee588bSJeff Roberson #if defined(KTR_VERBOSE) || defined(KTR_ALQ) 4045e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_INKTR; 40560a57b73SJake Burkholder #endif 406d8f03321SJohn Baldwin } 407de362218SJohn Baldwin 408de362218SJohn Baldwin #ifdef DDB 409de362218SJohn Baldwin 410de362218SJohn Baldwin struct tstate { 411de362218SJohn Baldwin int cur; 412de362218SJohn Baldwin int first; 413de362218SJohn Baldwin }; 414de362218SJohn Baldwin static struct tstate tstate; 415de362218SJohn Baldwin static int db_ktr_verbose; 416de362218SJohn Baldwin static int db_mach_vtrace(void); 417de362218SJohn Baldwin 418c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(ktr, db_ktr_all, DB_CMD_MEMSAFE) 419591faa2eSJohn Baldwin { 420591faa2eSJohn Baldwin 42128d91af3SJeff Roberson tstate.cur = (ktr_idx - 1) % ktr_entries; 422de362218SJohn Baldwin tstate.first = -1; 42394a04272SJulian Elischer db_ktr_verbose = 0; 424dc15eac0SEd Schouten db_ktr_verbose |= (strchr(modif, 'v') != NULL) ? 2 : 0; 425f3fed043SConrad Meyer db_ktr_verbose |= (strchr(modif, 'V') != NULL) ? 1 : 0; /* just timestamp please */ 426dc15eac0SEd Schouten if (strchr(modif, 'a') != NULL) { 42719e9205aSJohn Baldwin db_disable_pager(); 428f9c2ec64SAndrew Turner while (cncheckc() == -1) 4293f2a1b06SJohn Baldwin if (db_mach_vtrace() == 0) 430de362218SJohn Baldwin break; 4313f2a1b06SJohn Baldwin } else { 43219e9205aSJohn Baldwin while (!db_pager_quit) 4333f2a1b06SJohn Baldwin if (db_mach_vtrace() == 0) 434591faa2eSJohn Baldwin break; 435591faa2eSJohn Baldwin } 436de362218SJohn Baldwin } 437de362218SJohn Baldwin 438de362218SJohn Baldwin static int 439de362218SJohn Baldwin db_mach_vtrace(void) 440de362218SJohn Baldwin { 441de362218SJohn Baldwin struct ktr_entry *kp; 442de362218SJohn Baldwin 44328d91af3SJeff Roberson if (tstate.cur == tstate.first || ktr_buf == NULL) { 444de362218SJohn Baldwin db_printf("--- End of trace buffer ---\n"); 445de362218SJohn Baldwin return (0); 446de362218SJohn Baldwin } 447de362218SJohn Baldwin kp = &ktr_buf[tstate.cur]; 448de362218SJohn Baldwin 449de362218SJohn Baldwin /* Skip over unused entries. */ 450591faa2eSJohn Baldwin if (kp->ktr_desc == NULL) { 451591faa2eSJohn Baldwin db_printf("--- End of trace buffer ---\n"); 452591faa2eSJohn Baldwin return (0); 453591faa2eSJohn Baldwin } 45437ee2d8dSJeff Roberson db_printf("%d (%p", tstate.cur, kp->ktr_thread); 455de362218SJohn Baldwin #ifdef SMP 45637ee2d8dSJeff Roberson db_printf(":cpu%d", kp->ktr_cpu); 457de362218SJohn Baldwin #endif 45837ee2d8dSJeff Roberson db_printf(")"); 45994a04272SJulian Elischer if (db_ktr_verbose >= 1) { 46094a04272SJulian Elischer db_printf(" %10.10lld", (long long)kp->ktr_timestamp); 46194a04272SJulian Elischer } 46294a04272SJulian Elischer if (db_ktr_verbose >= 2) { 46394a04272SJulian Elischer db_printf(" %s.%d", kp->ktr_file, kp->ktr_line); 46460a57b73SJake Burkholder } 46537ee2d8dSJeff Roberson db_printf(": "); 46660a57b73SJake Burkholder db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1], 46760a57b73SJake Burkholder kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4], 46860a57b73SJake Burkholder kp->ktr_parms[5]); 469de362218SJohn Baldwin db_printf("\n"); 470de362218SJohn Baldwin 471de362218SJohn Baldwin if (tstate.first == -1) 472de362218SJohn Baldwin tstate.first = tstate.cur; 473de362218SJohn Baldwin 474de362218SJohn Baldwin if (--tstate.cur < 0) 47528d91af3SJeff Roberson tstate.cur = ktr_entries - 1; 476de362218SJohn Baldwin 477de362218SJohn Baldwin return (1); 478de362218SJohn Baldwin } 479de362218SJohn Baldwin 480de362218SJohn Baldwin #endif /* DDB */ 481