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
= {};
82 symbol_name_len
= lttng_strnlen(
83 probe
->symbol_name
, sizeof(probe
->symbol_name
));
84 if (symbol_name_len
== sizeof(probe
->symbol_name
)) {
85 /* Not null-termintated. */
90 /* Include the null terminator. */
93 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
94 comm
.addr
= probe
->addr
;
95 comm
.offset
= probe
->addr
;
97 ret
= lttng_dynamic_buffer_append(
98 &payload
->buffer
, &comm
, sizeof(comm
));
104 ret
= lttng_dynamic_buffer_append(
105 &payload
->buffer
, probe
->symbol_name
, symbol_name_len
);
110 static int lttng_event_function_attr_serialize(
111 const struct lttng_event_function_attr
*function
,
112 struct lttng_payload
*payload
)
115 size_t symbol_name_len
;
116 struct lttng_event_function_attr_comm comm
= { 0 };
118 symbol_name_len
= lttng_strnlen(
119 function
->symbol_name
, sizeof(function
->symbol_name
));
120 if (symbol_name_len
== sizeof(function
->symbol_name
)) {
121 /* Not null-termintated. */
126 /* Include the null terminator. */
127 symbol_name_len
+= 1;
129 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
131 ret
= lttng_dynamic_buffer_append(
132 &payload
->buffer
, &comm
, sizeof(comm
));
138 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
139 function
->symbol_name
, symbol_name_len
);
144 static ssize_t
lttng_event_probe_attr_create_from_payload(
145 struct lttng_payload_view
*view
,
146 struct lttng_event_probe_attr
**probe_attr
)
148 ssize_t ret
, offset
= 0;
149 const struct lttng_event_probe_attr_comm
*comm
;
150 struct lttng_event_probe_attr
*local_attr
= NULL
;
151 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
152 view
, offset
, sizeof(*comm
));
154 if (!lttng_payload_view_is_valid(&comm_view
)) {
159 comm
= (typeof(comm
)) comm_view
.buffer
.data
;
160 offset
+= sizeof(*comm
);
162 local_attr
= (struct lttng_event_probe_attr
*) zmalloc(
163 sizeof(*local_attr
));
164 if (local_attr
== NULL
) {
169 local_attr
->addr
= comm
->addr
;
170 local_attr
->offset
= comm
->offset
;
174 struct lttng_payload_view name_view
=
175 lttng_payload_view_from_view(view
, offset
,
176 comm
->symbol_name_len
);
178 if (!lttng_payload_view_is_valid(&name_view
)) {
183 name
= name_view
.buffer
.data
;
185 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
186 comm
->symbol_name_len
)) {
191 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
192 sizeof(local_attr
->symbol_name
));
198 offset
+= comm
->symbol_name_len
;
201 *probe_attr
= local_attr
;
209 static ssize_t
lttng_event_function_attr_create_from_payload(
210 struct lttng_payload_view
*view
,
211 struct lttng_event_function_attr
**function_attr
)
213 ssize_t ret
, offset
= 0;
214 const struct lttng_event_function_attr_comm
*comm
;
215 struct lttng_event_function_attr
*local_attr
= NULL
;
216 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
217 view
, offset
, sizeof(*comm
));
219 if (!lttng_payload_view_is_valid(&comm_view
)) {
224 comm
= (typeof(comm
)) view
->buffer
.data
;
225 offset
+= sizeof(*comm
);
227 local_attr
= (struct lttng_event_function_attr
*) zmalloc(
228 sizeof(*local_attr
));
229 if (local_attr
== NULL
) {
236 struct lttng_payload_view name_view
=
237 lttng_payload_view_from_view(view
, offset
,
238 comm
->symbol_name_len
);
240 if (!lttng_payload_view_is_valid(&name_view
)) {
245 name
= name_view
.buffer
.data
;
247 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
248 comm
->symbol_name_len
)) {
253 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
254 sizeof(local_attr
->symbol_name
));
260 offset
+= comm
->symbol_name_len
;
263 *function_attr
= local_attr
;
271 static ssize_t
lttng_event_exclusions_create_from_payload(
272 struct lttng_payload_view
*view
,
274 struct lttng_event_exclusion
**exclusions
)
276 ssize_t ret
, offset
= 0;
277 size_t size
= (count
* LTTNG_SYMBOL_NAME_LEN
);
279 const struct lttng_event_exclusion_comm
*comm
;
280 struct lttng_event_exclusion
*local_exclusions
;
282 local_exclusions
= (struct lttng_event_exclusion
*) zmalloc(
283 sizeof(struct lttng_event_exclusion
) + size
);
284 if (!local_exclusions
) {
289 local_exclusions
->count
= count
;
291 for (i
= 0; i
< count
; i
++) {
293 struct lttng_buffer_view string_view
;
294 const struct lttng_buffer_view comm_view
=
295 lttng_buffer_view_from_view(&view
->buffer
,
296 offset
, sizeof(*comm
));
298 if (!lttng_buffer_view_is_valid(&comm_view
)) {
303 comm
= (typeof(comm
)) comm_view
.data
;
304 offset
+= sizeof(*comm
);
306 string_view
= lttng_buffer_view_from_view(
307 &view
->buffer
, offset
, comm
->len
);
309 if (!lttng_buffer_view_is_valid(&string_view
)) {
314 string
= string_view
.data
;
316 if (!lttng_buffer_view_contains_string(
317 &string_view
, string
, comm
->len
)) {
322 ret
= lttng_strncpy(local_exclusions
->names
[i
], string
,
323 sizeof(local_exclusions
->names
[i
]));
332 *exclusions
= local_exclusions
;
333 local_exclusions
= NULL
;
336 free(local_exclusions
);
340 ssize_t
lttng_event_create_from_payload(struct lttng_payload_view
*view
,
341 struct lttng_event
**out_event
,
342 struct lttng_event_exclusion
**out_exclusion
,
343 char **out_filter_expression
,
344 struct lttng_bytecode
**out_bytecode
)
346 ssize_t ret
, offset
= 0;
347 struct lttng_event
*local_event
= NULL
;
348 struct lttng_event_exclusion
*local_exclusions
= NULL
;
349 struct lttng_bytecode
*local_bytecode
= NULL
;
350 char *local_filter_expression
= NULL
;
351 const struct lttng_event_comm
*event_comm
;
352 struct lttng_event_function_attr
*local_function_attr
= NULL
;
353 struct lttng_event_probe_attr
*local_probe_attr
= NULL
;
354 struct lttng_userspace_probe_location
*local_userspace_probe_location
=
358 * Only event is obligatory, the other output argument are optional and
359 * depends on what the caller is interested in.
365 struct lttng_payload_view comm_view
=
366 lttng_payload_view_from_view(view
, offset
,
367 sizeof(*event_comm
));
369 if (!lttng_payload_view_is_valid(&comm_view
)) {
374 /* lttng_event_comm header */
375 event_comm
= (typeof(event_comm
)) comm_view
.buffer
.data
;
376 offset
+= sizeof(*event_comm
);
379 local_event
= lttng_event_create();
380 if (local_event
== NULL
) {
385 local_event
->type
= (enum lttng_event_type
) event_comm
->event_type
;
386 local_event
->loglevel_type
= (enum lttng_loglevel_type
) event_comm
->loglevel_type
;
387 local_event
->loglevel
= event_comm
->loglevel
;
388 local_event
->enabled
= event_comm
->enabled
;
389 local_event
->pid
= event_comm
->pid
;
390 local_event
->flags
= (enum lttng_event_flag
) event_comm
->flags
;
394 const struct lttng_buffer_view name_view
=
395 lttng_buffer_view_from_view(&view
->buffer
,
396 offset
, event_comm
->name_len
);
398 if (!lttng_buffer_view_is_valid(&name_view
)) {
403 name
= (const char *) name_view
.data
;
405 if (!lttng_buffer_view_contains_string(
406 &name_view
, name
, event_comm
->name_len
)) {
411 ret
= lttng_strncpy(local_event
->name
, name
,
412 sizeof(local_event
->name
));
418 offset
+= event_comm
->name_len
;
422 if (event_comm
->exclusion_count
== 0) {
423 goto deserialize_filter_expression
;
427 struct lttng_payload_view exclusions_view
=
428 lttng_payload_view_from_view(
431 if (!lttng_payload_view_is_valid(&exclusions_view
)) {
436 ret
= lttng_event_exclusions_create_from_payload(&exclusions_view
,
437 event_comm
->exclusion_count
, &local_exclusions
);
444 local_event
->exclusion
= 1;
447 deserialize_filter_expression
:
449 if (event_comm
->filter_expression_len
== 0) {
450 if (event_comm
->bytecode_len
!= 0) {
452 * This is an invalid event payload.
454 * Filter expression without bytecode is possible but
455 * not the other way around.
460 goto deserialize_event_type_payload
;
464 const char *filter_expression_buffer
;
465 struct lttng_buffer_view filter_expression_view
=
466 lttng_buffer_view_from_view(&view
->buffer
, offset
,
467 event_comm
->filter_expression_len
);
469 if (!lttng_buffer_view_is_valid(&filter_expression_view
)) {
474 filter_expression_buffer
= filter_expression_view
.data
;
476 if (!lttng_buffer_view_contains_string(&filter_expression_view
,
477 filter_expression_buffer
,
478 event_comm
->filter_expression_len
)) {
483 local_filter_expression
= lttng_strndup(
484 filter_expression_buffer
,
485 event_comm
->filter_expression_len
);
486 if (!local_filter_expression
) {
491 local_event
->filter
= 1;
493 offset
+= event_comm
->filter_expression_len
;
496 if (event_comm
->bytecode_len
== 0) {
498 * Filter expression can be present but without bytecode
499 * when dealing with event listing.
501 goto deserialize_event_type_payload
;
506 struct lttng_payload_view bytecode_view
=
507 lttng_payload_view_from_view(view
, offset
,
508 event_comm
->bytecode_len
);
510 if (!lttng_payload_view_is_valid(&bytecode_view
)) {
515 local_bytecode
= (struct lttng_bytecode
*) zmalloc(
516 event_comm
->bytecode_len
);
517 if (!local_bytecode
) {
522 memcpy(local_bytecode
, bytecode_view
.buffer
.data
,
523 event_comm
->bytecode_len
);
524 if ((local_bytecode
->len
+ sizeof(*local_bytecode
)) !=
525 event_comm
->bytecode_len
) {
530 offset
+= event_comm
->bytecode_len
;
533 deserialize_event_type_payload
:
534 /* Event type specific payload */
535 switch (local_event
->type
) {
536 case LTTNG_EVENT_FUNCTION
:
538 case LTTNG_EVENT_PROBE
:
540 struct lttng_payload_view probe_attr_view
=
541 lttng_payload_view_from_view(view
, offset
,
542 event_comm
->lttng_event_probe_attr_len
);
544 if (event_comm
->lttng_event_probe_attr_len
== 0) {
549 if (!lttng_payload_view_is_valid(&probe_attr_view
)) {
554 ret
= lttng_event_probe_attr_create_from_payload(
555 &probe_attr_view
, &local_probe_attr
);
556 if (ret
< 0 || ret
!= event_comm
->lttng_event_probe_attr_len
) {
561 /* Copy to the local event. */
562 memcpy(&local_event
->attr
.probe
, local_probe_attr
,
563 sizeof(local_event
->attr
.probe
));
568 case LTTNG_EVENT_FUNCTION_ENTRY
:
570 struct lttng_payload_view function_attr_view
=
571 lttng_payload_view_from_view(view
, offset
,
572 event_comm
->lttng_event_function_attr_len
);
574 if (event_comm
->lttng_event_function_attr_len
== 0) {
579 if (!lttng_payload_view_is_valid(&function_attr_view
)) {
584 ret
= lttng_event_function_attr_create_from_payload(
585 &function_attr_view
, &local_function_attr
);
586 if (ret
< 0 || ret
!= event_comm
->lttng_event_function_attr_len
) {
591 /* Copy to the local event. */
592 memcpy(&local_event
->attr
.ftrace
, local_function_attr
,
593 sizeof(local_event
->attr
.ftrace
));
599 case LTTNG_EVENT_USERSPACE_PROBE
:
601 struct lttng_payload_view userspace_probe_location_view
=
602 lttng_payload_view_from_view(view
, offset
,
603 event_comm
->userspace_probe_location_len
);
605 if (event_comm
->userspace_probe_location_len
== 0) {
610 if (!lttng_payload_view_is_valid(
611 &userspace_probe_location_view
)) {
616 ret
= lttng_userspace_probe_location_create_from_payload(
617 &userspace_probe_location_view
,
618 &local_userspace_probe_location
);
620 WARN("Failed to create a userspace probe location from the received buffer");
625 if (ret
!= event_comm
->userspace_probe_location_len
) {
626 WARN("Userspace probe location from the received buffer is not the advertised length: header length = %" PRIu32
", payload length = %zd", event_comm
->userspace_probe_location_len
, ret
);
631 /* Attach the probe location to the event. */
632 ret
= lttng_event_set_userspace_probe_location(
633 local_event
, local_userspace_probe_location
);
635 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
640 * Userspace probe location object ownership transfered to the
643 local_userspace_probe_location
= NULL
;
644 offset
+= event_comm
->userspace_probe_location_len
;
647 case LTTNG_EVENT_TRACEPOINT
:
649 case LTTNG_EVENT_ALL
:
651 case LTTNG_EVENT_SYSCALL
:
653 case LTTNG_EVENT_NOOP
:
654 /* Nothing to do here */
662 /* Transfer ownership to the caller. */
663 *out_event
= local_event
;
667 *out_bytecode
= local_bytecode
;
668 local_bytecode
= NULL
;
672 *out_exclusion
= local_exclusions
;
673 local_exclusions
= NULL
;
676 if (out_filter_expression
) {
677 *out_filter_expression
= local_filter_expression
;
678 local_filter_expression
= NULL
;
683 lttng_event_destroy(local_event
);
684 lttng_userspace_probe_location_destroy(local_userspace_probe_location
);
685 free(local_filter_expression
);
686 free(local_exclusions
);
687 free(local_bytecode
);
688 free(local_function_attr
);
689 free(local_probe_attr
);
693 int lttng_event_serialize(const struct lttng_event
*event
,
694 unsigned int exclusion_count
,
695 char **exclusion_list
,
696 char *filter_expression
,
698 struct lttng_bytecode
*bytecode
,
699 struct lttng_payload
*payload
)
703 size_t header_offset
, size_before_payload
;
705 struct lttng_event_comm event_comm
= {};
706 struct lttng_event_comm
*header
;
710 assert(exclusion_count
== 0 || exclusion_list
);
712 /* Save the header location for later in-place header update. */
713 header_offset
= payload
->buffer
.size
;
715 name_len
= lttng_strnlen(event
->name
, sizeof(event
->name
));
716 if (name_len
== sizeof(event
->name
)) {
717 /* Event name is not NULL-terminated. */
722 /* Add null termination. */
725 if (exclusion_count
> UINT32_MAX
) {
726 /* Possible overflow. */
731 if (bytecode_len
> UINT32_MAX
) {
732 /* Possible overflow. */
737 event_comm
.name_len
= (uint32_t) name_len
;
738 event_comm
.event_type
= (int8_t) event
->type
;
739 event_comm
.loglevel_type
= (int8_t) event
->loglevel_type
;
740 event_comm
.loglevel
= (int32_t) event
->loglevel
;
741 event_comm
.enabled
= (int8_t) event
->enabled
;
742 event_comm
.pid
= (int32_t) event
->pid
;
743 event_comm
.exclusion_count
= (uint32_t) exclusion_count
;
744 event_comm
.bytecode_len
= (uint32_t) bytecode_len
;
745 event_comm
.flags
= (int32_t) event
->flags
;
747 if (filter_expression
) {
748 event_comm
.filter_expression_len
=
749 strlen(filter_expression
) + 1;
753 ret
= lttng_dynamic_buffer_append(
754 &payload
->buffer
, &event_comm
, sizeof(event_comm
));
760 ret
= lttng_dynamic_buffer_append(
761 &payload
->buffer
, event
->name
, name_len
);
767 for (i
= 0; i
< exclusion_count
; i
++) {
768 const size_t exclusion_len
= lttng_strnlen(
769 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
770 const struct lttng_event_exclusion_comm exclusion_header
= {
771 .len
= (uint32_t) exclusion_len
+ 1,
774 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
775 /* Exclusion is not NULL-terminated. */
780 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
781 &exclusion_header
, sizeof(exclusion_header
));
786 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
787 *(exclusion_list
+ i
), exclusion_len
+ 1);
793 /* Filter expression and its bytecode */
794 if (filter_expression
) {
795 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
797 event_comm
.filter_expression_len
);
803 * Bytecode can be absent when we serialize to the client
807 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
808 bytecode
, bytecode_len
);
815 size_before_payload
= payload
->buffer
.size
;
817 /* Event type specific payload */
818 switch (event
->type
) {
819 case LTTNG_EVENT_FUNCTION
:
821 case LTTNG_EVENT_PROBE
:
822 ret
= lttng_event_probe_attr_serialize(&event
->attr
.probe
, payload
);
828 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
830 header
->lttng_event_probe_attr_len
=
831 payload
->buffer
.size
- size_before_payload
;
834 case LTTNG_EVENT_FUNCTION_ENTRY
:
835 ret
= lttng_event_function_attr_serialize(
836 &event
->attr
.ftrace
, payload
);
842 /* Update the lttng_event_function_attr len. */
843 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
845 header
->lttng_event_function_attr_len
=
846 payload
->buffer
.size
- size_before_payload
;
849 case LTTNG_EVENT_USERSPACE_PROBE
:
851 const struct lttng_event_extended
*ev_ext
=
852 (const struct lttng_event_extended
*)
855 assert(event
->extended
.ptr
);
856 assert(ev_ext
->probe_location
);
858 size_before_payload
= payload
->buffer
.size
;
859 if (ev_ext
->probe_location
) {
861 * lttng_userspace_probe_location_serialize returns the
862 * number of bytes that were appended to the buffer.
864 ret
= lttng_userspace_probe_location_serialize(
865 ev_ext
->probe_location
, payload
);
872 /* Update the userspace probe location len. */
873 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
875 header
->userspace_probe_location_len
=
876 payload
->buffer
.size
- size_before_payload
;
880 case LTTNG_EVENT_TRACEPOINT
:
882 case LTTNG_EVENT_ALL
:
885 /* Nothing to do here. */
893 static ssize_t
lttng_event_context_app_populate_from_payload(
894 const struct lttng_payload_view
*view
,
895 struct lttng_event_context
*event_ctx
)
897 ssize_t ret
, offset
= 0;
898 const struct lttng_event_context_app_comm
*comm
;
899 char *provider_name
= NULL
, *context_name
= NULL
;
900 size_t provider_name_len
, context_name_len
;
901 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
902 &view
->buffer
, offset
, sizeof(*comm
));
904 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
906 if (!lttng_buffer_view_is_valid(&comm_view
)) {
911 comm
= (typeof(comm
)) comm_view
.data
;
912 offset
+= sizeof(*comm
);
914 provider_name_len
= comm
->provider_name_len
;
915 context_name_len
= comm
->ctx_name_len
;
917 if (provider_name_len
== 0 || context_name_len
== 0) {
919 * Application provider and context names MUST
928 const struct lttng_buffer_view provider_name_view
=
929 lttng_buffer_view_from_view(&view
->buffer
,
933 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
938 name
= provider_name_view
.data
;
940 if (!lttng_buffer_view_contains_string(&provider_name_view
,
941 name
, provider_name_len
)) {
946 provider_name
= lttng_strndup(name
, provider_name_len
);
947 if (!provider_name
) {
952 offset
+= provider_name_len
;
957 const struct lttng_buffer_view context_name_view
=
958 lttng_buffer_view_from_view(
959 &view
->buffer
, offset
,
962 if (!lttng_buffer_view_is_valid(&context_name_view
)) {
967 name
= context_name_view
.data
;
969 if (!lttng_buffer_view_contains_string(&context_name_view
, name
,
975 context_name
= lttng_strndup(name
, context_name_len
);
981 offset
+= context_name_len
;
984 /* Transfer ownership of the strings */
985 event_ctx
->u
.app_ctx
.provider_name
= provider_name
;
986 event_ctx
->u
.app_ctx
.ctx_name
= context_name
;
987 provider_name
= NULL
;
998 static ssize_t
lttng_event_context_perf_counter_populate_from_payload(
999 const struct lttng_payload_view
*view
,
1000 struct lttng_event_context
*event_ctx
)
1003 ssize_t consumed
, offset
= 0;
1004 const struct lttng_event_context_perf_counter_comm
*comm
;
1006 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1007 &view
->buffer
, offset
, sizeof(*comm
));
1009 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_COUNTER
||
1011 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
||
1012 event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
);
1014 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1019 comm
= (typeof(comm
)) comm_view
.data
;
1020 offset
+= sizeof(*comm
);
1022 name_len
= comm
->name_len
;
1026 const struct lttng_buffer_view provider_name_view
=
1027 lttng_buffer_view_from_view(
1028 &view
->buffer
, offset
,
1031 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
1036 name
= provider_name_view
.data
;
1038 if (!lttng_buffer_view_contains_string(
1039 &provider_name_view
, name
, name_len
)) {
1044 ret
= lttng_strncpy(event_ctx
->u
.perf_counter
.name
, name
, name_len
);
1052 event_ctx
->u
.perf_counter
.config
= comm
->config
;
1053 event_ctx
->u
.perf_counter
.type
= comm
->type
;
1061 ssize_t
lttng_event_context_create_from_payload(
1062 struct lttng_payload_view
*view
,
1063 struct lttng_event_context
**event_ctx
)
1065 ssize_t ret
, offset
= 0;
1066 const struct lttng_event_context_comm
*comm
;
1067 struct lttng_event_context
*local_context
= NULL
;
1068 struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1069 &view
->buffer
, offset
, sizeof(*comm
));
1074 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1079 comm
= (typeof(comm
)) comm_view
.data
;
1080 offset
+= sizeof(*comm
);
1082 local_context
= (struct lttng_event_context
*)
1083 zmalloc(sizeof(*local_context
));
1084 if (!local_context
) {
1089 local_context
->ctx
= (lttng_event_context_type
) comm
->type
;
1092 struct lttng_payload_view subtype_view
=
1093 lttng_payload_view_from_view(view
, offset
, -1);
1095 switch (local_context
->ctx
) {
1096 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1097 ret
= lttng_event_context_app_populate_from_payload(
1098 &subtype_view
, local_context
);
1100 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1101 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1102 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1103 ret
= lttng_event_context_perf_counter_populate_from_payload(
1104 &subtype_view
, local_context
);
1107 /* Nothing else to deserialize. */
1119 *event_ctx
= local_context
;
1120 local_context
= NULL
;
1124 free(local_context
);
1128 static int lttng_event_context_app_serialize(
1129 struct lttng_event_context
*context
,
1130 struct lttng_payload
*payload
)
1133 struct lttng_event_context_app_comm comm
= {};
1134 size_t provider_len
, ctx_len
;
1135 const char *provider_name
;
1136 const char *ctx_name
;
1140 assert(context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1142 provider_name
= context
->u
.app_ctx
.provider_name
;
1143 ctx_name
= context
->u
.app_ctx
.ctx_name
;
1145 if (!provider_name
|| !ctx_name
) {
1146 ret
= -LTTNG_ERR_INVALID
;
1150 provider_len
= strlen(provider_name
);
1151 if (provider_len
== 0) {
1152 ret
= -LTTNG_ERR_INVALID
;
1156 /* Include the null terminator. */
1158 comm
.provider_name_len
= provider_len
;
1160 ctx_len
= strlen(ctx_name
);
1162 ret
= -LTTNG_ERR_INVALID
;
1166 /* Include the null terminator. */
1168 comm
.ctx_name_len
= ctx_len
;
1171 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1178 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, provider_name
,
1185 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, ctx_name
,
1196 static int lttng_event_context_perf_counter_serialize(
1197 struct lttng_event_perf_counter_ctx
*context
,
1198 struct lttng_payload
*payload
)
1201 struct lttng_event_context_perf_counter_comm comm
= {};
1206 comm
.config
= context
->config
;
1207 comm
.type
= context
->type
;
1208 comm
.name_len
= lttng_strnlen(context
->name
, sizeof(context
->name
));
1210 if (comm
.name_len
== sizeof(context
->name
)) {
1215 /* Include the null terminator. */
1219 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1226 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, context
->name
,
1237 int lttng_event_context_serialize(struct lttng_event_context
*context
,
1238 struct lttng_payload
*payload
)
1241 struct lttng_event_context_comm context_comm
= { 0 };
1246 context_comm
.type
= (uint32_t) context
->ctx
;
1249 ret
= lttng_dynamic_buffer_append(
1250 &payload
->buffer
, &context_comm
, sizeof(context_comm
));
1255 switch (context
->ctx
) {
1256 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1257 ret
= lttng_event_context_app_serialize(context
, payload
);
1259 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1260 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1261 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1262 ret
= lttng_event_context_perf_counter_serialize(
1263 &context
->u
.perf_counter
, payload
);
1266 /* Nothing else to serialize. */
1278 void lttng_event_context_destroy(struct lttng_event_context
*context
)
1284 if (context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1285 free(context
->u
.app_ctx
.provider_name
);
1286 free(context
->u
.app_ctx
.ctx_name
);
1293 * This is a specialized populate for lttng_event_field since it ignores
1294 * the extension field of the lttng_event struct and simply copies what it can
1295 * to the internal struct lttng_event of a lttng_event_field.
1297 static void lttng_event_field_populate_lttng_event_from_event(
1298 const struct lttng_event
*src
, struct lttng_event
*destination
)
1300 memcpy(destination
, src
, sizeof(*destination
));
1302 /* Remove all possible dynamic data from the destination event rule. */
1303 destination
->extended
.ptr
= NULL
;
1306 ssize_t
lttng_event_field_create_from_payload(
1307 struct lttng_payload_view
*view
,
1308 struct lttng_event_field
**field
)
1310 ssize_t ret
, offset
= 0;
1311 struct lttng_event_field
*local_event_field
= NULL
;
1312 struct lttng_event
*event
= NULL
;
1313 const struct lttng_event_field_comm
*comm
;
1314 const char* name
= NULL
;
1320 const struct lttng_buffer_view comm_view
=
1321 lttng_buffer_view_from_view(
1322 &view
->buffer
, offset
,
1325 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1330 /* lttng_event_field_comm header */
1331 comm
= (const lttng_event_field_comm
*) comm_view
.data
;
1332 offset
+= sizeof(*comm
);
1335 local_event_field
= (struct lttng_event_field
*)
1336 zmalloc(sizeof(*local_event_field
));
1337 if (!local_event_field
) {
1342 local_event_field
->type
= (lttng_event_field_type
) comm
->type
;
1343 local_event_field
->nowrite
= comm
->nowrite
;
1347 const struct lttng_buffer_view name_view
=
1348 lttng_buffer_view_from_view(
1349 &view
->buffer
, offset
,
1352 if (!lttng_buffer_view_is_valid(&name_view
)) {
1357 name
= name_view
.data
;
1359 if (!lttng_buffer_view_contains_string(&name_view
,
1360 name_view
.data
, comm
->name_len
)) {
1365 if (comm
->name_len
> LTTNG_SYMBOL_NAME_LEN
- 1) {
1366 /* Name is too long.*/
1371 offset
+= comm
->name_len
;
1376 struct lttng_payload_view event_view
=
1377 lttng_payload_view_from_view(
1381 if (!lttng_payload_view_is_valid(&event_view
)) {
1386 ret
= lttng_event_create_from_payload(&event_view
, &event
, NULL
,
1388 if (ret
!= comm
->event_len
) {
1399 if (lttng_strncpy(local_event_field
->field_name
, name
,
1400 sizeof(local_event_field
->field_name
))) {
1405 lttng_event_field_populate_lttng_event_from_event(
1406 event
, &local_event_field
->event
);
1408 *field
= local_event_field
;
1409 local_event_field
= NULL
;
1412 lttng_event_destroy(event
);
1413 free(local_event_field
);
1417 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1418 struct lttng_payload
*payload
)
1421 size_t header_offset
, size_before_event
;
1423 struct lttng_event_field_comm event_field_comm
= {};
1424 struct lttng_event_field_comm
*header
;
1429 /* Save the header location for later in-place header update. */
1430 header_offset
= payload
->buffer
.size
;
1432 name_len
= strnlen(field
->field_name
, sizeof(field
->field_name
));
1433 if (name_len
== sizeof(field
->field_name
)) {
1434 /* Event name is not NULL-terminated. */
1439 /* Add null termination. */
1442 event_field_comm
.type
= field
->type
;
1443 event_field_comm
.nowrite
= (uint8_t)field
->nowrite
;
1444 event_field_comm
.name_len
= name_len
;
1447 ret
= lttng_dynamic_buffer_append(
1448 &payload
->buffer
, &event_field_comm
,
1449 sizeof(event_field_comm
));
1455 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, field
->field_name
,
1461 size_before_event
= payload
->buffer
.size
;
1462 ret
= lttng_event_serialize(
1463 &field
->event
, 0, NULL
, NULL
, 0, 0, payload
);
1469 /* Update the event len. */
1470 header
= (struct lttng_event_field_comm
*)
1471 ((char *) payload
->buffer
.data
+
1473 header
->event_len
= payload
->buffer
.size
- size_before_event
;
1479 static enum lttng_error_code
compute_flattened_size(
1480 struct lttng_dynamic_pointer_array
*events
, size_t *size
)
1482 enum lttng_error_code ret_code
;
1484 size_t storage_req
, event_count
, i
;
1489 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1491 /* The basic struct lttng_event */
1492 storage_req
= event_count
* sizeof(struct lttng_event
);
1494 /* The struct·lttng_event_extended */
1495 storage_req
+= event_count
* sizeof(struct lttng_event_extended
);
1497 for (i
= 0; i
< event_count
; i
++) {
1498 int probe_storage_req
= 0;
1499 const struct event_list_element
*element
= (const struct event_list_element
*)
1500 lttng_dynamic_pointer_array_get_pointer(
1502 const struct lttng_userspace_probe_location
*location
= NULL
;
1504 location
= lttng_event_get_userspace_probe_location(
1507 ret
= lttng_userspace_probe_location_flatten(
1510 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1514 probe_storage_req
= ret
;
1517 if (element
->filter_expression
) {
1518 storage_req
+= strlen(element
->filter_expression
) + 1;
1521 if (element
->exclusions
) {
1522 storage_req
+= element
->exclusions
->count
*
1523 LTTNG_SYMBOL_NAME_LEN
;
1526 /* Padding to ensure the flat probe is aligned. */
1527 storage_req
= lttng_align_ceil(storage_req
, sizeof(uint64_t));
1528 storage_req
+= probe_storage_req
;
1531 *size
= storage_req
;
1532 ret_code
= LTTNG_OK
;
1539 * Flatten a list of struct lttng_event.
1541 * The buffer that is returned to the API client must contain a "flat" version
1542 * of the events that are returned. In other words, all pointers within an
1543 * lttng_event must point to a location within the returned buffer so that the
1544 * user may free everything by simply calling free() on the returned buffer.
1545 * This is needed in order to maintain API compatibility.
1547 * A first pass is performed to compute the size of the buffer that must be
1548 * allocated. A second pass is then performed to setup the returned events so
1549 * that their members always point within the buffer.
1551 * The layout of the returned buffer is as follows:
1552 * - struct lttng_event[nb_events],
1553 * - nb_events times the following:
1554 * - struct lttng_event_extended,
1555 * - filter_expression
1557 * - padding to align to 64-bits
1558 * - flattened version of userspace_probe_location
1560 static enum lttng_error_code
flatten_lttng_events(
1561 struct lttng_dynamic_pointer_array
*events
,
1562 struct lttng_event
**flattened_events
)
1564 enum lttng_error_code ret_code
;
1567 struct lttng_dynamic_buffer local_flattened_events
;
1571 assert(flattened_events
);
1573 lttng_dynamic_buffer_init(&local_flattened_events
);
1574 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1576 ret_code
= compute_flattened_size(events
, &storage_req
);
1577 if (ret_code
!= LTTNG_OK
) {
1582 * We must ensure that "local_flattened_events" is never resized so as
1583 * to preserve the validity of the flattened objects.
1585 ret
= lttng_dynamic_buffer_set_capacity(
1586 &local_flattened_events
, storage_req
);
1588 ret_code
= LTTNG_ERR_NOMEM
;
1592 /* Start by laying the struct lttng_event */
1593 for (i
= 0; i
< nb_events
; i
++) {
1594 const struct event_list_element
*element
= (const struct event_list_element
*)
1595 lttng_dynamic_pointer_array_get_pointer(
1599 ret_code
= LTTNG_ERR_FATAL
;
1603 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1604 element
->event
, sizeof(struct lttng_event
));
1606 ret_code
= LTTNG_ERR_NOMEM
;
1611 for (i
= 0; i
< nb_events
; i
++) {
1612 const struct event_list_element
*element
= (const struct event_list_element
*)
1613 lttng_dynamic_pointer_array_get_pointer(events
, i
);
1614 struct lttng_event
*event
= (struct lttng_event
*)
1615 (local_flattened_events
.data
+ (sizeof(struct lttng_event
) * i
));
1616 struct lttng_event_extended
*event_extended
=
1617 (struct lttng_event_extended
*)
1618 (local_flattened_events
.data
+ local_flattened_events
.size
);
1619 const struct lttng_userspace_probe_location
*location
= NULL
;
1623 /* Insert struct lttng_event_extended. */
1624 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1625 local_flattened_events
.size
+
1626 sizeof(*event_extended
));
1628 ret_code
= LTTNG_ERR_NOMEM
;
1631 event
->extended
.ptr
= event_extended
;
1633 /* Insert filter expression. */
1634 if (element
->filter_expression
) {
1635 const size_t len
= strlen(element
->filter_expression
) + 1;
1637 event_extended
->filter_expression
=
1638 local_flattened_events
.data
+
1639 local_flattened_events
.size
;
1640 ret
= lttng_dynamic_buffer_append(
1641 &local_flattened_events
,
1642 element
->filter_expression
, len
);
1644 ret_code
= LTTNG_ERR_NOMEM
;
1649 /* Insert exclusions. */
1650 if (element
->exclusions
) {
1651 event_extended
->exclusions
.count
=
1652 element
->exclusions
->count
;
1653 event_extended
->exclusions
.strings
=
1654 local_flattened_events
.data
+
1655 local_flattened_events
.size
;
1657 ret
= lttng_dynamic_buffer_append(
1658 &local_flattened_events
,
1659 element
->exclusions
->names
,
1660 element
->exclusions
->count
*
1661 LTTNG_SYMBOL_NAME_LEN
);
1663 ret_code
= LTTNG_ERR_NOMEM
;
1668 /* Insert padding to align to 64-bits. */
1669 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1670 lttng_align_ceil(local_flattened_events
.size
,
1673 ret_code
= LTTNG_ERR_NOMEM
;
1677 location
= lttng_event_get_userspace_probe_location(
1680 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
1681 (local_flattened_events
.data
+ local_flattened_events
.size
);
1682 ret
= lttng_userspace_probe_location_flatten(
1683 location
, &local_flattened_events
);
1685 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1691 /* Don't reset local_flattened_events buffer as we return its content. */
1692 *flattened_events
= (struct lttng_event
*) local_flattened_events
.data
;
1693 lttng_dynamic_buffer_init(&local_flattened_events
);
1694 ret_code
= LTTNG_OK
;
1696 lttng_dynamic_buffer_reset(&local_flattened_events
);
1700 static enum lttng_error_code
event_list_create_from_payload(
1701 struct lttng_payload_view
*view
,
1703 struct lttng_dynamic_pointer_array
*event_list
)
1705 enum lttng_error_code ret_code
;
1713 for (i
= 0; i
< count
; i
++) {
1715 struct lttng_payload_view event_view
=
1716 lttng_payload_view_from_view(view
, offset
, -1);
1717 struct event_list_element
*element
=
1718 (struct event_list_element
*) zmalloc(sizeof(*element
));
1721 ret_code
= LTTNG_ERR_NOMEM
;
1726 * Lifetime and management of the object is now bound to the
1729 ret
= lttng_dynamic_pointer_array_add_pointer(
1730 event_list
, element
);
1732 event_list_destructor(element
);
1733 ret_code
= LTTNG_ERR_NOMEM
;
1738 * Bytecode is not transmitted on listing in any case we do not
1741 event_size
= lttng_event_create_from_payload(&event_view
,
1743 &element
->exclusions
,
1744 &element
->filter_expression
, NULL
);
1745 if (event_size
< 0) {
1746 ret_code
= LTTNG_ERR_INVALID
;
1750 offset
+= event_size
;
1753 if (view
->buffer
.size
!= offset
) {
1754 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
1758 ret_code
= LTTNG_OK
;
1764 enum lttng_error_code
lttng_events_create_and_flatten_from_payload(
1765 struct lttng_payload_view
*payload
,
1767 struct lttng_event
**events
)
1769 enum lttng_error_code ret
= LTTNG_OK
;
1770 struct lttng_dynamic_pointer_array local_events
;
1772 lttng_dynamic_pointer_array_init(&local_events
, event_list_destructor
);
1774 /* Deserialize the events. */
1776 struct lttng_payload_view events_view
=
1777 lttng_payload_view_from_view(payload
, 0, -1);
1779 ret
= event_list_create_from_payload(
1780 &events_view
, count
, &local_events
);
1781 if (ret
!= LTTNG_OK
) {
1786 ret
= flatten_lttng_events(&local_events
, events
);
1787 if (ret
!= LTTNG_OK
) {
1792 lttng_dynamic_pointer_array_reset(&local_events
);
1796 static enum lttng_error_code
flatten_lttng_event_fields(
1797 struct lttng_dynamic_pointer_array
*event_fields
,
1798 struct lttng_event_field
**flattened_event_fields
)
1801 enum lttng_error_code ret_code
;
1802 size_t storage_req
= 0;
1803 struct lttng_dynamic_buffer local_flattened_event_fields
;
1806 assert(event_fields
);
1807 assert(flattened_event_fields
);
1809 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1810 nb_event_field
= lttng_dynamic_pointer_array_get_count(event_fields
);
1813 * Here even if the event field contains a `struct lttng_event` that
1814 * could contain dynamic data, in reality it is not the case.
1815 * Dynamic data is not present. Here the flattening is mostly a direct
1816 * memcpy. This is less than ideal but this code is still better than
1817 * direct usage of an unpacked lttng_event_field array.
1819 storage_req
+= sizeof(struct lttng_event_field
) * nb_event_field
;
1821 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1824 * We must ensure that "local_flattened_event_fields" is never resized
1825 * so as to preserve the validity of the flattened objects.
1827 ret
= lttng_dynamic_buffer_set_capacity(
1828 &local_flattened_event_fields
, storage_req
);
1830 ret_code
= LTTNG_ERR_NOMEM
;
1834 for (i
= 0; i
< nb_event_field
; i
++) {
1835 const struct lttng_event_field
*element
=
1836 (const struct lttng_event_field
*)
1837 lttng_dynamic_pointer_array_get_pointer(
1841 ret_code
= LTTNG_ERR_FATAL
;
1844 ret
= lttng_dynamic_buffer_append(&local_flattened_event_fields
,
1845 element
, sizeof(struct lttng_event_field
));
1847 ret_code
= LTTNG_ERR_NOMEM
;
1852 /* Don't reset local_flattened_channels buffer as we return its content. */
1853 *flattened_event_fields
= (struct lttng_event_field
*) local_flattened_event_fields
.data
;
1854 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1855 ret_code
= LTTNG_OK
;
1857 lttng_dynamic_buffer_reset(&local_flattened_event_fields
);
1861 static enum lttng_error_code
event_field_list_create_from_payload(
1862 struct lttng_payload_view
*view
,
1864 struct lttng_dynamic_pointer_array
**event_field_list
)
1866 enum lttng_error_code ret_code
;
1867 int ret
, offset
= 0;
1869 struct lttng_dynamic_pointer_array
*list
= NULL
;
1872 assert(event_field_list
);
1874 list
= (struct lttng_dynamic_pointer_array
*) zmalloc(sizeof(*list
));
1876 ret_code
= LTTNG_ERR_NOMEM
;
1880 lttng_dynamic_pointer_array_init(list
, free
);
1882 for (i
= 0; i
< count
; i
++) {
1883 ssize_t event_field_size
;
1884 struct lttng_event_field
*field
= NULL
;
1885 struct lttng_payload_view event_field_view
=
1886 lttng_payload_view_from_view(view
, offset
, -1);
1888 event_field_size
= lttng_event_field_create_from_payload(
1889 &event_field_view
, &field
);
1890 if (event_field_size
< 0) {
1891 ret_code
= LTTNG_ERR_INVALID
;
1895 /* Lifetime and management of the object is now bound to the array. */
1896 ret
= lttng_dynamic_pointer_array_add_pointer(list
, field
);
1899 ret_code
= LTTNG_ERR_NOMEM
;
1903 offset
+= event_field_size
;
1906 if (view
->buffer
.size
!= offset
) {
1907 ret_code
= LTTNG_ERR_INVALID
;
1911 *event_field_list
= list
;
1913 ret_code
= LTTNG_OK
;
1917 lttng_dynamic_pointer_array_reset(list
);
1924 enum lttng_error_code
lttng_event_fields_create_and_flatten_from_payload(
1925 struct lttng_payload_view
*view
,
1927 struct lttng_event_field
**fields
)
1929 enum lttng_error_code ret_code
;
1930 struct lttng_dynamic_pointer_array
*local_event_fields
= NULL
;
1932 ret_code
= event_field_list_create_from_payload(
1933 view
, count
, &local_event_fields
);
1934 if (ret_code
!= LTTNG_OK
) {
1938 ret_code
= flatten_lttng_event_fields(local_event_fields
, fields
);
1939 if (ret_code
!= LTTNG_OK
) {
1943 if (local_event_fields
) {
1944 lttng_dynamic_pointer_array_reset(local_event_fields
);
1945 free(local_event_fields
);