Commit | Line | Data |
---|---|---|
7a3dcaf6 JR |
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> | |
959e3c66 JR |
14 | #include <common/hashtable/hashtable.h> |
15 | #include <common/hashtable/utils.h> | |
7a3dcaf6 | 16 | #include <lttng/event-rule/event-rule-internal.h> |
d84633c4 | 17 | #include <lttng/event-rule/kernel-kprobe-internal.h> |
213efb0c | 18 | #include <lttng/event-rule/kernel-syscall-internal.h> |
08662c9b | 19 | #include <lttng/event-rule/log4j-logging-internal.h> |
d440991b | 20 | #include <lttng/event-rule/jul-logging-internal.h> |
f30f4bf4 | 21 | #include <lttng/event-rule/python-logging-internal.h> |
b25b2570 | 22 | #include <lttng/event-rule/kernel-tracepoint-internal.h> |
e60e9a9a | 23 | #include <lttng/event-rule/kernel-uprobe-internal.h> |
cc66eccf | 24 | #include <lttng/event-rule/user-tracepoint-internal.h> |
7a3dcaf6 JR |
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)) { | |
cc66eccf JR |
40 | case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT: |
41 | domain_type = LTTNG_DOMAIN_UST; | |
42 | break; | |
d440991b JR |
43 | case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING: |
44 | domain_type = LTTNG_DOMAIN_JUL; | |
45 | break; | |
08662c9b JR |
46 | case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING: |
47 | domain_type = LTTNG_DOMAIN_LOG4J; | |
48 | break; | |
f30f4bf4 JR |
49 | case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING: |
50 | domain_type = LTTNG_DOMAIN_PYTHON; | |
51 | break; | |
213efb0c | 52 | case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL: |
d84633c4 | 53 | case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE: |
e60e9a9a | 54 | case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE: |
b25b2570 | 55 | case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT: |
7a3dcaf6 JR |
56 | domain_type = LTTNG_DOMAIN_KERNEL; |
57 | break; | |
58 | case LTTNG_EVENT_RULE_TYPE_UNKNOWN: | |
59 | domain_type = LTTNG_DOMAIN_NONE; | |
60 | break; | |
61 | } | |
62 | ||
63 | return domain_type; | |
64 | } | |
65 | ||
66 | static void lttng_event_rule_release(struct urcu_ref *ref) | |
67 | { | |
68 | struct lttng_event_rule *event_rule = | |
69 | container_of(ref, typeof(*event_rule), ref); | |
70 | ||
71 | assert(event_rule->destroy); | |
72 | event_rule->destroy(event_rule); | |
73 | } | |
74 | ||
75 | void lttng_event_rule_destroy(struct lttng_event_rule *event_rule) | |
76 | { | |
77 | lttng_event_rule_put(event_rule); | |
78 | } | |
79 | ||
80 | LTTNG_HIDDEN | |
81 | bool lttng_event_rule_validate(const struct lttng_event_rule *event_rule) | |
82 | { | |
83 | bool valid; | |
84 | ||
85 | if (!event_rule) { | |
86 | valid = false; | |
87 | goto end; | |
88 | } | |
89 | ||
90 | if (!event_rule->validate) { | |
91 | /* Sub-class guarantees that it can never be invalid. */ | |
92 | valid = true; | |
93 | goto end; | |
94 | } | |
95 | ||
96 | valid = event_rule->validate(event_rule); | |
97 | end: | |
98 | return valid; | |
99 | } | |
100 | ||
101 | LTTNG_HIDDEN | |
102 | int lttng_event_rule_serialize(const struct lttng_event_rule *event_rule, | |
103 | struct lttng_payload *payload) | |
104 | { | |
105 | int ret; | |
106 | struct lttng_event_rule_comm event_rule_comm = {}; | |
107 | ||
108 | if (!event_rule) { | |
109 | ret = -1; | |
110 | goto end; | |
111 | } | |
112 | ||
113 | event_rule_comm.event_rule_type = (int8_t) event_rule->type; | |
114 | ||
115 | ret = lttng_dynamic_buffer_append( | |
116 | &payload->buffer, &event_rule_comm, sizeof(event_rule_comm)); | |
117 | if (ret) { | |
118 | goto end; | |
119 | } | |
120 | ||
121 | ret = event_rule->serialize(event_rule, payload); | |
122 | if (ret) { | |
123 | goto end; | |
124 | } | |
125 | end: | |
126 | return ret; | |
127 | } | |
128 | ||
129 | LTTNG_HIDDEN | |
130 | bool lttng_event_rule_is_equal(const struct lttng_event_rule *a, | |
131 | const struct lttng_event_rule *b) | |
132 | { | |
133 | bool is_equal = false; | |
134 | ||
135 | if (!a || !b) { | |
136 | goto end; | |
137 | } | |
138 | ||
139 | if (a->type != b->type) { | |
140 | goto end; | |
141 | } | |
142 | ||
143 | if (a == b) { | |
144 | is_equal = true; | |
145 | goto end; | |
146 | } | |
147 | ||
148 | is_equal = a->equal ? a->equal(a, b) : true; | |
149 | end: | |
150 | return is_equal; | |
151 | } | |
152 | ||
153 | LTTNG_HIDDEN | |
154 | ssize_t lttng_event_rule_create_from_payload( | |
155 | struct lttng_payload_view *view, | |
156 | struct lttng_event_rule **event_rule) | |
157 | { | |
158 | ssize_t ret, consumed = 0; | |
7a3dcaf6 | 159 | event_rule_create_from_payload_cb create_from_payload = NULL; |
3e6e0df2 JG |
160 | const struct lttng_event_rule_comm *event_rule_comm; |
161 | const struct lttng_payload_view event_rule_comm_view = | |
162 | lttng_payload_view_from_view( | |
163 | view, 0, sizeof(*event_rule_comm)); | |
7a3dcaf6 JR |
164 | |
165 | if (!view || !event_rule) { | |
166 | ret = -1; | |
167 | goto end; | |
168 | } | |
169 | ||
3e6e0df2 JG |
170 | if (!lttng_payload_view_is_valid(&event_rule_comm_view)) { |
171 | ret = -1; | |
172 | goto end; | |
173 | } | |
174 | ||
683d081a | 175 | DBG("Deserializing event_rule from payload"); |
3e6e0df2 | 176 | event_rule_comm = (const struct lttng_event_rule_comm *) event_rule_comm_view.buffer.data; |
7a3dcaf6 JR |
177 | consumed += sizeof(*event_rule_comm); |
178 | ||
179 | switch ((enum lttng_event_rule_type) event_rule_comm->event_rule_type) { | |
d84633c4 JR |
180 | case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE: |
181 | create_from_payload = lttng_event_rule_kernel_kprobe_create_from_payload; | |
7a3dcaf6 | 182 | break; |
e60e9a9a JR |
183 | case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE: |
184 | create_from_payload = lttng_event_rule_kernel_uprobe_create_from_payload; | |
7a3dcaf6 | 185 | break; |
213efb0c | 186 | case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL: |
e6a39346 | 187 | create_from_payload = |
213efb0c | 188 | lttng_event_rule_kernel_syscall_create_from_payload; |
7a3dcaf6 | 189 | break; |
b25b2570 JR |
190 | case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT: |
191 | create_from_payload = | |
192 | lttng_event_rule_kernel_tracepoint_create_from_payload; | |
193 | break; | |
cc66eccf JR |
194 | case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT: |
195 | create_from_payload = | |
196 | lttng_event_rule_user_tracepoint_create_from_payload; | |
197 | break; | |
d440991b JR |
198 | case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING: |
199 | create_from_payload = | |
200 | lttng_event_rule_jul_logging_create_from_payload; | |
201 | break; | |
08662c9b JR |
202 | case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING: |
203 | create_from_payload = | |
204 | lttng_event_rule_log4j_logging_create_from_payload; | |
205 | break; | |
f30f4bf4 JR |
206 | case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING: |
207 | create_from_payload = | |
208 | lttng_event_rule_python_logging_create_from_payload; | |
209 | break; | |
7a3dcaf6 JR |
210 | default: |
211 | ERR("Attempted to create event rule of unknown type (%i)", | |
212 | (int) event_rule_comm->event_rule_type); | |
213 | ret = -1; | |
214 | goto end; | |
215 | } | |
216 | ||
217 | assert(create_from_payload); | |
218 | ||
219 | { | |
220 | struct lttng_payload_view child_view = | |
221 | lttng_payload_view_from_view( | |
222 | view, consumed, -1); | |
223 | ||
224 | ret = create_from_payload(&child_view, event_rule); | |
225 | if (ret < 0) { | |
226 | goto end; | |
227 | } | |
228 | ||
229 | consumed += ret; | |
230 | } | |
231 | ||
232 | if (!lttng_event_rule_validate(*event_rule)) { | |
233 | ret = -1; | |
234 | goto end; | |
235 | } | |
236 | ||
237 | ret = consumed; | |
238 | end: | |
239 | return ret; | |
240 | } | |
241 | ||
242 | LTTNG_HIDDEN | |
243 | void lttng_event_rule_init(struct lttng_event_rule *event_rule, | |
244 | enum lttng_event_rule_type type) | |
245 | { | |
246 | urcu_ref_init(&event_rule->ref); | |
247 | event_rule->type = type; | |
248 | } | |
249 | ||
250 | LTTNG_HIDDEN | |
251 | bool lttng_event_rule_get(struct lttng_event_rule *event_rule) | |
252 | { | |
253 | return urcu_ref_get_unless_zero(&event_rule->ref); | |
254 | } | |
255 | ||
256 | LTTNG_HIDDEN | |
257 | void lttng_event_rule_put(struct lttng_event_rule *event_rule) | |
258 | { | |
259 | if (!event_rule) { | |
260 | return; | |
261 | } | |
262 | ||
263 | assert(event_rule->ref.refcount); | |
264 | urcu_ref_put(&event_rule->ref, lttng_event_rule_release); | |
265 | } | |
266 | ||
267 | LTTNG_HIDDEN | |
268 | enum lttng_error_code lttng_event_rule_generate_filter_bytecode( | |
58daac01 JR |
269 | struct lttng_event_rule *rule, |
270 | const struct lttng_credentials *creds) | |
7a3dcaf6 JR |
271 | { |
272 | assert(rule->generate_filter_bytecode); | |
58daac01 | 273 | return rule->generate_filter_bytecode(rule, creds); |
7a3dcaf6 JR |
274 | } |
275 | ||
276 | LTTNG_HIDDEN | |
277 | const char *lttng_event_rule_get_filter(const struct lttng_event_rule *rule) | |
278 | { | |
279 | assert(rule->get_filter); | |
280 | return rule->get_filter(rule); | |
281 | } | |
282 | ||
283 | LTTNG_HIDDEN | |
2b00d462 | 284 | const struct lttng_bytecode *lttng_event_rule_get_filter_bytecode( |
7a3dcaf6 JR |
285 | const struct lttng_event_rule *rule) |
286 | { | |
287 | assert(rule->get_filter_bytecode); | |
288 | return rule->get_filter_bytecode(rule); | |
289 | } | |
290 | ||
291 | LTTNG_HIDDEN | |
993578ff JR |
292 | enum lttng_event_rule_generate_exclusions_status |
293 | lttng_event_rule_generate_exclusions(const struct lttng_event_rule *rule, | |
294 | struct lttng_event_exclusion **exclusions) | |
7a3dcaf6 JR |
295 | { |
296 | assert(rule->generate_exclusions); | |
993578ff | 297 | return rule->generate_exclusions(rule, exclusions); |
7a3dcaf6 JR |
298 | } |
299 | ||
300 | LTTNG_HIDDEN | |
44760c20 JR |
301 | struct lttng_event *lttng_event_rule_generate_lttng_event( |
302 | const struct lttng_event_rule *rule) | |
303 | { | |
304 | assert(rule->generate_lttng_event); | |
305 | return rule->generate_lttng_event(rule); | |
306 | } | |
307 | ||
308 | LTTNG_HIDDEN | |
309 | bool lttng_event_rule_targets_agent_domain(const struct lttng_event_rule *rule) | |
310 | { | |
311 | bool targets_agent_domain = false; | |
312 | enum lttng_domain_type type = lttng_event_rule_get_domain_type(rule); | |
313 | ||
314 | switch (type) { | |
315 | case LTTNG_DOMAIN_JUL: | |
316 | case LTTNG_DOMAIN_LOG4J: | |
317 | case LTTNG_DOMAIN_PYTHON: | |
318 | targets_agent_domain = true; | |
319 | break; | |
320 | case LTTNG_DOMAIN_UST: | |
321 | case LTTNG_DOMAIN_KERNEL: | |
322 | targets_agent_domain = false; | |
323 | break; | |
324 | default: | |
325 | abort(); | |
326 | }; | |
327 | ||
328 | return targets_agent_domain; | |
329 | } | |
330 | ||
7a3dcaf6 JR |
331 | const char *lttng_event_rule_type_str(enum lttng_event_rule_type type) |
332 | { | |
333 | switch (type) { | |
334 | case LTTNG_EVENT_RULE_TYPE_UNKNOWN: | |
335 | return "unknown"; | |
213efb0c JR |
336 | case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL: |
337 | return "kernel syscall"; | |
d84633c4 JR |
338 | case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE: |
339 | return "kernel kprobe"; | |
e60e9a9a JR |
340 | case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE: |
341 | return "kernel uprobe"; | |
b25b2570 JR |
342 | case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT: |
343 | return "kernel tracepoint"; | |
cc66eccf JR |
344 | case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT: |
345 | return "user tracepoint"; | |
d440991b JR |
346 | case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING: |
347 | return "jul logging"; | |
08662c9b JR |
348 | case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING: |
349 | return "log4j logging"; | |
f30f4bf4 JR |
350 | case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING: |
351 | return "python logging"; | |
352 | ||
7a3dcaf6 JR |
353 | default: |
354 | abort(); | |
355 | } | |
356 | } | |
959e3c66 JR |
357 | |
358 | LTTNG_HIDDEN | |
359 | unsigned long lttng_event_rule_hash(const struct lttng_event_rule *rule) | |
360 | { | |
361 | assert(rule->hash); | |
362 | return rule->hash(rule); | |
363 | } |