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