2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This module holds the global variables used by KTR and the ktr_tracepoint()
30 * function that does the actual tracing.
33 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
42 #include <sys/cpuset.h>
44 #include <sys/kernel.h>
46 #include <sys/libkern.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
56 #include <machine/cpu.h>
60 #include <ddb/db_output.h>
63 #ifndef KTR_BOOT_ENTRIES
64 #define KTR_BOOT_ENTRIES 1024
68 #define KTR_ENTRIES 1024
71 /* Limit the allocations to something manageable. */
72 #define KTR_ENTRIES_MAX (8 * 1024 * 1024)
79 #define KTR_CPUMASK CPUSET_FSET
83 #define KTR_TIME get_cyclecount()
87 #define KTR_CPU PCPU_GET(cpuid)
90 static MALLOC_DEFINE(M_KTR, "KTR", "KTR");
92 FEATURE(ktr, "Kernel support for KTR kernel tracing facility");
94 volatile int ktr_idx = 0;
95 uint64_t ktr_mask = KTR_MASK;
96 uint64_t ktr_compile = KTR_COMPILE;
97 int ktr_entries = KTR_BOOT_ENTRIES;
98 int ktr_version = KTR_VERSION;
99 struct ktr_entry ktr_buf_init[KTR_BOOT_ENTRIES];
100 struct ktr_entry *ktr_buf = ktr_buf_init;
101 cpuset_t ktr_cpumask = CPUSET_T_INITIALIZER(KTR_CPUMASK);
103 static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
106 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD,
107 &ktr_version, 0, "Version of the KTR interface");
109 SYSCTL_UQUAD(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD,
110 &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel");
113 sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS)
115 char lktr_cpumask_str[CPUSETBUFSIZ];
119 cpusetobj_strprint(lktr_cpumask_str, &ktr_cpumask);
120 error = sysctl_handle_string(oidp, lktr_cpumask_str,
121 sizeof(lktr_cpumask_str), req);
122 if (error != 0 || req->newptr == NULL)
124 if (cpusetobj_strscan(&imask, lktr_cpumask_str) == -1)
126 CPU_COPY(&imask, &ktr_cpumask);
130 SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask,
131 CTLFLAG_RWTUN | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0,
132 sysctl_debug_ktr_cpumask, "S",
133 "Bitmask of CPUs on which KTR logging is enabled");
136 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
141 error = sysctl_handle_int(oidp, &clear, 0, req);
142 if (error || !req->newptr)
146 bzero(ktr_buf, sizeof(*ktr_buf) * ktr_entries);
152 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear,
153 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
154 sysctl_debug_ktr_clear, "I",
158 * This is a sysctl proc so that it is serialized as !MPSAFE along with
159 * the other ktr sysctl procs.
162 sysctl_debug_ktr_mask(SYSCTL_HANDLER_ARGS)
168 error = sysctl_handle_64(oidp, &mask, 0, req);
169 if (error || !req->newptr)
175 SYSCTL_PROC(_debug_ktr, OID_AUTO, mask,
176 CTLTYPE_U64 | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
177 0, 0, sysctl_debug_ktr_mask, "QU",
178 "Bitmask of KTR event classes for which logging is enabled");
180 #if KTR_ENTRIES > KTR_BOOT_ENTRIES
182 * A simplified version of sysctl_debug_ktr_entries.
183 * No need to care about SMP, scheduling, etc.
186 ktr_entries_initializer(void *dummy __unused)
190 /* Temporarily disable ktr in case malloc() is being traced. */
193 ktr_buf = malloc(sizeof(*ktr_buf) * KTR_ENTRIES, M_KTR,
195 memcpy(ktr_buf, ktr_buf_init + ktr_idx,
196 (KTR_BOOT_ENTRIES - ktr_idx) * sizeof(*ktr_buf));
198 memcpy(ktr_buf + KTR_BOOT_ENTRIES - ktr_idx, ktr_buf_init,
199 ktr_idx * sizeof(*ktr_buf));
200 ktr_idx = KTR_BOOT_ENTRIES;
202 ktr_entries = KTR_ENTRIES;
205 SYSINIT(ktr_entries_initializer, SI_SUB_KMEM, SI_ORDER_ANY,
206 ktr_entries_initializer, NULL);
210 sysctl_debug_ktr_entries(SYSCTL_HANDLER_ARGS)
214 struct ktr_entry *buf, *oldbuf;
216 entries = ktr_entries;
217 error = sysctl_handle_int(oidp, &entries, 0, req);
218 if (error || !req->newptr)
220 if (entries > KTR_ENTRIES_MAX)
222 /* Disable ktr temporarily. */
225 /* Wait for threads to go idle. */
226 if ((error = quiesce_all_cpus("ktrent", PCATCH)) != 0) {
230 if (ktr_buf != ktr_buf_init)
234 /* Allocate a new buffer. */
235 buf = malloc(sizeof(*buf) * entries, M_KTR, M_WAITOK | M_ZERO);
236 /* Install the new buffer and restart ktr. */
238 ktr_entries = entries;
247 SYSCTL_PROC(_debug_ktr, OID_AUTO, entries,
248 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
249 0, 0, sysctl_debug_ktr_entries, "I",
250 "Number of entries in the KTR buffer");
253 int ktr_verbose = KTR_VERBOSE;
254 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
255 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
260 char ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
262 int ktr_alq_depth = KTR_ENTRIES;
263 int ktr_alq_enabled = 0;
264 int ktr_alq_failed = 0;
267 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
268 "Maximum number of entries to write");
269 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
270 "Current number of written entries");
271 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
272 "Number of times we overran the buffer");
273 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
274 "Number of items in the write buffer");
275 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
276 sizeof(ktr_alq_file), "KTR logging file");
279 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
284 enable = ktr_alq_enabled;
286 error = sysctl_handle_int(oidp, &enable, 0, req);
287 if (error || !req->newptr)
293 error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
294 req->td->td_ucred, ALQ_DEFAULT_CMODE,
295 sizeof(struct ktr_entry), ktr_alq_depth);
302 if (ktr_alq_enabled == 0)
311 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
312 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
313 sysctl_debug_ktr_alq_enable, "I",
314 "Enable KTR logging");
318 ktr_tracepoint(uint64_t mask, const char *file, int line, const char *format,
319 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
322 struct ktr_entry *entry;
324 struct ale *ale = NULL;
326 int newindex, saveindex;
327 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
332 if (KERNEL_PANICKED() || kdb_active)
334 if ((ktr_mask & mask) == 0 || ktr_buf == NULL)
337 if (!CPU_ISSET(cpu, &ktr_cpumask))
339 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
341 if (td->td_pflags & TDP_INKTR)
343 td->td_pflags |= TDP_INKTR;
346 if (ktr_alq_enabled) {
347 if (td->td_critnest == 0 &&
348 (TD_IS_IDLETHREAD(td)) == 0 &&
350 if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
352 if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
357 entry = (struct ktr_entry *)ale->ae_data;
366 newindex = (saveindex + 1) % ktr_entries;
367 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
368 entry = &ktr_buf[saveindex];
370 entry->ktr_timestamp = KTR_TIME;
371 entry->ktr_cpu = cpu;
372 entry->ktr_thread = curthread;
374 while (strncmp(file, "../", 3) == 0)
376 entry->ktr_file = file;
377 entry->ktr_line = line;
381 printf("cpu%d ", cpu);
383 if (ktr_verbose > 1) {
384 printf("%s.%d\t", entry->ktr_file,
387 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
391 entry->ktr_desc = format;
392 entry->ktr_parms[0] = arg1;
393 entry->ktr_parms[1] = arg2;
394 entry->ktr_parms[2] = arg3;
395 entry->ktr_parms[3] = arg4;
396 entry->ktr_parms[4] = arg5;
397 entry->ktr_parms[5] = arg6;
399 if (ktr_alq_enabled && ale)
400 alq_post(ktr_alq, ale);
403 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
404 td->td_pflags &= ~TDP_INKTR;
414 static struct tstate tstate;
415 static int db_ktr_verbose;
416 static int db_mach_vtrace(void);
418 DB_SHOW_COMMAND_FLAGS(ktr, db_ktr_all, DB_CMD_MEMSAFE)
421 tstate.cur = (ktr_idx - 1) % ktr_entries;
424 db_ktr_verbose |= (strchr(modif, 'v') != NULL) ? 2 : 0;
425 db_ktr_verbose |= (strchr(modif, 'V') != NULL) ? 1 : 0; /* just timestamp please */
426 if (strchr(modif, 'a') != NULL) {
428 while (cncheckc() == -1)
429 if (db_mach_vtrace() == 0)
432 while (!db_pager_quit)
433 if (db_mach_vtrace() == 0)
441 struct ktr_entry *kp;
443 if (tstate.cur == tstate.first || ktr_buf == NULL) {
444 db_printf("--- End of trace buffer ---\n");
447 kp = &ktr_buf[tstate.cur];
449 /* Skip over unused entries. */
450 if (kp->ktr_desc == NULL) {
451 db_printf("--- End of trace buffer ---\n");
454 db_printf("%d (%p", tstate.cur, kp->ktr_thread);
456 db_printf(":cpu%d", kp->ktr_cpu);
459 if (db_ktr_verbose >= 1) {
460 db_printf(" %10.10lld", (long long)kp->ktr_timestamp);
462 if (db_ktr_verbose >= 2) {
463 db_printf(" %s.%d", kp->ktr_file, kp->ktr_line);
466 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
467 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
471 if (tstate.first == -1)
472 tstate.first = tstate.cur;
474 if (--tstate.cur < 0)
475 tstate.cur = ktr_entries - 1;