xref: /freebsd/sys/kern/kern_ktr.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*
2  * Copyright (c) 2000
3  *	John Baldwin <jhb@FreeBSD.org>.  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  * 4. 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 JOHN BALDWIN 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 JOHN BALDWIN OR THE VOICES IN HIS HEAD
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * This module holds the global variables used by KTR and the ktr_tracepoint()
34  * function that does the actual tracing.
35  */
36 
37 #include "opt_ktr.h"
38 
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <sys/ktr.h>
42 #include <sys/libkern.h>
43 #include <sys/linker_set.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46 #include <machine/globals.h>
47 #include <machine/stdarg.h>
48 
49 #ifndef KTR_MASK
50 #define	KTR_MASK	(KTR_GEN)
51 #endif
52 
53 #ifndef KTR_CPUMASK
54 #define	KTR_CPUMASK	(~0)
55 #endif
56 
57 #ifdef SMP
58 #define KTR_CPU		cpuid
59 #else
60 #define KTR_CPU		0
61 #endif
62 
63 #ifdef KTR_EXTEND
64 /*
65  * This variable is used only by gdb to work out what fields are in
66  * ktr_entry.
67  */
68 int     ktr_extend = 1;
69 SYSCTL_INT(_debug, OID_AUTO, ktr_extend, CTLFLAG_RD, &ktr_extend, 1, "");
70 #else
71 int     ktr_extend = 0;
72 SYSCTL_INT(_debug, OID_AUTO, ktr_extend, CTLFLAG_RD, &ktr_extend, 0, "");
73 #endif	/* KTR_EXTEND */
74 
75 int	ktr_cpumask = KTR_CPUMASK;
76 SYSCTL_INT(_debug, OID_AUTO, ktr_cpumask, CTLFLAG_RW, &ktr_cpumask, KTR_CPUMASK, "");
77 
78 int	ktr_mask = KTR_MASK;
79 SYSCTL_INT(_debug, OID_AUTO, ktr_mask, CTLFLAG_RW, &ktr_mask, KTR_MASK, "");
80 
81 int	ktr_entries = KTR_ENTRIES;
82 SYSCTL_INT(_debug, OID_AUTO, ktr_entries, CTLFLAG_RD, &ktr_entries, KTR_ENTRIES, "");
83 
84 volatile int	ktr_idx = 0;
85 struct	ktr_entry ktr_buf[KTR_ENTRIES];
86 
87 #ifdef KTR_VERBOSE
88 int	ktr_verbose = 1;
89 #else
90 int	ktr_verbose = 0;
91 #endif
92 SYSCTL_INT(_debug, OID_AUTO, ktr_verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
93 
94 #ifdef KTR
95 #ifdef KTR_EXTEND
96 void
97 ktr_tracepoint(u_int mask, char *filename, u_int line, char *format, ...)
98 #else
99 void
100 ktr_tracepoint(u_int mask, char *format, u_long arg1, u_long arg2, u_long arg3,
101 	       u_long arg4, u_long arg5)
102 #endif
103 {
104 	struct ktr_entry *entry;
105 	int newindex, saveindex, saveintr;
106 #ifdef KTR_EXTEND
107 	va_list ap;
108 #endif
109 
110 	if ((ktr_mask & mask) == 0)
111 		return;
112 #ifdef KTR_EXTEND
113 	if (((1 << KTR_CPU) & ktr_cpumask) == 0)
114 		return;
115 #endif
116 	saveintr = save_intr();
117 	disable_intr();
118 	do {
119 		saveindex = ktr_idx;
120 		newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
121 	} while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
122 	entry = &ktr_buf[saveindex];
123 	restore_intr(saveintr);
124 	nanotime(&entry->ktr_tv);
125 #ifdef KTR_EXTEND
126 	strncpy(entry->ktr_filename, filename, KTRFILENAMESIZE - 1);
127 	entry->ktr_filename[KTRFILENAMESIZE - 1] = '\0';
128 	entry->ktr_line = line;
129 	entry->ktr_cpu = KTR_CPU;
130 	va_start(ap, format);
131 	vsnprintf(entry->ktr_desc, KTRDESCSIZE, format, ap);
132 	va_end(ap);
133 	if (ktr_verbose) {
134 #ifdef SMP
135 		printf("cpu%d ", entry->ktr_cpu);
136 #endif
137 		if (ktr_verbose > 1)
138 			printf("%s.%d\t", entry->ktr_filename, entry->ktr_line);
139 		va_start(ap, format);
140 		vprintf(format, ap);
141 		printf("\n");
142 		va_end(ap);
143 	}
144 #else
145 	entry->ktr_desc = format;
146 	entry->ktr_parm1 = arg1;
147 	entry->ktr_parm2 = arg2;
148 	entry->ktr_parm3 = arg3;
149 	entry->ktr_parm4 = arg4;
150 	entry->ktr_parm5 = arg5;
151 #endif
152 }
153 #endif	/* KTR */
154