ABI update: only send loglevel value to UST: fixed loglevels
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.c
CommitLineData
20fe2104
DG
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
20fe2104
DG
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
8c0faa1d 19#define _GNU_SOURCE
20fe2104 20#include <errno.h>
7b395890 21#include <fcntl.h>
20fe2104
DG
22#include <stdlib.h>
23#include <stdio.h>
f34daff7 24#include <string.h>
8c0faa1d 25#include <unistd.h>
20fe2104 26
990570ed 27#include <common/common.h>
db758600 28#include <common/kernel-ctl/kernel-ctl.h>
1e307fab 29
4771f025 30#include "kernel.h"
096102bd 31#include "kern-modules.h"
20fe2104 32
d65106b1 33/*
050349bb 34 * Add context on a kernel channel.
d65106b1
DG
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) {
b579acd9
DG
44 if (errno != EEXIST) {
45 perror("add context ioctl");
46 } else {
47 /* If EEXIST, we just ignore the error */
48 ret = 0;
49 }
d65106b1
DG
50 goto error;
51 }
52
ba7f0ae5 53 chan->ctx = zmalloc(sizeof(struct lttng_kernel_context));
d65106b1 54 if (chan->ctx == NULL) {
ba7f0ae5 55 perror("zmalloc event context");
d65106b1
DG
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/*
050349bb 68 * Add context on a kernel event.
d65106b1
DG
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
ba7f0ae5 82 event->ctx = zmalloc(sizeof(struct lttng_kernel_context));
d65106b1 83 if (event->ctx == NULL) {
ba7f0ae5 84 perror("zmalloc event context");
d65106b1
DG
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
20fe2104 96/*
050349bb
DG
97 * Create a new kernel session, register it to the kernel tracer and add it to
98 * the session daemon session.
20fe2104 99 */
8c0faa1d 100int kernel_create_session(struct ltt_session *session, int tracer_fd)
20fe2104
DG
101{
102 int ret;
103 struct ltt_kernel_session *lks;
104
54012638 105 /* Allocate data structure */
f9815039 106 lks = trace_kernel_create_session(session->path);
20fe2104 107 if (lks == NULL) {
54012638 108 ret = -1;
20fe2104
DG
109 goto error;
110 }
111
54012638 112 /* Kernel tracer session creation */
20fe2104
DG
113 ret = kernctl_create_session(tracer_fd);
114 if (ret < 0) {
54012638 115 perror("ioctl kernel create session");
20fe2104
DG
116 goto error;
117 }
118
20fe2104 119 lks->fd = ret;
7b395890
DG
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
3bd1e081 126 lks->consumer_fds_sent = 0;
8c0faa1d 127 session->kernel_session = lks;
8c0faa1d
DG
128
129 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104
DG
130
131 return 0;
132
133error:
134 return ret;
135}
136
137/*
050349bb
DG
138 * Create a kernel channel, register it to the kernel tracer and add it to the
139 * kernel session.
20fe2104 140 */
050349bb
DG
141int kernel_create_channel(struct ltt_kernel_session *session,
142 struct lttng_channel *chan, char *path)
20fe2104
DG
143{
144 int ret;
145 struct ltt_kernel_channel *lkc;
20fe2104 146
54012638 147 /* Allocate kernel channel */
62499ad6 148 lkc = trace_kernel_create_channel(chan, path);
54012638 149 if (lkc == NULL) {
20fe2104
DG
150 goto error;
151 }
152
54012638 153 /* Kernel tracer channel creation */
f3ed775e 154 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 155 if (ret < 0) {
54012638 156 perror("ioctl kernel create channel");
20fe2104
DG
157 goto error;
158 }
159
54012638 160 /* Setup the channel fd */
20fe2104 161 lkc->fd = ret;
7b395890
DG
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
54012638 168 /* Add channel to session */
8c0faa1d
DG
169 cds_list_add(&lkc->list, &session->channel_list.head);
170 session->channel_count++;
20fe2104 171
f3ed775e
DG
172 DBG("Kernel channel %s created (fd: %d and path: %s)",
173 lkc->channel->name, lkc->fd, lkc->pathname);
20fe2104
DG
174
175 return 0;
176
177error:
54012638 178 return -1;
20fe2104 179}
f34daff7
DG
180
181/*
050349bb
DG
182 * Create a kernel event, enable it to the kernel tracer and add it to the
183 * channel event list of the kernel session.
f34daff7 184 */
050349bb
DG
185int kernel_create_event(struct lttng_event *ev,
186 struct ltt_kernel_channel *channel)
f34daff7
DG
187{
188 int ret;
189 struct ltt_kernel_event *event;
f34daff7 190
62499ad6 191 event = trace_kernel_create_event(ev);
54012638 192 if (event == NULL) {
d87bfb32 193 ret = -1;
f34daff7
DG
194 goto error;
195 }
196
f3ed775e
DG
197 ret = kernctl_create_event(channel->fd, event->event);
198 if (ret < 0) {
d87bfb32
DG
199 if (errno != EEXIST) {
200 PERROR("create event ioctl");
201 }
202 ret = -errno;
e953ef25 203 goto free_event;
8c0faa1d 204 }
f34daff7 205
5f822d0a
DG
206 /*
207 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success. However
208 * this FD must not be added to the event list.
209 */
210 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
211 DBG2("Kernel event syscall creation success");
212 goto end;
213 }
214
f3ed775e 215 event->fd = ret;
7b395890
DG
216 /* Prevent fd duplication after execlp() */
217 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
218 if (ret < 0) {
219 perror("fcntl session fd");
220 }
221
f3ed775e
DG
222 /* Add event to event list */
223 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
224 channel->event_count++;
225
e953ef25
DG
226 DBG("Event %s created (fd: %d)", ev->name, event->fd);
227
5f822d0a 228end:
e953ef25
DG
229 return 0;
230
231free_event:
232 free(event);
233error:
d87bfb32 234 return ret;
e953ef25
DG
235}
236
26cc6b4e 237/*
050349bb 238 * Disable a kernel channel.
26cc6b4e
DG
239 */
240int kernel_disable_channel(struct ltt_kernel_channel *chan)
241{
242 int ret;
243
244 ret = kernctl_disable(chan->fd);
245 if (ret < 0) {
246 perror("disable chan ioctl");
247 ret = errno;
248 goto error;
249 }
250
251 chan->enabled = 0;
252 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
253
254 return 0;
255
256error:
257 return ret;
258}
259
d36b8583 260/*
050349bb 261 * Enable a kernel channel.
d36b8583
DG
262 */
263int kernel_enable_channel(struct ltt_kernel_channel *chan)
264{
265 int ret;
266
267 ret = kernctl_enable(chan->fd);
54d01ffb
DG
268 if (ret < 0 && errno != EEXIST) {
269 perror("Enable kernel chan");
d36b8583
DG
270 goto error;
271 }
272
273 chan->enabled = 1;
274 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
275
276 return 0;
277
278error:
279 return ret;
280}
281
19e70852 282/*
050349bb 283 * Enable a kernel event.
19e70852
DG
284 */
285int kernel_enable_event(struct ltt_kernel_event *event)
286{
287 int ret;
288
289 ret = kernctl_enable(event->fd);
54d01ffb
DG
290 if (ret < 0 && errno != EEXIST) {
291 perror("enable kernel event");
19e70852
DG
292 goto error;
293 }
294
295 event->enabled = 1;
296 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
297
298 return 0;
299
300error:
d36b8583 301 return ret;
19e70852
DG
302}
303
e953ef25 304/*
050349bb 305 * Disable a kernel event.
e953ef25 306 */
19e70852 307int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
308{
309 int ret;
19e70852
DG
310
311 ret = kernctl_disable(event->fd);
54d01ffb
DG
312 if (ret < 0 && errno != EEXIST) {
313 perror("disable kernel event");
19e70852 314 goto error;
e953ef25 315 }
f3ed775e 316
19e70852
DG
317 event->enabled = 0;
318 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
319
f34daff7
DG
320 return 0;
321
322error:
d36b8583 323 return ret;
f34daff7 324}
aaf26714
DG
325
326/*
050349bb
DG
327 * Create kernel metadata, open from the kernel tracer and add it to the
328 * kernel session.
aaf26714 329 */
58a97671 330int kernel_open_metadata(struct ltt_kernel_session *session, char *path)
aaf26714
DG
331{
332 int ret;
333 struct ltt_kernel_metadata *lkm;
aaf26714 334
54012638 335 /* Allocate kernel metadata */
62499ad6 336 lkm = trace_kernel_create_metadata(path);
54012638 337 if (lkm == NULL) {
aaf26714
DG
338 goto error;
339 }
340
54012638 341 /* Kernel tracer metadata creation */
f3ed775e 342 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714
DG
343 if (ret < 0) {
344 goto error;
345 }
346
8c0faa1d 347 lkm->fd = ret;
7b395890
DG
348 /* Prevent fd duplication after execlp() */
349 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
350 if (ret < 0) {
351 perror("fcntl session fd");
352 }
353
aaf26714 354 session->metadata = lkm;
8c0faa1d 355
54012638 356 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
8c0faa1d
DG
357
358 return 0;
359
360error:
54012638 361 return -1;
8c0faa1d
DG
362}
363
364/*
050349bb 365 * Start tracing session.
8c0faa1d
DG
366 */
367int kernel_start_session(struct ltt_kernel_session *session)
368{
369 int ret;
370
371 ret = kernctl_start_session(session->fd);
372 if (ret < 0) {
f3ed775e 373 perror("ioctl start session");
8c0faa1d
DG
374 goto error;
375 }
376
377 DBG("Kernel session started");
378
379 return 0;
380
381error:
382 return ret;
383}
384
f3ed775e 385/*
050349bb 386 * Make a kernel wait to make sure in-flight probe have completed.
f3ed775e
DG
387 */
388void kernel_wait_quiescent(int fd)
389{
390 int ret;
391
392 DBG("Kernel quiescent wait on %d", fd);
393
394 ret = kernctl_wait_quiescent(fd);
395 if (ret < 0) {
396 perror("wait quiescent ioctl");
397 ERR("Kernel quiescent wait failed");
398 }
399}
400
d0254c7c 401/*
050349bb 402 * Kernel calibrate
d0254c7c
MD
403 */
404int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
405{
406 int ret;
407
408 ret = kernctl_calibrate(fd, calibrate);
409 if (ret < 0) {
410 perror("calibrate ioctl");
411 return -1;
412 }
413
414 return 0;
415}
416
417
f3ed775e 418/*
f3ed775e
DG
419 * Force flush buffer of metadata.
420 */
421int kernel_metadata_flush_buffer(int fd)
422{
423 int ret;
424
425 ret = kernctl_buffer_flush(fd);
426 if (ret < 0) {
427 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
428 }
429
430 return 0;
431}
432
433/*
050349bb 434 * Force flush buffer for channel.
f3ed775e
DG
435 */
436int kernel_flush_buffer(struct ltt_kernel_channel *channel)
437{
438 int ret;
439 struct ltt_kernel_stream *stream;
440
441 DBG("Flush buffer for channel %s", channel->channel->name);
442
443 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
444 DBG("Flushing channel stream %d", stream->fd);
445 ret = kernctl_buffer_flush(stream->fd);
446 if (ret < 0) {
447 perror("ioctl");
448 ERR("Fail to flush buffer for stream %d (ret: %d)",
449 stream->fd, ret);
450 }
451 }
452
453 return 0;
454}
455
8c0faa1d 456/*
050349bb 457 * Stop tracing session.
8c0faa1d
DG
458 */
459int kernel_stop_session(struct ltt_kernel_session *session)
460{
461 int ret;
462
463 ret = kernctl_stop_session(session->fd);
464 if (ret < 0) {
465 goto error;
466 }
467
468 DBG("Kernel session stopped");
469
470 return 0;
471
472error:
473 return ret;
474}
475
476/*
050349bb
DG
477 * Open stream of channel, register it to the kernel tracer and add it
478 * to the stream list of the channel.
8c0faa1d 479 *
050349bb 480 * Return the number of created stream. Else, a negative value.
8c0faa1d 481 */
f3ed775e 482int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d
DG
483{
484 int ret;
485 struct ltt_kernel_stream *lks;
486
487 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
62499ad6 488 lks = trace_kernel_create_stream();
8c0faa1d 489 if (lks == NULL) {
54012638 490 close(ret);
8c0faa1d
DG
491 goto error;
492 }
493
494 lks->fd = ret;
7b395890
DG
495 /* Prevent fd duplication after execlp() */
496 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
497 if (ret < 0) {
498 perror("fcntl session fd");
499 }
500
8e68d1c8
DG
501 ret = asprintf(&lks->pathname, "%s/%s_%d",
502 channel->pathname, channel->channel->name, channel->stream_count);
8c0faa1d
DG
503 if (ret < 0) {
504 perror("asprintf kernel create stream");
505 goto error;
506 }
8c0faa1d 507
54012638 508 /* Add stream to channe stream list */
8c0faa1d
DG
509 cds_list_add(&lks->list, &channel->stream_list.head);
510 channel->stream_count++;
8c0faa1d 511
54012638
DG
512 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
513 channel->stream_count, lks->fd, lks->state, lks->pathname);
514 }
8c0faa1d
DG
515
516 return channel->stream_count;
517
518error:
54012638 519 return -1;
8c0faa1d
DG
520}
521
522/*
050349bb 523 * Open the metadata stream and set it to the kernel session.
8c0faa1d 524 */
f3ed775e 525int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
526{
527 int ret;
528
529 ret = kernctl_create_stream(session->metadata->fd);
530 if (ret < 0) {
531 perror("kernel create metadata stream");
8c0faa1d
DG
532 goto error;
533 }
534
535 DBG("Kernel metadata stream created (fd: %d)", ret);
536 session->metadata_stream_fd = ret;
7b395890
DG
537 /* Prevent fd duplication after execlp() */
538 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
539 if (ret < 0) {
540 perror("fcntl session fd");
541 }
aaf26714
DG
542
543 return 0;
544
545error:
54012638 546 return -1;
aaf26714 547}
2ef84c95
DG
548
549/*
9f19cc17 550 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 551 */
9f19cc17 552ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
2ef84c95 553{
9f19cc17
DG
554 int fd, pos;
555 char *event;
556 size_t nbmem, count = 0;
2ef84c95
DG
557 ssize_t size;
558 FILE *fp;
9f19cc17 559 struct lttng_event *elist;
2ef84c95
DG
560
561 fd = kernctl_tracepoint_list(tracer_fd);
562 if (fd < 0) {
563 perror("kernel tracepoint list");
564 goto error;
565 }
566
567 fp = fdopen(fd, "r");
568 if (fp == NULL) {
569 perror("kernel tracepoint list fdopen");
61b73b12 570 goto error_fp;
2ef84c95
DG
571 }
572
573 /*
574 * Init memory size counter
575 * See kernel-ctl.h for explanation of this value
576 */
6725fe19 577 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
ba7f0ae5 578 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
2ef84c95 579
9f19cc17 580 while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
6725fe19 581 if (count >= nbmem) {
ced2f820 582 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
6725fe19
DG
583 nbmem * 2);
584 /* Double the size */
585 nbmem <<= 1;
2f221590 586 elist = realloc(elist, nbmem * sizeof(struct lttng_event));
9f19cc17 587 if (elist == NULL) {
2ef84c95 588 perror("realloc list events");
61b73b12
MD
589 count = -ENOMEM;
590 goto end;
2ef84c95
DG
591 }
592 }
99497cd0
MD
593 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
594 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
67b9d018 595 elist[count].enabled = -1;
9f19cc17 596 count++;
2ef84c95
DG
597 }
598
9f19cc17 599 *events = elist;
ced2f820 600 DBG("Kernel list events done (%zu events)", count);
61b73b12
MD
601end:
602 fclose(fp); /* closes both fp and fd */
9f19cc17 603 return count;
2ef84c95 604
61b73b12
MD
605error_fp:
606 close(fd);
2ef84c95
DG
607error:
608 return -1;
609}
096102bd
DG
610
611/*
612 * Get kernel version and validate it.
613 */
614int kernel_validate_version(int tracer_fd)
615{
616 int ret;
617 struct lttng_kernel_tracer_version version;
618
619 ret = kernctl_tracer_version(tracer_fd, &version);
620 if (ret < 0) {
621 ERR("Failed at getting the lttng-modules version");
622 goto error;
623 }
624
625 /* Validate version */
626 if (version.version > KERN_MODULES_VERSION) {
627 goto error_version;
096102bd
DG
628 }
629
5df0f285 630 DBG2("Kernel tracer version validated (major version %d)", version.version);
096102bd
DG
631 return 0;
632
633error_version:
5df0f285
DG
634 ERR("Kernel major version %d is not compatible (supporting <= %d)",
635 version.version, KERN_MODULES_VERSION)
096102bd
DG
636 ret = -1;
637
638error:
639 return ret;
640}
335a95b7
MD
641
642/*
643 * Kernel work-arounds called at the start of sessiond main().
644 */
645int init_kernel_workarounds(void)
646{
8936c33a 647 int ret;
335a95b7
MD
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
8936c33a
DG
662 ret = fread(buf, 1, sizeof(buf), fp);
663 if (ret < 0) {
664 /* Ignore error, we don't really care */
665 }
335a95b7
MD
666 }
667 fclose(fp);
668end_boot_id:
669
670 return 0;
671}
This page took 0.064418 seconds and 5 git commands to generate.