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 | ||
c9e313bc SM |
8 | #include <common/credentials.hpp> |
9 | #include <common/error.hpp> | |
10 | #include <common/hashtable/hashtable.hpp> | |
11 | #include <common/hashtable/utils.hpp> | |
12 | #include <common/macros.hpp> | |
13 | #include <common/mi-lttng.hpp> | |
14 | #include <common/payload-view.hpp> | |
15 | #include <common/payload.hpp> | |
16 | #include <common/runas.hpp> | |
28ab034a | 17 | |
077192fd | 18 | #include <lttng/constant.h> |
c9e313bc | 19 | #include <lttng/event-rule/event-rule-internal.hpp> |
6a751b95 | 20 | #include <lttng/event-rule/event-rule.h> |
c9e313bc SM |
21 | #include <lttng/event-rule/kernel-kprobe-internal.hpp> |
22 | #include <lttng/kernel-probe-internal.hpp> | |
6a751b95 | 23 | #include <lttng/kernel-probe.h> |
28ab034a JG |
24 | |
25 | #include <ctype.h> | |
077192fd JR |
26 | #include <stdio.h> |
27 | ||
28 | #define IS_KPROBE_EVENT_RULE(rule) \ | |
85522de5 | 29 | (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE) |
077192fd JR |
30 | |
31 | #if (LTTNG_SYMBOL_NAME_LEN == 256) | |
32 | #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255" | |
33 | #endif | |
34 | ||
85522de5 | 35 | static void lttng_event_rule_kernel_kprobe_destroy(struct lttng_event_rule *rule) |
077192fd | 36 | { |
85522de5 | 37 | struct lttng_event_rule_kernel_kprobe *kprobe; |
077192fd | 38 | |
0114db0e | 39 | kprobe = lttng::utils::container_of(rule, <tng_event_rule_kernel_kprobe::parent); |
077192fd JR |
40 | |
41 | lttng_kernel_probe_location_destroy(kprobe->location); | |
42 | free(kprobe->name); | |
43 | free(kprobe); | |
44 | } | |
45 | ||
28ab034a | 46 | static bool lttng_event_rule_kernel_kprobe_validate(const struct lttng_event_rule *rule) |
077192fd JR |
47 | { |
48 | bool valid = false; | |
85522de5 | 49 | struct lttng_event_rule_kernel_kprobe *kprobe; |
077192fd JR |
50 | |
51 | if (!rule) { | |
52 | goto end; | |
53 | } | |
54 | ||
0114db0e | 55 | kprobe = lttng::utils::container_of(rule, <tng_event_rule_kernel_kprobe::parent); |
077192fd JR |
56 | |
57 | /* Required field. */ | |
58 | if (!kprobe->name) { | |
59 | ERR("Invalid name event rule: a name must be set."); | |
60 | goto end; | |
61 | } | |
62 | ||
63 | /* Required field. */ | |
28ab034a | 64 | if (!kprobe->location) { |
077192fd JR |
65 | ERR("Invalid name event rule: a location must be set."); |
66 | goto end; | |
67 | } | |
68 | ||
69 | valid = true; | |
70 | end: | |
71 | return valid; | |
72 | } | |
73 | ||
28ab034a JG |
74 | static int lttng_event_rule_kernel_kprobe_serialize(const struct lttng_event_rule *rule, |
75 | struct lttng_payload *payload) | |
077192fd JR |
76 | { |
77 | int ret; | |
78 | size_t name_len, header_offset, size_before_location; | |
85522de5 JR |
79 | struct lttng_event_rule_kernel_kprobe *kprobe; |
80 | struct lttng_event_rule_kernel_kprobe_comm kprobe_comm; | |
81 | struct lttng_event_rule_kernel_kprobe_comm *header; | |
077192fd JR |
82 | |
83 | if (!rule || !IS_KPROBE_EVENT_RULE(rule)) { | |
84 | ret = -1; | |
85 | goto end; | |
86 | } | |
87 | ||
88 | header_offset = payload->buffer.size; | |
89 | ||
90 | DBG("Serializing kprobe event rule."); | |
0114db0e | 91 | kprobe = lttng::utils::container_of(rule, <tng_event_rule_kernel_kprobe::parent); |
077192fd JR |
92 | |
93 | name_len = strlen(kprobe->name) + 1; | |
94 | kprobe_comm.name_len = name_len; | |
95 | ||
28ab034a | 96 | ret = lttng_dynamic_buffer_append(&payload->buffer, &kprobe_comm, sizeof(kprobe_comm)); |
077192fd JR |
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. */ | |
28ab034a JG |
114 | header = (struct lttng_event_rule_kernel_kprobe_comm *) ((char *) payload->buffer.data + |
115 | header_offset); | |
077192fd JR |
116 | header->location_len = payload->buffer.size - size_before_location; |
117 | ||
118 | ret = 0; | |
119 | ||
120 | end: | |
121 | return ret; | |
122 | } | |
123 | ||
85522de5 | 124 | static bool lttng_event_rule_kernel_kprobe_is_equal(const struct lttng_event_rule *_a, |
28ab034a | 125 | const struct lttng_event_rule *_b) |
077192fd JR |
126 | { |
127 | bool is_equal = false; | |
85522de5 | 128 | struct lttng_event_rule_kernel_kprobe *a, *b; |
077192fd | 129 | |
0114db0e JG |
130 | a = lttng::utils::container_of(_a, <tng_event_rule_kernel_kprobe::parent); |
131 | b = lttng::utils::container_of(_b, <tng_event_rule_kernel_kprobe::parent); | |
077192fd JR |
132 | |
133 | /* Quick checks */ | |
134 | if (!!a->name != !!b->name) { | |
135 | goto end; | |
136 | } | |
137 | ||
138 | /* Long check */ | |
a0377dfe FD |
139 | LTTNG_ASSERT(a->name); |
140 | LTTNG_ASSERT(b->name); | |
5c7248cd | 141 | if (strcmp(a->name, b->name) != 0) { |
077192fd JR |
142 | goto end; |
143 | } | |
144 | ||
28ab034a | 145 | is_equal = lttng_kernel_probe_location_is_equal(a->location, b->location); |
077192fd JR |
146 | end: |
147 | return is_equal; | |
148 | } | |
149 | ||
85522de5 | 150 | static enum lttng_error_code lttng_event_rule_kernel_kprobe_generate_filter_bytecode( |
28ab034a JG |
151 | struct lttng_event_rule *rule __attribute__((unused)), |
152 | const struct lttng_credentials *creds __attribute__((unused))) | |
077192fd JR |
153 | { |
154 | /* Nothing to do. */ | |
155 | return LTTNG_OK; | |
156 | } | |
157 | ||
28ab034a JG |
158 | static const char *lttng_event_rule_kernel_kprobe_get_filter(const struct lttng_event_rule *rule |
159 | __attribute__((unused))) | |
077192fd JR |
160 | { |
161 | /* Not supported. */ | |
cd9adb8b | 162 | return nullptr; |
077192fd JR |
163 | } |
164 | ||
2b00d462 | 165 | static const struct lttng_bytecode * |
28ab034a JG |
166 | lttng_event_rule_kernel_kprobe_get_filter_bytecode(const struct lttng_event_rule *rule |
167 | __attribute__((unused))) | |
077192fd JR |
168 | { |
169 | /* Not supported. */ | |
cd9adb8b | 170 | return nullptr; |
077192fd JR |
171 | } |
172 | ||
993578ff | 173 | static enum lttng_event_rule_generate_exclusions_status |
28ab034a JG |
174 | lttng_event_rule_kernel_kprobe_generate_exclusions(const struct lttng_event_rule *rule |
175 | __attribute__((unused)), | |
176 | struct lttng_event_exclusion **exclusions) | |
077192fd JR |
177 | { |
178 | /* Not supported. */ | |
cd9adb8b | 179 | *exclusions = nullptr; |
993578ff | 180 | return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE; |
077192fd JR |
181 | } |
182 | ||
28ab034a | 183 | static unsigned long lttng_event_rule_kernel_kprobe_hash(const struct lttng_event_rule *rule) |
959e3c66 JR |
184 | { |
185 | unsigned long hash; | |
85522de5 | 186 | struct lttng_event_rule_kernel_kprobe *krule = |
28ab034a | 187 | lttng::utils::container_of(rule, <tng_event_rule_kernel_kprobe::parent); |
959e3c66 | 188 | |
28ab034a | 189 | hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE, lttng_ht_seed); |
959e3c66 JR |
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 | ||
28ab034a JG |
196 | static int kernel_probe_set_location(struct lttng_event_rule_kernel_kprobe *kprobe, |
197 | const struct lttng_kernel_probe_location *location) | |
602a6d40 JR |
198 | { |
199 | int ret; | |
cd9adb8b | 200 | struct lttng_kernel_probe_location *location_copy = nullptr; |
602a6d40 JR |
201 | |
202 | if (!kprobe || !location || kprobe->location) { | |
203 | ret = -1; | |
204 | goto end; | |
205 | } | |
206 | ||
207 | location_copy = lttng_kernel_probe_location_copy(location); | |
208 | if (!location_copy) { | |
209 | ret = -1; | |
210 | goto end; | |
211 | } | |
212 | ||
213 | kprobe->location = location_copy; | |
cd9adb8b | 214 | location_copy = nullptr; |
602a6d40 JR |
215 | ret = 0; |
216 | end: | |
217 | lttng_kernel_probe_location_destroy(location_copy); | |
218 | return ret; | |
219 | } | |
220 | ||
28ab034a JG |
221 | static enum lttng_error_code |
222 | lttng_event_rule_kernel_kprobe_mi_serialize(const struct lttng_event_rule *rule, | |
223 | struct mi_writer *writer) | |
6a751b95 JR |
224 | { |
225 | int ret; | |
226 | enum lttng_error_code ret_code; | |
227 | enum lttng_event_rule_status status; | |
cd9adb8b JG |
228 | const char *event_name = nullptr; |
229 | const struct lttng_kernel_probe_location *location = nullptr; | |
6a751b95 | 230 | |
a0377dfe FD |
231 | LTTNG_ASSERT(rule); |
232 | LTTNG_ASSERT(writer); | |
233 | LTTNG_ASSERT(IS_KPROBE_EVENT_RULE(rule)); | |
6a751b95 | 234 | |
28ab034a | 235 | status = lttng_event_rule_kernel_kprobe_get_event_name(rule, &event_name); |
a0377dfe FD |
236 | LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK); |
237 | LTTNG_ASSERT(event_name); | |
6a751b95 JR |
238 | |
239 | status = lttng_event_rule_kernel_kprobe_get_location(rule, &location); | |
a0377dfe FD |
240 | LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK); |
241 | LTTNG_ASSERT(location); | |
6a751b95 JR |
242 | |
243 | /* Open event rule kernel kprobe element. */ | |
28ab034a | 244 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_rule_kernel_kprobe); |
6a751b95 JR |
245 | if (ret) { |
246 | goto mi_error; | |
247 | } | |
248 | ||
249 | /* Name. */ | |
28ab034a JG |
250 | ret = mi_lttng_writer_write_element_string( |
251 | writer, mi_lttng_element_event_rule_event_name, event_name); | |
6a751b95 JR |
252 | if (ret) { |
253 | goto mi_error; | |
254 | } | |
255 | ||
256 | /* Probe location. */ | |
257 | ret_code = lttng_kernel_probe_location_mi_serialize(location, writer); | |
258 | if (ret_code != LTTNG_OK) { | |
259 | goto end; | |
260 | } | |
261 | ||
262 | /* Close event rule kernel kprobe element. */ | |
263 | ret = mi_lttng_writer_close_element(writer); | |
264 | if (ret) { | |
265 | goto mi_error; | |
266 | } | |
267 | ||
268 | ret_code = LTTNG_OK; | |
269 | goto end; | |
270 | ||
271 | mi_error: | |
272 | ret_code = LTTNG_ERR_MI_IO_FAIL; | |
273 | end: | |
274 | return ret_code; | |
275 | } | |
276 | ||
28ab034a JG |
277 | struct lttng_event_rule * |
278 | lttng_event_rule_kernel_kprobe_create(const struct lttng_kernel_probe_location *location) | |
077192fd | 279 | { |
cd9adb8b | 280 | struct lttng_event_rule *rule = nullptr; |
85522de5 | 281 | struct lttng_event_rule_kernel_kprobe *krule; |
077192fd | 282 | |
64803277 | 283 | krule = zmalloc<lttng_event_rule_kernel_kprobe>(); |
077192fd JR |
284 | if (!krule) { |
285 | goto end; | |
286 | } | |
287 | ||
288 | rule = &krule->parent; | |
85522de5 JR |
289 | lttng_event_rule_init(&krule->parent, LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE); |
290 | krule->parent.validate = lttng_event_rule_kernel_kprobe_validate; | |
291 | krule->parent.serialize = lttng_event_rule_kernel_kprobe_serialize; | |
292 | krule->parent.equal = lttng_event_rule_kernel_kprobe_is_equal; | |
293 | krule->parent.destroy = lttng_event_rule_kernel_kprobe_destroy; | |
077192fd | 294 | krule->parent.generate_filter_bytecode = |
28ab034a | 295 | lttng_event_rule_kernel_kprobe_generate_filter_bytecode; |
85522de5 | 296 | krule->parent.get_filter = lttng_event_rule_kernel_kprobe_get_filter; |
28ab034a JG |
297 | krule->parent.get_filter_bytecode = lttng_event_rule_kernel_kprobe_get_filter_bytecode; |
298 | krule->parent.generate_exclusions = lttng_event_rule_kernel_kprobe_generate_exclusions; | |
85522de5 | 299 | krule->parent.hash = lttng_event_rule_kernel_kprobe_hash; |
6a751b95 | 300 | krule->parent.mi_serialize = lttng_event_rule_kernel_kprobe_mi_serialize; |
602a6d40 JR |
301 | |
302 | if (kernel_probe_set_location(krule, location)) { | |
303 | lttng_event_rule_destroy(rule); | |
cd9adb8b | 304 | rule = nullptr; |
602a6d40 JR |
305 | } |
306 | ||
077192fd JR |
307 | end: |
308 | return rule; | |
309 | } | |
310 | ||
28ab034a JG |
311 | ssize_t lttng_event_rule_kernel_kprobe_create_from_payload(struct lttng_payload_view *view, |
312 | struct lttng_event_rule **_event_rule) | |
077192fd JR |
313 | { |
314 | ssize_t ret, offset = 0; | |
315 | enum lttng_event_rule_status status; | |
85522de5 | 316 | const struct lttng_event_rule_kernel_kprobe_comm *kprobe_comm; |
077192fd JR |
317 | const char *name; |
318 | struct lttng_buffer_view current_buffer_view; | |
cd9adb8b JG |
319 | struct lttng_event_rule *rule = nullptr; |
320 | struct lttng_kernel_probe_location *location = nullptr; | |
077192fd JR |
321 | |
322 | if (!_event_rule) { | |
323 | ret = -1; | |
324 | goto end; | |
325 | } | |
326 | ||
28ab034a JG |
327 | current_buffer_view = |
328 | lttng_buffer_view_from_view(&view->buffer, offset, sizeof(*kprobe_comm)); | |
3e6e0df2 | 329 | if (!lttng_buffer_view_is_valid(¤t_buffer_view)) { |
077192fd JR |
330 | ERR("Failed to initialize from malformed event rule kprobe: buffer too short to contain header."); |
331 | ret = -1; | |
332 | goto end; | |
333 | } | |
334 | ||
077192fd | 335 | kprobe_comm = (typeof(kprobe_comm)) current_buffer_view.data; |
077192fd | 336 | |
077192fd JR |
337 | /* Skip to payload */ |
338 | offset += current_buffer_view.size; | |
339 | ||
340 | { | |
341 | /* Map the name. */ | |
342 | struct lttng_payload_view current_payload_view = | |
28ab034a | 343 | lttng_payload_view_from_view(view, offset, kprobe_comm->name_len); |
077192fd | 344 | |
3e6e0df2 | 345 | if (!lttng_payload_view_is_valid(¤t_payload_view)) { |
077192fd JR |
346 | ret = -1; |
347 | goto end; | |
348 | } | |
349 | ||
3e6e0df2 | 350 | name = current_payload_view.buffer.data; |
077192fd | 351 | if (!lttng_buffer_view_contains_string( |
28ab034a | 352 | ¤t_payload_view.buffer, name, kprobe_comm->name_len)) { |
077192fd JR |
353 | ret = -1; |
354 | goto end; | |
355 | } | |
356 | } | |
357 | ||
358 | /* Skip after the name. */ | |
359 | offset += kprobe_comm->name_len; | |
360 | ||
361 | /* Map the kernel probe location. */ | |
362 | { | |
363 | struct lttng_payload_view current_payload_view = | |
28ab034a | 364 | lttng_payload_view_from_view(view, offset, kprobe_comm->location_len); |
077192fd | 365 | |
3e6e0df2 JG |
366 | if (!lttng_payload_view_is_valid(¤t_payload_view)) { |
367 | ret = -1; | |
368 | goto end; | |
369 | } | |
370 | ||
28ab034a JG |
371 | ret = lttng_kernel_probe_location_create_from_payload(¤t_payload_view, |
372 | &location); | |
077192fd JR |
373 | if (ret < 0) { |
374 | ret = -1; | |
375 | goto end; | |
376 | } | |
377 | } | |
378 | ||
379 | if (ret != kprobe_comm->location_len) { | |
380 | ret = -1; | |
381 | goto end; | |
382 | } | |
383 | ||
077192fd JR |
384 | /* Skip after the location */ |
385 | offset += kprobe_comm->location_len; | |
386 | ||
85522de5 | 387 | rule = lttng_event_rule_kernel_kprobe_create(location); |
602a6d40 JR |
388 | if (!rule) { |
389 | ERR("Failed to create event rule kprobe."); | |
390 | ret = -1; | |
391 | goto end; | |
392 | } | |
393 | ||
85522de5 | 394 | status = lttng_event_rule_kernel_kprobe_set_event_name(rule, name); |
077192fd JR |
395 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
396 | ERR("Failed to set event rule kprobe name."); | |
397 | ret = -1; | |
398 | goto end; | |
399 | } | |
400 | ||
401 | *_event_rule = rule; | |
cd9adb8b | 402 | rule = nullptr; |
077192fd JR |
403 | ret = offset; |
404 | end: | |
602a6d40 | 405 | lttng_kernel_probe_location_destroy(location); |
077192fd JR |
406 | lttng_event_rule_destroy(rule); |
407 | return ret; | |
408 | } | |
409 | ||
28ab034a JG |
410 | enum lttng_event_rule_status |
411 | lttng_event_rule_kernel_kprobe_get_location(const struct lttng_event_rule *rule, | |
412 | const struct lttng_kernel_probe_location **location) | |
077192fd JR |
413 | { |
414 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; | |
85522de5 | 415 | struct lttng_event_rule_kernel_kprobe *kprobe; |
077192fd JR |
416 | |
417 | if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !location) { | |
418 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
419 | goto end; | |
420 | } | |
421 | ||
0114db0e | 422 | kprobe = lttng::utils::container_of(rule, <tng_event_rule_kernel_kprobe::parent); |
077192fd JR |
423 | *location = kprobe->location; |
424 | ||
425 | if (!*location) { | |
426 | status = LTTNG_EVENT_RULE_STATUS_UNSET; | |
427 | goto end; | |
428 | } | |
429 | ||
430 | end: | |
431 | return status; | |
432 | } | |
433 | ||
28ab034a JG |
434 | enum lttng_event_rule_status |
435 | lttng_event_rule_kernel_kprobe_set_event_name(struct lttng_event_rule *rule, const char *name) | |
077192fd | 436 | { |
cd9adb8b | 437 | char *name_copy = nullptr; |
85522de5 | 438 | struct lttng_event_rule_kernel_kprobe *kprobe; |
077192fd JR |
439 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; |
440 | ||
28ab034a | 441 | if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name || strlen(name) == 0) { |
077192fd JR |
442 | status = LTTNG_EVENT_RULE_STATUS_INVALID; |
443 | goto end; | |
444 | } | |
445 | ||
0114db0e | 446 | kprobe = lttng::utils::container_of(rule, <tng_event_rule_kernel_kprobe::parent); |
077192fd JR |
447 | name_copy = strdup(name); |
448 | if (!name_copy) { | |
449 | status = LTTNG_EVENT_RULE_STATUS_ERROR; | |
450 | goto end; | |
451 | } | |
452 | ||
453 | free(kprobe->name); | |
454 | ||
455 | kprobe->name = name_copy; | |
cd9adb8b | 456 | name_copy = nullptr; |
077192fd JR |
457 | end: |
458 | return status; | |
459 | } | |
460 | ||
28ab034a JG |
461 | enum lttng_event_rule_status |
462 | lttng_event_rule_kernel_kprobe_get_event_name(const struct lttng_event_rule *rule, | |
463 | const char **name) | |
077192fd | 464 | { |
85522de5 | 465 | struct lttng_event_rule_kernel_kprobe *kprobe; |
077192fd JR |
466 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; |
467 | ||
468 | if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name) { | |
469 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
470 | goto end; | |
471 | } | |
472 | ||
0114db0e | 473 | kprobe = lttng::utils::container_of(rule, <tng_event_rule_kernel_kprobe::parent); |
077192fd JR |
474 | if (!kprobe->name) { |
475 | status = LTTNG_EVENT_RULE_STATUS_UNSET; | |
476 | goto end; | |
477 | } | |
478 | ||
479 | *name = kprobe->name; | |
480 | end: | |
481 | return status; | |
482 | } |