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.hpp>
11 struct lttng_action_path_comm
{
17 struct lttng_action_path
*lttng_action_path_create(
18 const uint64_t *indexes
, size_t index_count
)
22 struct lttng_action_path
*path
= NULL
;
24 if (!indexes
&& index_count
> 0) {
28 path
= zmalloc
<lttng_action_path
>();
33 lttng_dynamic_array_init(&path
->indexes
, sizeof(uint64_t), NULL
);
35 for (i
= 0; i
< index_count
; i
++) {
36 ret
= lttng_dynamic_array_add_element(
37 &path
->indexes
, &indexes
[i
]);
45 lttng_action_path_destroy(path
);
51 enum lttng_action_path_status
lttng_action_path_get_index_count(
52 const struct lttng_action_path
*path
, size_t *index_count
)
54 enum lttng_action_path_status status
;
56 if (!path
|| !index_count
) {
57 status
= LTTNG_ACTION_PATH_STATUS_INVALID
;
61 *index_count
= lttng_dynamic_array_get_count(&path
->indexes
);
62 status
= LTTNG_ACTION_PATH_STATUS_OK
;
67 enum lttng_action_path_status
lttng_action_path_get_index_at_index(
68 const struct lttng_action_path
*path
,
72 enum lttng_action_path_status status
;
74 if (!path
|| !out_index
||
75 path_index
>= lttng_dynamic_array_get_count(
77 status
= LTTNG_ACTION_PATH_STATUS_INVALID
;
81 *out_index
= *((typeof(out_index
)) lttng_dynamic_array_get_element(
82 &path
->indexes
, path_index
));
83 status
= LTTNG_ACTION_PATH_STATUS_OK
;
88 void lttng_action_path_destroy(struct lttng_action_path
*action_path
)
94 lttng_dynamic_array_reset(&action_path
->indexes
);
100 int lttng_action_path_copy(const struct lttng_action_path
*src
,
101 struct lttng_action_path
**dst
)
104 struct lttng_action_path
*new_path
;
109 new_path
= lttng_action_path_create(
110 (uint64_t *) lttng_dynamic_array_get_element(
112 lttng_dynamic_array_get_count(&src
->indexes
));
123 ssize_t
lttng_action_path_create_from_payload(
124 struct lttng_payload_view
*view
,
125 struct lttng_action_path
**_action_path
)
127 ssize_t consumed_size
= 0, ret
= -1;
128 const struct lttng_action_path_comm
*header
;
129 struct lttng_action_path
*action_path
= NULL
;
130 const struct lttng_payload_view header_view
=
131 lttng_payload_view_from_view(view
, 0, sizeof(*header
));
133 if (!lttng_payload_view_is_valid(&header_view
)) {
137 header
= (typeof(header
)) header_view
.buffer
.data
;
138 consumed_size
+= header_view
.buffer
.size
;
141 * An action path of size 0 can exist and represents a trigger with a
142 * single non-list action. Handle it differently since a payload view of
143 * size 0 is considered invalid.
145 if (header
->index_count
!= 0)
147 const struct lttng_payload_view indexes_view
=
148 lttng_payload_view_from_view(view
,
150 header
->index_count
*
153 if (!lttng_payload_view_is_valid(&indexes_view
)) {
157 consumed_size
+= indexes_view
.buffer
.size
;
158 action_path
= lttng_action_path_create(
159 (const uint64_t *) indexes_view
.buffer
.data
,
160 header
->index_count
);
165 action_path
= lttng_action_path_create(NULL
, 0);
172 *_action_path
= action_path
;
177 int lttng_action_path_serialize(const struct lttng_action_path
*action_path
,
178 struct lttng_payload
*payload
)
181 size_t index_count
, i
;
182 enum lttng_action_path_status status
;
183 lttng_action_path_comm comm
;
185 status
= lttng_action_path_get_index_count(action_path
, &index_count
);
186 if (status
!= LTTNG_ACTION_PATH_STATUS_OK
) {
192 .index_count
= (uint32_t) index_count
,
194 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
196 sizeof(struct lttng_action_path_comm
));
198 for (i
= 0; i
< index_count
; i
++) {
201 status
= lttng_action_path_get_index_at_index(
202 action_path
, i
, &path_index
);
203 if (status
!= LTTNG_ACTION_PATH_STATUS_OK
) {
208 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &path_index
,