2 * trigger-notification.c
4 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
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.
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.
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
27 #include <lttng/ust-events.h>
28 #include <usterr-signal-safe.h>
30 #include "../libmsgpack/msgpack.h"
31 #include "lttng-bytecode.h"
35 * We want this write to be atomic AND non-blocking, meaning that we
36 * want to write either everything OR nothing.
37 * According to `pipe(7)`, writes that are less than `PIPE_BUF` bytes must be
38 * atomic, so we bound the capture buffer size to the `PIPE_BUF` minus the size
39 * of the notification struct we are sending alongside the capture buffer.
41 #define CAPTURE_BUFFER_SIZE \
42 (PIPE_BUF - sizeof(struct lttng_ust_trigger_notification) - 1)
44 struct lttng_trigger_notification
{
47 uint8_t capture_buf
[CAPTURE_BUFFER_SIZE
];
48 struct lttng_msgpack_writer writer
;
53 void capture_enum(struct lttng_msgpack_writer
*writer
,
54 struct lttng_interpreter_output
*output
)
56 lttng_msgpack_begin_map(writer
, 2);
57 lttng_msgpack_write_str(writer
, "type");
58 lttng_msgpack_write_str(writer
, "enum");
60 lttng_msgpack_write_str(writer
, "value");
62 switch (output
->type
) {
63 case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM
:
64 lttng_msgpack_write_signed_integer(writer
, output
->u
.s
);
66 case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM
:
67 lttng_msgpack_write_signed_integer(writer
, output
->u
.u
);
73 lttng_msgpack_end_map(writer
);
77 int64_t capture_sequence_element_signed(uint8_t *ptr
,
78 const struct lttng_integer_type
*type
)
81 unsigned int size
= type
->size
;
82 bool byte_order_reversed
= type
->reverse_byte_order
;
91 tmp
= *(int16_t *) ptr
;
92 if (byte_order_reversed
)
101 tmp
= *(int32_t *) ptr
;
102 if (byte_order_reversed
)
111 tmp
= *(int64_t *) ptr
;
112 if (byte_order_reversed
)
126 uint64_t capture_sequence_element_unsigned(uint8_t *ptr
,
127 const struct lttng_integer_type
*type
)
130 unsigned int size
= type
->size
;
131 bool byte_order_reversed
= type
->reverse_byte_order
;
140 tmp
= *(uint16_t *) ptr
;
141 if (byte_order_reversed
)
150 tmp
= *(uint32_t *) ptr
;
151 if (byte_order_reversed
)
160 tmp
= *(uint64_t *) ptr
;
161 if (byte_order_reversed
)
175 void capture_sequence(struct lttng_msgpack_writer
*writer
,
176 struct lttng_interpreter_output
*output
)
178 const struct lttng_integer_type
*integer_type
;
179 const struct lttng_type
*nested_type
;
184 lttng_msgpack_begin_array(writer
, output
->u
.sequence
.nr_elem
);
186 ptr
= (uint8_t *) output
->u
.sequence
.ptr
;
187 nested_type
= output
->u
.sequence
.nested_type
;
188 switch (nested_type
->atype
) {
190 integer_type
= &nested_type
->u
.integer
;
193 /* Treat enumeration as an integer. */
194 integer_type
= &nested_type
->u
.enum_nestable
.container_type
->u
.integer
;
197 /* Capture of array of non-integer are not supported. */
200 signedness
= integer_type
->signedness
;
201 for (i
= 0; i
< output
->u
.sequence
.nr_elem
; i
++) {
203 lttng_msgpack_write_signed_integer(writer
,
204 capture_sequence_element_signed(ptr
, integer_type
));
206 lttng_msgpack_write_unsigned_integer(writer
,
207 capture_sequence_element_unsigned(ptr
, integer_type
));
211 * We assume that alignment is smaller or equal to the size.
212 * This currently holds true but if it changes in the future,
213 * we will want to change the pointer arithmetics below to
214 * take into account that the next element might be further
217 assert(integer_type
->alignment
<= integer_type
->size
);
219 /* Size is in number of bits. */
220 ptr
+= (integer_type
->size
/ CHAR_BIT
) ;
223 lttng_msgpack_end_array(writer
);
227 void notification_init(struct lttng_trigger_notification
*notif
,
228 struct lttng_trigger
*trigger
)
230 struct lttng_msgpack_writer
*writer
= ¬if
->writer
;
232 notif
->trigger_id
= trigger
->id
;
233 notif
->notification_fd
= trigger
->group
->notification_fd
;
234 notif
->has_captures
= false;
236 if (trigger
->num_captures
> 0) {
237 lttng_msgpack_writer_init(writer
, notif
->capture_buf
,
238 CAPTURE_BUFFER_SIZE
);
240 lttng_msgpack_begin_array(writer
, trigger
->num_captures
);
241 notif
->has_captures
= true;
246 void notification_append_capture(
247 struct lttng_trigger_notification
*notif
,
248 struct lttng_interpreter_output
*output
)
250 struct lttng_msgpack_writer
*writer
= ¬if
->writer
;
252 switch (output
->type
) {
253 case LTTNG_INTERPRETER_TYPE_S64
:
254 lttng_msgpack_write_signed_integer(writer
, output
->u
.s
);
256 case LTTNG_INTERPRETER_TYPE_U64
:
257 lttng_msgpack_write_unsigned_integer(writer
, output
->u
.u
);
259 case LTTNG_INTERPRETER_TYPE_DOUBLE
:
260 lttng_msgpack_write_double(writer
, output
->u
.d
);
262 case LTTNG_INTERPRETER_TYPE_STRING
:
263 lttng_msgpack_write_str(writer
, output
->u
.str
.str
);
265 case LTTNG_INTERPRETER_TYPE_SEQUENCE
:
266 capture_sequence(writer
, output
);
268 case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM
:
269 case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM
:
270 capture_enum(writer
, output
);
278 void notification_append_empty_capture(
279 struct lttng_trigger_notification
*notif
)
281 lttng_msgpack_write_nil(¬if
->writer
);
284 static void record_error(struct lttng_trigger
*trigger
)
286 struct lttng_trigger_group
*trigger_group
= trigger
->group
;
287 size_t dimension_index
[1];
290 dimension_index
[0] = trigger
->error_counter_index
;
291 ret
= trigger_group
->error_counter
->ops
->counter_add(
292 trigger_group
->error_counter
->counter
,
299 void notification_send(struct lttng_trigger_notification
*notif
,
300 struct lttng_trigger
*trigger
)
305 struct lttng_ust_trigger_notification ust_notif
;
310 ust_notif
.id
= trigger
->id
;
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
= patient_writev(notif
->notification_fd
, iov
, iovec_count
);
347 if (errno
== EAGAIN
) {
348 record_error(trigger
);
349 DBG("Cannot send trigger notification without blocking: %s",
352 DBG("Error to sending trigger notification: %s",
359 void lttng_trigger_notification_send(struct lttng_trigger
*trigger
,
360 const char *stack_data
)
363 * This function is called from the probe, we must do dynamic
364 * allocation in this context.
366 struct lttng_trigger_notification notif
= {0};
368 notification_init(¬if
, trigger
);
370 if (caa_unlikely(!cds_list_empty(&trigger
->capture_bytecode_runtime_head
))) {
371 struct lttng_bytecode_runtime
*capture_bc_runtime
;
374 * Iterate over all the capture bytecodes. If the interpreter
375 * functions returns successfully, append the value of the
376 * `output` parameter to the capture buffer. If the interpreter
377 * fails, append an empty capture to the buffer.
379 cds_list_for_each_entry(capture_bc_runtime
,
380 &trigger
->capture_bytecode_runtime_head
, node
) {
381 struct lttng_interpreter_output output
;
383 if (capture_bc_runtime
->interpreter_funcs
.capture(capture_bc_runtime
,
384 stack_data
, &output
) & LTTNG_INTERPRETER_RECORD_FLAG
)
385 notification_append_capture(¬if
, &output
);
387 notification_append_empty_capture(¬if
);
392 * Send the notification (including the capture buffer) to the
395 notification_send(¬if
, trigger
);