Commit | Line | Data |
---|---|---|
0c51e8f3 SM |
1 | /* |
2 | * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: LGPL-2.1-only | |
5 | * | |
6 | */ | |
7 | ||
0c51e8f3 | 8 | #include <common/dynamic-array.h> |
0c51e8f3 SM |
9 | #include <common/error.h> |
10 | #include <common/macros.h> | |
6a751b95 JR |
11 | #include <common/mi-lttng.h> |
12 | #include <common/payload-view.h> | |
13 | #include <common/payload.h> | |
0c51e8f3 | 14 | #include <lttng/action/action-internal.h> |
ad63a966 JR |
15 | #include <lttng/action/list-internal.h> |
16 | #include <lttng/action/list.h> | |
0c51e8f3 | 17 | |
7c2fae7c JG |
18 | #define IS_LIST_ACTION(action) \ |
19 | (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_LIST) | |
0c51e8f3 | 20 | |
702f26c8 | 21 | struct lttng_action_list { |
0c51e8f3 SM |
22 | struct lttng_action parent; |
23 | ||
24 | /* The array owns the action elements. */ | |
25 | struct lttng_dynamic_pointer_array actions; | |
26 | }; | |
27 | ||
702f26c8 | 28 | struct lttng_action_list_comm { |
0c51e8f3 SM |
29 | uint32_t action_count; |
30 | ||
31 | /* | |
32 | * Variable data: each element serialized sequentially. | |
33 | */ | |
34 | char data[]; | |
35 | } LTTNG_PACKED; | |
36 | ||
702f26c8 | 37 | static void destroy_lttng_action_list_element(void *ptr) |
0c51e8f3 SM |
38 | { |
39 | struct lttng_action *element = (struct lttng_action *) ptr; | |
40 | ||
41 | lttng_action_destroy(element); | |
42 | } | |
43 | ||
702f26c8 | 44 | static struct lttng_action_list *action_list_from_action( |
0c51e8f3 SM |
45 | const struct lttng_action *action) |
46 | { | |
a0377dfe | 47 | LTTNG_ASSERT(action); |
0c51e8f3 | 48 | |
702f26c8 | 49 | return container_of(action, struct lttng_action_list, parent); |
0c51e8f3 SM |
50 | } |
51 | ||
702f26c8 | 52 | static const struct lttng_action_list *action_list_from_action_const( |
0c51e8f3 SM |
53 | const struct lttng_action *action) |
54 | { | |
a0377dfe | 55 | LTTNG_ASSERT(action); |
0c51e8f3 | 56 | |
702f26c8 | 57 | return container_of(action, struct lttng_action_list, parent); |
0c51e8f3 SM |
58 | } |
59 | ||
702f26c8 | 60 | static bool lttng_action_list_validate(struct lttng_action *action) |
0c51e8f3 SM |
61 | { |
62 | unsigned int i, count; | |
702f26c8 | 63 | struct lttng_action_list *action_list; |
0c51e8f3 SM |
64 | bool valid; |
65 | ||
a0377dfe | 66 | LTTNG_ASSERT(IS_LIST_ACTION(action)); |
0c51e8f3 | 67 | |
702f26c8 | 68 | action_list = action_list_from_action(action); |
0c51e8f3 | 69 | |
702f26c8 | 70 | count = lttng_dynamic_pointer_array_get_count(&action_list->actions); |
0c51e8f3 SM |
71 | |
72 | for (i = 0; i < count; i++) { | |
73 | struct lttng_action *child = | |
a6bc4ca9 | 74 | (lttng_action *) lttng_dynamic_pointer_array_get_pointer( |
702f26c8 | 75 | &action_list->actions, i); |
0c51e8f3 | 76 | |
a0377dfe | 77 | LTTNG_ASSERT(child); |
0c51e8f3 SM |
78 | |
79 | if (!lttng_action_validate(child)) { | |
80 | valid = false; | |
81 | goto end; | |
82 | } | |
83 | } | |
84 | ||
85 | valid = true; | |
86 | ||
87 | end: | |
88 | return valid; | |
89 | } | |
90 | ||
702f26c8 | 91 | static bool lttng_action_list_is_equal( |
0c51e8f3 SM |
92 | const struct lttng_action *_a, const struct lttng_action *_b) |
93 | { | |
94 | bool is_equal = false; | |
95 | unsigned int i; | |
96 | unsigned int a_count, b_count; | |
97 | ||
702f26c8 | 98 | if (lttng_action_list_get_count(_a, &a_count) != |
0c51e8f3 SM |
99 | LTTNG_ACTION_STATUS_OK) { |
100 | goto end; | |
101 | } | |
102 | ||
702f26c8 | 103 | if (lttng_action_list_get_count(_b, &b_count) != |
0c51e8f3 SM |
104 | LTTNG_ACTION_STATUS_OK) { |
105 | goto end; | |
106 | } | |
107 | ||
108 | if (a_count != b_count) { | |
109 | goto end; | |
110 | } | |
111 | ||
112 | for (i = 0; i < a_count; i++) { | |
113 | const struct lttng_action *child_a = | |
702f26c8 | 114 | lttng_action_list_get_at_index(_a, i); |
0c51e8f3 | 115 | const struct lttng_action *child_b = |
702f26c8 | 116 | lttng_action_list_get_at_index(_b, i); |
0c51e8f3 | 117 | |
a0377dfe FD |
118 | LTTNG_ASSERT(child_a); |
119 | LTTNG_ASSERT(child_b); | |
0c51e8f3 SM |
120 | |
121 | if (!lttng_action_is_equal(child_a, child_b)) { | |
122 | goto end; | |
123 | } | |
124 | } | |
125 | ||
126 | is_equal = true; | |
127 | end: | |
128 | return is_equal; | |
129 | } | |
130 | ||
702f26c8 | 131 | static int lttng_action_list_serialize( |
0c51e8f3 SM |
132 | struct lttng_action *action, struct lttng_payload *payload) |
133 | { | |
702f26c8 JR |
134 | struct lttng_action_list *action_list; |
135 | struct lttng_action_list_comm comm; | |
0c51e8f3 SM |
136 | int ret; |
137 | unsigned int i, count; | |
138 | ||
a0377dfe FD |
139 | LTTNG_ASSERT(action); |
140 | LTTNG_ASSERT(payload); | |
141 | LTTNG_ASSERT(IS_LIST_ACTION(action)); | |
0c51e8f3 | 142 | |
702f26c8 | 143 | action_list = action_list_from_action(action); |
0c51e8f3 | 144 | |
702f26c8 | 145 | DBG("Serializing action list"); |
0c51e8f3 | 146 | |
702f26c8 | 147 | count = lttng_dynamic_pointer_array_get_count(&action_list->actions); |
0c51e8f3 SM |
148 | |
149 | comm.action_count = count; | |
150 | ||
151 | ret = lttng_dynamic_buffer_append( | |
152 | &payload->buffer, &comm, sizeof(comm)); | |
153 | if (ret) { | |
154 | ret = -1; | |
155 | goto end; | |
156 | } | |
157 | ||
158 | for (i = 0; i < count; i++) { | |
159 | struct lttng_action *child = | |
a6bc4ca9 | 160 | (lttng_action *) lttng_dynamic_pointer_array_get_pointer( |
702f26c8 | 161 | &action_list->actions, i); |
0c51e8f3 | 162 | |
a0377dfe | 163 | LTTNG_ASSERT(child); |
0c51e8f3 SM |
164 | |
165 | ret = lttng_action_serialize(child, payload); | |
166 | if (ret) { | |
167 | goto end; | |
168 | } | |
169 | } | |
170 | ||
171 | ret = 0; | |
172 | ||
173 | end: | |
174 | return ret; | |
175 | } | |
176 | ||
702f26c8 | 177 | static void lttng_action_list_destroy(struct lttng_action *action) |
0c51e8f3 | 178 | { |
702f26c8 | 179 | struct lttng_action_list *action_list; |
0c51e8f3 SM |
180 | |
181 | if (!action) { | |
182 | goto end; | |
183 | } | |
184 | ||
702f26c8 JR |
185 | action_list = action_list_from_action(action); |
186 | lttng_dynamic_pointer_array_reset(&action_list->actions); | |
187 | free(action_list); | |
0c51e8f3 SM |
188 | |
189 | end: | |
190 | return; | |
191 | } | |
192 | ||
702f26c8 | 193 | ssize_t lttng_action_list_create_from_payload( |
0c51e8f3 SM |
194 | struct lttng_payload_view *view, |
195 | struct lttng_action **p_action) | |
196 | { | |
197 | ssize_t consumed_len; | |
702f26c8 | 198 | const struct lttng_action_list_comm *comm; |
7c2fae7c | 199 | struct lttng_action *list; |
0c51e8f3 SM |
200 | struct lttng_action *child_action = NULL; |
201 | enum lttng_action_status status; | |
202 | size_t i; | |
203 | ||
7c2fae7c JG |
204 | list = lttng_action_list_create(); |
205 | if (!list) { | |
0c51e8f3 SM |
206 | consumed_len = -1; |
207 | goto end; | |
208 | } | |
209 | ||
210 | comm = (typeof(comm)) view->buffer.data; | |
211 | ||
702f26c8 | 212 | consumed_len = sizeof(struct lttng_action_list_comm); |
0c51e8f3 SM |
213 | |
214 | for (i = 0; i < comm->action_count; i++) { | |
215 | ssize_t consumed_len_child; | |
216 | struct lttng_payload_view child_view = | |
217 | lttng_payload_view_from_view(view, consumed_len, | |
218 | view->buffer.size - consumed_len); | |
219 | ||
3e6e0df2 JG |
220 | if (!lttng_payload_view_is_valid(&child_view)) { |
221 | consumed_len = -1; | |
222 | goto end; | |
223 | } | |
224 | ||
0c51e8f3 SM |
225 | consumed_len_child = lttng_action_create_from_payload( |
226 | &child_view, &child_action); | |
227 | if (consumed_len_child < 0) { | |
228 | consumed_len = -1; | |
229 | goto end; | |
230 | } | |
231 | ||
7c2fae7c | 232 | status = lttng_action_list_add_action(list, child_action); |
0c51e8f3 SM |
233 | if (status != LTTNG_ACTION_STATUS_OK) { |
234 | consumed_len = -1; | |
235 | goto end; | |
236 | } | |
237 | ||
702f26c8 | 238 | /* Transfer ownership to the action list. */ |
0c51e8f3 SM |
239 | lttng_action_put(child_action); |
240 | child_action = NULL; | |
241 | ||
242 | consumed_len += consumed_len_child; | |
243 | } | |
244 | ||
7c2fae7c JG |
245 | *p_action = list; |
246 | list = NULL; | |
0c51e8f3 SM |
247 | |
248 | end: | |
7c2fae7c | 249 | lttng_action_list_destroy(list); |
0c51e8f3 SM |
250 | return consumed_len; |
251 | } | |
252 | ||
702f26c8 | 253 | static enum lttng_action_status lttng_action_list_add_error_query_results( |
588c4b0d JG |
254 | const struct lttng_action *action, |
255 | struct lttng_error_query_results *results) | |
256 | { | |
257 | unsigned int i, count; | |
258 | enum lttng_action_status action_status; | |
7c2fae7c JG |
259 | const struct lttng_action_list *list = |
260 | container_of(action, typeof(*list), parent); | |
588c4b0d | 261 | |
702f26c8 | 262 | action_status = lttng_action_list_get_count(action, &count); |
588c4b0d JG |
263 | if (action_status != LTTNG_ACTION_STATUS_OK) { |
264 | goto end; | |
265 | } | |
266 | ||
267 | for (i = 0; i < count; i++) { | |
268 | struct lttng_action *inner_action = | |
702f26c8 | 269 | lttng_action_list_borrow_mutable_at_index(action, i); |
588c4b0d JG |
270 | |
271 | action_status = lttng_action_add_error_query_results( | |
272 | inner_action, results); | |
273 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
274 | goto end; | |
275 | } | |
276 | } | |
277 | end: | |
278 | return action_status; | |
279 | } | |
280 | ||
6a751b95 JR |
281 | enum lttng_error_code lttng_action_list_mi_serialize( |
282 | const struct lttng_trigger *trigger, | |
283 | const struct lttng_action *action, | |
284 | struct mi_writer *writer, | |
285 | const struct mi_lttng_error_query_callbacks | |
286 | *error_query_callbacks, | |
287 | struct lttng_dynamic_array *action_path_indexes) | |
288 | { | |
289 | int ret; | |
290 | struct lttng_action_list *action_list; | |
291 | unsigned int i, count; | |
292 | enum lttng_error_code ret_code; | |
293 | ||
a0377dfe FD |
294 | LTTNG_ASSERT(action); |
295 | LTTNG_ASSERT(IS_LIST_ACTION(action)); | |
296 | LTTNG_ASSERT(writer); | |
6a751b95 JR |
297 | |
298 | /* Open action list. */ | |
299 | ret = mi_lttng_writer_open_element( | |
300 | writer, mi_lttng_element_action_list); | |
301 | if (ret) { | |
302 | goto mi_error; | |
303 | } | |
304 | ||
305 | /* Serialize every action of the list. */ | |
306 | action_list = action_list_from_action(action); | |
307 | count = lttng_dynamic_pointer_array_get_count(&action_list->actions); | |
308 | for (i = 0; i < count; i++) { | |
309 | const struct lttng_action *child = | |
310 | lttng_action_list_get_at_index(action, i); | |
311 | const uint64_t index = (uint64_t) i; | |
312 | ||
a0377dfe | 313 | LTTNG_ASSERT(child); |
6a751b95 JR |
314 | |
315 | /* | |
316 | * Add the index to the action path. | |
317 | * | |
318 | * This index is replaced on every iteration to walk the action | |
319 | * tree in-order and to re-use the dynamic array instead of | |
320 | * copying it at every level. | |
321 | */ | |
322 | ret = lttng_dynamic_array_add_element( | |
323 | action_path_indexes, &index); | |
324 | if (ret) { | |
325 | ret_code = LTTNG_ERR_NOMEM; | |
326 | goto end; | |
327 | } | |
328 | ||
329 | ret_code = lttng_action_mi_serialize(trigger, child, writer, | |
330 | error_query_callbacks, action_path_indexes); | |
331 | if (ret_code != LTTNG_OK) { | |
332 | goto end; | |
333 | } | |
334 | ||
335 | ret = lttng_dynamic_array_remove_element(action_path_indexes, | |
336 | lttng_dynamic_array_get_count( | |
337 | action_path_indexes) - | |
338 | 1); | |
339 | if (ret) { | |
340 | ret_code = LTTNG_ERR_UNK; | |
341 | goto end; | |
342 | } | |
343 | } | |
344 | ||
345 | /* Close action_list element. */ | |
346 | ret = mi_lttng_writer_close_element(writer); | |
347 | if (ret) { | |
348 | goto mi_error; | |
349 | } | |
350 | ||
351 | ret_code = LTTNG_OK; | |
352 | goto end; | |
353 | ||
354 | mi_error: | |
355 | ret_code = LTTNG_ERR_MI_IO_FAIL; | |
356 | end: | |
357 | return ret_code; | |
358 | } | |
359 | ||
702f26c8 | 360 | struct lttng_action *lttng_action_list_create(void) |
0c51e8f3 | 361 | { |
702f26c8 | 362 | struct lttng_action_list *action_list; |
0c51e8f3 SM |
363 | struct lttng_action *action; |
364 | ||
a6bc4ca9 | 365 | action_list = (lttng_action_list *) zmalloc(sizeof(struct lttng_action_list)); |
702f26c8 | 366 | if (!action_list) { |
0c51e8f3 SM |
367 | action = NULL; |
368 | goto end; | |
369 | } | |
370 | ||
702f26c8 | 371 | action = &action_list->parent; |
0c51e8f3 | 372 | |
6a751b95 JR |
373 | /* |
374 | * The mi for the list is handled at the lttng_action_mi level to ease | |
375 | * action path management for error query. | |
376 | */ | |
7c2fae7c | 377 | lttng_action_init(action, LTTNG_ACTION_TYPE_LIST, |
6a751b95 | 378 | lttng_action_list_validate, lttng_action_list_serialize, |
702f26c8 | 379 | lttng_action_list_is_equal, lttng_action_list_destroy, |
6a751b95 | 380 | NULL, lttng_action_list_add_error_query_results, NULL); |
0c51e8f3 | 381 | |
702f26c8 JR |
382 | lttng_dynamic_pointer_array_init(&action_list->actions, |
383 | destroy_lttng_action_list_element); | |
0c51e8f3 SM |
384 | |
385 | end: | |
386 | return action; | |
387 | } | |
388 | ||
702f26c8 | 389 | enum lttng_action_status lttng_action_list_add_action( |
7c2fae7c | 390 | struct lttng_action *list, struct lttng_action *action) |
0c51e8f3 | 391 | { |
702f26c8 | 392 | struct lttng_action_list *action_list; |
0c51e8f3 SM |
393 | enum lttng_action_status status; |
394 | int ret; | |
395 | ||
7c2fae7c | 396 | if (!list || !IS_LIST_ACTION(list) || !action) { |
0c51e8f3 SM |
397 | status = LTTNG_ACTION_STATUS_INVALID; |
398 | goto end; | |
399 | } | |
400 | ||
401 | /* | |
7c2fae7c | 402 | * Don't allow adding lists in lists for now, since we're afraid of |
0c51e8f3 SM |
403 | * cycles. |
404 | */ | |
7c2fae7c | 405 | if (IS_LIST_ACTION(action)) { |
0c51e8f3 SM |
406 | status = LTTNG_ACTION_STATUS_INVALID; |
407 | goto end; | |
408 | } | |
409 | ||
7c2fae7c | 410 | action_list = action_list_from_action(list); |
0c51e8f3 | 411 | |
702f26c8 | 412 | ret = lttng_dynamic_pointer_array_add_pointer(&action_list->actions, |
0c51e8f3 SM |
413 | action); |
414 | if (ret < 0) { | |
415 | status = LTTNG_ACTION_STATUS_ERROR; | |
416 | goto end; | |
417 | } | |
418 | ||
419 | /* Take ownership of the object. */ | |
420 | lttng_action_get(action); | |
421 | status = LTTNG_ACTION_STATUS_OK; | |
422 | end: | |
423 | return status; | |
424 | } | |
425 | ||
702f26c8 | 426 | enum lttng_action_status lttng_action_list_get_count( |
7c2fae7c | 427 | const struct lttng_action *list, unsigned int *count) |
0c51e8f3 | 428 | { |
702f26c8 | 429 | const struct lttng_action_list *action_list; |
0c51e8f3 SM |
430 | enum lttng_action_status status = LTTNG_ACTION_STATUS_OK; |
431 | ||
7c2fae7c | 432 | if (!list || !IS_LIST_ACTION(list)) { |
0c51e8f3 SM |
433 | status = LTTNG_ACTION_STATUS_INVALID; |
434 | *count = 0; | |
435 | goto end; | |
436 | } | |
437 | ||
7c2fae7c | 438 | action_list = action_list_from_action_const(list); |
702f26c8 | 439 | *count = lttng_dynamic_pointer_array_get_count(&action_list->actions); |
0c51e8f3 SM |
440 | end: |
441 | return status; | |
442 | } | |
443 | ||
702f26c8 | 444 | const struct lttng_action *lttng_action_list_get_at_index( |
7c2fae7c | 445 | const struct lttng_action *list, unsigned int index) |
2d57482c | 446 | { |
7c2fae7c | 447 | return lttng_action_list_borrow_mutable_at_index(list, index); |
2d57482c JR |
448 | } |
449 | ||
702f26c8 | 450 | struct lttng_action *lttng_action_list_borrow_mutable_at_index( |
7c2fae7c | 451 | const struct lttng_action *list, unsigned int index) |
0c51e8f3 SM |
452 | { |
453 | unsigned int count; | |
702f26c8 | 454 | const struct lttng_action_list *action_list; |
2d57482c | 455 | struct lttng_action *action = NULL; |
0c51e8f3 | 456 | |
7c2fae7c | 457 | if (lttng_action_list_get_count(list, &count) != |
0c51e8f3 SM |
458 | LTTNG_ACTION_STATUS_OK) { |
459 | goto end; | |
460 | } | |
461 | ||
462 | if (index >= count) { | |
463 | goto end; | |
464 | } | |
465 | ||
7c2fae7c | 466 | action_list = action_list_from_action_const(list); |
a6bc4ca9 | 467 | action = (lttng_action *) lttng_dynamic_pointer_array_get_pointer(&action_list->actions, |
0c51e8f3 SM |
468 | index); |
469 | end: | |
470 | return action; | |
471 | } |