2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include "common/compat/string.h"
9 #include "common/macros.h"
10 #include "lttng/lttng-error.h"
12 #include <common/buffer-view.h>
13 #include <common/dynamic-array.h>
14 #include <common/dynamic-buffer.h>
15 #include <common/error.h>
16 #include <common/sessiond-comm/sessiond-comm.h>
17 #include <common/align.h>
18 #include <lttng/constant.h>
19 #include <lttng/event-internal.h>
20 #include <lttng/event.h>
21 #include <lttng/userspace-probe-internal.h>
25 struct event_list_element
{
26 struct lttng_event
*event
;
27 struct lttng_event_exclusion
*exclusions
;
28 char *filter_expression
;
31 static void event_list_destructor(void *ptr
)
33 struct event_list_element
*element
= (struct event_list_element
*) ptr
;
35 free(element
->filter_expression
);
36 free(element
->exclusions
);
37 lttng_event_destroy(element
->event
);
41 struct lttng_event
*lttng_event_copy(const struct lttng_event
*event
)
43 struct lttng_event
*new_event
;
44 struct lttng_event_extended
*new_event_extended
;
46 new_event
= (lttng_event
*) zmalloc(sizeof(*event
));
48 PERROR("Error allocating event structure");
52 /* Copy the content of the old event. */
53 memcpy(new_event
, event
, sizeof(*event
));
56 * We need to create a new extended since the previous pointer is now
59 new_event_extended
= (lttng_event_extended
*) zmalloc(sizeof(*new_event_extended
));
60 if (!new_event_extended
) {
61 PERROR("Error allocating event extended structure");
65 new_event
->extended
.ptr
= new_event_extended
;
74 static int lttng_event_probe_attr_serialize(
75 const struct lttng_event_probe_attr
*probe
,
76 struct lttng_payload
*payload
)
79 size_t symbol_name_len
;
80 struct lttng_event_probe_attr_comm comm
= { 0 };
82 symbol_name_len
= lttng_strnlen(probe
->symbol_name
, LTTNG_SYMBOL_NAME_LEN
);
83 if (symbol_name_len
== LTTNG_SYMBOL_NAME_LEN
) {
84 /* Not null-termintated. */
89 /* Include the null terminator. */
92 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
93 comm
.addr
= probe
->addr
;
94 comm
.offset
= probe
->addr
;
96 ret
= lttng_dynamic_buffer_append(
97 &payload
->buffer
, &comm
, sizeof(comm
));
103 ret
= lttng_dynamic_buffer_append(
104 &payload
->buffer
, probe
->symbol_name
, symbol_name_len
);
109 static int lttng_event_function_attr_serialize(
110 const struct lttng_event_function_attr
*function
,
111 struct lttng_payload
*payload
)
114 size_t symbol_name_len
;
115 struct lttng_event_function_attr_comm comm
= { 0 };
117 symbol_name_len
= lttng_strnlen(function
->symbol_name
, LTTNG_SYMBOL_NAME_LEN
);
118 if (symbol_name_len
== LTTNG_SYMBOL_NAME_LEN
) {
119 /* Not null-termintated. */
124 /* Include the null terminator. */
125 symbol_name_len
+= 1;
127 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
129 ret
= lttng_dynamic_buffer_append(
130 &payload
->buffer
, &comm
, sizeof(comm
));
136 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
137 function
->symbol_name
, symbol_name_len
);
142 static ssize_t
lttng_event_probe_attr_create_from_payload(
143 struct lttng_payload_view
*view
,
144 struct lttng_event_probe_attr
**probe_attr
)
146 ssize_t ret
, offset
= 0;
147 const struct lttng_event_probe_attr_comm
*comm
;
148 struct lttng_event_probe_attr
*local_attr
= NULL
;
149 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
150 view
, offset
, sizeof(*comm
));
152 if (!lttng_payload_view_is_valid(&comm_view
)) {
157 comm
= (typeof(comm
)) comm_view
.buffer
.data
;
158 offset
+= sizeof(*comm
);
160 local_attr
= (struct lttng_event_probe_attr
*) zmalloc(
161 sizeof(*local_attr
));
162 if (local_attr
== NULL
) {
167 local_attr
->addr
= comm
->addr
;
168 local_attr
->offset
= comm
->offset
;
172 struct lttng_payload_view name_view
=
173 lttng_payload_view_from_view(view
, offset
,
174 comm
->symbol_name_len
);
176 if (!lttng_payload_view_is_valid(&name_view
)) {
181 name
= name_view
.buffer
.data
;
183 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
184 comm
->symbol_name_len
)) {
189 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
190 LTTNG_SYMBOL_NAME_LEN
);
196 offset
+= comm
->symbol_name_len
;
199 *probe_attr
= local_attr
;
207 static ssize_t
lttng_event_function_attr_create_from_payload(
208 struct lttng_payload_view
*view
,
209 struct lttng_event_function_attr
**function_attr
)
211 ssize_t ret
, offset
= 0;
212 const struct lttng_event_function_attr_comm
*comm
;
213 struct lttng_event_function_attr
*local_attr
= NULL
;
214 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
215 view
, offset
, sizeof(*comm
));
217 if (!lttng_payload_view_is_valid(&comm_view
)) {
222 comm
= (typeof(comm
)) view
->buffer
.data
;
223 offset
+= sizeof(*comm
);
225 local_attr
= (struct lttng_event_function_attr
*) zmalloc(
226 sizeof(*local_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 LTTNG_SYMBOL_NAME_LEN
);
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 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
= (struct lttng_event_exclusion
*) zmalloc(
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(local_exclusions
->names
[i
],
321 string
, LTTNG_SYMBOL_NAME_LEN
);
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
)) {
410 local_event
->name
, name
, LTTNG_SYMBOL_NAME_LEN
);
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
= (struct lttng_bytecode
*) zmalloc(
514 event_comm
->bytecode_len
);
515 if (!local_bytecode
) {
520 memcpy(local_bytecode
, bytecode_view
.buffer
.data
,
521 event_comm
->bytecode_len
);
522 if ((local_bytecode
->len
+ sizeof(*local_bytecode
)) !=
523 event_comm
->bytecode_len
) {
528 offset
+= event_comm
->bytecode_len
;
531 deserialize_event_type_payload
:
532 /* Event type specific payload */
533 switch (local_event
->type
) {
534 case LTTNG_EVENT_FUNCTION
:
536 case LTTNG_EVENT_PROBE
:
538 struct lttng_payload_view probe_attr_view
=
539 lttng_payload_view_from_view(view
, offset
,
540 event_comm
->lttng_event_probe_attr_len
);
542 if (event_comm
->lttng_event_probe_attr_len
== 0) {
547 if (!lttng_payload_view_is_valid(&probe_attr_view
)) {
552 ret
= lttng_event_probe_attr_create_from_payload(
553 &probe_attr_view
, &local_probe_attr
);
554 if (ret
< 0 || ret
!= event_comm
->lttng_event_probe_attr_len
) {
559 /* Copy to the local event. */
560 memcpy(&local_event
->attr
.probe
, local_probe_attr
,
561 sizeof(local_event
->attr
.probe
));
566 case LTTNG_EVENT_FUNCTION_ENTRY
:
568 struct lttng_payload_view function_attr_view
=
569 lttng_payload_view_from_view(view
, offset
,
570 event_comm
->lttng_event_function_attr_len
);
572 if (event_comm
->lttng_event_function_attr_len
== 0) {
577 if (!lttng_payload_view_is_valid(&function_attr_view
)) {
582 ret
= lttng_event_function_attr_create_from_payload(
583 &function_attr_view
, &local_function_attr
);
584 if (ret
< 0 || ret
!= event_comm
->lttng_event_function_attr_len
) {
589 /* Copy to the local event. */
590 memcpy(&local_event
->attr
.ftrace
, local_function_attr
,
591 sizeof(local_event
->attr
.ftrace
));
597 case LTTNG_EVENT_USERSPACE_PROBE
:
599 struct lttng_payload_view userspace_probe_location_view
=
600 lttng_payload_view_from_view(view
, offset
,
601 event_comm
->userspace_probe_location_len
);
603 if (event_comm
->userspace_probe_location_len
== 0) {
608 if (!lttng_payload_view_is_valid(
609 &userspace_probe_location_view
)) {
614 ret
= lttng_userspace_probe_location_create_from_payload(
615 &userspace_probe_location_view
,
616 &local_userspace_probe_location
);
618 WARN("Failed to create a userspace probe location from the received buffer");
623 if (ret
!= event_comm
->userspace_probe_location_len
) {
624 WARN("Userspace probe location from the received buffer is not the advertised length: header length = %" PRIu32
", payload length = %lu", event_comm
->userspace_probe_location_len
, ret
);
629 /* Attach the probe location to the event. */
630 ret
= lttng_event_set_userspace_probe_location(
631 local_event
, local_userspace_probe_location
);
633 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
638 * Userspace probe location object ownership transfered to the
641 local_userspace_probe_location
= NULL
;
642 offset
+= event_comm
->userspace_probe_location_len
;
645 case LTTNG_EVENT_TRACEPOINT
:
647 case LTTNG_EVENT_ALL
:
649 case LTTNG_EVENT_SYSCALL
:
651 case LTTNG_EVENT_NOOP
:
652 /* Nothing to do here */
660 /* Transfer ownership to the caller. */
661 *out_event
= local_event
;
665 *out_bytecode
= local_bytecode
;
666 local_bytecode
= NULL
;
670 *out_exclusion
= local_exclusions
;
671 local_exclusions
= NULL
;
674 if (out_filter_expression
) {
675 *out_filter_expression
= local_filter_expression
;
676 local_filter_expression
= NULL
;
681 lttng_event_destroy(local_event
);
682 lttng_userspace_probe_location_destroy(local_userspace_probe_location
);
683 free(local_filter_expression
);
684 free(local_exclusions
);
685 free(local_bytecode
);
686 free(local_function_attr
);
687 free(local_probe_attr
);
691 int lttng_event_serialize(const struct lttng_event
*event
,
692 unsigned int exclusion_count
,
693 char **exclusion_list
,
694 char *filter_expression
,
696 struct lttng_bytecode
*bytecode
,
697 struct lttng_payload
*payload
)
701 size_t header_offset
, size_before_payload
;
703 struct lttng_event_comm event_comm
= { 0 };
704 struct lttng_event_comm
*header
;
708 assert(exclusion_count
== 0 || exclusion_list
);
710 /* Save the header location for later in-place header update. */
711 header_offset
= payload
->buffer
.size
;
713 name_len
= lttng_strnlen(event
->name
, LTTNG_SYMBOL_NAME_LEN
);
714 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
715 /* Event name is not NULL-terminated. */
720 /* Add null termination. */
723 if (exclusion_count
> UINT32_MAX
) {
724 /* Possible overflow. */
729 if (bytecode_len
> UINT32_MAX
) {
730 /* Possible overflow. */
735 event_comm
.name_len
= (uint32_t) name_len
;
736 event_comm
.event_type
= (int8_t) event
->type
;
737 event_comm
.loglevel_type
= (int8_t) event
->loglevel_type
;
738 event_comm
.loglevel
= (int32_t) event
->loglevel
;
739 event_comm
.enabled
= (int8_t) event
->enabled
;
740 event_comm
.pid
= (int32_t) event
->pid
;
741 event_comm
.exclusion_count
= (uint32_t) exclusion_count
;
742 event_comm
.bytecode_len
= (uint32_t) bytecode_len
;
743 event_comm
.flags
= (int32_t) event
->flags
;
745 if (filter_expression
) {
746 event_comm
.filter_expression_len
=
747 strlen(filter_expression
) + 1;
751 ret
= lttng_dynamic_buffer_append(
752 &payload
->buffer
, &event_comm
, sizeof(event_comm
));
758 ret
= lttng_dynamic_buffer_append(
759 &payload
->buffer
, event
->name
, name_len
);
765 for (i
= 0; i
< exclusion_count
; i
++) {
766 const size_t exclusion_len
= lttng_strnlen(
767 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
768 const struct lttng_event_exclusion_comm exclusion_header
= {
769 .len
= (uint32_t) exclusion_len
+ 1,
772 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
773 /* Exclusion is not NULL-terminated. */
778 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
779 &exclusion_header
, sizeof(exclusion_header
));
784 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
785 *(exclusion_list
+ i
), exclusion_len
+ 1);
791 /* Filter expression and its bytecode */
792 if (filter_expression
) {
793 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
795 event_comm
.filter_expression_len
);
801 * Bytecode can be absent when we serialize to the client
805 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
806 bytecode
, bytecode_len
);
813 size_before_payload
= payload
->buffer
.size
;
815 /* Event type specific payload */
816 switch (event
->type
) {
817 case LTTNG_EVENT_FUNCTION
:
819 case LTTNG_EVENT_PROBE
:
820 ret
= lttng_event_probe_attr_serialize(&event
->attr
.probe
, payload
);
826 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
828 header
->lttng_event_probe_attr_len
=
829 payload
->buffer
.size
- size_before_payload
;
832 case LTTNG_EVENT_FUNCTION_ENTRY
:
833 ret
= lttng_event_function_attr_serialize(
834 &event
->attr
.ftrace
, payload
);
840 /* Update the lttng_event_function_attr len. */
841 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
843 header
->lttng_event_function_attr_len
=
844 payload
->buffer
.size
- size_before_payload
;
847 case LTTNG_EVENT_USERSPACE_PROBE
:
849 const struct lttng_event_extended
*ev_ext
=
850 (const struct lttng_event_extended
*)
853 assert(event
->extended
.ptr
);
854 assert(ev_ext
->probe_location
);
856 size_before_payload
= payload
->buffer
.size
;
857 if (ev_ext
->probe_location
) {
859 * lttng_userspace_probe_location_serialize returns the
860 * number of bytes that were appended to the buffer.
862 ret
= lttng_userspace_probe_location_serialize(
863 ev_ext
->probe_location
, payload
);
870 /* Update the userspace probe location len. */
871 header
= (struct lttng_event_comm
*) ((char *) payload
->buffer
.data
+
873 header
->userspace_probe_location_len
=
874 payload
->buffer
.size
- size_before_payload
;
878 case LTTNG_EVENT_TRACEPOINT
:
880 case LTTNG_EVENT_ALL
:
883 /* Nothing to do here. */
891 static ssize_t
lttng_event_context_app_populate_from_payload(
892 const struct lttng_payload_view
*view
,
893 struct lttng_event_context
*event_ctx
)
895 ssize_t ret
, offset
= 0;
896 const struct lttng_event_context_app_comm
*comm
;
897 char *provider_name
= NULL
, *context_name
= NULL
;
898 size_t provider_name_len
, context_name_len
;
899 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
900 &view
->buffer
, offset
, sizeof(*comm
));
902 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
904 if (!lttng_buffer_view_is_valid(&comm_view
)) {
909 comm
= (typeof(comm
)) comm_view
.data
;
910 offset
+= sizeof(*comm
);
912 provider_name_len
= comm
->provider_name_len
;
913 context_name_len
= comm
->ctx_name_len
;
915 if (provider_name_len
== 0 || context_name_len
== 0) {
917 * Application provider and context names MUST
926 const struct lttng_buffer_view provider_name_view
=
927 lttng_buffer_view_from_view(&view
->buffer
,
931 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
936 name
= provider_name_view
.data
;
938 if (!lttng_buffer_view_contains_string(&provider_name_view
,
939 name
, provider_name_len
)) {
944 provider_name
= lttng_strndup(name
, provider_name_len
);
945 if (!provider_name
) {
950 offset
+= provider_name_len
;
955 const struct lttng_buffer_view context_name_view
=
956 lttng_buffer_view_from_view(
957 &view
->buffer
, offset
,
960 if (!lttng_buffer_view_is_valid(&context_name_view
)) {
965 name
= context_name_view
.data
;
967 if (!lttng_buffer_view_contains_string(&context_name_view
, name
,
973 context_name
= lttng_strndup(name
, context_name_len
);
979 offset
+= context_name_len
;
982 /* Transfer ownership of the strings */
983 event_ctx
->u
.app_ctx
.provider_name
= provider_name
;
984 event_ctx
->u
.app_ctx
.ctx_name
= context_name
;
985 provider_name
= NULL
;
996 static ssize_t
lttng_event_context_perf_counter_populate_from_payload(
997 const struct lttng_payload_view
*view
,
998 struct lttng_event_context
*event_ctx
)
1000 ssize_t ret
, 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 lttng_strncpy(event_ctx
->u
.perf_counter
.name
, name
, name_len
);
1045 event_ctx
->u
.perf_counter
.config
= comm
->config
;
1046 event_ctx
->u
.perf_counter
.type
= comm
->type
;
1054 ssize_t
lttng_event_context_create_from_payload(
1055 struct lttng_payload_view
*view
,
1056 struct lttng_event_context
**event_ctx
)
1058 ssize_t ret
, offset
= 0;
1059 const struct lttng_event_context_comm
*comm
;
1060 struct lttng_event_context
*local_context
= NULL
;
1061 struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1062 &view
->buffer
, offset
, sizeof(*comm
));
1067 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1072 comm
= (typeof(comm
)) comm_view
.data
;
1073 offset
+= sizeof(*comm
);
1075 local_context
= (struct lttng_event_context
*)
1076 zmalloc(sizeof(*local_context
));
1077 if (!local_context
) {
1082 local_context
->ctx
= (lttng_event_context_type
) comm
->type
;
1085 struct lttng_payload_view subtype_view
=
1086 lttng_payload_view_from_view(view
, offset
, -1);
1088 switch (local_context
->ctx
) {
1089 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1090 ret
= lttng_event_context_app_populate_from_payload(
1091 &subtype_view
, local_context
);
1093 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1094 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1095 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1096 ret
= lttng_event_context_perf_counter_populate_from_payload(
1097 &subtype_view
, local_context
);
1100 /* Nothing else to deserialize. */
1112 *event_ctx
= local_context
;
1113 local_context
= NULL
;
1117 free(local_context
);
1121 static int lttng_event_context_app_serialize(
1122 struct lttng_event_context
*context
,
1123 struct lttng_payload
*payload
)
1126 struct lttng_event_context_app_comm comm
= { 0 };
1127 size_t provider_len
, ctx_len
;
1128 const char *provider_name
;
1129 const char *ctx_name
;
1133 assert(context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1135 provider_name
= context
->u
.app_ctx
.provider_name
;
1136 ctx_name
= context
->u
.app_ctx
.ctx_name
;
1138 if (!provider_name
|| !ctx_name
) {
1139 ret
= -LTTNG_ERR_INVALID
;
1143 provider_len
= strlen(provider_name
);
1144 if (provider_len
== 0) {
1145 ret
= -LTTNG_ERR_INVALID
;
1149 /* Include the null terminator. */
1151 comm
.provider_name_len
= provider_len
;
1153 ctx_len
= strlen(ctx_name
);
1155 ret
= -LTTNG_ERR_INVALID
;
1159 /* Include the null terminator. */
1161 comm
.ctx_name_len
= ctx_len
;
1164 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1171 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, provider_name
,
1178 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, ctx_name
,
1189 static int lttng_event_context_perf_counter_serialize(
1190 struct lttng_event_perf_counter_ctx
*context
,
1191 struct lttng_payload
*payload
)
1194 struct lttng_event_context_perf_counter_comm comm
= { 0 };
1199 comm
.config
= context
->config
;
1200 comm
.type
= context
->type
;
1201 comm
.name_len
= lttng_strnlen(context
->name
, LTTNG_SYMBOL_NAME_LEN
);
1203 if (comm
.name_len
== LTTNG_SYMBOL_NAME_LEN
) {
1208 /* Include the null terminator. */
1212 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
1219 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, context
->name
,
1230 int lttng_event_context_serialize(struct lttng_event_context
*context
,
1231 struct lttng_payload
*payload
)
1234 struct lttng_event_context_comm context_comm
= { 0 };
1239 context_comm
.type
= (uint32_t) context
->ctx
;
1242 ret
= lttng_dynamic_buffer_append(
1243 &payload
->buffer
, &context_comm
, sizeof(context_comm
));
1248 switch (context
->ctx
) {
1249 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1250 ret
= lttng_event_context_app_serialize(context
, payload
);
1252 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1253 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1254 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1255 ret
= lttng_event_context_perf_counter_serialize(
1256 &context
->u
.perf_counter
, payload
);
1259 /* Nothing else to serialize. */
1271 void lttng_event_context_destroy(struct lttng_event_context
*context
)
1277 if (context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1278 free(context
->u
.app_ctx
.provider_name
);
1279 free(context
->u
.app_ctx
.ctx_name
);
1286 * This is a specialized populate for lttng_event_field since it ignores
1287 * the extension field of the lttng_event struct and simply copies what it can
1288 * to the internal struct lttng_event of a lttng_event_field.
1290 static void lttng_event_field_populate_lttng_event_from_event(
1291 const struct lttng_event
*src
, struct lttng_event
*destination
)
1293 memcpy(destination
, src
, sizeof(*destination
));
1295 /* Remove all possible dynamic data from the destination event rule. */
1296 destination
->extended
.ptr
= NULL
;
1299 ssize_t
lttng_event_field_create_from_payload(
1300 struct lttng_payload_view
*view
,
1301 struct lttng_event_field
**field
)
1303 ssize_t ret
, offset
= 0;
1304 struct lttng_event_field
*local_event_field
= NULL
;
1305 struct lttng_event
*event
= NULL
;
1306 const struct lttng_event_field_comm
*comm
;
1307 const char* name
= NULL
;
1313 const struct lttng_buffer_view comm_view
=
1314 lttng_buffer_view_from_view(
1315 &view
->buffer
, offset
,
1318 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1323 /* lttng_event_field_comm header */
1324 comm
= (const lttng_event_field_comm
*) comm_view
.data
;
1325 offset
+= sizeof(*comm
);
1328 local_event_field
= (struct lttng_event_field
*)
1329 zmalloc(sizeof(*local_event_field
));
1330 if (!local_event_field
) {
1335 local_event_field
->type
= (lttng_event_field_type
) comm
->type
;
1336 local_event_field
->nowrite
= comm
->nowrite
;
1340 const struct lttng_buffer_view name_view
=
1341 lttng_buffer_view_from_view(
1342 &view
->buffer
, offset
,
1345 if (!lttng_buffer_view_is_valid(&name_view
)) {
1350 name
= name_view
.data
;
1352 if (!lttng_buffer_view_contains_string(&name_view
,
1353 name_view
.data
, comm
->name_len
)) {
1358 if (comm
->name_len
> LTTNG_SYMBOL_NAME_LEN
- 1) {
1359 /* Name is too long.*/
1364 offset
+= comm
->name_len
;
1369 struct lttng_payload_view event_view
=
1370 lttng_payload_view_from_view(
1374 if (!lttng_payload_view_is_valid(&event_view
)) {
1379 ret
= lttng_event_create_from_payload(&event_view
, &event
, NULL
,
1381 if (ret
!= comm
->event_len
) {
1392 if (lttng_strncpy(local_event_field
->field_name
, name
, LTTNG_SYMBOL_NAME_LEN
)) {
1397 lttng_event_field_populate_lttng_event_from_event(
1398 event
, &local_event_field
->event
);
1400 *field
= local_event_field
;
1401 local_event_field
= NULL
;
1404 lttng_event_destroy(event
);
1405 free(local_event_field
);
1409 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1410 struct lttng_payload
*payload
)
1413 size_t header_offset
, size_before_event
;
1415 struct lttng_event_field_comm event_field_comm
= { 0 };
1416 struct lttng_event_field_comm
*header
;
1421 /* Save the header location for later in-place header update. */
1422 header_offset
= payload
->buffer
.size
;
1424 name_len
= strnlen(field
->field_name
, LTTNG_SYMBOL_NAME_LEN
);
1425 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
1426 /* Event name is not NULL-terminated. */
1431 /* Add null termination. */
1434 event_field_comm
.type
= field
->type
;
1435 event_field_comm
.nowrite
= (uint8_t)field
->nowrite
;
1436 event_field_comm
.name_len
= name_len
;
1439 ret
= lttng_dynamic_buffer_append(
1440 &payload
->buffer
, &event_field_comm
,
1441 sizeof(event_field_comm
));
1447 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, field
->field_name
,
1453 size_before_event
= payload
->buffer
.size
;
1454 ret
= lttng_event_serialize(
1455 &field
->event
, 0, NULL
, NULL
, 0, 0, payload
);
1461 /* Update the event len. */
1462 header
= (struct lttng_event_field_comm
*)
1463 ((char *) payload
->buffer
.data
+
1465 header
->event_len
= payload
->buffer
.size
- size_before_event
;
1471 static enum lttng_error_code
compute_flattened_size(
1472 struct lttng_dynamic_pointer_array
*events
, size_t *size
)
1474 enum lttng_error_code ret_code
;
1476 size_t storage_req
, event_count
, i
;
1481 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1483 /* The basic struct lttng_event */
1484 storage_req
= event_count
* sizeof(struct lttng_event
);
1486 for (i
= 0; i
< event_count
; i
++) {
1487 int probe_storage_req
= 0;
1488 const struct event_list_element
*element
= (const struct event_list_element
*)
1489 lttng_dynamic_pointer_array_get_pointer(
1491 const struct lttng_userspace_probe_location
*location
= NULL
;
1493 location
= lttng_event_get_userspace_probe_location(
1496 ret
= lttng_userspace_probe_location_flatten(
1499 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1503 probe_storage_req
= ret
;
1506 /* The struct·lttng_event_extended */
1507 storage_req
+= event_count
*
1508 sizeof(struct lttng_event_extended
);
1510 if (element
->filter_expression
) {
1511 storage_req
+= strlen(element
->filter_expression
) + 1;
1514 if (element
->exclusions
) {
1515 storage_req
+= element
->exclusions
->count
*
1516 LTTNG_SYMBOL_NAME_LEN
;
1519 /* Padding to ensure the flat probe is aligned. */
1520 storage_req
= lttng_align_ceil(storage_req
, sizeof(uint64_t));
1521 storage_req
+= probe_storage_req
;
1524 *size
= storage_req
;
1525 ret_code
= LTTNG_OK
;
1532 * Flatten a list of struct lttng_event.
1534 * The buffer that is returned to the API client must contain a "flat" version
1535 * of the events that are returned. In other words, all pointers within an
1536 * lttng_event must point to a location within the returned buffer so that the
1537 * user may free everything by simply calling free() on the returned buffer.
1538 * This is needed in order to maintain API compatibility.
1540 * A first pass is performed to compute the size of the buffer that must be
1541 * allocated. A second pass is then performed to setup the returned events so
1542 * that their members always point within the buffer.
1544 * The layout of the returned buffer is as follows:
1545 * - struct lttng_event[nb_events],
1546 * - nb_events times the following:
1547 * - struct lttng_event_extended,
1548 * - filter_expression
1550 * - padding to align to 64-bits
1551 * - flattened version of userspace_probe_location
1553 static enum lttng_error_code
flatten_lttng_events(
1554 struct lttng_dynamic_pointer_array
*events
,
1555 struct lttng_event
**flattened_events
)
1557 enum lttng_error_code ret_code
;
1560 struct lttng_dynamic_buffer local_flattened_events
;
1564 assert(flattened_events
);
1566 lttng_dynamic_buffer_init(&local_flattened_events
);
1567 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1569 ret_code
= compute_flattened_size(events
, &storage_req
);
1570 if (ret_code
!= LTTNG_OK
) {
1575 * We must ensure that "local_flattened_events" is never resized so as
1576 * to preserve the validity of the flattened objects.
1578 ret
= lttng_dynamic_buffer_set_capacity(
1579 &local_flattened_events
, storage_req
);
1581 ret_code
= LTTNG_ERR_NOMEM
;
1585 /* Start by laying the struct lttng_event */
1586 for (i
= 0; i
< nb_events
; i
++) {
1587 const struct event_list_element
*element
= (const struct event_list_element
*)
1588 lttng_dynamic_pointer_array_get_pointer(
1592 ret_code
= LTTNG_ERR_FATAL
;
1596 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1597 element
->event
, sizeof(struct lttng_event
));
1599 ret_code
= LTTNG_ERR_NOMEM
;
1604 for (i
= 0; i
< nb_events
; i
++) {
1605 const struct event_list_element
*element
= (const struct event_list_element
*)
1606 lttng_dynamic_pointer_array_get_pointer(events
, i
);
1607 struct lttng_event
*event
= (struct lttng_event
*)
1608 (local_flattened_events
.data
+ (sizeof(struct lttng_event
) * i
));
1609 struct lttng_event_extended
*event_extended
=
1610 (struct lttng_event_extended
*)
1611 (local_flattened_events
.data
+ local_flattened_events
.size
);
1612 const struct lttng_userspace_probe_location
*location
= NULL
;
1616 /* Insert struct lttng_event_extended. */
1617 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1618 local_flattened_events
.size
+
1619 sizeof(*event_extended
));
1621 ret_code
= LTTNG_ERR_NOMEM
;
1624 event
->extended
.ptr
= event_extended
;
1626 /* Insert filter expression. */
1627 if (element
->filter_expression
) {
1628 const size_t len
= strlen(element
->filter_expression
) + 1;
1630 event_extended
->filter_expression
=
1631 local_flattened_events
.data
+
1632 local_flattened_events
.size
;
1633 ret
= lttng_dynamic_buffer_append(
1634 &local_flattened_events
,
1635 element
->filter_expression
, len
);
1637 ret_code
= LTTNG_ERR_NOMEM
;
1642 /* Insert exclusions. */
1643 if (element
->exclusions
) {
1644 event_extended
->exclusions
.count
=
1645 element
->exclusions
->count
;
1646 event_extended
->exclusions
.strings
=
1647 local_flattened_events
.data
+
1648 local_flattened_events
.size
;
1650 ret
= lttng_dynamic_buffer_append(
1651 &local_flattened_events
,
1652 element
->exclusions
->names
,
1653 element
->exclusions
->count
*
1654 LTTNG_SYMBOL_NAME_LEN
);
1656 ret_code
= LTTNG_ERR_NOMEM
;
1661 /* Insert padding to align to 64-bits. */
1662 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1663 lttng_align_ceil(local_flattened_events
.size
,
1666 ret_code
= LTTNG_ERR_NOMEM
;
1670 location
= lttng_event_get_userspace_probe_location(
1673 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
1674 (local_flattened_events
.data
+ local_flattened_events
.size
);
1675 ret
= lttng_userspace_probe_location_flatten(
1676 location
, &local_flattened_events
);
1678 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1684 /* Don't reset local_flattened_events buffer as we return its content. */
1685 *flattened_events
= (struct lttng_event
*) local_flattened_events
.data
;
1686 lttng_dynamic_buffer_init(&local_flattened_events
);
1687 ret_code
= LTTNG_OK
;
1689 lttng_dynamic_buffer_reset(&local_flattened_events
);
1693 static enum lttng_error_code
event_list_create_from_payload(
1694 struct lttng_payload_view
*view
,
1696 struct lttng_dynamic_pointer_array
*event_list
)
1698 enum lttng_error_code ret_code
;
1706 for (i
= 0; i
< count
; i
++) {
1708 struct lttng_payload_view event_view
=
1709 lttng_payload_view_from_view(view
, offset
, -1);
1710 struct event_list_element
*element
=
1711 (struct event_list_element
*) zmalloc(sizeof(*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
= (struct lttng_dynamic_pointer_array
*) zmalloc(sizeof(*list
));
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
);