xref: /freebsd/sys/kern/kern_ktr.c (revision d086ded32300bc0f33fb1574d0bcfccfbc60881d)
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 
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$");
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/alq.h>
44 #include <sys/cons.h>
45 #include <sys/kernel.h>
46 #include <sys/ktr.h>
47 #include <sys/libkern.h>
48 #include <sys/proc.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/time.h>
52 
53 #include <machine/cpu.h>
54 #ifdef __sparc64__
55 #include <machine/ktr.h>
56 #endif
57 
58 
59 #include <ddb/ddb.h>
60 
61 #ifndef KTR_ENTRIES
62 #define	KTR_ENTRIES	1024
63 #endif
64 
65 #ifndef KTR_MASK
66 #define	KTR_MASK	(KTR_GEN)
67 #endif
68 
69 #ifndef KTR_CPUMASK
70 #define	KTR_CPUMASK	(~0)
71 #endif
72 
73 #ifndef KTR_TIME
74 #define	KTR_TIME	get_cyclecount()
75 #endif
76 
77 #ifndef KTR_CPU
78 #define	KTR_CPU		PCPU_GET(cpuid)
79 #endif
80 
81 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
82 
83 int	ktr_cpumask = KTR_CPUMASK;
84 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
85 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
86 
87 int	ktr_mask = KTR_MASK;
88 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
89 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
90 
91 int	ktr_entries = KTR_ENTRIES;
92 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
93 
94 int	ktr_version = KTR_VERSION;
95 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
96 
97 volatile int	ktr_idx = 0;
98 struct	ktr_entry ktr_buf[KTR_ENTRIES];
99 
100 #ifdef KTR_VERBOSE
101 int	ktr_verbose = KTR_VERBOSE;
102 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
103 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
104 #endif
105 
106 #ifdef KTR_ALQ
107 struct alq *ktr_alq;
108 char	ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
109 int	ktr_alq_cnt = 0;
110 int	ktr_alq_depth = KTR_ENTRIES;
111 int	ktr_alq_enabled = 0;
112 int	ktr_alq_failed = 0;
113 int	ktr_alq_max = 0;
114 
115 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
116     "Maximum number of entries to write");
117 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
118     "Current number of written entries");
119 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
120     "Number of times we overran the buffer");
121 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
122     "Number of items in the write buffer");
123 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
124     sizeof(ktr_alq_file), "KTR logging file");
125 
126 static int
127 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
128 {
129 	int error;
130 	int enable;
131 
132 	enable = ktr_alq_enabled;
133 
134         error = sysctl_handle_int(oidp, &enable, 0, req);
135         if (error || !req->newptr)
136                 return (error);
137 
138 	if (enable) {
139 		if (ktr_alq_enabled)
140 			return (0);
141 		error = suser(curthread);
142 		if (error)
143 			return (error);
144 		error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
145 		    sizeof(struct ktr_entry), ktr_alq_depth);
146 		if (error == 0) {
147 			ktr_mask &= ~KTR_ALQ_MASK;
148 			ktr_alq_cnt = 0;
149 			ktr_alq_failed = 0;
150 			ktr_alq_enabled = 1;
151 		}
152 	} else {
153 		if (ktr_alq_enabled == 0)
154 			return (0);
155 		ktr_alq_enabled = 0;
156 		alq_close(ktr_alq);
157 		ktr_alq = NULL;
158 	}
159 
160 	return (error);
161 }
162 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
163     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
164     "I", "Enable KTR logging");
165 #endif
166 
167 void
168 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
169     u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
170     u_long arg6)
171 {
172 	struct ktr_entry *entry;
173 #ifdef KTR_ALQ
174 	struct ale *ale = NULL;
175 #else
176 	int newindex, saveindex;
177 #endif
178 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
179 	struct thread *td;
180 #endif
181 	int cpu;
182 
183 	if (panicstr)
184 		return;
185 	if ((ktr_mask & mask) == 0)
186 		return;
187 	cpu = KTR_CPU;
188 	if (((1 << cpu) & ktr_cpumask) == 0)
189 		return;
190 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
191 	td = curthread;
192 	if (td->td_pflags & TDP_INKTR)
193 		return;
194 	td->td_pflags |= TDP_INKTR;
195 #endif
196 #ifdef KTR_ALQ
197 	if (ktr_alq_enabled &&
198 	    td->td_critnest == 0 &&
199 	    (td->td_flags & TDF_IDLETD) == 0 &&
200 	    td != ald_thread) {
201 		if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
202 			goto done;
203 		if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
204 			ktr_alq_failed++;
205 			goto done;
206 		}
207 		ktr_alq_cnt++;
208 		entry = (struct ktr_entry *)ale->ae_data;
209 	} else
210 		goto done;
211 #else
212 	do {
213 		saveindex = ktr_idx;
214 		newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
215 	} while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
216 	entry = &ktr_buf[saveindex];
217 #endif
218 	entry->ktr_timestamp = KTR_TIME;
219 	entry->ktr_cpu = cpu;
220 	if (file != NULL)
221 		while (strncmp(file, "../", 3) == 0)
222 			file += 3;
223 	entry->ktr_file = file;
224 	entry->ktr_line = line;
225 #ifdef KTR_VERBOSE
226 	if (ktr_verbose) {
227 #ifdef SMP
228 		printf("cpu%d ", cpu);
229 #endif
230 		if (ktr_verbose > 1) {
231 			printf("%s.%d\t", entry->ktr_file,
232 			    entry->ktr_line);
233 		}
234 		printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
235 		printf("\n");
236 	}
237 #endif
238 	entry->ktr_desc = format;
239 	entry->ktr_parms[0] = arg1;
240 	entry->ktr_parms[1] = arg2;
241 	entry->ktr_parms[2] = arg3;
242 	entry->ktr_parms[3] = arg4;
243 	entry->ktr_parms[4] = arg5;
244 	entry->ktr_parms[5] = arg6;
245 #ifdef KTR_ALQ
246 	if (ale)
247 		alq_post(ktr_alq, ale);
248 done:
249 #endif
250 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
251 	td->td_pflags &= ~TDP_INKTR;
252 #endif
253 }
254 
255 #ifdef DDB
256 
257 struct tstate {
258 	int	cur;
259 	int	first;
260 };
261 static	struct tstate tstate;
262 static	int db_ktr_verbose;
263 static	int db_mach_vtrace(void);
264 
265 #define	NUM_LINES_PER_PAGE	18
266 
267 DB_SHOW_COMMAND(ktr, db_ktr_all)
268 {
269 	int	c, lines;
270 	int	all = 0;
271 
272 	lines = NUM_LINES_PER_PAGE;
273 	tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
274 	tstate.first = -1;
275 	if (strcmp(modif, "v") == 0)
276 		db_ktr_verbose = 1;
277 	else
278 		db_ktr_verbose = 0;
279 	if (strcmp(modif, "a") == 0)
280 		all = 1;
281 	while (db_mach_vtrace())
282 		if (all) {
283 			if (cncheckc() != -1)
284 				return;
285 		} else if (--lines == 0) {
286 			db_printf("--More--");
287 			c = cngetc();
288 			db_printf("\r");
289 			switch (c) {
290 			case '\n':	/* one more line */
291 				lines = 1;
292 				break;
293 			case ' ':	/* one more page */
294 				lines = NUM_LINES_PER_PAGE;
295 				break;
296 			default:
297 				db_printf("\n");
298 				return;
299 			}
300 		}
301 }
302 
303 static int
304 db_mach_vtrace(void)
305 {
306 	struct ktr_entry	*kp;
307 
308 	if (tstate.cur == tstate.first) {
309 		db_printf("--- End of trace buffer ---\n");
310 		return (0);
311 	}
312 	kp = &ktr_buf[tstate.cur];
313 
314 	/* Skip over unused entries. */
315 	if (kp->ktr_desc == NULL) {
316 		db_printf("--- End of trace buffer ---\n");
317 		return (0);
318 	}
319 	db_printf("%d: ", tstate.cur);
320 #ifdef SMP
321 	db_printf("cpu%d ", kp->ktr_cpu);
322 #endif
323 	if (db_ktr_verbose) {
324 		db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp,
325 		    kp->ktr_file, kp->ktr_line);
326 	}
327 	db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
328 	    kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
329 	    kp->ktr_parms[5]);
330 	db_printf("\n");
331 
332 	if (tstate.first == -1)
333 		tstate.first = tstate.cur;
334 
335 	if (--tstate.cur < 0)
336 		tstate.cur = KTR_ENTRIES - 1;
337 
338 	return (1);
339 }
340 
341 #endif	/* DDB */
342