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>
50 #include <machine/cpu.h>
52 #include <machine/ktr.h>
58 #define KTR_ENTRIES 1024
62 #define KTR_MASK (KTR_GEN)
66 #define KTR_CPUMASK (~0)
70 #define KTR_TIME get_cyclecount()
74 #define KTR_CPU PCPU_GET(cpuid)
77 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
79 int ktr_cpumask = KTR_CPUMASK;
80 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
81 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
83 int ktr_mask = KTR_MASK;
84 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
85 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
87 int ktr_entries = KTR_ENTRIES;
88 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
90 int ktr_version = KTR_VERSION;
91 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
93 volatile int ktr_idx = 0;
94 struct ktr_entry ktr_buf[KTR_ENTRIES];
97 int ktr_verbose = KTR_VERBOSE;
98 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
99 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
103 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
104 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
107 struct ktr_entry *entry;
108 int newindex, saveindex;
116 if ((ktr_mask & mask) == 0)
119 if (((1 << cpu) & ktr_cpumask) == 0)
129 newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
130 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
131 entry = &ktr_buf[saveindex];
132 entry->ktr_timestamp = KTR_TIME;
133 entry->ktr_cpu = cpu;
134 entry->ktr_file = file;
135 entry->ktr_line = line;
139 printf("cpu%d ", cpu);
141 if (ktr_verbose > 1) {
142 printf("%s.%d\t", entry->ktr_file,
145 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
149 entry->ktr_desc = format;
150 entry->ktr_parms[0] = arg1;
151 entry->ktr_parms[1] = arg2;
152 entry->ktr_parms[2] = arg3;
153 entry->ktr_parms[3] = arg4;
154 entry->ktr_parms[4] = arg5;
155 entry->ktr_parms[5] = arg6;
167 static struct tstate tstate;
168 static int db_ktr_verbose;
169 static int db_mach_vtrace(void);
171 #define NUM_LINES_PER_PAGE 18
173 DB_SHOW_COMMAND(ktr, db_ktr_all)
177 lines = NUM_LINES_PER_PAGE;
178 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
180 if (strcmp(modif, "v") == 0)
184 while (db_mach_vtrace())
186 db_printf("--More--");
190 case '\n': /* one more line */
193 case ' ': /* one more page */
194 lines = NUM_LINES_PER_PAGE;
206 struct ktr_entry *kp;
208 if (tstate.cur == tstate.first) {
209 db_printf("--- End of trace buffer ---\n");
212 kp = &ktr_buf[tstate.cur];
214 /* Skip over unused entries. */
215 if (kp->ktr_desc == NULL) {
216 db_printf("--- End of trace buffer ---\n");
219 db_printf("%d: ", tstate.cur);
221 db_printf("cpu%d ", kp->ktr_cpu);
223 if (db_ktr_verbose) {
224 db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp,
225 kp->ktr_file, kp->ktr_line);
227 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
228 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
232 if (tstate.first == -1)
233 tstate.first = tstate.cur;
235 if (--tstate.cur < 0)
236 tstate.cur = KTR_ENTRIES - 1;