Refactoring: use an opaque lttng_tracker_id type
[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_id **saved_ids;
711 ssize_t saved_ids_count;
712
713 ret = lttng_tracker_id_lookup_string(tracker_type, id, &value);
714 if (ret != LTTNG_OK) {
715 return ret;
716 }
717
718 tracker_list = get_id_tracker_list(session, tracker_type);
719 if (!tracker_list) {
720 return LTTNG_ERR_INVALID;
721 }
722
723 /* Save list for restore on error. */
724 saved_ids_count = lttng_tracker_id_get_list(tracker_list, &saved_ids);
725 if (saved_ids_count < 0) {
726 return LTTNG_ERR_INVALID;
727 }
728
729 /* Add to list. */
730 ret = lttng_tracker_list_add(tracker_list, id);
731 if (ret != LTTNG_OK) {
732 goto end;
733 }
734
735 switch (tracker_type) {
736 case LTTNG_TRACKER_PID:
737 DBG("Kernel track PID %d for session id %" PRIu64 ".", value,
738 session->id);
739 ret = kernctl_track_pid(session->fd, value);
740 if (!ret) {
741 ret = LTTNG_OK;
742 goto end;
743 }
744 break;
745 case LTTNG_TRACKER_VPID:
746 DBG("Kernel track VPID %d for session id %" PRIu64 ".", value,
747 session->id);
748 ret = kernctl_track_id(session->fd, LTTNG_TRACKER_VPID, value);
749 if (!ret) {
750 ret = LTTNG_OK;
751 goto end;
752 }
753 break;
754 case LTTNG_TRACKER_UID:
755 DBG("Kernel track UID %d for session id %" PRIu64 ".", value,
756 session->id);
757 ret = kernctl_track_id(session->fd, LTTNG_TRACKER_UID, value);
758 if (!ret) {
759 ret = LTTNG_OK;
760 goto end;
761 }
762 break;
763 case LTTNG_TRACKER_GID:
764 DBG("Kernel track GID %d for session id %" PRIu64 ".", value,
765 session->id);
766 ret = kernctl_track_id(session->fd, LTTNG_TRACKER_GID, value);
767 if (!ret) {
768 ret = LTTNG_OK;
769 goto end;
770 }
771 break;
772 case LTTNG_TRACKER_VUID:
773 DBG("Kernel track VUID %d for session id %" PRIu64 ".", value,
774 session->id);
775 ret = kernctl_track_id(session->fd, LTTNG_TRACKER_VUID, value);
776 if (!ret) {
777 ret = LTTNG_OK;
778 goto end;
779 }
780 break;
781 case LTTNG_TRACKER_VGID:
782 DBG("Kernel track VGID %d for session id %" PRIu64 ".", value,
783 session->id);
784 ret = kernctl_track_id(session->fd, LTTNG_TRACKER_VGID, value);
785 if (!ret) {
786 ret = LTTNG_OK;
787 goto end;
788 }
789 break;
790 default:
791 ret = -EINVAL;
792 break;
793 }
794
795 /* Error handling. */
796 switch (-ret) {
797 case EINVAL:
798 ret = LTTNG_ERR_INVALID;
799 break;
800 case ENOMEM:
801 ret = LTTNG_ERR_NOMEM;
802 break;
803 case EEXIST:
804 ret = LTTNG_ERR_ID_TRACKED;
805 break;
806 default:
807 ret = LTTNG_ERR_UNK;
808 break;
809 }
810
811 if (lttng_tracker_id_set_list(tracker_list, saved_ids,
812 saved_ids_count) != LTTNG_OK) {
813 ERR("Error on tracker add error handling.\n");
814 }
815 end:
816 lttng_tracker_ids_destroy(saved_ids, saved_ids_count);
817 free(saved_ids);
818 return ret;
819 }
820
821 int kernel_untrack_id(enum lttng_tracker_type tracker_type,
822 struct ltt_kernel_session *session,
823 const struct lttng_tracker_id *id)
824 {
825 int ret, value;
826 struct lttng_tracker_list *tracker_list;
827 struct lttng_tracker_id **saved_ids;
828 ssize_t saved_ids_count;
829
830 ret = lttng_tracker_id_lookup_string(tracker_type, id, &value);
831 if (ret != LTTNG_OK) {
832 return ret;
833 }
834
835 tracker_list = get_id_tracker_list(session, tracker_type);
836 if (!tracker_list) {
837 return LTTNG_ERR_INVALID;
838 }
839 /* Save list for restore on error. */
840 saved_ids_count = lttng_tracker_id_get_list(tracker_list, &saved_ids);
841 if (saved_ids_count < 0) {
842 return LTTNG_ERR_INVALID;
843 }
844 /* Remove from list. */
845 ret = lttng_tracker_list_remove(tracker_list, id);
846 if (ret != LTTNG_OK) {
847 goto end;
848 }
849
850 switch (tracker_type) {
851 case LTTNG_TRACKER_PID:
852 DBG("Kernel untrack PID %d for session id %" PRIu64 ".", value,
853 session->id);
854 ret = kernctl_untrack_pid(session->fd, value);
855 if (!ret) {
856 ret = LTTNG_OK;
857 goto end;
858 }
859 break;
860 case LTTNG_TRACKER_VPID:
861 DBG("Kernel untrack VPID %d for session id %" PRIu64 ".", value,
862 session->id);
863 ret = kernctl_untrack_id(
864 session->fd, LTTNG_TRACKER_VPID, value);
865 if (!ret) {
866 ret = LTTNG_OK;
867 goto end;
868 }
869 break;
870 case LTTNG_TRACKER_UID:
871 DBG("Kernel untrack UID %d for session id %" PRIu64 ".", value,
872 session->id);
873 ret = kernctl_untrack_id(session->fd, LTTNG_TRACKER_UID, value);
874 if (!ret) {
875 ret = LTTNG_OK;
876 goto end;
877 }
878 break;
879 case LTTNG_TRACKER_GID:
880 DBG("Kernel untrack GID %d for session id %" PRIu64 ".", value,
881 session->id);
882 ret = kernctl_untrack_id(session->fd, LTTNG_TRACKER_GID, value);
883 if (!ret) {
884 ret = LTTNG_OK;
885 goto end;
886 }
887 break;
888 case LTTNG_TRACKER_VUID:
889 DBG("Kernel untrack VUID %d for session id %" PRIu64 ".", value,
890 session->id);
891 ret = kernctl_untrack_id(
892 session->fd, LTTNG_TRACKER_VUID, value);
893 if (!ret) {
894 ret = LTTNG_OK;
895 goto end;
896 }
897 break;
898 case LTTNG_TRACKER_VGID:
899 DBG("Kernel untrack VGID %d for session id %" PRIu64 ".", value,
900 session->id);
901 ret = kernctl_untrack_id(
902 session->fd, LTTNG_TRACKER_VGID, value);
903 if (!ret) {
904 ret = LTTNG_OK;
905 goto end;
906 }
907 break;
908 default:
909 ret = -EINVAL;
910 break;
911 }
912
913 /* Error handling. */
914 switch (-ret) {
915 case EINVAL:
916 ret = LTTNG_ERR_INVALID;
917 break;
918 case ENOMEM:
919 ret = LTTNG_ERR_NOMEM;
920 break;
921 case EEXIST:
922 ret = LTTNG_ERR_ID_TRACKED;
923 break;
924 default:
925 ret = LTTNG_ERR_UNK;
926 break;
927 }
928
929 if (lttng_tracker_id_set_list(tracker_list, saved_ids,
930 saved_ids_count) != LTTNG_OK) {
931 ERR("Error on tracker remove error handling.\n");
932 }
933 end:
934 lttng_tracker_ids_destroy(saved_ids, saved_ids_count);
935 free(saved_ids);
936 return ret;
937 }
938
939 /*
940 * Called with session lock held.
941 */
942 ssize_t kernel_list_tracker_ids(enum lttng_tracker_type tracker_type,
943 struct ltt_kernel_session *session,
944 struct lttng_tracker_id ***_ids)
945 {
946 struct lttng_tracker_list *tracker_list;
947
948 tracker_list = get_id_tracker_list(session, tracker_type);
949 if (!tracker_list) {
950 return -LTTNG_ERR_INVALID;
951 }
952 return lttng_tracker_id_get_list(tracker_list, _ids);
953 }
954
955 /*
956 * Create kernel metadata, open from the kernel tracer and add it to the
957 * kernel session.
958 */
959 int kernel_open_metadata(struct ltt_kernel_session *session)
960 {
961 int ret;
962 struct ltt_kernel_metadata *lkm = NULL;
963
964 assert(session);
965
966 /* Allocate kernel metadata */
967 lkm = trace_kernel_create_metadata();
968 if (lkm == NULL) {
969 goto error;
970 }
971
972 /* Kernel tracer metadata creation */
973 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
974 if (ret < 0) {
975 goto error_open;
976 }
977
978 lkm->fd = ret;
979 lkm->key = ++next_kernel_channel_key;
980 /* Prevent fd duplication after execlp() */
981 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
982 if (ret < 0) {
983 PERROR("fcntl session fd");
984 }
985
986 session->metadata = lkm;
987
988 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
989
990 return 0;
991
992 error_open:
993 trace_kernel_destroy_metadata(lkm);
994 error:
995 return -1;
996 }
997
998 /*
999 * Start tracing session.
1000 */
1001 int kernel_start_session(struct ltt_kernel_session *session)
1002 {
1003 int ret;
1004
1005 assert(session);
1006
1007 ret = kernctl_start_session(session->fd);
1008 if (ret < 0) {
1009 PERROR("ioctl start session");
1010 goto error;
1011 }
1012
1013 DBG("Kernel session started");
1014
1015 return 0;
1016
1017 error:
1018 return ret;
1019 }
1020
1021 /*
1022 * Make a kernel wait to make sure in-flight probe have completed.
1023 */
1024 void kernel_wait_quiescent(void)
1025 {
1026 int ret;
1027 int fd = kernel_tracer_fd;
1028
1029 DBG("Kernel quiescent wait on %d", fd);
1030
1031 ret = kernctl_wait_quiescent(fd);
1032 if (ret < 0) {
1033 PERROR("wait quiescent ioctl");
1034 ERR("Kernel quiescent wait failed");
1035 }
1036 }
1037
1038 /*
1039 * Force flush buffer of metadata.
1040 */
1041 int kernel_metadata_flush_buffer(int fd)
1042 {
1043 int ret;
1044
1045 DBG("Kernel flushing metadata buffer on fd %d", fd);
1046
1047 ret = kernctl_buffer_flush(fd);
1048 if (ret < 0) {
1049 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
1050 }
1051
1052 return 0;
1053 }
1054
1055 /*
1056 * Force flush buffer for channel.
1057 */
1058 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
1059 {
1060 int ret;
1061 struct ltt_kernel_stream *stream;
1062
1063 assert(channel);
1064
1065 DBG("Flush buffer for channel %s", channel->channel->name);
1066
1067 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
1068 DBG("Flushing channel stream %d", stream->fd);
1069 ret = kernctl_buffer_flush(stream->fd);
1070 if (ret < 0) {
1071 PERROR("ioctl");
1072 ERR("Fail to flush buffer for stream %d (ret: %d)",
1073 stream->fd, ret);
1074 }
1075 }
1076
1077 return 0;
1078 }
1079
1080 /*
1081 * Stop tracing session.
1082 */
1083 int kernel_stop_session(struct ltt_kernel_session *session)
1084 {
1085 int ret;
1086
1087 assert(session);
1088
1089 ret = kernctl_stop_session(session->fd);
1090 if (ret < 0) {
1091 goto error;
1092 }
1093
1094 DBG("Kernel session stopped");
1095
1096 return 0;
1097
1098 error:
1099 return ret;
1100 }
1101
1102 /*
1103 * Open stream of channel, register it to the kernel tracer and add it
1104 * to the stream list of the channel.
1105 *
1106 * Note: given that the streams may appear in random order wrt CPU
1107 * number (e.g. cpu hotplug), the index value of the stream number in
1108 * the stream name is not necessarily linked to the CPU number.
1109 *
1110 * Return the number of created stream. Else, a negative value.
1111 */
1112 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
1113 {
1114 int ret;
1115 struct ltt_kernel_stream *lks;
1116
1117 assert(channel);
1118
1119 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
1120 lks = trace_kernel_create_stream(channel->channel->name,
1121 channel->stream_count);
1122 if (lks == NULL) {
1123 ret = close(ret);
1124 if (ret) {
1125 PERROR("close");
1126 }
1127 goto error;
1128 }
1129
1130 lks->fd = ret;
1131 /* Prevent fd duplication after execlp() */
1132 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
1133 if (ret < 0) {
1134 PERROR("fcntl session fd");
1135 }
1136
1137 lks->tracefile_size = channel->channel->attr.tracefile_size;
1138 lks->tracefile_count = channel->channel->attr.tracefile_count;
1139
1140 /* Add stream to channel stream list */
1141 cds_list_add(&lks->list, &channel->stream_list.head);
1142 channel->stream_count++;
1143
1144 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
1145 lks->state);
1146 }
1147
1148 return channel->stream_count;
1149
1150 error:
1151 return -1;
1152 }
1153
1154 /*
1155 * Open the metadata stream and set it to the kernel session.
1156 */
1157 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
1158 {
1159 int ret;
1160
1161 assert(session);
1162
1163 ret = kernctl_create_stream(session->metadata->fd);
1164 if (ret < 0) {
1165 PERROR("kernel create metadata stream");
1166 goto error;
1167 }
1168
1169 DBG("Kernel metadata stream created (fd: %d)", ret);
1170 session->metadata_stream_fd = ret;
1171 /* Prevent fd duplication after execlp() */
1172 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
1173 if (ret < 0) {
1174 PERROR("fcntl session fd");
1175 }
1176
1177 return 0;
1178
1179 error:
1180 return -1;
1181 }
1182
1183 /*
1184 * Get the event list from the kernel tracer and return the number of elements.
1185 */
1186 ssize_t kernel_list_events(struct lttng_event **events)
1187 {
1188 int fd, ret;
1189 char *event;
1190 size_t nbmem, count = 0;
1191 FILE *fp;
1192 struct lttng_event *elist;
1193
1194 assert(events);
1195
1196 fd = kernctl_tracepoint_list(kernel_tracer_fd);
1197 if (fd < 0) {
1198 PERROR("kernel tracepoint list");
1199 goto error;
1200 }
1201
1202 fp = fdopen(fd, "r");
1203 if (fp == NULL) {
1204 PERROR("kernel tracepoint list fdopen");
1205 goto error_fp;
1206 }
1207
1208 /*
1209 * Init memory size counter
1210 * See kernel-ctl.h for explanation of this value
1211 */
1212 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
1213 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
1214 if (elist == NULL) {
1215 PERROR("alloc list events");
1216 count = -ENOMEM;
1217 goto end;
1218 }
1219
1220 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
1221 if (count >= nbmem) {
1222 struct lttng_event *new_elist;
1223 size_t new_nbmem;
1224
1225 new_nbmem = nbmem << 1;
1226 DBG("Reallocating event list from %zu to %zu bytes",
1227 nbmem, new_nbmem);
1228 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
1229 if (new_elist == NULL) {
1230 PERROR("realloc list events");
1231 free(event);
1232 free(elist);
1233 count = -ENOMEM;
1234 goto end;
1235 }
1236 /* Zero the new memory */
1237 memset(new_elist + nbmem, 0,
1238 (new_nbmem - nbmem) * sizeof(struct lttng_event));
1239 nbmem = new_nbmem;
1240 elist = new_elist;
1241 }
1242 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
1243 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1244 elist[count].enabled = -1;
1245 count++;
1246 free(event);
1247 }
1248
1249 *events = elist;
1250 DBG("Kernel list events done (%zu events)", count);
1251 end:
1252 ret = fclose(fp); /* closes both fp and fd */
1253 if (ret) {
1254 PERROR("fclose");
1255 }
1256 return count;
1257
1258 error_fp:
1259 ret = close(fd);
1260 if (ret) {
1261 PERROR("close");
1262 }
1263 error:
1264 return -1;
1265 }
1266
1267 /*
1268 * Get kernel version and validate it.
1269 */
1270 int kernel_validate_version(struct lttng_kernel_tracer_version *version,
1271 struct lttng_kernel_tracer_abi_version *abi_version)
1272 {
1273 int ret;
1274
1275 ret = kernctl_tracer_version(kernel_tracer_fd, version);
1276 if (ret < 0) {
1277 ERR("Failed to retrieve the lttng-modules version");
1278 goto error;
1279 }
1280
1281 /* Validate version */
1282 if (version->major != VERSION_MAJOR) {
1283 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1284 version->major, VERSION_MAJOR);
1285 goto error_version;
1286 }
1287 ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version);
1288 if (ret < 0) {
1289 ERR("Failed to retrieve lttng-modules ABI version");
1290 goto error;
1291 }
1292 if (abi_version->major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
1293 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1294 abi_version->major, abi_version->minor,
1295 LTTNG_MODULES_ABI_MAJOR_VERSION);
1296 goto error;
1297 }
1298 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1299 version->major, version->minor,
1300 abi_version->major, abi_version->minor);
1301 return 0;
1302
1303 error_version:
1304 ret = -1;
1305
1306 error:
1307 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1308 return ret;
1309 }
1310
1311 /*
1312 * Kernel work-arounds called at the start of sessiond main().
1313 */
1314 int init_kernel_workarounds(void)
1315 {
1316 int ret;
1317 FILE *fp;
1318
1319 /*
1320 * boot_id needs to be read once before being used concurrently
1321 * to deal with a Linux kernel race. A fix is proposed for
1322 * upstream, but the work-around is needed for older kernels.
1323 */
1324 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
1325 if (!fp) {
1326 goto end_boot_id;
1327 }
1328 while (!feof(fp)) {
1329 char buf[37] = "";
1330
1331 ret = fread(buf, 1, sizeof(buf), fp);
1332 if (ret < 0) {
1333 /* Ignore error, we don't really care */
1334 }
1335 }
1336 ret = fclose(fp);
1337 if (ret) {
1338 PERROR("fclose");
1339 }
1340 end_boot_id:
1341 return 0;
1342 }
1343
1344 /*
1345 * Teardown of a kernel session, keeping data required by destroy notifiers.
1346 */
1347 void kernel_destroy_session(struct ltt_kernel_session *ksess)
1348 {
1349 struct lttng_trace_chunk *trace_chunk;
1350
1351 if (ksess == NULL) {
1352 DBG3("No kernel session when tearing down session");
1353 return;
1354 }
1355
1356 DBG("Tearing down kernel session");
1357 trace_chunk = ksess->current_trace_chunk;
1358
1359 /*
1360 * Destroy channels on the consumer if at least one FD has been sent and we
1361 * are in no output mode because the streams are in *no* monitor mode so we
1362 * have to send a command to clean them up or else they leaked.
1363 */
1364 if (!ksess->output_traces && ksess->consumer_fds_sent) {
1365 int ret;
1366 struct consumer_socket *socket;
1367 struct lttng_ht_iter iter;
1368
1369 /* For each consumer socket. */
1370 rcu_read_lock();
1371 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1372 socket, node.node) {
1373 struct ltt_kernel_channel *chan;
1374
1375 /* For each channel, ask the consumer to destroy it. */
1376 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1377 ret = kernel_consumer_destroy_channel(socket, chan);
1378 if (ret < 0) {
1379 /* Consumer is probably dead. Use next socket. */
1380 continue;
1381 }
1382 }
1383 }
1384 rcu_read_unlock();
1385 }
1386
1387 /* Close any relayd session */
1388 consumer_output_send_destroy_relayd(ksess->consumer);
1389
1390 trace_kernel_destroy_session(ksess);
1391 lttng_trace_chunk_put(trace_chunk);
1392 }
1393
1394 /* Teardown of data required by destroy notifiers. */
1395 void kernel_free_session(struct ltt_kernel_session *ksess)
1396 {
1397 if (ksess == NULL) {
1398 return;
1399 }
1400 trace_kernel_free_session(ksess);
1401 }
1402
1403 /*
1404 * Destroy a kernel channel object. It does not do anything on the tracer side.
1405 */
1406 void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
1407 {
1408 struct ltt_kernel_session *ksess = NULL;
1409
1410 assert(kchan);
1411 assert(kchan->channel);
1412
1413 DBG3("Kernel destroy channel %s", kchan->channel->name);
1414
1415 /* Update channel count of associated session. */
1416 if (kchan->session) {
1417 /* Keep pointer reference so we can update it after the destroy. */
1418 ksess = kchan->session;
1419 }
1420
1421 trace_kernel_destroy_channel(kchan);
1422
1423 /*
1424 * At this point the kernel channel is not visible anymore. This is safe
1425 * since in order to work on a visible kernel session, the tracing session
1426 * lock (ltt_session.lock) MUST be acquired.
1427 */
1428 if (ksess) {
1429 ksess->channel_count--;
1430 }
1431 }
1432
1433 /*
1434 * Take a snapshot for a given kernel session.
1435 *
1436 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1437 */
1438 enum lttng_error_code kernel_snapshot_record(
1439 struct ltt_kernel_session *ksess,
1440 const struct consumer_output *output, int wait,
1441 uint64_t nb_packets_per_stream)
1442 {
1443 int err, ret, saved_metadata_fd;
1444 enum lttng_error_code status = LTTNG_OK;
1445 struct consumer_socket *socket;
1446 struct lttng_ht_iter iter;
1447 struct ltt_kernel_metadata *saved_metadata;
1448 char *trace_path = NULL;
1449 size_t consumer_path_offset = 0;
1450
1451 assert(ksess);
1452 assert(ksess->consumer);
1453 assert(output);
1454
1455 DBG("Kernel snapshot record started");
1456
1457 /* Save current metadata since the following calls will change it. */
1458 saved_metadata = ksess->metadata;
1459 saved_metadata_fd = ksess->metadata_stream_fd;
1460
1461 rcu_read_lock();
1462
1463 ret = kernel_open_metadata(ksess);
1464 if (ret < 0) {
1465 status = LTTNG_ERR_KERN_META_FAIL;
1466 goto error;
1467 }
1468
1469 ret = kernel_open_metadata_stream(ksess);
1470 if (ret < 0) {
1471 status = LTTNG_ERR_KERN_META_FAIL;
1472 goto error_open_stream;
1473 }
1474
1475 trace_path = setup_channel_trace_path(ksess->consumer,
1476 DEFAULT_KERNEL_TRACE_DIR, &consumer_path_offset);
1477 if (!trace_path) {
1478 status = LTTNG_ERR_INVALID;
1479 goto error;
1480 }
1481 /* Send metadata to consumer and snapshot everything. */
1482 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
1483 socket, node.node) {
1484 struct ltt_kernel_channel *chan;
1485
1486 pthread_mutex_lock(socket->lock);
1487 /* This stream must not be monitored by the consumer. */
1488 ret = kernel_consumer_add_metadata(socket, ksess, 0);
1489 pthread_mutex_unlock(socket->lock);
1490 if (ret < 0) {
1491 status = LTTNG_ERR_KERN_META_FAIL;
1492 goto error_consumer;
1493 }
1494
1495 /* For each channel, ask the consumer to snapshot it. */
1496 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1497 status = consumer_snapshot_channel(socket, chan->key, output, 0,
1498 ksess->uid, ksess->gid,
1499 &trace_path[consumer_path_offset], wait,
1500 nb_packets_per_stream);
1501 if (status != LTTNG_OK) {
1502 (void) kernel_consumer_destroy_metadata(socket,
1503 ksess->metadata);
1504 goto error_consumer;
1505 }
1506 }
1507
1508 /* Snapshot metadata, */
1509 status = consumer_snapshot_channel(socket, ksess->metadata->key, output,
1510 1, ksess->uid, ksess->gid, &trace_path[consumer_path_offset],
1511 wait, 0);
1512 if (status != LTTNG_OK) {
1513 goto error_consumer;
1514 }
1515
1516 /*
1517 * The metadata snapshot is done, ask the consumer to destroy it since
1518 * it's not monitored on the consumer side.
1519 */
1520 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
1521 }
1522
1523 error_consumer:
1524 /* Close newly opened metadata stream. It's now on the consumer side. */
1525 err = close(ksess->metadata_stream_fd);
1526 if (err < 0) {
1527 PERROR("close snapshot kernel");
1528 }
1529
1530 error_open_stream:
1531 trace_kernel_destroy_metadata(ksess->metadata);
1532 error:
1533 /* Restore metadata state.*/
1534 ksess->metadata = saved_metadata;
1535 ksess->metadata_stream_fd = saved_metadata_fd;
1536 rcu_read_unlock();
1537 free(trace_path);
1538 return status;
1539 }
1540
1541 /*
1542 * Get the syscall mask array from the kernel tracer.
1543 *
1544 * Return 0 on success else a negative value. In both case, syscall_mask should
1545 * be freed.
1546 */
1547 int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1548 {
1549 assert(syscall_mask);
1550 assert(nr_bits);
1551
1552 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1553 }
1554
1555 /*
1556 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1557 * version number.
1558 *
1559 * Return 1 on success, 0 when feature is not supported, negative value in case
1560 * of errors.
1561 */
1562 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1563 {
1564 int ret = 0; // Not supported by default
1565 struct lttng_kernel_tracer_abi_version abi;
1566
1567 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1568 if (ret < 0) {
1569 ERR("Failed to retrieve lttng-modules ABI version");
1570 goto error;
1571 }
1572
1573 /*
1574 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1575 */
1576 if (abi.major >= 2 && abi.minor >= 3) {
1577 /* Supported */
1578 ret = 1;
1579 } else {
1580 /* Not supported */
1581 ret = 0;
1582 }
1583 error:
1584 return ret;
1585 }
1586
1587 /*
1588 * Check for the support of the packet sequence number via abi version number.
1589 *
1590 * Return 1 on success, 0 when feature is not supported, negative value in case
1591 * of errors.
1592 */
1593 int kernel_supports_ring_buffer_packet_sequence_number(void)
1594 {
1595 int ret = 0; // Not supported by default
1596 struct lttng_kernel_tracer_abi_version abi;
1597
1598 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1599 if (ret < 0) {
1600 ERR("Failed to retrieve lttng-modules ABI version");
1601 goto error;
1602 }
1603
1604 /*
1605 * Packet sequence number was introduced in LTTng 2.8,
1606 * lttng-modules ABI 2.1.
1607 */
1608 if (abi.major >= 2 && abi.minor >= 1) {
1609 /* Supported */
1610 ret = 1;
1611 } else {
1612 /* Not supported */
1613 ret = 0;
1614 }
1615 error:
1616 return ret;
1617 }
1618
1619 /*
1620 * Rotate a kernel session.
1621 *
1622 * Return LTTNG_OK on success or else an LTTng error code.
1623 */
1624 enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
1625 {
1626 int ret;
1627 enum lttng_error_code status = LTTNG_OK;
1628 struct consumer_socket *socket;
1629 struct lttng_ht_iter iter;
1630 struct ltt_kernel_session *ksess = session->kernel_session;
1631
1632 assert(ksess);
1633 assert(ksess->consumer);
1634
1635 DBG("Rotate kernel session %s started (session %" PRIu64 ")",
1636 session->name, session->id);
1637
1638 rcu_read_lock();
1639
1640 /*
1641 * Note that this loop will end after one iteration given that there is
1642 * only one kernel consumer.
1643 */
1644 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1645 socket, node.node) {
1646 struct ltt_kernel_channel *chan;
1647
1648 /* For each channel, ask the consumer to rotate it. */
1649 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1650 DBG("Rotate kernel channel %" PRIu64 ", session %s",
1651 chan->key, session->name);
1652 ret = consumer_rotate_channel(socket, chan->key,
1653 ksess->uid, ksess->gid, ksess->consumer,
1654 /* is_metadata_channel */ false);
1655 if (ret < 0) {
1656 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
1657 goto error;
1658 }
1659 }
1660
1661 /*
1662 * Rotate the metadata channel.
1663 */
1664 ret = consumer_rotate_channel(socket, ksess->metadata->key,
1665 ksess->uid, ksess->gid, ksess->consumer,
1666 /* is_metadata_channel */ true);
1667 if (ret < 0) {
1668 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
1669 goto error;
1670 }
1671 }
1672
1673 error:
1674 rcu_read_unlock();
1675 return status;
1676 }
1677
1678 enum lttng_error_code kernel_create_channel_subdirectories(
1679 const struct ltt_kernel_session *ksess)
1680 {
1681 enum lttng_error_code ret = LTTNG_OK;
1682 enum lttng_trace_chunk_status chunk_status;
1683
1684 rcu_read_lock();
1685 assert(ksess->current_trace_chunk);
1686
1687 /*
1688 * Create the index subdirectory which will take care
1689 * of implicitly creating the channel's path.
1690 */
1691 chunk_status = lttng_trace_chunk_create_subdirectory(
1692 ksess->current_trace_chunk,
1693 DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR);
1694 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1695 ret = LTTNG_ERR_CREATE_DIR_FAIL;
1696 goto error;
1697 }
1698 error:
1699 rcu_read_unlock();
1700 return ret;
1701 }
1702
1703 /*
1704 * Setup necessary data for kernel tracer action.
1705 */
1706 LTTNG_HIDDEN
1707 int init_kernel_tracer(void)
1708 {
1709 int ret;
1710 bool is_root = !getuid();
1711
1712 /* Modprobe lttng kernel modules */
1713 ret = modprobe_lttng_control();
1714 if (ret < 0) {
1715 goto error;
1716 }
1717
1718 /* Open debugfs lttng */
1719 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1720 if (kernel_tracer_fd < 0) {
1721 DBG("Failed to open %s", module_proc_lttng);
1722 goto error_open;
1723 }
1724
1725 /* Validate kernel version */
1726 ret = kernel_validate_version(&kernel_tracer_version,
1727 &kernel_tracer_abi_version);
1728 if (ret < 0) {
1729 goto error_version;
1730 }
1731
1732 ret = modprobe_lttng_data();
1733 if (ret < 0) {
1734 goto error_modules;
1735 }
1736
1737 ret = kernel_supports_ring_buffer_snapshot_sample_positions();
1738 if (ret < 0) {
1739 goto error_modules;
1740 }
1741
1742 if (ret < 1) {
1743 WARN("Kernel tracer does not support buffer monitoring. "
1744 "The monitoring timer of channels in the kernel domain "
1745 "will be set to 0 (disabled).");
1746 }
1747
1748 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1749
1750 ret = syscall_init_table(kernel_tracer_fd);
1751 if (ret < 0) {
1752 ERR("Unable to populate syscall table. Syscall tracing won't "
1753 "work for this session daemon.");
1754 }
1755 return 0;
1756
1757 error_version:
1758 modprobe_remove_lttng_control();
1759 ret = close(kernel_tracer_fd);
1760 if (ret) {
1761 PERROR("close");
1762 }
1763 kernel_tracer_fd = -1;
1764 return LTTNG_ERR_KERN_VERSION;
1765
1766 error_modules:
1767 ret = close(kernel_tracer_fd);
1768 if (ret) {
1769 PERROR("close");
1770 }
1771
1772 error_open:
1773 modprobe_remove_lttng_control();
1774
1775 error:
1776 WARN("No kernel tracer available");
1777 kernel_tracer_fd = -1;
1778 if (!is_root) {
1779 return LTTNG_ERR_NEED_ROOT_SESSIOND;
1780 } else {
1781 return LTTNG_ERR_KERN_NA;
1782 }
1783 }
1784
1785 LTTNG_HIDDEN
1786 void cleanup_kernel_tracer(void)
1787 {
1788 int ret;
1789
1790 DBG2("Closing kernel fd");
1791 if (kernel_tracer_fd >= 0) {
1792 ret = close(kernel_tracer_fd);
1793 if (ret) {
1794 PERROR("close");
1795 }
1796 kernel_tracer_fd = -1;
1797 }
1798 DBG("Unloading kernel modules");
1799 modprobe_remove_lttng_all();
1800 free(syscall_table);
1801 }
1802
1803 LTTNG_HIDDEN
1804 bool kernel_tracer_is_initialized(void)
1805 {
1806 return kernel_tracer_fd >= 0;
1807 }
1808
1809 /*
1810 * Clear a kernel session.
1811 *
1812 * Return LTTNG_OK on success or else an LTTng error code.
1813 */
1814 enum lttng_error_code kernel_clear_session(struct ltt_session *session)
1815 {
1816 int ret;
1817 enum lttng_error_code status = LTTNG_OK;
1818 struct consumer_socket *socket;
1819 struct lttng_ht_iter iter;
1820 struct ltt_kernel_session *ksess = session->kernel_session;
1821
1822 assert(ksess);
1823 assert(ksess->consumer);
1824
1825 DBG("Clear kernel session %s (session %" PRIu64 ")",
1826 session->name, session->id);
1827
1828 rcu_read_lock();
1829
1830 if (ksess->active) {
1831 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
1832 status = LTTNG_ERR_FATAL;
1833 goto end;
1834 }
1835
1836 /*
1837 * Note that this loop will end after one iteration given that there is
1838 * only one kernel consumer.
1839 */
1840 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1841 socket, node.node) {
1842 struct ltt_kernel_channel *chan;
1843
1844 /* For each channel, ask the consumer to clear it. */
1845 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1846 DBG("Clear kernel channel %" PRIu64 ", session %s",
1847 chan->key, session->name);
1848 ret = consumer_clear_channel(socket, chan->key);
1849 if (ret < 0) {
1850 goto error;
1851 }
1852 }
1853
1854 if (!ksess->metadata) {
1855 /*
1856 * Nothing to do for the metadata.
1857 * This is a snapshot session.
1858 * The metadata is genererated on the fly.
1859 */
1860 continue;
1861 }
1862
1863 /*
1864 * Clear the metadata channel.
1865 * Metadata channel is not cleared per se but we still need to
1866 * perform a rotation operation on it behind the scene.
1867 */
1868 ret = consumer_clear_channel(socket, ksess->metadata->key);
1869 if (ret < 0) {
1870 goto error;
1871 }
1872 }
1873
1874 goto end;
1875 error:
1876 switch (-ret) {
1877 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
1878 status = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
1879 break;
1880 default:
1881 status = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
1882 break;
1883 }
1884 end:
1885 rcu_read_unlock();
1886 return status;
1887 }
This page took 0.116481 seconds and 4 git commands to generate.