Sessiond timer thread
[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 * Key used to reference a channel between the sessiond and the consumer. This
39 * is only read and updated with the session_list lock held.
40 */
41 static uint64_t next_kernel_channel_key;
42
43 /*
44 * Add context on a kernel channel.
45 *
46 * Assumes the ownership of ctx.
47 */
48 int kernel_add_channel_context(struct ltt_kernel_channel *chan,
49 struct ltt_kernel_context *ctx)
50 {
51 int ret;
52
53 assert(chan);
54 assert(ctx);
55
56 DBG("Adding context to channel %s", chan->channel->name);
57 ret = kernctl_add_context(chan->fd, &ctx->ctx);
58 if (ret < 0) {
59 switch (-ret) {
60 case ENOSYS:
61 /* Exists but not available for this kernel */
62 ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE;
63 goto error;
64 case EEXIST:
65 /* If EEXIST, we just ignore the error */
66 ret = 0;
67 goto end;
68 default:
69 PERROR("add context ioctl");
70 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
71 goto error;
72 }
73 }
74 ret = 0;
75
76 end:
77 cds_list_add_tail(&ctx->list, &chan->ctx_list);
78 ctx->in_list = true;
79 ctx = NULL;
80 error:
81 if (ctx) {
82 trace_kernel_destroy_context(ctx);
83 }
84 return ret;
85 }
86
87 /*
88 * Create a new kernel session, register it to the kernel tracer and add it to
89 * the session daemon session.
90 */
91 int kernel_create_session(struct ltt_session *session, int tracer_fd)
92 {
93 int ret;
94 struct ltt_kernel_session *lks;
95
96 assert(session);
97
98 /* Allocate data structure */
99 lks = trace_kernel_create_session();
100 if (lks == NULL) {
101 ret = -1;
102 goto error;
103 }
104
105 /* Kernel tracer session creation */
106 ret = kernctl_create_session(tracer_fd);
107 if (ret < 0) {
108 PERROR("ioctl kernel create session");
109 goto error;
110 }
111
112 lks->fd = ret;
113 /* Prevent fd duplication after execlp() */
114 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
115 if (ret < 0) {
116 PERROR("fcntl session fd");
117 }
118
119 lks->id = session->id;
120 lks->consumer_fds_sent = 0;
121 session->kernel_session = lks;
122
123 DBG("Kernel session created (fd: %d)", lks->fd);
124
125 return 0;
126
127 error:
128 if (lks) {
129 trace_kernel_destroy_session(lks);
130 }
131 return ret;
132 }
133
134 /*
135 * Create a kernel channel, register it to the kernel tracer and add it to the
136 * kernel session.
137 */
138 int kernel_create_channel(struct ltt_kernel_session *session,
139 struct lttng_channel *chan)
140 {
141 int ret;
142 struct ltt_kernel_channel *lkc;
143
144 assert(session);
145 assert(chan);
146
147 /* Allocate kernel channel */
148 lkc = trace_kernel_create_channel(chan);
149 if (lkc == NULL) {
150 goto error;
151 }
152
153 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
154 chan->name, lkc->channel->attr.overwrite,
155 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
156 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
157 lkc->channel->attr.live_timer_interval, lkc->channel->attr.output);
158
159 /* Kernel tracer channel creation */
160 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
161 if (ret < 0) {
162 PERROR("ioctl kernel create channel");
163 goto error;
164 }
165
166 /* Setup the channel fd */
167 lkc->fd = ret;
168 /* Prevent fd duplication after execlp() */
169 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
170 if (ret < 0) {
171 PERROR("fcntl session fd");
172 }
173
174 /* Add channel to session */
175 cds_list_add(&lkc->list, &session->channel_list.head);
176 session->channel_count++;
177 lkc->session = session;
178 lkc->key = ++next_kernel_channel_key;
179
180 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")",
181 lkc->channel->name, lkc->fd, lkc->key);
182
183 return 0;
184
185 error:
186 if (lkc) {
187 free(lkc->channel);
188 free(lkc);
189 }
190 return -1;
191 }
192
193 /*
194 * Create a kernel event, enable it to the kernel tracer and add it to the
195 * channel event list of the kernel session.
196 * We own filter_expression and filter.
197 */
198 int kernel_create_event(struct lttng_event *ev,
199 struct ltt_kernel_channel *channel,
200 char *filter_expression,
201 struct lttng_filter_bytecode *filter)
202 {
203 int ret;
204 struct ltt_kernel_event *event;
205
206 assert(ev);
207 assert(channel);
208
209 /* We pass ownership of filter_expression and filter */
210 event = trace_kernel_create_event(ev, filter_expression,
211 filter);
212 if (event == NULL) {
213 ret = -1;
214 goto error;
215 }
216
217 ret = kernctl_create_event(channel->fd, event->event);
218 if (ret < 0) {
219 switch (-ret) {
220 case EEXIST:
221 break;
222 case ENOSYS:
223 WARN("Event type not implemented");
224 break;
225 case ENOENT:
226 WARN("Event %s not found!", ev->name);
227 break;
228 default:
229 PERROR("create event ioctl");
230 }
231 goto free_event;
232 }
233
234 event->type = ev->type;
235 event->fd = ret;
236 /* Prevent fd duplication after execlp() */
237 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
238 if (ret < 0) {
239 PERROR("fcntl session fd");
240 }
241
242 if (filter) {
243 ret = kernctl_filter(event->fd, filter);
244 if (ret) {
245 goto filter_error;
246 }
247 }
248
249 ret = kernctl_enable(event->fd);
250 if (ret < 0) {
251 switch (-ret) {
252 case EEXIST:
253 ret = LTTNG_ERR_KERN_EVENT_EXIST;
254 break;
255 default:
256 PERROR("enable kernel event");
257 break;
258 }
259 goto enable_error;
260 }
261
262 /* Add event to event list */
263 cds_list_add(&event->list, &channel->events_list.head);
264 channel->event_count++;
265
266 DBG("Event %s created (fd: %d)", ev->name, event->fd);
267
268 return 0;
269
270 enable_error:
271 filter_error:
272 {
273 int closeret;
274
275 closeret = close(event->fd);
276 if (closeret) {
277 PERROR("close event fd");
278 }
279 }
280 free_event:
281 free(event);
282 error:
283 return ret;
284 }
285
286 /*
287 * Disable a kernel channel.
288 */
289 int kernel_disable_channel(struct ltt_kernel_channel *chan)
290 {
291 int ret;
292
293 assert(chan);
294
295 ret = kernctl_disable(chan->fd);
296 if (ret < 0) {
297 PERROR("disable chan ioctl");
298 goto error;
299 }
300
301 chan->enabled = 0;
302 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")",
303 chan->channel->name, chan->fd, chan->key);
304
305 return 0;
306
307 error:
308 return ret;
309 }
310
311 /*
312 * Enable a kernel channel.
313 */
314 int kernel_enable_channel(struct ltt_kernel_channel *chan)
315 {
316 int ret;
317
318 assert(chan);
319
320 ret = kernctl_enable(chan->fd);
321 if (ret < 0 && ret != -EEXIST) {
322 PERROR("Enable kernel chan");
323 goto error;
324 }
325
326 chan->enabled = 1;
327 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")",
328 chan->channel->name, chan->fd, chan->key);
329
330 return 0;
331
332 error:
333 return ret;
334 }
335
336 /*
337 * Enable a kernel event.
338 */
339 int kernel_enable_event(struct ltt_kernel_event *event)
340 {
341 int ret;
342
343 assert(event);
344
345 ret = kernctl_enable(event->fd);
346 if (ret < 0) {
347 switch (-ret) {
348 case EEXIST:
349 ret = LTTNG_ERR_KERN_EVENT_EXIST;
350 break;
351 default:
352 PERROR("enable kernel event");
353 break;
354 }
355 goto error;
356 }
357
358 event->enabled = 1;
359 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
360
361 return 0;
362
363 error:
364 return ret;
365 }
366
367 /*
368 * Disable a kernel event.
369 */
370 int kernel_disable_event(struct ltt_kernel_event *event)
371 {
372 int ret;
373
374 assert(event);
375
376 ret = kernctl_disable(event->fd);
377 if (ret < 0) {
378 switch (-ret) {
379 case EEXIST:
380 ret = LTTNG_ERR_KERN_EVENT_EXIST;
381 break;
382 default:
383 PERROR("disable kernel event");
384 break;
385 }
386 goto error;
387 }
388
389 event->enabled = 0;
390 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
391
392 return 0;
393
394 error:
395 return ret;
396 }
397
398
399 int kernel_track_pid(struct ltt_kernel_session *session, int pid)
400 {
401 int ret;
402
403 DBG("Kernel track PID %d for session id %" PRIu64 ".",
404 pid, session->id);
405 ret = kernctl_track_pid(session->fd, pid);
406 if (!ret) {
407 return LTTNG_OK;
408 }
409 switch (-ret) {
410 case EINVAL:
411 return LTTNG_ERR_INVALID;
412 case ENOMEM:
413 return LTTNG_ERR_NOMEM;
414 case EEXIST:
415 return LTTNG_ERR_PID_TRACKED;
416 default:
417 return LTTNG_ERR_UNK;
418 }
419 }
420
421 int kernel_untrack_pid(struct ltt_kernel_session *session, int pid)
422 {
423 int ret;
424
425 DBG("Kernel untrack PID %d for session id %" PRIu64 ".",
426 pid, session->id);
427 ret = kernctl_untrack_pid(session->fd, pid);
428 if (!ret) {
429 return LTTNG_OK;
430 }
431 switch (-ret) {
432 case EINVAL:
433 return LTTNG_ERR_INVALID;
434 case ENOMEM:
435 return LTTNG_ERR_NOMEM;
436 case ENOENT:
437 return LTTNG_ERR_PID_NOT_TRACKED;
438 default:
439 return LTTNG_ERR_UNK;
440 }
441 }
442
443 ssize_t kernel_list_tracker_pids(struct ltt_kernel_session *session,
444 int **_pids)
445 {
446 int fd, ret;
447 int pid;
448 ssize_t nbmem, count = 0;
449 FILE *fp;
450 int *pids;
451
452 fd = kernctl_list_tracker_pids(session->fd);
453 if (fd < 0) {
454 PERROR("kernel tracker pids list");
455 goto error;
456 }
457
458 fp = fdopen(fd, "r");
459 if (fp == NULL) {
460 PERROR("kernel tracker pids list fdopen");
461 goto error_fp;
462 }
463
464 nbmem = KERNEL_TRACKER_PIDS_INIT_LIST_SIZE;
465 pids = zmalloc(sizeof(*pids) * nbmem);
466 if (pids == NULL) {
467 PERROR("alloc list pids");
468 count = -ENOMEM;
469 goto end;
470 }
471
472 while (fscanf(fp, "process { pid = %u; };\n", &pid) == 1) {
473 if (count >= nbmem) {
474 int *new_pids;
475 size_t new_nbmem;
476
477 new_nbmem = nbmem << 1;
478 DBG("Reallocating pids list from %zu to %zu entries",
479 nbmem, new_nbmem);
480 new_pids = realloc(pids, new_nbmem * sizeof(*new_pids));
481 if (new_pids == NULL) {
482 PERROR("realloc list events");
483 free(pids);
484 count = -ENOMEM;
485 goto end;
486 }
487 /* Zero the new memory */
488 memset(new_pids + nbmem, 0,
489 (new_nbmem - nbmem) * sizeof(*new_pids));
490 nbmem = new_nbmem;
491 pids = new_pids;
492 }
493 pids[count++] = pid;
494 }
495
496 *_pids = pids;
497 DBG("Kernel list tracker pids done (%zd pids)", count);
498 end:
499 ret = fclose(fp); /* closes both fp and fd */
500 if (ret) {
501 PERROR("fclose");
502 }
503 return count;
504
505 error_fp:
506 ret = close(fd);
507 if (ret) {
508 PERROR("close");
509 }
510 error:
511 return -1;
512 }
513
514 /*
515 * Create kernel metadata, open from the kernel tracer and add it to the
516 * kernel session.
517 */
518 int kernel_open_metadata(struct ltt_kernel_session *session)
519 {
520 int ret;
521 struct ltt_kernel_metadata *lkm = NULL;
522
523 assert(session);
524
525 /* Allocate kernel metadata */
526 lkm = trace_kernel_create_metadata();
527 if (lkm == NULL) {
528 goto error;
529 }
530
531 /* Kernel tracer metadata creation */
532 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
533 if (ret < 0) {
534 goto error_open;
535 }
536
537 lkm->fd = ret;
538 lkm->key = ++next_kernel_channel_key;
539 /* Prevent fd duplication after execlp() */
540 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
541 if (ret < 0) {
542 PERROR("fcntl session fd");
543 }
544
545 session->metadata = lkm;
546
547 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
548
549 return 0;
550
551 error_open:
552 trace_kernel_destroy_metadata(lkm);
553 error:
554 return -1;
555 }
556
557 /*
558 * Start tracing session.
559 */
560 int kernel_start_session(struct ltt_kernel_session *session)
561 {
562 int ret;
563
564 assert(session);
565
566 ret = kernctl_start_session(session->fd);
567 if (ret < 0) {
568 PERROR("ioctl start session");
569 goto error;
570 }
571
572 DBG("Kernel session started");
573
574 return 0;
575
576 error:
577 return ret;
578 }
579
580 /*
581 * Make a kernel wait to make sure in-flight probe have completed.
582 */
583 void kernel_wait_quiescent(int fd)
584 {
585 int ret;
586
587 DBG("Kernel quiescent wait on %d", fd);
588
589 ret = kernctl_wait_quiescent(fd);
590 if (ret < 0) {
591 PERROR("wait quiescent ioctl");
592 ERR("Kernel quiescent wait failed");
593 }
594 }
595
596 /*
597 * Force flush buffer of metadata.
598 */
599 int kernel_metadata_flush_buffer(int fd)
600 {
601 int ret;
602
603 DBG("Kernel flushing metadata buffer on fd %d", fd);
604
605 ret = kernctl_buffer_flush(fd);
606 if (ret < 0) {
607 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
608 }
609
610 return 0;
611 }
612
613 /*
614 * Force flush buffer for channel.
615 */
616 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
617 {
618 int ret;
619 struct ltt_kernel_stream *stream;
620
621 assert(channel);
622
623 DBG("Flush buffer for channel %s", channel->channel->name);
624
625 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
626 DBG("Flushing channel stream %d", stream->fd);
627 ret = kernctl_buffer_flush(stream->fd);
628 if (ret < 0) {
629 PERROR("ioctl");
630 ERR("Fail to flush buffer for stream %d (ret: %d)",
631 stream->fd, ret);
632 }
633 }
634
635 return 0;
636 }
637
638 /*
639 * Stop tracing session.
640 */
641 int kernel_stop_session(struct ltt_kernel_session *session)
642 {
643 int ret;
644
645 assert(session);
646
647 ret = kernctl_stop_session(session->fd);
648 if (ret < 0) {
649 goto error;
650 }
651
652 DBG("Kernel session stopped");
653
654 return 0;
655
656 error:
657 return ret;
658 }
659
660 /*
661 * Open stream of channel, register it to the kernel tracer and add it
662 * to the stream list of the channel.
663 *
664 * Note: given that the streams may appear in random order wrt CPU
665 * number (e.g. cpu hotplug), the index value of the stream number in
666 * the stream name is not necessarily linked to the CPU number.
667 *
668 * Return the number of created stream. Else, a negative value.
669 */
670 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
671 {
672 int ret;
673 struct ltt_kernel_stream *lks;
674
675 assert(channel);
676
677 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
678 lks = trace_kernel_create_stream(channel->channel->name,
679 channel->stream_count);
680 if (lks == NULL) {
681 ret = close(ret);
682 if (ret) {
683 PERROR("close");
684 }
685 goto error;
686 }
687
688 lks->fd = ret;
689 /* Prevent fd duplication after execlp() */
690 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
691 if (ret < 0) {
692 PERROR("fcntl session fd");
693 }
694
695 lks->tracefile_size = channel->channel->attr.tracefile_size;
696 lks->tracefile_count = channel->channel->attr.tracefile_count;
697
698 /* Add stream to channel stream list */
699 cds_list_add(&lks->list, &channel->stream_list.head);
700 channel->stream_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
708 error:
709 return -1;
710 }
711
712 /*
713 * Open the metadata stream and set it to the kernel session.
714 */
715 int 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
737 error:
738 return -1;
739 }
740
741 /*
742 * Get the event list from the kernel tracer and return the number of elements.
743 */
744 ssize_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);
809 end:
810 ret = fclose(fp); /* closes both fp and fd */
811 if (ret) {
812 PERROR("fclose");
813 }
814 return count;
815
816 error_fp:
817 ret = close(fd);
818 if (ret) {
819 PERROR("close");
820 }
821 error:
822 return -1;
823 }
824
825 /*
826 * Get kernel version and validate it.
827 */
828 int kernel_validate_version(int tracer_fd,
829 struct lttng_kernel_tracer_version *version,
830 struct lttng_kernel_tracer_abi_version *abi_version)
831 {
832 int ret;
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
862 error_version:
863 ret = -1;
864
865 error:
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 */
873 int 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 }
899 end_boot_id:
900 return 0;
901 }
902
903 /*
904 * Complete teardown of a kernel session.
905 */
906 void 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 */
952 void 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 */
984 int 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 ret = consumer_snapshot_channel(socket, chan->key, output, 0,
1044 ksess->uid, ksess->gid,
1045 DEFAULT_KERNEL_TRACE_DIR, wait,
1046 nb_packets_per_stream);
1047 if (ret < 0) {
1048 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1049 (void) kernel_consumer_destroy_metadata(socket,
1050 ksess->metadata);
1051 goto error_consumer;
1052 }
1053 }
1054
1055 /* Snapshot metadata, */
1056 ret = consumer_snapshot_channel(socket, ksess->metadata->key, output,
1057 1, ksess->uid, ksess->gid,
1058 DEFAULT_KERNEL_TRACE_DIR, wait, 0);
1059 if (ret < 0) {
1060 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1061 goto error_consumer;
1062 }
1063
1064 /*
1065 * The metadata snapshot is done, ask the consumer to destroy it since
1066 * it's not monitored on the consumer side.
1067 */
1068 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
1069 }
1070
1071 ret = LTTNG_OK;
1072
1073 error_consumer:
1074 /* Close newly opened metadata stream. It's now on the consumer side. */
1075 err = close(ksess->metadata_stream_fd);
1076 if (err < 0) {
1077 PERROR("close snapshot kernel");
1078 }
1079
1080 error_open_stream:
1081 trace_kernel_destroy_metadata(ksess->metadata);
1082 error:
1083 /* Restore metadata state.*/
1084 ksess->metadata = saved_metadata;
1085 ksess->metadata_stream_fd = saved_metadata_fd;
1086
1087 rcu_read_unlock();
1088 return ret;
1089 }
1090
1091 /*
1092 * Get the syscall mask array from the kernel tracer.
1093 *
1094 * Return 0 on success else a negative value. In both case, syscall_mask should
1095 * be freed.
1096 */
1097 int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1098 {
1099 assert(syscall_mask);
1100 assert(nr_bits);
1101
1102 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1103 }
1104
1105 /*
1106 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1107 * version number.
1108 *
1109 * Return 1 on success, 0 when feature is not supported, negative value in case
1110 * of errors.
1111 */
1112 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd)
1113 {
1114 int ret = 0; // Not supported by default
1115 struct lttng_kernel_tracer_abi_version abi;
1116
1117 ret = kernctl_tracer_abi_version(tracer_fd, &abi);
1118 if (ret < 0) {
1119 ERR("Failed to retrieve lttng-modules ABI version");
1120 goto error;
1121 }
1122
1123 /*
1124 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1125 */
1126 if (abi.major >= 2 && abi.minor >= 3) {
1127 /* Supported */
1128 ret = 1;
1129 } else {
1130 /* Not supported */
1131 ret = 0;
1132 }
1133 error:
1134 return ret;
1135 }
This page took 0.057152 seconds and 4 git commands to generate.