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