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 <lttng/constant.h>
18 #include <lttng/event-internal.h>
19 #include <lttng/event.h>
20 #include <lttng/userspace-probe-internal.h>
24 struct event_list_element
{
25 struct lttng_event
*event
;
26 struct lttng_event_exclusion
*exclusions
;
27 char *filter_expression
;
30 static void event_list_destructor(void *ptr
)
32 struct event_list_element
*element
= (struct event_list_element
*) ptr
;
34 free(element
->filter_expression
);
35 free(element
->exclusions
);
36 lttng_event_destroy(element
->event
);
41 struct lttng_event
*lttng_event_copy(const struct lttng_event
*event
)
43 struct lttng_event
*new_event
;
44 struct lttng_event_extended
*new_event_extended
;
46 new_event
= zmalloc(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
= 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_dynamic_buffer
*buf
)
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(buf
, &comm
, sizeof(comm
));
102 ret
= lttng_dynamic_buffer_append(buf
, 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_dynamic_buffer
*buf
)
113 size_t symbol_name_len
;
114 struct lttng_event_function_attr_comm comm
= { 0 };
116 symbol_name_len
= lttng_strnlen(function
->symbol_name
, LTTNG_SYMBOL_NAME_LEN
);
117 if (symbol_name_len
== LTTNG_SYMBOL_NAME_LEN
) {
118 /* Not null-termintated. */
123 /* Include the null terminator. */
124 symbol_name_len
+= 1;
126 comm
.symbol_name_len
= (uint32_t) symbol_name_len
;
128 ret
= lttng_dynamic_buffer_append(buf
, &comm
, sizeof(comm
));
134 ret
= lttng_dynamic_buffer_append(buf
, function
->symbol_name
, symbol_name_len
);
139 static ssize_t
lttng_event_probe_attr_create_from_buffer(
140 const struct lttng_buffer_view
*view
,
141 struct lttng_event_probe_attr
**probe_attr
)
143 ssize_t ret
, offset
= 0;
144 const struct lttng_event_probe_attr_comm
*comm
;
145 struct lttng_event_probe_attr
*local_attr
= NULL
;
146 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
147 view
, offset
, sizeof(*comm
));
149 if (!lttng_buffer_view_is_valid(&comm_view
)) {
154 comm
= (typeof(comm
)) view
->data
;
155 offset
+= sizeof(*comm
);
157 local_attr
= zmalloc(sizeof(*local_attr
));
158 if (local_attr
== NULL
) {
163 local_attr
->addr
= comm
->addr
;
164 local_attr
->offset
= comm
->offset
;
168 const struct lttng_buffer_view name_view
=
169 lttng_buffer_view_from_view(view
, offset
,
170 comm
->symbol_name_len
);
171 if (!lttng_buffer_view_is_valid(&name_view
)) {
176 name
= name_view
.data
;
178 if (!lttng_buffer_view_contains_string(
179 &name_view
, name
, comm
->symbol_name_len
)) {
184 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
185 LTTNG_SYMBOL_NAME_LEN
);
190 offset
+= comm
->symbol_name_len
;
193 *probe_attr
= local_attr
;
201 static ssize_t
lttng_event_function_attr_create_from_buffer(
202 const struct lttng_buffer_view
*view
,
203 struct lttng_event_function_attr
**function_attr
)
205 ssize_t ret
, offset
= 0;
206 const struct lttng_event_function_attr_comm
*comm
;
207 struct lttng_event_function_attr
*local_attr
= NULL
;
208 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
209 view
, offset
, sizeof(*comm
));
211 if (!lttng_buffer_view_is_valid(&comm_view
)) {
216 comm
= (typeof(comm
)) view
->data
;
217 offset
+= sizeof(*comm
);
219 local_attr
= zmalloc(sizeof(*local_attr
));
220 if (local_attr
== NULL
) {
227 const struct lttng_buffer_view name_view
=
228 lttng_buffer_view_from_view(view
, offset
,
229 comm
->symbol_name_len
);
230 if (!lttng_buffer_view_is_valid(&name_view
)) {
235 name
= name_view
.data
;
237 if (!lttng_buffer_view_contains_string(
238 &name_view
, name
, comm
->symbol_name_len
)) {
243 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
244 LTTNG_SYMBOL_NAME_LEN
);
249 offset
+= comm
->symbol_name_len
;
252 *function_attr
= local_attr
;
260 static ssize_t
lttng_event_exclusions_create_from_buffer(const struct lttng_buffer_view
*view
,
261 uint32_t count
, struct lttng_event_exclusion
**exclusions
)
263 ssize_t ret
, offset
= 0;
264 size_t size
= (count
* LTTNG_SYMBOL_NAME_LEN
);
266 const struct lttng_event_exclusion_comm
*comm
;
267 struct lttng_event_exclusion
*local_exclusions
;
270 local_exclusions
= zmalloc(sizeof(struct lttng_event_exclusion
) + size
);
271 if (!local_exclusions
) {
275 local_exclusions
->count
= count
;
277 for (i
= 0; i
< count
; i
++) {
279 struct lttng_buffer_view string_view
;
280 const struct lttng_buffer_view comm_view
=
281 lttng_buffer_view_from_view(
282 view
, offset
, sizeof(*comm
));
284 if (!lttng_buffer_view_is_valid(&comm_view
)) {
289 comm
= (typeof(comm
)) comm_view
.data
;
290 offset
+= sizeof(*comm
);
292 string_view
= lttng_buffer_view_from_view(
293 view
, offset
, comm
->len
);
295 if (!lttng_buffer_view_is_valid(&string_view
)) {
300 string
= string_view
.data
;
302 if (!lttng_buffer_view_contains_string(
303 &string_view
, string
, comm
->len
)) {
309 local_exclusions
->names
[i
],
318 *exclusions
= local_exclusions
;
319 local_exclusions
= NULL
;
322 free(local_exclusions
);
328 ssize_t
lttng_event_create_from_buffer(const struct lttng_buffer_view
*view
,
329 struct lttng_event
**event
,
330 struct lttng_event_exclusion
**exclusion
,
331 char **filter_expression
,
332 struct lttng_filter_bytecode
**bytecode
,
335 ssize_t ret
, offset
= 0;
336 struct lttng_event
*local_event
= NULL
;
337 struct lttng_event_exclusion
*local_exclusions
= NULL
;
338 struct lttng_filter_bytecode
*local_bytecode
= NULL
;
339 char *local_filter_expression
= NULL
;
340 const struct lttng_event_comm
*event_comm
;
341 struct lttng_event_function_attr
*local_function_attr
= NULL
;
342 struct lttng_event_probe_attr
*local_probe_attr
= NULL
;
343 struct lttng_userspace_probe_location
*local_userspace_probe_location
=
345 int received_fd
= -1;
348 * Only event is obligatory, the other output argument are optional and
349 * depends on what the caller is interested in.
355 const struct lttng_buffer_view comm_view
=
356 lttng_buffer_view_from_view(view
, offset
,
357 sizeof(*event_comm
));
359 if (!lttng_buffer_view_is_valid(&comm_view
)) {
364 /* lttng_event_comm header */
365 event_comm
= (typeof(event_comm
)) comm_view
.data
;
366 offset
+= sizeof(*event_comm
);
369 local_event
= lttng_event_create();
370 if (local_event
== NULL
) {
375 local_event
->type
= event_comm
->event_type
;
376 local_event
->loglevel_type
= event_comm
->loglevel_type
;
377 local_event
->loglevel
= event_comm
->loglevel
;
378 local_event
->enabled
= event_comm
->enabled
;
379 local_event
->pid
= event_comm
->pid
;
380 local_event
->flags
= event_comm
->flags
;
384 const struct lttng_buffer_view name_view
=
385 lttng_buffer_view_from_view(view
, offset
,
386 event_comm
->name_len
);
387 if (!lttng_buffer_view_is_valid(&name_view
)) {
392 name
= name_view
.data
;
394 if (!lttng_buffer_view_contains_string(
395 &name_view
, name
, event_comm
->name_len
)) {
401 local_event
->name
, name
, LTTNG_SYMBOL_NAME_LEN
);
406 offset
+= event_comm
->name_len
;
410 if (event_comm
->exclusion_count
== 0) {
411 goto deserialize_filter_expression
;
416 const struct lttng_buffer_view exclusions_view
=
417 lttng_buffer_view_from_view(
420 if (!lttng_buffer_view_is_valid(&exclusions_view
)) {
425 ret
= lttng_event_exclusions_create_from_buffer(&exclusions_view
,
426 event_comm
->exclusion_count
, &local_exclusions
);
433 local_event
->exclusion
= 1;
436 deserialize_filter_expression
:
438 if (event_comm
->filter_expression_len
== 0) {
439 if (event_comm
->bytecode_len
!= 0) {
441 * This is an invalid event payload.
443 * Filter expression without bytecode is possible but
444 * not the other way around.
449 goto deserialize_event_type_payload
;
453 const char *filter_expression_buffer
;
454 const struct lttng_buffer_view filter_expression_view
=
455 lttng_buffer_view_from_view(view
, offset
,
456 event_comm
->filter_expression_len
);
458 if (!lttng_buffer_view_is_valid(&filter_expression_view
)) {
463 filter_expression_buffer
= filter_expression_view
.data
;
465 if (!lttng_buffer_view_contains_string(&filter_expression_view
,
466 filter_expression_buffer
,
467 event_comm
->filter_expression_len
)) {
472 local_filter_expression
= lttng_strndup(
473 filter_expression_buffer
,
474 event_comm
->filter_expression_len
);
475 if (!local_filter_expression
) {
480 local_event
->filter
= 1;
482 offset
+= event_comm
->filter_expression_len
;
485 if (event_comm
->bytecode_len
== 0) {
487 * Filter expression can be present but without bytecode
488 * when dealing with event listing.
490 goto deserialize_event_type_payload
;
495 const struct lttng_buffer_view bytecode_view
=
496 lttng_buffer_view_from_view(view
, offset
,
497 event_comm
->bytecode_len
);
499 if (!lttng_buffer_view_is_valid(&bytecode_view
)) {
504 local_bytecode
= zmalloc(event_comm
->bytecode_len
);
505 if (!local_bytecode
) {
510 memcpy(local_bytecode
, bytecode_view
.data
,
511 event_comm
->bytecode_len
);
512 if ((local_bytecode
->len
+ sizeof(*local_bytecode
)) !=
513 event_comm
->bytecode_len
) {
518 offset
+= event_comm
->bytecode_len
;
521 deserialize_event_type_payload
:
522 /* Event type specific payload */
523 switch (local_event
->type
) {
524 case LTTNG_EVENT_FUNCTION
:
526 case LTTNG_EVENT_PROBE
:
528 const struct lttng_buffer_view probe_attr_view
=
529 lttng_buffer_view_from_view(view
, offset
,
530 event_comm
->lttng_event_probe_attr_len
);
532 if (event_comm
->lttng_event_probe_attr_len
== 0) {
537 if (!lttng_buffer_view_is_valid(&probe_attr_view
)) {
542 ret
= lttng_event_probe_attr_create_from_buffer(
543 &probe_attr_view
, &local_probe_attr
);
544 if (ret
< 0 || ret
!= event_comm
->lttng_event_probe_attr_len
) {
549 /* Copy to the local event. */
550 memcpy(&local_event
->attr
.probe
, local_probe_attr
,
551 sizeof(local_event
->attr
.probe
));
556 case LTTNG_EVENT_FUNCTION_ENTRY
:
558 const struct lttng_buffer_view function_attr_view
=
559 lttng_buffer_view_from_view(view
, offset
,
560 event_comm
->lttng_event_function_attr_len
);
562 if (event_comm
->lttng_event_function_attr_len
== 0) {
567 if (!lttng_buffer_view_is_valid(&function_attr_view
)) {
572 ret
= lttng_event_function_attr_create_from_buffer(
573 &function_attr_view
, &local_function_attr
);
574 if (ret
< 0 || ret
!= event_comm
->lttng_event_function_attr_len
) {
579 /* Copy to the local event. */
580 memcpy(&local_event
->attr
.ftrace
, local_function_attr
,
581 sizeof(local_event
->attr
.ftrace
));
587 case LTTNG_EVENT_USERSPACE_PROBE
:
589 const struct lttng_buffer_view userspace_probe_location_view
=
590 lttng_buffer_view_from_view(view
, offset
,
591 event_comm
->userspace_probe_location_len
);
592 const struct lttng_userspace_probe_location_lookup_method
595 if (event_comm
->userspace_probe_location_len
== 0) {
600 if (!lttng_buffer_view_is_valid(
601 &userspace_probe_location_view
)) {
606 ret
= lttng_userspace_probe_location_create_from_buffer(
607 &userspace_probe_location_view
,
608 &local_userspace_probe_location
);
610 WARN("Failed to create a userspace probe location from the received buffer");
615 if (ret
!= event_comm
->userspace_probe_location_len
) {
616 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
);
623 * The userspace FD is simply not sent and we do not
624 * care about it. This happens on listing.
626 goto attach_userspace_probe_to_event
;
630 * Receive the file descriptor to the target binary from the
633 DBG("Receiving userspace probe target FD from client ...");
634 ret
= lttcomm_recv_fds_unix_sock(sock
, &received_fd
, 1);
636 DBG("Nothing recv() from client userspace probe fd... continuing");
641 * Set the file descriptor received from the client through the
642 * unix socket in the probe location.
644 lookup
= lttng_userspace_probe_location_get_lookup_method(
645 local_userspace_probe_location
);
647 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
652 * From the kernel tracer's perspective, all userspace probe
653 * event types are all the same: a file and an offset.
655 switch (lttng_userspace_probe_location_lookup_method_get_type(
657 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
658 ret
= lttng_userspace_probe_location_function_set_binary_fd(
659 local_userspace_probe_location
,
662 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
663 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
664 local_userspace_probe_location
,
668 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
673 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
677 /* Fd transfered to the object. */
680 attach_userspace_probe_to_event
:
682 /* Attach the probe location to the event. */
683 ret
= lttng_event_set_userspace_probe_location(
684 local_event
, local_userspace_probe_location
);
686 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
691 * Userspace probe location object ownership transfered to the
694 local_userspace_probe_location
= NULL
;
695 offset
+= event_comm
->userspace_probe_location_len
;
698 case LTTNG_EVENT_TRACEPOINT
:
700 case LTTNG_EVENT_ALL
:
702 case LTTNG_EVENT_SYSCALL
:
704 case LTTNG_EVENT_NOOP
:
705 /* Nothing to do here */
713 /* Transfer ownership to the caller. */
714 *event
= local_event
;
718 *bytecode
= local_bytecode
;
719 local_bytecode
= NULL
;
723 *exclusion
= local_exclusions
;
724 local_exclusions
= NULL
;
727 if (filter_expression
) {
728 *filter_expression
= local_filter_expression
;
729 local_filter_expression
= NULL
;
734 lttng_event_destroy(local_event
);
735 lttng_userspace_probe_location_destroy(local_userspace_probe_location
);
736 free(local_filter_expression
);
737 free(local_exclusions
);
738 free(local_bytecode
);
739 free(local_function_attr
);
740 free(local_probe_attr
);
741 if (received_fd
> -1) {
742 if (close(received_fd
)) {
743 PERROR("Failed to close received fd");
750 int lttng_event_serialize(const struct lttng_event
*event
,
751 unsigned int exclusion_count
,
752 char **exclusion_list
,
753 char *filter_expression
,
756 struct lttng_dynamic_buffer
*buf
,
761 size_t header_offset
, size_before_payload
;
763 struct lttng_event_comm event_comm
= { 0 };
764 struct lttng_event_comm
*header
;
769 /* Save the header location for later in-place header update. */
770 header_offset
= buf
->size
;
772 name_len
= lttng_strnlen(event
->name
, LTTNG_SYMBOL_NAME_LEN
);
773 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
774 /* Event name is not NULL-terminated. */
779 /* Add null termination. */
782 if (exclusion_count
> UINT32_MAX
) {
783 /* Possible overflow. */
788 if (bytecode_len
> UINT32_MAX
) {
789 /* Possible overflow. */
794 event_comm
.name_len
= (uint32_t) name_len
;
795 event_comm
.event_type
= (int8_t) event
->type
;
796 event_comm
.loglevel_type
= (int8_t) event
->loglevel_type
;
797 event_comm
.loglevel
= (int32_t) event
->loglevel
;
798 event_comm
.enabled
= (int8_t) event
->enabled
;
799 event_comm
.pid
= (int32_t) event
->pid
;
800 event_comm
.exclusion_count
= (uint32_t) exclusion_count
;
801 event_comm
.bytecode_len
= (uint32_t) bytecode_len
;
802 event_comm
.flags
= (int32_t) event
->flags
;
804 if (filter_expression
) {
805 event_comm
.filter_expression_len
=
806 strlen(filter_expression
) + 1;
810 ret
= lttng_dynamic_buffer_append(buf
, &event_comm
, sizeof(event_comm
));
816 ret
= lttng_dynamic_buffer_append(buf
, event
->name
, name_len
);
822 for (i
= 0; i
< exclusion_count
; i
++) {
823 size_t exclusion_len
;
824 struct lttng_event_exclusion_comm exclusion_comm
= { 0 };
826 assert(exclusion_list
);
828 exclusion_len
= lttng_strnlen(
829 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
830 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
831 /* Exclusion is not NULL-terminated. */
836 /* Include null terminator '\0'. */
839 exclusion_comm
.len
= exclusion_len
;
841 ret
= lttng_dynamic_buffer_append(buf
, &exclusion_comm
, sizeof(exclusion_comm
));
846 ret
= lttng_dynamic_buffer_append(buf
, *(exclusion_list
+ i
),
853 /* Filter expression and its bytecode */
854 if (filter_expression
) {
855 ret
= lttng_dynamic_buffer_append(buf
, filter_expression
,
856 event_comm
.filter_expression_len
);
862 * Bytecode can be absent when we serialize to the client
866 ret
= lttng_dynamic_buffer_append(
867 buf
, bytecode
, bytecode_len
);
874 size_before_payload
= buf
->size
;
876 /* Event type specific payload */
877 switch (event
->type
) {
878 case LTTNG_EVENT_FUNCTION
:
880 case LTTNG_EVENT_PROBE
:
881 ret
= lttng_event_probe_attr_serialize(&event
->attr
.probe
, buf
);
887 header
= (struct lttng_event_comm
*) ((char *) buf
->data
+
889 header
->lttng_event_probe_attr_len
=
890 buf
->size
- size_before_payload
;
893 case LTTNG_EVENT_FUNCTION_ENTRY
:
894 ret
= lttng_event_function_attr_serialize(
895 &event
->attr
.ftrace
, buf
);
901 /* Update the lttng_event_function_attr len. */
902 header
= (struct lttng_event_comm
*) ((char *) buf
->data
+
904 header
->lttng_event_function_attr_len
=
905 buf
->size
- size_before_payload
;
908 case LTTNG_EVENT_USERSPACE_PROBE
:
910 struct lttng_event_extended
*ev_ext
=
911 (struct lttng_event_extended
*)
913 assert(event
->extended
.ptr
);
914 assert(ev_ext
->probe_location
);
916 size_before_payload
= buf
->size
;
917 if (ev_ext
->probe_location
) {
919 * lttng_userspace_probe_location_serialize returns the
920 * number of bytes that were appended to the buffer.
922 ret
= lttng_userspace_probe_location_serialize(
923 ev_ext
->probe_location
, buf
,
930 /* Update the userspace probe location len. */
931 header
= (struct lttng_event_comm
*) ((char *) buf
->data
+
933 header
->userspace_probe_location_len
=
934 buf
->size
- size_before_payload
;
938 case LTTNG_EVENT_TRACEPOINT
:
940 case LTTNG_EVENT_ALL
:
943 /* Nothing to do here */
951 static ssize_t
lttng_event_context_app_populate_from_buffer(
952 const struct lttng_buffer_view
*view
,
953 struct lttng_event_context
*event_ctx
)
955 ssize_t ret
, offset
= 0;
956 const struct lttng_event_context_app_comm
*comm
;
957 char *provider_name
= NULL
, *context_name
= NULL
;
958 size_t provider_name_len
, context_name_len
;
959 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
960 view
, offset
, sizeof(*comm
));
962 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
964 if (!lttng_buffer_view_is_valid(&comm_view
)) {
969 comm
= (typeof(comm
)) comm_view
.data
;
970 offset
+= sizeof(*comm
);
972 provider_name_len
= comm
->provider_name_len
;
973 context_name_len
= comm
->ctx_name_len
;
975 if (provider_name_len
== 0 || context_name_len
== 0) {
977 * Application provider and context names MUST
986 const struct lttng_buffer_view provider_name_view
=
987 lttng_buffer_view_from_view(view
, offset
,
990 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
995 name
= provider_name_view
.data
;
997 if (!lttng_buffer_view_contains_string(&provider_name_view
,
998 name
, provider_name_len
)) {
1003 provider_name
= lttng_strndup(name
, provider_name_len
);
1004 if (!provider_name
) {
1008 offset
+= provider_name_len
;
1013 const struct lttng_buffer_view context_name_view
=
1014 lttng_buffer_view_from_view(
1015 view
, offset
, context_name_len
);
1017 if (!lttng_buffer_view_is_valid(&context_name_view
)) {
1022 name
= context_name_view
.data
;
1024 if (!lttng_buffer_view_contains_string(&context_name_view
, name
,
1025 context_name_len
)) {
1030 context_name
= lttng_strndup(name
, context_name_len
);
1031 if (!context_name
) {
1036 offset
+= context_name_len
;
1039 /* Transfer ownership of the strings */
1040 event_ctx
->u
.app_ctx
.provider_name
= provider_name
;
1041 event_ctx
->u
.app_ctx
.ctx_name
= context_name
;
1042 provider_name
= NULL
;
1043 context_name
= NULL
;
1047 free(provider_name
);
1053 static ssize_t
lttng_event_context_perf_counter_populate_from_buffer(
1054 const struct lttng_buffer_view
*view
,
1055 struct lttng_event_context
*event_ctx
)
1057 ssize_t ret
, offset
= 0;
1058 const struct lttng_event_context_perf_counter_comm
*comm
;
1060 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1061 view
, offset
, sizeof(*comm
));
1063 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_COUNTER
||
1065 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
||
1066 event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
);
1068 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1073 comm
= (typeof(comm
)) comm_view
.data
;
1074 offset
+= sizeof(*comm
);
1076 name_len
= comm
->name_len
;
1079 const struct lttng_buffer_view provider_name_view
=
1080 lttng_buffer_view_from_view(
1081 view
, offset
, name_len
);
1082 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
1087 name
= provider_name_view
.data
;
1089 if (!lttng_buffer_view_contains_string(
1090 &provider_name_view
, name
, name_len
)) {
1095 lttng_strncpy(event_ctx
->u
.perf_counter
.name
, name
, name_len
);
1099 event_ctx
->u
.perf_counter
.config
= comm
->config
;
1100 event_ctx
->u
.perf_counter
.type
= comm
->type
;
1109 ssize_t
lttng_event_context_create_from_buffer(
1110 const struct lttng_buffer_view
*view
,
1111 struct lttng_event_context
**event_ctx
)
1113 ssize_t ret
, offset
= 0;
1114 const struct lttng_event_context_comm
*comm
;
1115 struct lttng_event_context
*local_context
= NULL
;
1116 struct lttng_buffer_view subtype_view
;
1117 struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1118 view
, offset
, sizeof(*comm
));
1123 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1128 comm
= (typeof(comm
)) comm_view
.data
;
1129 offset
+= sizeof(*comm
);
1131 local_context
= zmalloc(sizeof(*local_context
));
1132 if (!local_context
) {
1137 local_context
->ctx
= comm
->type
;
1139 subtype_view
= lttng_buffer_view_from_view(view
, offset
, -1);
1141 switch (local_context
->ctx
) {
1142 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1143 ret
= lttng_event_context_app_populate_from_buffer(
1144 &subtype_view
, local_context
);
1146 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1147 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1148 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1149 ret
= lttng_event_context_perf_counter_populate_from_buffer(
1150 &subtype_view
, local_context
);
1153 /* Nothing else to deserialize. */
1164 *event_ctx
= local_context
;
1165 local_context
= NULL
;
1169 free(local_context
);
1173 static int lttng_event_context_app_serialize(
1174 struct lttng_event_context
*context
,
1175 struct lttng_dynamic_buffer
*buffer
)
1178 struct lttng_event_context_app_comm comm
= { 0 };
1179 size_t provider_len
, ctx_len
;
1180 const char *provider_name
;
1181 const char *ctx_name
;
1185 assert(context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1187 provider_name
= context
->u
.app_ctx
.provider_name
;
1188 ctx_name
= context
->u
.app_ctx
.ctx_name
;
1190 if (!provider_name
|| !ctx_name
) {
1191 ret
= -LTTNG_ERR_INVALID
;
1195 provider_len
= strlen(provider_name
);
1196 if (provider_len
== 0) {
1197 ret
= -LTTNG_ERR_INVALID
;
1201 /* Include the null terminator. */
1203 comm
.provider_name_len
= provider_len
;
1205 ctx_len
= strlen(ctx_name
);
1207 ret
= -LTTNG_ERR_INVALID
;
1211 /* Include the null terminator. */
1213 comm
.ctx_name_len
= ctx_len
;
1216 ret
= lttng_dynamic_buffer_append(buffer
, &comm
, sizeof(comm
));
1222 ret
= lttng_dynamic_buffer_append(buffer
, provider_name
, provider_len
);
1227 ret
= lttng_dynamic_buffer_append(buffer
, ctx_name
, ctx_len
);
1237 static int lttng_event_context_perf_counter_serialize(
1238 struct lttng_event_perf_counter_ctx
*context
,
1239 struct lttng_dynamic_buffer
*buffer
)
1242 struct lttng_event_context_perf_counter_comm comm
= { 0 };
1247 comm
.config
= context
->config
;
1248 comm
.type
= context
->type
;
1249 comm
.name_len
= lttng_strnlen(context
->name
, LTTNG_SYMBOL_NAME_LEN
);
1251 if (comm
.name_len
== LTTNG_SYMBOL_NAME_LEN
) {
1256 /* Include the null terminator. */
1260 ret
= lttng_dynamic_buffer_append(buffer
, &comm
, sizeof(comm
));
1266 ret
= lttng_dynamic_buffer_append(buffer
, context
->name
, comm
.name_len
);
1277 int lttng_event_context_serialize(struct lttng_event_context
*context
,
1278 struct lttng_dynamic_buffer
*buf
)
1281 struct lttng_event_context_comm context_comm
= { 0 };
1286 context_comm
.type
= (uint32_t) context
->ctx
;
1289 ret
= lttng_dynamic_buffer_append(
1290 buf
, &context_comm
, sizeof(context_comm
));
1295 switch (context
->ctx
) {
1296 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1297 ret
= lttng_event_context_app_serialize(context
, buf
);
1299 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1300 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1301 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1302 ret
= lttng_event_context_perf_counter_serialize(
1303 &context
->u
.perf_counter
, buf
);
1306 /* Nothing else to serialize. */
1319 void lttng_event_context_destroy(struct lttng_event_context
*context
)
1325 if (context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1326 free(context
->u
.app_ctx
.provider_name
);
1327 free(context
->u
.app_ctx
.ctx_name
);
1334 * This is a specialized populate for lttng_event_field since it ignores
1335 * the extension field of the lttng_event struct and simply copies what it can
1336 * to the internal struct lttng_event of a lttng_event_field.
1338 static void lttng_event_field_populate_lttng_event_from_event(
1339 const struct lttng_event
*src
, struct lttng_event
*destination
)
1341 memcpy(destination
, src
, sizeof(*destination
));
1343 /* Remove all possible dynamic data from the destination event rule. */
1344 destination
->extended
.ptr
= NULL
;
1348 ssize_t
lttng_event_field_create_from_buffer(
1349 const struct lttng_buffer_view
*view
,
1350 struct lttng_event_field
**field
)
1352 ssize_t ret
, offset
= 0;
1353 struct lttng_event_field
*local_event_field
= NULL
;
1354 struct lttng_event
*event
= NULL
;
1355 const struct lttng_event_field_comm
*comm
;
1356 const char* name
= NULL
;
1362 const struct lttng_buffer_view comm_view
=
1363 lttng_buffer_view_from_view(
1364 view
, offset
, sizeof(*comm
));
1366 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1370 /* lttng_event_field_comm header */
1371 comm
= (const struct lttng_event_field_comm
*) view
->data
;
1372 offset
+= sizeof(*comm
);
1375 local_event_field
= zmalloc(sizeof(*local_event_field
));
1376 if (!local_event_field
) {
1381 local_event_field
->type
= comm
->type
;
1382 local_event_field
->nowrite
= comm
->nowrite
;
1386 const struct lttng_buffer_view name_view
=
1387 lttng_buffer_view_from_view(
1388 view
, offset
, comm
->name_len
);
1390 if (!lttng_buffer_view_is_valid(&name_view
)) {
1395 name
= name_view
.data
;
1397 if (!lttng_buffer_view_contains_string(&name_view
, name_view
.data
, comm
->name_len
)) {
1402 if (comm
->name_len
> LTTNG_SYMBOL_NAME_LEN
- 1) {
1403 /* Name is too long.*/
1407 offset
+= comm
->name_len
;
1412 const struct lttng_buffer_view event_view
=
1413 lttng_buffer_view_from_view(
1414 view
, offset
, comm
->event_len
);
1416 if (!lttng_buffer_view_is_valid(&event_view
)) {
1420 ret
= lttng_event_create_from_buffer(&event_view
, &event
, NULL
,
1422 if (ret
!= comm
->event_len
) {
1431 if (lttng_strncpy(local_event_field
->field_name
, name
, LTTNG_SYMBOL_NAME_LEN
)) {
1436 lttng_event_field_populate_lttng_event_from_event(
1437 event
, &local_event_field
->event
);
1439 *field
= local_event_field
;
1440 local_event_field
= NULL
;
1443 lttng_event_destroy(event
);
1444 free(local_event_field
);
1449 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1450 struct lttng_dynamic_buffer
*buffer
)
1453 size_t header_offset
, size_before_event
;
1455 struct lttng_event_field_comm event_field_comm
= { 0 };
1456 struct lttng_event_field_comm
*header
;
1461 /* Save the header location for later in-place header update. */
1462 header_offset
= buffer
->size
;
1464 name_len
= strnlen(field
->field_name
, LTTNG_SYMBOL_NAME_LEN
);
1465 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
1466 /* Event name is not NULL-terminated. */
1471 /* Add null termination. */
1474 event_field_comm
.type
= field
->type
;
1475 event_field_comm
.nowrite
= (uint8_t)field
->nowrite
;
1476 event_field_comm
.name_len
= name_len
;
1479 ret
= lttng_dynamic_buffer_append(
1480 buffer
, &event_field_comm
, sizeof(event_field_comm
));
1486 ret
= lttng_dynamic_buffer_append(buffer
, field
->field_name
, name_len
);
1491 size_before_event
= buffer
->size
;
1492 ret
= lttng_event_serialize(
1493 &field
->event
, 0, NULL
, NULL
, 0, 0, buffer
, NULL
);
1499 /* Update the event len. */
1500 header
= (struct lttng_event_field_comm
*) ((char *) buffer
->data
+
1502 header
->event_len
= buffer
->size
- size_before_event
;
1508 static enum lttng_error_code
compute_flattened_size(
1509 struct lttng_dynamic_pointer_array
*events
, size_t *size
)
1511 enum lttng_error_code ret_code
;
1513 size_t storage_req
, event_count
, i
;
1518 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1520 /* The basic struct lttng_event */
1521 storage_req
= event_count
* sizeof(struct lttng_event
);
1523 for (i
= 0; i
< event_count
; i
++) {
1524 int probe_storage_req
= 0;
1525 const struct event_list_element
*element
=
1526 lttng_dynamic_pointer_array_get_pointer(
1528 const struct lttng_userspace_probe_location
*location
= NULL
;
1530 location
= lttng_event_get_userspace_probe_location(
1533 ret
= lttng_userspace_probe_location_flatten(
1536 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1540 probe_storage_req
= ret
;
1543 /* The struct·lttng_event_extended */
1544 storage_req
+= event_count
*
1545 sizeof(struct lttng_event_extended
);
1547 if (element
->filter_expression
) {
1548 storage_req
+= strlen(element
->filter_expression
) + 1;
1551 if (element
->exclusions
) {
1552 storage_req
+= element
->exclusions
->count
*
1553 LTTNG_SYMBOL_NAME_LEN
;
1556 /* Padding to ensure the flat probe is aligned. */
1557 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
1558 storage_req
+= probe_storage_req
;
1561 *size
= storage_req
;
1562 ret_code
= LTTNG_OK
;
1569 * Flatten a list of struct lttng_event.
1571 * The buffer that is returned to the API client must contain a "flat" version
1572 * of the events that are returned. In other words, all pointers within an
1573 * lttng_event must point to a location within the returned buffer so that the
1574 * user may free everything by simply calling free() on the returned buffer.
1575 * This is needed in order to maintain API compatibility.
1577 * A first pass is performed to compute the size of the buffer that must be
1578 * allocated. A second pass is then performed to setup the returned events so
1579 * that their members always point within the buffer.
1581 * The layout of the returned buffer is as follows:
1582 * - struct lttng_event[nb_events],
1583 * - nb_events times the following:
1584 * - struct lttng_event_extended,
1585 * - filter_expression
1587 * - padding to align to 64-bits
1588 * - flattened version of userspace_probe_location
1590 static enum lttng_error_code
flatten_lttng_events(
1591 struct lttng_dynamic_pointer_array
*events
,
1592 struct lttng_event
**flattened_events
)
1594 enum lttng_error_code ret_code
;
1597 struct lttng_dynamic_buffer local_flattened_events
;
1601 assert(flattened_events
);
1603 lttng_dynamic_buffer_init(&local_flattened_events
);
1604 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1606 ret_code
= compute_flattened_size(events
, &storage_req
);
1607 if (ret_code
!= LTTNG_OK
) {
1612 * We must ensure that "local_flattened_events" is never resized so as
1613 * to preserve the validity of the flattened objects.
1615 ret
= lttng_dynamic_buffer_set_capacity(
1616 &local_flattened_events
, storage_req
);
1618 ret_code
= LTTNG_ERR_NOMEM
;
1622 /* Start by laying the struct lttng_event */
1623 for (i
= 0; i
< nb_events
; i
++) {
1624 struct event_list_element
*element
=
1625 lttng_dynamic_pointer_array_get_pointer(
1629 ret_code
= LTTNG_ERR_FATAL
;
1632 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1633 element
->event
, sizeof(struct lttng_event
));
1635 ret_code
= LTTNG_ERR_NOMEM
;
1640 for (i
= 0; i
< nb_events
; i
++) {
1641 struct event_list_element
*element
= lttng_dynamic_pointer_array_get_pointer(events
, i
);
1642 struct lttng_event
*event
= (struct lttng_event
*)
1643 (local_flattened_events
.data
+ (sizeof(struct lttng_event
) * i
));
1644 struct lttng_event_extended
*event_extended
=
1645 (struct lttng_event_extended
*)
1646 (local_flattened_events
.data
+ local_flattened_events
.size
);
1647 const struct lttng_userspace_probe_location
*location
= NULL
;
1651 /* Insert struct lttng_event_extended. */
1652 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1653 local_flattened_events
.size
+
1654 sizeof(*event_extended
));
1656 ret_code
= LTTNG_ERR_NOMEM
;
1659 event
->extended
.ptr
= event_extended
;
1661 /* Insert filter expression. */
1662 if (element
->filter_expression
) {
1663 size_t len
= strlen(element
->filter_expression
) + 1;
1665 event_extended
->filter_expression
=
1666 local_flattened_events
.data
+
1667 local_flattened_events
.size
;
1668 ret
= lttng_dynamic_buffer_append(
1669 &local_flattened_events
,
1670 element
->filter_expression
, len
);
1672 ret_code
= LTTNG_ERR_NOMEM
;
1677 /* Insert exclusions. */
1678 if (element
->exclusions
) {
1679 event_extended
->exclusions
.count
=
1680 element
->exclusions
->count
;
1681 event_extended
->exclusions
.strings
=
1682 local_flattened_events
.data
+
1683 local_flattened_events
.size
;
1685 ret
= lttng_dynamic_buffer_append(
1686 &local_flattened_events
,
1687 element
->exclusions
->names
,
1688 element
->exclusions
->count
*
1689 LTTNG_SYMBOL_NAME_LEN
);
1691 ret_code
= LTTNG_ERR_NOMEM
;
1696 /* Insert padding to align to 64-bits. */
1697 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1698 ALIGN_TO(local_flattened_events
.size
,
1701 ret_code
= LTTNG_ERR_NOMEM
;
1705 location
= lttng_event_get_userspace_probe_location(
1708 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
1709 (local_flattened_events
.data
+ local_flattened_events
.size
);
1710 ret
= lttng_userspace_probe_location_flatten(
1711 location
, &local_flattened_events
);
1713 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1719 /* Don't reset local_flattened_events buffer as we return its content. */
1720 *flattened_events
= (struct lttng_event
*) local_flattened_events
.data
;
1721 lttng_dynamic_buffer_init(&local_flattened_events
);
1722 ret_code
= LTTNG_OK
;
1724 lttng_dynamic_buffer_reset(&local_flattened_events
);
1728 static enum lttng_error_code
event_list_create_from_buffer(
1729 const struct lttng_buffer_view
*view
,
1731 struct lttng_dynamic_pointer_array
*event_list
)
1733 enum lttng_error_code ret_code
;
1741 for (i
= 0; i
< count
; i
++) {
1743 const struct lttng_buffer_view event_view
=
1744 lttng_buffer_view_from_view(view
, offset
, -1);
1745 struct event_list_element
*element
= zmalloc(sizeof(*element
));
1748 ret_code
= LTTNG_ERR_NOMEM
;
1753 * Lifetime and management of the object is now bound to the
1756 ret
= lttng_dynamic_pointer_array_add_pointer(
1757 event_list
, element
);
1759 event_list_destructor(element
);
1760 ret_code
= LTTNG_ERR_NOMEM
;
1765 * Bytecode is not transmitted on listing in any case we do not
1768 event_size
= lttng_event_create_from_buffer(&event_view
,
1770 &element
->exclusions
,
1771 &element
->filter_expression
, NULL
, -1);
1772 if (event_size
< 0) {
1773 ret_code
= LTTNG_ERR_INVALID
;
1777 offset
+= event_size
;
1780 if (view
->size
!= offset
) {
1781 ret_code
= LTTNG_ERR_INVALID
;
1785 ret_code
= LTTNG_OK
;
1792 enum lttng_error_code
lttng_events_create_and_flatten_from_buffer(
1793 const struct lttng_buffer_view
*view
,
1795 struct lttng_event
**events
)
1797 enum lttng_error_code ret
= LTTNG_OK
;
1798 struct lttng_dynamic_pointer_array local_events
;
1800 lttng_dynamic_pointer_array_init(&local_events
, event_list_destructor
);
1802 /* Deserialize the events */
1804 const struct lttng_buffer_view events_view
=
1805 lttng_buffer_view_from_view(view
, 0, -1);
1807 ret
= event_list_create_from_buffer(
1808 &events_view
, count
, &local_events
);
1809 if (ret
!= LTTNG_OK
) {
1814 ret
= flatten_lttng_events(&local_events
, events
);
1815 if (ret
!= LTTNG_OK
) {
1820 lttng_dynamic_pointer_array_reset(&local_events
);
1824 static enum lttng_error_code
flatten_lttng_event_fields(
1825 struct lttng_dynamic_pointer_array
*event_fields
,
1826 struct lttng_event_field
**flattened_event_fields
)
1829 enum lttng_error_code ret_code
;
1830 size_t storage_req
= 0;
1831 struct lttng_dynamic_buffer local_flattened_event_fields
;
1834 assert(event_fields
);
1835 assert(flattened_event_fields
);
1837 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1838 nb_event_field
= lttng_dynamic_pointer_array_get_count(event_fields
);
1841 * Here even if the event field contains a `struct lttng_event` that
1842 * could contain dynamic data, in reality it is not the case.
1843 * Dynamic data is not present. Here the flattening is mostly a direct
1844 * memcpy. This is less than ideal but this code is still better than
1845 * direct usage of an unpacked lttng_event_field array.
1847 storage_req
+= sizeof(struct lttng_event_field
) * nb_event_field
;
1849 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1852 * We must ensure that "local_flattened_event_fields" is never resized
1853 * so as to preserve the validity of the flattened objects.
1855 ret
= lttng_dynamic_buffer_set_capacity(
1856 &local_flattened_event_fields
, storage_req
);
1858 ret_code
= LTTNG_ERR_NOMEM
;
1862 for (i
= 0; i
< nb_event_field
; i
++) {
1863 const struct lttng_event_field
*element
=
1864 lttng_dynamic_pointer_array_get_pointer(
1868 ret_code
= LTTNG_ERR_FATAL
;
1871 ret
= lttng_dynamic_buffer_append(&local_flattened_event_fields
,
1872 element
, sizeof(struct lttng_event_field
));
1874 ret_code
= LTTNG_ERR_NOMEM
;
1879 /* Don't reset local_flattened_channels buffer as we return its content. */
1880 *flattened_event_fields
= (struct lttng_event_field
*) local_flattened_event_fields
.data
;
1881 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1882 ret_code
= LTTNG_OK
;
1884 lttng_dynamic_buffer_reset(&local_flattened_event_fields
);
1888 static enum lttng_error_code
event_field_list_create_from_buffer(
1889 const struct lttng_buffer_view
*view
,
1891 struct lttng_dynamic_pointer_array
**event_field_list
)
1893 enum lttng_error_code ret_code
;
1894 int ret
, offset
= 0;
1896 struct lttng_dynamic_pointer_array
*list
= NULL
;
1899 assert(event_field_list
);
1901 list
= zmalloc(sizeof(*list
));
1903 ret_code
= LTTNG_ERR_NOMEM
;
1907 lttng_dynamic_pointer_array_init(list
, free
);
1909 for (i
= 0; i
< count
; i
++) {
1910 ssize_t event_field_size
;
1911 struct lttng_event_field
*field
= NULL
;
1912 const struct lttng_buffer_view event_field_view
=
1913 lttng_buffer_view_from_view(view
, offset
, -1);
1915 event_field_size
= lttng_event_field_create_from_buffer(
1916 &event_field_view
, &field
);
1917 if (event_field_size
< 0) {
1918 ret_code
= LTTNG_ERR_INVALID
;
1922 /* Lifetime and management of the object is now bound to the array. */
1923 ret
= lttng_dynamic_pointer_array_add_pointer(list
, field
);
1926 ret_code
= LTTNG_ERR_NOMEM
;
1929 offset
+= event_field_size
;
1932 if (view
->size
!= offset
) {
1933 ret_code
= LTTNG_ERR_INVALID
;
1937 *event_field_list
= list
;
1939 ret_code
= LTTNG_OK
;
1943 lttng_dynamic_pointer_array_reset(list
);
1950 enum lttng_error_code
lttng_event_fields_create_and_flatten_from_buffer(
1951 const struct lttng_buffer_view
*view
,
1953 struct lttng_event_field
**fields
)
1955 enum lttng_error_code ret
;
1956 struct lttng_dynamic_pointer_array
*local_event_fields
= NULL
;
1958 ret
= event_field_list_create_from_buffer(
1959 view
, count
, &local_event_fields
);
1960 if (ret
!= LTTNG_OK
) {
1964 ret
= flatten_lttng_event_fields(local_event_fields
, fields
);
1965 if (ret
!= LTTNG_OK
) {
1969 if (local_event_fields
) {
1970 lttng_dynamic_pointer_array_reset(local_event_fields
);
1971 free(local_event_fields
);