2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/error.hpp>
9 #include <common/mi-lttng.hpp>
11 #include <lttng/action/action-internal.hpp>
12 #include <lttng/action/list-internal.hpp>
13 #include <lttng/action/notify-internal.hpp>
14 #include <lttng/action/rate-policy-internal.hpp>
15 #include <lttng/action/rotate-session-internal.hpp>
16 #include <lttng/action/snapshot-session-internal.hpp>
17 #include <lttng/action/start-session-internal.hpp>
18 #include <lttng/action/stop-session-internal.hpp>
19 #include <lttng/error-query-internal.hpp>
21 const char *lttng_action_type_string(enum lttng_action_type action_type
)
23 switch (action_type
) {
24 case LTTNG_ACTION_TYPE_UNKNOWN
:
26 case LTTNG_ACTION_TYPE_LIST
:
28 case LTTNG_ACTION_TYPE_NOTIFY
:
30 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
31 return "ROTATE_SESSION";
32 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
33 return "SNAPSHOT_SESSION";
34 case LTTNG_ACTION_TYPE_START_SESSION
:
35 return "START_SESSION";
36 case LTTNG_ACTION_TYPE_STOP_SESSION
:
37 return "STOP_SESSION";
43 enum lttng_action_type
lttng_action_get_type(const struct lttng_action
*action
)
45 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
48 void lttng_action_init(struct lttng_action
*action
,
49 enum lttng_action_type type
,
50 action_validate_cb validate
,
51 action_serialize_cb serialize
,
52 action_equal_cb equal
,
53 action_destroy_cb destroy
,
54 action_get_rate_policy_cb get_rate_policy
,
55 action_add_error_query_results_cb add_error_query_results
,
56 action_mi_serialize_cb mi
)
58 urcu_ref_init(&action
->ref
);
60 action
->validate
= validate
;
61 action
->serialize
= serialize
;
62 action
->equal
= equal
;
63 action
->destroy
= destroy
;
64 action
->get_rate_policy
= get_rate_policy
;
65 action
->add_error_query_results
= add_error_query_results
;
66 action
->mi_serialize
= mi
;
68 action
->execution_request_counter
= 0;
69 action
->execution_counter
= 0;
70 action
->execution_failure_counter
= 0;
73 static void action_destroy_ref(struct urcu_ref
*ref
)
75 struct lttng_action
*action
= lttng::utils::container_of(ref
, <tng_action::ref
);
77 action
->destroy(action
);
80 void lttng_action_get(struct lttng_action
*action
)
82 urcu_ref_get(&action
->ref
);
85 void lttng_action_put(struct lttng_action
*action
)
91 LTTNG_ASSERT(action
->destroy
);
92 urcu_ref_put(&action
->ref
, action_destroy_ref
);
95 void lttng_action_destroy(struct lttng_action
*action
)
97 lttng_action_put(action
);
100 bool lttng_action_validate(struct lttng_action
*action
)
109 if (!action
->validate
) {
110 /* Sub-class guarantees that it can never be invalid. */
115 valid
= action
->validate(action
);
120 int lttng_action_serialize(struct lttng_action
*action
, struct lttng_payload
*payload
)
123 struct lttng_action_comm action_comm
= {
124 .action_type
= (int8_t) action
->type
,
127 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &action_comm
, sizeof(action_comm
));
132 ret
= action
->serialize(action
, payload
);
140 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
141 struct lttng_action
**action
)
143 ssize_t consumed_len
, specific_action_consumed_len
;
144 action_create_from_payload_cb create_from_payload_cb
;
145 const struct lttng_action_comm
*action_comm
;
146 const struct lttng_payload_view action_comm_view
=
147 lttng_payload_view_from_view(view
, 0, sizeof(*action_comm
));
149 if (!view
|| !action
) {
154 if (!lttng_payload_view_is_valid(&action_comm_view
)) {
155 /* Payload not large enough to contain the header. */
160 action_comm
= (const struct lttng_action_comm
*) action_comm_view
.buffer
.data
;
162 DBG("Create action from payload: action-type=%s",
163 lttng_action_type_string((lttng_action_type
) action_comm
->action_type
));
165 switch (action_comm
->action_type
) {
166 case LTTNG_ACTION_TYPE_NOTIFY
:
167 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
169 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
170 create_from_payload_cb
= lttng_action_rotate_session_create_from_payload
;
172 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
173 create_from_payload_cb
= lttng_action_snapshot_session_create_from_payload
;
175 case LTTNG_ACTION_TYPE_START_SESSION
:
176 create_from_payload_cb
= lttng_action_start_session_create_from_payload
;
178 case LTTNG_ACTION_TYPE_STOP_SESSION
:
179 create_from_payload_cb
= lttng_action_stop_session_create_from_payload
;
181 case LTTNG_ACTION_TYPE_LIST
:
182 create_from_payload_cb
= lttng_action_list_create_from_payload
;
185 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
186 action_comm
->action_type
,
187 lttng_action_type_string((lttng_action_type
) action_comm
->action_type
));
193 /* Create buffer view for the action-type-specific data. */
194 struct lttng_payload_view specific_action_view
=
195 lttng_payload_view_from_view(view
, sizeof(struct lttng_action_comm
), -1);
197 specific_action_consumed_len
=
198 create_from_payload_cb(&specific_action_view
, action
);
200 if (specific_action_consumed_len
< 0) {
201 ERR("Failed to create specific action from buffer.");
206 LTTNG_ASSERT(*action
);
208 consumed_len
= sizeof(struct lttng_action_comm
) + specific_action_consumed_len
;
214 bool lttng_action_is_equal(const struct lttng_action
*a
, const struct lttng_action
*b
)
216 bool is_equal
= false;
222 if (a
->type
!= b
->type
) {
231 LTTNG_ASSERT(a
->equal
);
232 is_equal
= a
->equal(a
, b
);
237 void lttng_action_increase_execution_request_count(struct lttng_action
*action
)
239 action
->execution_request_counter
++;
242 void lttng_action_increase_execution_count(struct lttng_action
*action
)
244 action
->execution_counter
++;
247 void lttng_action_increase_execution_failure_count(struct lttng_action
*action
)
249 uatomic_inc(&action
->execution_failure_counter
);
252 bool lttng_action_should_execute(const struct lttng_action
*action
)
254 const struct lttng_rate_policy
*policy
= nullptr;
255 bool execute
= false;
257 if (action
->get_rate_policy
== nullptr) {
262 policy
= action
->get_rate_policy(action
);
263 if (policy
== nullptr) {
268 execute
= lttng_rate_policy_should_execute(policy
, action
->execution_request_counter
);
273 enum lttng_action_status
274 lttng_action_add_error_query_results(const struct lttng_action
*action
,
275 struct lttng_error_query_results
*results
)
277 return action
->add_error_query_results(action
, results
);
280 enum lttng_action_status
281 lttng_action_generic_add_error_query_results(const struct lttng_action
*action
,
282 struct lttng_error_query_results
*results
)
284 enum lttng_action_status action_status
;
285 struct lttng_error_query_result
*error_counter
= nullptr;
286 const uint64_t execution_failure_counter
= uatomic_read(&action
->execution_failure_counter
);
288 error_counter
= lttng_error_query_result_counter_create(
289 "total execution failures",
290 "Aggregated count of errors encountered when executing the action",
291 execution_failure_counter
);
292 if (!error_counter
) {
293 action_status
= LTTNG_ACTION_STATUS_ERROR
;
297 if (lttng_error_query_results_add_result(results
, error_counter
)) {
298 action_status
= LTTNG_ACTION_STATUS_ERROR
;
302 /* Ownership transferred to the results. */
303 error_counter
= nullptr;
304 action_status
= LTTNG_ACTION_STATUS_OK
;
306 lttng_error_query_result_destroy(error_counter
);
307 return action_status
;
310 enum lttng_error_code
311 lttng_action_mi_serialize(const struct lttng_trigger
*trigger
,
312 const struct lttng_action
*action
,
313 struct mi_writer
*writer
,
314 const struct mi_lttng_error_query_callbacks
*error_query_callbacks
,
315 struct lttng_dynamic_array
*action_path_indexes
)
318 enum lttng_error_code ret_code
;
319 struct lttng_action_path
*action_path
= nullptr;
320 struct lttng_error_query_results
*error_query_results
= nullptr;
322 LTTNG_ASSERT(action
);
323 LTTNG_ASSERT(writer
);
326 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_action
);
331 if (action
->type
== LTTNG_ACTION_TYPE_LIST
) {
333 * Recursion is safe since action lists can't be nested for
336 ret_code
= lttng_action_list_mi_serialize(
337 trigger
, action
, writer
, error_query_callbacks
, action_path_indexes
);
338 if (ret_code
!= LTTNG_OK
) {
342 /* Nothing else to do. */
343 goto close_action_element
;
346 LTTNG_ASSERT(action
->mi_serialize
);
347 ret_code
= action
->mi_serialize(action
, writer
);
348 if (ret_code
!= LTTNG_OK
) {
352 /* Error query for the action. */
353 if (error_query_callbacks
&& error_query_callbacks
->action_cb
) {
354 const uint64_t *action_path_indexes_raw_pointer
= nullptr;
355 const size_t action_path_indexes_size
=
356 lttng_dynamic_array_get_count(action_path_indexes
);
358 if (action_path_indexes_size
!= 0) {
359 action_path_indexes_raw_pointer
=
360 (const uint64_t *) action_path_indexes
->buffer
.data
;
363 action_path
= lttng_action_path_create(action_path_indexes_raw_pointer
,
364 action_path_indexes_size
);
365 LTTNG_ASSERT(action_path
);
367 ret_code
= error_query_callbacks
->action_cb(
368 trigger
, action_path
, &error_query_results
);
369 if (ret_code
!= LTTNG_OK
) {
373 /* Serialize the error query results. */
374 ret_code
= lttng_error_query_results_mi_serialize(error_query_results
, writer
);
375 if (ret_code
!= LTTNG_OK
) {
380 close_action_element
:
382 ret
= mi_lttng_writer_close_element(writer
);
391 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
393 lttng_action_path_destroy(action_path
);
394 lttng_error_query_results_destroy(error_query_results
);