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