Remove session daemon kernel list events command
[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
8c0faa1d 27#include "lttngerr.h"
20fe2104
DG
28#include "libkernelctl.h"
29#include "kernel-ctl.h"
20fe2104 30
d65106b1
DG
31/*
32 * kernel_add_channel_context
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 perror("add context ioctl");
45 goto error;
46 }
47
48 chan->ctx = malloc(sizeof(struct lttng_kernel_context));
49 if (chan->ctx == NULL) {
50 perror("malloc event context");
51 goto error;
52 }
53
54 memcpy(chan->ctx, ctx, sizeof(struct lttng_kernel_context));
55
56 return 0;
57
58error:
59 return ret;
60}
61
62/*
63 * kernel_add_event_context
64 *
65 * Add context on a kernel event.
66 */
67int kernel_add_event_context(struct ltt_kernel_event *event,
68 struct lttng_kernel_context *ctx)
69{
70 int ret;
71
72 DBG("Adding context to event %s", event->event->name);
73 ret = kernctl_add_context(event->fd, ctx);
74 if (ret < 0) {
75 perror("add context ioctl");
76 goto error;
77 }
78
79 event->ctx = malloc(sizeof(struct lttng_kernel_context));
80 if (event->ctx == NULL) {
81 perror("malloc event context");
82 goto error;
83 }
84
85 memcpy(event->ctx, ctx, sizeof(struct lttng_kernel_context));
86
87 return 0;
88
89error:
90 return ret;
91}
92
20fe2104
DG
93/*
94 * kernel_create_session
95 *
54012638
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
DG
104 /* Allocate data structure */
105 lks = trace_create_kernel_session();
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/*
137 * kernel_create_channel
138 *
54012638
DG
139 * Create a kernel channel, register it to the kernel tracer and add it to the
140 * kernel session.
20fe2104 141 */
b082db07 142int kernel_create_channel(struct ltt_kernel_session *session, 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 */
b082db07 148 lkc = trace_create_kernel_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/*
f3ed775e 182 * kernel_create_event
f34daff7 183 *
54012638
DG
184 * Create a kernel event, enable it to the kernel tracer and add it to the
185 * channel event list of the kernel session.
f34daff7 186 */
19e70852 187int kernel_create_event(struct lttng_event *ev, struct ltt_kernel_channel *channel)
f34daff7
DG
188{
189 int ret;
190 struct ltt_kernel_event *event;
f34daff7 191
f3ed775e 192 event = trace_create_kernel_event(ev);
54012638 193 if (event == NULL) {
f34daff7
DG
194 goto error;
195 }
196
f3ed775e
DG
197 ret = kernctl_create_event(channel->fd, event->event);
198 if (ret < 0) {
e953ef25
DG
199 perror("create event ioctl");
200 goto free_event;
8c0faa1d 201 }
f34daff7 202
f3ed775e 203 event->fd = ret;
7b395890
DG
204 /* Prevent fd duplication after execlp() */
205 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
206 if (ret < 0) {
207 perror("fcntl session fd");
208 }
209
f3ed775e
DG
210 /* Add event to event list */
211 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
212 channel->event_count++;
213
e953ef25
DG
214 DBG("Event %s created (fd: %d)", ev->name, event->fd);
215
216 return 0;
217
218free_event:
219 free(event);
220error:
221 return -1;
222}
223
26cc6b4e
DG
224/*
225 * kernel_disable_channel
226 *
227 * Disable a kernel channel.
228 */
229int 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
245error:
246 return ret;
247}
248
d36b8583
DG
249/*
250 * kernel_enable_channel
251 *
252 * Enable a kernel channel.
253 */
254int kernel_enable_channel(struct ltt_kernel_channel *chan)
255{
256 int ret;
257
258 ret = kernctl_enable(chan->fd);
259 if (ret < 0) {
260 perror("enable chan ioctl");
261 ret = errno;
262 goto error;
263 }
264
265 chan->enabled = 1;
266 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
267
268 return 0;
269
270error:
271 return ret;
272}
273
19e70852
DG
274/*
275 * kernel_enable_event
276 *
277 * Enable a kernel event.
278 */
279int kernel_enable_event(struct ltt_kernel_event *event)
280{
281 int ret;
282
283 ret = kernctl_enable(event->fd);
284 if (ret < 0) {
285 perror("enable event ioctl");
7d29a247
DG
286 if (errno == EEXIST) {
287 ret = -EEXIST;
288 }
19e70852
DG
289 goto error;
290 }
291
292 event->enabled = 1;
293 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
294
295 return 0;
296
297error:
d36b8583 298 return ret;
19e70852
DG
299}
300
e953ef25
DG
301/*
302 * kernel_disable_event
303 *
19e70852 304 * Disable a kernel event.
e953ef25 305 */
19e70852 306int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
307{
308 int ret;
19e70852
DG
309
310 ret = kernctl_disable(event->fd);
311 if (ret < 0) {
312 perror("disable event ioctl");
313 goto error;
e953ef25 314 }
f3ed775e 315
19e70852
DG
316 event->enabled = 0;
317 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
318
f34daff7
DG
319 return 0;
320
321error:
d36b8583 322 return ret;
f34daff7 323}
aaf26714
DG
324
325/*
326 * kernel_open_metadata
327 *
54012638
DG
328 * Create kernel metadata, open from the kernel tracer and add it to the
329 * kernel session.
aaf26714 330 */
58a97671 331int kernel_open_metadata(struct ltt_kernel_session *session, char *path)
aaf26714
DG
332{
333 int ret;
334 struct ltt_kernel_metadata *lkm;
aaf26714 335
54012638 336 /* Allocate kernel metadata */
58a97671 337 lkm = trace_create_kernel_metadata(path);
54012638 338 if (lkm == NULL) {
aaf26714
DG
339 goto error;
340 }
341
54012638 342 /* Kernel tracer metadata creation */
f3ed775e 343 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714
DG
344 if (ret < 0) {
345 goto error;
346 }
347
8c0faa1d 348 lkm->fd = ret;
7b395890
DG
349 /* Prevent fd duplication after execlp() */
350 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
351 if (ret < 0) {
352 perror("fcntl session fd");
353 }
354
aaf26714 355 session->metadata = lkm;
8c0faa1d 356
54012638 357 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
8c0faa1d
DG
358
359 return 0;
360
361error:
54012638 362 return -1;
8c0faa1d
DG
363}
364
365/*
366 * kernel_start_session
367 *
368 * Start tracing session.
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) {
f3ed775e 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
DG
388/*
389 * kernel_wait_quiescent
390 *
391 * Make a kernel wait to make sure in-flight probe have completed.
392 */
393void kernel_wait_quiescent(int fd)
394{
395 int ret;
396
397 DBG("Kernel quiescent wait on %d", fd);
398
399 ret = kernctl_wait_quiescent(fd);
400 if (ret < 0) {
401 perror("wait quiescent ioctl");
402 ERR("Kernel quiescent wait failed");
403 }
404}
405
406/*
407 * kernel_metadata_flush_buffer
408 *
409 * Force flush buffer of metadata.
410 */
411int kernel_metadata_flush_buffer(int fd)
412{
413 int ret;
414
415 ret = kernctl_buffer_flush(fd);
416 if (ret < 0) {
417 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
418 }
419
420 return 0;
421}
422
423/*
424 * kernel_flush_buffer
425 *
426 * Force flush buffer for channel.
427 */
428int kernel_flush_buffer(struct ltt_kernel_channel *channel)
429{
430 int ret;
431 struct ltt_kernel_stream *stream;
432
433 DBG("Flush buffer for channel %s", channel->channel->name);
434
435 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
436 DBG("Flushing channel stream %d", stream->fd);
437 ret = kernctl_buffer_flush(stream->fd);
438 if (ret < 0) {
439 perror("ioctl");
440 ERR("Fail to flush buffer for stream %d (ret: %d)",
441 stream->fd, ret);
442 }
443 }
444
445 return 0;
446}
447
8c0faa1d
DG
448/*
449 * kernel_stop_session
450 *
451 * Stop tracing session.
452 */
453int kernel_stop_session(struct ltt_kernel_session *session)
454{
455 int ret;
456
457 ret = kernctl_stop_session(session->fd);
458 if (ret < 0) {
459 goto error;
460 }
461
462 DBG("Kernel session stopped");
463
464 return 0;
465
466error:
467 return ret;
468}
469
470/*
f3ed775e 471 * kernel_open_channel_stream
8c0faa1d 472 *
f3ed775e 473 * Open stream of channel, register it to the kernel tracer and add it
54012638 474 * to the stream list of the channel.
8c0faa1d
DG
475 *
476 * Return the number of created stream. Else, a negative value.
477 */
f3ed775e 478int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d
DG
479{
480 int ret;
481 struct ltt_kernel_stream *lks;
482
483 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
54012638 484 lks = trace_create_kernel_stream();
8c0faa1d 485 if (lks == NULL) {
54012638 486 close(ret);
8c0faa1d
DG
487 goto error;
488 }
489
490 lks->fd = ret;
7b395890
DG
491 /* Prevent fd duplication after execlp() */
492 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
493 if (ret < 0) {
494 perror("fcntl session fd");
495 }
496
8e68d1c8
DG
497 ret = asprintf(&lks->pathname, "%s/%s_%d",
498 channel->pathname, channel->channel->name, channel->stream_count);
8c0faa1d
DG
499 if (ret < 0) {
500 perror("asprintf kernel create stream");
501 goto error;
502 }
8c0faa1d 503
54012638 504 /* Add stream to channe stream list */
8c0faa1d
DG
505 cds_list_add(&lks->list, &channel->stream_list.head);
506 channel->stream_count++;
8c0faa1d 507
54012638
DG
508 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
509 channel->stream_count, lks->fd, lks->state, lks->pathname);
510 }
8c0faa1d
DG
511
512 return channel->stream_count;
513
514error:
54012638 515 return -1;
8c0faa1d
DG
516}
517
518/*
f3ed775e 519 * kernel_open_metadata_stream
8c0faa1d 520 *
f3ed775e 521 * Open the metadata stream and set it to the kernel session.
8c0faa1d 522 */
f3ed775e 523int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
524{
525 int ret;
526
527 ret = kernctl_create_stream(session->metadata->fd);
528 if (ret < 0) {
529 perror("kernel create metadata stream");
8c0faa1d
DG
530 goto error;
531 }
532
533 DBG("Kernel metadata stream created (fd: %d)", ret);
534 session->metadata_stream_fd = ret;
7b395890
DG
535 /* Prevent fd duplication after execlp() */
536 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
537 if (ret < 0) {
538 perror("fcntl session fd");
539 }
aaf26714
DG
540
541 return 0;
542
543error:
54012638 544 return -1;
aaf26714 545}
2ef84c95
DG
546
547/*
9f19cc17 548 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 549 */
9f19cc17 550ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
2ef84c95 551{
9f19cc17
DG
552 int fd, pos;
553 char *event;
554 size_t nbmem, count = 0;
2ef84c95
DG
555 ssize_t size;
556 FILE *fp;
9f19cc17 557 struct lttng_event *elist;
2ef84c95
DG
558
559 fd = kernctl_tracepoint_list(tracer_fd);
560 if (fd < 0) {
561 perror("kernel tracepoint list");
562 goto error;
563 }
564
565 fp = fdopen(fd, "r");
566 if (fp == NULL) {
567 perror("kernel tracepoint list fdopen");
568 goto error;
569 }
570
571 /*
572 * Init memory size counter
573 * See kernel-ctl.h for explanation of this value
574 */
575 nbmem = KERNEL_EVENT_LIST_SIZE;
9f19cc17 576 elist = malloc(sizeof(struct lttng_event) * nbmem);
2ef84c95 577
9f19cc17
DG
578 while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
579 if (count > nbmem) {
d686b40f 580 DBG("Reallocating event list from %zd to %zd bytes", nbmem,
9f19cc17 581 nbmem + KERNEL_EVENT_LIST_SIZE);
2ef84c95 582 /* Adding the default size again */
9f19cc17
DG
583 nbmem += KERNEL_EVENT_LIST_SIZE;
584 elist = realloc(elist, nbmem);
585 if (elist == NULL) {
2ef84c95
DG
586 perror("realloc list events");
587 goto error;
588 }
589 }
9f19cc17
DG
590 strncpy(elist[count].name, event, strlen(event));
591 count++;
2ef84c95
DG
592 }
593
9f19cc17 594 *events = elist;
2ef84c95 595
9f19cc17 596 DBG("Kernel list events done (%ld events)", count);
2ef84c95 597
9f19cc17 598 return count;
2ef84c95
DG
599
600error:
601 return -1;
602}
This page took 0.051695 seconds and 5 git commands to generate.