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