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