Commit | Line | Data |
---|---|---|
077192fd JR |
1 | /* |
2 | * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: LGPL-2.1-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <assert.h> | |
58daac01 | 9 | #include <common/credentials.h> |
077192fd JR |
10 | #include <common/error.h> |
11 | #include <common/macros.h> | |
12 | #include <common/payload.h> | |
13 | #include <common/payload-view.h> | |
14 | #include <common/runas.h> | |
959e3c66 JR |
15 | #include <common/hashtable/hashtable.h> |
16 | #include <common/hashtable/utils.h> | |
077192fd JR |
17 | #include <ctype.h> |
18 | #include <lttng/constant.h> | |
19 | #include <lttng/event-rule/event-rule-internal.h> | |
20 | #include <lttng/event-rule/kprobe-internal.h> | |
21 | #include <lttng/kernel-probe.h> | |
22 | #include <lttng/kernel-probe-internal.h> | |
23 | #include <stdio.h> | |
24 | ||
25 | #define IS_KPROBE_EVENT_RULE(rule) \ | |
26 | (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KPROBE) | |
27 | ||
28 | #if (LTTNG_SYMBOL_NAME_LEN == 256) | |
29 | #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255" | |
30 | #endif | |
31 | ||
32 | static void lttng_event_rule_kprobe_destroy(struct lttng_event_rule *rule) | |
33 | { | |
34 | struct lttng_event_rule_kprobe *kprobe; | |
35 | ||
36 | kprobe = container_of(rule, struct lttng_event_rule_kprobe, parent); | |
37 | ||
38 | lttng_kernel_probe_location_destroy(kprobe->location); | |
39 | free(kprobe->name); | |
40 | free(kprobe); | |
41 | } | |
42 | ||
43 | static bool lttng_event_rule_kprobe_validate( | |
44 | const struct lttng_event_rule *rule) | |
45 | { | |
46 | bool valid = false; | |
47 | struct lttng_event_rule_kprobe *kprobe; | |
48 | ||
49 | if (!rule) { | |
50 | goto end; | |
51 | } | |
52 | ||
53 | kprobe = container_of(rule, struct lttng_event_rule_kprobe, parent); | |
54 | ||
55 | /* Required field. */ | |
56 | if (!kprobe->name) { | |
57 | ERR("Invalid name event rule: a name must be set."); | |
58 | goto end; | |
59 | } | |
60 | ||
61 | /* Required field. */ | |
62 | if(!kprobe->location) { | |
63 | ERR("Invalid name event rule: a location must be set."); | |
64 | goto end; | |
65 | } | |
66 | ||
67 | valid = true; | |
68 | end: | |
69 | return valid; | |
70 | } | |
71 | ||
72 | static int lttng_event_rule_kprobe_serialize( | |
73 | const struct lttng_event_rule *rule, | |
74 | struct lttng_payload *payload) | |
75 | { | |
76 | int ret; | |
77 | size_t name_len, header_offset, size_before_location; | |
78 | struct lttng_event_rule_kprobe *kprobe; | |
79 | struct lttng_event_rule_kprobe_comm kprobe_comm; | |
80 | struct lttng_event_rule_kprobe_comm *header; | |
81 | ||
82 | if (!rule || !IS_KPROBE_EVENT_RULE(rule)) { | |
83 | ret = -1; | |
84 | goto end; | |
85 | } | |
86 | ||
87 | header_offset = payload->buffer.size; | |
88 | ||
89 | DBG("Serializing kprobe event rule."); | |
90 | kprobe = container_of(rule, struct lttng_event_rule_kprobe, parent); | |
91 | ||
92 | name_len = strlen(kprobe->name) + 1; | |
93 | kprobe_comm.name_len = name_len; | |
94 | ||
95 | ret = lttng_dynamic_buffer_append( | |
96 | &payload->buffer, &kprobe_comm, sizeof(kprobe_comm)); | |
97 | if (ret) { | |
98 | goto end; | |
99 | } | |
100 | ||
101 | ret = lttng_dynamic_buffer_append(&payload->buffer, kprobe->name, name_len); | |
102 | if (ret) { | |
103 | goto end; | |
104 | } | |
105 | ||
106 | size_before_location = payload->buffer.size; | |
107 | ||
108 | ret = lttng_kernel_probe_location_serialize(kprobe->location, payload); | |
109 | if (ret < 0) { | |
110 | goto end; | |
111 | } | |
112 | ||
113 | /* Update the header regarding the probe size. */ | |
114 | header = (struct lttng_event_rule_kprobe_comm*) ( | |
115 | (char *) payload->buffer.data + header_offset); | |
116 | header->location_len = payload->buffer.size - size_before_location; | |
117 | ||
118 | ret = 0; | |
119 | ||
120 | end: | |
121 | return ret; | |
122 | } | |
123 | ||
124 | static bool lttng_event_rule_kprobe_is_equal(const struct lttng_event_rule *_a, | |
125 | const struct lttng_event_rule *_b) | |
126 | { | |
127 | bool is_equal = false; | |
128 | struct lttng_event_rule_kprobe *a, *b; | |
129 | ||
130 | a = container_of(_a, struct lttng_event_rule_kprobe, parent); | |
131 | b = container_of(_b, struct lttng_event_rule_kprobe, parent); | |
132 | ||
133 | /* Quick checks */ | |
134 | if (!!a->name != !!b->name) { | |
135 | goto end; | |
136 | } | |
137 | ||
138 | /* Long check */ | |
139 | assert(a->name); | |
140 | assert(b->name); | |
141 | if (strcmp(a->name, b->name)) { | |
142 | goto end; | |
143 | } | |
144 | ||
145 | is_equal = lttng_kernel_probe_location_is_equal( | |
146 | a->location, b->location); | |
147 | end: | |
148 | return is_equal; | |
149 | } | |
150 | ||
151 | static enum lttng_error_code lttng_event_rule_kprobe_generate_filter_bytecode( | |
58daac01 JR |
152 | struct lttng_event_rule *rule, |
153 | const struct lttng_credentials *creds) | |
077192fd JR |
154 | { |
155 | /* Nothing to do. */ | |
156 | return LTTNG_OK; | |
157 | } | |
158 | ||
159 | static const char *lttng_event_rule_kprobe_get_filter( | |
160 | const struct lttng_event_rule *rule) | |
161 | { | |
162 | /* Not supported. */ | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | static const struct lttng_filter_bytecode * | |
167 | lttng_event_rule_kprobe_get_filter_bytecode(const struct lttng_event_rule *rule) | |
168 | { | |
169 | /* Not supported. */ | |
170 | return NULL; | |
171 | } | |
172 | ||
173 | static struct lttng_event_exclusion * | |
174 | lttng_event_rule_kprobe_generate_exclusions(const struct lttng_event_rule *rule) | |
175 | { | |
176 | /* Not supported. */ | |
177 | return NULL; | |
178 | } | |
179 | ||
959e3c66 JR |
180 | static unsigned long |
181 | lttng_event_rule_kprobe_hash( | |
182 | const struct lttng_event_rule *rule) | |
183 | { | |
184 | unsigned long hash; | |
185 | struct lttng_event_rule_kprobe *krule = | |
186 | container_of(rule, typeof(*krule), parent); | |
187 | ||
188 | hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KPROBE, | |
189 | lttng_ht_seed); | |
190 | hash ^= hash_key_str(krule->name, lttng_ht_seed); | |
191 | hash ^= lttng_kernel_probe_location_hash(krule->location); | |
192 | ||
193 | return hash; | |
194 | } | |
195 | ||
077192fd JR |
196 | struct lttng_event_rule *lttng_event_rule_kprobe_create() |
197 | { | |
198 | struct lttng_event_rule *rule = NULL; | |
199 | struct lttng_event_rule_kprobe *krule; | |
200 | ||
201 | krule = zmalloc(sizeof(struct lttng_event_rule_kprobe)); | |
202 | if (!krule) { | |
203 | goto end; | |
204 | } | |
205 | ||
206 | rule = &krule->parent; | |
207 | lttng_event_rule_init(&krule->parent, LTTNG_EVENT_RULE_TYPE_KPROBE); | |
208 | krule->parent.validate = lttng_event_rule_kprobe_validate; | |
209 | krule->parent.serialize = lttng_event_rule_kprobe_serialize; | |
210 | krule->parent.equal = lttng_event_rule_kprobe_is_equal; | |
211 | krule->parent.destroy = lttng_event_rule_kprobe_destroy; | |
212 | krule->parent.generate_filter_bytecode = | |
213 | lttng_event_rule_kprobe_generate_filter_bytecode; | |
214 | krule->parent.get_filter = lttng_event_rule_kprobe_get_filter; | |
215 | krule->parent.get_filter_bytecode = | |
216 | lttng_event_rule_kprobe_get_filter_bytecode; | |
217 | krule->parent.generate_exclusions = | |
218 | lttng_event_rule_kprobe_generate_exclusions; | |
959e3c66 | 219 | krule->parent.hash = lttng_event_rule_kprobe_hash; |
077192fd JR |
220 | end: |
221 | return rule; | |
222 | } | |
223 | ||
224 | LTTNG_HIDDEN | |
225 | ssize_t lttng_event_rule_kprobe_create_from_payload( | |
226 | struct lttng_payload_view *view, | |
227 | struct lttng_event_rule **_event_rule) | |
228 | { | |
229 | ssize_t ret, offset = 0; | |
230 | enum lttng_event_rule_status status; | |
231 | const struct lttng_event_rule_kprobe_comm *kprobe_comm; | |
232 | const char *name; | |
233 | struct lttng_buffer_view current_buffer_view; | |
234 | struct lttng_event_rule *rule = NULL; | |
235 | struct lttng_event_rule_kprobe *kprobe = NULL; | |
236 | struct lttng_kernel_probe_location *location; | |
237 | ||
238 | if (!_event_rule) { | |
239 | ret = -1; | |
240 | goto end; | |
241 | } | |
242 | ||
3e6e0df2 JG |
243 | current_buffer_view = lttng_buffer_view_from_view( |
244 | &view->buffer, offset, sizeof(*kprobe_comm)); | |
245 | if (!lttng_buffer_view_is_valid(¤t_buffer_view)) { | |
077192fd JR |
246 | ERR("Failed to initialize from malformed event rule kprobe: buffer too short to contain header."); |
247 | ret = -1; | |
248 | goto end; | |
249 | } | |
250 | ||
077192fd | 251 | kprobe_comm = (typeof(kprobe_comm)) current_buffer_view.data; |
077192fd JR |
252 | |
253 | rule = lttng_event_rule_kprobe_create(); | |
254 | if (!rule) { | |
255 | ERR("Failed to create event rule kprobe."); | |
256 | ret = -1; | |
257 | goto end; | |
258 | } | |
259 | ||
260 | kprobe = container_of(rule, struct lttng_event_rule_kprobe, parent); | |
261 | ||
262 | /* Skip to payload */ | |
263 | offset += current_buffer_view.size; | |
264 | ||
265 | { | |
266 | /* Map the name. */ | |
267 | struct lttng_payload_view current_payload_view = | |
268 | lttng_payload_view_from_view(view, offset, | |
269 | kprobe_comm->name_len); | |
270 | ||
3e6e0df2 | 271 | if (!lttng_payload_view_is_valid(¤t_payload_view)) { |
077192fd JR |
272 | ret = -1; |
273 | goto end; | |
274 | } | |
275 | ||
3e6e0df2 | 276 | name = current_payload_view.buffer.data; |
077192fd JR |
277 | if (!lttng_buffer_view_contains_string( |
278 | ¤t_payload_view.buffer, name, | |
279 | kprobe_comm->name_len)) { | |
280 | ret = -1; | |
281 | goto end; | |
282 | } | |
283 | } | |
284 | ||
285 | /* Skip after the name. */ | |
286 | offset += kprobe_comm->name_len; | |
287 | ||
288 | /* Map the kernel probe location. */ | |
289 | { | |
290 | struct lttng_payload_view current_payload_view = | |
291 | lttng_payload_view_from_view(view, offset, | |
292 | kprobe_comm->location_len); | |
293 | ||
3e6e0df2 JG |
294 | if (!lttng_payload_view_is_valid(¤t_payload_view)) { |
295 | ret = -1; | |
296 | goto end; | |
297 | } | |
298 | ||
077192fd JR |
299 | ret = lttng_kernel_probe_location_create_from_payload( |
300 | ¤t_payload_view, &location); | |
301 | if (ret < 0) { | |
302 | ret = -1; | |
303 | goto end; | |
304 | } | |
305 | } | |
306 | ||
307 | if (ret != kprobe_comm->location_len) { | |
308 | ret = -1; | |
309 | goto end; | |
310 | } | |
311 | ||
312 | kprobe->location = location; | |
313 | ||
314 | /* Skip after the location */ | |
315 | offset += kprobe_comm->location_len; | |
316 | ||
317 | status = lttng_event_rule_kprobe_set_name(rule, name); | |
318 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { | |
319 | ERR("Failed to set event rule kprobe name."); | |
320 | ret = -1; | |
321 | goto end; | |
322 | } | |
323 | ||
324 | *_event_rule = rule; | |
325 | rule = NULL; | |
326 | ret = offset; | |
327 | end: | |
328 | lttng_event_rule_destroy(rule); | |
329 | return ret; | |
330 | } | |
331 | ||
332 | enum lttng_event_rule_status lttng_event_rule_kprobe_set_location( | |
333 | struct lttng_event_rule *rule, | |
334 | const struct lttng_kernel_probe_location *location) | |
335 | { | |
336 | struct lttng_kernel_probe_location *location_copy = NULL; | |
337 | struct lttng_event_rule_kprobe *kprobe; | |
338 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; | |
339 | ||
340 | if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !location) { | |
341 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
342 | goto end; | |
343 | } | |
344 | ||
345 | kprobe = container_of(rule, struct lttng_event_rule_kprobe, parent); | |
346 | location_copy = lttng_kernel_probe_location_copy(location); | |
347 | if (!location_copy) { | |
348 | status = LTTNG_EVENT_RULE_STATUS_ERROR; | |
349 | goto end; | |
350 | } | |
351 | ||
352 | if (kprobe->location) { | |
353 | lttng_kernel_probe_location_destroy(kprobe->location); | |
354 | } | |
355 | ||
356 | kprobe->location = location_copy; | |
357 | location_copy = NULL; | |
358 | end: | |
359 | lttng_kernel_probe_location_destroy(location_copy); | |
360 | return status; | |
361 | } | |
362 | ||
363 | enum lttng_event_rule_status lttng_event_rule_kprobe_get_location( | |
364 | const struct lttng_event_rule *rule, | |
365 | const struct lttng_kernel_probe_location **location) | |
366 | { | |
367 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; | |
368 | struct lttng_event_rule_kprobe *kprobe; | |
369 | ||
370 | if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !location) { | |
371 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
372 | goto end; | |
373 | } | |
374 | ||
375 | kprobe = container_of(rule, struct lttng_event_rule_kprobe, parent); | |
376 | *location = kprobe->location; | |
377 | ||
378 | if (!*location) { | |
379 | status = LTTNG_EVENT_RULE_STATUS_UNSET; | |
380 | goto end; | |
381 | } | |
382 | ||
383 | end: | |
384 | return status; | |
385 | } | |
386 | ||
387 | enum lttng_event_rule_status lttng_event_rule_kprobe_set_name( | |
388 | struct lttng_event_rule *rule, const char *name) | |
389 | { | |
390 | char *name_copy = NULL; | |
391 | struct lttng_event_rule_kprobe *kprobe; | |
392 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; | |
393 | ||
394 | if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name || | |
395 | strlen(name) == 0) { | |
396 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
397 | goto end; | |
398 | } | |
399 | ||
400 | kprobe = container_of(rule, struct lttng_event_rule_kprobe, parent); | |
401 | name_copy = strdup(name); | |
402 | if (!name_copy) { | |
403 | status = LTTNG_EVENT_RULE_STATUS_ERROR; | |
404 | goto end; | |
405 | } | |
406 | ||
407 | free(kprobe->name); | |
408 | ||
409 | kprobe->name = name_copy; | |
410 | name_copy = NULL; | |
411 | end: | |
412 | return status; | |
413 | } | |
414 | ||
415 | enum lttng_event_rule_status lttng_event_rule_kprobe_get_name( | |
416 | const struct lttng_event_rule *rule, const char **name) | |
417 | { | |
418 | struct lttng_event_rule_kprobe *kprobe; | |
419 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; | |
420 | ||
421 | if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name) { | |
422 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
423 | goto end; | |
424 | } | |
425 | ||
426 | kprobe = container_of(rule, struct lttng_event_rule_kprobe, parent); | |
427 | if (!kprobe->name) { | |
428 | status = LTTNG_EVENT_RULE_STATUS_UNSET; | |
429 | goto end; | |
430 | } | |
431 | ||
432 | *name = kprobe->name; | |
433 | end: | |
434 | return status; | |
435 | } |