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