0922c3d490918f3a177a48cecd1b5b94772aa8cb
[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 close(ret);
495 goto error;
496 }
497
498 lks->fd = ret;
499 /* Prevent fd duplication after execlp() */
500 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
501 if (ret < 0) {
502 PERROR("fcntl session fd");
503 }
504
505 ret = asprintf(&lks->pathname, "%s/%s_%d",
506 channel->pathname, channel->channel->name, channel->stream_count);
507 if (ret < 0) {
508 PERROR("asprintf kernel create stream");
509 goto error;
510 }
511
512 /* Add stream to channe stream list */
513 cds_list_add(&lks->list, &channel->stream_list.head);
514 channel->stream_count++;
515
516 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
517 channel->stream_count, lks->fd, lks->state, lks->pathname);
518 }
519
520 return channel->stream_count;
521
522 error:
523 return -1;
524 }
525
526 /*
527 * Open the metadata stream and set it to the kernel session.
528 */
529 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
530 {
531 int ret;
532
533 ret = kernctl_create_stream(session->metadata->fd);
534 if (ret < 0) {
535 PERROR("kernel create metadata stream");
536 goto error;
537 }
538
539 DBG("Kernel metadata stream created (fd: %d)", ret);
540 session->metadata_stream_fd = ret;
541 /* Prevent fd duplication after execlp() */
542 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
543 if (ret < 0) {
544 PERROR("fcntl session fd");
545 }
546
547 return 0;
548
549 error:
550 return -1;
551 }
552
553 /*
554 * Get the event list from the kernel tracer and return the number of elements.
555 */
556 ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
557 {
558 int fd, pos;
559 char *event;
560 size_t nbmem, count = 0;
561 ssize_t size;
562 FILE *fp;
563 struct lttng_event *elist;
564
565 fd = kernctl_tracepoint_list(tracer_fd);
566 if (fd < 0) {
567 PERROR("kernel tracepoint list");
568 goto error;
569 }
570
571 fp = fdopen(fd, "r");
572 if (fp == NULL) {
573 PERROR("kernel tracepoint list fdopen");
574 goto error_fp;
575 }
576
577 /*
578 * Init memory size counter
579 * See kernel-ctl.h for explanation of this value
580 */
581 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
582 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
583
584 while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
585 if (count >= nbmem) {
586 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
587 nbmem * 2);
588 /* Double the size */
589 nbmem <<= 1;
590 elist = realloc(elist, nbmem * sizeof(struct lttng_event));
591 if (elist == NULL) {
592 PERROR("realloc list events");
593 count = -ENOMEM;
594 goto end;
595 }
596 }
597 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
598 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
599 elist[count].enabled = -1;
600 count++;
601 }
602
603 *events = elist;
604 DBG("Kernel list events done (%zu events)", count);
605 end:
606 fclose(fp); /* closes both fp and fd */
607 return count;
608
609 error_fp:
610 close(fd);
611 error:
612 return -1;
613 }
614
615 /*
616 * Get kernel version and validate it.
617 */
618 int kernel_validate_version(int tracer_fd)
619 {
620 int ret;
621 struct lttng_kernel_tracer_version version;
622
623 ret = kernctl_tracer_version(tracer_fd, &version);
624 if (ret < 0) {
625 ERR("Failed at getting the lttng-modules version");
626 goto error;
627 }
628
629 /* Validate version */
630 if (version.major != KERN_MODULES_PRE_MAJOR
631 && version.major != KERN_MODULES_MAJOR) {
632 goto error_version;
633 }
634
635 DBG2("Kernel tracer version validated (major version %d)", version.major);
636 return 0;
637
638 error_version:
639 ERR("Kernel major version %d is not compatible (supporting <= %d)",
640 version.major, KERN_MODULES_MAJOR)
641 ret = -1;
642
643 error:
644 return ret;
645 }
646
647 /*
648 * Kernel work-arounds called at the start of sessiond main().
649 */
650 int init_kernel_workarounds(void)
651 {
652 int ret;
653 FILE *fp;
654
655 /*
656 * boot_id needs to be read once before being used concurrently
657 * to deal with a Linux kernel race. A fix is proposed for
658 * upstream, but the work-around is needed for older kernels.
659 */
660 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
661 if (!fp) {
662 goto end_boot_id;
663 }
664 while (!feof(fp)) {
665 char buf[37] = "";
666
667 ret = fread(buf, 1, sizeof(buf), fp);
668 if (ret < 0) {
669 /* Ignore error, we don't really care */
670 }
671 }
672 fclose(fp);
673 end_boot_id:
674
675 return 0;
676 }
This page took 0.066896 seconds and 3 git commands to generate.