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