MI: implement all objects related to trigger machine interface
[lttng-tools.git] / src / common / event-rule / event-rule.c
1 /*
2 * Copyright (C) 2019 Jonathan Rajotte
3 * <jonathan.rajotte-julien@efficios.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 *
7 */
8
9 #include <assert.h>
10 #include <common/error.h>
11 #include <common/hashtable/hashtable.h>
12 #include <common/hashtable/utils.h>
13 #include <common/macros.h>
14 #include <common/mi-lttng.h>
15 #include <common/payload-view.h>
16 #include <common/payload.h>
17 #include <lttng/event-rule/event-rule-internal.h>
18 #include <lttng/event-rule/jul-logging-internal.h>
19 #include <lttng/event-rule/kernel-kprobe-internal.h>
20 #include <lttng/event-rule/kernel-syscall-internal.h>
21 #include <lttng/event-rule/kernel-tracepoint-internal.h>
22 #include <lttng/event-rule/kernel-uprobe-internal.h>
23 #include <lttng/event-rule/log4j-logging-internal.h>
24 #include <lttng/event-rule/python-logging-internal.h>
25 #include <lttng/event-rule/user-tracepoint-internal.h>
26 #include <stdbool.h>
27
28 enum lttng_event_rule_type lttng_event_rule_get_type(
29 const struct lttng_event_rule *event_rule)
30 {
31 return event_rule ? event_rule->type : LTTNG_EVENT_RULE_TYPE_UNKNOWN;
32 }
33
34 LTTNG_HIDDEN
35 enum lttng_domain_type lttng_event_rule_get_domain_type(
36 const struct lttng_event_rule *event_rule)
37 {
38 enum lttng_domain_type domain_type = LTTNG_DOMAIN_NONE;
39
40 switch (lttng_event_rule_get_type(event_rule)) {
41 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
42 domain_type = LTTNG_DOMAIN_UST;
43 break;
44 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
45 domain_type = LTTNG_DOMAIN_JUL;
46 break;
47 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
48 domain_type = LTTNG_DOMAIN_LOG4J;
49 break;
50 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
51 domain_type = LTTNG_DOMAIN_PYTHON;
52 break;
53 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
54 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
55 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
56 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
57 domain_type = LTTNG_DOMAIN_KERNEL;
58 break;
59 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
60 domain_type = LTTNG_DOMAIN_NONE;
61 break;
62 }
63
64 return domain_type;
65 }
66
67 static void lttng_event_rule_release(struct urcu_ref *ref)
68 {
69 struct lttng_event_rule *event_rule =
70 container_of(ref, typeof(*event_rule), ref);
71
72 assert(event_rule->destroy);
73 event_rule->destroy(event_rule);
74 }
75
76 void lttng_event_rule_destroy(struct lttng_event_rule *event_rule)
77 {
78 lttng_event_rule_put(event_rule);
79 }
80
81 LTTNG_HIDDEN
82 bool lttng_event_rule_validate(const struct lttng_event_rule *event_rule)
83 {
84 bool valid;
85
86 if (!event_rule) {
87 valid = false;
88 goto end;
89 }
90
91 if (!event_rule->validate) {
92 /* Sub-class guarantees that it can never be invalid. */
93 valid = true;
94 goto end;
95 }
96
97 valid = event_rule->validate(event_rule);
98 end:
99 return valid;
100 }
101
102 LTTNG_HIDDEN
103 int lttng_event_rule_serialize(const struct lttng_event_rule *event_rule,
104 struct lttng_payload *payload)
105 {
106 int ret;
107 struct lttng_event_rule_comm event_rule_comm = {};
108
109 if (!event_rule) {
110 ret = -1;
111 goto end;
112 }
113
114 event_rule_comm.event_rule_type = (int8_t) event_rule->type;
115
116 ret = lttng_dynamic_buffer_append(
117 &payload->buffer, &event_rule_comm, sizeof(event_rule_comm));
118 if (ret) {
119 goto end;
120 }
121
122 ret = event_rule->serialize(event_rule, payload);
123 if (ret) {
124 goto end;
125 }
126 end:
127 return ret;
128 }
129
130 LTTNG_HIDDEN
131 bool lttng_event_rule_is_equal(const struct lttng_event_rule *a,
132 const struct lttng_event_rule *b)
133 {
134 bool is_equal = false;
135
136 if (!a || !b) {
137 goto end;
138 }
139
140 if (a->type != b->type) {
141 goto end;
142 }
143
144 if (a == b) {
145 is_equal = true;
146 goto end;
147 }
148
149 is_equal = a->equal ? a->equal(a, b) : true;
150 end:
151 return is_equal;
152 }
153
154 LTTNG_HIDDEN
155 ssize_t lttng_event_rule_create_from_payload(
156 struct lttng_payload_view *view,
157 struct lttng_event_rule **event_rule)
158 {
159 ssize_t ret, consumed = 0;
160 event_rule_create_from_payload_cb create_from_payload = NULL;
161 const struct lttng_event_rule_comm *event_rule_comm;
162 const struct lttng_payload_view event_rule_comm_view =
163 lttng_payload_view_from_view(
164 view, 0, sizeof(*event_rule_comm));
165
166 if (!view || !event_rule) {
167 ret = -1;
168 goto end;
169 }
170
171 if (!lttng_payload_view_is_valid(&event_rule_comm_view)) {
172 ret = -1;
173 goto end;
174 }
175
176 DBG("Deserializing event_rule from payload");
177 event_rule_comm = (const struct lttng_event_rule_comm *) event_rule_comm_view.buffer.data;
178 consumed += sizeof(*event_rule_comm);
179
180 switch ((enum lttng_event_rule_type) event_rule_comm->event_rule_type) {
181 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
182 create_from_payload = lttng_event_rule_kernel_kprobe_create_from_payload;
183 break;
184 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
185 create_from_payload = lttng_event_rule_kernel_uprobe_create_from_payload;
186 break;
187 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
188 create_from_payload =
189 lttng_event_rule_kernel_syscall_create_from_payload;
190 break;
191 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
192 create_from_payload =
193 lttng_event_rule_kernel_tracepoint_create_from_payload;
194 break;
195 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
196 create_from_payload =
197 lttng_event_rule_user_tracepoint_create_from_payload;
198 break;
199 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
200 create_from_payload =
201 lttng_event_rule_jul_logging_create_from_payload;
202 break;
203 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
204 create_from_payload =
205 lttng_event_rule_log4j_logging_create_from_payload;
206 break;
207 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
208 create_from_payload =
209 lttng_event_rule_python_logging_create_from_payload;
210 break;
211 default:
212 ERR("Attempted to create event rule of unknown type (%i)",
213 (int) event_rule_comm->event_rule_type);
214 ret = -1;
215 goto end;
216 }
217
218 assert(create_from_payload);
219
220 {
221 struct lttng_payload_view child_view =
222 lttng_payload_view_from_view(
223 view, consumed, -1);
224
225 ret = create_from_payload(&child_view, event_rule);
226 if (ret < 0) {
227 goto end;
228 }
229
230 consumed += ret;
231 }
232
233 if (!lttng_event_rule_validate(*event_rule)) {
234 ret = -1;
235 goto end;
236 }
237
238 ret = consumed;
239 end:
240 return ret;
241 }
242
243 LTTNG_HIDDEN
244 void lttng_event_rule_init(struct lttng_event_rule *event_rule,
245 enum lttng_event_rule_type type)
246 {
247 urcu_ref_init(&event_rule->ref);
248 event_rule->type = type;
249 }
250
251 LTTNG_HIDDEN
252 bool lttng_event_rule_get(struct lttng_event_rule *event_rule)
253 {
254 return urcu_ref_get_unless_zero(&event_rule->ref);
255 }
256
257 LTTNG_HIDDEN
258 void lttng_event_rule_put(struct lttng_event_rule *event_rule)
259 {
260 if (!event_rule) {
261 return;
262 }
263
264 assert(event_rule->ref.refcount);
265 urcu_ref_put(&event_rule->ref, lttng_event_rule_release);
266 }
267
268 LTTNG_HIDDEN
269 enum lttng_error_code lttng_event_rule_generate_filter_bytecode(
270 struct lttng_event_rule *rule,
271 const struct lttng_credentials *creds)
272 {
273 assert(rule->generate_filter_bytecode);
274 return rule->generate_filter_bytecode(rule, creds);
275 }
276
277 LTTNG_HIDDEN
278 const char *lttng_event_rule_get_filter(const struct lttng_event_rule *rule)
279 {
280 assert(rule->get_filter);
281 return rule->get_filter(rule);
282 }
283
284 LTTNG_HIDDEN
285 const struct lttng_bytecode *lttng_event_rule_get_filter_bytecode(
286 const struct lttng_event_rule *rule)
287 {
288 assert(rule->get_filter_bytecode);
289 return rule->get_filter_bytecode(rule);
290 }
291
292 LTTNG_HIDDEN
293 enum lttng_event_rule_generate_exclusions_status
294 lttng_event_rule_generate_exclusions(const struct lttng_event_rule *rule,
295 struct lttng_event_exclusion **exclusions)
296 {
297 assert(rule->generate_exclusions);
298 return rule->generate_exclusions(rule, exclusions);
299 }
300
301 LTTNG_HIDDEN
302 struct lttng_event *lttng_event_rule_generate_lttng_event(
303 const struct lttng_event_rule *rule)
304 {
305 assert(rule->generate_lttng_event);
306 return rule->generate_lttng_event(rule);
307 }
308
309 LTTNG_HIDDEN
310 bool lttng_event_rule_targets_agent_domain(const struct lttng_event_rule *rule)
311 {
312 bool targets_agent_domain = false;
313 enum lttng_domain_type type = lttng_event_rule_get_domain_type(rule);
314
315 switch (type) {
316 case LTTNG_DOMAIN_JUL:
317 case LTTNG_DOMAIN_LOG4J:
318 case LTTNG_DOMAIN_PYTHON:
319 targets_agent_domain = true;
320 break;
321 case LTTNG_DOMAIN_UST:
322 case LTTNG_DOMAIN_KERNEL:
323 targets_agent_domain = false;
324 break;
325 default:
326 abort();
327 };
328
329 return targets_agent_domain;
330 }
331
332 const char *lttng_event_rule_type_str(enum lttng_event_rule_type type)
333 {
334 switch (type) {
335 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
336 return "unknown";
337 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
338 return "kernel syscall";
339 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
340 return "kernel kprobe";
341 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
342 return "kernel uprobe";
343 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
344 return "kernel tracepoint";
345 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
346 return "user tracepoint";
347 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
348 return "jul logging";
349 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
350 return "log4j logging";
351 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
352 return "python logging";
353
354 default:
355 abort();
356 }
357 }
358
359 LTTNG_HIDDEN
360 unsigned long lttng_event_rule_hash(const struct lttng_event_rule *rule)
361 {
362 assert(rule->hash);
363 return rule->hash(rule);
364 }
365
366 LTTNG_HIDDEN
367 enum lttng_error_code lttng_event_rule_mi_serialize(
368 const struct lttng_event_rule *rule, struct mi_writer *writer)
369 {
370 int ret;
371 enum lttng_error_code ret_code;
372
373 assert(rule);
374 assert(writer);
375 assert(rule->mi_serialize);
376
377 /* Open event rule element. */
378 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_rule);
379 if (ret) {
380 goto mi_error;
381 }
382
383 /* Serialize underlying event rule. */
384 ret_code = rule->mi_serialize(rule, writer);
385 if (ret_code != LTTNG_OK) {
386 goto end;
387 }
388
389 /* Close event rule element. */
390 ret = mi_lttng_writer_close_element(writer);
391 if (ret) {
392 goto mi_error;
393 }
394
395 ret_code = LTTNG_OK;
396 goto end;
397
398 mi_error:
399 ret_code = LTTNG_ERR_MI_IO_FAIL;
400 end:
401 return ret_code;
402 }
This page took 0.051942 seconds and 5 git commands to generate.