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