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