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>
10 #include <lttng/action/action-internal.hpp>
11 #include <lttng/action/list-internal.hpp>
12 #include <lttng/action/notify-internal.hpp>
13 #include <lttng/action/rate-policy-internal.hpp>
14 #include <lttng/action/rotate-session-internal.hpp>
15 #include <lttng/action/snapshot-session-internal.hpp>
16 #include <lttng/action/start-session-internal.hpp>
17 #include <lttng/action/stop-session-internal.hpp>
18 #include <lttng/error-query-internal.hpp>
20 const char *lttng_action_type_string(enum lttng_action_type action_type
)
22 switch (action_type
) {
23 case LTTNG_ACTION_TYPE_UNKNOWN
:
25 case LTTNG_ACTION_TYPE_LIST
:
27 case LTTNG_ACTION_TYPE_NOTIFY
:
29 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
30 return "ROTATE_SESSION";
31 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
32 return "SNAPSHOT_SESSION";
33 case LTTNG_ACTION_TYPE_START_SESSION
:
34 return "START_SESSION";
35 case LTTNG_ACTION_TYPE_STOP_SESSION
:
36 return "STOP_SESSION";
42 enum lttng_action_type
lttng_action_get_type(const struct lttng_action
*action
)
44 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
47 void lttng_action_init(struct lttng_action
*action
,
48 enum lttng_action_type type
,
49 action_validate_cb validate
,
50 action_serialize_cb serialize
,
51 action_equal_cb equal
,
52 action_destroy_cb destroy
,
53 action_get_rate_policy_cb get_rate_policy
,
54 action_add_error_query_results_cb add_error_query_results
,
55 action_mi_serialize_cb mi
)
57 urcu_ref_init(&action
->ref
);
59 action
->validate
= validate
;
60 action
->serialize
= serialize
;
61 action
->equal
= equal
;
62 action
->destroy
= destroy
;
63 action
->get_rate_policy
= get_rate_policy
;
64 action
->add_error_query_results
= add_error_query_results
;
65 action
->mi_serialize
= mi
;
67 action
->execution_request_counter
= 0;
68 action
->execution_counter
= 0;
69 action
->execution_failure_counter
= 0;
73 void action_destroy_ref(struct urcu_ref
*ref
)
75 struct lttng_action
*action
=
76 lttng::utils::container_of(ref
, <tng_action::ref
);
78 action
->destroy(action
);
81 void lttng_action_get(struct lttng_action
*action
)
83 urcu_ref_get(&action
->ref
);
86 void lttng_action_put(struct lttng_action
*action
)
92 LTTNG_ASSERT(action
->destroy
);
93 urcu_ref_put(&action
->ref
, action_destroy_ref
);
96 void lttng_action_destroy(struct lttng_action
*action
)
98 lttng_action_put(action
);
101 bool lttng_action_validate(struct lttng_action
*action
)
110 if (!action
->validate
) {
111 /* Sub-class guarantees that it can never be invalid. */
116 valid
= action
->validate(action
);
121 int lttng_action_serialize(struct lttng_action
*action
,
122 struct lttng_payload
*payload
)
125 struct lttng_action_comm action_comm
= {
126 .action_type
= (int8_t) action
->type
,
129 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &action_comm
,
130 sizeof(action_comm
));
135 ret
= action
->serialize(action
, payload
);
143 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
144 struct lttng_action
**action
)
146 ssize_t consumed_len
, specific_action_consumed_len
;
147 action_create_from_payload_cb create_from_payload_cb
;
148 const struct lttng_action_comm
*action_comm
;
149 const struct lttng_payload_view action_comm_view
=
150 lttng_payload_view_from_view(
151 view
, 0, sizeof(*action_comm
));
153 if (!view
|| !action
) {
158 if (!lttng_payload_view_is_valid(&action_comm_view
)) {
159 /* Payload not large enough to contain the header. */
164 action_comm
= (const struct lttng_action_comm
*) action_comm_view
.buffer
.data
;
166 DBG("Create action from payload: action-type=%s",
167 lttng_action_type_string((lttng_action_type
) action_comm
->action_type
));
169 switch (action_comm
->action_type
) {
170 case LTTNG_ACTION_TYPE_NOTIFY
:
171 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
173 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
174 create_from_payload_cb
=
175 lttng_action_rotate_session_create_from_payload
;
177 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
178 create_from_payload_cb
=
179 lttng_action_snapshot_session_create_from_payload
;
181 case LTTNG_ACTION_TYPE_START_SESSION
:
182 create_from_payload_cb
=
183 lttng_action_start_session_create_from_payload
;
185 case LTTNG_ACTION_TYPE_STOP_SESSION
:
186 create_from_payload_cb
=
187 lttng_action_stop_session_create_from_payload
;
189 case LTTNG_ACTION_TYPE_LIST
:
190 create_from_payload_cb
= lttng_action_list_create_from_payload
;
193 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
194 action_comm
->action_type
,
195 lttng_action_type_string(
196 (lttng_action_type
) action_comm
->action_type
));
202 /* Create buffer view for the action-type-specific data. */
203 struct lttng_payload_view specific_action_view
=
204 lttng_payload_view_from_view(view
,
205 sizeof(struct lttng_action_comm
),
208 specific_action_consumed_len
= create_from_payload_cb(
209 &specific_action_view
, action
);
211 if (specific_action_consumed_len
< 0) {
212 ERR("Failed to create specific action from buffer.");
217 LTTNG_ASSERT(*action
);
219 consumed_len
= sizeof(struct lttng_action_comm
) +
220 specific_action_consumed_len
;
226 bool lttng_action_is_equal(const struct lttng_action
*a
,
227 const struct lttng_action
*b
)
229 bool is_equal
= false;
235 if (a
->type
!= b
->type
) {
244 LTTNG_ASSERT(a
->equal
);
245 is_equal
= a
->equal(a
, b
);
250 void lttng_action_increase_execution_request_count(struct lttng_action
*action
)
252 action
->execution_request_counter
++;
255 void lttng_action_increase_execution_count(struct lttng_action
*action
)
257 action
->execution_counter
++;
260 void lttng_action_increase_execution_failure_count(struct lttng_action
*action
)
262 uatomic_inc(&action
->execution_failure_counter
);
265 bool lttng_action_should_execute(const struct lttng_action
*action
)
267 const struct lttng_rate_policy
*policy
= NULL
;
268 bool execute
= false;
270 if (action
->get_rate_policy
== NULL
) {
275 policy
= action
->get_rate_policy(action
);
276 if (policy
== NULL
) {
281 execute
= lttng_rate_policy_should_execute(
282 policy
, action
->execution_request_counter
);
287 enum lttng_action_status
lttng_action_add_error_query_results(
288 const struct lttng_action
*action
,
289 struct lttng_error_query_results
*results
)
291 return action
->add_error_query_results(action
, results
);
294 enum lttng_action_status
lttng_action_generic_add_error_query_results(
295 const struct lttng_action
*action
,
296 struct lttng_error_query_results
*results
)
298 enum lttng_action_status action_status
;
299 struct lttng_error_query_result
*error_counter
= NULL
;
300 const uint64_t execution_failure_counter
=
301 uatomic_read(&action
->execution_failure_counter
);
303 error_counter
= lttng_error_query_result_counter_create(
304 "total execution failures",
305 "Aggregated count of errors encountered when executing the action",
306 execution_failure_counter
);
307 if (!error_counter
) {
308 action_status
= LTTNG_ACTION_STATUS_ERROR
;
312 if (lttng_error_query_results_add_result(
313 results
, error_counter
)) {
314 action_status
= LTTNG_ACTION_STATUS_ERROR
;
318 /* Ownership transferred to the results. */
319 error_counter
= NULL
;
320 action_status
= LTTNG_ACTION_STATUS_OK
;
322 lttng_error_query_result_destroy(error_counter
);
323 return action_status
;
326 enum lttng_error_code
lttng_action_mi_serialize(const struct lttng_trigger
*trigger
,
327 const struct lttng_action
*action
,
328 struct mi_writer
*writer
,
329 const struct mi_lttng_error_query_callbacks
330 *error_query_callbacks
,
331 struct lttng_dynamic_array
*action_path_indexes
)
334 enum lttng_error_code ret_code
;
335 struct lttng_action_path
*action_path
= NULL
;
336 struct lttng_error_query_results
*error_query_results
= NULL
;
338 LTTNG_ASSERT(action
);
339 LTTNG_ASSERT(writer
);
342 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_action
);
347 if (action
->type
== LTTNG_ACTION_TYPE_LIST
) {
349 * Recursion is safe since action lists can't be nested for
352 ret_code
= lttng_action_list_mi_serialize(trigger
, action
, writer
,
353 error_query_callbacks
, action_path_indexes
);
354 if (ret_code
!= LTTNG_OK
) {
358 /* Nothing else to do. */
359 goto close_action_element
;
362 LTTNG_ASSERT(action
->mi_serialize
);
363 ret_code
= action
->mi_serialize(action
, writer
);
364 if (ret_code
!= LTTNG_OK
) {
368 /* Error query for the action. */
369 if (error_query_callbacks
&& error_query_callbacks
->action_cb
) {
370 const uint64_t *action_path_indexes_raw_pointer
= NULL
;
371 const size_t action_path_indexes_size
=
372 lttng_dynamic_array_get_count(
373 action_path_indexes
);
375 if (action_path_indexes_size
!= 0) {
376 action_path_indexes_raw_pointer
=
377 (const uint64_t *) action_path_indexes
381 action_path
= lttng_action_path_create(
382 action_path_indexes_raw_pointer
,
383 action_path_indexes_size
);
384 LTTNG_ASSERT(action_path
);
386 ret_code
= error_query_callbacks
->action_cb(
387 trigger
, action_path
, &error_query_results
);
388 if (ret_code
!= LTTNG_OK
) {
392 /* Serialize the error query results. */
393 ret_code
= lttng_error_query_results_mi_serialize(
394 error_query_results
, writer
);
395 if (ret_code
!= LTTNG_OK
) {
400 close_action_element
:
402 ret
= mi_lttng_writer_close_element(writer
);
411 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
413 lttng_action_path_destroy(action_path
);
414 lttng_error_query_results_destroy(error_query_results
);