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