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