Add base support for event rule hit
[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/kprobe-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_KPROBE:
45 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
46 case LTTNG_EVENT_RULE_TYPE_UPROBE:
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_KPROBE:
176 create_from_payload = lttng_event_rule_kprobe_create_from_payload;
177 break;
178 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
179 /* TODO */
180 break;
181 case LTTNG_EVENT_RULE_TYPE_UPROBE:
182 create_from_payload = lttng_event_rule_uprobe_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_filter_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 struct lttng_event_exclusion *lttng_event_rule_generate_exclusions(
271 const struct lttng_event_rule *rule)
272 {
273 assert(rule->generate_exclusions);
274 return rule->generate_exclusions(rule);
275 }
276
277 LTTNG_HIDDEN
278 const char *lttng_event_rule_type_str(enum lttng_event_rule_type type)
279 {
280 switch (type) {
281 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
282 return "unknown";
283 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
284 return "tracepoint";
285 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
286 return "syscall";
287 case LTTNG_EVENT_RULE_TYPE_KPROBE:
288 return "probe";
289 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
290 return "function";
291 case LTTNG_EVENT_RULE_TYPE_UPROBE:
292 return "userspace-probe";
293 default:
294 abort();
295 }
296 }
297
298 LTTNG_HIDDEN
299 unsigned long lttng_event_rule_hash(const struct lttng_event_rule *rule)
300 {
301 assert(rule->hash);
302 return rule->hash(rule);
303 }
This page took 0.050804 seconds and 4 git commands to generate.