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