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