Clean-up: harmonize kernctl API error checking
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.c
... / ...
CommitLineData
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 */
40int 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
67end:
68 cds_list_add_tail(&ctx->list, &chan->ctx_list);
69 return 0;
70
71error:
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 */
79int 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
115error:
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 */
126int 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
171error:
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 */
184int 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
256enable_error:
257filter_error:
258 {
259 int closeret;
260
261 closeret = close(event->fd);
262 if (closeret) {
263 PERROR("close event fd");
264 }
265 }
266free_event:
267 free(event);
268error:
269 return ret;
270}
271
272/*
273 * Disable a kernel channel.
274 */
275int 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
292error:
293 return ret;
294}
295
296/*
297 * Enable a kernel channel.
298 */
299int 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
316error:
317 return ret;
318}
319
320/*
321 * Enable a kernel event.
322 */
323int 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
347error:
348 return ret;
349}
350
351/*
352 * Disable a kernel event.
353 */
354int 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
378error:
379 return ret;
380}
381
382
383int 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
405int 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
427ssize_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);
482end:
483 ret = fclose(fp); /* closes both fp and fd */
484 if (ret) {
485 PERROR("fclose");
486 }
487 return count;
488
489error_fp:
490 ret = close(fd);
491 if (ret) {
492 PERROR("close");
493 }
494error:
495 return -1;
496}
497
498/*
499 * Create kernel metadata, open from the kernel tracer and add it to the
500 * kernel session.
501 */
502int 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
534error_open:
535 trace_kernel_destroy_metadata(lkm);
536error:
537 return -1;
538}
539
540/*
541 * Start tracing session.
542 */
543int 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
559error:
560 return ret;
561}
562
563/*
564 * Make a kernel wait to make sure in-flight probe have completed.
565 */
566void 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 * Kernel calibrate
581 */
582int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
583{
584 int ret;
585
586 assert(calibrate);
587
588 ret = kernctl_calibrate(fd, calibrate);
589 if (ret < 0) {
590 PERROR("calibrate ioctl");
591 return -1;
592 }
593
594 return 0;
595}
596
597
598/*
599 * Force flush buffer of metadata.
600 */
601int kernel_metadata_flush_buffer(int fd)
602{
603 int ret;
604
605 DBG("Kernel flushing metadata buffer on fd %d", fd);
606
607 ret = kernctl_buffer_flush(fd);
608 if (ret < 0) {
609 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
610 }
611
612 return 0;
613}
614
615/*
616 * Force flush buffer for channel.
617 */
618int kernel_flush_buffer(struct ltt_kernel_channel *channel)
619{
620 int ret;
621 struct ltt_kernel_stream *stream;
622
623 assert(channel);
624
625 DBG("Flush buffer for channel %s", channel->channel->name);
626
627 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
628 DBG("Flushing channel stream %d", stream->fd);
629 ret = kernctl_buffer_flush(stream->fd);
630 if (ret < 0) {
631 PERROR("ioctl");
632 ERR("Fail to flush buffer for stream %d (ret: %d)",
633 stream->fd, ret);
634 }
635 }
636
637 return 0;
638}
639
640/*
641 * Stop tracing session.
642 */
643int kernel_stop_session(struct ltt_kernel_session *session)
644{
645 int ret;
646
647 assert(session);
648
649 ret = kernctl_stop_session(session->fd);
650 if (ret < 0) {
651 goto error;
652 }
653
654 DBG("Kernel session stopped");
655
656 return 0;
657
658error:
659 return ret;
660}
661
662/*
663 * Open stream of channel, register it to the kernel tracer and add it
664 * to the stream list of the channel.
665 *
666 * Return the number of created stream. Else, a negative value.
667 */
668int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
669{
670 int ret, count = 0;
671 struct ltt_kernel_stream *lks;
672
673 assert(channel);
674
675 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
676 lks = trace_kernel_create_stream(channel->channel->name, count);
677 if (lks == NULL) {
678 ret = close(ret);
679 if (ret) {
680 PERROR("close");
681 }
682 goto error;
683 }
684
685 lks->fd = ret;
686 /* Prevent fd duplication after execlp() */
687 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
688 if (ret < 0) {
689 PERROR("fcntl session fd");
690 }
691
692 lks->tracefile_size = channel->channel->attr.tracefile_size;
693 lks->tracefile_count = channel->channel->attr.tracefile_count;
694
695 /* Add stream to channe stream list */
696 cds_list_add(&lks->list, &channel->stream_list.head);
697 channel->stream_count++;
698
699 /* Increment counter which represent CPU number. */
700 count++;
701
702 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
703 lks->state);
704 }
705
706 return channel->stream_count;
707
708error:
709 return -1;
710}
711
712/*
713 * Open the metadata stream and set it to the kernel session.
714 */
715int kernel_open_metadata_stream(struct ltt_kernel_session *session)
716{
717 int ret;
718
719 assert(session);
720
721 ret = kernctl_create_stream(session->metadata->fd);
722 if (ret < 0) {
723 PERROR("kernel create metadata stream");
724 goto error;
725 }
726
727 DBG("Kernel metadata stream created (fd: %d)", ret);
728 session->metadata_stream_fd = ret;
729 /* Prevent fd duplication after execlp() */
730 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
731 if (ret < 0) {
732 PERROR("fcntl session fd");
733 }
734
735 return 0;
736
737error:
738 return -1;
739}
740
741/*
742 * Get the event list from the kernel tracer and return the number of elements.
743 */
744ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
745{
746 int fd, ret;
747 char *event;
748 size_t nbmem, count = 0;
749 FILE *fp;
750 struct lttng_event *elist;
751
752 assert(events);
753
754 fd = kernctl_tracepoint_list(tracer_fd);
755 if (fd < 0) {
756 PERROR("kernel tracepoint list");
757 goto error;
758 }
759
760 fp = fdopen(fd, "r");
761 if (fp == NULL) {
762 PERROR("kernel tracepoint list fdopen");
763 goto error_fp;
764 }
765
766 /*
767 * Init memory size counter
768 * See kernel-ctl.h for explanation of this value
769 */
770 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
771 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
772 if (elist == NULL) {
773 PERROR("alloc list events");
774 count = -ENOMEM;
775 goto end;
776 }
777
778 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
779 if (count >= nbmem) {
780 struct lttng_event *new_elist;
781 size_t new_nbmem;
782
783 new_nbmem = nbmem << 1;
784 DBG("Reallocating event list from %zu to %zu bytes",
785 nbmem, new_nbmem);
786 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
787 if (new_elist == NULL) {
788 PERROR("realloc list events");
789 free(event);
790 free(elist);
791 count = -ENOMEM;
792 goto end;
793 }
794 /* Zero the new memory */
795 memset(new_elist + nbmem, 0,
796 (new_nbmem - nbmem) * sizeof(struct lttng_event));
797 nbmem = new_nbmem;
798 elist = new_elist;
799 }
800 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
801 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
802 elist[count].enabled = -1;
803 count++;
804 free(event);
805 }
806
807 *events = elist;
808 DBG("Kernel list events done (%zu events)", count);
809end:
810 ret = fclose(fp); /* closes both fp and fd */
811 if (ret) {
812 PERROR("fclose");
813 }
814 return count;
815
816error_fp:
817 ret = close(fd);
818 if (ret) {
819 PERROR("close");
820 }
821error:
822 return -1;
823}
824
825/*
826 * Get kernel version and validate it.
827 */
828int kernel_validate_version(int tracer_fd)
829{
830 int ret;
831 struct lttng_kernel_tracer_version version;
832 struct lttng_kernel_tracer_abi_version abi_version;
833
834 ret = kernctl_tracer_version(tracer_fd, &version);
835 if (ret < 0) {
836 ERR("Failed to retrieve the lttng-modules version");
837 goto error;
838 }
839
840 /* Validate version */
841 if (version.major != VERSION_MAJOR) {
842 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
843 version.major, VERSION_MAJOR);
844 goto error_version;
845 }
846 ret = kernctl_tracer_abi_version(tracer_fd, &abi_version);
847 if (ret < 0) {
848 ERR("Failed to retrieve lttng-modules ABI version");
849 goto error;
850 }
851 if (abi_version.major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
852 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
853 abi_version.major, abi_version.minor,
854 LTTNG_MODULES_ABI_MAJOR_VERSION);
855 goto error;
856 }
857 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
858 version.major, version.minor,
859 abi_version.major, abi_version.minor);
860 return 0;
861
862error_version:
863 ret = -1;
864
865error:
866 ERR("Kernel tracer version check failed; kernel tracing will not be available");
867 return ret;
868}
869
870/*
871 * Kernel work-arounds called at the start of sessiond main().
872 */
873int init_kernel_workarounds(void)
874{
875 int ret;
876 FILE *fp;
877
878 /*
879 * boot_id needs to be read once before being used concurrently
880 * to deal with a Linux kernel race. A fix is proposed for
881 * upstream, but the work-around is needed for older kernels.
882 */
883 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
884 if (!fp) {
885 goto end_boot_id;
886 }
887 while (!feof(fp)) {
888 char buf[37] = "";
889
890 ret = fread(buf, 1, sizeof(buf), fp);
891 if (ret < 0) {
892 /* Ignore error, we don't really care */
893 }
894 }
895 ret = fclose(fp);
896 if (ret) {
897 PERROR("fclose");
898 }
899end_boot_id:
900 return 0;
901}
902
903/*
904 * Complete teardown of a kernel session.
905 */
906void kernel_destroy_session(struct ltt_kernel_session *ksess)
907{
908 if (ksess == NULL) {
909 DBG3("No kernel session when tearing down session");
910 return;
911 }
912
913 DBG("Tearing down kernel session");
914
915 /*
916 * Destroy channels on the consumer if at least one FD has been sent and we
917 * are in no output mode because the streams are in *no* monitor mode so we
918 * have to send a command to clean them up or else they leaked.
919 */
920 if (!ksess->output_traces && ksess->consumer_fds_sent) {
921 int ret;
922 struct consumer_socket *socket;
923 struct lttng_ht_iter iter;
924
925 /* For each consumer socket. */
926 rcu_read_lock();
927 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
928 socket, node.node) {
929 struct ltt_kernel_channel *chan;
930
931 /* For each channel, ask the consumer to destroy it. */
932 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
933 ret = kernel_consumer_destroy_channel(socket, chan);
934 if (ret < 0) {
935 /* Consumer is probably dead. Use next socket. */
936 continue;
937 }
938 }
939 }
940 rcu_read_unlock();
941 }
942
943 /* Close any relayd session */
944 consumer_output_send_destroy_relayd(ksess->consumer);
945
946 trace_kernel_destroy_session(ksess);
947}
948
949/*
950 * Destroy a kernel channel object. It does not do anything on the tracer side.
951 */
952void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
953{
954 struct ltt_kernel_session *ksess = NULL;
955
956 assert(kchan);
957 assert(kchan->channel);
958
959 DBG3("Kernel destroy channel %s", kchan->channel->name);
960
961 /* Update channel count of associated session. */
962 if (kchan->session) {
963 /* Keep pointer reference so we can update it after the destroy. */
964 ksess = kchan->session;
965 }
966
967 trace_kernel_destroy_channel(kchan);
968
969 /*
970 * At this point the kernel channel is not visible anymore. This is safe
971 * since in order to work on a visible kernel session, the tracing session
972 * lock (ltt_session.lock) MUST be acquired.
973 */
974 if (ksess) {
975 ksess->channel_count--;
976 }
977}
978
979/*
980 * Take a snapshot for a given kernel session.
981 *
982 * Return 0 on success or else return a LTTNG_ERR code.
983 */
984int kernel_snapshot_record(struct ltt_kernel_session *ksess,
985 struct snapshot_output *output, int wait,
986 uint64_t nb_packets_per_stream)
987{
988 int err, ret, saved_metadata_fd;
989 struct consumer_socket *socket;
990 struct lttng_ht_iter iter;
991 struct ltt_kernel_metadata *saved_metadata;
992
993 assert(ksess);
994 assert(ksess->consumer);
995 assert(output);
996
997 DBG("Kernel snapshot record started");
998
999 /* Save current metadata since the following calls will change it. */
1000 saved_metadata = ksess->metadata;
1001 saved_metadata_fd = ksess->metadata_stream_fd;
1002
1003 rcu_read_lock();
1004
1005 ret = kernel_open_metadata(ksess);
1006 if (ret < 0) {
1007 ret = LTTNG_ERR_KERN_META_FAIL;
1008 goto error;
1009 }
1010
1011 ret = kernel_open_metadata_stream(ksess);
1012 if (ret < 0) {
1013 ret = LTTNG_ERR_KERN_META_FAIL;
1014 goto error_open_stream;
1015 }
1016
1017 /* Send metadata to consumer and snapshot everything. */
1018 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1019 socket, node.node) {
1020 struct consumer_output *saved_output;
1021 struct ltt_kernel_channel *chan;
1022
1023 /*
1024 * Temporarly switch consumer output for our snapshot output. As long
1025 * as the session lock is taken, this is safe.
1026 */
1027 saved_output = ksess->consumer;
1028 ksess->consumer = output->consumer;
1029
1030 pthread_mutex_lock(socket->lock);
1031 /* This stream must not be monitored by the consumer. */
1032 ret = kernel_consumer_add_metadata(socket, ksess, 0);
1033 pthread_mutex_unlock(socket->lock);
1034 /* Put back the saved consumer output into the session. */
1035 ksess->consumer = saved_output;
1036 if (ret < 0) {
1037 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1038 goto error_consumer;
1039 }
1040
1041 /* For each channel, ask the consumer to snapshot it. */
1042 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1043 pthread_mutex_lock(socket->lock);
1044 ret = consumer_snapshot_channel(socket, chan->fd, output, 0,
1045 ksess->uid, ksess->gid,
1046 DEFAULT_KERNEL_TRACE_DIR, wait,
1047 nb_packets_per_stream);
1048 pthread_mutex_unlock(socket->lock);
1049 if (ret < 0) {
1050 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1051 (void) kernel_consumer_destroy_metadata(socket,
1052 ksess->metadata);
1053 goto error_consumer;
1054 }
1055 }
1056
1057 /* Snapshot metadata, */
1058 pthread_mutex_lock(socket->lock);
1059 ret = consumer_snapshot_channel(socket, ksess->metadata->fd, output,
1060 1, ksess->uid, ksess->gid,
1061 DEFAULT_KERNEL_TRACE_DIR, wait, 0);
1062 pthread_mutex_unlock(socket->lock);
1063 if (ret < 0) {
1064 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1065 goto error_consumer;
1066 }
1067
1068 /*
1069 * The metadata snapshot is done, ask the consumer to destroy it since
1070 * it's not monitored on the consumer side.
1071 */
1072 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
1073 }
1074
1075 ret = LTTNG_OK;
1076
1077error_consumer:
1078 /* Close newly opened metadata stream. It's now on the consumer side. */
1079 err = close(ksess->metadata_stream_fd);
1080 if (err < 0) {
1081 PERROR("close snapshot kernel");
1082 }
1083
1084error_open_stream:
1085 trace_kernel_destroy_metadata(ksess->metadata);
1086error:
1087 /* Restore metadata state.*/
1088 ksess->metadata = saved_metadata;
1089 ksess->metadata_stream_fd = saved_metadata_fd;
1090
1091 rcu_read_unlock();
1092 return ret;
1093}
1094
1095/*
1096 * Get the syscall mask array from the kernel tracer.
1097 *
1098 * Return 0 on success else a negative value. In both case, syscall_mask should
1099 * be freed.
1100 */
1101int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1102{
1103 assert(syscall_mask);
1104 assert(nr_bits);
1105
1106 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1107}
This page took 0.026249 seconds and 4 git commands to generate.