2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include "common/compat/string.h"
9 #include "common/macros.h"
10 #include "lttng/lttng-error.h"
12 #include <common/buffer-view.h>
13 #include <common/dynamic-array.h>
14 #include <common/dynamic-buffer.h>
15 #include <common/error.h>
16 #include <common/sessiond-comm/sessiond-comm.h>
17 #include <common/align.h>
18 #include <lttng/constant.h>
19 #include <lttng/event-internal.h>
20 #include <lttng/event.h>
21 #include <lttng/userspace-probe-internal.h>
25 struct event_list_element
{
26 struct lttng_event
*event
;
27 struct lttng_event_exclusion
*exclusions
;
28 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
= (lttng_event
*) zmalloc(sizeof(*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
= (lttng_event_extended
*) zmalloc(sizeof(*new_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(
75 const struct lttng_event_probe_attr
*probe
,
76 struct lttng_payload
*payload
)
79 size_t symbol_name_len
;
80 struct lttng_event_probe_attr_comm comm
= { 0 };
82 symbol_name_len
= lttng_strnlen(probe
->symbol_name
, LTTNG_SYMBOL_NAME_LEN
);
83 if (symbol_name_len
== LTTNG_SYMBOL_NAME_LEN
) {
84 /* Not null-termintated. */
89 /* Include the null terminator. */
92 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
93 comm
.addr
= probe
->addr
;
94 comm
.offset
= probe
->addr
;
96 ret
= lttng_dynamic_buffer_append(
97 &payload
->buffer
, &comm
, sizeof(comm
));
103 ret
= lttng_dynamic_buffer_append(
104 &payload
->buffer
, probe
->symbol_name
, symbol_name_len
);
109 static int lttng_event_function_attr_serialize(
110 const struct lttng_event_function_attr
*function
,
111 struct lttng_payload
*payload
)
114 size_t symbol_name_len
;
115 struct lttng_event_function_attr_comm comm
= { 0 };
117 symbol_name_len
= lttng_strnlen(function
->symbol_name
, LTTNG_SYMBOL_NAME_LEN
);
118 if (symbol_name_len
== LTTNG_SYMBOL_NAME_LEN
) {
119 /* Not null-termintated. */
124 /* Include the null terminator. */
125 symbol_name_len
+= 1;
127 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
129 ret
= lttng_dynamic_buffer_append(
130 &payload
->buffer
, &comm
, sizeof(comm
));
136 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
137 function
->symbol_name
, symbol_name_len
);
142 static ssize_t
lttng_event_probe_attr_create_from_payload(
143 struct lttng_payload_view
*view
,
144 struct lttng_event_probe_attr
**probe_attr
)
146 ssize_t ret
, offset
= 0;
147 const struct lttng_event_probe_attr_comm
*comm
;
148 struct lttng_event_probe_attr
*local_attr
= NULL
;
149 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
150 view
, offset
, sizeof(*comm
));
152 if (!lttng_payload_view_is_valid(&comm_view
)) {
157 comm
= (typeof(comm
)) comm_view
.buffer
.data
;
158 offset
+= sizeof(*comm
);
160 local_attr
= (struct lttng_event_probe_attr
*) zmalloc(
161 sizeof(*local_attr
));
162 if (local_attr
== NULL
) {
167 local_attr
->addr
= comm
->addr
;
168 local_attr
->offset
= comm
->offset
;
172 struct lttng_payload_view name_view
=
173 lttng_payload_view_from_view(view
, offset
,
174 comm
->symbol_name_len
);
176 if (!lttng_payload_view_is_valid(&name_view
)) {
181 name
= name_view
.buffer
.data
;
183 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
184 comm
->symbol_name_len
)) {
189 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
190 LTTNG_SYMBOL_NAME_LEN
);
196 offset
+= comm
->symbol_name_len
;
199 *probe_attr
= local_attr
;
206 static ssize_t
lttng_event_function_attr_create_from_payload(
207 struct lttng_payload_view
*view
,
208 struct lttng_event_function_attr
**function_attr
)
210 ssize_t ret
, offset
= 0;
211 const struct lttng_event_function_attr_comm
*comm
;
212 struct lttng_event_function_attr
*local_attr
= NULL
;
213 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
214 view
, offset
, sizeof(*comm
));
216 if (!lttng_payload_view_is_valid(&comm_view
)) {
221 comm
= (typeof(comm
)) view
->buffer
.data
;
222 offset
+= sizeof(*comm
);
224 local_attr
= (struct lttng_event_function_attr
*) zmalloc(
225 sizeof(*local_attr
));
226 if (local_attr
== NULL
) {
233 struct lttng_payload_view name_view
=
234 lttng_payload_view_from_view(view
, offset
,
235 comm
->symbol_name_len
);
237 if (!lttng_payload_view_is_valid(&name_view
)) {
242 name
= name_view
.buffer
.data
;
244 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
245 comm
->symbol_name_len
)) {
250 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
251 LTTNG_SYMBOL_NAME_LEN
);
257 offset
+= comm
->symbol_name_len
;
260 *function_attr
= local_attr
;
267 static ssize_t
lttng_event_exclusions_create_from_payload(
268 struct lttng_payload_view
*view
,
270 struct lttng_event_exclusion
**exclusions
)
272 ssize_t ret
, offset
= 0;
273 size_t size
= (count
* LTTNG_SYMBOL_NAME_LEN
);
275 const struct lttng_event_exclusion_comm
*comm
;
276 struct lttng_event_exclusion
*local_exclusions
;
278 local_exclusions
= (struct lttng_event_exclusion
*) zmalloc(
279 sizeof(struct lttng_event_exclusion
) + size
);
280 if (!local_exclusions
) {
285 local_exclusions
->count
= count
;
287 for (i
= 0; i
< count
; i
++) {
289 struct lttng_buffer_view string_view
;
290 const struct lttng_buffer_view comm_view
=
291 lttng_buffer_view_from_view(&view
->buffer
,
292 offset
, sizeof(*comm
));
294 if (!lttng_buffer_view_is_valid(&comm_view
)) {
299 comm
= (typeof(comm
)) comm_view
.data
;
300 offset
+= sizeof(*comm
);
302 string_view
= lttng_buffer_view_from_view(
303 &view
->buffer
, offset
, comm
->len
);
305 if (!lttng_buffer_view_is_valid(&string_view
)) {
310 string
= string_view
.data
;
312 if (!lttng_buffer_view_contains_string(
313 &string_view
, string
, comm
->len
)) {
318 ret
= lttng_strncpy(local_exclusions
->names
[i
],
319 string
, LTTNG_SYMBOL_NAME_LEN
);
328 *exclusions
= local_exclusions
;
329 local_exclusions
= NULL
;
332 free(local_exclusions
);
336 ssize_t
lttng_event_create_from_payload(struct lttng_payload_view
*view
,
337 struct lttng_event
**out_event
,
338 struct lttng_event_exclusion
**out_exclusion
,
339 char **out_filter_expression
,
340 struct lttng_bytecode
**out_bytecode
)
342 ssize_t ret
, offset
= 0;
343 struct lttng_event
*local_event
= NULL
;
344 struct lttng_event_exclusion
*local_exclusions
= NULL
;
345 struct lttng_bytecode
*local_bytecode
= NULL
;
346 char *local_filter_expression
= NULL
;
347 const struct lttng_event_comm
*event_comm
;
348 struct lttng_event_function_attr
*local_function_attr
= NULL
;
349 struct lttng_event_probe_attr
*local_probe_attr
= NULL
;
350 struct lttng_userspace_probe_location
*local_userspace_probe_location
=
354 * Only event is obligatory, the other output argument are optional and
355 * depends on what the caller is interested in.
361 struct lttng_payload_view comm_view
=
362 lttng_payload_view_from_view(view
, offset
,
363 sizeof(*event_comm
));
365 if (!lttng_payload_view_is_valid(&comm_view
)) {
370 /* lttng_event_comm header */
371 event_comm
= (typeof(event_comm
)) comm_view
.buffer
.data
;
372 offset
+= sizeof(*event_comm
);
375 local_event
= lttng_event_create();
376 if (local_event
== NULL
) {
381 local_event
->type
= (enum lttng_event_type
) event_comm
->event_type
;
382 local_event
->loglevel_type
= (enum lttng_loglevel_type
) event_comm
->loglevel_type
;
383 local_event
->loglevel
= event_comm
->loglevel
;
384 local_event
->enabled
= event_comm
->enabled
;
385 local_event
->pid
= event_comm
->pid
;
386 local_event
->flags
= (enum lttng_event_flag
) event_comm
->flags
;
390 const struct lttng_buffer_view name_view
=
391 lttng_buffer_view_from_view(&view
->buffer
,
392 offset
, event_comm
->name_len
);
394 if (!lttng_buffer_view_is_valid(&name_view
)) {
399 name
= (const char *) name_view
.data
;
401 if (!lttng_buffer_view_contains_string(
402 &name_view
, name
, event_comm
->name_len
)) {
408 local_event
->name
, name
, LTTNG_SYMBOL_NAME_LEN
);
414 offset
+= event_comm
->name_len
;
418 if (event_comm
->exclusion_count
== 0) {
419 goto deserialize_filter_expression
;
423 struct lttng_payload_view exclusions_view
=
424 lttng_payload_view_from_view(
427 if (!lttng_payload_view_is_valid(&exclusions_view
)) {
432 ret
= lttng_event_exclusions_create_from_payload(&exclusions_view
,
433 event_comm
->exclusion_count
, &local_exclusions
);
440 local_event
->exclusion
= 1;
443 deserialize_filter_expression
:
445 if (event_comm
->filter_expression_len
== 0) {
446 if (event_comm
->bytecode_len
!= 0) {
448 * This is an invalid event payload.
450 * Filter expression without bytecode is possible but
451 * not the other way around.
456 goto deserialize_event_type_payload
;
460 const char *filter_expression_buffer
;
461 struct lttng_buffer_view filter_expression_view
=
462 lttng_buffer_view_from_view(&view
->buffer
, offset
,
463 event_comm
->filter_expression_len
);
465 if (!lttng_buffer_view_is_valid(&filter_expression_view
)) {
470 filter_expression_buffer
= filter_expression_view
.data
;
472 if (!lttng_buffer_view_contains_string(&filter_expression_view
,
473 filter_expression_buffer
,
474 event_comm
->filter_expression_len
)) {
479 local_filter_expression
= lttng_strndup(
480 filter_expression_buffer
,
481 event_comm
->filter_expression_len
);
482 if (!local_filter_expression
) {
487 local_event
->filter
= 1;
489 offset
+= event_comm
->filter_expression_len
;
492 if (event_comm
->bytecode_len
== 0) {
494 * Filter expression can be present but without bytecode
495 * when dealing with event listing.
497 goto deserialize_event_type_payload
;
502 struct lttng_payload_view bytecode_view
=
503 lttng_payload_view_from_view(view
, offset
,
504 event_comm
->bytecode_len
);
506 if (!lttng_payload_view_is_valid(&bytecode_view
)) {
511 local_bytecode
= (struct lttng_bytecode
*) zmalloc(
512 event_comm
->bytecode_len
);
513 if (!local_bytecode
) {
518 memcpy(local_bytecode
, bytecode_view
.buffer
.data
,
519 event_comm
->bytecode_len
);
520 if ((local_bytecode
->len
+ sizeof(*local_bytecode
)) !=
521 event_comm
->bytecode_len
) {
526 offset
+= event_comm
->bytecode_len
;
529 deserialize_event_type_payload
:
530 /* Event type specific payload */
531 switch (local_event
->type
) {
532 case LTTNG_EVENT_FUNCTION
:
534 case LTTNG_EVENT_PROBE
:
536 struct lttng_payload_view probe_attr_view
=
537 lttng_payload_view_from_view(view
, offset
,
538 event_comm
->lttng_event_probe_attr_len
);
540 if (event_comm
->lttng_event_probe_attr_len
== 0) {
545 if (!lttng_payload_view_is_valid(&probe_attr_view
)) {
550 ret
= lttng_event_probe_attr_create_from_payload(
551 &probe_attr_view
, &local_probe_attr
);
552 if (ret
< 0 || ret
!= event_comm
->lttng_event_probe_attr_len
) {
557 /* Copy to the local event. */
558 memcpy(&local_event
->attr
.probe
, local_probe_attr
,
559 sizeof(local_event
->attr
.probe
));
564 case LTTNG_EVENT_FUNCTION_ENTRY
:
566 struct lttng_payload_view function_attr_view
=
567 lttng_payload_view_from_view(view
, offset
,
568 event_comm
->lttng_event_function_attr_len
);
570 if (event_comm
->lttng_event_function_attr_len
== 0) {
575 if (!lttng_payload_view_is_valid(&function_attr_view
)) {
580 ret
= lttng_event_function_attr_create_from_payload(
581 &function_attr_view
, &local_function_attr
);
582 if (ret
< 0 || ret
!= event_comm
->lttng_event_function_attr_len
) {
587 /* Copy to the local event. */
588 memcpy(&local_event
->attr
.ftrace
, local_function_attr
,
589 sizeof(local_event
->attr
.ftrace
));
595 case LTTNG_EVENT_USERSPACE_PROBE
:
597 struct lttng_payload_view userspace_probe_location_view
=
598 lttng_payload_view_from_view(view
, offset
,
599 event_comm
->userspace_probe_location_len
);
601 if (event_comm
->userspace_probe_location_len
== 0) {
606 if (!lttng_payload_view_is_valid(
607 &userspace_probe_location_view
)) {
612 ret
= lttng_userspace_probe_location_create_from_payload(
613 &userspace_probe_location_view
,
614 &local_userspace_probe_location
);
616 WARN("Failed to create a userspace probe location from the received buffer");
621 if (ret
!= event_comm
->userspace_probe_location_len
) {
622 WARN("Userspace probe location from the received buffer is not the advertised length: header length = %" PRIu32
", payload length = %lu", event_comm
->userspace_probe_location_len
, ret
);
627 /* Attach the probe location to the event. */
628 ret
= lttng_event_set_userspace_probe_location(
629 local_event
, local_userspace_probe_location
);
631 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
636 * Userspace probe location object ownership transfered to the
639 local_userspace_probe_location
= NULL
;
640 offset
+= event_comm
->userspace_probe_location_len
;
643 case LTTNG_EVENT_TRACEPOINT
:
645 case LTTNG_EVENT_ALL
:
647 case LTTNG_EVENT_SYSCALL
:
649 case LTTNG_EVENT_NOOP
:
650 /* Nothing to do here */
658 /* Transfer ownership to the caller. */
659 *out_event
= local_event
;
663 *out_bytecode
= local_bytecode
;
664 local_bytecode
= NULL
;
668 *out_exclusion
= local_exclusions
;
669 local_exclusions
= NULL
;
672 if (out_filter_expression
) {
673 *out_filter_expression
= local_filter_expression
;
674 local_filter_expression
= NULL
;
679 lttng_event_destroy(local_event
);
680 lttng_userspace_probe_location_destroy(local_userspace_probe_location
);
681 free(local_filter_expression
);
682 free(local_exclusions
);
683 free(local_bytecode
);
684 free(local_function_attr
);
685 free(local_probe_attr
);
689 int lttng_event_serialize(const struct lttng_event
*event
,
690 unsigned int exclusion_count
,
691 char **exclusion_list
,
692 char *filter_expression
,
694 struct lttng_bytecode
*bytecode
,
695 struct lttng_payload
*payload
)
699 size_t header_offset
, size_before_payload
;
701 struct lttng_event_comm event_comm
= { 0 };
702 struct lttng_event_comm
*header
;
706 assert(exclusion_count
== 0 || exclusion_list
);
708 /* Save the header location for later in-place header update. */
709 header_offset
= payload
->buffer
.size
;
711 name_len
= lttng_strnlen(event
->name
, LTTNG_SYMBOL_NAME_LEN
);
712 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
713 /* Event name is not NULL-terminated. */
718 /* Add null termination. */
721 if (exclusion_count
> UINT32_MAX
) {
722 /* Possible overflow. */
727 if (bytecode_len
> UINT32_MAX
) {
728 /* Possible overflow. */
733 event_comm
.name_len
= (uint32_t) name_len
;
734 event_comm
.event_type
= (int8_t) event
->type
;
735 event_comm
.loglevel_type
= (int8_t) event
->loglevel_type
;
736 event_comm
.loglevel
= (int32_t) event
->loglevel
;
737 event_comm
.enabled
= (int8_t) event
->enabled
;
738 event_comm
.pid
= (int32_t) event
->pid
;
739 event_comm
.exclusion_count
= (uint32_t) exclusion_count
;
740 event_comm
.bytecode_len
= (uint32_t) bytecode_len
;
741 event_comm
.flags
= (int32_t) event
->flags
;
743 if (filter_expression
) {
744 event_comm
.filter_expression_len
=
745 strlen(filter_expression
) + 1;
749 ret
= lttng_dynamic_buffer_append(
750 &payload
->buffer
, &event_comm
, sizeof(event_comm
));
756 ret
= lttng_dynamic_buffer_append(
757 &payload
->buffer
, event
->name
, name_len
);
763 for (i
= 0; i
< exclusion_count
; i
++) {
764 const size_t exclusion_len
= lttng_strnlen(
765 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
766 const struct lttng_event_exclusion_comm exclusion_header
= {
767 .len
= (uint32_t) exclusion_len
+ 1,
770 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
771 /* Exclusion is not NULL-terminated. */
776 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
777 &exclusion_header
, sizeof(exclusion_header
));
782 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
783 *(exclusion_list
+ i
), exclusion_len
+ 1);
789 /* Filter expression and its bytecode */
790 if (filter_expression
) {
791 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
793 event_comm
.filter_expression_len
);
799 * Bytecode can be absent when we serialize to the client
803 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
804 bytecode
, bytecode_len
);
811 size_before_payload
= payload
->buffer
.size
;
813 /* Event type specific payload */
814 switch (event
->type
) {
815 case LTTNG_EVENT_FUNCTION
:
817 case LTTNG_EVENT_PROBE
:
818 ret
= lttng_event_probe_attr_serialize(&event
->attr
.probe
, payload
);
824 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
826 header
->lttng_event_probe_attr_len
=
827 payload
->buffer
.size
- size_before_payload
;
830 case LTTNG_EVENT_FUNCTION_ENTRY
:
831 ret
= lttng_event_function_attr_serialize(
832 &event
->attr
.ftrace
, payload
);
838 /* Update the lttng_event_function_attr len. */
839 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
841 header
->lttng_event_function_attr_len
=
842 payload
->buffer
.size
- size_before_payload
;
845 case LTTNG_EVENT_USERSPACE_PROBE
:
847 const struct lttng_event_extended
*ev_ext
=
848 (const struct lttng_event_extended
*)
851 assert(event
->extended
.ptr
);
852 assert(ev_ext
->probe_location
);
854 size_before_payload
= payload
->buffer
.size
;
855 if (ev_ext
->probe_location
) {
857 * lttng_userspace_probe_location_serialize returns the
858 * number of bytes that were appended to the buffer.
860 ret
= lttng_userspace_probe_location_serialize(
861 ev_ext
->probe_location
, payload
);
868 /* Update the userspace probe location len. */
869 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
871 header
->userspace_probe_location_len
=
872 payload
->buffer
.size
- size_before_payload
;
876 case LTTNG_EVENT_TRACEPOINT
:
878 case LTTNG_EVENT_ALL
:
881 /* Nothing to do here. */
889 static ssize_t
lttng_event_context_app_populate_from_payload(
890 const struct lttng_payload_view
*view
,
891 struct lttng_event_context
*event_ctx
)
893 ssize_t ret
, offset
= 0;
894 const struct lttng_event_context_app_comm
*comm
;
895 char *provider_name
= NULL
, *context_name
= NULL
;
896 size_t provider_name_len
, context_name_len
;
897 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
898 &view
->buffer
, offset
, sizeof(*comm
));
900 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
902 if (!lttng_buffer_view_is_valid(&comm_view
)) {
907 comm
= (typeof(comm
)) comm_view
.data
;
908 offset
+= sizeof(*comm
);
910 provider_name_len
= comm
->provider_name_len
;
911 context_name_len
= comm
->ctx_name_len
;
913 if (provider_name_len
== 0 || context_name_len
== 0) {
915 * Application provider and context names MUST
924 const struct lttng_buffer_view provider_name_view
=
925 lttng_buffer_view_from_view(&view
->buffer
,
929 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
934 name
= provider_name_view
.data
;
936 if (!lttng_buffer_view_contains_string(&provider_name_view
,
937 name
, provider_name_len
)) {
942 provider_name
= lttng_strndup(name
, provider_name_len
);
943 if (!provider_name
) {
948 offset
+= provider_name_len
;
953 const struct lttng_buffer_view context_name_view
=
954 lttng_buffer_view_from_view(
955 &view
->buffer
, offset
,
958 if (!lttng_buffer_view_is_valid(&context_name_view
)) {
963 name
= context_name_view
.data
;
965 if (!lttng_buffer_view_contains_string(&context_name_view
, name
,
971 context_name
= lttng_strndup(name
, context_name_len
);
977 offset
+= context_name_len
;
980 /* Transfer ownership of the strings */
981 event_ctx
->u
.app_ctx
.provider_name
= provider_name
;
982 event_ctx
->u
.app_ctx
.ctx_name
= context_name
;
983 provider_name
= NULL
;
994 static ssize_t
lttng_event_context_perf_counter_populate_from_payload(
995 const struct lttng_payload_view
*view
,
996 struct lttng_event_context
*event_ctx
)
998 ssize_t ret
, offset
= 0;
999 const struct lttng_event_context_perf_counter_comm
*comm
;
1001 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1002 &view
->buffer
, offset
, sizeof(*comm
));
1004 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_COUNTER
||
1006 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
||
1007 event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
);
1009 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1014 comm
= (typeof(comm
)) comm_view
.data
;
1015 offset
+= sizeof(*comm
);
1017 name_len
= comm
->name_len
;
1021 const struct lttng_buffer_view provider_name_view
=
1022 lttng_buffer_view_from_view(
1023 &view
->buffer
, offset
,
1026 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
1031 name
= provider_name_view
.data
;
1033 if (!lttng_buffer_view_contains_string(
1034 &provider_name_view
, name
, name_len
)) {
1039 lttng_strncpy(event_ctx
->u
.perf_counter
.name
, name
, name_len
);
1043 event_ctx
->u
.perf_counter
.config
= comm
->config
;
1044 event_ctx
->u
.perf_counter
.type
= comm
->type
;
1052 ssize_t
lttng_event_context_create_from_payload(
1053 struct lttng_payload_view
*view
,
1054 struct lttng_event_context
**event_ctx
)
1056 ssize_t ret
, offset
= 0;
1057 const struct lttng_event_context_comm
*comm
;
1058 struct lttng_event_context
*local_context
= NULL
;
1059 struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1060 &view
->buffer
, offset
, sizeof(*comm
));
1065 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1070 comm
= (typeof(comm
)) comm_view
.data
;
1071 offset
+= sizeof(*comm
);
1073 local_context
= (struct lttng_event_context
*)
1074 zmalloc(sizeof(*local_context
));
1075 if (!local_context
) {
1080 local_context
->ctx
= (lttng_event_context_type
) comm
->type
;
1083 struct lttng_payload_view subtype_view
=
1084 lttng_payload_view_from_view(view
, offset
, -1);
1086 switch (local_context
->ctx
) {
1087 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1088 ret
= lttng_event_context_app_populate_from_payload(
1089 &subtype_view
, local_context
);
1091 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1092 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1093 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1094 ret
= lttng_event_context_perf_counter_populate_from_payload(
1095 &subtype_view
, local_context
);
1098 /* Nothing else to deserialize. */
1110 *event_ctx
= local_context
;
1111 local_context
= NULL
;
1115 free(local_context
);
1119 static int lttng_event_context_app_serialize(
1120 struct lttng_event_context
*context
,
1121 struct lttng_payload
*payload
)
1124 struct lttng_event_context_app_comm comm
= { 0 };
1125 size_t provider_len
, ctx_len
;
1126 const char *provider_name
;
1127 const char *ctx_name
;
1131 assert(context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1133 provider_name
= context
->u
.app_ctx
.provider_name
;
1134 ctx_name
= context
->u
.app_ctx
.ctx_name
;
1136 if (!provider_name
|| !ctx_name
) {
1137 ret
= -LTTNG_ERR_INVALID
;
1141 provider_len
= strlen(provider_name
);
1142 if (provider_len
== 0) {
1143 ret
= -LTTNG_ERR_INVALID
;
1147 /* Include the null terminator. */
1149 comm
.provider_name_len
= provider_len
;
1151 ctx_len
= strlen(ctx_name
);
1153 ret
= -LTTNG_ERR_INVALID
;
1157 /* Include the null terminator. */
1159 comm
.ctx_name_len
= ctx_len
;
1162 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1169 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, provider_name
,
1176 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, ctx_name
,
1187 static int lttng_event_context_perf_counter_serialize(
1188 struct lttng_event_perf_counter_ctx
*context
,
1189 struct lttng_payload
*payload
)
1192 struct lttng_event_context_perf_counter_comm comm
= { 0 };
1197 comm
.config
= context
->config
;
1198 comm
.type
= context
->type
;
1199 comm
.name_len
= lttng_strnlen(context
->name
, LTTNG_SYMBOL_NAME_LEN
);
1201 if (comm
.name_len
== LTTNG_SYMBOL_NAME_LEN
) {
1206 /* Include the null terminator. */
1210 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1217 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, context
->name
,
1228 int lttng_event_context_serialize(struct lttng_event_context
*context
,
1229 struct lttng_payload
*payload
)
1232 struct lttng_event_context_comm context_comm
= { 0 };
1237 context_comm
.type
= (uint32_t) context
->ctx
;
1240 ret
= lttng_dynamic_buffer_append(
1241 &payload
->buffer
, &context_comm
, sizeof(context_comm
));
1246 switch (context
->ctx
) {
1247 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1248 ret
= lttng_event_context_app_serialize(context
, payload
);
1250 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1251 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1252 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1253 ret
= lttng_event_context_perf_counter_serialize(
1254 &context
->u
.perf_counter
, payload
);
1257 /* Nothing else to serialize. */
1269 void lttng_event_context_destroy(struct lttng_event_context
*context
)
1275 if (context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1276 free(context
->u
.app_ctx
.provider_name
);
1277 free(context
->u
.app_ctx
.ctx_name
);
1284 * This is a specialized populate for lttng_event_field since it ignores
1285 * the extension field of the lttng_event struct and simply copies what it can
1286 * to the internal struct lttng_event of a lttng_event_field.
1288 static void lttng_event_field_populate_lttng_event_from_event(
1289 const struct lttng_event
*src
, struct lttng_event
*destination
)
1291 memcpy(destination
, src
, sizeof(*destination
));
1293 /* Remove all possible dynamic data from the destination event rule. */
1294 destination
->extended
.ptr
= NULL
;
1297 ssize_t
lttng_event_field_create_from_payload(
1298 struct lttng_payload_view
*view
,
1299 struct lttng_event_field
**field
)
1301 ssize_t ret
, offset
= 0;
1302 struct lttng_event_field
*local_event_field
= NULL
;
1303 struct lttng_event
*event
= NULL
;
1304 const struct lttng_event_field_comm
*comm
;
1305 const char* name
= NULL
;
1311 const struct lttng_buffer_view comm_view
=
1312 lttng_buffer_view_from_view(
1313 &view
->buffer
, offset
,
1316 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1321 /* lttng_event_field_comm header */
1322 comm
= (const lttng_event_field_comm
*) comm_view
.data
;
1323 offset
+= sizeof(*comm
);
1326 local_event_field
= (struct lttng_event_field
*)
1327 zmalloc(sizeof(*local_event_field
));
1328 if (!local_event_field
) {
1333 local_event_field
->type
= (lttng_event_field_type
) comm
->type
;
1334 local_event_field
->nowrite
= comm
->nowrite
;
1338 const struct lttng_buffer_view name_view
=
1339 lttng_buffer_view_from_view(
1340 &view
->buffer
, offset
,
1343 if (!lttng_buffer_view_is_valid(&name_view
)) {
1348 name
= name_view
.data
;
1350 if (!lttng_buffer_view_contains_string(&name_view
,
1351 name_view
.data
, comm
->name_len
)) {
1356 if (comm
->name_len
> LTTNG_SYMBOL_NAME_LEN
- 1) {
1357 /* Name is too long.*/
1362 offset
+= comm
->name_len
;
1367 struct lttng_payload_view event_view
=
1368 lttng_payload_view_from_view(
1372 if (!lttng_payload_view_is_valid(&event_view
)) {
1377 ret
= lttng_event_create_from_payload(&event_view
, &event
, NULL
,
1379 if (ret
!= comm
->event_len
) {
1390 if (lttng_strncpy(local_event_field
->field_name
, name
, LTTNG_SYMBOL_NAME_LEN
)) {
1395 lttng_event_field_populate_lttng_event_from_event(
1396 event
, &local_event_field
->event
);
1398 *field
= local_event_field
;
1399 local_event_field
= NULL
;
1402 lttng_event_destroy(event
);
1403 free(local_event_field
);
1407 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1408 struct lttng_payload
*payload
)
1411 size_t header_offset
, size_before_event
;
1413 struct lttng_event_field_comm event_field_comm
= { 0 };
1414 struct lttng_event_field_comm
*header
;
1419 /* Save the header location for later in-place header update. */
1420 header_offset
= payload
->buffer
.size
;
1422 name_len
= strnlen(field
->field_name
, LTTNG_SYMBOL_NAME_LEN
);
1423 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
1424 /* Event name is not NULL-terminated. */
1429 /* Add null termination. */
1432 event_field_comm
.type
= field
->type
;
1433 event_field_comm
.nowrite
= (uint8_t)field
->nowrite
;
1434 event_field_comm
.name_len
= name_len
;
1437 ret
= lttng_dynamic_buffer_append(
1438 &payload
->buffer
, &event_field_comm
,
1439 sizeof(event_field_comm
));
1445 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, field
->field_name
,
1451 size_before_event
= payload
->buffer
.size
;
1452 ret
= lttng_event_serialize(
1453 &field
->event
, 0, NULL
, NULL
, 0, 0, payload
);
1459 /* Update the event len. */
1460 header
= (struct lttng_event_field_comm
*)
1461 ((char *) payload
->buffer
.data
+
1463 header
->event_len
= payload
->buffer
.size
- size_before_event
;
1469 static enum lttng_error_code
compute_flattened_size(
1470 struct lttng_dynamic_pointer_array
*events
, size_t *size
)
1472 enum lttng_error_code ret_code
;
1474 size_t storage_req
, event_count
, i
;
1479 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1481 /* The basic struct lttng_event */
1482 storage_req
= event_count
* sizeof(struct lttng_event
);
1484 for (i
= 0; i
< event_count
; i
++) {
1485 int probe_storage_req
= 0;
1486 const struct event_list_element
*element
= (const struct event_list_element
*)
1487 lttng_dynamic_pointer_array_get_pointer(
1489 const struct lttng_userspace_probe_location
*location
= NULL
;
1491 location
= lttng_event_get_userspace_probe_location(
1494 ret
= lttng_userspace_probe_location_flatten(
1497 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1501 probe_storage_req
= ret
;
1504 /* The struct·lttng_event_extended */
1505 storage_req
+= event_count
*
1506 sizeof(struct lttng_event_extended
);
1508 if (element
->filter_expression
) {
1509 storage_req
+= strlen(element
->filter_expression
) + 1;
1512 if (element
->exclusions
) {
1513 storage_req
+= element
->exclusions
->count
*
1514 LTTNG_SYMBOL_NAME_LEN
;
1517 /* Padding to ensure the flat probe is aligned. */
1518 storage_req
= lttng_align_ceil(storage_req
, sizeof(uint64_t));
1519 storage_req
+= probe_storage_req
;
1522 *size
= storage_req
;
1523 ret_code
= LTTNG_OK
;
1530 * Flatten a list of struct lttng_event.
1532 * The buffer that is returned to the API client must contain a "flat" version
1533 * of the events that are returned. In other words, all pointers within an
1534 * lttng_event must point to a location within the returned buffer so that the
1535 * user may free everything by simply calling free() on the returned buffer.
1536 * This is needed in order to maintain API compatibility.
1538 * A first pass is performed to compute the size of the buffer that must be
1539 * allocated. A second pass is then performed to setup the returned events so
1540 * that their members always point within the buffer.
1542 * The layout of the returned buffer is as follows:
1543 * - struct lttng_event[nb_events],
1544 * - nb_events times the following:
1545 * - struct lttng_event_extended,
1546 * - filter_expression
1548 * - padding to align to 64-bits
1549 * - flattened version of userspace_probe_location
1551 static enum lttng_error_code
flatten_lttng_events(
1552 struct lttng_dynamic_pointer_array
*events
,
1553 struct lttng_event
**flattened_events
)
1555 enum lttng_error_code ret_code
;
1558 struct lttng_dynamic_buffer local_flattened_events
;
1562 assert(flattened_events
);
1564 lttng_dynamic_buffer_init(&local_flattened_events
);
1565 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1567 ret_code
= compute_flattened_size(events
, &storage_req
);
1568 if (ret_code
!= LTTNG_OK
) {
1573 * We must ensure that "local_flattened_events" is never resized so as
1574 * to preserve the validity of the flattened objects.
1576 ret
= lttng_dynamic_buffer_set_capacity(
1577 &local_flattened_events
, storage_req
);
1579 ret_code
= LTTNG_ERR_NOMEM
;
1583 /* Start by laying the struct lttng_event */
1584 for (i
= 0; i
< nb_events
; i
++) {
1585 const struct event_list_element
*element
= (const struct event_list_element
*)
1586 lttng_dynamic_pointer_array_get_pointer(
1590 ret_code
= LTTNG_ERR_FATAL
;
1594 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1595 element
->event
, sizeof(struct lttng_event
));
1597 ret_code
= LTTNG_ERR_NOMEM
;
1602 for (i
= 0; i
< nb_events
; i
++) {
1603 const struct event_list_element
*element
= (const struct event_list_element
*)
1604 lttng_dynamic_pointer_array_get_pointer(events
, i
);
1605 struct lttng_event
*event
= (struct lttng_event
*)
1606 (local_flattened_events
.data
+ (sizeof(struct lttng_event
) * i
));
1607 struct lttng_event_extended
*event_extended
=
1608 (struct lttng_event_extended
*)
1609 (local_flattened_events
.data
+ local_flattened_events
.size
);
1610 const struct lttng_userspace_probe_location
*location
= NULL
;
1614 /* Insert struct lttng_event_extended. */
1615 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1616 local_flattened_events
.size
+
1617 sizeof(*event_extended
));
1619 ret_code
= LTTNG_ERR_NOMEM
;
1622 event
->extended
.ptr
= event_extended
;
1624 /* Insert filter expression. */
1625 if (element
->filter_expression
) {
1626 const size_t len
= strlen(element
->filter_expression
) + 1;
1628 event_extended
->filter_expression
=
1629 local_flattened_events
.data
+
1630 local_flattened_events
.size
;
1631 ret
= lttng_dynamic_buffer_append(
1632 &local_flattened_events
,
1633 element
->filter_expression
, len
);
1635 ret_code
= LTTNG_ERR_NOMEM
;
1640 /* Insert exclusions. */
1641 if (element
->exclusions
) {
1642 event_extended
->exclusions
.count
=
1643 element
->exclusions
->count
;
1644 event_extended
->exclusions
.strings
=
1645 local_flattened_events
.data
+
1646 local_flattened_events
.size
;
1648 ret
= lttng_dynamic_buffer_append(
1649 &local_flattened_events
,
1650 element
->exclusions
->names
,
1651 element
->exclusions
->count
*
1652 LTTNG_SYMBOL_NAME_LEN
);
1654 ret_code
= LTTNG_ERR_NOMEM
;
1659 /* Insert padding to align to 64-bits. */
1660 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1661 lttng_align_ceil(local_flattened_events
.size
,
1664 ret_code
= LTTNG_ERR_NOMEM
;
1668 location
= lttng_event_get_userspace_probe_location(
1671 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
1672 (local_flattened_events
.data
+ local_flattened_events
.size
);
1673 ret
= lttng_userspace_probe_location_flatten(
1674 location
, &local_flattened_events
);
1676 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1682 /* Don't reset local_flattened_events buffer as we return its content. */
1683 *flattened_events
= (struct lttng_event
*) local_flattened_events
.data
;
1684 lttng_dynamic_buffer_init(&local_flattened_events
);
1685 ret_code
= LTTNG_OK
;
1687 lttng_dynamic_buffer_reset(&local_flattened_events
);
1691 static enum lttng_error_code
event_list_create_from_payload(
1692 struct lttng_payload_view
*view
,
1694 struct lttng_dynamic_pointer_array
*event_list
)
1696 enum lttng_error_code ret_code
;
1704 for (i
= 0; i
< count
; i
++) {
1706 struct lttng_payload_view event_view
=
1707 lttng_payload_view_from_view(view
, offset
, -1);
1708 struct event_list_element
*element
=
1709 (struct event_list_element
*) zmalloc(sizeof(*element
));
1712 ret_code
= LTTNG_ERR_NOMEM
;
1717 * Lifetime and management of the object is now bound to the
1720 ret
= lttng_dynamic_pointer_array_add_pointer(
1721 event_list
, element
);
1723 event_list_destructor(element
);
1724 ret_code
= LTTNG_ERR_NOMEM
;
1729 * Bytecode is not transmitted on listing in any case we do not
1732 event_size
= lttng_event_create_from_payload(&event_view
,
1734 &element
->exclusions
,
1735 &element
->filter_expression
, NULL
);
1736 if (event_size
< 0) {
1737 ret_code
= LTTNG_ERR_INVALID
;
1741 offset
+= event_size
;
1744 if (view
->buffer
.size
!= offset
) {
1745 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
1749 ret_code
= LTTNG_OK
;
1755 enum lttng_error_code
lttng_events_create_and_flatten_from_payload(
1756 struct lttng_payload_view
*payload
,
1758 struct lttng_event
**events
)
1760 enum lttng_error_code ret
= LTTNG_OK
;
1761 struct lttng_dynamic_pointer_array local_events
;
1763 lttng_dynamic_pointer_array_init(&local_events
, event_list_destructor
);
1765 /* Deserialize the events. */
1767 struct lttng_payload_view events_view
=
1768 lttng_payload_view_from_view(payload
, 0, -1);
1770 ret
= event_list_create_from_payload(
1771 &events_view
, count
, &local_events
);
1772 if (ret
!= LTTNG_OK
) {
1777 ret
= flatten_lttng_events(&local_events
, events
);
1778 if (ret
!= LTTNG_OK
) {
1783 lttng_dynamic_pointer_array_reset(&local_events
);
1787 static enum lttng_error_code
flatten_lttng_event_fields(
1788 struct lttng_dynamic_pointer_array
*event_fields
,
1789 struct lttng_event_field
**flattened_event_fields
)
1792 enum lttng_error_code ret_code
;
1793 size_t storage_req
= 0;
1794 struct lttng_dynamic_buffer local_flattened_event_fields
;
1797 assert(event_fields
);
1798 assert(flattened_event_fields
);
1800 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1801 nb_event_field
= lttng_dynamic_pointer_array_get_count(event_fields
);
1804 * Here even if the event field contains a `struct lttng_event` that
1805 * could contain dynamic data, in reality it is not the case.
1806 * Dynamic data is not present. Here the flattening is mostly a direct
1807 * memcpy. This is less than ideal but this code is still better than
1808 * direct usage of an unpacked lttng_event_field array.
1810 storage_req
+= sizeof(struct lttng_event_field
) * nb_event_field
;
1812 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1815 * We must ensure that "local_flattened_event_fields" is never resized
1816 * so as to preserve the validity of the flattened objects.
1818 ret
= lttng_dynamic_buffer_set_capacity(
1819 &local_flattened_event_fields
, storage_req
);
1821 ret_code
= LTTNG_ERR_NOMEM
;
1825 for (i
= 0; i
< nb_event_field
; i
++) {
1826 const struct lttng_event_field
*element
=
1827 (const struct lttng_event_field
*)
1828 lttng_dynamic_pointer_array_get_pointer(
1832 ret_code
= LTTNG_ERR_FATAL
;
1835 ret
= lttng_dynamic_buffer_append(&local_flattened_event_fields
,
1836 element
, sizeof(struct lttng_event_field
));
1838 ret_code
= LTTNG_ERR_NOMEM
;
1843 /* Don't reset local_flattened_channels buffer as we return its content. */
1844 *flattened_event_fields
= (struct lttng_event_field
*) local_flattened_event_fields
.data
;
1845 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1846 ret_code
= LTTNG_OK
;
1848 lttng_dynamic_buffer_reset(&local_flattened_event_fields
);
1852 static enum lttng_error_code
event_field_list_create_from_payload(
1853 struct lttng_payload_view
*view
,
1855 struct lttng_dynamic_pointer_array
**event_field_list
)
1857 enum lttng_error_code ret_code
;
1858 int ret
, offset
= 0;
1860 struct lttng_dynamic_pointer_array
*list
= NULL
;
1863 assert(event_field_list
);
1865 list
= (struct lttng_dynamic_pointer_array
*) zmalloc(sizeof(*list
));
1867 ret_code
= LTTNG_ERR_NOMEM
;
1871 lttng_dynamic_pointer_array_init(list
, free
);
1873 for (i
= 0; i
< count
; i
++) {
1874 ssize_t event_field_size
;
1875 struct lttng_event_field
*field
= NULL
;
1876 struct lttng_payload_view event_field_view
=
1877 lttng_payload_view_from_view(view
, offset
, -1);
1879 event_field_size
= lttng_event_field_create_from_payload(
1880 &event_field_view
, &field
);
1881 if (event_field_size
< 0) {
1882 ret_code
= LTTNG_ERR_INVALID
;
1886 /* Lifetime and management of the object is now bound to the array. */
1887 ret
= lttng_dynamic_pointer_array_add_pointer(list
, field
);
1890 ret_code
= LTTNG_ERR_NOMEM
;
1894 offset
+= event_field_size
;
1897 if (view
->buffer
.size
!= offset
) {
1898 ret_code
= LTTNG_ERR_INVALID
;
1902 *event_field_list
= list
;
1904 ret_code
= LTTNG_OK
;
1908 lttng_dynamic_pointer_array_reset(list
);
1915 enum lttng_error_code
lttng_event_fields_create_and_flatten_from_payload(
1916 struct lttng_payload_view
*view
,
1918 struct lttng_event_field
**fields
)
1920 enum lttng_error_code ret_code
;
1921 struct lttng_dynamic_pointer_array
*local_event_fields
= NULL
;
1923 ret_code
= event_field_list_create_from_payload(
1924 view
, count
, &local_event_fields
);
1925 if (ret_code
!= LTTNG_OK
) {
1929 ret_code
= flatten_lttng_event_fields(local_event_fields
, fields
);
1930 if (ret_code
!= LTTNG_OK
) {
1934 if (local_event_fields
) {
1935 lttng_dynamic_pointer_array_reset(local_event_fields
);
1936 free(local_event_fields
);