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
= NULL
;
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
== NULL
) {
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
;
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
= NULL
;
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
== NULL
) {
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
;
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
= NULL
;
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
= NULL
;
333 struct lttng_event_exclusion
*local_exclusions
= NULL
;
334 struct lttng_bytecode
*local_bytecode
= NULL
;
335 char *local_filter_expression
= NULL
;
336 const struct lttng_event_comm
*event_comm
;
337 struct lttng_event_function_attr
*local_function_attr
= NULL
;
338 struct lttng_event_probe_attr
*local_probe_attr
= NULL
;
339 struct lttng_userspace_probe_location
*local_userspace_probe_location
= NULL
;
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
== NULL
) {
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
= NULL
;
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
;
639 *out_bytecode
= local_bytecode
;
640 local_bytecode
= NULL
;
644 *out_exclusion
= local_exclusions
;
645 local_exclusions
= NULL
;
648 if (out_filter_expression
) {
649 *out_filter_expression
= local_filter_expression
;
650 local_filter_expression
= NULL
;
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 char **exclusion_list
,
668 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
= NULL
, *context_name
= NULL
;
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
= NULL
;
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
= NULL
;
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
= NULL
;
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
= NULL
;
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
= NULL
;
1257 struct lttng_event
*event
= NULL
;
1258 const struct lttng_event_field_comm
*comm
;
1259 const char *name
= NULL
;
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(&event_view
, &event
, NULL
, NULL
, NULL
);
1324 if (ret
!= comm
->event_len
) {
1336 local_event_field
->field_name
, name
, sizeof(local_event_field
->field_name
))) {
1341 lttng_event_field_populate_lttng_event_from_event(event
, &local_event_field
->event
);
1343 *field
= local_event_field
;
1344 local_event_field
= NULL
;
1347 lttng_event_destroy(event
);
1348 free(local_event_field
);
1352 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1353 struct lttng_payload
*payload
)
1356 size_t header_offset
, size_before_event
;
1358 struct lttng_event_field_comm event_field_comm
= {};
1359 struct lttng_event_field_comm
*header
;
1364 /* Save the header location for later in-place header update. */
1365 header_offset
= payload
->buffer
.size
;
1367 name_len
= strnlen(field
->field_name
, sizeof(field
->field_name
));
1368 if (name_len
== sizeof(field
->field_name
)) {
1369 /* Event name is not NULL-terminated. */
1374 /* Add null termination. */
1377 event_field_comm
.type
= field
->type
;
1378 event_field_comm
.nowrite
= (uint8_t) field
->nowrite
;
1379 event_field_comm
.name_len
= name_len
;
1382 ret
= lttng_dynamic_buffer_append(
1383 &payload
->buffer
, &event_field_comm
, sizeof(event_field_comm
));
1389 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, field
->field_name
, name_len
);
1394 size_before_event
= payload
->buffer
.size
;
1395 ret
= lttng_event_serialize(&field
->event
, 0, NULL
, NULL
, 0, 0, payload
);
1401 /* Update the event len. */
1402 header
= (struct lttng_event_field_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
1403 header
->event_len
= payload
->buffer
.size
- size_before_event
;
1409 static enum lttng_error_code
compute_flattened_size(struct lttng_dynamic_pointer_array
*events
,
1412 enum lttng_error_code ret_code
;
1414 size_t storage_req
, event_count
, i
;
1419 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1421 /* The basic struct lttng_event */
1422 storage_req
= event_count
* sizeof(struct lttng_event
);
1424 /* The struct·lttng_event_extended */
1425 storage_req
+= event_count
* sizeof(struct lttng_event_extended
);
1427 for (i
= 0; i
< event_count
; i
++) {
1428 int probe_storage_req
= 0;
1429 const struct event_list_element
*element
=
1430 (const struct event_list_element
*) lttng_dynamic_pointer_array_get_pointer(
1432 const struct lttng_userspace_probe_location
*location
= NULL
;
1434 location
= lttng_event_get_userspace_probe_location(element
->event
);
1436 ret
= lttng_userspace_probe_location_flatten(location
, NULL
);
1438 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1442 probe_storage_req
= ret
;
1445 if (element
->filter_expression
) {
1446 storage_req
+= strlen(element
->filter_expression
) + 1;
1449 if (element
->exclusions
) {
1450 storage_req
+= element
->exclusions
->count
* LTTNG_SYMBOL_NAME_LEN
;
1453 /* Padding to ensure the flat probe is aligned. */
1454 storage_req
= lttng_align_ceil(storage_req
, sizeof(uint64_t));
1455 storage_req
+= probe_storage_req
;
1458 *size
= storage_req
;
1459 ret_code
= LTTNG_OK
;
1466 * Flatten a list of struct lttng_event.
1468 * The buffer that is returned to the API client must contain a "flat" version
1469 * of the events that are returned. In other words, all pointers within an
1470 * lttng_event must point to a location within the returned buffer so that the
1471 * user may free everything by simply calling free() on the returned buffer.
1472 * This is needed in order to maintain API compatibility.
1474 * A first pass is performed to compute the size of the buffer that must be
1475 * allocated. A second pass is then performed to setup the returned events so
1476 * that their members always point within the buffer.
1478 * The layout of the returned buffer is as follows:
1479 * - struct lttng_event[nb_events],
1480 * - nb_events times the following:
1481 * - struct lttng_event_extended,
1482 * - filter_expression
1484 * - padding to align to 64-bits
1485 * - flattened version of userspace_probe_location
1487 static enum lttng_error_code
flatten_lttng_events(struct lttng_dynamic_pointer_array
*events
,
1488 struct lttng_event
**flattened_events
)
1490 enum lttng_error_code ret_code
;
1493 struct lttng_dynamic_buffer local_flattened_events
;
1497 assert(flattened_events
);
1499 lttng_dynamic_buffer_init(&local_flattened_events
);
1500 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1502 ret_code
= compute_flattened_size(events
, &storage_req
);
1503 if (ret_code
!= LTTNG_OK
) {
1508 * We must ensure that "local_flattened_events" is never resized so as
1509 * to preserve the validity of the flattened objects.
1511 ret
= lttng_dynamic_buffer_set_capacity(&local_flattened_events
, storage_req
);
1513 ret_code
= LTTNG_ERR_NOMEM
;
1517 /* Start by laying the struct lttng_event */
1518 for (i
= 0; i
< nb_events
; i
++) {
1519 const struct event_list_element
*element
=
1520 (const struct event_list_element
*) lttng_dynamic_pointer_array_get_pointer(
1524 ret_code
= LTTNG_ERR_FATAL
;
1528 ret
= lttng_dynamic_buffer_append(
1529 &local_flattened_events
, element
->event
, sizeof(struct lttng_event
));
1531 ret_code
= LTTNG_ERR_NOMEM
;
1536 for (i
= 0; i
< nb_events
; i
++) {
1537 const struct event_list_element
*element
=
1538 (const struct event_list_element
*) lttng_dynamic_pointer_array_get_pointer(
1540 struct lttng_event
*event
=
1541 (struct lttng_event
*) (local_flattened_events
.data
+
1542 (sizeof(struct lttng_event
) * i
));
1543 struct lttng_event_extended
*event_extended
=
1544 (struct lttng_event_extended
*) (local_flattened_events
.data
+
1545 local_flattened_events
.size
);
1546 const struct lttng_userspace_probe_location
*location
= NULL
;
1550 /* Insert struct lttng_event_extended. */
1551 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1552 local_flattened_events
.size
+
1553 sizeof(*event_extended
));
1555 ret_code
= LTTNG_ERR_NOMEM
;
1558 event
->extended
.ptr
= event_extended
;
1560 /* Insert filter expression. */
1561 if (element
->filter_expression
) {
1562 const size_t len
= strlen(element
->filter_expression
) + 1;
1564 event_extended
->filter_expression
=
1565 local_flattened_events
.data
+ local_flattened_events
.size
;
1566 ret
= lttng_dynamic_buffer_append(
1567 &local_flattened_events
, element
->filter_expression
, len
);
1569 ret_code
= LTTNG_ERR_NOMEM
;
1574 /* Insert exclusions. */
1575 if (element
->exclusions
) {
1576 event_extended
->exclusions
.count
= element
->exclusions
->count
;
1577 event_extended
->exclusions
.strings
=
1578 local_flattened_events
.data
+ local_flattened_events
.size
;
1580 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1581 element
->exclusions
->names
,
1582 element
->exclusions
->count
*
1583 LTTNG_SYMBOL_NAME_LEN
);
1585 ret_code
= LTTNG_ERR_NOMEM
;
1590 /* Insert padding to align to 64-bits. */
1591 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1592 lttng_align_ceil(local_flattened_events
.size
,
1595 ret_code
= LTTNG_ERR_NOMEM
;
1599 location
= lttng_event_get_userspace_probe_location(element
->event
);
1601 event_extended
->probe_location
= (struct lttng_userspace_probe_location
1602 *) (local_flattened_events
.data
+
1603 local_flattened_events
.size
);
1604 ret
= lttng_userspace_probe_location_flatten(location
,
1605 &local_flattened_events
);
1607 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1613 /* Don't reset local_flattened_events buffer as we return its content. */
1614 *flattened_events
= (struct lttng_event
*) local_flattened_events
.data
;
1615 lttng_dynamic_buffer_init(&local_flattened_events
);
1616 ret_code
= LTTNG_OK
;
1618 lttng_dynamic_buffer_reset(&local_flattened_events
);
1622 static enum lttng_error_code
1623 event_list_create_from_payload(struct lttng_payload_view
*view
,
1625 struct lttng_dynamic_pointer_array
*event_list
)
1627 enum lttng_error_code ret_code
;
1635 for (i
= 0; i
< count
; i
++) {
1637 struct lttng_payload_view event_view
=
1638 lttng_payload_view_from_view(view
, offset
, -1);
1639 struct event_list_element
*element
= zmalloc
<event_list_element
>();
1642 ret_code
= LTTNG_ERR_NOMEM
;
1647 * Lifetime and management of the object is now bound to the
1650 ret
= lttng_dynamic_pointer_array_add_pointer(event_list
, element
);
1652 event_list_destructor(element
);
1653 ret_code
= LTTNG_ERR_NOMEM
;
1658 * Bytecode is not transmitted on listing in any case we do not
1661 event_size
= lttng_event_create_from_payload(&event_view
,
1663 &element
->exclusions
,
1664 &element
->filter_expression
,
1666 if (event_size
< 0) {
1667 ret_code
= LTTNG_ERR_INVALID
;
1671 offset
+= event_size
;
1674 if (view
->buffer
.size
!= offset
) {
1675 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
1679 ret_code
= LTTNG_OK
;
1685 enum lttng_error_code
lttng_events_create_and_flatten_from_payload(
1686 struct lttng_payload_view
*payload
, unsigned int count
, struct lttng_event
**events
)
1688 enum lttng_error_code ret
= LTTNG_OK
;
1689 struct lttng_dynamic_pointer_array local_events
;
1691 lttng_dynamic_pointer_array_init(&local_events
, event_list_destructor
);
1693 /* Deserialize the events. */
1695 struct lttng_payload_view events_view
=
1696 lttng_payload_view_from_view(payload
, 0, -1);
1698 ret
= event_list_create_from_payload(&events_view
, count
, &local_events
);
1699 if (ret
!= LTTNG_OK
) {
1704 ret
= flatten_lttng_events(&local_events
, events
);
1705 if (ret
!= LTTNG_OK
) {
1710 lttng_dynamic_pointer_array_reset(&local_events
);
1714 static enum lttng_error_code
1715 flatten_lttng_event_fields(struct lttng_dynamic_pointer_array
*event_fields
,
1716 struct lttng_event_field
**flattened_event_fields
)
1719 enum lttng_error_code ret_code
;
1720 size_t storage_req
= 0;
1721 struct lttng_dynamic_buffer local_flattened_event_fields
;
1724 assert(event_fields
);
1725 assert(flattened_event_fields
);
1727 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1728 nb_event_field
= lttng_dynamic_pointer_array_get_count(event_fields
);
1731 * Here even if the event field contains a `struct lttng_event` that
1732 * could contain dynamic data, in reality it is not the case.
1733 * Dynamic data is not present. Here the flattening is mostly a direct
1734 * memcpy. This is less than ideal but this code is still better than
1735 * direct usage of an unpacked lttng_event_field array.
1737 storage_req
+= sizeof(struct lttng_event_field
) * nb_event_field
;
1739 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1742 * We must ensure that "local_flattened_event_fields" is never resized
1743 * so as to preserve the validity of the flattened objects.
1745 ret
= lttng_dynamic_buffer_set_capacity(&local_flattened_event_fields
, storage_req
);
1747 ret_code
= LTTNG_ERR_NOMEM
;
1751 for (i
= 0; i
< nb_event_field
; i
++) {
1752 const struct lttng_event_field
*element
=
1753 (const struct lttng_event_field
*) lttng_dynamic_pointer_array_get_pointer(
1757 ret_code
= LTTNG_ERR_FATAL
;
1760 ret
= lttng_dynamic_buffer_append(
1761 &local_flattened_event_fields
, element
, sizeof(struct lttng_event_field
));
1763 ret_code
= LTTNG_ERR_NOMEM
;
1768 /* Don't reset local_flattened_channels buffer as we return its content. */
1769 *flattened_event_fields
= (struct lttng_event_field
*) local_flattened_event_fields
.data
;
1770 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1771 ret_code
= LTTNG_OK
;
1773 lttng_dynamic_buffer_reset(&local_flattened_event_fields
);
1777 static enum lttng_error_code
1778 event_field_list_create_from_payload(struct lttng_payload_view
*view
,
1780 struct lttng_dynamic_pointer_array
**event_field_list
)
1782 enum lttng_error_code ret_code
;
1783 int ret
, offset
= 0;
1785 struct lttng_dynamic_pointer_array
*list
= NULL
;
1788 assert(event_field_list
);
1790 list
= zmalloc
<lttng_dynamic_pointer_array
>();
1792 ret_code
= LTTNG_ERR_NOMEM
;
1796 lttng_dynamic_pointer_array_init(list
, free
);
1798 for (i
= 0; i
< count
; i
++) {
1799 ssize_t event_field_size
;
1800 struct lttng_event_field
*field
= NULL
;
1801 struct lttng_payload_view event_field_view
=
1802 lttng_payload_view_from_view(view
, offset
, -1);
1804 event_field_size
= lttng_event_field_create_from_payload(&event_field_view
, &field
);
1805 if (event_field_size
< 0) {
1806 ret_code
= LTTNG_ERR_INVALID
;
1810 /* Lifetime and management of the object is now bound to the array. */
1811 ret
= lttng_dynamic_pointer_array_add_pointer(list
, field
);
1814 ret_code
= LTTNG_ERR_NOMEM
;
1818 offset
+= event_field_size
;
1821 if (view
->buffer
.size
!= offset
) {
1822 ret_code
= LTTNG_ERR_INVALID
;
1826 *event_field_list
= list
;
1828 ret_code
= LTTNG_OK
;
1832 lttng_dynamic_pointer_array_reset(list
);
1839 enum lttng_error_code
lttng_event_fields_create_and_flatten_from_payload(
1840 struct lttng_payload_view
*view
, unsigned int count
, struct lttng_event_field
**fields
)
1842 enum lttng_error_code ret_code
;
1843 struct lttng_dynamic_pointer_array
*local_event_fields
= NULL
;
1845 ret_code
= event_field_list_create_from_payload(view
, count
, &local_event_fields
);
1846 if (ret_code
!= LTTNG_OK
) {
1850 ret_code
= flatten_lttng_event_fields(local_event_fields
, fields
);
1851 if (ret_code
!= LTTNG_OK
) {
1855 if (local_event_fields
) {
1856 lttng_dynamic_pointer_array_reset(local_event_fields
);
1857 free(local_event_fields
);