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.hpp>
9 #include <common/dynamic-buffer.hpp>
10 #include <common/error.hpp>
11 #include <common/macros.hpp>
12 #include <common/mi-lttng.hpp>
14 #include <lttng/condition/buffer-usage-internal.hpp>
15 #include <lttng/condition/condition-internal.hpp>
16 #include <lttng/condition/event-rule-matches-internal.hpp>
17 #include <lttng/condition/session-consumed-size-internal.hpp>
18 #include <lttng/condition/session-rotation-internal.hpp>
19 #include <lttng/error-query-internal.hpp>
23 enum lttng_condition_type
lttng_condition_get_type(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
= lttng::utils::container_of(ref
, <tng_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
);
55 bool lttng_condition_validate(const struct lttng_condition
*condition
)
64 if (!condition
->validate
) {
65 /* Sub-class guarantees that it can never be invalid. */
70 valid
= condition
->validate(condition
);
75 int lttng_condition_serialize(const struct lttng_condition
*condition
,
76 struct lttng_payload
*payload
)
79 struct lttng_condition_comm condition_comm
= {};
86 condition_comm
.condition_type
= (int8_t) condition
->type
;
88 ret
= lttng_dynamic_buffer_append(
89 &payload
->buffer
, &condition_comm
, sizeof(condition_comm
));
94 ret
= condition
->serialize(condition
, payload
);
102 bool lttng_condition_is_equal(const struct lttng_condition
*a
, const struct lttng_condition
*b
)
104 bool is_equal
= false;
110 if (a
->type
!= b
->type
) {
119 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
124 ssize_t
lttng_condition_create_from_payload(struct lttng_payload_view
*view
,
125 struct lttng_condition
**condition
)
127 ssize_t ret
, condition_size
= 0;
128 condition_create_from_payload_cb create_from_payload
= nullptr;
129 const struct lttng_condition_comm
*condition_comm
;
130 const struct lttng_payload_view condition_comm_view
=
131 lttng_payload_view_from_view(view
, 0, sizeof(*condition_comm
));
133 if (!view
|| !condition
) {
138 if (!lttng_payload_view_is_valid(&condition_comm_view
)) {
139 /* Payload not large enough to contain the header. */
144 DBG("Deserializing condition from buffer");
145 condition_comm
= (typeof(condition_comm
)) condition_comm_view
.buffer
.data
;
146 condition_size
+= sizeof(*condition_comm
);
148 switch ((enum lttng_condition_type
) condition_comm
->condition_type
) {
149 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
150 create_from_payload
= lttng_condition_buffer_usage_low_create_from_payload
;
152 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
153 create_from_payload
= lttng_condition_buffer_usage_high_create_from_payload
;
155 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
156 create_from_payload
= lttng_condition_session_consumed_size_create_from_payload
;
158 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
159 create_from_payload
= lttng_condition_session_rotation_ongoing_create_from_payload
;
161 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
162 create_from_payload
=
163 lttng_condition_session_rotation_completed_create_from_payload
;
165 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
166 create_from_payload
= lttng_condition_event_rule_matches_create_from_payload
;
169 ERR("Attempted to create condition of unknown type (%i)",
170 (int) condition_comm
->condition_type
);
175 if (create_from_payload
) {
176 struct lttng_payload_view condition_view
=
177 lttng_payload_view_from_view(view
, sizeof(*condition_comm
), -1);
179 ret
= create_from_payload(&condition_view
, condition
);
183 condition_size
+= ret
;
189 ret
= condition_size
;
194 void lttng_condition_init(struct lttng_condition
*condition
, enum lttng_condition_type type
)
196 condition
->type
= type
;
197 urcu_ref_init(&condition
->ref
);
200 const char *lttng_condition_type_str(enum lttng_condition_type type
)
203 case LTTNG_CONDITION_TYPE_UNKNOWN
:
206 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
207 return "session consumed size";
209 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
210 return "buffer usage high";
212 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
213 return "buffer usage low";
215 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
216 return "session rotation ongoing";
218 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
219 return "session rotation completed";
221 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
222 return "event rule matches";
229 enum lttng_error_code
230 lttng_condition_mi_serialize(const struct lttng_trigger
*trigger
,
231 const struct lttng_condition
*condition
,
232 struct mi_writer
*writer
,
233 const struct mi_lttng_error_query_callbacks
*error_query_callbacks
)
236 enum lttng_error_code ret_code
;
237 struct lttng_error_query_results
*error_query_results
= nullptr;
239 LTTNG_ASSERT(condition
);
240 LTTNG_ASSERT(writer
);
241 LTTNG_ASSERT(condition
->mi_serialize
);
243 /* Open condition element. */
244 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_condition
);
249 /* Serialize underlying condition. */
250 ret_code
= condition
->mi_serialize(condition
, writer
);
251 if (ret_code
!= LTTNG_OK
) {
255 /* Serialize error query results for the action. */
256 if (error_query_callbacks
&& error_query_callbacks
->action_cb
) {
257 ret_code
= error_query_callbacks
->condition_cb(trigger
, &error_query_results
);
258 if (ret_code
!= LTTNG_OK
) {
262 ret_code
= lttng_error_query_results_mi_serialize(error_query_results
, writer
);
263 if (ret_code
!= LTTNG_OK
) {
268 /* Close condition element. */
269 ret
= mi_lttng_writer_close_element(writer
);
278 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
280 lttng_error_query_results_destroy(error_query_results
);