Fix: rename of perf counter context
[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 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, 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", &event) == 1) {
638 if (count >= nbmem) {
639 struct lttng_event *new_elist;
640 size_t new_nbmem;
641
642 new_nbmem = nbmem << 1;
643 DBG("Reallocating event list from %zu to %zu bytes",
644 nbmem, new_nbmem);
645 new_elist = realloc(elist, new_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 /* Zero the new memory */
654 memset(new_elist + nbmem, 0,
655 (new_nbmem - nbmem) * sizeof(struct lttng_event));
656 nbmem = new_nbmem;
657 elist = new_elist;
658 }
659 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
660 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
661 elist[count].enabled = -1;
662 count++;
663 free(event);
664 }
665
666 *events = elist;
667 DBG("Kernel list events done (%zu events)", count);
668 end:
669 ret = fclose(fp); /* closes both fp and fd */
670 if (ret) {
671 PERROR("fclose");
672 }
673 return count;
674
675 error_fp:
676 ret = close(fd);
677 if (ret) {
678 PERROR("close");
679 }
680 error:
681 return -1;
682 }
683
684 /*
685 * Get kernel version and validate it.
686 */
687 int kernel_validate_version(int tracer_fd)
688 {
689 int ret;
690 struct lttng_kernel_tracer_version version;
691
692 ret = kernctl_tracer_version(tracer_fd, &version);
693 if (ret < 0) {
694 ERR("Failed at getting the lttng-modules version");
695 goto error;
696 }
697
698 /* Validate version */
699 if (version.major != KERN_MODULES_PRE_MAJOR
700 && version.major != KERN_MODULES_MAJOR) {
701 goto error_version;
702 }
703
704 DBG2("Kernel tracer version validated (major version %d)", version.major);
705 return 0;
706
707 error_version:
708 ERR("Kernel major version %d is not compatible (supporting <= %d)",
709 version.major, KERN_MODULES_MAJOR)
710 ret = -1;
711
712 error:
713 return ret;
714 }
715
716 /*
717 * Kernel work-arounds called at the start of sessiond main().
718 */
719 int init_kernel_workarounds(void)
720 {
721 int ret;
722 FILE *fp;
723
724 /*
725 * boot_id needs to be read once before being used concurrently
726 * to deal with a Linux kernel race. A fix is proposed for
727 * upstream, but the work-around is needed for older kernels.
728 */
729 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
730 if (!fp) {
731 goto end_boot_id;
732 }
733 while (!feof(fp)) {
734 char buf[37] = "";
735
736 ret = fread(buf, 1, sizeof(buf), fp);
737 if (ret < 0) {
738 /* Ignore error, we don't really care */
739 }
740 }
741 ret = fclose(fp);
742 if (ret) {
743 PERROR("fclose");
744 }
745 end_boot_id:
746 return 0;
747 }
748
749 /*
750 * Complete teardown of a kernel session.
751 */
752 void kernel_destroy_session(struct ltt_kernel_session *ksess)
753 {
754 if (ksess == NULL) {
755 DBG3("No kernel session when tearing down session");
756 return;
757 }
758
759 DBG("Tearing down kernel session");
760
761 /*
762 * Destroy channels on the consumer if at least one FD has been sent and we
763 * are in no output mode because the streams are in *no* monitor mode so we
764 * have to send a command to clean them up or else they leaked.
765 */
766 if (!ksess->output_traces && ksess->consumer_fds_sent) {
767 int ret;
768 struct consumer_socket *socket;
769 struct lttng_ht_iter iter;
770
771 /* For each consumer socket. */
772 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
773 socket, node.node) {
774 struct ltt_kernel_channel *chan;
775
776 /* For each channel, ask the consumer to destroy it. */
777 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
778 ret = kernel_consumer_destroy_channel(socket, chan);
779 if (ret < 0) {
780 /* Consumer is probably dead. Use next socket. */
781 continue;
782 }
783 }
784 }
785 }
786
787 /* Close any relayd session */
788 consumer_output_send_destroy_relayd(ksess->consumer);
789
790 trace_kernel_destroy_session(ksess);
791 }
792
793 /*
794 * Destroy a kernel channel object. It does not do anything on the tracer side.
795 */
796 void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
797 {
798 struct ltt_kernel_session *ksess = NULL;
799
800 assert(kchan);
801 assert(kchan->channel);
802
803 DBG3("Kernel destroy channel %s", kchan->channel->name);
804
805 /* Update channel count of associated session. */
806 if (kchan->session) {
807 /* Keep pointer reference so we can update it after the destroy. */
808 ksess = kchan->session;
809 }
810
811 trace_kernel_destroy_channel(kchan);
812
813 /*
814 * At this point the kernel channel is not visible anymore. This is safe
815 * since in order to work on a visible kernel session, the tracing session
816 * lock (ltt_session.lock) MUST be acquired.
817 */
818 if (ksess) {
819 ksess->channel_count--;
820 }
821 }
822
823 /*
824 * Take a snapshot for a given kernel session.
825 *
826 * Return 0 on success or else return a LTTNG_ERR code.
827 */
828 int kernel_snapshot_record(struct ltt_kernel_session *ksess,
829 struct snapshot_output *output, int wait, unsigned int nb_streams)
830 {
831 int err, ret, saved_metadata_fd;
832 struct consumer_socket *socket;
833 struct lttng_ht_iter iter;
834 struct ltt_kernel_metadata *saved_metadata;
835 uint64_t max_size_per_stream = 0;
836
837 assert(ksess);
838 assert(ksess->consumer);
839 assert(output);
840
841 DBG("Kernel snapshot record started");
842
843 /* Save current metadata since the following calls will change it. */
844 saved_metadata = ksess->metadata;
845 saved_metadata_fd = ksess->metadata_stream_fd;
846
847 rcu_read_lock();
848
849 ret = kernel_open_metadata(ksess);
850 if (ret < 0) {
851 ret = LTTNG_ERR_KERN_META_FAIL;
852 goto error;
853 }
854
855 ret = kernel_open_metadata_stream(ksess);
856 if (ret < 0) {
857 ret = LTTNG_ERR_KERN_META_FAIL;
858 goto error_open_stream;
859 }
860
861 if (output->max_size > 0 && nb_streams > 0) {
862 max_size_per_stream = output->max_size / nb_streams;
863 }
864
865 /* Send metadata to consumer and snapshot everything. */
866 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
867 socket, node.node) {
868 struct consumer_output *saved_output;
869 struct ltt_kernel_channel *chan;
870
871 /*
872 * Temporarly switch consumer output for our snapshot output. As long
873 * as the session lock is taken, this is safe.
874 */
875 saved_output = ksess->consumer;
876 ksess->consumer = output->consumer;
877
878 pthread_mutex_lock(socket->lock);
879 /* This stream must not be monitored by the consumer. */
880 ret = kernel_consumer_add_metadata(socket, ksess, 0);
881 pthread_mutex_unlock(socket->lock);
882 /* Put back the saved consumer output into the session. */
883 ksess->consumer = saved_output;
884 if (ret < 0) {
885 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
886 goto error_consumer;
887 }
888
889 /* For each channel, ask the consumer to snapshot it. */
890 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
891 if (max_size_per_stream &&
892 chan->channel->attr.subbuf_size > max_size_per_stream) {
893 ret = LTTNG_ERR_INVALID;
894 DBG3("Kernel snapshot record maximum stream size %" PRIu64
895 " is smaller than subbuffer size of %" PRIu64,
896 max_size_per_stream, chan->channel->attr.subbuf_size);
897 (void) kernel_consumer_destroy_metadata(socket,
898 ksess->metadata);
899 goto error_consumer;
900 }
901
902 pthread_mutex_lock(socket->lock);
903 ret = consumer_snapshot_channel(socket, chan->fd, output, 0,
904 ksess->uid, ksess->gid,
905 DEFAULT_KERNEL_TRACE_DIR, wait,
906 max_size_per_stream);
907 pthread_mutex_unlock(socket->lock);
908 if (ret < 0) {
909 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
910 (void) kernel_consumer_destroy_metadata(socket,
911 ksess->metadata);
912 goto error_consumer;
913 }
914 }
915
916 /* Snapshot metadata, */
917 pthread_mutex_lock(socket->lock);
918 ret = consumer_snapshot_channel(socket, ksess->metadata->fd, output,
919 1, ksess->uid, ksess->gid,
920 DEFAULT_KERNEL_TRACE_DIR, wait, max_size_per_stream);
921 pthread_mutex_unlock(socket->lock);
922 if (ret < 0) {
923 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
924 goto error_consumer;
925 }
926
927 /*
928 * The metadata snapshot is done, ask the consumer to destroy it since
929 * it's not monitored on the consumer side.
930 */
931 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
932 }
933
934 ret = LTTNG_OK;
935
936 error_consumer:
937 /* Close newly opened metadata stream. It's now on the consumer side. */
938 err = close(ksess->metadata_stream_fd);
939 if (err < 0) {
940 PERROR("close snapshot kernel");
941 }
942
943 error_open_stream:
944 trace_kernel_destroy_metadata(ksess->metadata);
945 error:
946 /* Restore metadata state.*/
947 ksess->metadata = saved_metadata;
948 ksess->metadata_stream_fd = saved_metadata_fd;
949
950 rcu_read_unlock();
951 return ret;
952 }
This page took 0.049853 seconds and 4 git commands to generate.