Fix handling of multiple FDs
[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
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
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#include <errno.h>
20#include <stdlib.h>
21#include <stdio.h>
f34daff7 22#include <string.h>
20fe2104
DG
23
24#include "ltt-sessiond.h"
25#include "libkernelctl.h"
26#include "kernel-ctl.h"
27#include "trace.h"
28
29/*
30 * kernel_create_session
31 *
32 * Create a new kernel session using the command context session.
33 */
34int kernel_create_session(struct command_ctx *cmd_ctx, int tracer_fd)
35{
36 int ret;
37 struct ltt_kernel_session *lks;
38
39 /* Allocate a new kernel session */
40 lks = malloc(sizeof(struct ltt_kernel_session));
41 if (lks == NULL) {
42 perror("kernel session malloc");
43 ret = -errno;
44 goto error;
45 }
46
47 ret = kernctl_create_session(tracer_fd);
48 if (ret < 0) {
49 goto error;
50 }
51
52 /* Assigning session fd and to the command context */
53 lks->fd = ret;
54 cmd_ctx->session->kernel_session = lks;
55 cmd_ctx->session->kern_session_count++;
56
57 return 0;
58
59error:
60 return ret;
61}
62
63/*
64 * kernel_create_channel
65 *
66 * Create a kernel channel within the kernel session.
67 */
68int kernel_create_channel(struct command_ctx *cmd_ctx)
69{
70 int ret;
71 struct ltt_kernel_channel *lkc;
9cb98350 72 struct lttng_kernel_channel *chan;
20fe2104
DG
73
74 lkc = malloc(sizeof(struct ltt_kernel_channel));
9cb98350 75 chan = malloc(sizeof(struct lttng_kernel_channel));
20fe2104
DG
76
77 if (lkc == NULL || chan == NULL) {
78 perror("kernel channel malloc");
79 ret = -errno;
80 goto error;
81 }
82
83 chan->overwrite = DEFAULT_KERNEL_OVERWRITE;
84 chan->subbuf_size = DEFAULT_KERNEL_SUBBUF_SIZE;
85 chan->num_subbuf = DEFAULT_KERNEL_SUBBUF_NUM;
86 chan->switch_timer_interval = DEFAULT_KERNEL_SWITCH_TIMER;
87 chan->read_timer_interval = DEFAULT_KERNEL_READ_TIMER;
88
89 ret = kernctl_create_channel(cmd_ctx->session->kernel_session->fd, chan);
90 if (ret < 0) {
91 goto error;
92 }
93
94 lkc->fd = ret;
95 lkc->channel = chan;
96 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
97
98 cmd_ctx->session->kernel_session->channel = lkc;
99
100 return 0;
101
102error:
103 return ret;
104}
f34daff7
DG
105
106/*
107 * kernel_enable_event
108 *
109 * Enable kernel event.
110 */
111int kernel_enable_event(struct ltt_kernel_channel *channel, char *name)
112{
113 int ret;
114 struct ltt_kernel_event *event;
115 struct lttng_kernel_event *lke;
116
117 event = malloc(sizeof(struct ltt_kernel_event));
118 lke = malloc(sizeof(struct lttng_kernel_event));
119
120 if (event == NULL || lke == NULL) {
121 perror("kernel enable event malloc");
122 ret = -errno;
123 goto error;
124 }
125
126 /* Setting up a kernel event */
127 strncpy(lke->name, name, LTTNG_SYM_NAME_LEN);
128 lke->instrumentation = LTTNG_KERNEL_TRACEPOINTS;
129 event->event = lke;
130
131 ret = kernctl_create_event(channel->fd, lke);
132 if (ret < 0) {
133 goto error;
134 }
135
136 /* Add event to event list */
137 cds_list_add(&event->list, &channel->events_list.head);
138
139 return 0;
140
141error:
142 return ret;
143}
This page took 0.040439 seconds and 4 git commands to generate.