12b3c42a9SJohn Baldwin /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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> 34677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 35677b542eSDavid E. O'Brien 36de362218SJohn Baldwin #include "opt_ddb.h" 37d8f03321SJohn Baldwin #include "opt_ktr.h" 38abee588bSJeff Roberson #include "opt_alq.h" 39d8f03321SJohn Baldwin 40de362218SJohn Baldwin #include <sys/param.h> 41e3709597SAttilio Rao #include <sys/queue.h> 42abee588bSJeff Roberson #include <sys/alq.h> 43de362218SJohn Baldwin #include <sys/cons.h> 44e3709597SAttilio Rao #include <sys/cpuset.h> 45054b57a7SMarcel Moolenaar #include <sys/kdb.h> 46a0a7328bSJohn Baldwin #include <sys/kernel.h> 4762ae6c89SJason Evans #include <sys/ktr.h> 48d8f03321SJohn Baldwin #include <sys/libkern.h> 4928d91af3SJeff Roberson #include <sys/lock.h> 5028d91af3SJeff Roberson #include <sys/malloc.h> 5128d91af3SJeff Roberson #include <sys/mutex.h> 5269e94957SJohn Baldwin #include <sys/proc.h> 5328d91af3SJeff Roberson #include <sys/smp.h> 5462ae6c89SJason Evans #include <sys/sysctl.h> 55d8f03321SJohn Baldwin #include <sys/systm.h> 561715f07dSJohn Baldwin #include <sys/time.h> 5760a57b73SJake Burkholder 5860a57b73SJake Burkholder #include <machine/cpu.h> 59d8f03321SJohn Baldwin 6019e9205aSJohn Baldwin #ifdef DDB 61de362218SJohn Baldwin #include <ddb/ddb.h> 6219e9205aSJohn Baldwin #include <ddb/db_output.h> 6319e9205aSJohn Baldwin #endif 64de362218SJohn Baldwin 6536b7dde4SAndriy Gapon #ifndef KTR_BOOT_ENTRIES 6636b7dde4SAndriy Gapon #define KTR_BOOT_ENTRIES 1024 6736b7dde4SAndriy Gapon #endif 6836b7dde4SAndriy Gapon 691715f07dSJohn Baldwin #ifndef KTR_ENTRIES 701715f07dSJohn Baldwin #define KTR_ENTRIES 1024 711715f07dSJohn Baldwin #endif 721715f07dSJohn Baldwin 7328d91af3SJeff Roberson /* Limit the allocations to something manageable. */ 7428d91af3SJeff Roberson #define KTR_ENTRIES_MAX (8 * 1024 * 1024) 7528d91af3SJeff Roberson 76d8f03321SJohn Baldwin #ifndef KTR_MASK 774e55157fSAttilio Rao #define KTR_MASK (0) 78d8f03321SJohn Baldwin #endif 79d8f03321SJohn Baldwin 80d4a2ab8cSAttilio Rao #ifndef KTR_CPUMASK 81d4a2ab8cSAttilio Rao #define KTR_CPUMASK CPUSET_FSET 82d4a2ab8cSAttilio Rao #endif 83d4a2ab8cSAttilio Rao 8460a57b73SJake Burkholder #ifndef KTR_TIME 8560a57b73SJake Burkholder #define KTR_TIME get_cyclecount() 8660a57b73SJake Burkholder #endif 8760a57b73SJake Burkholder 8860a57b73SJake Burkholder #ifndef KTR_CPU 89ef73ae4bSJake Burkholder #define KTR_CPU PCPU_GET(cpuid) 90a0a7328bSJohn Baldwin #endif 91a0a7328bSJohn Baldwin 9228d91af3SJeff Roberson static MALLOC_DEFINE(M_KTR, "KTR", "KTR"); 9328d91af3SJeff Roberson 94de5b1952SAlexander Leidinger FEATURE(ktr, "Kernel support for KTR kernel tracing facility"); 95de5b1952SAlexander Leidinger 9628d91af3SJeff Roberson volatile int ktr_idx = 0; 9721119d06SJohn Baldwin uint64_t ktr_mask = KTR_MASK; 9821119d06SJohn Baldwin uint64_t ktr_compile = KTR_COMPILE; 9936b7dde4SAndriy Gapon int ktr_entries = KTR_BOOT_ENTRIES; 10028d91af3SJeff Roberson int ktr_version = KTR_VERSION; 10136b7dde4SAndriy Gapon struct ktr_entry ktr_buf_init[KTR_BOOT_ENTRIES]; 10228d91af3SJeff Roberson struct ktr_entry *ktr_buf = ktr_buf_init; 10328d91af3SJeff Roberson cpuset_t ktr_cpumask = CPUSET_T_INITIALIZER(KTR_CPUMASK); 10428d91af3SJeff Roberson 1057029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1067029da5cSPawel Biernacki "KTR options"); 1073da1cf1eSHans Petter Selasky 1081b25979bSJohn Baldwin SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, 1091b25979bSJohn Baldwin &ktr_version, 0, "Version of the KTR interface"); 11060a57b73SJake Burkholder 11121119d06SJohn Baldwin SYSCTL_UQUAD(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD, 11228d91af3SJeff Roberson &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel"); 113e3709597SAttilio Rao 114e3709597SAttilio Rao static int 115e3709597SAttilio Rao sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS) 116e3709597SAttilio Rao { 117e3709597SAttilio Rao char lktr_cpumask_str[CPUSETBUFSIZ]; 118e3709597SAttilio Rao cpuset_t imask; 119e3709597SAttilio Rao int error; 120e3709597SAttilio Rao 121e3709597SAttilio Rao cpusetobj_strprint(lktr_cpumask_str, &ktr_cpumask); 122e3709597SAttilio Rao error = sysctl_handle_string(oidp, lktr_cpumask_str, 123e3709597SAttilio Rao sizeof(lktr_cpumask_str), req); 124e3709597SAttilio Rao if (error != 0 || req->newptr == NULL) 125e3709597SAttilio Rao return (error); 126e3709597SAttilio Rao if (cpusetobj_strscan(&imask, lktr_cpumask_str) == -1) 127e3709597SAttilio Rao return (EINVAL); 128e3709597SAttilio Rao CPU_COPY(&imask, &ktr_cpumask); 129e3709597SAttilio Rao 130e3709597SAttilio Rao return (error); 131e3709597SAttilio Rao } 132e3709597SAttilio Rao SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask, 133af3b2549SHans Petter Selasky CTLFLAG_RWTUN | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0, 134e3709597SAttilio Rao sysctl_debug_ktr_cpumask, "S", 135e3709597SAttilio Rao "Bitmask of CPUs on which KTR logging is enabled"); 136e3709597SAttilio Rao 137bef4bf1aSJohn Baldwin static int 138bef4bf1aSJohn Baldwin sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS) 139bef4bf1aSJohn Baldwin { 140bef4bf1aSJohn Baldwin int clear, error; 141bef4bf1aSJohn Baldwin 142bef4bf1aSJohn Baldwin clear = 0; 143bef4bf1aSJohn Baldwin error = sysctl_handle_int(oidp, &clear, 0, req); 144bef4bf1aSJohn Baldwin if (error || !req->newptr) 145bef4bf1aSJohn Baldwin return (error); 146bef4bf1aSJohn Baldwin 147bef4bf1aSJohn Baldwin if (clear) { 14828d91af3SJeff Roberson bzero(ktr_buf, sizeof(*ktr_buf) * ktr_entries); 149bef4bf1aSJohn Baldwin ktr_idx = 0; 150bef4bf1aSJohn Baldwin } 151bef4bf1aSJohn Baldwin 152bef4bf1aSJohn Baldwin return (error); 153bef4bf1aSJohn Baldwin } 1547029da5cSPawel Biernacki SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, 1557029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0, 1567029da5cSPawel Biernacki sysctl_debug_ktr_clear, "I", 1577029da5cSPawel Biernacki "Clear KTR Buffer"); 158bef4bf1aSJohn Baldwin 15928d91af3SJeff Roberson /* 16028d91af3SJeff Roberson * This is a sysctl proc so that it is serialized as !MPSAFE along with 16128d91af3SJeff Roberson * the other ktr sysctl procs. 16228d91af3SJeff Roberson */ 16328d91af3SJeff Roberson static int 16428d91af3SJeff Roberson sysctl_debug_ktr_mask(SYSCTL_HANDLER_ARGS) 16528d91af3SJeff Roberson { 16621119d06SJohn Baldwin uint64_t mask; 16721119d06SJohn Baldwin int error; 16828d91af3SJeff Roberson 16928d91af3SJeff Roberson mask = ktr_mask; 17021119d06SJohn Baldwin error = sysctl_handle_64(oidp, &mask, 0, req); 17128d91af3SJeff Roberson if (error || !req->newptr) 17228d91af3SJeff Roberson return (error); 17328d91af3SJeff Roberson ktr_mask = mask; 17428d91af3SJeff Roberson return (error); 17528d91af3SJeff Roberson } 17628d91af3SJeff Roberson 1777029da5cSPawel Biernacki SYSCTL_PROC(_debug_ktr, OID_AUTO, mask, 1787029da5cSPawel Biernacki CTLTYPE_U64 | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 1797029da5cSPawel Biernacki 0, 0, sysctl_debug_ktr_mask, "QU", 18028d91af3SJeff Roberson "Bitmask of KTR event classes for which logging is enabled"); 18128d91af3SJeff Roberson 182c8199bc9SAndriy Gapon #if KTR_ENTRIES > KTR_BOOT_ENTRIES 18336b7dde4SAndriy Gapon /* 18436b7dde4SAndriy Gapon * A simplified version of sysctl_debug_ktr_entries. 18536b7dde4SAndriy Gapon * No need to care about SMP, scheduling, etc. 18636b7dde4SAndriy Gapon */ 18736b7dde4SAndriy Gapon static void 18836b7dde4SAndriy Gapon ktr_entries_initializer(void *dummy __unused) 18936b7dde4SAndriy Gapon { 19021119d06SJohn Baldwin uint64_t mask; 19136b7dde4SAndriy Gapon 19236b7dde4SAndriy Gapon /* Temporarily disable ktr in case malloc() is being traced. */ 19336b7dde4SAndriy Gapon mask = ktr_mask; 19436b7dde4SAndriy Gapon ktr_mask = 0; 19536b7dde4SAndriy Gapon ktr_buf = malloc(sizeof(*ktr_buf) * KTR_ENTRIES, M_KTR, 19636b7dde4SAndriy Gapon M_WAITOK | M_ZERO); 197c43b08dcSAndriy Gapon memcpy(ktr_buf, ktr_buf_init + ktr_idx, 198c43b08dcSAndriy Gapon (KTR_BOOT_ENTRIES - ktr_idx) * sizeof(*ktr_buf)); 199d6543c67SNeel Natu if (ktr_idx != 0) { 200c43b08dcSAndriy Gapon memcpy(ktr_buf + KTR_BOOT_ENTRIES - ktr_idx, ktr_buf_init, 201c43b08dcSAndriy Gapon ktr_idx * sizeof(*ktr_buf)); 202d6543c67SNeel Natu ktr_idx = KTR_BOOT_ENTRIES; 203d6543c67SNeel Natu } 20436b7dde4SAndriy Gapon ktr_entries = KTR_ENTRIES; 20536b7dde4SAndriy Gapon ktr_mask = mask; 20636b7dde4SAndriy Gapon } 20736b7dde4SAndriy Gapon SYSINIT(ktr_entries_initializer, SI_SUB_KMEM, SI_ORDER_ANY, 20836b7dde4SAndriy Gapon ktr_entries_initializer, NULL); 20936b7dde4SAndriy Gapon #endif 21036b7dde4SAndriy Gapon 21128d91af3SJeff Roberson static int 21228d91af3SJeff Roberson sysctl_debug_ktr_entries(SYSCTL_HANDLER_ARGS) 21328d91af3SJeff Roberson { 21421119d06SJohn Baldwin uint64_t mask; 21521119d06SJohn Baldwin int entries, error; 21628d91af3SJeff Roberson struct ktr_entry *buf, *oldbuf; 21728d91af3SJeff Roberson 21828d91af3SJeff Roberson entries = ktr_entries; 21928d91af3SJeff Roberson error = sysctl_handle_int(oidp, &entries, 0, req); 22028d91af3SJeff Roberson if (error || !req->newptr) 22128d91af3SJeff Roberson return (error); 22228d91af3SJeff Roberson if (entries > KTR_ENTRIES_MAX) 22328d91af3SJeff Roberson return (ERANGE); 22428d91af3SJeff Roberson /* Disable ktr temporarily. */ 22528d91af3SJeff Roberson mask = ktr_mask; 22621119d06SJohn Baldwin ktr_mask = 0; 22728d91af3SJeff Roberson /* Wait for threads to go idle. */ 22828d91af3SJeff Roberson if ((error = quiesce_all_cpus("ktrent", PCATCH)) != 0) { 22928d91af3SJeff Roberson ktr_mask = mask; 23028d91af3SJeff Roberson return (error); 23128d91af3SJeff Roberson } 23228d91af3SJeff Roberson if (ktr_buf != ktr_buf_init) 23328d91af3SJeff Roberson oldbuf = ktr_buf; 23428d91af3SJeff Roberson else 23528d91af3SJeff Roberson oldbuf = NULL; 23628d91af3SJeff Roberson /* Allocate a new buffer. */ 23728d91af3SJeff Roberson buf = malloc(sizeof(*buf) * entries, M_KTR, M_WAITOK | M_ZERO); 23828d91af3SJeff Roberson /* Install the new buffer and restart ktr. */ 23928d91af3SJeff Roberson ktr_buf = buf; 24028d91af3SJeff Roberson ktr_entries = entries; 24128d91af3SJeff Roberson ktr_idx = 0; 24221119d06SJohn Baldwin ktr_mask = mask; 24328d91af3SJeff Roberson if (oldbuf != NULL) 24428d91af3SJeff Roberson free(oldbuf, M_KTR); 24528d91af3SJeff Roberson 24628d91af3SJeff Roberson return (error); 24728d91af3SJeff Roberson } 24828d91af3SJeff Roberson 2497029da5cSPawel Biernacki SYSCTL_PROC(_debug_ktr, OID_AUTO, entries, 2507029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 2517029da5cSPawel Biernacki 0, 0, sysctl_debug_ktr_entries, "I", 2527029da5cSPawel Biernacki "Number of entries in the KTR buffer"); 25328d91af3SJeff Roberson 25460a57b73SJake Burkholder #ifdef KTR_VERBOSE 25560a57b73SJake Burkholder int ktr_verbose = KTR_VERBOSE; 25609786698SPeter Wemm TUNABLE_INT("debug.ktr.verbose", &ktr_verbose); 257a0a7328bSJohn Baldwin SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, ""); 258d8f03321SJohn Baldwin #endif 25960a57b73SJake Burkholder 260abee588bSJeff Roberson #ifdef KTR_ALQ 261abee588bSJeff Roberson struct alq *ktr_alq; 262abee588bSJeff Roberson char ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out"; 263abee588bSJeff Roberson int ktr_alq_cnt = 0; 264abee588bSJeff Roberson int ktr_alq_depth = KTR_ENTRIES; 265abee588bSJeff Roberson int ktr_alq_enabled = 0; 266abee588bSJeff Roberson int ktr_alq_failed = 0; 267abee588bSJeff Roberson int ktr_alq_max = 0; 268abee588bSJeff Roberson 269abee588bSJeff Roberson SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0, 270abee588bSJeff Roberson "Maximum number of entries to write"); 271abee588bSJeff Roberson SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0, 272abee588bSJeff Roberson "Current number of written entries"); 273abee588bSJeff Roberson SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0, 274abee588bSJeff Roberson "Number of times we overran the buffer"); 275abee588bSJeff Roberson SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0, 276abee588bSJeff Roberson "Number of items in the write buffer"); 277abee588bSJeff Roberson SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file, 278abee588bSJeff Roberson sizeof(ktr_alq_file), "KTR logging file"); 279abee588bSJeff Roberson 280abee588bSJeff Roberson static int 281abee588bSJeff Roberson sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS) 282abee588bSJeff Roberson { 283abee588bSJeff Roberson int error; 284abee588bSJeff Roberson int enable; 285abee588bSJeff Roberson 286abee588bSJeff Roberson enable = ktr_alq_enabled; 287abee588bSJeff Roberson 288abee588bSJeff Roberson error = sysctl_handle_int(oidp, &enable, 0, req); 289abee588bSJeff Roberson if (error || !req->newptr) 290abee588bSJeff Roberson return (error); 291abee588bSJeff Roberson 292abee588bSJeff Roberson if (enable) { 293abee588bSJeff Roberson if (ktr_alq_enabled) 294abee588bSJeff Roberson return (0); 295abee588bSJeff Roberson error = alq_open(&ktr_alq, (const char *)ktr_alq_file, 296e551d452SRobert Watson req->td->td_ucred, ALQ_DEFAULT_CMODE, 297e551d452SRobert Watson sizeof(struct ktr_entry), ktr_alq_depth); 298abee588bSJeff Roberson if (error == 0) { 299abee588bSJeff Roberson ktr_alq_cnt = 0; 300abee588bSJeff Roberson ktr_alq_failed = 0; 301abee588bSJeff Roberson ktr_alq_enabled = 1; 302abee588bSJeff Roberson } 303abee588bSJeff Roberson } else { 304abee588bSJeff Roberson if (ktr_alq_enabled == 0) 305abee588bSJeff Roberson return (0); 306abee588bSJeff Roberson ktr_alq_enabled = 0; 307abee588bSJeff Roberson alq_close(ktr_alq); 308abee588bSJeff Roberson ktr_alq = NULL; 309abee588bSJeff Roberson } 310abee588bSJeff Roberson 311abee588bSJeff Roberson return (error); 312abee588bSJeff Roberson } 313abee588bSJeff Roberson SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable, 3147029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0, 3157029da5cSPawel Biernacki sysctl_debug_ktr_alq_enable, "I", 3167029da5cSPawel Biernacki "Enable KTR logging"); 317abee588bSJeff Roberson #endif 318abee588bSJeff Roberson 31960a57b73SJake Burkholder void 32021119d06SJohn Baldwin ktr_tracepoint(uint64_t mask, const char *file, int line, const char *format, 32160a57b73SJake Burkholder u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5, 32260a57b73SJake Burkholder u_long arg6) 323d8f03321SJohn Baldwin { 324d8f03321SJohn Baldwin struct ktr_entry *entry; 325abee588bSJeff Roberson #ifdef KTR_ALQ 326abee588bSJeff Roberson struct ale *ale = NULL; 327abee588bSJeff Roberson #endif 32894a04272SJulian Elischer int newindex, saveindex; 329abee588bSJeff Roberson #if defined(KTR_VERBOSE) || defined(KTR_ALQ) 33069e94957SJohn Baldwin struct thread *td; 331d8f03321SJohn Baldwin #endif 33260a57b73SJake Burkholder int cpu; 333d8f03321SJohn Baldwin 334879e0604SMateusz Guzik if (KERNEL_PANICKED() || kdb_active) 335d664747bSJohn Baldwin return; 33628d91af3SJeff Roberson if ((ktr_mask & mask) == 0 || ktr_buf == NULL) 337d8f03321SJohn Baldwin return; 338c29824dbSJohn Baldwin cpu = KTR_CPU; 339e3709597SAttilio Rao if (!CPU_ISSET(cpu, &ktr_cpumask)) 3401715f07dSJohn Baldwin return; 341abee588bSJeff Roberson #if defined(KTR_VERBOSE) || defined(KTR_ALQ) 34260a57b73SJake Burkholder td = curthread; 3435e26dcb5SJohn Baldwin if (td->td_pflags & TDP_INKTR) 34460a57b73SJake Burkholder return; 3455e26dcb5SJohn Baldwin td->td_pflags |= TDP_INKTR; 34660a57b73SJake Burkholder #endif 347abee588bSJeff Roberson #ifdef KTR_ALQ 34894a04272SJulian Elischer if (ktr_alq_enabled) { 34994a04272SJulian Elischer if (td->td_critnest == 0 && 350305bb04eSXin LI (TD_IS_IDLETHREAD(td)) == 0 && 351a414302fSJeff Roberson td != ald_thread) { 352abee588bSJeff Roberson if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max) 353abee588bSJeff Roberson goto done; 354abee588bSJeff Roberson if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) { 355abee588bSJeff Roberson ktr_alq_failed++; 356abee588bSJeff Roberson goto done; 357abee588bSJeff Roberson } 358abee588bSJeff Roberson ktr_alq_cnt++; 359abee588bSJeff Roberson entry = (struct ktr_entry *)ale->ae_data; 36094a04272SJulian Elischer } else { 361abee588bSJeff Roberson goto done; 36294a04272SJulian Elischer } 36394a04272SJulian Elischer } else 36494a04272SJulian Elischer #endif 36594a04272SJulian Elischer { 366d8f03321SJohn Baldwin do { 367d8f03321SJohn Baldwin saveindex = ktr_idx; 36828d91af3SJeff Roberson newindex = (saveindex + 1) % ktr_entries; 369d8f03321SJohn Baldwin } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0); 370d8f03321SJohn Baldwin entry = &ktr_buf[saveindex]; 37194a04272SJulian Elischer } 37260a57b73SJake Burkholder entry->ktr_timestamp = KTR_TIME; 373c29824dbSJohn Baldwin entry->ktr_cpu = cpu; 37437ee2d8dSJeff Roberson entry->ktr_thread = curthread; 3754c6ffc94SJohn Baldwin if (file != NULL) 3764c6ffc94SJohn Baldwin while (strncmp(file, "../", 3) == 0) 3774c6ffc94SJohn Baldwin file += 3; 37860a57b73SJake Burkholder entry->ktr_file = file; 379d8f03321SJohn Baldwin entry->ktr_line = line; 38060a57b73SJake Burkholder #ifdef KTR_VERBOSE 381d8f03321SJohn Baldwin if (ktr_verbose) { 38222f1b342SJohn Baldwin #ifdef SMP 38360a57b73SJake Burkholder printf("cpu%d ", cpu); 38422f1b342SJohn Baldwin #endif 38560a57b73SJake Burkholder if (ktr_verbose > 1) { 38660a57b73SJake Burkholder printf("%s.%d\t", entry->ktr_file, 38760a57b73SJake Burkholder entry->ktr_line); 388d8f03321SJohn Baldwin } 38960a57b73SJake Burkholder printf(format, arg1, arg2, arg3, arg4, arg5, arg6); 39060a57b73SJake Burkholder printf("\n"); 39160a57b73SJake Burkholder } 392d8f03321SJohn Baldwin #endif 39360a57b73SJake Burkholder entry->ktr_desc = format; 39460a57b73SJake Burkholder entry->ktr_parms[0] = arg1; 39560a57b73SJake Burkholder entry->ktr_parms[1] = arg2; 39660a57b73SJake Burkholder entry->ktr_parms[2] = arg3; 39760a57b73SJake Burkholder entry->ktr_parms[3] = arg4; 39860a57b73SJake Burkholder entry->ktr_parms[4] = arg5; 39960a57b73SJake Burkholder entry->ktr_parms[5] = arg6; 400abee588bSJeff Roberson #ifdef KTR_ALQ 40194a04272SJulian Elischer if (ktr_alq_enabled && ale) 402abee588bSJeff Roberson alq_post(ktr_alq, ale); 403abee588bSJeff Roberson done: 404abee588bSJeff Roberson #endif 405abee588bSJeff Roberson #if defined(KTR_VERBOSE) || defined(KTR_ALQ) 4065e26dcb5SJohn Baldwin td->td_pflags &= ~TDP_INKTR; 40760a57b73SJake Burkholder #endif 408d8f03321SJohn Baldwin } 409de362218SJohn Baldwin 410de362218SJohn Baldwin #ifdef DDB 411de362218SJohn Baldwin 412de362218SJohn Baldwin struct tstate { 413de362218SJohn Baldwin int cur; 414de362218SJohn Baldwin int first; 415de362218SJohn Baldwin }; 416de362218SJohn Baldwin static struct tstate tstate; 417de362218SJohn Baldwin static int db_ktr_verbose; 418de362218SJohn Baldwin static int db_mach_vtrace(void); 419de362218SJohn Baldwin 420*c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(ktr, db_ktr_all, DB_CMD_MEMSAFE) 421591faa2eSJohn Baldwin { 422591faa2eSJohn Baldwin 42328d91af3SJeff Roberson tstate.cur = (ktr_idx - 1) % ktr_entries; 424de362218SJohn Baldwin tstate.first = -1; 42594a04272SJulian Elischer db_ktr_verbose = 0; 426dc15eac0SEd Schouten db_ktr_verbose |= (strchr(modif, 'v') != NULL) ? 2 : 0; 427f3fed043SConrad Meyer db_ktr_verbose |= (strchr(modif, 'V') != NULL) ? 1 : 0; /* just timestamp please */ 428dc15eac0SEd Schouten if (strchr(modif, 'a') != NULL) { 42919e9205aSJohn Baldwin db_disable_pager(); 430f9c2ec64SAndrew Turner while (cncheckc() == -1) 4313f2a1b06SJohn Baldwin if (db_mach_vtrace() == 0) 432de362218SJohn Baldwin break; 4333f2a1b06SJohn Baldwin } else { 43419e9205aSJohn Baldwin while (!db_pager_quit) 4353f2a1b06SJohn Baldwin if (db_mach_vtrace() == 0) 436591faa2eSJohn Baldwin break; 437591faa2eSJohn Baldwin } 438de362218SJohn Baldwin } 439de362218SJohn Baldwin 440de362218SJohn Baldwin static int 441de362218SJohn Baldwin db_mach_vtrace(void) 442de362218SJohn Baldwin { 443de362218SJohn Baldwin struct ktr_entry *kp; 444de362218SJohn Baldwin 44528d91af3SJeff Roberson if (tstate.cur == tstate.first || ktr_buf == NULL) { 446de362218SJohn Baldwin db_printf("--- End of trace buffer ---\n"); 447de362218SJohn Baldwin return (0); 448de362218SJohn Baldwin } 449de362218SJohn Baldwin kp = &ktr_buf[tstate.cur]; 450de362218SJohn Baldwin 451de362218SJohn Baldwin /* Skip over unused entries. */ 452591faa2eSJohn Baldwin if (kp->ktr_desc == NULL) { 453591faa2eSJohn Baldwin db_printf("--- End of trace buffer ---\n"); 454591faa2eSJohn Baldwin return (0); 455591faa2eSJohn Baldwin } 45637ee2d8dSJeff Roberson db_printf("%d (%p", tstate.cur, kp->ktr_thread); 457de362218SJohn Baldwin #ifdef SMP 45837ee2d8dSJeff Roberson db_printf(":cpu%d", kp->ktr_cpu); 459de362218SJohn Baldwin #endif 46037ee2d8dSJeff Roberson db_printf(")"); 46194a04272SJulian Elischer if (db_ktr_verbose >= 1) { 46294a04272SJulian Elischer db_printf(" %10.10lld", (long long)kp->ktr_timestamp); 46394a04272SJulian Elischer } 46494a04272SJulian Elischer if (db_ktr_verbose >= 2) { 46594a04272SJulian Elischer db_printf(" %s.%d", kp->ktr_file, kp->ktr_line); 46660a57b73SJake Burkholder } 46737ee2d8dSJeff Roberson db_printf(": "); 46860a57b73SJake Burkholder db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1], 46960a57b73SJake Burkholder kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4], 47060a57b73SJake Burkholder kp->ktr_parms[5]); 471de362218SJohn Baldwin db_printf("\n"); 472de362218SJohn Baldwin 473de362218SJohn Baldwin if (tstate.first == -1) 474de362218SJohn Baldwin tstate.first = tstate.cur; 475de362218SJohn Baldwin 476de362218SJohn Baldwin if (--tstate.cur < 0) 47728d91af3SJeff Roberson tstate.cur = ktr_entries - 1; 478de362218SJohn Baldwin 479de362218SJohn Baldwin return (1); 480de362218SJohn Baldwin } 481de362218SJohn Baldwin 482de362218SJohn Baldwin #endif /* DDB */ 483