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