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