Remove unused header file
[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 "libkernelctl.h"
29 #include "kernel-ctl.h"
30
31 /*
32 * kernel_add_channel_context
33 *
34 * Add context on a kernel channel.
35 */
36 int 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
58 error:
59 return ret;
60 }
61
62 /*
63 * kernel_add_event_context
64 *
65 * Add context on a kernel event.
66 */
67 int 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
89 error:
90 return ret;
91 }
92
93 /*
94 * kernel_create_session
95 *
96 * Create a new kernel session, register it to the kernel tracer and add it to
97 * the session daemon session.
98 */
99 int kernel_create_session(struct ltt_session *session, int tracer_fd)
100 {
101 int ret;
102 struct ltt_kernel_session *lks;
103
104 /* Allocate data structure */
105 lks = trace_create_kernel_session();
106 if (lks == NULL) {
107 ret = -1;
108 goto error;
109 }
110
111 /* Kernel tracer session creation */
112 ret = kernctl_create_session(tracer_fd);
113 if (ret < 0) {
114 perror("ioctl kernel create session");
115 goto error;
116 }
117
118 lks->fd = ret;
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
125 lks->kconsumer_fds_sent = 0;
126 session->kernel_session = lks;
127 session->kern_session_count++;
128
129 DBG("Kernel session created (fd: %d)", lks->fd);
130
131 return 0;
132
133 error:
134 return ret;
135 }
136
137 /*
138 * kernel_create_channel
139 *
140 * Create a kernel channel, register it to the kernel tracer and add it to the
141 * kernel session.
142 */
143 int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_channel *chan, char *path)
144 {
145 int ret;
146 struct ltt_kernel_channel *lkc;
147
148 /* Allocate kernel channel */
149 lkc = trace_create_kernel_channel(chan, path);
150 if (lkc == NULL) {
151 goto error;
152 }
153
154 /* Kernel tracer channel creation */
155 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
156 if (ret < 0) {
157 perror("ioctl kernel create channel");
158 goto error;
159 }
160
161 /* Setup the channel fd */
162 lkc->fd = ret;
163 /* Prevent fd duplication after execlp() */
164 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
165 if (ret < 0) {
166 perror("fcntl session fd");
167 }
168
169 /* Add channel to session */
170 cds_list_add(&lkc->list, &session->channel_list.head);
171 session->channel_count++;
172
173 DBG("Kernel channel %s created (fd: %d and path: %s)",
174 lkc->channel->name, lkc->fd, lkc->pathname);
175
176 return 0;
177
178 error:
179 return -1;
180 }
181
182 /*
183 * kernel_create_event
184 *
185 * Create a kernel event, enable it to the kernel tracer and add it to the
186 * channel event list of the kernel session.
187 */
188 int kernel_create_event(struct lttng_event *ev, struct ltt_kernel_channel *channel)
189 {
190 int ret;
191 struct ltt_kernel_event *event;
192
193 event = trace_create_kernel_event(ev);
194 if (event == NULL) {
195 goto error;
196 }
197
198 ret = kernctl_create_event(channel->fd, event->event);
199 if (ret < 0) {
200 perror("create event ioctl");
201 goto free_event;
202 }
203
204 event->fd = ret;
205 /* Prevent fd duplication after execlp() */
206 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
207 if (ret < 0) {
208 perror("fcntl session fd");
209 }
210
211 /* Add event to event list */
212 cds_list_add(&event->list, &channel->events_list.head);
213 DBG("Event %s created (fd: %d)", ev->name, event->fd);
214
215 return 0;
216
217 free_event:
218 free(event);
219 error:
220 return -1;
221 }
222
223 /*
224 * kernel_disable_channel
225 *
226 * Disable a kernel channel.
227 */
228 int kernel_disable_channel(struct ltt_kernel_channel *chan)
229 {
230 int ret;
231
232 ret = kernctl_disable(chan->fd);
233 if (ret < 0) {
234 perror("disable chan ioctl");
235 ret = errno;
236 goto error;
237 }
238
239 chan->enabled = 0;
240 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
241
242 return 0;
243
244 error:
245 return ret;
246 }
247
248 /*
249 * kernel_enable_channel
250 *
251 * Enable a kernel channel.
252 */
253 int kernel_enable_channel(struct ltt_kernel_channel *chan)
254 {
255 int ret;
256
257 ret = kernctl_enable(chan->fd);
258 if (ret < 0) {
259 perror("enable chan ioctl");
260 ret = errno;
261 goto error;
262 }
263
264 chan->enabled = 1;
265 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
266
267 return 0;
268
269 error:
270 return ret;
271 }
272
273 /*
274 * kernel_enable_event
275 *
276 * Enable a kernel event.
277 */
278 int kernel_enable_event(struct ltt_kernel_event *event)
279 {
280 int ret;
281
282 ret = kernctl_enable(event->fd);
283 if (ret < 0) {
284 perror("enable event ioctl");
285 goto error;
286 }
287
288 event->enabled = 1;
289 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
290
291 return 0;
292
293 error:
294 return ret;
295 }
296
297 /*
298 * kernel_disable_event
299 *
300 * Disable a kernel event.
301 */
302 int kernel_disable_event(struct ltt_kernel_event *event)
303 {
304 int ret;
305
306 ret = kernctl_disable(event->fd);
307 if (ret < 0) {
308 perror("disable event ioctl");
309 goto error;
310 }
311
312 event->enabled = 0;
313 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
314
315 return 0;
316
317 error:
318 return ret;
319 }
320
321 /*
322 * kernel_open_metadata
323 *
324 * Create kernel metadata, open from the kernel tracer and add it to the
325 * kernel session.
326 */
327 int kernel_open_metadata(struct ltt_kernel_session *session, char *path)
328 {
329 int ret;
330 struct ltt_kernel_metadata *lkm;
331
332 /* Allocate kernel metadata */
333 lkm = trace_create_kernel_metadata(path);
334 if (lkm == NULL) {
335 goto error;
336 }
337
338 /* Kernel tracer metadata creation */
339 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
340 if (ret < 0) {
341 goto error;
342 }
343
344 lkm->fd = ret;
345 /* Prevent fd duplication after execlp() */
346 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
347 if (ret < 0) {
348 perror("fcntl session fd");
349 }
350
351 session->metadata = lkm;
352
353 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
354
355 return 0;
356
357 error:
358 return -1;
359 }
360
361 /*
362 * kernel_start_session
363 *
364 * Start tracing session.
365 */
366 int kernel_start_session(struct ltt_kernel_session *session)
367 {
368 int ret;
369
370 ret = kernctl_start_session(session->fd);
371 if (ret < 0) {
372 perror("ioctl start session");
373 goto error;
374 }
375
376 DBG("Kernel session started");
377
378 return 0;
379
380 error:
381 return ret;
382 }
383
384 /*
385 * kernel_wait_quiescent
386 *
387 * Make a kernel wait to make sure in-flight probe have completed.
388 */
389 void kernel_wait_quiescent(int fd)
390 {
391 int ret;
392
393 DBG("Kernel quiescent wait on %d", fd);
394
395 ret = kernctl_wait_quiescent(fd);
396 if (ret < 0) {
397 perror("wait quiescent ioctl");
398 ERR("Kernel quiescent wait failed");
399 }
400 }
401
402 /*
403 * kernel_metadata_flush_buffer
404 *
405 * Force flush buffer of metadata.
406 */
407 int kernel_metadata_flush_buffer(int fd)
408 {
409 int ret;
410
411 ret = kernctl_buffer_flush(fd);
412 if (ret < 0) {
413 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
414 }
415
416 return 0;
417 }
418
419 /*
420 * kernel_flush_buffer
421 *
422 * Force flush buffer for channel.
423 */
424 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
425 {
426 int ret;
427 struct ltt_kernel_stream *stream;
428
429 DBG("Flush buffer for channel %s", channel->channel->name);
430
431 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
432 DBG("Flushing channel stream %d", stream->fd);
433 ret = kernctl_buffer_flush(stream->fd);
434 if (ret < 0) {
435 perror("ioctl");
436 ERR("Fail to flush buffer for stream %d (ret: %d)",
437 stream->fd, ret);
438 }
439 }
440
441 return 0;
442 }
443
444 /*
445 * kernel_stop_session
446 *
447 * Stop tracing session.
448 */
449 int kernel_stop_session(struct ltt_kernel_session *session)
450 {
451 int ret;
452
453 ret = kernctl_stop_session(session->fd);
454 if (ret < 0) {
455 goto error;
456 }
457
458 DBG("Kernel session stopped");
459
460 return 0;
461
462 error:
463 return ret;
464 }
465
466 /*
467 * kernel_open_channel_stream
468 *
469 * Open stream of channel, register it to the kernel tracer and add it
470 * to the stream list of the channel.
471 *
472 * Return the number of created stream. Else, a negative value.
473 */
474 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
475 {
476 int ret;
477 struct ltt_kernel_stream *lks;
478
479 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
480 lks = trace_create_kernel_stream();
481 if (lks == NULL) {
482 close(ret);
483 goto error;
484 }
485
486 lks->fd = ret;
487 /* Prevent fd duplication after execlp() */
488 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
489 if (ret < 0) {
490 perror("fcntl session fd");
491 }
492
493 ret = asprintf(&lks->pathname, "%s/%s_%d",
494 channel->pathname, channel->channel->name, channel->stream_count);
495 if (ret < 0) {
496 perror("asprintf kernel create stream");
497 goto error;
498 }
499
500 /* Add stream to channe stream list */
501 cds_list_add(&lks->list, &channel->stream_list.head);
502 channel->stream_count++;
503
504 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
505 channel->stream_count, lks->fd, lks->state, lks->pathname);
506 }
507
508 return channel->stream_count;
509
510 error:
511 return -1;
512 }
513
514 /*
515 * kernel_open_metadata_stream
516 *
517 * Open the metadata stream and set it to the kernel session.
518 */
519 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
520 {
521 int ret;
522
523 ret = kernctl_create_stream(session->metadata->fd);
524 if (ret < 0) {
525 perror("kernel create metadata stream");
526 goto error;
527 }
528
529 DBG("Kernel metadata stream created (fd: %d)", ret);
530 session->metadata_stream_fd = ret;
531 /* Prevent fd duplication after execlp() */
532 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
533 if (ret < 0) {
534 perror("fcntl session fd");
535 }
536
537 return 0;
538
539 error:
540 return -1;
541 }
542
543 /*
544 * kernel_list_events
545 *
546 * Get the event list from the kernel tracer and return that list in the CTF
547 * format.
548 */
549 ssize_t kernel_list_events(int tracer_fd, char **list)
550 {
551 int fd;
552 char *buf, *line = NULL;
553 size_t nb, nbmem, total = 0;
554 ssize_t size;
555 FILE *fp;
556
557 fd = kernctl_tracepoint_list(tracer_fd);
558 if (fd < 0) {
559 perror("kernel tracepoint list");
560 goto error;
561 }
562
563 fp = fdopen(fd, "r");
564 if (fp == NULL) {
565 perror("kernel tracepoint list fdopen");
566 goto error;
567 }
568
569 /*
570 * Init memory size counter
571 * See kernel-ctl.h for explanation of this value
572 */
573 nbmem = KERNEL_EVENT_LIST_SIZE;
574 buf = malloc(nbmem);
575
576 while ((size = getline(&line, &nb, fp)) != -1) {
577 if (total + size > nbmem) {
578 DBG("Reallocating event list from %zd to %zd bytes", nbmem,
579 total + size + KERNEL_EVENT_LIST_SIZE);
580 /* Adding the default size again */
581 nbmem = total + size + KERNEL_EVENT_LIST_SIZE;
582 buf = realloc(buf, nbmem);
583 if (buf == NULL) {
584 perror("realloc list events");
585 goto error;
586 }
587 }
588 memcpy(buf + total, line, size);
589 total += size;
590 }
591
592 *list = buf;
593
594 DBG("Kernel list events done");
595
596 return total;
597
598 error:
599 return -1;
600 }
This page took 0.042575 seconds and 4 git commands to generate.