thread_manage_apps: update and comment socket handling
[lttng-tools.git] / ltt-sessiond / kernel-ctl.c
CommitLineData
20fe2104
DG
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
20fe2104
DG
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
8c0faa1d 19#define _GNU_SOURCE
20fe2104 20#include <errno.h>
7b395890 21#include <fcntl.h>
20fe2104
DG
22#include <stdlib.h>
23#include <stdio.h>
f34daff7 24#include <string.h>
8c0faa1d 25#include <unistd.h>
20fe2104 26
8c0faa1d 27#include "lttngerr.h"
50ecdf72 28#include "kernelctl.h"
20fe2104 29#include "kernel-ctl.h"
20fe2104 30
d65106b1
DG
31/*
32 * kernel_add_channel_context
33 *
34 * Add context on a kernel channel.
35 */
36int 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) {
b579acd9
DG
44 if (errno != EEXIST) {
45 perror("add context ioctl");
46 } else {
47 /* If EEXIST, we just ignore the error */
48 ret = 0;
49 }
d65106b1
DG
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
63error:
64 return ret;
65}
66
67/*
68 * kernel_add_event_context
69 *
70 * Add context on a kernel event.
71 */
72int 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
94error:
95 return ret;
96}
97
20fe2104
DG
98/*
99 * kernel_create_session
100 *
54012638
DG
101 * Create a new kernel session, register it to the kernel tracer and add it to
102 * the session daemon session.
20fe2104 103 */
8c0faa1d 104int kernel_create_session(struct ltt_session *session, int tracer_fd)
20fe2104
DG
105{
106 int ret;
107 struct ltt_kernel_session *lks;
108
54012638
DG
109 /* Allocate data structure */
110 lks = trace_create_kernel_session();
20fe2104 111 if (lks == NULL) {
54012638 112 ret = -1;
20fe2104
DG
113 goto error;
114 }
115
54012638 116 /* Kernel tracer session creation */
20fe2104
DG
117 ret = kernctl_create_session(tracer_fd);
118 if (ret < 0) {
54012638 119 perror("ioctl kernel create session");
20fe2104
DG
120 goto error;
121 }
122
20fe2104 123 lks->fd = ret;
7b395890
DG
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
f3ed775e 130 lks->kconsumer_fds_sent = 0;
8c0faa1d 131 session->kernel_session = lks;
8c0faa1d
DG
132
133 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104
DG
134
135 return 0;
136
137error:
138 return ret;
139}
140
141/*
142 * kernel_create_channel
143 *
54012638
DG
144 * Create a kernel channel, register it to the kernel tracer and add it to the
145 * kernel session.
20fe2104 146 */
b082db07 147int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_channel *chan, char *path)
20fe2104
DG
148{
149 int ret;
150 struct ltt_kernel_channel *lkc;
20fe2104 151
54012638 152 /* Allocate kernel channel */
b082db07 153 lkc = trace_create_kernel_channel(chan, path);
54012638 154 if (lkc == NULL) {
20fe2104
DG
155 goto error;
156 }
157
54012638 158 /* Kernel tracer channel creation */
f3ed775e 159 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 160 if (ret < 0) {
54012638 161 perror("ioctl kernel create channel");
20fe2104
DG
162 goto error;
163 }
164
54012638 165 /* Setup the channel fd */
20fe2104 166 lkc->fd = ret;
7b395890
DG
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
54012638 173 /* Add channel to session */
8c0faa1d
DG
174 cds_list_add(&lkc->list, &session->channel_list.head);
175 session->channel_count++;
20fe2104 176
f3ed775e
DG
177 DBG("Kernel channel %s created (fd: %d and path: %s)",
178 lkc->channel->name, lkc->fd, lkc->pathname);
20fe2104
DG
179
180 return 0;
181
182error:
54012638 183 return -1;
20fe2104 184}
f34daff7
DG
185
186/*
f3ed775e 187 * kernel_create_event
f34daff7 188 *
54012638
DG
189 * Create a kernel event, enable it to the kernel tracer and add it to the
190 * channel event list of the kernel session.
f34daff7 191 */
19e70852 192int kernel_create_event(struct lttng_event *ev, struct ltt_kernel_channel *channel)
f34daff7
DG
193{
194 int ret;
195 struct ltt_kernel_event *event;
f34daff7 196
f3ed775e 197 event = trace_create_kernel_event(ev);
54012638 198 if (event == NULL) {
f34daff7
DG
199 goto error;
200 }
201
f3ed775e
DG
202 ret = kernctl_create_event(channel->fd, event->event);
203 if (ret < 0) {
e953ef25
DG
204 perror("create event ioctl");
205 goto free_event;
8c0faa1d 206 }
f34daff7 207
f3ed775e 208 event->fd = ret;
7b395890
DG
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
f3ed775e
DG
215 /* Add event to event list */
216 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
217 channel->event_count++;
218
e953ef25
DG
219 DBG("Event %s created (fd: %d)", ev->name, event->fd);
220
221 return 0;
222
223free_event:
224 free(event);
225error:
226 return -1;
227}
228
26cc6b4e
DG
229/*
230 * kernel_disable_channel
231 *
232 * Disable a kernel channel.
233 */
234int 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
250error:
251 return ret;
252}
253
d36b8583
DG
254/*
255 * kernel_enable_channel
256 *
257 * Enable a kernel channel.
258 */
259int 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
275error:
276 return ret;
277}
278
19e70852
DG
279/*
280 * kernel_enable_event
281 *
282 * Enable a kernel event.
283 */
284int 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");
7d29a247
DG
291 if (errno == EEXIST) {
292 ret = -EEXIST;
293 }
19e70852
DG
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
302error:
d36b8583 303 return ret;
19e70852
DG
304}
305
e953ef25
DG
306/*
307 * kernel_disable_event
308 *
19e70852 309 * Disable a kernel event.
e953ef25 310 */
19e70852 311int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
312{
313 int ret;
19e70852
DG
314
315 ret = kernctl_disable(event->fd);
316 if (ret < 0) {
317 perror("disable event ioctl");
318 goto error;
e953ef25 319 }
f3ed775e 320
19e70852
DG
321 event->enabled = 0;
322 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
323
f34daff7
DG
324 return 0;
325
326error:
d36b8583 327 return ret;
f34daff7 328}
aaf26714
DG
329
330/*
331 * kernel_open_metadata
332 *
54012638
DG
333 * Create kernel metadata, open from the kernel tracer and add it to the
334 * kernel session.
aaf26714 335 */
58a97671 336int kernel_open_metadata(struct ltt_kernel_session *session, char *path)
aaf26714
DG
337{
338 int ret;
339 struct ltt_kernel_metadata *lkm;
aaf26714 340
54012638 341 /* Allocate kernel metadata */
58a97671 342 lkm = trace_create_kernel_metadata(path);
54012638 343 if (lkm == NULL) {
aaf26714
DG
344 goto error;
345 }
346
54012638 347 /* Kernel tracer metadata creation */
f3ed775e 348 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714
DG
349 if (ret < 0) {
350 goto error;
351 }
352
8c0faa1d 353 lkm->fd = ret;
7b395890
DG
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
aaf26714 360 session->metadata = lkm;
8c0faa1d 361
54012638 362 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
8c0faa1d
DG
363
364 return 0;
365
366error:
54012638 367 return -1;
8c0faa1d
DG
368}
369
370/*
371 * kernel_start_session
372 *
373 * Start tracing session.
374 */
375int kernel_start_session(struct ltt_kernel_session *session)
376{
377 int ret;
378
379 ret = kernctl_start_session(session->fd);
380 if (ret < 0) {
f3ed775e 381 perror("ioctl start session");
8c0faa1d
DG
382 goto error;
383 }
384
385 DBG("Kernel session started");
386
387 return 0;
388
389error:
390 return ret;
391}
392
f3ed775e
DG
393/*
394 * kernel_wait_quiescent
395 *
396 * Make a kernel wait to make sure in-flight probe have completed.
397 */
398void 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
d0254c7c
MD
411/*
412 * kernel_calibrate
413 */
414int 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
f3ed775e
DG
428/*
429 * kernel_metadata_flush_buffer
430 *
431 * Force flush buffer of metadata.
432 */
433int 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 */
450int 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
8c0faa1d
DG
470/*
471 * kernel_stop_session
472 *
473 * Stop tracing session.
474 */
475int 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
488error:
489 return ret;
490}
491
492/*
f3ed775e 493 * kernel_open_channel_stream
8c0faa1d 494 *
f3ed775e 495 * Open stream of channel, register it to the kernel tracer and add it
54012638 496 * to the stream list of the channel.
8c0faa1d
DG
497 *
498 * Return the number of created stream. Else, a negative value.
499 */
f3ed775e 500int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d
DG
501{
502 int ret;
503 struct ltt_kernel_stream *lks;
504
505 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
54012638 506 lks = trace_create_kernel_stream();
8c0faa1d 507 if (lks == NULL) {
54012638 508 close(ret);
8c0faa1d
DG
509 goto error;
510 }
511
512 lks->fd = ret;
7b395890
DG
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
8e68d1c8
DG
519 ret = asprintf(&lks->pathname, "%s/%s_%d",
520 channel->pathname, channel->channel->name, channel->stream_count);
8c0faa1d
DG
521 if (ret < 0) {
522 perror("asprintf kernel create stream");
523 goto error;
524 }
8c0faa1d 525
54012638 526 /* Add stream to channe stream list */
8c0faa1d
DG
527 cds_list_add(&lks->list, &channel->stream_list.head);
528 channel->stream_count++;
8c0faa1d 529
54012638
DG
530 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
531 channel->stream_count, lks->fd, lks->state, lks->pathname);
532 }
8c0faa1d
DG
533
534 return channel->stream_count;
535
536error:
54012638 537 return -1;
8c0faa1d
DG
538}
539
540/*
f3ed775e 541 * kernel_open_metadata_stream
8c0faa1d 542 *
f3ed775e 543 * Open the metadata stream and set it to the kernel session.
8c0faa1d 544 */
f3ed775e 545int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
546{
547 int ret;
548
549 ret = kernctl_create_stream(session->metadata->fd);
550 if (ret < 0) {
551 perror("kernel create metadata stream");
8c0faa1d
DG
552 goto error;
553 }
554
555 DBG("Kernel metadata stream created (fd: %d)", ret);
556 session->metadata_stream_fd = ret;
7b395890
DG
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 }
aaf26714
DG
562
563 return 0;
564
565error:
54012638 566 return -1;
aaf26714 567}
2ef84c95
DG
568
569/*
9f19cc17 570 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 571 */
9f19cc17 572ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
2ef84c95 573{
9f19cc17
DG
574 int fd, pos;
575 char *event;
576 size_t nbmem, count = 0;
2ef84c95
DG
577 ssize_t size;
578 FILE *fp;
9f19cc17 579 struct lttng_event *elist;
2ef84c95
DG
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");
61b73b12 590 goto error_fp;
2ef84c95
DG
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;
9f19cc17 598 elist = malloc(sizeof(struct lttng_event) * nbmem);
2ef84c95 599
9f19cc17
DG
600 while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
601 if (count > nbmem) {
ced2f820 602 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
9f19cc17 603 nbmem + KERNEL_EVENT_LIST_SIZE);
2ef84c95 604 /* Adding the default size again */
9f19cc17
DG
605 nbmem += KERNEL_EVENT_LIST_SIZE;
606 elist = realloc(elist, nbmem);
607 if (elist == NULL) {
2ef84c95 608 perror("realloc list events");
61b73b12
MD
609 count = -ENOMEM;
610 goto end;
2ef84c95
DG
611 }
612 }
99497cd0
MD
613 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
614 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
9f19cc17 615 count++;
2ef84c95
DG
616 }
617
9f19cc17 618 *events = elist;
ced2f820 619 DBG("Kernel list events done (%zu events)", count);
61b73b12
MD
620end:
621 fclose(fp); /* closes both fp and fd */
9f19cc17 622 return count;
2ef84c95 623
61b73b12
MD
624error_fp:
625 close(fd);
2ef84c95
DG
626error:
627 return -1;
628}
This page took 0.081714 seconds and 4 git commands to generate.