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