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