2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Generic Kernel trace buffer support.
42 #include <sys/types.h>
43 #include <sys/sysctl.h>
44 #include <sys/cpputil.h>
51 * Conveniently auto-enable KTRs when particular KTRs are specified
52 * in kernel options. This way we can get logging during boot.
53 * However, if KTR_ALL is optioned, just disable everything by default.
54 * The user can enable individual masks via sysctl.
57 #define KTR_AUTO_ENABLE 0
60 #define KTR_AUTO_ENABLE -1
63 #define KTR_BUFSIZE 192
66 #define KTR_VERSION_WITH_FREQ 3
67 #define KTR_VERSION_KTR_CPU 4
72 const char *kf_name; /* human-interpreted subsystem name */
73 int32_t *kf_master_enable; /* the master enable variable */
74 const char * const kf_format; /* data format */
75 int kf_data_size; /* relevance of the data buffer */
79 u_int64_t ktr_timestamp;
80 struct ktr_info *ktr_info;
86 int32_t ktr_data[KTR_BUFSIZE / sizeof(int32_t)];
90 struct ktr_entry *ktr_buf;
95 struct ktr_cpu_core core;
96 char pad[__VM_CACHELINE_ALIGN(sizeof(struct ktr_cpu_core))];
101 struct ktr_entry * ktr_begin_write_entry(struct ktr_info *,
103 int ktr_finish_write_entry(struct ktr_info *, struct ktr_entry *);
104 void cpu_ktr_caller(struct ktr_entry *ktr);
109 * Take advantage of constant integer optimizations by the compiler
110 * to optimize-out disabled code at compile-time. If KTR_ENABLE_<name>
111 * is 0, the compiler avoids generating all related code when KTR is enabled.
116 SYSCTL_DECL(_debug_ktr);
118 #define KTR_INFO_MASTER(master) \
119 SYSCTL_NODE(_debug_ktr, OID_AUTO, master, CTLFLAG_RW, 0, ""); \
120 int ktr_ ## master ## _enable = KTR_AUTO_ENABLE; \
121 SYSCTL_INT(_debug_ktr, OID_AUTO, master ## _enable, CTLFLAG_RW, \
122 &ktr_ ## master ## _enable, 0, \
123 "Bit mask to control " __XSTRING(master) "'s event logging")
125 #define KTR_INFO_MASTER_EXTERN(master) \
126 SYSCTL_DECL(_debug_ktr_ ## master); \
127 extern int ktr_ ## master ## _enable \
130 * This creates a read-only sysctl so the user knows what the mask
131 * definitions are and a number of static const int's which are used
132 * by the compiler to optimize the trace logging at compile-time and
134 * You're supposed to specify any arguments to the format as you would in
135 * a function prototype.
136 * We automagically create a struct for the arguments and a throwaway
137 * function that will be called with the format and the actual arguments
138 * at the KTR_LOG site. This makes sure that the compiler will typecheck
139 * the arguments against the format string. On gcc, this requires -Wformat
140 * (which is included in -Wall) and -O1 at least. Note that using
141 * ktr_info.kf_format will not work, hence we need to define another
142 * throwaway const wariable for the format.
144 #define KTR_INFO(compile, master, name, maskbit, format, ...) \
145 static const int ktr_ ## master ## _ ## name ## _mask = \
147 static const int ktr_ ## master ## _ ##name ## _enable = \
149 static int ktr_ ## master ## _ ## name ## _mask_ro = \
151 SYSCTL_INT(_debug_ktr_ ## master, OID_AUTO, name ## _mask, \
152 CTLFLAG_RD, &ktr_ ## master ## _ ## name ## _mask_ro, \
153 0, "Value of the " __XSTRING(name) " event in " __XSTRING(master) "'s mask"); \
154 __GENSTRUCT(ktr_info_ ## master ## _ ## name ## _args, __VA_ARGS__) \
156 CTASSERT(sizeof(struct ktr_info_ ## master ## _ ## name ## _args) <= KTR_BUFSIZE); \
158 __ktr_info_ ## master ## _ ## name ## _fmtcheck(const char *fmt, \
159 ...) __printf0like(1, 2); \
161 __ktr_info_ ## master ## _ ## name ## _fmtcheck(const char *fmt __unused, \
164 static const char * const __ktr_ ## master ## _ ## name ## _fmt = format; \
165 static struct ktr_info ktr_info_ ## master ## _ ## name = { \
166 .kf_name = #master "_" #name, \
167 .kf_master_enable = &ktr_ ## master ## _enable, \
168 .kf_format = format, \
169 .kf_data_size = sizeof(struct ktr_info_ ## master ## _ ## name ## _args), \
176 * Call ktr_begin_write_entry() that sets up the entry for us; use
177 * a struct copy to give as max flexibility as possible to the compiler.
178 * In higher optimization levels, it will copy the arguments directly from
179 * registers to the destination buffer. Call our dummy function that will
180 * typecheck the arguments against the format string.
182 #define KTR_LOG(name, ...) \
184 __ktr_info_ ## name ## _fmtcheck (__ktr_ ## name ## _fmt, ##__VA_ARGS__); \
185 if (ktr_ ## name ## _enable && \
186 (ktr_ ## name ## _mask & *ktr_info_ ## name .kf_master_enable)) { \
187 struct ktr_entry *entry; \
188 entry = ktr_begin_write_entry(&ktr_info_ ## name, __FILE__, __LINE__); \
191 *(struct ktr_info_ ## name ## _args *)&entry->ktr_data[0] = \
192 (struct ktr_info_ ## name ## _args){ __VA_ARGS__}; \
193 if (ktr_finish_write_entry(&ktr_info_ ## name, entry)) { \
194 kprintf(ktr_info_ ## name .kf_format, ##__VA_ARGS__); \
200 #define KTR_COND_LOG(name, cond, ...) \
202 __ktr_info_ ## name ## _fmtcheck (__ktr_ ## name ## _fmt, ##__VA_ARGS__); \
204 ktr_ ## name ## _enable && \
205 (ktr_ ## name ## _mask & *ktr_info_ ## name .kf_master_enable)) { \
206 struct ktr_entry *entry; \
207 entry = ktr_begin_write_entry(&ktr_info_ ## name, __FILE__, __LINE__); \
210 *(struct ktr_info_ ## name ## _args *)&entry->ktr_data[0] = \
211 (struct ktr_info_ ## name ## _args){ __VA_ARGS__}; \
212 if (ktr_finish_write_entry(&ktr_info_ ## name, entry)) { \
213 kprintf(ktr_info_ ## name .kf_format, ##__VA_ARGS__); \
221 #define KTR_INFO_MASTER(master) \
222 static const int ktr_ ## master ## _enable = 0
224 #define KTR_INFO_MASTER_EXTERN(master) \
225 static const int ktr_ ## master ## _enable
227 #define KTR_INFO(compile, master, name, maskbit, format, ...) \
228 static const int ktr_ ## master ## _ ## name ## _mask = \
231 #define KTR_LOG(info, args...)
233 #define KTR_COND_LOG(info, args...)
238 #endif /* !_SYS_KTR_H_ */