]> sourceware.org Git - systemtap.git/blob - includes/sys/sdt.h
Add -DSTAP_SDT_ARG_CONSTRAINT
[systemtap.git] / includes / sys / sdt.h
1 /* <sys/sdt.h> - Systemtap static probe definition macros.
2 Copyright (C) 2010-2011 Red Hat Inc.
3
4 This file is part of systemtap, and is free software in the public domain.
5 */
6
7 #ifndef _SYS_SDT_H
8 #define _SYS_SDT_H 1
9
10 #ifdef __ASSEMBLER__
11 # define _SDT_PROBE(provider, name, n, arglist) \
12 _SDT_ASM_BODY(provider, name, _SDT_ASM_STRING_1, (_SDT_DEPAREN_##n arglist)) \
13 _SDT_ASM_BASE
14 # define _SDT_ASM_1(x) x;
15 # define _SDT_ASM_2(a, b) a,b;
16 # define _SDT_ASM_3(a, b, c) a,b,c;
17 # define _SDT_ASM_5(a, b, c, d, e) a,b,c,d,e;
18 # define _SDT_ASM_STRING_1(x) .asciz #x;
19 # define _SDT_DEPAREN_0() /* empty */
20 # define _SDT_DEPAREN_1(a) a
21 # define _SDT_DEPAREN_2(a,b) a b
22 # define _SDT_DEPAREN_3(a,b,c) a b c
23 # define _SDT_DEPAREN_4(a,b,c,d) a b c d
24 # define _SDT_DEPAREN_5(a,b,c,d,e) a b c d e
25 # define _SDT_DEPAREN_6(a,b,c,d,e,f) a b c d e f
26 # define _SDT_DEPAREN_7(a,b,c,d,e,f,g) a b c d e f g
27 # define _SDT_DEPAREN_8(a,b,c,d,e,f,g,h) a b c d e f g h
28 # define _SDT_DEPAREN_9(a,b,c,d,e,f,g,h,i) a b c d e f g h i
29 # define _SDT_DEPAREN_10(a,b,c,d,e,f,g,h,i,j) a b c d e f g h i j
30 #else
31 # include <stdint.h>
32 # define _SDT_PROBE(provider, name, n, arglist) \
33 do { \
34 __asm__ __volatile__ (_SDT_ASM_BODY(provider, name, _SDT_ASM_ARGS, (n)) \
35 :: _SDT_ASM_OPERANDS_##n arglist); \
36 __asm__ __volatile__ (_SDT_ASM_BASE); \
37 } while (0)
38 # define _SDT_S(x) #x
39 # define _SDT_ASM_1(x) _SDT_S(x)"\n"
40 # define _SDT_ASM_2(a, b) _SDT_S(a)","_SDT_S(b)"\n"
41 # define _SDT_ASM_3(a, b, c) _SDT_S(a)","_SDT_S(b)","_SDT_S(c)"\n"
42 # define _SDT_ASM_5(a, b, c, d, e) _SDT_S(a)","_SDT_S(b)","_SDT_S(c)","\
43 _SDT_S(d)","_SDT_S(e)"\n"
44 # define _SDT_ASM_ARGS(n) _SDT_ASM_STRING(_SDT_ASM_TEMPLATE_##n)
45 # define _SDT_ASM_STRING_1(x) _SDT_ASM_1(.asciz #x)
46
47 # define _SDT_ARGFMT(no) %n[_SDT_S##no]@_SDT_ARGTMPL(_SDT_A##no)
48 # ifndef STAP_SDT_ARG_CONSTRAINT
49 # define STAP_SDT_ARG_CONSTRAINT nor
50 # endif
51 # define _SDT_STRINGIFY(x) #x
52 # define _SDT_ARG_CONSTRAINT_STRING(x) _SDT_STRINGIFY(x)
53 # define _SDT_ARG(n, x) \
54 [_SDT_S##n] "n" ((_SDT_ARGSIGNED (x) ? 1 : -1) * (int) _SDT_ARGSIZE (x)), \
55 [_SDT_A##n] _SDT_ARG_CONSTRAINT_STRING (STAP_SDT_ARG_CONSTRAINT) (_SDT_ARGVAL (x))
56 #endif
57 #define _SDT_ASM_STRING(x) _SDT_ASM_STRING_1(x)
58
59 #define _SDT_ARGARRAY(x) (__builtin_classify_type (x) == 14 \
60 || __builtin_classify_type (x) == 5)
61
62 #ifdef __cplusplus
63 # define _SDT_ARGSIGNED(x) (!_SDT_ARGARRAY (x) \
64 && __sdt_type<__typeof (x)>::__sdt_signed)
65 # define _SDT_ARGSIZE(x) (_SDT_ARGARRAY (x) \
66 ? sizeof (void *) : sizeof (x))
67 # define _SDT_ARGVAL(x) (x)
68
69 # include <cstddef>
70
71 template<typename __sdt_T>
72 struct __sdt_type
73 {
74 static const bool __sdt_signed = false;
75 };
76
77 #define __SDT_ALWAYS_SIGNED(T) \
78 template<> struct __sdt_type<T> { static const bool __sdt_signed = true; };
79 #define __SDT_COND_SIGNED(T) \
80 template<> struct __sdt_type<T> { static const bool __sdt_signed = ((T)(-1) < 0); };
81 __SDT_ALWAYS_SIGNED(signed char)
82 __SDT_ALWAYS_SIGNED(short)
83 __SDT_ALWAYS_SIGNED(int)
84 __SDT_ALWAYS_SIGNED(long)
85 __SDT_ALWAYS_SIGNED(long long)
86 __SDT_ALWAYS_SIGNED(volatile signed char)
87 __SDT_ALWAYS_SIGNED(volatile short)
88 __SDT_ALWAYS_SIGNED(volatile int)
89 __SDT_ALWAYS_SIGNED(volatile long)
90 __SDT_ALWAYS_SIGNED(volatile long long)
91 __SDT_ALWAYS_SIGNED(const signed char)
92 __SDT_ALWAYS_SIGNED(const short)
93 __SDT_ALWAYS_SIGNED(const int)
94 __SDT_ALWAYS_SIGNED(const long)
95 __SDT_ALWAYS_SIGNED(const long long)
96 __SDT_ALWAYS_SIGNED(const volatile signed char)
97 __SDT_ALWAYS_SIGNED(const volatile short)
98 __SDT_ALWAYS_SIGNED(const volatile int)
99 __SDT_ALWAYS_SIGNED(const volatile long)
100 __SDT_ALWAYS_SIGNED(const volatile long long)
101 __SDT_COND_SIGNED(char)
102 __SDT_COND_SIGNED(wchar_t)
103 __SDT_COND_SIGNED(volatile char)
104 __SDT_COND_SIGNED(volatile wchar_t)
105 __SDT_COND_SIGNED(const char)
106 __SDT_COND_SIGNED(const wchar_t)
107 __SDT_COND_SIGNED(const volatile char)
108 __SDT_COND_SIGNED(const volatile wchar_t)
109 #if defined (__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
110 /* __SDT_COND_SIGNED(char16_t) */
111 /* __SDT_COND_SIGNED(char32_t) */
112 #endif
113
114 template<typename __sdt_E>
115 struct __sdt_type<__sdt_E[]> : public __sdt_type<__sdt_E *> {};
116
117 template<typename __sdt_E, size_t __sdt_N>
118 struct __sdt_type<__sdt_E[__sdt_N]> : public __sdt_type<__sdt_E *> {};
119
120 #elif !defined(__ASSEMBLER__)
121 __extension__ extern unsigned long long __sdt_unsp;
122 # define _SDT_ARGINTTYPE(x) \
123 __typeof (__builtin_choose_expr (((__builtin_classify_type (x) \
124 + 3) & -4) == 4, (x), 0U))
125 # define _SDT_ARGSIGNED(x) \
126 (!__extension__ \
127 (__builtin_constant_p ((((unsigned long long) \
128 (_SDT_ARGINTTYPE (x)) __sdt_unsp) \
129 & (1ULL << (sizeof (unsigned long long) \
130 * __CHAR_BIT__ - 1))) == 0) \
131 || (_SDT_ARGINTTYPE (x)) -1 > (_SDT_ARGINTTYPE (x)) 0))
132 # define _SDT_ARGSIZE(x) \
133 (_SDT_ARGARRAY (x) ? sizeof (void *) : sizeof (x))
134 # define _SDT_ARGVAL(x) (x)
135 #endif
136
137 #if defined __powerpc__ || defined __powerpc64__
138 # define _SDT_ARGTMPL(id) %I[id]%[id]
139 #else
140 # define _SDT_ARGTMPL(id) %[id]
141 #endif
142
143 #ifdef __LP64__
144 # define _SDT_ASM_ADDR .8byte
145 #else
146 # define _SDT_ASM_ADDR .4byte
147 #endif
148
149 /* The ia64 and s390 nop instructions take an argument. */
150 #if defined(__ia64__) || defined(__s390__) || defined(__s390x__)
151 #define _SDT_NOP nop 0
152 #else
153 #define _SDT_NOP nop
154 #endif
155
156 #define _SDT_NOTE_NAME "stapsdt"
157 #define _SDT_NOTE_TYPE 3
158
159 /* If the assembler supports the necessary feature, then we can play
160 nice with code in COMDAT sections, which comes up in C++ code.
161 Without that assembler support, some combinations of probe placements
162 in certain kinds of C++ code may produce link-time errors. */
163 #include "sdt-config.h"
164 #if _SDT_ASM_SECTION_AUTOGROUP_SUPPORT
165 # define _SDT_ASM_AUTOGROUP "?"
166 #else
167 # define _SDT_ASM_AUTOGROUP ""
168 #endif
169
170 #define _SDT_ASM_BODY(provider, name, pack_args, args) \
171 _SDT_ASM_1(990: _SDT_NOP) \
172 _SDT_ASM_3( .pushsection .note.stapsdt,_SDT_ASM_AUTOGROUP,"note") \
173 _SDT_ASM_1( .balign 4) \
174 _SDT_ASM_3( .4byte 992f-991f, 994f-993f, _SDT_NOTE_TYPE) \
175 _SDT_ASM_1(991: .asciz _SDT_NOTE_NAME) \
176 _SDT_ASM_1(992: .balign 4) \
177 _SDT_ASM_1(993: _SDT_ASM_ADDR 990b) \
178 _SDT_ASM_1( _SDT_ASM_ADDR _.stapsdt.base) \
179 _SDT_SEMAPHORE(provider,name) \
180 _SDT_ASM_STRING(provider) \
181 _SDT_ASM_STRING(name) \
182 pack_args args \
183 _SDT_ASM_1(994: .balign 4) \
184 _SDT_ASM_1( .popsection)
185
186 #define _SDT_ASM_BASE \
187 _SDT_ASM_1(.ifndef _.stapsdt.base) \
188 _SDT_ASM_5( .pushsection .stapsdt.base,"aG","progbits", \
189 .stapsdt.base,comdat) \
190 _SDT_ASM_1( .weak _.stapsdt.base) \
191 _SDT_ASM_1( .hidden _.stapsdt.base) \
192 _SDT_ASM_1( _.stapsdt.base: .space 1) \
193 _SDT_ASM_2( .size _.stapsdt.base, 1) \
194 _SDT_ASM_1( .popsection) \
195 _SDT_ASM_1(.endif)
196
197 #if defined _SDT_HAS_SEMAPHORES
198 #define _SDT_SEMAPHORE(p,n) _SDT_ASM_1( _SDT_ASM_ADDR p##_##n##_semaphore)
199 #else
200 #define _SDT_SEMAPHORE(p,n) _SDT_ASM_1( _SDT_ASM_ADDR 0)
201 #endif
202
203 #define _SDT_ASM_TEMPLATE_0 /* no arguments */
204 #define _SDT_ASM_TEMPLATE_1 _SDT_ARGFMT(1)
205 #define _SDT_ASM_TEMPLATE_2 _SDT_ASM_TEMPLATE_1 _SDT_ARGFMT(2)
206 #define _SDT_ASM_TEMPLATE_3 _SDT_ASM_TEMPLATE_2 _SDT_ARGFMT(3)
207 #define _SDT_ASM_TEMPLATE_4 _SDT_ASM_TEMPLATE_3 _SDT_ARGFMT(4)
208 #define _SDT_ASM_TEMPLATE_5 _SDT_ASM_TEMPLATE_4 _SDT_ARGFMT(5)
209 #define _SDT_ASM_TEMPLATE_6 _SDT_ASM_TEMPLATE_5 _SDT_ARGFMT(6)
210 #define _SDT_ASM_TEMPLATE_7 _SDT_ASM_TEMPLATE_6 _SDT_ARGFMT(7)
211 #define _SDT_ASM_TEMPLATE_8 _SDT_ASM_TEMPLATE_7 _SDT_ARGFMT(8)
212 #define _SDT_ASM_TEMPLATE_9 _SDT_ASM_TEMPLATE_8 _SDT_ARGFMT(9)
213 #define _SDT_ASM_TEMPLATE_10 _SDT_ASM_TEMPLATE_9 _SDT_ARGFMT(10)
214 #define _SDT_ASM_OPERANDS_0() [__sdt_dummy] "g" (0)
215 #define _SDT_ASM_OPERANDS_1(arg1) _SDT_ARG(1, arg1)
216 #define _SDT_ASM_OPERANDS_2(arg1, arg2) \
217 _SDT_ASM_OPERANDS_1(arg1), _SDT_ARG(2, arg2)
218 #define _SDT_ASM_OPERANDS_3(arg1, arg2, arg3) \
219 _SDT_ASM_OPERANDS_2(arg1, arg2), _SDT_ARG(3, arg3)
220 #define _SDT_ASM_OPERANDS_4(arg1, arg2, arg3, arg4) \
221 _SDT_ASM_OPERANDS_3(arg1, arg2, arg3), _SDT_ARG(4, arg4)
222 #define _SDT_ASM_OPERANDS_5(arg1, arg2, arg3, arg4, arg5) \
223 _SDT_ASM_OPERANDS_4(arg1, arg2, arg3, arg4), _SDT_ARG(5, arg5)
224 #define _SDT_ASM_OPERANDS_6(arg1, arg2, arg3, arg4, arg5, arg6) \
225 _SDT_ASM_OPERANDS_5(arg1, arg2, arg3, arg4, arg5), _SDT_ARG(6, arg6)
226 #define _SDT_ASM_OPERANDS_7(arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
227 _SDT_ASM_OPERANDS_6(arg1, arg2, arg3, arg4, arg5, arg6), _SDT_ARG(7, arg7)
228 #define _SDT_ASM_OPERANDS_8(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
229 _SDT_ASM_OPERANDS_7(arg1, arg2, arg3, arg4, arg5, arg6, arg7), \
230 _SDT_ARG(8, arg8)
231 #define _SDT_ASM_OPERANDS_9(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9) \
232 _SDT_ASM_OPERANDS_8(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8), \
233 _SDT_ARG(9, arg9)
234 #define _SDT_ASM_OPERANDS_10(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10) \
235 _SDT_ASM_OPERANDS_9(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9), \
236 _SDT_ARG(10, arg10)
237
238 /* These macros can be used in C, C++, or assembly code.
239 In assembly code the arguments should use normal assembly operand syntax. */
240
241 #define STAP_PROBE(provider, name) \
242 _SDT_PROBE(provider, name, 0, ())
243 #define STAP_PROBE1(provider, name, arg1) \
244 _SDT_PROBE(provider, name, 1, (arg1))
245 #define STAP_PROBE2(provider, name, arg1, arg2) \
246 _SDT_PROBE(provider, name, 2, (arg1, arg2))
247 #define STAP_PROBE3(provider, name, arg1, arg2, arg3) \
248 _SDT_PROBE(provider, name, 3, (arg1, arg2, arg3))
249 #define STAP_PROBE4(provider, name, arg1, arg2, arg3, arg4) \
250 _SDT_PROBE(provider, name, 4, (arg1, arg2, arg3, arg4))
251 #define STAP_PROBE5(provider, name, arg1, arg2, arg3, arg4, arg5) \
252 _SDT_PROBE(provider, name, 5, (arg1, arg2, arg3, arg4, arg5))
253 #define STAP_PROBE6(provider, name, arg1, arg2, arg3, arg4, arg5, arg6) \
254 _SDT_PROBE(provider, name, 6, (arg1, arg2, arg3, arg4, arg5, arg6))
255 #define STAP_PROBE7(provider, name, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
256 _SDT_PROBE(provider, name, 7, (arg1, arg2, arg3, arg4, arg5, arg6, arg7))
257 #define STAP_PROBE8(provider,name,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8) \
258 _SDT_PROBE(provider, name, 8, (arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8))
259 #define STAP_PROBE9(provider,name,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
260 _SDT_PROBE(provider, name, 9, (arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9))
261 #define STAP_PROBE10(provider,name,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10) \
262 _SDT_PROBE(provider, name, 10, \
263 (arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10))
264
265 /* This STAP_PROBEV macro can be used in variadic scenarios, where the
266 number of probe arguments is not known until compile time. Since
267 variadic macro support may vary with compiler options, you must
268 pre-#define SDT_USE_VARIADIC to enable this type of probe.
269
270 The trick to count __VA_ARGS__ was inspired by this post by
271 Laurent Deniau <laurent.deniau@cern.ch>:
272 http://groups.google.com/group/comp.std.c/msg/346fc464319b1ee5
273
274 Note that our _SDT_NARG is called with an extra 0 arg that's not
275 counted, so we don't have to worry about the behavior of macros
276 called without any arguments. */
277
278 #ifdef SDT_USE_VARIADIC
279 #define _SDT_NARG(...) __SDT_NARG(__VA_ARGS__, 10,9,8,7,6,5,4,3,2,1,0)
280 #define __SDT_NARG(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10, N, ...) N
281 #define _SDT_PROBE_N(provider, name, N, ...) \
282 _SDT_PROBE(provider, name, N, (__VA_ARGS__))
283 #define STAP_PROBEV(provider, name, ...) \
284 _SDT_PROBE_N(provider, name, _SDT_NARG(0, ##__VA_ARGS__), ##__VA_ARGS__)
285 #endif
286
287 /* These macros are for use in asm statements. You must compile
288 with -std=gnu99 or -std=c99 to use the STAP_PROBE_ASM macro.
289
290 The STAP_PROBE_ASM macro generates a quoted string to be used in the
291 template portion of the asm statement, concatenated with strings that
292 contain the actual assembly code around the probe site.
293
294 For example:
295
296 asm ("before\n"
297 STAP_PROBE_ASM(provider, fooprobe, %eax 4(%esi))
298 "after");
299
300 emits the assembly code for "before\nafter", with a probe in between.
301 The probe arguments are the %eax register, and the value of the memory
302 word located 4 bytes past the address in the %esi register. Note that
303 because this is a simple asm, not a GNU C extended asm statement, these
304 % characters do not need to be doubled to generate literal %reg names.
305
306 In a GNU C extended asm statement, the probe arguments can be specified
307 using the macro STAP_PROBE_ASM_TEMPLATE(n) for n arguments. The paired
308 macro STAP_PROBE_ASM_OPERANDS gives the C values of these probe arguments,
309 and appears in the input operand list of the asm statement. For example:
310
311 asm ("someinsn %0,%1\n" // %0 is output operand, %1 is input operand
312 STAP_PROBE_ASM(provider, fooprobe, STAP_PROBE_ASM_TEMPLATE(3))
313 "otherinsn %[namedarg]"
314 : "r" (outvar)
315 : "g" (some_value), [namedarg] "i" (1234),
316 STAP_PROBE_ASM_OPERANDS(3, some_value, some_ptr->field, 1234));
317
318 This is just like writing:
319
320 STAP_PROBE3(provider, fooprobe, some_value, some_ptr->field, 1234));
321
322 but the probe site is right between "someinsn" and "otherinsn".
323
324 The probe arguments in STAP_PROBE_ASM can be given as assembly
325 operands instead, even inside a GNU C extended asm statement.
326 Note that these can use operand templates like %0 or %[name],
327 and likewise they must write %%reg for a literal operand of %reg. */
328
329 #if __STDC_VERSION__ >= 199901L
330 # define STAP_PROBE_ASM(provider, name, ...) \
331 _SDT_ASM_BODY(provider, name, _SDT_ASM_STRING, (__VA_ARGS__)) \
332 _SDT_ASM_BASE
333 # define STAP_PROBE_ASM_OPERANDS(n, ...) _SDT_ASM_OPERANDS_##n(__VA_ARGS__)
334 #else
335 # define STAP_PROBE_ASM(provider, name, args) \
336 _SDT_ASM_BODY(provider, name, _SDT_ASM_STRING, (args)) \
337 _SDT_ASM_BASE
338 #endif
339 #define STAP_PROBE_ASM_TEMPLATE(n) _SDT_ASM_TEMPLATE_##n
340
341
342 /* DTrace compatible macro names. */
343 #define DTRACE_PROBE(provider,probe) \
344 STAP_PROBE(provider,probe)
345 #define DTRACE_PROBE1(provider,probe,parm1) \
346 STAP_PROBE1(provider,probe,parm1)
347 #define DTRACE_PROBE2(provider,probe,parm1,parm2) \
348 STAP_PROBE2(provider,probe,parm1,parm2)
349 #define DTRACE_PROBE3(provider,probe,parm1,parm2,parm3) \
350 STAP_PROBE3(provider,probe,parm1,parm2,parm3)
351 #define DTRACE_PROBE4(provider,probe,parm1,parm2,parm3,parm4) \
352 STAP_PROBE4(provider,probe,parm1,parm2,parm3,parm4)
353 #define DTRACE_PROBE5(provider,probe,parm1,parm2,parm3,parm4,parm5) \
354 STAP_PROBE5(provider,probe,parm1,parm2,parm3,parm4,parm5)
355 #define DTRACE_PROBE6(provider,probe,parm1,parm2,parm3,parm4,parm5,parm6) \
356 STAP_PROBE6(provider,probe,parm1,parm2,parm3,parm4,parm5,parm6)
357 #define DTRACE_PROBE7(provider,probe,parm1,parm2,parm3,parm4,parm5,parm6,parm7) \
358 STAP_PROBE7(provider,probe,parm1,parm2,parm3,parm4,parm5,parm6,parm7)
359 #define DTRACE_PROBE8(provider,probe,parm1,parm2,parm3,parm4,parm5,parm6,parm7,parm8) \
360 STAP_PROBE8(provider,probe,parm1,parm2,parm3,parm4,parm5,parm6,parm7,parm8)
361 #define DTRACE_PROBE9(provider,probe,parm1,parm2,parm3,parm4,parm5,parm6,parm7,parm8,parm9) \
362 STAP_PROBE9(provider,probe,parm1,parm2,parm3,parm4,parm5,parm6,parm7,parm8,parm9)
363 #define DTRACE_PROBE10(provider,probe,parm1,parm2,parm3,parm4,parm5,parm6,parm7,parm8,parm9,parm10) \
364 STAP_PROBE10(provider,probe,parm1,parm2,parm3,parm4,parm5,parm6,parm7,parm8,parm9,parm10)
365
366
367 #endif /* sys/sdt.h */
This page took 0.05548 seconds and 5 git commands to generate.