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
);
42 struct lttng_event
*lttng_event_copy(const struct lttng_event
*event
)
44 struct lttng_event
*new_event
;
45 struct lttng_event_extended
*new_event_extended
;
47 new_event
= zmalloc(sizeof(*event
));
49 PERROR("Error allocating event structure");
53 /* Copy the content of the old event. */
54 memcpy(new_event
, event
, sizeof(*event
));
57 * We need to create a new extended since the previous pointer is now
60 new_event_extended
= zmalloc(sizeof(*new_event_extended
));
61 if (!new_event_extended
) {
62 PERROR("Error allocating event extended structure");
66 new_event
->extended
.ptr
= new_event_extended
;
75 static int lttng_event_probe_attr_serialize(
76 const struct lttng_event_probe_attr
*probe
,
77 struct lttng_payload
*payload
)
80 size_t symbol_name_len
;
81 struct lttng_event_probe_attr_comm comm
= { 0 };
83 symbol_name_len
= lttng_strnlen(probe
->symbol_name
, LTTNG_SYMBOL_NAME_LEN
);
84 if (symbol_name_len
== LTTNG_SYMBOL_NAME_LEN
) {
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(function
->symbol_name
, LTTNG_SYMBOL_NAME_LEN
);
119 if (symbol_name_len
== LTTNG_SYMBOL_NAME_LEN
) {
120 /* Not null-termintated. */
125 /* Include the null terminator. */
126 symbol_name_len
+= 1;
128 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
130 ret
= lttng_dynamic_buffer_append(
131 &payload
->buffer
, &comm
, sizeof(comm
));
137 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
138 function
->symbol_name
, symbol_name_len
);
143 static ssize_t
lttng_event_probe_attr_create_from_payload(
144 struct lttng_payload_view
*view
,
145 struct lttng_event_probe_attr
**probe_attr
)
147 ssize_t ret
, offset
= 0;
148 const struct lttng_event_probe_attr_comm
*comm
;
149 struct lttng_event_probe_attr
*local_attr
= NULL
;
150 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
151 view
, offset
, sizeof(*comm
));
153 if (!lttng_payload_view_is_valid(&comm_view
)) {
158 comm
= (typeof(comm
)) comm_view
.buffer
.data
;
159 offset
+= sizeof(*comm
);
161 local_attr
= (struct lttng_event_probe_attr
*) zmalloc(
162 sizeof(*local_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 LTTNG_SYMBOL_NAME_LEN
);
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
= (struct lttng_event_function_attr
*) zmalloc(
227 sizeof(*local_attr
));
228 if (local_attr
== NULL
) {
235 struct lttng_payload_view name_view
=
236 lttng_payload_view_from_view(view
, offset
,
237 comm
->symbol_name_len
);
239 if (!lttng_payload_view_is_valid(&name_view
)) {
244 name
= name_view
.buffer
.data
;
246 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
247 comm
->symbol_name_len
)) {
252 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
253 LTTNG_SYMBOL_NAME_LEN
);
259 offset
+= comm
->symbol_name_len
;
262 *function_attr
= local_attr
;
270 static ssize_t
lttng_event_exclusions_create_from_payload(
271 struct lttng_payload_view
*view
,
273 struct lttng_event_exclusion
**exclusions
)
275 ssize_t ret
, offset
= 0;
276 size_t size
= (count
* LTTNG_SYMBOL_NAME_LEN
);
278 const struct lttng_event_exclusion_comm
*comm
;
279 struct lttng_event_exclusion
*local_exclusions
;
281 local_exclusions
= (struct lttng_event_exclusion
*) zmalloc(
282 sizeof(struct lttng_event_exclusion
) + size
);
283 if (!local_exclusions
) {
288 local_exclusions
->count
= count
;
290 for (i
= 0; i
< count
; i
++) {
292 struct lttng_buffer_view string_view
;
293 const struct lttng_buffer_view comm_view
=
294 lttng_buffer_view_from_view(&view
->buffer
,
295 offset
, sizeof(*comm
));
297 if (!lttng_buffer_view_is_valid(&comm_view
)) {
302 comm
= (typeof(comm
)) comm_view
.data
;
303 offset
+= sizeof(*comm
);
305 string_view
= lttng_buffer_view_from_view(
306 &view
->buffer
, offset
, comm
->len
);
308 if (!lttng_buffer_view_is_valid(&string_view
)) {
313 string
= string_view
.data
;
315 if (!lttng_buffer_view_contains_string(
316 &string_view
, string
, comm
->len
)) {
321 ret
= lttng_strncpy(local_exclusions
->names
[i
],
322 string
, LTTNG_SYMBOL_NAME_LEN
);
331 *exclusions
= local_exclusions
;
332 local_exclusions
= NULL
;
335 free(local_exclusions
);
339 ssize_t
lttng_event_create_from_payload(struct lttng_payload_view
*view
,
340 struct lttng_event
**out_event
,
341 struct lttng_event_exclusion
**out_exclusion
,
342 char **out_filter_expression
,
343 struct lttng_bytecode
**out_bytecode
)
345 ssize_t ret
, offset
= 0;
346 struct lttng_event
*local_event
= NULL
;
347 struct lttng_event_exclusion
*local_exclusions
= NULL
;
348 struct lttng_bytecode
*local_bytecode
= NULL
;
349 char *local_filter_expression
= NULL
;
350 const struct lttng_event_comm
*event_comm
;
351 struct lttng_event_function_attr
*local_function_attr
= NULL
;
352 struct lttng_event_probe_attr
*local_probe_attr
= NULL
;
353 struct lttng_userspace_probe_location
*local_userspace_probe_location
=
357 * Only event is obligatory, the other output argument are optional and
358 * depends on what the caller is interested in.
364 struct lttng_payload_view comm_view
=
365 lttng_payload_view_from_view(view
, offset
,
366 sizeof(*event_comm
));
368 if (!lttng_payload_view_is_valid(&comm_view
)) {
373 /* lttng_event_comm header */
374 event_comm
= (typeof(event_comm
)) comm_view
.buffer
.data
;
375 offset
+= sizeof(*event_comm
);
378 local_event
= lttng_event_create();
379 if (local_event
== NULL
) {
384 local_event
->type
= (enum lttng_event_type
) event_comm
->event_type
;
385 local_event
->loglevel_type
= (enum lttng_loglevel_type
) event_comm
->loglevel_type
;
386 local_event
->loglevel
= event_comm
->loglevel
;
387 local_event
->enabled
= event_comm
->enabled
;
388 local_event
->pid
= event_comm
->pid
;
389 local_event
->flags
= (enum lttng_event_flag
) event_comm
->flags
;
393 const struct lttng_buffer_view name_view
=
394 lttng_buffer_view_from_view(&view
->buffer
,
395 offset
, event_comm
->name_len
);
397 if (!lttng_buffer_view_is_valid(&name_view
)) {
402 name
= (const char *) name_view
.data
;
404 if (!lttng_buffer_view_contains_string(
405 &name_view
, name
, event_comm
->name_len
)) {
411 local_event
->name
, name
, LTTNG_SYMBOL_NAME_LEN
);
417 offset
+= event_comm
->name_len
;
421 if (event_comm
->exclusion_count
== 0) {
422 goto deserialize_filter_expression
;
426 struct lttng_payload_view exclusions_view
=
427 lttng_payload_view_from_view(
430 if (!lttng_payload_view_is_valid(&exclusions_view
)) {
435 ret
= lttng_event_exclusions_create_from_payload(&exclusions_view
,
436 event_comm
->exclusion_count
, &local_exclusions
);
443 local_event
->exclusion
= 1;
446 deserialize_filter_expression
:
448 if (event_comm
->filter_expression_len
== 0) {
449 if (event_comm
->bytecode_len
!= 0) {
451 * This is an invalid event payload.
453 * Filter expression without bytecode is possible but
454 * not the other way around.
459 goto deserialize_event_type_payload
;
463 const char *filter_expression_buffer
;
464 struct lttng_buffer_view filter_expression_view
=
465 lttng_buffer_view_from_view(&view
->buffer
, offset
,
466 event_comm
->filter_expression_len
);
468 if (!lttng_buffer_view_is_valid(&filter_expression_view
)) {
473 filter_expression_buffer
= filter_expression_view
.data
;
475 if (!lttng_buffer_view_contains_string(&filter_expression_view
,
476 filter_expression_buffer
,
477 event_comm
->filter_expression_len
)) {
482 local_filter_expression
= lttng_strndup(
483 filter_expression_buffer
,
484 event_comm
->filter_expression_len
);
485 if (!local_filter_expression
) {
490 local_event
->filter
= 1;
492 offset
+= event_comm
->filter_expression_len
;
495 if (event_comm
->bytecode_len
== 0) {
497 * Filter expression can be present but without bytecode
498 * when dealing with event listing.
500 goto deserialize_event_type_payload
;
505 struct lttng_payload_view bytecode_view
=
506 lttng_payload_view_from_view(view
, offset
,
507 event_comm
->bytecode_len
);
509 if (!lttng_payload_view_is_valid(&bytecode_view
)) {
514 local_bytecode
= (struct lttng_bytecode
*) zmalloc(
515 event_comm
->bytecode_len
);
516 if (!local_bytecode
) {
521 memcpy(local_bytecode
, bytecode_view
.buffer
.data
,
522 event_comm
->bytecode_len
);
523 if ((local_bytecode
->len
+ sizeof(*local_bytecode
)) !=
524 event_comm
->bytecode_len
) {
529 offset
+= event_comm
->bytecode_len
;
532 deserialize_event_type_payload
:
533 /* Event type specific payload */
534 switch (local_event
->type
) {
535 case LTTNG_EVENT_FUNCTION
:
537 case LTTNG_EVENT_PROBE
:
539 struct lttng_payload_view probe_attr_view
=
540 lttng_payload_view_from_view(view
, offset
,
541 event_comm
->lttng_event_probe_attr_len
);
543 if (event_comm
->lttng_event_probe_attr_len
== 0) {
548 if (!lttng_payload_view_is_valid(&probe_attr_view
)) {
553 ret
= lttng_event_probe_attr_create_from_payload(
554 &probe_attr_view
, &local_probe_attr
);
555 if (ret
< 0 || ret
!= event_comm
->lttng_event_probe_attr_len
) {
560 /* Copy to the local event. */
561 memcpy(&local_event
->attr
.probe
, local_probe_attr
,
562 sizeof(local_event
->attr
.probe
));
567 case LTTNG_EVENT_FUNCTION_ENTRY
:
569 struct lttng_payload_view function_attr_view
=
570 lttng_payload_view_from_view(view
, offset
,
571 event_comm
->lttng_event_function_attr_len
);
573 if (event_comm
->lttng_event_function_attr_len
== 0) {
578 if (!lttng_payload_view_is_valid(&function_attr_view
)) {
583 ret
= lttng_event_function_attr_create_from_payload(
584 &function_attr_view
, &local_function_attr
);
585 if (ret
< 0 || ret
!= event_comm
->lttng_event_function_attr_len
) {
590 /* Copy to the local event. */
591 memcpy(&local_event
->attr
.ftrace
, local_function_attr
,
592 sizeof(local_event
->attr
.ftrace
));
598 case LTTNG_EVENT_USERSPACE_PROBE
:
600 struct lttng_payload_view userspace_probe_location_view
=
601 lttng_payload_view_from_view(view
, offset
,
602 event_comm
->userspace_probe_location_len
);
604 if (event_comm
->userspace_probe_location_len
== 0) {
609 if (!lttng_payload_view_is_valid(
610 &userspace_probe_location_view
)) {
615 ret
= lttng_userspace_probe_location_create_from_payload(
616 &userspace_probe_location_view
,
617 &local_userspace_probe_location
);
619 WARN("Failed to create a userspace probe location from the received buffer");
624 if (ret
!= event_comm
->userspace_probe_location_len
) {
625 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
);
630 /* Attach the probe location to the event. */
631 ret
= lttng_event_set_userspace_probe_location(
632 local_event
, local_userspace_probe_location
);
634 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
639 * Userspace probe location object ownership transfered to the
642 local_userspace_probe_location
= NULL
;
643 offset
+= event_comm
->userspace_probe_location_len
;
646 case LTTNG_EVENT_TRACEPOINT
:
648 case LTTNG_EVENT_ALL
:
650 case LTTNG_EVENT_SYSCALL
:
652 case LTTNG_EVENT_NOOP
:
653 /* Nothing to do here */
661 /* Transfer ownership to the caller. */
662 *out_event
= local_event
;
666 *out_bytecode
= local_bytecode
;
667 local_bytecode
= NULL
;
671 *out_exclusion
= local_exclusions
;
672 local_exclusions
= NULL
;
675 if (out_filter_expression
) {
676 *out_filter_expression
= local_filter_expression
;
677 local_filter_expression
= NULL
;
682 lttng_event_destroy(local_event
);
683 lttng_userspace_probe_location_destroy(local_userspace_probe_location
);
684 free(local_filter_expression
);
685 free(local_exclusions
);
686 free(local_bytecode
);
687 free(local_function_attr
);
688 free(local_probe_attr
);
692 int lttng_event_serialize(const struct lttng_event
*event
,
693 unsigned int exclusion_count
,
694 char **exclusion_list
,
695 char *filter_expression
,
697 struct lttng_bytecode
*bytecode
,
698 struct lttng_payload
*payload
)
702 size_t header_offset
, size_before_payload
;
704 struct lttng_event_comm event_comm
= { 0 };
705 struct lttng_event_comm
*header
;
709 assert(exclusion_count
== 0 || exclusion_list
);
711 /* Save the header location for later in-place header update. */
712 header_offset
= payload
->buffer
.size
;
714 name_len
= lttng_strnlen(event
->name
, LTTNG_SYMBOL_NAME_LEN
);
715 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
716 /* Event name is not NULL-terminated. */
721 /* Add null termination. */
724 if (exclusion_count
> UINT32_MAX
) {
725 /* Possible overflow. */
730 if (bytecode_len
> UINT32_MAX
) {
731 /* Possible overflow. */
736 event_comm
.name_len
= (uint32_t) name_len
;
737 event_comm
.event_type
= (int8_t) event
->type
;
738 event_comm
.loglevel_type
= (int8_t) event
->loglevel_type
;
739 event_comm
.loglevel
= (int32_t) event
->loglevel
;
740 event_comm
.enabled
= (int8_t) event
->enabled
;
741 event_comm
.pid
= (int32_t) event
->pid
;
742 event_comm
.exclusion_count
= (uint32_t) exclusion_count
;
743 event_comm
.bytecode_len
= (uint32_t) bytecode_len
;
744 event_comm
.flags
= (int32_t) event
->flags
;
746 if (filter_expression
) {
747 event_comm
.filter_expression_len
=
748 strlen(filter_expression
) + 1;
752 ret
= lttng_dynamic_buffer_append(
753 &payload
->buffer
, &event_comm
, sizeof(event_comm
));
759 ret
= lttng_dynamic_buffer_append(
760 &payload
->buffer
, event
->name
, name_len
);
766 for (i
= 0; i
< exclusion_count
; i
++) {
767 const size_t exclusion_len
= lttng_strnlen(
768 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
769 const struct lttng_event_exclusion_comm exclusion_header
= {
770 .len
= (uint32_t) exclusion_len
+ 1,
773 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
774 /* Exclusion is not NULL-terminated. */
779 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
780 &exclusion_header
, sizeof(exclusion_header
));
785 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
786 *(exclusion_list
+ i
), exclusion_len
+ 1);
792 /* Filter expression and its bytecode */
793 if (filter_expression
) {
794 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
796 event_comm
.filter_expression_len
);
802 * Bytecode can be absent when we serialize to the client
806 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
807 bytecode
, bytecode_len
);
814 size_before_payload
= payload
->buffer
.size
;
816 /* Event type specific payload */
817 switch (event
->type
) {
818 case LTTNG_EVENT_FUNCTION
:
820 case LTTNG_EVENT_PROBE
:
821 ret
= lttng_event_probe_attr_serialize(&event
->attr
.probe
, payload
);
827 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
829 header
->lttng_event_probe_attr_len
=
830 payload
->buffer
.size
- size_before_payload
;
833 case LTTNG_EVENT_FUNCTION_ENTRY
:
834 ret
= lttng_event_function_attr_serialize(
835 &event
->attr
.ftrace
, payload
);
841 /* Update the lttng_event_function_attr len. */
842 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
844 header
->lttng_event_function_attr_len
=
845 payload
->buffer
.size
- size_before_payload
;
848 case LTTNG_EVENT_USERSPACE_PROBE
:
850 const struct lttng_event_extended
*ev_ext
=
851 (const struct lttng_event_extended
*)
854 assert(event
->extended
.ptr
);
855 assert(ev_ext
->probe_location
);
857 size_before_payload
= payload
->buffer
.size
;
858 if (ev_ext
->probe_location
) {
860 * lttng_userspace_probe_location_serialize returns the
861 * number of bytes that were appended to the buffer.
863 ret
= lttng_userspace_probe_location_serialize(
864 ev_ext
->probe_location
, payload
);
871 /* Update the userspace probe location len. */
872 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
874 header
->userspace_probe_location_len
=
875 payload
->buffer
.size
- size_before_payload
;
879 case LTTNG_EVENT_TRACEPOINT
:
881 case LTTNG_EVENT_ALL
:
884 /* Nothing to do here. */
892 static ssize_t
lttng_event_context_app_populate_from_payload(
893 const struct lttng_payload_view
*view
,
894 struct lttng_event_context
*event_ctx
)
896 ssize_t ret
, offset
= 0;
897 const struct lttng_event_context_app_comm
*comm
;
898 char *provider_name
= NULL
, *context_name
= NULL
;
899 size_t provider_name_len
, context_name_len
;
900 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
901 &view
->buffer
, offset
, sizeof(*comm
));
903 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
905 if (!lttng_buffer_view_is_valid(&comm_view
)) {
910 comm
= (typeof(comm
)) comm_view
.data
;
911 offset
+= sizeof(*comm
);
913 provider_name_len
= comm
->provider_name_len
;
914 context_name_len
= comm
->ctx_name_len
;
916 if (provider_name_len
== 0 || context_name_len
== 0) {
918 * Application provider and context names MUST
927 const struct lttng_buffer_view provider_name_view
=
928 lttng_buffer_view_from_view(&view
->buffer
,
932 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
937 name
= provider_name_view
.data
;
939 if (!lttng_buffer_view_contains_string(&provider_name_view
,
940 name
, provider_name_len
)) {
945 provider_name
= lttng_strndup(name
, provider_name_len
);
946 if (!provider_name
) {
951 offset
+= provider_name_len
;
956 const struct lttng_buffer_view context_name_view
=
957 lttng_buffer_view_from_view(
958 &view
->buffer
, offset
,
961 if (!lttng_buffer_view_is_valid(&context_name_view
)) {
966 name
= context_name_view
.data
;
968 if (!lttng_buffer_view_contains_string(&context_name_view
, name
,
974 context_name
= lttng_strndup(name
, context_name_len
);
980 offset
+= context_name_len
;
983 /* Transfer ownership of the strings */
984 event_ctx
->u
.app_ctx
.provider_name
= provider_name
;
985 event_ctx
->u
.app_ctx
.ctx_name
= context_name
;
986 provider_name
= NULL
;
997 static ssize_t
lttng_event_context_perf_counter_populate_from_payload(
998 const struct lttng_payload_view
*view
,
999 struct lttng_event_context
*event_ctx
)
1002 ssize_t consumed
, offset
= 0;
1003 const struct lttng_event_context_perf_counter_comm
*comm
;
1005 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1006 &view
->buffer
, offset
, sizeof(*comm
));
1008 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_COUNTER
||
1010 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
||
1011 event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
);
1013 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1018 comm
= (typeof(comm
)) comm_view
.data
;
1019 offset
+= sizeof(*comm
);
1021 name_len
= comm
->name_len
;
1025 const struct lttng_buffer_view provider_name_view
=
1026 lttng_buffer_view_from_view(
1027 &view
->buffer
, offset
,
1030 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
1035 name
= provider_name_view
.data
;
1037 if (!lttng_buffer_view_contains_string(
1038 &provider_name_view
, name
, name_len
)) {
1043 ret
= lttng_strncpy(event_ctx
->u
.perf_counter
.name
, name
, name_len
);
1051 event_ctx
->u
.perf_counter
.config
= comm
->config
;
1052 event_ctx
->u
.perf_counter
.type
= comm
->type
;
1060 ssize_t
lttng_event_context_create_from_payload(
1061 struct lttng_payload_view
*view
,
1062 struct lttng_event_context
**event_ctx
)
1064 ssize_t ret
, offset
= 0;
1065 const struct lttng_event_context_comm
*comm
;
1066 struct lttng_event_context
*local_context
= NULL
;
1067 struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1068 &view
->buffer
, offset
, sizeof(*comm
));
1073 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1078 comm
= (typeof(comm
)) comm_view
.data
;
1079 offset
+= sizeof(*comm
);
1081 local_context
= (struct lttng_event_context
*)
1082 zmalloc(sizeof(*local_context
));
1083 if (!local_context
) {
1088 local_context
->ctx
= (enum lttng_event_context_type
) comm
->type
;
1091 struct lttng_payload_view subtype_view
=
1092 lttng_payload_view_from_view(view
, offset
, -1);
1094 switch (local_context
->ctx
) {
1095 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1096 ret
= lttng_event_context_app_populate_from_payload(
1097 &subtype_view
, local_context
);
1099 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1100 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1101 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1102 ret
= lttng_event_context_perf_counter_populate_from_payload(
1103 &subtype_view
, local_context
);
1106 /* Nothing else to deserialize. */
1118 *event_ctx
= local_context
;
1119 local_context
= NULL
;
1123 free(local_context
);
1127 static int lttng_event_context_app_serialize(
1128 struct lttng_event_context
*context
,
1129 struct lttng_payload
*payload
)
1132 struct lttng_event_context_app_comm comm
= { 0 };
1133 size_t provider_len
, ctx_len
;
1134 const char *provider_name
;
1135 const char *ctx_name
;
1139 assert(context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1141 provider_name
= context
->u
.app_ctx
.provider_name
;
1142 ctx_name
= context
->u
.app_ctx
.ctx_name
;
1144 if (!provider_name
|| !ctx_name
) {
1145 ret
= -LTTNG_ERR_INVALID
;
1149 provider_len
= strlen(provider_name
);
1150 if (provider_len
== 0) {
1151 ret
= -LTTNG_ERR_INVALID
;
1155 /* Include the null terminator. */
1157 comm
.provider_name_len
= provider_len
;
1159 ctx_len
= strlen(ctx_name
);
1161 ret
= -LTTNG_ERR_INVALID
;
1165 /* Include the null terminator. */
1167 comm
.ctx_name_len
= ctx_len
;
1170 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1177 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, provider_name
,
1184 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, ctx_name
,
1195 static int lttng_event_context_perf_counter_serialize(
1196 struct lttng_event_perf_counter_ctx
*context
,
1197 struct lttng_payload
*payload
)
1200 struct lttng_event_context_perf_counter_comm comm
= { 0 };
1205 comm
.config
= context
->config
;
1206 comm
.type
= context
->type
;
1207 comm
.name_len
= lttng_strnlen(context
->name
, LTTNG_SYMBOL_NAME_LEN
);
1209 if (comm
.name_len
== LTTNG_SYMBOL_NAME_LEN
) {
1214 /* Include the null terminator. */
1218 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1225 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, context
->name
,
1236 int lttng_event_context_serialize(struct lttng_event_context
*context
,
1237 struct lttng_payload
*payload
)
1240 struct lttng_event_context_comm context_comm
= { 0 };
1245 context_comm
.type
= (uint32_t) context
->ctx
;
1248 ret
= lttng_dynamic_buffer_append(
1249 &payload
->buffer
, &context_comm
, sizeof(context_comm
));
1254 switch (context
->ctx
) {
1255 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1256 ret
= lttng_event_context_app_serialize(context
, payload
);
1258 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1259 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1260 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1261 ret
= lttng_event_context_perf_counter_serialize(
1262 &context
->u
.perf_counter
, payload
);
1265 /* Nothing else to serialize. */
1277 void lttng_event_context_destroy(struct lttng_event_context
*context
)
1283 if (context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1284 free(context
->u
.app_ctx
.provider_name
);
1285 free(context
->u
.app_ctx
.ctx_name
);
1292 * This is a specialized populate for lttng_event_field since it ignores
1293 * the extension field of the lttng_event struct and simply copies what it can
1294 * to the internal struct lttng_event of a lttng_event_field.
1296 static void lttng_event_field_populate_lttng_event_from_event(
1297 const struct lttng_event
*src
, struct lttng_event
*destination
)
1299 memcpy(destination
, src
, sizeof(*destination
));
1301 /* Remove all possible dynamic data from the destination event rule. */
1302 destination
->extended
.ptr
= NULL
;
1305 ssize_t
lttng_event_field_create_from_payload(
1306 struct lttng_payload_view
*view
,
1307 struct lttng_event_field
**field
)
1309 ssize_t ret
, offset
= 0;
1310 struct lttng_event_field
*local_event_field
= NULL
;
1311 struct lttng_event
*event
= NULL
;
1312 const struct lttng_event_field_comm
*comm
;
1313 const char* name
= NULL
;
1319 const struct lttng_buffer_view comm_view
=
1320 lttng_buffer_view_from_view(
1321 &view
->buffer
, offset
,
1324 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1329 /* lttng_event_field_comm header */
1330 comm
= (const struct lttng_event_field_comm
*) comm_view
.data
;
1331 offset
+= sizeof(*comm
);
1334 local_event_field
= (struct lttng_event_field
*)
1335 zmalloc(sizeof(*local_event_field
));
1336 if (!local_event_field
) {
1341 local_event_field
->type
= (enum lttng_event_field_type
) comm
->type
;
1342 local_event_field
->nowrite
= comm
->nowrite
;
1346 const struct lttng_buffer_view name_view
=
1347 lttng_buffer_view_from_view(
1348 &view
->buffer
, offset
,
1351 if (!lttng_buffer_view_is_valid(&name_view
)) {
1356 name
= name_view
.data
;
1358 if (!lttng_buffer_view_contains_string(&name_view
,
1359 name_view
.data
, comm
->name_len
)) {
1364 if (comm
->name_len
> LTTNG_SYMBOL_NAME_LEN
- 1) {
1365 /* Name is too long.*/
1370 offset
+= comm
->name_len
;
1375 struct lttng_payload_view event_view
=
1376 lttng_payload_view_from_view(
1380 if (!lttng_payload_view_is_valid(&event_view
)) {
1385 ret
= lttng_event_create_from_payload(&event_view
, &event
, NULL
,
1387 if (ret
!= comm
->event_len
) {
1398 if (lttng_strncpy(local_event_field
->field_name
, name
, LTTNG_SYMBOL_NAME_LEN
)) {
1403 lttng_event_field_populate_lttng_event_from_event(
1404 event
, &local_event_field
->event
);
1406 *field
= local_event_field
;
1407 local_event_field
= NULL
;
1410 lttng_event_destroy(event
);
1411 free(local_event_field
);
1415 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1416 struct lttng_payload
*payload
)
1419 size_t header_offset
, size_before_event
;
1421 struct lttng_event_field_comm event_field_comm
= { 0 };
1422 struct lttng_event_field_comm
*header
;
1427 /* Save the header location for later in-place header update. */
1428 header_offset
= payload
->buffer
.size
;
1430 name_len
= strnlen(field
->field_name
, LTTNG_SYMBOL_NAME_LEN
);
1431 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
1432 /* Event name is not NULL-terminated. */
1437 /* Add null termination. */
1440 event_field_comm
.type
= field
->type
;
1441 event_field_comm
.nowrite
= (uint8_t)field
->nowrite
;
1442 event_field_comm
.name_len
= name_len
;
1445 ret
= lttng_dynamic_buffer_append(
1446 &payload
->buffer
, &event_field_comm
,
1447 sizeof(event_field_comm
));
1453 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, field
->field_name
,
1459 size_before_event
= payload
->buffer
.size
;
1460 ret
= lttng_event_serialize(
1461 &field
->event
, 0, NULL
, NULL
, 0, 0, payload
);
1467 /* Update the event len. */
1468 header
= (struct lttng_event_field_comm
*)
1469 ((char *) payload
->buffer
.data
+
1471 header
->event_len
= payload
->buffer
.size
- size_before_event
;
1477 static enum lttng_error_code
compute_flattened_size(
1478 struct lttng_dynamic_pointer_array
*events
, size_t *size
)
1480 enum lttng_error_code ret_code
;
1482 size_t storage_req
, event_count
, i
;
1487 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1489 /* The basic struct lttng_event */
1490 storage_req
= event_count
* sizeof(struct lttng_event
);
1492 /* The struct·lttng_event_extended */
1493 storage_req
+= event_count
* sizeof(struct lttng_event_extended
);
1495 for (i
= 0; i
< event_count
; i
++) {
1496 int probe_storage_req
= 0;
1497 const struct event_list_element
*element
= (const struct event_list_element
*)
1498 lttng_dynamic_pointer_array_get_pointer(
1500 const struct lttng_userspace_probe_location
*location
= NULL
;
1502 location
= lttng_event_get_userspace_probe_location(
1505 ret
= lttng_userspace_probe_location_flatten(
1508 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1512 probe_storage_req
= ret
;
1515 if (element
->filter_expression
) {
1516 storage_req
+= strlen(element
->filter_expression
) + 1;
1519 if (element
->exclusions
) {
1520 storage_req
+= element
->exclusions
->count
*
1521 LTTNG_SYMBOL_NAME_LEN
;
1524 /* Padding to ensure the flat probe is aligned. */
1525 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
1526 storage_req
+= probe_storage_req
;
1529 *size
= storage_req
;
1530 ret_code
= LTTNG_OK
;
1537 * Flatten a list of struct lttng_event.
1539 * The buffer that is returned to the API client must contain a "flat" version
1540 * of the events that are returned. In other words, all pointers within an
1541 * lttng_event must point to a location within the returned buffer so that the
1542 * user may free everything by simply calling free() on the returned buffer.
1543 * This is needed in order to maintain API compatibility.
1545 * A first pass is performed to compute the size of the buffer that must be
1546 * allocated. A second pass is then performed to setup the returned events so
1547 * that their members always point within the buffer.
1549 * The layout of the returned buffer is as follows:
1550 * - struct lttng_event[nb_events],
1551 * - nb_events times the following:
1552 * - struct lttng_event_extended,
1553 * - filter_expression
1555 * - padding to align to 64-bits
1556 * - flattened version of userspace_probe_location
1558 static enum lttng_error_code
flatten_lttng_events(
1559 struct lttng_dynamic_pointer_array
*events
,
1560 struct lttng_event
**flattened_events
)
1562 enum lttng_error_code ret_code
;
1565 struct lttng_dynamic_buffer local_flattened_events
;
1569 assert(flattened_events
);
1571 lttng_dynamic_buffer_init(&local_flattened_events
);
1572 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1574 ret_code
= compute_flattened_size(events
, &storage_req
);
1575 if (ret_code
!= LTTNG_OK
) {
1580 * We must ensure that "local_flattened_events" is never resized so as
1581 * to preserve the validity of the flattened objects.
1583 ret
= lttng_dynamic_buffer_set_capacity(
1584 &local_flattened_events
, storage_req
);
1586 ret_code
= LTTNG_ERR_NOMEM
;
1590 /* Start by laying the struct lttng_event */
1591 for (i
= 0; i
< nb_events
; i
++) {
1592 const struct event_list_element
*element
= (const struct event_list_element
*)
1593 lttng_dynamic_pointer_array_get_pointer(
1597 ret_code
= LTTNG_ERR_FATAL
;
1601 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1602 element
->event
, sizeof(struct lttng_event
));
1604 ret_code
= LTTNG_ERR_NOMEM
;
1609 for (i
= 0; i
< nb_events
; i
++) {
1610 const struct event_list_element
*element
= (const struct event_list_element
*)
1611 lttng_dynamic_pointer_array_get_pointer(events
, i
);
1612 struct lttng_event
*event
= (struct lttng_event
*)
1613 (local_flattened_events
.data
+ (sizeof(struct lttng_event
) * i
));
1614 struct lttng_event_extended
*event_extended
=
1615 (struct lttng_event_extended
*)
1616 (local_flattened_events
.data
+ local_flattened_events
.size
);
1617 const struct lttng_userspace_probe_location
*location
= NULL
;
1621 /* Insert struct lttng_event_extended. */
1622 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1623 local_flattened_events
.size
+
1624 sizeof(*event_extended
));
1626 ret_code
= LTTNG_ERR_NOMEM
;
1629 event
->extended
.ptr
= event_extended
;
1631 /* Insert filter expression. */
1632 if (element
->filter_expression
) {
1633 const size_t len
= strlen(element
->filter_expression
) + 1;
1635 event_extended
->filter_expression
=
1636 local_flattened_events
.data
+
1637 local_flattened_events
.size
;
1638 ret
= lttng_dynamic_buffer_append(
1639 &local_flattened_events
,
1640 element
->filter_expression
, len
);
1642 ret_code
= LTTNG_ERR_NOMEM
;
1647 /* Insert exclusions. */
1648 if (element
->exclusions
) {
1649 event_extended
->exclusions
.count
=
1650 element
->exclusions
->count
;
1651 event_extended
->exclusions
.strings
=
1652 local_flattened_events
.data
+
1653 local_flattened_events
.size
;
1655 ret
= lttng_dynamic_buffer_append(
1656 &local_flattened_events
,
1657 element
->exclusions
->names
,
1658 element
->exclusions
->count
*
1659 LTTNG_SYMBOL_NAME_LEN
);
1661 ret_code
= LTTNG_ERR_NOMEM
;
1666 /* Insert padding to align to 64-bits. */
1667 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1668 ALIGN_TO(local_flattened_events
.size
,
1671 ret_code
= LTTNG_ERR_NOMEM
;
1675 location
= lttng_event_get_userspace_probe_location(
1678 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
1679 (local_flattened_events
.data
+ local_flattened_events
.size
);
1680 ret
= lttng_userspace_probe_location_flatten(
1681 location
, &local_flattened_events
);
1683 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1689 /* Don't reset local_flattened_events buffer as we return its content. */
1690 *flattened_events
= (struct lttng_event
*) local_flattened_events
.data
;
1691 lttng_dynamic_buffer_init(&local_flattened_events
);
1692 ret_code
= LTTNG_OK
;
1694 lttng_dynamic_buffer_reset(&local_flattened_events
);
1698 static enum lttng_error_code
event_list_create_from_payload(
1699 struct lttng_payload_view
*view
,
1701 struct lttng_dynamic_pointer_array
*event_list
)
1703 enum lttng_error_code ret_code
;
1711 for (i
= 0; i
< count
; i
++) {
1713 struct lttng_payload_view event_view
=
1714 lttng_payload_view_from_view(view
, offset
, -1);
1715 struct event_list_element
*element
=
1716 (struct event_list_element
*) zmalloc(sizeof(*element
));
1719 ret_code
= LTTNG_ERR_NOMEM
;
1724 * Lifetime and management of the object is now bound to the
1727 ret
= lttng_dynamic_pointer_array_add_pointer(
1728 event_list
, element
);
1730 event_list_destructor(element
);
1731 ret_code
= LTTNG_ERR_NOMEM
;
1736 * Bytecode is not transmitted on listing in any case we do not
1739 event_size
= lttng_event_create_from_payload(&event_view
,
1741 &element
->exclusions
,
1742 &element
->filter_expression
, NULL
);
1743 if (event_size
< 0) {
1744 ret_code
= LTTNG_ERR_INVALID
;
1748 offset
+= event_size
;
1751 if (view
->buffer
.size
!= offset
) {
1752 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
1756 ret_code
= LTTNG_OK
;
1762 enum lttng_error_code
lttng_events_create_and_flatten_from_payload(
1763 struct lttng_payload_view
*payload
,
1765 struct lttng_event
**events
)
1767 enum lttng_error_code ret
= LTTNG_OK
;
1768 struct lttng_dynamic_pointer_array local_events
;
1770 lttng_dynamic_pointer_array_init(&local_events
, event_list_destructor
);
1772 /* Deserialize the events. */
1774 struct lttng_payload_view events_view
=
1775 lttng_payload_view_from_view(payload
, 0, -1);
1777 ret
= event_list_create_from_payload(
1778 &events_view
, count
, &local_events
);
1779 if (ret
!= LTTNG_OK
) {
1784 ret
= flatten_lttng_events(&local_events
, events
);
1785 if (ret
!= LTTNG_OK
) {
1790 lttng_dynamic_pointer_array_reset(&local_events
);
1794 static enum lttng_error_code
flatten_lttng_event_fields(
1795 struct lttng_dynamic_pointer_array
*event_fields
,
1796 struct lttng_event_field
**flattened_event_fields
)
1799 enum lttng_error_code ret_code
;
1800 size_t storage_req
= 0;
1801 struct lttng_dynamic_buffer local_flattened_event_fields
;
1804 assert(event_fields
);
1805 assert(flattened_event_fields
);
1807 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1808 nb_event_field
= lttng_dynamic_pointer_array_get_count(event_fields
);
1811 * Here even if the event field contains a `struct lttng_event` that
1812 * could contain dynamic data, in reality it is not the case.
1813 * Dynamic data is not present. Here the flattening is mostly a direct
1814 * memcpy. This is less than ideal but this code is still better than
1815 * direct usage of an unpacked lttng_event_field array.
1817 storage_req
+= sizeof(struct lttng_event_field
) * nb_event_field
;
1819 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1822 * We must ensure that "local_flattened_event_fields" is never resized
1823 * so as to preserve the validity of the flattened objects.
1825 ret
= lttng_dynamic_buffer_set_capacity(
1826 &local_flattened_event_fields
, storage_req
);
1828 ret_code
= LTTNG_ERR_NOMEM
;
1832 for (i
= 0; i
< nb_event_field
; i
++) {
1833 const struct lttng_event_field
*element
=
1834 (const struct lttng_event_field
*)
1835 lttng_dynamic_pointer_array_get_pointer(
1839 ret_code
= LTTNG_ERR_FATAL
;
1842 ret
= lttng_dynamic_buffer_append(&local_flattened_event_fields
,
1843 element
, sizeof(struct lttng_event_field
));
1845 ret_code
= LTTNG_ERR_NOMEM
;
1850 /* Don't reset local_flattened_channels buffer as we return its content. */
1851 *flattened_event_fields
= (struct lttng_event_field
*) local_flattened_event_fields
.data
;
1852 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1853 ret_code
= LTTNG_OK
;
1855 lttng_dynamic_buffer_reset(&local_flattened_event_fields
);
1859 static enum lttng_error_code
event_field_list_create_from_payload(
1860 struct lttng_payload_view
*view
,
1862 struct lttng_dynamic_pointer_array
**event_field_list
)
1864 enum lttng_error_code ret_code
;
1865 int ret
, offset
= 0;
1867 struct lttng_dynamic_pointer_array
*list
= NULL
;
1870 assert(event_field_list
);
1872 list
= (struct lttng_dynamic_pointer_array
*) zmalloc(sizeof(*list
));
1874 ret_code
= LTTNG_ERR_NOMEM
;
1878 lttng_dynamic_pointer_array_init(list
, free
);
1880 for (i
= 0; i
< count
; i
++) {
1881 ssize_t event_field_size
;
1882 struct lttng_event_field
*field
= NULL
;
1883 struct lttng_payload_view event_field_view
=
1884 lttng_payload_view_from_view(view
, offset
, -1);
1886 event_field_size
= lttng_event_field_create_from_payload(
1887 &event_field_view
, &field
);
1888 if (event_field_size
< 0) {
1889 ret_code
= LTTNG_ERR_INVALID
;
1893 /* Lifetime and management of the object is now bound to the array. */
1894 ret
= lttng_dynamic_pointer_array_add_pointer(list
, field
);
1897 ret_code
= LTTNG_ERR_NOMEM
;
1901 offset
+= event_field_size
;
1904 if (view
->buffer
.size
!= offset
) {
1905 ret_code
= LTTNG_ERR_INVALID
;
1909 *event_field_list
= list
;
1911 ret_code
= LTTNG_OK
;
1915 lttng_dynamic_pointer_array_reset(list
);
1922 enum lttng_error_code
lttng_event_fields_create_and_flatten_from_payload(
1923 struct lttng_payload_view
*view
,
1925 struct lttng_event_field
**fields
)
1927 enum lttng_error_code ret_code
;
1928 struct lttng_dynamic_pointer_array
*local_event_fields
= NULL
;
1930 ret_code
= event_field_list_create_from_payload(
1931 view
, count
, &local_event_fields
);
1932 if (ret_code
!= LTTNG_OK
) {
1936 ret_code
= flatten_lttng_event_fields(local_event_fields
, fields
);
1937 if (ret_code
!= LTTNG_OK
) {
1941 if (local_event_fields
) {
1942 lttng_dynamic_pointer_array_reset(local_event_fields
);
1943 free(local_event_fields
);