Commit | Line | Data |
---|---|---|
d8d2416d FD |
1 | /* |
2 | * event-notifier-notification.c | |
3 | * | |
4 | * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com> | |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; only | |
9 | * version 2.1 of the License. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #define _LGPL_SOURCE | |
22 | ||
b9f82a28 FD |
23 | #include <assert.h> |
24 | #include <byteswap.h> | |
d8d2416d FD |
25 | #include <errno.h> |
26 | #include <lttng/ust-events.h> | |
27 | #include <usterr-signal-safe.h> | |
28 | ||
b9f82a28 FD |
29 | #include "../libmsgpack/msgpack.h" |
30 | #include "lttng-bytecode.h" | |
d8d2416d FD |
31 | #include "share.h" |
32 | ||
d37ecb3f FD |
33 | /* |
34 | * We want this write to be atomic AND non-blocking, meaning that we | |
35 | * want to write either everything OR nothing. | |
36 | * According to `pipe(7)`, writes that are less than `PIPE_BUF` bytes must be | |
37 | * atomic, so we bound the capture buffer size to the `PIPE_BUF` minus the size | |
38 | * of the notification struct we are sending alongside the capture buffer. | |
39 | */ | |
40 | #define CAPTURE_BUFFER_SIZE \ | |
41 | (PIPE_BUF - sizeof(struct lttng_ust_event_notifier_notification) - 1) | |
42 | ||
43 | struct lttng_event_notifier_notification { | |
44 | int notification_fd; | |
45 | uint64_t event_notifier_token; | |
46 | uint8_t capture_buf[CAPTURE_BUFFER_SIZE]; | |
47 | struct lttng_msgpack_writer writer; | |
48 | bool has_captures; | |
49 | }; | |
50 | ||
b9f82a28 FD |
51 | static |
52 | void capture_enum(struct lttng_msgpack_writer *writer, | |
53 | struct lttng_interpreter_output *output) | |
54 | { | |
55 | lttng_msgpack_begin_map(writer, 2); | |
56 | lttng_msgpack_write_str(writer, "type"); | |
57 | lttng_msgpack_write_str(writer, "enum"); | |
58 | ||
59 | lttng_msgpack_write_str(writer, "value"); | |
60 | ||
61 | switch (output->type) { | |
62 | case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM: | |
63 | lttng_msgpack_write_signed_integer(writer, output->u.s); | |
64 | break; | |
65 | case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM: | |
66 | lttng_msgpack_write_signed_integer(writer, output->u.u); | |
67 | break; | |
68 | default: | |
69 | abort(); | |
70 | } | |
71 | ||
72 | lttng_msgpack_end_map(writer); | |
73 | } | |
74 | ||
75 | static | |
76 | int64_t capture_sequence_element_signed(uint8_t *ptr, | |
77 | const struct lttng_integer_type *type) | |
78 | { | |
79 | int64_t value; | |
80 | unsigned int size = type->size; | |
81 | bool byte_order_reversed = type->reverse_byte_order; | |
82 | ||
83 | switch (size) { | |
84 | case 8: | |
85 | value = *ptr; | |
86 | break; | |
87 | case 16: | |
88 | { | |
89 | int16_t tmp; | |
90 | tmp = *(int16_t *) ptr; | |
91 | if (byte_order_reversed) | |
92 | tmp = bswap_16(tmp); | |
93 | ||
94 | value = tmp; | |
95 | break; | |
96 | } | |
97 | case 32: | |
98 | { | |
99 | int32_t tmp; | |
100 | tmp = *(int32_t *) ptr; | |
101 | if (byte_order_reversed) | |
102 | tmp = bswap_32(tmp); | |
103 | ||
104 | value = tmp; | |
105 | break; | |
106 | } | |
107 | case 64: | |
108 | { | |
109 | int64_t tmp; | |
110 | tmp = *(int64_t *) ptr; | |
111 | if (byte_order_reversed) | |
112 | tmp = bswap_64(tmp); | |
113 | ||
114 | value = tmp; | |
115 | break; | |
116 | } | |
117 | default: | |
118 | abort(); | |
119 | } | |
120 | ||
121 | return value; | |
122 | } | |
123 | ||
124 | static | |
125 | uint64_t capture_sequence_element_unsigned(uint8_t *ptr, | |
126 | const struct lttng_integer_type *type) | |
127 | { | |
128 | uint64_t value; | |
129 | unsigned int size = type->size; | |
130 | bool byte_order_reversed = type->reverse_byte_order; | |
131 | ||
132 | switch (size) { | |
133 | case 8: | |
134 | value = *ptr; | |
135 | break; | |
136 | case 16: | |
137 | { | |
138 | uint16_t tmp; | |
139 | tmp = *(uint16_t *) ptr; | |
140 | if (byte_order_reversed) | |
141 | tmp = bswap_16(tmp); | |
142 | ||
143 | value = tmp; | |
144 | break; | |
145 | } | |
146 | case 32: | |
147 | { | |
148 | uint32_t tmp; | |
149 | tmp = *(uint32_t *) ptr; | |
150 | if (byte_order_reversed) | |
151 | tmp = bswap_32(tmp); | |
152 | ||
153 | value = tmp; | |
154 | break; | |
155 | } | |
156 | case 64: | |
157 | { | |
158 | uint64_t tmp; | |
159 | tmp = *(uint64_t *) ptr; | |
160 | if (byte_order_reversed) | |
161 | tmp = bswap_64(tmp); | |
162 | ||
163 | value = tmp; | |
164 | break; | |
165 | } | |
166 | default: | |
167 | abort(); | |
168 | } | |
169 | ||
170 | return value; | |
171 | } | |
172 | ||
b9f82a28 FD |
173 | static |
174 | void capture_sequence(struct lttng_msgpack_writer *writer, | |
175 | struct lttng_interpreter_output *output) | |
176 | { | |
177 | const struct lttng_integer_type *integer_type; | |
178 | const struct lttng_type *nested_type; | |
179 | uint8_t *ptr; | |
180 | bool signedness; | |
181 | int i; | |
182 | ||
183 | lttng_msgpack_begin_array(writer, output->u.sequence.nr_elem); | |
184 | ||
185 | ptr = (uint8_t *) output->u.sequence.ptr; | |
186 | nested_type = output->u.sequence.nested_type; | |
187 | switch (nested_type->atype) { | |
188 | case atype_integer: | |
189 | integer_type = &nested_type->u.integer; | |
190 | break; | |
191 | case atype_enum: | |
192 | /* Treat enumeration as an integer. */ | |
193 | integer_type = &nested_type->u.enum_nestable.container_type->u.integer; | |
194 | break; | |
195 | default: | |
196 | /* Capture of array of non-integer are not supported. */ | |
197 | abort(); | |
198 | } | |
199 | signedness = integer_type->signedness; | |
200 | for (i = 0; i < output->u.sequence.nr_elem; i++) { | |
201 | if (signedness) { | |
202 | lttng_msgpack_write_signed_integer(writer, | |
203 | capture_sequence_element_signed(ptr, integer_type)); | |
204 | } else { | |
205 | lttng_msgpack_write_unsigned_integer(writer, | |
206 | capture_sequence_element_unsigned(ptr, integer_type)); | |
207 | } | |
208 | ||
209 | /* | |
210 | * We assume that alignment is smaller or equal to the size. | |
211 | * This currently holds true but if it changes in the future, | |
212 | * we will want to change the pointer arithmetics below to | |
213 | * take into account that the next element might be further | |
214 | * away. | |
215 | */ | |
216 | assert(integer_type->alignment <= integer_type->size); | |
217 | ||
218 | /* Size is in number of bits. */ | |
219 | ptr += (integer_type->size / CHAR_BIT) ; | |
220 | } | |
221 | ||
222 | lttng_msgpack_end_array(writer); | |
223 | } | |
224 | ||
d37ecb3f FD |
225 | static |
226 | void notification_init(struct lttng_event_notifier_notification *notif, | |
d8d2416d FD |
227 | struct lttng_event_notifier *event_notifier) |
228 | { | |
d37ecb3f FD |
229 | struct lttng_msgpack_writer *writer = ¬if->writer; |
230 | ||
231 | notif->event_notifier_token = event_notifier->user_token; | |
232 | notif->notification_fd = event_notifier->group->notification_fd; | |
233 | notif->has_captures = false; | |
234 | ||
235 | if (event_notifier->num_captures > 0) { | |
236 | lttng_msgpack_writer_init(writer, notif->capture_buf, | |
237 | CAPTURE_BUFFER_SIZE); | |
238 | ||
239 | lttng_msgpack_begin_array(writer, event_notifier->num_captures); | |
240 | notif->has_captures = true; | |
241 | } | |
242 | } | |
243 | ||
244 | static | |
245 | void notification_append_capture( | |
246 | struct lttng_event_notifier_notification *notif, | |
247 | struct lttng_interpreter_output *output) | |
248 | { | |
249 | struct lttng_msgpack_writer *writer = ¬if->writer; | |
250 | ||
251 | switch (output->type) { | |
252 | case LTTNG_INTERPRETER_TYPE_S64: | |
253 | lttng_msgpack_write_signed_integer(writer, output->u.s); | |
254 | break; | |
255 | case LTTNG_INTERPRETER_TYPE_U64: | |
256 | lttng_msgpack_write_unsigned_integer(writer, output->u.u); | |
257 | break; | |
258 | case LTTNG_INTERPRETER_TYPE_DOUBLE: | |
259 | lttng_msgpack_write_double(writer, output->u.d); | |
260 | break; | |
261 | case LTTNG_INTERPRETER_TYPE_STRING: | |
262 | lttng_msgpack_write_str(writer, output->u.str.str); | |
263 | break; | |
264 | case LTTNG_INTERPRETER_TYPE_SEQUENCE: | |
265 | capture_sequence(writer, output); | |
266 | break; | |
267 | case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM: | |
268 | case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM: | |
269 | capture_enum(writer, output); | |
270 | break; | |
271 | default: | |
272 | abort(); | |
273 | } | |
274 | } | |
275 | ||
276 | static | |
277 | void notification_append_empty_capture( | |
278 | struct lttng_event_notifier_notification *notif) | |
279 | { | |
280 | lttng_msgpack_write_nil(¬if->writer); | |
281 | } | |
282 | ||
283 | static | |
284 | void notification_send(struct lttng_event_notifier_notification *notif) | |
285 | { | |
286 | ssize_t ret; | |
287 | size_t content_len; | |
288 | int iovec_count = 1; | |
289 | struct lttng_ust_event_notifier_notification ust_notif; | |
290 | struct iovec iov[2]; | |
291 | ||
292 | assert(notif); | |
293 | ||
294 | ust_notif.token = notif->event_notifier_token; | |
295 | ||
d8d2416d | 296 | /* |
d37ecb3f FD |
297 | * Prepare sending the notification from multiple buffers using an |
298 | * array of `struct iovec`. The first buffer of the vector is | |
299 | * notification structure itself and is always present. | |
d8d2416d | 300 | */ |
d37ecb3f FD |
301 | iov[0].iov_base = &ust_notif; |
302 | iov[0].iov_len = sizeof(ust_notif); | |
303 | ||
304 | if (notif->has_captures) { | |
305 | /* | |
306 | * If captures were requested, the second buffer of the array | |
307 | * is the capture buffer. | |
308 | */ | |
309 | assert(notif->writer.buffer); | |
310 | content_len = notif->writer.write_pos - notif->writer.buffer; | |
d8d2416d | 311 | |
d37ecb3f | 312 | assert(content_len > 0 && content_len <= CAPTURE_BUFFER_SIZE); |
d8d2416d | 313 | |
d37ecb3f FD |
314 | iov[1].iov_base = notif->capture_buf; |
315 | iov[1].iov_len = content_len; | |
316 | ||
317 | iovec_count++; | |
318 | } else { | |
319 | content_len = 0; | |
320 | } | |
321 | ||
322 | /* | |
323 | * Update the capture buffer size so that receiver of the buffer will | |
324 | * know how much to expect. | |
325 | */ | |
326 | ust_notif.capture_buf_size = content_len; | |
d8d2416d | 327 | |
d37ecb3f FD |
328 | /* Send all the buffers. */ |
329 | ret = patient_writev(notif->notification_fd, iov, iovec_count); | |
d8d2416d FD |
330 | if (ret == -1) { |
331 | if (errno == EAGAIN) { | |
332 | DBG("Cannot send event notifier notification without blocking: %s", | |
333 | strerror(errno)); | |
334 | } else { | |
335 | DBG("Error to sending event notifier notification: %s", | |
336 | strerror(errno)); | |
337 | abort(); | |
338 | } | |
339 | } | |
340 | } | |
d37ecb3f FD |
341 | |
342 | void lttng_event_notifier_notification_send(struct lttng_event_notifier *event_notifier, | |
343 | const char *stack_data) | |
344 | { | |
345 | /* | |
346 | * This function is called from the probe, we must do dynamic | |
347 | * allocation in this context. | |
348 | */ | |
349 | struct lttng_event_notifier_notification notif = {0}; | |
350 | ||
351 | notification_init(¬if, event_notifier); | |
352 | ||
353 | if (caa_unlikely(!cds_list_empty(&event_notifier->capture_bytecode_runtime_head))) { | |
354 | struct lttng_bytecode_runtime *capture_bc_runtime; | |
355 | ||
356 | /* | |
357 | * Iterate over all the capture bytecodes. If the interpreter | |
358 | * functions returns successfully, append the value of the | |
359 | * `output` parameter to the capture buffer. If the interpreter | |
360 | * fails, append an empty capture to the buffer. | |
361 | */ | |
362 | cds_list_for_each_entry(capture_bc_runtime, | |
363 | &event_notifier->capture_bytecode_runtime_head, node) { | |
364 | struct lttng_interpreter_output output; | |
365 | ||
366 | if (capture_bc_runtime->interpreter_funcs.capture(capture_bc_runtime, | |
367 | stack_data, &output) & LTTNG_INTERPRETER_RECORD_FLAG) | |
368 | notification_append_capture(¬if, &output); | |
369 | else | |
370 | notification_append_empty_capture(¬if); | |
371 | } | |
372 | } | |
373 | ||
374 | /* | |
375 | * Send the notification (including the capture buffer) to the | |
376 | * sessiond. | |
377 | */ | |
378 | notification_send(¬if); | |
379 | } |