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