Add disable kernel event support
[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 */
f3ed775e 127int kernel_create_event(struct ltt_kernel_channel *channel, struct lttng_event *ev)
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
163/*
164 * kernel_disable_event
165 *
166 * Disable a kernel event for a specific channel.
167 */
168int kernel_disable_event(char *event_name, struct ltt_kernel_channel *channel)
169{
170 int ret;
171 struct ltt_kernel_event *iter;
172
173 cds_list_for_each_entry(iter, &channel->events_list.head, list) {
174 if (strcmp(iter->event->name, event_name) == 0) {
175 ret = kernctl_disable(iter->fd);
176 if (ret < 0) {
177 perror("disable event ioctl");
178 goto error;
179 }
180
181 iter->enabled = 0;
182 DBG("Kernel event %s disabled (fd: %d)", iter->event->name, iter->fd);
183 break;
184 }
185 }
f3ed775e 186
f34daff7
DG
187 return 0;
188
189error:
54012638 190 return -1;
f34daff7 191}
aaf26714
DG
192
193/*
194 * kernel_open_metadata
195 *
54012638
DG
196 * Create kernel metadata, open from the kernel tracer and add it to the
197 * kernel session.
aaf26714
DG
198 */
199int kernel_open_metadata(struct ltt_kernel_session *session)
200{
201 int ret;
202 struct ltt_kernel_metadata *lkm;
aaf26714 203
54012638
DG
204 /* Allocate kernel metadata */
205 lkm = trace_create_kernel_metadata();
206 if (lkm == NULL) {
aaf26714
DG
207 goto error;
208 }
209
54012638 210 /* Kernel tracer metadata creation */
f3ed775e 211 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714
DG
212 if (ret < 0) {
213 goto error;
214 }
215
8c0faa1d 216 lkm->fd = ret;
7b395890
DG
217 /* Prevent fd duplication after execlp() */
218 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
219 if (ret < 0) {
220 perror("fcntl session fd");
221 }
222
aaf26714 223 session->metadata = lkm;
8c0faa1d 224
54012638 225 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
8c0faa1d
DG
226
227 return 0;
228
229error:
54012638 230 return -1;
8c0faa1d
DG
231}
232
233/*
234 * kernel_start_session
235 *
236 * Start tracing session.
237 */
238int kernel_start_session(struct ltt_kernel_session *session)
239{
240 int ret;
241
242 ret = kernctl_start_session(session->fd);
243 if (ret < 0) {
f3ed775e 244 perror("ioctl start session");
8c0faa1d
DG
245 goto error;
246 }
247
248 DBG("Kernel session started");
249
250 return 0;
251
252error:
253 return ret;
254}
255
f3ed775e
DG
256/*
257 * kernel_wait_quiescent
258 *
259 * Make a kernel wait to make sure in-flight probe have completed.
260 */
261void kernel_wait_quiescent(int fd)
262{
263 int ret;
264
265 DBG("Kernel quiescent wait on %d", fd);
266
267 ret = kernctl_wait_quiescent(fd);
268 if (ret < 0) {
269 perror("wait quiescent ioctl");
270 ERR("Kernel quiescent wait failed");
271 }
272}
273
274/*
275 * kernel_metadata_flush_buffer
276 *
277 * Force flush buffer of metadata.
278 */
279int kernel_metadata_flush_buffer(int fd)
280{
281 int ret;
282
283 ret = kernctl_buffer_flush(fd);
284 if (ret < 0) {
285 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
286 }
287
288 return 0;
289}
290
291/*
292 * kernel_flush_buffer
293 *
294 * Force flush buffer for channel.
295 */
296int kernel_flush_buffer(struct ltt_kernel_channel *channel)
297{
298 int ret;
299 struct ltt_kernel_stream *stream;
300
301 DBG("Flush buffer for channel %s", channel->channel->name);
302
303 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
304 DBG("Flushing channel stream %d", stream->fd);
305 ret = kernctl_buffer_flush(stream->fd);
306 if (ret < 0) {
307 perror("ioctl");
308 ERR("Fail to flush buffer for stream %d (ret: %d)",
309 stream->fd, ret);
310 }
311 }
312
313 return 0;
314}
315
8c0faa1d
DG
316/*
317 * kernel_stop_session
318 *
319 * Stop tracing session.
320 */
321int kernel_stop_session(struct ltt_kernel_session *session)
322{
323 int ret;
324
325 ret = kernctl_stop_session(session->fd);
326 if (ret < 0) {
327 goto error;
328 }
329
330 DBG("Kernel session stopped");
331
332 return 0;
333
334error:
335 return ret;
336}
337
338/*
f3ed775e 339 * kernel_open_channel_stream
8c0faa1d 340 *
f3ed775e 341 * Open stream of channel, register it to the kernel tracer and add it
54012638 342 * to the stream list of the channel.
8c0faa1d
DG
343 *
344 * Return the number of created stream. Else, a negative value.
345 */
f3ed775e 346int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d
DG
347{
348 int ret;
349 struct ltt_kernel_stream *lks;
350
351 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
54012638 352 lks = trace_create_kernel_stream();
8c0faa1d 353 if (lks == NULL) {
54012638 354 close(ret);
8c0faa1d
DG
355 goto error;
356 }
357
358 lks->fd = ret;
7b395890
DG
359 /* Prevent fd duplication after execlp() */
360 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
361 if (ret < 0) {
362 perror("fcntl session fd");
363 }
364
8c0faa1d
DG
365 ret = asprintf(&lks->pathname, "%s/trace_%d",
366 channel->pathname, channel->stream_count);
367 if (ret < 0) {
368 perror("asprintf kernel create stream");
369 goto error;
370 }
8c0faa1d 371
54012638 372 /* Add stream to channe stream list */
8c0faa1d
DG
373 cds_list_add(&lks->list, &channel->stream_list.head);
374 channel->stream_count++;
8c0faa1d 375
54012638
DG
376 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
377 channel->stream_count, lks->fd, lks->state, lks->pathname);
378 }
8c0faa1d
DG
379
380 return channel->stream_count;
381
382error:
54012638 383 return -1;
8c0faa1d
DG
384}
385
386/*
f3ed775e 387 * kernel_open_metadata_stream
8c0faa1d 388 *
f3ed775e 389 * Open the metadata stream and set it to the kernel session.
8c0faa1d 390 */
f3ed775e 391int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
392{
393 int ret;
394
395 ret = kernctl_create_stream(session->metadata->fd);
396 if (ret < 0) {
397 perror("kernel create metadata stream");
8c0faa1d
DG
398 goto error;
399 }
400
401 DBG("Kernel metadata stream created (fd: %d)", ret);
402 session->metadata_stream_fd = ret;
7b395890
DG
403 /* Prevent fd duplication after execlp() */
404 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
405 if (ret < 0) {
406 perror("fcntl session fd");
407 }
aaf26714
DG
408
409 return 0;
410
411error:
54012638 412 return -1;
aaf26714 413}
2ef84c95
DG
414
415/*
416 * kernel_list_events
417 *
418 * Get the event list from the kernel tracer and return that list in the CTF
419 * format.
420 */
421ssize_t kernel_list_events(int tracer_fd, char **list)
422{
423 int fd;
424 char *buf, *line = NULL;
425 size_t nb, nbmem, total = 0;
426 ssize_t size;
427 FILE *fp;
428
429 fd = kernctl_tracepoint_list(tracer_fd);
430 if (fd < 0) {
431 perror("kernel tracepoint list");
432 goto error;
433 }
434
435 fp = fdopen(fd, "r");
436 if (fp == NULL) {
437 perror("kernel tracepoint list fdopen");
438 goto error;
439 }
440
441 /*
442 * Init memory size counter
443 * See kernel-ctl.h for explanation of this value
444 */
445 nbmem = KERNEL_EVENT_LIST_SIZE;
446 buf = malloc(nbmem);
447
448 while ((size = getline(&line, &nb, fp)) != -1) {
449 if (total + size > nbmem) {
d686b40f 450 DBG("Reallocating event list from %zd to %zd bytes", nbmem,
2ef84c95
DG
451 total + size + KERNEL_EVENT_LIST_SIZE);
452 /* Adding the default size again */
453 nbmem = total + size + KERNEL_EVENT_LIST_SIZE;
454 buf = realloc(buf, nbmem);
455 if (buf == NULL) {
456 perror("realloc list events");
457 goto error;
458 }
459 }
460 memcpy(buf + total, line, size);
461 total += size;
462 }
463
464 *list = buf;
465
466 DBG("Kernel list events done");
467
468 return total;
469
470error:
471 return -1;
472}
This page took 0.06731 seconds and 4 git commands to generate.