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