cfea8942da7f421d4eeaf272f59b51261fe08e3e
[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 "kernelctl.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_calibrate
413 */
414 int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
415 {
416 int ret;
417
418 ret = kernctl_calibrate(fd, calibrate);
419 if (ret < 0) {
420 perror("calibrate ioctl");
421 return -1;
422 }
423
424 return 0;
425 }
426
427
428 /*
429 * kernel_metadata_flush_buffer
430 *
431 * Force flush buffer of metadata.
432 */
433 int kernel_metadata_flush_buffer(int fd)
434 {
435 int ret;
436
437 ret = kernctl_buffer_flush(fd);
438 if (ret < 0) {
439 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
440 }
441
442 return 0;
443 }
444
445 /*
446 * kernel_flush_buffer
447 *
448 * Force flush buffer for channel.
449 */
450 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
451 {
452 int ret;
453 struct ltt_kernel_stream *stream;
454
455 DBG("Flush buffer for channel %s", channel->channel->name);
456
457 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
458 DBG("Flushing channel stream %d", stream->fd);
459 ret = kernctl_buffer_flush(stream->fd);
460 if (ret < 0) {
461 perror("ioctl");
462 ERR("Fail to flush buffer for stream %d (ret: %d)",
463 stream->fd, ret);
464 }
465 }
466
467 return 0;
468 }
469
470 /*
471 * kernel_stop_session
472 *
473 * Stop tracing session.
474 */
475 int kernel_stop_session(struct ltt_kernel_session *session)
476 {
477 int ret;
478
479 ret = kernctl_stop_session(session->fd);
480 if (ret < 0) {
481 goto error;
482 }
483
484 DBG("Kernel session stopped");
485
486 return 0;
487
488 error:
489 return ret;
490 }
491
492 /*
493 * kernel_open_channel_stream
494 *
495 * Open stream of channel, register it to the kernel tracer and add it
496 * to the stream list of the channel.
497 *
498 * Return the number of created stream. Else, a negative value.
499 */
500 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
501 {
502 int ret;
503 struct ltt_kernel_stream *lks;
504
505 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
506 lks = trace_create_kernel_stream();
507 if (lks == NULL) {
508 close(ret);
509 goto error;
510 }
511
512 lks->fd = ret;
513 /* Prevent fd duplication after execlp() */
514 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
515 if (ret < 0) {
516 perror("fcntl session fd");
517 }
518
519 ret = asprintf(&lks->pathname, "%s/%s_%d",
520 channel->pathname, channel->channel->name, channel->stream_count);
521 if (ret < 0) {
522 perror("asprintf kernel create stream");
523 goto error;
524 }
525
526 /* Add stream to channe stream list */
527 cds_list_add(&lks->list, &channel->stream_list.head);
528 channel->stream_count++;
529
530 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
531 channel->stream_count, lks->fd, lks->state, lks->pathname);
532 }
533
534 return channel->stream_count;
535
536 error:
537 return -1;
538 }
539
540 /*
541 * kernel_open_metadata_stream
542 *
543 * Open the metadata stream and set it to the kernel session.
544 */
545 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
546 {
547 int ret;
548
549 ret = kernctl_create_stream(session->metadata->fd);
550 if (ret < 0) {
551 perror("kernel create metadata stream");
552 goto error;
553 }
554
555 DBG("Kernel metadata stream created (fd: %d)", ret);
556 session->metadata_stream_fd = ret;
557 /* Prevent fd duplication after execlp() */
558 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
559 if (ret < 0) {
560 perror("fcntl session fd");
561 }
562
563 return 0;
564
565 error:
566 return -1;
567 }
568
569 /*
570 * Get the event list from the kernel tracer and return the number of elements.
571 */
572 ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
573 {
574 int fd, pos;
575 char *event;
576 size_t nbmem, count = 0;
577 ssize_t size;
578 FILE *fp;
579 struct lttng_event *elist;
580
581 fd = kernctl_tracepoint_list(tracer_fd);
582 if (fd < 0) {
583 perror("kernel tracepoint list");
584 goto error;
585 }
586
587 fp = fdopen(fd, "r");
588 if (fp == NULL) {
589 perror("kernel tracepoint list fdopen");
590 goto error;
591 }
592
593 /*
594 * Init memory size counter
595 * See kernel-ctl.h for explanation of this value
596 */
597 nbmem = KERNEL_EVENT_LIST_SIZE;
598 elist = malloc(sizeof(struct lttng_event) * nbmem);
599
600 while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
601 if (count > nbmem) {
602 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
603 nbmem + KERNEL_EVENT_LIST_SIZE);
604 /* Adding the default size again */
605 nbmem += KERNEL_EVENT_LIST_SIZE;
606 elist = realloc(elist, nbmem);
607 if (elist == NULL) {
608 perror("realloc list events");
609 goto error;
610 }
611 }
612 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
613 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
614 count++;
615 }
616
617 *events = elist;
618
619 DBG("Kernel list events done (%zu events)", count);
620
621 return count;
622
623 error:
624 return -1;
625 }
This page took 0.040991 seconds and 3 git commands to generate.