xref: /minix/sys/sys/dtrace_bsd.h (revision 6c8f7fc3)
1 /*	$NetBSD: dtrace_bsd.h,v 1.5 2012/01/30 23:31:27 matt Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007-2008 John Birrell (jb@freebsd.org)
5  * All rights reserved.
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/sys/dtrace_bsd.h,v 1.3.2.1 2009/08/03 08:13:06 kensmith Exp $
29  *
30  * This file contains BSD shims for Sun's DTrace code.
31  */
32 
33 #ifndef _SYS_DTRACE_BSD_H
34 #define	_SYS_DTRACE_BSD_H
35 
36 #if defined(_KERNEL_OPT)
37 #include "opt_dtrace.h"
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/kmem.h>
44 #include <sys/proc.h>
45 
46 /* Forward definitions: */
47 struct mbuf;
48 struct trapframe;
49 struct lwp;
50 struct vattr;
51 struct vnode;
52 struct ucred;
53 
54 /*
55  * Cyclic clock function type definition used to hook the cyclic
56  * subsystem into the appropriate timer interrupt.
57  */
58 typedef	void (*cyclic_clock_func_t)(struct trapframe *);
59 
60 /*
61  * These external variables are actually machine-dependent, so
62  * they might not actually exist.
63  *
64  * Defining them here avoids a proliferation of header files.
65  */
66 extern cyclic_clock_func_t     lapic_cyclic_clock_func[];
67 
68 /*
69  * The dtrace module handles traps that occur during a DTrace probe.
70  * This type definition is used in the trap handler to provide a
71  * hook for the dtrace module to register it's handler with.
72  */
73 typedef int (*dtrace_trap_func_t)(struct trapframe *, u_int);
74 
75 int	dtrace_trap(struct trapframe *, u_int);
76 
77 extern dtrace_trap_func_t	dtrace_trap_func;
78 
79 /* Used by the machine dependent trap() code. */
80 typedef	int (*dtrace_invop_func_t)(uintptr_t, uintptr_t *, uintptr_t);
81 typedef void (*dtrace_doubletrap_func_t)(void);
82 
83 /* Global variables in trap.c */
84 extern	dtrace_invop_func_t	dtrace_invop_func;
85 extern	dtrace_doubletrap_func_t	dtrace_doubletrap_func;
86 
87 /* Virtual time hook function type. */
88 typedef	void (*dtrace_vtime_switch_func_t)(struct lwp *);
89 
90 extern int			dtrace_vtime_active;
91 extern dtrace_vtime_switch_func_t	dtrace_vtime_switch_func;
92 
93 /* The fasttrap module hooks into the fork, exit and exit. */
94 typedef void (*dtrace_fork_func_t)(struct proc *, struct proc *);
95 typedef void (*dtrace_execexit_func_t)(struct proc *);
96 
97 /* Global variable in kern_fork.c */
98 extern dtrace_fork_func_t	dtrace_fasttrap_fork;
99 
100 /* Global variable in kern_exec.c */
101 extern dtrace_execexit_func_t	dtrace_fasttrap_exec;
102 
103 /* Global variable in kern_exit.c */
104 extern dtrace_execexit_func_t	dtrace_fasttrap_exit;
105 
106 /* The dtmalloc provider hooks into malloc. */
107 typedef	void (*dtrace_malloc_probe_func_t)(u_int32_t, uintptr_t arg0,
108     uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4);
109 
110 extern dtrace_malloc_probe_func_t   dtrace_malloc_probe;
111 
112 /* dtnfsclient NFSv3 access cache provider hooks. */
113 typedef void (*dtrace_nfsclient_accesscache_flush_probe_func_t)(uint32_t,
114     struct vnode *);
115 extern dtrace_nfsclient_accesscache_flush_probe_func_t
116     dtrace_nfsclient_accesscache_flush_done_probe;
117 
118 typedef void (*dtrace_nfsclient_accesscache_get_probe_func_t)(uint32_t,
119     struct vnode *, uid_t, uint32_t);
120 extern dtrace_nfsclient_accesscache_get_probe_func_t
121     dtrace_nfsclient_accesscache_get_hit_probe,
122     dtrace_nfsclient_accesscache_get_miss_probe;
123 
124 typedef void (*dtrace_nfsclient_accesscache_load_probe_func_t)(uint32_t,
125     struct vnode *, uid_t, uint32_t, int);
126 extern dtrace_nfsclient_accesscache_load_probe_func_t
127     dtrace_nfsclient_accesscache_load_done_probe;
128 
129 /* dtnfsclient NFSv[23] attribute cache provider hooks. */
130 typedef void (*dtrace_nfsclient_attrcache_flush_probe_func_t)(uint32_t,
131     struct vnode *);
132 extern dtrace_nfsclient_attrcache_flush_probe_func_t
133     dtrace_nfsclient_attrcache_flush_done_probe;
134 
135 typedef void (*dtrace_nfsclient_attrcache_get_hit_probe_func_t)(uint32_t,
136     struct vnode *, struct vattr *);
137 extern dtrace_nfsclient_attrcache_get_hit_probe_func_t
138     dtrace_nfsclient_attrcache_get_hit_probe;
139 
140 typedef void (*dtrace_nfsclient_attrcache_get_miss_probe_func_t)(uint32_t,
141     struct vnode *);
142 extern dtrace_nfsclient_attrcache_get_miss_probe_func_t
143     dtrace_nfsclient_attrcache_get_miss_probe;
144 
145 typedef void (*dtrace_nfsclient_attrcache_load_probe_func_t)(uint32_t,
146     struct vnode *, struct vattr *, int);
147 extern dtrace_nfsclient_attrcache_load_probe_func_t
148     dtrace_nfsclient_attrcache_load_done_probe;
149 
150 /* dtnfsclient NFSv[23] RPC provider hooks. */
151 typedef void (*dtrace_nfsclient_nfs23_start_probe_func_t)(uint32_t,
152     struct vnode *, struct mbuf *, struct ucred *, int);
153 extern dtrace_nfsclient_nfs23_start_probe_func_t
154     dtrace_nfsclient_nfs23_start_probe;
155 
156 typedef void (*dtrace_nfsclient_nfs23_done_probe_func_t)(uint32_t,
157     struct vnode *, struct mbuf *, struct ucred *, int, int);
158 extern dtrace_nfsclient_nfs23_done_probe_func_t
159     dtrace_nfsclient_nfs23_done_probe;
160 
161 /*
162  * OpenSolaris compatible time functions returning nanoseconds.
163  * On OpenSolaris these return hrtime_t which we define as uint64_t.
164  */
165 uint64_t	dtrace_gethrtime(void);
166 uint64_t	dtrace_gethrestime(void);
167 
168 /* sizes based on DTrace structure requirements */
169 #define KDTRACE_PROC_SIZE	64
170 #define KDTRACE_PROC_ZERO	8
171 #define	KDTRACE_THREAD_SIZE	256
172 #define	KDTRACE_THREAD_ZERO	64
173 
174 /*
175  * Functions for managing the opaque DTrace memory areas for
176  * processes and lwps.
177  */
178 
179 static inline size_t	kdtrace_proc_size(void);
180 static inline void kdtrace_proc_ctor(void *, struct proc *);
181 static inline void kdtrace_proc_dtor(void *, struct proc *);
182 static inline size_t	kdtrace_thread_size(void);
183 static inline void kdtrace_thread_ctor(void *, struct lwp *);
184 static inline void kdtrace_thread_dtor(void *, struct lwp *);
185 
186 
187 /* Return the DTrace process data size compiled in the kernel hooks. */
188 static inline size_t
189 kdtrace_proc_size(void)
190 {
191 
192 	return KDTRACE_PROC_SIZE;
193 }
194 
195 /* Return the DTrace thread data size compiled in the kernel hooks. */
196 static inline size_t
197 kdtrace_thread_size(void)
198 {
199 
200 	return KDTRACE_THREAD_SIZE;
201 }
202 
203 static inline void
204 kdtrace_proc_ctor(void *arg, struct proc *p)
205 {
206 
207 #ifdef KDTRACE_HOOKS
208 	p->p_dtrace = kmem_zalloc(KDTRACE_PROC_SIZE, KM_SLEEP);
209 #endif
210 }
211 
212 static inline void
213 kdtrace_proc_dtor(void *arg, struct proc *p)
214 {
215 
216 #ifdef KDTRACE_HOOKS
217 	if (p->p_dtrace != NULL) {
218 		kmem_free(p->p_dtrace, KDTRACE_PROC_SIZE);
219 		p->p_dtrace = NULL;
220 	}
221 #endif
222 }
223 
224 static inline void
225 kdtrace_thread_ctor(void *arg, struct lwp *l)
226 {
227 
228 #ifdef KDTRACE_HOOKS
229 	l->l_dtrace = kmem_zalloc(KDTRACE_THREAD_SIZE, KM_SLEEP);
230 #endif
231 }
232 
233 static inline void
234 kdtrace_thread_dtor(void *arg, struct lwp *l)
235 {
236 
237 #ifdef KDTRACE_HOOKS
238 	if (l->l_dtrace != NULL) {
239 		kmem_free(l->l_dtrace, KDTRACE_THREAD_SIZE);
240 		l->l_dtrace = NULL;
241 	}
242 #endif
243 }
244 
245 #endif /* _SYS_DTRACE_BSD_H */
246