1 // SPDX-License-Identifier: MIT
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
16 #include <side/macros.h>
17 #include <side/endian.h>
20 * SIDE stands for "Software Instrumentation Dynamically Enabled"
22 * This is an instrumentation ABI for Linux user-space, which exposes an
23 * instrumentation type system and facilities allowing a kernel or
24 * user-space tracer to consume user-space instrumentation.
26 * The extensibility scheme for the SIDE ABI for event state is as
29 * * If the semantic of the "struct side_event_state_N" fields change,
30 * the SIDE_EVENT_STATE_ABI_VERSION should be increased. The
31 * "struct side_event_state_N" is not extensible and must have its
32 * ABI version increased whenever it is changed. Note that increasing
33 * the version of SIDE_EVENT_DESCRIPTION_ABI_VERSION is not necessary
34 * when changing the layout of "struct side_event_state_N".
37 #define SIDE_EVENT_STATE_ABI_VERSION 0
39 #include <side/abi/event-description.h>
40 #include <side/abi/type-argument.h>
41 #include <side/instrumentation-c-api.h>
49 SIDE_ERROR_EXITING
= 5,
53 * This structure is _not_ packed to allow atomic operations on its
54 * fields. Changes to this structure must bump the "Event state ABI
55 * version" and tracers _must_ learn how to deal with this ABI,
56 * otherwise they should reject the event.
59 struct side_event_state
{
60 uint32_t version
; /* Event state ABI version. */
63 struct side_event_state_0
{
64 struct side_event_state parent
; /* Required first field. */
65 uint32_t nr_callbacks
;
67 const struct side_callback
*callbacks
;
68 struct side_event_description
*desc
;
77 extern const char side_empty_callback
[];
79 void side_call(const struct side_event_state
*state
,
80 const struct side_arg_vec
*side_arg_vec
);
81 void side_call_variadic(const struct side_event_state
*state
,
82 const struct side_arg_vec
*side_arg_vec
,
83 const struct side_arg_dynamic_struct
*var_struct
);
84 void side_call_key(const struct side_event_state
*state
,
85 const struct side_arg_vec
*side_arg_vec
,
87 void side_call_variadic_key(const struct side_event_state
*state
,
88 const struct side_arg_vec
*side_arg_vec
,
89 const struct side_arg_dynamic_struct
*var_struct
,
92 struct side_events_register_handle
*side_events_register(struct side_event_description
**events
,
94 void side_events_unregister(struct side_events_register_handle
*handle
);
97 * Userspace tracer registration API. This allows userspace tracers to
98 * register event notification callbacks to be notified of the currently
99 * registered instrumentation, and to register their callbacks to
102 typedef void (*side_tracer_callback_func
)(const struct side_event_description
*desc
,
103 const struct side_arg_vec
*side_arg_vec
,
105 typedef void (*side_tracer_callback_variadic_func
)(const struct side_event_description
*desc
,
106 const struct side_arg_vec
*side_arg_vec
,
107 const struct side_arg_dynamic_struct
*var_struct
,
110 int side_tracer_callback_register(struct side_event_description
*desc
,
111 side_tracer_callback_func call
,
112 void *priv
, void *key
);
113 int side_tracer_callback_variadic_register(struct side_event_description
*desc
,
114 side_tracer_callback_variadic_func call_variadic
,
115 void *priv
, void *key
);
116 int side_tracer_callback_unregister(struct side_event_description
*desc
,
117 side_tracer_callback_func call
,
118 void *priv
, void *key
);
119 int side_tracer_callback_variadic_unregister(struct side_event_description
*desc
,
120 side_tracer_callback_variadic_func call_variadic
,
121 void *priv
, void *key
);
123 enum side_tracer_notification
{
124 SIDE_TRACER_NOTIFICATION_INSERT_EVENTS
,
125 SIDE_TRACER_NOTIFICATION_REMOVE_EVENTS
,
128 /* Callback is invoked with side library internal lock held. */
129 struct side_tracer_handle
*side_tracer_event_notification_register(
130 void (*cb
)(enum side_tracer_notification notif
,
131 struct side_event_description
**events
, uint32_t nr_events
, void *priv
),
133 void side_tracer_event_notification_unregister(struct side_tracer_handle
*handle
);
136 * Explicit hooks to initialize/finalize the side instrumentation
137 * library. Those are also library constructor/destructor.
139 void side_init(void) __attribute__((constructor
));
140 void side_exit(void) __attribute__((destructor
));
143 * The following constructors/destructors perform automatic registration
144 * of the declared side events. Those may have to be called explicitly
145 * in a statically linked library.
149 * These weak symbols, the constructor, and destructor take care of
150 * registering only _one_ instance of the side instrumentation per
151 * shared-ojbect (or for the whole main program).
153 extern struct side_event_description
* __start_side_event_description_ptr
[]
154 __attribute__((weak
, visibility("hidden")));
155 extern struct side_event_description
* __stop_side_event_description_ptr
[]
156 __attribute__((weak
, visibility("hidden")));
157 int side_event_description_ptr_registered
158 __attribute__((weak
, visibility("hidden")));
159 struct side_events_register_handle
*side_events_handle
160 __attribute__((weak
, visibility("hidden")));
163 side_event_description_ptr_init(void)
164 __attribute__((no_instrument_function
))
165 __attribute__((constructor
));
167 side_event_description_ptr_init(void)
169 if (side_event_description_ptr_registered
++)
171 side_events_handle
= side_events_register(__start_side_event_description_ptr
,
172 __stop_side_event_description_ptr
- __start_side_event_description_ptr
);
176 side_event_description_ptr_exit(void)
177 __attribute__((no_instrument_function
))
178 __attribute__((destructor
));
180 side_event_description_ptr_exit(void)
182 if (--side_event_description_ptr_registered
)
184 side_events_unregister(side_events_handle
);
185 side_events_handle
= NULL
;
192 #endif /* _SIDE_TRACE_H */