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