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