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