2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
13 #include <lttng/ust-endian.h>
14 #include "common/logging.h"
15 #include <urcu/rculist.h>
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"
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.
30 #define CAPTURE_BUFFER_SIZE \
31 (PIPE_BUF - sizeof(struct lttng_ust_abi_event_notifier_notification) - 1)
33 struct lttng_event_notifier_notification
{
35 uint64_t event_notifier_token
;
36 uint8_t capture_buf
[CAPTURE_BUFFER_SIZE
];
37 struct lttng_msgpack_writer writer
;
42 int capture_enum(struct lttng_msgpack_writer
*writer
,
43 struct lttng_interpreter_output
*output
)
47 ret
= lttng_msgpack_begin_map(writer
, 2);
51 ret
= lttng_msgpack_write_str(writer
, "type");
55 ret
= lttng_msgpack_write_str(writer
, "enum");
59 ret
= lttng_msgpack_write_str(writer
, "value");
64 switch (output
->type
) {
65 case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM
:
66 ret
= lttng_msgpack_write_signed_integer(writer
, output
->u
.s
);
71 case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM
:
72 ret
= lttng_msgpack_write_signed_integer(writer
, output
->u
.u
);
78 CRIT("Unknown enum output type\n");
83 ret
= lttng_msgpack_end_map(writer
);
89 int64_t capture_sequence_element_signed(uint8_t *ptr
,
90 const struct lttng_ust_type_integer
*integer_type
)
93 unsigned int size
= integer_type
->size
;
94 bool byte_order_reversed
= integer_type
->reverse_byte_order
;
103 tmp
= *(int16_t *) ptr
;
104 if (byte_order_reversed
)
105 tmp
= lttng_ust_bswap_16(tmp
);
113 tmp
= *(int32_t *) ptr
;
114 if (byte_order_reversed
)
115 tmp
= lttng_ust_bswap_32(tmp
);
123 tmp
= *(int64_t *) ptr
;
124 if (byte_order_reversed
)
125 tmp
= lttng_ust_bswap_64(tmp
);
131 CRIT("Unknown sequence element size\n");
139 uint64_t capture_sequence_element_unsigned(uint8_t *ptr
,
140 const struct lttng_ust_type_integer
*integer_type
)
143 unsigned int size
= integer_type
->size
;
144 bool byte_order_reversed
= integer_type
->reverse_byte_order
;
153 tmp
= *(uint16_t *) ptr
;
154 if (byte_order_reversed
)
155 tmp
= lttng_ust_bswap_16(tmp
);
163 tmp
= *(uint32_t *) ptr
;
164 if (byte_order_reversed
)
165 tmp
= lttng_ust_bswap_32(tmp
);
173 tmp
= *(uint64_t *) ptr
;
174 if (byte_order_reversed
)
175 tmp
= lttng_ust_bswap_64(tmp
);
181 CRIT("Unknown sequence element size\n");
189 int capture_sequence(struct lttng_msgpack_writer
*writer
,
190 struct lttng_interpreter_output
*output
)
192 const struct lttng_ust_type_integer
*integer_type
;
193 const struct lttng_ust_type_common
*nested_type
;
198 ret
= lttng_msgpack_begin_array(writer
, output
->u
.sequence
.nr_elem
);
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
);
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
);
213 CRIT("Capture of array of non-integer are not supported\n");
217 signedness
= integer_type
->signedness
;
218 for (i
= 0; i
< output
->u
.sequence
.nr_elem
; i
++) {
220 ret
= lttng_msgpack_write_signed_integer(writer
,
221 capture_sequence_element_signed(ptr
, integer_type
));
223 ret
= lttng_msgpack_write_unsigned_integer(writer
,
224 capture_sequence_element_unsigned(ptr
, integer_type
));
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 arithmetics below to
234 * take into account that the next element might be further
237 assert(integer_type
->alignment
<= integer_type
->size
);
239 /* Size is in number of bits. */
240 ptr
+= (integer_type
->size
/ CHAR_BIT
) ;
243 ret
= lttng_msgpack_end_array(writer
);
249 int notification_init(struct lttng_event_notifier_notification
*notif
,
250 const struct lttng_ust_event_notifier
*event_notifier
)
252 struct lttng_msgpack_writer
*writer
= ¬if
->writer
;
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;
259 if (event_notifier
->priv
->num_captures
> 0) {
260 lttng_msgpack_writer_init(writer
, notif
->capture_buf
,
261 CAPTURE_BUFFER_SIZE
);
263 ret
= lttng_msgpack_begin_array(writer
, event_notifier
->priv
->num_captures
);
267 notif
->has_captures
= true;
274 int notification_append_capture(
275 struct lttng_event_notifier_notification
*notif
,
276 struct lttng_interpreter_output
*output
)
278 struct lttng_msgpack_writer
*writer
= ¬if
->writer
;
281 switch (output
->type
) {
282 case LTTNG_INTERPRETER_TYPE_S64
:
283 ret
= lttng_msgpack_write_signed_integer(writer
, output
->u
.s
);
285 case LTTNG_INTERPRETER_TYPE_U64
:
286 ret
= lttng_msgpack_write_unsigned_integer(writer
, output
->u
.u
);
288 case LTTNG_INTERPRETER_TYPE_DOUBLE
:
289 ret
= lttng_msgpack_write_double(writer
, output
->u
.d
);
291 case LTTNG_INTERPRETER_TYPE_STRING
:
292 ret
= lttng_msgpack_write_str(writer
, output
->u
.str
.str
);
294 case LTTNG_INTERPRETER_TYPE_SEQUENCE
:
295 ret
= capture_sequence(writer
, output
);
297 case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM
:
298 case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM
:
299 ret
= capture_enum(writer
, output
);
302 CRIT("Unknown capture output type\n");
309 int notification_append_empty_capture(
310 struct lttng_event_notifier_notification
*notif
)
312 return lttng_msgpack_write_nil(¬if
->writer
);
315 static void record_error(const struct lttng_ust_event_notifier
*event_notifier
)
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];
323 error_counter
= CMM_LOAD_SHARED(event_notifier_group
->error_counter
);
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.
332 /* This group may not have an error counter attached to it. */
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);
344 void notification_send(struct lttng_event_notifier_notification
*notif
,
345 const struct lttng_ust_event_notifier
*event_notifier
)
350 struct lttng_ust_abi_event_notifier_notification ust_notif
= {0};
355 ust_notif
.token
= event_notifier
->priv
->parent
.user_token
;
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.
362 iov
[0].iov_base
= &ust_notif
;
363 iov
[0].iov_len
= sizeof(ust_notif
);
365 if (notif
->has_captures
) {
367 * If captures were requested, the second buffer of the array
368 * is the capture buffer.
370 assert(notif
->writer
.buffer
);
371 content_len
= notif
->writer
.write_pos
- notif
->writer
.buffer
;
373 assert(content_len
> 0 && content_len
<= CAPTURE_BUFFER_SIZE
);
375 iov
[1].iov_base
= notif
->capture_buf
;
376 iov
[1].iov_len
= content_len
;
384 * Update the capture buffer size so that receiver of the buffer will
385 * know how much to expect.
387 ust_notif
.capture_buf_size
= content_len
;
389 /* Send all the buffers. */
390 ret
= ust_patient_writev(notif
->notification_fd
, iov
, iovec_count
);
392 if (errno
== EAGAIN
) {
393 record_error(event_notifier
);
394 DBG("Cannot send event_notifier notification without blocking: %s",
397 DBG("Error to sending event notifier notification: %s",
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
)
411 * This function is called from the probe, we must do dynamic
412 * allocation in this context.
414 struct lttng_event_notifier_notification notif
= {0};
416 if (notification_init(¬if
, event_notifier
)) {
417 record_error(event_notifier
);
421 if (caa_unlikely(notif_ctx
->eval_capture
)) {
422 struct lttng_ust_bytecode_runtime
*capture_bc_runtime
;
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.
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
;
436 lttng_msgpack_save_writer_pos(¬if
.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(¬if
, &output
);
441 ret
= notification_append_empty_capture(¬if
);
444 * On append capture error, skip the field
445 * capture by restoring the msgpack writer
448 lttng_msgpack_restore_writer_pos(¬if
.writer
, save_pos
);
454 * Send the notification (including the capture buffer) to the
457 notification_send(¬if
, event_notifier
);