2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/buffer-view.h>
9 #include <common/dynamic-buffer.h>
10 #include <common/error.h>
11 #include <common/macros.h>
12 #include <common/mi-lttng.h>
13 #include <lttng/condition/buffer-usage-internal.h>
14 #include <lttng/condition/condition-internal.h>
15 #include <lttng/condition/event-rule-matches-internal.h>
16 #include <lttng/condition/session-consumed-size-internal.h>
17 #include <lttng/condition/session-rotation-internal.h>
18 #include <lttng/error-query-internal.h>
21 enum lttng_condition_type
lttng_condition_get_type(
22 const struct lttng_condition
*condition
)
24 return condition
? condition
->type
: LTTNG_CONDITION_TYPE_UNKNOWN
;
27 void lttng_condition_destroy(struct lttng_condition
*condition
)
29 lttng_condition_put(condition
);
32 static void condition_destroy_ref(struct urcu_ref
*ref
)
34 struct lttng_condition
*condition
=
35 container_of(ref
, struct lttng_condition
, ref
);
37 condition
->destroy(condition
);
40 void lttng_condition_get(struct lttng_condition
*condition
)
42 urcu_ref_get(&condition
->ref
);
45 void lttng_condition_put(struct lttng_condition
*condition
)
51 LTTNG_ASSERT(condition
->destroy
);
52 urcu_ref_put(&condition
->ref
, condition_destroy_ref
);
56 bool lttng_condition_validate(const struct lttng_condition
*condition
)
65 if (!condition
->validate
) {
66 /* Sub-class guarantees that it can never be invalid. */
71 valid
= condition
->validate(condition
);
76 int lttng_condition_serialize(const struct lttng_condition
*condition
,
77 struct lttng_payload
*payload
)
80 struct lttng_condition_comm condition_comm
= {};
87 condition_comm
.condition_type
= (int8_t) condition
->type
;
89 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &condition_comm
,
90 sizeof(condition_comm
));
95 ret
= condition
->serialize(condition
, payload
);
103 bool lttng_condition_is_equal(const struct lttng_condition
*a
,
104 const struct lttng_condition
*b
)
106 bool is_equal
= false;
112 if (a
->type
!= b
->type
) {
121 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
126 ssize_t
lttng_condition_create_from_payload(
127 struct lttng_payload_view
*view
,
128 struct lttng_condition
**condition
)
130 ssize_t ret
, condition_size
= 0;
131 condition_create_from_payload_cb create_from_payload
= NULL
;
132 const struct lttng_condition_comm
*condition_comm
;
133 const struct lttng_payload_view condition_comm_view
=
134 lttng_payload_view_from_view(
135 view
, 0, sizeof(*condition_comm
));
137 if (!view
|| !condition
) {
142 if (!lttng_payload_view_is_valid(&condition_comm_view
)) {
143 /* Payload not large enough to contain the header. */
148 DBG("Deserializing condition from buffer");
149 condition_comm
= (typeof(condition_comm
)) condition_comm_view
.buffer
.data
;
150 condition_size
+= sizeof(*condition_comm
);
152 switch ((enum lttng_condition_type
) condition_comm
->condition_type
) {
153 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
154 create_from_payload
= lttng_condition_buffer_usage_low_create_from_payload
;
156 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
157 create_from_payload
= lttng_condition_buffer_usage_high_create_from_payload
;
159 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
160 create_from_payload
= lttng_condition_session_consumed_size_create_from_payload
;
162 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
163 create_from_payload
= lttng_condition_session_rotation_ongoing_create_from_payload
;
165 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
166 create_from_payload
= lttng_condition_session_rotation_completed_create_from_payload
;
168 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
169 create_from_payload
=
170 lttng_condition_event_rule_matches_create_from_payload
;
173 ERR("Attempted to create condition of unknown type (%i)",
174 (int) condition_comm
->condition_type
);
179 if (create_from_payload
) {
180 struct lttng_payload_view condition_view
=
181 lttng_payload_view_from_view(view
,
182 sizeof(*condition_comm
), -1);
184 ret
= create_from_payload(&condition_view
, condition
);
188 condition_size
+= ret
;
194 ret
= condition_size
;
199 void lttng_condition_init(struct lttng_condition
*condition
,
200 enum lttng_condition_type type
)
202 condition
->type
= type
;
203 urcu_ref_init(&condition
->ref
);
206 const char *lttng_condition_type_str(enum lttng_condition_type type
)
209 case LTTNG_CONDITION_TYPE_UNKNOWN
:
212 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
213 return "session consumed size";
215 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
216 return "buffer usage high";
218 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
219 return "buffer usage low";
221 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
222 return "session rotation ongoing";
224 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
225 return "session rotation completed";
227 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
228 return "event rule matches";
235 enum lttng_error_code
lttng_condition_mi_serialize(
236 const struct lttng_trigger
*trigger
,
237 const struct lttng_condition
*condition
,
238 struct mi_writer
*writer
,
239 const struct mi_lttng_error_query_callbacks
*error_query_callbacks
)
242 enum lttng_error_code ret_code
;
243 struct lttng_error_query_results
*error_query_results
= NULL
;
245 LTTNG_ASSERT(condition
);
246 LTTNG_ASSERT(writer
);
247 LTTNG_ASSERT(condition
->mi_serialize
);
249 /* Open condition element. */
250 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_condition
);
255 /* Serialize underlying condition. */
256 ret_code
= condition
->mi_serialize(condition
, writer
);
257 if (ret_code
!= LTTNG_OK
) {
261 /* Serialize error query results for the action. */
262 if (error_query_callbacks
&& error_query_callbacks
->action_cb
) {
263 ret_code
= error_query_callbacks
->condition_cb(
264 trigger
, &error_query_results
);
265 if (ret_code
!= LTTNG_OK
) {
269 ret_code
= lttng_error_query_results_mi_serialize(
270 error_query_results
, writer
);
271 if (ret_code
!= LTTNG_OK
) {
276 /* Close condition element. */
277 ret
= mi_lttng_writer_close_element(writer
);
286 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
288 lttng_error_query_results_destroy(error_query_results
);