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