2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/error.h>
10 #include <lttng/action/action-internal.h>
11 #include <lttng/action/group-internal.h>
12 #include <lttng/action/incr-value-internal.h>
13 #include <lttng/action/notify-internal.h>
14 #include <lttng/action/rotate-session-internal.h>
15 #include <lttng/action/snapshot-session-internal.h>
16 #include <lttng/action/start-session-internal.h>
17 #include <lttng/action/stop-session-internal.h>
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_GROUP
:
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";
37 case LTTNG_ACTION_TYPE_INCREMENT_VALUE
:
38 return "INCREMENT_VALUE";
44 enum lttng_action_type
lttng_action_get_type(const struct lttng_action
*action
)
46 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
50 void lttng_action_init(
51 struct lttng_action
*action
,
52 enum lttng_action_type type
,
53 action_validate_cb validate
,
54 action_serialize_cb serialize
,
55 action_equal_cb equal
,
56 action_destroy_cb destroy
)
58 urcu_ref_init(&action
->ref
);
60 action
->validate
= validate
;
61 action
->serialize
= serialize
;
62 action
->equal
= equal
;
63 action
->destroy
= destroy
;
67 void action_destroy_ref(struct urcu_ref
*ref
)
69 struct lttng_action
*action
=
70 container_of(ref
, struct lttng_action
, ref
);
72 action
->destroy(action
);
76 void lttng_action_get(struct lttng_action
*action
)
78 urcu_ref_get(&action
->ref
);
82 void lttng_action_put(struct lttng_action
*action
)
88 assert(action
->destroy
);
89 urcu_ref_put(&action
->ref
, action_destroy_ref
);
92 void lttng_action_destroy(struct lttng_action
*action
)
94 lttng_action_put(action
);
98 bool lttng_action_validate(struct lttng_action
*action
)
107 if (!action
->validate
) {
108 /* Sub-class guarantees that it can never be invalid. */
113 valid
= action
->validate(action
);
119 int lttng_action_serialize(struct lttng_action
*action
,
120 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
,
128 sizeof(action_comm
));
133 ret
= action
->serialize(action
, payload
);
142 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
143 struct lttng_action
**action
)
145 ssize_t consumed_len
, specific_action_consumed_len
;
146 action_create_from_payload_cb create_from_payload_cb
;
147 const struct lttng_action_comm
*action_comm
;
148 const struct lttng_payload_view action_comm_view
=
149 lttng_payload_view_from_view(
150 view
, 0, sizeof(*action_comm
));
152 if (!view
|| !action
) {
157 if (!lttng_payload_view_is_valid(&action_comm_view
)) {
158 /* Payload not large enough to contain the header. */
163 action_comm
= (const struct lttng_action_comm
*) action_comm_view
.buffer
.data
;
165 DBG("Create action from payload: action-type=%s",
166 lttng_action_type_string(action_comm
->action_type
));
168 switch (action_comm
->action_type
) {
169 case LTTNG_ACTION_TYPE_NOTIFY
:
170 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
172 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
173 create_from_payload_cb
=
174 lttng_action_rotate_session_create_from_payload
;
176 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
177 create_from_payload_cb
=
178 lttng_action_snapshot_session_create_from_payload
;
180 case LTTNG_ACTION_TYPE_START_SESSION
:
181 create_from_payload_cb
=
182 lttng_action_start_session_create_from_payload
;
184 case LTTNG_ACTION_TYPE_STOP_SESSION
:
185 create_from_payload_cb
=
186 lttng_action_stop_session_create_from_payload
;
188 case LTTNG_ACTION_TYPE_INCREMENT_VALUE
:
189 create_from_payload_cb
=
190 lttng_action_incr_value_create_from_payload
;
192 case LTTNG_ACTION_TYPE_GROUP
:
193 create_from_payload_cb
= lttng_action_group_create_from_payload
;
196 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
197 action_comm
->action_type
,
198 lttng_action_type_string(
199 action_comm
->action_type
));
205 /* Create buffer view for the action-type-specific data. */
206 struct lttng_payload_view specific_action_view
=
207 lttng_payload_view_from_view(view
,
208 sizeof(struct lttng_action_comm
),
211 specific_action_consumed_len
= create_from_payload_cb(
212 &specific_action_view
, action
);
214 if (specific_action_consumed_len
< 0) {
215 ERR("Failed to create specific action from buffer.");
222 consumed_len
= sizeof(struct lttng_action_comm
) +
223 specific_action_consumed_len
;
230 bool lttng_action_is_equal(const struct lttng_action
*a
,
231 const struct lttng_action
*b
)
233 bool is_equal
= false;
239 if (a
->type
!= b
->type
) {
249 is_equal
= a
->equal(a
, b
);