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