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