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