Add KTR support to the slab allocator. Track malloc's, free's, oversized
[dragonfly.git] / sys / sys / ktr.h
CommitLineData
d3776285
MD
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 *
81540c2d
EN
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
d3776285 10 *
81540c2d
EN
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
d3776285
MD
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
81540c2d 32 * SUCH DAMAGE.
d3776285 33 *
af6ff89e 34 * $DragonFly: src/sys/sys/ktr.h,v 1.6 2005/06/20 20:37:26 dillon Exp $
81540c2d 35 */
81540c2d 36/*
d3776285
MD
37 * Generic Kernel trace buffer support.
38 *
81540c2d
EN
39 */
40
41#ifndef _SYS_KTR_H_
42#define _SYS_KTR_H_
43
282a0149 44#ifdef _KERNEL
81540c2d 45#include "opt_ktr.h"
282a0149 46#endif
81540c2d 47
d3776285
MD
48#if !defined(KTR_ALL)
49#define KTR_ALL 0
81540c2d
EN
50#endif
51
d3776285
MD
52#define KTR_BUFSIZE 48
53#define KTR_VERSION 2
81540c2d
EN
54
55#ifndef LOCORE
56
d3776285
MD
57struct ktr_info {
58 const char *kf_name; /* human-interpreted subsystem name */
59 int32_t *kf_master_enable; /* the master enable variable */
60 const char *kf_format; /* data format */
61 int kf_data_size; /* relevance of the data buffer */
62};
63
81540c2d
EN
64struct ktr_entry {
65 u_int64_t ktr_timestamp;
d3776285 66 struct ktr_info *ktr_info;
81540c2d 67 const char *ktr_file;
d3776285
MD
68 void *ktr_caller1;
69 void *ktr_caller2;
70 int32_t ktr_line;
71 int32_t ktr_unused;
af6ff89e 72 int32_t ktr_data[KTR_BUFSIZE / sizeof(int32_t)];
81540c2d
EN
73};
74
d3776285
MD
75void ktr_log(struct ktr_info *info, const char *file, int line, ...);
76void ktr_log_ptr(struct ktr_info *info, const char *file, int line, const void *ptr);
af6ff89e 77void cpu_ktr_caller(struct ktr_entry *ktr);
81540c2d 78
d3776285
MD
79/*
80 * Take advantage of constant integer optimizations by the compiler
81 * to optimize-out disabled code at compile-time. If KTR_ENABLE_<name>
82 * is 0, the compiler avoids generating all related code when KTR is enabled.
83 */
81540c2d
EN
84
85#ifdef KTR
86
d3776285
MD
87SYSCTL_DECL(_debug_ktr);
88
89#define KTR_INFO_MASTER(master) \
90 SYSCTL_NODE(_debug_ktr, OID_AUTO, master, CTLFLAG_RW, 0, ""); \
91 int ktr_ ## master ## _enable = -1; \
92 SYSCTL_INT(_debug_ktr, OID_AUTO, master ## _enable, CTLFLAG_RW, \
93 &ktr_ ## master ## _enable, 0, "")
94
95#define KTR_INFO_MASTER_EXTERN(master) \
96 SYSCTL_DECL(_debug_ktr_ ## master); \
97 extern int ktr_ ## master ## _enable \
81540c2d
EN
98
99/*
d3776285
MD
100 * This creates a read-only sysctl so the user knows what the mask
101 * definitions are and a number of static const int's which are used
102 * by the compiler to optimize the trace logging at compile-time and
103 * run-time.
81540c2d 104 */
d3776285
MD
105#define KTR_INFO(compile, master, name, maskbit, format, datasize) \
106 static const int ktr_ ## master ## _ ## name ## _mask = \
107 1 << maskbit; \
108 static const int ktr_ ## master ## _ ## name ## _enable = \
109 compile; \
110 static int ktr_ ## master ## _ ## name ## _mask_ro = \
111 1 << maskbit; \
112 SYSCTL_INT(_debug_ktr_ ## master, OID_AUTO, name ## _mask, \
113 CTLFLAG_RD, &ktr_ ## master ## _ ## name ## _mask_ro, \
114 0, ""); \
115 static struct ktr_info ktr_info_ ## master ## _ ## name = { \
116 #master "_" #name, \
117 &ktr_ ## master ## _enable, \
118 format, \
119 datasize }
120
121#define KTR_LOG(name, args...) \
122 if (ktr_ ## name ## _enable && \
123 (ktr_ ## name ## _mask & *ktr_info_ ## name .kf_master_enable)) \
124 ktr_log(&ktr_info_ ## name, __FILE__, __LINE__, ##args)
125
126#define KTR_LOG_PTR(name, ptr) \
127 if (ktr_ ## name ## _enable && \
128 (ktr_ ## name ## _mask & *ktr_info_ ## name .kf_master_enable)) \
129 ktr_log_ptr(&ktr_info_ ## name, __FILE__, __LINE__, ptr)
130
81540c2d 131#else
d3776285
MD
132
133#define KTR_INFO_MASTER(master) \
134 const static int ktr_ ## master ## _enable = 0
135
136#define KTR_INFO_MASTER_EXTERN(master) \
137 const static int ktr_ ## master ## _enable = 0
138
139#define KTR_INFO(master, name, maskbit, format, datasize) \
140 static const int ktr_ ## master ## _ ## name ## _mask = \
141 1 << maskbit
142
143#define KTR_LOG(info, args...)
144#define KTR_LOG_PTR(info, ptr)
145
81540c2d
EN
146#endif
147
d3776285 148#endif /* !LOCORE */
81540c2d 149#endif /* !_SYS_KTR_H_ */