24666f968d61da9b755eb0df52c31eb465de8158
[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/kernel-ctl/kernel-ioctl.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31
32 #include "consumer.h"
33 #include "kernel.h"
34 #include "kernel-consumer.h"
35 #include "kern-modules.h"
36 #include "utils.h"
37
38 /*
39 * Add context on a kernel channel.
40 */
41 int kernel_add_channel_context(struct ltt_kernel_channel *chan,
42 struct ltt_kernel_context *ctx)
43 {
44 int ret;
45
46 assert(chan);
47 assert(ctx);
48
49 DBG("Adding context to channel %s", chan->channel->name);
50 ret = kernctl_add_context(chan->fd, &ctx->ctx);
51 if (ret < 0) {
52 if (errno != EEXIST) {
53 PERROR("add context ioctl");
54 } else {
55 /* If EEXIST, we just ignore the error */
56 ret = 0;
57 }
58 goto error;
59 }
60
61 cds_list_add_tail(&ctx->list, &chan->ctx_list);
62
63 return 0;
64
65 error:
66 return ret;
67 }
68
69 /*
70 * Create a new kernel session, register it to the kernel tracer and add it to
71 * the session daemon session.
72 */
73 int kernel_create_session(struct ltt_session *session, int tracer_fd)
74 {
75 int ret;
76 struct ltt_kernel_session *lks;
77
78 assert(session);
79
80 /* Allocate data structure */
81 lks = trace_kernel_create_session();
82 if (lks == NULL) {
83 ret = -1;
84 goto error;
85 }
86
87 /* Kernel tracer session creation */
88 ret = kernctl_create_session(tracer_fd);
89 if (ret < 0) {
90 PERROR("ioctl kernel create session");
91 goto error;
92 }
93
94 lks->fd = ret;
95 /* Prevent fd duplication after execlp() */
96 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
97 if (ret < 0) {
98 PERROR("fcntl session fd");
99 }
100
101 lks->id = session->id;
102 lks->consumer_fds_sent = 0;
103 session->kernel_session = lks;
104
105 DBG("Kernel session created (fd: %d)", lks->fd);
106
107 return 0;
108
109 error:
110 if (lks) {
111 trace_kernel_destroy_session(lks);
112 }
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)
122 {
123 int ret;
124 struct ltt_kernel_channel *lkc;
125
126 assert(session);
127 assert(chan);
128
129 /* Allocate kernel channel */
130 lkc = trace_kernel_create_channel(chan);
131 if (lkc == NULL) {
132 goto error;
133 }
134
135 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
136 chan->name, lkc->channel->attr.overwrite,
137 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
138 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
139 lkc->channel->attr.live_timer_interval, lkc->channel->attr.output);
140
141 /* Kernel tracer channel creation */
142 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
143 if (ret < 0) {
144 PERROR("ioctl kernel create channel");
145 goto error;
146 }
147
148 /* Setup the channel fd */
149 lkc->fd = ret;
150 /* Prevent fd duplication after execlp() */
151 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
152 if (ret < 0) {
153 PERROR("fcntl session fd");
154 }
155
156 /* Add channel to session */
157 cds_list_add(&lkc->list, &session->channel_list.head);
158 session->channel_count++;
159 lkc->session = session;
160
161 DBG("Kernel channel %s created (fd: %d)", lkc->channel->name, lkc->fd);
162
163 return 0;
164
165 error:
166 if (lkc) {
167 free(lkc->channel);
168 free(lkc);
169 }
170 return -1;
171 }
172
173 /*
174 * Create a kernel event, enable it to the kernel tracer and add it to the
175 * channel event list of the kernel session.
176 * We own filter_expression and filter.
177 */
178 int kernel_create_event(struct lttng_event *ev,
179 struct ltt_kernel_channel *channel)
180 {
181 int ret;
182 struct ltt_kernel_event *event;
183
184 assert(ev);
185 assert(channel);
186
187 event = trace_kernel_create_event(ev);
188 if (event == NULL) {
189 ret = -1;
190 goto error;
191 }
192
193 ret = kernctl_create_event(channel->fd, event->event);
194 if (ret < 0) {
195 switch (errno) {
196 case EEXIST:
197 break;
198 case ENOSYS:
199 WARN("Event type not implemented");
200 break;
201 case ENOENT:
202 WARN("Event %s not found!", ev->name);
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 int kernel_enable_syscall(const char *syscall_name,
358 struct ltt_kernel_channel *channel)
359 {
360 return kernctl_enable_syscall(channel->fd, syscall_name);
361 }
362
363 int kernel_disable_syscall(const char *syscall_name,
364 struct ltt_kernel_channel *channel)
365 {
366 return kernctl_disable_syscall(channel->fd, syscall_name);
367 }
368
369 /*
370 * Create kernel metadata, open from the kernel tracer and add it to the
371 * kernel session.
372 */
373 int kernel_open_metadata(struct ltt_kernel_session *session)
374 {
375 int ret;
376 struct ltt_kernel_metadata *lkm = NULL;
377
378 assert(session);
379
380 /* Allocate kernel metadata */
381 lkm = trace_kernel_create_metadata();
382 if (lkm == NULL) {
383 goto error;
384 }
385
386 /* Kernel tracer metadata creation */
387 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
388 if (ret < 0) {
389 goto error_open;
390 }
391
392 lkm->fd = ret;
393 /* Prevent fd duplication after execlp() */
394 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
395 if (ret < 0) {
396 PERROR("fcntl session fd");
397 }
398
399 session->metadata = lkm;
400
401 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
402
403 return 0;
404
405 error_open:
406 trace_kernel_destroy_metadata(lkm);
407 error:
408 return -1;
409 }
410
411 /*
412 * Start tracing session.
413 */
414 int kernel_start_session(struct ltt_kernel_session *session)
415 {
416 int ret;
417
418 assert(session);
419
420 ret = kernctl_start_session(session->fd);
421 if (ret < 0) {
422 PERROR("ioctl start session");
423 goto error;
424 }
425
426 DBG("Kernel session started");
427
428 return 0;
429
430 error:
431 return ret;
432 }
433
434 /*
435 * Make a kernel wait to make sure in-flight probe have completed.
436 */
437 void kernel_wait_quiescent(int fd)
438 {
439 int ret;
440
441 DBG("Kernel quiescent wait on %d", fd);
442
443 ret = kernctl_wait_quiescent(fd);
444 if (ret < 0) {
445 PERROR("wait quiescent ioctl");
446 ERR("Kernel quiescent wait failed");
447 }
448 }
449
450 /*
451 * Kernel calibrate
452 */
453 int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
454 {
455 int ret;
456
457 assert(calibrate);
458
459 ret = kernctl_calibrate(fd, calibrate);
460 if (ret < 0) {
461 PERROR("calibrate ioctl");
462 return -1;
463 }
464
465 return 0;
466 }
467
468
469 /*
470 * Force flush buffer of metadata.
471 */
472 int kernel_metadata_flush_buffer(int fd)
473 {
474 int ret;
475
476 DBG("Kernel flushing metadata buffer on fd %d", fd);
477
478 ret = kernctl_buffer_flush(fd);
479 if (ret < 0) {
480 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
481 }
482
483 return 0;
484 }
485
486 /*
487 * Force flush buffer for channel.
488 */
489 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
490 {
491 int ret;
492 struct ltt_kernel_stream *stream;
493
494 assert(channel);
495
496 DBG("Flush buffer for channel %s", channel->channel->name);
497
498 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
499 DBG("Flushing channel stream %d", stream->fd);
500 ret = kernctl_buffer_flush(stream->fd);
501 if (ret < 0) {
502 PERROR("ioctl");
503 ERR("Fail to flush buffer for stream %d (ret: %d)",
504 stream->fd, ret);
505 }
506 }
507
508 return 0;
509 }
510
511 /*
512 * Stop tracing session.
513 */
514 int kernel_stop_session(struct ltt_kernel_session *session)
515 {
516 int ret;
517
518 assert(session);
519
520 ret = kernctl_stop_session(session->fd);
521 if (ret < 0) {
522 goto error;
523 }
524
525 DBG("Kernel session stopped");
526
527 return 0;
528
529 error:
530 return ret;
531 }
532
533 /*
534 * Open stream of channel, register it to the kernel tracer and add it
535 * to the stream list of the channel.
536 *
537 * Return the number of created stream. Else, a negative value.
538 */
539 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
540 {
541 int ret, count = 0;
542 struct ltt_kernel_stream *lks;
543
544 assert(channel);
545
546 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
547 lks = trace_kernel_create_stream(channel->channel->name, count);
548 if (lks == NULL) {
549 ret = close(ret);
550 if (ret) {
551 PERROR("close");
552 }
553 goto error;
554 }
555
556 lks->fd = ret;
557 /* Prevent fd duplication after execlp() */
558 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
559 if (ret < 0) {
560 PERROR("fcntl session fd");
561 }
562
563 lks->tracefile_size = channel->channel->attr.tracefile_size;
564 lks->tracefile_count = channel->channel->attr.tracefile_count;
565
566 /* Add stream to channe stream list */
567 cds_list_add(&lks->list, &channel->stream_list.head);
568 channel->stream_count++;
569
570 /* Increment counter which represent CPU number. */
571 count++;
572
573 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
574 lks->state);
575 }
576
577 return channel->stream_count;
578
579 error:
580 return -1;
581 }
582
583 /*
584 * Open the metadata stream and set it to the kernel session.
585 */
586 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
587 {
588 int ret;
589
590 assert(session);
591
592 ret = kernctl_create_stream(session->metadata->fd);
593 if (ret < 0) {
594 PERROR("kernel create metadata stream");
595 goto error;
596 }
597
598 DBG("Kernel metadata stream created (fd: %d)", ret);
599 session->metadata_stream_fd = ret;
600 /* Prevent fd duplication after execlp() */
601 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
602 if (ret < 0) {
603 PERROR("fcntl session fd");
604 }
605
606 return 0;
607
608 error:
609 return -1;
610 }
611
612 /*
613 * Get the event list from the kernel tracer and return the number of elements.
614 */
615 ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
616 {
617 int fd, ret;
618 char *event;
619 size_t nbmem, count = 0;
620 FILE *fp;
621 struct lttng_event *elist;
622
623 assert(events);
624
625 fd = kernctl_tracepoint_list(tracer_fd);
626 if (fd < 0) {
627 PERROR("kernel tracepoint list");
628 goto error;
629 }
630
631 fp = fdopen(fd, "r");
632 if (fp == NULL) {
633 PERROR("kernel tracepoint list fdopen");
634 goto error_fp;
635 }
636
637 /*
638 * Init memory size counter
639 * See kernel-ctl.h for explanation of this value
640 */
641 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
642 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
643 if (elist == NULL) {
644 PERROR("alloc list events");
645 count = -ENOMEM;
646 goto end;
647 }
648
649 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
650 if (count >= nbmem) {
651 struct lttng_event *new_elist;
652 size_t new_nbmem;
653
654 new_nbmem = nbmem << 1;
655 DBG("Reallocating event list from %zu to %zu bytes",
656 nbmem, new_nbmem);
657 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
658 if (new_elist == NULL) {
659 PERROR("realloc list events");
660 free(event);
661 free(elist);
662 count = -ENOMEM;
663 goto end;
664 }
665 /* Zero the new memory */
666 memset(new_elist + nbmem, 0,
667 (new_nbmem - nbmem) * sizeof(struct lttng_event));
668 nbmem = new_nbmem;
669 elist = new_elist;
670 }
671 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
672 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
673 elist[count].enabled = -1;
674 count++;
675 free(event);
676 }
677
678 *events = elist;
679 DBG("Kernel list events done (%zu events)", count);
680 end:
681 ret = fclose(fp); /* closes both fp and fd */
682 if (ret) {
683 PERROR("fclose");
684 }
685 return count;
686
687 error_fp:
688 ret = close(fd);
689 if (ret) {
690 PERROR("close");
691 }
692 error:
693 return -1;
694 }
695
696 /*
697 * Get kernel version and validate it.
698 */
699 int kernel_validate_version(int tracer_fd)
700 {
701 int ret;
702 struct lttng_kernel_tracer_version version;
703 struct lttng_kernel_tracer_abi_version abi_version;
704
705 ret = kernctl_tracer_version(tracer_fd, &version);
706 if (ret < 0) {
707 ERR("Failed at getting the lttng-modules version");
708 goto error;
709 }
710
711 /* Validate version */
712 if (version.major != VERSION_MAJOR) {
713 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
714 version.major, VERSION_MAJOR);
715 goto error_version;
716 }
717 ret = kernctl_tracer_abi_version(tracer_fd, &abi_version);
718 if (ret < 0) {
719 ERR("Failed at getting lttng-modules ABI version");
720 goto error;
721 }
722 if (abi_version.major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
723 ERR("Kernel tracer ABI version (%d.%d) is not compatible with expected ABI major version (%d.*)",
724 abi_version.major, abi_version.minor,
725 LTTNG_MODULES_ABI_MAJOR_VERSION);
726 goto error;
727 }
728 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
729 version.major, version.minor,
730 abi_version.major, abi_version.minor);
731 return 0;
732
733 error_version:
734 ret = -1;
735
736 error:
737 return ret;
738 }
739
740 /*
741 * Kernel work-arounds called at the start of sessiond main().
742 */
743 int init_kernel_workarounds(void)
744 {
745 int ret;
746 FILE *fp;
747
748 /*
749 * boot_id needs to be read once before being used concurrently
750 * to deal with a Linux kernel race. A fix is proposed for
751 * upstream, but the work-around is needed for older kernels.
752 */
753 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
754 if (!fp) {
755 goto end_boot_id;
756 }
757 while (!feof(fp)) {
758 char buf[37] = "";
759
760 ret = fread(buf, 1, sizeof(buf), fp);
761 if (ret < 0) {
762 /* Ignore error, we don't really care */
763 }
764 }
765 ret = fclose(fp);
766 if (ret) {
767 PERROR("fclose");
768 }
769 end_boot_id:
770 return 0;
771 }
772
773 /*
774 * Complete teardown of a kernel session.
775 */
776 void kernel_destroy_session(struct ltt_kernel_session *ksess)
777 {
778 if (ksess == NULL) {
779 DBG3("No kernel session when tearing down session");
780 return;
781 }
782
783 DBG("Tearing down kernel session");
784
785 /*
786 * Destroy channels on the consumer if at least one FD has been sent and we
787 * are in no output mode because the streams are in *no* monitor mode so we
788 * have to send a command to clean them up or else they leaked.
789 */
790 if (!ksess->output_traces && ksess->consumer_fds_sent) {
791 int ret;
792 struct consumer_socket *socket;
793 struct lttng_ht_iter iter;
794
795 /* For each consumer socket. */
796 rcu_read_lock();
797 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
798 socket, node.node) {
799 struct ltt_kernel_channel *chan;
800
801 /* For each channel, ask the consumer to destroy it. */
802 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
803 ret = kernel_consumer_destroy_channel(socket, chan);
804 if (ret < 0) {
805 /* Consumer is probably dead. Use next socket. */
806 continue;
807 }
808 }
809 }
810 rcu_read_unlock();
811 }
812
813 /* Close any relayd session */
814 consumer_output_send_destroy_relayd(ksess->consumer);
815
816 trace_kernel_destroy_session(ksess);
817 }
818
819 /*
820 * Destroy a kernel channel object. It does not do anything on the tracer side.
821 */
822 void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
823 {
824 struct ltt_kernel_session *ksess = NULL;
825
826 assert(kchan);
827 assert(kchan->channel);
828
829 DBG3("Kernel destroy channel %s", kchan->channel->name);
830
831 /* Update channel count of associated session. */
832 if (kchan->session) {
833 /* Keep pointer reference so we can update it after the destroy. */
834 ksess = kchan->session;
835 }
836
837 trace_kernel_destroy_channel(kchan);
838
839 /*
840 * At this point the kernel channel is not visible anymore. This is safe
841 * since in order to work on a visible kernel session, the tracing session
842 * lock (ltt_session.lock) MUST be acquired.
843 */
844 if (ksess) {
845 ksess->channel_count--;
846 }
847 }
848
849 /*
850 * Take a snapshot for a given kernel session.
851 *
852 * Return 0 on success or else return a LTTNG_ERR code.
853 */
854 int kernel_snapshot_record(struct ltt_kernel_session *ksess,
855 struct snapshot_output *output, int wait,
856 uint64_t nb_packets_per_stream)
857 {
858 int err, ret, saved_metadata_fd;
859 struct consumer_socket *socket;
860 struct lttng_ht_iter iter;
861 struct ltt_kernel_metadata *saved_metadata;
862
863 assert(ksess);
864 assert(ksess->consumer);
865 assert(output);
866
867 DBG("Kernel snapshot record started");
868
869 /* Save current metadata since the following calls will change it. */
870 saved_metadata = ksess->metadata;
871 saved_metadata_fd = ksess->metadata_stream_fd;
872
873 rcu_read_lock();
874
875 ret = kernel_open_metadata(ksess);
876 if (ret < 0) {
877 ret = LTTNG_ERR_KERN_META_FAIL;
878 goto error;
879 }
880
881 ret = kernel_open_metadata_stream(ksess);
882 if (ret < 0) {
883 ret = LTTNG_ERR_KERN_META_FAIL;
884 goto error_open_stream;
885 }
886
887 /* Send metadata to consumer and snapshot everything. */
888 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
889 socket, node.node) {
890 struct consumer_output *saved_output;
891 struct ltt_kernel_channel *chan;
892
893 /*
894 * Temporarly switch consumer output for our snapshot output. As long
895 * as the session lock is taken, this is safe.
896 */
897 saved_output = ksess->consumer;
898 ksess->consumer = output->consumer;
899
900 pthread_mutex_lock(socket->lock);
901 /* This stream must not be monitored by the consumer. */
902 ret = kernel_consumer_add_metadata(socket, ksess, 0);
903 pthread_mutex_unlock(socket->lock);
904 /* Put back the saved consumer output into the session. */
905 ksess->consumer = saved_output;
906 if (ret < 0) {
907 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
908 goto error_consumer;
909 }
910
911 /* For each channel, ask the consumer to snapshot it. */
912 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
913 pthread_mutex_lock(socket->lock);
914 ret = consumer_snapshot_channel(socket, chan->fd, output, 0,
915 ksess->uid, ksess->gid,
916 DEFAULT_KERNEL_TRACE_DIR, wait,
917 nb_packets_per_stream);
918 pthread_mutex_unlock(socket->lock);
919 if (ret < 0) {
920 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
921 (void) kernel_consumer_destroy_metadata(socket,
922 ksess->metadata);
923 goto error_consumer;
924 }
925 }
926
927 /* Snapshot metadata, */
928 pthread_mutex_lock(socket->lock);
929 ret = consumer_snapshot_channel(socket, ksess->metadata->fd, output,
930 1, ksess->uid, ksess->gid,
931 DEFAULT_KERNEL_TRACE_DIR, wait, 0);
932 pthread_mutex_unlock(socket->lock);
933 if (ret < 0) {
934 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
935 goto error_consumer;
936 }
937
938 /*
939 * The metadata snapshot is done, ask the consumer to destroy it since
940 * it's not monitored on the consumer side.
941 */
942 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
943 }
944
945 ret = LTTNG_OK;
946
947 error_consumer:
948 /* Close newly opened metadata stream. It's now on the consumer side. */
949 err = close(ksess->metadata_stream_fd);
950 if (err < 0) {
951 PERROR("close snapshot kernel");
952 }
953
954 error_open_stream:
955 trace_kernel_destroy_metadata(ksess->metadata);
956 error:
957 /* Restore metadata state.*/
958 ksess->metadata = saved_metadata;
959 ksess->metadata_stream_fd = saved_metadata_fd;
960
961 rcu_read_unlock();
962 return ret;
963 }
964
965 /*
966 * Get the syscall mask array from the kernel tracer.
967 *
968 * Return 0 on success else a negative value. In both case, syscall_mask should
969 * be freed.
970 */
971 int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
972 {
973 assert(syscall_mask);
974 assert(nr_bits);
975
976 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
977 }
This page took 0.047963 seconds and 3 git commands to generate.