sessiond: kernel triggers: add infrastructure to create event notifiers
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include <lttng/event.h>
15 #include <lttng/lttng-error.h>
16 #include <lttng/kernel-probe.h>
17 #include <lttng/userspace-probe.h>
18 #include <lttng/userspace-probe-internal.h>
19 #include <lttng/event-rule/event-rule.h>
20 #include <lttng/event-rule/event-rule-internal.h>
21 #include <lttng/event-rule/kprobe.h>
22 #include <lttng/event-rule/kprobe-internal.h>
23 #include <lttng/event-rule/syscall.h>
24 #include <lttng/event-rule/syscall-internal.h>
25 #include <lttng/event-rule/tracepoint.h>
26 #include <lttng/event-rule/tracepoint-internal.h>
27 #include <lttng/event-rule/uprobe-internal.h>
28 #include <common/common.h>
29 #include <common/defaults.h>
30 #include <common/trace-chunk.h>
31 #include <common/macros.h>
32
33 #include "consumer.h"
34 #include "trace-kernel.h"
35 #include "lttng-sessiond.h"
36 #include "notification-thread-commands.h"
37
38 /*
39 * Find the channel name for the given kernel session.
40 */
41 struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
42 const char *name, struct ltt_kernel_session *session)
43 {
44 struct ltt_kernel_channel *chan;
45
46 assert(session);
47 assert(name);
48
49 /*
50 * If we receive an empty string for channel name, it means the
51 * default channel name is requested.
52 */
53 if (name[0] == '\0')
54 name = DEFAULT_CHANNEL_NAME;
55
56 DBG("Trying to find channel %s", name);
57
58 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
59 if (strcmp(name, chan->channel->name) == 0) {
60 DBG("Found channel by name %s", name);
61 return chan;
62 }
63 }
64
65 return NULL;
66 }
67
68 /*
69 * Find the event for the given channel.
70 */
71 struct ltt_kernel_event *trace_kernel_find_event(
72 char *name, struct ltt_kernel_channel *channel,
73 enum lttng_event_type type,
74 struct lttng_filter_bytecode *filter)
75 {
76 struct ltt_kernel_event *ev;
77 int found = 0;
78
79 assert(name);
80 assert(channel);
81
82 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
83 if (type != LTTNG_EVENT_ALL && ev->type != type) {
84 continue;
85 }
86 if (strcmp(name, ev->event->name)) {
87 continue;
88 }
89 if ((ev->filter && !filter) || (!ev->filter && filter)) {
90 continue;
91 }
92 if (ev->filter && filter) {
93 if (ev->filter->len != filter->len ||
94 memcmp(ev->filter->data, filter->data,
95 filter->len) != 0) {
96 continue;
97 }
98 }
99 found = 1;
100 break;
101 }
102 if (found) {
103 DBG("Found event %s for channel %s", name,
104 channel->channel->name);
105 return ev;
106 } else {
107 return NULL;
108 }
109 }
110
111 /*
112 * Find the event name for the given channel.
113 */
114 struct ltt_kernel_event *trace_kernel_get_event_by_name(
115 char *name, struct ltt_kernel_channel *channel,
116 enum lttng_event_type type)
117 {
118 struct ltt_kernel_event *ev;
119 int found = 0;
120
121 assert(name);
122 assert(channel);
123
124 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
125 if (type != LTTNG_EVENT_ALL && ev->type != type) {
126 continue;
127 }
128 if (strcmp(name, ev->event->name)) {
129 continue;
130 }
131 found = 1;
132 break;
133 }
134 if (found) {
135 DBG("Found event %s for channel %s", name,
136 channel->channel->name);
137 return ev;
138 } else {
139 return NULL;
140 }
141 }
142
143 /*
144 * Allocate and initialize a kernel session data structure.
145 *
146 * Return pointer to structure or NULL.
147 */
148 struct ltt_kernel_session *trace_kernel_create_session(void)
149 {
150 struct ltt_kernel_session *lks = NULL;
151
152 /* Allocate a new ltt kernel session */
153 lks = zmalloc(sizeof(struct ltt_kernel_session));
154 if (lks == NULL) {
155 PERROR("create kernel session zmalloc");
156 goto alloc_error;
157 }
158
159 /* Init data structure */
160 lks->fd = -1;
161 lks->metadata_stream_fd = -1;
162 lks->channel_count = 0;
163 lks->stream_count_global = 0;
164 lks->metadata = NULL;
165 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
166
167 lks->tracker_pid = process_attr_tracker_create();
168 if (!lks->tracker_pid) {
169 goto error;
170 }
171 lks->tracker_vpid = process_attr_tracker_create();
172 if (!lks->tracker_vpid) {
173 goto error;
174 }
175 lks->tracker_uid = process_attr_tracker_create();
176 if (!lks->tracker_uid) {
177 goto error;
178 }
179 lks->tracker_vuid = process_attr_tracker_create();
180 if (!lks->tracker_vuid) {
181 goto error;
182 }
183 lks->tracker_gid = process_attr_tracker_create();
184 if (!lks->tracker_gid) {
185 goto error;
186 }
187 lks->tracker_vgid = process_attr_tracker_create();
188 if (!lks->tracker_vgid) {
189 goto error;
190 }
191 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
192 if (lks->consumer == NULL) {
193 goto error;
194 }
195
196 return lks;
197
198 error:
199 process_attr_tracker_destroy(lks->tracker_pid);
200 process_attr_tracker_destroy(lks->tracker_vpid);
201 process_attr_tracker_destroy(lks->tracker_uid);
202 process_attr_tracker_destroy(lks->tracker_vuid);
203 process_attr_tracker_destroy(lks->tracker_gid);
204 process_attr_tracker_destroy(lks->tracker_vgid);
205 free(lks);
206
207 alloc_error:
208 return NULL;
209 }
210
211 /*
212 * Allocate and initialize a kernel channel data structure.
213 *
214 * Return pointer to structure or NULL.
215 */
216 struct ltt_kernel_channel *trace_kernel_create_channel(
217 struct lttng_channel *chan)
218 {
219 struct ltt_kernel_channel *lkc;
220 struct lttng_channel_extended *extended = NULL;
221
222 assert(chan);
223
224 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
225 if (lkc == NULL) {
226 PERROR("ltt_kernel_channel zmalloc");
227 goto error;
228 }
229
230 lkc->channel = zmalloc(sizeof(struct lttng_channel));
231 if (lkc->channel == NULL) {
232 PERROR("lttng_channel zmalloc");
233 goto error;
234 }
235
236 extended = zmalloc(sizeof(struct lttng_channel_extended));
237 if (!extended) {
238 PERROR("lttng_channel_channel zmalloc");
239 goto error;
240 }
241 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
242 memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended));
243 lkc->channel->attr.extended.ptr = extended;
244 extended = NULL;
245
246 /*
247 * If we receive an empty string for channel name, it means the
248 * default channel name is requested.
249 */
250 if (chan->name[0] == '\0') {
251 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
252 sizeof(lkc->channel->name));
253 }
254 lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
255
256 lkc->fd = -1;
257 lkc->stream_count = 0;
258 lkc->event_count = 0;
259 lkc->enabled = 1;
260 lkc->published_to_notification_thread = false;
261 /* Init linked list */
262 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
263 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
264 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
265
266 return lkc;
267
268 error:
269 if (lkc) {
270 free(lkc->channel);
271 }
272 free(extended);
273 free(lkc);
274 return NULL;
275 }
276
277 /*
278 * Allocate and init a kernel context object.
279 *
280 * Return the allocated object or NULL on error.
281 */
282 struct ltt_kernel_context *trace_kernel_create_context(
283 struct lttng_kernel_context *ctx)
284 {
285 struct ltt_kernel_context *kctx;
286
287 kctx = zmalloc(sizeof(*kctx));
288 if (!kctx) {
289 PERROR("zmalloc kernel context");
290 goto error;
291 }
292
293 if (ctx) {
294 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
295 }
296 error:
297 return kctx;
298 }
299
300 /*
301 * Allocate and init a kernel context object from an existing kernel context
302 * object.
303 *
304 * Return the allocated object or NULL on error.
305 */
306 struct ltt_kernel_context *trace_kernel_copy_context(
307 struct ltt_kernel_context *kctx)
308 {
309 struct ltt_kernel_context *kctx_copy;
310
311 assert(kctx);
312 kctx_copy = zmalloc(sizeof(*kctx_copy));
313 if (!kctx_copy) {
314 PERROR("zmalloc ltt_kernel_context");
315 goto error;
316 }
317
318 memcpy(kctx_copy, kctx, sizeof(*kctx_copy));
319 memset(&kctx_copy->list, 0, sizeof(kctx_copy->list));
320
321 error:
322 return kctx_copy;
323 }
324
325 /*
326 * Allocate and initialize a kernel event. Set name and event type.
327 * We own filter_expression, and filter.
328 *
329 * Return pointer to structure or NULL.
330 */
331 enum lttng_error_code trace_kernel_create_event(
332 struct lttng_event *ev, char *filter_expression,
333 struct lttng_filter_bytecode *filter,
334 struct ltt_kernel_event **kernel_event)
335 {
336 enum lttng_error_code ret;
337 struct lttng_kernel_event *attr;
338 struct ltt_kernel_event *local_kernel_event;
339 struct lttng_userspace_probe_location *userspace_probe_location = NULL;
340
341 assert(ev);
342
343 local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event));
344 attr = zmalloc(sizeof(struct lttng_kernel_event));
345 if (local_kernel_event == NULL || attr == NULL) {
346 PERROR("kernel event zmalloc");
347 ret = LTTNG_ERR_NOMEM;
348 goto error;
349 }
350
351 switch (ev->type) {
352 case LTTNG_EVENT_PROBE:
353 attr->instrumentation = LTTNG_KERNEL_KPROBE;
354 attr->u.kprobe.addr = ev->attr.probe.addr;
355 attr->u.kprobe.offset = ev->attr.probe.offset;
356 strncpy(attr->u.kprobe.symbol_name,
357 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
358 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
359 break;
360 case LTTNG_EVENT_USERSPACE_PROBE:
361 {
362 const struct lttng_userspace_probe_location* location = NULL;
363 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
364
365 location = lttng_event_get_userspace_probe_location(ev);
366 if (!location) {
367 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
368 goto error;
369 }
370
371 /*
372 * From this point on, the specific term 'uprobe' is used
373 * instead of the generic 'userspace probe' because it's the
374 * technology used at the moment for this instrumentation.
375 * LTTng currently implements userspace probes using uprobes.
376 * In the interactions with the kernel tracer, we use the
377 * uprobe term.
378 */
379 attr->instrumentation = LTTNG_KERNEL_UPROBE;
380
381 lookup = lttng_userspace_probe_location_get_lookup_method(
382 location);
383 if (!lookup) {
384 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
385 goto error;
386 }
387
388 /*
389 * From the kernel tracer's perspective, all userspace probe
390 * event types are all the same: a file and an offset.
391 */
392 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
393 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
394 /* Get the file descriptor on the target binary. */
395 attr->u.uprobe.fd =
396 lttng_userspace_probe_location_function_get_binary_fd(location);
397
398 /*
399 * Save a reference to the probe location used during
400 * the listing of events.
401 */
402 userspace_probe_location =
403 lttng_userspace_probe_location_copy(location);
404 break;
405 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
406 /* Get the file descriptor on the target binary. */
407 attr->u.uprobe.fd =
408 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
409
410 /*
411 * Save a reference to the probe location used during the listing of
412 * events.
413 */
414 userspace_probe_location =
415 lttng_userspace_probe_location_copy(location);
416 break;
417 default:
418 DBG("Unsupported lookup method type");
419 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
420 goto error;
421 }
422 break;
423 }
424 case LTTNG_EVENT_FUNCTION:
425 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
426 attr->u.kretprobe.addr = ev->attr.probe.addr;
427 attr->u.kretprobe.offset = ev->attr.probe.offset;
428 strncpy(attr->u.kretprobe.symbol_name,
429 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
430 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
431 break;
432 case LTTNG_EVENT_FUNCTION_ENTRY:
433 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
434 strncpy(attr->u.ftrace.symbol_name,
435 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
436 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
437 break;
438 case LTTNG_EVENT_TRACEPOINT:
439 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
440 break;
441 case LTTNG_EVENT_SYSCALL:
442 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
443 attr->u.syscall.abi = LTTNG_KERNEL_SYSCALL_ABI_ALL;
444 attr->u.syscall.entryexit = LTTNG_KERNEL_SYSCALL_ENTRYEXIT;
445 attr->u.syscall.match = LTTNG_KERNEL_SYSCALL_MATCH_NAME;
446 break;
447 case LTTNG_EVENT_ALL:
448 attr->instrumentation = LTTNG_KERNEL_ALL;
449 break;
450 default:
451 ERR("Unknown kernel instrumentation type (%d)", ev->type);
452 ret = LTTNG_ERR_INVALID;
453 goto error;
454 }
455
456 /* Copy event name */
457 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
458 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
459
460 /* Setting up a kernel event */
461 local_kernel_event->fd = -1;
462 local_kernel_event->event = attr;
463 local_kernel_event->enabled = 1;
464 local_kernel_event->filter_expression = filter_expression;
465 local_kernel_event->filter = filter;
466 local_kernel_event->userspace_probe_location = userspace_probe_location;
467
468 *kernel_event = local_kernel_event;
469
470 return LTTNG_OK;
471
472 error:
473 free(filter_expression);
474 free(filter);
475 free(local_kernel_event);
476 free(attr);
477 return ret;
478 }
479
480 /*
481 * Allocate and initialize a kernel token event rule.
482 *
483 * Return pointer to structure or NULL.
484 */
485 enum lttng_error_code trace_kernel_create_event_notifier_rule(
486 struct lttng_trigger *trigger,
487 uint64_t token,
488 struct ltt_kernel_event_notifier_rule **event_notifier_rule)
489 {
490 enum lttng_error_code ret = LTTNG_OK;
491 enum lttng_condition_type condition_type;
492 enum lttng_event_rule_type event_rule_type;
493 enum lttng_condition_status condition_status;
494 struct ltt_kernel_event_notifier_rule *local_kernel_token_event_rule;
495 const struct lttng_condition *condition = NULL;
496 const struct lttng_event_rule *event_rule = NULL;
497
498 assert(event_notifier_rule);
499
500 condition = lttng_trigger_get_condition(trigger);
501 assert(condition);
502
503 condition_type = lttng_condition_get_type(condition);
504 assert(condition_type == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT);
505
506 condition_status = lttng_condition_event_rule_get_rule(
507 condition, &event_rule);
508 assert(condition_status == LTTNG_CONDITION_STATUS_OK);
509 assert(event_rule);
510
511 event_rule_type = lttng_event_rule_get_type(event_rule);
512 assert(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN);
513
514 local_kernel_token_event_rule =
515 zmalloc(sizeof(struct ltt_kernel_event_notifier_rule));
516 if (local_kernel_token_event_rule == NULL) {
517 PERROR("Failed to allocate ltt_kernel_token_event_rule structure");
518 ret = LTTNG_ERR_NOMEM;
519 goto error;
520 }
521
522 local_kernel_token_event_rule->fd = -1;
523 local_kernel_token_event_rule->enabled = 1;
524 local_kernel_token_event_rule->token = token;
525
526 /* Get the reference of the event rule. */
527 lttng_trigger_get(trigger);
528
529 local_kernel_token_event_rule->trigger = trigger;
530 /* The event rule still owns the filter and bytecode. */
531 local_kernel_token_event_rule->filter =
532 lttng_event_rule_get_filter_bytecode(event_rule);
533
534 DBG3("Created kernel event notifier rule: token = %" PRIu64,
535 local_kernel_token_event_rule->token);
536 error:
537 *event_notifier_rule = local_kernel_token_event_rule;
538 return ret;
539 }
540
541 /*
542 * Initialize a kernel trigger from an event rule.
543 */
544 enum lttng_error_code trace_kernel_init_event_notifier_from_event_rule(
545 const struct lttng_event_rule *rule,
546 struct lttng_kernel_event_notifier *kernel_event_notifier)
547 {
548 enum lttng_error_code ret;
549 const char *name;
550
551 switch (lttng_event_rule_get_type(rule)) {
552 case LTTNG_EVENT_RULE_TYPE_KPROBE:
553 {
554 uint64_t address = 0, offset = 0;
555 const char *symbol_name = NULL;
556 const struct lttng_kernel_probe_location *location = NULL;
557 enum lttng_kernel_probe_location_status k_status;
558 enum lttng_event_rule_status status;
559
560 status = lttng_event_rule_kprobe_get_location(rule, &location);
561 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
562 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
563 goto error;
564 }
565
566 switch (lttng_kernel_probe_location_get_type(location)) {
567 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS:
568 {
569 k_status = lttng_kernel_probe_location_address_get_address(
570 location, &address);
571 assert(k_status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK);
572 break;
573 }
574 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET:
575 {
576 k_status = lttng_kernel_probe_location_symbol_get_offset(
577 location, &offset);
578 assert(k_status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK);
579 symbol_name = lttng_kernel_probe_location_symbol_get_name(
580 location);
581 break;
582 }
583 default:
584 abort();
585 }
586
587 kernel_event_notifier->event.instrumentation = LTTNG_KERNEL_KPROBE;
588 kernel_event_notifier->event.u.kprobe.addr = address;
589 kernel_event_notifier->event.u.kprobe.offset = offset;
590 if (symbol_name) {
591 const int copy_ret = lttng_strncpy(
592 kernel_event_notifier->event.u.kprobe.symbol_name,
593 symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
594
595 if (copy_ret) {
596 ret = LTTNG_ERR_INVALID;
597 goto error;
598 }
599 }
600
601 kernel_event_notifier->event.u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
602 status = lttng_event_rule_kprobe_get_name(rule, &name);
603 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
604 ret = LTTNG_OK;
605 break;
606 }
607 case LTTNG_EVENT_RULE_TYPE_UPROBE:
608 {
609 const struct lttng_userspace_probe_location* location = NULL;
610 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
611 enum lttng_event_rule_status status;
612
613 status = lttng_event_rule_uprobe_get_location(rule, &location);
614 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
615 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
616 goto error;
617 }
618
619 kernel_event_notifier->event.instrumentation = LTTNG_KERNEL_UPROBE;
620
621 lookup = lttng_userspace_probe_location_get_lookup_method(
622 location);
623 if (!lookup) {
624 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
625 goto error;
626 }
627
628 /*
629 * From the kernel tracer's perspective, all userspace probe
630 * event types are all the same: a file and an offset.
631 */
632 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
633 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
634 /* Get the file descriptor on the target binary. */
635 kernel_event_notifier->event.u.uprobe.fd =
636 lttng_userspace_probe_location_function_get_binary_fd(location);
637
638 break;
639 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
640 /* Get the file descriptor on the target binary. */
641 kernel_event_notifier->event.u.uprobe.fd =
642 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
643 break;
644 default:
645 abort();
646 }
647
648 status = lttng_event_rule_uprobe_get_name(rule, &name);
649 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
650 ret = LTTNG_OK;
651 break;
652 }
653 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
654 {
655 const enum lttng_domain_type domain =
656 lttng_event_rule_get_domain_type(rule);
657 const enum lttng_event_rule_status status =
658 lttng_event_rule_tracepoint_get_pattern(
659 rule, &name);
660
661 assert(domain == LTTNG_DOMAIN_KERNEL);
662 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
663 kernel_event_notifier->event.instrumentation =
664 LTTNG_KERNEL_TRACEPOINT;
665
666 ret = LTTNG_OK;
667 break;
668 }
669 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
670 {
671 const enum lttng_event_rule_status status =
672 lttng_event_rule_syscall_get_pattern(
673 rule, &name);
674
675 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
676
677 kernel_event_notifier->event.instrumentation =
678 LTTNG_KERNEL_SYSCALL;
679 kernel_event_notifier->event.u.syscall.abi =
680 LTTNG_KERNEL_SYSCALL_ABI_ALL;
681 kernel_event_notifier->event.u.syscall.entryexit =
682 LTTNG_KERNEL_SYSCALL_ENTRY;
683 kernel_event_notifier->event.u.syscall.match =
684 LTTNG_KERNEL_SYSCALL_MATCH_NAME;
685 ret = LTTNG_OK;
686 break;
687 }
688 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
689 default:
690 abort();
691 break;
692 }
693
694 ret = lttng_strncpy(kernel_event_notifier->event.name, name,
695 LTTNG_KERNEL_SYM_NAME_LEN);
696 if (ret) {
697 ret = LTTNG_ERR_INVALID;
698 goto error;
699 }
700
701 error:
702 return ret;
703 }
704 /*
705 * Allocate and initialize a kernel metadata.
706 *
707 * Return pointer to structure or NULL.
708 */
709 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
710 {
711 int ret;
712 struct ltt_kernel_metadata *lkm;
713 struct lttng_channel *chan;
714
715 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
716 chan = zmalloc(sizeof(struct lttng_channel));
717 if (lkm == NULL || chan == NULL) {
718 PERROR("kernel metadata zmalloc");
719 goto error;
720 }
721
722 ret = lttng_strncpy(
723 chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name));
724 if (ret) {
725 ERR("Failed to initialize metadata channel name to `%s`",
726 DEFAULT_METADATA_NAME);
727 goto error;
728 }
729
730 /* Set default attributes */
731 chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE;
732 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
733 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
734 chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
735 chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;;
736
737
738 /*
739 * The metadata channel of kernel sessions must use the "mmap"
740 * back-end since the consumer daemon accumulates complete
741 * metadata units before sending them to the relay daemon in
742 * live mode. The consumer daemon also needs to extract the contents
743 * of the metadata cache when computing a rotation position.
744 *
745 * In both cases, it is not possible to rely on the splice
746 * back-end as the consumer daemon may need to accumulate more
747 * content than can be backed by the ring buffer's underlying
748 * pages.
749 */
750 chan->attr.output = LTTNG_EVENT_MMAP;
751 chan->attr.tracefile_size = 0;
752 chan->attr.tracefile_count = 0;
753 chan->attr.live_timer_interval = 0;
754
755 /* Init metadata */
756 lkm->fd = -1;
757 lkm->conf = chan;
758
759 return lkm;
760
761 error:
762 free(lkm);
763 free(chan);
764 return NULL;
765 }
766
767 /*
768 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
769 * default.
770 *
771 * Return pointer to structure or NULL.
772 */
773 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
774 unsigned int count)
775 {
776 int ret;
777 struct ltt_kernel_stream *lks;
778
779 assert(name);
780
781 lks = zmalloc(sizeof(struct ltt_kernel_stream));
782 if (lks == NULL) {
783 PERROR("kernel stream zmalloc");
784 goto error;
785 }
786
787 /* Set name */
788 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
789 if (ret < 0) {
790 PERROR("snprintf stream name");
791 goto error;
792 }
793 lks->name[sizeof(lks->name) - 1] = '\0';
794
795 /* Init stream */
796 lks->fd = -1;
797 lks->state = 0;
798 lks->cpu = count;
799
800 return lks;
801
802 error:
803 return NULL;
804 }
805
806 /*
807 * Cleanup kernel stream structure.
808 */
809 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
810 {
811 assert(stream);
812
813 DBG("[trace] Closing stream fd %d", stream->fd);
814 /* Close kernel fd */
815 if (stream->fd >= 0) {
816 int ret;
817
818 ret = close(stream->fd);
819 if (ret) {
820 PERROR("close");
821 }
822 }
823 /* Remove from stream list */
824 cds_list_del(&stream->list);
825
826 free(stream);
827 }
828
829 /*
830 * Cleanup kernel event structure.
831 */
832 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
833 {
834 assert(event);
835
836 if (event->fd >= 0) {
837 int ret;
838
839 DBG("[trace] Closing event fd %d", event->fd);
840 /* Close kernel fd */
841 ret = close(event->fd);
842 if (ret) {
843 PERROR("close");
844 }
845 } else {
846 DBG("[trace] Tearing down event (no associated file descriptor)");
847 }
848
849 /* Remove from event list */
850 cds_list_del(&event->list);
851
852 free(event->filter_expression);
853 free(event->filter);
854
855 free(event->event);
856 free(event);
857 }
858
859 /*
860 * Cleanup kernel event structure.
861 */
862 static void free_token_event_rule_rcu(struct rcu_head *rcu_node)
863 {
864 struct ltt_kernel_event_notifier_rule *rule = caa_container_of(rcu_node,
865 struct ltt_kernel_event_notifier_rule, rcu_node);
866
867 free(rule);
868 }
869
870 void trace_kernel_destroy_event_notifier_rule(
871 struct ltt_kernel_event_notifier_rule *event)
872 {
873 assert(event);
874
875 if (event->fd >= 0) {
876 const int ret = close(event->fd);
877
878 DBG("Closing kernel event notifier rule file descriptor: fd = %d",
879 event->fd);
880 if (ret) {
881 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
882 event->fd);
883 }
884 } else {
885 DBG("Destroying kernel event notifier rule (no associated file descriptor)");
886 }
887
888 lttng_trigger_put(event->trigger);
889 call_rcu(&event->rcu_node, free_token_event_rule_rcu);
890 }
891 /*
892 * Cleanup kernel context structure.
893 */
894 void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
895 {
896 assert(ctx);
897
898 if (ctx->in_list) {
899 cds_list_del(&ctx->list);
900 }
901 free(ctx);
902 }
903
904 /*
905 * Cleanup kernel channel structure.
906 */
907 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
908 {
909 struct ltt_kernel_stream *stream, *stmp;
910 struct ltt_kernel_event *event, *etmp;
911 struct ltt_kernel_context *ctx, *ctmp;
912 int ret;
913 enum lttng_error_code status;
914
915 assert(channel);
916
917 DBG("[trace] Closing channel fd %d", channel->fd);
918 /* Close kernel fd */
919 if (channel->fd >= 0) {
920 ret = close(channel->fd);
921 if (ret) {
922 PERROR("close");
923 }
924 }
925
926 /* For each stream in the channel list */
927 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
928 trace_kernel_destroy_stream(stream);
929 }
930
931 /* For each event in the channel list */
932 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
933 trace_kernel_destroy_event(event);
934 }
935
936 /* For each context in the channel list */
937 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
938 trace_kernel_destroy_context(ctx);
939 }
940
941 /* Remove from channel list */
942 cds_list_del(&channel->list);
943
944 if (notification_thread_handle
945 && channel->published_to_notification_thread) {
946 status = notification_thread_command_remove_channel(
947 notification_thread_handle,
948 channel->key, LTTNG_DOMAIN_KERNEL);
949 assert(status == LTTNG_OK);
950 }
951 free(channel->channel->attr.extended.ptr);
952 free(channel->channel);
953 free(channel);
954 }
955
956 /*
957 * Cleanup kernel metadata structure.
958 */
959 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
960 {
961 assert(metadata);
962
963 DBG("[trace] Closing metadata fd %d", metadata->fd);
964 /* Close kernel fd */
965 if (metadata->fd >= 0) {
966 int ret;
967
968 ret = close(metadata->fd);
969 if (ret) {
970 PERROR("close");
971 }
972 }
973
974 free(metadata->conf);
975 free(metadata);
976 }
977
978 /*
979 * Cleanup kernel session structure
980 *
981 * Should *NOT* be called with RCU read-side lock held.
982 */
983 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
984 {
985 struct ltt_kernel_channel *channel, *ctmp;
986 int ret;
987
988 assert(session);
989
990 DBG("[trace] Closing session fd %d", session->fd);
991 /* Close kernel fds */
992 if (session->fd >= 0) {
993 ret = close(session->fd);
994 if (ret) {
995 PERROR("close");
996 }
997 }
998
999 if (session->metadata_stream_fd >= 0) {
1000 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
1001 ret = close(session->metadata_stream_fd);
1002 if (ret) {
1003 PERROR("close");
1004 }
1005 }
1006
1007 if (session->metadata != NULL) {
1008 trace_kernel_destroy_metadata(session->metadata);
1009 }
1010
1011 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
1012 trace_kernel_destroy_channel(channel);
1013 }
1014 }
1015
1016 /* Free elements needed by destroy notifiers. */
1017 void trace_kernel_free_session(struct ltt_kernel_session *session)
1018 {
1019 /* Wipe consumer output object */
1020 consumer_output_put(session->consumer);
1021
1022 process_attr_tracker_destroy(session->tracker_pid);
1023 process_attr_tracker_destroy(session->tracker_vpid);
1024 process_attr_tracker_destroy(session->tracker_uid);
1025 process_attr_tracker_destroy(session->tracker_vuid);
1026 process_attr_tracker_destroy(session->tracker_gid);
1027 process_attr_tracker_destroy(session->tracker_vgid);
1028
1029 free(session);
1030 }
This page took 0.081844 seconds and 4 git commands to generate.