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
;
200 static ssize_t
lttng_event_function_attr_create_from_buffer(
201 const struct lttng_buffer_view
*view
,
202 struct lttng_event_function_attr
**function_attr
)
204 ssize_t ret
, offset
= 0;
205 const struct lttng_event_function_attr_comm
*comm
;
206 struct lttng_event_function_attr
*local_attr
= NULL
;
207 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
208 view
, offset
, sizeof(*comm
));
210 if (!lttng_buffer_view_is_valid(&comm_view
)) {
215 comm
= (typeof(comm
)) view
->data
;
216 offset
+= sizeof(*comm
);
218 local_attr
= zmalloc(sizeof(*local_attr
));
219 if (local_attr
== NULL
) {
226 const struct lttng_buffer_view name_view
=
227 lttng_buffer_view_from_view(view
, offset
,
228 comm
->symbol_name_len
);
229 if (!lttng_buffer_view_is_valid(&name_view
)) {
234 name
= name_view
.data
;
236 if (!lttng_buffer_view_contains_string(
237 &name_view
, name
, comm
->symbol_name_len
)) {
242 ret
= lttng_strncpy(local_attr
->symbol_name
, name
,
243 LTTNG_SYMBOL_NAME_LEN
);
248 offset
+= comm
->symbol_name_len
;
251 *function_attr
= local_attr
;
258 static ssize_t
lttng_event_exclusions_create_from_buffer(const struct lttng_buffer_view
*view
,
259 uint32_t count
, struct lttng_event_exclusion
**exclusions
)
261 ssize_t ret
, offset
= 0;
262 size_t size
= (count
* LTTNG_SYMBOL_NAME_LEN
);
264 const struct lttng_event_exclusion_comm
*comm
;
265 struct lttng_event_exclusion
*local_exclusions
;
268 local_exclusions
= zmalloc(sizeof(struct lttng_event_exclusion
) + size
);
269 if (!local_exclusions
) {
273 local_exclusions
->count
= count
;
275 for (i
= 0; i
< count
; i
++) {
277 struct lttng_buffer_view string_view
;
278 const struct lttng_buffer_view comm_view
=
279 lttng_buffer_view_from_view(
280 view
, offset
, sizeof(*comm
));
282 if (!lttng_buffer_view_is_valid(&comm_view
)) {
287 comm
= (typeof(comm
)) comm_view
.data
;
288 offset
+= sizeof(*comm
);
290 string_view
= lttng_buffer_view_from_view(
291 view
, offset
, comm
->len
);
293 if (!lttng_buffer_view_is_valid(&string_view
)) {
298 string
= string_view
.data
;
300 if (!lttng_buffer_view_contains_string(
301 &string_view
, string
, comm
->len
)) {
307 local_exclusions
->names
[i
],
316 *exclusions
= local_exclusions
;
317 local_exclusions
= NULL
;
320 free(local_exclusions
);
326 ssize_t
lttng_event_create_from_buffer(const struct lttng_buffer_view
*view
,
327 struct lttng_event
**event
,
328 struct lttng_event_exclusion
**exclusion
,
329 char **filter_expression
,
330 struct lttng_filter_bytecode
**bytecode
,
333 ssize_t ret
, offset
= 0;
334 struct lttng_event
*local_event
= NULL
;
335 struct lttng_event_exclusion
*local_exclusions
= NULL
;
336 struct lttng_filter_bytecode
*local_bytecode
= NULL
;
337 char *local_filter_expression
= NULL
;
338 const struct lttng_event_comm
*event_comm
;
339 struct lttng_event_function_attr
*local_function_attr
= NULL
;
340 struct lttng_event_probe_attr
*local_probe_attr
= NULL
;
341 struct lttng_userspace_probe_location
*local_userspace_probe_location
=
343 int received_fd
= -1;
346 * Only event is obligatory, the other output argument are optional and
347 * depends on what the caller is interested in.
353 const struct lttng_buffer_view comm_view
=
354 lttng_buffer_view_from_view(view
, offset
,
355 sizeof(*event_comm
));
357 if (!lttng_buffer_view_is_valid(&comm_view
)) {
362 /* lttng_event_comm header */
363 event_comm
= (typeof(event_comm
)) comm_view
.data
;
364 offset
+= sizeof(*event_comm
);
367 local_event
= lttng_event_create();
368 if (local_event
== NULL
) {
373 local_event
->type
= event_comm
->event_type
;
374 local_event
->loglevel_type
= event_comm
->loglevel_type
;
375 local_event
->loglevel
= event_comm
->loglevel
;
376 local_event
->enabled
= event_comm
->enabled
;
377 local_event
->pid
= event_comm
->pid
;
378 local_event
->flags
= event_comm
->flags
;
382 const struct lttng_buffer_view name_view
=
383 lttng_buffer_view_from_view(view
, offset
,
384 event_comm
->name_len
);
385 if (!lttng_buffer_view_is_valid(&name_view
)) {
390 name
= name_view
.data
;
392 if (!lttng_buffer_view_contains_string(
393 &name_view
, name
, event_comm
->name_len
)) {
399 local_event
->name
, name
, LTTNG_SYMBOL_NAME_LEN
);
404 offset
+= event_comm
->name_len
;
408 if (event_comm
->exclusion_count
== 0) {
409 goto deserialize_filter_expression
;
414 const struct lttng_buffer_view exclusions_view
=
415 lttng_buffer_view_from_view(
418 if (!lttng_buffer_view_is_valid(&exclusions_view
)) {
423 ret
= lttng_event_exclusions_create_from_buffer(&exclusions_view
,
424 event_comm
->exclusion_count
, &local_exclusions
);
431 local_event
->exclusion
= 1;
434 deserialize_filter_expression
:
436 if (event_comm
->filter_expression_len
== 0) {
437 if (event_comm
->bytecode_len
!= 0) {
439 * This is an invalid event payload.
441 * Filter expression without bytecode is possible but
442 * not the other way around.
447 goto deserialize_event_type_payload
;
451 const char *filter_expression_buffer
;
452 const struct lttng_buffer_view filter_expression_view
=
453 lttng_buffer_view_from_view(view
, offset
,
454 event_comm
->filter_expression_len
);
456 if (!lttng_buffer_view_is_valid(&filter_expression_view
)) {
461 filter_expression_buffer
= filter_expression_view
.data
;
463 if (!lttng_buffer_view_contains_string(&filter_expression_view
,
464 filter_expression_buffer
,
465 event_comm
->filter_expression_len
)) {
470 local_filter_expression
= lttng_strndup(
471 filter_expression_buffer
,
472 event_comm
->filter_expression_len
);
473 if (!local_filter_expression
) {
478 local_event
->filter
= 1;
480 offset
+= event_comm
->filter_expression_len
;
483 if (event_comm
->bytecode_len
== 0) {
485 * Filter expression can be present but without bytecode
486 * when dealing with event listing.
488 goto deserialize_event_type_payload
;
493 const struct lttng_buffer_view bytecode_view
=
494 lttng_buffer_view_from_view(view
, offset
,
495 event_comm
->bytecode_len
);
497 if (!lttng_buffer_view_is_valid(&bytecode_view
)) {
502 local_bytecode
= zmalloc(event_comm
->bytecode_len
);
503 if (!local_bytecode
) {
508 memcpy(local_bytecode
, bytecode_view
.data
,
509 event_comm
->bytecode_len
);
510 if ((local_bytecode
->len
+ sizeof(*local_bytecode
)) !=
511 event_comm
->bytecode_len
) {
516 offset
+= event_comm
->bytecode_len
;
519 deserialize_event_type_payload
:
520 /* Event type specific payload */
521 switch (local_event
->type
) {
522 case LTTNG_EVENT_FUNCTION
:
524 case LTTNG_EVENT_PROBE
:
526 const struct lttng_buffer_view probe_attr_view
=
527 lttng_buffer_view_from_view(view
, offset
,
528 event_comm
->lttng_event_probe_attr_len
);
530 if (event_comm
->lttng_event_probe_attr_len
== 0) {
535 if (!lttng_buffer_view_is_valid(&probe_attr_view
)) {
540 ret
= lttng_event_probe_attr_create_from_buffer(
541 &probe_attr_view
, &local_probe_attr
);
542 if (ret
< 0 || ret
!= event_comm
->lttng_event_probe_attr_len
) {
547 /* Copy to the local event. */
548 memcpy(&local_event
->attr
.probe
, local_probe_attr
,
549 sizeof(local_event
->attr
.probe
));
554 case LTTNG_EVENT_FUNCTION_ENTRY
:
556 const struct lttng_buffer_view function_attr_view
=
557 lttng_buffer_view_from_view(view
, offset
,
558 event_comm
->lttng_event_function_attr_len
);
560 if (event_comm
->lttng_event_function_attr_len
== 0) {
565 if (!lttng_buffer_view_is_valid(&function_attr_view
)) {
570 ret
= lttng_event_function_attr_create_from_buffer(
571 &function_attr_view
, &local_function_attr
);
572 if (ret
< 0 || ret
!= event_comm
->lttng_event_function_attr_len
) {
577 /* Copy to the local event. */
578 memcpy(&local_event
->attr
.ftrace
, local_function_attr
,
579 sizeof(local_event
->attr
.ftrace
));
585 case LTTNG_EVENT_USERSPACE_PROBE
:
587 const struct lttng_buffer_view userspace_probe_location_view
=
588 lttng_buffer_view_from_view(view
, offset
,
589 event_comm
->userspace_probe_location_len
);
590 const struct lttng_userspace_probe_location_lookup_method
593 if (event_comm
->userspace_probe_location_len
== 0) {
598 if (!lttng_buffer_view_is_valid(
599 &userspace_probe_location_view
)) {
604 ret
= lttng_userspace_probe_location_create_from_buffer(
605 &userspace_probe_location_view
,
606 &local_userspace_probe_location
);
608 WARN("Failed to create a userspace probe location from the received buffer");
613 if (ret
!= event_comm
->userspace_probe_location_len
) {
614 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
);
621 * The userspace FD is simply not sent and we do not
622 * care about it. This happens on listing.
624 goto attach_userspace_probe_to_event
;
628 * Receive the file descriptor to the target binary from the
631 DBG("Receiving userspace probe target FD from client ...");
632 ret
= lttcomm_recv_fds_unix_sock(sock
, &received_fd
, 1);
634 DBG("Nothing recv() from client userspace probe fd... continuing");
639 * Set the file descriptor received from the client through the
640 * unix socket in the probe location.
642 lookup
= lttng_userspace_probe_location_get_lookup_method(
643 local_userspace_probe_location
);
645 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
650 * From the kernel tracer's perspective, all userspace probe
651 * event types are all the same: a file and an offset.
653 switch (lttng_userspace_probe_location_lookup_method_get_type(
655 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
656 ret
= lttng_userspace_probe_location_function_set_binary_fd(
657 local_userspace_probe_location
,
660 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
661 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
662 local_userspace_probe_location
,
666 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
671 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
675 /* Fd transfered to the object. */
678 attach_userspace_probe_to_event
:
680 /* Attach the probe location to the event. */
681 ret
= lttng_event_set_userspace_probe_location(
682 local_event
, local_userspace_probe_location
);
684 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
689 * Userspace probe location object ownership transfered to the
692 local_userspace_probe_location
= NULL
;
693 offset
+= event_comm
->userspace_probe_location_len
;
696 case LTTNG_EVENT_TRACEPOINT
:
698 case LTTNG_EVENT_ALL
:
700 case LTTNG_EVENT_SYSCALL
:
702 case LTTNG_EVENT_NOOP
:
703 /* Nothing to do here */
711 /* Transfer ownership to the caller. */
712 *event
= local_event
;
716 *bytecode
= local_bytecode
;
717 local_bytecode
= NULL
;
721 *exclusion
= local_exclusions
;
722 local_exclusions
= NULL
;
725 if (filter_expression
) {
726 *filter_expression
= local_filter_expression
;
727 local_filter_expression
= NULL
;
732 lttng_event_destroy(local_event
);
733 lttng_userspace_probe_location_destroy(local_userspace_probe_location
);
734 free(local_filter_expression
);
735 free(local_exclusions
);
736 free(local_bytecode
);
737 free(local_function_attr
);
738 free(local_probe_attr
);
739 if (received_fd
> -1) {
740 if (close(received_fd
)) {
741 PERROR("Failed to close received fd");
748 int lttng_event_serialize(const struct lttng_event
*event
,
749 unsigned int exclusion_count
,
750 char **exclusion_list
,
751 char *filter_expression
,
754 struct lttng_dynamic_buffer
*buf
,
759 size_t header_offset
, size_before_payload
;
761 struct lttng_event_comm event_comm
= { 0 };
762 struct lttng_event_comm
*header
;
767 /* Save the header location for later in-place header update. */
768 header_offset
= buf
->size
;
770 name_len
= lttng_strnlen(event
->name
, LTTNG_SYMBOL_NAME_LEN
);
771 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
772 /* Event name is not NULL-terminated. */
777 /* Add null termination. */
780 if (exclusion_count
> UINT32_MAX
) {
781 /* Possible overflow. */
786 if (bytecode_len
> UINT32_MAX
) {
787 /* Possible overflow. */
792 event_comm
.name_len
= (uint32_t) name_len
;
793 event_comm
.event_type
= (int8_t) event
->type
;
794 event_comm
.loglevel_type
= (int8_t) event
->loglevel_type
;
795 event_comm
.loglevel
= (int32_t) event
->loglevel
;
796 event_comm
.enabled
= (int8_t) event
->enabled
;
797 event_comm
.pid
= (int32_t) event
->pid
;
798 event_comm
.exclusion_count
= (uint32_t) exclusion_count
;
799 event_comm
.bytecode_len
= (uint32_t) bytecode_len
;
800 event_comm
.flags
= (int32_t) event
->flags
;
802 if (filter_expression
) {
803 event_comm
.filter_expression_len
=
804 strlen(filter_expression
) + 1;
808 ret
= lttng_dynamic_buffer_append(buf
, &event_comm
, sizeof(event_comm
));
814 ret
= lttng_dynamic_buffer_append(buf
, event
->name
, name_len
);
820 for (i
= 0; i
< exclusion_count
; i
++) {
821 size_t exclusion_len
;
822 struct lttng_event_exclusion_comm exclusion_comm
= { 0 };
824 assert(exclusion_list
);
826 exclusion_len
= lttng_strnlen(
827 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
828 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
829 /* Exclusion is not NULL-terminated. */
834 /* Include null terminator '\0'. */
837 exclusion_comm
.len
= exclusion_len
;
839 ret
= lttng_dynamic_buffer_append(buf
, &exclusion_comm
, sizeof(exclusion_comm
));
844 ret
= lttng_dynamic_buffer_append(buf
, *(exclusion_list
+ i
),
851 /* Filter expression and its bytecode */
852 if (filter_expression
) {
853 ret
= lttng_dynamic_buffer_append(buf
, filter_expression
,
854 event_comm
.filter_expression_len
);
860 * Bytecode can be absent when we serialize to the client
864 ret
= lttng_dynamic_buffer_append(
865 buf
, bytecode
, bytecode_len
);
872 size_before_payload
= buf
->size
;
874 /* Event type specific payload */
875 switch (event
->type
) {
876 case LTTNG_EVENT_FUNCTION
:
878 case LTTNG_EVENT_PROBE
:
879 ret
= lttng_event_probe_attr_serialize(&event
->attr
.probe
, buf
);
885 header
= (struct lttng_event_comm
*) ((char *) buf
->data
+
887 header
->lttng_event_probe_attr_len
=
888 buf
->size
- size_before_payload
;
891 case LTTNG_EVENT_FUNCTION_ENTRY
:
892 ret
= lttng_event_function_attr_serialize(
893 &event
->attr
.ftrace
, buf
);
899 /* Update the lttng_event_function_attr len. */
900 header
= (struct lttng_event_comm
*) ((char *) buf
->data
+
902 header
->lttng_event_function_attr_len
=
903 buf
->size
- size_before_payload
;
906 case LTTNG_EVENT_USERSPACE_PROBE
:
908 struct lttng_event_extended
*ev_ext
=
909 (struct lttng_event_extended
*)
911 assert(event
->extended
.ptr
);
912 assert(ev_ext
->probe_location
);
914 size_before_payload
= buf
->size
;
915 if (ev_ext
->probe_location
) {
917 * lttng_userspace_probe_location_serialize returns the
918 * number of bytes that were appended to the buffer.
920 ret
= lttng_userspace_probe_location_serialize(
921 ev_ext
->probe_location
, buf
,
928 /* Update the userspace probe location len. */
929 header
= (struct lttng_event_comm
*) ((char *) buf
->data
+
931 header
->userspace_probe_location_len
=
932 buf
->size
- size_before_payload
;
936 case LTTNG_EVENT_TRACEPOINT
:
938 case LTTNG_EVENT_ALL
:
941 /* Nothing to do here */
949 static ssize_t
lttng_event_context_app_populate_from_buffer(
950 const struct lttng_buffer_view
*view
,
951 struct lttng_event_context
*event_ctx
)
953 ssize_t ret
, offset
= 0;
954 const struct lttng_event_context_app_comm
*comm
;
955 char *provider_name
= NULL
, *context_name
= NULL
;
956 size_t provider_name_len
, context_name_len
;
957 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
958 view
, offset
, sizeof(*comm
));
960 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
962 if (!lttng_buffer_view_is_valid(&comm_view
)) {
967 comm
= (typeof(comm
)) comm_view
.data
;
968 offset
+= sizeof(*comm
);
970 provider_name_len
= comm
->provider_name_len
;
971 context_name_len
= comm
->ctx_name_len
;
973 if (provider_name_len
== 0 || context_name_len
== 0) {
975 * Application provider and context names MUST
984 const struct lttng_buffer_view provider_name_view
=
985 lttng_buffer_view_from_view(view
, offset
,
988 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
993 name
= provider_name_view
.data
;
995 if (!lttng_buffer_view_contains_string(&provider_name_view
,
996 name
, provider_name_len
)) {
1001 provider_name
= lttng_strndup(name
, provider_name_len
);
1002 if (!provider_name
) {
1006 offset
+= provider_name_len
;
1011 const struct lttng_buffer_view context_name_view
=
1012 lttng_buffer_view_from_view(
1013 view
, offset
, context_name_len
);
1015 if (!lttng_buffer_view_is_valid(&context_name_view
)) {
1020 name
= context_name_view
.data
;
1022 if (!lttng_buffer_view_contains_string(&context_name_view
, name
,
1023 context_name_len
)) {
1028 context_name
= lttng_strndup(name
, context_name_len
);
1029 if (!context_name
) {
1034 offset
+= context_name_len
;
1037 /* Transfer ownership of the strings */
1038 event_ctx
->u
.app_ctx
.provider_name
= provider_name
;
1039 event_ctx
->u
.app_ctx
.ctx_name
= context_name
;
1040 provider_name
= NULL
;
1041 context_name
= NULL
;
1045 free(provider_name
);
1051 static ssize_t
lttng_event_context_perf_counter_populate_from_buffer(
1052 const struct lttng_buffer_view
*view
,
1053 struct lttng_event_context
*event_ctx
)
1055 ssize_t ret
, offset
= 0;
1056 const struct lttng_event_context_perf_counter_comm
*comm
;
1058 const struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1059 view
, offset
, sizeof(*comm
));
1061 assert(event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_COUNTER
||
1063 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
||
1064 event_ctx
->ctx
== LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
);
1066 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1071 comm
= (typeof(comm
)) comm_view
.data
;
1072 offset
+= sizeof(*comm
);
1074 name_len
= comm
->name_len
;
1077 const struct lttng_buffer_view provider_name_view
=
1078 lttng_buffer_view_from_view(
1079 view
, offset
, name_len
);
1080 if (!lttng_buffer_view_is_valid(&provider_name_view
)) {
1085 name
= provider_name_view
.data
;
1087 if (!lttng_buffer_view_contains_string(
1088 &provider_name_view
, name
, name_len
)) {
1093 lttng_strncpy(event_ctx
->u
.perf_counter
.name
, name
, name_len
);
1097 event_ctx
->u
.perf_counter
.config
= comm
->config
;
1098 event_ctx
->u
.perf_counter
.type
= comm
->type
;
1107 ssize_t
lttng_event_context_create_from_buffer(
1108 const struct lttng_buffer_view
*view
,
1109 struct lttng_event_context
**event_ctx
)
1111 ssize_t ret
, offset
= 0;
1112 const struct lttng_event_context_comm
*comm
;
1113 struct lttng_event_context
*local_context
= NULL
;
1114 struct lttng_buffer_view subtype_view
;
1115 struct lttng_buffer_view comm_view
= lttng_buffer_view_from_view(
1116 view
, offset
, sizeof(*comm
));
1121 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1126 comm
= (typeof(comm
)) comm_view
.data
;
1127 offset
+= sizeof(*comm
);
1129 local_context
= zmalloc(sizeof(*local_context
));
1130 if (!local_context
) {
1135 local_context
->ctx
= comm
->type
;
1137 subtype_view
= lttng_buffer_view_from_view(view
, offset
, -1);
1139 switch (local_context
->ctx
) {
1140 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1141 ret
= lttng_event_context_app_populate_from_buffer(
1142 &subtype_view
, local_context
);
1144 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1145 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1146 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1147 ret
= lttng_event_context_perf_counter_populate_from_buffer(
1148 &subtype_view
, local_context
);
1151 /* Nothing else to deserialize. */
1162 *event_ctx
= local_context
;
1163 local_context
= NULL
;
1167 free(local_context
);
1171 static int lttng_event_context_app_serialize(
1172 struct lttng_event_context
*context
,
1173 struct lttng_dynamic_buffer
*buffer
)
1176 struct lttng_event_context_app_comm comm
= { 0 };
1177 size_t provider_len
, ctx_len
;
1178 const char *provider_name
;
1179 const char *ctx_name
;
1183 assert(context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1185 provider_name
= context
->u
.app_ctx
.provider_name
;
1186 ctx_name
= context
->u
.app_ctx
.ctx_name
;
1188 if (!provider_name
|| !ctx_name
) {
1189 ret
= -LTTNG_ERR_INVALID
;
1193 provider_len
= strlen(provider_name
);
1194 if (provider_len
== 0) {
1195 ret
= -LTTNG_ERR_INVALID
;
1199 /* Include the null terminator. */
1200 comm
.provider_name_len
= provider_len
+ 1;
1202 ctx_len
= strlen(ctx_name
);
1204 ret
= -LTTNG_ERR_INVALID
;
1208 /* Include the null terminator. */
1209 comm
.ctx_name_len
= ctx_len
+ 1;
1212 ret
= lttng_dynamic_buffer_append(buffer
, &comm
, sizeof(comm
));
1218 ret
= lttng_dynamic_buffer_append(buffer
, provider_name
, provider_len
);
1223 ret
= lttng_dynamic_buffer_append(buffer
, ctx_name
, ctx_len
);
1233 static int lttng_event_context_perf_counter_serialize(
1234 struct lttng_event_perf_counter_ctx
*context
,
1235 struct lttng_dynamic_buffer
*buffer
)
1238 struct lttng_event_context_perf_counter_comm comm
= { 0 };
1243 comm
.config
= context
->config
;
1244 comm
.type
= context
->type
;
1245 comm
.name_len
= lttng_strnlen(context
->name
, LTTNG_SYMBOL_NAME_LEN
);
1247 if (comm
.name_len
== LTTNG_SYMBOL_NAME_LEN
) {
1252 /* Include the null terminator. */
1256 ret
= lttng_dynamic_buffer_append(buffer
, &comm
, sizeof(comm
));
1262 ret
= lttng_dynamic_buffer_append(buffer
, context
->name
, comm
.name_len
);
1273 int lttng_event_context_serialize(struct lttng_event_context
*context
,
1274 struct lttng_dynamic_buffer
*buf
)
1277 struct lttng_event_context_comm context_comm
= { 0 };
1282 context_comm
.type
= (uint32_t) context
->ctx
;
1285 ret
= lttng_dynamic_buffer_append(
1286 buf
, &context_comm
, sizeof(context_comm
));
1291 switch (context
->ctx
) {
1292 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
1293 ret
= lttng_event_context_app_serialize(context
, buf
);
1295 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
1296 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
1297 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1298 ret
= lttng_event_context_perf_counter_serialize(
1299 &context
->u
.perf_counter
, buf
);
1302 /* Nothing else to serialize. */
1315 void lttng_event_context_destroy(struct lttng_event_context
*context
)
1321 if (context
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1322 free(context
->u
.app_ctx
.provider_name
);
1323 free(context
->u
.app_ctx
.ctx_name
);
1330 * This is a specialized populate for lttng_event_field since it ignores
1331 * the extension field of the lttng_event struct and simply copies what it can
1332 * to the internal struct lttng_event of a lttng_event_field.
1334 static void lttng_event_field_populate_lttng_event_from_event(
1335 const struct lttng_event
*src
, struct lttng_event
*destination
)
1337 memcpy(destination
, src
, sizeof(*destination
));
1339 /* Remove all possible dynamic data from the destination event rule. */
1340 destination
->extended
.ptr
= NULL
;
1344 ssize_t
lttng_event_field_create_from_buffer(
1345 const struct lttng_buffer_view
*view
,
1346 struct lttng_event_field
**field
)
1348 ssize_t ret
, offset
= 0;
1349 struct lttng_event_field
*local_event_field
= NULL
;
1350 struct lttng_event
*event
= NULL
;
1351 const struct lttng_event_field_comm
*comm
;
1352 const char* name
= NULL
;
1358 const struct lttng_buffer_view comm_view
=
1359 lttng_buffer_view_from_view(
1360 view
, offset
, sizeof(*comm
));
1362 if (!lttng_buffer_view_is_valid(&comm_view
)) {
1366 /* lttng_event_field_comm header */
1367 comm
= (const struct lttng_event_field_comm
*) view
->data
;
1368 offset
+= sizeof(*comm
);
1371 local_event_field
= zmalloc(sizeof(*local_event_field
));
1372 if (!local_event_field
) {
1377 local_event_field
->type
= comm
->type
;
1378 local_event_field
->nowrite
= comm
->nowrite
;
1382 const struct lttng_buffer_view name_view
=
1383 lttng_buffer_view_from_view(
1384 view
, offset
, comm
->name_len
);
1386 if (!lttng_buffer_view_is_valid(&name_view
)) {
1391 name
= name_view
.data
;
1393 if (!lttng_buffer_view_contains_string(&name_view
, name_view
.data
, comm
->name_len
)) {
1398 if (comm
->name_len
> LTTNG_SYMBOL_NAME_LEN
- 1) {
1399 /* Name is too long.*/
1403 offset
+= comm
->name_len
;
1408 const struct lttng_buffer_view event_view
=
1409 lttng_buffer_view_from_view(
1410 view
, offset
, comm
->event_len
);
1412 if (!lttng_buffer_view_is_valid(&event_view
)) {
1416 ret
= lttng_event_create_from_buffer(&event_view
, &event
, NULL
,
1418 if (ret
!= comm
->event_len
) {
1427 if (lttng_strncpy(local_event_field
->field_name
, name
, LTTNG_SYMBOL_NAME_LEN
)) {
1432 lttng_event_field_populate_lttng_event_from_event(
1433 event
, &local_event_field
->event
);
1435 *field
= local_event_field
;
1436 local_event_field
= NULL
;
1439 lttng_event_destroy(event
);
1440 free(local_event_field
);
1445 int lttng_event_field_serialize(const struct lttng_event_field
*field
,
1446 struct lttng_dynamic_buffer
*buffer
)
1449 size_t header_offset
, size_before_event
;
1451 struct lttng_event_field_comm event_field_comm
= { 0 };
1452 struct lttng_event_field_comm
*header
;
1457 /* Save the header location for later in-place header update. */
1458 header_offset
= buffer
->size
;
1460 name_len
= strnlen(field
->field_name
, LTTNG_SYMBOL_NAME_LEN
);
1461 if (name_len
== LTTNG_SYMBOL_NAME_LEN
) {
1462 /* Event name is not NULL-terminated. */
1467 /* Add null termination. */
1470 event_field_comm
.type
= field
->type
;
1471 event_field_comm
.nowrite
= (uint8_t)field
->nowrite
;
1472 event_field_comm
.name_len
= name_len
;
1475 ret
= lttng_dynamic_buffer_append(
1476 buffer
, &event_field_comm
, sizeof(event_field_comm
));
1482 ret
= lttng_dynamic_buffer_append(buffer
, field
->field_name
, name_len
);
1487 size_before_event
= buffer
->size
;
1488 ret
= lttng_event_serialize(
1489 &field
->event
, 0, NULL
, NULL
, 0, 0, buffer
, NULL
);
1495 /* Update the event len. */
1496 header
= (struct lttng_event_field_comm
*) ((char *) buffer
->data
+
1498 header
->event_len
= buffer
->size
- size_before_event
;
1504 static enum lttng_error_code
compute_flattened_size(
1505 struct lttng_dynamic_pointer_array
*events
, size_t *size
)
1507 enum lttng_error_code ret_code
;
1509 size_t storage_req
, event_count
, i
;
1514 event_count
= lttng_dynamic_pointer_array_get_count(events
);
1516 /* The basic struct lttng_event */
1517 storage_req
= event_count
* sizeof(struct lttng_event
);
1519 for (i
= 0; i
< event_count
; i
++) {
1520 int probe_storage_req
= 0;
1521 const struct event_list_element
*element
=
1522 lttng_dynamic_pointer_array_get_pointer(
1524 const struct lttng_userspace_probe_location
*location
= NULL
;
1526 location
= lttng_event_get_userspace_probe_location(
1529 ret
= lttng_userspace_probe_location_flatten(
1532 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1536 probe_storage_req
= ret
;
1539 /* The struct·lttng_event_extended */
1540 storage_req
+= event_count
*
1541 sizeof(struct lttng_event_extended
);
1543 if (element
->filter_expression
) {
1544 storage_req
+= strlen(element
->filter_expression
) + 1;
1547 if (element
->exclusions
) {
1548 storage_req
+= element
->exclusions
->count
*
1549 LTTNG_SYMBOL_NAME_LEN
;
1552 /* Padding to ensure the flat probe is aligned. */
1553 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
1554 storage_req
+= probe_storage_req
;
1557 *size
= storage_req
;
1558 ret_code
= LTTNG_OK
;
1565 * Flatten a list of struct lttng_event.
1567 * The buffer that is returned to the API client must contain a "flat" version
1568 * of the events that are returned. In other words, all pointers within an
1569 * lttng_event must point to a location within the returned buffer so that the
1570 * user may free everything by simply calling free() on the returned buffer.
1571 * This is needed in order to maintain API compatibility.
1573 * A first pass is performed to compute the size of the buffer that must be
1574 * allocated. A second pass is then performed to setup the returned events so
1575 * that their members always point within the buffer.
1577 * The layout of the returned buffer is as follows:
1578 * - struct lttng_event[nb_events],
1579 * - nb_events times the following:
1580 * - struct lttng_event_extended,
1581 * - filter_expression
1583 * - padding to align to 64-bits
1584 * - flattened version of userspace_probe_location
1586 static enum lttng_error_code
flatten_lttng_events(
1587 struct lttng_dynamic_pointer_array
*events
,
1588 struct lttng_event
**flattened_events
)
1590 enum lttng_error_code ret_code
;
1593 struct lttng_dynamic_buffer local_flattened_events
;
1597 assert(flattened_events
);
1599 lttng_dynamic_buffer_init(&local_flattened_events
);
1600 nb_events
= lttng_dynamic_pointer_array_get_count(events
);
1602 ret_code
= compute_flattened_size(events
, &storage_req
);
1603 if (ret_code
!= LTTNG_OK
) {
1608 * We must ensure that "local_flattened_events" is never resized so as
1609 * to preserve the validity of the flattened objects.
1611 ret
= lttng_dynamic_buffer_set_capacity(
1612 &local_flattened_events
, storage_req
);
1614 ret_code
= LTTNG_ERR_NOMEM
;
1618 /* Start by laying the struct lttng_event */
1619 for (i
= 0; i
< nb_events
; i
++) {
1620 struct event_list_element
*element
=
1621 lttng_dynamic_pointer_array_get_pointer(
1625 ret_code
= LTTNG_ERR_FATAL
;
1628 ret
= lttng_dynamic_buffer_append(&local_flattened_events
,
1629 element
->event
, sizeof(struct lttng_event
));
1631 ret_code
= LTTNG_ERR_NOMEM
;
1636 for (i
= 0; i
< nb_events
; i
++) {
1637 struct event_list_element
*element
= lttng_dynamic_pointer_array_get_pointer(events
, i
);
1638 struct lttng_event
*event
= (struct lttng_event
*)
1639 (local_flattened_events
.data
+ (sizeof(struct lttng_event
) * i
));
1640 struct lttng_event_extended
*event_extended
=
1641 (struct lttng_event_extended
*)
1642 (local_flattened_events
.data
+ local_flattened_events
.size
);
1643 const struct lttng_userspace_probe_location
*location
= NULL
;
1647 /* Insert struct lttng_event_extended. */
1648 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1649 local_flattened_events
.size
+
1650 sizeof(*event_extended
));
1652 ret_code
= LTTNG_ERR_NOMEM
;
1655 event
->extended
.ptr
= event_extended
;
1657 /* Insert filter expression. */
1658 if (element
->filter_expression
) {
1659 size_t len
= strlen(element
->filter_expression
) + 1;
1661 event_extended
->filter_expression
=
1662 local_flattened_events
.data
+
1663 local_flattened_events
.size
;
1664 ret
= lttng_dynamic_buffer_append(
1665 &local_flattened_events
,
1666 element
->filter_expression
, len
);
1668 ret_code
= LTTNG_ERR_NOMEM
;
1673 /* Insert exclusions. */
1674 if (element
->exclusions
) {
1675 event_extended
->exclusions
.count
=
1676 element
->exclusions
->count
;
1677 event_extended
->exclusions
.strings
=
1678 local_flattened_events
.data
+
1679 local_flattened_events
.size
;
1681 ret
= lttng_dynamic_buffer_append(
1682 &local_flattened_events
,
1683 element
->exclusions
->names
,
1684 element
->exclusions
->count
*
1685 LTTNG_SYMBOL_NAME_LEN
);
1687 ret_code
= LTTNG_ERR_NOMEM
;
1692 /* Insert padding to align to 64-bits. */
1693 ret
= lttng_dynamic_buffer_set_size(&local_flattened_events
,
1694 ALIGN_TO(local_flattened_events
.size
,
1697 ret_code
= LTTNG_ERR_NOMEM
;
1701 location
= lttng_event_get_userspace_probe_location(
1704 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
1705 (local_flattened_events
.data
+ local_flattened_events
.size
);
1706 ret
= lttng_userspace_probe_location_flatten(
1707 location
, &local_flattened_events
);
1709 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
1715 /* Don't reset local_flattened_events buffer as we return its content. */
1716 *flattened_events
= (struct lttng_event
*) local_flattened_events
.data
;
1717 lttng_dynamic_buffer_init(&local_flattened_events
);
1718 ret_code
= LTTNG_OK
;
1720 lttng_dynamic_buffer_reset(&local_flattened_events
);
1724 static enum lttng_error_code
event_list_create_from_buffer(
1725 const struct lttng_buffer_view
*view
,
1727 struct lttng_dynamic_pointer_array
*event_list
)
1729 enum lttng_error_code ret_code
;
1737 for (i
= 0; i
< count
; i
++) {
1739 const struct lttng_buffer_view event_view
=
1740 lttng_buffer_view_from_view(view
, offset
, -1);
1741 struct event_list_element
*element
= zmalloc(sizeof(*element
));
1744 ret_code
= LTTNG_ERR_NOMEM
;
1749 * Lifetime and management of the object is now bound to the
1752 ret
= lttng_dynamic_pointer_array_add_pointer(
1753 event_list
, element
);
1755 event_list_destructor(element
);
1756 ret_code
= LTTNG_ERR_NOMEM
;
1761 * Bytecode is not transmitted on listing in any case we do not
1764 event_size
= lttng_event_create_from_buffer(&event_view
,
1766 &element
->exclusions
,
1767 &element
->filter_expression
, NULL
, -1);
1768 if (event_size
< 0) {
1769 ret_code
= LTTNG_ERR_INVALID
;
1773 offset
+= event_size
;
1776 if (view
->size
!= offset
) {
1777 ret_code
= LTTNG_ERR_INVALID
;
1781 ret_code
= LTTNG_OK
;
1788 enum lttng_error_code
lttng_events_create_and_flatten_from_buffer(
1789 const struct lttng_buffer_view
*view
,
1791 struct lttng_event
**events
)
1793 enum lttng_error_code ret
= LTTNG_OK
;
1794 struct lttng_dynamic_pointer_array local_events
;
1796 lttng_dynamic_pointer_array_init(&local_events
, event_list_destructor
);
1798 /* Deserialize the events */
1800 const struct lttng_buffer_view events_view
=
1801 lttng_buffer_view_from_view(view
, 0, -1);
1803 ret
= event_list_create_from_buffer(
1804 &events_view
, count
, &local_events
);
1805 if (ret
!= LTTNG_OK
) {
1810 ret
= flatten_lttng_events(&local_events
, events
);
1811 if (ret
!= LTTNG_OK
) {
1816 lttng_dynamic_pointer_array_reset(&local_events
);
1820 static enum lttng_error_code
flatten_lttng_event_fields(
1821 struct lttng_dynamic_pointer_array
*event_fields
,
1822 struct lttng_event_field
**flattened_event_fields
)
1825 enum lttng_error_code ret_code
;
1826 size_t storage_req
= 0;
1827 struct lttng_dynamic_buffer local_flattened_event_fields
;
1830 assert(event_fields
);
1831 assert(flattened_event_fields
);
1833 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1834 nb_event_field
= lttng_dynamic_pointer_array_get_count(event_fields
);
1837 * Here even if the event field contains a `struct lttng_event` that
1838 * could contain dynamic data, in reality it is not the case.
1839 * Dynamic data is not present. Here the flattening is mostly a direct
1840 * memcpy. This is less than ideal but this code is still better than
1841 * direct usage of an unpacked lttng_event_field array.
1843 storage_req
+= sizeof(struct lttng_event_field
) * nb_event_field
;
1845 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1848 * We must ensure that "local_flattened_event_fields" is never resized
1849 * so as to preserve the validity of the flattened objects.
1851 ret
= lttng_dynamic_buffer_set_capacity(
1852 &local_flattened_event_fields
, storage_req
);
1854 ret_code
= LTTNG_ERR_NOMEM
;
1858 for (i
= 0; i
< nb_event_field
; i
++) {
1859 const struct lttng_event_field
*element
=
1860 lttng_dynamic_pointer_array_get_pointer(
1864 ret_code
= LTTNG_ERR_FATAL
;
1867 ret
= lttng_dynamic_buffer_append(&local_flattened_event_fields
,
1868 element
, sizeof(struct lttng_event_field
));
1870 ret_code
= LTTNG_ERR_NOMEM
;
1875 /* Don't reset local_flattened_channels buffer as we return its content. */
1876 *flattened_event_fields
= (struct lttng_event_field
*) local_flattened_event_fields
.data
;
1877 lttng_dynamic_buffer_init(&local_flattened_event_fields
);
1878 ret_code
= LTTNG_OK
;
1880 lttng_dynamic_buffer_reset(&local_flattened_event_fields
);
1884 static enum lttng_error_code
event_field_list_create_from_buffer(
1885 const struct lttng_buffer_view
*view
,
1887 struct lttng_dynamic_pointer_array
**event_field_list
)
1889 enum lttng_error_code ret_code
;
1890 int ret
, offset
= 0;
1892 struct lttng_dynamic_pointer_array
*list
= NULL
;
1895 assert(event_field_list
);
1897 list
= zmalloc(sizeof(*list
));
1899 ret_code
= LTTNG_ERR_NOMEM
;
1903 lttng_dynamic_pointer_array_init(list
, free
);
1905 for (i
= 0; i
< count
; i
++) {
1906 ssize_t event_field_size
;
1907 struct lttng_event_field
*field
= NULL
;
1908 const struct lttng_buffer_view event_field_view
=
1909 lttng_buffer_view_from_view(view
, offset
, -1);
1911 event_field_size
= lttng_event_field_create_from_buffer(
1912 &event_field_view
, &field
);
1913 if (event_field_size
< 0) {
1914 ret_code
= LTTNG_ERR_INVALID
;
1918 /* Lifetime and management of the object is now bound to the array. */
1919 ret
= lttng_dynamic_pointer_array_add_pointer(list
, field
);
1922 ret_code
= LTTNG_ERR_NOMEM
;
1925 offset
+= event_field_size
;
1928 if (view
->size
!= offset
) {
1929 ret_code
= LTTNG_ERR_INVALID
;
1933 *event_field_list
= list
;
1935 ret_code
= LTTNG_OK
;
1939 lttng_dynamic_pointer_array_reset(list
);
1946 enum lttng_error_code
lttng_event_fields_create_and_flatten_from_buffer(
1947 const struct lttng_buffer_view
*view
,
1949 struct lttng_event_field
**fields
)
1951 enum lttng_error_code ret
;
1952 struct lttng_dynamic_pointer_array
*local_event_fields
= NULL
;
1954 ret
= event_field_list_create_from_buffer(
1955 view
, count
, &local_event_fields
);
1956 if (ret
!= LTTNG_OK
) {
1960 ret
= flatten_lttng_event_fields(local_event_fields
, fields
);
1961 if (ret
!= LTTNG_OK
) {
1965 if (local_event_fields
) {
1966 lttng_dynamic_pointer_array_reset(local_event_fields
);
1967 free(local_event_fields
);