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(
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
= zmalloc
<lttng_event_probe_attr
>();
163 if (local_attr
== NULL
) {
168 local_attr
->addr
= comm
->addr
;
169 local_attr
->offset
= comm
->offset
;
173 struct lttng_payload_view name_view
=
174 lttng_payload_view_from_view(view
, offset
,
175 comm
->symbol_name_len
);
177 if (!lttng_payload_view_is_valid(&name_view
)) {
182 name
= name_view
.buffer
.data
;
184 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
185 comm
->symbol_name_len
)) {
190 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
191 sizeof(local_attr
->symbol_name
));
197 offset
+= comm
->symbol_name_len
;
200 *probe_attr
= local_attr
;
208 static ssize_t
lttng_event_function_attr_create_from_payload(
209 struct lttng_payload_view
*view
,
210 struct lttng_event_function_attr
**function_attr
)
212 ssize_t ret
, offset
= 0;
213 const struct lttng_event_function_attr_comm
*comm
;
214 struct lttng_event_function_attr
*local_attr
= NULL
;
215 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
216 view
, offset
, sizeof(*comm
));
218 if (!lttng_payload_view_is_valid(&comm_view
)) {
223 comm
= (typeof(comm
)) view
->buffer
.data
;
224 offset
+= sizeof(*comm
);
226 local_attr
= zmalloc
<lttng_event_function_attr
>();
227 if (local_attr
== NULL
) {
234 struct lttng_payload_view name_view
=
235 lttng_payload_view_from_view(view
, offset
,
236 comm
->symbol_name_len
);
238 if (!lttng_payload_view_is_valid(&name_view
)) {
243 name
= name_view
.buffer
.data
;
245 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
246 comm
->symbol_name_len
)) {
251 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
252 sizeof(local_attr
->symbol_name
));
258 offset
+= comm
->symbol_name_len
;
261 *function_attr
= local_attr
;
269 static ssize_t
lttng_event_exclusions_create_from_payload(
270 struct lttng_payload_view
*view
,
272 struct lttng_event_exclusion
**exclusions
)
274 ssize_t ret
, offset
= 0;
275 const size_t size
= (count
* LTTNG_SYMBOL_NAME_LEN
);
277 const struct lttng_event_exclusion_comm
*comm
;
278 struct lttng_event_exclusion
*local_exclusions
;
280 local_exclusions
= zmalloc
<lttng_event_exclusion
>(
281 sizeof(struct lttng_event_exclusion
) + size
);
282 if (!local_exclusions
) {
287 local_exclusions
->count
= count
;
289 for (i
= 0; i
< count
; i
++) {
291 struct lttng_buffer_view string_view
;
292 const struct lttng_buffer_view comm_view
=
293 lttng_buffer_view_from_view(&view
->buffer
,
294 offset
, sizeof(*comm
));
296 if (!lttng_buffer_view_is_valid(&comm_view
)) {
301 comm
= (typeof(comm
)) comm_view
.data
;
302 offset
+= sizeof(*comm
);
304 string_view
= lttng_buffer_view_from_view(
305 &view
->buffer
, offset
, comm
->len
);
307 if (!lttng_buffer_view_is_valid(&string_view
)) {
312 string
= string_view
.data
;
314 if (!lttng_buffer_view_contains_string(
315 &string_view
, string
, comm
->len
)) {
320 ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(local_exclusions
, i
), string
,
321 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(local_exclusions
, i
)));
330 *exclusions
= local_exclusions
;
331 local_exclusions
= NULL
;
334 free(local_exclusions
);
338 ssize_t
lttng_event_create_from_payload(struct lttng_payload_view
*view
,
339 struct lttng_event
**out_event
,
340 struct lttng_event_exclusion
**out_exclusion
,
341 char **out_filter_expression
,
342 struct lttng_bytecode
**out_bytecode
)
344 ssize_t ret
, offset
= 0;
345 struct lttng_event
*local_event
= NULL
;
346 struct lttng_event_exclusion
*local_exclusions
= NULL
;
347 struct lttng_bytecode
*local_bytecode
= NULL
;
348 char *local_filter_expression
= NULL
;
349 const struct lttng_event_comm
*event_comm
;
350 struct lttng_event_function_attr
*local_function_attr
= NULL
;
351 struct lttng_event_probe_attr
*local_probe_attr
= NULL
;
352 struct lttng_userspace_probe_location
*local_userspace_probe_location
=
356 * Only event is obligatory, the other output argument are optional and
357 * depends on what the caller is interested in.
363 struct lttng_payload_view comm_view
=
364 lttng_payload_view_from_view(view
, offset
,
365 sizeof(*event_comm
));
367 if (!lttng_payload_view_is_valid(&comm_view
)) {
372 /* lttng_event_comm header */
373 event_comm
= (typeof(event_comm
)) comm_view
.buffer
.data
;
374 offset
+= sizeof(*event_comm
);
377 local_event
= lttng_event_create();
378 if (local_event
== NULL
) {
383 local_event
->type
= (enum lttng_event_type
) event_comm
->event_type
;
384 local_event
->loglevel_type
= (enum lttng_loglevel_type
) event_comm
->loglevel_type
;
385 local_event
->loglevel
= event_comm
->loglevel
;
386 local_event
->enabled
= event_comm
->enabled
;
387 local_event
->pid
= event_comm
->pid
;
388 local_event
->flags
= (enum lttng_event_flag
) event_comm
->flags
;
392 const struct lttng_buffer_view name_view
=
393 lttng_buffer_view_from_view(&view
->buffer
,
394 offset
, event_comm
->name_len
);
396 if (!lttng_buffer_view_is_valid(&name_view
)) {
401 name
= (const char *) name_view
.data
;
403 if (!lttng_buffer_view_contains_string(
404 &name_view
, name
, event_comm
->name_len
)) {
409 ret
= lttng_strncpy(local_event
->name
, name
,
410 sizeof(local_event
->name
));
416 offset
+= event_comm
->name_len
;
420 if (event_comm
->exclusion_count
== 0) {
421 goto deserialize_filter_expression
;
425 struct lttng_payload_view exclusions_view
=
426 lttng_payload_view_from_view(
429 if (!lttng_payload_view_is_valid(&exclusions_view
)) {
434 ret
= lttng_event_exclusions_create_from_payload(&exclusions_view
,
435 event_comm
->exclusion_count
, &local_exclusions
);
442 local_event
->exclusion
= 1;
445 deserialize_filter_expression
:
447 if (event_comm
->filter_expression_len
== 0) {
448 if (event_comm
->bytecode_len
!= 0) {
450 * This is an invalid event payload.
452 * Filter expression without bytecode is possible but
453 * not the other way around.
458 goto deserialize_event_type_payload
;
462 const char *filter_expression_buffer
;
463 struct lttng_buffer_view filter_expression_view
=
464 lttng_buffer_view_from_view(&view
->buffer
, offset
,
465 event_comm
->filter_expression_len
);
467 if (!lttng_buffer_view_is_valid(&filter_expression_view
)) {
472 filter_expression_buffer
= filter_expression_view
.data
;
474 if (!lttng_buffer_view_contains_string(&filter_expression_view
,
475 filter_expression_buffer
,
476 event_comm
->filter_expression_len
)) {
481 local_filter_expression
= lttng_strndup(
482 filter_expression_buffer
,
483 event_comm
->filter_expression_len
);
484 if (!local_filter_expression
) {
489 local_event
->filter
= 1;
491 offset
+= event_comm
->filter_expression_len
;
494 if (event_comm
->bytecode_len
== 0) {
496 * Filter expression can be present but without bytecode
497 * when dealing with event listing.
499 goto deserialize_event_type_payload
;
504 struct lttng_payload_view bytecode_view
=
505 lttng_payload_view_from_view(view
, offset
,
506 event_comm
->bytecode_len
);
508 if (!lttng_payload_view_is_valid(&bytecode_view
)) {
513 local_bytecode
= zmalloc
<lttng_bytecode
>(event_comm
->bytecode_len
);
514 if (!local_bytecode
) {
519 memcpy(local_bytecode
, bytecode_view
.buffer
.data
,
520 event_comm
->bytecode_len
);
521 if ((local_bytecode
->len
+ sizeof(*local_bytecode
)) !=
522 event_comm
->bytecode_len
) {
527 offset
+= event_comm
->bytecode_len
;
530 deserialize_event_type_payload
:
531 /* Event type specific payload */
532 switch (local_event
->type
) {
533 case LTTNG_EVENT_FUNCTION
:
535 case LTTNG_EVENT_PROBE
:
537 struct lttng_payload_view probe_attr_view
=
538 lttng_payload_view_from_view(view
, offset
,
539 event_comm
->lttng_event_probe_attr_len
);
541 if (event_comm
->lttng_event_probe_attr_len
== 0) {
546 if (!lttng_payload_view_is_valid(&probe_attr_view
)) {
551 ret
= lttng_event_probe_attr_create_from_payload(
552 &probe_attr_view
, &local_probe_attr
);
553 if (ret
< 0 || ret
!= event_comm
->lttng_event_probe_attr_len
) {
558 /* Copy to the local event. */
559 memcpy(&local_event
->attr
.probe
, local_probe_attr
,
560 sizeof(local_event
->attr
.probe
));
565 case LTTNG_EVENT_FUNCTION_ENTRY
:
567 struct lttng_payload_view function_attr_view
=
568 lttng_payload_view_from_view(view
, offset
,
569 event_comm
->lttng_event_function_attr_len
);
571 if (event_comm
->lttng_event_function_attr_len
== 0) {
576 if (!lttng_payload_view_is_valid(&function_attr_view
)) {
581 ret
= lttng_event_function_attr_create_from_payload(
582 &function_attr_view
, &local_function_attr
);
583 if (ret
< 0 || ret
!= event_comm
->lttng_event_function_attr_len
) {
588 /* Copy to the local event. */
589 memcpy(&local_event
->attr
.ftrace
, local_function_attr
,
590 sizeof(local_event
->attr
.ftrace
));
596 case LTTNG_EVENT_USERSPACE_PROBE
:
598 struct lttng_payload_view userspace_probe_location_view
=
599 lttng_payload_view_from_view(view
, offset
,
600 event_comm
->userspace_probe_location_len
);
602 if (event_comm
->userspace_probe_location_len
== 0) {
607 if (!lttng_payload_view_is_valid(
608 &userspace_probe_location_view
)) {
613 ret
= lttng_userspace_probe_location_create_from_payload(
614 &userspace_probe_location_view
,
615 &local_userspace_probe_location
);
617 WARN("Failed to create a userspace probe location from the received buffer");
622 if (ret
!= event_comm
->userspace_probe_location_len
) {
623 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
);
628 /* Attach the probe location to the event. */
629 ret
= lttng_event_set_userspace_probe_location(
630 local_event
, local_userspace_probe_location
);
632 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
637 * Userspace probe location object ownership transfered to the
640 local_userspace_probe_location
= NULL
;
641 offset
+= event_comm
->userspace_probe_location_len
;
644 case LTTNG_EVENT_TRACEPOINT
:
646 case LTTNG_EVENT_ALL
:
648 case LTTNG_EVENT_SYSCALL
:
650 case LTTNG_EVENT_NOOP
:
651 /* Nothing to do here */
659 /* Transfer ownership to the caller. */
660 *out_event
= local_event
;
664 *out_bytecode
= local_bytecode
;
665 local_bytecode
= NULL
;
669 *out_exclusion
= local_exclusions
;
670 local_exclusions
= NULL
;
673 if (out_filter_expression
) {
674 *out_filter_expression
= local_filter_expression
;
675 local_filter_expression
= NULL
;
680 lttng_event_destroy(local_event
);
681 lttng_userspace_probe_location_destroy(local_userspace_probe_location
);
682 free(local_filter_expression
);
683 free(local_exclusions
);
684 free(local_bytecode
);
685 free(local_function_attr
);
686 free(local_probe_attr
);
690 int lttng_event_serialize(const struct lttng_event
*event
,
691 unsigned int exclusion_count
,
692 char **exclusion_list
,
693 char *filter_expression
,
695 struct lttng_bytecode
*bytecode
,
696 struct lttng_payload
*payload
)
700 size_t header_offset
, size_before_payload
;
702 struct lttng_event_comm event_comm
= {};
703 struct lttng_event_comm
*header
;
707 assert(exclusion_count
== 0 || exclusion_list
);
709 /* Save the header location for later in-place header update. */
710 header_offset
= payload
->buffer
.size
;
712 name_len
= lttng_strnlen(event
->name
, sizeof(event
->name
));
713 if (name_len
== sizeof(event
->name
)) {
714 /* Event name is not NULL-terminated. */
719 /* Add null termination. */
722 if (exclusion_count
> UINT32_MAX
) {
723 /* Possible overflow. */
728 if (bytecode_len
> UINT32_MAX
) {
729 /* Possible overflow. */
734 event_comm
.name_len
= (uint32_t) name_len
;
735 event_comm
.event_type
= (int8_t) event
->type
;
736 event_comm
.loglevel_type
= (int8_t) event
->loglevel_type
;
737 event_comm
.loglevel
= (int32_t) event
->loglevel
;
738 event_comm
.enabled
= (int8_t) event
->enabled
;
739 event_comm
.pid
= (int32_t) event
->pid
;
740 event_comm
.exclusion_count
= (uint32_t) exclusion_count
;
741 event_comm
.bytecode_len
= (uint32_t) bytecode_len
;
742 event_comm
.flags
= (int32_t) event
->flags
;
744 if (filter_expression
) {
745 event_comm
.filter_expression_len
=
746 strlen(filter_expression
) + 1;
750 ret
= lttng_dynamic_buffer_append(
751 &payload
->buffer
, &event_comm
, sizeof(event_comm
));
757 ret
= lttng_dynamic_buffer_append(
758 &payload
->buffer
, event
->name
, name_len
);
764 for (i
= 0; i
< exclusion_count
; i
++) {
765 const size_t exclusion_len
= lttng_strnlen(
766 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
767 const struct lttng_event_exclusion_comm exclusion_header
= {
768 .len
= (uint32_t) exclusion_len
+ 1,
771 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
772 /* Exclusion is not NULL-terminated. */
777 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
778 &exclusion_header
, sizeof(exclusion_header
));
783 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
784 *(exclusion_list
+ i
), exclusion_len
+ 1);
790 /* Filter expression and its bytecode */
791 if (filter_expression
) {
792 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
794 event_comm
.filter_expression_len
);
800 * Bytecode can be absent when we serialize to the client
804 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
805 bytecode
, bytecode_len
);
812 size_before_payload
= payload
->buffer
.size
;
814 /* Event type specific payload */
815 switch (event
->type
) {
816 case LTTNG_EVENT_FUNCTION
:
818 case LTTNG_EVENT_PROBE
:
819 ret
= lttng_event_probe_attr_serialize(&event
->attr
.probe
, payload
);
825 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
827 header
->lttng_event_probe_attr_len
=
828 payload
->buffer
.size
- size_before_payload
;
831 case LTTNG_EVENT_FUNCTION_ENTRY
:
832 ret
= lttng_event_function_attr_serialize(
833 &event
->attr
.ftrace
, payload
);
839 /* Update the lttng_event_function_attr len. */
840 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
842 header
->lttng_event_function_attr_len
=
843 payload
->buffer
.size
- size_before_payload
;
846 case LTTNG_EVENT_USERSPACE_PROBE
:
848 const struct lttng_event_extended
*ev_ext
=
849 (const struct lttng_event_extended
*)
852 assert(event
->extended
.ptr
);
853 assert(ev_ext
->probe_location
);
855 size_before_payload
= payload
->buffer
.size
;
856 if (ev_ext
->probe_location
) {
858 * lttng_userspace_probe_location_serialize returns the
859 * number of bytes that were appended to the buffer.
861 ret
= lttng_userspace_probe_location_serialize(
862 ev_ext
->probe_location
, payload
);
869 /* Update the userspace probe location len. */
870 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
872 header
->userspace_probe_location_len
=
873 payload
->buffer
.size
- size_before_payload
;
877 case LTTNG_EVENT_TRACEPOINT
:
879 case LTTNG_EVENT_ALL
:
882 /* Nothing to do here. */
890 static ssize_t
lttng_event_context_app_populate_from_payload(
891 const struct lttng_payload_view
*view
,
892 struct lttng_event_context
*event_ctx
)
894 ssize_t ret
, offset
= 0;
895 const struct lttng_event_context_app_comm
*comm
;
896 char *provider_name
= NULL
, *context_name
= NULL
;
897 size_t provider_name_len
, context_name_len
;
898 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
899 &view
->buffer
, offset
, sizeof(*comm
));
901 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
903 if (!lttng_buffer_view_is_valid(&comm_view
)) {
908 comm
= (typeof(comm
)) comm_view
.data
;
909 offset
+= sizeof(*comm
);
911 provider_name_len
= comm
->provider_name_len
;
912 context_name_len
= comm
->ctx_name_len
;
914 if (provider_name_len
== 0 || context_name_len
== 0) {
916 * Application provider and context names MUST
925 const struct lttng_buffer_view provider_name_view
=
926 lttng_buffer_view_from_view(&view
->buffer
,
930 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
935 name
= provider_name_view
.data
;
937 if (!lttng_buffer_view_contains_string(&provider_name_view
,
938 name
, provider_name_len
)) {
943 provider_name
= lttng_strndup(name
, provider_name_len
);
944 if (!provider_name
) {
949 offset
+= provider_name_len
;
954 const struct lttng_buffer_view context_name_view
=
955 lttng_buffer_view_from_view(
956 &view
->buffer
, offset
,
959 if (!lttng_buffer_view_is_valid(&context_name_view
)) {
964 name
= context_name_view
.data
;
966 if (!lttng_buffer_view_contains_string(&context_name_view
, name
,
972 context_name
= lttng_strndup(name
, context_name_len
);
978 offset
+= context_name_len
;
981 /* Transfer ownership of the strings */
982 event_ctx
->u
.app_ctx
.provider_name
= provider_name
;
983 event_ctx
->u
.app_ctx
.ctx_name
= context_name
;
984 provider_name
= NULL
;
995 static ssize_t
lttng_event_context_perf_counter_populate_from_payload(
996 const struct lttng_payload_view
*view
,
997 struct lttng_event_context
*event_ctx
)
1000 ssize_t consumed
, offset
= 0;
1001 const struct lttng_event_context_perf_counter_comm
*comm
;
1003 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1004 &view
->buffer
, offset
, sizeof(*comm
));
1006 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_COUNTER
||
1008 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
||
1009 event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
);
1011 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1016 comm
= (typeof(comm
)) comm_view
.data
;
1017 offset
+= sizeof(*comm
);
1019 name_len
= comm
->name_len
;
1023 const struct lttng_buffer_view provider_name_view
=
1024 lttng_buffer_view_from_view(
1025 &view
->buffer
, offset
,
1028 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
1033 name
= provider_name_view
.data
;
1035 if (!lttng_buffer_view_contains_string(
1036 &provider_name_view
, name
, name_len
)) {
1041 ret
= lttng_strncpy(event_ctx
->u
.perf_counter
.name
, name
,
1042 sizeof(event_ctx
->u
.perf_counter
.name
));
1050 event_ctx
->u
.perf_counter
.config
= comm
->config
;
1051 event_ctx
->u
.perf_counter
.type
= comm
->type
;
1059 ssize_t
lttng_event_context_create_from_payload(
1060 struct lttng_payload_view
*view
,
1061 struct lttng_event_context
**event_ctx
)
1063 ssize_t ret
, offset
= 0;
1064 const struct lttng_event_context_comm
*comm
;
1065 struct lttng_event_context
*local_context
= NULL
;
1066 struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1067 &view
->buffer
, offset
, sizeof(*comm
));
1072 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1077 comm
= (typeof(comm
)) comm_view
.data
;
1078 offset
+= sizeof(*comm
);
1080 local_context
= zmalloc
<lttng_event_context
>();
1081 if (!local_context
) {
1086 local_context
->ctx
= (lttng_event_context_type
) comm
->type
;
1089 struct lttng_payload_view subtype_view
=
1090 lttng_payload_view_from_view(view
, offset
, -1);
1092 switch (local_context
->ctx
) {
1093 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1094 ret
= lttng_event_context_app_populate_from_payload(
1095 &subtype_view
, local_context
);
1097 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1098 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1099 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1100 ret
= lttng_event_context_perf_counter_populate_from_payload(
1101 &subtype_view
, local_context
);
1104 /* Nothing else to deserialize. */
1116 *event_ctx
= local_context
;
1117 local_context
= NULL
;
1121 free(local_context
);
1125 static int lttng_event_context_app_serialize(
1126 struct lttng_event_context
*context
,
1127 struct lttng_payload
*payload
)
1130 struct lttng_event_context_app_comm comm
= {};
1131 size_t provider_len
, ctx_len
;
1132 const char *provider_name
;
1133 const char *ctx_name
;
1137 assert(context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1139 provider_name
= context
->u
.app_ctx
.provider_name
;
1140 ctx_name
= context
->u
.app_ctx
.ctx_name
;
1142 if (!provider_name
|| !ctx_name
) {
1143 ret
= -LTTNG_ERR_INVALID
;
1147 provider_len
= strlen(provider_name
);
1148 if (provider_len
== 0) {
1149 ret
= -LTTNG_ERR_INVALID
;
1153 /* Include the null terminator. */
1155 comm
.provider_name_len
= provider_len
;
1157 ctx_len
= strlen(ctx_name
);
1159 ret
= -LTTNG_ERR_INVALID
;
1163 /* Include the null terminator. */
1165 comm
.ctx_name_len
= ctx_len
;
1168 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1175 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, provider_name
,
1182 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, ctx_name
,
1193 static int lttng_event_context_perf_counter_serialize(
1194 struct lttng_event_perf_counter_ctx
*context
,
1195 struct lttng_payload
*payload
)
1198 struct lttng_event_context_perf_counter_comm comm
= {};
1203 comm
.config
= context
->config
;
1204 comm
.type
= context
->type
;
1205 comm
.name_len
= lttng_strnlen(context
->name
, sizeof(context
->name
));
1207 if (comm
.name_len
== sizeof(context
->name
)) {
1212 /* Include the null terminator. */
1216 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1223 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, context
->name
,
1234 int lttng_event_context_serialize(struct lttng_event_context
*context
,
1235 struct lttng_payload
*payload
)
1238 struct lttng_event_context_comm context_comm
= { 0 };
1243 context_comm
.type
= (uint32_t) context
->ctx
;
1246 ret
= lttng_dynamic_buffer_append(
1247 &payload
->buffer
, &context_comm
, sizeof(context_comm
));
1252 switch (context
->ctx
) {
1253 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1254 ret
= lttng_event_context_app_serialize(context
, payload
);
1256 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1257 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1258 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1259 ret
= lttng_event_context_perf_counter_serialize(
1260 &context
->u
.perf_counter
, payload
);
1263 /* Nothing else to serialize. */
1275 void lttng_event_context_destroy(struct lttng_event_context
*context
)
1281 if (context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1282 free(context
->u
.app_ctx
.provider_name
);
1283 free(context
->u
.app_ctx
.ctx_name
);
1290 * This is a specialized populate for lttng_event_field since it ignores
1291 * the extension field of the lttng_event struct and simply copies what it can
1292 * to the internal struct lttng_event of a lttng_event_field.
1294 static void lttng_event_field_populate_lttng_event_from_event(
1295 const struct lttng_event
*src
, struct lttng_event
*destination
)
1297 memcpy(destination
, src
, sizeof(*destination
));
1299 /* Remove all possible dynamic data from the destination event rule. */
1300 destination
->extended
.ptr
= NULL
;
1303 ssize_t
lttng_event_field_create_from_payload(
1304 struct lttng_payload_view
*view
,
1305 struct lttng_event_field
**field
)
1307 ssize_t ret
, offset
= 0;
1308 struct lttng_event_field
*local_event_field
= NULL
;
1309 struct lttng_event
*event
= NULL
;
1310 const struct lttng_event_field_comm
*comm
;
1311 const char* name
= NULL
;
1317 const struct lttng_buffer_view comm_view
=
1318 lttng_buffer_view_from_view(
1319 &view
->buffer
, offset
,
1322 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1327 /* lttng_event_field_comm header */
1328 comm
= (const lttng_event_field_comm
*) comm_view
.data
;
1329 offset
+= sizeof(*comm
);
1332 local_event_field
= zmalloc
<lttng_event_field
>();
1333 if (!local_event_field
) {
1338 local_event_field
->type
= (lttng_event_field_type
) comm
->type
;
1339 local_event_field
->nowrite
= comm
->nowrite
;
1343 const struct lttng_buffer_view name_view
=
1344 lttng_buffer_view_from_view(
1345 &view
->buffer
, offset
,
1348 if (!lttng_buffer_view_is_valid(&name_view
)) {
1353 name
= name_view
.data
;
1355 if (!lttng_buffer_view_contains_string(&name_view
,
1356 name_view
.data
, comm
->name_len
)) {
1361 if (comm
->name_len
> LTTNG_SYMBOL_NAME_LEN
- 1) {
1362 /* Name is too long.*/
1367 offset
+= comm
->name_len
;
1372 struct lttng_payload_view event_view
=
1373 lttng_payload_view_from_view(
1377 if (!lttng_payload_view_is_valid(&event_view
)) {
1382 ret
= lttng_event_create_from_payload(&event_view
, &event
, NULL
,
1384 if (ret
!= comm
->event_len
) {
1395 if (lttng_strncpy(local_event_field
->field_name
, name
,
1396 sizeof(local_event_field
->field_name
))) {
1401 lttng_event_field_populate_lttng_event_from_event(
1402 event
, &local_event_field
->event
);
1404 *field
= local_event_field
;
1405 local_event_field
= NULL
;
1408 lttng_event_destroy(event
);
1409 free(local_event_field
);
1413 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1414 struct lttng_payload
*payload
)
1417 size_t header_offset
, size_before_event
;
1419 struct lttng_event_field_comm event_field_comm
= {};
1420 struct lttng_event_field_comm
*header
;
1425 /* Save the header location for later in-place header update. */
1426 header_offset
= payload
->buffer
.size
;
1428 name_len
= strnlen(field
->field_name
, sizeof(field
->field_name
));
1429 if (name_len
== sizeof(field
->field_name
)) {
1430 /* Event name is not NULL-terminated. */
1435 /* Add null termination. */
1438 event_field_comm
.type
= field
->type
;
1439 event_field_comm
.nowrite
= (uint8_t)field
->nowrite
;
1440 event_field_comm
.name_len
= name_len
;
1443 ret
= lttng_dynamic_buffer_append(
1444 &payload
->buffer
, &event_field_comm
,
1445 sizeof(event_field_comm
));
1451 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, field
->field_name
,
1457 size_before_event
= payload
->buffer
.size
;
1458 ret
= lttng_event_serialize(
1459 &field
->event
, 0, NULL
, NULL
, 0, 0, payload
);
1465 /* Update the event len. */
1466 header
= (struct lttng_event_field_comm
*)
1467 ((char *) payload
->buffer
.data
+
1469 header
->event_len
= payload
->buffer
.size
- size_before_event
;
1475 static enum lttng_error_code
compute_flattened_size(
1476 struct lttng_dynamic_pointer_array
*events
, size_t *size
)
1478 enum lttng_error_code ret_code
;
1480 size_t storage_req
, event_count
, i
;
1485 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1487 /* The basic struct lttng_event */
1488 storage_req
= event_count
* sizeof(struct lttng_event
);
1490 /* The struct·lttng_event_extended */
1491 storage_req
+= event_count
* sizeof(struct lttng_event_extended
);
1493 for (i
= 0; i
< event_count
; i
++) {
1494 int probe_storage_req
= 0;
1495 const struct event_list_element
*element
= (const struct event_list_element
*)
1496 lttng_dynamic_pointer_array_get_pointer(
1498 const struct lttng_userspace_probe_location
*location
= NULL
;
1500 location
= lttng_event_get_userspace_probe_location(
1503 ret
= lttng_userspace_probe_location_flatten(
1506 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1510 probe_storage_req
= ret
;
1513 if (element
->filter_expression
) {
1514 storage_req
+= strlen(element
->filter_expression
) + 1;
1517 if (element
->exclusions
) {
1518 storage_req
+= element
->exclusions
->count
*
1519 LTTNG_SYMBOL_NAME_LEN
;
1522 /* Padding to ensure the flat probe is aligned. */
1523 storage_req
= lttng_align_ceil(storage_req
, sizeof(uint64_t));
1524 storage_req
+= probe_storage_req
;
1527 *size
= storage_req
;
1528 ret_code
= LTTNG_OK
;
1535 * Flatten a list of struct lttng_event.
1537 * The buffer that is returned to the API client must contain a "flat" version
1538 * of the events that are returned. In other words, all pointers within an
1539 * lttng_event must point to a location within the returned buffer so that the
1540 * user may free everything by simply calling free() on the returned buffer.
1541 * This is needed in order to maintain API compatibility.
1543 * A first pass is performed to compute the size of the buffer that must be
1544 * allocated. A second pass is then performed to setup the returned events so
1545 * that their members always point within the buffer.
1547 * The layout of the returned buffer is as follows:
1548 * - struct lttng_event[nb_events],
1549 * - nb_events times the following:
1550 * - struct lttng_event_extended,
1551 * - filter_expression
1553 * - padding to align to 64-bits
1554 * - flattened version of userspace_probe_location
1556 static enum lttng_error_code
flatten_lttng_events(
1557 struct lttng_dynamic_pointer_array
*events
,
1558 struct lttng_event
**flattened_events
)
1560 enum lttng_error_code ret_code
;
1563 struct lttng_dynamic_buffer local_flattened_events
;
1567 assert(flattened_events
);
1569 lttng_dynamic_buffer_init(&local_flattened_events
);
1570 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1572 ret_code
= compute_flattened_size(events
, &storage_req
);
1573 if (ret_code
!= LTTNG_OK
) {
1578 * We must ensure that "local_flattened_events" is never resized so as
1579 * to preserve the validity of the flattened objects.
1581 ret
= lttng_dynamic_buffer_set_capacity(
1582 &local_flattened_events
, storage_req
);
1584 ret_code
= LTTNG_ERR_NOMEM
;
1588 /* Start by laying the struct lttng_event */
1589 for (i
= 0; i
< nb_events
; i
++) {
1590 const struct event_list_element
*element
= (const struct event_list_element
*)
1591 lttng_dynamic_pointer_array_get_pointer(
1595 ret_code
= LTTNG_ERR_FATAL
;
1599 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1600 element
->event
, sizeof(struct lttng_event
));
1602 ret_code
= LTTNG_ERR_NOMEM
;
1607 for (i
= 0; i
< nb_events
; i
++) {
1608 const struct event_list_element
*element
= (const struct event_list_element
*)
1609 lttng_dynamic_pointer_array_get_pointer(events
, i
);
1610 struct lttng_event
*event
= (struct lttng_event
*)
1611 (local_flattened_events
.data
+ (sizeof(struct lttng_event
) * i
));
1612 struct lttng_event_extended
*event_extended
=
1613 (struct lttng_event_extended
*)
1614 (local_flattened_events
.data
+ local_flattened_events
.size
);
1615 const struct lttng_userspace_probe_location
*location
= NULL
;
1619 /* Insert struct lttng_event_extended. */
1620 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1621 local_flattened_events
.size
+
1622 sizeof(*event_extended
));
1624 ret_code
= LTTNG_ERR_NOMEM
;
1627 event
->extended
.ptr
= event_extended
;
1629 /* Insert filter expression. */
1630 if (element
->filter_expression
) {
1631 const size_t len
= strlen(element
->filter_expression
) + 1;
1633 event_extended
->filter_expression
=
1634 local_flattened_events
.data
+
1635 local_flattened_events
.size
;
1636 ret
= lttng_dynamic_buffer_append(
1637 &local_flattened_events
,
1638 element
->filter_expression
, len
);
1640 ret_code
= LTTNG_ERR_NOMEM
;
1645 /* Insert exclusions. */
1646 if (element
->exclusions
) {
1647 event_extended
->exclusions
.count
=
1648 element
->exclusions
->count
;
1649 event_extended
->exclusions
.strings
=
1650 local_flattened_events
.data
+
1651 local_flattened_events
.size
;
1653 ret
= lttng_dynamic_buffer_append(
1654 &local_flattened_events
,
1655 element
->exclusions
->names
,
1656 element
->exclusions
->count
*
1657 LTTNG_SYMBOL_NAME_LEN
);
1659 ret_code
= LTTNG_ERR_NOMEM
;
1664 /* Insert padding to align to 64-bits. */
1665 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1666 lttng_align_ceil(local_flattened_events
.size
,
1669 ret_code
= LTTNG_ERR_NOMEM
;
1673 location
= lttng_event_get_userspace_probe_location(
1676 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
1677 (local_flattened_events
.data
+ local_flattened_events
.size
);
1678 ret
= lttng_userspace_probe_location_flatten(
1679 location
, &local_flattened_events
);
1681 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1687 /* Don't reset local_flattened_events buffer as we return its content. */
1688 *flattened_events
= (struct lttng_event
*) local_flattened_events
.data
;
1689 lttng_dynamic_buffer_init(&local_flattened_events
);
1690 ret_code
= LTTNG_OK
;
1692 lttng_dynamic_buffer_reset(&local_flattened_events
);
1696 static enum lttng_error_code
event_list_create_from_payload(
1697 struct lttng_payload_view
*view
,
1699 struct lttng_dynamic_pointer_array
*event_list
)
1701 enum lttng_error_code ret_code
;
1709 for (i
= 0; i
< count
; i
++) {
1711 struct lttng_payload_view event_view
=
1712 lttng_payload_view_from_view(view
, offset
, -1);
1713 struct event_list_element
*element
= zmalloc
<event_list_element
>();
1716 ret_code
= LTTNG_ERR_NOMEM
;
1721 * Lifetime and management of the object is now bound to the
1724 ret
= lttng_dynamic_pointer_array_add_pointer(
1725 event_list
, element
);
1727 event_list_destructor(element
);
1728 ret_code
= LTTNG_ERR_NOMEM
;
1733 * Bytecode is not transmitted on listing in any case we do not
1736 event_size
= lttng_event_create_from_payload(&event_view
,
1738 &element
->exclusions
,
1739 &element
->filter_expression
, NULL
);
1740 if (event_size
< 0) {
1741 ret_code
= LTTNG_ERR_INVALID
;
1745 offset
+= event_size
;
1748 if (view
->buffer
.size
!= offset
) {
1749 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
1753 ret_code
= LTTNG_OK
;
1759 enum lttng_error_code
lttng_events_create_and_flatten_from_payload(
1760 struct lttng_payload_view
*payload
,
1762 struct lttng_event
**events
)
1764 enum lttng_error_code ret
= LTTNG_OK
;
1765 struct lttng_dynamic_pointer_array local_events
;
1767 lttng_dynamic_pointer_array_init(&local_events
, event_list_destructor
);
1769 /* Deserialize the events. */
1771 struct lttng_payload_view events_view
=
1772 lttng_payload_view_from_view(payload
, 0, -1);
1774 ret
= event_list_create_from_payload(
1775 &events_view
, count
, &local_events
);
1776 if (ret
!= LTTNG_OK
) {
1781 ret
= flatten_lttng_events(&local_events
, events
);
1782 if (ret
!= LTTNG_OK
) {
1787 lttng_dynamic_pointer_array_reset(&local_events
);
1791 static enum lttng_error_code
flatten_lttng_event_fields(
1792 struct lttng_dynamic_pointer_array
*event_fields
,
1793 struct lttng_event_field
**flattened_event_fields
)
1796 enum lttng_error_code ret_code
;
1797 size_t storage_req
= 0;
1798 struct lttng_dynamic_buffer local_flattened_event_fields
;
1801 assert(event_fields
);
1802 assert(flattened_event_fields
);
1804 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1805 nb_event_field
= lttng_dynamic_pointer_array_get_count(event_fields
);
1808 * Here even if the event field contains a `struct lttng_event` that
1809 * could contain dynamic data, in reality it is not the case.
1810 * Dynamic data is not present. Here the flattening is mostly a direct
1811 * memcpy. This is less than ideal but this code is still better than
1812 * direct usage of an unpacked lttng_event_field array.
1814 storage_req
+= sizeof(struct lttng_event_field
) * nb_event_field
;
1816 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1819 * We must ensure that "local_flattened_event_fields" is never resized
1820 * so as to preserve the validity of the flattened objects.
1822 ret
= lttng_dynamic_buffer_set_capacity(
1823 &local_flattened_event_fields
, storage_req
);
1825 ret_code
= LTTNG_ERR_NOMEM
;
1829 for (i
= 0; i
< nb_event_field
; i
++) {
1830 const struct lttng_event_field
*element
=
1831 (const struct lttng_event_field
*)
1832 lttng_dynamic_pointer_array_get_pointer(
1836 ret_code
= LTTNG_ERR_FATAL
;
1839 ret
= lttng_dynamic_buffer_append(&local_flattened_event_fields
,
1840 element
, sizeof(struct lttng_event_field
));
1842 ret_code
= LTTNG_ERR_NOMEM
;
1847 /* Don't reset local_flattened_channels buffer as we return its content. */
1848 *flattened_event_fields
= (struct lttng_event_field
*) local_flattened_event_fields
.data
;
1849 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1850 ret_code
= LTTNG_OK
;
1852 lttng_dynamic_buffer_reset(&local_flattened_event_fields
);
1856 static enum lttng_error_code
event_field_list_create_from_payload(
1857 struct lttng_payload_view
*view
,
1859 struct lttng_dynamic_pointer_array
**event_field_list
)
1861 enum lttng_error_code ret_code
;
1862 int ret
, offset
= 0;
1864 struct lttng_dynamic_pointer_array
*list
= NULL
;
1867 assert(event_field_list
);
1869 list
= zmalloc
<lttng_dynamic_pointer_array
>();
1871 ret_code
= LTTNG_ERR_NOMEM
;
1875 lttng_dynamic_pointer_array_init(list
, free
);
1877 for (i
= 0; i
< count
; i
++) {
1878 ssize_t event_field_size
;
1879 struct lttng_event_field
*field
= NULL
;
1880 struct lttng_payload_view event_field_view
=
1881 lttng_payload_view_from_view(view
, offset
, -1);
1883 event_field_size
= lttng_event_field_create_from_payload(
1884 &event_field_view
, &field
);
1885 if (event_field_size
< 0) {
1886 ret_code
= LTTNG_ERR_INVALID
;
1890 /* Lifetime and management of the object is now bound to the array. */
1891 ret
= lttng_dynamic_pointer_array_add_pointer(list
, field
);
1894 ret_code
= LTTNG_ERR_NOMEM
;
1898 offset
+= event_field_size
;
1901 if (view
->buffer
.size
!= offset
) {
1902 ret_code
= LTTNG_ERR_INVALID
;
1906 *event_field_list
= list
;
1908 ret_code
= LTTNG_OK
;
1912 lttng_dynamic_pointer_array_reset(list
);
1919 enum lttng_error_code
lttng_event_fields_create_and_flatten_from_payload(
1920 struct lttng_payload_view
*view
,
1922 struct lttng_event_field
**fields
)
1924 enum lttng_error_code ret_code
;
1925 struct lttng_dynamic_pointer_array
*local_event_fields
= NULL
;
1927 ret_code
= event_field_list_create_from_payload(
1928 view
, count
, &local_event_fields
);
1929 if (ret_code
!= LTTNG_OK
) {
1933 ret_code
= flatten_lttng_event_fields(local_event_fields
, fields
);
1934 if (ret_code
!= LTTNG_OK
) {
1938 if (local_event_fields
) {
1939 lttng_dynamic_pointer_array_reset(local_event_fields
);
1940 free(local_event_fields
);