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