xref: /freebsd/sys/kern/kern_ktr.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
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_ddb.h"
38 #include "opt_ktr.h"
39 
40 #include <sys/param.h>
41 #include <sys/cons.h>
42 #include <sys/kernel.h>
43 #include <sys/ktr.h>
44 #include <sys/libkern.h>
45 #include <sys/proc.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/time.h>
49 
50 #include <machine/cpu.h>
51 #ifdef __sparc64__
52 #include <machine/ktr.h>
53 #endif
54 
55 #include <ddb/ddb.h>
56 
57 #ifndef KTR_ENTRIES
58 #define	KTR_ENTRIES	1024
59 #endif
60 
61 #ifndef KTR_MASK
62 #define	KTR_MASK	(KTR_GEN)
63 #endif
64 
65 #ifndef KTR_CPUMASK
66 #define	KTR_CPUMASK	(~0)
67 #endif
68 
69 #ifndef KTR_TIME
70 #define	KTR_TIME	get_cyclecount()
71 #endif
72 
73 #ifndef KTR_CPU
74 #define	KTR_CPU		PCPU_GET(cpuid)
75 #endif
76 
77 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
78 
79 int	ktr_cpumask = KTR_CPUMASK;
80 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
81 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
82 
83 int	ktr_mask = KTR_MASK;
84 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
85 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
86 
87 int	ktr_entries = KTR_ENTRIES;
88 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
89 
90 int	ktr_version = KTR_VERSION;
91 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
92 
93 volatile int	ktr_idx = 0;
94 struct	ktr_entry ktr_buf[KTR_ENTRIES];
95 
96 #ifdef KTR_VERBOSE
97 int	ktr_verbose = KTR_VERBOSE;
98 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
99 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
100 #endif
101 
102 void
103 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
104     u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
105     u_long arg6)
106 {
107 	struct ktr_entry *entry;
108 	int newindex, saveindex;
109 #ifdef KTR_VERBOSE
110 	struct thread *td;
111 #endif
112 	int cpu;
113 
114 	if (panicstr)
115 		return;
116 	if ((ktr_mask & mask) == 0)
117 		return;
118 	cpu = KTR_CPU;
119 	if (((1 << cpu) & ktr_cpumask) == 0)
120 		return;
121 #ifdef KTR_VERBOSE
122 	td = curthread;
123 	if (td->td_inktr)
124 		return;
125 	td->td_inktr++;
126 #endif
127 	do {
128 		saveindex = ktr_idx;
129 		newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
130 	} while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
131 	entry = &ktr_buf[saveindex];
132 	entry->ktr_timestamp = KTR_TIME;
133 	entry->ktr_cpu = cpu;
134 	entry->ktr_file = file;
135 	entry->ktr_line = line;
136 #ifdef KTR_VERBOSE
137 	if (ktr_verbose) {
138 #ifdef SMP
139 		printf("cpu%d ", cpu);
140 #endif
141 		if (ktr_verbose > 1) {
142 			printf("%s.%d\t", entry->ktr_file,
143 			    entry->ktr_line);
144 		}
145 		printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
146 		printf("\n");
147 	}
148 #endif
149 	entry->ktr_desc = format;
150 	entry->ktr_parms[0] = arg1;
151 	entry->ktr_parms[1] = arg2;
152 	entry->ktr_parms[2] = arg3;
153 	entry->ktr_parms[3] = arg4;
154 	entry->ktr_parms[4] = arg5;
155 	entry->ktr_parms[5] = arg6;
156 #ifdef KTR_VERBOSE
157 	td->td_inktr--;
158 #endif
159 }
160 
161 #ifdef DDB
162 
163 struct tstate {
164 	int	cur;
165 	int	first;
166 };
167 static	struct tstate tstate;
168 static	int db_ktr_verbose;
169 static	int db_mach_vtrace(void);
170 
171 #define	NUM_LINES_PER_PAGE	18
172 
173 DB_SHOW_COMMAND(ktr, db_ktr_all)
174 {
175 	int	c, lines;
176 
177 	lines = NUM_LINES_PER_PAGE;
178 	tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
179 	tstate.first = -1;
180 	if (strcmp(modif, "v") == 0)
181 		db_ktr_verbose = 1;
182 	else
183 		db_ktr_verbose = 0;
184 	while (db_mach_vtrace())
185 		if (--lines == 0) {
186 			db_printf("--More--");
187 			c = cngetc();
188 			db_printf("\r");
189 			switch (c) {
190 			case '\n':	/* one more line */
191 				lines = 1;
192 				break;
193 			case ' ':	/* one more page */
194 				lines = NUM_LINES_PER_PAGE;
195 				break;
196 			default:
197 				db_printf("\n");
198 				return;
199 			}
200 		}
201 }
202 
203 static int
204 db_mach_vtrace(void)
205 {
206 	struct ktr_entry	*kp;
207 
208 	if (tstate.cur == tstate.first) {
209 		db_printf("--- End of trace buffer ---\n");
210 		return (0);
211 	}
212 	kp = &ktr_buf[tstate.cur];
213 
214 	/* Skip over unused entries. */
215 	if (kp->ktr_desc == NULL) {
216 		db_printf("--- End of trace buffer ---\n");
217 		return (0);
218 	}
219 	db_printf("%d: ", tstate.cur);
220 #ifdef SMP
221 	db_printf("cpu%d ", kp->ktr_cpu);
222 #endif
223 	if (db_ktr_verbose) {
224 		db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp,
225 		    kp->ktr_file, kp->ktr_line);
226 	}
227 	db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
228 	    kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
229 	    kp->ktr_parms[5]);
230 	db_printf("\n");
231 
232 	if (tstate.first == -1)
233 		tstate.first = tstate.cur;
234 
235 	if (--tstate.cur < 0)
236 		tstate.cur = KTR_ENTRIES - 1;
237 
238 	return (1);
239 }
240 
241 #endif	/* DDB */
242