Deleted Added
full compact
kern_ktr.c (74903) kern_ktr.c (77843)
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 *
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: head/sys/kern/kern_ktr.c 74903 2001-03-28 03:06:10Z jhb $
29 * $FreeBSD: head/sys/kern/kern_ktr.c 77843 2001-06-06 22:17:08Z peter $
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/types.h>
42#include <sys/cons.h>
43#include <sys/time.h>
44#include <sys/kernel.h>
45#include <sys/ktr.h>
46#include <sys/libkern.h>
47#include <sys/linker_set.h>
48#include <sys/sysctl.h>
49#include <sys/systm.h>
50#include <machine/globals.h>
51#include <machine/stdarg.h>
52
53#include <ddb/ddb.h>
54
55#ifndef KTR_MASK
56#define KTR_MASK (KTR_GEN)
57#endif
58
59#ifndef KTR_CPUMASK
60#define KTR_CPUMASK (~0)
61#endif
62
63#ifdef SMP
64#define KTR_CPU PCPU_GET(cpuid)
65#else
66#define KTR_CPU 0
67#endif
68
69#ifdef KTR_EXTEND
70#define KTR_EXTEND_DEFAULT 1
71#else
72#define KTR_EXTEND_DEFAULT 0
73#endif
74
75#ifdef KTR_VERBOSE
76#define KTR_VERBOSE_DEFAULT 1
77#else
78#define KTR_VERBOSE_DEFAULT 0
79#endif
80
81SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
82
83/*
84 * This variable is used only by gdb to work out what fields are in
85 * ktr_entry.
86 */
87int ktr_extend = KTR_EXTEND_DEFAULT;
88SYSCTL_INT(_debug_ktr, OID_AUTO, extend, CTLFLAG_RD, &ktr_extend, 0, "");
89
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/types.h>
42#include <sys/cons.h>
43#include <sys/time.h>
44#include <sys/kernel.h>
45#include <sys/ktr.h>
46#include <sys/libkern.h>
47#include <sys/linker_set.h>
48#include <sys/sysctl.h>
49#include <sys/systm.h>
50#include <machine/globals.h>
51#include <machine/stdarg.h>
52
53#include <ddb/ddb.h>
54
55#ifndef KTR_MASK
56#define KTR_MASK (KTR_GEN)
57#endif
58
59#ifndef KTR_CPUMASK
60#define KTR_CPUMASK (~0)
61#endif
62
63#ifdef SMP
64#define KTR_CPU PCPU_GET(cpuid)
65#else
66#define KTR_CPU 0
67#endif
68
69#ifdef KTR_EXTEND
70#define KTR_EXTEND_DEFAULT 1
71#else
72#define KTR_EXTEND_DEFAULT 0
73#endif
74
75#ifdef KTR_VERBOSE
76#define KTR_VERBOSE_DEFAULT 1
77#else
78#define KTR_VERBOSE_DEFAULT 0
79#endif
80
81SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
82
83/*
84 * This variable is used only by gdb to work out what fields are in
85 * ktr_entry.
86 */
87int ktr_extend = KTR_EXTEND_DEFAULT;
88SYSCTL_INT(_debug_ktr, OID_AUTO, extend, CTLFLAG_RD, &ktr_extend, 0, "");
89
90int ktr_cpumask;
91TUNABLE_INT_DECL("debug.ktr.cpumask", KTR_CPUMASK, ktr_cpumask);
90int ktr_cpumask = KTR_CPUMASK;
91TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
92SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
93
92SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
93
94int ktr_mask;
95TUNABLE_INT_DECL("debug.ktr.mask", KTR_MASK, ktr_mask);
94int ktr_mask = KTR_MASK;
95TUNABLE_INT("debug.ktr.mask", &ktr_mask);
96SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
97
98int ktr_entries = KTR_ENTRIES;
99SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
100
101volatile int ktr_idx = 0;
102struct ktr_entry ktr_buf[KTR_ENTRIES];
103
96SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
97
98int ktr_entries = KTR_ENTRIES;
99SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
100
101volatile int ktr_idx = 0;
102struct ktr_entry ktr_buf[KTR_ENTRIES];
103
104int ktr_verbose;
105TUNABLE_INT_DECL("debug.ktr.verbose", KTR_VERBOSE_DEFAULT, ktr_verbose);
104int ktr_verbose = KTR_VERBOSE_DEFAULT;
105TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
106SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
107
108#ifdef KTR
109#ifdef KTR_EXTEND
110void
111ktr_tracepoint(u_int mask, const char *filename, u_int line,
112 const char *format, ...)
113#else
114void
115ktr_tracepoint(u_int mask, const char *format, u_long arg1, u_long arg2,
116 u_long arg3, u_long arg4, u_long arg5)
117#endif
118{
119 struct ktr_entry *entry;
120 int newindex, saveindex;
121 critical_t savecrit;
122#ifdef KTR_EXTEND
123 va_list ap;
124#endif
125
126 if (panicstr)
127 return;
128 if ((ktr_mask & mask) == 0)
129 return;
130#ifdef KTR_EXTEND
131 if (((1 << KTR_CPU) & ktr_cpumask) == 0)
132 return;
133#endif
134 savecrit = critical_enter();
135 do {
136 saveindex = ktr_idx;
137 newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
138 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
139 entry = &ktr_buf[saveindex];
140 critical_exit(savecrit);
141 if (ktr_mask & KTR_LOCK)
142 /*
143 * We can't use nanotime with KTR_LOCK, it would cause
144 * endless recursion, at least under the Intel
145 * architecture.
146 */
147 getnanotime(&entry->ktr_tv);
148 else
149 nanotime(&entry->ktr_tv);
150#ifdef KTR_EXTEND
151 entry->ktr_filename = filename;
152 entry->ktr_line = line;
153 entry->ktr_cpu = KTR_CPU;
154 va_start(ap, format);
155 vsnprintf(entry->ktr_desc, KTRDESCSIZE, format, ap);
156 va_end(ap);
157 if (ktr_verbose) {
158#ifdef SMP
159 printf("cpu%d ", entry->ktr_cpu);
160#endif
161 if (ktr_verbose > 1)
162 printf("%s.%d\t", entry->ktr_filename, entry->ktr_line);
163 va_start(ap, format);
164 vprintf(format, ap);
165 printf("\n");
166 va_end(ap);
167 }
168#else
169 entry->ktr_desc = format;
170 entry->ktr_parm1 = arg1;
171 entry->ktr_parm2 = arg2;
172 entry->ktr_parm3 = arg3;
173 entry->ktr_parm4 = arg4;
174 entry->ktr_parm5 = arg5;
175#endif
176}
177
178#ifdef DDB
179
180struct tstate {
181 int cur;
182 int first;
183};
184static struct tstate tstate;
185static int db_ktr_verbose;
186static int db_mach_vtrace(void);
187
188#define NUM_LINES_PER_PAGE 18
189
190DB_SHOW_COMMAND(ktr, db_ktr_all)
191{
192 int c, lines;
193
194 lines = NUM_LINES_PER_PAGE;
195 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
196 tstate.first = -1;
197 if (strcmp(modif, "v") == 0)
198 db_ktr_verbose = 1;
199 else
200 db_ktr_verbose = 0;
201 while (db_mach_vtrace())
202 if (--lines == 0) {
203 db_printf("--More--");
204 c = cngetc();
205 db_printf("\r");
206 switch (c) {
207 case '\n': /* one more line */
208 lines = 1;
209 break;
210 case ' ': /* one more page */
211 lines = NUM_LINES_PER_PAGE;
212 break;
213 default:
214 db_printf("\n");
215 return;
216 }
217 }
218}
219
220static int
221db_mach_vtrace(void)
222{
223 struct ktr_entry *kp;
224
225 if (tstate.cur == tstate.first) {
226 db_printf("--- End of trace buffer ---\n");
227 return (0);
228 }
229 kp = &ktr_buf[tstate.cur];
230
231 /* Skip over unused entries. */
232#ifdef KTR_EXTEND
233 if (kp->ktr_desc[0] == '\0') {
234#else
235 if (kp->ktr_desc == NULL) {
236#endif
237 db_printf("--- End of trace buffer ---\n");
238 return (0);
239 }
240 db_printf("%d: ", tstate.cur);
241 if (db_ktr_verbose)
242 db_printf("%4ld.%06ld ", (long)kp->ktr_tv.tv_sec,
243 kp->ktr_tv.tv_nsec / 1000);
244#ifdef KTR_EXTEND
245#ifdef SMP
246 db_printf("cpu%d ", kp->ktr_cpu);
247#endif
248 if (db_ktr_verbose)
249 db_printf("%s.%d\t", kp->ktr_filename, kp->ktr_line);
250 db_printf("%s", kp->ktr_desc);
251#else
252 db_printf(kp->ktr_desc, kp->ktr_parm1, kp->ktr_parm2, kp->ktr_parm3,
253 kp->ktr_parm4, kp->ktr_parm5);
254#endif
255 db_printf("\n");
256
257 if (tstate.first == -1)
258 tstate.first = tstate.cur;
259
260 if (--tstate.cur < 0)
261 tstate.cur = KTR_ENTRIES - 1;
262
263 return (1);
264}
265
266#endif /* DDB */
267#endif /* KTR */
106SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
107
108#ifdef KTR
109#ifdef KTR_EXTEND
110void
111ktr_tracepoint(u_int mask, const char *filename, u_int line,
112 const char *format, ...)
113#else
114void
115ktr_tracepoint(u_int mask, const char *format, u_long arg1, u_long arg2,
116 u_long arg3, u_long arg4, u_long arg5)
117#endif
118{
119 struct ktr_entry *entry;
120 int newindex, saveindex;
121 critical_t savecrit;
122#ifdef KTR_EXTEND
123 va_list ap;
124#endif
125
126 if (panicstr)
127 return;
128 if ((ktr_mask & mask) == 0)
129 return;
130#ifdef KTR_EXTEND
131 if (((1 << KTR_CPU) & ktr_cpumask) == 0)
132 return;
133#endif
134 savecrit = critical_enter();
135 do {
136 saveindex = ktr_idx;
137 newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
138 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
139 entry = &ktr_buf[saveindex];
140 critical_exit(savecrit);
141 if (ktr_mask & KTR_LOCK)
142 /*
143 * We can't use nanotime with KTR_LOCK, it would cause
144 * endless recursion, at least under the Intel
145 * architecture.
146 */
147 getnanotime(&entry->ktr_tv);
148 else
149 nanotime(&entry->ktr_tv);
150#ifdef KTR_EXTEND
151 entry->ktr_filename = filename;
152 entry->ktr_line = line;
153 entry->ktr_cpu = KTR_CPU;
154 va_start(ap, format);
155 vsnprintf(entry->ktr_desc, KTRDESCSIZE, format, ap);
156 va_end(ap);
157 if (ktr_verbose) {
158#ifdef SMP
159 printf("cpu%d ", entry->ktr_cpu);
160#endif
161 if (ktr_verbose > 1)
162 printf("%s.%d\t", entry->ktr_filename, entry->ktr_line);
163 va_start(ap, format);
164 vprintf(format, ap);
165 printf("\n");
166 va_end(ap);
167 }
168#else
169 entry->ktr_desc = format;
170 entry->ktr_parm1 = arg1;
171 entry->ktr_parm2 = arg2;
172 entry->ktr_parm3 = arg3;
173 entry->ktr_parm4 = arg4;
174 entry->ktr_parm5 = arg5;
175#endif
176}
177
178#ifdef DDB
179
180struct tstate {
181 int cur;
182 int first;
183};
184static struct tstate tstate;
185static int db_ktr_verbose;
186static int db_mach_vtrace(void);
187
188#define NUM_LINES_PER_PAGE 18
189
190DB_SHOW_COMMAND(ktr, db_ktr_all)
191{
192 int c, lines;
193
194 lines = NUM_LINES_PER_PAGE;
195 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
196 tstate.first = -1;
197 if (strcmp(modif, "v") == 0)
198 db_ktr_verbose = 1;
199 else
200 db_ktr_verbose = 0;
201 while (db_mach_vtrace())
202 if (--lines == 0) {
203 db_printf("--More--");
204 c = cngetc();
205 db_printf("\r");
206 switch (c) {
207 case '\n': /* one more line */
208 lines = 1;
209 break;
210 case ' ': /* one more page */
211 lines = NUM_LINES_PER_PAGE;
212 break;
213 default:
214 db_printf("\n");
215 return;
216 }
217 }
218}
219
220static int
221db_mach_vtrace(void)
222{
223 struct ktr_entry *kp;
224
225 if (tstate.cur == tstate.first) {
226 db_printf("--- End of trace buffer ---\n");
227 return (0);
228 }
229 kp = &ktr_buf[tstate.cur];
230
231 /* Skip over unused entries. */
232#ifdef KTR_EXTEND
233 if (kp->ktr_desc[0] == '\0') {
234#else
235 if (kp->ktr_desc == NULL) {
236#endif
237 db_printf("--- End of trace buffer ---\n");
238 return (0);
239 }
240 db_printf("%d: ", tstate.cur);
241 if (db_ktr_verbose)
242 db_printf("%4ld.%06ld ", (long)kp->ktr_tv.tv_sec,
243 kp->ktr_tv.tv_nsec / 1000);
244#ifdef KTR_EXTEND
245#ifdef SMP
246 db_printf("cpu%d ", kp->ktr_cpu);
247#endif
248 if (db_ktr_verbose)
249 db_printf("%s.%d\t", kp->ktr_filename, kp->ktr_line);
250 db_printf("%s", kp->ktr_desc);
251#else
252 db_printf(kp->ktr_desc, kp->ktr_parm1, kp->ktr_parm2, kp->ktr_parm3,
253 kp->ktr_parm4, kp->ktr_parm5);
254#endif
255 db_printf("\n");
256
257 if (tstate.first == -1)
258 tstate.first = tstate.cur;
259
260 if (--tstate.cur < 0)
261 tstate.cur = KTR_ENTRIES - 1;
262
263 return (1);
264}
265
266#endif /* DDB */
267#endif /* KTR */