Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / src / common / actions / action.c
CommitLineData
a58c490f 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
a58c490f 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
a58c490f 5 *
a58c490f
JG
6 */
7
14ec7e87 8#include <common/error.h>
6a751b95 9#include <common/mi-lttng.h>
a58c490f 10#include <lttng/action/action-internal.h>
ad63a966 11#include <lttng/action/list-internal.h>
a58c490f 12#include <lttng/action/notify-internal.h>
7f4d5b07 13#include <lttng/action/rate-policy-internal.h>
bfb2ec6a 14#include <lttng/action/rotate-session-internal.h>
757c48a2 15#include <lttng/action/snapshot-session-internal.h>
58397d0d 16#include <lttng/action/start-session-internal.h>
931bdbaa 17#include <lttng/action/stop-session-internal.h>
588c4b0d 18#include <lttng/error-query-internal.h>
a58c490f 19
10615eee
JR
20LTTNG_HIDDEN
21const char *lttng_action_type_string(enum lttng_action_type action_type)
2666d352
SM
22{
23 switch (action_type) {
24 case LTTNG_ACTION_TYPE_UNKNOWN:
25 return "UNKNOWN";
7c2fae7c
JG
26 case LTTNG_ACTION_TYPE_LIST:
27 return "LIST";
2666d352
SM
28 case LTTNG_ACTION_TYPE_NOTIFY:
29 return "NOTIFY";
bfb2ec6a
SM
30 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
31 return "ROTATE_SESSION";
757c48a2
SM
32 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
33 return "SNAPSHOT_SESSION";
58397d0d
SM
34 case LTTNG_ACTION_TYPE_START_SESSION:
35 return "START_SESSION";
931bdbaa
SM
36 case LTTNG_ACTION_TYPE_STOP_SESSION:
37 return "STOP_SESSION";
2666d352
SM
38 default:
39 return "???";
40 }
41}
42
17182cfd 43enum lttng_action_type lttng_action_get_type(const struct lttng_action *action)
a58c490f
JG
44{
45 return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN;
46}
47
6acb3f46 48LTTNG_HIDDEN
2d57482c 49void lttng_action_init(struct lttng_action *action,
6acb3f46
SM
50 enum lttng_action_type type,
51 action_validate_cb validate,
52 action_serialize_cb serialize,
3dd04a6a 53 action_equal_cb equal,
2d57482c 54 action_destroy_cb destroy,
588c4b0d 55 action_get_rate_policy_cb get_rate_policy,
6a751b95
JR
56 action_add_error_query_results_cb add_error_query_results,
57 action_mi_serialize_cb mi)
6acb3f46 58{
c852ce4e 59 urcu_ref_init(&action->ref);
6acb3f46
SM
60 action->type = type;
61 action->validate = validate;
62 action->serialize = serialize;
3dd04a6a 63 action->equal = equal;
6acb3f46 64 action->destroy = destroy;
7f4d5b07 65 action->get_rate_policy = get_rate_policy;
588c4b0d 66 action->add_error_query_results = add_error_query_results;
6a751b95 67 action->mi_serialize = mi;
2d57482c
JR
68
69 action->execution_request_counter = 0;
70 action->execution_counter = 0;
71 action->execution_failure_counter = 0;
6acb3f46
SM
72}
73
c852ce4e
JG
74static
75void action_destroy_ref(struct urcu_ref *ref)
76{
77 struct lttng_action *action =
78 container_of(ref, struct lttng_action, ref);
79
80 action->destroy(action);
81}
82
83LTTNG_HIDDEN
84void lttng_action_get(struct lttng_action *action)
85{
86 urcu_ref_get(&action->ref);
87}
88
89LTTNG_HIDDEN
90void lttng_action_put(struct lttng_action *action)
a58c490f
JG
91{
92 if (!action) {
93 return;
94 }
95
a0377dfe 96 LTTNG_ASSERT(action->destroy);
c852ce4e
JG
97 urcu_ref_put(&action->ref, action_destroy_ref);
98}
99
100void lttng_action_destroy(struct lttng_action *action)
101{
102 lttng_action_put(action);
a58c490f
JG
103}
104
105LTTNG_HIDDEN
106bool lttng_action_validate(struct lttng_action *action)
107{
108 bool valid;
109
110 if (!action) {
111 valid = false;
112 goto end;
113 }
114
115 if (!action->validate) {
116 /* Sub-class guarantees that it can never be invalid. */
117 valid = true;
118 goto end;
119 }
120
121 valid = action->validate(action);
122end:
123 return valid;
124}
125
126LTTNG_HIDDEN
3647288f 127int lttng_action_serialize(struct lttng_action *action,
c0a66c84 128 struct lttng_payload *payload)
a58c490f 129{
3647288f
JG
130 int ret;
131 struct lttng_action_comm action_comm = {
132 .action_type = (int8_t) action->type,
133 };
134
c0a66c84 135 ret = lttng_dynamic_buffer_append(&payload->buffer, &action_comm,
3647288f
JG
136 sizeof(action_comm));
137 if (ret) {
a58c490f
JG
138 goto end;
139 }
140
c0a66c84 141 ret = action->serialize(action, payload);
3647288f 142 if (ret) {
a58c490f
JG
143 goto end;
144 }
a58c490f
JG
145end:
146 return ret;
147}
148
149LTTNG_HIDDEN
c0a66c84 150ssize_t lttng_action_create_from_payload(struct lttng_payload_view *view,
869a3c2d 151 struct lttng_action **action)
a58c490f 152{
869a3c2d 153 ssize_t consumed_len, specific_action_consumed_len;
c0a66c84 154 action_create_from_payload_cb create_from_payload_cb;
3e6e0df2
JG
155 const struct lttng_action_comm *action_comm;
156 const struct lttng_payload_view action_comm_view =
157 lttng_payload_view_from_view(
158 view, 0, sizeof(*action_comm));
a58c490f 159
869a3c2d
SM
160 if (!view || !action) {
161 consumed_len = -1;
a58c490f
JG
162 goto end;
163 }
164
3e6e0df2
JG
165 if (!lttng_payload_view_is_valid(&action_comm_view)) {
166 /* Payload not large enough to contain the header. */
167 consumed_len = -1;
168 goto end;
169 }
170
171 action_comm = (const struct lttng_action_comm *) action_comm_view.buffer.data;
869a3c2d 172
c0a66c84 173 DBG("Create action from payload: action-type=%s",
2666d352
SM
174 lttng_action_type_string(action_comm->action_type));
175
a58c490f
JG
176 switch (action_comm->action_type) {
177 case LTTNG_ACTION_TYPE_NOTIFY:
c0a66c84 178 create_from_payload_cb = lttng_action_notify_create_from_payload;
a58c490f 179 break;
bfb2ec6a 180 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
c0a66c84
JG
181 create_from_payload_cb =
182 lttng_action_rotate_session_create_from_payload;
bfb2ec6a 183 break;
757c48a2
SM
184 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
185 create_from_payload_cb =
186 lttng_action_snapshot_session_create_from_payload;
187 break;
58397d0d 188 case LTTNG_ACTION_TYPE_START_SESSION:
c0a66c84
JG
189 create_from_payload_cb =
190 lttng_action_start_session_create_from_payload;
58397d0d 191 break;
931bdbaa 192 case LTTNG_ACTION_TYPE_STOP_SESSION:
c0a66c84
JG
193 create_from_payload_cb =
194 lttng_action_stop_session_create_from_payload;
931bdbaa 195 break;
7c2fae7c 196 case LTTNG_ACTION_TYPE_LIST:
702f26c8 197 create_from_payload_cb = lttng_action_list_create_from_payload;
0c51e8f3 198 break;
a58c490f 199 default:
c0a66c84 200 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
2666d352
SM
201 action_comm->action_type,
202 lttng_action_type_string(
203 action_comm->action_type));
869a3c2d 204 consumed_len = -1;
a58c490f
JG
205 goto end;
206 }
207
c0a66c84
JG
208 {
209 /* Create buffer view for the action-type-specific data. */
210 struct lttng_payload_view specific_action_view =
211 lttng_payload_view_from_view(view,
212 sizeof(struct lttng_action_comm),
213 -1);
869a3c2d 214
c0a66c84
JG
215 specific_action_consumed_len = create_from_payload_cb(
216 &specific_action_view, action);
217 }
869a3c2d
SM
218 if (specific_action_consumed_len < 0) {
219 ERR("Failed to create specific action from buffer.");
220 consumed_len = -1;
a58c490f
JG
221 goto end;
222 }
869a3c2d 223
a0377dfe 224 LTTNG_ASSERT(*action);
869a3c2d
SM
225
226 consumed_len = sizeof(struct lttng_action_comm) +
227 specific_action_consumed_len;
228
a58c490f 229end:
869a3c2d 230 return consumed_len;
a58c490f 231}
3dd04a6a
JR
232
233LTTNG_HIDDEN
234bool lttng_action_is_equal(const struct lttng_action *a,
235 const struct lttng_action *b)
236{
237 bool is_equal = false;
238
239 if (!a || !b) {
240 goto end;
241 }
242
243 if (a->type != b->type) {
244 goto end;
245 }
246
247 if (a == b) {
248 is_equal = true;
249 goto end;
250 }
251
a0377dfe 252 LTTNG_ASSERT(a->equal);
3dd04a6a
JR
253 is_equal = a->equal(a, b);
254end:
255 return is_equal;
256}
2d57482c
JR
257
258LTTNG_HIDDEN
259void lttng_action_increase_execution_request_count(struct lttng_action *action)
260{
261 action->execution_request_counter++;
262}
263
264LTTNG_HIDDEN
265void lttng_action_increase_execution_count(struct lttng_action *action)
266{
267 action->execution_counter++;
268}
269
270LTTNG_HIDDEN
271void lttng_action_increase_execution_failure_count(struct lttng_action *action)
272{
588c4b0d 273 uatomic_inc(&action->execution_failure_counter);
2d57482c
JR
274}
275
276LTTNG_HIDDEN
277bool lttng_action_should_execute(const struct lttng_action *action)
278{
7f4d5b07 279 const struct lttng_rate_policy *policy = NULL;
2d57482c
JR
280 bool execute = false;
281
7f4d5b07 282 if (action->get_rate_policy == NULL) {
2d57482c
JR
283 execute = true;
284 goto end;
285 }
286
7f4d5b07 287 policy = action->get_rate_policy(action);
2d57482c
JR
288 if (policy == NULL) {
289 execute = true;
290 goto end;
291 }
292
7f4d5b07 293 execute = lttng_rate_policy_should_execute(
2d57482c
JR
294 policy, action->execution_request_counter);
295end:
296 return execute;
297}
588c4b0d
JG
298
299LTTNG_HIDDEN
300enum lttng_action_status lttng_action_add_error_query_results(
301 const struct lttng_action *action,
302 struct lttng_error_query_results *results)
303{
304 return action->add_error_query_results(action, results);
305}
306
307LTTNG_HIDDEN
308enum lttng_action_status lttng_action_generic_add_error_query_results(
309 const struct lttng_action *action,
310 struct lttng_error_query_results *results)
311{
312 enum lttng_action_status action_status;
313 struct lttng_error_query_result *error_counter = NULL;
314 const uint64_t execution_failure_counter =
315 uatomic_read(&action->execution_failure_counter);
316
317 error_counter = lttng_error_query_result_counter_create(
318 "total execution failures",
319 "Aggregated count of errors encountered when executing the action",
320 execution_failure_counter);
321 if (!error_counter) {
322 action_status = LTTNG_ACTION_STATUS_ERROR;
323 goto end;
324 }
325
326 if (lttng_error_query_results_add_result(
327 results, error_counter)) {
328 action_status = LTTNG_ACTION_STATUS_ERROR;
329 goto end;
330 }
331
332 /* Ownership transferred to the results. */
333 error_counter = NULL;
334 action_status = LTTNG_ACTION_STATUS_OK;
335end:
336 lttng_error_query_result_destroy(error_counter);
337 return action_status;
338}
6a751b95
JR
339
340LTTNG_HIDDEN
341enum lttng_error_code lttng_action_mi_serialize(const struct lttng_trigger *trigger,
342 const struct lttng_action *action,
343 struct mi_writer *writer,
344 const struct mi_lttng_error_query_callbacks
345 *error_query_callbacks,
346 struct lttng_dynamic_array *action_path_indexes)
347{
348 int ret;
349 enum lttng_error_code ret_code;
350 struct lttng_action_path *action_path = NULL;
351 struct lttng_error_query_results *error_query_results = NULL;
352
a0377dfe
FD
353 LTTNG_ASSERT(action);
354 LTTNG_ASSERT(writer);
6a751b95
JR
355
356 /* Open action. */
357 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_action);
358 if (ret) {
359 goto mi_error;
360 }
361
362 if (action->type == LTTNG_ACTION_TYPE_LIST) {
363 /*
364 * Recursion is safe since action lists can't be nested for
365 * the moment.
366 */
367 ret_code = lttng_action_list_mi_serialize(trigger, action, writer,
368 error_query_callbacks, action_path_indexes);
369 if (ret_code != LTTNG_OK) {
370 goto end;
371 }
372
373 /* Nothing else to do. */
374 goto close_action_element;
375 }
376
a0377dfe 377 LTTNG_ASSERT(action->mi_serialize);
6a751b95
JR
378 ret_code = action->mi_serialize(action, writer);
379 if (ret_code != LTTNG_OK) {
380 goto end;
381 }
382
383 /* Error query for the action. */
384 if (error_query_callbacks && error_query_callbacks->action_cb) {
385 const uint64_t *action_path_indexes_raw_pointer = NULL;
386 const size_t action_path_indexes_size =
387 lttng_dynamic_array_get_count(
388 action_path_indexes);
389
390 if (action_path_indexes_size != 0) {
391 action_path_indexes_raw_pointer =
392 (const uint64_t *) action_path_indexes
393 ->buffer.data;
394 }
395
396 action_path = lttng_action_path_create(
397 action_path_indexes_raw_pointer,
398 action_path_indexes_size);
a0377dfe 399 LTTNG_ASSERT(action_path);
6a751b95
JR
400
401 ret_code = error_query_callbacks->action_cb(
402 trigger, action_path, &error_query_results);
403 if (ret_code != LTTNG_OK) {
404 goto end;
405 }
406
407 /* Serialize the error query results. */
408 ret_code = lttng_error_query_results_mi_serialize(
409 error_query_results, writer);
410 if (ret_code != LTTNG_OK) {
411 goto end;
412 }
413 }
414
415close_action_element:
416 /* Close action. */
417 ret = mi_lttng_writer_close_element(writer);
418 if (ret) {
419 goto mi_error;
420 }
421
422 ret_code = LTTNG_OK;
423 goto end;
424
425mi_error:
426 ret_code = LTTNG_ERR_MI_IO_FAIL;
427end:
428 lttng_action_path_destroy(action_path);
429 lttng_error_query_results_destroy(error_query_results);
430 return ret_code;
431}
This page took 0.055547 seconds and 4 git commands to generate.