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