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