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/sysctl.h>
52 #include <sys/systm.h>
55 #include <machine/cpu.h>
57 #include <machine/ktr.h>
62 #include <ddb/db_output.h>
66 #define KTR_ENTRIES 1024
74 #define KTR_TIME get_cyclecount()
78 #define KTR_CPU PCPU_GET(cpuid)
81 FEATURE(ktr, "Kernel support for KTR kernel tracing facility");
83 static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
85 int ktr_mask = KTR_MASK;
86 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
87 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW,
88 &ktr_mask, 0, "Bitmask of KTR event classes for which logging is enabled");
90 int ktr_compile = KTR_COMPILE;
91 SYSCTL_INT(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD,
92 &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel");
94 int ktr_entries = KTR_ENTRIES;
95 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD,
96 &ktr_entries, 0, "Number of entries in the KTR buffer");
98 int ktr_version = KTR_VERSION;
99 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD,
100 &ktr_version, 0, "Version of the KTR interface");
102 cpuset_t ktr_cpumask;
103 static char ktr_cpumask_str[CPUSETBUFSIZ];
104 TUNABLE_STR("debug.ktr.cpumask", ktr_cpumask_str, sizeof(ktr_cpumask_str));
107 ktr_cpumask_initializer(void *dummy __unused)
110 CPU_FILL(&ktr_cpumask);
112 if (cpusetobj_strscan(&ktr_cpumask, KTR_CPUMASK) == -1)
113 CPU_FILL(&ktr_cpumask);
117 * TUNABLE_STR() runs with SI_ORDER_MIDDLE priority, thus it must be
118 * already set, if necessary.
120 if (ktr_cpumask_str[0] != '\0' &&
121 cpusetobj_strscan(&ktr_cpumask, ktr_cpumask_str) == -1)
122 CPU_FILL(&ktr_cpumask);
124 SYSINIT(ktr_cpumask_initializer, SI_SUB_TUNABLES, SI_ORDER_ANY,
125 ktr_cpumask_initializer, NULL);
128 sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS)
130 char lktr_cpumask_str[CPUSETBUFSIZ];
134 cpusetobj_strprint(lktr_cpumask_str, &ktr_cpumask);
135 error = sysctl_handle_string(oidp, lktr_cpumask_str,
136 sizeof(lktr_cpumask_str), req);
137 if (error != 0 || req->newptr == NULL)
139 if (cpusetobj_strscan(&imask, lktr_cpumask_str) == -1)
141 CPU_COPY(&imask, &ktr_cpumask);
145 SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask,
146 CTLFLAG_RW | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0,
147 sysctl_debug_ktr_cpumask, "S",
148 "Bitmask of CPUs on which KTR logging is enabled");
150 volatile int ktr_idx = 0;
151 struct ktr_entry ktr_buf[KTR_ENTRIES];
154 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
159 error = sysctl_handle_int(oidp, &clear, 0, req);
160 if (error || !req->newptr)
164 bzero(ktr_buf, sizeof(ktr_buf));
170 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
171 sysctl_debug_ktr_clear, "I", "Clear KTR Buffer");
174 int ktr_verbose = KTR_VERBOSE;
175 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
176 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
181 char ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
183 int ktr_alq_depth = KTR_ENTRIES;
184 int ktr_alq_enabled = 0;
185 int ktr_alq_failed = 0;
188 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
189 "Maximum number of entries to write");
190 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
191 "Current number of written entries");
192 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
193 "Number of times we overran the buffer");
194 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
195 "Number of items in the write buffer");
196 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
197 sizeof(ktr_alq_file), "KTR logging file");
200 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
205 enable = ktr_alq_enabled;
207 error = sysctl_handle_int(oidp, &enable, 0, req);
208 if (error || !req->newptr)
214 error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
215 req->td->td_ucred, ALQ_DEFAULT_CMODE,
216 sizeof(struct ktr_entry), ktr_alq_depth);
223 if (ktr_alq_enabled == 0)
232 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
233 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
234 "I", "Enable KTR logging");
238 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
239 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
242 struct ktr_entry *entry;
244 struct ale *ale = NULL;
246 int newindex, saveindex;
247 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
254 if ((ktr_mask & mask) == 0)
257 if (!CPU_ISSET(cpu, &ktr_cpumask))
259 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
261 if (td->td_pflags & TDP_INKTR)
263 td->td_pflags |= TDP_INKTR;
266 if (ktr_alq_enabled) {
267 if (td->td_critnest == 0 &&
268 (td->td_flags & TDF_IDLETD) == 0 &&
270 if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
272 if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
277 entry = (struct ktr_entry *)ale->ae_data;
286 newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
287 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
288 entry = &ktr_buf[saveindex];
290 entry->ktr_timestamp = KTR_TIME;
291 entry->ktr_cpu = cpu;
292 entry->ktr_thread = curthread;
294 while (strncmp(file, "../", 3) == 0)
296 entry->ktr_file = file;
297 entry->ktr_line = line;
301 printf("cpu%d ", cpu);
303 if (ktr_verbose > 1) {
304 printf("%s.%d\t", entry->ktr_file,
307 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
311 entry->ktr_desc = format;
312 entry->ktr_parms[0] = arg1;
313 entry->ktr_parms[1] = arg2;
314 entry->ktr_parms[2] = arg3;
315 entry->ktr_parms[3] = arg4;
316 entry->ktr_parms[4] = arg5;
317 entry->ktr_parms[5] = arg6;
319 if (ktr_alq_enabled && ale)
320 alq_post(ktr_alq, ale);
323 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
324 td->td_pflags &= ~TDP_INKTR;
334 static struct tstate tstate;
335 static int db_ktr_verbose;
336 static int db_mach_vtrace(void);
338 DB_SHOW_COMMAND(ktr, db_ktr_all)
341 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
344 db_ktr_verbose |= (index(modif, 'v') != NULL) ? 2 : 0;
345 db_ktr_verbose |= (index(modif, 'V') != NULL) ? 1 : 0; /* just timestap please */
346 if (index(modif, 'a') != NULL) {
348 while (cncheckc() != -1)
349 if (db_mach_vtrace() == 0)
352 while (!db_pager_quit)
353 if (db_mach_vtrace() == 0)
361 struct ktr_entry *kp;
363 if (tstate.cur == tstate.first) {
364 db_printf("--- End of trace buffer ---\n");
367 kp = &ktr_buf[tstate.cur];
369 /* Skip over unused entries. */
370 if (kp->ktr_desc == NULL) {
371 db_printf("--- End of trace buffer ---\n");
374 db_printf("%d (%p", tstate.cur, kp->ktr_thread);
376 db_printf(":cpu%d", kp->ktr_cpu);
379 if (db_ktr_verbose >= 1) {
380 db_printf(" %10.10lld", (long long)kp->ktr_timestamp);
382 if (db_ktr_verbose >= 2) {
383 db_printf(" %s.%d", kp->ktr_file, kp->ktr_line);
386 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
387 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
391 if (tstate.first == -1)
392 tstate.first = tstate.cur;
394 if (--tstate.cur < 0)
395 tstate.cur = KTR_ENTRIES - 1;