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