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