xref: /freebsd/sys/kern/kern_ktr.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org>
3  * 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  * 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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * This module holds the global variables used by KTR and the ktr_tracepoint()
32  * function that does the actual tracing.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_ddb.h"
39 #include "opt_ktr.h"
40 #include "opt_alq.h"
41 
42 #include <sys/param.h>
43 #include <sys/alq.h>
44 #include <sys/cons.h>
45 #include <sys/kernel.h>
46 #include <sys/ktr.h>
47 #include <sys/libkern.h>
48 #include <sys/proc.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/time.h>
52 
53 #include <machine/cpu.h>
54 #ifdef __sparc64__
55 #include <machine/ktr.h>
56 #endif
57 
58 #ifdef DDB
59 #include <ddb/ddb.h>
60 #include <ddb/db_output.h>
61 #endif
62 
63 #ifndef KTR_ENTRIES
64 #define	KTR_ENTRIES	1024
65 #endif
66 
67 #ifndef KTR_MASK
68 #define	KTR_MASK	(0)
69 #endif
70 
71 #ifndef KTR_CPUMASK
72 #define	KTR_CPUMASK	(~0)
73 #endif
74 
75 #ifndef KTR_TIME
76 #define	KTR_TIME	get_cyclecount()
77 #endif
78 
79 #ifndef KTR_CPU
80 #define	KTR_CPU		PCPU_GET(cpuid)
81 #endif
82 
83 FEATURE(ktr, "Kernel support for KTR kernel tracing facility");
84 
85 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
86 
87 int	ktr_cpumask = KTR_CPUMASK;
88 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
89 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW,
90     &ktr_cpumask, 0, "Bitmask of CPUs on which KTR logging is enabled");
91 
92 int	ktr_mask = KTR_MASK;
93 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
94 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW,
95     &ktr_mask, 0, "Bitmask of KTR event classes for which logging is enabled");
96 
97 int	ktr_compile = KTR_COMPILE;
98 SYSCTL_INT(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD,
99     &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel");
100 
101 int	ktr_entries = KTR_ENTRIES;
102 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD,
103     &ktr_entries, 0, "Number of entries in the KTR buffer");
104 
105 int	ktr_version = KTR_VERSION;
106 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD,
107     &ktr_version, 0, "Version of the KTR interface");
108 
109 volatile int	ktr_idx = 0;
110 struct	ktr_entry ktr_buf[KTR_ENTRIES];
111 
112 static int
113 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
114 {
115 	int clear, error;
116 
117 	clear = 0;
118 	error = sysctl_handle_int(oidp, &clear, 0, req);
119 	if (error || !req->newptr)
120 		return (error);
121 
122 	if (clear) {
123 		bzero(ktr_buf, sizeof(ktr_buf));
124 		ktr_idx = 0;
125 	}
126 
127 	return (error);
128 }
129 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
130     sysctl_debug_ktr_clear, "I", "Clear KTR Buffer");
131 
132 #ifdef KTR_VERBOSE
133 int	ktr_verbose = KTR_VERBOSE;
134 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
135 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
136 #endif
137 
138 #ifdef KTR_ALQ
139 struct alq *ktr_alq;
140 char	ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
141 int	ktr_alq_cnt = 0;
142 int	ktr_alq_depth = KTR_ENTRIES;
143 int	ktr_alq_enabled = 0;
144 int	ktr_alq_failed = 0;
145 int	ktr_alq_max = 0;
146 
147 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
148     "Maximum number of entries to write");
149 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
150     "Current number of written entries");
151 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
152     "Number of times we overran the buffer");
153 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
154     "Number of items in the write buffer");
155 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
156     sizeof(ktr_alq_file), "KTR logging file");
157 
158 static int
159 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
160 {
161 	int error;
162 	int enable;
163 
164 	enable = ktr_alq_enabled;
165 
166 	error = sysctl_handle_int(oidp, &enable, 0, req);
167 	if (error || !req->newptr)
168 		return (error);
169 
170 	if (enable) {
171 		if (ktr_alq_enabled)
172 			return (0);
173 		error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
174 		    req->td->td_ucred, ALQ_DEFAULT_CMODE,
175 		    sizeof(struct ktr_entry), ktr_alq_depth);
176 		if (error == 0) {
177 			ktr_alq_cnt = 0;
178 			ktr_alq_failed = 0;
179 			ktr_alq_enabled = 1;
180 		}
181 	} else {
182 		if (ktr_alq_enabled == 0)
183 			return (0);
184 		ktr_alq_enabled = 0;
185 		alq_close(ktr_alq);
186 		ktr_alq = NULL;
187 	}
188 
189 	return (error);
190 }
191 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
192     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
193     "I", "Enable KTR logging");
194 #endif
195 
196 void
197 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
198     u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
199     u_long arg6)
200 {
201 	struct ktr_entry *entry;
202 #ifdef KTR_ALQ
203 	struct ale *ale = NULL;
204 #endif
205 	int newindex, saveindex;
206 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
207 	struct thread *td;
208 #endif
209 	int cpu;
210 
211 	if (panicstr)
212 		return;
213 	if ((ktr_mask & mask) == 0)
214 		return;
215 	cpu = KTR_CPU;
216 	if (((1 << cpu) & ktr_cpumask) == 0)
217 		return;
218 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
219 	td = curthread;
220 	if (td->td_pflags & TDP_INKTR)
221 		return;
222 	td->td_pflags |= TDP_INKTR;
223 #endif
224 #ifdef KTR_ALQ
225 	if (ktr_alq_enabled) {
226 		if (td->td_critnest == 0 &&
227 		    (td->td_flags & TDF_IDLETD) == 0 &&
228 		    td != ald_thread) {
229 			if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
230 				goto done;
231 			if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
232 				ktr_alq_failed++;
233 				goto done;
234 			}
235 			ktr_alq_cnt++;
236 			entry = (struct ktr_entry *)ale->ae_data;
237 		} else {
238 			goto done;
239 		}
240 	} else
241 #endif
242 	{
243 		do {
244 			saveindex = ktr_idx;
245 			newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
246 		} while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
247 		entry = &ktr_buf[saveindex];
248 	}
249 	entry->ktr_timestamp = KTR_TIME;
250 	entry->ktr_cpu = cpu;
251 	entry->ktr_thread = curthread;
252 	if (file != NULL)
253 		while (strncmp(file, "../", 3) == 0)
254 			file += 3;
255 	entry->ktr_file = file;
256 	entry->ktr_line = line;
257 #ifdef KTR_VERBOSE
258 	if (ktr_verbose) {
259 #ifdef SMP
260 		printf("cpu%d ", cpu);
261 #endif
262 		if (ktr_verbose > 1) {
263 			printf("%s.%d\t", entry->ktr_file,
264 			    entry->ktr_line);
265 		}
266 		printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
267 		printf("\n");
268 	}
269 #endif
270 	entry->ktr_desc = format;
271 	entry->ktr_parms[0] = arg1;
272 	entry->ktr_parms[1] = arg2;
273 	entry->ktr_parms[2] = arg3;
274 	entry->ktr_parms[3] = arg4;
275 	entry->ktr_parms[4] = arg5;
276 	entry->ktr_parms[5] = arg6;
277 #ifdef KTR_ALQ
278 	if (ktr_alq_enabled && ale)
279 		alq_post(ktr_alq, ale);
280 done:
281 #endif
282 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
283 	td->td_pflags &= ~TDP_INKTR;
284 #endif
285 }
286 
287 #ifdef DDB
288 
289 struct tstate {
290 	int	cur;
291 	int	first;
292 };
293 static	struct tstate tstate;
294 static	int db_ktr_verbose;
295 static	int db_mach_vtrace(void);
296 
297 DB_SHOW_COMMAND(ktr, db_ktr_all)
298 {
299 
300 	tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
301 	tstate.first = -1;
302 	db_ktr_verbose = 0;
303 	db_ktr_verbose |= (index(modif, 'v') != NULL) ? 2 : 0;
304 	db_ktr_verbose |= (index(modif, 'V') != NULL) ? 1 : 0; /* just timestap please */
305 	if (index(modif, 'a') != NULL) {
306 		db_disable_pager();
307 		while (cncheckc() != -1)
308 			if (db_mach_vtrace() == 0)
309 				break;
310 	} else {
311 		while (!db_pager_quit)
312 			if (db_mach_vtrace() == 0)
313 				break;
314 	}
315 }
316 
317 static int
318 db_mach_vtrace(void)
319 {
320 	struct ktr_entry	*kp;
321 
322 	if (tstate.cur == tstate.first) {
323 		db_printf("--- End of trace buffer ---\n");
324 		return (0);
325 	}
326 	kp = &ktr_buf[tstate.cur];
327 
328 	/* Skip over unused entries. */
329 	if (kp->ktr_desc == NULL) {
330 		db_printf("--- End of trace buffer ---\n");
331 		return (0);
332 	}
333 	db_printf("%d (%p", tstate.cur, kp->ktr_thread);
334 #ifdef SMP
335 	db_printf(":cpu%d", kp->ktr_cpu);
336 #endif
337 	db_printf(")");
338 	if (db_ktr_verbose >= 1) {
339 		db_printf(" %10.10lld", (long long)kp->ktr_timestamp);
340 	}
341 	if (db_ktr_verbose >= 2) {
342 		db_printf(" %s.%d", kp->ktr_file, kp->ktr_line);
343 	}
344 	db_printf(": ");
345 	db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
346 	    kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
347 	    kp->ktr_parms[5]);
348 	db_printf("\n");
349 
350 	if (tstate.first == -1)
351 		tstate.first = tstate.cur;
352 
353 	if (--tstate.cur < 0)
354 		tstate.cur = KTR_ENTRIES - 1;
355 
356 	return (1);
357 }
358 
359 #endif	/* DDB */
360