Merge branch 'vendor/AWK'
[dragonfly.git] / sys / sys / ktr.h
1 /*
2  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  */
34 /*
35  * Generic Kernel trace buffer support.  
36  *
37  */
38
39 #ifndef _SYS_KTR_H_
40 #define _SYS_KTR_H_
41
42 #ifndef _SYS_TYPES_H_
43 #include <sys/types.h>
44 #endif
45 #ifndef _SYS_SYSCTL_H_
46 #include <sys/sysctl.h>
47 #endif
48
49 #ifdef _KERNEL
50 #include "opt_ktr.h"
51 #endif
52
53 /*
54  * Conveniently auto-enable KTRs when particular KTRs are specified
55  * in kernel options.  This way we can get logging during boot.
56  * However, if KTR_ALL is optioned, just disable everything by default.
57  * The user can enable individual masks via sysctl.
58  */
59 #if defined(KTR_ALL)
60 #define KTR_AUTO_ENABLE         0
61 #else
62 #define KTR_ALL                 0
63 #define KTR_AUTO_ENABLE         -1
64 #endif
65
66 #define KTR_BUFSIZE             192
67 #define KTR_VERSION             4
68
69 #define KTR_VERSION_WITH_FREQ   3
70 #define KTR_VERSION_KTR_CPU     4
71
72 #ifndef LOCORE
73
74 struct ktr_info {
75         const char *kf_name;    /* human-interpreted subsystem name */
76         int32_t *kf_master_enable; /* the master enable variable */
77         const char *kf_format;  /* data format */
78         int kf_data_size;       /* relevance of the data buffer */
79 };
80
81 struct ktr_entry {
82         u_int64_t ktr_timestamp;
83         struct ktr_info *ktr_info;
84         const   char *ktr_file;
85         void    *ktr_caller1;
86         void    *ktr_caller2;
87         int32_t ktr_line;
88         int32_t ktr_unused;
89         int32_t ktr_data[KTR_BUFSIZE / sizeof(int32_t)];
90 };
91
92 struct ktr_cpu_core {
93         struct ktr_entry *ktr_buf;
94         int              ktr_idx;
95 };
96
97 struct ktr_cpu {
98         struct ktr_cpu_core core;
99         char pad[__VM_CACHELINE_ALIGN(sizeof(struct ktr_cpu_core))];
100 };
101
102 #ifdef _KERNEL
103
104 void    ktr_log(struct ktr_info *info, const char *file, int line, ...);
105 void    cpu_ktr_caller(struct ktr_entry *ktr);
106
107 #endif
108
109 /*
110  * Take advantage of constant integer optimizations by the compiler
111  * to optimize-out disabled code at compile-time.  If KTR_ENABLE_<name>
112  * is 0, the compiler avoids generating all related code when KTR is enabled.
113  */
114
115 #ifdef KTR
116
117 SYSCTL_DECL(_debug_ktr);
118
119 #define KTR_INFO_MASTER(master)                                             \
120             SYSCTL_NODE(_debug_ktr, OID_AUTO, master, CTLFLAG_RW, 0, "");   \
121             int ktr_ ## master ## _enable = KTR_AUTO_ENABLE;                \
122             SYSCTL_INT(_debug_ktr, OID_AUTO, master ## _enable, CTLFLAG_RW, \
123                 &ktr_ ## master ## _enable, 0,                              \
124                 "Bit mask to control " __XSTRING(master) "'s event logging")
125
126 #define KTR_INFO_MASTER_EXTERN(master)                                  \
127             SYSCTL_DECL(_debug_ktr_ ## master);                         \
128             extern int ktr_ ## master ## _enable                        \
129
130 /*
131  * This creates a read-only sysctl so the user knows what the mask
132  * definitions are and a number of static const int's which are used
133  * by the compiler to optimize the trace logging at compile-time and
134  * run-time.
135  */
136 #define KTR_INFO(compile, master, name, maskbit, format, datasize)      \
137             static const int ktr_ ## master ## _ ## name ## _mask =     \
138                 1 << maskbit;                                           \
139             static const int ktr_ ## master ## _ ## name ## _enable =   \
140                 compile;                                                \
141             static int ktr_ ## master ## _ ## name ## _mask_ro =        \
142                 1 << maskbit;                                           \
143             SYSCTL_INT(_debug_ktr_ ## master, OID_AUTO, name ## _mask,  \
144                 CTLFLAG_RD, &ktr_ ## master ## _ ## name ## _mask_ro,   \
145                 0, "Value of the " __XSTRING(name) " event in " __XSTRING(master) "'s mask"); \
146             static struct ktr_info ktr_info_ ## master ## _ ## name = { \
147                 #master "_" #name,                                      \
148                 &ktr_ ## master ## _enable,                             \
149                 format,                                                 \
150                 datasize }
151
152 #define KTR_LOG(name, args...)                                          \
153             if (ktr_ ## name ## _enable &&                              \
154               (ktr_ ## name ## _mask & *ktr_info_ ## name .kf_master_enable)) \
155             ktr_log(&ktr_info_ ## name, __FILE__, __LINE__, ##args)
156
157 #else
158
159 #define KTR_INFO_MASTER(master)                                         \
160             const static int ktr_ ## master ## _enable = 0
161
162 #define KTR_INFO_MASTER_EXTERN(master)                                  \
163             const static int ktr_ ## master ## _enable
164
165 #define KTR_INFO(compile, master, name, maskbit, format, datasize)      \
166             static const int ktr_ ## master ## _ ## name ## _mask =     \
167                 1 << maskbit
168
169 #define KTR_LOG(info, args...)
170
171 #endif
172
173 #endif /* !LOCORE */
174 #endif /* !_SYS_KTR_H_ */