Add internal kernel channel destroy function
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.c
... / ...
CommitLineData
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 _GNU_SOURCE
19#include <errno.h>
20#include <fcntl.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <unistd.h>
25#include <inttypes.h>
26
27#include <common/common.h>
28#include <common/kernel-ctl/kernel-ctl.h>
29#include <common/sessiond-comm/sessiond-comm.h>
30
31#include "consumer.h"
32#include "kernel.h"
33#include "kern-modules.h"
34
35/*
36 * Add context on a kernel channel.
37 */
38int kernel_add_channel_context(struct ltt_kernel_channel *chan,
39 struct lttng_kernel_context *ctx)
40{
41 int ret;
42
43 DBG("Adding context to channel %s", chan->channel->name);
44 ret = kernctl_add_context(chan->fd, ctx);
45 if (ret < 0) {
46 if (errno != EEXIST) {
47 PERROR("add context ioctl");
48 } else {
49 /* If EEXIST, we just ignore the error */
50 ret = 0;
51 }
52 goto error;
53 }
54
55 chan->ctx = zmalloc(sizeof(struct lttng_kernel_context));
56 if (chan->ctx == NULL) {
57 PERROR("zmalloc event context");
58 goto error;
59 }
60
61 memcpy(chan->ctx, ctx, sizeof(struct lttng_kernel_context));
62
63 return 0;
64
65error:
66 return ret;
67}
68
69/*
70 * Create a new kernel session, register it to the kernel tracer and add it to
71 * the session daemon session.
72 */
73int kernel_create_session(struct ltt_session *session, int tracer_fd)
74{
75 int ret;
76 struct ltt_kernel_session *lks;
77
78 /* Allocate data structure */
79 lks = trace_kernel_create_session(session->path);
80 if (lks == NULL) {
81 ret = -1;
82 goto error;
83 }
84
85 /* Kernel tracer session creation */
86 ret = kernctl_create_session(tracer_fd);
87 if (ret < 0) {
88 PERROR("ioctl kernel create session");
89 goto error;
90 }
91
92 lks->fd = ret;
93 /* Prevent fd duplication after execlp() */
94 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
95 if (ret < 0) {
96 PERROR("fcntl session fd");
97 }
98
99 lks->id = session->id;
100 lks->consumer_fds_sent = 0;
101 session->kernel_session = lks;
102
103 DBG("Kernel session created (fd: %d)", lks->fd);
104
105 return 0;
106
107error:
108 return ret;
109}
110
111/*
112 * Create a kernel channel, register it to the kernel tracer and add it to the
113 * kernel session.
114 */
115int kernel_create_channel(struct ltt_kernel_session *session,
116 struct lttng_channel *chan, char *path)
117{
118 int ret;
119 struct ltt_kernel_channel *lkc;
120
121 /* Allocate kernel channel */
122 lkc = trace_kernel_create_channel(chan, path);
123 if (lkc == NULL) {
124 goto error;
125 }
126
127 DBG3("Kernel create channel %s in %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d",
128 chan->name, path, lkc->channel->attr.overwrite,
129 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
130 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
131 lkc->channel->attr.output);
132
133 /* Kernel tracer channel creation */
134 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
135 if (ret < 0) {
136 PERROR("ioctl kernel create channel");
137 goto error;
138 }
139
140 /* Setup the channel fd */
141 lkc->fd = ret;
142 /* Prevent fd duplication after execlp() */
143 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
144 if (ret < 0) {
145 PERROR("fcntl session fd");
146 }
147
148 /* Add channel to session */
149 cds_list_add(&lkc->list, &session->channel_list.head);
150 session->channel_count++;
151 lkc->session = session;
152
153 DBG("Kernel channel %s created (fd: %d)", lkc->channel->name, lkc->fd);
154
155 return 0;
156
157error:
158 return -1;
159}
160
161/*
162 * Create a kernel event, enable it to the kernel tracer and add it to the
163 * channel event list of the kernel session.
164 */
165int kernel_create_event(struct lttng_event *ev,
166 struct ltt_kernel_channel *channel)
167{
168 int ret;
169 struct ltt_kernel_event *event;
170
171 event = trace_kernel_create_event(ev);
172 if (event == NULL) {
173 ret = -1;
174 goto error;
175 }
176
177 ret = kernctl_create_event(channel->fd, event->event);
178 if (ret < 0) {
179 switch (errno) {
180 case EEXIST:
181 break;
182 case ENOSYS:
183 WARN("Event type not implemented");
184 break;
185 default:
186 PERROR("create event ioctl");
187 }
188 ret = -errno;
189 goto free_event;
190 }
191
192 /*
193 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
194 */
195 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
196 DBG2("Kernel event syscall creation success");
197 /*
198 * We use fd == -1 to ensure that we never trigger a close of fd
199 * 0.
200 */
201 event->fd = -1;
202 goto add_list;
203 }
204
205 event->fd = ret;
206 /* Prevent fd duplication after execlp() */
207 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
208 if (ret < 0) {
209 PERROR("fcntl session fd");
210 }
211
212add_list:
213 /* Add event to event list */
214 cds_list_add(&event->list, &channel->events_list.head);
215 channel->event_count++;
216
217 DBG("Event %s created (fd: %d)", ev->name, event->fd);
218
219 return 0;
220
221free_event:
222 free(event);
223error:
224 return ret;
225}
226
227/*
228 * Disable a kernel channel.
229 */
230int kernel_disable_channel(struct ltt_kernel_channel *chan)
231{
232 int ret;
233
234 ret = kernctl_disable(chan->fd);
235 if (ret < 0) {
236 PERROR("disable chan ioctl");
237 ret = errno;
238 goto error;
239 }
240
241 chan->enabled = 0;
242 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
243
244 return 0;
245
246error:
247 return ret;
248}
249
250/*
251 * Enable a kernel channel.
252 */
253int kernel_enable_channel(struct ltt_kernel_channel *chan)
254{
255 int ret;
256
257 ret = kernctl_enable(chan->fd);
258 if (ret < 0 && errno != EEXIST) {
259 PERROR("Enable kernel chan");
260 goto error;
261 }
262
263 chan->enabled = 1;
264 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
265
266 return 0;
267
268error:
269 return ret;
270}
271
272/*
273 * Enable a kernel event.
274 */
275int kernel_enable_event(struct ltt_kernel_event *event)
276{
277 int ret;
278
279 ret = kernctl_enable(event->fd);
280 if (ret < 0) {
281 switch (errno) {
282 case EEXIST:
283 ret = LTTNG_ERR_KERN_EVENT_EXIST;
284 break;
285 default:
286 PERROR("enable kernel event");
287 break;
288 }
289 goto error;
290 }
291
292 event->enabled = 1;
293 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
294
295 return 0;
296
297error:
298 return ret;
299}
300
301/*
302 * Disable a kernel event.
303 */
304int kernel_disable_event(struct ltt_kernel_event *event)
305{
306 int ret;
307
308 ret = kernctl_disable(event->fd);
309 if (ret < 0) {
310 switch (errno) {
311 case EEXIST:
312 ret = LTTNG_ERR_KERN_EVENT_EXIST;
313 break;
314 default:
315 PERROR("disable kernel event");
316 break;
317 }
318 goto error;
319 }
320
321 event->enabled = 0;
322 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
323
324 return 0;
325
326error:
327 return ret;
328}
329
330/*
331 * Create kernel metadata, open from the kernel tracer and add it to the
332 * kernel session.
333 */
334int kernel_open_metadata(struct ltt_kernel_session *session)
335{
336 int ret;
337 struct ltt_kernel_metadata *lkm;
338
339 /* Allocate kernel metadata */
340 lkm = trace_kernel_create_metadata();
341 if (lkm == NULL) {
342 goto error;
343 }
344
345 /* Kernel tracer metadata creation */
346 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
347 if (ret < 0) {
348 goto error;
349 }
350
351 lkm->fd = ret;
352 /* Prevent fd duplication after execlp() */
353 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
354 if (ret < 0) {
355 PERROR("fcntl session fd");
356 }
357
358 session->metadata = lkm;
359
360 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
361
362 return 0;
363
364error:
365 return -1;
366}
367
368/*
369 * Start tracing session.
370 */
371int kernel_start_session(struct ltt_kernel_session *session)
372{
373 int ret;
374
375 ret = kernctl_start_session(session->fd);
376 if (ret < 0) {
377 PERROR("ioctl start session");
378 goto error;
379 }
380
381 DBG("Kernel session started");
382
383 return 0;
384
385error:
386 return ret;
387}
388
389/*
390 * Make a kernel wait to make sure in-flight probe have completed.
391 */
392void kernel_wait_quiescent(int fd)
393{
394 int ret;
395
396 DBG("Kernel quiescent wait on %d", fd);
397
398 ret = kernctl_wait_quiescent(fd);
399 if (ret < 0) {
400 PERROR("wait quiescent ioctl");
401 ERR("Kernel quiescent wait failed");
402 }
403}
404
405/*
406 * Kernel calibrate
407 */
408int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
409{
410 int ret;
411
412 ret = kernctl_calibrate(fd, calibrate);
413 if (ret < 0) {
414 PERROR("calibrate ioctl");
415 return -1;
416 }
417
418 return 0;
419}
420
421
422/*
423 * Force flush buffer of metadata.
424 */
425int kernel_metadata_flush_buffer(int fd)
426{
427 int ret;
428
429 ret = kernctl_buffer_flush(fd);
430 if (ret < 0) {
431 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
432 }
433
434 return 0;
435}
436
437/*
438 * Force flush buffer for channel.
439 */
440int kernel_flush_buffer(struct ltt_kernel_channel *channel)
441{
442 int ret;
443 struct ltt_kernel_stream *stream;
444
445 DBG("Flush buffer for channel %s", channel->channel->name);
446
447 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
448 DBG("Flushing channel stream %d", stream->fd);
449 ret = kernctl_buffer_flush(stream->fd);
450 if (ret < 0) {
451 PERROR("ioctl");
452 ERR("Fail to flush buffer for stream %d (ret: %d)",
453 stream->fd, ret);
454 }
455 }
456
457 return 0;
458}
459
460/*
461 * Stop tracing session.
462 */
463int kernel_stop_session(struct ltt_kernel_session *session)
464{
465 int ret;
466
467 ret = kernctl_stop_session(session->fd);
468 if (ret < 0) {
469 goto error;
470 }
471
472 DBG("Kernel session stopped");
473
474 return 0;
475
476error:
477 return ret;
478}
479
480/*
481 * Open stream of channel, register it to the kernel tracer and add it
482 * to the stream list of the channel.
483 *
484 * Return the number of created stream. Else, a negative value.
485 */
486int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
487{
488 int ret, count = 0;
489 struct ltt_kernel_stream *lks;
490
491 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
492 lks = trace_kernel_create_stream(channel->channel->name, count);
493 if (lks == NULL) {
494 ret = close(ret);
495 if (ret) {
496 PERROR("close");
497 }
498 goto error;
499 }
500
501 lks->fd = ret;
502 /* Prevent fd duplication after execlp() */
503 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
504 if (ret < 0) {
505 PERROR("fcntl session fd");
506 }
507
508 /* Add stream to channe stream list */
509 cds_list_add(&lks->list, &channel->stream_list.head);
510 channel->stream_count++;
511
512 /* Increment counter which represent CPU number. */
513 count++;
514
515 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
516 lks->state);
517 }
518
519 return channel->stream_count;
520
521error:
522 return -1;
523}
524
525/*
526 * Open the metadata stream and set it to the kernel session.
527 */
528int kernel_open_metadata_stream(struct ltt_kernel_session *session)
529{
530 int ret;
531
532 ret = kernctl_create_stream(session->metadata->fd);
533 if (ret < 0) {
534 PERROR("kernel create metadata stream");
535 goto error;
536 }
537
538 DBG("Kernel metadata stream created (fd: %d)", ret);
539 session->metadata_stream_fd = ret;
540 /* Prevent fd duplication after execlp() */
541 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
542 if (ret < 0) {
543 PERROR("fcntl session fd");
544 }
545
546 return 0;
547
548error:
549 return -1;
550}
551
552/*
553 * Get the event list from the kernel tracer and return the number of elements.
554 */
555ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
556{
557 int fd, pos, ret;
558 char *event;
559 size_t nbmem, count = 0;
560 FILE *fp;
561 struct lttng_event *elist;
562
563 fd = kernctl_tracepoint_list(tracer_fd);
564 if (fd < 0) {
565 PERROR("kernel tracepoint list");
566 goto error;
567 }
568
569 fp = fdopen(fd, "r");
570 if (fp == NULL) {
571 PERROR("kernel tracepoint list fdopen");
572 goto error_fp;
573 }
574
575 /*
576 * Init memory size counter
577 * See kernel-ctl.h for explanation of this value
578 */
579 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
580 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
581 if (elist == NULL) {
582 PERROR("alloc list events");
583 count = -ENOMEM;
584 goto end;
585 }
586
587 while (fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos) == 1) {
588 if (count >= nbmem) {
589 struct lttng_event *new_elist;
590
591 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
592 nbmem * 2);
593 /* Double the size */
594 nbmem <<= 1;
595 new_elist = realloc(elist, nbmem * sizeof(struct lttng_event));
596 if (new_elist == NULL) {
597 PERROR("realloc list events");
598 free(event);
599 free(elist);
600 count = -ENOMEM;
601 goto end;
602 }
603 elist = new_elist;
604 }
605 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
606 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
607 elist[count].enabled = -1;
608 count++;
609 free(event);
610 }
611
612 *events = elist;
613 DBG("Kernel list events done (%zu events)", count);
614end:
615 ret = fclose(fp); /* closes both fp and fd */
616 if (ret) {
617 PERROR("fclose");
618 }
619 return count;
620
621error_fp:
622 ret = close(fd);
623 if (ret) {
624 PERROR("close");
625 }
626error:
627 return -1;
628}
629
630/*
631 * Get kernel version and validate it.
632 */
633int kernel_validate_version(int tracer_fd)
634{
635 int ret;
636 struct lttng_kernel_tracer_version version;
637
638 ret = kernctl_tracer_version(tracer_fd, &version);
639 if (ret < 0) {
640 ERR("Failed at getting the lttng-modules version");
641 goto error;
642 }
643
644 /* Validate version */
645 if (version.major != KERN_MODULES_PRE_MAJOR
646 && version.major != KERN_MODULES_MAJOR) {
647 goto error_version;
648 }
649
650 DBG2("Kernel tracer version validated (major version %d)", version.major);
651 return 0;
652
653error_version:
654 ERR("Kernel major version %d is not compatible (supporting <= %d)",
655 version.major, KERN_MODULES_MAJOR)
656 ret = -1;
657
658error:
659 return ret;
660}
661
662/*
663 * Kernel work-arounds called at the start of sessiond main().
664 */
665int init_kernel_workarounds(void)
666{
667 int ret;
668 FILE *fp;
669
670 /*
671 * boot_id needs to be read once before being used concurrently
672 * to deal with a Linux kernel race. A fix is proposed for
673 * upstream, but the work-around is needed for older kernels.
674 */
675 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
676 if (!fp) {
677 goto end_boot_id;
678 }
679 while (!feof(fp)) {
680 char buf[37] = "";
681
682 ret = fread(buf, 1, sizeof(buf), fp);
683 if (ret < 0) {
684 /* Ignore error, we don't really care */
685 }
686 }
687 ret = fclose(fp);
688 if (ret) {
689 PERROR("fclose");
690 }
691end_boot_id:
692 return 0;
693}
694
695/*
696 * Complete teardown of a kernel session.
697 */
698void kernel_destroy_session(struct ltt_kernel_session *ksess)
699{
700 if (ksess == NULL) {
701 DBG3("No kernel session when tearing down session");
702 return;
703 }
704
705 DBG("Tearing down kernel session");
706
707 /* Close any relayd session */
708 consumer_output_send_destroy_relayd(ksess->consumer);
709
710 trace_kernel_destroy_session(ksess);
711}
712
713/*
714 * Destroy a kernel channel object. It does not do anything on the tracer side.
715 */
716void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
717{
718 struct ltt_kernel_session *ksess = NULL;
719
720 assert(kchan);
721 assert(kchan->channel);
722
723 DBG3("Kernel destroy channel %s", kchan->channel->name);
724
725 /* Update channel count of associated session. */
726 if (kchan->session) {
727 /* Keep pointer reference so we can update it after the destroy. */
728 ksess = kchan->session;
729 }
730
731 trace_kernel_destroy_channel(kchan);
732
733 /*
734 * At this point the kernel channel is not visible anymore. This is safe
735 * since in order to work on a visible kernel session, the tracing session
736 * lock (ltt_session.lock) MUST be acquired.
737 */
738 if (ksess) {
739 ksess->channel_count--;
740 }
741}
This page took 0.024482 seconds and 4 git commands to generate.