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