2 * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/error.hpp>
9 #include <common/macros.hpp>
10 #include <common/mi-lttng.hpp>
12 #include <lttng/action/action-internal.hpp>
13 #include <lttng/action/rate-policy-internal.hpp>
14 #include <lttng/action/rate-policy.h>
15 #include <lttng/action/start-session-internal.hpp>
16 #include <lttng/action/start-session.h>
18 #define IS_START_SESSION_ACTION(action) \
19 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_START_SESSION)
22 struct lttng_action_start_session
{
23 struct lttng_action parent
;
27 struct lttng_rate_policy
*policy
;
30 struct lttng_action_start_session_comm
{
31 /* Includes the trailing \0. */
32 uint32_t session_name_len
;
37 * - session name (null terminated)
44 static const struct lttng_rate_policy
*
45 lttng_action_start_session_internal_get_rate_policy(const struct lttng_action
*action
);
47 static struct lttng_action_start_session
*
48 action_start_session_from_action(struct lttng_action
*action
)
52 return lttng::utils::container_of(action
, <tng_action_start_session::parent
);
55 static const struct lttng_action_start_session
*
56 action_start_session_from_action_const(const struct lttng_action
*action
)
60 return lttng::utils::container_of(action
, <tng_action_start_session::parent
);
63 static bool lttng_action_start_session_validate(struct lttng_action
*action
)
66 struct lttng_action_start_session
*action_start_session
;
73 action_start_session
= action_start_session_from_action(action
);
75 /* A non-empty session name is mandatory. */
76 if (!action_start_session
->session_name
||
77 strlen(action_start_session
->session_name
) == 0) {
87 static bool lttng_action_start_session_is_equal(const struct lttng_action
*_a
,
88 const struct lttng_action
*_b
)
90 bool is_equal
= false;
91 struct lttng_action_start_session
*a
, *b
;
93 a
= lttng::utils::container_of(_a
, <tng_action_start_session::parent
);
94 b
= lttng::utils::container_of(_b
, <tng_action_start_session::parent
);
96 /* Action is not valid if this is not true. */
97 LTTNG_ASSERT(a
->session_name
);
98 LTTNG_ASSERT(b
->session_name
);
99 if (strcmp(a
->session_name
, b
->session_name
) != 0) {
103 is_equal
= lttng_rate_policy_is_equal(a
->policy
, b
->policy
);
108 static int lttng_action_start_session_serialize(struct lttng_action
*action
,
109 struct lttng_payload
*payload
)
111 struct lttng_action_start_session
*action_start_session
;
112 struct lttng_action_start_session_comm comm
;
113 size_t session_name_len
;
116 LTTNG_ASSERT(action
);
117 LTTNG_ASSERT(payload
);
119 action_start_session
= action_start_session_from_action(action
);
121 LTTNG_ASSERT(action_start_session
->session_name
);
123 DBG("Serializing start session action: session-name: %s",
124 action_start_session
->session_name
);
126 session_name_len
= strlen(action_start_session
->session_name
) + 1;
127 comm
.session_name_len
= session_name_len
;
129 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
, sizeof(comm
));
135 ret
= lttng_dynamic_buffer_append(
136 &payload
->buffer
, action_start_session
->session_name
, session_name_len
);
142 ret
= lttng_rate_policy_serialize(action_start_session
->policy
, payload
);
153 static void lttng_action_start_session_destroy(struct lttng_action
*action
)
155 struct lttng_action_start_session
*action_start_session
;
161 action_start_session
= action_start_session_from_action(action
);
163 lttng_rate_policy_destroy(action_start_session
->policy
);
164 free(action_start_session
->session_name
);
165 free(action_start_session
);
171 ssize_t
lttng_action_start_session_create_from_payload(struct lttng_payload_view
*view
,
172 struct lttng_action
**p_action
)
174 ssize_t consumed_len
, ret
;
175 const struct lttng_action_start_session_comm
*comm
;
176 const char *session_name
;
177 struct lttng_action
*action
= nullptr;
178 enum lttng_action_status status
;
179 struct lttng_rate_policy
*policy
= nullptr;
181 comm
= (typeof(comm
)) view
->buffer
.data
;
182 session_name
= (const char *) &comm
->data
;
185 if (!lttng_buffer_view_contains_string(
186 &view
->buffer
, session_name
, comm
->session_name_len
)) {
190 consumed_len
= sizeof(*comm
) + comm
->session_name_len
;
194 struct lttng_payload_view policy_view
=
195 lttng_payload_view_from_view(view
, consumed_len
, -1);
196 ret
= lttng_rate_policy_create_from_payload(&policy_view
, &policy
);
204 action
= lttng_action_start_session_create();
210 status
= lttng_action_start_session_set_session_name(action
, session_name
);
211 if (status
!= LTTNG_ACTION_STATUS_OK
) {
216 LTTNG_ASSERT(policy
);
217 status
= lttng_action_start_session_set_rate_policy(action
, policy
);
218 if (status
!= LTTNG_ACTION_STATUS_OK
) {
227 lttng_rate_policy_destroy(policy
);
228 lttng_action_start_session_destroy(action
);
233 static enum lttng_error_code
234 lttng_action_start_session_mi_serialize(const struct lttng_action
*action
, struct mi_writer
*writer
)
237 enum lttng_error_code ret_code
;
238 enum lttng_action_status status
;
239 const char *session_name
= nullptr;
240 const struct lttng_rate_policy
*policy
= nullptr;
242 LTTNG_ASSERT(action
);
243 LTTNG_ASSERT(IS_START_SESSION_ACTION(action
));
245 status
= lttng_action_start_session_get_session_name(action
, &session_name
);
246 LTTNG_ASSERT(status
== LTTNG_ACTION_STATUS_OK
);
247 LTTNG_ASSERT(session_name
!= nullptr);
249 status
= lttng_action_start_session_get_rate_policy(action
, &policy
);
250 LTTNG_ASSERT(status
== LTTNG_ACTION_STATUS_OK
);
251 LTTNG_ASSERT(policy
!= nullptr);
253 /* Open action start session element. */
254 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_action_start_session
);
260 ret
= mi_lttng_writer_write_element_string(
261 writer
, mi_lttng_element_session_name
, session_name
);
267 ret_code
= lttng_rate_policy_mi_serialize(policy
, writer
);
268 if (ret_code
!= LTTNG_OK
) {
272 /* Close action start session element. */
273 ret
= mi_lttng_writer_close_element(writer
);
282 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
287 struct lttng_action
*lttng_action_start_session_create(void)
289 struct lttng_action_start_session
*action_start
= nullptr;
290 struct lttng_rate_policy
*policy
= nullptr;
291 enum lttng_action_status status
;
293 /* Create a every N = 1 rate policy. */
294 policy
= lttng_rate_policy_every_n_create(1);
299 action_start
= zmalloc
<lttng_action_start_session
>();
304 lttng_action_init(&action_start
->parent
,
305 LTTNG_ACTION_TYPE_START_SESSION
,
306 lttng_action_start_session_validate
,
307 lttng_action_start_session_serialize
,
308 lttng_action_start_session_is_equal
,
309 lttng_action_start_session_destroy
,
310 lttng_action_start_session_internal_get_rate_policy
,
311 lttng_action_generic_add_error_query_results
,
312 lttng_action_start_session_mi_serialize
);
314 status
= lttng_action_start_session_set_rate_policy(&action_start
->parent
, policy
);
315 if (status
!= LTTNG_ACTION_STATUS_OK
) {
316 lttng_action_destroy(&action_start
->parent
);
317 action_start
= nullptr;
322 lttng_rate_policy_destroy(policy
);
323 return &action_start
->parent
;
326 enum lttng_action_status
lttng_action_start_session_set_session_name(struct lttng_action
*action
,
327 const char *session_name
)
329 struct lttng_action_start_session
*action_start_session
;
330 enum lttng_action_status status
;
332 if (!action
|| !IS_START_SESSION_ACTION(action
) || !session_name
||
333 strlen(session_name
) == 0) {
334 status
= LTTNG_ACTION_STATUS_INVALID
;
338 action_start_session
= action_start_session_from_action(action
);
340 free(action_start_session
->session_name
);
342 action_start_session
->session_name
= strdup(session_name
);
343 if (!action_start_session
->session_name
) {
344 status
= LTTNG_ACTION_STATUS_ERROR
;
348 status
= LTTNG_ACTION_STATUS_OK
;
353 enum lttng_action_status
354 lttng_action_start_session_get_session_name(const struct lttng_action
*action
,
355 const char **session_name
)
357 const struct lttng_action_start_session
*action_start_session
;
358 enum lttng_action_status status
;
360 if (!action
|| !IS_START_SESSION_ACTION(action
) || !session_name
) {
361 status
= LTTNG_ACTION_STATUS_INVALID
;
365 action_start_session
= action_start_session_from_action_const(action
);
367 *session_name
= action_start_session
->session_name
;
369 status
= LTTNG_ACTION_STATUS_OK
;
374 enum lttng_action_status
375 lttng_action_start_session_set_rate_policy(struct lttng_action
*action
,
376 const struct lttng_rate_policy
*policy
)
378 enum lttng_action_status status
;
379 struct lttng_action_start_session
*start_session_action
;
380 struct lttng_rate_policy
*copy
= nullptr;
382 if (!action
|| !policy
|| !IS_START_SESSION_ACTION(action
)) {
383 status
= LTTNG_ACTION_STATUS_INVALID
;
387 copy
= lttng_rate_policy_copy(policy
);
389 status
= LTTNG_ACTION_STATUS_ERROR
;
393 start_session_action
= action_start_session_from_action(action
);
395 /* Release the previous rate policy .*/
396 lttng_rate_policy_destroy(start_session_action
->policy
);
398 /* Assign the policy. */
399 start_session_action
->policy
= copy
;
400 status
= LTTNG_ACTION_STATUS_OK
;
404 lttng_rate_policy_destroy(copy
);
408 enum lttng_action_status
409 lttng_action_start_session_get_rate_policy(const struct lttng_action
*action
,
410 const struct lttng_rate_policy
**policy
)
412 enum lttng_action_status status
;
413 const struct lttng_action_start_session
*start_session_action
;
415 if (!action
|| !policy
|| !IS_START_SESSION_ACTION(action
)) {
416 status
= LTTNG_ACTION_STATUS_INVALID
;
420 start_session_action
= action_start_session_from_action_const(action
);
422 *policy
= start_session_action
->policy
;
423 status
= LTTNG_ACTION_STATUS_OK
;
428 static const struct lttng_rate_policy
*
429 lttng_action_start_session_internal_get_rate_policy(const struct lttng_action
*action
)
431 const struct lttng_action_start_session
*_action
;
432 _action
= action_start_session_from_action_const(action
);
434 return _action
->policy
;