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