consumerd: tag metadata channel as being part of a live session
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.c
... / ...
CommitLineData
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
19#include <common/common.h>
20#include <common/defaults.h>
21#include <common/trace-chunk.h>
22
23#include "consumer.h"
24#include "trace-kernel.h"
25#include "lttng-sessiond.h"
26#include "notification-thread-commands.h"
27
28/*
29 * Find the channel name for the given kernel session.
30 */
31struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
32 const char *name, struct ltt_kernel_session *session)
33{
34 struct ltt_kernel_channel *chan;
35
36 assert(session);
37 assert(name);
38
39 /*
40 * If we receive an empty string for channel name, it means the
41 * default channel name is requested.
42 */
43 if (name[0] == '\0')
44 name = DEFAULT_CHANNEL_NAME;
45
46 DBG("Trying to find channel %s", name);
47
48 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
49 if (strcmp(name, chan->channel->name) == 0) {
50 DBG("Found channel by name %s", name);
51 return chan;
52 }
53 }
54
55 return NULL;
56}
57
58/*
59 * Find the event for the given channel.
60 */
61struct ltt_kernel_event *trace_kernel_find_event(
62 char *name, struct ltt_kernel_channel *channel,
63 enum lttng_event_type type,
64 struct lttng_filter_bytecode *filter)
65{
66 struct ltt_kernel_event *ev;
67 int found = 0;
68
69 assert(name);
70 assert(channel);
71
72 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
73 if (type != LTTNG_EVENT_ALL && ev->type != type) {
74 continue;
75 }
76 if (strcmp(name, ev->event->name)) {
77 continue;
78 }
79 if ((ev->filter && !filter) || (!ev->filter && filter)) {
80 continue;
81 }
82 if (ev->filter && filter) {
83 if (ev->filter->len != filter->len ||
84 memcmp(ev->filter->data, filter->data,
85 filter->len) != 0) {
86 continue;
87 }
88 }
89 found = 1;
90 break;
91 }
92 if (found) {
93 DBG("Found event %s for channel %s", name,
94 channel->channel->name);
95 return ev;
96 } else {
97 return NULL;
98 }
99}
100
101/*
102 * Find the event name for the given channel.
103 */
104struct ltt_kernel_event *trace_kernel_get_event_by_name(
105 char *name, struct ltt_kernel_channel *channel,
106 enum lttng_event_type type)
107{
108 struct ltt_kernel_event *ev;
109 int found = 0;
110
111 assert(name);
112 assert(channel);
113
114 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
115 if (type != LTTNG_EVENT_ALL && ev->type != type) {
116 continue;
117 }
118 if (strcmp(name, ev->event->name)) {
119 continue;
120 }
121 found = 1;
122 break;
123 }
124 if (found) {
125 DBG("Found event %s for channel %s", name,
126 channel->channel->name);
127 return ev;
128 } else {
129 return NULL;
130 }
131}
132
133/*
134 * Allocate and initialize a kernel session data structure.
135 *
136 * Return pointer to structure or NULL.
137 */
138struct ltt_kernel_session *trace_kernel_create_session(void)
139{
140 struct ltt_kernel_session *lks = NULL;
141
142 /* Allocate a new ltt kernel session */
143 lks = zmalloc(sizeof(struct ltt_kernel_session));
144 if (lks == NULL) {
145 PERROR("create kernel session zmalloc");
146 goto alloc_error;
147 }
148
149 /* Init data structure */
150 lks->fd = -1;
151 lks->metadata_stream_fd = -1;
152 lks->channel_count = 0;
153 lks->stream_count_global = 0;
154 lks->metadata = NULL;
155 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
156
157 lks->tracker_pid = process_attr_tracker_create();
158 if (!lks->tracker_pid) {
159 goto error;
160 }
161 lks->tracker_vpid = process_attr_tracker_create();
162 if (!lks->tracker_vpid) {
163 goto error;
164 }
165 lks->tracker_uid = process_attr_tracker_create();
166 if (!lks->tracker_uid) {
167 goto error;
168 }
169 lks->tracker_vuid = process_attr_tracker_create();
170 if (!lks->tracker_vuid) {
171 goto error;
172 }
173 lks->tracker_gid = process_attr_tracker_create();
174 if (!lks->tracker_gid) {
175 goto error;
176 }
177 lks->tracker_vgid = process_attr_tracker_create();
178 if (!lks->tracker_vgid) {
179 goto error;
180 }
181 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
182 if (lks->consumer == NULL) {
183 goto error;
184 }
185
186 return lks;
187
188error:
189 process_attr_tracker_destroy(lks->tracker_pid);
190 process_attr_tracker_destroy(lks->tracker_vpid);
191 process_attr_tracker_destroy(lks->tracker_uid);
192 process_attr_tracker_destroy(lks->tracker_vuid);
193 process_attr_tracker_destroy(lks->tracker_gid);
194 process_attr_tracker_destroy(lks->tracker_vgid);
195 free(lks);
196
197alloc_error:
198 return NULL;
199}
200
201/*
202 * Allocate and initialize a kernel channel data structure.
203 *
204 * Return pointer to structure or NULL.
205 */
206struct ltt_kernel_channel *trace_kernel_create_channel(
207 struct lttng_channel *chan)
208{
209 struct ltt_kernel_channel *lkc;
210 struct lttng_channel_extended *extended = NULL;
211
212 assert(chan);
213
214 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
215 if (lkc == NULL) {
216 PERROR("ltt_kernel_channel zmalloc");
217 goto error;
218 }
219
220 lkc->channel = zmalloc(sizeof(struct lttng_channel));
221 if (lkc->channel == NULL) {
222 PERROR("lttng_channel zmalloc");
223 goto error;
224 }
225
226 extended = zmalloc(sizeof(struct lttng_channel_extended));
227 if (!extended) {
228 PERROR("lttng_channel_channel zmalloc");
229 goto error;
230 }
231 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
232 memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended));
233 lkc->channel->attr.extended.ptr = extended;
234 extended = NULL;
235
236 /*
237 * If we receive an empty string for channel name, it means the
238 * default channel name is requested.
239 */
240 if (chan->name[0] == '\0') {
241 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
242 sizeof(lkc->channel->name));
243 }
244 lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
245
246 lkc->fd = -1;
247 lkc->stream_count = 0;
248 lkc->event_count = 0;
249 lkc->enabled = 1;
250 lkc->published_to_notification_thread = false;
251 /* Init linked list */
252 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
253 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
254 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
255
256 return lkc;
257
258error:
259 if (lkc) {
260 free(lkc->channel);
261 }
262 free(extended);
263 free(lkc);
264 return NULL;
265}
266
267/*
268 * Allocate and init a kernel context object.
269 *
270 * Return the allocated object or NULL on error.
271 */
272struct ltt_kernel_context *trace_kernel_create_context(
273 struct lttng_kernel_context *ctx)
274{
275 struct ltt_kernel_context *kctx;
276
277 kctx = zmalloc(sizeof(*kctx));
278 if (!kctx) {
279 PERROR("zmalloc kernel context");
280 goto error;
281 }
282
283 if (ctx) {
284 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
285 }
286error:
287 return kctx;
288}
289
290/*
291 * Allocate and init a kernel context object from an existing kernel context
292 * object.
293 *
294 * Return the allocated object or NULL on error.
295 */
296struct ltt_kernel_context *trace_kernel_copy_context(
297 struct ltt_kernel_context *kctx)
298{
299 struct ltt_kernel_context *kctx_copy;
300
301 assert(kctx);
302 kctx_copy = zmalloc(sizeof(*kctx_copy));
303 if (!kctx_copy) {
304 PERROR("zmalloc ltt_kernel_context");
305 goto error;
306 }
307
308 memcpy(kctx_copy, kctx, sizeof(*kctx_copy));
309 memset(&kctx_copy->list, 0, sizeof(kctx_copy->list));
310
311error:
312 return kctx_copy;
313}
314
315/*
316 * Allocate and initialize a kernel event. Set name and event type.
317 * We own filter_expression, and filter.
318 *
319 * Return pointer to structure or NULL.
320 */
321enum lttng_error_code trace_kernel_create_event(
322 struct lttng_event *ev, char *filter_expression,
323 struct lttng_filter_bytecode *filter,
324 struct ltt_kernel_event **kernel_event)
325{
326 enum lttng_error_code ret;
327 struct lttng_kernel_event *attr;
328 struct ltt_kernel_event *local_kernel_event;
329 struct lttng_userspace_probe_location *userspace_probe_location = NULL;
330
331 assert(ev);
332
333 local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event));
334 attr = zmalloc(sizeof(struct lttng_kernel_event));
335 if (local_kernel_event == NULL || attr == NULL) {
336 PERROR("kernel event zmalloc");
337 ret = LTTNG_ERR_NOMEM;
338 goto error;
339 }
340
341 switch (ev->type) {
342 case LTTNG_EVENT_PROBE:
343 attr->instrumentation = LTTNG_KERNEL_KPROBE;
344 attr->u.kprobe.addr = ev->attr.probe.addr;
345 attr->u.kprobe.offset = ev->attr.probe.offset;
346 strncpy(attr->u.kprobe.symbol_name,
347 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
348 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
349 break;
350 case LTTNG_EVENT_USERSPACE_PROBE:
351 {
352 const struct lttng_userspace_probe_location* location = NULL;
353 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
354
355 location = lttng_event_get_userspace_probe_location(ev);
356 if (!location) {
357 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
358 goto error;
359 }
360
361 /*
362 * From this point on, the specific term 'uprobe' is used
363 * instead of the generic 'userspace probe' because it's the
364 * technology used at the moment for this instrumentation.
365 * LTTng currently implements userspace probes using uprobes.
366 * In the interactions with the kernel tracer, we use the
367 * uprobe term.
368 */
369 attr->instrumentation = LTTNG_KERNEL_UPROBE;
370
371 /*
372 * Only the elf lookup method is supported at the moment.
373 */
374 lookup = lttng_userspace_probe_location_get_lookup_method(
375 location);
376 if (!lookup) {
377 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
378 goto error;
379 }
380
381 /*
382 * From the kernel tracer's perspective, all userspace probe
383 * event types are all the same: a file and an offset.
384 */
385 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
386 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
387 /* Get the file descriptor on the target binary. */
388 attr->u.uprobe.fd =
389 lttng_userspace_probe_location_function_get_binary_fd(location);
390
391 /*
392 * Save a reference to the probe location used during
393 * the listing of events. Close its FD since it won't
394 * be needed for listing.
395 */
396 userspace_probe_location =
397 lttng_userspace_probe_location_copy(location);
398 ret = lttng_userspace_probe_location_function_set_binary_fd(
399 userspace_probe_location, -1);
400 if (ret) {
401 goto error;
402 }
403 break;
404 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
405 /* Get the file descriptor on the target binary. */
406 attr->u.uprobe.fd =
407 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
408
409 /*
410 * Save a reference to the probe location used during the listing of
411 * events. Close its FD since it won't be needed for listing.
412 */
413 userspace_probe_location =
414 lttng_userspace_probe_location_copy(location);
415 ret = lttng_userspace_probe_location_tracepoint_set_binary_fd(
416 userspace_probe_location, -1);
417 if (ret) {
418 goto error;
419 }
420 break;
421 default:
422 DBG("Unsupported lookup method type");
423 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
424 goto error;
425 }
426 break;
427 }
428 case LTTNG_EVENT_FUNCTION:
429 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
430 attr->u.kretprobe.addr = ev->attr.probe.addr;
431 attr->u.kretprobe.offset = ev->attr.probe.offset;
432 strncpy(attr->u.kretprobe.symbol_name,
433 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
434 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
435 break;
436 case LTTNG_EVENT_FUNCTION_ENTRY:
437 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
438 strncpy(attr->u.ftrace.symbol_name,
439 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
440 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
441 break;
442 case LTTNG_EVENT_TRACEPOINT:
443 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
444 break;
445 case LTTNG_EVENT_SYSCALL:
446 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
447 break;
448 case LTTNG_EVENT_ALL:
449 attr->instrumentation = LTTNG_KERNEL_ALL;
450 break;
451 default:
452 ERR("Unknown kernel instrumentation type (%d)", ev->type);
453 ret = LTTNG_ERR_INVALID;
454 goto error;
455 }
456
457 /* Copy event name */
458 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
459 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
460
461 /* Setting up a kernel event */
462 local_kernel_event->fd = -1;
463 local_kernel_event->event = attr;
464 local_kernel_event->enabled = 1;
465 local_kernel_event->filter_expression = filter_expression;
466 local_kernel_event->filter = filter;
467 local_kernel_event->userspace_probe_location = userspace_probe_location;
468
469 *kernel_event = local_kernel_event;
470
471 return LTTNG_OK;
472
473error:
474 free(filter_expression);
475 free(filter);
476 free(local_kernel_event);
477 free(attr);
478 return ret;
479}
480
481/*
482 * Allocate and initialize a kernel metadata.
483 *
484 * Return pointer to structure or NULL.
485 */
486struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
487{
488 struct ltt_kernel_metadata *lkm;
489 struct lttng_channel *chan;
490
491 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
492 chan = zmalloc(sizeof(struct lttng_channel));
493 if (lkm == NULL || chan == NULL) {
494 PERROR("kernel metadata zmalloc");
495 goto error;
496 }
497
498 /* Set default attributes */
499 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
500 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
501 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
502 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
503 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
504 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
505
506 /* Init metadata */
507 lkm->fd = -1;
508 lkm->conf = chan;
509
510 return lkm;
511
512error:
513 free(lkm);
514 free(chan);
515 return NULL;
516}
517
518/*
519 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
520 * default.
521 *
522 * Return pointer to structure or NULL.
523 */
524struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
525 unsigned int count)
526{
527 int ret;
528 struct ltt_kernel_stream *lks;
529
530 assert(name);
531
532 lks = zmalloc(sizeof(struct ltt_kernel_stream));
533 if (lks == NULL) {
534 PERROR("kernel stream zmalloc");
535 goto error;
536 }
537
538 /* Set name */
539 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
540 if (ret < 0) {
541 PERROR("snprintf stream name");
542 goto error;
543 }
544 lks->name[sizeof(lks->name) - 1] = '\0';
545
546 /* Init stream */
547 lks->fd = -1;
548 lks->state = 0;
549 lks->cpu = count;
550
551 return lks;
552
553error:
554 return NULL;
555}
556
557/*
558 * Cleanup kernel stream structure.
559 */
560void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
561{
562 assert(stream);
563
564 DBG("[trace] Closing stream fd %d", stream->fd);
565 /* Close kernel fd */
566 if (stream->fd >= 0) {
567 int ret;
568
569 ret = close(stream->fd);
570 if (ret) {
571 PERROR("close");
572 }
573 }
574 /* Remove from stream list */
575 cds_list_del(&stream->list);
576
577 free(stream);
578}
579
580/*
581 * Cleanup kernel event structure.
582 */
583void trace_kernel_destroy_event(struct ltt_kernel_event *event)
584{
585 assert(event);
586
587 if (event->fd >= 0) {
588 int ret;
589
590 DBG("[trace] Closing event fd %d", event->fd);
591 /* Close kernel fd */
592 ret = close(event->fd);
593 if (ret) {
594 PERROR("close");
595 }
596 } else {
597 DBG("[trace] Tearing down event (no associated fd)");
598 }
599
600 /* Remove from event list */
601 cds_list_del(&event->list);
602
603 free(event->filter_expression);
604 free(event->filter);
605
606 free(event->event);
607 free(event);
608}
609
610/*
611 * Cleanup kernel context structure.
612 */
613void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
614{
615 assert(ctx);
616
617 if (ctx->in_list) {
618 cds_list_del(&ctx->list);
619 }
620 free(ctx);
621}
622
623/*
624 * Cleanup kernel channel structure.
625 */
626void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
627{
628 struct ltt_kernel_stream *stream, *stmp;
629 struct ltt_kernel_event *event, *etmp;
630 struct ltt_kernel_context *ctx, *ctmp;
631 int ret;
632 enum lttng_error_code status;
633
634 assert(channel);
635
636 DBG("[trace] Closing channel fd %d", channel->fd);
637 /* Close kernel fd */
638 if (channel->fd >= 0) {
639 ret = close(channel->fd);
640 if (ret) {
641 PERROR("close");
642 }
643 }
644
645 /* For each stream in the channel list */
646 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
647 trace_kernel_destroy_stream(stream);
648 }
649
650 /* For each event in the channel list */
651 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
652 trace_kernel_destroy_event(event);
653 }
654
655 /* For each context in the channel list */
656 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
657 trace_kernel_destroy_context(ctx);
658 }
659
660 /* Remove from channel list */
661 cds_list_del(&channel->list);
662
663 if (notification_thread_handle
664 && channel->published_to_notification_thread) {
665 status = notification_thread_command_remove_channel(
666 notification_thread_handle,
667 channel->key, LTTNG_DOMAIN_KERNEL);
668 assert(status == LTTNG_OK);
669 }
670 free(channel->channel->attr.extended.ptr);
671 free(channel->channel);
672 free(channel);
673}
674
675/*
676 * Cleanup kernel metadata structure.
677 */
678void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
679{
680 assert(metadata);
681
682 DBG("[trace] Closing metadata fd %d", metadata->fd);
683 /* Close kernel fd */
684 if (metadata->fd >= 0) {
685 int ret;
686
687 ret = close(metadata->fd);
688 if (ret) {
689 PERROR("close");
690 }
691 }
692
693 free(metadata->conf);
694 free(metadata);
695}
696
697/*
698 * Cleanup kernel session structure
699 *
700 * Should *NOT* be called with RCU read-side lock held.
701 */
702void trace_kernel_destroy_session(struct ltt_kernel_session *session)
703{
704 struct ltt_kernel_channel *channel, *ctmp;
705 int ret;
706
707 assert(session);
708
709 DBG("[trace] Closing session fd %d", session->fd);
710 /* Close kernel fds */
711 if (session->fd >= 0) {
712 ret = close(session->fd);
713 if (ret) {
714 PERROR("close");
715 }
716 }
717
718 if (session->metadata_stream_fd >= 0) {
719 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
720 ret = close(session->metadata_stream_fd);
721 if (ret) {
722 PERROR("close");
723 }
724 }
725
726 if (session->metadata != NULL) {
727 trace_kernel_destroy_metadata(session->metadata);
728 }
729
730 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
731 trace_kernel_destroy_channel(channel);
732 }
733}
734
735/* Free elements needed by destroy notifiers. */
736void trace_kernel_free_session(struct ltt_kernel_session *session)
737{
738 /* Wipe consumer output object */
739 consumer_output_put(session->consumer);
740
741 process_attr_tracker_destroy(session->tracker_pid);
742 process_attr_tracker_destroy(session->tracker_vpid);
743 process_attr_tracker_destroy(session->tracker_uid);
744 process_attr_tracker_destroy(session->tracker_vuid);
745 process_attr_tracker_destroy(session->tracker_gid);
746 process_attr_tracker_destroy(session->tracker_vgid);
747
748 free(session);
749}
This page took 0.024929 seconds and 4 git commands to generate.