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