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