2 * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * This module holds the global variables used by KTR and the ktr_tracepoint()
32 * function that does the actual tracing.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/queue.h>
46 #include <sys/cpuset.h>
47 #include <sys/kernel.h>
49 #include <sys/libkern.h>
51 #include <sys/malloc.h>
52 #include <sys/mutex.h>
55 #include <sys/sysctl.h>
56 #include <sys/systm.h>
59 #include <machine/cpu.h>
61 #include <machine/ktr.h>
66 #include <ddb/db_output.h>
69 #ifndef KTR_BOOT_ENTRIES
70 #define KTR_BOOT_ENTRIES 1024
74 #define KTR_ENTRIES 1024
77 /* Limit the allocations to something manageable. */
78 #define KTR_ENTRIES_MAX (8 * 1024 * 1024)
85 #define KTR_CPUMASK CPUSET_FSET
89 #define KTR_TIME get_cyclecount()
93 #define KTR_CPU PCPU_GET(cpuid)
96 static MALLOC_DEFINE(M_KTR, "KTR", "KTR");
98 FEATURE(ktr, "Kernel support for KTR kernel tracing facility");
100 volatile int ktr_idx = 0;
101 int ktr_mask = KTR_MASK;
102 int ktr_compile = KTR_COMPILE;
103 int ktr_entries = KTR_BOOT_ENTRIES;
104 int ktr_version = KTR_VERSION;
105 struct ktr_entry ktr_buf_init[KTR_BOOT_ENTRIES];
106 struct ktr_entry *ktr_buf = ktr_buf_init;
107 cpuset_t ktr_cpumask = CPUSET_T_INITIALIZER(KTR_CPUMASK);
108 static char ktr_cpumask_str[CPUSETBUFSIZ];
110 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
112 TUNABLE_STR("debug.ktr.cpumask", ktr_cpumask_str, sizeof(ktr_cpumask_str));
114 static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
116 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD,
117 &ktr_version, 0, "Version of the KTR interface");
119 SYSCTL_UINT(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD,
120 &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel");
123 ktr_cpumask_initializer(void *dummy __unused)
127 * TUNABLE_STR() runs with SI_ORDER_MIDDLE priority, thus it must be
128 * already set, if necessary.
130 if (ktr_cpumask_str[0] != '\0' &&
131 cpusetobj_strscan(&ktr_cpumask, ktr_cpumask_str) == -1)
132 CPU_FILL(&ktr_cpumask);
134 SYSINIT(ktr_cpumask_initializer, SI_SUB_TUNABLES, SI_ORDER_ANY,
135 ktr_cpumask_initializer, NULL);
138 sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS)
140 char lktr_cpumask_str[CPUSETBUFSIZ];
144 cpusetobj_strprint(lktr_cpumask_str, &ktr_cpumask);
145 error = sysctl_handle_string(oidp, lktr_cpumask_str,
146 sizeof(lktr_cpumask_str), req);
147 if (error != 0 || req->newptr == NULL)
149 if (cpusetobj_strscan(&imask, lktr_cpumask_str) == -1)
151 CPU_COPY(&imask, &ktr_cpumask);
155 SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask,
156 CTLFLAG_RW | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0,
157 sysctl_debug_ktr_cpumask, "S",
158 "Bitmask of CPUs on which KTR logging is enabled");
161 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
166 error = sysctl_handle_int(oidp, &clear, 0, req);
167 if (error || !req->newptr)
171 bzero(ktr_buf, sizeof(*ktr_buf) * ktr_entries);
177 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
178 sysctl_debug_ktr_clear, "I", "Clear KTR Buffer");
181 * This is a sysctl proc so that it is serialized as !MPSAFE along with
182 * the other ktr sysctl procs.
185 sysctl_debug_ktr_mask(SYSCTL_HANDLER_ARGS)
190 error = sysctl_handle_int(oidp, &mask, 0, req);
191 if (error || !req->newptr)
197 SYSCTL_PROC(_debug_ktr, OID_AUTO, mask, CTLTYPE_UINT|CTLFLAG_RW, 0, 0,
198 sysctl_debug_ktr_mask, "IU",
199 "Bitmask of KTR event classes for which logging is enabled");
201 #if KTR_ENTRIES > KTR_BOOT_ENTRIES
203 * A simplified version of sysctl_debug_ktr_entries.
204 * No need to care about SMP, scheduling, etc.
207 ktr_entries_initializer(void *dummy __unused)
211 /* Temporarily disable ktr in case malloc() is being traced. */
214 ktr_buf = malloc(sizeof(*ktr_buf) * KTR_ENTRIES, M_KTR,
216 memcpy(ktr_buf, ktr_buf_init, sizeof(ktr_buf_init));
217 ktr_entries = KTR_ENTRIES;
220 SYSINIT(ktr_entries_initializer, SI_SUB_KMEM, SI_ORDER_ANY,
221 ktr_entries_initializer, NULL);
225 sysctl_debug_ktr_entries(SYSCTL_HANDLER_ARGS)
227 int entries, error, mask;
228 struct ktr_entry *buf, *oldbuf;
230 entries = ktr_entries;
231 error = sysctl_handle_int(oidp, &entries, 0, req);
232 if (error || !req->newptr)
234 if (entries > KTR_ENTRIES_MAX)
236 /* Disable ktr temporarily. */
238 atomic_store_rel_int(&ktr_mask, 0);
239 /* Wait for threads to go idle. */
240 if ((error = quiesce_all_cpus("ktrent", PCATCH)) != 0) {
244 if (ktr_buf != ktr_buf_init)
248 /* Allocate a new buffer. */
249 buf = malloc(sizeof(*buf) * entries, M_KTR, M_WAITOK | M_ZERO);
250 /* Install the new buffer and restart ktr. */
252 ktr_entries = entries;
254 atomic_store_rel_int(&ktr_mask, mask);
261 SYSCTL_PROC(_debug_ktr, OID_AUTO, entries, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
262 sysctl_debug_ktr_entries, "I", "Number of entries in the KTR buffer");
265 int ktr_verbose = KTR_VERBOSE;
266 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
267 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
272 char ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
274 int ktr_alq_depth = KTR_ENTRIES;
275 int ktr_alq_enabled = 0;
276 int ktr_alq_failed = 0;
279 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
280 "Maximum number of entries to write");
281 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
282 "Current number of written entries");
283 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
284 "Number of times we overran the buffer");
285 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
286 "Number of items in the write buffer");
287 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
288 sizeof(ktr_alq_file), "KTR logging file");
291 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
296 enable = ktr_alq_enabled;
298 error = sysctl_handle_int(oidp, &enable, 0, req);
299 if (error || !req->newptr)
305 error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
306 req->td->td_ucred, ALQ_DEFAULT_CMODE,
307 sizeof(struct ktr_entry), ktr_alq_depth);
314 if (ktr_alq_enabled == 0)
323 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
324 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
325 "I", "Enable KTR logging");
329 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
330 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
333 struct ktr_entry *entry;
335 struct ale *ale = NULL;
337 int newindex, saveindex;
338 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
345 if ((ktr_mask & mask) == 0 || ktr_buf == NULL)
348 if (!CPU_ISSET(cpu, &ktr_cpumask))
350 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
352 if (td->td_pflags & TDP_INKTR)
354 td->td_pflags |= TDP_INKTR;
357 if (ktr_alq_enabled) {
358 if (td->td_critnest == 0 &&
359 (td->td_flags & TDF_IDLETD) == 0 &&
361 if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
363 if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
368 entry = (struct ktr_entry *)ale->ae_data;
377 newindex = (saveindex + 1) % ktr_entries;
378 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
379 entry = &ktr_buf[saveindex];
381 entry->ktr_timestamp = KTR_TIME;
382 entry->ktr_cpu = cpu;
383 entry->ktr_thread = curthread;
385 while (strncmp(file, "../", 3) == 0)
387 entry->ktr_file = file;
388 entry->ktr_line = line;
392 printf("cpu%d ", cpu);
394 if (ktr_verbose > 1) {
395 printf("%s.%d\t", entry->ktr_file,
398 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
402 entry->ktr_desc = format;
403 entry->ktr_parms[0] = arg1;
404 entry->ktr_parms[1] = arg2;
405 entry->ktr_parms[2] = arg3;
406 entry->ktr_parms[3] = arg4;
407 entry->ktr_parms[4] = arg5;
408 entry->ktr_parms[5] = arg6;
410 if (ktr_alq_enabled && ale)
411 alq_post(ktr_alq, ale);
414 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
415 td->td_pflags &= ~TDP_INKTR;
425 static struct tstate tstate;
426 static int db_ktr_verbose;
427 static int db_mach_vtrace(void);
429 DB_SHOW_COMMAND(ktr, db_ktr_all)
432 tstate.cur = (ktr_idx - 1) % ktr_entries;
435 db_ktr_verbose |= (strchr(modif, 'v') != NULL) ? 2 : 0;
436 db_ktr_verbose |= (strchr(modif, 'V') != NULL) ? 1 : 0; /* just timestap please */
437 if (strchr(modif, 'a') != NULL) {
439 while (cncheckc() != -1)
440 if (db_mach_vtrace() == 0)
443 while (!db_pager_quit)
444 if (db_mach_vtrace() == 0)
452 struct ktr_entry *kp;
454 if (tstate.cur == tstate.first || ktr_buf == NULL) {
455 db_printf("--- End of trace buffer ---\n");
458 kp = &ktr_buf[tstate.cur];
460 /* Skip over unused entries. */
461 if (kp->ktr_desc == NULL) {
462 db_printf("--- End of trace buffer ---\n");
465 db_printf("%d (%p", tstate.cur, kp->ktr_thread);
467 db_printf(":cpu%d", kp->ktr_cpu);
470 if (db_ktr_verbose >= 1) {
471 db_printf(" %10.10lld", (long long)kp->ktr_timestamp);
473 if (db_ktr_verbose >= 2) {
474 db_printf(" %s.%d", kp->ktr_file, kp->ktr_line);
477 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
478 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
482 if (tstate.first == -1)
483 tstate.first = tstate.cur;
485 if (--tstate.cur < 0)
486 tstate.cur = ktr_entries - 1;