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>
45 #include <sys/kernel.h>
47 #include <sys/libkern.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
53 #include <machine/cpu.h>
55 #include <machine/ktr.h>
62 #define KTR_ENTRIES 1024
66 #define KTR_MASK (KTR_GEN)
70 #define KTR_CPUMASK (~0)
74 #define KTR_TIME get_cyclecount()
78 #define KTR_CPU PCPU_GET(cpuid)
81 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
83 int ktr_cpumask = KTR_CPUMASK;
84 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
85 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
87 int ktr_mask = KTR_MASK;
88 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
89 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
91 int ktr_compile = KTR_COMPILE;
92 SYSCTL_INT(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD, &ktr_compile, 0, "");
94 int ktr_entries = KTR_ENTRIES;
95 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
97 int ktr_version = KTR_VERSION;
98 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
100 volatile int ktr_idx = 0;
101 struct ktr_entry ktr_buf[KTR_ENTRIES];
104 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
109 error = sysctl_handle_int(oidp, &clear, 0, req);
110 if (error || !req->newptr)
114 bzero(ktr_buf, sizeof(ktr_buf));
120 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
121 sysctl_debug_ktr_clear, "I", "Clear KTR Buffer");
124 int ktr_verbose = KTR_VERBOSE;
125 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
126 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
131 char ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
133 int ktr_alq_depth = KTR_ENTRIES;
134 int ktr_alq_enabled = 0;
135 int ktr_alq_failed = 0;
138 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
139 "Maximum number of entries to write");
140 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
141 "Current number of written entries");
142 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
143 "Number of times we overran the buffer");
144 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
145 "Number of items in the write buffer");
146 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
147 sizeof(ktr_alq_file), "KTR logging file");
150 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
155 enable = ktr_alq_enabled;
157 error = sysctl_handle_int(oidp, &enable, 0, req);
158 if (error || !req->newptr)
164 error = suser(curthread);
167 error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
168 req->td->td_ucred, ALQ_DEFAULT_CMODE,
169 sizeof(struct ktr_entry), ktr_alq_depth);
176 if (ktr_alq_enabled == 0)
185 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
186 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
187 "I", "Enable KTR logging");
191 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
192 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
195 struct ktr_entry *entry;
197 struct ale *ale = NULL;
199 int newindex, saveindex;
201 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
208 if ((ktr_mask & mask) == 0)
211 if (((1 << cpu) & ktr_cpumask) == 0)
213 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
215 if (td->td_pflags & TDP_INKTR)
217 td->td_pflags |= TDP_INKTR;
220 if (ktr_alq_enabled &&
221 td->td_critnest == 0 &&
222 (td->td_flags & TDF_IDLETD) == 0 &&
224 if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
226 if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
231 entry = (struct ktr_entry *)ale->ae_data;
237 newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
238 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
239 entry = &ktr_buf[saveindex];
241 entry->ktr_timestamp = KTR_TIME;
242 entry->ktr_cpu = cpu;
243 entry->ktr_thread = curthread;
245 while (strncmp(file, "../", 3) == 0)
247 entry->ktr_file = file;
248 entry->ktr_line = line;
252 printf("cpu%d ", cpu);
254 if (ktr_verbose > 1) {
255 printf("%s.%d\t", entry->ktr_file,
258 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
262 entry->ktr_desc = format;
263 entry->ktr_parms[0] = arg1;
264 entry->ktr_parms[1] = arg2;
265 entry->ktr_parms[2] = arg3;
266 entry->ktr_parms[3] = arg4;
267 entry->ktr_parms[4] = arg5;
268 entry->ktr_parms[5] = arg6;
271 alq_post(ktr_alq, ale);
274 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
275 td->td_pflags &= ~TDP_INKTR;
285 static struct tstate tstate;
286 static int db_ktr_verbose;
287 static int db_mach_vtrace(void);
289 DB_SHOW_COMMAND(ktr, db_ktr_all)
294 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
296 if (strcmp(modif, "v") == 0)
300 if (strcmp(modif, "a") == 0) {
301 while (cncheckc() != -1)
302 if (db_mach_vtrace() == 0)
305 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
307 if (db_mach_vtrace() == 0)
315 struct ktr_entry *kp;
317 if (tstate.cur == tstate.first) {
318 db_printf("--- End of trace buffer ---\n");
321 kp = &ktr_buf[tstate.cur];
323 /* Skip over unused entries. */
324 if (kp->ktr_desc == NULL) {
325 db_printf("--- End of trace buffer ---\n");
328 db_printf("%d (%p", tstate.cur, kp->ktr_thread);
330 db_printf(":cpu%d", kp->ktr_cpu);
333 if (db_ktr_verbose) {
334 db_printf(" %10.10lld %s.%d", (long long)kp->ktr_timestamp,
335 kp->ktr_file, kp->ktr_line);
338 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
339 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
343 if (tstate.first == -1)
344 tstate.first = tstate.cur;
346 if (--tstate.cur < 0)
347 tstate.cur = KTR_ENTRIES - 1;