xref: /freebsd/sys/kern/kern_ktr.c (revision c68159a6d8eede11766cf13896d0f7670dbd51aa)
1 /*
2  * Copyright (c) 2000
3  *	John Baldwin <jhb@FreeBSD.org>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * This module holds the global variables used by KTR and the ktr_tracepoint()
34  * function that does the actual tracing.
35  */
36 
37 #include "opt_ddb.h"
38 #include "opt_ktr.h"
39 
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/cons.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/ktr.h>
46 #include <sys/libkern.h>
47 #include <sys/linker_set.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <machine/globals.h>
51 #include <machine/stdarg.h>
52 
53 #include <ddb/ddb.h>
54 
55 #ifndef KTR_MASK
56 #define	KTR_MASK	(KTR_GEN)
57 #endif
58 
59 #ifndef KTR_CPUMASK
60 #define	KTR_CPUMASK	(~0)
61 #endif
62 
63 #ifdef SMP
64 #define KTR_CPU		cpuid
65 #else
66 #define KTR_CPU		0
67 #endif
68 
69 #ifdef KTR_EXTEND
70 #define KTR_EXTEND_DEFAULT	1
71 #else
72 #define KTR_EXTEND_DEFAULT	0
73 #endif
74 
75 #ifdef KTR_VERBOSE
76 #define KTR_VERBOSE_DEFAULT	1
77 #else
78 #define KTR_VERBOSE_DEFAULT	0
79 #endif
80 
81 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
82 
83 /*
84  * This variable is used only by gdb to work out what fields are in
85  * ktr_entry.
86  */
87 int     ktr_extend = KTR_EXTEND_DEFAULT;
88 SYSCTL_INT(_debug_ktr, OID_AUTO, extend, CTLFLAG_RD, &ktr_extend, 0, "");
89 
90 int	ktr_cpumask;
91 TUNABLE_INT_DECL("debug.ktr.cpumask", KTR_CPUMASK, ktr_cpumask);
92 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
93 
94 int	ktr_mask;
95 TUNABLE_INT_DECL("debug.ktr.mask", KTR_MASK, ktr_mask);
96 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
97 
98 int	ktr_entries = KTR_ENTRIES;
99 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
100 
101 volatile int	ktr_idx = 0;
102 struct	ktr_entry ktr_buf[KTR_ENTRIES];
103 
104 int	ktr_verbose;
105 TUNABLE_INT_DECL("debug.ktr.verbose", KTR_VERBOSE_DEFAULT, ktr_verbose);
106 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
107 
108 #ifdef KTR
109 #ifdef KTR_EXTEND
110 void
111 ktr_tracepoint(u_int mask, char *filename, u_int line, char *format, ...)
112 #else
113 void
114 ktr_tracepoint(u_int mask, char *format, u_long arg1, u_long arg2, u_long arg3,
115 	       u_long arg4, u_long arg5)
116 #endif
117 {
118 	struct ktr_entry *entry;
119 	int newindex, saveindex, saveintr;
120 #ifdef KTR_EXTEND
121 	va_list ap;
122 #endif
123 
124 	if (panicstr)
125 		return;
126 	if ((ktr_mask & mask) == 0)
127 		return;
128 #ifdef KTR_EXTEND
129 	if (((1 << KTR_CPU) & ktr_cpumask) == 0)
130 		return;
131 #endif
132 	saveintr = save_intr();
133 	disable_intr();
134 	do {
135 		saveindex = ktr_idx;
136 		newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
137 	} while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
138 	entry = &ktr_buf[saveindex];
139 	restore_intr(saveintr);
140 	if (ktr_mask & KTR_LOCK)
141 		/*
142 		 * We can't use nanotime with KTR_LOCK, it would cause
143 		 * endless recursion, at least under the Intel
144 		 * architecture.
145 		 */
146 		getnanotime(&entry->ktr_tv);
147 	else
148 		nanotime(&entry->ktr_tv);
149 #ifdef KTR_EXTEND
150 	strncpy(entry->ktr_filename, filename, KTRFILENAMESIZE - 1);
151 	entry->ktr_filename[KTRFILENAMESIZE - 1] = '\0';
152 	entry->ktr_line = line;
153 	entry->ktr_cpu = KTR_CPU;
154 	va_start(ap, format);
155 	vsnprintf(entry->ktr_desc, KTRDESCSIZE, format, ap);
156 	va_end(ap);
157 	if (ktr_verbose) {
158 #ifdef SMP
159 		printf("cpu%d ", entry->ktr_cpu);
160 #endif
161 		if (ktr_verbose > 1)
162 			printf("%s.%d\t", entry->ktr_filename, entry->ktr_line);
163 		va_start(ap, format);
164 		vprintf(format, ap);
165 		printf("\n");
166 		va_end(ap);
167 	}
168 #else
169 	entry->ktr_desc = format;
170 	entry->ktr_parm1 = arg1;
171 	entry->ktr_parm2 = arg2;
172 	entry->ktr_parm3 = arg3;
173 	entry->ktr_parm4 = arg4;
174 	entry->ktr_parm5 = arg5;
175 #endif
176 }
177 
178 #ifdef DDB
179 
180 struct tstate {
181 	int	cur;
182 	int	first;
183 };
184 static	struct tstate tstate;
185 static	int db_ktr_verbose;
186 static	int db_mach_vtrace(void);
187 
188 DB_COMMAND(tbuf, db_mach_tbuf)
189 {
190 
191 	tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
192 	tstate.first = -1;
193 	if (strcmp(modif, "v") == 0)
194 		db_ktr_verbose = 1;
195 	else
196 		db_ktr_verbose = 0;
197 	db_mach_vtrace();
198 
199 	return;
200 }
201 
202 DB_COMMAND(tall, db_mach_tall)
203 {
204 	int	c;
205 
206 	db_mach_tbuf(addr, have_addr, count, modif);
207 	while (db_mach_vtrace()) {
208 		c = cncheckc();
209 		if (c != -1)
210 			break;
211 	}
212 
213 	return;
214 }
215 
216 DB_COMMAND(tnext, db_mach_tnext)
217 {
218 
219 	if (strcmp(modif, "v") == 0)
220 		db_ktr_verbose ^= 1;
221 	db_mach_vtrace();
222 }
223 
224 static int
225 db_mach_vtrace(void)
226 {
227 	struct ktr_entry	*kp;
228 
229 	if (tstate.cur == tstate.first) {
230 		db_printf("--- End of trace buffer ---\n");
231 		return (0);
232 	}
233 	kp = &ktr_buf[tstate.cur];
234 
235 	/* Skip over unused entries. */
236 #ifdef KTR_EXTEND
237 	if (kp->ktr_desc[0] != '\0') {
238 #else
239 	if (kp->ktr_desc != NULL) {
240 #endif
241 		db_printf("%d: ", tstate.cur);
242 		if (db_ktr_verbose)
243 			db_printf("%4ld.%06ld ", kp->ktr_tv.tv_sec,
244 			    kp->ktr_tv.tv_nsec / 1000);
245 #ifdef KTR_EXTEND
246 #ifdef SMP
247 		db_printf("cpu%d ", kp->ktr_cpu);
248 #endif
249 		if (db_ktr_verbose)
250 			db_printf("%s.%d\t", kp->ktr_filename, kp->ktr_line);
251 		db_printf("%s", kp->ktr_desc);
252 #else
253 		db_printf(kp->ktr_desc, kp->ktr_parm1, kp->ktr_parm2,
254 		    kp->ktr_parm3, kp->ktr_parm4, kp->ktr_parm5);
255 #endif
256 		db_printf("\n");
257 	}
258 
259 	if (tstate.first == -1)
260 		tstate.first = tstate.cur;
261 
262 	if (--tstate.cur < 0)
263 		tstate.cur = KTR_ENTRIES - 1;
264 
265 	return (1);
266 }
267 
268 #endif	/* DDB */
269 #endif	/* KTR */
270