Fix enable syscall and bad value of poll set size
[lttng-tools.git] / ltt-sessiond / kernel-ctl.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
1e307fab
DG
27#include <lttng-kernel-ctl.h>
28#include <lttngerr.h>
29
20fe2104 30#include "kernel-ctl.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
DG
43 if (errno != EEXIST) {
44 perror("add context ioctl");
45 } else {
46 /* If EEXIST, we just ignore the error */
47 ret = 0;
48 }
d65106b1
DG
49 goto error;
50 }
51
52 chan->ctx = malloc(sizeof(struct lttng_kernel_context));
53 if (chan->ctx == NULL) {
54 perror("malloc event context");
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) {
77 perror("add context ioctl");
78 goto error;
79 }
80
81 event->ctx = malloc(sizeof(struct lttng_kernel_context));
82 if (event->ctx == NULL) {
83 perror("malloc event context");
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) {
54012638 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) {
122 perror("fcntl session fd");
123 }
124
f3ed775e 125 lks->kconsumer_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) {
54012638 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) {
164 perror("fcntl session fd");
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) {
f34daff7
DG
192 goto error;
193 }
194
f3ed775e
DG
195 ret = kernctl_create_event(channel->fd, event->event);
196 if (ret < 0) {
5f822d0a 197 PERROR("create event ioctl");
e953ef25 198 goto free_event;
8c0faa1d 199 }
f34daff7 200
5f822d0a
DG
201 /*
202 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success. However
203 * this FD must not be added to the event list.
204 */
205 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
206 DBG2("Kernel event syscall creation success");
207 goto end;
208 }
209
f3ed775e 210 event->fd = ret;
7b395890
DG
211 /* Prevent fd duplication after execlp() */
212 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
213 if (ret < 0) {
214 perror("fcntl session fd");
215 }
216
f3ed775e
DG
217 /* Add event to event list */
218 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
219 channel->event_count++;
220
e953ef25
DG
221 DBG("Event %s created (fd: %d)", ev->name, event->fd);
222
5f822d0a 223end:
e953ef25
DG
224 return 0;
225
226free_event:
227 free(event);
228error:
229 return -1;
230}
231
26cc6b4e 232/*
050349bb 233 * Disable a kernel channel.
26cc6b4e
DG
234 */
235int kernel_disable_channel(struct ltt_kernel_channel *chan)
236{
237 int ret;
238
239 ret = kernctl_disable(chan->fd);
240 if (ret < 0) {
241 perror("disable chan ioctl");
242 ret = errno;
243 goto error;
244 }
245
246 chan->enabled = 0;
247 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
248
249 return 0;
250
251error:
252 return ret;
253}
254
d36b8583 255/*
050349bb 256 * Enable a kernel channel.
d36b8583
DG
257 */
258int kernel_enable_channel(struct ltt_kernel_channel *chan)
259{
260 int ret;
261
262 ret = kernctl_enable(chan->fd);
54d01ffb
DG
263 if (ret < 0 && errno != EEXIST) {
264 perror("Enable kernel chan");
d36b8583
DG
265 goto error;
266 }
267
268 chan->enabled = 1;
269 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
270
271 return 0;
272
273error:
274 return ret;
275}
276
19e70852 277/*
050349bb 278 * Enable a kernel event.
19e70852
DG
279 */
280int kernel_enable_event(struct ltt_kernel_event *event)
281{
282 int ret;
283
284 ret = kernctl_enable(event->fd);
54d01ffb
DG
285 if (ret < 0 && errno != EEXIST) {
286 perror("enable kernel event");
19e70852
DG
287 goto error;
288 }
289
290 event->enabled = 1;
291 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
292
293 return 0;
294
295error:
d36b8583 296 return ret;
19e70852
DG
297}
298
e953ef25 299/*
050349bb 300 * Disable a kernel event.
e953ef25 301 */
19e70852 302int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
303{
304 int ret;
19e70852
DG
305
306 ret = kernctl_disable(event->fd);
54d01ffb
DG
307 if (ret < 0 && errno != EEXIST) {
308 perror("disable kernel event");
19e70852 309 goto error;
e953ef25 310 }
f3ed775e 311
19e70852
DG
312 event->enabled = 0;
313 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
314
f34daff7
DG
315 return 0;
316
317error:
d36b8583 318 return ret;
f34daff7 319}
aaf26714
DG
320
321/*
050349bb
DG
322 * Create kernel metadata, open from the kernel tracer and add it to the
323 * kernel session.
aaf26714 324 */
58a97671 325int kernel_open_metadata(struct ltt_kernel_session *session, char *path)
aaf26714
DG
326{
327 int ret;
328 struct ltt_kernel_metadata *lkm;
aaf26714 329
54012638 330 /* Allocate kernel metadata */
62499ad6 331 lkm = trace_kernel_create_metadata(path);
54012638 332 if (lkm == NULL) {
aaf26714
DG
333 goto error;
334 }
335
54012638 336 /* Kernel tracer metadata creation */
f3ed775e 337 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714
DG
338 if (ret < 0) {
339 goto error;
340 }
341
8c0faa1d 342 lkm->fd = ret;
7b395890
DG
343 /* Prevent fd duplication after execlp() */
344 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
345 if (ret < 0) {
346 perror("fcntl session fd");
347 }
348
aaf26714 349 session->metadata = lkm;
8c0faa1d 350
54012638 351 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
8c0faa1d
DG
352
353 return 0;
354
355error:
54012638 356 return -1;
8c0faa1d
DG
357}
358
359/*
050349bb 360 * Start tracing session.
8c0faa1d
DG
361 */
362int kernel_start_session(struct ltt_kernel_session *session)
363{
364 int ret;
365
366 ret = kernctl_start_session(session->fd);
367 if (ret < 0) {
f3ed775e 368 perror("ioctl start session");
8c0faa1d
DG
369 goto error;
370 }
371
372 DBG("Kernel session started");
373
374 return 0;
375
376error:
377 return ret;
378}
379
f3ed775e 380/*
050349bb 381 * Make a kernel wait to make sure in-flight probe have completed.
f3ed775e
DG
382 */
383void kernel_wait_quiescent(int fd)
384{
385 int ret;
386
387 DBG("Kernel quiescent wait on %d", fd);
388
389 ret = kernctl_wait_quiescent(fd);
390 if (ret < 0) {
391 perror("wait quiescent ioctl");
392 ERR("Kernel quiescent wait failed");
393 }
394}
395
d0254c7c 396/*
050349bb 397 * Kernel calibrate
d0254c7c
MD
398 */
399int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
400{
401 int ret;
402
403 ret = kernctl_calibrate(fd, calibrate);
404 if (ret < 0) {
405 perror("calibrate ioctl");
406 return -1;
407 }
408
409 return 0;
410}
411
412
f3ed775e 413/*
f3ed775e
DG
414 * Force flush buffer of metadata.
415 */
416int kernel_metadata_flush_buffer(int fd)
417{
418 int ret;
419
420 ret = kernctl_buffer_flush(fd);
421 if (ret < 0) {
422 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
423 }
424
425 return 0;
426}
427
428/*
050349bb 429 * Force flush buffer for channel.
f3ed775e
DG
430 */
431int kernel_flush_buffer(struct ltt_kernel_channel *channel)
432{
433 int ret;
434 struct ltt_kernel_stream *stream;
435
436 DBG("Flush buffer for channel %s", channel->channel->name);
437
438 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
439 DBG("Flushing channel stream %d", stream->fd);
440 ret = kernctl_buffer_flush(stream->fd);
441 if (ret < 0) {
442 perror("ioctl");
443 ERR("Fail to flush buffer for stream %d (ret: %d)",
444 stream->fd, ret);
445 }
446 }
447
448 return 0;
449}
450
8c0faa1d 451/*
050349bb 452 * Stop tracing session.
8c0faa1d
DG
453 */
454int kernel_stop_session(struct ltt_kernel_session *session)
455{
456 int ret;
457
458 ret = kernctl_stop_session(session->fd);
459 if (ret < 0) {
460 goto error;
461 }
462
463 DBG("Kernel session stopped");
464
465 return 0;
466
467error:
468 return ret;
469}
470
471/*
050349bb
DG
472 * Open stream of channel, register it to the kernel tracer and add it
473 * to the stream list of the channel.
8c0faa1d 474 *
050349bb 475 * Return the number of created stream. Else, a negative value.
8c0faa1d 476 */
f3ed775e 477int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d
DG
478{
479 int ret;
480 struct ltt_kernel_stream *lks;
481
482 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
62499ad6 483 lks = trace_kernel_create_stream();
8c0faa1d 484 if (lks == NULL) {
54012638 485 close(ret);
8c0faa1d
DG
486 goto error;
487 }
488
489 lks->fd = ret;
7b395890
DG
490 /* Prevent fd duplication after execlp() */
491 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
492 if (ret < 0) {
493 perror("fcntl session fd");
494 }
495
8e68d1c8
DG
496 ret = asprintf(&lks->pathname, "%s/%s_%d",
497 channel->pathname, channel->channel->name, channel->stream_count);
8c0faa1d
DG
498 if (ret < 0) {
499 perror("asprintf kernel create stream");
500 goto error;
501 }
8c0faa1d 502
54012638 503 /* Add stream to channe stream list */
8c0faa1d
DG
504 cds_list_add(&lks->list, &channel->stream_list.head);
505 channel->stream_count++;
8c0faa1d 506
54012638
DG
507 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
508 channel->stream_count, lks->fd, lks->state, lks->pathname);
509 }
8c0faa1d
DG
510
511 return channel->stream_count;
512
513error:
54012638 514 return -1;
8c0faa1d
DG
515}
516
517/*
050349bb 518 * Open the metadata stream and set it to the kernel session.
8c0faa1d 519 */
f3ed775e 520int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
521{
522 int ret;
523
524 ret = kernctl_create_stream(session->metadata->fd);
525 if (ret < 0) {
526 perror("kernel create metadata stream");
8c0faa1d
DG
527 goto error;
528 }
529
530 DBG("Kernel metadata stream created (fd: %d)", ret);
531 session->metadata_stream_fd = ret;
7b395890
DG
532 /* Prevent fd duplication after execlp() */
533 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
534 if (ret < 0) {
535 perror("fcntl session fd");
536 }
aaf26714
DG
537
538 return 0;
539
540error:
54012638 541 return -1;
aaf26714 542}
2ef84c95
DG
543
544/*
9f19cc17 545 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 546 */
9f19cc17 547ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
2ef84c95 548{
9f19cc17
DG
549 int fd, pos;
550 char *event;
551 size_t nbmem, count = 0;
2ef84c95
DG
552 ssize_t size;
553 FILE *fp;
9f19cc17 554 struct lttng_event *elist;
2ef84c95
DG
555
556 fd = kernctl_tracepoint_list(tracer_fd);
557 if (fd < 0) {
558 perror("kernel tracepoint list");
559 goto error;
560 }
561
562 fp = fdopen(fd, "r");
563 if (fp == NULL) {
564 perror("kernel tracepoint list fdopen");
61b73b12 565 goto error_fp;
2ef84c95
DG
566 }
567
568 /*
569 * Init memory size counter
570 * See kernel-ctl.h for explanation of this value
571 */
572 nbmem = KERNEL_EVENT_LIST_SIZE;
9f19cc17 573 elist = malloc(sizeof(struct lttng_event) * nbmem);
2ef84c95 574
9f19cc17
DG
575 while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
576 if (count > nbmem) {
ced2f820 577 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
9f19cc17 578 nbmem + KERNEL_EVENT_LIST_SIZE);
2ef84c95 579 /* Adding the default size again */
9f19cc17
DG
580 nbmem += KERNEL_EVENT_LIST_SIZE;
581 elist = realloc(elist, nbmem);
582 if (elist == NULL) {
2ef84c95 583 perror("realloc list events");
61b73b12
MD
584 count = -ENOMEM;
585 goto end;
2ef84c95
DG
586 }
587 }
99497cd0
MD
588 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
589 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
9f19cc17 590 count++;
2ef84c95
DG
591 }
592
9f19cc17 593 *events = elist;
ced2f820 594 DBG("Kernel list events done (%zu events)", count);
61b73b12
MD
595end:
596 fclose(fp); /* closes both fp and fd */
9f19cc17 597 return count;
2ef84c95 598
61b73b12
MD
599error_fp:
600 close(fd);
2ef84c95
DG
601error:
602 return -1;
603}
This page took 0.059454 seconds and 5 git commands to generate.