Commit | Line | Data |
---|---|---|
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> |
9d315d6d | 14 | #include "common/logging.h" |
a2e4d05e | 15 | #include <urcu/rculist.h> |
d8d2416d | 16 | |
4b4a1337 | 17 | #include "lttng-tracer-core.h" |
36c52fff | 18 | #include "lib/lttng-ust/events.h" |
c3ba99c2 | 19 | #include "common/msgpack/msgpack.h" |
b9f82a28 | 20 | #include "lttng-bytecode.h" |
9d315d6d | 21 | #include "common/patient.h" |
d8d2416d | 22 | |
d37ecb3f FD |
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 \ | |
fd17d7ce | 31 | (PIPE_BUF - sizeof(struct lttng_ust_abi_event_notifier_notification) - 1) |
d37ecb3f FD |
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 | ||
b9f82a28 | 41 | static |
d9c66e5a | 42 | int capture_enum(struct lttng_msgpack_writer *writer, |
b9f82a28 FD |
43 | struct lttng_interpreter_output *output) |
44 | { | |
d9c66e5a | 45 | int ret; |
b9f82a28 | 46 | |
d9c66e5a MD |
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 | } | |
b9f82a28 FD |
63 | |
64 | switch (output->type) { | |
65 | case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM: | |
d9c66e5a MD |
66 | ret = lttng_msgpack_write_signed_integer(writer, output->u.s); |
67 | if (ret) { | |
68 | goto end; | |
69 | } | |
b9f82a28 FD |
70 | break; |
71 | case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM: | |
d9c66e5a MD |
72 | ret = lttng_msgpack_write_signed_integer(writer, output->u.u); |
73 | if (ret) { | |
74 | goto end; | |
75 | } | |
b9f82a28 FD |
76 | break; |
77 | default: | |
d9c66e5a MD |
78 | CRIT("Unknown enum output type\n"); |
79 | ret = -1; | |
80 | goto end; | |
b9f82a28 FD |
81 | } |
82 | ||
d9c66e5a MD |
83 | ret = lttng_msgpack_end_map(writer); |
84 | end: | |
85 | return ret; | |
b9f82a28 FD |
86 | } |
87 | ||
88 | static | |
89 | int64_t capture_sequence_element_signed(uint8_t *ptr, | |
4e48b5d2 | 90 | const struct lttng_ust_type_integer *integer_type) |
b9f82a28 FD |
91 | { |
92 | int64_t value; | |
a084756d MD |
93 | unsigned int size = integer_type->size; |
94 | bool byte_order_reversed = integer_type->reverse_byte_order; | |
b9f82a28 FD |
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) | |
baa8acf3 | 105 | tmp = lttng_ust_bswap_16(tmp); |
b9f82a28 FD |
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) | |
baa8acf3 | 115 | tmp = lttng_ust_bswap_32(tmp); |
b9f82a28 FD |
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) | |
baa8acf3 | 125 | tmp = lttng_ust_bswap_64(tmp); |
b9f82a28 FD |
126 | |
127 | value = tmp; | |
128 | break; | |
129 | } | |
130 | default: | |
d9c66e5a MD |
131 | CRIT("Unknown sequence element size\n"); |
132 | value = 0; | |
b9f82a28 FD |
133 | } |
134 | ||
135 | return value; | |
136 | } | |
137 | ||
138 | static | |
139 | uint64_t capture_sequence_element_unsigned(uint8_t *ptr, | |
4e48b5d2 | 140 | const struct lttng_ust_type_integer *integer_type) |
b9f82a28 FD |
141 | { |
142 | uint64_t value; | |
a084756d MD |
143 | unsigned int size = integer_type->size; |
144 | bool byte_order_reversed = integer_type->reverse_byte_order; | |
b9f82a28 FD |
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) | |
baa8acf3 | 155 | tmp = lttng_ust_bswap_16(tmp); |
b9f82a28 FD |
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) | |
baa8acf3 | 165 | tmp = lttng_ust_bswap_32(tmp); |
b9f82a28 FD |
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) | |
baa8acf3 | 175 | tmp = lttng_ust_bswap_64(tmp); |
b9f82a28 FD |
176 | |
177 | value = tmp; | |
178 | break; | |
179 | } | |
180 | default: | |
d9c66e5a MD |
181 | CRIT("Unknown sequence element size\n"); |
182 | value = 0; | |
b9f82a28 FD |
183 | } |
184 | ||
185 | return value; | |
186 | } | |
187 | ||
b9f82a28 | 188 | static |
d9c66e5a | 189 | int capture_sequence(struct lttng_msgpack_writer *writer, |
b9f82a28 FD |
190 | struct lttng_interpreter_output *output) |
191 | { | |
4e48b5d2 MD |
192 | const struct lttng_ust_type_integer *integer_type; |
193 | const struct lttng_ust_type_common *nested_type; | |
b9f82a28 FD |
194 | uint8_t *ptr; |
195 | bool signedness; | |
d9c66e5a | 196 | int i, ret; |
b9f82a28 | 197 | |
d9c66e5a MD |
198 | ret = lttng_msgpack_begin_array(writer, output->u.sequence.nr_elem); |
199 | if (ret) { | |
200 | goto end; | |
201 | } | |
b9f82a28 FD |
202 | ptr = (uint8_t *) output->u.sequence.ptr; |
203 | nested_type = output->u.sequence.nested_type; | |
a084756d MD |
204 | switch (nested_type->type) { |
205 | case lttng_ust_type_integer: | |
206 | integer_type = lttng_ust_get_type_integer(nested_type); | |
b9f82a28 | 207 | break; |
a084756d | 208 | case lttng_ust_type_enum: |
b9f82a28 | 209 | /* Treat enumeration as an integer. */ |
a084756d | 210 | integer_type = lttng_ust_get_type_integer(lttng_ust_get_type_enum(nested_type)->container_type); |
b9f82a28 FD |
211 | break; |
212 | default: | |
d9c66e5a MD |
213 | CRIT("Capture of array of non-integer are not supported\n"); |
214 | ret = -1; | |
215 | goto end; | |
b9f82a28 FD |
216 | } |
217 | signedness = integer_type->signedness; | |
218 | for (i = 0; i < output->u.sequence.nr_elem; i++) { | |
219 | if (signedness) { | |
d9c66e5a | 220 | ret = lttng_msgpack_write_signed_integer(writer, |
b9f82a28 FD |
221 | capture_sequence_element_signed(ptr, integer_type)); |
222 | } else { | |
d9c66e5a | 223 | ret = lttng_msgpack_write_unsigned_integer(writer, |
b9f82a28 FD |
224 | capture_sequence_element_unsigned(ptr, integer_type)); |
225 | } | |
d9c66e5a MD |
226 | if (ret) { |
227 | goto end; | |
228 | } | |
b9f82a28 FD |
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 arithmetics 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 | ||
d9c66e5a MD |
243 | ret = lttng_msgpack_end_array(writer); |
244 | end: | |
245 | return ret; | |
b9f82a28 FD |
246 | } |
247 | ||
d37ecb3f | 248 | static |
d9c66e5a | 249 | int notification_init(struct lttng_event_notifier_notification *notif, |
cda77256 | 250 | const struct lttng_ust_event_notifier *event_notifier) |
d8d2416d | 251 | { |
d37ecb3f | 252 | struct lttng_msgpack_writer *writer = ¬if->writer; |
d9c66e5a | 253 | int ret = 0; |
d37ecb3f | 254 | |
115db533 MD |
255 | notif->event_notifier_token = event_notifier->priv->parent.user_token; |
256 | notif->notification_fd = event_notifier->priv->group->notification_fd; | |
d37ecb3f FD |
257 | notif->has_captures = false; |
258 | ||
115db533 | 259 | if (event_notifier->priv->num_captures > 0) { |
d37ecb3f FD |
260 | lttng_msgpack_writer_init(writer, notif->capture_buf, |
261 | CAPTURE_BUFFER_SIZE); | |
262 | ||
d9c66e5a MD |
263 | ret = lttng_msgpack_begin_array(writer, event_notifier->priv->num_captures); |
264 | if (ret) { | |
265 | goto end; | |
266 | } | |
d37ecb3f FD |
267 | notif->has_captures = true; |
268 | } | |
d9c66e5a MD |
269 | end: |
270 | return ret; | |
d37ecb3f FD |
271 | } |
272 | ||
273 | static | |
d9c66e5a | 274 | int notification_append_capture( |
d37ecb3f FD |
275 | struct lttng_event_notifier_notification *notif, |
276 | struct lttng_interpreter_output *output) | |
277 | { | |
278 | struct lttng_msgpack_writer *writer = ¬if->writer; | |
d9c66e5a | 279 | int ret; |
d37ecb3f FD |
280 | |
281 | switch (output->type) { | |
282 | case LTTNG_INTERPRETER_TYPE_S64: | |
d9c66e5a | 283 | ret = lttng_msgpack_write_signed_integer(writer, output->u.s); |
d37ecb3f FD |
284 | break; |
285 | case LTTNG_INTERPRETER_TYPE_U64: | |
d9c66e5a | 286 | ret = lttng_msgpack_write_unsigned_integer(writer, output->u.u); |
d37ecb3f FD |
287 | break; |
288 | case LTTNG_INTERPRETER_TYPE_DOUBLE: | |
d9c66e5a | 289 | ret = lttng_msgpack_write_double(writer, output->u.d); |
d37ecb3f FD |
290 | break; |
291 | case LTTNG_INTERPRETER_TYPE_STRING: | |
d9c66e5a | 292 | ret = lttng_msgpack_write_str(writer, output->u.str.str); |
d37ecb3f FD |
293 | break; |
294 | case LTTNG_INTERPRETER_TYPE_SEQUENCE: | |
d9c66e5a | 295 | ret = capture_sequence(writer, output); |
d37ecb3f FD |
296 | break; |
297 | case LTTNG_INTERPRETER_TYPE_SIGNED_ENUM: | |
298 | case LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM: | |
d9c66e5a | 299 | ret = capture_enum(writer, output); |
d37ecb3f FD |
300 | break; |
301 | default: | |
d9c66e5a MD |
302 | CRIT("Unknown capture output type\n"); |
303 | ret = -1; | |
d37ecb3f | 304 | } |
d9c66e5a | 305 | return ret; |
d37ecb3f FD |
306 | } |
307 | ||
308 | static | |
d9c66e5a | 309 | int notification_append_empty_capture( |
d37ecb3f FD |
310 | struct lttng_event_notifier_notification *notif) |
311 | { | |
d9c66e5a | 312 | return lttng_msgpack_write_nil(¬if->writer); |
d37ecb3f FD |
313 | } |
314 | ||
cda77256 | 315 | static void record_error(const struct lttng_ust_event_notifier *event_notifier) |
6566528b FD |
316 | { |
317 | struct lttng_event_notifier_group *event_notifier_group = | |
115db533 | 318 | event_notifier->priv->group; |
33f43caa | 319 | struct lttng_counter *error_counter; |
6566528b FD |
320 | size_t dimension_index[1]; |
321 | int ret; | |
322 | ||
33f43caa MD |
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(); | |
c30ad764 | 332 | /* This group may not have an error counter attached to it. */ |
33f43caa | 333 | if (!error_counter) |
c30ad764 MD |
334 | return; |
335 | ||
115db533 | 336 | dimension_index[0] = event_notifier->priv->error_counter_index; |
6566528b | 337 | ret = event_notifier_group->error_counter->ops->counter_add( |
33f43caa | 338 | error_counter->counter, dimension_index, 1); |
6566528b FD |
339 | if (ret) |
340 | WARN_ON_ONCE(1); | |
341 | } | |
342 | ||
d37ecb3f | 343 | static |
6566528b | 344 | void notification_send(struct lttng_event_notifier_notification *notif, |
cda77256 | 345 | const struct lttng_ust_event_notifier *event_notifier) |
d37ecb3f FD |
346 | { |
347 | ssize_t ret; | |
348 | size_t content_len; | |
349 | int iovec_count = 1; | |
fd17d7ce | 350 | struct lttng_ust_abi_event_notifier_notification ust_notif = {0}; |
d37ecb3f FD |
351 | struct iovec iov[2]; |
352 | ||
353 | assert(notif); | |
354 | ||
115db533 | 355 | ust_notif.token = event_notifier->priv->parent.user_token; |
d37ecb3f | 356 | |
d8d2416d | 357 | /* |
d37ecb3f FD |
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. | |
d8d2416d | 361 | */ |
d37ecb3f FD |
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; | |
d8d2416d | 372 | |
d37ecb3f | 373 | assert(content_len > 0 && content_len <= CAPTURE_BUFFER_SIZE); |
d8d2416d | 374 | |
d37ecb3f FD |
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; | |
d8d2416d | 388 | |
d37ecb3f | 389 | /* Send all the buffers. */ |
516d12da | 390 | ret = ust_patient_writev(notif->notification_fd, iov, iovec_count); |
d8d2416d FD |
391 | if (ret == -1) { |
392 | if (errno == EAGAIN) { | |
6566528b FD |
393 | record_error(event_notifier); |
394 | DBG("Cannot send event_notifier notification without blocking: %s", | |
d8d2416d FD |
395 | strerror(errno)); |
396 | } else { | |
397 | DBG("Error to sending event notifier notification: %s", | |
398 | strerror(errno)); | |
399 | abort(); | |
400 | } | |
401 | } | |
402 | } | |
d37ecb3f | 403 | |
6566528b | 404 | void lttng_event_notifier_notification_send( |
cda77256 | 405 | const struct lttng_ust_event_notifier *event_notifier, |
a2e4d05e | 406 | const char *stack_data, |
b2e37d27 | 407 | struct lttng_ust_probe_ctx *probe_ctx, |
a2e4d05e | 408 | struct lttng_ust_notification_ctx *notif_ctx) |
d37ecb3f FD |
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 | ||
d9c66e5a MD |
416 | if (notification_init(¬if, event_notifier)) { |
417 | record_error(event_notifier); | |
418 | goto end; | |
419 | } | |
d37ecb3f | 420 | |
a2e4d05e | 421 | if (caa_unlikely(notif_ctx->eval_capture)) { |
5469a374 | 422 | struct lttng_ust_bytecode_runtime *capture_bc_runtime; |
d37ecb3f FD |
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 | */ | |
a2e4d05e MD |
430 | cds_list_for_each_entry_rcu(capture_bc_runtime, |
431 | &event_notifier->priv->capture_bytecode_runtime_head, node) { | |
d37ecb3f | 432 | struct lttng_interpreter_output output; |
d9c66e5a MD |
433 | uint8_t *save_pos; |
434 | int ret; | |
d37ecb3f | 435 | |
d9c66e5a | 436 | lttng_msgpack_save_writer_pos(¬if.writer, &save_pos); |
22c30e27 | 437 | if (capture_bc_runtime->interpreter_func(capture_bc_runtime, |
b2e37d27 | 438 | stack_data, probe_ctx, &output) == LTTNG_UST_BYTECODE_INTERPRETER_OK) |
d9c66e5a | 439 | ret = notification_append_capture(¬if, &output); |
d37ecb3f | 440 | else |
d9c66e5a MD |
441 | ret = notification_append_empty_capture(¬if); |
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(¬if.writer, save_pos); | |
449 | } | |
d37ecb3f FD |
450 | } |
451 | } | |
452 | ||
453 | /* | |
454 | * Send the notification (including the capture buffer) to the | |
455 | * sessiond. | |
456 | */ | |
6566528b | 457 | notification_send(¬if, event_notifier); |
d9c66e5a MD |
458 | end: |
459 | return; | |
d37ecb3f | 460 | } |