Add disable all support and fix enable all
[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
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
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 "ltt-sessiond.h"
29#include "libkernelctl.h"
30#include "kernel-ctl.h"
20fe2104
DG
31
32/*
33 * kernel_create_session
34 *
54012638
DG
35 * Create a new kernel session, register it to the kernel tracer and add it to
36 * the session daemon session.
20fe2104 37 */
8c0faa1d 38int kernel_create_session(struct ltt_session *session, int tracer_fd)
20fe2104
DG
39{
40 int ret;
41 struct ltt_kernel_session *lks;
42
54012638
DG
43 /* Allocate data structure */
44 lks = trace_create_kernel_session();
20fe2104 45 if (lks == NULL) {
54012638 46 ret = -1;
20fe2104
DG
47 goto error;
48 }
49
54012638 50 /* Kernel tracer session creation */
20fe2104
DG
51 ret = kernctl_create_session(tracer_fd);
52 if (ret < 0) {
54012638 53 perror("ioctl kernel create session");
20fe2104
DG
54 goto error;
55 }
56
20fe2104 57 lks->fd = ret;
7b395890
DG
58 /* Prevent fd duplication after execlp() */
59 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
60 if (ret < 0) {
61 perror("fcntl session fd");
62 }
63
f3ed775e 64 lks->kconsumer_fds_sent = 0;
8c0faa1d
DG
65 session->kernel_session = lks;
66 session->kern_session_count++;
8c0faa1d
DG
67
68 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104
DG
69
70 return 0;
71
72error:
73 return ret;
74}
75
76/*
77 * kernel_create_channel
78 *
54012638
DG
79 * Create a kernel channel, register it to the kernel tracer and add it to the
80 * kernel session.
20fe2104 81 */
f3ed775e 82int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_channel *chan)
20fe2104
DG
83{
84 int ret;
85 struct ltt_kernel_channel *lkc;
20fe2104 86
54012638 87 /* Allocate kernel channel */
f3ed775e 88 lkc = trace_create_kernel_channel(chan);
54012638 89 if (lkc == NULL) {
20fe2104
DG
90 goto error;
91 }
92
54012638 93 /* Kernel tracer channel creation */
f3ed775e 94 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 95 if (ret < 0) {
54012638 96 perror("ioctl kernel create channel");
20fe2104
DG
97 goto error;
98 }
99
54012638 100 /* Setup the channel fd */
20fe2104 101 lkc->fd = ret;
7b395890
DG
102 /* Prevent fd duplication after execlp() */
103 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
104 if (ret < 0) {
105 perror("fcntl session fd");
106 }
107
54012638 108 /* Add channel to session */
8c0faa1d
DG
109 cds_list_add(&lkc->list, &session->channel_list.head);
110 session->channel_count++;
20fe2104 111
f3ed775e
DG
112 DBG("Kernel channel %s created (fd: %d and path: %s)",
113 lkc->channel->name, lkc->fd, lkc->pathname);
20fe2104
DG
114
115 return 0;
116
117error:
54012638 118 return -1;
20fe2104 119}
f34daff7
DG
120
121/*
f3ed775e 122 * kernel_create_event
f34daff7 123 *
54012638
DG
124 * Create a kernel event, enable it to the kernel tracer and add it to the
125 * channel event list of the kernel session.
f34daff7 126 */
19e70852 127int kernel_create_event(struct lttng_event *ev, struct ltt_kernel_channel *channel)
f34daff7
DG
128{
129 int ret;
130 struct ltt_kernel_event *event;
f34daff7 131
f3ed775e 132 event = trace_create_kernel_event(ev);
54012638 133 if (event == NULL) {
f34daff7
DG
134 goto error;
135 }
136
f3ed775e
DG
137 ret = kernctl_create_event(channel->fd, event->event);
138 if (ret < 0) {
e953ef25
DG
139 perror("create event ioctl");
140 goto free_event;
8c0faa1d 141 }
f34daff7 142
f3ed775e 143 event->fd = ret;
e953ef25 144 event->enabled = 1;
7b395890
DG
145 /* Prevent fd duplication after execlp() */
146 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
147 if (ret < 0) {
148 perror("fcntl session fd");
149 }
150
f3ed775e
DG
151 /* Add event to event list */
152 cds_list_add(&event->list, &channel->events_list.head);
e953ef25
DG
153 DBG("Event %s created (fd: %d)", ev->name, event->fd);
154
155 return 0;
156
157free_event:
158 free(event);
159error:
160 return -1;
161}
162
19e70852
DG
163/*
164 * kernel_enable_event
165 *
166 * Enable a kernel event.
167 */
168int kernel_enable_event(struct ltt_kernel_event *event)
169{
170 int ret;
171
172 ret = kernctl_enable(event->fd);
173 if (ret < 0) {
174 perror("enable event ioctl");
175 goto error;
176 }
177
178 event->enabled = 1;
179 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
180
181 return 0;
182
183error:
184 return -1;
185}
186
e953ef25
DG
187/*
188 * kernel_disable_event
189 *
19e70852 190 * Disable a kernel event.
e953ef25 191 */
19e70852 192int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
193{
194 int ret;
19e70852
DG
195
196 ret = kernctl_disable(event->fd);
197 if (ret < 0) {
198 perror("disable event ioctl");
199 goto error;
e953ef25 200 }
f3ed775e 201
19e70852
DG
202 event->enabled = 0;
203 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
204
f34daff7
DG
205 return 0;
206
207error:
54012638 208 return -1;
f34daff7 209}
aaf26714
DG
210
211/*
212 * kernel_open_metadata
213 *
54012638
DG
214 * Create kernel metadata, open from the kernel tracer and add it to the
215 * kernel session.
aaf26714
DG
216 */
217int kernel_open_metadata(struct ltt_kernel_session *session)
218{
219 int ret;
220 struct ltt_kernel_metadata *lkm;
aaf26714 221
54012638
DG
222 /* Allocate kernel metadata */
223 lkm = trace_create_kernel_metadata();
224 if (lkm == NULL) {
aaf26714
DG
225 goto error;
226 }
227
54012638 228 /* Kernel tracer metadata creation */
f3ed775e 229 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714
DG
230 if (ret < 0) {
231 goto error;
232 }
233
8c0faa1d 234 lkm->fd = ret;
7b395890
DG
235 /* Prevent fd duplication after execlp() */
236 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
237 if (ret < 0) {
238 perror("fcntl session fd");
239 }
240
aaf26714 241 session->metadata = lkm;
8c0faa1d 242
54012638 243 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
8c0faa1d
DG
244
245 return 0;
246
247error:
54012638 248 return -1;
8c0faa1d
DG
249}
250
251/*
252 * kernel_start_session
253 *
254 * Start tracing session.
255 */
256int kernel_start_session(struct ltt_kernel_session *session)
257{
258 int ret;
259
260 ret = kernctl_start_session(session->fd);
261 if (ret < 0) {
f3ed775e 262 perror("ioctl start session");
8c0faa1d
DG
263 goto error;
264 }
265
266 DBG("Kernel session started");
267
268 return 0;
269
270error:
271 return ret;
272}
273
f3ed775e
DG
274/*
275 * kernel_wait_quiescent
276 *
277 * Make a kernel wait to make sure in-flight probe have completed.
278 */
279void kernel_wait_quiescent(int fd)
280{
281 int ret;
282
283 DBG("Kernel quiescent wait on %d", fd);
284
285 ret = kernctl_wait_quiescent(fd);
286 if (ret < 0) {
287 perror("wait quiescent ioctl");
288 ERR("Kernel quiescent wait failed");
289 }
290}
291
292/*
293 * kernel_metadata_flush_buffer
294 *
295 * Force flush buffer of metadata.
296 */
297int kernel_metadata_flush_buffer(int fd)
298{
299 int ret;
300
301 ret = kernctl_buffer_flush(fd);
302 if (ret < 0) {
303 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
304 }
305
306 return 0;
307}
308
309/*
310 * kernel_flush_buffer
311 *
312 * Force flush buffer for channel.
313 */
314int kernel_flush_buffer(struct ltt_kernel_channel *channel)
315{
316 int ret;
317 struct ltt_kernel_stream *stream;
318
319 DBG("Flush buffer for channel %s", channel->channel->name);
320
321 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
322 DBG("Flushing channel stream %d", stream->fd);
323 ret = kernctl_buffer_flush(stream->fd);
324 if (ret < 0) {
325 perror("ioctl");
326 ERR("Fail to flush buffer for stream %d (ret: %d)",
327 stream->fd, ret);
328 }
329 }
330
331 return 0;
332}
333
8c0faa1d
DG
334/*
335 * kernel_stop_session
336 *
337 * Stop tracing session.
338 */
339int kernel_stop_session(struct ltt_kernel_session *session)
340{
341 int ret;
342
343 ret = kernctl_stop_session(session->fd);
344 if (ret < 0) {
345 goto error;
346 }
347
348 DBG("Kernel session stopped");
349
350 return 0;
351
352error:
353 return ret;
354}
355
356/*
f3ed775e 357 * kernel_open_channel_stream
8c0faa1d 358 *
f3ed775e 359 * Open stream of channel, register it to the kernel tracer and add it
54012638 360 * to the stream list of the channel.
8c0faa1d
DG
361 *
362 * Return the number of created stream. Else, a negative value.
363 */
f3ed775e 364int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d
DG
365{
366 int ret;
367 struct ltt_kernel_stream *lks;
368
369 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
54012638 370 lks = trace_create_kernel_stream();
8c0faa1d 371 if (lks == NULL) {
54012638 372 close(ret);
8c0faa1d
DG
373 goto error;
374 }
375
376 lks->fd = ret;
7b395890
DG
377 /* Prevent fd duplication after execlp() */
378 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
379 if (ret < 0) {
380 perror("fcntl session fd");
381 }
382
8c0faa1d
DG
383 ret = asprintf(&lks->pathname, "%s/trace_%d",
384 channel->pathname, channel->stream_count);
385 if (ret < 0) {
386 perror("asprintf kernel create stream");
387 goto error;
388 }
8c0faa1d 389
54012638 390 /* Add stream to channe stream list */
8c0faa1d
DG
391 cds_list_add(&lks->list, &channel->stream_list.head);
392 channel->stream_count++;
8c0faa1d 393
54012638
DG
394 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
395 channel->stream_count, lks->fd, lks->state, lks->pathname);
396 }
8c0faa1d
DG
397
398 return channel->stream_count;
399
400error:
54012638 401 return -1;
8c0faa1d
DG
402}
403
404/*
f3ed775e 405 * kernel_open_metadata_stream
8c0faa1d 406 *
f3ed775e 407 * Open the metadata stream and set it to the kernel session.
8c0faa1d 408 */
f3ed775e 409int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
410{
411 int ret;
412
413 ret = kernctl_create_stream(session->metadata->fd);
414 if (ret < 0) {
415 perror("kernel create metadata stream");
8c0faa1d
DG
416 goto error;
417 }
418
419 DBG("Kernel metadata stream created (fd: %d)", ret);
420 session->metadata_stream_fd = ret;
7b395890
DG
421 /* Prevent fd duplication after execlp() */
422 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
423 if (ret < 0) {
424 perror("fcntl session fd");
425 }
aaf26714
DG
426
427 return 0;
428
429error:
54012638 430 return -1;
aaf26714 431}
2ef84c95
DG
432
433/*
434 * kernel_list_events
435 *
436 * Get the event list from the kernel tracer and return that list in the CTF
437 * format.
438 */
439ssize_t kernel_list_events(int tracer_fd, char **list)
440{
441 int fd;
442 char *buf, *line = NULL;
443 size_t nb, nbmem, total = 0;
444 ssize_t size;
445 FILE *fp;
446
447 fd = kernctl_tracepoint_list(tracer_fd);
448 if (fd < 0) {
449 perror("kernel tracepoint list");
450 goto error;
451 }
452
453 fp = fdopen(fd, "r");
454 if (fp == NULL) {
455 perror("kernel tracepoint list fdopen");
456 goto error;
457 }
458
459 /*
460 * Init memory size counter
461 * See kernel-ctl.h for explanation of this value
462 */
463 nbmem = KERNEL_EVENT_LIST_SIZE;
464 buf = malloc(nbmem);
465
466 while ((size = getline(&line, &nb, fp)) != -1) {
467 if (total + size > nbmem) {
d686b40f 468 DBG("Reallocating event list from %zd to %zd bytes", nbmem,
2ef84c95
DG
469 total + size + KERNEL_EVENT_LIST_SIZE);
470 /* Adding the default size again */
471 nbmem = total + size + KERNEL_EVENT_LIST_SIZE;
472 buf = realloc(buf, nbmem);
473 if (buf == NULL) {
474 perror("realloc list events");
475 goto error;
476 }
477 }
478 memcpy(buf + total, line, size);
479 total += size;
480 }
481
482 *list = buf;
483
484 DBG("Kernel list events done");
485
486 return total;
487
488error:
489 return -1;
490}
This page took 0.07177 seconds and 4 git commands to generate.