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