8afc184170aa8e879dcca46bd3fd670f870465fd
[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-probe-internal.h>
18 #include <lttng/event-rule/syscall-internal.h>
19 #include <lttng/event-rule/tracepoint-internal.h>
20 #include <lttng/event-rule/uprobe-internal.h>
21 #include <stdbool.h>
22
23 enum lttng_event_rule_type lttng_event_rule_get_type(
24 const struct lttng_event_rule *event_rule)
25 {
26 return event_rule ? event_rule->type : LTTNG_EVENT_RULE_TYPE_UNKNOWN;
27 }
28
29 LTTNG_HIDDEN
30 enum lttng_domain_type lttng_event_rule_get_domain_type(
31 const struct lttng_event_rule *event_rule)
32 {
33 enum lttng_domain_type domain_type = LTTNG_DOMAIN_NONE;
34
35 switch (lttng_event_rule_get_type(event_rule)) {
36 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
37 {
38 enum lttng_event_rule_status status;
39 status = lttng_event_rule_tracepoint_get_domain_type(event_rule, &domain_type);
40 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
41 break;
42 }
43 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
44 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE:
45 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
46 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE:
47 domain_type = LTTNG_DOMAIN_KERNEL;
48 break;
49 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
50 domain_type = LTTNG_DOMAIN_NONE;
51 break;
52 }
53
54 return domain_type;
55 }
56
57 static void lttng_event_rule_release(struct urcu_ref *ref)
58 {
59 struct lttng_event_rule *event_rule =
60 container_of(ref, typeof(*event_rule), ref);
61
62 assert(event_rule->destroy);
63 event_rule->destroy(event_rule);
64 }
65
66 void lttng_event_rule_destroy(struct lttng_event_rule *event_rule)
67 {
68 lttng_event_rule_put(event_rule);
69 }
70
71 LTTNG_HIDDEN
72 bool lttng_event_rule_validate(const struct lttng_event_rule *event_rule)
73 {
74 bool valid;
75
76 if (!event_rule) {
77 valid = false;
78 goto end;
79 }
80
81 if (!event_rule->validate) {
82 /* Sub-class guarantees that it can never be invalid. */
83 valid = true;
84 goto end;
85 }
86
87 valid = event_rule->validate(event_rule);
88 end:
89 return valid;
90 }
91
92 LTTNG_HIDDEN
93 int lttng_event_rule_serialize(const struct lttng_event_rule *event_rule,
94 struct lttng_payload *payload)
95 {
96 int ret;
97 struct lttng_event_rule_comm event_rule_comm = {};
98
99 if (!event_rule) {
100 ret = -1;
101 goto end;
102 }
103
104 event_rule_comm.event_rule_type = (int8_t) event_rule->type;
105
106 ret = lttng_dynamic_buffer_append(
107 &payload->buffer, &event_rule_comm, sizeof(event_rule_comm));
108 if (ret) {
109 goto end;
110 }
111
112 ret = event_rule->serialize(event_rule, payload);
113 if (ret) {
114 goto end;
115 }
116 end:
117 return ret;
118 }
119
120 LTTNG_HIDDEN
121 bool lttng_event_rule_is_equal(const struct lttng_event_rule *a,
122 const struct lttng_event_rule *b)
123 {
124 bool is_equal = false;
125
126 if (!a || !b) {
127 goto end;
128 }
129
130 if (a->type != b->type) {
131 goto end;
132 }
133
134 if (a == b) {
135 is_equal = true;
136 goto end;
137 }
138
139 is_equal = a->equal ? a->equal(a, b) : true;
140 end:
141 return is_equal;
142 }
143
144 LTTNG_HIDDEN
145 ssize_t lttng_event_rule_create_from_payload(
146 struct lttng_payload_view *view,
147 struct lttng_event_rule **event_rule)
148 {
149 ssize_t ret, consumed = 0;
150 event_rule_create_from_payload_cb create_from_payload = NULL;
151 const struct lttng_event_rule_comm *event_rule_comm;
152 const struct lttng_payload_view event_rule_comm_view =
153 lttng_payload_view_from_view(
154 view, 0, sizeof(*event_rule_comm));
155
156 if (!view || !event_rule) {
157 ret = -1;
158 goto end;
159 }
160
161 if (!lttng_payload_view_is_valid(&event_rule_comm_view)) {
162 ret = -1;
163 goto end;
164 }
165
166 DBG("Deserializing event_rule from payload");
167 event_rule_comm = (const struct lttng_event_rule_comm *) event_rule_comm_view.buffer.data;
168 consumed += sizeof(*event_rule_comm);
169
170 switch ((enum lttng_event_rule_type) event_rule_comm->event_rule_type) {
171 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
172 create_from_payload =
173 lttng_event_rule_tracepoint_create_from_payload;
174 break;
175 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE:
176 create_from_payload = lttng_event_rule_kernel_probe_create_from_payload;
177 break;
178 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
179 /* TODO */
180 break;
181 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE:
182 create_from_payload = lttng_event_rule_userspace_probe_create_from_payload;
183 break;
184 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
185 create_from_payload =
186 lttng_event_rule_syscall_create_from_payload;
187 break;
188 default:
189 ERR("Attempted to create event rule of unknown type (%i)",
190 (int) event_rule_comm->event_rule_type);
191 ret = -1;
192 goto end;
193 }
194
195 assert(create_from_payload);
196
197 {
198 struct lttng_payload_view child_view =
199 lttng_payload_view_from_view(
200 view, consumed, -1);
201
202 ret = create_from_payload(&child_view, event_rule);
203 if (ret < 0) {
204 goto end;
205 }
206
207 consumed += ret;
208 }
209
210 if (!lttng_event_rule_validate(*event_rule)) {
211 ret = -1;
212 goto end;
213 }
214
215 ret = consumed;
216 end:
217 return ret;
218 }
219
220 LTTNG_HIDDEN
221 void lttng_event_rule_init(struct lttng_event_rule *event_rule,
222 enum lttng_event_rule_type type)
223 {
224 urcu_ref_init(&event_rule->ref);
225 event_rule->type = type;
226 }
227
228 LTTNG_HIDDEN
229 bool lttng_event_rule_get(struct lttng_event_rule *event_rule)
230 {
231 return urcu_ref_get_unless_zero(&event_rule->ref);
232 }
233
234 LTTNG_HIDDEN
235 void lttng_event_rule_put(struct lttng_event_rule *event_rule)
236 {
237 if (!event_rule) {
238 return;
239 }
240
241 assert(event_rule->ref.refcount);
242 urcu_ref_put(&event_rule->ref, lttng_event_rule_release);
243 }
244
245 LTTNG_HIDDEN
246 enum lttng_error_code lttng_event_rule_generate_filter_bytecode(
247 struct lttng_event_rule *rule,
248 const struct lttng_credentials *creds)
249 {
250 assert(rule->generate_filter_bytecode);
251 return rule->generate_filter_bytecode(rule, creds);
252 }
253
254 LTTNG_HIDDEN
255 const char *lttng_event_rule_get_filter(const struct lttng_event_rule *rule)
256 {
257 assert(rule->get_filter);
258 return rule->get_filter(rule);
259 }
260
261 LTTNG_HIDDEN
262 const struct lttng_bytecode *lttng_event_rule_get_filter_bytecode(
263 const struct lttng_event_rule *rule)
264 {
265 assert(rule->get_filter_bytecode);
266 return rule->get_filter_bytecode(rule);
267 }
268
269 LTTNG_HIDDEN
270 enum lttng_event_rule_generate_exclusions_status
271 lttng_event_rule_generate_exclusions(const struct lttng_event_rule *rule,
272 struct lttng_event_exclusion **exclusions)
273 {
274 assert(rule->generate_exclusions);
275 return rule->generate_exclusions(rule, exclusions);
276 }
277
278 LTTNG_HIDDEN
279 struct lttng_event *lttng_event_rule_generate_lttng_event(
280 const struct lttng_event_rule *rule)
281 {
282 assert(rule->generate_lttng_event);
283 return rule->generate_lttng_event(rule);
284 }
285
286 LTTNG_HIDDEN
287 bool lttng_event_rule_targets_agent_domain(const struct lttng_event_rule *rule)
288 {
289 bool targets_agent_domain = false;
290 enum lttng_domain_type type = lttng_event_rule_get_domain_type(rule);
291
292 switch (type) {
293 case LTTNG_DOMAIN_JUL:
294 case LTTNG_DOMAIN_LOG4J:
295 case LTTNG_DOMAIN_PYTHON:
296 targets_agent_domain = true;
297 break;
298 case LTTNG_DOMAIN_UST:
299 case LTTNG_DOMAIN_KERNEL:
300 targets_agent_domain = false;
301 break;
302 default:
303 abort();
304 };
305
306 return targets_agent_domain;
307 }
308
309 const char *lttng_event_rule_type_str(enum lttng_event_rule_type type)
310 {
311 switch (type) {
312 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
313 return "unknown";
314 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
315 return "tracepoint";
316 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
317 return "syscall";
318 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE:
319 return "probe";
320 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
321 return "function";
322 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE:
323 return "userspace-probe";
324 default:
325 abort();
326 }
327 }
328
329 LTTNG_HIDDEN
330 unsigned long lttng_event_rule_hash(const struct lttng_event_rule *rule)
331 {
332 assert(rule->hash);
333 return rule->hash(rule);
334 }
This page took 0.037628 seconds and 3 git commands to generate.