Remove lttng_event_rule_tracepoint
[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/macros.h>
12 #include <common/payload.h>
13 #include <common/payload-view.h>
14 #include <common/hashtable/hashtable.h>
15 #include <common/hashtable/utils.h>
16 #include <lttng/event-rule/event-rule-internal.h>
17 #include <lttng/event-rule/kernel-kprobe-internal.h>
18 #include <lttng/event-rule/kernel-syscall-internal.h>
19 #include <lttng/event-rule/log4j-logging-internal.h>
20 #include <lttng/event-rule/jul-logging-internal.h>
21 #include <lttng/event-rule/python-logging-internal.h>
22 #include <lttng/event-rule/kernel-tracepoint-internal.h>
23 #include <lttng/event-rule/kernel-uprobe-internal.h>
24 #include <lttng/event-rule/user-tracepoint-internal.h>
25 #include <stdbool.h>
26
27 enum lttng_event_rule_type lttng_event_rule_get_type(
28 const struct lttng_event_rule *event_rule)
29 {
30 return event_rule ? event_rule->type : LTTNG_EVENT_RULE_TYPE_UNKNOWN;
31 }
32
33 LTTNG_HIDDEN
34 enum lttng_domain_type lttng_event_rule_get_domain_type(
35 const struct lttng_event_rule *event_rule)
36 {
37 enum lttng_domain_type domain_type = LTTNG_DOMAIN_NONE;
38
39 switch (lttng_event_rule_get_type(event_rule)) {
40 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
41 domain_type = LTTNG_DOMAIN_UST;
42 break;
43 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
44 domain_type = LTTNG_DOMAIN_JUL;
45 break;
46 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
47 domain_type = LTTNG_DOMAIN_LOG4J;
48 break;
49 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
50 domain_type = LTTNG_DOMAIN_PYTHON;
51 break;
52 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
53 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
54 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION:
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_FUNCTION:
185 /* TODO */
186 break;
187 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
188 create_from_payload = lttng_event_rule_kernel_uprobe_create_from_payload;
189 break;
190 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
191 create_from_payload =
192 lttng_event_rule_kernel_syscall_create_from_payload;
193 break;
194 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
195 create_from_payload =
196 lttng_event_rule_kernel_tracepoint_create_from_payload;
197 break;
198 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
199 create_from_payload =
200 lttng_event_rule_user_tracepoint_create_from_payload;
201 break;
202 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
203 create_from_payload =
204 lttng_event_rule_jul_logging_create_from_payload;
205 break;
206 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
207 create_from_payload =
208 lttng_event_rule_log4j_logging_create_from_payload;
209 break;
210 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
211 create_from_payload =
212 lttng_event_rule_python_logging_create_from_payload;
213 break;
214 default:
215 ERR("Attempted to create event rule of unknown type (%i)",
216 (int) event_rule_comm->event_rule_type);
217 ret = -1;
218 goto end;
219 }
220
221 assert(create_from_payload);
222
223 {
224 struct lttng_payload_view child_view =
225 lttng_payload_view_from_view(
226 view, consumed, -1);
227
228 ret = create_from_payload(&child_view, event_rule);
229 if (ret < 0) {
230 goto end;
231 }
232
233 consumed += ret;
234 }
235
236 if (!lttng_event_rule_validate(*event_rule)) {
237 ret = -1;
238 goto end;
239 }
240
241 ret = consumed;
242 end:
243 return ret;
244 }
245
246 LTTNG_HIDDEN
247 void lttng_event_rule_init(struct lttng_event_rule *event_rule,
248 enum lttng_event_rule_type type)
249 {
250 urcu_ref_init(&event_rule->ref);
251 event_rule->type = type;
252 }
253
254 LTTNG_HIDDEN
255 bool lttng_event_rule_get(struct lttng_event_rule *event_rule)
256 {
257 return urcu_ref_get_unless_zero(&event_rule->ref);
258 }
259
260 LTTNG_HIDDEN
261 void lttng_event_rule_put(struct lttng_event_rule *event_rule)
262 {
263 if (!event_rule) {
264 return;
265 }
266
267 assert(event_rule->ref.refcount);
268 urcu_ref_put(&event_rule->ref, lttng_event_rule_release);
269 }
270
271 LTTNG_HIDDEN
272 enum lttng_error_code lttng_event_rule_generate_filter_bytecode(
273 struct lttng_event_rule *rule,
274 const struct lttng_credentials *creds)
275 {
276 assert(rule->generate_filter_bytecode);
277 return rule->generate_filter_bytecode(rule, creds);
278 }
279
280 LTTNG_HIDDEN
281 const char *lttng_event_rule_get_filter(const struct lttng_event_rule *rule)
282 {
283 assert(rule->get_filter);
284 return rule->get_filter(rule);
285 }
286
287 LTTNG_HIDDEN
288 const struct lttng_bytecode *lttng_event_rule_get_filter_bytecode(
289 const struct lttng_event_rule *rule)
290 {
291 assert(rule->get_filter_bytecode);
292 return rule->get_filter_bytecode(rule);
293 }
294
295 LTTNG_HIDDEN
296 enum lttng_event_rule_generate_exclusions_status
297 lttng_event_rule_generate_exclusions(const struct lttng_event_rule *rule,
298 struct lttng_event_exclusion **exclusions)
299 {
300 assert(rule->generate_exclusions);
301 return rule->generate_exclusions(rule, exclusions);
302 }
303
304 LTTNG_HIDDEN
305 struct lttng_event *lttng_event_rule_generate_lttng_event(
306 const struct lttng_event_rule *rule)
307 {
308 assert(rule->generate_lttng_event);
309 return rule->generate_lttng_event(rule);
310 }
311
312 LTTNG_HIDDEN
313 bool lttng_event_rule_targets_agent_domain(const struct lttng_event_rule *rule)
314 {
315 bool targets_agent_domain = false;
316 enum lttng_domain_type type = lttng_event_rule_get_domain_type(rule);
317
318 switch (type) {
319 case LTTNG_DOMAIN_JUL:
320 case LTTNG_DOMAIN_LOG4J:
321 case LTTNG_DOMAIN_PYTHON:
322 targets_agent_domain = true;
323 break;
324 case LTTNG_DOMAIN_UST:
325 case LTTNG_DOMAIN_KERNEL:
326 targets_agent_domain = false;
327 break;
328 default:
329 abort();
330 };
331
332 return targets_agent_domain;
333 }
334
335 const char *lttng_event_rule_type_str(enum lttng_event_rule_type type)
336 {
337 switch (type) {
338 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
339 return "unknown";
340 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
341 return "kernel syscall";
342 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
343 return "kernel kprobe";
344 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION:
345 return "function";
346 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
347 return "kernel uprobe";
348 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
349 return "kernel tracepoint";
350 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
351 return "user tracepoint";
352 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
353 return "jul logging";
354 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
355 return "log4j logging";
356 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
357 return "python logging";
358
359 default:
360 abort();
361 }
362 }
363
364 LTTNG_HIDDEN
365 unsigned long lttng_event_rule_hash(const struct lttng_event_rule *rule)
366 {
367 assert(rule->hash);
368 return rule->hash(rule);
369 }
This page took 0.043577 seconds and 4 git commands to generate.