Fix: poll: show the correct number of fds
[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, %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.live_timer_interval, 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 case ENOENT:
205 WARN("Event %s not found!", ev->name);
206 break;
207 default:
208 PERROR("create event ioctl");
209 }
210 ret = -errno;
211 goto free_event;
212 }
213
214 /*
215 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
216 */
217 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
218 DBG2("Kernel event syscall creation success");
219 /*
220 * We use fd == -1 to ensure that we never trigger a close of fd
221 * 0.
222 */
223 event->fd = -1;
224 goto add_list;
225 }
226
227 event->fd = ret;
228 /* Prevent fd duplication after execlp() */
229 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
230 if (ret < 0) {
231 PERROR("fcntl session fd");
232 }
233
234 add_list:
235 /* Add event to event list */
236 cds_list_add(&event->list, &channel->events_list.head);
237 channel->event_count++;
238
239 DBG("Event %s created (fd: %d)", ev->name, event->fd);
240
241 return 0;
242
243 free_event:
244 free(event);
245 error:
246 return ret;
247 }
248
249 /*
250 * Disable a kernel channel.
251 */
252 int kernel_disable_channel(struct ltt_kernel_channel *chan)
253 {
254 int ret;
255
256 assert(chan);
257
258 ret = kernctl_disable(chan->fd);
259 if (ret < 0) {
260 PERROR("disable chan ioctl");
261 ret = errno;
262 goto error;
263 }
264
265 chan->enabled = 0;
266 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
267
268 return 0;
269
270 error:
271 return ret;
272 }
273
274 /*
275 * Enable a kernel channel.
276 */
277 int kernel_enable_channel(struct ltt_kernel_channel *chan)
278 {
279 int ret;
280
281 assert(chan);
282
283 ret = kernctl_enable(chan->fd);
284 if (ret < 0 && errno != EEXIST) {
285 PERROR("Enable kernel chan");
286 goto error;
287 }
288
289 chan->enabled = 1;
290 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
291
292 return 0;
293
294 error:
295 return ret;
296 }
297
298 /*
299 * Enable a kernel event.
300 */
301 int kernel_enable_event(struct ltt_kernel_event *event)
302 {
303 int ret;
304
305 assert(event);
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 assert(event);
337
338 ret = kernctl_disable(event->fd);
339 if (ret < 0) {
340 switch (errno) {
341 case EEXIST:
342 ret = LTTNG_ERR_KERN_EVENT_EXIST;
343 break;
344 default:
345 PERROR("disable kernel event");
346 break;
347 }
348 goto error;
349 }
350
351 event->enabled = 0;
352 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
353
354 return 0;
355
356 error:
357 return ret;
358 }
359
360 /*
361 * Create kernel metadata, open from the kernel tracer and add it to the
362 * kernel session.
363 */
364 int kernel_open_metadata(struct ltt_kernel_session *session)
365 {
366 int ret;
367 struct ltt_kernel_metadata *lkm = NULL;
368
369 assert(session);
370
371 /* Allocate kernel metadata */
372 lkm = trace_kernel_create_metadata();
373 if (lkm == NULL) {
374 goto error;
375 }
376
377 /* Kernel tracer metadata creation */
378 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
379 if (ret < 0) {
380 goto error_open;
381 }
382
383 lkm->fd = ret;
384 /* Prevent fd duplication after execlp() */
385 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
386 if (ret < 0) {
387 PERROR("fcntl session fd");
388 }
389
390 session->metadata = lkm;
391
392 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
393
394 return 0;
395
396 error_open:
397 trace_kernel_destroy_metadata(lkm);
398 error:
399 return -1;
400 }
401
402 /*
403 * Start tracing session.
404 */
405 int kernel_start_session(struct ltt_kernel_session *session)
406 {
407 int ret;
408
409 assert(session);
410
411 ret = kernctl_start_session(session->fd);
412 if (ret < 0) {
413 PERROR("ioctl start session");
414 goto error;
415 }
416
417 DBG("Kernel session started");
418
419 return 0;
420
421 error:
422 return ret;
423 }
424
425 /*
426 * Make a kernel wait to make sure in-flight probe have completed.
427 */
428 void kernel_wait_quiescent(int fd)
429 {
430 int ret;
431
432 DBG("Kernel quiescent wait on %d", fd);
433
434 ret = kernctl_wait_quiescent(fd);
435 if (ret < 0) {
436 PERROR("wait quiescent ioctl");
437 ERR("Kernel quiescent wait failed");
438 }
439 }
440
441 /*
442 * Kernel calibrate
443 */
444 int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
445 {
446 int ret;
447
448 assert(calibrate);
449
450 ret = kernctl_calibrate(fd, calibrate);
451 if (ret < 0) {
452 PERROR("calibrate ioctl");
453 return -1;
454 }
455
456 return 0;
457 }
458
459
460 /*
461 * Force flush buffer of metadata.
462 */
463 int kernel_metadata_flush_buffer(int fd)
464 {
465 int ret;
466
467 DBG("Kernel flushing metadata buffer on fd %d", fd);
468
469 ret = kernctl_buffer_flush(fd);
470 if (ret < 0) {
471 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
472 }
473
474 return 0;
475 }
476
477 /*
478 * Force flush buffer for channel.
479 */
480 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
481 {
482 int ret;
483 struct ltt_kernel_stream *stream;
484
485 assert(channel);
486
487 DBG("Flush buffer for channel %s", channel->channel->name);
488
489 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
490 DBG("Flushing channel stream %d", stream->fd);
491 ret = kernctl_buffer_flush(stream->fd);
492 if (ret < 0) {
493 PERROR("ioctl");
494 ERR("Fail to flush buffer for stream %d (ret: %d)",
495 stream->fd, ret);
496 }
497 }
498
499 return 0;
500 }
501
502 /*
503 * Stop tracing session.
504 */
505 int kernel_stop_session(struct ltt_kernel_session *session)
506 {
507 int ret;
508
509 assert(session);
510
511 ret = kernctl_stop_session(session->fd);
512 if (ret < 0) {
513 goto error;
514 }
515
516 DBG("Kernel session stopped");
517
518 return 0;
519
520 error:
521 return ret;
522 }
523
524 /*
525 * Open stream of channel, register it to the kernel tracer and add it
526 * to the stream list of the channel.
527 *
528 * Return the number of created stream. Else, a negative value.
529 */
530 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
531 {
532 int ret, count = 0;
533 struct ltt_kernel_stream *lks;
534
535 assert(channel);
536
537 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
538 lks = trace_kernel_create_stream(channel->channel->name, count);
539 if (lks == NULL) {
540 ret = close(ret);
541 if (ret) {
542 PERROR("close");
543 }
544 goto error;
545 }
546
547 lks->fd = ret;
548 /* Prevent fd duplication after execlp() */
549 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
550 if (ret < 0) {
551 PERROR("fcntl session fd");
552 }
553
554 lks->tracefile_size = channel->channel->attr.tracefile_size;
555 lks->tracefile_count = channel->channel->attr.tracefile_count;
556
557 /* Add stream to channe stream list */
558 cds_list_add(&lks->list, &channel->stream_list.head);
559 channel->stream_count++;
560
561 /* Increment counter which represent CPU number. */
562 count++;
563
564 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
565 lks->state);
566 }
567
568 return channel->stream_count;
569
570 error:
571 return -1;
572 }
573
574 /*
575 * Open the metadata stream and set it to the kernel session.
576 */
577 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
578 {
579 int ret;
580
581 assert(session);
582
583 ret = kernctl_create_stream(session->metadata->fd);
584 if (ret < 0) {
585 PERROR("kernel create metadata stream");
586 goto error;
587 }
588
589 DBG("Kernel metadata stream created (fd: %d)", ret);
590 session->metadata_stream_fd = ret;
591 /* Prevent fd duplication after execlp() */
592 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
593 if (ret < 0) {
594 PERROR("fcntl session fd");
595 }
596
597 return 0;
598
599 error:
600 return -1;
601 }
602
603 /*
604 * Get the event list from the kernel tracer and return the number of elements.
605 */
606 ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
607 {
608 int fd, ret;
609 char *event;
610 size_t nbmem, count = 0;
611 FILE *fp;
612 struct lttng_event *elist;
613
614 assert(events);
615
616 fd = kernctl_tracepoint_list(tracer_fd);
617 if (fd < 0) {
618 PERROR("kernel tracepoint list");
619 goto error;
620 }
621
622 fp = fdopen(fd, "r");
623 if (fp == NULL) {
624 PERROR("kernel tracepoint list fdopen");
625 goto error_fp;
626 }
627
628 /*
629 * Init memory size counter
630 * See kernel-ctl.h for explanation of this value
631 */
632 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
633 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
634 if (elist == NULL) {
635 PERROR("alloc list events");
636 count = -ENOMEM;
637 goto end;
638 }
639
640 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
641 if (count >= nbmem) {
642 struct lttng_event *new_elist;
643 size_t new_nbmem;
644
645 new_nbmem = nbmem << 1;
646 DBG("Reallocating event list from %zu to %zu bytes",
647 nbmem, new_nbmem);
648 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
649 if (new_elist == NULL) {
650 PERROR("realloc list events");
651 free(event);
652 free(elist);
653 count = -ENOMEM;
654 goto end;
655 }
656 /* Zero the new memory */
657 memset(new_elist + nbmem, 0,
658 (new_nbmem - nbmem) * sizeof(struct lttng_event));
659 nbmem = new_nbmem;
660 elist = new_elist;
661 }
662 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
663 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
664 elist[count].enabled = -1;
665 count++;
666 free(event);
667 }
668
669 *events = elist;
670 DBG("Kernel list events done (%zu events)", count);
671 end:
672 ret = fclose(fp); /* closes both fp and fd */
673 if (ret) {
674 PERROR("fclose");
675 }
676 return count;
677
678 error_fp:
679 ret = close(fd);
680 if (ret) {
681 PERROR("close");
682 }
683 error:
684 return -1;
685 }
686
687 /*
688 * Get kernel version and validate it.
689 */
690 int kernel_validate_version(int tracer_fd)
691 {
692 int ret;
693 struct lttng_kernel_tracer_version version;
694
695 ret = kernctl_tracer_version(tracer_fd, &version);
696 if (ret < 0) {
697 ERR("Failed at getting the lttng-modules version");
698 goto error;
699 }
700
701 /* Validate version */
702 if (version.major != KERN_MODULES_PRE_MAJOR
703 && version.major != KERN_MODULES_MAJOR) {
704 goto error_version;
705 }
706
707 DBG2("Kernel tracer version validated (major version %d)", version.major);
708 return 0;
709
710 error_version:
711 ERR("Kernel major version %d is not compatible (supporting <= %d)",
712 version.major, KERN_MODULES_MAJOR)
713 ret = -1;
714
715 error:
716 return ret;
717 }
718
719 /*
720 * Kernel work-arounds called at the start of sessiond main().
721 */
722 int init_kernel_workarounds(void)
723 {
724 int ret;
725 FILE *fp;
726
727 /*
728 * boot_id needs to be read once before being used concurrently
729 * to deal with a Linux kernel race. A fix is proposed for
730 * upstream, but the work-around is needed for older kernels.
731 */
732 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
733 if (!fp) {
734 goto end_boot_id;
735 }
736 while (!feof(fp)) {
737 char buf[37] = "";
738
739 ret = fread(buf, 1, sizeof(buf), fp);
740 if (ret < 0) {
741 /* Ignore error, we don't really care */
742 }
743 }
744 ret = fclose(fp);
745 if (ret) {
746 PERROR("fclose");
747 }
748 end_boot_id:
749 return 0;
750 }
751
752 /*
753 * Complete teardown of a kernel session.
754 */
755 void kernel_destroy_session(struct ltt_kernel_session *ksess)
756 {
757 if (ksess == NULL) {
758 DBG3("No kernel session when tearing down session");
759 return;
760 }
761
762 DBG("Tearing down kernel session");
763
764 /*
765 * Destroy channels on the consumer if at least one FD has been sent and we
766 * are in no output mode because the streams are in *no* monitor mode so we
767 * have to send a command to clean them up or else they leaked.
768 */
769 if (!ksess->output_traces && ksess->consumer_fds_sent) {
770 int ret;
771 struct consumer_socket *socket;
772 struct lttng_ht_iter iter;
773
774 /* For each consumer socket. */
775 rcu_read_lock();
776 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
777 socket, node.node) {
778 struct ltt_kernel_channel *chan;
779
780 /* For each channel, ask the consumer to destroy it. */
781 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
782 ret = kernel_consumer_destroy_channel(socket, chan);
783 if (ret < 0) {
784 /* Consumer is probably dead. Use next socket. */
785 continue;
786 }
787 }
788 }
789 rcu_read_unlock();
790 }
791
792 /* Close any relayd session */
793 consumer_output_send_destroy_relayd(ksess->consumer);
794
795 trace_kernel_destroy_session(ksess);
796 }
797
798 /*
799 * Destroy a kernel channel object. It does not do anything on the tracer side.
800 */
801 void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
802 {
803 struct ltt_kernel_session *ksess = NULL;
804
805 assert(kchan);
806 assert(kchan->channel);
807
808 DBG3("Kernel destroy channel %s", kchan->channel->name);
809
810 /* Update channel count of associated session. */
811 if (kchan->session) {
812 /* Keep pointer reference so we can update it after the destroy. */
813 ksess = kchan->session;
814 }
815
816 trace_kernel_destroy_channel(kchan);
817
818 /*
819 * At this point the kernel channel is not visible anymore. This is safe
820 * since in order to work on a visible kernel session, the tracing session
821 * lock (ltt_session.lock) MUST be acquired.
822 */
823 if (ksess) {
824 ksess->channel_count--;
825 }
826 }
827
828 /*
829 * Take a snapshot for a given kernel session.
830 *
831 * Return 0 on success or else return a LTTNG_ERR code.
832 */
833 int kernel_snapshot_record(struct ltt_kernel_session *ksess,
834 struct snapshot_output *output, int wait, unsigned int nb_streams)
835 {
836 int err, ret, saved_metadata_fd;
837 struct consumer_socket *socket;
838 struct lttng_ht_iter iter;
839 struct ltt_kernel_metadata *saved_metadata;
840 uint64_t max_size_per_stream = 0;
841
842 assert(ksess);
843 assert(ksess->consumer);
844 assert(output);
845
846 DBG("Kernel snapshot record started");
847
848 /* Save current metadata since the following calls will change it. */
849 saved_metadata = ksess->metadata;
850 saved_metadata_fd = ksess->metadata_stream_fd;
851
852 rcu_read_lock();
853
854 ret = kernel_open_metadata(ksess);
855 if (ret < 0) {
856 ret = LTTNG_ERR_KERN_META_FAIL;
857 goto error;
858 }
859
860 ret = kernel_open_metadata_stream(ksess);
861 if (ret < 0) {
862 ret = LTTNG_ERR_KERN_META_FAIL;
863 goto error_open_stream;
864 }
865
866 if (output->max_size > 0 && nb_streams > 0) {
867 max_size_per_stream = output->max_size / nb_streams;
868 }
869
870 /* Send metadata to consumer and snapshot everything. */
871 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
872 socket, node.node) {
873 struct consumer_output *saved_output;
874 struct ltt_kernel_channel *chan;
875
876 /*
877 * Temporarly switch consumer output for our snapshot output. As long
878 * as the session lock is taken, this is safe.
879 */
880 saved_output = ksess->consumer;
881 ksess->consumer = output->consumer;
882
883 pthread_mutex_lock(socket->lock);
884 /* This stream must not be monitored by the consumer. */
885 ret = kernel_consumer_add_metadata(socket, ksess, 0);
886 pthread_mutex_unlock(socket->lock);
887 /* Put back the saved consumer output into the session. */
888 ksess->consumer = saved_output;
889 if (ret < 0) {
890 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
891 goto error_consumer;
892 }
893
894 /* For each channel, ask the consumer to snapshot it. */
895 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
896 if (max_size_per_stream &&
897 chan->channel->attr.subbuf_size > max_size_per_stream) {
898 ret = LTTNG_ERR_INVALID;
899 DBG3("Kernel snapshot record maximum stream size %" PRIu64
900 " is smaller than subbuffer size of %" PRIu64,
901 max_size_per_stream, chan->channel->attr.subbuf_size);
902 (void) kernel_consumer_destroy_metadata(socket,
903 ksess->metadata);
904 goto error_consumer;
905 }
906
907 pthread_mutex_lock(socket->lock);
908 ret = consumer_snapshot_channel(socket, chan->fd, output, 0,
909 ksess->uid, ksess->gid,
910 DEFAULT_KERNEL_TRACE_DIR, wait,
911 max_size_per_stream);
912 pthread_mutex_unlock(socket->lock);
913 if (ret < 0) {
914 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
915 (void) kernel_consumer_destroy_metadata(socket,
916 ksess->metadata);
917 goto error_consumer;
918 }
919 }
920
921 /* Snapshot metadata, */
922 pthread_mutex_lock(socket->lock);
923 ret = consumer_snapshot_channel(socket, ksess->metadata->fd, output,
924 1, ksess->uid, ksess->gid,
925 DEFAULT_KERNEL_TRACE_DIR, wait, max_size_per_stream);
926 pthread_mutex_unlock(socket->lock);
927 if (ret < 0) {
928 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
929 goto error_consumer;
930 }
931
932 /*
933 * The metadata snapshot is done, ask the consumer to destroy it since
934 * it's not monitored on the consumer side.
935 */
936 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
937 }
938
939 ret = LTTNG_OK;
940
941 error_consumer:
942 /* Close newly opened metadata stream. It's now on the consumer side. */
943 err = close(ksess->metadata_stream_fd);
944 if (err < 0) {
945 PERROR("close snapshot kernel");
946 }
947
948 error_open_stream:
949 trace_kernel_destroy_metadata(ksess->metadata);
950 error:
951 /* Restore metadata state.*/
952 ksess->metadata = saved_metadata;
953 ksess->metadata_stream_fd = saved_metadata_fd;
954
955 rcu_read_unlock();
956 return ret;
957 }
This page took 0.058819 seconds and 4 git commands to generate.