xref: /freebsd/sys/kern/kern_ktr.c (revision dda5b39711dab90ae1c5624bdd6ff7453177df31)
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * This module holds the global variables used by KTR and the ktr_tracepoint()
29  * function that does the actual tracing.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_ddb.h"
36 #include "opt_ktr.h"
37 #include "opt_alq.h"
38 
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/alq.h>
42 #include <sys/cons.h>
43 #include <sys/cpuset.h>
44 #include <sys/kernel.h>
45 #include <sys/ktr.h>
46 #include <sys/libkern.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/proc.h>
51 #include <sys/smp.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54 #include <sys/time.h>
55 
56 #include <machine/cpu.h>
57 #ifdef __sparc64__
58 #include <machine/ktr.h>
59 #endif
60 
61 #ifdef DDB
62 #include <ddb/ddb.h>
63 #include <ddb/db_output.h>
64 #endif
65 
66 #ifndef KTR_BOOT_ENTRIES
67 #define	KTR_BOOT_ENTRIES	1024
68 #endif
69 
70 #ifndef KTR_ENTRIES
71 #define	KTR_ENTRIES	1024
72 #endif
73 
74 /* Limit the allocations to something manageable. */
75 #define	KTR_ENTRIES_MAX	(8 * 1024 * 1024)
76 
77 #ifndef KTR_MASK
78 #define	KTR_MASK	(0)
79 #endif
80 
81 #ifndef KTR_CPUMASK
82 #define	KTR_CPUMASK	CPUSET_FSET
83 #endif
84 
85 #ifndef KTR_TIME
86 #define	KTR_TIME	get_cyclecount()
87 #endif
88 
89 #ifndef KTR_CPU
90 #define	KTR_CPU		PCPU_GET(cpuid)
91 #endif
92 
93 static MALLOC_DEFINE(M_KTR, "KTR", "KTR");
94 
95 FEATURE(ktr, "Kernel support for KTR kernel tracing facility");
96 
97 volatile int	ktr_idx = 0;
98 int	ktr_mask = KTR_MASK;
99 int	ktr_compile = KTR_COMPILE;
100 int	ktr_entries = KTR_BOOT_ENTRIES;
101 int	ktr_version = KTR_VERSION;
102 struct	ktr_entry ktr_buf_init[KTR_BOOT_ENTRIES];
103 struct	ktr_entry *ktr_buf = ktr_buf_init;
104 cpuset_t ktr_cpumask = CPUSET_T_INITIALIZER(KTR_CPUMASK);
105 static char ktr_cpumask_str[CPUSETBUFSIZ];
106 
107 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
108 
109 TUNABLE_STR("debug.ktr.cpumask", ktr_cpumask_str, sizeof(ktr_cpumask_str));
110 
111 static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
112 
113 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD,
114     &ktr_version, 0, "Version of the KTR interface");
115 
116 SYSCTL_UINT(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD,
117     &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel");
118 
119 static void
120 ktr_cpumask_initializer(void *dummy __unused)
121 {
122 
123 	/*
124 	 * TUNABLE_STR() runs with SI_ORDER_MIDDLE priority, thus it must be
125 	 * already set, if necessary.
126 	 */
127 	if (ktr_cpumask_str[0] != '\0' &&
128 	    cpusetobj_strscan(&ktr_cpumask, ktr_cpumask_str) == -1)
129 		CPU_FILL(&ktr_cpumask);
130 }
131 SYSINIT(ktr_cpumask_initializer, SI_SUB_TUNABLES, SI_ORDER_ANY,
132     ktr_cpumask_initializer, NULL);
133 
134 static int
135 sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS)
136 {
137 	char lktr_cpumask_str[CPUSETBUFSIZ];
138 	cpuset_t imask;
139 	int error;
140 
141 	cpusetobj_strprint(lktr_cpumask_str, &ktr_cpumask);
142 	error = sysctl_handle_string(oidp, lktr_cpumask_str,
143 	    sizeof(lktr_cpumask_str), req);
144 	if (error != 0 || req->newptr == NULL)
145 		return (error);
146 	if (cpusetobj_strscan(&imask, lktr_cpumask_str) == -1)
147 		return (EINVAL);
148 	CPU_COPY(&imask, &ktr_cpumask);
149 
150 	return (error);
151 }
152 SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask,
153     CTLFLAG_RW | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0,
154     sysctl_debug_ktr_cpumask, "S",
155     "Bitmask of CPUs on which KTR logging is enabled");
156 
157 static int
158 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
159 {
160 	int clear, error;
161 
162 	clear = 0;
163 	error = sysctl_handle_int(oidp, &clear, 0, req);
164 	if (error || !req->newptr)
165 		return (error);
166 
167 	if (clear) {
168 		bzero(ktr_buf, sizeof(*ktr_buf) * ktr_entries);
169 		ktr_idx = 0;
170 	}
171 
172 	return (error);
173 }
174 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
175     sysctl_debug_ktr_clear, "I", "Clear KTR Buffer");
176 
177 /*
178  * This is a sysctl proc so that it is serialized as !MPSAFE along with
179  * the other ktr sysctl procs.
180  */
181 static int
182 sysctl_debug_ktr_mask(SYSCTL_HANDLER_ARGS)
183 {
184 	int mask, error;
185 
186 	mask = ktr_mask;
187 	error = sysctl_handle_int(oidp, &mask, 0, req);
188 	if (error || !req->newptr)
189 		return (error);
190 	ktr_mask = mask;
191 	return (error);
192 }
193 
194 SYSCTL_PROC(_debug_ktr, OID_AUTO, mask, CTLTYPE_UINT|CTLFLAG_RW, 0, 0,
195     sysctl_debug_ktr_mask, "IU",
196     "Bitmask of KTR event classes for which logging is enabled");
197 
198 #if KTR_ENTRIES > KTR_BOOT_ENTRIES
199 /*
200  * A simplified version of sysctl_debug_ktr_entries.
201  * No need to care about SMP, scheduling, etc.
202  */
203 static void
204 ktr_entries_initializer(void *dummy __unused)
205 {
206 	int mask;
207 
208 	/* Temporarily disable ktr in case malloc() is being traced. */
209 	mask = ktr_mask;
210 	ktr_mask = 0;
211 	ktr_buf = malloc(sizeof(*ktr_buf) * KTR_ENTRIES, M_KTR,
212 	    M_WAITOK | M_ZERO);
213 	memcpy(ktr_buf, ktr_buf_init + ktr_idx,
214 	    (KTR_BOOT_ENTRIES - ktr_idx) * sizeof(*ktr_buf));
215 	if (ktr_idx != 0)
216 		memcpy(ktr_buf + KTR_BOOT_ENTRIES - ktr_idx, ktr_buf_init,
217 		    ktr_idx * sizeof(*ktr_buf));
218 	ktr_entries = KTR_ENTRIES;
219 	ktr_mask = mask;
220 }
221 SYSINIT(ktr_entries_initializer, SI_SUB_KMEM, SI_ORDER_ANY,
222     ktr_entries_initializer, NULL);
223 #endif
224 
225 static int
226 sysctl_debug_ktr_entries(SYSCTL_HANDLER_ARGS)
227 {
228 	int entries, error, mask;
229 	struct ktr_entry *buf, *oldbuf;
230 
231 	entries = ktr_entries;
232 	error = sysctl_handle_int(oidp, &entries, 0, req);
233 	if (error || !req->newptr)
234 		return (error);
235 	if (entries > KTR_ENTRIES_MAX)
236 		return (ERANGE);
237 	/* Disable ktr temporarily. */
238 	mask = ktr_mask;
239 	atomic_store_rel_int(&ktr_mask, 0);
240 	/* Wait for threads to go idle. */
241 	if ((error = quiesce_all_cpus("ktrent", PCATCH)) != 0) {
242 		ktr_mask = mask;
243 		return (error);
244 	}
245 	if (ktr_buf != ktr_buf_init)
246 		oldbuf = ktr_buf;
247 	else
248 		oldbuf = NULL;
249 	/* Allocate a new buffer. */
250 	buf = malloc(sizeof(*buf) * entries, M_KTR, M_WAITOK | M_ZERO);
251 	/* Install the new buffer and restart ktr. */
252 	ktr_buf = buf;
253 	ktr_entries = entries;
254 	ktr_idx = 0;
255 	atomic_store_rel_int(&ktr_mask, mask);
256 	if (oldbuf != NULL)
257 		free(oldbuf, M_KTR);
258 
259 	return (error);
260 }
261 
262 SYSCTL_PROC(_debug_ktr, OID_AUTO, entries, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
263     sysctl_debug_ktr_entries, "I", "Number of entries in the KTR buffer");
264 
265 #ifdef KTR_VERBOSE
266 int	ktr_verbose = KTR_VERBOSE;
267 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
268 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
269 #endif
270 
271 #ifdef KTR_ALQ
272 struct alq *ktr_alq;
273 char	ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
274 int	ktr_alq_cnt = 0;
275 int	ktr_alq_depth = KTR_ENTRIES;
276 int	ktr_alq_enabled = 0;
277 int	ktr_alq_failed = 0;
278 int	ktr_alq_max = 0;
279 
280 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
281     "Maximum number of entries to write");
282 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
283     "Current number of written entries");
284 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
285     "Number of times we overran the buffer");
286 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
287     "Number of items in the write buffer");
288 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
289     sizeof(ktr_alq_file), "KTR logging file");
290 
291 static int
292 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
293 {
294 	int error;
295 	int enable;
296 
297 	enable = ktr_alq_enabled;
298 
299 	error = sysctl_handle_int(oidp, &enable, 0, req);
300 	if (error || !req->newptr)
301 		return (error);
302 
303 	if (enable) {
304 		if (ktr_alq_enabled)
305 			return (0);
306 		error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
307 		    req->td->td_ucred, ALQ_DEFAULT_CMODE,
308 		    sizeof(struct ktr_entry), ktr_alq_depth);
309 		if (error == 0) {
310 			ktr_alq_cnt = 0;
311 			ktr_alq_failed = 0;
312 			ktr_alq_enabled = 1;
313 		}
314 	} else {
315 		if (ktr_alq_enabled == 0)
316 			return (0);
317 		ktr_alq_enabled = 0;
318 		alq_close(ktr_alq);
319 		ktr_alq = NULL;
320 	}
321 
322 	return (error);
323 }
324 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
325     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
326     "I", "Enable KTR logging");
327 #endif
328 
329 void
330 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
331     u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
332     u_long arg6)
333 {
334 	struct ktr_entry *entry;
335 #ifdef KTR_ALQ
336 	struct ale *ale = NULL;
337 #endif
338 	int newindex, saveindex;
339 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
340 	struct thread *td;
341 #endif
342 	int cpu;
343 
344 	if (panicstr)
345 		return;
346 	if ((ktr_mask & mask) == 0 || ktr_buf == NULL)
347 		return;
348 	cpu = KTR_CPU;
349 	if (!CPU_ISSET(cpu, &ktr_cpumask))
350 		return;
351 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
352 	td = curthread;
353 	if (td->td_pflags & TDP_INKTR)
354 		return;
355 	td->td_pflags |= TDP_INKTR;
356 #endif
357 #ifdef KTR_ALQ
358 	if (ktr_alq_enabled) {
359 		if (td->td_critnest == 0 &&
360 		    (td->td_flags & TDF_IDLETD) == 0 &&
361 		    td != ald_thread) {
362 			if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
363 				goto done;
364 			if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
365 				ktr_alq_failed++;
366 				goto done;
367 			}
368 			ktr_alq_cnt++;
369 			entry = (struct ktr_entry *)ale->ae_data;
370 		} else {
371 			goto done;
372 		}
373 	} else
374 #endif
375 	{
376 		do {
377 			saveindex = ktr_idx;
378 			newindex = (saveindex + 1) % ktr_entries;
379 		} while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
380 		entry = &ktr_buf[saveindex];
381 	}
382 	entry->ktr_timestamp = KTR_TIME;
383 	entry->ktr_cpu = cpu;
384 	entry->ktr_thread = curthread;
385 	if (file != NULL)
386 		while (strncmp(file, "../", 3) == 0)
387 			file += 3;
388 	entry->ktr_file = file;
389 	entry->ktr_line = line;
390 #ifdef KTR_VERBOSE
391 	if (ktr_verbose) {
392 #ifdef SMP
393 		printf("cpu%d ", cpu);
394 #endif
395 		if (ktr_verbose > 1) {
396 			printf("%s.%d\t", entry->ktr_file,
397 			    entry->ktr_line);
398 		}
399 		printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
400 		printf("\n");
401 	}
402 #endif
403 	entry->ktr_desc = format;
404 	entry->ktr_parms[0] = arg1;
405 	entry->ktr_parms[1] = arg2;
406 	entry->ktr_parms[2] = arg3;
407 	entry->ktr_parms[3] = arg4;
408 	entry->ktr_parms[4] = arg5;
409 	entry->ktr_parms[5] = arg6;
410 #ifdef KTR_ALQ
411 	if (ktr_alq_enabled && ale)
412 		alq_post(ktr_alq, ale);
413 done:
414 #endif
415 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
416 	td->td_pflags &= ~TDP_INKTR;
417 #endif
418 }
419 
420 #ifdef DDB
421 
422 struct tstate {
423 	int	cur;
424 	int	first;
425 };
426 static	struct tstate tstate;
427 static	int db_ktr_verbose;
428 static	int db_mach_vtrace(void);
429 
430 DB_SHOW_COMMAND(ktr, db_ktr_all)
431 {
432 
433 	tstate.cur = (ktr_idx - 1) % ktr_entries;
434 	tstate.first = -1;
435 	db_ktr_verbose = 0;
436 	db_ktr_verbose |= (strchr(modif, 'v') != NULL) ? 2 : 0;
437 	db_ktr_verbose |= (strchr(modif, 'V') != NULL) ? 1 : 0; /* just timestap please */
438 	if (strchr(modif, 'a') != NULL) {
439 		db_disable_pager();
440 		while (cncheckc() != -1)
441 			if (db_mach_vtrace() == 0)
442 				break;
443 	} else {
444 		while (!db_pager_quit)
445 			if (db_mach_vtrace() == 0)
446 				break;
447 	}
448 }
449 
450 static int
451 db_mach_vtrace(void)
452 {
453 	struct ktr_entry	*kp;
454 
455 	if (tstate.cur == tstate.first || ktr_buf == NULL) {
456 		db_printf("--- End of trace buffer ---\n");
457 		return (0);
458 	}
459 	kp = &ktr_buf[tstate.cur];
460 
461 	/* Skip over unused entries. */
462 	if (kp->ktr_desc == NULL) {
463 		db_printf("--- End of trace buffer ---\n");
464 		return (0);
465 	}
466 	db_printf("%d (%p", tstate.cur, kp->ktr_thread);
467 #ifdef SMP
468 	db_printf(":cpu%d", kp->ktr_cpu);
469 #endif
470 	db_printf(")");
471 	if (db_ktr_verbose >= 1) {
472 		db_printf(" %10.10lld", (long long)kp->ktr_timestamp);
473 	}
474 	if (db_ktr_verbose >= 2) {
475 		db_printf(" %s.%d", kp->ktr_file, kp->ktr_line);
476 	}
477 	db_printf(": ");
478 	db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
479 	    kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
480 	    kp->ktr_parms[5]);
481 	db_printf("\n");
482 
483 	if (tstate.first == -1)
484 		tstate.first = tstate.cur;
485 
486 	if (--tstate.cur < 0)
487 		tstate.cur = ktr_entries - 1;
488 
489 	return (1);
490 }
491 
492 #endif	/* DDB */
493