Fix: sessiond: kernel: invalid error code check
[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 PERROR("Failed to disable kernel event: name = '%s', fd = %d",
664 event->event->name, event->fd);
665 goto error;
666 }
667
668 event->enabled = 0;
669 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
670
671 return 0;
672
673 error:
674 return ret;
675 }
676
677
678 int kernel_track_pid(struct ltt_kernel_session *session, int pid)
679 {
680 int ret;
681
682 DBG("Kernel track PID %d for session id %" PRIu64 ".",
683 pid, session->id);
684 ret = kernctl_track_pid(session->fd, pid);
685 if (!ret) {
686 return LTTNG_OK;
687 }
688 switch (-ret) {
689 case EINVAL:
690 return LTTNG_ERR_INVALID;
691 case ENOMEM:
692 return LTTNG_ERR_NOMEM;
693 case EEXIST:
694 return LTTNG_ERR_PID_TRACKED;
695 default:
696 return LTTNG_ERR_UNK;
697 }
698 }
699
700 int kernel_untrack_pid(struct ltt_kernel_session *session, int pid)
701 {
702 int ret;
703
704 DBG("Kernel untrack PID %d for session id %" PRIu64 ".",
705 pid, session->id);
706 ret = kernctl_untrack_pid(session->fd, pid);
707 if (!ret) {
708 return LTTNG_OK;
709 }
710 switch (-ret) {
711 case EINVAL:
712 return LTTNG_ERR_INVALID;
713 case ENOMEM:
714 return LTTNG_ERR_NOMEM;
715 case ENOENT:
716 return LTTNG_ERR_PID_NOT_TRACKED;
717 default:
718 return LTTNG_ERR_UNK;
719 }
720 }
721
722 ssize_t kernel_list_tracker_pids(struct ltt_kernel_session *session,
723 int **_pids)
724 {
725 int fd, ret;
726 int pid;
727 ssize_t nbmem, count = 0;
728 FILE *fp;
729 int *pids;
730
731 fd = kernctl_list_tracker_pids(session->fd);
732 if (fd < 0) {
733 PERROR("kernel tracker pids list");
734 goto error;
735 }
736
737 fp = fdopen(fd, "r");
738 if (fp == NULL) {
739 PERROR("kernel tracker pids list fdopen");
740 goto error_fp;
741 }
742
743 nbmem = KERNEL_TRACKER_PIDS_INIT_LIST_SIZE;
744 pids = zmalloc(sizeof(*pids) * nbmem);
745 if (pids == NULL) {
746 PERROR("alloc list pids");
747 count = -ENOMEM;
748 goto end;
749 }
750
751 while (fscanf(fp, "process { pid = %u; };\n", &pid) == 1) {
752 if (count >= nbmem) {
753 int *new_pids;
754 size_t new_nbmem;
755
756 new_nbmem = nbmem << 1;
757 DBG("Reallocating pids list from %zu to %zu entries",
758 nbmem, new_nbmem);
759 new_pids = realloc(pids, new_nbmem * sizeof(*new_pids));
760 if (new_pids == NULL) {
761 PERROR("realloc list events");
762 free(pids);
763 count = -ENOMEM;
764 goto end;
765 }
766 /* Zero the new memory */
767 memset(new_pids + nbmem, 0,
768 (new_nbmem - nbmem) * sizeof(*new_pids));
769 nbmem = new_nbmem;
770 pids = new_pids;
771 }
772 pids[count++] = pid;
773 }
774
775 *_pids = pids;
776 DBG("Kernel list tracker pids done (%zd pids)", count);
777 end:
778 ret = fclose(fp); /* closes both fp and fd */
779 if (ret) {
780 PERROR("fclose");
781 }
782 return count;
783
784 error_fp:
785 ret = close(fd);
786 if (ret) {
787 PERROR("close");
788 }
789 error:
790 return -1;
791 }
792
793 /*
794 * Create kernel metadata, open from the kernel tracer and add it to the
795 * kernel session.
796 */
797 int kernel_open_metadata(struct ltt_kernel_session *session)
798 {
799 int ret;
800 struct ltt_kernel_metadata *lkm = NULL;
801
802 assert(session);
803
804 /* Allocate kernel metadata */
805 lkm = trace_kernel_create_metadata();
806 if (lkm == NULL) {
807 goto error;
808 }
809
810 /* Kernel tracer metadata creation */
811 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
812 if (ret < 0) {
813 goto error_open;
814 }
815
816 lkm->fd = ret;
817 lkm->key = ++next_kernel_channel_key;
818 /* Prevent fd duplication after execlp() */
819 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
820 if (ret < 0) {
821 PERROR("fcntl session fd");
822 }
823
824 session->metadata = lkm;
825
826 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
827
828 return 0;
829
830 error_open:
831 trace_kernel_destroy_metadata(lkm);
832 error:
833 return -1;
834 }
835
836 /*
837 * Start tracing session.
838 */
839 int kernel_start_session(struct ltt_kernel_session *session)
840 {
841 int ret;
842
843 assert(session);
844
845 ret = kernctl_start_session(session->fd);
846 if (ret < 0) {
847 PERROR("ioctl start session");
848 goto error;
849 }
850
851 DBG("Kernel session started");
852
853 return 0;
854
855 error:
856 return ret;
857 }
858
859 /*
860 * Make a kernel wait to make sure in-flight probe have completed.
861 */
862 void kernel_wait_quiescent(void)
863 {
864 int ret;
865 int fd = kernel_tracer_fd;
866
867 DBG("Kernel quiescent wait on %d", fd);
868
869 ret = kernctl_wait_quiescent(fd);
870 if (ret < 0) {
871 PERROR("wait quiescent ioctl");
872 ERR("Kernel quiescent wait failed");
873 }
874 }
875
876 /*
877 * Force flush buffer of metadata.
878 */
879 int kernel_metadata_flush_buffer(int fd)
880 {
881 int ret;
882
883 DBG("Kernel flushing metadata buffer on fd %d", fd);
884
885 ret = kernctl_buffer_flush(fd);
886 if (ret < 0) {
887 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
888 }
889
890 return 0;
891 }
892
893 /*
894 * Force flush buffer for channel.
895 */
896 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
897 {
898 int ret;
899 struct ltt_kernel_stream *stream;
900
901 assert(channel);
902
903 DBG("Flush buffer for channel %s", channel->channel->name);
904
905 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
906 DBG("Flushing channel stream %d", stream->fd);
907 ret = kernctl_buffer_flush(stream->fd);
908 if (ret < 0) {
909 PERROR("ioctl");
910 ERR("Fail to flush buffer for stream %d (ret: %d)",
911 stream->fd, ret);
912 }
913 }
914
915 return 0;
916 }
917
918 /*
919 * Stop tracing session.
920 */
921 int kernel_stop_session(struct ltt_kernel_session *session)
922 {
923 int ret;
924
925 assert(session);
926
927 ret = kernctl_stop_session(session->fd);
928 if (ret < 0) {
929 goto error;
930 }
931
932 DBG("Kernel session stopped");
933
934 return 0;
935
936 error:
937 return ret;
938 }
939
940 /*
941 * Open stream of channel, register it to the kernel tracer and add it
942 * to the stream list of the channel.
943 *
944 * Note: given that the streams may appear in random order wrt CPU
945 * number (e.g. cpu hotplug), the index value of the stream number in
946 * the stream name is not necessarily linked to the CPU number.
947 *
948 * Return the number of created stream. Else, a negative value.
949 */
950 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
951 {
952 int ret;
953 struct ltt_kernel_stream *lks;
954
955 assert(channel);
956
957 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
958 lks = trace_kernel_create_stream(channel->channel->name,
959 channel->stream_count);
960 if (lks == NULL) {
961 ret = close(ret);
962 if (ret) {
963 PERROR("close");
964 }
965 goto error;
966 }
967
968 lks->fd = ret;
969 /* Prevent fd duplication after execlp() */
970 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
971 if (ret < 0) {
972 PERROR("fcntl session fd");
973 }
974
975 lks->tracefile_size = channel->channel->attr.tracefile_size;
976 lks->tracefile_count = channel->channel->attr.tracefile_count;
977
978 /* Add stream to channel stream list */
979 cds_list_add(&lks->list, &channel->stream_list.head);
980 channel->stream_count++;
981
982 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
983 lks->state);
984 }
985
986 return channel->stream_count;
987
988 error:
989 return -1;
990 }
991
992 /*
993 * Open the metadata stream and set it to the kernel session.
994 */
995 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
996 {
997 int ret;
998
999 assert(session);
1000
1001 ret = kernctl_create_stream(session->metadata->fd);
1002 if (ret < 0) {
1003 PERROR("kernel create metadata stream");
1004 goto error;
1005 }
1006
1007 DBG("Kernel metadata stream created (fd: %d)", ret);
1008 session->metadata_stream_fd = ret;
1009 /* Prevent fd duplication after execlp() */
1010 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
1011 if (ret < 0) {
1012 PERROR("fcntl session fd");
1013 }
1014
1015 return 0;
1016
1017 error:
1018 return -1;
1019 }
1020
1021 /*
1022 * Get the event list from the kernel tracer and return the number of elements.
1023 */
1024 ssize_t kernel_list_events(struct lttng_event **events)
1025 {
1026 int fd, ret;
1027 char *event;
1028 size_t nbmem, count = 0;
1029 FILE *fp;
1030 struct lttng_event *elist;
1031
1032 assert(events);
1033
1034 fd = kernctl_tracepoint_list(kernel_tracer_fd);
1035 if (fd < 0) {
1036 PERROR("kernel tracepoint list");
1037 goto error;
1038 }
1039
1040 fp = fdopen(fd, "r");
1041 if (fp == NULL) {
1042 PERROR("kernel tracepoint list fdopen");
1043 goto error_fp;
1044 }
1045
1046 /*
1047 * Init memory size counter
1048 * See kernel-ctl.h for explanation of this value
1049 */
1050 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
1051 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
1052 if (elist == NULL) {
1053 PERROR("alloc list events");
1054 count = -ENOMEM;
1055 goto end;
1056 }
1057
1058 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
1059 if (count >= nbmem) {
1060 struct lttng_event *new_elist;
1061 size_t new_nbmem;
1062
1063 new_nbmem = nbmem << 1;
1064 DBG("Reallocating event list from %zu to %zu bytes",
1065 nbmem, new_nbmem);
1066 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
1067 if (new_elist == NULL) {
1068 PERROR("realloc list events");
1069 free(event);
1070 free(elist);
1071 count = -ENOMEM;
1072 goto end;
1073 }
1074 /* Zero the new memory */
1075 memset(new_elist + nbmem, 0,
1076 (new_nbmem - nbmem) * sizeof(struct lttng_event));
1077 nbmem = new_nbmem;
1078 elist = new_elist;
1079 }
1080 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
1081 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1082 elist[count].enabled = -1;
1083 count++;
1084 free(event);
1085 }
1086
1087 *events = elist;
1088 DBG("Kernel list events done (%zu events)", count);
1089 end:
1090 ret = fclose(fp); /* closes both fp and fd */
1091 if (ret) {
1092 PERROR("fclose");
1093 }
1094 return count;
1095
1096 error_fp:
1097 ret = close(fd);
1098 if (ret) {
1099 PERROR("close");
1100 }
1101 error:
1102 return -1;
1103 }
1104
1105 /*
1106 * Get kernel version and validate it.
1107 */
1108 int kernel_validate_version(struct lttng_kernel_tracer_version *version,
1109 struct lttng_kernel_tracer_abi_version *abi_version)
1110 {
1111 int ret;
1112
1113 ret = kernctl_tracer_version(kernel_tracer_fd, version);
1114 if (ret < 0) {
1115 ERR("Failed to retrieve the lttng-modules version");
1116 goto error;
1117 }
1118
1119 /* Validate version */
1120 if (version->major != VERSION_MAJOR) {
1121 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1122 version->major, VERSION_MAJOR);
1123 goto error_version;
1124 }
1125 ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version);
1126 if (ret < 0) {
1127 ERR("Failed to retrieve lttng-modules ABI version");
1128 goto error;
1129 }
1130 if (abi_version->major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
1131 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1132 abi_version->major, abi_version->minor,
1133 LTTNG_MODULES_ABI_MAJOR_VERSION);
1134 goto error;
1135 }
1136 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1137 version->major, version->minor,
1138 abi_version->major, abi_version->minor);
1139 return 0;
1140
1141 error_version:
1142 ret = -1;
1143
1144 error:
1145 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1146 return ret;
1147 }
1148
1149 /*
1150 * Kernel work-arounds called at the start of sessiond main().
1151 */
1152 int init_kernel_workarounds(void)
1153 {
1154 int ret;
1155 FILE *fp;
1156
1157 /*
1158 * boot_id needs to be read once before being used concurrently
1159 * to deal with a Linux kernel race. A fix is proposed for
1160 * upstream, but the work-around is needed for older kernels.
1161 */
1162 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
1163 if (!fp) {
1164 goto end_boot_id;
1165 }
1166 while (!feof(fp)) {
1167 char buf[37] = "";
1168
1169 ret = fread(buf, 1, sizeof(buf), fp);
1170 if (ret < 0) {
1171 /* Ignore error, we don't really care */
1172 }
1173 }
1174 ret = fclose(fp);
1175 if (ret) {
1176 PERROR("fclose");
1177 }
1178 end_boot_id:
1179 return 0;
1180 }
1181
1182 /*
1183 * Teardown of a kernel session, keeping data required by destroy notifiers.
1184 */
1185 void kernel_destroy_session(struct ltt_kernel_session *ksess)
1186 {
1187 struct lttng_trace_chunk *trace_chunk;
1188
1189 if (ksess == NULL) {
1190 DBG3("No kernel session when tearing down session");
1191 return;
1192 }
1193
1194 DBG("Tearing down kernel session");
1195 trace_chunk = ksess->current_trace_chunk;
1196
1197 /*
1198 * Destroy channels on the consumer if at least one FD has been sent and we
1199 * are in no output mode because the streams are in *no* monitor mode so we
1200 * have to send a command to clean them up or else they leaked.
1201 */
1202 if (!ksess->output_traces && ksess->consumer_fds_sent) {
1203 int ret;
1204 struct consumer_socket *socket;
1205 struct lttng_ht_iter iter;
1206
1207 /* For each consumer socket. */
1208 rcu_read_lock();
1209 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1210 socket, node.node) {
1211 struct ltt_kernel_channel *chan;
1212
1213 /* For each channel, ask the consumer to destroy it. */
1214 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1215 ret = kernel_consumer_destroy_channel(socket, chan);
1216 if (ret < 0) {
1217 /* Consumer is probably dead. Use next socket. */
1218 continue;
1219 }
1220 }
1221 }
1222 rcu_read_unlock();
1223 }
1224
1225 /* Close any relayd session */
1226 consumer_output_send_destroy_relayd(ksess->consumer);
1227
1228 trace_kernel_destroy_session(ksess);
1229 lttng_trace_chunk_put(trace_chunk);
1230 }
1231
1232 /* Teardown of data required by destroy notifiers. */
1233 void kernel_free_session(struct ltt_kernel_session *ksess)
1234 {
1235 if (ksess == NULL) {
1236 return;
1237 }
1238 trace_kernel_free_session(ksess);
1239 }
1240
1241 /*
1242 * Destroy a kernel channel object. It does not do anything on the tracer side.
1243 */
1244 void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
1245 {
1246 struct ltt_kernel_session *ksess = NULL;
1247
1248 assert(kchan);
1249 assert(kchan->channel);
1250
1251 DBG3("Kernel destroy channel %s", kchan->channel->name);
1252
1253 /* Update channel count of associated session. */
1254 if (kchan->session) {
1255 /* Keep pointer reference so we can update it after the destroy. */
1256 ksess = kchan->session;
1257 }
1258
1259 trace_kernel_destroy_channel(kchan);
1260
1261 /*
1262 * At this point the kernel channel is not visible anymore. This is safe
1263 * since in order to work on a visible kernel session, the tracing session
1264 * lock (ltt_session.lock) MUST be acquired.
1265 */
1266 if (ksess) {
1267 ksess->channel_count--;
1268 }
1269 }
1270
1271 /*
1272 * Take a snapshot for a given kernel session.
1273 *
1274 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1275 */
1276 enum lttng_error_code kernel_snapshot_record(
1277 struct ltt_kernel_session *ksess,
1278 const struct consumer_output *output, int wait,
1279 uint64_t nb_packets_per_stream)
1280 {
1281 int err, ret, saved_metadata_fd;
1282 enum lttng_error_code status = LTTNG_OK;
1283 struct consumer_socket *socket;
1284 struct lttng_ht_iter iter;
1285 struct ltt_kernel_metadata *saved_metadata;
1286 char *trace_path = NULL;
1287
1288 assert(ksess);
1289 assert(ksess->consumer);
1290 assert(output);
1291
1292 DBG("Kernel snapshot record started");
1293
1294 /* Save current metadata since the following calls will change it. */
1295 saved_metadata = ksess->metadata;
1296 saved_metadata_fd = ksess->metadata_stream_fd;
1297
1298 rcu_read_lock();
1299
1300 ret = kernel_open_metadata(ksess);
1301 if (ret < 0) {
1302 status = LTTNG_ERR_KERN_META_FAIL;
1303 goto error;
1304 }
1305
1306 ret = kernel_open_metadata_stream(ksess);
1307 if (ret < 0) {
1308 status = LTTNG_ERR_KERN_META_FAIL;
1309 goto error_open_stream;
1310 }
1311
1312 trace_path = setup_channel_trace_path(ksess->consumer,
1313 DEFAULT_KERNEL_TRACE_DIR);
1314 if (!trace_path) {
1315 status = LTTNG_ERR_INVALID;
1316 goto error;
1317 }
1318 /* Send metadata to consumer and snapshot everything. */
1319 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
1320 socket, node.node) {
1321 struct ltt_kernel_channel *chan;
1322
1323 pthread_mutex_lock(socket->lock);
1324 /* This stream must not be monitored by the consumer. */
1325 ret = kernel_consumer_add_metadata(socket, ksess, 0);
1326 pthread_mutex_unlock(socket->lock);
1327 if (ret < 0) {
1328 status = LTTNG_ERR_KERN_META_FAIL;
1329 goto error_consumer;
1330 }
1331
1332 /* For each channel, ask the consumer to snapshot it. */
1333 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1334 status = consumer_snapshot_channel(socket, chan->key, output, 0,
1335 ksess->uid, ksess->gid,
1336 trace_path, wait,
1337 nb_packets_per_stream);
1338 if (status != LTTNG_OK) {
1339 (void) kernel_consumer_destroy_metadata(socket,
1340 ksess->metadata);
1341 goto error_consumer;
1342 }
1343 }
1344
1345 /* Snapshot metadata, */
1346 status = consumer_snapshot_channel(socket, ksess->metadata->key, output,
1347 1, ksess->uid, ksess->gid, trace_path, wait, 0);
1348 if (status != LTTNG_OK) {
1349 goto error_consumer;
1350 }
1351
1352 /*
1353 * The metadata snapshot is done, ask the consumer to destroy it since
1354 * it's not monitored on the consumer side.
1355 */
1356 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
1357 }
1358
1359 error_consumer:
1360 /* Close newly opened metadata stream. It's now on the consumer side. */
1361 err = close(ksess->metadata_stream_fd);
1362 if (err < 0) {
1363 PERROR("close snapshot kernel");
1364 }
1365
1366 error_open_stream:
1367 trace_kernel_destroy_metadata(ksess->metadata);
1368 error:
1369 /* Restore metadata state.*/
1370 ksess->metadata = saved_metadata;
1371 ksess->metadata_stream_fd = saved_metadata_fd;
1372 rcu_read_unlock();
1373 free(trace_path);
1374 return status;
1375 }
1376
1377 /*
1378 * Get the syscall mask array from the kernel tracer.
1379 *
1380 * Return 0 on success else a negative value. In both case, syscall_mask should
1381 * be freed.
1382 */
1383 int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1384 {
1385 assert(syscall_mask);
1386 assert(nr_bits);
1387
1388 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1389 }
1390
1391 /*
1392 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1393 * version number.
1394 *
1395 * Return 1 on success, 0 when feature is not supported, negative value in case
1396 * of errors.
1397 */
1398 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1399 {
1400 int ret = 0; // Not supported by default
1401 struct lttng_kernel_tracer_abi_version abi;
1402
1403 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1404 if (ret < 0) {
1405 ERR("Failed to retrieve lttng-modules ABI version");
1406 goto error;
1407 }
1408
1409 /*
1410 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1411 */
1412 if (abi.major >= 2 && abi.minor >= 3) {
1413 /* Supported */
1414 ret = 1;
1415 } else {
1416 /* Not supported */
1417 ret = 0;
1418 }
1419 error:
1420 return ret;
1421 }
1422
1423 /*
1424 * Check for the support of the packet sequence number via abi version number.
1425 *
1426 * Return 1 on success, 0 when feature is not supported, negative value in case
1427 * of errors.
1428 */
1429 int kernel_supports_ring_buffer_packet_sequence_number(void)
1430 {
1431 int ret = 0; // Not supported by default
1432 struct lttng_kernel_tracer_abi_version abi;
1433
1434 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1435 if (ret < 0) {
1436 ERR("Failed to retrieve lttng-modules ABI version");
1437 goto error;
1438 }
1439
1440 /*
1441 * Packet sequence number was introduced in LTTng 2.8,
1442 * lttng-modules ABI 2.1.
1443 */
1444 if (abi.major >= 2 && abi.minor >= 1) {
1445 /* Supported */
1446 ret = 1;
1447 } else {
1448 /* Not supported */
1449 ret = 0;
1450 }
1451 error:
1452 return ret;
1453 }
1454
1455 /*
1456 * Rotate a kernel session.
1457 *
1458 * Return LTTNG_OK on success or else an LTTng error code.
1459 */
1460 enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
1461 {
1462 int ret;
1463 enum lttng_error_code status = LTTNG_OK;
1464 struct consumer_socket *socket;
1465 struct lttng_ht_iter iter;
1466 struct ltt_kernel_session *ksess = session->kernel_session;
1467
1468 assert(ksess);
1469 assert(ksess->consumer);
1470
1471 DBG("Rotate kernel session %s started (session %" PRIu64 ")",
1472 session->name, session->id);
1473
1474 rcu_read_lock();
1475
1476 /*
1477 * Note that this loop will end after one iteration given that there is
1478 * only one kernel consumer.
1479 */
1480 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1481 socket, node.node) {
1482 struct ltt_kernel_channel *chan;
1483
1484 /* For each channel, ask the consumer to rotate it. */
1485 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1486 DBG("Rotate kernel channel %" PRIu64 ", session %s",
1487 chan->key, session->name);
1488 ret = consumer_rotate_channel(socket, chan->key,
1489 ksess->uid, ksess->gid, ksess->consumer,
1490 /* is_metadata_channel */ false);
1491 if (ret < 0) {
1492 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
1493 goto error;
1494 }
1495 }
1496
1497 /*
1498 * Rotate the metadata channel.
1499 */
1500 ret = consumer_rotate_channel(socket, ksess->metadata->key,
1501 ksess->uid, ksess->gid, ksess->consumer,
1502 /* is_metadata_channel */ true);
1503 if (ret < 0) {
1504 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
1505 goto error;
1506 }
1507 }
1508
1509 error:
1510 rcu_read_unlock();
1511 return status;
1512 }
1513
1514 enum lttng_error_code kernel_create_channel_subdirectories(
1515 const struct ltt_kernel_session *ksess)
1516 {
1517 enum lttng_error_code ret = LTTNG_OK;
1518 enum lttng_trace_chunk_status chunk_status;
1519
1520 rcu_read_lock();
1521 assert(ksess->current_trace_chunk);
1522
1523 /*
1524 * Create the index subdirectory which will take care
1525 * of implicitly creating the channel's path.
1526 */
1527 chunk_status = lttng_trace_chunk_create_subdirectory(
1528 ksess->current_trace_chunk,
1529 DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR);
1530 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1531 ret = LTTNG_ERR_CREATE_DIR_FAIL;
1532 goto error;
1533 }
1534 error:
1535 rcu_read_unlock();
1536 return ret;
1537 }
1538
1539 /*
1540 * Setup necessary data for kernel tracer action.
1541 */
1542 LTTNG_HIDDEN
1543 int init_kernel_tracer(void)
1544 {
1545 int ret;
1546 bool is_root = !getuid();
1547
1548 /* Modprobe lttng kernel modules */
1549 ret = modprobe_lttng_control();
1550 if (ret < 0) {
1551 goto error;
1552 }
1553
1554 /* Open debugfs lttng */
1555 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1556 if (kernel_tracer_fd < 0) {
1557 DBG("Failed to open %s", module_proc_lttng);
1558 goto error_open;
1559 }
1560
1561 /* Validate kernel version */
1562 ret = kernel_validate_version(&kernel_tracer_version,
1563 &kernel_tracer_abi_version);
1564 if (ret < 0) {
1565 goto error_version;
1566 }
1567
1568 ret = modprobe_lttng_data();
1569 if (ret < 0) {
1570 goto error_modules;
1571 }
1572
1573 ret = kernel_supports_ring_buffer_snapshot_sample_positions();
1574 if (ret < 0) {
1575 goto error_modules;
1576 }
1577
1578 if (ret < 1) {
1579 WARN("Kernel tracer does not support buffer monitoring. "
1580 "The monitoring timer of channels in the kernel domain "
1581 "will be set to 0 (disabled).");
1582 }
1583
1584 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1585
1586 ret = syscall_init_table(kernel_tracer_fd);
1587 if (ret < 0) {
1588 ERR("Unable to populate syscall table. Syscall tracing won't "
1589 "work for this session daemon.");
1590 }
1591 return 0;
1592
1593 error_version:
1594 modprobe_remove_lttng_control();
1595 ret = close(kernel_tracer_fd);
1596 if (ret) {
1597 PERROR("close");
1598 }
1599 kernel_tracer_fd = -1;
1600 return LTTNG_ERR_KERN_VERSION;
1601
1602 error_modules:
1603 ret = close(kernel_tracer_fd);
1604 if (ret) {
1605 PERROR("close");
1606 }
1607
1608 error_open:
1609 modprobe_remove_lttng_control();
1610
1611 error:
1612 WARN("No kernel tracer available");
1613 kernel_tracer_fd = -1;
1614 if (!is_root) {
1615 return LTTNG_ERR_NEED_ROOT_SESSIOND;
1616 } else {
1617 return LTTNG_ERR_KERN_NA;
1618 }
1619 }
1620
1621 LTTNG_HIDDEN
1622 void cleanup_kernel_tracer(void)
1623 {
1624 int ret;
1625
1626 DBG2("Closing kernel fd");
1627 if (kernel_tracer_fd >= 0) {
1628 ret = close(kernel_tracer_fd);
1629 if (ret) {
1630 PERROR("close");
1631 }
1632 kernel_tracer_fd = -1;
1633 }
1634 DBG("Unloading kernel modules");
1635 modprobe_remove_lttng_all();
1636 free(syscall_table);
1637 }
1638
1639 LTTNG_HIDDEN
1640 bool kernel_tracer_is_initialized(void)
1641 {
1642 return kernel_tracer_fd >= 0;
1643 }
This page took 0.098744 seconds and 4 git commands to generate.