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