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