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