Add consumer socket object and relayd commands
[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 _GNU_SOURCE
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <common/common.h>
27 #include <common/kernel-ctl/kernel-ctl.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29
30 #include "kernel.h"
31 #include "kern-modules.h"
32
33 /*
34 * Add context on a kernel channel.
35 */
36 int kernel_add_channel_context(struct ltt_kernel_channel *chan,
37 struct lttng_kernel_context *ctx)
38 {
39 int ret;
40
41 DBG("Adding context to channel %s", chan->channel->name);
42 ret = kernctl_add_context(chan->fd, ctx);
43 if (ret < 0) {
44 if (errno != EEXIST) {
45 PERROR("add context ioctl");
46 } else {
47 /* If EEXIST, we just ignore the error */
48 ret = 0;
49 }
50 goto error;
51 }
52
53 chan->ctx = zmalloc(sizeof(struct lttng_kernel_context));
54 if (chan->ctx == NULL) {
55 PERROR("zmalloc event context");
56 goto error;
57 }
58
59 memcpy(chan->ctx, ctx, sizeof(struct lttng_kernel_context));
60
61 return 0;
62
63 error:
64 return ret;
65 }
66
67 /*
68 * Add context on a kernel event.
69 */
70 int kernel_add_event_context(struct ltt_kernel_event *event,
71 struct lttng_kernel_context *ctx)
72 {
73 int ret;
74
75 DBG("Adding context to event %s", event->event->name);
76 ret = kernctl_add_context(event->fd, ctx);
77 if (ret < 0) {
78 PERROR("add context ioctl");
79 goto error;
80 }
81
82 event->ctx = zmalloc(sizeof(struct lttng_kernel_context));
83 if (event->ctx == NULL) {
84 PERROR("zmalloc event context");
85 goto error;
86 }
87
88 memcpy(event->ctx, ctx, sizeof(struct lttng_kernel_context));
89
90 return 0;
91
92 error:
93 return ret;
94 }
95
96 /*
97 * Create a new kernel session, register it to the kernel tracer and add it to
98 * the session daemon session.
99 */
100 int kernel_create_session(struct ltt_session *session, int tracer_fd)
101 {
102 int ret;
103 struct ltt_kernel_session *lks;
104
105 /* Allocate data structure */
106 lks = trace_kernel_create_session(session->path);
107 if (lks == NULL) {
108 ret = -1;
109 goto error;
110 }
111
112 /* Kernel tracer session creation */
113 ret = kernctl_create_session(tracer_fd);
114 if (ret < 0) {
115 PERROR("ioctl kernel create session");
116 goto error;
117 }
118
119 lks->fd = ret;
120 /* Prevent fd duplication after execlp() */
121 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
122 if (ret < 0) {
123 PERROR("fcntl session fd");
124 }
125
126 lks->consumer_fds_sent = 0;
127 session->kernel_session = lks;
128
129 DBG("Kernel session created (fd: %d)", lks->fd);
130
131 return 0;
132
133 error:
134 return ret;
135 }
136
137 /*
138 * Create a kernel channel, register it to the kernel tracer and add it to the
139 * kernel session.
140 */
141 int kernel_create_channel(struct ltt_kernel_session *session,
142 struct lttng_channel *chan, char *path)
143 {
144 int ret;
145 struct ltt_kernel_channel *lkc;
146
147 /* Allocate kernel channel */
148 lkc = trace_kernel_create_channel(chan, path);
149 if (lkc == NULL) {
150 goto error;
151 }
152
153 DBG3("Kernel create channel %s in %s with attr: %d, %zu, %zu, %u, %u, %d",
154 chan->name, path, lkc->channel->attr.overwrite,
155 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
156 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
157 lkc->channel->attr.output);
158
159 /* Kernel tracer channel creation */
160 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
161 if (ret < 0) {
162 PERROR("ioctl kernel create channel");
163 goto error;
164 }
165
166 /* Setup the channel fd */
167 lkc->fd = ret;
168 /* Prevent fd duplication after execlp() */
169 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
170 if (ret < 0) {
171 PERROR("fcntl session fd");
172 }
173
174 /* Add channel to session */
175 cds_list_add(&lkc->list, &session->channel_list.head);
176 session->channel_count++;
177
178 DBG("Kernel channel %s created (fd: %d)", lkc->channel->name, lkc->fd);
179
180 return 0;
181
182 error:
183 return -1;
184 }
185
186 /*
187 * Create a kernel event, enable it to the kernel tracer and add it to the
188 * channel event list of the kernel session.
189 */
190 int kernel_create_event(struct lttng_event *ev,
191 struct ltt_kernel_channel *channel)
192 {
193 int ret;
194 struct ltt_kernel_event *event;
195
196 event = trace_kernel_create_event(ev);
197 if (event == NULL) {
198 ret = -1;
199 goto error;
200 }
201
202 ret = kernctl_create_event(channel->fd, event->event);
203 if (ret < 0) {
204 switch (errno) {
205 case EEXIST:
206 break;
207 case ENOSYS:
208 WARN("Event type not implemented");
209 break;
210 default:
211 PERROR("create event ioctl");
212 }
213 ret = -errno;
214 goto free_event;
215 }
216
217 /*
218 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
219 */
220 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
221 DBG2("Kernel event syscall creation success");
222 /*
223 * We use fd == -1 to ensure that we never trigger a close of fd
224 * 0.
225 */
226 event->fd = -1;
227 goto add_list;
228 }
229
230 event->fd = ret;
231 /* Prevent fd duplication after execlp() */
232 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
233 if (ret < 0) {
234 PERROR("fcntl session fd");
235 }
236
237 add_list:
238 /* Add event to event list */
239 cds_list_add(&event->list, &channel->events_list.head);
240 channel->event_count++;
241
242 DBG("Event %s created (fd: %d)", ev->name, event->fd);
243
244 return 0;
245
246 free_event:
247 free(event);
248 error:
249 return ret;
250 }
251
252 /*
253 * Disable a kernel channel.
254 */
255 int kernel_disable_channel(struct ltt_kernel_channel *chan)
256 {
257 int ret;
258
259 ret = kernctl_disable(chan->fd);
260 if (ret < 0) {
261 PERROR("disable chan ioctl");
262 ret = errno;
263 goto error;
264 }
265
266 chan->enabled = 0;
267 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
268
269 return 0;
270
271 error:
272 return ret;
273 }
274
275 /*
276 * Enable a kernel channel.
277 */
278 int kernel_enable_channel(struct ltt_kernel_channel *chan)
279 {
280 int ret;
281
282 ret = kernctl_enable(chan->fd);
283 if (ret < 0 && errno != EEXIST) {
284 PERROR("Enable kernel chan");
285 goto error;
286 }
287
288 chan->enabled = 1;
289 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
290
291 return 0;
292
293 error:
294 return ret;
295 }
296
297 /*
298 * Enable a kernel event.
299 */
300 int kernel_enable_event(struct ltt_kernel_event *event)
301 {
302 int ret;
303
304 ret = kernctl_enable(event->fd);
305 if (ret < 0) {
306 switch (errno) {
307 case EEXIST:
308 ret = LTTCOMM_KERN_EVENT_EXIST;
309 break;
310 default:
311 PERROR("enable kernel event");
312 break;
313 }
314 goto error;
315 }
316
317 event->enabled = 1;
318 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
319
320 return 0;
321
322 error:
323 return ret;
324 }
325
326 /*
327 * Disable a kernel event.
328 */
329 int kernel_disable_event(struct ltt_kernel_event *event)
330 {
331 int ret;
332
333 ret = kernctl_disable(event->fd);
334 if (ret < 0) {
335 switch (errno) {
336 case EEXIST:
337 ret = LTTCOMM_KERN_EVENT_EXIST;
338 break;
339 default:
340 PERROR("disable kernel event");
341 break;
342 }
343 goto error;
344 }
345
346 event->enabled = 0;
347 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
348
349 return 0;
350
351 error:
352 return ret;
353 }
354
355 /*
356 * Create kernel metadata, open from the kernel tracer and add it to the
357 * kernel session.
358 */
359 int kernel_open_metadata(struct ltt_kernel_session *session, char *path)
360 {
361 int ret;
362 struct ltt_kernel_metadata *lkm;
363
364 /* Allocate kernel metadata */
365 lkm = trace_kernel_create_metadata(path);
366 if (lkm == NULL) {
367 goto error;
368 }
369
370 /* Kernel tracer metadata creation */
371 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
372 if (ret < 0) {
373 goto error;
374 }
375
376 lkm->fd = ret;
377 /* Prevent fd duplication after execlp() */
378 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
379 if (ret < 0) {
380 PERROR("fcntl session fd");
381 }
382
383 session->metadata = lkm;
384
385 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
386
387 return 0;
388
389 error:
390 return -1;
391 }
392
393 /*
394 * Start tracing session.
395 */
396 int kernel_start_session(struct ltt_kernel_session *session)
397 {
398 int ret;
399
400 ret = kernctl_start_session(session->fd);
401 if (ret < 0) {
402 PERROR("ioctl start session");
403 goto error;
404 }
405
406 DBG("Kernel session started");
407
408 return 0;
409
410 error:
411 return ret;
412 }
413
414 /*
415 * Make a kernel wait to make sure in-flight probe have completed.
416 */
417 void kernel_wait_quiescent(int fd)
418 {
419 int ret;
420
421 DBG("Kernel quiescent wait on %d", fd);
422
423 ret = kernctl_wait_quiescent(fd);
424 if (ret < 0) {
425 PERROR("wait quiescent ioctl");
426 ERR("Kernel quiescent wait failed");
427 }
428 }
429
430 /*
431 * Kernel calibrate
432 */
433 int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
434 {
435 int ret;
436
437 ret = kernctl_calibrate(fd, calibrate);
438 if (ret < 0) {
439 PERROR("calibrate ioctl");
440 return -1;
441 }
442
443 return 0;
444 }
445
446
447 /*
448 * Force flush buffer of metadata.
449 */
450 int kernel_metadata_flush_buffer(int fd)
451 {
452 int ret;
453
454 ret = kernctl_buffer_flush(fd);
455 if (ret < 0) {
456 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
457 }
458
459 return 0;
460 }
461
462 /*
463 * Force flush buffer for channel.
464 */
465 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
466 {
467 int ret;
468 struct ltt_kernel_stream *stream;
469
470 DBG("Flush buffer for channel %s", channel->channel->name);
471
472 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
473 DBG("Flushing channel stream %d", stream->fd);
474 ret = kernctl_buffer_flush(stream->fd);
475 if (ret < 0) {
476 PERROR("ioctl");
477 ERR("Fail to flush buffer for stream %d (ret: %d)",
478 stream->fd, ret);
479 }
480 }
481
482 return 0;
483 }
484
485 /*
486 * Stop tracing session.
487 */
488 int kernel_stop_session(struct ltt_kernel_session *session)
489 {
490 int ret;
491
492 ret = kernctl_stop_session(session->fd);
493 if (ret < 0) {
494 goto error;
495 }
496
497 DBG("Kernel session stopped");
498
499 return 0;
500
501 error:
502 return ret;
503 }
504
505 /*
506 * Open stream of channel, register it to the kernel tracer and add it
507 * to the stream list of the channel.
508 *
509 * Return the number of created stream. Else, a negative value.
510 */
511 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
512 {
513 int ret, count = 0;
514 struct ltt_kernel_stream *lks;
515
516 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
517 lks = trace_kernel_create_stream(channel->channel->name, count);
518 if (lks == NULL) {
519 ret = close(ret);
520 if (ret) {
521 PERROR("close");
522 }
523 goto error;
524 }
525
526 lks->fd = ret;
527 /* Prevent fd duplication after execlp() */
528 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
529 if (ret < 0) {
530 PERROR("fcntl session fd");
531 }
532
533 /* Add stream to channe stream list */
534 cds_list_add(&lks->list, &channel->stream_list.head);
535 channel->stream_count++;
536
537 /* Increment counter which represent CPU number. */
538 count++;
539
540 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
541 lks->state);
542 }
543
544 return channel->stream_count;
545
546 error:
547 return -1;
548 }
549
550 /*
551 * Open the metadata stream and set it to the kernel session.
552 */
553 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
554 {
555 int ret;
556
557 ret = kernctl_create_stream(session->metadata->fd);
558 if (ret < 0) {
559 PERROR("kernel create metadata stream");
560 goto error;
561 }
562
563 DBG("Kernel metadata stream created (fd: %d)", ret);
564 session->metadata_stream_fd = ret;
565 /* Prevent fd duplication after execlp() */
566 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
567 if (ret < 0) {
568 PERROR("fcntl session fd");
569 }
570
571 return 0;
572
573 error:
574 return -1;
575 }
576
577 /*
578 * Get the event list from the kernel tracer and return the number of elements.
579 */
580 ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
581 {
582 int fd, pos, ret;
583 char *event;
584 size_t nbmem, count = 0;
585 ssize_t size;
586 FILE *fp;
587 struct lttng_event *elist;
588
589 fd = kernctl_tracepoint_list(tracer_fd);
590 if (fd < 0) {
591 PERROR("kernel tracepoint list");
592 goto error;
593 }
594
595 fp = fdopen(fd, "r");
596 if (fp == NULL) {
597 PERROR("kernel tracepoint list fdopen");
598 goto error_fp;
599 }
600
601 /*
602 * Init memory size counter
603 * See kernel-ctl.h for explanation of this value
604 */
605 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
606 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
607 if (elist == NULL) {
608 PERROR("alloc list events");
609 count = -ENOMEM;
610 goto end;
611 }
612
613 while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
614 if (count >= nbmem) {
615 struct lttng_event *new_elist;
616
617 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
618 nbmem * 2);
619 /* Double the size */
620 nbmem <<= 1;
621 new_elist = realloc(elist, nbmem * sizeof(struct lttng_event));
622 if (new_elist == NULL) {
623 PERROR("realloc list events");
624 free(event);
625 free(elist);
626 count = -ENOMEM;
627 goto end;
628 }
629 elist = new_elist;
630 }
631 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
632 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
633 elist[count].enabled = -1;
634 count++;
635 free(event);
636 }
637
638 *events = elist;
639 DBG("Kernel list events done (%zu events)", count);
640 end:
641 ret = fclose(fp); /* closes both fp and fd */
642 if (ret) {
643 PERROR("fclose");
644 }
645 return count;
646
647 error_fp:
648 ret = close(fd);
649 if (ret) {
650 PERROR("close");
651 }
652 error:
653 return -1;
654 }
655
656 /*
657 * Get kernel version and validate it.
658 */
659 int kernel_validate_version(int tracer_fd)
660 {
661 int ret;
662 struct lttng_kernel_tracer_version version;
663
664 ret = kernctl_tracer_version(tracer_fd, &version);
665 if (ret < 0) {
666 ERR("Failed at getting the lttng-modules version");
667 goto error;
668 }
669
670 /* Validate version */
671 if (version.major != KERN_MODULES_PRE_MAJOR
672 && version.major != KERN_MODULES_MAJOR) {
673 goto error_version;
674 }
675
676 DBG2("Kernel tracer version validated (major version %d)", version.major);
677 return 0;
678
679 error_version:
680 ERR("Kernel major version %d is not compatible (supporting <= %d)",
681 version.major, KERN_MODULES_MAJOR)
682 ret = -1;
683
684 error:
685 return ret;
686 }
687
688 /*
689 * Kernel work-arounds called at the start of sessiond main().
690 */
691 int init_kernel_workarounds(void)
692 {
693 int ret;
694 FILE *fp;
695
696 /*
697 * boot_id needs to be read once before being used concurrently
698 * to deal with a Linux kernel race. A fix is proposed for
699 * upstream, but the work-around is needed for older kernels.
700 */
701 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
702 if (!fp) {
703 goto end_boot_id;
704 }
705 while (!feof(fp)) {
706 char buf[37] = "";
707
708 ret = fread(buf, 1, sizeof(buf), fp);
709 if (ret < 0) {
710 /* Ignore error, we don't really care */
711 }
712 }
713 ret = fclose(fp);
714 if (ret) {
715 PERROR("fclose");
716 }
717 end_boot_id:
718 return 0;
719 }
This page took 0.043391 seconds and 4 git commands to generate.