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