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
;
76 struct side_tracer_handle
;
77 struct side_statedump_request_handle
;
79 extern const char side_empty_callback
[];
81 void side_call(const struct side_event_state
*state
,
82 const struct side_arg_vec
*side_arg_vec
);
83 void side_call_variadic(const struct side_event_state
*state
,
84 const struct side_arg_vec
*side_arg_vec
,
85 const struct side_arg_dynamic_struct
*var_struct
);
87 struct side_events_register_handle
*side_events_register(struct side_event_description
**events
,
89 void side_events_unregister(struct side_events_register_handle
*handle
);
92 * Userspace tracer registration API. This allows userspace tracers to
93 * register event notification callbacks to be notified of the currently
94 * registered instrumentation, and to register their callbacks to
97 typedef void (*side_tracer_callback_func
)(const struct side_event_description
*desc
,
98 const struct side_arg_vec
*side_arg_vec
,
100 typedef void (*side_tracer_callback_variadic_func
)(const struct side_event_description
*desc
,
101 const struct side_arg_vec
*side_arg_vec
,
102 const struct side_arg_dynamic_struct
*var_struct
,
105 int side_tracer_callback_register(struct side_event_description
*desc
,
106 side_tracer_callback_func call
,
107 void *priv
, void *key
);
108 int side_tracer_callback_variadic_register(struct side_event_description
*desc
,
109 side_tracer_callback_variadic_func call_variadic
,
110 void *priv
, void *key
);
111 int side_tracer_callback_unregister(struct side_event_description
*desc
,
112 side_tracer_callback_func call
,
113 void *priv
, void *key
);
114 int side_tracer_callback_variadic_unregister(struct side_event_description
*desc
,
115 side_tracer_callback_variadic_func call_variadic
,
116 void *priv
, void *key
);
118 enum side_tracer_notification
{
119 SIDE_TRACER_NOTIFICATION_INSERT_EVENTS
,
120 SIDE_TRACER_NOTIFICATION_REMOVE_EVENTS
,
123 /* Callback is invoked with side library internal lock held. */
124 struct side_tracer_handle
*side_tracer_event_notification_register(
125 void (*cb
)(enum side_tracer_notification notif
,
126 struct side_event_description
**events
, uint32_t nr_events
, void *priv
),
128 void side_tracer_event_notification_unregister(struct side_tracer_handle
*handle
);
131 * The side_statedump_call APIs should be used for application/library
133 * The statedump callback dumps application state to tracers by invoking
134 * side_statedump_call APIs.
135 * The statedump callback is invoked with side library internal lock held.
136 * The statedump callback should not invoke functions which require the
137 * side library internal lock.
139 void side_statedump_call(const struct side_event_state
*state
,
140 const struct side_arg_vec
*side_arg_vec
);
141 void side_statedump_call_variadic(const struct side_event_state
*state
,
142 const struct side_arg_vec
*side_arg_vec
,
143 const struct side_arg_dynamic_struct
*var_struct
);
146 * If side_statedump_request_notification_register is invoked from
147 * library constructors and side_statedump_request_notification_unregister
148 * from library destructors, make sure to:
149 * - invoke side_event_description_ptr_init before registration of the
151 * - invoke side_event_description_ptr_exit after unregistration of the
155 struct side_statedump_request_handle
*side_statedump_request_notification_register(void (*statedump_cb
)(void));
156 void side_statedump_request_notification_unregister(struct side_statedump_request_handle
*handle
);
158 void side_tracer_statedump_request(void *key
);
161 * Explicit hooks to initialize/finalize the side instrumentation
162 * library. Those are also library constructor/destructor.
164 void side_init(void) __attribute__((constructor
));
165 void side_exit(void) __attribute__((destructor
));
168 * The following constructors/destructors perform automatic registration
169 * of the declared side events. Those may have to be called explicitly
170 * in a statically linked library.
174 * These weak symbols, the constructor, and destructor take care of
175 * registering only _one_ instance of the side instrumentation per
176 * shared-ojbect (or for the whole main program).
178 extern struct side_event_description
* __start_side_event_description_ptr
[]
179 __attribute__((weak
, visibility("hidden")));
180 extern struct side_event_description
* __stop_side_event_description_ptr
[]
181 __attribute__((weak
, visibility("hidden")));
182 int side_event_description_ptr_registered
183 __attribute__((weak
, visibility("hidden")));
184 struct side_events_register_handle
*side_events_handle
185 __attribute__((weak
, visibility("hidden")));
188 side_event_description_ptr_init(void)
189 __attribute__((no_instrument_function
))
190 __attribute__((constructor
));
192 side_event_description_ptr_init(void)
194 if (side_event_description_ptr_registered
++)
196 side_events_handle
= side_events_register(__start_side_event_description_ptr
,
197 __stop_side_event_description_ptr
- __start_side_event_description_ptr
);
201 side_event_description_ptr_exit(void)
202 __attribute__((no_instrument_function
))
203 __attribute__((destructor
));
205 side_event_description_ptr_exit(void)
207 if (--side_event_description_ptr_registered
)
209 side_events_unregister(side_events_handle
);
210 side_events_handle
= NULL
;
217 #endif /* _SIDE_TRACE_H */