2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
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>
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>
24 struct event_list_element
{
25 struct lttng_event
*event
;
26 struct lttng_event_exclusion
*exclusions
;
27 char *filter_expression
;
31 static void event_list_destructor(void *ptr
)
33 struct event_list_element
*element
= (struct event_list_element
*) ptr
;
35 free(element
->filter_expression
);
36 free(element
->exclusions
);
37 lttng_event_destroy(element
->event
);
41 struct lttng_event
*lttng_event_copy(const struct lttng_event
*event
)
43 struct lttng_event
*new_event
;
44 struct lttng_event_extended
*new_event_extended
;
46 new_event
= zmalloc
<lttng_event
>();
48 PERROR("Error allocating event structure");
52 /* Copy the content of the old event. */
53 memcpy(new_event
, event
, sizeof(*event
));
56 * We need to create a new extended since the previous pointer is now
59 new_event_extended
= zmalloc
<lttng_event_extended
>();
60 if (!new_event_extended
) {
61 PERROR("Error allocating event extended structure");
65 new_event
->extended
.ptr
= new_event_extended
;
74 static int lttng_event_probe_attr_serialize(const struct lttng_event_probe_attr
*probe
,
75 struct lttng_payload
*payload
)
78 size_t symbol_name_len
;
79 struct lttng_event_probe_attr_comm comm
= {};
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. */
88 /* Include the null terminator. */
91 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
92 comm
.addr
= probe
->addr
;
93 comm
.offset
= probe
->addr
;
95 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
, sizeof(comm
));
101 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, probe
->symbol_name
, symbol_name_len
);
106 static int lttng_event_function_attr_serialize(const struct lttng_event_function_attr
*function
,
107 struct lttng_payload
*payload
)
110 size_t symbol_name_len
;
111 struct lttng_event_function_attr_comm comm
;
113 comm
.symbol_name_len
= 0;
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. */
122 /* Include the null terminator. */
123 symbol_name_len
+= 1;
125 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
127 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
, sizeof(comm
));
133 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, function
->symbol_name
, symbol_name_len
);
139 lttng_event_probe_attr_create_from_payload(struct lttng_payload_view
*view
,
140 struct lttng_event_probe_attr
**probe_attr
)
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
));
148 if (!lttng_payload_view_is_valid(&comm_view
)) {
153 comm
= (typeof(comm
)) comm_view
.buffer
.data
;
154 offset
+= sizeof(*comm
);
156 local_attr
= zmalloc
<lttng_event_probe_attr
>();
157 if (local_attr
== nullptr) {
162 local_attr
->addr
= comm
->addr
;
163 local_attr
->offset
= comm
->offset
;
167 struct lttng_payload_view name_view
=
168 lttng_payload_view_from_view(view
, offset
, comm
->symbol_name_len
);
170 if (!lttng_payload_view_is_valid(&name_view
)) {
175 name
= name_view
.buffer
.data
;
177 if (!lttng_buffer_view_contains_string(
178 &name_view
.buffer
, name
, comm
->symbol_name_len
)) {
183 ret
= lttng_strncpy(local_attr
->symbol_name
, name
, sizeof(local_attr
->symbol_name
));
189 offset
+= comm
->symbol_name_len
;
192 *probe_attr
= local_attr
;
193 local_attr
= nullptr;
201 lttng_event_function_attr_create_from_payload(struct lttng_payload_view
*view
,
202 struct lttng_event_function_attr
**function_attr
)
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
));
210 if (!lttng_payload_view_is_valid(&comm_view
)) {
215 comm
= (typeof(comm
)) view
->buffer
.data
;
216 offset
+= sizeof(*comm
);
218 local_attr
= zmalloc
<lttng_event_function_attr
>();
219 if (local_attr
== nullptr) {
226 struct lttng_payload_view name_view
=
227 lttng_payload_view_from_view(view
, offset
, comm
->symbol_name_len
);
229 if (!lttng_payload_view_is_valid(&name_view
)) {
234 name
= name_view
.buffer
.data
;
236 if (!lttng_buffer_view_contains_string(
237 &name_view
.buffer
, name
, comm
->symbol_name_len
)) {
242 ret
= lttng_strncpy(local_attr
->symbol_name
, name
, sizeof(local_attr
->symbol_name
));
248 offset
+= comm
->symbol_name_len
;
251 *function_attr
= local_attr
;
252 local_attr
= nullptr;
259 static ssize_t
lttng_event_exclusions_create_from_payload(struct lttng_payload_view
*view
,
261 struct lttng_event_exclusion
**exclusions
)
263 ssize_t ret
, offset
= 0;
264 const size_t size
= (count
* LTTNG_SYMBOL_NAME_LEN
);
266 const struct lttng_event_exclusion_comm
*comm
;
267 struct lttng_event_exclusion
*local_exclusions
;
270 zmalloc
<lttng_event_exclusion
>(sizeof(struct lttng_event_exclusion
) + size
);
271 if (!local_exclusions
) {
276 local_exclusions
->count
= count
;
278 for (i
= 0; i
< count
; i
++) {
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
));
284 if (!lttng_buffer_view_is_valid(&comm_view
)) {
289 comm
= (typeof(comm
)) comm_view
.data
;
290 offset
+= sizeof(*comm
);
292 string_view
= lttng_buffer_view_from_view(&view
->buffer
, offset
, comm
->len
);
294 if (!lttng_buffer_view_is_valid(&string_view
)) {
299 string
= string_view
.data
;
301 if (!lttng_buffer_view_contains_string(&string_view
, string
, comm
->len
)) {
306 ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(local_exclusions
, i
),
308 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(local_exclusions
, i
)));
317 *exclusions
= local_exclusions
;
318 local_exclusions
= nullptr;
321 free(local_exclusions
);
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
)
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;
342 * Only event is obligatory, the other output argument are optional and
343 * depends on what the caller is interested in.
349 struct lttng_payload_view comm_view
=
350 lttng_payload_view_from_view(view
, offset
, sizeof(*event_comm
));
352 if (!lttng_payload_view_is_valid(&comm_view
)) {
357 /* lttng_event_comm header */
358 event_comm
= (typeof(event_comm
)) comm_view
.buffer
.data
;
359 offset
+= sizeof(*event_comm
);
362 local_event
= lttng_event_create();
363 if (local_event
== nullptr) {
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
;
377 const struct lttng_buffer_view name_view
=
378 lttng_buffer_view_from_view(&view
->buffer
, offset
, event_comm
->name_len
);
380 if (!lttng_buffer_view_is_valid(&name_view
)) {
385 name
= (const char *) name_view
.data
;
387 if (!lttng_buffer_view_contains_string(&name_view
, name
, event_comm
->name_len
)) {
392 ret
= lttng_strncpy(local_event
->name
, name
, sizeof(local_event
->name
));
398 offset
+= event_comm
->name_len
;
402 if (event_comm
->exclusion_count
== 0) {
403 goto deserialize_filter_expression
;
407 struct lttng_payload_view exclusions_view
=
408 lttng_payload_view_from_view(view
, offset
, -1);
410 if (!lttng_payload_view_is_valid(&exclusions_view
)) {
415 ret
= lttng_event_exclusions_create_from_payload(
416 &exclusions_view
, event_comm
->exclusion_count
, &local_exclusions
);
423 local_event
->exclusion
= 1;
426 deserialize_filter_expression
:
428 if (event_comm
->filter_expression_len
== 0) {
429 if (event_comm
->bytecode_len
!= 0) {
431 * This is an invalid event payload.
433 * Filter expression without bytecode is possible but
434 * not the other way around.
439 goto deserialize_event_type_payload
;
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
);
447 if (!lttng_buffer_view_is_valid(&filter_expression_view
)) {
452 filter_expression_buffer
= filter_expression_view
.data
;
454 if (!lttng_buffer_view_contains_string(&filter_expression_view
,
455 filter_expression_buffer
,
456 event_comm
->filter_expression_len
)) {
461 local_filter_expression
=
462 lttng_strndup(filter_expression_buffer
, event_comm
->filter_expression_len
);
463 if (!local_filter_expression
) {
468 local_event
->filter
= 1;
470 offset
+= event_comm
->filter_expression_len
;
473 if (event_comm
->bytecode_len
== 0) {
475 * Filter expression can be present but without bytecode
476 * when dealing with event listing.
478 goto deserialize_event_type_payload
;
483 struct lttng_payload_view bytecode_view
=
484 lttng_payload_view_from_view(view
, offset
, event_comm
->bytecode_len
);
486 if (!lttng_payload_view_is_valid(&bytecode_view
)) {
491 local_bytecode
= zmalloc
<lttng_bytecode
>(event_comm
->bytecode_len
);
492 if (!local_bytecode
) {
497 memcpy(local_bytecode
, bytecode_view
.buffer
.data
, event_comm
->bytecode_len
);
498 if ((local_bytecode
->len
+ sizeof(*local_bytecode
)) != event_comm
->bytecode_len
) {
503 offset
+= event_comm
->bytecode_len
;
506 deserialize_event_type_payload
:
507 /* Event type specific payload */
508 switch (local_event
->type
) {
509 case LTTNG_EVENT_FUNCTION
:
511 case LTTNG_EVENT_PROBE
:
513 struct lttng_payload_view probe_attr_view
= lttng_payload_view_from_view(
514 view
, offset
, event_comm
->lttng_event_probe_attr_len
);
516 if (event_comm
->lttng_event_probe_attr_len
== 0) {
521 if (!lttng_payload_view_is_valid(&probe_attr_view
)) {
526 ret
= lttng_event_probe_attr_create_from_payload(&probe_attr_view
,
528 if (ret
< 0 || ret
!= event_comm
->lttng_event_probe_attr_len
) {
533 /* Copy to the local event. */
534 memcpy(&local_event
->attr
.probe
, local_probe_attr
, sizeof(local_event
->attr
.probe
));
539 case LTTNG_EVENT_FUNCTION_ENTRY
:
541 struct lttng_payload_view function_attr_view
= lttng_payload_view_from_view(
542 view
, offset
, event_comm
->lttng_event_function_attr_len
);
544 if (event_comm
->lttng_event_function_attr_len
== 0) {
549 if (!lttng_payload_view_is_valid(&function_attr_view
)) {
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
) {
561 /* Copy to the local event. */
562 memcpy(&local_event
->attr
.ftrace
,
564 sizeof(local_event
->attr
.ftrace
));
570 case LTTNG_EVENT_USERSPACE_PROBE
:
572 struct lttng_payload_view userspace_probe_location_view
=
573 lttng_payload_view_from_view(
574 view
, offset
, event_comm
->userspace_probe_location_len
);
576 if (event_comm
->userspace_probe_location_len
== 0) {
581 if (!lttng_payload_view_is_valid(&userspace_probe_location_view
)) {
586 ret
= lttng_userspace_probe_location_create_from_payload(
587 &userspace_probe_location_view
, &local_userspace_probe_location
);
589 WARN("Failed to create a userspace probe location from the received buffer");
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
,
603 /* Attach the probe location to the event. */
604 ret
= lttng_event_set_userspace_probe_location(local_event
,
605 local_userspace_probe_location
);
607 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
612 * Userspace probe location object ownership transfered to the
615 local_userspace_probe_location
= nullptr;
616 offset
+= event_comm
->userspace_probe_location_len
;
619 case LTTNG_EVENT_TRACEPOINT
:
621 case LTTNG_EVENT_ALL
:
623 case LTTNG_EVENT_SYSCALL
:
625 case LTTNG_EVENT_NOOP
:
626 /* Nothing to do here */
634 /* Transfer ownership to the caller. */
635 *out_event
= local_event
;
636 local_event
= nullptr;
639 *out_bytecode
= local_bytecode
;
640 local_bytecode
= nullptr;
644 *out_exclusion
= local_exclusions
;
645 local_exclusions
= nullptr;
648 if (out_filter_expression
) {
649 *out_filter_expression
= local_filter_expression
;
650 local_filter_expression
= nullptr;
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
);
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
,
670 struct lttng_bytecode
*bytecode
,
671 struct lttng_payload
*payload
)
675 size_t header_offset
, size_before_payload
;
677 struct lttng_event_comm event_comm
= {};
678 struct lttng_event_comm
*header
;
682 assert(exclusion_count
== 0 || exclusion_list
);
684 /* Save the header location for later in-place header update. */
685 header_offset
= payload
->buffer
.size
;
687 name_len
= lttng_strnlen(event
->name
, sizeof(event
->name
));
688 if (name_len
== sizeof(event
->name
)) {
689 /* Event name is not NULL-terminated. */
694 /* Add null termination. */
697 if (exclusion_count
> UINT32_MAX
) {
698 /* Possible overflow. */
703 if (bytecode_len
> UINT32_MAX
) {
704 /* Possible overflow. */
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
;
719 if (filter_expression
) {
720 event_comm
.filter_expression_len
= strlen(filter_expression
) + 1;
724 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event_comm
, sizeof(event_comm
));
730 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, event
->name
, name_len
);
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
= {};
741 exclusion_header
.len
= (uint32_t) exclusion_len
+ 1;
743 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
744 /* Exclusion is not NULL-terminated. */
749 ret
= lttng_dynamic_buffer_append(
750 &payload
->buffer
, &exclusion_header
, sizeof(exclusion_header
));
755 ret
= lttng_dynamic_buffer_append(
756 &payload
->buffer
, *(exclusion_list
+ i
), exclusion_len
+ 1);
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
);
771 * Bytecode can be absent when we serialize to the client
775 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, bytecode
, bytecode_len
);
782 size_before_payload
= payload
->buffer
.size
;
784 /* Event type specific payload */
785 switch (event
->type
) {
786 case LTTNG_EVENT_FUNCTION
:
788 case LTTNG_EVENT_PROBE
:
789 ret
= lttng_event_probe_attr_serialize(&event
->attr
.probe
, payload
);
796 (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
797 header
->lttng_event_probe_attr_len
= payload
->buffer
.size
- size_before_payload
;
800 case LTTNG_EVENT_FUNCTION_ENTRY
:
801 ret
= lttng_event_function_attr_serialize(&event
->attr
.ftrace
, payload
);
807 /* Update the lttng_event_function_attr len. */
809 (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
810 header
->lttng_event_function_attr_len
= payload
->buffer
.size
- size_before_payload
;
813 case LTTNG_EVENT_USERSPACE_PROBE
:
815 const struct lttng_event_extended
*ev_ext
=
816 (const struct lttng_event_extended
*) event
->extended
.ptr
;
818 assert(event
->extended
.ptr
);
819 assert(ev_ext
->probe_location
);
821 size_before_payload
= payload
->buffer
.size
;
822 if (ev_ext
->probe_location
) {
824 * lttng_userspace_probe_location_serialize returns the
825 * number of bytes that were appended to the buffer.
827 ret
= lttng_userspace_probe_location_serialize(ev_ext
->probe_location
,
835 /* Update the userspace probe location len. */
836 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
838 header
->userspace_probe_location_len
=
839 payload
->buffer
.size
- size_before_payload
;
843 case LTTNG_EVENT_TRACEPOINT
:
845 case LTTNG_EVENT_ALL
:
848 /* Nothing to do here. */
856 static ssize_t
lttng_event_context_app_populate_from_payload(const struct lttng_payload_view
*view
,
857 struct lttng_event_context
*event_ctx
)
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
));
866 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
868 if (!lttng_buffer_view_is_valid(&comm_view
)) {
873 comm
= (typeof(comm
)) comm_view
.data
;
874 offset
+= sizeof(*comm
);
876 provider_name_len
= comm
->provider_name_len
;
877 context_name_len
= comm
->ctx_name_len
;
879 if (provider_name_len
== 0 || context_name_len
== 0) {
881 * Application provider and context names MUST
890 const struct lttng_buffer_view provider_name_view
=
891 lttng_buffer_view_from_view(&view
->buffer
, offset
, provider_name_len
);
893 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
898 name
= provider_name_view
.data
;
900 if (!lttng_buffer_view_contains_string(
901 &provider_name_view
, name
, provider_name_len
)) {
906 provider_name
= lttng_strndup(name
, provider_name_len
);
907 if (!provider_name
) {
912 offset
+= provider_name_len
;
917 const struct lttng_buffer_view context_name_view
=
918 lttng_buffer_view_from_view(&view
->buffer
, offset
, context_name_len
);
920 if (!lttng_buffer_view_is_valid(&context_name_view
)) {
925 name
= context_name_view
.data
;
927 if (!lttng_buffer_view_contains_string(&context_name_view
, name
, context_name_len
)) {
932 context_name
= lttng_strndup(name
, context_name_len
);
938 offset
+= context_name_len
;
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;
956 lttng_event_context_perf_counter_populate_from_payload(const struct lttng_payload_view
*view
,
957 struct lttng_event_context
*event_ctx
)
960 ssize_t consumed
, offset
= 0;
961 const struct lttng_event_context_perf_counter_comm
*comm
;
963 const struct lttng_buffer_view comm_view
=
964 lttng_buffer_view_from_view(&view
->buffer
, offset
, sizeof(*comm
));
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
);
970 if (!lttng_buffer_view_is_valid(&comm_view
)) {
975 comm
= (typeof(comm
)) comm_view
.data
;
976 offset
+= sizeof(*comm
);
978 name_len
= comm
->name_len
;
982 const struct lttng_buffer_view provider_name_view
=
983 lttng_buffer_view_from_view(&view
->buffer
, offset
, name_len
);
985 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
990 name
= provider_name_view
.data
;
992 if (!lttng_buffer_view_contains_string(&provider_name_view
, name
, name_len
)) {
997 ret
= lttng_strncpy(event_ctx
->u
.perf_counter
.name
,
999 sizeof(event_ctx
->u
.perf_counter
.name
));
1007 event_ctx
->u
.perf_counter
.config
= comm
->config
;
1008 event_ctx
->u
.perf_counter
.type
= comm
->type
;
1016 ssize_t
lttng_event_context_create_from_payload(struct lttng_payload_view
*view
,
1017 struct lttng_event_context
**event_ctx
)
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
));
1028 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1033 comm
= (typeof(comm
)) comm_view
.data
;
1034 offset
+= sizeof(*comm
);
1036 local_context
= zmalloc
<lttng_event_context
>();
1037 if (!local_context
) {
1042 local_context
->ctx
= (lttng_event_context_type
) comm
->type
;
1045 struct lttng_payload_view subtype_view
=
1046 lttng_payload_view_from_view(view
, offset
, -1);
1048 switch (local_context
->ctx
) {
1049 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1050 ret
= lttng_event_context_app_populate_from_payload(&subtype_view
,
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
,
1060 /* Nothing else to deserialize. */
1072 *event_ctx
= local_context
;
1073 local_context
= nullptr;
1077 free(local_context
);
1081 static int lttng_event_context_app_serialize(struct lttng_event_context
*context
,
1082 struct lttng_payload
*payload
)
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
;
1092 assert(context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1094 provider_name
= context
->u
.app_ctx
.provider_name
;
1095 ctx_name
= context
->u
.app_ctx
.ctx_name
;
1097 if (!provider_name
|| !ctx_name
) {
1098 ret
= -LTTNG_ERR_INVALID
;
1102 provider_len
= strlen(provider_name
);
1103 if (provider_len
== 0) {
1104 ret
= -LTTNG_ERR_INVALID
;
1108 /* Include the null terminator. */
1110 comm
.provider_name_len
= provider_len
;
1112 ctx_len
= strlen(ctx_name
);
1114 ret
= -LTTNG_ERR_INVALID
;
1118 /* Include the null terminator. */
1120 comm
.ctx_name_len
= ctx_len
;
1123 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
, sizeof(comm
));
1129 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, provider_name
, provider_len
);
1135 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, ctx_name
, ctx_len
);
1145 static int lttng_event_context_perf_counter_serialize(struct lttng_event_perf_counter_ctx
*context
,
1146 struct lttng_payload
*payload
)
1149 struct lttng_event_context_perf_counter_comm comm
= {};
1154 comm
.config
= context
->config
;
1155 comm
.type
= context
->type
;
1156 comm
.name_len
= lttng_strnlen(context
->name
, sizeof(context
->name
));
1158 if (comm
.name_len
== sizeof(context
->name
)) {
1163 /* Include the null terminator. */
1167 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
, sizeof(comm
));
1173 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, context
->name
, comm
.name_len
);
1183 int lttng_event_context_serialize(struct lttng_event_context
*context
,
1184 struct lttng_payload
*payload
)
1187 struct lttng_event_context_comm context_comm
;
1189 context_comm
.type
= 0;
1194 context_comm
.type
= (uint32_t) context
->ctx
;
1197 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &context_comm
, sizeof(context_comm
));
1202 switch (context
->ctx
) {
1203 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1204 ret
= lttng_event_context_app_serialize(context
, payload
);
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
);
1212 /* Nothing else to serialize. */
1224 void lttng_event_context_destroy(struct lttng_event_context
*context
)
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
);
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.
1243 static void lttng_event_field_populate_lttng_event_from_event(const struct lttng_event
*src
,
1244 struct lttng_event
*destination
)
1246 memcpy(destination
, src
, sizeof(*destination
));
1248 /* Remove all possible dynamic data from the destination event rule. */
1249 destination
->extended
.ptr
= nullptr;
1252 ssize_t
lttng_event_field_create_from_payload(struct lttng_payload_view
*view
,
1253 struct lttng_event_field
**field
)
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;
1265 const struct lttng_buffer_view comm_view
=
1266 lttng_buffer_view_from_view(&view
->buffer
, offset
, sizeof(*comm
));
1268 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1273 /* lttng_event_field_comm header */
1274 comm
= (const lttng_event_field_comm
*) comm_view
.data
;
1275 offset
+= sizeof(*comm
);
1278 local_event_field
= zmalloc
<lttng_event_field
>();
1279 if (!local_event_field
) {
1284 local_event_field
->type
= (lttng_event_field_type
) comm
->type
;
1285 local_event_field
->nowrite
= comm
->nowrite
;
1289 const struct lttng_buffer_view name_view
=
1290 lttng_buffer_view_from_view(&view
->buffer
, offset
, comm
->name_len
);
1292 if (!lttng_buffer_view_is_valid(&name_view
)) {
1297 name
= name_view
.data
;
1299 if (!lttng_buffer_view_contains_string(&name_view
, name_view
.data
, comm
->name_len
)) {
1304 if (comm
->name_len
> LTTNG_SYMBOL_NAME_LEN
- 1) {
1305 /* Name is too long.*/
1310 offset
+= comm
->name_len
;
1315 struct lttng_payload_view event_view
=
1316 lttng_payload_view_from_view(view
, offset
, comm
->event_len
);
1318 if (!lttng_payload_view_is_valid(&event_view
)) {
1323 ret
= lttng_event_create_from_payload(
1324 &event_view
, &event
, nullptr, nullptr, nullptr);
1325 if (ret
!= comm
->event_len
) {
1337 local_event_field
->field_name
, name
, sizeof(local_event_field
->field_name
))) {
1342 lttng_event_field_populate_lttng_event_from_event(event
, &local_event_field
->event
);
1344 *field
= local_event_field
;
1345 local_event_field
= nullptr;
1348 lttng_event_destroy(event
);
1349 free(local_event_field
);
1353 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1354 struct lttng_payload
*payload
)
1357 size_t header_offset
, size_before_event
;
1359 struct lttng_event_field_comm event_field_comm
= {};
1360 struct lttng_event_field_comm
*header
;
1365 /* Save the header location for later in-place header update. */
1366 header_offset
= payload
->buffer
.size
;
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. */
1375 /* Add null termination. */
1378 event_field_comm
.type
= field
->type
;
1379 event_field_comm
.nowrite
= (uint8_t) field
->nowrite
;
1380 event_field_comm
.name_len
= name_len
;
1383 ret
= lttng_dynamic_buffer_append(
1384 &payload
->buffer
, &event_field_comm
, sizeof(event_field_comm
));
1390 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, field
->field_name
, name_len
);
1395 size_before_event
= payload
->buffer
.size
;
1396 ret
= lttng_event_serialize(&field
->event
, 0, nullptr, nullptr, 0, nullptr, payload
);
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
;
1410 static enum lttng_error_code
compute_flattened_size(struct lttng_dynamic_pointer_array
*events
,
1413 enum lttng_error_code ret_code
;
1415 size_t storage_req
, event_count
, i
;
1420 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1422 /* The basic struct lttng_event */
1423 storage_req
= event_count
* sizeof(struct lttng_event
);
1425 /* The struct·lttng_event_extended */
1426 storage_req
+= event_count
* sizeof(struct lttng_event_extended
);
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(
1433 const struct lttng_userspace_probe_location
*location
= nullptr;
1435 location
= lttng_event_get_userspace_probe_location(element
->event
);
1437 ret
= lttng_userspace_probe_location_flatten(location
, nullptr);
1439 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1443 probe_storage_req
= ret
;
1446 if (element
->filter_expression
) {
1447 storage_req
+= strlen(element
->filter_expression
) + 1;
1450 if (element
->exclusions
) {
1451 storage_req
+= element
->exclusions
->count
* LTTNG_SYMBOL_NAME_LEN
;
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
;
1459 *size
= storage_req
;
1460 ret_code
= LTTNG_OK
;
1467 * Flatten a list of struct lttng_event.
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.
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.
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
1485 * - padding to align to 64-bits
1486 * - flattened version of userspace_probe_location
1488 static enum lttng_error_code
flatten_lttng_events(struct lttng_dynamic_pointer_array
*events
,
1489 struct lttng_event
**flattened_events
)
1491 enum lttng_error_code ret_code
;
1494 struct lttng_dynamic_buffer local_flattened_events
;
1498 assert(flattened_events
);
1500 lttng_dynamic_buffer_init(&local_flattened_events
);
1501 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1503 ret_code
= compute_flattened_size(events
, &storage_req
);
1504 if (ret_code
!= LTTNG_OK
) {
1509 * We must ensure that "local_flattened_events" is never resized so as
1510 * to preserve the validity of the flattened objects.
1512 ret
= lttng_dynamic_buffer_set_capacity(&local_flattened_events
, storage_req
);
1514 ret_code
= LTTNG_ERR_NOMEM
;
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(
1525 ret_code
= LTTNG_ERR_FATAL
;
1529 ret
= lttng_dynamic_buffer_append(
1530 &local_flattened_events
, element
->event
, sizeof(struct lttng_event
));
1532 ret_code
= LTTNG_ERR_NOMEM
;
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(
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;
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
));
1556 ret_code
= LTTNG_ERR_NOMEM
;
1559 event
->extended
.ptr
= event_extended
;
1561 /* Insert filter expression. */
1562 if (element
->filter_expression
) {
1563 const size_t len
= strlen(element
->filter_expression
) + 1;
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
);
1570 ret_code
= LTTNG_ERR_NOMEM
;
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
;
1581 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1582 element
->exclusions
->names
,
1583 element
->exclusions
->count
*
1584 LTTNG_SYMBOL_NAME_LEN
);
1586 ret_code
= LTTNG_ERR_NOMEM
;
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
,
1596 ret_code
= LTTNG_ERR_NOMEM
;
1600 location
= lttng_event_get_userspace_probe_location(element
->event
);
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
);
1608 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
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
;
1619 lttng_dynamic_buffer_reset(&local_flattened_events
);
1623 static enum lttng_error_code
1624 event_list_create_from_payload(struct lttng_payload_view
*view
,
1626 struct lttng_dynamic_pointer_array
*event_list
)
1628 enum lttng_error_code ret_code
;
1636 for (i
= 0; i
< count
; i
++) {
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
>();
1643 ret_code
= LTTNG_ERR_NOMEM
;
1648 * Lifetime and management of the object is now bound to the
1651 ret
= lttng_dynamic_pointer_array_add_pointer(event_list
, element
);
1653 event_list_destructor(element
);
1654 ret_code
= LTTNG_ERR_NOMEM
;
1659 * Bytecode is not transmitted on listing in any case we do not
1662 event_size
= lttng_event_create_from_payload(&event_view
,
1664 &element
->exclusions
,
1665 &element
->filter_expression
,
1667 if (event_size
< 0) {
1668 ret_code
= LTTNG_ERR_INVALID
;
1672 offset
+= event_size
;
1675 if (view
->buffer
.size
!= offset
) {
1676 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
1680 ret_code
= LTTNG_OK
;
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
)
1689 enum lttng_error_code ret
= LTTNG_OK
;
1690 struct lttng_dynamic_pointer_array local_events
;
1692 lttng_dynamic_pointer_array_init(&local_events
, event_list_destructor
);
1694 /* Deserialize the events. */
1696 struct lttng_payload_view events_view
=
1697 lttng_payload_view_from_view(payload
, 0, -1);
1699 ret
= event_list_create_from_payload(&events_view
, count
, &local_events
);
1700 if (ret
!= LTTNG_OK
) {
1705 ret
= flatten_lttng_events(&local_events
, events
);
1706 if (ret
!= LTTNG_OK
) {
1711 lttng_dynamic_pointer_array_reset(&local_events
);
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
)
1720 enum lttng_error_code ret_code
;
1721 size_t storage_req
= 0;
1722 struct lttng_dynamic_buffer local_flattened_event_fields
;
1725 assert(event_fields
);
1726 assert(flattened_event_fields
);
1728 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1729 nb_event_field
= lttng_dynamic_pointer_array_get_count(event_fields
);
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.
1738 storage_req
+= sizeof(struct lttng_event_field
) * nb_event_field
;
1740 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1743 * We must ensure that "local_flattened_event_fields" is never resized
1744 * so as to preserve the validity of the flattened objects.
1746 ret
= lttng_dynamic_buffer_set_capacity(&local_flattened_event_fields
, storage_req
);
1748 ret_code
= LTTNG_ERR_NOMEM
;
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(
1758 ret_code
= LTTNG_ERR_FATAL
;
1761 ret
= lttng_dynamic_buffer_append(
1762 &local_flattened_event_fields
, element
, sizeof(struct lttng_event_field
));
1764 ret_code
= LTTNG_ERR_NOMEM
;
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
;
1774 lttng_dynamic_buffer_reset(&local_flattened_event_fields
);
1778 static enum lttng_error_code
1779 event_field_list_create_from_payload(struct lttng_payload_view
*view
,
1781 struct lttng_dynamic_pointer_array
**event_field_list
)
1783 enum lttng_error_code ret_code
;
1784 int ret
, offset
= 0;
1786 struct lttng_dynamic_pointer_array
*list
= nullptr;
1789 assert(event_field_list
);
1791 list
= zmalloc
<lttng_dynamic_pointer_array
>();
1793 ret_code
= LTTNG_ERR_NOMEM
;
1797 lttng_dynamic_pointer_array_init(list
, free
);
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);
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
;
1811 /* Lifetime and management of the object is now bound to the array. */
1812 ret
= lttng_dynamic_pointer_array_add_pointer(list
, field
);
1815 ret_code
= LTTNG_ERR_NOMEM
;
1819 offset
+= event_field_size
;
1822 if (view
->buffer
.size
!= offset
) {
1823 ret_code
= LTTNG_ERR_INVALID
;
1827 *event_field_list
= list
;
1829 ret_code
= LTTNG_OK
;
1833 lttng_dynamic_pointer_array_reset(list
);
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
)
1843 enum lttng_error_code ret_code
;
1844 struct lttng_dynamic_pointer_array
*local_event_fields
= nullptr;
1846 ret_code
= event_field_list_create_from_payload(view
, count
, &local_event_fields
);
1847 if (ret_code
!= LTTNG_OK
) {
1851 ret_code
= flatten_lttng_event_fields(local_event_fields
, fields
);
1852 if (ret_code
!= LTTNG_OK
) {
1856 if (local_event_fields
) {
1857 lttng_dynamic_pointer_array_reset(local_event_fields
);
1858 free(local_event_fields
);