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