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