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