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