2 * Copyright (C) 2021 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/action/path-internal.h>
10 struct lttng_action_path_comm
{
15 struct lttng_action_path
*lttng_action_path_create(
16 const uint64_t *indexes
, size_t index_count
)
20 struct lttng_action_path
*path
= NULL
;
22 if (!indexes
&& index_count
> 0) {
26 path
= (lttng_action_path
*) zmalloc(sizeof(*path
));
31 lttng_dynamic_array_init(&path
->indexes
, sizeof(uint64_t), NULL
);
33 for (i
= 0; i
< index_count
; i
++) {
34 ret
= lttng_dynamic_array_add_element(
35 &path
->indexes
, &indexes
[i
]);
43 lttng_action_path_destroy(path
);
49 enum lttng_action_path_status
lttng_action_path_get_index_count(
50 const struct lttng_action_path
*path
, size_t *index_count
)
52 enum lttng_action_path_status status
;
54 if (!path
|| !index_count
) {
55 status
= LTTNG_ACTION_PATH_STATUS_INVALID
;
59 *index_count
= lttng_dynamic_array_get_count(&path
->indexes
);
60 status
= LTTNG_ACTION_PATH_STATUS_OK
;
65 enum lttng_action_path_status
lttng_action_path_get_index_at_index(
66 const struct lttng_action_path
*path
,
70 enum lttng_action_path_status status
;
72 if (!path
|| !out_index
||
73 path_index
>= lttng_dynamic_array_get_count(
75 status
= LTTNG_ACTION_PATH_STATUS_INVALID
;
79 *out_index
= *((typeof(out_index
)) lttng_dynamic_array_get_element(
80 &path
->indexes
, path_index
));
81 status
= LTTNG_ACTION_PATH_STATUS_OK
;
86 void lttng_action_path_destroy(struct lttng_action_path
*action_path
)
92 lttng_dynamic_array_reset(&action_path
->indexes
);
98 int lttng_action_path_copy(const struct lttng_action_path
*src
,
99 struct lttng_action_path
*dst
)
107 lttng_dynamic_array_init(&dst
->indexes
, sizeof(uint64_t), NULL
);
108 src_count
= lttng_dynamic_array_get_count(&src
->indexes
);
110 for (i
= 0; i
< src_count
; i
++) {
111 const void *index
= lttng_dynamic_array_get_element(
114 ret
= lttng_dynamic_array_add_element(&dst
->indexes
, index
);
123 lttng_dynamic_array_reset(&dst
->indexes
);
128 ssize_t
lttng_action_path_create_from_payload(
129 struct lttng_payload_view
*view
,
130 struct lttng_action_path
**_action_path
)
132 ssize_t consumed_size
= 0, ret
= -1;
133 const struct lttng_action_path_comm
*header
;
134 struct lttng_action_path
*action_path
= NULL
;
135 const struct lttng_payload_view header_view
=
136 lttng_payload_view_from_view(view
, 0, sizeof(*header
));
138 if (!lttng_payload_view_is_valid(&header_view
)) {
142 header
= (typeof(header
)) header_view
.buffer
.data
;
143 consumed_size
+= header_view
.buffer
.size
;
146 * An action path of size 0 can exist and represents a trigger with a
147 * single non-list action. Handle it differently since a payload view of
148 * size 0 is considered invalid.
150 if (header
->index_count
!= 0)
152 const struct lttng_payload_view indexes_view
=
153 lttng_payload_view_from_view(view
,
155 header
->index_count
*
158 if (!lttng_payload_view_is_valid(&indexes_view
)) {
162 consumed_size
+= indexes_view
.buffer
.size
;
163 action_path
= lttng_action_path_create(
164 (const uint64_t *) indexes_view
.buffer
.data
,
165 header
->index_count
);
170 action_path
= lttng_action_path_create(NULL
, 0);
177 *_action_path
= action_path
;
182 int lttng_action_path_serialize(const struct lttng_action_path
*action_path
,
183 struct lttng_payload
*payload
)
186 size_t index_count
, i
;
187 enum lttng_action_path_status status
;
188 lttng_action_path_comm comm
;
190 status
= lttng_action_path_get_index_count(action_path
, &index_count
);
191 if (status
!= LTTNG_ACTION_PATH_STATUS_OK
) {
197 .index_count
= (uint32_t) index_count
,
199 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
201 sizeof(struct lttng_action_path_comm
));
203 for (i
= 0; i
< index_count
; i
++) {
206 status
= lttng_action_path_get_index_at_index(
207 action_path
, i
, &path_index
);
208 if (status
!= LTTNG_ACTION_PATH_STATUS_OK
) {
213 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &path_index
,