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