Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * This single-header library defines a collection of variadic macros for
4 * defining and triggering USDTs (User Statically-Defined Tracepoints):
5 *
6 * - For USDTs without associated semaphore:
7 * USDT(group, name, args...)
8 *
9 * - For USDTs with implicit (transparent to the user) semaphore:
10 * USDT_WITH_SEMA(group, name, args...)
11 * USDT_IS_ACTIVE(group, name)
12 *
13 * - For USDTs with explicit (user-defined and provided) semaphore:
14 * USDT_WITH_EXPLICIT_SEMA(sema, group, name, args...)
15 * USDT_SEMA_IS_ACTIVE(sema)
16 *
17 * all of which emit a NOP instruction into the instruction stream, and so
18 * have *zero* overhead for the surrounding code. USDTs are identified by
19 * a combination of `group` and `name` identifiers, which is used by external
20 * tracing tooling (tracers) for identifying exact USDTs of interest.
21 *
22 * USDTs can have an associated (2-byte) activity counter (USDT semaphore),
23 * automatically maintained by Linux kernel whenever any correctly written
24 * BPF-based tracer is attached to the USDT. This USDT semaphore can be used
25 * to check whether there is a need to do any extra data collection and
26 * processing for a given USDT (if necessary), and otherwise avoid extra work
27 * for a common case of USDT not being traced ("active").
28 *
29 * See documentation for USDT_WITH_SEMA()/USDT_IS_ACTIVE() or
30 * USDT_WITH_EXPLICIT_SEMA()/USDT_SEMA_IS_ACTIVE() APIs below for details on
31 * working with USDTs with implicitly or explicitly associated
32 * USDT semaphores, respectively.
33 *
34 * There is also some additional data recorded into an auxiliary note
35 * section. The data in the note section describes the operands, in terms of
36 * size and location, used by tracing tooling to know where to find USDT
37 * arguments. Each location is encoded as an assembler operand string.
38 * Tracing tools (bpftrace and BPF-based tracers, systemtap, etc) insert
39 * breakpoints on top of the nop, and decode the location operand-strings,
40 * like an assembler, to find the values being passed.
41 *
42 * The operand strings are selected by the compiler for each operand.
43 * They are constrained by inline-assembler codes.The default is:
44 *
45 * #define USDT_ARG_CONSTRAINT nor
46 *
47 * This is a good default if the operands tend to be integral and
48 * moderate in number (smaller than number of registers). In other
49 * cases, the compiler may report "'asm' requires impossible reload" or
50 * similar. In this case, consider simplifying the macro call (fewer
51 * and simpler operands), reduce optimization, or override the default
52 * constraints string via:
53 *
54 * #define USDT_ARG_CONSTRAINT g
55 * #include <usdt.h>
56 *
57 * For some historical description of USDT v3 format (the one used by this
58 * library and generally recognized and assumed by BPF-based tracing tools)
59 * see [0]. The more formal specification can be found at [1]. Additional
60 * argument constraints information can be found at [2].
61 *
62 * Original SystemTap's sys/sdt.h implementation ([3]) was used as a base for
63 * this USDT library implementation. Current implementation differs *a lot* in
64 * terms of exposed user API and general usability, which was the main goal
65 * and focus of the reimplementation work. Nevertheless, underlying recorded
66 * USDT definitions are fully binary compatible and any USDT-based tooling
67 * should work equally well with USDTs defined by either SystemTap's or this
68 * library's USDT implementation.
69 *
70 * [0] https://ecos.sourceware.org/ml/systemtap/2010-q3/msg00145.html
71 * [1] https://sourceware.org/systemtap/wiki/UserSpaceProbeImplementation
72 * [2] https://gcc.gnu.org/onlinedocs/gcc/Constraints.html
73 * [3] https://sourceware.org/git/?p=systemtap.git;a=blob;f=includes/sys/sdt.h
74 */
75#ifndef __USDT_H
76#define __USDT_H
77
78/*
79 * Changelog:
80 *
81 * 0.1.0
82 * -----
83 * - Initial release
84 */
85#define USDT_MAJOR_VERSION 0
86#define USDT_MINOR_VERSION 1
87#define USDT_PATCH_VERSION 0
88
89/* C++20 and C23 added __VA_OPT__ as a standard replacement for non-standard `##__VA_ARGS__` extension */
90#if (defined(__STDC_VERSION__) && __STDC_VERSION__ > 201710L) || (defined(__cplusplus) && __cplusplus > 201703L)
91#define __usdt_va_opt 1
92#define __usdt_va_args(...) __VA_OPT__(,) __VA_ARGS__
93#else
94#define __usdt_va_args(...) , ##__VA_ARGS__
95#endif
96
97/*
98 * Trigger USDT with `group`:`name` identifier and pass through `args` as its
99 * arguments. Zero arguments are acceptable as well. No USDT semaphore is
100 * associated with this USDT.
101 *
102 * Such "semaphoreless" USDTs are commonly used when there is no extra data
103 * collection or processing needed to collect and prepare USDT arguments and
104 * they are just available in the surrounding code. USDT() macro will just
105 * record their locations in CPU registers or in memory for tracing tooling to
106 * be able to access them, if necessary.
107 */
108#ifdef __usdt_va_opt
109#define USDT(group, name, ...) \
110 __usdt_probe(group, name, __usdt_sema_none, 0 __VA_OPT__(,) __VA_ARGS__)
111#else
112#define USDT(group, name, ...) \
113 __usdt_probe(group, name, __usdt_sema_none, 0, ##__VA_ARGS__)
114#endif
115
116/*
117 * Trigger USDT with `group`:`name` identifier and pass through `args` as its
118 * arguments. Zero arguments are acceptable as well. USDT also get an
119 * implicitly-defined associated USDT semaphore, which will be "activated" by
120 * tracing tooling and can be used to check whether USDT is being actively
121 * observed.
122 *
123 * USDTs with semaphore are commonly used when there is a need to perform
124 * additional data collection and processing to prepare USDT arguments, which
125 * otherwise might not be necessary for the rest of application logic. In such
126 * case, USDT semaphore can be used to avoid unnecessary extra work. If USDT
127 * is not traced (which is presumed to be a common situation), the associated
128 * USDT semaphore is "inactive", and so there is no need to waste resources to
129 * prepare USDT arguments. Use USDT_IS_ACTIVE(group, name) to check whether
130 * USDT is "active".
131 *
132 * N.B. There is an inherent (albeit short) gap between checking whether USDT
133 * is active and triggering corresponding USDT, in which external tracer can
134 * be attached to an USDT and activate USDT semaphore after the activity check.
135 * If such a race occurs, tracers might miss one USDT execution. Tracers are
136 * expected to accommodate such possibility and this is expected to not be
137 * a problem for applications and tracers.
138 *
139 * N.B. Implicit USDT semaphore defined by USDT_WITH_SEMA() is contained
140 * within a single executable or shared library and is not shared outside
141 * them. I.e., if you use USDT_WITH_SEMA() with the same USDT group and name
142 * identifier across executable and shared library, it will work and won't
143 * conflict, per se, but will define independent USDT semaphores, one for each
144 * shared library/executable in which USDT_WITH_SEMA(group, name) is used.
145 * That is, if you attach to this USDT in one shared library (or executable),
146 * then only USDT semaphore within that shared library (or executable) will be
147 * updated by the kernel, while other libraries (or executable) will not see
148 * activated USDT semaphore. In short, it's best to use unique USDT group:name
149 * identifiers across different shared libraries (and, equivalently, between
150 * executable and shared library). This is advanced consideration and is
151 * rarely (if ever) seen in practice, but just to avoid surprises this is
152 * called out here. (Static libraries become a part of final executable, once
153 * linked by linker, so the above considerations don't apply to them.)
154 */
155#ifdef __usdt_va_opt
156#define USDT_WITH_SEMA(group, name, ...) \
157 __usdt_probe(group, name, \
158 __usdt_sema_implicit, __usdt_sema_name(group, name) \
159 __VA_OPT__(,) __VA_ARGS__)
160#else
161#define USDT_WITH_SEMA(group, name, ...) \
162 __usdt_probe(group, name, \
163 __usdt_sema_implicit, __usdt_sema_name(group, name), \
164 ##__VA_ARGS__)
165#endif
166
167struct usdt_sema { volatile unsigned short active; };
168
169/*
170 * Check if USDT with `group`:`name` identifier is "active" (i.e., whether it
171 * is attached to by external tracing tooling and is actively observed).
172 *
173 * This macro can be used to decide whether any additional and potentially
174 * expensive data collection or processing should be done to pass extra
175 * information into the given USDT. It is assumed that USDT is triggered with
176 * USDT_WITH_SEMA() macro which will implicitly define associated USDT
177 * semaphore. (If one needs more control over USDT semaphore, see
178 * USDT_DEFINE_SEMA() and USDT_WITH_EXPLICIT_SEMA() macros below.)
179 *
180 * N.B. Such checks are necessarily racy and speculative. Between checking
181 * whether USDT is active and triggering the USDT itself, tracer can be
182 * detached with no notification. This race should be extremely rare and worst
183 * case should result in one-time wasted extra data collection and processing.
184 */
185#define USDT_IS_ACTIVE(group, name) ({ \
186 extern struct usdt_sema __usdt_sema_name(group, name) \
187 __usdt_asm_name(__usdt_sema_name(group, name)); \
188 __usdt_sema_implicit(__usdt_sema_name(group, name)); \
189 __usdt_sema_name(group, name).active > 0; \
190})
191
192/*
193 * APIs for working with user-defined explicit USDT semaphores.
194 *
195 * This is a less commonly used advanced API for use cases in which user needs
196 * an explicit control over (potentially shared across multiple USDTs) USDT
197 * semaphore instance. This can be used when there is a group of logically
198 * related USDTs that all need extra data collection and processing whenever
199 * any of a family of related USDTs are "activated" (i.e., traced). In such
200 * a case, all such related USDTs will be associated with the same shared USDT
201 * semaphore defined with USDT_DEFINE_SEMA() and the USDTs themselves will be
202 * triggered with USDT_WITH_EXPLICIT_SEMA() macros, taking an explicit extra
203 * USDT semaphore identifier as an extra parameter.
204 */
205
206/**
207 * Underlying C global variable name for user-defined USDT semaphore with
208 * `sema` identifier. Could be useful for debugging, but normally shouldn't be
209 * used explicitly.
210 */
211#define USDT_SEMA(sema) __usdt_sema_##sema
212
213/*
214 * Define storage for user-defined USDT semaphore `sema`.
215 *
216 * Should be used only once in non-header source file to let compiler allocate
217 * space for the semaphore variable. Just like with any other global variable.
218 *
219 * This macro can be used anywhere where global variable declaration is
220 * allowed. Just like with global variable definitions, there should be only
221 * one definition of user-defined USDT semaphore with given `sema` identifier,
222 * otherwise compiler or linker will complain about duplicate variable
223 * definition.
224 *
225 * For C++, it is allowed to use USDT_DEFINE_SEMA() both in global namespace
226 * and inside namespaces (including nested namespaces). Just make sure that
227 * USDT_DECLARE_SEMA() is placed within the namespace where this semaphore is
228 * referenced, or any of its parent namespaces, so the C++ language-level
229 * identifier is visible to the code that needs to reference the semaphore.
230 * At the lowest layer, USDT semaphores have global naming and visibility
231 * (they have a corresponding `__usdt_sema_<name>` symbol, which can be linked
232 * against from C or C++ code, if necessary). To keep it simple, putting
233 * USDT_DECLARE_SEMA() declarations into global namespaces is the simplest
234 * no-brainer solution. All these aspects are irrelevant for plain C, because
235 * C doesn't have namespaces and everything is always in the global namespace.
236 *
237 * N.B. Due to USDT metadata being recorded in non-allocatable ELF note
238 * section, it has limitations when it comes to relocations, which, in
239 * practice, means that it's not possible to correctly share USDT semaphores
240 * between main executable and shared libraries, or even between multiple
241 * shared libraries. USDT semaphore has to be contained to individual shared
242 * library or executable to avoid unpleasant surprises with half-working USDT
243 * semaphores. We enforce this by marking semaphore ELF symbols as having
244 * a hidden visibility. This is quite an advanced use case and consideration
245 * and for most users this should have no consequences whatsoever.
246 */
247#define USDT_DEFINE_SEMA(sema) \
248 struct usdt_sema __usdt_sema_sec USDT_SEMA(sema) \
249 __usdt_asm_name(USDT_SEMA(sema)) \
250 __attribute__((visibility("hidden"))) = { 0 }
251
252/*
253 * Declare extern reference to user-defined USDT semaphore `sema`.
254 *
255 * Refers to a variable defined in another compilation unit by
256 * USDT_DEFINE_SEMA() and allows to use the same USDT semaphore across
257 * multiple compilation units (i.e., .c and .cpp files).
258 *
259 * See USDT_DEFINE_SEMA() notes above for C++ language usage peculiarities.
260 */
261#define USDT_DECLARE_SEMA(sema) \
262 extern struct usdt_sema USDT_SEMA(sema) __usdt_asm_name(USDT_SEMA(sema))
263
264/*
265 * Check if user-defined USDT semaphore `sema` is "active" (i.e., whether it
266 * is attached to by external tracing tooling and is actively observed).
267 *
268 * This macro can be used to decide whether any additional and potentially
269 * expensive data collection or processing should be done to pass extra
270 * information into USDT(s) associated with USDT semaphore `sema`.
271 *
272 * N.B. Such checks are necessarily racy. Between checking the state of USDT
273 * semaphore and triggering associated USDT(s), the active tracer might attach
274 * or detach. This race should be extremely rare and worst case should result
275 * in one-time missed USDT event or wasted extra data collection and
276 * processing. USDT-using tracers should be written with this in mind and is
277 * not a concern of the application defining USDTs with associated semaphore.
278 */
279#define USDT_SEMA_IS_ACTIVE(sema) (USDT_SEMA(sema).active > 0)
280
281/*
282 * Invoke USDT specified by `group` and `name` identifiers and associate
283 * explicitly user-defined semaphore `sema` with it. Pass through `args` as
284 * USDT arguments. `args` are optional and zero arguments are acceptable.
285 *
286 * Semaphore is defined with the help of USDT_DEFINE_SEMA() macro and can be
287 * checked whether active with USDT_SEMA_IS_ACTIVE().
288 */
289#ifdef __usdt_va_opt
290#define USDT_WITH_EXPLICIT_SEMA(sema, group, name, ...) \
291 __usdt_probe(group, name, __usdt_sema_explicit, USDT_SEMA(sema), ##__VA_ARGS__)
292#else
293#define USDT_WITH_EXPLICIT_SEMA(sema, group, name, ...) \
294 __usdt_probe(group, name, __usdt_sema_explicit, USDT_SEMA(sema) __VA_OPT__(,) __VA_ARGS__)
295#endif
296
297/*
298 * Adjustable implementation aspects
299 */
300#ifndef USDT_ARG_CONSTRAINT
301#if defined __powerpc__
302#define USDT_ARG_CONSTRAINT nZr
303#elif defined __arm__
304#define USDT_ARG_CONSTRAINT g
305#elif defined __loongarch__
306#define USDT_ARG_CONSTRAINT nmr
307#else
308#define USDT_ARG_CONSTRAINT nor
309#endif
310#endif /* USDT_ARG_CONSTRAINT */
311
312#ifndef USDT_NOP
313#if defined(__ia64__) || defined(__s390__) || defined(__s390x__)
314#define USDT_NOP nop 0
315#else
316#define USDT_NOP nop
317#endif
318#endif /* USDT_NOP */
319
320/*
321 * Implementation details
322 */
323/* USDT name for implicitly-defined USDT semaphore, derived from group:name */
324#define __usdt_sema_name(group, name) __usdt_sema_##group##__##name
325/* ELF section into which USDT semaphores are put */
326#define __usdt_sema_sec __attribute__((section(".probes")))
327
328#define __usdt_concat(a, b) a ## b
329#define __usdt_apply(fn, n) __usdt_concat(fn, n)
330
331#ifndef __usdt_nth
332#define __usdt_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N, ...) N
333#endif
334
335#ifndef __usdt_narg
336#ifdef __usdt_va_opt
337#define __usdt_narg(...) __usdt_nth(_ __VA_OPT__(,) __VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
338#else
339#define __usdt_narg(...) __usdt_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
340#endif
341#endif /* __usdt_narg */
342
343#define __usdt_hash #
344#define __usdt_str_(x) #x
345#define __usdt_str(x) __usdt_str_(x)
346
347#ifndef __usdt_asm_name
348#define __usdt_asm_name(name) __asm__(__usdt_str(name))
349#endif
350
351#define __usdt_asm0() "\n"
352#define __usdt_asm1(x) __usdt_str(x) "\n"
353#define __usdt_asm2(x, ...) __usdt_str(x) "," __usdt_asm1(__VA_ARGS__)
354#define __usdt_asm3(x, ...) __usdt_str(x) "," __usdt_asm2(__VA_ARGS__)
355#define __usdt_asm4(x, ...) __usdt_str(x) "," __usdt_asm3(__VA_ARGS__)
356#define __usdt_asm5(x, ...) __usdt_str(x) "," __usdt_asm4(__VA_ARGS__)
357#define __usdt_asm6(x, ...) __usdt_str(x) "," __usdt_asm5(__VA_ARGS__)
358#define __usdt_asm7(x, ...) __usdt_str(x) "," __usdt_asm6(__VA_ARGS__)
359#define __usdt_asm8(x, ...) __usdt_str(x) "," __usdt_asm7(__VA_ARGS__)
360#define __usdt_asm9(x, ...) __usdt_str(x) "," __usdt_asm8(__VA_ARGS__)
361#define __usdt_asm10(x, ...) __usdt_str(x) "," __usdt_asm9(__VA_ARGS__)
362#define __usdt_asm11(x, ...) __usdt_str(x) "," __usdt_asm10(__VA_ARGS__)
363#define __usdt_asm12(x, ...) __usdt_str(x) "," __usdt_asm11(__VA_ARGS__)
364#define __usdt_asm(...) __usdt_apply(__usdt_asm, __usdt_narg(__VA_ARGS__))(__VA_ARGS__)
365
366#ifdef __LP64__
367#define __usdt_asm_addr .8byte
368#else
369#define __usdt_asm_addr .4byte
370#endif
371
372#define __usdt_asm_strz_(x) __usdt_asm1(.asciz #x)
373#define __usdt_asm_strz(x) __usdt_asm_strz_(x)
374#define __usdt_asm_str_(x) __usdt_asm1(.ascii #x)
375#define __usdt_asm_str(x) __usdt_asm_str_(x)
376
377/* "semaphoreless" USDT case */
378#ifndef __usdt_sema_none
379#define __usdt_sema_none(sema)
380#endif
381
382/* implicitly defined __usdt_sema__group__name semaphore (using weak symbols) */
383#ifndef __usdt_sema_implicit
384#define __usdt_sema_implicit(sema) \
385 __asm__ __volatile__ ( \
386 __usdt_asm1(.ifndef sema) \
387 __usdt_asm3( .pushsection .probes, "aw", "progbits") \
388 __usdt_asm1( .weak sema) \
389 __usdt_asm1( .hidden sema) \
390 __usdt_asm1( .align 2) \
391 __usdt_asm1(sema:) \
392 __usdt_asm1( .zero 2) \
393 __usdt_asm2( .type sema, @object) \
394 __usdt_asm2( .size sema, 2) \
395 __usdt_asm1( .popsection) \
396 __usdt_asm1(.endif) \
397 );
398#endif
399
400/* externally defined semaphore using USDT_DEFINE_SEMA() and passed explicitly by user */
401#ifndef __usdt_sema_explicit
402#define __usdt_sema_explicit(sema) \
403 __asm__ __volatile__ ("" :: "m" (sema));
404#endif
405
406/* main USDT definition (nop and .note.stapsdt metadata) */
407#define __usdt_probe(group, name, sema_def, sema, ...) do { \
408 sema_def(sema) \
409 __asm__ __volatile__ ( \
410 __usdt_asm( 990: USDT_NOP) \
411 __usdt_asm3( .pushsection .note.stapsdt, "", "note") \
412 __usdt_asm1( .balign 4) \
413 __usdt_asm3( .4byte 992f-991f,994f-993f,3) \
414 __usdt_asm1(991: .asciz "stapsdt") \
415 __usdt_asm1(992: .balign 4) \
416 __usdt_asm1(993: __usdt_asm_addr 990b) \
417 __usdt_asm1( __usdt_asm_addr _.stapsdt.base) \
418 __usdt_asm1( __usdt_asm_addr sema) \
419 __usdt_asm_strz(group) \
420 __usdt_asm_strz(name) \
421 __usdt_asm_args(__VA_ARGS__) \
422 __usdt_asm1( .ascii "\0") \
423 __usdt_asm1(994: .balign 4) \
424 __usdt_asm1( .popsection) \
425 __usdt_asm1(.ifndef _.stapsdt.base) \
426 __usdt_asm5( .pushsection .stapsdt.base,"aG","progbits",.stapsdt.base,comdat)\
427 __usdt_asm1( .weak _.stapsdt.base) \
428 __usdt_asm1( .hidden _.stapsdt.base) \
429 __usdt_asm1(_.stapsdt.base:) \
430 __usdt_asm1( .space 1) \
431 __usdt_asm2( .size _.stapsdt.base, 1) \
432 __usdt_asm1( .popsection) \
433 __usdt_asm1(.endif) \
434 :: __usdt_asm_ops(__VA_ARGS__) \
435 ); \
436} while (0)
437
438/*
439 * NB: gdb PR24541 highlighted an unspecified corner of the sdt.h
440 * operand note format.
441 *
442 * The named register may be a longer or shorter (!) alias for the
443 * storage where the value in question is found. For example, on
444 * i386, 64-bit value may be put in register pairs, and a register
445 * name stored would identify just one of them. Previously, gcc was
446 * asked to emit the %w[id] (16-bit alias of some registers holding
447 * operands), even when a wider 32-bit value was used.
448 *
449 * Bottom line: the byte-width given before the @ sign governs. If
450 * there is a mismatch between that width and that of the named
451 * register, then a sys/sdt.h note consumer may need to employ
452 * architecture-specific heuristics to figure out where the compiler
453 * has actually put the complete value.
454 */
455#if defined(__powerpc__) || defined(__powerpc64__)
456#define __usdt_argref(id) %I[id]%[id]
457#elif defined(__i386__)
458#define __usdt_argref(id) %k[id] /* gcc.gnu.org/PR80115 sourceware.org/PR24541 */
459#else
460#define __usdt_argref(id) %[id]
461#endif
462
463#define __usdt_asm_arg(n) __usdt_asm_str(%c[__usdt_asz##n]) \
464 __usdt_asm1(.ascii "@") \
465 __usdt_asm_str(__usdt_argref(__usdt_aval##n))
466
467#define __usdt_asm_args0 /* no arguments */
468#define __usdt_asm_args1 __usdt_asm_arg(1)
469#define __usdt_asm_args2 __usdt_asm_args1 __usdt_asm1(.ascii " ") __usdt_asm_arg(2)
470#define __usdt_asm_args3 __usdt_asm_args2 __usdt_asm1(.ascii " ") __usdt_asm_arg(3)
471#define __usdt_asm_args4 __usdt_asm_args3 __usdt_asm1(.ascii " ") __usdt_asm_arg(4)
472#define __usdt_asm_args5 __usdt_asm_args4 __usdt_asm1(.ascii " ") __usdt_asm_arg(5)
473#define __usdt_asm_args6 __usdt_asm_args5 __usdt_asm1(.ascii " ") __usdt_asm_arg(6)
474#define __usdt_asm_args7 __usdt_asm_args6 __usdt_asm1(.ascii " ") __usdt_asm_arg(7)
475#define __usdt_asm_args8 __usdt_asm_args7 __usdt_asm1(.ascii " ") __usdt_asm_arg(8)
476#define __usdt_asm_args9 __usdt_asm_args8 __usdt_asm1(.ascii " ") __usdt_asm_arg(9)
477#define __usdt_asm_args10 __usdt_asm_args9 __usdt_asm1(.ascii " ") __usdt_asm_arg(10)
478#define __usdt_asm_args11 __usdt_asm_args10 __usdt_asm1(.ascii " ") __usdt_asm_arg(11)
479#define __usdt_asm_args12 __usdt_asm_args11 __usdt_asm1(.ascii " ") __usdt_asm_arg(12)
480#define __usdt_asm_args(...) __usdt_apply(__usdt_asm_args, __usdt_narg(__VA_ARGS__))
481
482#define __usdt_is_arr(x) (__builtin_classify_type(x) == 14 || __builtin_classify_type(x) == 5)
483#define __usdt_arg_size(x) (__usdt_is_arr(x) ? sizeof(void *) : sizeof(x))
484
485/*
486 * We can't use __builtin_choose_expr() in C++, so fall back to table-based
487 * signedness determination for known types, utilizing templates magic.
488 */
489#ifdef __cplusplus
490
491#define __usdt_is_signed(x) (!__usdt_is_arr(x) && __usdt_t<__typeof(x)>::is_signed)
492
493#include <cstddef>
494
495template<typename T> struct __usdt_t { static const bool is_signed = false; };
496template<typename A> struct __usdt_t<A[]> : public __usdt_t<A *> {};
497template<typename A, size_t N> struct __usdt_t<A[N]> : public __usdt_t<A *> {};
498
499#define __usdt_def_signed(T) \
500template<> struct __usdt_t<T> { static const bool is_signed = true; }; \
501template<> struct __usdt_t<const T> { static const bool is_signed = true; }; \
502template<> struct __usdt_t<volatile T> { static const bool is_signed = true; }; \
503template<> struct __usdt_t<const volatile T> { static const bool is_signed = true; }
504#define __usdt_maybe_signed(T) \
505template<> struct __usdt_t<T> { static const bool is_signed = (T)-1 < (T)1; }; \
506template<> struct __usdt_t<const T> { static const bool is_signed = (T)-1 < (T)1; }; \
507template<> struct __usdt_t<volatile T> { static const bool is_signed = (T)-1 < (T)1; }; \
508template<> struct __usdt_t<const volatile T> { static const bool is_signed = (T)-1 < (T)1; }
509
510__usdt_def_signed(signed char);
511__usdt_def_signed(short);
512__usdt_def_signed(int);
513__usdt_def_signed(long);
514__usdt_def_signed(long long);
515__usdt_maybe_signed(char);
516__usdt_maybe_signed(wchar_t);
517
518#else /* !__cplusplus */
519
520#define __usdt_is_inttype(x) (__builtin_classify_type(x) >= 1 && __builtin_classify_type(x) <= 4)
521#define __usdt_inttype(x) __typeof(__builtin_choose_expr(__usdt_is_inttype(x), (x), 0U))
522#define __usdt_is_signed(x) ((__usdt_inttype(x))-1 < (__usdt_inttype(x))1)
523
524#endif /* __cplusplus */
525
526#define __usdt_asm_op(n, x) \
527 [__usdt_asz##n] "n" ((__usdt_is_signed(x) ? (int)-1 : 1) * (int)__usdt_arg_size(x)), \
528 [__usdt_aval##n] __usdt_str(USDT_ARG_CONSTRAINT)(x)
529
530#define __usdt_asm_ops0() [__usdt_dummy] "g" (0)
531#define __usdt_asm_ops1(x) __usdt_asm_op(1, x)
532#define __usdt_asm_ops2(a,x) __usdt_asm_ops1(a), __usdt_asm_op(2, x)
533#define __usdt_asm_ops3(a,b,x) __usdt_asm_ops2(a,b), __usdt_asm_op(3, x)
534#define __usdt_asm_ops4(a,b,c,x) __usdt_asm_ops3(a,b,c), __usdt_asm_op(4, x)
535#define __usdt_asm_ops5(a,b,c,d,x) __usdt_asm_ops4(a,b,c,d), __usdt_asm_op(5, x)
536#define __usdt_asm_ops6(a,b,c,d,e,x) __usdt_asm_ops5(a,b,c,d,e), __usdt_asm_op(6, x)
537#define __usdt_asm_ops7(a,b,c,d,e,f,x) __usdt_asm_ops6(a,b,c,d,e,f), __usdt_asm_op(7, x)
538#define __usdt_asm_ops8(a,b,c,d,e,f,g,x) __usdt_asm_ops7(a,b,c,d,e,f,g), __usdt_asm_op(8, x)
539#define __usdt_asm_ops9(a,b,c,d,e,f,g,h,x) __usdt_asm_ops8(a,b,c,d,e,f,g,h), __usdt_asm_op(9, x)
540#define __usdt_asm_ops10(a,b,c,d,e,f,g,h,i,x) __usdt_asm_ops9(a,b,c,d,e,f,g,h,i), __usdt_asm_op(10, x)
541#define __usdt_asm_ops11(a,b,c,d,e,f,g,h,i,j,x) __usdt_asm_ops10(a,b,c,d,e,f,g,h,i,j), __usdt_asm_op(11, x)
542#define __usdt_asm_ops12(a,b,c,d,e,f,g,h,i,j,k,x) __usdt_asm_ops11(a,b,c,d,e,f,g,h,i,j,k), __usdt_asm_op(12, x)
543#define __usdt_asm_ops(...) __usdt_apply(__usdt_asm_ops, __usdt_narg(__VA_ARGS__))(__VA_ARGS__)
544
545#endif /* __USDT_H */