sessiond: enforce mmap output type for kernel metadata channel
[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
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <inttypes.h>
26 #include <sys/types.h>
27
28 #include <common/common.h>
29 #include <common/trace-chunk.h>
30 #include <common/kernel-ctl/kernel-ctl.h>
31 #include <common/kernel-ctl/kernel-ioctl.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
33
34 #include "lttng-sessiond.h"
35 #include "lttng-syscall.h"
36 #include "consumer.h"
37 #include "kernel.h"
38 #include "kernel-consumer.h"
39 #include "kern-modules.h"
40 #include "utils.h"
41 #include "rotate.h"
42 #include "modprobe.h"
43
44 /*
45 * Key used to reference a channel between the sessiond and the consumer. This
46 * is only read and updated with the session_list lock held.
47 */
48 static uint64_t next_kernel_channel_key;
49
50 static const char *module_proc_lttng = "/proc/lttng";
51
52 static int kernel_tracer_fd = -1;
53
54 #include <lttng/userspace-probe.h>
55 #include <lttng/userspace-probe-internal.h>
56 /*
57 * Add context on a kernel channel.
58 *
59 * Assumes the ownership of ctx.
60 */
61 int kernel_add_channel_context(struct ltt_kernel_channel *chan,
62 struct ltt_kernel_context *ctx)
63 {
64 int ret;
65
66 assert(chan);
67 assert(ctx);
68
69 DBG("Adding context to channel %s", chan->channel->name);
70 ret = kernctl_add_context(chan->fd, &ctx->ctx);
71 if (ret < 0) {
72 switch (-ret) {
73 case ENOSYS:
74 /* Exists but not available for this kernel */
75 ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE;
76 goto error;
77 case EEXIST:
78 /* If EEXIST, we just ignore the error */
79 ret = 0;
80 goto end;
81 default:
82 PERROR("add context ioctl");
83 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
84 goto error;
85 }
86 }
87 ret = 0;
88
89 end:
90 cds_list_add_tail(&ctx->list, &chan->ctx_list);
91 ctx->in_list = true;
92 ctx = NULL;
93 error:
94 if (ctx) {
95 trace_kernel_destroy_context(ctx);
96 }
97 return ret;
98 }
99
100 /*
101 * Create a new kernel session, register it to the kernel tracer and add it to
102 * the session daemon session.
103 */
104 int kernel_create_session(struct ltt_session *session)
105 {
106 int ret;
107 struct ltt_kernel_session *lks;
108
109 assert(session);
110
111 /* Allocate data structure */
112 lks = trace_kernel_create_session();
113 if (lks == NULL) {
114 ret = -1;
115 goto error;
116 }
117
118 /* Kernel tracer session creation */
119 ret = kernctl_create_session(kernel_tracer_fd);
120 if (ret < 0) {
121 PERROR("ioctl kernel create session");
122 goto error;
123 }
124
125 lks->fd = ret;
126 /* Prevent fd duplication after execlp() */
127 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
128 if (ret < 0) {
129 PERROR("fcntl session fd");
130 }
131
132 lks->id = session->id;
133 lks->consumer_fds_sent = 0;
134 session->kernel_session = lks;
135
136 DBG("Kernel session created (fd: %d)", lks->fd);
137
138 /*
139 * This is necessary since the creation time is present in the session
140 * name when it is generated.
141 */
142 if (session->has_auto_generated_name) {
143 ret = kernctl_session_set_name(lks->fd, DEFAULT_SESSION_NAME);
144 } else {
145 ret = kernctl_session_set_name(lks->fd, session->name);
146 }
147 if (ret) {
148 WARN("Could not set kernel session name for session %" PRIu64 " name: %s",
149 session->id, session->name);
150 }
151
152 ret = kernctl_session_set_creation_time(lks->fd, session->creation_time);
153 if (ret) {
154 WARN("Could not set kernel session creation time for session %" PRIu64 " name: %s",
155 session->id, session->name);
156 }
157
158 return 0;
159
160 error:
161 if (lks) {
162 trace_kernel_destroy_session(lks);
163 trace_kernel_free_session(lks);
164 }
165 return ret;
166 }
167
168 /*
169 * Create a kernel channel, register it to the kernel tracer and add it to the
170 * kernel session.
171 */
172 int kernel_create_channel(struct ltt_kernel_session *session,
173 struct lttng_channel *chan)
174 {
175 int ret;
176 struct ltt_kernel_channel *lkc;
177
178 assert(session);
179 assert(chan);
180
181 /* Allocate kernel channel */
182 lkc = trace_kernel_create_channel(chan);
183 if (lkc == NULL) {
184 goto error;
185 }
186
187 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
188 chan->name, lkc->channel->attr.overwrite,
189 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
190 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
191 lkc->channel->attr.live_timer_interval, lkc->channel->attr.output);
192
193 /* Kernel tracer channel creation */
194 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
195 if (ret < 0) {
196 PERROR("ioctl kernel create channel");
197 goto error;
198 }
199
200 /* Setup the channel fd */
201 lkc->fd = ret;
202 /* Prevent fd duplication after execlp() */
203 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
204 if (ret < 0) {
205 PERROR("fcntl session fd");
206 }
207
208 /* Add channel to session */
209 cds_list_add(&lkc->list, &session->channel_list.head);
210 session->channel_count++;
211 lkc->session = session;
212 lkc->key = ++next_kernel_channel_key;
213
214 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")",
215 lkc->channel->name, lkc->fd, lkc->key);
216
217 return 0;
218
219 error:
220 if (lkc) {
221 free(lkc->channel);
222 free(lkc);
223 }
224 return -1;
225 }
226
227 /*
228 * Compute the offset of the instrumentation byte in the binary based on the
229 * function probe location using the ELF lookup method.
230 *
231 * Returns 0 on success and set the offset out parameter to the offset of the
232 * elf symbol
233 * Returns -1 on error
234 */
235 static
236 int extract_userspace_probe_offset_function_elf(
237 const struct lttng_userspace_probe_location *probe_location,
238 struct ltt_kernel_session *session, uint64_t *offset)
239 {
240 int fd;
241 int ret = 0;
242 const char *symbol = NULL;
243 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
244 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
245
246 assert(lttng_userspace_probe_location_get_type(probe_location) ==
247 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
248
249 lookup = lttng_userspace_probe_location_get_lookup_method(
250 probe_location);
251 if (!lookup) {
252 ret = -1;
253 goto end;
254 }
255
256 lookup_method_type =
257 lttng_userspace_probe_location_lookup_method_get_type(lookup);
258
259 assert(lookup_method_type ==
260 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF);
261
262 symbol = lttng_userspace_probe_location_function_get_function_name(
263 probe_location);
264 if (!symbol) {
265 ret = -1;
266 goto end;
267 }
268
269 fd = lttng_userspace_probe_location_function_get_binary_fd(probe_location);
270 if (fd < 0) {
271 ret = -1;
272 goto end;
273 }
274
275 ret = run_as_extract_elf_symbol_offset(fd, symbol, session->uid,
276 session->gid, offset);
277 if (ret < 0) {
278 DBG("userspace probe offset calculation failed for "
279 "function %s", symbol);
280 goto end;
281 }
282
283 DBG("userspace probe elf offset for %s is 0x%jd", symbol, (intmax_t)(*offset));
284 end:
285 return ret;
286 }
287
288 /*
289 * Compute the offsets of the instrumentation bytes in the binary based on the
290 * tracepoint probe location using the SDT lookup method. This function
291 * allocates the offsets buffer, the caller must free it.
292 *
293 * Returns 0 on success and set the offset out parameter to the offsets of the
294 * SDT tracepoint.
295 * Returns -1 on error.
296 */
297 static
298 int extract_userspace_probe_offset_tracepoint_sdt(
299 const struct lttng_userspace_probe_location *probe_location,
300 struct ltt_kernel_session *session, uint64_t **offsets,
301 uint32_t *offsets_count)
302 {
303 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
304 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
305 const char *probe_name = NULL, *provider_name = NULL;
306 int ret = 0;
307 int fd, i;
308
309 assert(lttng_userspace_probe_location_get_type(probe_location) ==
310 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
311
312 lookup = lttng_userspace_probe_location_get_lookup_method(probe_location);
313 if (!lookup) {
314 ret = -1;
315 goto end;
316 }
317
318 lookup_method_type =
319 lttng_userspace_probe_location_lookup_method_get_type(lookup);
320
321 assert(lookup_method_type ==
322 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT);
323
324
325 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(
326 probe_location);
327 if (!probe_name) {
328 ret = -1;
329 goto end;
330 }
331
332 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(
333 probe_location);
334 if (!provider_name) {
335 ret = -1;
336 goto end;
337 }
338
339 fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location);
340 if (fd < 0) {
341 ret = -1;
342 goto end;
343 }
344
345 ret = run_as_extract_sdt_probe_offsets(fd, provider_name, probe_name,
346 session->uid, session->gid, offsets, offsets_count);
347 if (ret < 0) {
348 DBG("userspace probe offset calculation failed for sdt "
349 "probe %s:%s", provider_name, probe_name);
350 goto end;
351 }
352
353 if (*offsets_count == 0) {
354 DBG("no userspace probe offset found");
355 goto end;
356 }
357
358 DBG("%u userspace probe SDT offsets found for %s:%s at:",
359 *offsets_count, provider_name, probe_name);
360 for (i = 0; i < *offsets_count; i++) {
361 DBG("\t0x%jd", (intmax_t)((*offsets)[i]));
362 }
363 end:
364 return ret;
365 }
366
367 /*
368 * Extract the offsets of the instrumentation point for the different lookup
369 * methods.
370 */
371 static
372 int userspace_probe_add_callsites(struct lttng_event *ev,
373 struct ltt_kernel_session *session, int fd)
374 {
375 const struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
376 enum lttng_userspace_probe_location_lookup_method_type type;
377 const struct lttng_userspace_probe_location *location = NULL;
378 int ret;
379
380 assert(ev);
381 assert(ev->type == LTTNG_EVENT_USERSPACE_PROBE);
382
383 location = lttng_event_get_userspace_probe_location(ev);
384 if (!location) {
385 ret = -1;
386 goto end;
387 }
388 lookup_method =
389 lttng_userspace_probe_location_get_lookup_method(location);
390 if (!lookup_method) {
391 ret = -1;
392 goto end;
393 }
394
395 type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method);
396 switch (type) {
397 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
398 {
399 struct lttng_kernel_event_callsite callsite;
400 uint64_t offset;
401
402 ret = extract_userspace_probe_offset_function_elf(location, session, &offset);
403 if (ret) {
404 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
405 goto end;
406 }
407
408 callsite.u.uprobe.offset = offset;
409 ret = kernctl_add_callsite(fd, &callsite);
410 if (ret) {
411 WARN("Adding callsite to userspace probe "
412 "event %s failed.", ev->name);
413 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
414 goto end;
415 }
416 break;
417 }
418 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
419 {
420 int i;
421 uint64_t *offsets = NULL;
422 uint32_t offsets_count;
423 struct lttng_kernel_event_callsite callsite;
424
425 /*
426 * This call allocates the offsets buffer. This buffer must be freed
427 * by the caller
428 */
429 ret = extract_userspace_probe_offset_tracepoint_sdt(location, session,
430 &offsets, &offsets_count);
431 if (ret) {
432 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
433 goto end;
434 }
435 for (i = 0; i < offsets_count; i++) {
436 callsite.u.uprobe.offset = offsets[i];
437 ret = kernctl_add_callsite(fd, &callsite);
438 if (ret) {
439 WARN("Adding callsite to userspace probe "
440 "event %s failed.", ev->name);
441 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
442 free(offsets);
443 goto end;
444 }
445 }
446 free(offsets);
447 break;
448 }
449 default:
450 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
451 goto end;
452 }
453 end:
454 return ret;
455 }
456
457 /*
458 * Create a kernel event, enable it to the kernel tracer and add it to the
459 * channel event list of the kernel session.
460 * We own filter_expression and filter.
461 */
462 int kernel_create_event(struct lttng_event *ev,
463 struct ltt_kernel_channel *channel,
464 char *filter_expression,
465 struct lttng_filter_bytecode *filter)
466 {
467 int err, fd;
468 enum lttng_error_code ret;
469 struct ltt_kernel_event *event;
470
471 assert(ev);
472 assert(channel);
473
474 /* We pass ownership of filter_expression and filter */
475 ret = trace_kernel_create_event(ev, filter_expression,
476 filter, &event);
477 if (ret != LTTNG_OK) {
478 goto error;
479 }
480
481 fd = kernctl_create_event(channel->fd, event->event);
482 if (fd < 0) {
483 switch (-fd) {
484 case EEXIST:
485 ret = LTTNG_ERR_KERN_EVENT_EXIST;
486 break;
487 case ENOSYS:
488 WARN("Event type not implemented");
489 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
490 break;
491 case ENOENT:
492 WARN("Event %s not found!", ev->name);
493 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
494 break;
495 default:
496 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
497 PERROR("create event ioctl");
498 }
499 goto free_event;
500 }
501
502 event->type = ev->type;
503 event->fd = fd;
504 /* Prevent fd duplication after execlp() */
505 err = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
506 if (err < 0) {
507 PERROR("fcntl session fd");
508 }
509
510 if (filter) {
511 err = kernctl_filter(event->fd, filter);
512 if (err < 0) {
513 switch (-err) {
514 case ENOMEM:
515 ret = LTTNG_ERR_FILTER_NOMEM;
516 break;
517 default:
518 ret = LTTNG_ERR_FILTER_INVAL;
519 break;
520 }
521 goto filter_error;
522 }
523 }
524
525 if (ev->type == LTTNG_EVENT_USERSPACE_PROBE) {
526 ret = userspace_probe_add_callsites(ev, channel->session, event->fd);
527 if (ret) {
528 goto add_callsite_error;
529 }
530 }
531
532 err = kernctl_enable(event->fd);
533 if (err < 0) {
534 switch (-err) {
535 case EEXIST:
536 ret = LTTNG_ERR_KERN_EVENT_EXIST;
537 break;
538 default:
539 PERROR("enable kernel event");
540 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
541 break;
542 }
543 goto enable_error;
544 }
545
546 /* Add event to event list */
547 cds_list_add(&event->list, &channel->events_list.head);
548 channel->event_count++;
549
550 DBG("Event %s created (fd: %d)", ev->name, event->fd);
551
552 return 0;
553
554 add_callsite_error:
555 enable_error:
556 filter_error:
557 {
558 int closeret;
559
560 closeret = close(event->fd);
561 if (closeret) {
562 PERROR("close event fd");
563 }
564 }
565 free_event:
566 free(event);
567 error:
568 return ret;
569 }
570
571 /*
572 * Disable a kernel channel.
573 */
574 int kernel_disable_channel(struct ltt_kernel_channel *chan)
575 {
576 int ret;
577
578 assert(chan);
579
580 ret = kernctl_disable(chan->fd);
581 if (ret < 0) {
582 PERROR("disable chan ioctl");
583 goto error;
584 }
585
586 chan->enabled = 0;
587 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")",
588 chan->channel->name, chan->fd, chan->key);
589
590 return 0;
591
592 error:
593 return ret;
594 }
595
596 /*
597 * Enable a kernel channel.
598 */
599 int kernel_enable_channel(struct ltt_kernel_channel *chan)
600 {
601 int ret;
602
603 assert(chan);
604
605 ret = kernctl_enable(chan->fd);
606 if (ret < 0 && ret != -EEXIST) {
607 PERROR("Enable kernel chan");
608 goto error;
609 }
610
611 chan->enabled = 1;
612 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")",
613 chan->channel->name, chan->fd, chan->key);
614
615 return 0;
616
617 error:
618 return ret;
619 }
620
621 /*
622 * Enable a kernel event.
623 */
624 int kernel_enable_event(struct ltt_kernel_event *event)
625 {
626 int ret;
627
628 assert(event);
629
630 ret = kernctl_enable(event->fd);
631 if (ret < 0) {
632 switch (-ret) {
633 case EEXIST:
634 ret = LTTNG_ERR_KERN_EVENT_EXIST;
635 break;
636 default:
637 PERROR("enable kernel event");
638 break;
639 }
640 goto error;
641 }
642
643 event->enabled = 1;
644 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
645
646 return 0;
647
648 error:
649 return ret;
650 }
651
652 /*
653 * Disable a kernel event.
654 */
655 int kernel_disable_event(struct ltt_kernel_event *event)
656 {
657 int ret;
658
659 assert(event);
660
661 ret = kernctl_disable(event->fd);
662 if (ret < 0) {
663 switch (-ret) {
664 case EEXIST:
665 ret = LTTNG_ERR_KERN_EVENT_EXIST;
666 break;
667 default:
668 PERROR("disable kernel event");
669 break;
670 }
671 goto error;
672 }
673
674 event->enabled = 0;
675 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
676
677 return 0;
678
679 error:
680 return ret;
681 }
682
683
684 int kernel_track_pid(struct ltt_kernel_session *session, int pid)
685 {
686 int ret;
687
688 DBG("Kernel track PID %d for session id %" PRIu64 ".",
689 pid, session->id);
690 ret = kernctl_track_pid(session->fd, pid);
691 if (!ret) {
692 return LTTNG_OK;
693 }
694 switch (-ret) {
695 case EINVAL:
696 return LTTNG_ERR_INVALID;
697 case ENOMEM:
698 return LTTNG_ERR_NOMEM;
699 case EEXIST:
700 return LTTNG_ERR_PID_TRACKED;
701 default:
702 return LTTNG_ERR_UNK;
703 }
704 }
705
706 int kernel_untrack_pid(struct ltt_kernel_session *session, int pid)
707 {
708 int ret;
709
710 DBG("Kernel untrack PID %d for session id %" PRIu64 ".",
711 pid, session->id);
712 ret = kernctl_untrack_pid(session->fd, pid);
713 if (!ret) {
714 return LTTNG_OK;
715 }
716 switch (-ret) {
717 case EINVAL:
718 return LTTNG_ERR_INVALID;
719 case ENOMEM:
720 return LTTNG_ERR_NOMEM;
721 case ENOENT:
722 return LTTNG_ERR_PID_NOT_TRACKED;
723 default:
724 return LTTNG_ERR_UNK;
725 }
726 }
727
728 ssize_t kernel_list_tracker_pids(struct ltt_kernel_session *session,
729 int **_pids)
730 {
731 int fd, ret;
732 int pid;
733 ssize_t nbmem, count = 0;
734 FILE *fp;
735 int *pids;
736
737 fd = kernctl_list_tracker_pids(session->fd);
738 if (fd < 0) {
739 PERROR("kernel tracker pids list");
740 goto error;
741 }
742
743 fp = fdopen(fd, "r");
744 if (fp == NULL) {
745 PERROR("kernel tracker pids list fdopen");
746 goto error_fp;
747 }
748
749 nbmem = KERNEL_TRACKER_PIDS_INIT_LIST_SIZE;
750 pids = zmalloc(sizeof(*pids) * nbmem);
751 if (pids == NULL) {
752 PERROR("alloc list pids");
753 count = -ENOMEM;
754 goto end;
755 }
756
757 while (fscanf(fp, "process { pid = %u; };\n", &pid) == 1) {
758 if (count >= nbmem) {
759 int *new_pids;
760 size_t new_nbmem;
761
762 new_nbmem = nbmem << 1;
763 DBG("Reallocating pids list from %zu to %zu entries",
764 nbmem, new_nbmem);
765 new_pids = realloc(pids, new_nbmem * sizeof(*new_pids));
766 if (new_pids == NULL) {
767 PERROR("realloc list events");
768 free(pids);
769 count = -ENOMEM;
770 goto end;
771 }
772 /* Zero the new memory */
773 memset(new_pids + nbmem, 0,
774 (new_nbmem - nbmem) * sizeof(*new_pids));
775 nbmem = new_nbmem;
776 pids = new_pids;
777 }
778 pids[count++] = pid;
779 }
780
781 *_pids = pids;
782 DBG("Kernel list tracker pids done (%zd pids)", count);
783 end:
784 ret = fclose(fp); /* closes both fp and fd */
785 if (ret) {
786 PERROR("fclose");
787 }
788 return count;
789
790 error_fp:
791 ret = close(fd);
792 if (ret) {
793 PERROR("close");
794 }
795 error:
796 return -1;
797 }
798
799 /*
800 * Create kernel metadata, open from the kernel tracer and add it to the
801 * kernel session.
802 */
803 int kernel_open_metadata(struct ltt_kernel_session *session)
804 {
805 int ret;
806 struct ltt_kernel_metadata *lkm = NULL;
807
808 assert(session);
809
810 /* Allocate kernel metadata */
811 lkm = trace_kernel_create_metadata();
812 if (lkm == NULL) {
813 goto error;
814 }
815
816 /* Kernel tracer metadata creation */
817 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
818 if (ret < 0) {
819 goto error_open;
820 }
821
822 lkm->fd = ret;
823 lkm->key = ++next_kernel_channel_key;
824 /* Prevent fd duplication after execlp() */
825 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
826 if (ret < 0) {
827 PERROR("fcntl session fd");
828 }
829
830 session->metadata = lkm;
831
832 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
833
834 return 0;
835
836 error_open:
837 trace_kernel_destroy_metadata(lkm);
838 error:
839 return -1;
840 }
841
842 /*
843 * Start tracing session.
844 */
845 int kernel_start_session(struct ltt_kernel_session *session)
846 {
847 int ret;
848
849 assert(session);
850
851 ret = kernctl_start_session(session->fd);
852 if (ret < 0) {
853 PERROR("ioctl start session");
854 goto error;
855 }
856
857 DBG("Kernel session started");
858
859 return 0;
860
861 error:
862 return ret;
863 }
864
865 /*
866 * Make a kernel wait to make sure in-flight probe have completed.
867 */
868 void kernel_wait_quiescent(void)
869 {
870 int ret;
871 int fd = kernel_tracer_fd;
872
873 DBG("Kernel quiescent wait on %d", fd);
874
875 ret = kernctl_wait_quiescent(fd);
876 if (ret < 0) {
877 PERROR("wait quiescent ioctl");
878 ERR("Kernel quiescent wait failed");
879 }
880 }
881
882 /*
883 * Force flush buffer of metadata.
884 */
885 int kernel_metadata_flush_buffer(int fd)
886 {
887 int ret;
888
889 DBG("Kernel flushing metadata buffer on fd %d", fd);
890
891 ret = kernctl_buffer_flush(fd);
892 if (ret < 0) {
893 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
894 }
895
896 return 0;
897 }
898
899 /*
900 * Force flush buffer for channel.
901 */
902 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
903 {
904 int ret;
905 struct ltt_kernel_stream *stream;
906
907 assert(channel);
908
909 DBG("Flush buffer for channel %s", channel->channel->name);
910
911 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
912 DBG("Flushing channel stream %d", stream->fd);
913 ret = kernctl_buffer_flush(stream->fd);
914 if (ret < 0) {
915 PERROR("ioctl");
916 ERR("Fail to flush buffer for stream %d (ret: %d)",
917 stream->fd, ret);
918 }
919 }
920
921 return 0;
922 }
923
924 /*
925 * Stop tracing session.
926 */
927 int kernel_stop_session(struct ltt_kernel_session *session)
928 {
929 int ret;
930
931 assert(session);
932
933 ret = kernctl_stop_session(session->fd);
934 if (ret < 0) {
935 goto error;
936 }
937
938 DBG("Kernel session stopped");
939
940 return 0;
941
942 error:
943 return ret;
944 }
945
946 /*
947 * Open stream of channel, register it to the kernel tracer and add it
948 * to the stream list of the channel.
949 *
950 * Note: given that the streams may appear in random order wrt CPU
951 * number (e.g. cpu hotplug), the index value of the stream number in
952 * the stream name is not necessarily linked to the CPU number.
953 *
954 * Return the number of created stream. Else, a negative value.
955 */
956 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
957 {
958 int ret;
959 struct ltt_kernel_stream *lks;
960
961 assert(channel);
962
963 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
964 lks = trace_kernel_create_stream(channel->channel->name,
965 channel->stream_count);
966 if (lks == NULL) {
967 ret = close(ret);
968 if (ret) {
969 PERROR("close");
970 }
971 goto error;
972 }
973
974 lks->fd = ret;
975 /* Prevent fd duplication after execlp() */
976 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
977 if (ret < 0) {
978 PERROR("fcntl session fd");
979 }
980
981 lks->tracefile_size = channel->channel->attr.tracefile_size;
982 lks->tracefile_count = channel->channel->attr.tracefile_count;
983
984 /* Add stream to channel stream list */
985 cds_list_add(&lks->list, &channel->stream_list.head);
986 channel->stream_count++;
987
988 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
989 lks->state);
990 }
991
992 return channel->stream_count;
993
994 error:
995 return -1;
996 }
997
998 /*
999 * Open the metadata stream and set it to the kernel session.
1000 */
1001 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
1002 {
1003 int ret;
1004
1005 assert(session);
1006
1007 ret = kernctl_create_stream(session->metadata->fd);
1008 if (ret < 0) {
1009 PERROR("kernel create metadata stream");
1010 goto error;
1011 }
1012
1013 DBG("Kernel metadata stream created (fd: %d)", ret);
1014 session->metadata_stream_fd = ret;
1015 /* Prevent fd duplication after execlp() */
1016 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
1017 if (ret < 0) {
1018 PERROR("fcntl session fd");
1019 }
1020
1021 return 0;
1022
1023 error:
1024 return -1;
1025 }
1026
1027 /*
1028 * Get the event list from the kernel tracer and return the number of elements.
1029 */
1030 ssize_t kernel_list_events(struct lttng_event **events)
1031 {
1032 int fd, ret;
1033 char *event;
1034 size_t nbmem, count = 0;
1035 FILE *fp;
1036 struct lttng_event *elist;
1037
1038 assert(events);
1039
1040 fd = kernctl_tracepoint_list(kernel_tracer_fd);
1041 if (fd < 0) {
1042 PERROR("kernel tracepoint list");
1043 goto error;
1044 }
1045
1046 fp = fdopen(fd, "r");
1047 if (fp == NULL) {
1048 PERROR("kernel tracepoint list fdopen");
1049 goto error_fp;
1050 }
1051
1052 /*
1053 * Init memory size counter
1054 * See kernel-ctl.h for explanation of this value
1055 */
1056 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
1057 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
1058 if (elist == NULL) {
1059 PERROR("alloc list events");
1060 count = -ENOMEM;
1061 goto end;
1062 }
1063
1064 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
1065 if (count >= nbmem) {
1066 struct lttng_event *new_elist;
1067 size_t new_nbmem;
1068
1069 new_nbmem = nbmem << 1;
1070 DBG("Reallocating event list from %zu to %zu bytes",
1071 nbmem, new_nbmem);
1072 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
1073 if (new_elist == NULL) {
1074 PERROR("realloc list events");
1075 free(event);
1076 free(elist);
1077 count = -ENOMEM;
1078 goto end;
1079 }
1080 /* Zero the new memory */
1081 memset(new_elist + nbmem, 0,
1082 (new_nbmem - nbmem) * sizeof(struct lttng_event));
1083 nbmem = new_nbmem;
1084 elist = new_elist;
1085 }
1086 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
1087 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1088 elist[count].enabled = -1;
1089 count++;
1090 free(event);
1091 }
1092
1093 *events = elist;
1094 DBG("Kernel list events done (%zu events)", count);
1095 end:
1096 ret = fclose(fp); /* closes both fp and fd */
1097 if (ret) {
1098 PERROR("fclose");
1099 }
1100 return count;
1101
1102 error_fp:
1103 ret = close(fd);
1104 if (ret) {
1105 PERROR("close");
1106 }
1107 error:
1108 return -1;
1109 }
1110
1111 /*
1112 * Get kernel version and validate it.
1113 */
1114 int kernel_validate_version(struct lttng_kernel_tracer_version *version,
1115 struct lttng_kernel_tracer_abi_version *abi_version)
1116 {
1117 int ret;
1118
1119 ret = kernctl_tracer_version(kernel_tracer_fd, version);
1120 if (ret < 0) {
1121 ERR("Failed to retrieve the lttng-modules version");
1122 goto error;
1123 }
1124
1125 /* Validate version */
1126 if (version->major != VERSION_MAJOR) {
1127 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1128 version->major, VERSION_MAJOR);
1129 goto error_version;
1130 }
1131 ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version);
1132 if (ret < 0) {
1133 ERR("Failed to retrieve lttng-modules ABI version");
1134 goto error;
1135 }
1136 if (abi_version->major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
1137 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1138 abi_version->major, abi_version->minor,
1139 LTTNG_MODULES_ABI_MAJOR_VERSION);
1140 goto error;
1141 }
1142 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1143 version->major, version->minor,
1144 abi_version->major, abi_version->minor);
1145 return 0;
1146
1147 error_version:
1148 ret = -1;
1149
1150 error:
1151 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1152 return ret;
1153 }
1154
1155 /*
1156 * Kernel work-arounds called at the start of sessiond main().
1157 */
1158 int init_kernel_workarounds(void)
1159 {
1160 int ret;
1161 FILE *fp;
1162
1163 /*
1164 * boot_id needs to be read once before being used concurrently
1165 * to deal with a Linux kernel race. A fix is proposed for
1166 * upstream, but the work-around is needed for older kernels.
1167 */
1168 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
1169 if (!fp) {
1170 goto end_boot_id;
1171 }
1172 while (!feof(fp)) {
1173 char buf[37] = "";
1174
1175 ret = fread(buf, 1, sizeof(buf), fp);
1176 if (ret < 0) {
1177 /* Ignore error, we don't really care */
1178 }
1179 }
1180 ret = fclose(fp);
1181 if (ret) {
1182 PERROR("fclose");
1183 }
1184 end_boot_id:
1185 return 0;
1186 }
1187
1188 /*
1189 * Teardown of a kernel session, keeping data required by destroy notifiers.
1190 */
1191 void kernel_destroy_session(struct ltt_kernel_session *ksess)
1192 {
1193 struct lttng_trace_chunk *trace_chunk;
1194
1195 if (ksess == NULL) {
1196 DBG3("No kernel session when tearing down session");
1197 return;
1198 }
1199
1200 DBG("Tearing down kernel session");
1201 trace_chunk = ksess->current_trace_chunk;
1202
1203 /*
1204 * Destroy channels on the consumer if at least one FD has been sent and we
1205 * are in no output mode because the streams are in *no* monitor mode so we
1206 * have to send a command to clean them up or else they leaked.
1207 */
1208 if (!ksess->output_traces && ksess->consumer_fds_sent) {
1209 int ret;
1210 struct consumer_socket *socket;
1211 struct lttng_ht_iter iter;
1212
1213 /* For each consumer socket. */
1214 rcu_read_lock();
1215 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1216 socket, node.node) {
1217 struct ltt_kernel_channel *chan;
1218
1219 /* For each channel, ask the consumer to destroy it. */
1220 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1221 ret = kernel_consumer_destroy_channel(socket, chan);
1222 if (ret < 0) {
1223 /* Consumer is probably dead. Use next socket. */
1224 continue;
1225 }
1226 }
1227 }
1228 rcu_read_unlock();
1229 }
1230
1231 /* Close any relayd session */
1232 consumer_output_send_destroy_relayd(ksess->consumer);
1233
1234 trace_kernel_destroy_session(ksess);
1235 lttng_trace_chunk_put(trace_chunk);
1236 }
1237
1238 /* Teardown of data required by destroy notifiers. */
1239 void kernel_free_session(struct ltt_kernel_session *ksess)
1240 {
1241 if (ksess == NULL) {
1242 return;
1243 }
1244 trace_kernel_free_session(ksess);
1245 }
1246
1247 /*
1248 * Destroy a kernel channel object. It does not do anything on the tracer side.
1249 */
1250 void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
1251 {
1252 struct ltt_kernel_session *ksess = NULL;
1253
1254 assert(kchan);
1255 assert(kchan->channel);
1256
1257 DBG3("Kernel destroy channel %s", kchan->channel->name);
1258
1259 /* Update channel count of associated session. */
1260 if (kchan->session) {
1261 /* Keep pointer reference so we can update it after the destroy. */
1262 ksess = kchan->session;
1263 }
1264
1265 trace_kernel_destroy_channel(kchan);
1266
1267 /*
1268 * At this point the kernel channel is not visible anymore. This is safe
1269 * since in order to work on a visible kernel session, the tracing session
1270 * lock (ltt_session.lock) MUST be acquired.
1271 */
1272 if (ksess) {
1273 ksess->channel_count--;
1274 }
1275 }
1276
1277 /*
1278 * Take a snapshot for a given kernel session.
1279 *
1280 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1281 */
1282 enum lttng_error_code kernel_snapshot_record(
1283 struct ltt_kernel_session *ksess,
1284 const struct consumer_output *output, int wait,
1285 uint64_t nb_packets_per_stream)
1286 {
1287 int err, ret, saved_metadata_fd;
1288 enum lttng_error_code status = LTTNG_OK;
1289 struct consumer_socket *socket;
1290 struct lttng_ht_iter iter;
1291 struct ltt_kernel_metadata *saved_metadata;
1292 char *trace_path = NULL;
1293
1294 assert(ksess);
1295 assert(ksess->consumer);
1296 assert(output);
1297
1298 DBG("Kernel snapshot record started");
1299
1300 /* Save current metadata since the following calls will change it. */
1301 saved_metadata = ksess->metadata;
1302 saved_metadata_fd = ksess->metadata_stream_fd;
1303
1304 rcu_read_lock();
1305
1306 ret = kernel_open_metadata(ksess);
1307 if (ret < 0) {
1308 status = LTTNG_ERR_KERN_META_FAIL;
1309 goto error;
1310 }
1311
1312 ret = kernel_open_metadata_stream(ksess);
1313 if (ret < 0) {
1314 status = LTTNG_ERR_KERN_META_FAIL;
1315 goto error_open_stream;
1316 }
1317
1318 trace_path = setup_channel_trace_path(ksess->consumer,
1319 DEFAULT_KERNEL_TRACE_DIR);
1320 if (!trace_path) {
1321 status = LTTNG_ERR_INVALID;
1322 goto error;
1323 }
1324 /* Send metadata to consumer and snapshot everything. */
1325 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
1326 socket, node.node) {
1327 struct ltt_kernel_channel *chan;
1328
1329 pthread_mutex_lock(socket->lock);
1330 /* This stream must not be monitored by the consumer. */
1331 ret = kernel_consumer_add_metadata(socket, ksess, 0);
1332 pthread_mutex_unlock(socket->lock);
1333 if (ret < 0) {
1334 status = LTTNG_ERR_KERN_META_FAIL;
1335 goto error_consumer;
1336 }
1337
1338 /* For each channel, ask the consumer to snapshot it. */
1339 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1340 status = consumer_snapshot_channel(socket, chan->key, output, 0,
1341 ksess->uid, ksess->gid,
1342 trace_path, wait,
1343 nb_packets_per_stream);
1344 if (status != LTTNG_OK) {
1345 (void) kernel_consumer_destroy_metadata(socket,
1346 ksess->metadata);
1347 goto error_consumer;
1348 }
1349 }
1350
1351 /* Snapshot metadata, */
1352 status = consumer_snapshot_channel(socket, ksess->metadata->key, output,
1353 1, ksess->uid, ksess->gid, trace_path, wait, 0);
1354 if (status != LTTNG_OK) {
1355 goto error_consumer;
1356 }
1357
1358 /*
1359 * The metadata snapshot is done, ask the consumer to destroy it since
1360 * it's not monitored on the consumer side.
1361 */
1362 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
1363 }
1364
1365 error_consumer:
1366 /* Close newly opened metadata stream. It's now on the consumer side. */
1367 err = close(ksess->metadata_stream_fd);
1368 if (err < 0) {
1369 PERROR("close snapshot kernel");
1370 }
1371
1372 error_open_stream:
1373 trace_kernel_destroy_metadata(ksess->metadata);
1374 error:
1375 /* Restore metadata state.*/
1376 ksess->metadata = saved_metadata;
1377 ksess->metadata_stream_fd = saved_metadata_fd;
1378 rcu_read_unlock();
1379 free(trace_path);
1380 return status;
1381 }
1382
1383 /*
1384 * Get the syscall mask array from the kernel tracer.
1385 *
1386 * Return 0 on success else a negative value. In both case, syscall_mask should
1387 * be freed.
1388 */
1389 int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1390 {
1391 assert(syscall_mask);
1392 assert(nr_bits);
1393
1394 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1395 }
1396
1397 /*
1398 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1399 * version number.
1400 *
1401 * Return 1 on success, 0 when feature is not supported, negative value in case
1402 * of errors.
1403 */
1404 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1405 {
1406 int ret = 0; // Not supported by default
1407 struct lttng_kernel_tracer_abi_version abi;
1408
1409 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1410 if (ret < 0) {
1411 ERR("Failed to retrieve lttng-modules ABI version");
1412 goto error;
1413 }
1414
1415 /*
1416 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1417 */
1418 if (abi.major >= 2 && abi.minor >= 3) {
1419 /* Supported */
1420 ret = 1;
1421 } else {
1422 /* Not supported */
1423 ret = 0;
1424 }
1425 error:
1426 return ret;
1427 }
1428
1429 /*
1430 * Check for the support of the packet sequence number via abi version number.
1431 *
1432 * Return 1 on success, 0 when feature is not supported, negative value in case
1433 * of errors.
1434 */
1435 int kernel_supports_ring_buffer_packet_sequence_number(void)
1436 {
1437 int ret = 0; // Not supported by default
1438 struct lttng_kernel_tracer_abi_version abi;
1439
1440 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1441 if (ret < 0) {
1442 ERR("Failed to retrieve lttng-modules ABI version");
1443 goto error;
1444 }
1445
1446 /*
1447 * Packet sequence number was introduced in LTTng 2.8,
1448 * lttng-modules ABI 2.1.
1449 */
1450 if (abi.major >= 2 && abi.minor >= 1) {
1451 /* Supported */
1452 ret = 1;
1453 } else {
1454 /* Not supported */
1455 ret = 0;
1456 }
1457 error:
1458 return ret;
1459 }
1460
1461 /*
1462 * Rotate a kernel session.
1463 *
1464 * Return LTTNG_OK on success or else an LTTng error code.
1465 */
1466 enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
1467 {
1468 int ret;
1469 enum lttng_error_code status = LTTNG_OK;
1470 struct consumer_socket *socket;
1471 struct lttng_ht_iter iter;
1472 struct ltt_kernel_session *ksess = session->kernel_session;
1473
1474 assert(ksess);
1475 assert(ksess->consumer);
1476
1477 DBG("Rotate kernel session %s started (session %" PRIu64 ")",
1478 session->name, session->id);
1479
1480 rcu_read_lock();
1481
1482 /*
1483 * Note that this loop will end after one iteration given that there is
1484 * only one kernel consumer.
1485 */
1486 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1487 socket, node.node) {
1488 struct ltt_kernel_channel *chan;
1489
1490 /* For each channel, ask the consumer to rotate it. */
1491 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1492 DBG("Rotate kernel channel %" PRIu64 ", session %s",
1493 chan->key, session->name);
1494 ret = consumer_rotate_channel(socket, chan->key,
1495 ksess->uid, ksess->gid, ksess->consumer,
1496 /* is_metadata_channel */ false);
1497 if (ret < 0) {
1498 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
1499 goto error;
1500 }
1501 }
1502
1503 /*
1504 * Rotate the metadata channel.
1505 */
1506 ret = consumer_rotate_channel(socket, ksess->metadata->key,
1507 ksess->uid, ksess->gid, ksess->consumer,
1508 /* is_metadata_channel */ true);
1509 if (ret < 0) {
1510 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
1511 goto error;
1512 }
1513 }
1514
1515 error:
1516 rcu_read_unlock();
1517 return status;
1518 }
1519
1520 enum lttng_error_code kernel_create_channel_subdirectories(
1521 const struct ltt_kernel_session *ksess)
1522 {
1523 enum lttng_error_code ret = LTTNG_OK;
1524 enum lttng_trace_chunk_status chunk_status;
1525
1526 rcu_read_lock();
1527 assert(ksess->current_trace_chunk);
1528
1529 /*
1530 * Create the index subdirectory which will take care
1531 * of implicitly creating the channel's path.
1532 */
1533 chunk_status = lttng_trace_chunk_create_subdirectory(
1534 ksess->current_trace_chunk,
1535 DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR);
1536 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1537 ret = LTTNG_ERR_CREATE_DIR_FAIL;
1538 goto error;
1539 }
1540 error:
1541 rcu_read_unlock();
1542 return ret;
1543 }
1544
1545 /*
1546 * Setup necessary data for kernel tracer action.
1547 */
1548 LTTNG_HIDDEN
1549 int init_kernel_tracer(void)
1550 {
1551 int ret;
1552 bool is_root = !getuid();
1553
1554 /* Modprobe lttng kernel modules */
1555 ret = modprobe_lttng_control();
1556 if (ret < 0) {
1557 goto error;
1558 }
1559
1560 /* Open debugfs lttng */
1561 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1562 if (kernel_tracer_fd < 0) {
1563 DBG("Failed to open %s", module_proc_lttng);
1564 goto error_open;
1565 }
1566
1567 /* Validate kernel version */
1568 ret = kernel_validate_version(&kernel_tracer_version,
1569 &kernel_tracer_abi_version);
1570 if (ret < 0) {
1571 goto error_version;
1572 }
1573
1574 ret = modprobe_lttng_data();
1575 if (ret < 0) {
1576 goto error_modules;
1577 }
1578
1579 ret = kernel_supports_ring_buffer_snapshot_sample_positions();
1580 if (ret < 0) {
1581 goto error_modules;
1582 }
1583
1584 if (ret < 1) {
1585 WARN("Kernel tracer does not support buffer monitoring. "
1586 "The monitoring timer of channels in the kernel domain "
1587 "will be set to 0 (disabled).");
1588 }
1589
1590 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1591
1592 ret = syscall_init_table(kernel_tracer_fd);
1593 if (ret < 0) {
1594 ERR("Unable to populate syscall table. Syscall tracing won't "
1595 "work for this session daemon.");
1596 }
1597 return 0;
1598
1599 error_version:
1600 modprobe_remove_lttng_control();
1601 ret = close(kernel_tracer_fd);
1602 if (ret) {
1603 PERROR("close");
1604 }
1605 kernel_tracer_fd = -1;
1606 return LTTNG_ERR_KERN_VERSION;
1607
1608 error_modules:
1609 ret = close(kernel_tracer_fd);
1610 if (ret) {
1611 PERROR("close");
1612 }
1613
1614 error_open:
1615 modprobe_remove_lttng_control();
1616
1617 error:
1618 WARN("No kernel tracer available");
1619 kernel_tracer_fd = -1;
1620 if (!is_root) {
1621 return LTTNG_ERR_NEED_ROOT_SESSIOND;
1622 } else {
1623 return LTTNG_ERR_KERN_NA;
1624 }
1625 }
1626
1627 LTTNG_HIDDEN
1628 void cleanup_kernel_tracer(void)
1629 {
1630 int ret;
1631
1632 DBG2("Closing kernel fd");
1633 if (kernel_tracer_fd >= 0) {
1634 ret = close(kernel_tracer_fd);
1635 if (ret) {
1636 PERROR("close");
1637 }
1638 kernel_tracer_fd = -1;
1639 }
1640 DBG("Unloading kernel modules");
1641 modprobe_remove_lttng_all();
1642 free(syscall_table);
1643 }
1644
1645 LTTNG_HIDDEN
1646 bool kernel_tracer_is_initialized(void)
1647 {
1648 return kernel_tracer_fd >= 0;
1649 }
This page took 0.094458 seconds and 4 git commands to generate.