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