2 * Copyright (c) 2004 Eirik Nygaard <eirikn@kerneled.com>
4 * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of any co-contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * This module holds the global variables used by KTR and the ktr_tracepoint()
34 * function that does the actual tracing.
38 * $FreeBSD: /repoman/r/ncvs/src/sys/kern/kern_ktr.c,v 1.43 2003/09/10 01:09:32 jhb Exp $
39 * $DragonFly: src/sys/kern/kern_ktr.c,v 1.2 2005/02/12 21:09:46 eirikn Exp $
45 #include <sys/param.h>
47 #include <sys/kernel.h>
49 #include <sys/libkern.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
54 #include <sys/malloc.h>
55 #include <sys/thread2.h>
56 #include <sys/ctype.h>
58 #include <machine/cpu.h>
59 #include <machine/cpufunc.h>
60 #include <machine/specialreg.h>
61 #include <machine/md_var.h>
66 #define KTR_ENTRIES 1024
70 #define KTR_MASK (KTR_ALL)
74 #define KTR_CPUMASK (~0)
78 #define KTR_TIME ktr_getts()
82 #define KTR_CPU mycpu->gd_cpuid;
85 MALLOC_DEFINE(M_KTR, "ktr", "ktr buffers");
87 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
89 int ktr_cpumask = KTR_CPUMASK;
90 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
91 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
93 int ktr_mask = KTR_MASK;
94 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
95 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
97 int ktr_entries = KTR_ENTRIES;
98 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
100 int ktr_version = KTR_VERSION;
101 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
104 SYSCTL_INT(_debug_ktr, OID_AUTO, discarded, CTLFLAG_RD, &ktr_discarded, 0, "");
106 volatile int ktr_idx[MAXCPU], ktr_initiated = 0;
107 struct ktr_entry *ktr_buf[MAXCPU];
110 int ktr_verbose = KTR_VERBOSE;
111 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
112 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
116 ktr_sysinit(void *dummy)
120 for(i = 0; i < ncpus; i++) {
121 ktr_buf[i] = malloc(KTR_ENTRIES * sizeof(struct ktr_entry),
127 SYSINIT(announce, SI_SUB_INTRINSIC, SI_ORDER_FIRST, ktr_sysinit, NULL);
130 ktr_nextindex(int cpu)
135 ktrindex = ktr_idx[cpu] = (ktr_idx[cpu] + 1) & (KTR_ENTRIES - 1);
140 static __inline uint64_t
143 struct globaldata *gd = curthread->td_gd;
144 if (cpu_feature & CPUID_TSC)
146 return(gd->gd_time_seconds + basetime.tv_sec);
150 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
151 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
154 struct ktr_entry *entry;
157 if (ktr_initiated == 0) {
163 if ((ktr_mask & mask) == 0)
166 if (((1 << cpu) & ktr_cpumask) == 0)
168 newindex = ktr_nextindex(cpu);
169 entry = &ktr_buf[cpu][newindex];
170 entry->ktr_timestamp = KTR_TIME;
171 entry->ktr_cpu = cpu;
173 while (strncmp(file, "../", 3) == 0)
175 entry->ktr_file = file;
176 entry->ktr_line = line;
180 printf("cpu%d ", cpu);
182 if (ktr_verbose > 1) {
183 printf("%s.%d\t", entry->ktr_file, entry->ktr_line);
185 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
189 entry->ktr_desc = format;
190 entry->ktr_parms[0] = arg1;
191 entry->ktr_parms[1] = arg2;
192 entry->ktr_parms[2] = arg3;
193 entry->ktr_parms[3] = arg4;
194 entry->ktr_parms[4] = arg5;
195 entry->ktr_parms[5] = arg6;
200 #define NUM_LINES_PER_PAGE 19
206 static int db_ktr_verbose;
207 static int db_mach_vtrace(struct ktr_entry *kp, int idx);
209 DB_SHOW_COMMAND(ktr, db_ktr_all)
215 struct tstate tstate[MAXCPU];
218 for(i = 0; i < ncpus; i++) {
219 tstate[i].first = -1;
220 tstate[i].cur = ktr_idx[i];
223 while ((c = *(modif++)) != '\0') {
232 while ((c = *(modif++)) != '\0') {
245 if (printcpu > ncpus - 1) {
246 db_printf("Invalid cpu number\n");
250 * Lopp throug all the buffers and print the content of them, sorted
255 u_int64_t highest_ts;
257 struct ktr_entry *kp;
259 if (a_flag == 1 && cncheckc() != -1)
264 * Find the lowest timestamp
266 for (i = 0, counter = 0; i < ncpus; i++) {
267 if (printcpu != -1 && printcpu != i)
269 if (tstate[i].cur == -1) {
271 if (counter == ncpus) {
272 db_printf("--- End of trace buffer ---\n");
277 if (ktr_buf[i][tstate[i].cur].ktr_timestamp > highest_ts) {
278 highest_ts = ktr_buf[i][tstate[i].cur].ktr_timestamp;
284 kp = &ktr_buf[i][tstate[i].cur];
285 if (tstate[i].first == -1)
286 tstate[i].first = tstate[i].cur;
287 if (--tstate[i].cur < 0)
288 tstate[i].cur = KTR_ENTRIES - 1;
289 if (tstate[i].first == tstate[i].cur) {
290 db_mach_vtrace(kp, tstate[i].cur + 1);
294 if (ktr_buf[i][tstate[i].cur].ktr_desc == NULL)
296 if (db_more(&nl) == -1)
298 if (db_mach_vtrace(kp, tstate[i].cur + 1) == 0)
304 db_mach_vtrace(struct ktr_entry *kp, int idx)
306 if (kp->ktr_desc == NULL)
309 db_printf("cpu%d ", kp->ktr_cpu);
311 db_printf("%d: ", idx);
312 if (db_ktr_verbose) {
313 db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp,
314 kp->ktr_file, kp->ktr_line);
316 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
317 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],