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