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