Fix clang-tidy cppcoreguidelines-pro-type-const-cast warning
[lttng-tools.git] / src / common / event.cpp
CommitLineData
76fcf151 1/*
ab5be9fa 2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
76fcf151 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
76fcf151 5 *
76fcf151
JG
6 */
7
c9e313bc
SM
8#include <common/align.hpp>
9#include <common/buffer-view.hpp>
10#include <common/compat/string.hpp>
11#include <common/dynamic-array.hpp>
12#include <common/dynamic-buffer.hpp>
13#include <common/error.hpp>
14#include <common/macros.hpp>
15#include <common/sessiond-comm/sessiond-comm.hpp>
16
8ddd72ef 17#include <lttng/constant.h>
c9e313bc 18#include <lttng/event-internal.hpp>
8ddd72ef 19#include <lttng/event.h>
c9e313bc
SM
20#include <lttng/lttng-error.h>
21#include <lttng/userspace-probe-internal.hpp>
8ddd72ef 22
f1494934 23namespace {
8ddd72ef
JR
24struct event_list_element {
25 struct lttng_event *event;
26 struct lttng_event_exclusion *exclusions;
27 char *filter_expression;
28};
f1494934 29} /* namespace */
8ddd72ef
JR
30
31static void event_list_destructor(void *ptr)
32{
33 struct event_list_element *element = (struct event_list_element *) ptr;
34
35 free(element->filter_expression);
36 free(element->exclusions);
37 lttng_event_destroy(element->event);
38 free(element);
39}
76fcf151 40
76fcf151
JG
41struct lttng_event *lttng_event_copy(const struct lttng_event *event)
42{
43 struct lttng_event *new_event;
44 struct lttng_event_extended *new_event_extended;
45
64803277 46 new_event = zmalloc<lttng_event>();
76fcf151
JG
47 if (!new_event) {
48 PERROR("Error allocating event structure");
49 goto end;
50 }
51
52 /* Copy the content of the old event. */
53 memcpy(new_event, event, sizeof(*event));
54
55 /*
56 * We need to create a new extended since the previous pointer is now
57 * invalid.
58 */
64803277 59 new_event_extended = zmalloc<lttng_event_extended>();
76fcf151
JG
60 if (!new_event_extended) {
61 PERROR("Error allocating event extended structure");
62 goto error;
63 }
64
65 new_event->extended.ptr = new_event_extended;
66end:
67 return new_event;
68error:
69 free(new_event);
cd9adb8b 70 new_event = nullptr;
76fcf151
JG
71 goto end;
72}
8ddd72ef 73
28ab034a
JG
74static int lttng_event_probe_attr_serialize(const struct lttng_event_probe_attr *probe,
75 struct lttng_payload *payload)
8ddd72ef
JR
76{
77 int ret;
78 size_t symbol_name_len;
1c9a0b0e 79 struct lttng_event_probe_attr_comm comm = {};
8ddd72ef 80
28ab034a 81 symbol_name_len = lttng_strnlen(probe->symbol_name, sizeof(probe->symbol_name));
2d6df81a 82 if (symbol_name_len == sizeof(probe->symbol_name)) {
8ddd72ef
JR
83 /* Not null-termintated. */
84 ret = -1;
85 goto end;
86 }
87
88 /* Include the null terminator. */
89 symbol_name_len += 1;
90
91 comm.symbol_name_len = (uint32_t) symbol_name_len;
92 comm.addr = probe->addr;
93 comm.offset = probe->addr;
94
28ab034a 95 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm, sizeof(comm));
8ddd72ef
JR
96 if (ret < 0) {
97 ret = -1;
98 goto end;
99 }
100
28ab034a 101 ret = lttng_dynamic_buffer_append(&payload->buffer, probe->symbol_name, symbol_name_len);
8ddd72ef
JR
102end:
103 return ret;
104}
105
28ab034a
JG
106static int lttng_event_function_attr_serialize(const struct lttng_event_function_attr *function,
107 struct lttng_payload *payload)
8ddd72ef
JR
108{
109 int ret;
110 size_t symbol_name_len;
c58be5ff
JG
111 struct lttng_event_function_attr_comm comm;
112
113 comm.symbol_name_len = 0;
8ddd72ef 114
28ab034a 115 symbol_name_len = lttng_strnlen(function->symbol_name, sizeof(function->symbol_name));
2d6df81a 116 if (symbol_name_len == sizeof(function->symbol_name)) {
8ddd72ef
JR
117 /* Not null-termintated. */
118 ret = -1;
119 goto end;
120 }
121
122 /* Include the null terminator. */
123 symbol_name_len += 1;
124
125 comm.symbol_name_len = (uint32_t) symbol_name_len;
126
28ab034a 127 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm, sizeof(comm));
8ddd72ef
JR
128 if (ret < 0) {
129 ret = -1;
130 goto end;
131 }
132
28ab034a 133 ret = lttng_dynamic_buffer_append(&payload->buffer, function->symbol_name, symbol_name_len);
8ddd72ef
JR
134end:
135 return ret;
136}
137
28ab034a
JG
138static ssize_t
139lttng_event_probe_attr_create_from_payload(struct lttng_payload_view *view,
140 struct lttng_event_probe_attr **probe_attr)
8ddd72ef
JR
141{
142 ssize_t ret, offset = 0;
143 const struct lttng_event_probe_attr_comm *comm;
cd9adb8b 144 struct lttng_event_probe_attr *local_attr = nullptr;
28ab034a
JG
145 struct lttng_payload_view comm_view =
146 lttng_payload_view_from_view(view, offset, sizeof(*comm));
8ddd72ef
JR
147
148 if (!lttng_payload_view_is_valid(&comm_view)) {
149 ret = -1;
150 goto end;
151 }
152
153 comm = (typeof(comm)) comm_view.buffer.data;
154 offset += sizeof(*comm);
155
64803277 156 local_attr = zmalloc<lttng_event_probe_attr>();
cd9adb8b 157 if (local_attr == nullptr) {
8ddd72ef
JR
158 ret = -1;
159 goto end;
160 }
161
162 local_attr->addr = comm->addr;
163 local_attr->offset = comm->offset;
164
165 {
166 const char *name;
167 struct lttng_payload_view name_view =
28ab034a 168 lttng_payload_view_from_view(view, offset, comm->symbol_name_len);
8ddd72ef
JR
169
170 if (!lttng_payload_view_is_valid(&name_view)) {
171 ret = -1;
172 goto end;
173 }
174
175 name = name_view.buffer.data;
176
28ab034a
JG
177 if (!lttng_buffer_view_contains_string(
178 &name_view.buffer, name, comm->symbol_name_len)) {
8ddd72ef
JR
179 ret = -1;
180 goto end;
181 }
182
28ab034a 183 ret = lttng_strncpy(local_attr->symbol_name, name, sizeof(local_attr->symbol_name));
8ddd72ef
JR
184 if (ret) {
185 ret = -1;
186 goto end;
187 }
188
189 offset += comm->symbol_name_len;
190 }
191
192 *probe_attr = local_attr;
cd9adb8b 193 local_attr = nullptr;
8ddd72ef
JR
194 ret = offset;
195end:
9eb895d7 196 free(local_attr);
8ddd72ef
JR
197 return ret;
198}
199
28ab034a
JG
200static ssize_t
201lttng_event_function_attr_create_from_payload(struct lttng_payload_view *view,
202 struct lttng_event_function_attr **function_attr)
8ddd72ef
JR
203{
204 ssize_t ret, offset = 0;
205 const struct lttng_event_function_attr_comm *comm;
cd9adb8b 206 struct lttng_event_function_attr *local_attr = nullptr;
28ab034a
JG
207 struct lttng_payload_view comm_view =
208 lttng_payload_view_from_view(view, offset, sizeof(*comm));
8ddd72ef
JR
209
210 if (!lttng_payload_view_is_valid(&comm_view)) {
211 ret = -1;
212 goto end;
213 }
214
215 comm = (typeof(comm)) view->buffer.data;
216 offset += sizeof(*comm);
217
64803277 218 local_attr = zmalloc<lttng_event_function_attr>();
cd9adb8b 219 if (local_attr == nullptr) {
8ddd72ef
JR
220 ret = -1;
221 goto end;
222 }
223
224 {
225 const char *name;
226 struct lttng_payload_view name_view =
28ab034a 227 lttng_payload_view_from_view(view, offset, comm->symbol_name_len);
8ddd72ef
JR
228
229 if (!lttng_payload_view_is_valid(&name_view)) {
230 ret = -1;
231 goto end;
232 }
233
234 name = name_view.buffer.data;
235
28ab034a
JG
236 if (!lttng_buffer_view_contains_string(
237 &name_view.buffer, name, comm->symbol_name_len)) {
8ddd72ef
JR
238 ret = -1;
239 goto end;
240 }
241
28ab034a 242 ret = lttng_strncpy(local_attr->symbol_name, name, sizeof(local_attr->symbol_name));
8ddd72ef
JR
243 if (ret) {
244 ret = -1;
245 goto end;
246 }
247
248 offset += comm->symbol_name_len;
249 }
250
251 *function_attr = local_attr;
cd9adb8b 252 local_attr = nullptr;
8ddd72ef
JR
253 ret = offset;
254end:
9eb895d7 255 free(local_attr);
8ddd72ef
JR
256 return ret;
257}
258
28ab034a
JG
259static ssize_t lttng_event_exclusions_create_from_payload(struct lttng_payload_view *view,
260 uint32_t count,
261 struct lttng_event_exclusion **exclusions)
8ddd72ef
JR
262{
263 ssize_t ret, offset = 0;
64803277 264 const size_t size = (count * LTTNG_SYMBOL_NAME_LEN);
8ddd72ef
JR
265 uint32_t i;
266 const struct lttng_event_exclusion_comm *comm;
267 struct lttng_event_exclusion *local_exclusions;
268
28ab034a
JG
269 local_exclusions =
270 zmalloc<lttng_event_exclusion>(sizeof(struct lttng_event_exclusion) + size);
8ddd72ef
JR
271 if (!local_exclusions) {
272 ret = -1;
273 goto end;
274 }
275
276 local_exclusions->count = count;
277
278 for (i = 0; i < count; i++) {
279 const char *string;
280 struct lttng_buffer_view string_view;
281 const struct lttng_buffer_view comm_view =
28ab034a 282 lttng_buffer_view_from_view(&view->buffer, offset, sizeof(*comm));
8ddd72ef
JR
283
284 if (!lttng_buffer_view_is_valid(&comm_view)) {
285 ret = -1;
286 goto end;
287 }
288
289 comm = (typeof(comm)) comm_view.data;
290 offset += sizeof(*comm);
291
28ab034a 292 string_view = lttng_buffer_view_from_view(&view->buffer, offset, comm->len);
8ddd72ef
JR
293
294 if (!lttng_buffer_view_is_valid(&string_view)) {
295 ret = -1;
296 goto end;
297 }
298
299 string = string_view.data;
300
28ab034a 301 if (!lttng_buffer_view_contains_string(&string_view, string, comm->len)) {
8ddd72ef
JR
302 ret = -1;
303 goto end;
304 }
305
28ab034a
JG
306 ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(local_exclusions, i),
307 string,
308 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(local_exclusions, i)));
8ddd72ef
JR
309 if (ret) {
310 ret = -1;
311 goto end;
312 }
313
314 offset += comm->len;
315 }
316
317 *exclusions = local_exclusions;
cd9adb8b 318 local_exclusions = nullptr;
8ddd72ef
JR
319 ret = offset;
320end:
321 free(local_exclusions);
322 return ret;
323}
324
325ssize_t lttng_event_create_from_payload(struct lttng_payload_view *view,
28ab034a
JG
326 struct lttng_event **out_event,
327 struct lttng_event_exclusion **out_exclusion,
328 char **out_filter_expression,
329 struct lttng_bytecode **out_bytecode)
8ddd72ef
JR
330{
331 ssize_t ret, offset = 0;
cd9adb8b
JG
332 struct lttng_event *local_event = nullptr;
333 struct lttng_event_exclusion *local_exclusions = nullptr;
334 struct lttng_bytecode *local_bytecode = nullptr;
335 char *local_filter_expression = nullptr;
8ddd72ef 336 const struct lttng_event_comm *event_comm;
cd9adb8b
JG
337 struct lttng_event_function_attr *local_function_attr = nullptr;
338 struct lttng_event_probe_attr *local_probe_attr = nullptr;
339 struct lttng_userspace_probe_location *local_userspace_probe_location = nullptr;
8ddd72ef
JR
340
341 /*
342 * Only event is obligatory, the other output argument are optional and
343 * depends on what the caller is interested in.
344 */
345 assert(out_event);
346 assert(view);
347
348 {
349 struct lttng_payload_view comm_view =
28ab034a 350 lttng_payload_view_from_view(view, offset, sizeof(*event_comm));
8ddd72ef
JR
351
352 if (!lttng_payload_view_is_valid(&comm_view)) {
353 ret = -1;
354 goto end;
355 }
356
357 /* lttng_event_comm header */
358 event_comm = (typeof(event_comm)) comm_view.buffer.data;
359 offset += sizeof(*event_comm);
360 }
361
362 local_event = lttng_event_create();
cd9adb8b 363 if (local_event == nullptr) {
8ddd72ef
JR
364 ret = -1;
365 goto end;
366 }
367
368 local_event->type = (enum lttng_event_type) event_comm->event_type;
369 local_event->loglevel_type = (enum lttng_loglevel_type) event_comm->loglevel_type;
370 local_event->loglevel = event_comm->loglevel;
5c7248cd 371 local_event->enabled = !!event_comm->enabled;
8ddd72ef
JR
372 local_event->pid = event_comm->pid;
373 local_event->flags = (enum lttng_event_flag) event_comm->flags;
374
375 {
376 const char *name;
377 const struct lttng_buffer_view name_view =
28ab034a 378 lttng_buffer_view_from_view(&view->buffer, offset, event_comm->name_len);
8ddd72ef
JR
379
380 if (!lttng_buffer_view_is_valid(&name_view)) {
381 ret = -1;
382 goto end;
383 }
384
385 name = (const char *) name_view.data;
386
28ab034a 387 if (!lttng_buffer_view_contains_string(&name_view, name, event_comm->name_len)) {
8ddd72ef
JR
388 ret = -1;
389 goto end;
390 }
391
28ab034a 392 ret = lttng_strncpy(local_event->name, name, sizeof(local_event->name));
8ddd72ef
JR
393 if (ret) {
394 ret = -1;
395 goto end;
396 }
397
398 offset += event_comm->name_len;
399 }
400
401 /* Exclusions */
402 if (event_comm->exclusion_count == 0) {
403 goto deserialize_filter_expression;
404 }
405
406 {
407 struct lttng_payload_view exclusions_view =
28ab034a 408 lttng_payload_view_from_view(view, offset, -1);
8ddd72ef
JR
409
410 if (!lttng_payload_view_is_valid(&exclusions_view)) {
411 ret = -1;
412 goto end;
413 }
414
28ab034a
JG
415 ret = lttng_event_exclusions_create_from_payload(
416 &exclusions_view, event_comm->exclusion_count, &local_exclusions);
8ddd72ef
JR
417 if (ret < 0) {
418 ret = -1;
419 goto end;
420 }
421 offset += ret;
422
423 local_event->exclusion = 1;
424 }
425
426deserialize_filter_expression:
427
428 if (event_comm->filter_expression_len == 0) {
429 if (event_comm->bytecode_len != 0) {
430 /*
431 * This is an invalid event payload.
432 *
433 * Filter expression without bytecode is possible but
434 * not the other way around.
435 * */
436 ret = -1;
437 goto end;
438 }
439 goto deserialize_event_type_payload;
440 }
441
442 {
443 const char *filter_expression_buffer;
28ab034a
JG
444 struct lttng_buffer_view filter_expression_view = lttng_buffer_view_from_view(
445 &view->buffer, offset, event_comm->filter_expression_len);
8ddd72ef
JR
446
447 if (!lttng_buffer_view_is_valid(&filter_expression_view)) {
448 ret = -1;
449 goto end;
450 }
451
452 filter_expression_buffer = filter_expression_view.data;
453
454 if (!lttng_buffer_view_contains_string(&filter_expression_view,
28ab034a
JG
455 filter_expression_buffer,
456 event_comm->filter_expression_len)) {
8ddd72ef
JR
457 ret = -1;
458 goto end;
459 }
460
28ab034a
JG
461 local_filter_expression =
462 lttng_strndup(filter_expression_buffer, event_comm->filter_expression_len);
8ddd72ef
JR
463 if (!local_filter_expression) {
464 ret = -1;
465 goto end;
466 }
467
468 local_event->filter = 1;
469
470 offset += event_comm->filter_expression_len;
471 }
472
473 if (event_comm->bytecode_len == 0) {
474 /*
475 * Filter expression can be present but without bytecode
476 * when dealing with event listing.
477 */
478 goto deserialize_event_type_payload;
479 }
480
481 /* Bytecode */
482 {
483 struct lttng_payload_view bytecode_view =
28ab034a 484 lttng_payload_view_from_view(view, offset, event_comm->bytecode_len);
8ddd72ef
JR
485
486 if (!lttng_payload_view_is_valid(&bytecode_view)) {
487 ret = -1;
488 goto end;
489 }
490
64803277 491 local_bytecode = zmalloc<lttng_bytecode>(event_comm->bytecode_len);
8ddd72ef
JR
492 if (!local_bytecode) {
493 ret = -1;
494 goto end;
495 }
496
28ab034a
JG
497 memcpy(local_bytecode, bytecode_view.buffer.data, event_comm->bytecode_len);
498 if ((local_bytecode->len + sizeof(*local_bytecode)) != event_comm->bytecode_len) {
8ddd72ef
JR
499 ret = -1;
500 goto end;
501 }
502
503 offset += event_comm->bytecode_len;
504 }
505
506deserialize_event_type_payload:
507 /* Event type specific payload */
508 switch (local_event->type) {
509 case LTTNG_EVENT_FUNCTION:
510 /* Fallthrough */
511 case LTTNG_EVENT_PROBE:
512 {
28ab034a
JG
513 struct lttng_payload_view probe_attr_view = lttng_payload_view_from_view(
514 view, offset, event_comm->lttng_event_probe_attr_len);
8ddd72ef
JR
515
516 if (event_comm->lttng_event_probe_attr_len == 0) {
517 ret = -1;
518 goto end;
519 }
520
521 if (!lttng_payload_view_is_valid(&probe_attr_view)) {
522 ret = -1;
523 goto end;
524 }
525
28ab034a
JG
526 ret = lttng_event_probe_attr_create_from_payload(&probe_attr_view,
527 &local_probe_attr);
8ddd72ef
JR
528 if (ret < 0 || ret != event_comm->lttng_event_probe_attr_len) {
529 ret = -1;
530 goto end;
531 }
532
533 /* Copy to the local event. */
28ab034a 534 memcpy(&local_event->attr.probe, local_probe_attr, sizeof(local_event->attr.probe));
8ddd72ef
JR
535
536 offset += ret;
537 break;
538 }
539 case LTTNG_EVENT_FUNCTION_ENTRY:
540 {
28ab034a
JG
541 struct lttng_payload_view function_attr_view = lttng_payload_view_from_view(
542 view, offset, event_comm->lttng_event_function_attr_len);
8ddd72ef
JR
543
544 if (event_comm->lttng_event_function_attr_len == 0) {
545 ret = -1;
546 goto end;
547 }
548
549 if (!lttng_payload_view_is_valid(&function_attr_view)) {
550 ret = -1;
551 goto end;
552 }
553
28ab034a
JG
554 ret = lttng_event_function_attr_create_from_payload(&function_attr_view,
555 &local_function_attr);
8ddd72ef
JR
556 if (ret < 0 || ret != event_comm->lttng_event_function_attr_len) {
557 ret = -1;
558 goto end;
559 }
560
561 /* Copy to the local event. */
28ab034a
JG
562 memcpy(&local_event->attr.ftrace,
563 local_function_attr,
564 sizeof(local_event->attr.ftrace));
8ddd72ef
JR
565
566 offset += ret;
567
568 break;
569 }
570 case LTTNG_EVENT_USERSPACE_PROBE:
571 {
572 struct lttng_payload_view userspace_probe_location_view =
28ab034a
JG
573 lttng_payload_view_from_view(
574 view, offset, event_comm->userspace_probe_location_len);
8ddd72ef
JR
575
576 if (event_comm->userspace_probe_location_len == 0) {
577 ret = -1;
578 goto end;
579 }
580
28ab034a 581 if (!lttng_payload_view_is_valid(&userspace_probe_location_view)) {
8ddd72ef
JR
582 ret = -1;
583 goto end;
584 }
585
586 ret = lttng_userspace_probe_location_create_from_payload(
28ab034a 587 &userspace_probe_location_view, &local_userspace_probe_location);
8ddd72ef
JR
588 if (ret < 0) {
589 WARN("Failed to create a userspace probe location from the received buffer");
590 ret = -1;
591 goto end;
592 }
593
594 if (ret != event_comm->userspace_probe_location_len) {
28ab034a
JG
595 WARN("Userspace probe location from the received buffer is not the advertised length: header length = %" PRIu32
596 ", payload length = %zd",
597 event_comm->userspace_probe_location_len,
598 ret);
8ddd72ef
JR
599 ret = -1;
600 goto end;
601 }
602
603 /* Attach the probe location to the event. */
28ab034a
JG
604 ret = lttng_event_set_userspace_probe_location(local_event,
605 local_userspace_probe_location);
8ddd72ef
JR
606 if (ret) {
607 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
608 goto end;
609 }
610
611 /*
612 * Userspace probe location object ownership transfered to the
613 * event object.
614 */
cd9adb8b 615 local_userspace_probe_location = nullptr;
8ddd72ef
JR
616 offset += event_comm->userspace_probe_location_len;
617 break;
618 }
619 case LTTNG_EVENT_TRACEPOINT:
620 /* Fallthrough */
621 case LTTNG_EVENT_ALL:
622 /* Fallthrough */
623 case LTTNG_EVENT_SYSCALL:
624 /* Fallthrough */
625 case LTTNG_EVENT_NOOP:
626 /* Nothing to do here */
627 break;
628 default:
629 ret = LTTNG_ERR_UND;
630 goto end;
631 break;
632 }
633
634 /* Transfer ownership to the caller. */
635 *out_event = local_event;
cd9adb8b 636 local_event = nullptr;
8ddd72ef
JR
637
638 if (out_bytecode) {
639 *out_bytecode = local_bytecode;
cd9adb8b 640 local_bytecode = nullptr;
8ddd72ef
JR
641 }
642
643 if (out_exclusion) {
644 *out_exclusion = local_exclusions;
cd9adb8b 645 local_exclusions = nullptr;
8ddd72ef
JR
646 }
647
648 if (out_filter_expression) {
649 *out_filter_expression = local_filter_expression;
cd9adb8b 650 local_filter_expression = nullptr;
8ddd72ef
JR
651 }
652
653 ret = offset;
654end:
655 lttng_event_destroy(local_event);
656 lttng_userspace_probe_location_destroy(local_userspace_probe_location);
657 free(local_filter_expression);
658 free(local_exclusions);
659 free(local_bytecode);
660 free(local_function_attr);
661 free(local_probe_attr);
662 return ret;
663}
664
665int lttng_event_serialize(const struct lttng_event *event,
28ab034a 666 unsigned int exclusion_count,
1e0019bb 667 const char *const *exclusion_list,
dd7ef124 668 const char *filter_expression,
28ab034a
JG
669 size_t bytecode_len,
670 struct lttng_bytecode *bytecode,
671 struct lttng_payload *payload)
8ddd72ef
JR
672{
673 int ret;
674 unsigned int i;
675 size_t header_offset, size_before_payload;
676 size_t name_len;
1c9a0b0e 677 struct lttng_event_comm event_comm = {};
8ddd72ef
JR
678 struct lttng_event_comm *header;
679
680 assert(event);
681 assert(payload);
682 assert(exclusion_count == 0 || exclusion_list);
683
684 /* Save the header location for later in-place header update. */
685 header_offset = payload->buffer.size;
686
2d6df81a
JG
687 name_len = lttng_strnlen(event->name, sizeof(event->name));
688 if (name_len == sizeof(event->name)) {
8ddd72ef
JR
689 /* Event name is not NULL-terminated. */
690 ret = -1;
691 goto end;
692 }
693
694 /* Add null termination. */
695 name_len += 1;
696
697 if (exclusion_count > UINT32_MAX) {
698 /* Possible overflow. */
699 ret = -1;
700 goto end;
701 }
702
703 if (bytecode_len > UINT32_MAX) {
704 /* Possible overflow. */
705 ret = -1;
706 goto end;
707 }
708
709 event_comm.name_len = (uint32_t) name_len;
710 event_comm.event_type = (int8_t) event->type;
711 event_comm.loglevel_type = (int8_t) event->loglevel_type;
712 event_comm.loglevel = (int32_t) event->loglevel;
713 event_comm.enabled = (int8_t) event->enabled;
714 event_comm.pid = (int32_t) event->pid;
715 event_comm.exclusion_count = (uint32_t) exclusion_count;
716 event_comm.bytecode_len = (uint32_t) bytecode_len;
717 event_comm.flags = (int32_t) event->flags;
718
719 if (filter_expression) {
28ab034a 720 event_comm.filter_expression_len = strlen(filter_expression) + 1;
8ddd72ef
JR
721 }
722
723 /* Header */
28ab034a 724 ret = lttng_dynamic_buffer_append(&payload->buffer, &event_comm, sizeof(event_comm));
8ddd72ef
JR
725 if (ret) {
726 goto end;
727 }
728
729 /* Event name */
28ab034a 730 ret = lttng_dynamic_buffer_append(&payload->buffer, event->name, name_len);
8ddd72ef
JR
731 if (ret) {
732 goto end;
733 }
734
735 /* Exclusions */
736 for (i = 0; i < exclusion_count; i++) {
28ab034a
JG
737 const size_t exclusion_len =
738 lttng_strnlen(*(exclusion_list + i), LTTNG_SYMBOL_NAME_LEN);
85eb6b83
JG
739 struct lttng_event_exclusion_comm exclusion_header;
740
741 exclusion_header.len = (uint32_t) exclusion_len + 1;
8ddd72ef
JR
742
743 if (exclusion_len == LTTNG_SYMBOL_NAME_LEN) {
744 /* Exclusion is not NULL-terminated. */
745 ret = -1;
746 goto end;
747 }
748
28ab034a
JG
749 ret = lttng_dynamic_buffer_append(
750 &payload->buffer, &exclusion_header, sizeof(exclusion_header));
8ddd72ef
JR
751 if (ret) {
752 goto end;
753 }
754
28ab034a
JG
755 ret = lttng_dynamic_buffer_append(
756 &payload->buffer, *(exclusion_list + i), exclusion_len + 1);
8ddd72ef
JR
757 if (ret) {
758 goto end;
759 }
760 }
761
762 /* Filter expression and its bytecode */
763 if (filter_expression) {
28ab034a
JG
764 ret = lttng_dynamic_buffer_append(
765 &payload->buffer, filter_expression, event_comm.filter_expression_len);
8ddd72ef
JR
766 if (ret) {
767 goto end;
768 }
769
770 /*
771 * Bytecode can be absent when we serialize to the client
772 * for listing.
773 */
774 if (bytecode) {
28ab034a 775 ret = lttng_dynamic_buffer_append(&payload->buffer, bytecode, bytecode_len);
8ddd72ef
JR
776 if (ret) {
777 goto end;
778 }
779 }
780 }
781
782 size_before_payload = payload->buffer.size;
783
784 /* Event type specific payload */
785 switch (event->type) {
786 case LTTNG_EVENT_FUNCTION:
787 /* Fallthrough */
788 case LTTNG_EVENT_PROBE:
789 ret = lttng_event_probe_attr_serialize(&event->attr.probe, payload);
790 if (ret) {
791 ret = -1;
792 goto end;
793 }
794
28ab034a
JG
795 header =
796 (struct lttng_event_comm *) ((char *) payload->buffer.data + header_offset);
797 header->lttng_event_probe_attr_len = payload->buffer.size - size_before_payload;
8ddd72ef
JR
798
799 break;
800 case LTTNG_EVENT_FUNCTION_ENTRY:
28ab034a 801 ret = lttng_event_function_attr_serialize(&event->attr.ftrace, payload);
8ddd72ef
JR
802 if (ret) {
803 ret = -1;
804 goto end;
805 }
806
807 /* Update the lttng_event_function_attr len. */
28ab034a
JG
808 header =
809 (struct lttng_event_comm *) ((char *) payload->buffer.data + header_offset);
810 header->lttng_event_function_attr_len = payload->buffer.size - size_before_payload;
8ddd72ef
JR
811
812 break;
813 case LTTNG_EVENT_USERSPACE_PROBE:
814 {
815 const struct lttng_event_extended *ev_ext =
28ab034a 816 (const struct lttng_event_extended *) event->extended.ptr;
8ddd72ef
JR
817
818 assert(event->extended.ptr);
819 assert(ev_ext->probe_location);
820
821 size_before_payload = payload->buffer.size;
822 if (ev_ext->probe_location) {
823 /*
824 * lttng_userspace_probe_location_serialize returns the
825 * number of bytes that were appended to the buffer.
826 */
28ab034a
JG
827 ret = lttng_userspace_probe_location_serialize(ev_ext->probe_location,
828 payload);
8ddd72ef
JR
829 if (ret < 0) {
830 goto end;
831 }
832
833 ret = 0;
834
835 /* Update the userspace probe location len. */
836 header = (struct lttng_event_comm *) ((char *) payload->buffer.data +
837 header_offset);
838 header->userspace_probe_location_len =
28ab034a 839 payload->buffer.size - size_before_payload;
8ddd72ef
JR
840 }
841 break;
842 }
843 case LTTNG_EVENT_TRACEPOINT:
844 /* Fallthrough */
845 case LTTNG_EVENT_ALL:
846 /* Fallthrough */
847 default:
848 /* Nothing to do here. */
849 break;
850 }
851
852end:
853 return ret;
854}
855
28ab034a
JG
856static ssize_t lttng_event_context_app_populate_from_payload(const struct lttng_payload_view *view,
857 struct lttng_event_context *event_ctx)
26e1c61f
JR
858{
859 ssize_t ret, offset = 0;
860 const struct lttng_event_context_app_comm *comm;
cd9adb8b 861 char *provider_name = nullptr, *context_name = nullptr;
26e1c61f 862 size_t provider_name_len, context_name_len;
28ab034a
JG
863 const struct lttng_buffer_view comm_view =
864 lttng_buffer_view_from_view(&view->buffer, offset, sizeof(*comm));
26e1c61f
JR
865
866 assert(event_ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
867
868 if (!lttng_buffer_view_is_valid(&comm_view)) {
869 ret = -1;
870 goto end;
871 }
872
873 comm = (typeof(comm)) comm_view.data;
874 offset += sizeof(*comm);
875
876 provider_name_len = comm->provider_name_len;
877 context_name_len = comm->ctx_name_len;
878
879 if (provider_name_len == 0 || context_name_len == 0) {
880 /*
881 * Application provider and context names MUST
882 * be provided.
883 */
884 ret = -1;
885 goto end;
886 }
887
888 {
889 const char *name;
890 const struct lttng_buffer_view provider_name_view =
28ab034a 891 lttng_buffer_view_from_view(&view->buffer, offset, provider_name_len);
26e1c61f
JR
892
893 if (!lttng_buffer_view_is_valid(&provider_name_view)) {
894 ret = -1;
895 goto end;
896 }
897
898 name = provider_name_view.data;
899
28ab034a
JG
900 if (!lttng_buffer_view_contains_string(
901 &provider_name_view, name, provider_name_len)) {
26e1c61f
JR
902 ret = -1;
903 goto end;
904 }
905
906 provider_name = lttng_strndup(name, provider_name_len);
907 if (!provider_name) {
908 ret = -1;
909 goto end;
910 }
911
912 offset += provider_name_len;
913 }
914
915 {
916 const char *name;
917 const struct lttng_buffer_view context_name_view =
28ab034a 918 lttng_buffer_view_from_view(&view->buffer, offset, context_name_len);
26e1c61f
JR
919
920 if (!lttng_buffer_view_is_valid(&context_name_view)) {
921 ret = -1;
922 goto end;
923 }
924
925 name = context_name_view.data;
926
28ab034a 927 if (!lttng_buffer_view_contains_string(&context_name_view, name, context_name_len)) {
26e1c61f
JR
928 ret = -1;
929 goto end;
930 }
931
932 context_name = lttng_strndup(name, context_name_len);
933 if (!context_name) {
934 ret = -1;
935 goto end;
936 }
937
938 offset += context_name_len;
939 }
940
941 /* Transfer ownership of the strings */
942 event_ctx->u.app_ctx.provider_name = provider_name;
943 event_ctx->u.app_ctx.ctx_name = context_name;
cd9adb8b
JG
944 provider_name = nullptr;
945 context_name = nullptr;
26e1c61f
JR
946
947 ret = offset;
948end:
949 free(provider_name);
950 free(context_name);
951
952 return ret;
953}
954
28ab034a
JG
955static ssize_t
956lttng_event_context_perf_counter_populate_from_payload(const struct lttng_payload_view *view,
957 struct lttng_event_context *event_ctx)
26e1c61f 958{
531e96a7
JR
959 int ret;
960 ssize_t consumed, offset = 0;
26e1c61f
JR
961 const struct lttng_event_context_perf_counter_comm *comm;
962 size_t name_len;
28ab034a
JG
963 const struct lttng_buffer_view comm_view =
964 lttng_buffer_view_from_view(&view->buffer, offset, sizeof(*comm));
26e1c61f
JR
965
966 assert(event_ctx->ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER ||
28ab034a
JG
967 event_ctx->ctx == LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER ||
968 event_ctx->ctx == LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER);
26e1c61f
JR
969
970 if (!lttng_buffer_view_is_valid(&comm_view)) {
531e96a7 971 consumed = -1;
26e1c61f
JR
972 goto end;
973 }
974
975 comm = (typeof(comm)) comm_view.data;
976 offset += sizeof(*comm);
977
978 name_len = comm->name_len;
979
980 {
981 const char *name;
982 const struct lttng_buffer_view provider_name_view =
28ab034a 983 lttng_buffer_view_from_view(&view->buffer, offset, name_len);
26e1c61f
JR
984
985 if (!lttng_buffer_view_is_valid(&provider_name_view)) {
531e96a7 986 consumed = -1;
26e1c61f
JR
987 goto end;
988 }
989
990 name = provider_name_view.data;
991
28ab034a 992 if (!lttng_buffer_view_contains_string(&provider_name_view, name, name_len)) {
531e96a7 993 consumed = -1;
26e1c61f
JR
994 goto end;
995 }
996
28ab034a
JG
997 ret = lttng_strncpy(event_ctx->u.perf_counter.name,
998 name,
999 sizeof(event_ctx->u.perf_counter.name));
531e96a7
JR
1000 if (ret) {
1001 consumed = -1;
1002 goto end;
1003 }
26e1c61f
JR
1004 offset += name_len;
1005 }
1006
1007 event_ctx->u.perf_counter.config = comm->config;
1008 event_ctx->u.perf_counter.type = comm->type;
1009
531e96a7 1010 consumed = offset;
26e1c61f
JR
1011
1012end:
531e96a7 1013 return consumed;
26e1c61f
JR
1014}
1015
28ab034a
JG
1016ssize_t lttng_event_context_create_from_payload(struct lttng_payload_view *view,
1017 struct lttng_event_context **event_ctx)
26e1c61f
JR
1018{
1019 ssize_t ret, offset = 0;
1020 const struct lttng_event_context_comm *comm;
cd9adb8b 1021 struct lttng_event_context *local_context = nullptr;
28ab034a
JG
1022 struct lttng_buffer_view comm_view =
1023 lttng_buffer_view_from_view(&view->buffer, offset, sizeof(*comm));
26e1c61f
JR
1024
1025 assert(event_ctx);
1026 assert(view);
1027
1028 if (!lttng_buffer_view_is_valid(&comm_view)) {
1029 ret = -1;
1030 goto end;
1031 }
1032
1033 comm = (typeof(comm)) comm_view.data;
1034 offset += sizeof(*comm);
1035
64803277 1036 local_context = zmalloc<lttng_event_context>();
26e1c61f
JR
1037 if (!local_context) {
1038 ret = -1;
1039 goto end;
1040 }
1041
1042 local_context->ctx = (lttng_event_context_type) comm->type;
1043
1044 {
1045 struct lttng_payload_view subtype_view =
28ab034a 1046 lttng_payload_view_from_view(view, offset, -1);
26e1c61f
JR
1047
1048 switch (local_context->ctx) {
1049 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
28ab034a
JG
1050 ret = lttng_event_context_app_populate_from_payload(&subtype_view,
1051 local_context);
26e1c61f
JR
1052 break;
1053 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1054 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1055 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
28ab034a
JG
1056 ret = lttng_event_context_perf_counter_populate_from_payload(&subtype_view,
1057 local_context);
26e1c61f
JR
1058 break;
1059 default:
1060 /* Nothing else to deserialize. */
1061 ret = 0;
1062 break;
1063 }
1064 }
1065
1066 if (ret < 0) {
1067 goto end;
1068 }
1069
1070 offset += ret;
1071
1072 *event_ctx = local_context;
cd9adb8b 1073 local_context = nullptr;
26e1c61f
JR
1074 ret = offset;
1075
1076end:
1077 free(local_context);
1078 return ret;
1079}
1080
28ab034a
JG
1081static int lttng_event_context_app_serialize(struct lttng_event_context *context,
1082 struct lttng_payload *payload)
26e1c61f
JR
1083{
1084 int ret;
1c9a0b0e 1085 struct lttng_event_context_app_comm comm = {};
26e1c61f
JR
1086 size_t provider_len, ctx_len;
1087 const char *provider_name;
1088 const char *ctx_name;
1089
1090 assert(payload);
1091 assert(context);
1092 assert(context->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
1093
1094 provider_name = context->u.app_ctx.provider_name;
1095 ctx_name = context->u.app_ctx.ctx_name;
1096
1097 if (!provider_name || !ctx_name) {
1098 ret = -LTTNG_ERR_INVALID;
1099 goto end;
1100 }
1101
1102 provider_len = strlen(provider_name);
1103 if (provider_len == 0) {
1104 ret = -LTTNG_ERR_INVALID;
1105 goto end;
1106 }
1107
1108 /* Include the null terminator. */
f4f239b3
JR
1109 provider_len += 1;
1110 comm.provider_name_len = provider_len;
26e1c61f
JR
1111
1112 ctx_len = strlen(ctx_name);
1113 if (ctx_len == 0) {
1114 ret = -LTTNG_ERR_INVALID;
1115 goto end;
1116 }
1117
1118 /* Include the null terminator. */
f4f239b3
JR
1119 ctx_len += 1;
1120 comm.ctx_name_len = ctx_len;
26e1c61f
JR
1121
1122 /* Header */
28ab034a 1123 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm, sizeof(comm));
26e1c61f
JR
1124 if (ret) {
1125 ret = -1;
1126 goto end;
1127 }
1128
28ab034a 1129 ret = lttng_dynamic_buffer_append(&payload->buffer, provider_name, provider_len);
26e1c61f
JR
1130 if (ret) {
1131 ret = -1;
1132 goto end;
1133 }
1134
28ab034a 1135 ret = lttng_dynamic_buffer_append(&payload->buffer, ctx_name, ctx_len);
26e1c61f
JR
1136 if (ret) {
1137 ret = -1;
1138 goto end;
1139 }
1140
1141end:
1142 return ret;
1143}
1144
28ab034a
JG
1145static int lttng_event_context_perf_counter_serialize(struct lttng_event_perf_counter_ctx *context,
1146 struct lttng_payload *payload)
26e1c61f
JR
1147{
1148 int ret;
1c9a0b0e 1149 struct lttng_event_context_perf_counter_comm comm = {};
26e1c61f
JR
1150
1151 assert(payload);
1152 assert(context);
1153
1154 comm.config = context->config;
1155 comm.type = context->type;
2d6df81a 1156 comm.name_len = lttng_strnlen(context->name, sizeof(context->name));
26e1c61f 1157
2d6df81a 1158 if (comm.name_len == sizeof(context->name)) {
26e1c61f
JR
1159 ret = -1;
1160 goto end;
1161 }
1162
1163 /* Include the null terminator. */
1164 comm.name_len += 1;
1165
1166 /* Header */
28ab034a 1167 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm, sizeof(comm));
26e1c61f
JR
1168 if (ret) {
1169 ret = -1;
1170 goto end;
1171 }
1172
28ab034a 1173 ret = lttng_dynamic_buffer_append(&payload->buffer, context->name, comm.name_len);
26e1c61f
JR
1174 if (ret) {
1175 ret = -1;
1176 goto end;
1177 }
1178
1179end:
1180 return ret;
1181}
1182
1183int lttng_event_context_serialize(struct lttng_event_context *context,
28ab034a 1184 struct lttng_payload *payload)
26e1c61f
JR
1185{
1186 int ret;
fa9870ce
JG
1187 struct lttng_event_context_comm context_comm;
1188
1189 context_comm.type = 0;
26e1c61f
JR
1190
1191 assert(context);
1192 assert(payload);
1193
1194 context_comm.type = (uint32_t) context->ctx;
1195
1196 /* Header */
28ab034a 1197 ret = lttng_dynamic_buffer_append(&payload->buffer, &context_comm, sizeof(context_comm));
26e1c61f
JR
1198 if (ret) {
1199 goto end;
1200 }
1201
1202 switch (context->ctx) {
1203 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
1204 ret = lttng_event_context_app_serialize(context, payload);
1205 break;
1206 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1207 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1208 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
28ab034a 1209 ret = lttng_event_context_perf_counter_serialize(&context->u.perf_counter, payload);
26e1c61f
JR
1210 break;
1211 default:
1212 /* Nothing else to serialize. */
1213 break;
1214 }
1215
1216 if (ret) {
1217 goto end;
1218 }
1219
1220end:
1221 return ret;
1222}
1223
1224void lttng_event_context_destroy(struct lttng_event_context *context)
1225{
1226 if (!context) {
1227 return;
1228 }
1229
1230 if (context->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1231 free(context->u.app_ctx.provider_name);
1232 free(context->u.app_ctx.ctx_name);
1233 }
1234
1235 free(context);
1236}
1237
b2d68839
JR
1238/*
1239 * This is a specialized populate for lttng_event_field since it ignores
1240 * the extension field of the lttng_event struct and simply copies what it can
1241 * to the internal struct lttng_event of a lttng_event_field.
1242 */
28ab034a
JG
1243static void lttng_event_field_populate_lttng_event_from_event(const struct lttng_event *src,
1244 struct lttng_event *destination)
b2d68839
JR
1245{
1246 memcpy(destination, src, sizeof(*destination));
1247
1248 /* Remove all possible dynamic data from the destination event rule. */
cd9adb8b 1249 destination->extended.ptr = nullptr;
b2d68839
JR
1250}
1251
28ab034a
JG
1252ssize_t lttng_event_field_create_from_payload(struct lttng_payload_view *view,
1253 struct lttng_event_field **field)
b2d68839
JR
1254{
1255 ssize_t ret, offset = 0;
cd9adb8b
JG
1256 struct lttng_event_field *local_event_field = nullptr;
1257 struct lttng_event *event = nullptr;
b2d68839 1258 const struct lttng_event_field_comm *comm;
cd9adb8b 1259 const char *name = nullptr;
b2d68839
JR
1260
1261 assert(field);
1262 assert(view);
1263
1264 {
1265 const struct lttng_buffer_view comm_view =
28ab034a 1266 lttng_buffer_view_from_view(&view->buffer, offset, sizeof(*comm));
b2d68839
JR
1267
1268 if (!lttng_buffer_view_is_valid(&comm_view)) {
1269 ret = -1;
1270 goto end;
1271 }
1272
1273 /* lttng_event_field_comm header */
1274 comm = (const lttng_event_field_comm *) comm_view.data;
1275 offset += sizeof(*comm);
1276 }
1277
64803277 1278 local_event_field = zmalloc<lttng_event_field>();
b2d68839
JR
1279 if (!local_event_field) {
1280 ret = -1;
1281 goto end;
1282 }
1283
1284 local_event_field->type = (lttng_event_field_type) comm->type;
1285 local_event_field->nowrite = comm->nowrite;
1286
1287 /* Field name */
1288 {
1289 const struct lttng_buffer_view name_view =
28ab034a 1290 lttng_buffer_view_from_view(&view->buffer, offset, comm->name_len);
b2d68839
JR
1291
1292 if (!lttng_buffer_view_is_valid(&name_view)) {
1293 ret = -1;
1294 goto end;
1295 }
1296
1297 name = name_view.data;
1298
28ab034a 1299 if (!lttng_buffer_view_contains_string(&name_view, name_view.data, comm->name_len)) {
b2d68839
JR
1300 ret = -1;
1301 goto end;
1302 }
1303
1304 if (comm->name_len > LTTNG_SYMBOL_NAME_LEN - 1) {
1305 /* Name is too long.*/
1306 ret = -1;
1307 goto end;
1308 }
1309
1310 offset += comm->name_len;
1311 }
1312
1313 /* Event */
1314 {
1315 struct lttng_payload_view event_view =
28ab034a 1316 lttng_payload_view_from_view(view, offset, comm->event_len);
b2d68839
JR
1317
1318 if (!lttng_payload_view_is_valid(&event_view)) {
1319 ret = -1;
1320 goto end;
1321 }
1322
cd9adb8b
JG
1323 ret = lttng_event_create_from_payload(
1324 &event_view, &event, nullptr, nullptr, nullptr);
b2d68839
JR
1325 if (ret != comm->event_len) {
1326 ret = -1;
1327 goto end;
1328 }
1329
1330 offset += ret;
1331 }
1332
1333 assert(name);
1334 assert(event);
1335
28ab034a
JG
1336 if (lttng_strncpy(
1337 local_event_field->field_name, name, sizeof(local_event_field->field_name))) {
b2d68839
JR
1338 ret = -1;
1339 goto end;
1340 }
1341
28ab034a 1342 lttng_event_field_populate_lttng_event_from_event(event, &local_event_field->event);
b2d68839
JR
1343
1344 *field = local_event_field;
cd9adb8b 1345 local_event_field = nullptr;
b2d68839
JR
1346 ret = offset;
1347end:
1348 lttng_event_destroy(event);
1349 free(local_event_field);
1350 return ret;
1351}
1352
1353int lttng_event_field_serialize(const struct lttng_event_field *field,
28ab034a 1354 struct lttng_payload *payload)
b2d68839
JR
1355{
1356 int ret;
1357 size_t header_offset, size_before_event;
1358 size_t name_len;
1c9a0b0e 1359 struct lttng_event_field_comm event_field_comm = {};
b2d68839
JR
1360 struct lttng_event_field_comm *header;
1361
1362 assert(field);
1363 assert(payload);
1364
1365 /* Save the header location for later in-place header update. */
1366 header_offset = payload->buffer.size;
1367
2d6df81a
JG
1368 name_len = strnlen(field->field_name, sizeof(field->field_name));
1369 if (name_len == sizeof(field->field_name)) {
b2d68839
JR
1370 /* Event name is not NULL-terminated. */
1371 ret = -1;
1372 goto end;
1373 }
1374
1375 /* Add null termination. */
1376 name_len += 1;
1377
1378 event_field_comm.type = field->type;
28ab034a 1379 event_field_comm.nowrite = (uint8_t) field->nowrite;
b2d68839
JR
1380 event_field_comm.name_len = name_len;
1381
1382 /* Header */
1383 ret = lttng_dynamic_buffer_append(
28ab034a 1384 &payload->buffer, &event_field_comm, sizeof(event_field_comm));
b2d68839
JR
1385 if (ret) {
1386 goto end;
1387 }
1388
1389 /* Field name */
28ab034a 1390 ret = lttng_dynamic_buffer_append(&payload->buffer, field->field_name, name_len);
b2d68839
JR
1391 if (ret) {
1392 goto end;
1393 }
1394
1395 size_before_event = payload->buffer.size;
cd9adb8b 1396 ret = lttng_event_serialize(&field->event, 0, nullptr, nullptr, 0, nullptr, payload);
b2d68839
JR
1397 if (ret) {
1398 ret = -1;
1399 goto end;
1400 }
1401
1402 /* Update the event len. */
28ab034a 1403 header = (struct lttng_event_field_comm *) ((char *) payload->buffer.data + header_offset);
b2d68839
JR
1404 header->event_len = payload->buffer.size - size_before_event;
1405
1406end:
1407 return ret;
1408}
1409
28ab034a
JG
1410static enum lttng_error_code compute_flattened_size(struct lttng_dynamic_pointer_array *events,
1411 size_t *size)
8ddd72ef
JR
1412{
1413 enum lttng_error_code ret_code;
1414 int ret = 0;
1415 size_t storage_req, event_count, i;
1416
1417 assert(size);
1418 assert(events);
1419
1420 event_count = lttng_dynamic_pointer_array_get_count(events);
1421
1422 /* The basic struct lttng_event */
1423 storage_req = event_count * sizeof(struct lttng_event);
1424
fc33156e
JG
1425 /* The struct·lttng_event_extended */
1426 storage_req += event_count * sizeof(struct lttng_event_extended);
1427
8ddd72ef
JR
1428 for (i = 0; i < event_count; i++) {
1429 int probe_storage_req = 0;
28ab034a
JG
1430 const struct event_list_element *element =
1431 (const struct event_list_element *) lttng_dynamic_pointer_array_get_pointer(
1432 events, i);
cd9adb8b 1433 const struct lttng_userspace_probe_location *location = nullptr;
8ddd72ef 1434
28ab034a 1435 location = lttng_event_get_userspace_probe_location(element->event);
8ddd72ef 1436 if (location) {
cd9adb8b 1437 ret = lttng_userspace_probe_location_flatten(location, nullptr);
8ddd72ef
JR
1438 if (ret < 0) {
1439 ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL;
1440 goto end;
1441 }
1442
1443 probe_storage_req = ret;
1444 }
1445
8ddd72ef
JR
1446 if (element->filter_expression) {
1447 storage_req += strlen(element->filter_expression) + 1;
1448 }
1449
1450 if (element->exclusions) {
28ab034a 1451 storage_req += element->exclusions->count * LTTNG_SYMBOL_NAME_LEN;
8ddd72ef
JR
1452 }
1453
1454 /* Padding to ensure the flat probe is aligned. */
1455 storage_req = lttng_align_ceil(storage_req, sizeof(uint64_t));
1456 storage_req += probe_storage_req;
1457 }
1458
1459 *size = storage_req;
1460 ret_code = LTTNG_OK;
1461
1462end:
1463 return ret_code;
1464}
1465
1466/*
1467 * Flatten a list of struct lttng_event.
1468 *
1469 * The buffer that is returned to the API client must contain a "flat" version
1470 * of the events that are returned. In other words, all pointers within an
1471 * lttng_event must point to a location within the returned buffer so that the
1472 * user may free everything by simply calling free() on the returned buffer.
1473 * This is needed in order to maintain API compatibility.
1474 *
1475 * A first pass is performed to compute the size of the buffer that must be
1476 * allocated. A second pass is then performed to setup the returned events so
1477 * that their members always point within the buffer.
1478 *
1479 * The layout of the returned buffer is as follows:
1480 * - struct lttng_event[nb_events],
1481 * - nb_events times the following:
1482 * - struct lttng_event_extended,
1483 * - filter_expression
1484 * - exclusions
1485 * - padding to align to 64-bits
1486 * - flattened version of userspace_probe_location
1487 */
28ab034a
JG
1488static enum lttng_error_code flatten_lttng_events(struct lttng_dynamic_pointer_array *events,
1489 struct lttng_event **flattened_events)
8ddd72ef
JR
1490{
1491 enum lttng_error_code ret_code;
1492 int ret, i;
1493 size_t storage_req;
1494 struct lttng_dynamic_buffer local_flattened_events;
1495 int nb_events;
1496
1497 assert(events);
1498 assert(flattened_events);
1499
1500 lttng_dynamic_buffer_init(&local_flattened_events);
1501 nb_events = lttng_dynamic_pointer_array_get_count(events);
1502
1503 ret_code = compute_flattened_size(events, &storage_req);
1504 if (ret_code != LTTNG_OK) {
1505 goto end;
1506 }
1507
1508 /*
1509 * We must ensure that "local_flattened_events" is never resized so as
1510 * to preserve the validity of the flattened objects.
1511 */
28ab034a 1512 ret = lttng_dynamic_buffer_set_capacity(&local_flattened_events, storage_req);
8ddd72ef
JR
1513 if (ret) {
1514 ret_code = LTTNG_ERR_NOMEM;
1515 goto end;
1516 }
1517
1518 /* Start by laying the struct lttng_event */
1519 for (i = 0; i < nb_events; i++) {
28ab034a
JG
1520 const struct event_list_element *element =
1521 (const struct event_list_element *) lttng_dynamic_pointer_array_get_pointer(
1522 events, i);
8ddd72ef
JR
1523
1524 if (!element) {
1525 ret_code = LTTNG_ERR_FATAL;
1526 goto end;
1527 }
1528
28ab034a
JG
1529 ret = lttng_dynamic_buffer_append(
1530 &local_flattened_events, element->event, sizeof(struct lttng_event));
8ddd72ef
JR
1531 if (ret) {
1532 ret_code = LTTNG_ERR_NOMEM;
1533 goto end;
1534 }
1535 }
1536
1537 for (i = 0; i < nb_events; i++) {
28ab034a
JG
1538 const struct event_list_element *element =
1539 (const struct event_list_element *) lttng_dynamic_pointer_array_get_pointer(
1540 events, i);
1541 struct lttng_event *event =
1542 (struct lttng_event *) (local_flattened_events.data +
1543 (sizeof(struct lttng_event) * i));
8ddd72ef 1544 struct lttng_event_extended *event_extended =
28ab034a
JG
1545 (struct lttng_event_extended *) (local_flattened_events.data +
1546 local_flattened_events.size);
cd9adb8b 1547 const struct lttng_userspace_probe_location *location = nullptr;
8ddd72ef
JR
1548
1549 assert(element);
1550
1551 /* Insert struct lttng_event_extended. */
1552 ret = lttng_dynamic_buffer_set_size(&local_flattened_events,
28ab034a
JG
1553 local_flattened_events.size +
1554 sizeof(*event_extended));
8ddd72ef
JR
1555 if (ret) {
1556 ret_code = LTTNG_ERR_NOMEM;
1557 goto end;
1558 }
1559 event->extended.ptr = event_extended;
1560
1561 /* Insert filter expression. */
1562 if (element->filter_expression) {
1563 const size_t len = strlen(element->filter_expression) + 1;
1564
1565 event_extended->filter_expression =
28ab034a 1566 local_flattened_events.data + local_flattened_events.size;
8ddd72ef 1567 ret = lttng_dynamic_buffer_append(
28ab034a 1568 &local_flattened_events, element->filter_expression, len);
8ddd72ef
JR
1569 if (ret) {
1570 ret_code = LTTNG_ERR_NOMEM;
1571 goto end;
1572 }
1573 }
1574
1575 /* Insert exclusions. */
1576 if (element->exclusions) {
28ab034a 1577 event_extended->exclusions.count = element->exclusions->count;
8ddd72ef 1578 event_extended->exclusions.strings =
28ab034a 1579 local_flattened_events.data + local_flattened_events.size;
8ddd72ef 1580
28ab034a
JG
1581 ret = lttng_dynamic_buffer_append(&local_flattened_events,
1582 element->exclusions->names,
1583 element->exclusions->count *
1584 LTTNG_SYMBOL_NAME_LEN);
8ddd72ef
JR
1585 if (ret) {
1586 ret_code = LTTNG_ERR_NOMEM;
1587 goto end;
1588 }
1589 }
1590
1591 /* Insert padding to align to 64-bits. */
1592 ret = lttng_dynamic_buffer_set_size(&local_flattened_events,
28ab034a
JG
1593 lttng_align_ceil(local_flattened_events.size,
1594 sizeof(uint64_t)));
8ddd72ef
JR
1595 if (ret) {
1596 ret_code = LTTNG_ERR_NOMEM;
1597 goto end;
1598 }
1599
28ab034a 1600 location = lttng_event_get_userspace_probe_location(element->event);
8ddd72ef 1601 if (location) {
28ab034a
JG
1602 event_extended->probe_location = (struct lttng_userspace_probe_location
1603 *) (local_flattened_events.data +
1604 local_flattened_events.size);
1605 ret = lttng_userspace_probe_location_flatten(location,
1606 &local_flattened_events);
8ddd72ef
JR
1607 if (ret < 0) {
1608 ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL;
1609 goto end;
1610 }
1611 }
1612 }
1613
1614 /* Don't reset local_flattened_events buffer as we return its content. */
1615 *flattened_events = (struct lttng_event *) local_flattened_events.data;
1616 lttng_dynamic_buffer_init(&local_flattened_events);
1617 ret_code = LTTNG_OK;
1618end:
1619 lttng_dynamic_buffer_reset(&local_flattened_events);
1620 return ret_code;
1621}
1622
28ab034a
JG
1623static enum lttng_error_code
1624event_list_create_from_payload(struct lttng_payload_view *view,
1625 unsigned int count,
1626 struct lttng_dynamic_pointer_array *event_list)
8ddd72ef
JR
1627{
1628 enum lttng_error_code ret_code;
1629 int ret;
1630 unsigned int i;
1631 int offset = 0;
1632
1633 assert(view);
1634 assert(event_list);
1635
1636 for (i = 0; i < count; i++) {
1637 ssize_t event_size;
1638 struct lttng_payload_view event_view =
28ab034a 1639 lttng_payload_view_from_view(view, offset, -1);
64803277 1640 struct event_list_element *element = zmalloc<event_list_element>();
8ddd72ef
JR
1641
1642 if (!element) {
1643 ret_code = LTTNG_ERR_NOMEM;
1644 goto end;
1645 }
1646
1647 /*
1648 * Lifetime and management of the object is now bound to the
1649 * array.
1650 */
28ab034a 1651 ret = lttng_dynamic_pointer_array_add_pointer(event_list, element);
8ddd72ef
JR
1652 if (ret) {
1653 event_list_destructor(element);
1654 ret_code = LTTNG_ERR_NOMEM;
1655 goto end;
1656 }
1657
1658 /*
1659 * Bytecode is not transmitted on listing in any case we do not
1660 * care about it.
1661 */
1662 event_size = lttng_event_create_from_payload(&event_view,
28ab034a
JG
1663 &element->event,
1664 &element->exclusions,
1665 &element->filter_expression,
cd9adb8b 1666 nullptr);
8ddd72ef
JR
1667 if (event_size < 0) {
1668 ret_code = LTTNG_ERR_INVALID;
1669 goto end;
1670 }
1671
1672 offset += event_size;
1673 }
1674
1675 if (view->buffer.size != offset) {
1676 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
1677 goto end;
1678 }
1679
1680 ret_code = LTTNG_OK;
1681
1682end:
1683 return ret_code;
1684}
1685
1686enum lttng_error_code lttng_events_create_and_flatten_from_payload(
28ab034a 1687 struct lttng_payload_view *payload, unsigned int count, struct lttng_event **events)
8ddd72ef
JR
1688{
1689 enum lttng_error_code ret = LTTNG_OK;
1690 struct lttng_dynamic_pointer_array local_events;
1691
1692 lttng_dynamic_pointer_array_init(&local_events, event_list_destructor);
1693
1694 /* Deserialize the events. */
1695 {
1696 struct lttng_payload_view events_view =
28ab034a 1697 lttng_payload_view_from_view(payload, 0, -1);
8ddd72ef 1698
28ab034a 1699 ret = event_list_create_from_payload(&events_view, count, &local_events);
8ddd72ef
JR
1700 if (ret != LTTNG_OK) {
1701 goto end;
1702 }
1703 }
1704
1705 ret = flatten_lttng_events(&local_events, events);
1706 if (ret != LTTNG_OK) {
1707 goto end;
1708 }
1709
1710end:
1711 lttng_dynamic_pointer_array_reset(&local_events);
1712 return ret;
1713}
b2d68839 1714
28ab034a
JG
1715static enum lttng_error_code
1716flatten_lttng_event_fields(struct lttng_dynamic_pointer_array *event_fields,
1717 struct lttng_event_field **flattened_event_fields)
b2d68839
JR
1718{
1719 int ret, i;
1720 enum lttng_error_code ret_code;
1721 size_t storage_req = 0;
1722 struct lttng_dynamic_buffer local_flattened_event_fields;
1723 int nb_event_field;
1724
1725 assert(event_fields);
1726 assert(flattened_event_fields);
1727
1728 lttng_dynamic_buffer_init(&local_flattened_event_fields);
1729 nb_event_field = lttng_dynamic_pointer_array_get_count(event_fields);
1730
1731 /*
1732 * Here even if the event field contains a `struct lttng_event` that
1733 * could contain dynamic data, in reality it is not the case.
1734 * Dynamic data is not present. Here the flattening is mostly a direct
1735 * memcpy. This is less than ideal but this code is still better than
1736 * direct usage of an unpacked lttng_event_field array.
1737 */
1738 storage_req += sizeof(struct lttng_event_field) * nb_event_field;
1739
1740 lttng_dynamic_buffer_init(&local_flattened_event_fields);
1741
1742 /*
1743 * We must ensure that "local_flattened_event_fields" is never resized
1744 * so as to preserve the validity of the flattened objects.
1745 */
28ab034a 1746 ret = lttng_dynamic_buffer_set_capacity(&local_flattened_event_fields, storage_req);
b2d68839
JR
1747 if (ret) {
1748 ret_code = LTTNG_ERR_NOMEM;
1749 goto end;
1750 }
1751
1752 for (i = 0; i < nb_event_field; i++) {
1753 const struct lttng_event_field *element =
28ab034a
JG
1754 (const struct lttng_event_field *) lttng_dynamic_pointer_array_get_pointer(
1755 event_fields, i);
b2d68839
JR
1756
1757 if (!element) {
1758 ret_code = LTTNG_ERR_FATAL;
1759 goto end;
1760 }
28ab034a
JG
1761 ret = lttng_dynamic_buffer_append(
1762 &local_flattened_event_fields, element, sizeof(struct lttng_event_field));
b2d68839
JR
1763 if (ret) {
1764 ret_code = LTTNG_ERR_NOMEM;
1765 goto end;
1766 }
1767 }
1768
1769 /* Don't reset local_flattened_channels buffer as we return its content. */
1770 *flattened_event_fields = (struct lttng_event_field *) local_flattened_event_fields.data;
1771 lttng_dynamic_buffer_init(&local_flattened_event_fields);
1772 ret_code = LTTNG_OK;
1773end:
1774 lttng_dynamic_buffer_reset(&local_flattened_event_fields);
1775 return ret_code;
1776}
1777
28ab034a
JG
1778static enum lttng_error_code
1779event_field_list_create_from_payload(struct lttng_payload_view *view,
1780 unsigned int count,
1781 struct lttng_dynamic_pointer_array **event_field_list)
b2d68839
JR
1782{
1783 enum lttng_error_code ret_code;
1784 int ret, offset = 0;
1785 unsigned int i;
cd9adb8b 1786 struct lttng_dynamic_pointer_array *list = nullptr;
b2d68839
JR
1787
1788 assert(view);
1789 assert(event_field_list);
1790
64803277 1791 list = zmalloc<lttng_dynamic_pointer_array>();
b2d68839
JR
1792 if (!list) {
1793 ret_code = LTTNG_ERR_NOMEM;
1794 goto end;
1795 }
1796
1797 lttng_dynamic_pointer_array_init(list, free);
1798
1799 for (i = 0; i < count; i++) {
1800 ssize_t event_field_size;
cd9adb8b 1801 struct lttng_event_field *field = nullptr;
b2d68839 1802 struct lttng_payload_view event_field_view =
28ab034a 1803 lttng_payload_view_from_view(view, offset, -1);
b2d68839 1804
28ab034a 1805 event_field_size = lttng_event_field_create_from_payload(&event_field_view, &field);
b2d68839
JR
1806 if (event_field_size < 0) {
1807 ret_code = LTTNG_ERR_INVALID;
1808 goto end;
1809 }
1810
1811 /* Lifetime and management of the object is now bound to the array. */
1812 ret = lttng_dynamic_pointer_array_add_pointer(list, field);
1813 if (ret) {
1814 free(field);
1815 ret_code = LTTNG_ERR_NOMEM;
1816 goto end;
1817 }
1818
1819 offset += event_field_size;
1820 }
1821
1822 if (view->buffer.size != offset) {
1823 ret_code = LTTNG_ERR_INVALID;
1824 goto end;
1825 }
1826
1827 *event_field_list = list;
cd9adb8b 1828 list = nullptr;
b2d68839
JR
1829 ret_code = LTTNG_OK;
1830
1831end:
1832 if (list) {
1833 lttng_dynamic_pointer_array_reset(list);
1834 free(list);
1835 }
1836
1837 return ret_code;
1838}
1839
1840enum lttng_error_code lttng_event_fields_create_and_flatten_from_payload(
28ab034a 1841 struct lttng_payload_view *view, unsigned int count, struct lttng_event_field **fields)
b2d68839
JR
1842{
1843 enum lttng_error_code ret_code;
cd9adb8b 1844 struct lttng_dynamic_pointer_array *local_event_fields = nullptr;
b2d68839 1845
28ab034a 1846 ret_code = event_field_list_create_from_payload(view, count, &local_event_fields);
b2d68839
JR
1847 if (ret_code != LTTNG_OK) {
1848 goto end;
1849 }
1850
1851 ret_code = flatten_lttng_event_fields(local_event_fields, fields);
1852 if (ret_code != LTTNG_OK) {
1853 goto end;
1854 }
1855end:
1856 if (local_event_fields) {
1857 lttng_dynamic_pointer_array_reset(local_event_fields);
1858 free(local_event_fields);
1859 }
1860
1861 return ret_code;
1862}
This page took 0.142648 seconds and 4 git commands to generate.