xref: /dragonfly/sys/sys/ktr.h (revision 0ac6bf9d)
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.10 2006/09/12 22:03:11 dillon 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 #if !defined(KTR_ALL)
56 #define KTR_ALL		0
57 #endif
58 
59 #define	KTR_BUFSIZE	48
60 #define KTR_VERSION	3
61 
62 #ifndef LOCORE
63 
64 struct ktr_info {
65 	const char *kf_name;	/* human-interpreted subsystem name */
66 	int32_t *kf_master_enable; /* the master enable variable */
67 	const char *kf_format;	/* data format */
68 	int kf_data_size;	/* relevance of the data buffer */
69 };
70 
71 struct ktr_entry {
72 	u_int64_t ktr_timestamp;
73 	struct ktr_info *ktr_info;
74 	const	char *ktr_file;
75 	void	*ktr_caller1;
76 	void	*ktr_caller2;
77 	int32_t	ktr_line;
78 	int32_t ktr_unused;
79 	int32_t	ktr_data[KTR_BUFSIZE / sizeof(int32_t)];
80 };
81 
82 void	ktr_log(struct ktr_info *info, const char *file, int line, ...);
83 void	ktr_log_ptr(struct ktr_info *info, const char *file, int line, const void *ptr);
84 void    cpu_ktr_caller(struct ktr_entry *ktr);
85 
86 /*
87  * Take advantage of constant integer optimizations by the compiler
88  * to optimize-out disabled code at compile-time.  If KTR_ENABLE_<name>
89  * is 0, the compiler avoids generating all related code when KTR is enabled.
90  */
91 
92 #ifdef KTR
93 
94 SYSCTL_DECL(_debug_ktr);
95 
96 #define KTR_INFO_MASTER(master)						    \
97 	    SYSCTL_NODE(_debug_ktr, OID_AUTO, master, CTLFLAG_RW, 0, "");   \
98 	    int ktr_ ## master ## _enable = -1;				    \
99 	    SYSCTL_INT(_debug_ktr, OID_AUTO, master ## _enable, CTLFLAG_RW, \
100 			&ktr_ ## master ## _enable, 0, "")
101 
102 #define KTR_INFO_MASTER_EXTERN(master)					\
103 	    SYSCTL_DECL(_debug_ktr_ ## master);				\
104 	    extern int ktr_ ## master ## _enable			\
105 
106 /*
107  * This creates a read-only sysctl so the user knows what the mask
108  * definitions are and a number of static const int's which are used
109  * by the compiler to optimize the trace logging at compile-time and
110  * run-time.
111  */
112 #define KTR_INFO(compile, master, name, maskbit, format, datasize)	\
113 	    static const int ktr_ ## master ## _ ## name ## _mask =	\
114 		1 << maskbit;						\
115 	    static const int ktr_ ## master ## _ ## name ## _enable =	\
116 		compile;						\
117 	    static int ktr_ ## master ## _ ## name ## _mask_ro =	\
118 		1 << maskbit;						\
119 	    SYSCTL_INT(_debug_ktr_ ## master, OID_AUTO, name ## _mask,	\
120 		CTLFLAG_RD, &ktr_ ## master ## _ ## name ## _mask_ro,	\
121 		0, "");							\
122 	    static struct ktr_info ktr_info_ ## master ## _ ## name = { \
123 		#master "_" #name,					\
124 		&ktr_ ## master ## _enable,				\
125 		format,							\
126 		datasize }
127 
128 #define KTR_LOG(name, args...)						\
129 	    if (ktr_ ## name ## _enable &&				\
130 	      (ktr_ ## name ## _mask & *ktr_info_ ## name .kf_master_enable)) \
131 	    ktr_log(&ktr_info_ ## name, __FILE__, __LINE__, ##args)
132 
133 #define KTR_LOG_PTR(name, ptr)						\
134 	    if (ktr_ ## name ## _enable &&				\
135 	      (ktr_ ## name ## _mask & *ktr_info_ ## name .kf_master_enable)) \
136 	    ktr_log_ptr(&ktr_info_ ## name, __FILE__, __LINE__, ptr)
137 
138 #else
139 
140 #define KTR_INFO_MASTER(master)						\
141 	    const static int ktr_ ## master ## _enable = 0
142 
143 #define KTR_INFO_MASTER_EXTERN(master)					\
144 	    const static int ktr_ ## master ## _enable = 0
145 
146 #define KTR_INFO(compile, master, name, maskbit, format, datasize)	\
147 	    static const int ktr_ ## master ## _ ## name ## _mask =	\
148 		1 << maskbit
149 
150 #define KTR_LOG(info, args...)
151 #define KTR_LOG_PTR(info, ptr)
152 
153 #endif
154 
155 #endif /* !LOCORE */
156 #endif /* !_SYS_KTR_H_ */
157