5df637fb1634c6558c55169fef32b62e36f93e74
[libside.git] / include / side / trace.h
1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 */
5
6 #ifndef _SIDE_TRACE_H
7 #define _SIDE_TRACE_H
8
9 #include <stdint.h>
10 #include <inttypes.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <math.h>
14 #include <stdbool.h>
15 #include <stddef.h>
16 #include <side/macros.h>
17 #include <side/endian.h>
18
19 /*
20 * SIDE stands for "Software Instrumentation Dynamically Enabled"
21 *
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.
25 *
26 * The extensibility scheme for the SIDE ABI for event state is as
27 * follows:
28 *
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".
35 */
36
37 #define SIDE_EVENT_STATE_ABI_VERSION 0
38
39 #include <side/abi/event-description.h>
40 #include <side/abi/type-argument.h>
41 #include <side/instrumentation-c-api.h>
42
43 enum side_error {
44 SIDE_ERROR_OK = 0,
45 SIDE_ERROR_INVAL = 1,
46 SIDE_ERROR_EXIST = 2,
47 SIDE_ERROR_NOMEM = 3,
48 SIDE_ERROR_NOENT = 4,
49 SIDE_ERROR_EXITING = 5,
50 };
51
52 /*
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.
57 */
58
59 struct side_event_state {
60 uint32_t version; /* Event state ABI version. */
61 };
62
63 struct side_event_state_0 {
64 struct side_event_state parent; /* Required first field. */
65 uint32_t nr_callbacks;
66 uintptr_t enabled;
67 const struct side_callback *callbacks;
68 struct side_event_description *desc;
69 };
70
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74
75 struct side_callback;
76
77 extern const char side_empty_callback[];
78
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
85 struct side_events_register_handle *side_events_register(struct side_event_description **events,
86 uint32_t nr_events);
87 void side_events_unregister(struct side_events_register_handle *handle);
88
89 /*
90 * Userspace tracer registration API. This allows userspace tracers to
91 * register event notification callbacks to be notified of the currently
92 * registered instrumentation, and to register their callbacks to
93 * specific events.
94 */
95 typedef void (*side_tracer_callback_func)(const struct side_event_description *desc,
96 const struct side_arg_vec *side_arg_vec,
97 void *priv);
98 typedef void (*side_tracer_callback_variadic_func)(const struct side_event_description *desc,
99 const struct side_arg_vec *side_arg_vec,
100 const struct side_arg_dynamic_struct *var_struct,
101 void *priv);
102
103 int side_tracer_callback_register(struct side_event_description *desc,
104 side_tracer_callback_func call,
105 void *priv);
106 int side_tracer_callback_variadic_register(struct side_event_description *desc,
107 side_tracer_callback_variadic_func call_variadic,
108 void *priv);
109 int side_tracer_callback_unregister(struct side_event_description *desc,
110 side_tracer_callback_func call,
111 void *priv);
112 int side_tracer_callback_variadic_unregister(struct side_event_description *desc,
113 side_tracer_callback_variadic_func call_variadic,
114 void *priv);
115
116 enum side_tracer_notification {
117 SIDE_TRACER_NOTIFICATION_INSERT_EVENTS,
118 SIDE_TRACER_NOTIFICATION_REMOVE_EVENTS,
119 };
120
121 /* Callback is invoked with side library internal lock held. */
122 struct side_tracer_handle *side_tracer_event_notification_register(
123 void (*cb)(enum side_tracer_notification notif,
124 struct side_event_description **events, uint32_t nr_events, void *priv),
125 void *priv);
126 void side_tracer_event_notification_unregister(struct side_tracer_handle *handle);
127
128 /*
129 * Explicit hooks to initialize/finalize the side instrumentation
130 * library. Those are also library constructor/destructor.
131 */
132 void side_init(void) __attribute__((constructor));
133 void side_exit(void) __attribute__((destructor));
134
135 /*
136 * The following constructors/destructors perform automatic registration
137 * of the declared side events. Those may have to be called explicitly
138 * in a statically linked library.
139 */
140
141 /*
142 * These weak symbols, the constructor, and destructor take care of
143 * registering only _one_ instance of the side instrumentation per
144 * shared-ojbect (or for the whole main program).
145 */
146 extern struct side_event_description * __start_side_event_description_ptr[]
147 __attribute__((weak, visibility("hidden")));
148 extern struct side_event_description * __stop_side_event_description_ptr[]
149 __attribute__((weak, visibility("hidden")));
150 int side_event_description_ptr_registered
151 __attribute__((weak, visibility("hidden")));
152 struct side_events_register_handle *side_events_handle
153 __attribute__((weak, visibility("hidden")));
154
155 static void
156 side_event_description_ptr_init(void)
157 __attribute__((no_instrument_function))
158 __attribute__((constructor));
159 static void
160 side_event_description_ptr_init(void)
161 {
162 if (side_event_description_ptr_registered++)
163 return;
164 side_events_handle = side_events_register(__start_side_event_description_ptr,
165 __stop_side_event_description_ptr - __start_side_event_description_ptr);
166 }
167
168 static void
169 side_event_description_ptr_exit(void)
170 __attribute__((no_instrument_function))
171 __attribute__((destructor));
172 static void
173 side_event_description_ptr_exit(void)
174 {
175 if (--side_event_description_ptr_registered)
176 return;
177 side_events_unregister(side_events_handle);
178 side_events_handle = NULL;
179 }
180
181 #ifdef __cplusplus
182 }
183 #endif
184
185 #endif /* _SIDE_TRACE_H */
This page took 0.032723 seconds and 3 git commands to generate.