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>
23 struct event_list_element
{
24 struct lttng_event
*event
;
25 struct lttng_event_exclusion
*exclusions
;
26 char *filter_expression
;
29 static void event_list_destructor(void *ptr
)
31 struct event_list_element
*element
= (struct event_list_element
*) ptr
;
33 free(element
->filter_expression
);
34 free(element
->exclusions
);
35 lttng_event_destroy(element
->event
);
39 struct lttng_event
*lttng_event_copy(const struct lttng_event
*event
)
41 struct lttng_event
*new_event
;
42 struct lttng_event_extended
*new_event_extended
;
44 new_event
= zmalloc
<lttng_event
>();
46 PERROR("Error allocating event structure");
50 /* Copy the content of the old event. */
51 memcpy(new_event
, event
, sizeof(*event
));
54 * We need to create a new extended since the previous pointer is now
57 new_event_extended
= zmalloc
<lttng_event_extended
>();
58 if (!new_event_extended
) {
59 PERROR("Error allocating event extended structure");
63 new_event
->extended
.ptr
= new_event_extended
;
72 static int lttng_event_probe_attr_serialize(
73 const struct lttng_event_probe_attr
*probe
,
74 struct lttng_payload
*payload
)
77 size_t symbol_name_len
;
78 struct lttng_event_probe_attr_comm comm
= {};
80 symbol_name_len
= lttng_strnlen(
81 probe
->symbol_name
, sizeof(probe
->symbol_name
));
82 if (symbol_name_len
== sizeof(probe
->symbol_name
)) {
83 /* Not null-termintated. */
88 /* Include the null terminator. */
91 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
92 comm
.addr
= probe
->addr
;
93 comm
.offset
= probe
->addr
;
95 ret
= lttng_dynamic_buffer_append(
96 &payload
->buffer
, &comm
, sizeof(comm
));
102 ret
= lttng_dynamic_buffer_append(
103 &payload
->buffer
, probe
->symbol_name
, symbol_name_len
);
108 static int lttng_event_function_attr_serialize(
109 const struct lttng_event_function_attr
*function
,
110 struct lttng_payload
*payload
)
113 size_t symbol_name_len
;
114 struct lttng_event_function_attr_comm comm
= { 0 };
116 symbol_name_len
= lttng_strnlen(
117 function
->symbol_name
, sizeof(function
->symbol_name
));
118 if (symbol_name_len
== sizeof(function
->symbol_name
)) {
119 /* Not null-termintated. */
124 /* Include the null terminator. */
125 symbol_name_len
+= 1;
127 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
129 ret
= lttng_dynamic_buffer_append(
130 &payload
->buffer
, &comm
, sizeof(comm
));
136 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
137 function
->symbol_name
, symbol_name_len
);
142 static ssize_t
lttng_event_probe_attr_create_from_payload(
143 struct lttng_payload_view
*view
,
144 struct lttng_event_probe_attr
**probe_attr
)
146 ssize_t ret
, offset
= 0;
147 const struct lttng_event_probe_attr_comm
*comm
;
148 struct lttng_event_probe_attr
*local_attr
= NULL
;
149 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
150 view
, offset
, sizeof(*comm
));
152 if (!lttng_payload_view_is_valid(&comm_view
)) {
157 comm
= (typeof(comm
)) comm_view
.buffer
.data
;
158 offset
+= sizeof(*comm
);
160 local_attr
= zmalloc
<lttng_event_probe_attr
>();
161 if (local_attr
== NULL
) {
166 local_attr
->addr
= comm
->addr
;
167 local_attr
->offset
= comm
->offset
;
171 struct lttng_payload_view name_view
=
172 lttng_payload_view_from_view(view
, offset
,
173 comm
->symbol_name_len
);
175 if (!lttng_payload_view_is_valid(&name_view
)) {
180 name
= name_view
.buffer
.data
;
182 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
183 comm
->symbol_name_len
)) {
188 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
189 sizeof(local_attr
->symbol_name
));
195 offset
+= comm
->symbol_name_len
;
198 *probe_attr
= local_attr
;
206 static ssize_t
lttng_event_function_attr_create_from_payload(
207 struct lttng_payload_view
*view
,
208 struct lttng_event_function_attr
**function_attr
)
210 ssize_t ret
, offset
= 0;
211 const struct lttng_event_function_attr_comm
*comm
;
212 struct lttng_event_function_attr
*local_attr
= NULL
;
213 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
214 view
, offset
, sizeof(*comm
));
216 if (!lttng_payload_view_is_valid(&comm_view
)) {
221 comm
= (typeof(comm
)) view
->buffer
.data
;
222 offset
+= sizeof(*comm
);
224 local_attr
= zmalloc
<lttng_event_function_attr
>();
225 if (local_attr
== NULL
) {
232 struct lttng_payload_view name_view
=
233 lttng_payload_view_from_view(view
, offset
,
234 comm
->symbol_name_len
);
236 if (!lttng_payload_view_is_valid(&name_view
)) {
241 name
= name_view
.buffer
.data
;
243 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
244 comm
->symbol_name_len
)) {
249 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
250 sizeof(local_attr
->symbol_name
));
256 offset
+= comm
->symbol_name_len
;
259 *function_attr
= local_attr
;
267 static ssize_t
lttng_event_exclusions_create_from_payload(
268 struct lttng_payload_view
*view
,
270 struct lttng_event_exclusion
**exclusions
)
272 ssize_t ret
, offset
= 0;
273 const size_t size
= (count
* LTTNG_SYMBOL_NAME_LEN
);
275 const struct lttng_event_exclusion_comm
*comm
;
276 struct lttng_event_exclusion
*local_exclusions
;
278 local_exclusions
= zmalloc
<lttng_event_exclusion
>(
279 sizeof(struct lttng_event_exclusion
) + size
);
280 if (!local_exclusions
) {
285 local_exclusions
->count
= count
;
287 for (i
= 0; i
< count
; i
++) {
289 struct lttng_buffer_view string_view
;
290 const struct lttng_buffer_view comm_view
=
291 lttng_buffer_view_from_view(&view
->buffer
,
292 offset
, sizeof(*comm
));
294 if (!lttng_buffer_view_is_valid(&comm_view
)) {
299 comm
= (typeof(comm
)) comm_view
.data
;
300 offset
+= sizeof(*comm
);
302 string_view
= lttng_buffer_view_from_view(
303 &view
->buffer
, offset
, comm
->len
);
305 if (!lttng_buffer_view_is_valid(&string_view
)) {
310 string
= string_view
.data
;
312 if (!lttng_buffer_view_contains_string(
313 &string_view
, string
, comm
->len
)) {
318 ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(local_exclusions
, i
), string
,
319 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(local_exclusions
, i
)));
328 *exclusions
= local_exclusions
;
329 local_exclusions
= NULL
;
332 free(local_exclusions
);
336 ssize_t
lttng_event_create_from_payload(struct lttng_payload_view
*view
,
337 struct lttng_event
**out_event
,
338 struct lttng_event_exclusion
**out_exclusion
,
339 char **out_filter_expression
,
340 struct lttng_bytecode
**out_bytecode
)
342 ssize_t ret
, offset
= 0;
343 struct lttng_event
*local_event
= NULL
;
344 struct lttng_event_exclusion
*local_exclusions
= NULL
;
345 struct lttng_bytecode
*local_bytecode
= NULL
;
346 char *local_filter_expression
= NULL
;
347 const struct lttng_event_comm
*event_comm
;
348 struct lttng_event_function_attr
*local_function_attr
= NULL
;
349 struct lttng_event_probe_attr
*local_probe_attr
= NULL
;
350 struct lttng_userspace_probe_location
*local_userspace_probe_location
=
354 * Only event is obligatory, the other output argument are optional and
355 * depends on what the caller is interested in.
361 struct lttng_payload_view comm_view
=
362 lttng_payload_view_from_view(view
, offset
,
363 sizeof(*event_comm
));
365 if (!lttng_payload_view_is_valid(&comm_view
)) {
370 /* lttng_event_comm header */
371 event_comm
= (typeof(event_comm
)) comm_view
.buffer
.data
;
372 offset
+= sizeof(*event_comm
);
375 local_event
= lttng_event_create();
376 if (local_event
== NULL
) {
381 local_event
->type
= (enum lttng_event_type
) event_comm
->event_type
;
382 local_event
->loglevel_type
= (enum lttng_loglevel_type
) event_comm
->loglevel_type
;
383 local_event
->loglevel
= event_comm
->loglevel
;
384 local_event
->enabled
= event_comm
->enabled
;
385 local_event
->pid
= event_comm
->pid
;
386 local_event
->flags
= (enum lttng_event_flag
) event_comm
->flags
;
390 const struct lttng_buffer_view name_view
=
391 lttng_buffer_view_from_view(&view
->buffer
,
392 offset
, event_comm
->name_len
);
394 if (!lttng_buffer_view_is_valid(&name_view
)) {
399 name
= (const char *) name_view
.data
;
401 if (!lttng_buffer_view_contains_string(
402 &name_view
, name
, event_comm
->name_len
)) {
407 ret
= lttng_strncpy(local_event
->name
, name
,
408 sizeof(local_event
->name
));
414 offset
+= event_comm
->name_len
;
418 if (event_comm
->exclusion_count
== 0) {
419 goto deserialize_filter_expression
;
423 struct lttng_payload_view exclusions_view
=
424 lttng_payload_view_from_view(
427 if (!lttng_payload_view_is_valid(&exclusions_view
)) {
432 ret
= lttng_event_exclusions_create_from_payload(&exclusions_view
,
433 event_comm
->exclusion_count
, &local_exclusions
);
440 local_event
->exclusion
= 1;
443 deserialize_filter_expression
:
445 if (event_comm
->filter_expression_len
== 0) {
446 if (event_comm
->bytecode_len
!= 0) {
448 * This is an invalid event payload.
450 * Filter expression without bytecode is possible but
451 * not the other way around.
456 goto deserialize_event_type_payload
;
460 const char *filter_expression_buffer
;
461 struct lttng_buffer_view filter_expression_view
=
462 lttng_buffer_view_from_view(&view
->buffer
, offset
,
463 event_comm
->filter_expression_len
);
465 if (!lttng_buffer_view_is_valid(&filter_expression_view
)) {
470 filter_expression_buffer
= filter_expression_view
.data
;
472 if (!lttng_buffer_view_contains_string(&filter_expression_view
,
473 filter_expression_buffer
,
474 event_comm
->filter_expression_len
)) {
479 local_filter_expression
= lttng_strndup(
480 filter_expression_buffer
,
481 event_comm
->filter_expression_len
);
482 if (!local_filter_expression
) {
487 local_event
->filter
= 1;
489 offset
+= event_comm
->filter_expression_len
;
492 if (event_comm
->bytecode_len
== 0) {
494 * Filter expression can be present but without bytecode
495 * when dealing with event listing.
497 goto deserialize_event_type_payload
;
502 struct lttng_payload_view bytecode_view
=
503 lttng_payload_view_from_view(view
, offset
,
504 event_comm
->bytecode_len
);
506 if (!lttng_payload_view_is_valid(&bytecode_view
)) {
511 local_bytecode
= zmalloc
<lttng_bytecode
>(event_comm
->bytecode_len
);
512 if (!local_bytecode
) {
517 memcpy(local_bytecode
, bytecode_view
.buffer
.data
,
518 event_comm
->bytecode_len
);
519 if ((local_bytecode
->len
+ sizeof(*local_bytecode
)) !=
520 event_comm
->bytecode_len
) {
525 offset
+= event_comm
->bytecode_len
;
528 deserialize_event_type_payload
:
529 /* Event type specific payload */
530 switch (local_event
->type
) {
531 case LTTNG_EVENT_FUNCTION
:
533 case LTTNG_EVENT_PROBE
:
535 struct lttng_payload_view probe_attr_view
=
536 lttng_payload_view_from_view(view
, offset
,
537 event_comm
->lttng_event_probe_attr_len
);
539 if (event_comm
->lttng_event_probe_attr_len
== 0) {
544 if (!lttng_payload_view_is_valid(&probe_attr_view
)) {
549 ret
= lttng_event_probe_attr_create_from_payload(
550 &probe_attr_view
, &local_probe_attr
);
551 if (ret
< 0 || ret
!= event_comm
->lttng_event_probe_attr_len
) {
556 /* Copy to the local event. */
557 memcpy(&local_event
->attr
.probe
, local_probe_attr
,
558 sizeof(local_event
->attr
.probe
));
563 case LTTNG_EVENT_FUNCTION_ENTRY
:
565 struct lttng_payload_view function_attr_view
=
566 lttng_payload_view_from_view(view
, offset
,
567 event_comm
->lttng_event_function_attr_len
);
569 if (event_comm
->lttng_event_function_attr_len
== 0) {
574 if (!lttng_payload_view_is_valid(&function_attr_view
)) {
579 ret
= lttng_event_function_attr_create_from_payload(
580 &function_attr_view
, &local_function_attr
);
581 if (ret
< 0 || ret
!= event_comm
->lttng_event_function_attr_len
) {
586 /* Copy to the local event. */
587 memcpy(&local_event
->attr
.ftrace
, local_function_attr
,
588 sizeof(local_event
->attr
.ftrace
));
594 case LTTNG_EVENT_USERSPACE_PROBE
:
596 struct lttng_payload_view userspace_probe_location_view
=
597 lttng_payload_view_from_view(view
, offset
,
598 event_comm
->userspace_probe_location_len
);
600 if (event_comm
->userspace_probe_location_len
== 0) {
605 if (!lttng_payload_view_is_valid(
606 &userspace_probe_location_view
)) {
611 ret
= lttng_userspace_probe_location_create_from_payload(
612 &userspace_probe_location_view
,
613 &local_userspace_probe_location
);
615 WARN("Failed to create a userspace probe location from the received buffer");
620 if (ret
!= event_comm
->userspace_probe_location_len
) {
621 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
);
626 /* Attach the probe location to the event. */
627 ret
= lttng_event_set_userspace_probe_location(
628 local_event
, local_userspace_probe_location
);
630 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
635 * Userspace probe location object ownership transfered to the
638 local_userspace_probe_location
= NULL
;
639 offset
+= event_comm
->userspace_probe_location_len
;
642 case LTTNG_EVENT_TRACEPOINT
:
644 case LTTNG_EVENT_ALL
:
646 case LTTNG_EVENT_SYSCALL
:
648 case LTTNG_EVENT_NOOP
:
649 /* Nothing to do here */
657 /* Transfer ownership to the caller. */
658 *out_event
= local_event
;
662 *out_bytecode
= local_bytecode
;
663 local_bytecode
= NULL
;
667 *out_exclusion
= local_exclusions
;
668 local_exclusions
= NULL
;
671 if (out_filter_expression
) {
672 *out_filter_expression
= local_filter_expression
;
673 local_filter_expression
= NULL
;
678 lttng_event_destroy(local_event
);
679 lttng_userspace_probe_location_destroy(local_userspace_probe_location
);
680 free(local_filter_expression
);
681 free(local_exclusions
);
682 free(local_bytecode
);
683 free(local_function_attr
);
684 free(local_probe_attr
);
688 int lttng_event_serialize(const struct lttng_event
*event
,
689 unsigned int exclusion_count
,
690 char **exclusion_list
,
691 char *filter_expression
,
693 struct lttng_bytecode
*bytecode
,
694 struct lttng_payload
*payload
)
698 size_t header_offset
, size_before_payload
;
700 struct lttng_event_comm event_comm
= {};
701 struct lttng_event_comm
*header
;
705 assert(exclusion_count
== 0 || exclusion_list
);
707 /* Save the header location for later in-place header update. */
708 header_offset
= payload
->buffer
.size
;
710 name_len
= lttng_strnlen(event
->name
, sizeof(event
->name
));
711 if (name_len
== sizeof(event
->name
)) {
712 /* Event name is not NULL-terminated. */
717 /* Add null termination. */
720 if (exclusion_count
> UINT32_MAX
) {
721 /* Possible overflow. */
726 if (bytecode_len
> UINT32_MAX
) {
727 /* Possible overflow. */
732 event_comm
.name_len
= (uint32_t) name_len
;
733 event_comm
.event_type
= (int8_t) event
->type
;
734 event_comm
.loglevel_type
= (int8_t) event
->loglevel_type
;
735 event_comm
.loglevel
= (int32_t) event
->loglevel
;
736 event_comm
.enabled
= (int8_t) event
->enabled
;
737 event_comm
.pid
= (int32_t) event
->pid
;
738 event_comm
.exclusion_count
= (uint32_t) exclusion_count
;
739 event_comm
.bytecode_len
= (uint32_t) bytecode_len
;
740 event_comm
.flags
= (int32_t) event
->flags
;
742 if (filter_expression
) {
743 event_comm
.filter_expression_len
=
744 strlen(filter_expression
) + 1;
748 ret
= lttng_dynamic_buffer_append(
749 &payload
->buffer
, &event_comm
, sizeof(event_comm
));
755 ret
= lttng_dynamic_buffer_append(
756 &payload
->buffer
, event
->name
, name_len
);
762 for (i
= 0; i
< exclusion_count
; i
++) {
763 const size_t exclusion_len
= lttng_strnlen(
764 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
765 const struct lttng_event_exclusion_comm exclusion_header
= {
766 .len
= (uint32_t) exclusion_len
+ 1,
769 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
770 /* Exclusion is not NULL-terminated. */
775 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
776 &exclusion_header
, sizeof(exclusion_header
));
781 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
782 *(exclusion_list
+ i
), exclusion_len
+ 1);
788 /* Filter expression and its bytecode */
789 if (filter_expression
) {
790 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
792 event_comm
.filter_expression_len
);
798 * Bytecode can be absent when we serialize to the client
802 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
803 bytecode
, bytecode_len
);
810 size_before_payload
= payload
->buffer
.size
;
812 /* Event type specific payload */
813 switch (event
->type
) {
814 case LTTNG_EVENT_FUNCTION
:
816 case LTTNG_EVENT_PROBE
:
817 ret
= lttng_event_probe_attr_serialize(&event
->attr
.probe
, payload
);
823 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
825 header
->lttng_event_probe_attr_len
=
826 payload
->buffer
.size
- size_before_payload
;
829 case LTTNG_EVENT_FUNCTION_ENTRY
:
830 ret
= lttng_event_function_attr_serialize(
831 &event
->attr
.ftrace
, payload
);
837 /* Update the lttng_event_function_attr len. */
838 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
840 header
->lttng_event_function_attr_len
=
841 payload
->buffer
.size
- size_before_payload
;
844 case LTTNG_EVENT_USERSPACE_PROBE
:
846 const struct lttng_event_extended
*ev_ext
=
847 (const struct lttng_event_extended
*)
850 assert(event
->extended
.ptr
);
851 assert(ev_ext
->probe_location
);
853 size_before_payload
= payload
->buffer
.size
;
854 if (ev_ext
->probe_location
) {
856 * lttng_userspace_probe_location_serialize returns the
857 * number of bytes that were appended to the buffer.
859 ret
= lttng_userspace_probe_location_serialize(
860 ev_ext
->probe_location
, payload
);
867 /* Update the userspace probe location len. */
868 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
870 header
->userspace_probe_location_len
=
871 payload
->buffer
.size
- size_before_payload
;
875 case LTTNG_EVENT_TRACEPOINT
:
877 case LTTNG_EVENT_ALL
:
880 /* Nothing to do here. */
888 static ssize_t
lttng_event_context_app_populate_from_payload(
889 const struct lttng_payload_view
*view
,
890 struct lttng_event_context
*event_ctx
)
892 ssize_t ret
, offset
= 0;
893 const struct lttng_event_context_app_comm
*comm
;
894 char *provider_name
= NULL
, *context_name
= NULL
;
895 size_t provider_name_len
, context_name_len
;
896 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
897 &view
->buffer
, offset
, sizeof(*comm
));
899 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
901 if (!lttng_buffer_view_is_valid(&comm_view
)) {
906 comm
= (typeof(comm
)) comm_view
.data
;
907 offset
+= sizeof(*comm
);
909 provider_name_len
= comm
->provider_name_len
;
910 context_name_len
= comm
->ctx_name_len
;
912 if (provider_name_len
== 0 || context_name_len
== 0) {
914 * Application provider and context names MUST
923 const struct lttng_buffer_view provider_name_view
=
924 lttng_buffer_view_from_view(&view
->buffer
,
928 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
933 name
= provider_name_view
.data
;
935 if (!lttng_buffer_view_contains_string(&provider_name_view
,
936 name
, provider_name_len
)) {
941 provider_name
= lttng_strndup(name
, provider_name_len
);
942 if (!provider_name
) {
947 offset
+= provider_name_len
;
952 const struct lttng_buffer_view context_name_view
=
953 lttng_buffer_view_from_view(
954 &view
->buffer
, offset
,
957 if (!lttng_buffer_view_is_valid(&context_name_view
)) {
962 name
= context_name_view
.data
;
964 if (!lttng_buffer_view_contains_string(&context_name_view
, name
,
970 context_name
= lttng_strndup(name
, context_name_len
);
976 offset
+= context_name_len
;
979 /* Transfer ownership of the strings */
980 event_ctx
->u
.app_ctx
.provider_name
= provider_name
;
981 event_ctx
->u
.app_ctx
.ctx_name
= context_name
;
982 provider_name
= NULL
;
993 static ssize_t
lttng_event_context_perf_counter_populate_from_payload(
994 const struct lttng_payload_view
*view
,
995 struct lttng_event_context
*event_ctx
)
998 ssize_t consumed
, offset
= 0;
999 const struct lttng_event_context_perf_counter_comm
*comm
;
1001 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1002 &view
->buffer
, offset
, sizeof(*comm
));
1004 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_COUNTER
||
1006 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
||
1007 event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
);
1009 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1014 comm
= (typeof(comm
)) comm_view
.data
;
1015 offset
+= sizeof(*comm
);
1017 name_len
= comm
->name_len
;
1021 const struct lttng_buffer_view provider_name_view
=
1022 lttng_buffer_view_from_view(
1023 &view
->buffer
, offset
,
1026 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
1031 name
= provider_name_view
.data
;
1033 if (!lttng_buffer_view_contains_string(
1034 &provider_name_view
, name
, name_len
)) {
1039 ret
= lttng_strncpy(event_ctx
->u
.perf_counter
.name
, name
,
1040 sizeof(event_ctx
->u
.perf_counter
.name
));
1048 event_ctx
->u
.perf_counter
.config
= comm
->config
;
1049 event_ctx
->u
.perf_counter
.type
= comm
->type
;
1057 ssize_t
lttng_event_context_create_from_payload(
1058 struct lttng_payload_view
*view
,
1059 struct lttng_event_context
**event_ctx
)
1061 ssize_t ret
, offset
= 0;
1062 const struct lttng_event_context_comm
*comm
;
1063 struct lttng_event_context
*local_context
= NULL
;
1064 struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1065 &view
->buffer
, offset
, sizeof(*comm
));
1070 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1075 comm
= (typeof(comm
)) comm_view
.data
;
1076 offset
+= sizeof(*comm
);
1078 local_context
= zmalloc
<lttng_event_context
>();
1079 if (!local_context
) {
1084 local_context
->ctx
= (lttng_event_context_type
) comm
->type
;
1087 struct lttng_payload_view subtype_view
=
1088 lttng_payload_view_from_view(view
, offset
, -1);
1090 switch (local_context
->ctx
) {
1091 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1092 ret
= lttng_event_context_app_populate_from_payload(
1093 &subtype_view
, local_context
);
1095 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1096 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1097 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1098 ret
= lttng_event_context_perf_counter_populate_from_payload(
1099 &subtype_view
, local_context
);
1102 /* Nothing else to deserialize. */
1114 *event_ctx
= local_context
;
1115 local_context
= NULL
;
1119 free(local_context
);
1123 static int lttng_event_context_app_serialize(
1124 struct lttng_event_context
*context
,
1125 struct lttng_payload
*payload
)
1128 struct lttng_event_context_app_comm comm
= {};
1129 size_t provider_len
, ctx_len
;
1130 const char *provider_name
;
1131 const char *ctx_name
;
1135 assert(context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1137 provider_name
= context
->u
.app_ctx
.provider_name
;
1138 ctx_name
= context
->u
.app_ctx
.ctx_name
;
1140 if (!provider_name
|| !ctx_name
) {
1141 ret
= -LTTNG_ERR_INVALID
;
1145 provider_len
= strlen(provider_name
);
1146 if (provider_len
== 0) {
1147 ret
= -LTTNG_ERR_INVALID
;
1151 /* Include the null terminator. */
1153 comm
.provider_name_len
= provider_len
;
1155 ctx_len
= strlen(ctx_name
);
1157 ret
= -LTTNG_ERR_INVALID
;
1161 /* Include the null terminator. */
1163 comm
.ctx_name_len
= ctx_len
;
1166 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1173 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, provider_name
,
1180 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, ctx_name
,
1191 static int lttng_event_context_perf_counter_serialize(
1192 struct lttng_event_perf_counter_ctx
*context
,
1193 struct lttng_payload
*payload
)
1196 struct lttng_event_context_perf_counter_comm comm
= {};
1201 comm
.config
= context
->config
;
1202 comm
.type
= context
->type
;
1203 comm
.name_len
= lttng_strnlen(context
->name
, sizeof(context
->name
));
1205 if (comm
.name_len
== sizeof(context
->name
)) {
1210 /* Include the null terminator. */
1214 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1221 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, context
->name
,
1232 int lttng_event_context_serialize(struct lttng_event_context
*context
,
1233 struct lttng_payload
*payload
)
1236 struct lttng_event_context_comm context_comm
= { 0 };
1241 context_comm
.type
= (uint32_t) context
->ctx
;
1244 ret
= lttng_dynamic_buffer_append(
1245 &payload
->buffer
, &context_comm
, sizeof(context_comm
));
1250 switch (context
->ctx
) {
1251 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1252 ret
= lttng_event_context_app_serialize(context
, payload
);
1254 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1255 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1256 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1257 ret
= lttng_event_context_perf_counter_serialize(
1258 &context
->u
.perf_counter
, payload
);
1261 /* Nothing else to serialize. */
1273 void lttng_event_context_destroy(struct lttng_event_context
*context
)
1279 if (context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1280 free(context
->u
.app_ctx
.provider_name
);
1281 free(context
->u
.app_ctx
.ctx_name
);
1288 * This is a specialized populate for lttng_event_field since it ignores
1289 * the extension field of the lttng_event struct and simply copies what it can
1290 * to the internal struct lttng_event of a lttng_event_field.
1292 static void lttng_event_field_populate_lttng_event_from_event(
1293 const struct lttng_event
*src
, struct lttng_event
*destination
)
1295 memcpy(destination
, src
, sizeof(*destination
));
1297 /* Remove all possible dynamic data from the destination event rule. */
1298 destination
->extended
.ptr
= NULL
;
1301 ssize_t
lttng_event_field_create_from_payload(
1302 struct lttng_payload_view
*view
,
1303 struct lttng_event_field
**field
)
1305 ssize_t ret
, offset
= 0;
1306 struct lttng_event_field
*local_event_field
= NULL
;
1307 struct lttng_event
*event
= NULL
;
1308 const struct lttng_event_field_comm
*comm
;
1309 const char* name
= NULL
;
1315 const struct lttng_buffer_view comm_view
=
1316 lttng_buffer_view_from_view(
1317 &view
->buffer
, offset
,
1320 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1325 /* lttng_event_field_comm header */
1326 comm
= (const lttng_event_field_comm
*) comm_view
.data
;
1327 offset
+= sizeof(*comm
);
1330 local_event_field
= zmalloc
<lttng_event_field
>();
1331 if (!local_event_field
) {
1336 local_event_field
->type
= (lttng_event_field_type
) comm
->type
;
1337 local_event_field
->nowrite
= comm
->nowrite
;
1341 const struct lttng_buffer_view name_view
=
1342 lttng_buffer_view_from_view(
1343 &view
->buffer
, offset
,
1346 if (!lttng_buffer_view_is_valid(&name_view
)) {
1351 name
= name_view
.data
;
1353 if (!lttng_buffer_view_contains_string(&name_view
,
1354 name_view
.data
, comm
->name_len
)) {
1359 if (comm
->name_len
> LTTNG_SYMBOL_NAME_LEN
- 1) {
1360 /* Name is too long.*/
1365 offset
+= comm
->name_len
;
1370 struct lttng_payload_view event_view
=
1371 lttng_payload_view_from_view(
1375 if (!lttng_payload_view_is_valid(&event_view
)) {
1380 ret
= lttng_event_create_from_payload(&event_view
, &event
, NULL
,
1382 if (ret
!= comm
->event_len
) {
1393 if (lttng_strncpy(local_event_field
->field_name
, name
,
1394 sizeof(local_event_field
->field_name
))) {
1399 lttng_event_field_populate_lttng_event_from_event(
1400 event
, &local_event_field
->event
);
1402 *field
= local_event_field
;
1403 local_event_field
= NULL
;
1406 lttng_event_destroy(event
);
1407 free(local_event_field
);
1411 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1412 struct lttng_payload
*payload
)
1415 size_t header_offset
, size_before_event
;
1417 struct lttng_event_field_comm event_field_comm
= {};
1418 struct lttng_event_field_comm
*header
;
1423 /* Save the header location for later in-place header update. */
1424 header_offset
= payload
->buffer
.size
;
1426 name_len
= strnlen(field
->field_name
, sizeof(field
->field_name
));
1427 if (name_len
== sizeof(field
->field_name
)) {
1428 /* Event name is not NULL-terminated. */
1433 /* Add null termination. */
1436 event_field_comm
.type
= field
->type
;
1437 event_field_comm
.nowrite
= (uint8_t)field
->nowrite
;
1438 event_field_comm
.name_len
= name_len
;
1441 ret
= lttng_dynamic_buffer_append(
1442 &payload
->buffer
, &event_field_comm
,
1443 sizeof(event_field_comm
));
1449 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, field
->field_name
,
1455 size_before_event
= payload
->buffer
.size
;
1456 ret
= lttng_event_serialize(
1457 &field
->event
, 0, NULL
, NULL
, 0, 0, payload
);
1463 /* Update the event len. */
1464 header
= (struct lttng_event_field_comm
*)
1465 ((char *) payload
->buffer
.data
+
1467 header
->event_len
= payload
->buffer
.size
- size_before_event
;
1473 static enum lttng_error_code
compute_flattened_size(
1474 struct lttng_dynamic_pointer_array
*events
, size_t *size
)
1476 enum lttng_error_code ret_code
;
1478 size_t storage_req
, event_count
, i
;
1483 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1485 /* The basic struct lttng_event */
1486 storage_req
= event_count
* sizeof(struct lttng_event
);
1488 /* The struct·lttng_event_extended */
1489 storage_req
+= event_count
* sizeof(struct lttng_event_extended
);
1491 for (i
= 0; i
< event_count
; i
++) {
1492 int probe_storage_req
= 0;
1493 const struct event_list_element
*element
= (const struct event_list_element
*)
1494 lttng_dynamic_pointer_array_get_pointer(
1496 const struct lttng_userspace_probe_location
*location
= NULL
;
1498 location
= lttng_event_get_userspace_probe_location(
1501 ret
= lttng_userspace_probe_location_flatten(
1504 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1508 probe_storage_req
= ret
;
1511 if (element
->filter_expression
) {
1512 storage_req
+= strlen(element
->filter_expression
) + 1;
1515 if (element
->exclusions
) {
1516 storage_req
+= element
->exclusions
->count
*
1517 LTTNG_SYMBOL_NAME_LEN
;
1520 /* Padding to ensure the flat probe is aligned. */
1521 storage_req
= lttng_align_ceil(storage_req
, sizeof(uint64_t));
1522 storage_req
+= probe_storage_req
;
1525 *size
= storage_req
;
1526 ret_code
= LTTNG_OK
;
1533 * Flatten a list of struct lttng_event.
1535 * The buffer that is returned to the API client must contain a "flat" version
1536 * of the events that are returned. In other words, all pointers within an
1537 * lttng_event must point to a location within the returned buffer so that the
1538 * user may free everything by simply calling free() on the returned buffer.
1539 * This is needed in order to maintain API compatibility.
1541 * A first pass is performed to compute the size of the buffer that must be
1542 * allocated. A second pass is then performed to setup the returned events so
1543 * that their members always point within the buffer.
1545 * The layout of the returned buffer is as follows:
1546 * - struct lttng_event[nb_events],
1547 * - nb_events times the following:
1548 * - struct lttng_event_extended,
1549 * - filter_expression
1551 * - padding to align to 64-bits
1552 * - flattened version of userspace_probe_location
1554 static enum lttng_error_code
flatten_lttng_events(
1555 struct lttng_dynamic_pointer_array
*events
,
1556 struct lttng_event
**flattened_events
)
1558 enum lttng_error_code ret_code
;
1561 struct lttng_dynamic_buffer local_flattened_events
;
1565 assert(flattened_events
);
1567 lttng_dynamic_buffer_init(&local_flattened_events
);
1568 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1570 ret_code
= compute_flattened_size(events
, &storage_req
);
1571 if (ret_code
!= LTTNG_OK
) {
1576 * We must ensure that "local_flattened_events" is never resized so as
1577 * to preserve the validity of the flattened objects.
1579 ret
= lttng_dynamic_buffer_set_capacity(
1580 &local_flattened_events
, storage_req
);
1582 ret_code
= LTTNG_ERR_NOMEM
;
1586 /* Start by laying the struct lttng_event */
1587 for (i
= 0; i
< nb_events
; i
++) {
1588 const struct event_list_element
*element
= (const struct event_list_element
*)
1589 lttng_dynamic_pointer_array_get_pointer(
1593 ret_code
= LTTNG_ERR_FATAL
;
1597 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1598 element
->event
, sizeof(struct lttng_event
));
1600 ret_code
= LTTNG_ERR_NOMEM
;
1605 for (i
= 0; i
< nb_events
; i
++) {
1606 const struct event_list_element
*element
= (const struct event_list_element
*)
1607 lttng_dynamic_pointer_array_get_pointer(events
, i
);
1608 struct lttng_event
*event
= (struct lttng_event
*)
1609 (local_flattened_events
.data
+ (sizeof(struct lttng_event
) * i
));
1610 struct lttng_event_extended
*event_extended
=
1611 (struct lttng_event_extended
*)
1612 (local_flattened_events
.data
+ local_flattened_events
.size
);
1613 const struct lttng_userspace_probe_location
*location
= NULL
;
1617 /* Insert struct lttng_event_extended. */
1618 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1619 local_flattened_events
.size
+
1620 sizeof(*event_extended
));
1622 ret_code
= LTTNG_ERR_NOMEM
;
1625 event
->extended
.ptr
= event_extended
;
1627 /* Insert filter expression. */
1628 if (element
->filter_expression
) {
1629 const size_t len
= strlen(element
->filter_expression
) + 1;
1631 event_extended
->filter_expression
=
1632 local_flattened_events
.data
+
1633 local_flattened_events
.size
;
1634 ret
= lttng_dynamic_buffer_append(
1635 &local_flattened_events
,
1636 element
->filter_expression
, len
);
1638 ret_code
= LTTNG_ERR_NOMEM
;
1643 /* Insert exclusions. */
1644 if (element
->exclusions
) {
1645 event_extended
->exclusions
.count
=
1646 element
->exclusions
->count
;
1647 event_extended
->exclusions
.strings
=
1648 local_flattened_events
.data
+
1649 local_flattened_events
.size
;
1651 ret
= lttng_dynamic_buffer_append(
1652 &local_flattened_events
,
1653 element
->exclusions
->names
,
1654 element
->exclusions
->count
*
1655 LTTNG_SYMBOL_NAME_LEN
);
1657 ret_code
= LTTNG_ERR_NOMEM
;
1662 /* Insert padding to align to 64-bits. */
1663 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1664 lttng_align_ceil(local_flattened_events
.size
,
1667 ret_code
= LTTNG_ERR_NOMEM
;
1671 location
= lttng_event_get_userspace_probe_location(
1674 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
1675 (local_flattened_events
.data
+ local_flattened_events
.size
);
1676 ret
= lttng_userspace_probe_location_flatten(
1677 location
, &local_flattened_events
);
1679 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1685 /* Don't reset local_flattened_events buffer as we return its content. */
1686 *flattened_events
= (struct lttng_event
*) local_flattened_events
.data
;
1687 lttng_dynamic_buffer_init(&local_flattened_events
);
1688 ret_code
= LTTNG_OK
;
1690 lttng_dynamic_buffer_reset(&local_flattened_events
);
1694 static enum lttng_error_code
event_list_create_from_payload(
1695 struct lttng_payload_view
*view
,
1697 struct lttng_dynamic_pointer_array
*event_list
)
1699 enum lttng_error_code ret_code
;
1707 for (i
= 0; i
< count
; i
++) {
1709 struct lttng_payload_view event_view
=
1710 lttng_payload_view_from_view(view
, offset
, -1);
1711 struct event_list_element
*element
= zmalloc
<event_list_element
>();
1714 ret_code
= LTTNG_ERR_NOMEM
;
1719 * Lifetime and management of the object is now bound to the
1722 ret
= lttng_dynamic_pointer_array_add_pointer(
1723 event_list
, element
);
1725 event_list_destructor(element
);
1726 ret_code
= LTTNG_ERR_NOMEM
;
1731 * Bytecode is not transmitted on listing in any case we do not
1734 event_size
= lttng_event_create_from_payload(&event_view
,
1736 &element
->exclusions
,
1737 &element
->filter_expression
, NULL
);
1738 if (event_size
< 0) {
1739 ret_code
= LTTNG_ERR_INVALID
;
1743 offset
+= event_size
;
1746 if (view
->buffer
.size
!= offset
) {
1747 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
1751 ret_code
= LTTNG_OK
;
1757 enum lttng_error_code
lttng_events_create_and_flatten_from_payload(
1758 struct lttng_payload_view
*payload
,
1760 struct lttng_event
**events
)
1762 enum lttng_error_code ret
= LTTNG_OK
;
1763 struct lttng_dynamic_pointer_array local_events
;
1765 lttng_dynamic_pointer_array_init(&local_events
, event_list_destructor
);
1767 /* Deserialize the events. */
1769 struct lttng_payload_view events_view
=
1770 lttng_payload_view_from_view(payload
, 0, -1);
1772 ret
= event_list_create_from_payload(
1773 &events_view
, count
, &local_events
);
1774 if (ret
!= LTTNG_OK
) {
1779 ret
= flatten_lttng_events(&local_events
, events
);
1780 if (ret
!= LTTNG_OK
) {
1785 lttng_dynamic_pointer_array_reset(&local_events
);
1789 static enum lttng_error_code
flatten_lttng_event_fields(
1790 struct lttng_dynamic_pointer_array
*event_fields
,
1791 struct lttng_event_field
**flattened_event_fields
)
1794 enum lttng_error_code ret_code
;
1795 size_t storage_req
= 0;
1796 struct lttng_dynamic_buffer local_flattened_event_fields
;
1799 assert(event_fields
);
1800 assert(flattened_event_fields
);
1802 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1803 nb_event_field
= lttng_dynamic_pointer_array_get_count(event_fields
);
1806 * Here even if the event field contains a `struct lttng_event` that
1807 * could contain dynamic data, in reality it is not the case.
1808 * Dynamic data is not present. Here the flattening is mostly a direct
1809 * memcpy. This is less than ideal but this code is still better than
1810 * direct usage of an unpacked lttng_event_field array.
1812 storage_req
+= sizeof(struct lttng_event_field
) * nb_event_field
;
1814 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1817 * We must ensure that "local_flattened_event_fields" is never resized
1818 * so as to preserve the validity of the flattened objects.
1820 ret
= lttng_dynamic_buffer_set_capacity(
1821 &local_flattened_event_fields
, storage_req
);
1823 ret_code
= LTTNG_ERR_NOMEM
;
1827 for (i
= 0; i
< nb_event_field
; i
++) {
1828 const struct lttng_event_field
*element
=
1829 (const struct lttng_event_field
*)
1830 lttng_dynamic_pointer_array_get_pointer(
1834 ret_code
= LTTNG_ERR_FATAL
;
1837 ret
= lttng_dynamic_buffer_append(&local_flattened_event_fields
,
1838 element
, sizeof(struct lttng_event_field
));
1840 ret_code
= LTTNG_ERR_NOMEM
;
1845 /* Don't reset local_flattened_channels buffer as we return its content. */
1846 *flattened_event_fields
= (struct lttng_event_field
*) local_flattened_event_fields
.data
;
1847 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1848 ret_code
= LTTNG_OK
;
1850 lttng_dynamic_buffer_reset(&local_flattened_event_fields
);
1854 static enum lttng_error_code
event_field_list_create_from_payload(
1855 struct lttng_payload_view
*view
,
1857 struct lttng_dynamic_pointer_array
**event_field_list
)
1859 enum lttng_error_code ret_code
;
1860 int ret
, offset
= 0;
1862 struct lttng_dynamic_pointer_array
*list
= NULL
;
1865 assert(event_field_list
);
1867 list
= zmalloc
<lttng_dynamic_pointer_array
>();
1869 ret_code
= LTTNG_ERR_NOMEM
;
1873 lttng_dynamic_pointer_array_init(list
, free
);
1875 for (i
= 0; i
< count
; i
++) {
1876 ssize_t event_field_size
;
1877 struct lttng_event_field
*field
= NULL
;
1878 struct lttng_payload_view event_field_view
=
1879 lttng_payload_view_from_view(view
, offset
, -1);
1881 event_field_size
= lttng_event_field_create_from_payload(
1882 &event_field_view
, &field
);
1883 if (event_field_size
< 0) {
1884 ret_code
= LTTNG_ERR_INVALID
;
1888 /* Lifetime and management of the object is now bound to the array. */
1889 ret
= lttng_dynamic_pointer_array_add_pointer(list
, field
);
1892 ret_code
= LTTNG_ERR_NOMEM
;
1896 offset
+= event_field_size
;
1899 if (view
->buffer
.size
!= offset
) {
1900 ret_code
= LTTNG_ERR_INVALID
;
1904 *event_field_list
= list
;
1906 ret_code
= LTTNG_OK
;
1910 lttng_dynamic_pointer_array_reset(list
);
1917 enum lttng_error_code
lttng_event_fields_create_and_flatten_from_payload(
1918 struct lttng_payload_view
*view
,
1920 struct lttng_event_field
**fields
)
1922 enum lttng_error_code ret_code
;
1923 struct lttng_dynamic_pointer_array
*local_event_fields
= NULL
;
1925 ret_code
= event_field_list_create_from_payload(
1926 view
, count
, &local_event_fields
);
1927 if (ret_code
!= LTTNG_OK
) {
1931 ret_code
= flatten_lttng_event_fields(local_event_fields
, fields
);
1932 if (ret_code
!= LTTNG_OK
) {
1936 if (local_event_fields
) {
1937 lttng_dynamic_pointer_array_reset(local_event_fields
);
1938 free(local_event_fields
);