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