event-rule: introduce event rule tracepoint
[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 const struct lttng_event_rule_comm *event_rule_comm;
149 event_rule_create_from_payload_cb create_from_payload = NULL;
150
151 if (!view || !event_rule) {
152 ret = -1;
153 goto end;
154 }
155
156 DBG("Deserializing event_rule from payload.");
157 event_rule_comm = (const struct lttng_event_rule_comm *) view->buffer.data;
158 consumed += sizeof(*event_rule_comm);
159
160 switch ((enum lttng_event_rule_type) event_rule_comm->event_rule_type) {
161 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
162 create_from_payload =
163 lttng_event_rule_tracepoint_create_from_payload;
164 break;
165 case LTTNG_EVENT_RULE_TYPE_KPROBE:
166 create_from_payload = lttng_event_rule_kprobe_create_from_payload;
167 break;
168 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
169 /* TODO */
170 break;
171 case LTTNG_EVENT_RULE_TYPE_UPROBE:
172 create_from_payload = lttng_event_rule_uprobe_create_from_payload;
173 break;
174 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
175 create_from_payload =
176 lttng_event_rule_syscall_create_from_payload;
177 break;
178 default:
179 ERR("Attempted to create event rule of unknown type (%i)",
180 (int) event_rule_comm->event_rule_type);
181 ret = -1;
182 goto end;
183 }
184
185 assert(create_from_payload);
186
187 {
188 struct lttng_payload_view child_view =
189 lttng_payload_view_from_view(
190 view, consumed, -1);
191
192 ret = create_from_payload(&child_view, event_rule);
193 if (ret < 0) {
194 goto end;
195 }
196
197 consumed += ret;
198 }
199
200 if (!lttng_event_rule_validate(*event_rule)) {
201 ret = -1;
202 goto end;
203 }
204
205 ret = consumed;
206 end:
207 return ret;
208 }
209
210 LTTNG_HIDDEN
211 void lttng_event_rule_init(struct lttng_event_rule *event_rule,
212 enum lttng_event_rule_type type)
213 {
214 urcu_ref_init(&event_rule->ref);
215 event_rule->type = type;
216 }
217
218 LTTNG_HIDDEN
219 bool lttng_event_rule_get(struct lttng_event_rule *event_rule)
220 {
221 return urcu_ref_get_unless_zero(&event_rule->ref);
222 }
223
224 LTTNG_HIDDEN
225 void lttng_event_rule_put(struct lttng_event_rule *event_rule)
226 {
227 if (!event_rule) {
228 return;
229 }
230
231 assert(event_rule->ref.refcount);
232 urcu_ref_put(&event_rule->ref, lttng_event_rule_release);
233 }
234
235 LTTNG_HIDDEN
236 enum lttng_error_code lttng_event_rule_generate_filter_bytecode(
237 struct lttng_event_rule *rule, uid_t uid, gid_t gid)
238 {
239 assert(rule->generate_filter_bytecode);
240 return rule->generate_filter_bytecode(rule, uid, gid);
241 }
242
243 LTTNG_HIDDEN
244 const char *lttng_event_rule_get_filter(const struct lttng_event_rule *rule)
245 {
246 assert(rule->get_filter);
247 return rule->get_filter(rule);
248 }
249
250 LTTNG_HIDDEN
251 const struct lttng_filter_bytecode *lttng_event_rule_get_filter_bytecode(
252 const struct lttng_event_rule *rule)
253 {
254 assert(rule->get_filter_bytecode);
255 return rule->get_filter_bytecode(rule);
256 }
257
258 LTTNG_HIDDEN
259 struct lttng_event_exclusion *lttng_event_rule_generate_exclusions(
260 const struct lttng_event_rule *rule)
261 {
262 assert(rule->generate_exclusions);
263 return rule->generate_exclusions(rule);
264 }
265
266 LTTNG_HIDDEN
267 const char *lttng_event_rule_type_str(enum lttng_event_rule_type type)
268 {
269 switch (type) {
270 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
271 return "unknown";
272 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
273 return "tracepoint";
274 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
275 return "syscall";
276 case LTTNG_EVENT_RULE_TYPE_KPROBE:
277 return "probe";
278 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
279 return "function";
280 case LTTNG_EVENT_RULE_TYPE_UPROBE:
281 return "userspace-probe";
282 default:
283 abort();
284 }
285 }
This page took 0.053768 seconds and 5 git commands to generate.