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