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