Add disable kernel event support
[lttng-tools.git] / ltt-sessiond / kernel-ctl.c
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
19 #define _GNU_SOURCE
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "lttngerr.h"
28 #include "ltt-sessiond.h"
29 #include "libkernelctl.h"
30 #include "kernel-ctl.h"
31
32 /*
33 * kernel_create_session
34 *
35 * Create a new kernel session, register it to the kernel tracer and add it to
36 * the session daemon session.
37 */
38 int kernel_create_session(struct ltt_session *session, int tracer_fd)
39 {
40 int ret;
41 struct ltt_kernel_session *lks;
42
43 /* Allocate data structure */
44 lks = trace_create_kernel_session();
45 if (lks == NULL) {
46 ret = -1;
47 goto error;
48 }
49
50 /* Kernel tracer session creation */
51 ret = kernctl_create_session(tracer_fd);
52 if (ret < 0) {
53 perror("ioctl kernel create session");
54 goto error;
55 }
56
57 lks->fd = ret;
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
64 lks->kconsumer_fds_sent = 0;
65 session->kernel_session = lks;
66 session->kern_session_count++;
67
68 DBG("Kernel session created (fd: %d)", lks->fd);
69
70 return 0;
71
72 error:
73 return ret;
74 }
75
76 /*
77 * kernel_create_channel
78 *
79 * Create a kernel channel, register it to the kernel tracer and add it to the
80 * kernel session.
81 */
82 int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_channel *chan)
83 {
84 int ret;
85 struct ltt_kernel_channel *lkc;
86
87 /* Allocate kernel channel */
88 lkc = trace_create_kernel_channel(chan);
89 if (lkc == NULL) {
90 goto error;
91 }
92
93 /* Kernel tracer channel creation */
94 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
95 if (ret < 0) {
96 perror("ioctl kernel create channel");
97 goto error;
98 }
99
100 /* Setup the channel fd */
101 lkc->fd = ret;
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
108 /* Add channel to session */
109 cds_list_add(&lkc->list, &session->channel_list.head);
110 session->channel_count++;
111
112 DBG("Kernel channel %s created (fd: %d and path: %s)",
113 lkc->channel->name, lkc->fd, lkc->pathname);
114
115 return 0;
116
117 error:
118 return -1;
119 }
120
121 /*
122 * kernel_create_event
123 *
124 * Create a kernel event, enable it to the kernel tracer and add it to the
125 * channel event list of the kernel session.
126 */
127 int kernel_create_event(struct ltt_kernel_channel *channel, struct lttng_event *ev)
128 {
129 int ret;
130 struct ltt_kernel_event *event;
131
132 event = trace_create_kernel_event(ev);
133 if (event == NULL) {
134 goto error;
135 }
136
137 ret = kernctl_create_event(channel->fd, event->event);
138 if (ret < 0) {
139 perror("create event ioctl");
140 goto free_event;
141 }
142
143 event->fd = ret;
144 event->enabled = 1;
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
151 /* Add event to event list */
152 cds_list_add(&event->list, &channel->events_list.head);
153 DBG("Event %s created (fd: %d)", ev->name, event->fd);
154
155 return 0;
156
157 free_event:
158 free(event);
159 error:
160 return -1;
161 }
162
163 /*
164 * kernel_disable_event
165 *
166 * Disable a kernel event for a specific channel.
167 */
168 int 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 }
186
187 return 0;
188
189 error:
190 return -1;
191 }
192
193 /*
194 * kernel_open_metadata
195 *
196 * Create kernel metadata, open from the kernel tracer and add it to the
197 * kernel session.
198 */
199 int kernel_open_metadata(struct ltt_kernel_session *session)
200 {
201 int ret;
202 struct ltt_kernel_metadata *lkm;
203
204 /* Allocate kernel metadata */
205 lkm = trace_create_kernel_metadata();
206 if (lkm == NULL) {
207 goto error;
208 }
209
210 /* Kernel tracer metadata creation */
211 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
212 if (ret < 0) {
213 goto error;
214 }
215
216 lkm->fd = ret;
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
223 session->metadata = lkm;
224
225 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
226
227 return 0;
228
229 error:
230 return -1;
231 }
232
233 /*
234 * kernel_start_session
235 *
236 * Start tracing session.
237 */
238 int kernel_start_session(struct ltt_kernel_session *session)
239 {
240 int ret;
241
242 ret = kernctl_start_session(session->fd);
243 if (ret < 0) {
244 perror("ioctl start session");
245 goto error;
246 }
247
248 DBG("Kernel session started");
249
250 return 0;
251
252 error:
253 return ret;
254 }
255
256 /*
257 * kernel_wait_quiescent
258 *
259 * Make a kernel wait to make sure in-flight probe have completed.
260 */
261 void 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 */
279 int 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 */
296 int 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
316 /*
317 * kernel_stop_session
318 *
319 * Stop tracing session.
320 */
321 int 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
334 error:
335 return ret;
336 }
337
338 /*
339 * kernel_open_channel_stream
340 *
341 * Open stream of channel, register it to the kernel tracer and add it
342 * to the stream list of the channel.
343 *
344 * Return the number of created stream. Else, a negative value.
345 */
346 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
347 {
348 int ret;
349 struct ltt_kernel_stream *lks;
350
351 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
352 lks = trace_create_kernel_stream();
353 if (lks == NULL) {
354 close(ret);
355 goto error;
356 }
357
358 lks->fd = ret;
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
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 }
371
372 /* Add stream to channe stream list */
373 cds_list_add(&lks->list, &channel->stream_list.head);
374 channel->stream_count++;
375
376 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
377 channel->stream_count, lks->fd, lks->state, lks->pathname);
378 }
379
380 return channel->stream_count;
381
382 error:
383 return -1;
384 }
385
386 /*
387 * kernel_open_metadata_stream
388 *
389 * Open the metadata stream and set it to the kernel session.
390 */
391 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
392 {
393 int ret;
394
395 ret = kernctl_create_stream(session->metadata->fd);
396 if (ret < 0) {
397 perror("kernel create metadata stream");
398 goto error;
399 }
400
401 DBG("Kernel metadata stream created (fd: %d)", ret);
402 session->metadata_stream_fd = ret;
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 }
408
409 return 0;
410
411 error:
412 return -1;
413 }
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 */
421 ssize_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) {
450 DBG("Reallocating event list from %zd to %zd bytes", nbmem,
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
470 error:
471 return -1;
472 }
This page took 0.038294 seconds and 4 git commands to generate.