xref: /netbsd/sys/sys/sdt.h (revision 6550d01e)
1 /*	$NetBSD: sdt.h,v 1.1 2010/03/01 21:10:13 darran Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by CoyotePoint Systems, Inc. It was developed under contract to
9  * CoyotePoint by Darran Hunt.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _SDT_H_
35 #define _SDT_H_
36 
37 #if defined(_KERNEL_OPT)
38 #include "opt_dtrace.h"
39 #endif
40 
41 #define SDT_MAX_PROVIDER	1024	/* max number of SDT providers */
42 #define SDT_MAX_ARGS		5	/* max number of probe arguments */
43 #define SDT_MAX_NAME_SIZE	64	/* max size of provider name */
44 
45 typedef struct {
46     int		created;	/* boolean: probe created? */
47     int		enabled;	/* boolean: probe enabled? */
48     int		id;		/* dtrace provided probe id */
49     const char	*provider;	/* provider name */
50     const char	*module;	/* module name */
51     const char	*function;	/* function name */
52     const char	*name;		/* probe name */
53     const char	*argv[SDT_MAX_ARGS];	/* probe argument types */
54     const char	*argx[SDT_MAX_ARGS];	/* probe argument xlate types */
55 } sdt_probe_t;
56 
57 
58 /*
59  * This type definition must match that of dtrace_probe. It is defined this
60  * way to avoid having to rely on CDDL code.
61  */
62 typedef	void (*sdt_probe_func_t)(u_int32_t, uintptr_t arg0, uintptr_t arg1,
63     uintptr_t arg2, uintptr_t arg3, uintptr_t arg4);
64 
65 /*
66  * The hook for the probe function. See kern_sdt.c which defaults this to
67  * it's own stub. The 'sdt' provider will set it to dtrace_probe when it
68  * loads.
69  */
70 extern sdt_probe_func_t	sdt_probe_func;
71 
72 
73 #define SDT_NAME(prov, mod, func, name) \
74     	prov##_##mod##_##func##_##name
75 
76 #ifdef KDTRACE_HOOKS
77 /*
78  * SDT_PROBE_DEFINE(prov, mod, func, name,
79  *		    arg0, argx0, arg1, argx1,
80  *		    arg2, argx2, arg3, argx3, arg4, argx4)
81  *
82  * 	prov	- provider name
83  * 	mod	- module name
84  * 	func	- function name
85  * 	name	- probe name
86  * 	arg0 - arg4, argument types as strings, or NULL.
87  * 	argx0 - argx4, translation types for arg0 - arg4
88  *
89  * 	e.g. SDT_PROBE_DEFINE(l7, l7lb, l7lb_create_node,
90  * 				"void *", NULL,
91  * 				NULL, NULL, NULL, NULL,
92  * 				NULL, NULL, NULL NULL, )
93  *
94  *	This is used in the target module to define probes to be used.
95  *	The translation type should be set to NULL if not used.
96  */
97 #define SDT_PROBE_DEFINE(prov, mod, func, name, \
98 	    arg0, argx0, arg1, argx1, arg2, argx2, \
99 	    arg3, argx3, arg4, argx4) \
100     	sdt_probe_t SDT_NAME(prov, mod, func, name) = { \
101 	    0, 0, 0, #prov, #mod, #func, #name, \
102 	    { arg0, arg1, arg2, arg3, arg4 }, \
103 	    { NULL, NULL, NULL, NULL, NULL } \
104 	}
105 
106 /* Use this in this module to declare probes defined in the kernel. */
107 #define SDT_PROBE_DECLARE(prov, mod, func, name) \
108     	extern sdt_probe_t SDT_NAME(prov, mod, func, name);
109 
110 /* Use this in the target modules to provide instrumentation points */
111 #define SDT_PROBE(prov, mod, func, name, arg0, arg1, arg2, arg3, arg4) \
112     	if (__predict_false(SDT_NAME(prov, mod, func, name).enabled)) { \
113 	    	(*sdt_probe_func)(SDT_NAME(prov, mod, func, name).id, \
114 		    (uintptr_t)(arg0), (uintptr_t)(arg1), (uintptr_t)(arg2), \
115 		    (uintptr_t)(arg3), (uintptr_t)(arg4)); \
116 	}
117 #else
118 #define SDT_PROBE_DEFINE(prov, mod, func, name, \
119 	    arg0, argx0, arg1, argx1, arg2, argx2, \
120 	    arg3, argx3, arg4, argx4)
121 #define SDT_PROBE_DECLARE(prov, mod, func, name)
122 #define SDT_PROBE(prov, mod, func, name, arg0, arg1, arg2, arg3, arg4)
123 #endif
124 
125 void sdt_init(void *);
126 void sdt_exit(void);
127 
128 #endif
129