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