3 * John Baldwin <jhb@FreeBSD.org>. All rights reserved.
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 * 4. 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 JOHN BALDWIN 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 JOHN BALDWIN OR THE VOICES IN HIS HEAD
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGE.
33 * This module holds the global variables used by KTR and the ktr_tracepoint()
34 * function that does the actual tracing.
40 #include <sys/param.h>
42 #include <sys/kernel.h>
44 #include <sys/libkern.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
49 #include <machine/stdarg.h>
54 #define KTR_ENTRIES 1024
58 #define KTR_MASK (KTR_GEN)
62 #define KTR_CPUMASK (~0)
66 #define KTR_CPU PCPU_GET(cpuid)
72 #define KTR_EXTEND_DEFAULT 1
74 #define KTR_EXTEND_DEFAULT 0
78 #define KTR_VERBOSE_DEFAULT 1
80 #define KTR_VERBOSE_DEFAULT 0
83 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
86 * This variable is used only by gdb to work out what fields are in
89 int ktr_extend = KTR_EXTEND_DEFAULT;
90 SYSCTL_INT(_debug_ktr, OID_AUTO, extend, CTLFLAG_RD, &ktr_extend, 0, "");
92 int ktr_cpumask = KTR_CPUMASK;
93 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
94 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
96 int ktr_mask = KTR_MASK;
97 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
98 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
100 int ktr_entries = KTR_ENTRIES;
101 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
103 volatile int ktr_idx = 0;
104 struct ktr_entry ktr_buf[KTR_ENTRIES];
106 int ktr_verbose = KTR_VERBOSE_DEFAULT;
107 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
108 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
113 ktr_tracepoint(u_int mask, const char *filename, u_int line,
114 const char *format, ...)
117 ktr_tracepoint(u_int mask, const char *format, u_long arg1, u_long arg2,
118 u_long arg3, u_long arg4, u_long arg5, u_long arg6)
121 struct ktr_entry *entry;
122 int newindex, saveindex;
131 if ((ktr_mask & mask) == 0)
136 savecrit = cpu_critical_enter();
137 if (((1 << KTR_CPU) & ktr_cpumask) == 0) {
138 cpu_critical_exit(savecrit);
144 newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
145 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
146 entry = &ktr_buf[saveindex];
147 entry->ktr_cpu = KTR_CPU;
148 cpu_critical_exit(savecrit);
149 nanotime(&entry->ktr_tv);
151 entry->ktr_filename = filename;
152 entry->ktr_line = line;
153 va_start(ap, format);
154 vsnprintf(entry->ktr_desc, KTRDESCSIZE, format, ap);
158 printf("cpu%d ", entry->ktr_cpu);
161 printf("%s.%d\t", entry->ktr_filename, entry->ktr_line);
162 va_start(ap, format);
168 entry->ktr_desc = format;
169 entry->ktr_parm1 = arg1;
170 entry->ktr_parm2 = arg2;
171 entry->ktr_parm3 = arg3;
172 entry->ktr_parm4 = arg4;
173 entry->ktr_parm5 = arg5;
174 entry->ktr_parm6 = arg6;
185 static struct tstate tstate;
186 static int db_ktr_verbose;
187 static int db_mach_vtrace(void);
189 #define NUM_LINES_PER_PAGE 18
191 DB_SHOW_COMMAND(ktr, db_ktr_all)
195 lines = NUM_LINES_PER_PAGE;
196 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
198 if (strcmp(modif, "v") == 0)
202 while (db_mach_vtrace())
204 db_printf("--More--");
208 case '\n': /* one more line */
211 case ' ': /* one more page */
212 lines = NUM_LINES_PER_PAGE;
224 struct ktr_entry *kp;
226 if (tstate.cur == tstate.first) {
227 db_printf("--- End of trace buffer ---\n");
230 kp = &ktr_buf[tstate.cur];
232 /* Skip over unused entries. */
234 if (kp->ktr_desc[0] == '\0') {
236 if (kp->ktr_desc == NULL) {
238 db_printf("--- End of trace buffer ---\n");
241 db_printf("%d: ", tstate.cur);
243 db_printf("%4ld.%06ld ", (long)kp->ktr_tv.tv_sec,
244 kp->ktr_tv.tv_nsec / 1000);
247 db_printf("cpu%d ", kp->ktr_cpu);
250 db_printf("%s.%d\t", kp->ktr_filename, kp->ktr_line);
251 db_printf("%s", kp->ktr_desc);
253 db_printf(kp->ktr_desc, kp->ktr_parm1, kp->ktr_parm2, kp->ktr_parm3,
254 kp->ktr_parm4, kp->ktr_parm5, kp->ktr_parm6);
258 if (tstate.first == -1)
259 tstate.first = tstate.cur;
261 if (--tstate.cur < 0)
262 tstate.cur = KTR_ENTRIES - 1;