2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
11 #include <lttng/ust-endian.h>
12 #include <usterr-signal-safe.h>
13 #include <urcu/rculist.h>
15 #include "ust-events-internal.h"
16 #include "../libmsgpack/msgpack.h"
17 #include "lttng-bytecode.h"
18 #include "ust-share.h"
21 * We want this write to be atomic AND non-blocking, meaning that we
22 * want to write either everything OR nothing.
23 * According to `pipe(7)`, writes that are less than `PIPE_BUF` bytes must be
24 * atomic, so we bound the capture buffer size to the `PIPE_BUF` minus the size
25 * of the notification struct we are sending alongside the capture buffer.
27 #define CAPTURE_BUFFER_SIZE \
28 (PIPE_BUF - sizeof(struct lttng_ust_abi_event_notifier_notification) - 1)
30 struct lttng_event_notifier_notification
{
32 uint64_t event_notifier_token
;
33 uint8_t capture_buf
[CAPTURE_BUFFER_SIZE
];
34 struct lttng_msgpack_writer writer
;
39 void capture_enum(struct lttng_msgpack_writer
*writer
,
40 struct lttng_interpreter_output
*output
)
42 lttng_msgpack_begin_map(writer
, 2);
43 lttng_msgpack_write_str(writer
, "type");
44 lttng_msgpack_write_str(writer
, "enum");
46 lttng_msgpack_write_str(writer
, "value");
48 switch (output
->type
) {
49 case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM
:
50 lttng_msgpack_write_signed_integer(writer
, output
->u
.s
);
52 case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM
:
53 lttng_msgpack_write_signed_integer(writer
, output
->u
.u
);
59 lttng_msgpack_end_map(writer
);
63 int64_t capture_sequence_element_signed(uint8_t *ptr
,
64 struct lttng_ust_type_integer
*integer_type
)
67 unsigned int size
= integer_type
->size
;
68 bool byte_order_reversed
= integer_type
->reverse_byte_order
;
77 tmp
= *(int16_t *) ptr
;
78 if (byte_order_reversed
)
87 tmp
= *(int32_t *) ptr
;
88 if (byte_order_reversed
)
97 tmp
= *(int64_t *) ptr
;
98 if (byte_order_reversed
)
112 uint64_t capture_sequence_element_unsigned(uint8_t *ptr
,
113 struct lttng_ust_type_integer
*integer_type
)
116 unsigned int size
= integer_type
->size
;
117 bool byte_order_reversed
= integer_type
->reverse_byte_order
;
126 tmp
= *(uint16_t *) ptr
;
127 if (byte_order_reversed
)
136 tmp
= *(uint32_t *) ptr
;
137 if (byte_order_reversed
)
146 tmp
= *(uint64_t *) ptr
;
147 if (byte_order_reversed
)
161 void capture_sequence(struct lttng_msgpack_writer
*writer
,
162 struct lttng_interpreter_output
*output
)
164 struct lttng_ust_type_integer
*integer_type
;
165 struct lttng_ust_type_common
*nested_type
;
170 lttng_msgpack_begin_array(writer
, output
->u
.sequence
.nr_elem
);
172 ptr
= (uint8_t *) output
->u
.sequence
.ptr
;
173 nested_type
= output
->u
.sequence
.nested_type
;
174 switch (nested_type
->type
) {
175 case lttng_ust_type_integer
:
176 integer_type
= lttng_ust_get_type_integer(nested_type
);
178 case lttng_ust_type_enum
:
179 /* Treat enumeration as an integer. */
180 integer_type
= lttng_ust_get_type_integer(lttng_ust_get_type_enum(nested_type
)->container_type
);
183 /* Capture of array of non-integer are not supported. */
186 signedness
= integer_type
->signedness
;
187 for (i
= 0; i
< output
->u
.sequence
.nr_elem
; i
++) {
189 lttng_msgpack_write_signed_integer(writer
,
190 capture_sequence_element_signed(ptr
, integer_type
));
192 lttng_msgpack_write_unsigned_integer(writer
,
193 capture_sequence_element_unsigned(ptr
, integer_type
));
197 * We assume that alignment is smaller or equal to the size.
198 * This currently holds true but if it changes in the future,
199 * we will want to change the pointer arithmetics below to
200 * take into account that the next element might be further
203 assert(integer_type
->alignment
<= integer_type
->size
);
205 /* Size is in number of bits. */
206 ptr
+= (integer_type
->size
/ CHAR_BIT
) ;
209 lttng_msgpack_end_array(writer
);
213 void notification_init(struct lttng_event_notifier_notification
*notif
,
214 struct lttng_ust_event_notifier
*event_notifier
)
216 struct lttng_msgpack_writer
*writer
= ¬if
->writer
;
218 notif
->event_notifier_token
= event_notifier
->priv
->parent
.user_token
;
219 notif
->notification_fd
= event_notifier
->priv
->group
->notification_fd
;
220 notif
->has_captures
= false;
222 if (event_notifier
->priv
->num_captures
> 0) {
223 lttng_msgpack_writer_init(writer
, notif
->capture_buf
,
224 CAPTURE_BUFFER_SIZE
);
226 lttng_msgpack_begin_array(writer
, event_notifier
->priv
->num_captures
);
227 notif
->has_captures
= true;
232 void notification_append_capture(
233 struct lttng_event_notifier_notification
*notif
,
234 struct lttng_interpreter_output
*output
)
236 struct lttng_msgpack_writer
*writer
= ¬if
->writer
;
238 switch (output
->type
) {
239 case LTTNG_INTERPRETER_TYPE_S64
:
240 lttng_msgpack_write_signed_integer(writer
, output
->u
.s
);
242 case LTTNG_INTERPRETER_TYPE_U64
:
243 lttng_msgpack_write_unsigned_integer(writer
, output
->u
.u
);
245 case LTTNG_INTERPRETER_TYPE_DOUBLE
:
246 lttng_msgpack_write_double(writer
, output
->u
.d
);
248 case LTTNG_INTERPRETER_TYPE_STRING
:
249 lttng_msgpack_write_str(writer
, output
->u
.str
.str
);
251 case LTTNG_INTERPRETER_TYPE_SEQUENCE
:
252 capture_sequence(writer
, output
);
254 case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM
:
255 case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM
:
256 capture_enum(writer
, output
);
264 void notification_append_empty_capture(
265 struct lttng_event_notifier_notification
*notif
)
267 lttng_msgpack_write_nil(¬if
->writer
);
270 static void record_error(struct lttng_ust_event_notifier
*event_notifier
)
272 struct lttng_event_notifier_group
*event_notifier_group
=
273 event_notifier
->priv
->group
;
274 struct lttng_counter
*error_counter
;
275 size_t dimension_index
[1];
278 error_counter
= CMM_LOAD_SHARED(event_notifier_group
->error_counter
);
280 * load-acquire paired with store-release orders creation of the
281 * error counter and setting error_counter_len before the
282 * error_counter is used.
283 * Currently a full memory barrier is used, which could be
284 * turned into acquire-release barriers.
287 /* This group may not have an error counter attached to it. */
291 dimension_index
[0] = event_notifier
->priv
->error_counter_index
;
292 ret
= event_notifier_group
->error_counter
->ops
->counter_add(
293 error_counter
->counter
, dimension_index
, 1);
299 void notification_send(struct lttng_event_notifier_notification
*notif
,
300 struct lttng_ust_event_notifier
*event_notifier
)
305 struct lttng_ust_abi_event_notifier_notification ust_notif
= {0};
310 ust_notif
.token
= event_notifier
->priv
->parent
.user_token
;
313 * Prepare sending the notification from multiple buffers using an
314 * array of `struct iovec`. The first buffer of the vector is
315 * notification structure itself and is always present.
317 iov
[0].iov_base
= &ust_notif
;
318 iov
[0].iov_len
= sizeof(ust_notif
);
320 if (notif
->has_captures
) {
322 * If captures were requested, the second buffer of the array
323 * is the capture buffer.
325 assert(notif
->writer
.buffer
);
326 content_len
= notif
->writer
.write_pos
- notif
->writer
.buffer
;
328 assert(content_len
> 0 && content_len
<= CAPTURE_BUFFER_SIZE
);
330 iov
[1].iov_base
= notif
->capture_buf
;
331 iov
[1].iov_len
= content_len
;
339 * Update the capture buffer size so that receiver of the buffer will
340 * know how much to expect.
342 ust_notif
.capture_buf_size
= content_len
;
344 /* Send all the buffers. */
345 ret
= ust_patient_writev(notif
->notification_fd
, iov
, iovec_count
);
347 if (errno
== EAGAIN
) {
348 record_error(event_notifier
);
349 DBG("Cannot send event_notifier notification without blocking: %s",
352 DBG("Error to sending event notifier notification: %s",
359 void lttng_event_notifier_notification_send(
360 struct lttng_ust_event_notifier
*event_notifier
,
361 const char *stack_data
,
362 struct lttng_ust_notification_ctx
*notif_ctx
)
365 * This function is called from the probe, we must do dynamic
366 * allocation in this context.
368 struct lttng_event_notifier_notification notif
= {0};
370 notification_init(¬if
, event_notifier
);
372 if (caa_unlikely(notif_ctx
->eval_capture
)) {
373 struct lttng_ust_bytecode_runtime
*capture_bc_runtime
;
376 * Iterate over all the capture bytecodes. If the interpreter
377 * functions returns successfully, append the value of the
378 * `output` parameter to the capture buffer. If the interpreter
379 * fails, append an empty capture to the buffer.
381 cds_list_for_each_entry_rcu(capture_bc_runtime
,
382 &event_notifier
->priv
->capture_bytecode_runtime_head
, node
) {
383 struct lttng_interpreter_output output
;
385 if (capture_bc_runtime
->interpreter_func(capture_bc_runtime
,
386 stack_data
, &output
) == LTTNG_UST_BYTECODE_INTERPRETER_OK
)
387 notification_append_capture(¬if
, &output
);
389 notification_append_empty_capture(¬if
);
394 * Send the notification (including the capture buffer) to the
397 notification_send(¬if
, event_notifier
);