SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[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_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_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 /*
382 * Only the elf lookup method is supported at the moment.
383 */
384 lookup = lttng_userspace_probe_location_get_lookup_method(
385 location);
386 if (!lookup) {
387 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
388 goto error;
389 }
390
391 /*
392 * From the kernel tracer's perspective, all userspace probe
393 * event types are all the same: a file and an offset.
394 */
395 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
396 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
397 /* Get the file descriptor on the target binary. */
398 attr->u.uprobe.fd =
399 lttng_userspace_probe_location_function_get_binary_fd(location);
400
401 /*
402 * Save a reference to the probe location used during
403 * the listing of events.
404 */
405 userspace_probe_location =
406 lttng_userspace_probe_location_copy(location);
407 break;
408 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
409 /* Get the file descriptor on the target binary. */
410 attr->u.uprobe.fd =
411 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
412
413 /*
414 * Save a reference to the probe location used during the listing of
415 * events.
416 */
417 userspace_probe_location =
418 lttng_userspace_probe_location_copy(location);
419 break;
420 default:
421 DBG("Unsupported lookup method type");
422 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
423 goto error;
424 }
425 break;
426 }
427 case LTTNG_EVENT_FUNCTION:
428 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
429 attr->u.kretprobe.addr = ev->attr.probe.addr;
430 attr->u.kretprobe.offset = ev->attr.probe.offset;
431 strncpy(attr->u.kretprobe.symbol_name,
432 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
433 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
434 break;
435 case LTTNG_EVENT_FUNCTION_ENTRY:
436 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
437 strncpy(attr->u.ftrace.symbol_name,
438 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
439 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
440 break;
441 case LTTNG_EVENT_TRACEPOINT:
442 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
443 break;
444 case LTTNG_EVENT_SYSCALL:
445 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
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_token_event_rule(
486 struct lttng_trigger *trigger,
487 uint64_t token,
488 uint64_t trigger_error_counter_index,
489 struct ltt_kernel_token_event_rule **kernel_token_event_rule)
490 {
491 enum lttng_error_code ret = LTTNG_OK;
492 struct ltt_kernel_token_event_rule *local_kernel_token_event_rule;
493 const struct lttng_condition *condition = NULL;
494 const struct lttng_event_rule *event_rule = NULL;
495
496 assert(kernel_token_event_rule);
497
498 condition = lttng_trigger_get_condition(trigger);
499 assert(condition);
500 assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT);
501
502 assert(lttng_condition_event_rule_get_rule(condition, &event_rule) == LTTNG_CONDITION_STATUS_OK);
503 assert(event_rule);
504 assert(lttng_event_rule_get_type(event_rule) != LTTNG_EVENT_RULE_TYPE_UNKNOWN);
505
506 local_kernel_token_event_rule = zmalloc(sizeof(struct ltt_kernel_token_event_rule));
507 if (local_kernel_token_event_rule == NULL) {
508 PERROR("Failed to allocate ltt_kernel_token_event_rule structure");
509 ret = LTTNG_ERR_NOMEM;
510 goto error;
511 }
512
513 local_kernel_token_event_rule->fd = -1;
514 local_kernel_token_event_rule->enabled = 1;
515 local_kernel_token_event_rule->token = token;
516 local_kernel_token_event_rule->error_counter_index = trigger_error_counter_index;
517
518 /* Get the reference of the event rule */
519 lttng_trigger_get(trigger);
520
521 local_kernel_token_event_rule->trigger = trigger;
522 /* The event rule still own the filter and bytecode */
523 local_kernel_token_event_rule->filter = lttng_event_rule_get_filter_bytecode(event_rule);
524
525 DBG3("[trace] Kernel token event rule %" PRIu64 " allocated", local_kernel_token_event_rule->token);
526 error:
527 *kernel_token_event_rule = local_kernel_token_event_rule;
528 return ret;
529 }
530
531 /*
532 * Initialize a kernel trigger from an event rule.
533 */
534 enum lttng_error_code trace_kernel_init_trigger_from_event_rule(const struct lttng_event_rule *rule,
535 struct lttng_kernel_trigger *kernel_trigger)
536 {
537 enum lttng_error_code ret;
538 enum lttng_event_rule_status status;
539 const char *name = NULL;
540
541 /* TODO: do this for now but have disucssion on if this could be the
542 * responsability of the event_rule itself ala
543 * "lttng_even_rule_generate_kernel_trigger"
544 */
545 switch (lttng_event_rule_get_type(rule)) {
546 case LTTNG_EVENT_RULE_TYPE_KPROBE:
547 {
548 uint64_t address = 0, offset = 0;
549 const char *symbol_name = NULL;
550 const struct lttng_kernel_probe_location *location = NULL;
551 enum lttng_kernel_probe_location_status k_status;
552
553 status = lttng_event_rule_kprobe_get_location(rule, &location);
554 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
555 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
556 goto error;
557 }
558
559 switch (lttng_kernel_probe_location_get_type(location)) {
560 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS:
561 {
562 k_status = lttng_kernel_probe_location_address_get_address(
563 location, &address);
564 if (k_status != LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK) {
565 ERR("Getting kernel probe address failed.");
566 }
567
568 break;
569 }
570 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET:
571 {
572 k_status = lttng_kernel_probe_location_symbol_get_offset(
573 location, &offset);
574 if (k_status != LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK) {
575 ERR("Getting kernel probe symbol offset failed.");
576 }
577
578 symbol_name = lttng_kernel_probe_location_symbol_get_name(
579 location);
580 break;
581 }
582 default:
583 ERR("Unknown kernel probe location type (%d)",
584 lttng_kernel_probe_location_get_type(
585 location));
586 ret = LTTNG_ERR_INVALID;
587 goto error;
588 }
589
590 kernel_trigger->instrumentation = LTTNG_KERNEL_KPROBE;
591 kernel_trigger->u.kprobe.addr = address;
592 kernel_trigger->u.kprobe.offset = offset;
593 if (symbol_name) {
594 strncpy(kernel_trigger->u.kprobe.symbol_name,
595 symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
596 }
597 kernel_trigger->u.kprobe
598 .symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] =
599 '\0';
600 (void) lttng_event_rule_kprobe_get_name(rule, &name);
601 ret = LTTNG_OK;
602 break;
603 }
604 case LTTNG_EVENT_RULE_TYPE_UPROBE:
605 {
606 const struct lttng_userspace_probe_location* location = NULL;
607 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
608
609 status = lttng_event_rule_uprobe_get_location(rule, &location);
610 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
611 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
612 goto error;
613 }
614
615 kernel_trigger->instrumentation = LTTNG_KERNEL_UPROBE;
616
617 /*
618 * Only the elf lookup method is supported at the moment.
619 */
620 lookup = lttng_userspace_probe_location_get_lookup_method(
621 location);
622 if (!lookup) {
623 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
624 goto error;
625 }
626
627 /*
628 * From the kernel tracer's perspective, all userspace probe
629 * event types are all the same: a file and an offset.
630 */
631 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
632 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
633 /* Get the file descriptor on the target binary. */
634 kernel_trigger->u.uprobe.fd =
635 lttng_userspace_probe_location_function_get_binary_fd(location);
636
637 break;
638 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
639 /* Get the file descriptor on the target binary. */
640 kernel_trigger->u.uprobe.fd =
641 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
642 break;
643 default:
644 DBG("Unsupported lookup method type");
645 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
646 goto error;
647 }
648
649 (void) lttng_event_rule_uprobe_get_name(rule, &name);
650
651 ret = LTTNG_OK;
652 break;
653 }
654 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
655 assert("Not supported" && 0);
656 ret = LTTNG_OK;
657 break;
658 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
659 /* TODO: assert his is a kernel domain event-rule */
660 kernel_trigger->instrumentation = LTTNG_KERNEL_TRACEPOINT;
661 (void) lttng_event_rule_tracepoint_get_pattern(rule, &name);
662 ret = LTTNG_OK;
663 break;
664 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
665 kernel_trigger->instrumentation = LTTNG_KERNEL_SYSCALL;
666 (void) lttng_event_rule_syscall_get_pattern(rule, &name);
667 ret = LTTNG_OK;
668 break;
669 default:
670 ERR("Unknown kernel event rule instrumentation type (%d)", lttng_event_rule_get_type(rule));
671 ret = LTTNG_ERR_INVALID;
672 goto error;
673 }
674
675 /*
676 * WTF is LTTNG_EVENT_ALL??? and LTTNG_EVENT_FUNTION_ENTRY?????
677 */
678
679 /* Copy event name */
680 strncpy(kernel_trigger->name, name, LTTNG_KERNEL_SYM_NAME_LEN);
681 kernel_trigger->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
682
683 error:
684 return ret;
685 }
686 /*
687 * Allocate and initialize a kernel metadata.
688 *
689 * Return pointer to structure or NULL.
690 */
691 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
692 {
693 int ret;
694 struct ltt_kernel_metadata *lkm;
695 struct lttng_channel *chan;
696
697 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
698 chan = zmalloc(sizeof(struct lttng_channel));
699 if (lkm == NULL || chan == NULL) {
700 PERROR("kernel metadata zmalloc");
701 goto error;
702 }
703
704 ret = lttng_strncpy(
705 chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name));
706 if (ret) {
707 ERR("Failed to initialize metadata channel name to `%s`",
708 DEFAULT_METADATA_NAME);
709 goto error;
710 }
711
712 /* Set default attributes */
713 chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE;
714 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
715 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
716 chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
717 chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;;
718
719
720 /*
721 * The metadata channel of kernel sessions must use the "mmap"
722 * back-end since the consumer daemon accumulates complete
723 * metadata units before sending them to the relay daemon in
724 * live mode. The consumer daemon also needs to extract the contents
725 * of the metadata cache when computing a rotation position.
726 *
727 * In both cases, it is not possible to rely on the splice
728 * back-end as the consumer daemon may need to accumulate more
729 * content than can be backed by the ring buffer's underlying
730 * pages.
731 */
732 chan->attr.output = LTTNG_EVENT_MMAP;
733 chan->attr.tracefile_size = 0;
734 chan->attr.tracefile_count = 0;
735 chan->attr.live_timer_interval = 0;
736
737 /* Init metadata */
738 lkm->fd = -1;
739 lkm->conf = chan;
740
741 return lkm;
742
743 error:
744 free(lkm);
745 free(chan);
746 return NULL;
747 }
748
749 /*
750 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
751 * default.
752 *
753 * Return pointer to structure or NULL.
754 */
755 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
756 unsigned int count)
757 {
758 int ret;
759 struct ltt_kernel_stream *lks;
760
761 assert(name);
762
763 lks = zmalloc(sizeof(struct ltt_kernel_stream));
764 if (lks == NULL) {
765 PERROR("kernel stream zmalloc");
766 goto error;
767 }
768
769 /* Set name */
770 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
771 if (ret < 0) {
772 PERROR("snprintf stream name");
773 goto error;
774 }
775 lks->name[sizeof(lks->name) - 1] = '\0';
776
777 /* Init stream */
778 lks->fd = -1;
779 lks->state = 0;
780 lks->cpu = count;
781
782 return lks;
783
784 error:
785 return NULL;
786 }
787
788 /*
789 * Cleanup kernel stream structure.
790 */
791 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
792 {
793 assert(stream);
794
795 DBG("[trace] Closing stream fd %d", stream->fd);
796 /* Close kernel fd */
797 if (stream->fd >= 0) {
798 int ret;
799
800 ret = close(stream->fd);
801 if (ret) {
802 PERROR("close");
803 }
804 }
805 /* Remove from stream list */
806 cds_list_del(&stream->list);
807
808 free(stream);
809 }
810
811 /*
812 * Cleanup kernel event structure.
813 */
814 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
815 {
816 assert(event);
817
818 if (event->fd >= 0) {
819 int ret;
820
821 DBG("[trace] Closing event fd %d", event->fd);
822 /* Close kernel fd */
823 ret = close(event->fd);
824 if (ret) {
825 PERROR("close");
826 }
827 } else {
828 DBG("[trace] Tearing down event (no associated fd)");
829 }
830
831 /* Remove from event list */
832 cds_list_del(&event->list);
833
834 free(event->filter_expression);
835 free(event->filter);
836
837 free(event->event);
838 free(event);
839 }
840
841 /*
842 * Cleanup kernel event structure.
843 */
844 void trace_kernel_destroy_token_event_rule(struct ltt_kernel_token_event_rule *event)
845 {
846 /* TODO: review in depth to ensure adequate disposing */
847 assert(event);
848
849 if (event->fd >= 0) {
850 int ret;
851
852 DBG("[trace] Closing ,token event rule fd %d", event->fd);
853 /* Close kernel fd */
854 ret = close(event->fd);
855 if (ret) {
856 PERROR("close");
857 }
858 } else {
859 DBG("[trace] Tearing down token event rule (no associated fd)");
860 }
861
862 lttng_trigger_put(event->trigger);
863 free(event);
864 }
865 /*
866 * Cleanup kernel context structure.
867 */
868 void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
869 {
870 assert(ctx);
871
872 if (ctx->in_list) {
873 cds_list_del(&ctx->list);
874 }
875 free(ctx);
876 }
877
878 /*
879 * Cleanup kernel channel structure.
880 */
881 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
882 {
883 struct ltt_kernel_stream *stream, *stmp;
884 struct ltt_kernel_event *event, *etmp;
885 struct ltt_kernel_context *ctx, *ctmp;
886 int ret;
887 enum lttng_error_code status;
888
889 assert(channel);
890
891 DBG("[trace] Closing channel fd %d", channel->fd);
892 /* Close kernel fd */
893 if (channel->fd >= 0) {
894 ret = close(channel->fd);
895 if (ret) {
896 PERROR("close");
897 }
898 }
899
900 /* For each stream in the channel list */
901 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
902 trace_kernel_destroy_stream(stream);
903 }
904
905 /* For each event in the channel list */
906 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
907 trace_kernel_destroy_event(event);
908 }
909
910 /* For each context in the channel list */
911 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
912 trace_kernel_destroy_context(ctx);
913 }
914
915 /* Remove from channel list */
916 cds_list_del(&channel->list);
917
918 if (notification_thread_handle
919 && channel->published_to_notification_thread) {
920 status = notification_thread_command_remove_channel(
921 notification_thread_handle,
922 channel->key, LTTNG_DOMAIN_KERNEL);
923 assert(status == LTTNG_OK);
924 }
925 free(channel->channel->attr.extended.ptr);
926 free(channel->channel);
927 free(channel);
928 }
929
930 /*
931 * Cleanup kernel metadata structure.
932 */
933 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
934 {
935 assert(metadata);
936
937 DBG("[trace] Closing metadata fd %d", metadata->fd);
938 /* Close kernel fd */
939 if (metadata->fd >= 0) {
940 int ret;
941
942 ret = close(metadata->fd);
943 if (ret) {
944 PERROR("close");
945 }
946 }
947
948 free(metadata->conf);
949 free(metadata);
950 }
951
952 /*
953 * Cleanup kernel session structure
954 *
955 * Should *NOT* be called with RCU read-side lock held.
956 */
957 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
958 {
959 struct ltt_kernel_channel *channel, *ctmp;
960 int ret;
961
962 assert(session);
963
964 DBG("[trace] Closing session fd %d", session->fd);
965 /* Close kernel fds */
966 if (session->fd >= 0) {
967 ret = close(session->fd);
968 if (ret) {
969 PERROR("close");
970 }
971 }
972
973 if (session->metadata_stream_fd >= 0) {
974 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
975 ret = close(session->metadata_stream_fd);
976 if (ret) {
977 PERROR("close");
978 }
979 }
980
981 if (session->metadata != NULL) {
982 trace_kernel_destroy_metadata(session->metadata);
983 }
984
985 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
986 trace_kernel_destroy_channel(channel);
987 }
988 }
989
990 /* Free elements needed by destroy notifiers. */
991 void trace_kernel_free_session(struct ltt_kernel_session *session)
992 {
993 /* Wipe consumer output object */
994 consumer_output_put(session->consumer);
995
996 process_attr_tracker_destroy(session->tracker_pid);
997 process_attr_tracker_destroy(session->tracker_vpid);
998 process_attr_tracker_destroy(session->tracker_uid);
999 process_attr_tracker_destroy(session->tracker_vuid);
1000 process_attr_tracker_destroy(session->tracker_gid);
1001 process_attr_tracker_destroy(session->tracker_vgid);
1002
1003 free(session);
1004 }
This page took 0.085958 seconds and 5 git commands to generate.