2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/buffer-view.h>
10 #include <common/dynamic-buffer.h>
11 #include <common/error.h>
12 #include <common/macros.h>
13 #include <common/mi-lttng.h>
14 #include <lttng/condition/buffer-usage-internal.h>
15 #include <lttng/condition/condition-internal.h>
16 #include <lttng/condition/event-rule-matches-internal.h>
17 #include <lttng/condition/session-consumed-size-internal.h>
18 #include <lttng/condition/session-rotation-internal.h>
19 #include <lttng/error-query-internal.h>
22 enum lttng_condition_type
lttng_condition_get_type(
23 const struct lttng_condition
*condition
)
25 return condition
? condition
->type
: LTTNG_CONDITION_TYPE_UNKNOWN
;
28 void lttng_condition_destroy(struct lttng_condition
*condition
)
30 lttng_condition_put(condition
);
33 static void condition_destroy_ref(struct urcu_ref
*ref
)
35 struct lttng_condition
*condition
=
36 container_of(ref
, struct lttng_condition
, ref
);
38 condition
->destroy(condition
);
42 void lttng_condition_get(struct lttng_condition
*condition
)
44 urcu_ref_get(&condition
->ref
);
48 void lttng_condition_put(struct lttng_condition
*condition
)
54 assert(condition
->destroy
);
55 urcu_ref_put(&condition
->ref
, condition_destroy_ref
);
60 bool lttng_condition_validate(const struct lttng_condition
*condition
)
69 if (!condition
->validate
) {
70 /* Sub-class guarantees that it can never be invalid. */
75 valid
= condition
->validate(condition
);
81 int lttng_condition_serialize(const struct lttng_condition
*condition
,
82 struct lttng_payload
*payload
)
85 struct lttng_condition_comm condition_comm
= {};
92 condition_comm
.condition_type
= (int8_t) condition
->type
;
94 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &condition_comm
,
95 sizeof(condition_comm
));
100 ret
= condition
->serialize(condition
, payload
);
109 bool lttng_condition_is_equal(const struct lttng_condition
*a
,
110 const struct lttng_condition
*b
)
112 bool is_equal
= false;
118 if (a
->type
!= b
->type
) {
127 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
133 ssize_t
lttng_condition_create_from_payload(
134 struct lttng_payload_view
*view
,
135 struct lttng_condition
**condition
)
137 ssize_t ret
, condition_size
= 0;
138 condition_create_from_payload_cb create_from_payload
= NULL
;
139 const struct lttng_condition_comm
*condition_comm
;
140 const struct lttng_payload_view condition_comm_view
=
141 lttng_payload_view_from_view(
142 view
, 0, sizeof(*condition_comm
));
144 if (!view
|| !condition
) {
149 if (!lttng_payload_view_is_valid(&condition_comm_view
)) {
150 /* Payload not large enough to contain the header. */
155 DBG("Deserializing condition from buffer");
156 condition_comm
= (typeof(condition_comm
)) condition_comm_view
.buffer
.data
;
157 condition_size
+= sizeof(*condition_comm
);
159 switch ((enum lttng_condition_type
) condition_comm
->condition_type
) {
160 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
161 create_from_payload
= lttng_condition_buffer_usage_low_create_from_payload
;
163 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
164 create_from_payload
= lttng_condition_buffer_usage_high_create_from_payload
;
166 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
167 create_from_payload
= lttng_condition_session_consumed_size_create_from_payload
;
169 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
170 create_from_payload
= lttng_condition_session_rotation_ongoing_create_from_payload
;
172 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
173 create_from_payload
= lttng_condition_session_rotation_completed_create_from_payload
;
175 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
176 create_from_payload
=
177 lttng_condition_event_rule_matches_create_from_payload
;
180 ERR("Attempted to create condition of unknown type (%i)",
181 (int) condition_comm
->condition_type
);
186 if (create_from_payload
) {
187 struct lttng_payload_view condition_view
=
188 lttng_payload_view_from_view(view
,
189 sizeof(*condition_comm
), -1);
191 ret
= create_from_payload(&condition_view
, condition
);
195 condition_size
+= ret
;
201 ret
= condition_size
;
207 void lttng_condition_init(struct lttng_condition
*condition
,
208 enum lttng_condition_type type
)
210 condition
->type
= type
;
211 urcu_ref_init(&condition
->ref
);
215 const char *lttng_condition_type_str(enum lttng_condition_type type
)
218 case LTTNG_CONDITION_TYPE_UNKNOWN
:
221 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
222 return "session consumed size";
224 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
225 return "buffer usage high";
227 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
228 return "buffer usage low";
230 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
231 return "session rotation ongoing";
233 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
234 return "session rotation completed";
236 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
237 return "event rule matches";
245 enum lttng_error_code
lttng_condition_mi_serialize(
246 const struct lttng_trigger
*trigger
,
247 const struct lttng_condition
*condition
,
248 struct mi_writer
*writer
,
249 const struct mi_lttng_error_query_callbacks
*error_query_callbacks
)
252 enum lttng_error_code ret_code
;
253 struct lttng_error_query_results
*error_query_results
= NULL
;
257 assert(condition
->mi_serialize
);
259 /* Open condition element. */
260 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_condition
);
265 /* Serialize underlying condition. */
266 ret_code
= condition
->mi_serialize(condition
, writer
);
267 if (ret_code
!= LTTNG_OK
) {
271 /* Serialize error query results for the action. */
272 if (error_query_callbacks
&& error_query_callbacks
->action_cb
) {
273 ret_code
= error_query_callbacks
->condition_cb(
274 trigger
, &error_query_results
);
275 if (ret_code
!= LTTNG_OK
) {
279 ret_code
= lttng_error_query_results_mi_serialize(
280 error_query_results
, writer
);
281 if (ret_code
!= LTTNG_OK
) {
286 /* Close condition element. */
287 ret
= mi_lttng_writer_close_element(writer
);
296 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
298 lttng_error_query_results_destroy(error_query_results
);