liblttng-ctl: use export list to define exported symbols
[lttng-tools.git] / src / common / conditions / condition.c
CommitLineData
a58c490f 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
a58c490f 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
a58c490f 5 *
a58c490f
JG
6 */
7
6a751b95
JR
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>
a58c490f 13#include <lttng/condition/buffer-usage-internal.h>
6a751b95 14#include <lttng/condition/condition-internal.h>
670a26e4 15#include <lttng/condition/event-rule-matches-internal.h>
e8360425 16#include <lttng/condition/session-consumed-size-internal.h>
c19092cd 17#include <lttng/condition/session-rotation-internal.h>
6a751b95 18#include <lttng/error-query-internal.h>
a58c490f 19#include <stdbool.h>
a58c490f
JG
20
21enum lttng_condition_type lttng_condition_get_type(
22 const struct lttng_condition *condition)
23{
24 return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN;
25}
26
27void lttng_condition_destroy(struct lttng_condition *condition)
4655b864
JR
28{
29 lttng_condition_put(condition);
30}
31
32static void condition_destroy_ref(struct urcu_ref *ref)
33{
34 struct lttng_condition *condition =
35 container_of(ref, struct lttng_condition, ref);
36
37 condition->destroy(condition);
38}
39
4655b864
JR
40void lttng_condition_get(struct lttng_condition *condition)
41{
42 urcu_ref_get(&condition->ref);
43}
44
4655b864 45void lttng_condition_put(struct lttng_condition *condition)
a58c490f
JG
46{
47 if (!condition) {
48 return;
49 }
50
a0377dfe 51 LTTNG_ASSERT(condition->destroy);
4655b864 52 urcu_ref_put(&condition->ref, condition_destroy_ref);
a58c490f
JG
53}
54
4655b864 55
a58c490f
JG
56bool lttng_condition_validate(const struct lttng_condition *condition)
57{
58 bool valid;
59
60 if (!condition) {
61 valid = false;
62 goto end;
63 }
64
65 if (!condition->validate) {
66 /* Sub-class guarantees that it can never be invalid. */
67 valid = true;
68 goto end;
69 }
70
71 valid = condition->validate(condition);
72end:
73 return valid;
74}
75
3647288f 76int lttng_condition_serialize(const struct lttng_condition *condition,
c0a66c84 77 struct lttng_payload *payload)
a58c490f 78{
3647288f 79 int ret;
c0a66c84 80 struct lttng_condition_comm condition_comm = {};
a58c490f
JG
81
82 if (!condition) {
83 ret = -1;
84 goto end;
85 }
86
3647288f
JG
87 condition_comm.condition_type = (int8_t) condition->type;
88
c0a66c84 89 ret = lttng_dynamic_buffer_append(&payload->buffer, &condition_comm,
3647288f
JG
90 sizeof(condition_comm));
91 if (ret) {
92 goto end;
a58c490f
JG
93 }
94
c0a66c84 95 ret = condition->serialize(condition, payload);
3647288f 96 if (ret) {
a58c490f
JG
97 goto end;
98 }
a58c490f
JG
99end:
100 return ret;
101}
102
a58c490f
JG
103bool lttng_condition_is_equal(const struct lttng_condition *a,
104 const struct lttng_condition *b)
105{
106 bool is_equal = false;
107
108 if (!a || !b) {
109 goto end;
110 }
111
112 if (a->type != b->type) {
113 goto end;
114 }
115
6c2fe319
JG
116 if (a == b) {
117 is_equal = true;
118 goto end;
119 }
120
a58c490f
JG
121 is_equal = a->equal ? a->equal(a, b) : true;
122end:
123 return is_equal;
124}
125
c0a66c84
JG
126ssize_t lttng_condition_create_from_payload(
127 struct lttng_payload_view *view,
a58c490f
JG
128 struct lttng_condition **condition)
129{
130 ssize_t ret, condition_size = 0;
c0a66c84 131 condition_create_from_payload_cb create_from_payload = NULL;
3e6e0df2
JG
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));
a58c490f 136
c0a66c84 137 if (!view || !condition) {
a58c490f
JG
138 ret = -1;
139 goto end;
140 }
141
3e6e0df2
JG
142 if (!lttng_payload_view_is_valid(&condition_comm_view)) {
143 /* Payload not large enough to contain the header. */
144 ret = -1;
145 goto end;
146 }
147
a58c490f 148 DBG("Deserializing condition from buffer");
3e6e0df2 149 condition_comm = (typeof(condition_comm)) condition_comm_view.buffer.data;
a58c490f
JG
150 condition_size += sizeof(*condition_comm);
151
152 switch ((enum lttng_condition_type) condition_comm->condition_type) {
153 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
c0a66c84 154 create_from_payload = lttng_condition_buffer_usage_low_create_from_payload;
a58c490f
JG
155 break;
156 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
c0a66c84 157 create_from_payload = lttng_condition_buffer_usage_high_create_from_payload;
a58c490f 158 break;
e8360425 159 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
c0a66c84 160 create_from_payload = lttng_condition_session_consumed_size_create_from_payload;
e8360425 161 break;
c19092cd 162 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
c0a66c84 163 create_from_payload = lttng_condition_session_rotation_ongoing_create_from_payload;
c19092cd
JG
164 break;
165 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
c0a66c84 166 create_from_payload = lttng_condition_session_rotation_completed_create_from_payload;
c19092cd 167 break;
8dbb86b8
JR
168 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
169 create_from_payload =
170 lttng_condition_event_rule_matches_create_from_payload;
683d081a 171 break;
a58c490f
JG
172 default:
173 ERR("Attempted to create condition of unknown type (%i)",
174 (int) condition_comm->condition_type);
175 ret = -1;
176 goto end;
177 }
178
c0a66c84
JG
179 if (create_from_payload) {
180 struct lttng_payload_view condition_view =
181 lttng_payload_view_from_view(view,
a58c490f
JG
182 sizeof(*condition_comm), -1);
183
c0a66c84 184 ret = create_from_payload(&condition_view, condition);
a58c490f
JG
185 if (ret < 0) {
186 goto end;
187 }
188 condition_size += ret;
189
190 } else {
191 abort();
192 }
193
194 ret = condition_size;
195end:
196 return ret;
197}
198
a58c490f
JG
199void lttng_condition_init(struct lttng_condition *condition,
200 enum lttng_condition_type type)
201{
202 condition->type = type;
4655b864 203 urcu_ref_init(&condition->ref);
a58c490f 204}
0de2479d 205
0de2479d
SM
206const char *lttng_condition_type_str(enum lttng_condition_type type)
207{
208 switch (type) {
209 case LTTNG_CONDITION_TYPE_UNKNOWN:
210 return "unknown";
211
212 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
213 return "session consumed size";
214
215 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
216 return "buffer usage high";
217
218 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
219 return "buffer usage low";
220
221 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
222 return "session rotation ongoing";
223
224 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
225 return "session rotation completed";
226
8dbb86b8 227 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
8c1d25ff 228 return "event rule matches";
0de2479d
SM
229
230 default:
231 return "???";
232 }
233}
6a751b95 234
6a751b95
JR
235enum 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)
240{
241 int ret;
242 enum lttng_error_code ret_code;
243 struct lttng_error_query_results *error_query_results = NULL;
244
a0377dfe
FD
245 LTTNG_ASSERT(condition);
246 LTTNG_ASSERT(writer);
247 LTTNG_ASSERT(condition->mi_serialize);
6a751b95
JR
248
249 /* Open condition element. */
250 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_condition);
251 if (ret) {
252 goto mi_error;
253 }
254
255 /* Serialize underlying condition. */
256 ret_code = condition->mi_serialize(condition, writer);
257 if (ret_code != LTTNG_OK) {
258 goto end;
259 }
260
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) {
266 goto end;
267 }
268
269 ret_code = lttng_error_query_results_mi_serialize(
270 error_query_results, writer);
271 if (ret_code != LTTNG_OK) {
272 goto end;
273 }
274 }
275
276 /* Close condition element. */
277 ret = mi_lttng_writer_close_element(writer);
278 if (ret) {
279 goto mi_error;
280 }
281
282 ret_code = LTTNG_OK;
283 goto end;
284
285mi_error:
286 ret_code = LTTNG_ERR_MI_IO_FAIL;
287end:
288 lttng_error_query_results_destroy(error_query_results);
289 return ret_code;
290}
This page took 0.048989 seconds and 4 git commands to generate.