sessiond: enforce mmap output type for kernel metadata channel
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <lttng/event.h>
25 #include <lttng/lttng-error.h>
26 #include <lttng/userspace-probe.h>
27 #include <lttng/userspace-probe-internal.h>
28
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_filter_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 /*
145 * Allocate and initialize a kernel session data structure.
146 *
147 * Return pointer to structure or NULL.
148 */
149 struct ltt_kernel_session *trace_kernel_create_session(void)
150 {
151 struct ltt_kernel_session *lks = NULL;
152
153 /* Allocate a new ltt kernel session */
154 lks = zmalloc(sizeof(struct ltt_kernel_session));
155 if (lks == NULL) {
156 PERROR("create kernel session zmalloc");
157 goto alloc_error;
158 }
159
160 /* Init data structure */
161 lks->fd = -1;
162 lks->metadata_stream_fd = -1;
163 lks->channel_count = 0;
164 lks->stream_count_global = 0;
165 lks->metadata = NULL;
166 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
167
168 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
169 if (lks->consumer == NULL) {
170 goto error;
171 }
172
173 return lks;
174
175 error:
176 free(lks);
177
178 alloc_error:
179 return NULL;
180 }
181
182 /*
183 * Allocate and initialize a kernel channel data structure.
184 *
185 * Return pointer to structure or NULL.
186 */
187 struct ltt_kernel_channel *trace_kernel_create_channel(
188 struct lttng_channel *chan)
189 {
190 struct ltt_kernel_channel *lkc;
191 struct lttng_channel_extended *extended = NULL;
192
193 assert(chan);
194
195 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
196 if (lkc == NULL) {
197 PERROR("ltt_kernel_channel zmalloc");
198 goto error;
199 }
200
201 lkc->channel = zmalloc(sizeof(struct lttng_channel));
202 if (lkc->channel == NULL) {
203 PERROR("lttng_channel zmalloc");
204 goto error;
205 }
206
207 extended = zmalloc(sizeof(struct lttng_channel_extended));
208 if (!extended) {
209 PERROR("lttng_channel_channel zmalloc");
210 goto error;
211 }
212 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
213 memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended));
214 lkc->channel->attr.extended.ptr = extended;
215 extended = NULL;
216
217 /*
218 * If we receive an empty string for channel name, it means the
219 * default channel name is requested.
220 */
221 if (chan->name[0] == '\0') {
222 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
223 sizeof(lkc->channel->name));
224 }
225 lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
226
227 lkc->fd = -1;
228 lkc->stream_count = 0;
229 lkc->event_count = 0;
230 lkc->enabled = 1;
231 lkc->published_to_notification_thread = false;
232 /* Init linked list */
233 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
234 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
235 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
236
237 return lkc;
238
239 error:
240 if (lkc) {
241 free(lkc->channel);
242 }
243 free(extended);
244 free(lkc);
245 return NULL;
246 }
247
248 /*
249 * Allocate and init a kernel context object.
250 *
251 * Return the allocated object or NULL on error.
252 */
253 struct ltt_kernel_context *trace_kernel_create_context(
254 struct lttng_kernel_context *ctx)
255 {
256 struct ltt_kernel_context *kctx;
257
258 kctx = zmalloc(sizeof(*kctx));
259 if (!kctx) {
260 PERROR("zmalloc kernel context");
261 goto error;
262 }
263
264 if (ctx) {
265 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
266 }
267 error:
268 return kctx;
269 }
270
271 /*
272 * Allocate and init a kernel context object from an existing kernel context
273 * object.
274 *
275 * Return the allocated object or NULL on error.
276 */
277 struct ltt_kernel_context *trace_kernel_copy_context(
278 struct ltt_kernel_context *kctx)
279 {
280 struct ltt_kernel_context *kctx_copy;
281
282 assert(kctx);
283 kctx_copy = zmalloc(sizeof(*kctx_copy));
284 if (!kctx_copy) {
285 PERROR("zmalloc ltt_kernel_context");
286 goto error;
287 }
288
289 memcpy(kctx_copy, kctx, sizeof(*kctx_copy));
290 memset(&kctx_copy->list, 0, sizeof(kctx_copy->list));
291
292 error:
293 return kctx_copy;
294 }
295
296 /*
297 * Allocate and initialize a kernel event. Set name and event type.
298 * We own filter_expression, and filter.
299 *
300 * Return pointer to structure or NULL.
301 */
302 enum lttng_error_code trace_kernel_create_event(
303 struct lttng_event *ev, char *filter_expression,
304 struct lttng_filter_bytecode *filter,
305 struct ltt_kernel_event **kernel_event)
306 {
307 enum lttng_error_code ret;
308 struct lttng_kernel_event *attr;
309 struct ltt_kernel_event *local_kernel_event;
310 struct lttng_userspace_probe_location *userspace_probe_location = NULL;
311
312 assert(ev);
313
314 local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event));
315 attr = zmalloc(sizeof(struct lttng_kernel_event));
316 if (local_kernel_event == NULL || attr == NULL) {
317 PERROR("kernel event zmalloc");
318 ret = LTTNG_ERR_NOMEM;
319 goto error;
320 }
321
322 switch (ev->type) {
323 case LTTNG_EVENT_PROBE:
324 attr->instrumentation = LTTNG_KERNEL_KPROBE;
325 attr->u.kprobe.addr = ev->attr.probe.addr;
326 attr->u.kprobe.offset = ev->attr.probe.offset;
327 strncpy(attr->u.kprobe.symbol_name,
328 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
329 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
330 break;
331 case LTTNG_EVENT_USERSPACE_PROBE:
332 {
333 const struct lttng_userspace_probe_location* location = NULL;
334 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
335
336 location = lttng_event_get_userspace_probe_location(ev);
337 if (!location) {
338 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
339 goto error;
340 }
341
342 /*
343 * From this point on, the specific term 'uprobe' is used
344 * instead of the generic 'userspace probe' because it's the
345 * technology used at the moment for this instrumentation.
346 * LTTng currently implements userspace probes using uprobes.
347 * In the interactions with the kernel tracer, we use the
348 * uprobe term.
349 */
350 attr->instrumentation = LTTNG_KERNEL_UPROBE;
351
352 /*
353 * Only the elf lookup method is supported at the moment.
354 */
355 lookup = lttng_userspace_probe_location_get_lookup_method(
356 location);
357 if (!lookup) {
358 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
359 goto error;
360 }
361
362 /*
363 * From the kernel tracer's perspective, all userspace probe
364 * event types are all the same: a file and an offset.
365 */
366 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
367 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
368 /* Get the file descriptor on the target binary. */
369 attr->u.uprobe.fd =
370 lttng_userspace_probe_location_function_get_binary_fd(location);
371
372 /*
373 * Save a reference to the probe location used during
374 * the listing of events. Close its FD since it won't
375 * be needed for listing.
376 */
377 userspace_probe_location =
378 lttng_userspace_probe_location_copy(location);
379 ret = lttng_userspace_probe_location_function_set_binary_fd(
380 userspace_probe_location, -1);
381 if (ret) {
382 goto error;
383 }
384 break;
385 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
386 /* Get the file descriptor on the target binary. */
387 attr->u.uprobe.fd =
388 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
389
390 /*
391 * Save a reference to the probe location used during the listing of
392 * events. Close its FD since it won't be needed for listing.
393 */
394 userspace_probe_location =
395 lttng_userspace_probe_location_copy(location);
396 ret = lttng_userspace_probe_location_tracepoint_set_binary_fd(
397 userspace_probe_location, -1);
398 if (ret) {
399 goto error;
400 }
401 break;
402 default:
403 DBG("Unsupported lookup method type");
404 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
405 goto error;
406 }
407 break;
408 }
409 case LTTNG_EVENT_FUNCTION:
410 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
411 attr->u.kretprobe.addr = ev->attr.probe.addr;
412 attr->u.kretprobe.offset = ev->attr.probe.offset;
413 strncpy(attr->u.kretprobe.symbol_name,
414 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
415 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
416 break;
417 case LTTNG_EVENT_FUNCTION_ENTRY:
418 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
419 strncpy(attr->u.ftrace.symbol_name,
420 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
421 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
422 break;
423 case LTTNG_EVENT_TRACEPOINT:
424 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
425 break;
426 case LTTNG_EVENT_SYSCALL:
427 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
428 break;
429 case LTTNG_EVENT_ALL:
430 attr->instrumentation = LTTNG_KERNEL_ALL;
431 break;
432 default:
433 ERR("Unknown kernel instrumentation type (%d)", ev->type);
434 ret = LTTNG_ERR_INVALID;
435 goto error;
436 }
437
438 /* Copy event name */
439 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
440 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
441
442 /* Setting up a kernel event */
443 local_kernel_event->fd = -1;
444 local_kernel_event->event = attr;
445 local_kernel_event->enabled = 1;
446 local_kernel_event->filter_expression = filter_expression;
447 local_kernel_event->filter = filter;
448 local_kernel_event->userspace_probe_location = userspace_probe_location;
449
450 *kernel_event = local_kernel_event;
451
452 return LTTNG_OK;
453
454 error:
455 free(filter_expression);
456 free(filter);
457 free(local_kernel_event);
458 free(attr);
459 return ret;
460 }
461
462 /*
463 * Allocate and initialize a kernel metadata.
464 *
465 * Return pointer to structure or NULL.
466 */
467 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
468 {
469 int ret;
470 struct ltt_kernel_metadata *lkm;
471 struct lttng_channel *chan;
472
473 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
474 chan = zmalloc(sizeof(struct lttng_channel));
475 if (lkm == NULL || chan == NULL) {
476 PERROR("kernel metadata zmalloc");
477 goto error;
478 }
479
480 ret = lttng_strncpy(
481 chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name));
482 if (ret) {
483 ERR("Failed to initialize metadata channel name to `%s`",
484 DEFAULT_METADATA_NAME);
485 goto error;
486 }
487
488 /* Set default attributes */
489 chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE;
490 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
491 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
492 chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
493 chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;;
494
495
496 /*
497 * The metadata channel of kernel sessions must use the "mmap"
498 * back-end since the consumer daemon accumulates complete
499 * metadata units before sending them to the relay daemon in
500 * live mode. The consumer daemon also needs to extract the contents
501 * of the metadata cache when computing a rotation position.
502 *
503 * In both cases, it is not possible to rely on the splice
504 * back-end as the consumer daemon may need to accumulate more
505 * content than can be backed by the ring buffer's underlying
506 * pages.
507 */
508 chan->attr.output = LTTNG_EVENT_MMAP;
509 chan->attr.tracefile_size = 0;
510 chan->attr.tracefile_count = 0;
511 chan->attr.live_timer_interval = 0;
512
513 /* Init metadata */
514 lkm->fd = -1;
515 lkm->conf = chan;
516
517 return lkm;
518
519 error:
520 free(lkm);
521 free(chan);
522 return NULL;
523 }
524
525 /*
526 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
527 * default.
528 *
529 * Return pointer to structure or NULL.
530 */
531 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
532 unsigned int count)
533 {
534 int ret;
535 struct ltt_kernel_stream *lks;
536
537 assert(name);
538
539 lks = zmalloc(sizeof(struct ltt_kernel_stream));
540 if (lks == NULL) {
541 PERROR("kernel stream zmalloc");
542 goto error;
543 }
544
545 /* Set name */
546 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
547 if (ret < 0) {
548 PERROR("snprintf stream name");
549 goto error;
550 }
551 lks->name[sizeof(lks->name) - 1] = '\0';
552
553 /* Init stream */
554 lks->fd = -1;
555 lks->state = 0;
556 lks->cpu = count;
557
558 return lks;
559
560 error:
561 return NULL;
562 }
563
564 /*
565 * Cleanup kernel stream structure.
566 */
567 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
568 {
569 assert(stream);
570
571 DBG("[trace] Closing stream fd %d", stream->fd);
572 /* Close kernel fd */
573 if (stream->fd >= 0) {
574 int ret;
575
576 ret = close(stream->fd);
577 if (ret) {
578 PERROR("close");
579 }
580 }
581 /* Remove from stream list */
582 cds_list_del(&stream->list);
583
584 free(stream);
585 }
586
587 /*
588 * Cleanup kernel event structure.
589 */
590 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
591 {
592 assert(event);
593
594 if (event->fd >= 0) {
595 int ret;
596
597 DBG("[trace] Closing event fd %d", event->fd);
598 /* Close kernel fd */
599 ret = close(event->fd);
600 if (ret) {
601 PERROR("close");
602 }
603 } else {
604 DBG("[trace] Tearing down event (no associated fd)");
605 }
606
607 /* Remove from event list */
608 cds_list_del(&event->list);
609
610 free(event->filter_expression);
611 free(event->filter);
612
613 free(event->event);
614 free(event);
615 }
616
617 /*
618 * Cleanup kernel context structure.
619 */
620 void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
621 {
622 assert(ctx);
623
624 if (ctx->in_list) {
625 cds_list_del(&ctx->list);
626 }
627 free(ctx);
628 }
629
630 /*
631 * Cleanup kernel channel structure.
632 */
633 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
634 {
635 struct ltt_kernel_stream *stream, *stmp;
636 struct ltt_kernel_event *event, *etmp;
637 struct ltt_kernel_context *ctx, *ctmp;
638 int ret;
639 enum lttng_error_code status;
640
641 assert(channel);
642
643 DBG("[trace] Closing channel fd %d", channel->fd);
644 /* Close kernel fd */
645 if (channel->fd >= 0) {
646 ret = close(channel->fd);
647 if (ret) {
648 PERROR("close");
649 }
650 }
651
652 /* For each stream in the channel list */
653 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
654 trace_kernel_destroy_stream(stream);
655 }
656
657 /* For each event in the channel list */
658 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
659 trace_kernel_destroy_event(event);
660 }
661
662 /* For each context in the channel list */
663 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
664 trace_kernel_destroy_context(ctx);
665 }
666
667 /* Remove from channel list */
668 cds_list_del(&channel->list);
669
670 if (notification_thread_handle
671 && channel->published_to_notification_thread) {
672 status = notification_thread_command_remove_channel(
673 notification_thread_handle,
674 channel->key, LTTNG_DOMAIN_KERNEL);
675 assert(status == LTTNG_OK);
676 }
677 free(channel->channel->attr.extended.ptr);
678 free(channel->channel);
679 free(channel);
680 }
681
682 /*
683 * Cleanup kernel metadata structure.
684 */
685 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
686 {
687 assert(metadata);
688
689 DBG("[trace] Closing metadata fd %d", metadata->fd);
690 /* Close kernel fd */
691 if (metadata->fd >= 0) {
692 int ret;
693
694 ret = close(metadata->fd);
695 if (ret) {
696 PERROR("close");
697 }
698 }
699
700 free(metadata->conf);
701 free(metadata);
702 }
703
704 /*
705 * Cleanup kernel session structure
706 *
707 * Should *NOT* be called with RCU read-side lock held.
708 */
709 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
710 {
711 struct ltt_kernel_channel *channel, *ctmp;
712 int ret;
713
714 assert(session);
715
716 DBG("[trace] Closing session fd %d", session->fd);
717 /* Close kernel fds */
718 if (session->fd >= 0) {
719 ret = close(session->fd);
720 if (ret) {
721 PERROR("close");
722 }
723 }
724
725 if (session->metadata_stream_fd >= 0) {
726 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
727 ret = close(session->metadata_stream_fd);
728 if (ret) {
729 PERROR("close");
730 }
731 }
732
733 if (session->metadata != NULL) {
734 trace_kernel_destroy_metadata(session->metadata);
735 }
736
737 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
738 trace_kernel_destroy_channel(channel);
739 }
740 }
741
742 /* Free elements needed by destroy notifiers. */
743 void trace_kernel_free_session(struct ltt_kernel_session *session)
744 {
745 /* Wipe consumer output object */
746 consumer_output_put(session->consumer);
747
748 free(session);
749 }
This page took 0.046056 seconds and 4 git commands to generate.