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