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
=
371 local_event
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_ALL
? -1 : event_comm
->loglevel
;
372 local_event
->enabled
= !!event_comm
->enabled
;
373 local_event
->pid
= event_comm
->pid
;
374 local_event
->flags
= (enum lttng_event_flag
) event_comm
->flags
;
378 const struct lttng_buffer_view name_view
=
379 lttng_buffer_view_from_view(&view
->buffer
, offset
, event_comm
->name_len
);
381 if (!lttng_buffer_view_is_valid(&name_view
)) {
386 name
= (const char *) name_view
.data
;
388 if (!lttng_buffer_view_contains_string(&name_view
, name
, event_comm
->name_len
)) {
393 ret
= lttng_strncpy(local_event
->name
, name
, sizeof(local_event
->name
));
399 offset
+= event_comm
->name_len
;
403 if (event_comm
->exclusion_count
== 0) {
404 goto deserialize_filter_expression
;
408 struct lttng_payload_view exclusions_view
=
409 lttng_payload_view_from_view(view
, offset
, -1);
411 if (!lttng_payload_view_is_valid(&exclusions_view
)) {
416 ret
= lttng_event_exclusions_create_from_payload(
417 &exclusions_view
, event_comm
->exclusion_count
, &local_exclusions
);
424 local_event
->exclusion
= 1;
427 deserialize_filter_expression
:
429 if (event_comm
->filter_expression_len
== 0) {
430 if (event_comm
->bytecode_len
!= 0) {
432 * This is an invalid event payload.
434 * Filter expression without bytecode is possible but
435 * not the other way around.
440 goto deserialize_event_type_payload
;
444 const char *filter_expression_buffer
;
445 struct lttng_buffer_view filter_expression_view
= lttng_buffer_view_from_view(
446 &view
->buffer
, offset
, event_comm
->filter_expression_len
);
448 if (!lttng_buffer_view_is_valid(&filter_expression_view
)) {
453 filter_expression_buffer
= filter_expression_view
.data
;
455 if (!lttng_buffer_view_contains_string(&filter_expression_view
,
456 filter_expression_buffer
,
457 event_comm
->filter_expression_len
)) {
462 local_filter_expression
=
463 lttng_strndup(filter_expression_buffer
, event_comm
->filter_expression_len
);
464 if (!local_filter_expression
) {
469 local_event
->filter
= 1;
471 offset
+= event_comm
->filter_expression_len
;
474 if (event_comm
->bytecode_len
== 0) {
476 * Filter expression can be present but without bytecode
477 * when dealing with event listing.
479 goto deserialize_event_type_payload
;
484 struct lttng_payload_view bytecode_view
=
485 lttng_payload_view_from_view(view
, offset
, event_comm
->bytecode_len
);
487 if (!lttng_payload_view_is_valid(&bytecode_view
)) {
492 local_bytecode
= zmalloc
<lttng_bytecode
>(event_comm
->bytecode_len
);
493 if (!local_bytecode
) {
498 memcpy(local_bytecode
, bytecode_view
.buffer
.data
, event_comm
->bytecode_len
);
499 if ((local_bytecode
->len
+ sizeof(*local_bytecode
)) != event_comm
->bytecode_len
) {
504 offset
+= event_comm
->bytecode_len
;
507 deserialize_event_type_payload
:
508 /* Event type specific payload */
509 switch (local_event
->type
) {
510 case LTTNG_EVENT_FUNCTION
:
512 case LTTNG_EVENT_PROBE
:
514 struct lttng_payload_view probe_attr_view
= lttng_payload_view_from_view(
515 view
, offset
, event_comm
->lttng_event_probe_attr_len
);
517 if (event_comm
->lttng_event_probe_attr_len
== 0) {
522 if (!lttng_payload_view_is_valid(&probe_attr_view
)) {
527 ret
= lttng_event_probe_attr_create_from_payload(&probe_attr_view
,
529 if (ret
< 0 || ret
!= event_comm
->lttng_event_probe_attr_len
) {
534 /* Copy to the local event. */
535 memcpy(&local_event
->attr
.probe
, local_probe_attr
, sizeof(local_event
->attr
.probe
));
540 case LTTNG_EVENT_FUNCTION_ENTRY
:
542 struct lttng_payload_view function_attr_view
= lttng_payload_view_from_view(
543 view
, offset
, event_comm
->lttng_event_function_attr_len
);
545 if (event_comm
->lttng_event_function_attr_len
== 0) {
550 if (!lttng_payload_view_is_valid(&function_attr_view
)) {
555 ret
= lttng_event_function_attr_create_from_payload(&function_attr_view
,
556 &local_function_attr
);
557 if (ret
< 0 || ret
!= event_comm
->lttng_event_function_attr_len
) {
562 /* Copy to the local event. */
563 memcpy(&local_event
->attr
.ftrace
,
565 sizeof(local_event
->attr
.ftrace
));
571 case LTTNG_EVENT_USERSPACE_PROBE
:
573 struct lttng_payload_view userspace_probe_location_view
=
574 lttng_payload_view_from_view(
575 view
, offset
, event_comm
->userspace_probe_location_len
);
577 if (event_comm
->userspace_probe_location_len
== 0) {
582 if (!lttng_payload_view_is_valid(&userspace_probe_location_view
)) {
587 ret
= lttng_userspace_probe_location_create_from_payload(
588 &userspace_probe_location_view
, &local_userspace_probe_location
);
590 WARN("Failed to create a userspace probe location from the received buffer");
595 if (ret
!= event_comm
->userspace_probe_location_len
) {
596 WARN("Userspace probe location from the received buffer is not the advertised length: header length = %" PRIu32
597 ", payload length = %zd",
598 event_comm
->userspace_probe_location_len
,
604 /* Attach the probe location to the event. */
605 ret
= lttng_event_set_userspace_probe_location(local_event
,
606 local_userspace_probe_location
);
608 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
613 * Userspace probe location object ownership transfered to the
616 local_userspace_probe_location
= nullptr;
617 offset
+= event_comm
->userspace_probe_location_len
;
620 case LTTNG_EVENT_TRACEPOINT
:
622 case LTTNG_EVENT_ALL
:
624 case LTTNG_EVENT_SYSCALL
:
626 case LTTNG_EVENT_NOOP
:
627 /* Nothing to do here */
635 /* Transfer ownership to the caller. */
636 *out_event
= local_event
;
637 local_event
= nullptr;
640 *out_bytecode
= local_bytecode
;
641 local_bytecode
= nullptr;
645 *out_exclusion
= local_exclusions
;
646 local_exclusions
= nullptr;
649 if (out_filter_expression
) {
650 *out_filter_expression
= local_filter_expression
;
651 local_filter_expression
= nullptr;
656 lttng_event_destroy(local_event
);
657 lttng_userspace_probe_location_destroy(local_userspace_probe_location
);
658 free(local_filter_expression
);
659 free(local_exclusions
);
660 free(local_bytecode
);
661 free(local_function_attr
);
662 free(local_probe_attr
);
666 int lttng_event_serialize(const struct lttng_event
*event
,
667 unsigned int exclusion_count
,
668 const char *const *exclusion_list
,
669 const char *filter_expression
,
671 struct lttng_bytecode
*bytecode
,
672 struct lttng_payload
*payload
)
676 size_t header_offset
, size_before_payload
;
678 struct lttng_event_comm event_comm
= {};
679 struct lttng_event_comm
*header
;
683 assert(exclusion_count
== 0 || exclusion_list
);
685 /* Save the header location for later in-place header update. */
686 header_offset
= payload
->buffer
.size
;
688 name_len
= lttng_strnlen(event
->name
, sizeof(event
->name
));
689 if (name_len
== sizeof(event
->name
)) {
690 /* Event name is not NULL-terminated. */
695 /* Add null termination. */
698 if (exclusion_count
> UINT32_MAX
) {
699 /* Possible overflow. */
704 if (bytecode_len
> UINT32_MAX
) {
705 /* Possible overflow. */
710 event_comm
.name_len
= (uint32_t) name_len
;
711 event_comm
.event_type
= (int8_t) event
->type
;
712 event_comm
.loglevel_type
= (int8_t) event
->loglevel_type
;
713 event_comm
.loglevel
= (int32_t) event
->loglevel
;
714 event_comm
.enabled
= (int8_t) event
->enabled
;
715 event_comm
.pid
= (int32_t) event
->pid
;
716 event_comm
.exclusion_count
= (uint32_t) exclusion_count
;
717 event_comm
.bytecode_len
= (uint32_t) bytecode_len
;
718 event_comm
.flags
= (int32_t) event
->flags
;
720 if (filter_expression
) {
721 event_comm
.filter_expression_len
= strlen(filter_expression
) + 1;
725 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event_comm
, sizeof(event_comm
));
731 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, event
->name
, name_len
);
737 for (i
= 0; i
< exclusion_count
; i
++) {
738 const size_t exclusion_len
=
739 lttng_strnlen(*(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
740 struct lttng_event_exclusion_comm exclusion_header
= {};
742 exclusion_header
.len
= (uint32_t) exclusion_len
+ 1;
744 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
745 /* Exclusion is not NULL-terminated. */
750 ret
= lttng_dynamic_buffer_append(
751 &payload
->buffer
, &exclusion_header
, sizeof(exclusion_header
));
756 ret
= lttng_dynamic_buffer_append(
757 &payload
->buffer
, *(exclusion_list
+ i
), exclusion_len
+ 1);
763 /* Filter expression and its bytecode */
764 if (filter_expression
) {
765 ret
= lttng_dynamic_buffer_append(
766 &payload
->buffer
, filter_expression
, event_comm
.filter_expression_len
);
772 * Bytecode can be absent when we serialize to the client
776 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, bytecode
, bytecode_len
);
783 size_before_payload
= payload
->buffer
.size
;
785 /* Event type specific payload */
786 switch (event
->type
) {
787 case LTTNG_EVENT_FUNCTION
:
789 case LTTNG_EVENT_PROBE
:
790 ret
= lttng_event_probe_attr_serialize(&event
->attr
.probe
, payload
);
797 (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
798 header
->lttng_event_probe_attr_len
= payload
->buffer
.size
- size_before_payload
;
801 case LTTNG_EVENT_FUNCTION_ENTRY
:
802 ret
= lttng_event_function_attr_serialize(&event
->attr
.ftrace
, payload
);
808 /* Update the lttng_event_function_attr len. */
810 (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
811 header
->lttng_event_function_attr_len
= payload
->buffer
.size
- size_before_payload
;
814 case LTTNG_EVENT_USERSPACE_PROBE
:
816 const struct lttng_event_extended
*ev_ext
=
817 (const struct lttng_event_extended
*) event
->extended
.ptr
;
819 assert(event
->extended
.ptr
);
820 assert(ev_ext
->probe_location
);
822 size_before_payload
= payload
->buffer
.size
;
823 if (ev_ext
->probe_location
) {
825 * lttng_userspace_probe_location_serialize returns the
826 * number of bytes that were appended to the buffer.
828 ret
= lttng_userspace_probe_location_serialize(ev_ext
->probe_location
,
836 /* Update the userspace probe location len. */
837 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
839 header
->userspace_probe_location_len
=
840 payload
->buffer
.size
- size_before_payload
;
844 case LTTNG_EVENT_TRACEPOINT
:
846 case LTTNG_EVENT_ALL
:
849 /* Nothing to do here. */
857 static ssize_t
lttng_event_context_app_populate_from_payload(const struct lttng_payload_view
*view
,
858 struct lttng_event_context
*event_ctx
)
860 ssize_t ret
, offset
= 0;
861 const struct lttng_event_context_app_comm
*comm
;
862 char *provider_name
= nullptr, *context_name
= nullptr;
863 size_t provider_name_len
, context_name_len
;
864 const struct lttng_buffer_view comm_view
=
865 lttng_buffer_view_from_view(&view
->buffer
, offset
, sizeof(*comm
));
867 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
869 if (!lttng_buffer_view_is_valid(&comm_view
)) {
874 comm
= (typeof(comm
)) comm_view
.data
;
875 offset
+= sizeof(*comm
);
877 provider_name_len
= comm
->provider_name_len
;
878 context_name_len
= comm
->ctx_name_len
;
880 if (provider_name_len
== 0 || context_name_len
== 0) {
882 * Application provider and context names MUST
891 const struct lttng_buffer_view provider_name_view
=
892 lttng_buffer_view_from_view(&view
->buffer
, offset
, provider_name_len
);
894 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
899 name
= provider_name_view
.data
;
901 if (!lttng_buffer_view_contains_string(
902 &provider_name_view
, name
, provider_name_len
)) {
907 provider_name
= lttng_strndup(name
, provider_name_len
);
908 if (!provider_name
) {
913 offset
+= provider_name_len
;
918 const struct lttng_buffer_view context_name_view
=
919 lttng_buffer_view_from_view(&view
->buffer
, offset
, context_name_len
);
921 if (!lttng_buffer_view_is_valid(&context_name_view
)) {
926 name
= context_name_view
.data
;
928 if (!lttng_buffer_view_contains_string(&context_name_view
, name
, context_name_len
)) {
933 context_name
= lttng_strndup(name
, context_name_len
);
939 offset
+= context_name_len
;
942 /* Transfer ownership of the strings */
943 event_ctx
->u
.app_ctx
.provider_name
= provider_name
;
944 event_ctx
->u
.app_ctx
.ctx_name
= context_name
;
945 provider_name
= nullptr;
946 context_name
= nullptr;
957 lttng_event_context_perf_counter_populate_from_payload(const struct lttng_payload_view
*view
,
958 struct lttng_event_context
*event_ctx
)
961 ssize_t consumed
, offset
= 0;
962 const struct lttng_event_context_perf_counter_comm
*comm
;
964 const struct lttng_buffer_view comm_view
=
965 lttng_buffer_view_from_view(&view
->buffer
, offset
, sizeof(*comm
));
967 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_COUNTER
||
968 event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
||
969 event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
);
971 if (!lttng_buffer_view_is_valid(&comm_view
)) {
976 comm
= (typeof(comm
)) comm_view
.data
;
977 offset
+= sizeof(*comm
);
979 name_len
= comm
->name_len
;
983 const struct lttng_buffer_view provider_name_view
=
984 lttng_buffer_view_from_view(&view
->buffer
, offset
, name_len
);
986 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
991 name
= provider_name_view
.data
;
993 if (!lttng_buffer_view_contains_string(&provider_name_view
, name
, name_len
)) {
998 ret
= lttng_strncpy(event_ctx
->u
.perf_counter
.name
,
1000 sizeof(event_ctx
->u
.perf_counter
.name
));
1008 event_ctx
->u
.perf_counter
.config
= comm
->config
;
1009 event_ctx
->u
.perf_counter
.type
= comm
->type
;
1017 ssize_t
lttng_event_context_create_from_payload(struct lttng_payload_view
*view
,
1018 struct lttng_event_context
**event_ctx
)
1020 ssize_t ret
, offset
= 0;
1021 const struct lttng_event_context_comm
*comm
;
1022 struct lttng_event_context
*local_context
= nullptr;
1023 struct lttng_buffer_view comm_view
=
1024 lttng_buffer_view_from_view(&view
->buffer
, offset
, sizeof(*comm
));
1029 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1034 comm
= (typeof(comm
)) comm_view
.data
;
1035 offset
+= sizeof(*comm
);
1037 local_context
= zmalloc
<lttng_event_context
>();
1038 if (!local_context
) {
1043 local_context
->ctx
= (lttng_event_context_type
) comm
->type
;
1046 struct lttng_payload_view subtype_view
=
1047 lttng_payload_view_from_view(view
, offset
, -1);
1049 switch (local_context
->ctx
) {
1050 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1051 ret
= lttng_event_context_app_populate_from_payload(&subtype_view
,
1054 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1055 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1056 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1057 ret
= lttng_event_context_perf_counter_populate_from_payload(&subtype_view
,
1061 /* Nothing else to deserialize. */
1073 *event_ctx
= local_context
;
1074 local_context
= nullptr;
1078 free(local_context
);
1082 static int lttng_event_context_app_serialize(struct lttng_event_context
*context
,
1083 struct lttng_payload
*payload
)
1086 struct lttng_event_context_app_comm comm
= {};
1087 size_t provider_len
, ctx_len
;
1088 const char *provider_name
;
1089 const char *ctx_name
;
1093 assert(context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1095 provider_name
= context
->u
.app_ctx
.provider_name
;
1096 ctx_name
= context
->u
.app_ctx
.ctx_name
;
1098 if (!provider_name
|| !ctx_name
) {
1099 ret
= -LTTNG_ERR_INVALID
;
1103 provider_len
= strlen(provider_name
);
1104 if (provider_len
== 0) {
1105 ret
= -LTTNG_ERR_INVALID
;
1109 /* Include the null terminator. */
1111 comm
.provider_name_len
= provider_len
;
1113 ctx_len
= strlen(ctx_name
);
1115 ret
= -LTTNG_ERR_INVALID
;
1119 /* Include the null terminator. */
1121 comm
.ctx_name_len
= ctx_len
;
1124 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
, sizeof(comm
));
1130 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, provider_name
, provider_len
);
1136 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, ctx_name
, ctx_len
);
1146 static int lttng_event_context_perf_counter_serialize(struct lttng_event_perf_counter_ctx
*context
,
1147 struct lttng_payload
*payload
)
1150 struct lttng_event_context_perf_counter_comm comm
= {};
1155 comm
.config
= context
->config
;
1156 comm
.type
= context
->type
;
1157 comm
.name_len
= lttng_strnlen(context
->name
, sizeof(context
->name
));
1159 if (comm
.name_len
== sizeof(context
->name
)) {
1164 /* Include the null terminator. */
1168 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
, sizeof(comm
));
1174 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, context
->name
, comm
.name_len
);
1184 int lttng_event_context_serialize(struct lttng_event_context
*context
,
1185 struct lttng_payload
*payload
)
1188 struct lttng_event_context_comm context_comm
;
1190 context_comm
.type
= 0;
1195 context_comm
.type
= (uint32_t) context
->ctx
;
1198 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &context_comm
, sizeof(context_comm
));
1203 switch (context
->ctx
) {
1204 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1205 ret
= lttng_event_context_app_serialize(context
, payload
);
1207 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1208 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1209 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1210 ret
= lttng_event_context_perf_counter_serialize(&context
->u
.perf_counter
, payload
);
1213 /* Nothing else to serialize. */
1225 void lttng_event_context_destroy(struct lttng_event_context
*context
)
1231 if (context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1232 free(context
->u
.app_ctx
.provider_name
);
1233 free(context
->u
.app_ctx
.ctx_name
);
1240 * This is a specialized populate for lttng_event_field since it ignores
1241 * the extension field of the lttng_event struct and simply copies what it can
1242 * to the internal struct lttng_event of a lttng_event_field.
1244 static void lttng_event_field_populate_lttng_event_from_event(const struct lttng_event
*src
,
1245 struct lttng_event
*destination
)
1247 memcpy(destination
, src
, sizeof(*destination
));
1249 /* Remove all possible dynamic data from the destination event rule. */
1250 destination
->extended
.ptr
= nullptr;
1253 ssize_t
lttng_event_field_create_from_payload(struct lttng_payload_view
*view
,
1254 struct lttng_event_field
**field
)
1256 ssize_t ret
, offset
= 0;
1257 struct lttng_event_field
*local_event_field
= nullptr;
1258 struct lttng_event
*event
= nullptr;
1259 const struct lttng_event_field_comm
*comm
;
1260 const char *name
= nullptr;
1266 const struct lttng_buffer_view comm_view
=
1267 lttng_buffer_view_from_view(&view
->buffer
, offset
, sizeof(*comm
));
1269 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1274 /* lttng_event_field_comm header */
1275 comm
= (const lttng_event_field_comm
*) comm_view
.data
;
1276 offset
+= sizeof(*comm
);
1279 local_event_field
= zmalloc
<lttng_event_field
>();
1280 if (!local_event_field
) {
1285 local_event_field
->type
= (lttng_event_field_type
) comm
->type
;
1286 local_event_field
->nowrite
= comm
->nowrite
;
1290 const struct lttng_buffer_view name_view
=
1291 lttng_buffer_view_from_view(&view
->buffer
, offset
, comm
->name_len
);
1293 if (!lttng_buffer_view_is_valid(&name_view
)) {
1298 name
= name_view
.data
;
1300 if (!lttng_buffer_view_contains_string(&name_view
, name_view
.data
, comm
->name_len
)) {
1305 if (comm
->name_len
> LTTNG_SYMBOL_NAME_LEN
- 1) {
1306 /* Name is too long.*/
1311 offset
+= comm
->name_len
;
1316 struct lttng_payload_view event_view
=
1317 lttng_payload_view_from_view(view
, offset
, comm
->event_len
);
1319 if (!lttng_payload_view_is_valid(&event_view
)) {
1324 ret
= lttng_event_create_from_payload(
1325 &event_view
, &event
, nullptr, nullptr, nullptr);
1326 if (ret
!= comm
->event_len
) {
1338 local_event_field
->field_name
, name
, sizeof(local_event_field
->field_name
))) {
1343 lttng_event_field_populate_lttng_event_from_event(event
, &local_event_field
->event
);
1345 *field
= local_event_field
;
1346 local_event_field
= nullptr;
1349 lttng_event_destroy(event
);
1350 free(local_event_field
);
1354 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1355 struct lttng_payload
*payload
)
1358 size_t header_offset
, size_before_event
;
1360 struct lttng_event_field_comm event_field_comm
= {};
1361 struct lttng_event_field_comm
*header
;
1366 /* Save the header location for later in-place header update. */
1367 header_offset
= payload
->buffer
.size
;
1369 name_len
= strnlen(field
->field_name
, sizeof(field
->field_name
));
1370 if (name_len
== sizeof(field
->field_name
)) {
1371 /* Event name is not NULL-terminated. */
1376 /* Add null termination. */
1379 event_field_comm
.type
= field
->type
;
1380 event_field_comm
.nowrite
= (uint8_t) field
->nowrite
;
1381 event_field_comm
.name_len
= name_len
;
1384 ret
= lttng_dynamic_buffer_append(
1385 &payload
->buffer
, &event_field_comm
, sizeof(event_field_comm
));
1391 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, field
->field_name
, name_len
);
1396 size_before_event
= payload
->buffer
.size
;
1397 ret
= lttng_event_serialize(&field
->event
, 0, nullptr, nullptr, 0, nullptr, payload
);
1403 /* Update the event len. */
1404 header
= (struct lttng_event_field_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
1405 header
->event_len
= payload
->buffer
.size
- size_before_event
;
1411 static enum lttng_error_code
compute_flattened_size(struct lttng_dynamic_pointer_array
*events
,
1414 enum lttng_error_code ret_code
;
1416 size_t storage_req
, event_count
, i
;
1421 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1423 /* The basic struct lttng_event */
1424 storage_req
= event_count
* sizeof(struct lttng_event
);
1426 /* The struct·lttng_event_extended */
1427 storage_req
+= event_count
* sizeof(struct lttng_event_extended
);
1429 for (i
= 0; i
< event_count
; i
++) {
1430 int probe_storage_req
= 0;
1431 const struct event_list_element
*element
=
1432 (const struct event_list_element
*) lttng_dynamic_pointer_array_get_pointer(
1434 const struct lttng_userspace_probe_location
*location
= nullptr;
1436 location
= lttng_event_get_userspace_probe_location(element
->event
);
1438 ret
= lttng_userspace_probe_location_flatten(location
, nullptr);
1440 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1444 probe_storage_req
= ret
;
1447 if (element
->filter_expression
) {
1448 storage_req
+= strlen(element
->filter_expression
) + 1;
1451 if (element
->exclusions
) {
1452 storage_req
+= element
->exclusions
->count
* LTTNG_SYMBOL_NAME_LEN
;
1455 /* Padding to ensure the flat probe is aligned. */
1456 storage_req
= lttng_align_ceil(storage_req
, sizeof(uint64_t));
1457 storage_req
+= probe_storage_req
;
1460 *size
= storage_req
;
1461 ret_code
= LTTNG_OK
;
1468 * Flatten a list of struct lttng_event.
1470 * The buffer that is returned to the API client must contain a "flat" version
1471 * of the events that are returned. In other words, all pointers within an
1472 * lttng_event must point to a location within the returned buffer so that the
1473 * user may free everything by simply calling free() on the returned buffer.
1474 * This is needed in order to maintain API compatibility.
1476 * A first pass is performed to compute the size of the buffer that must be
1477 * allocated. A second pass is then performed to setup the returned events so
1478 * that their members always point within the buffer.
1480 * The layout of the returned buffer is as follows:
1481 * - struct lttng_event[nb_events],
1482 * - nb_events times the following:
1483 * - struct lttng_event_extended,
1484 * - filter_expression
1486 * - padding to align to 64-bits
1487 * - flattened version of userspace_probe_location
1489 static enum lttng_error_code
flatten_lttng_events(struct lttng_dynamic_pointer_array
*events
,
1490 struct lttng_event
**flattened_events
)
1492 enum lttng_error_code ret_code
;
1495 struct lttng_dynamic_buffer local_flattened_events
;
1499 assert(flattened_events
);
1501 lttng_dynamic_buffer_init(&local_flattened_events
);
1502 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1504 ret_code
= compute_flattened_size(events
, &storage_req
);
1505 if (ret_code
!= LTTNG_OK
) {
1510 * We must ensure that "local_flattened_events" is never resized so as
1511 * to preserve the validity of the flattened objects.
1513 ret
= lttng_dynamic_buffer_set_capacity(&local_flattened_events
, storage_req
);
1515 ret_code
= LTTNG_ERR_NOMEM
;
1519 /* Start by laying the struct lttng_event */
1520 for (i
= 0; i
< nb_events
; i
++) {
1521 const struct event_list_element
*element
=
1522 (const struct event_list_element
*) lttng_dynamic_pointer_array_get_pointer(
1526 ret_code
= LTTNG_ERR_FATAL
;
1530 ret
= lttng_dynamic_buffer_append(
1531 &local_flattened_events
, element
->event
, sizeof(struct lttng_event
));
1533 ret_code
= LTTNG_ERR_NOMEM
;
1538 for (i
= 0; i
< nb_events
; i
++) {
1539 const struct event_list_element
*element
=
1540 (const struct event_list_element
*) lttng_dynamic_pointer_array_get_pointer(
1542 struct lttng_event
*event
=
1543 (struct lttng_event
*) (local_flattened_events
.data
+
1544 (sizeof(struct lttng_event
) * i
));
1545 struct lttng_event_extended
*event_extended
=
1546 (struct lttng_event_extended
*) (local_flattened_events
.data
+
1547 local_flattened_events
.size
);
1548 const struct lttng_userspace_probe_location
*location
= nullptr;
1552 /* Insert struct lttng_event_extended. */
1553 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1554 local_flattened_events
.size
+
1555 sizeof(*event_extended
));
1557 ret_code
= LTTNG_ERR_NOMEM
;
1560 event
->extended
.ptr
= event_extended
;
1562 /* Insert filter expression. */
1563 if (element
->filter_expression
) {
1564 const size_t len
= strlen(element
->filter_expression
) + 1;
1566 event_extended
->filter_expression
=
1567 local_flattened_events
.data
+ local_flattened_events
.size
;
1568 ret
= lttng_dynamic_buffer_append(
1569 &local_flattened_events
, element
->filter_expression
, len
);
1571 ret_code
= LTTNG_ERR_NOMEM
;
1576 /* Insert exclusions. */
1577 if (element
->exclusions
) {
1578 event_extended
->exclusions
.count
= element
->exclusions
->count
;
1579 event_extended
->exclusions
.strings
=
1580 local_flattened_events
.data
+ local_flattened_events
.size
;
1582 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1583 element
->exclusions
->names
,
1584 element
->exclusions
->count
*
1585 LTTNG_SYMBOL_NAME_LEN
);
1587 ret_code
= LTTNG_ERR_NOMEM
;
1592 /* Insert padding to align to 64-bits. */
1593 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1594 lttng_align_ceil(local_flattened_events
.size
,
1597 ret_code
= LTTNG_ERR_NOMEM
;
1601 location
= lttng_event_get_userspace_probe_location(element
->event
);
1603 event_extended
->probe_location
= (struct lttng_userspace_probe_location
1604 *) (local_flattened_events
.data
+
1605 local_flattened_events
.size
);
1606 ret
= lttng_userspace_probe_location_flatten(location
,
1607 &local_flattened_events
);
1609 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1615 /* Don't reset local_flattened_events buffer as we return its content. */
1616 *flattened_events
= (struct lttng_event
*) local_flattened_events
.data
;
1617 lttng_dynamic_buffer_init(&local_flattened_events
);
1618 ret_code
= LTTNG_OK
;
1620 lttng_dynamic_buffer_reset(&local_flattened_events
);
1624 static enum lttng_error_code
1625 event_list_create_from_payload(struct lttng_payload_view
*view
,
1627 struct lttng_dynamic_pointer_array
*event_list
)
1629 enum lttng_error_code ret_code
;
1637 for (i
= 0; i
< count
; i
++) {
1639 struct lttng_payload_view event_view
=
1640 lttng_payload_view_from_view(view
, offset
, -1);
1641 struct event_list_element
*element
= zmalloc
<event_list_element
>();
1644 ret_code
= LTTNG_ERR_NOMEM
;
1649 * Lifetime and management of the object is now bound to the
1652 ret
= lttng_dynamic_pointer_array_add_pointer(event_list
, element
);
1654 event_list_destructor(element
);
1655 ret_code
= LTTNG_ERR_NOMEM
;
1660 * Bytecode is not transmitted on listing in any case we do not
1663 event_size
= lttng_event_create_from_payload(&event_view
,
1665 &element
->exclusions
,
1666 &element
->filter_expression
,
1668 if (event_size
< 0) {
1669 ret_code
= LTTNG_ERR_INVALID
;
1673 offset
+= event_size
;
1676 if (view
->buffer
.size
!= offset
) {
1677 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
1681 ret_code
= LTTNG_OK
;
1687 enum lttng_error_code
lttng_events_create_and_flatten_from_payload(
1688 struct lttng_payload_view
*payload
, unsigned int count
, struct lttng_event
**events
)
1690 enum lttng_error_code ret
= LTTNG_OK
;
1691 struct lttng_dynamic_pointer_array local_events
;
1693 lttng_dynamic_pointer_array_init(&local_events
, event_list_destructor
);
1695 /* Deserialize the events. */
1697 struct lttng_payload_view events_view
=
1698 lttng_payload_view_from_view(payload
, 0, -1);
1700 ret
= event_list_create_from_payload(&events_view
, count
, &local_events
);
1701 if (ret
!= LTTNG_OK
) {
1706 ret
= flatten_lttng_events(&local_events
, events
);
1707 if (ret
!= LTTNG_OK
) {
1712 lttng_dynamic_pointer_array_reset(&local_events
);
1716 static enum lttng_error_code
1717 flatten_lttng_event_fields(struct lttng_dynamic_pointer_array
*event_fields
,
1718 struct lttng_event_field
**flattened_event_fields
)
1721 enum lttng_error_code ret_code
;
1722 size_t storage_req
= 0;
1723 struct lttng_dynamic_buffer local_flattened_event_fields
;
1726 assert(event_fields
);
1727 assert(flattened_event_fields
);
1729 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1730 nb_event_field
= lttng_dynamic_pointer_array_get_count(event_fields
);
1733 * Here even if the event field contains a `struct lttng_event` that
1734 * could contain dynamic data, in reality it is not the case.
1735 * Dynamic data is not present. Here the flattening is mostly a direct
1736 * memcpy. This is less than ideal but this code is still better than
1737 * direct usage of an unpacked lttng_event_field array.
1739 storage_req
+= sizeof(struct lttng_event_field
) * nb_event_field
;
1741 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1744 * We must ensure that "local_flattened_event_fields" is never resized
1745 * so as to preserve the validity of the flattened objects.
1747 ret
= lttng_dynamic_buffer_set_capacity(&local_flattened_event_fields
, storage_req
);
1749 ret_code
= LTTNG_ERR_NOMEM
;
1753 for (i
= 0; i
< nb_event_field
; i
++) {
1754 const struct lttng_event_field
*element
=
1755 (const struct lttng_event_field
*) lttng_dynamic_pointer_array_get_pointer(
1759 ret_code
= LTTNG_ERR_FATAL
;
1762 ret
= lttng_dynamic_buffer_append(
1763 &local_flattened_event_fields
, element
, sizeof(struct lttng_event_field
));
1765 ret_code
= LTTNG_ERR_NOMEM
;
1770 /* Don't reset local_flattened_channels buffer as we return its content. */
1771 *flattened_event_fields
= (struct lttng_event_field
*) local_flattened_event_fields
.data
;
1772 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1773 ret_code
= LTTNG_OK
;
1775 lttng_dynamic_buffer_reset(&local_flattened_event_fields
);
1779 static enum lttng_error_code
1780 event_field_list_create_from_payload(struct lttng_payload_view
*view
,
1782 struct lttng_dynamic_pointer_array
**event_field_list
)
1784 enum lttng_error_code ret_code
;
1785 int ret
, offset
= 0;
1787 struct lttng_dynamic_pointer_array
*list
= nullptr;
1790 assert(event_field_list
);
1792 list
= zmalloc
<lttng_dynamic_pointer_array
>();
1794 ret_code
= LTTNG_ERR_NOMEM
;
1798 lttng_dynamic_pointer_array_init(list
, free
);
1800 for (i
= 0; i
< count
; i
++) {
1801 ssize_t event_field_size
;
1802 struct lttng_event_field
*field
= nullptr;
1803 struct lttng_payload_view event_field_view
=
1804 lttng_payload_view_from_view(view
, offset
, -1);
1806 event_field_size
= lttng_event_field_create_from_payload(&event_field_view
, &field
);
1807 if (event_field_size
< 0) {
1808 ret_code
= LTTNG_ERR_INVALID
;
1812 /* Lifetime and management of the object is now bound to the array. */
1813 ret
= lttng_dynamic_pointer_array_add_pointer(list
, field
);
1816 ret_code
= LTTNG_ERR_NOMEM
;
1820 offset
+= event_field_size
;
1823 if (view
->buffer
.size
!= offset
) {
1824 ret_code
= LTTNG_ERR_INVALID
;
1828 *event_field_list
= list
;
1830 ret_code
= LTTNG_OK
;
1834 lttng_dynamic_pointer_array_reset(list
);
1841 enum lttng_error_code
lttng_event_fields_create_and_flatten_from_payload(
1842 struct lttng_payload_view
*view
, unsigned int count
, struct lttng_event_field
**fields
)
1844 enum lttng_error_code ret_code
;
1845 struct lttng_dynamic_pointer_array
*local_event_fields
= nullptr;
1847 ret_code
= event_field_list_create_from_payload(view
, count
, &local_event_fields
);
1848 if (ret_code
!= LTTNG_OK
) {
1852 ret_code
= flatten_lttng_event_fields(local_event_fields
, fields
);
1853 if (ret_code
!= LTTNG_OK
) {
1857 if (local_event_fields
) {
1858 lttng_dynamic_pointer_array_reset(local_event_fields
);
1859 free(local_event_fields
);