Fix: event notification capture error handling
[lttng-ust.git] / src / lib / lttng-ust / event-notifier-notification.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
5 */
6
7 #define _LGPL_SOURCE
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <limits.h>
12
13 #include <lttng/ust-endian.h>
14 #include "common/logging.h"
15 #include <urcu/rculist.h>
16
17 #include "lttng-tracer-core.h"
18 #include "lib/lttng-ust/events.h"
19 #include "common/msgpack/msgpack.h"
20 #include "lttng-bytecode.h"
21 #include "common/patient.h"
22
23 /*
24 * We want this write to be atomic AND non-blocking, meaning that we
25 * want to write either everything OR nothing.
26 * According to `pipe(7)`, writes that are less than `PIPE_BUF` bytes must be
27 * atomic, so we bound the capture buffer size to the `PIPE_BUF` minus the size
28 * of the notification struct we are sending alongside the capture buffer.
29 */
30 #define CAPTURE_BUFFER_SIZE \
31 (PIPE_BUF - sizeof(struct lttng_ust_abi_event_notifier_notification) - 1)
32
33 struct lttng_event_notifier_notification {
34 int notification_fd;
35 uint64_t event_notifier_token;
36 uint8_t capture_buf[CAPTURE_BUFFER_SIZE];
37 struct lttng_msgpack_writer writer;
38 bool has_captures;
39 };
40
41 static
42 int capture_enum(struct lttng_msgpack_writer *writer,
43 struct lttng_interpreter_output *output)
44 {
45 int ret;
46
47 ret = lttng_msgpack_begin_map(writer, 2);
48 if (ret) {
49 goto end;
50 }
51 ret = lttng_msgpack_write_str(writer, "type");
52 if (ret) {
53 goto end;
54 }
55 ret = lttng_msgpack_write_str(writer, "enum");
56 if (ret) {
57 goto end;
58 }
59 ret = lttng_msgpack_write_str(writer, "value");
60 if (ret) {
61 goto end;
62 }
63
64 switch (output->type) {
65 case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM:
66 ret = lttng_msgpack_write_signed_integer(writer, output->u.s);
67 if (ret) {
68 goto end;
69 }
70 break;
71 case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM:
72 ret = lttng_msgpack_write_signed_integer(writer, output->u.u);
73 if (ret) {
74 goto end;
75 }
76 break;
77 default:
78 CRIT("Unknown enum output type\n");
79 ret = -1;
80 goto end;
81 }
82
83 ret = lttng_msgpack_end_map(writer);
84 end:
85 return ret;
86 }
87
88 static
89 int64_t capture_sequence_element_signed(uint8_t *ptr,
90 const struct lttng_ust_type_integer *integer_type)
91 {
92 int64_t value;
93 unsigned int size = integer_type->size;
94 bool byte_order_reversed = integer_type->reverse_byte_order;
95
96 switch (size) {
97 case 8:
98 value = *ptr;
99 break;
100 case 16:
101 {
102 int16_t tmp;
103 tmp = *(int16_t *) ptr;
104 if (byte_order_reversed)
105 tmp = lttng_ust_bswap_16(tmp);
106
107 value = tmp;
108 break;
109 }
110 case 32:
111 {
112 int32_t tmp;
113 tmp = *(int32_t *) ptr;
114 if (byte_order_reversed)
115 tmp = lttng_ust_bswap_32(tmp);
116
117 value = tmp;
118 break;
119 }
120 case 64:
121 {
122 int64_t tmp;
123 tmp = *(int64_t *) ptr;
124 if (byte_order_reversed)
125 tmp = lttng_ust_bswap_64(tmp);
126
127 value = tmp;
128 break;
129 }
130 default:
131 CRIT("Unknown sequence element size\n");
132 value = 0;
133 }
134
135 return value;
136 }
137
138 static
139 uint64_t capture_sequence_element_unsigned(uint8_t *ptr,
140 const struct lttng_ust_type_integer *integer_type)
141 {
142 uint64_t value;
143 unsigned int size = integer_type->size;
144 bool byte_order_reversed = integer_type->reverse_byte_order;
145
146 switch (size) {
147 case 8:
148 value = *ptr;
149 break;
150 case 16:
151 {
152 uint16_t tmp;
153 tmp = *(uint16_t *) ptr;
154 if (byte_order_reversed)
155 tmp = lttng_ust_bswap_16(tmp);
156
157 value = tmp;
158 break;
159 }
160 case 32:
161 {
162 uint32_t tmp;
163 tmp = *(uint32_t *) ptr;
164 if (byte_order_reversed)
165 tmp = lttng_ust_bswap_32(tmp);
166
167 value = tmp;
168 break;
169 }
170 case 64:
171 {
172 uint64_t tmp;
173 tmp = *(uint64_t *) ptr;
174 if (byte_order_reversed)
175 tmp = lttng_ust_bswap_64(tmp);
176
177 value = tmp;
178 break;
179 }
180 default:
181 CRIT("Unknown sequence element size\n");
182 value = 0;
183 }
184
185 return value;
186 }
187
188 static
189 int capture_sequence(struct lttng_msgpack_writer *writer,
190 struct lttng_interpreter_output *output)
191 {
192 const struct lttng_ust_type_integer *integer_type;
193 const struct lttng_ust_type_common *nested_type;
194 uint8_t *ptr;
195 bool signedness;
196 int i, ret;
197
198 ret = lttng_msgpack_begin_array(writer, output->u.sequence.nr_elem);
199 if (ret) {
200 goto end;
201 }
202 ptr = (uint8_t *) output->u.sequence.ptr;
203 nested_type = output->u.sequence.nested_type;
204 switch (nested_type->type) {
205 case lttng_ust_type_integer:
206 integer_type = lttng_ust_get_type_integer(nested_type);
207 break;
208 case lttng_ust_type_enum:
209 /* Treat enumeration as an integer. */
210 integer_type = lttng_ust_get_type_integer(lttng_ust_get_type_enum(nested_type)->container_type);
211 break;
212 default:
213 CRIT("Capture of array of non-integer are not supported\n");
214 ret = -1;
215 goto end;
216 }
217 signedness = integer_type->signedness;
218 for (i = 0; i < output->u.sequence.nr_elem; i++) {
219 if (signedness) {
220 ret = lttng_msgpack_write_signed_integer(writer,
221 capture_sequence_element_signed(ptr, integer_type));
222 } else {
223 ret = lttng_msgpack_write_unsigned_integer(writer,
224 capture_sequence_element_unsigned(ptr, integer_type));
225 }
226 if (ret) {
227 goto end;
228 }
229
230 /*
231 * We assume that alignment is smaller or equal to the size.
232 * This currently holds true but if it changes in the future,
233 * we will want to change the pointer arithmetic below to
234 * take into account that the next element might be further
235 * away.
236 */
237 assert(integer_type->alignment <= integer_type->size);
238
239 /* Size is in number of bits. */
240 ptr += (integer_type->size / CHAR_BIT) ;
241 }
242
243 ret = lttng_msgpack_end_array(writer);
244 end:
245 return ret;
246 }
247
248 static
249 int notification_init(struct lttng_event_notifier_notification *notif,
250 const struct lttng_ust_event_notifier *event_notifier)
251 {
252 struct lttng_msgpack_writer *writer = &notif->writer;
253 int ret = 0;
254
255 notif->event_notifier_token = event_notifier->priv->parent.user_token;
256 notif->notification_fd = event_notifier->priv->group->notification_fd;
257 notif->has_captures = false;
258
259 if (event_notifier->priv->num_captures > 0) {
260 lttng_msgpack_writer_init(writer, notif->capture_buf,
261 CAPTURE_BUFFER_SIZE);
262
263 ret = lttng_msgpack_begin_array(writer, event_notifier->priv->num_captures);
264 if (ret) {
265 goto end;
266 }
267 notif->has_captures = true;
268 }
269 end:
270 return ret;
271 }
272
273 static
274 int notification_append_capture(
275 struct lttng_event_notifier_notification *notif,
276 struct lttng_interpreter_output *output)
277 {
278 struct lttng_msgpack_writer *writer = &notif->writer;
279 int ret;
280
281 switch (output->type) {
282 case LTTNG_INTERPRETER_TYPE_S64:
283 ret = lttng_msgpack_write_signed_integer(writer, output->u.s);
284 break;
285 case LTTNG_INTERPRETER_TYPE_U64:
286 ret = lttng_msgpack_write_unsigned_integer(writer, output->u.u);
287 break;
288 case LTTNG_INTERPRETER_TYPE_DOUBLE:
289 ret = lttng_msgpack_write_double(writer, output->u.d);
290 break;
291 case LTTNG_INTERPRETER_TYPE_STRING:
292 ret = lttng_msgpack_write_str(writer, output->u.str.str);
293 break;
294 case LTTNG_INTERPRETER_TYPE_SEQUENCE:
295 ret = capture_sequence(writer, output);
296 break;
297 case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM:
298 case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM:
299 ret = capture_enum(writer, output);
300 break;
301 default:
302 CRIT("Unknown capture output type\n");
303 ret = -1;
304 }
305 return ret;
306 }
307
308 static
309 int notification_append_empty_capture(
310 struct lttng_event_notifier_notification *notif)
311 {
312 return lttng_msgpack_write_nil(&notif->writer);
313 }
314
315 static void record_error(const struct lttng_ust_event_notifier *event_notifier)
316 {
317 struct lttng_event_notifier_group *event_notifier_group =
318 event_notifier->priv->group;
319 struct lttng_counter *error_counter;
320 size_t dimension_index[1];
321 int ret;
322
323 error_counter = CMM_LOAD_SHARED(event_notifier_group->error_counter);
324 /*
325 * load-acquire paired with store-release orders creation of the
326 * error counter and setting error_counter_len before the
327 * error_counter is used.
328 * Currently a full memory barrier is used, which could be
329 * turned into acquire-release barriers.
330 */
331 cmm_smp_mb();
332 /* This group may not have an error counter attached to it. */
333 if (!error_counter)
334 return;
335
336 dimension_index[0] = event_notifier->priv->error_counter_index;
337 ret = event_notifier_group->error_counter->ops->counter_add(
338 error_counter->counter, dimension_index, 1);
339 if (ret)
340 WARN_ON_ONCE(1);
341 }
342
343 static
344 void notification_send(struct lttng_event_notifier_notification *notif,
345 const struct lttng_ust_event_notifier *event_notifier)
346 {
347 ssize_t ret;
348 size_t content_len;
349 int iovec_count = 1;
350 struct lttng_ust_abi_event_notifier_notification ust_notif = {0};
351 struct iovec iov[2];
352
353 assert(notif);
354
355 ust_notif.token = event_notifier->priv->parent.user_token;
356
357 /*
358 * Prepare sending the notification from multiple buffers using an
359 * array of `struct iovec`. The first buffer of the vector is
360 * notification structure itself and is always present.
361 */
362 iov[0].iov_base = &ust_notif;
363 iov[0].iov_len = sizeof(ust_notif);
364
365 if (notif->has_captures) {
366 /*
367 * If captures were requested, the second buffer of the array
368 * is the capture buffer.
369 */
370 assert(notif->writer.buffer);
371 content_len = notif->writer.write_pos - notif->writer.buffer;
372
373 assert(content_len > 0 && content_len <= CAPTURE_BUFFER_SIZE);
374
375 iov[1].iov_base = notif->capture_buf;
376 iov[1].iov_len = content_len;
377
378 iovec_count++;
379 } else {
380 content_len = 0;
381 }
382
383 /*
384 * Update the capture buffer size so that receiver of the buffer will
385 * know how much to expect.
386 */
387 ust_notif.capture_buf_size = content_len;
388
389 /* Send all the buffers. */
390 ret = ust_patient_writev(notif->notification_fd, iov, iovec_count);
391 if (ret == -1) {
392 if (errno == EAGAIN) {
393 record_error(event_notifier);
394 DBG("Cannot send event_notifier notification without blocking: %s",
395 strerror(errno));
396 } else {
397 DBG("Error to sending event notifier notification: %s",
398 strerror(errno));
399 abort();
400 }
401 }
402 }
403
404 void lttng_event_notifier_notification_send(
405 const struct lttng_ust_event_notifier *event_notifier,
406 const char *stack_data,
407 struct lttng_ust_probe_ctx *probe_ctx,
408 struct lttng_ust_notification_ctx *notif_ctx)
409 {
410 /*
411 * This function is called from the probe, we must do dynamic
412 * allocation in this context.
413 */
414 struct lttng_event_notifier_notification notif = {0};
415
416 if (notification_init(&notif, event_notifier)) {
417 record_error(event_notifier);
418 goto end;
419 }
420
421 if (caa_unlikely(notif_ctx->eval_capture)) {
422 struct lttng_ust_bytecode_runtime *capture_bc_runtime;
423
424 /*
425 * Iterate over all the capture bytecodes. If the interpreter
426 * functions returns successfully, append the value of the
427 * `output` parameter to the capture buffer. If the interpreter
428 * fails, append an empty capture to the buffer.
429 */
430 cds_list_for_each_entry_rcu(capture_bc_runtime,
431 &event_notifier->priv->capture_bytecode_runtime_head, node) {
432 struct lttng_interpreter_output output;
433 uint8_t *save_pos;
434 int ret;
435
436 lttng_msgpack_save_writer_pos(&notif.writer, &save_pos);
437 if (capture_bc_runtime->interpreter_func(capture_bc_runtime,
438 stack_data, probe_ctx, &output) == LTTNG_UST_BYTECODE_INTERPRETER_OK)
439 ret = notification_append_capture(&notif, &output);
440 else
441 ret = notification_append_empty_capture(&notif);
442 if (ret) {
443 /*
444 * On append capture error, skip the field
445 * capture by restoring the msgpack writer
446 * position.
447 */
448 lttng_msgpack_restore_writer_pos(&notif.writer, save_pos);
449 }
450 }
451 }
452
453 /*
454 * Send the notification (including the capture buffer) to the
455 * sessiond.
456 */
457 notification_send(&notif, event_notifier);
458 end:
459 return;
460 }
This page took 0.039034 seconds and 4 git commands to generate.