Add pidfile creation for lttng-sessiond
[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
152 DBG("Kernel channel %s created (fd: %d)", lkc->channel->name, lkc->fd);
153
154 return 0;
155
156error:
157 return -1;
158}
159
160/*
161 * Create a kernel event, enable it to the kernel tracer and add it to the
162 * channel event list of the kernel session.
163 */
164int kernel_create_event(struct lttng_event *ev,
165 struct ltt_kernel_channel *channel)
166{
167 int ret;
168 struct ltt_kernel_event *event;
169
170 event = trace_kernel_create_event(ev);
171 if (event == NULL) {
172 ret = -1;
173 goto error;
174 }
175
176 ret = kernctl_create_event(channel->fd, event->event);
177 if (ret < 0) {
178 switch (errno) {
179 case EEXIST:
180 break;
181 case ENOSYS:
182 WARN("Event type not implemented");
183 break;
184 default:
185 PERROR("create event ioctl");
186 }
187 ret = -errno;
188 goto free_event;
189 }
190
191 /*
192 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
193 */
194 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
195 DBG2("Kernel event syscall creation success");
196 /*
197 * We use fd == -1 to ensure that we never trigger a close of fd
198 * 0.
199 */
200 event->fd = -1;
201 goto add_list;
202 }
203
204 event->fd = ret;
205 /* Prevent fd duplication after execlp() */
206 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
207 if (ret < 0) {
208 PERROR("fcntl session fd");
209 }
210
211add_list:
212 /* Add event to event list */
213 cds_list_add(&event->list, &channel->events_list.head);
214 channel->event_count++;
215
216 DBG("Event %s created (fd: %d)", ev->name, event->fd);
217
218 return 0;
219
220free_event:
221 free(event);
222error:
223 return ret;
224}
225
226/*
227 * Disable a kernel channel.
228 */
229int kernel_disable_channel(struct ltt_kernel_channel *chan)
230{
231 int ret;
232
233 ret = kernctl_disable(chan->fd);
234 if (ret < 0) {
235 PERROR("disable chan ioctl");
236 ret = errno;
237 goto error;
238 }
239
240 chan->enabled = 0;
241 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
242
243 return 0;
244
245error:
246 return ret;
247}
248
249/*
250 * Enable a kernel channel.
251 */
252int kernel_enable_channel(struct ltt_kernel_channel *chan)
253{
254 int ret;
255
256 ret = kernctl_enable(chan->fd);
257 if (ret < 0 && errno != EEXIST) {
258 PERROR("Enable kernel chan");
259 goto error;
260 }
261
262 chan->enabled = 1;
263 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
264
265 return 0;
266
267error:
268 return ret;
269}
270
271/*
272 * Enable a kernel event.
273 */
274int kernel_enable_event(struct ltt_kernel_event *event)
275{
276 int ret;
277
278 ret = kernctl_enable(event->fd);
279 if (ret < 0) {
280 switch (errno) {
281 case EEXIST:
282 ret = LTTNG_ERR_KERN_EVENT_EXIST;
283 break;
284 default:
285 PERROR("enable kernel event");
286 break;
287 }
288 goto error;
289 }
290
291 event->enabled = 1;
292 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
293
294 return 0;
295
296error:
297 return ret;
298}
299
300/*
301 * Disable a kernel event.
302 */
303int kernel_disable_event(struct ltt_kernel_event *event)
304{
305 int ret;
306
307 ret = kernctl_disable(event->fd);
308 if (ret < 0) {
309 switch (errno) {
310 case EEXIST:
311 ret = LTTNG_ERR_KERN_EVENT_EXIST;
312 break;
313 default:
314 PERROR("disable kernel event");
315 break;
316 }
317 goto error;
318 }
319
320 event->enabled = 0;
321 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
322
323 return 0;
324
325error:
326 return ret;
327}
328
329/*
330 * Create kernel metadata, open from the kernel tracer and add it to the
331 * kernel session.
332 */
333int kernel_open_metadata(struct ltt_kernel_session *session)
334{
335 int ret;
336 struct ltt_kernel_metadata *lkm;
337
338 /* Allocate kernel metadata */
339 lkm = trace_kernel_create_metadata();
340 if (lkm == NULL) {
341 goto error;
342 }
343
344 /* Kernel tracer metadata creation */
345 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
346 if (ret < 0) {
347 goto error;
348 }
349
350 lkm->fd = ret;
351 /* Prevent fd duplication after execlp() */
352 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
353 if (ret < 0) {
354 PERROR("fcntl session fd");
355 }
356
357 session->metadata = lkm;
358
359 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
360
361 return 0;
362
363error:
364 return -1;
365}
366
367/*
368 * Start tracing session.
369 */
370int kernel_start_session(struct ltt_kernel_session *session)
371{
372 int ret;
373
374 ret = kernctl_start_session(session->fd);
375 if (ret < 0) {
376 PERROR("ioctl start session");
377 goto error;
378 }
379
380 DBG("Kernel session started");
381
382 return 0;
383
384error:
385 return ret;
386}
387
388/*
389 * Make a kernel wait to make sure in-flight probe have completed.
390 */
391void kernel_wait_quiescent(int fd)
392{
393 int ret;
394
395 DBG("Kernel quiescent wait on %d", fd);
396
397 ret = kernctl_wait_quiescent(fd);
398 if (ret < 0) {
399 PERROR("wait quiescent ioctl");
400 ERR("Kernel quiescent wait failed");
401 }
402}
403
404/*
405 * Kernel calibrate
406 */
407int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
408{
409 int ret;
410
411 ret = kernctl_calibrate(fd, calibrate);
412 if (ret < 0) {
413 PERROR("calibrate ioctl");
414 return -1;
415 }
416
417 return 0;
418}
419
420
421/*
422 * Force flush buffer of metadata.
423 */
424int kernel_metadata_flush_buffer(int fd)
425{
426 int ret;
427
428 ret = kernctl_buffer_flush(fd);
429 if (ret < 0) {
430 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
431 }
432
433 return 0;
434}
435
436/*
437 * Force flush buffer for channel.
438 */
439int kernel_flush_buffer(struct ltt_kernel_channel *channel)
440{
441 int ret;
442 struct ltt_kernel_stream *stream;
443
444 DBG("Flush buffer for channel %s", channel->channel->name);
445
446 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
447 DBG("Flushing channel stream %d", stream->fd);
448 ret = kernctl_buffer_flush(stream->fd);
449 if (ret < 0) {
450 PERROR("ioctl");
451 ERR("Fail to flush buffer for stream %d (ret: %d)",
452 stream->fd, ret);
453 }
454 }
455
456 return 0;
457}
458
459/*
460 * Stop tracing session.
461 */
462int kernel_stop_session(struct ltt_kernel_session *session)
463{
464 int ret;
465
466 ret = kernctl_stop_session(session->fd);
467 if (ret < 0) {
468 goto error;
469 }
470
471 DBG("Kernel session stopped");
472
473 return 0;
474
475error:
476 return ret;
477}
478
479/*
480 * Open stream of channel, register it to the kernel tracer and add it
481 * to the stream list of the channel.
482 *
483 * Return the number of created stream. Else, a negative value.
484 */
485int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
486{
487 int ret, count = 0;
488 struct ltt_kernel_stream *lks;
489
490 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
491 lks = trace_kernel_create_stream(channel->channel->name, count);
492 if (lks == NULL) {
493 ret = close(ret);
494 if (ret) {
495 PERROR("close");
496 }
497 goto error;
498 }
499
500 lks->fd = ret;
501 /* Prevent fd duplication after execlp() */
502 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
503 if (ret < 0) {
504 PERROR("fcntl session fd");
505 }
506
507 /* Add stream to channe stream list */
508 cds_list_add(&lks->list, &channel->stream_list.head);
509 channel->stream_count++;
510
511 /* Increment counter which represent CPU number. */
512 count++;
513
514 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
515 lks->state);
516 }
517
518 return channel->stream_count;
519
520error:
521 return -1;
522}
523
524/*
525 * Open the metadata stream and set it to the kernel session.
526 */
527int kernel_open_metadata_stream(struct ltt_kernel_session *session)
528{
529 int ret;
530
531 ret = kernctl_create_stream(session->metadata->fd);
532 if (ret < 0) {
533 PERROR("kernel create metadata stream");
534 goto error;
535 }
536
537 DBG("Kernel metadata stream created (fd: %d)", ret);
538 session->metadata_stream_fd = ret;
539 /* Prevent fd duplication after execlp() */
540 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
541 if (ret < 0) {
542 PERROR("fcntl session fd");
543 }
544
545 return 0;
546
547error:
548 return -1;
549}
550
551/*
552 * Get the event list from the kernel tracer and return the number of elements.
553 */
554ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
555{
556 int fd, pos, ret;
557 char *event;
558 size_t nbmem, count = 0;
559 FILE *fp;
560 struct lttng_event *elist;
561
562 fd = kernctl_tracepoint_list(tracer_fd);
563 if (fd < 0) {
564 PERROR("kernel tracepoint list");
565 goto error;
566 }
567
568 fp = fdopen(fd, "r");
569 if (fp == NULL) {
570 PERROR("kernel tracepoint list fdopen");
571 goto error_fp;
572 }
573
574 /*
575 * Init memory size counter
576 * See kernel-ctl.h for explanation of this value
577 */
578 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
579 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
580 if (elist == NULL) {
581 PERROR("alloc list events");
582 count = -ENOMEM;
583 goto end;
584 }
585
586 while (fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos) == 1) {
587 if (count >= nbmem) {
588 struct lttng_event *new_elist;
589
590 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
591 nbmem * 2);
592 /* Double the size */
593 nbmem <<= 1;
594 new_elist = realloc(elist, nbmem * sizeof(struct lttng_event));
595 if (new_elist == NULL) {
596 PERROR("realloc list events");
597 free(event);
598 free(elist);
599 count = -ENOMEM;
600 goto end;
601 }
602 elist = new_elist;
603 }
604 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
605 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
606 elist[count].enabled = -1;
607 count++;
608 free(event);
609 }
610
611 *events = elist;
612 DBG("Kernel list events done (%zu events)", count);
613end:
614 ret = fclose(fp); /* closes both fp and fd */
615 if (ret) {
616 PERROR("fclose");
617 }
618 return count;
619
620error_fp:
621 ret = close(fd);
622 if (ret) {
623 PERROR("close");
624 }
625error:
626 return -1;
627}
628
629/*
630 * Get kernel version and validate it.
631 */
632int kernel_validate_version(int tracer_fd)
633{
634 int ret;
635 struct lttng_kernel_tracer_version version;
636
637 ret = kernctl_tracer_version(tracer_fd, &version);
638 if (ret < 0) {
639 ERR("Failed at getting the lttng-modules version");
640 goto error;
641 }
642
643 /* Validate version */
644 if (version.major != KERN_MODULES_PRE_MAJOR
645 && version.major != KERN_MODULES_MAJOR) {
646 goto error_version;
647 }
648
649 DBG2("Kernel tracer version validated (major version %d)", version.major);
650 return 0;
651
652error_version:
653 ERR("Kernel major version %d is not compatible (supporting <= %d)",
654 version.major, KERN_MODULES_MAJOR)
655 ret = -1;
656
657error:
658 return ret;
659}
660
661/*
662 * Kernel work-arounds called at the start of sessiond main().
663 */
664int init_kernel_workarounds(void)
665{
666 int ret;
667 FILE *fp;
668
669 /*
670 * boot_id needs to be read once before being used concurrently
671 * to deal with a Linux kernel race. A fix is proposed for
672 * upstream, but the work-around is needed for older kernels.
673 */
674 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
675 if (!fp) {
676 goto end_boot_id;
677 }
678 while (!feof(fp)) {
679 char buf[37] = "";
680
681 ret = fread(buf, 1, sizeof(buf), fp);
682 if (ret < 0) {
683 /* Ignore error, we don't really care */
684 }
685 }
686 ret = fclose(fp);
687 if (ret) {
688 PERROR("fclose");
689 }
690end_boot_id:
691 return 0;
692}
693
694/*
695 * Complete teardown of a kernel session.
696 */
697void kernel_destroy_session(struct ltt_kernel_session *ksess)
698{
699 if (ksess == NULL) {
700 DBG3("No kernel session when tearing down session");
701 return;
702 }
703
704 DBG("Tearing down kernel session");
705
706 /* Close any relayd session */
707 consumer_output_send_destroy_relayd(ksess->consumer);
708
709 trace_kernel_destroy_session(ksess);
710}
This page took 0.026474 seconds and 5 git commands to generate.