Add debug and remove useless fct prototype
[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; 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 #define _GNU_SOURCE
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "lttngerr.h"
27 #include "ltt-sessiond.h"
28 #include "libkernelctl.h"
29 #include "kernel-ctl.h"
30
31 /*
32 * kernel_create_session
33 *
34 * Create a new kernel session using the command context session.
35 */
36 int kernel_create_session(struct ltt_session *session, int tracer_fd)
37 {
38 int ret;
39 struct ltt_kernel_session *lks;
40
41 /* Allocate a new kernel session */
42 lks = malloc(sizeof(struct ltt_kernel_session));
43 if (lks == NULL) {
44 perror("kernel session malloc");
45 ret = -errno;
46 goto error;
47 }
48
49 ret = kernctl_create_session(tracer_fd);
50 if (ret < 0) {
51 goto error;
52 }
53
54 /* Assigning session fd and to the command context */
55 lks->fd = ret;
56 lks->channel_count = 0;
57 lks->stream_count_global = 0;
58 session->kernel_session = lks;
59 session->kern_session_count++;
60 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
61
62 DBG("Kernel session created (fd: %d)", lks->fd);
63
64 return 0;
65
66 error:
67 return ret;
68 }
69
70 /*
71 * kernel_create_channel
72 *
73 * Create a kernel channel within the kernel session.
74 */
75 int kernel_create_channel(struct ltt_kernel_session *session)
76 {
77 int ret;
78 struct ltt_kernel_channel *lkc;
79 struct lttng_kernel_channel *chan;
80
81 lkc = malloc(sizeof(struct ltt_kernel_channel));
82 chan = malloc(sizeof(struct lttng_kernel_channel));
83
84 if (lkc == NULL || chan == NULL) {
85 perror("kernel channel malloc");
86 ret = -errno;
87 goto error;
88 }
89
90 chan->overwrite = DEFAULT_KERNEL_OVERWRITE;
91 chan->subbuf_size = DEFAULT_KERNEL_SUBBUF_SIZE;
92 chan->num_subbuf = DEFAULT_KERNEL_SUBBUF_NUM;
93 chan->switch_timer_interval = DEFAULT_KERNEL_SWITCH_TIMER;
94 chan->read_timer_interval = DEFAULT_KERNEL_READ_TIMER;
95
96 ret = kernctl_create_channel(session->fd, chan);
97 if (ret < 0) {
98 perror("ioctl create channel");
99 goto error;
100 }
101
102 /* Setup the channel */
103 lkc->fd = ret;
104 lkc->channel = chan;
105 lkc->stream_count = 0;
106 ret = asprintf(&lkc->pathname, "%s", DEFAULT_TRACE_OUTPUT);
107 if (ret < 0) {
108 perror("asprintf kernel create channel");
109 goto error;
110 }
111
112 DBG("Channel path set to %s", lkc->pathname);
113
114 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
115 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
116 cds_list_add(&lkc->list, &session->channel_list.head);
117 session->channel_count++;
118
119 DBG("Kernel channel created (fd: %d)", lkc->fd);
120
121 return 0;
122
123 error:
124 return ret;
125 }
126
127 /*
128 * kernel_enable_event
129 *
130 * Enable kernel event.
131 */
132 int kernel_enable_event(struct ltt_kernel_session *session, char *name)
133 {
134 int ret;
135 struct ltt_kernel_channel *chan;
136 struct ltt_kernel_event *event;
137 struct lttng_kernel_event *lke;
138
139 event = malloc(sizeof(struct ltt_kernel_event));
140 lke = malloc(sizeof(struct lttng_kernel_event));
141
142 if (event == NULL || lke == NULL) {
143 perror("kernel enable event malloc");
144 ret = -errno;
145 goto error;
146 }
147
148 /* Setting up a kernel event */
149 strncpy(lke->name, name, LTTNG_SYM_NAME_LEN);
150 lke->instrumentation = LTTNG_KERNEL_TRACEPOINTS;
151 event->event = lke;
152
153 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
154 ret = kernctl_create_event(chan->fd, lke);
155 if (ret < 0) {
156 goto error;
157 }
158
159 event->fd = ret;
160 /* Add event to event list */
161 cds_list_add(&event->list, &chan->events_list.head);
162 DBG("Event %s enabled (fd: %d)", name, event->fd);
163 }
164
165 return 0;
166
167 error:
168 return ret;
169 }
170
171 /*
172 * kernel_open_metadata
173 *
174 * Open metadata stream.
175 */
176 int kernel_open_metadata(struct ltt_kernel_session *session)
177 {
178 int ret;
179 struct ltt_kernel_metadata *lkm;
180 struct lttng_kernel_channel *conf;
181
182 lkm = malloc(sizeof(struct ltt_kernel_metadata));
183 conf = malloc(sizeof(struct lttng_kernel_channel));
184
185 if (lkm == NULL || conf == NULL) {
186 perror("kernel open metadata malloc");
187 ret = -errno;
188 goto error;
189 }
190
191 conf->overwrite = DEFAULT_KERNEL_OVERWRITE;
192 conf->subbuf_size = DEFAULT_KERNEL_SUBBUF_SIZE;
193 conf->num_subbuf = DEFAULT_KERNEL_SUBBUF_NUM;
194 conf->switch_timer_interval = DEFAULT_KERNEL_SWITCH_TIMER;
195 conf->read_timer_interval = DEFAULT_KERNEL_READ_TIMER;
196
197 ret = kernctl_open_metadata(session->fd, conf);
198 if (ret < 0) {
199 goto error;
200 }
201
202 lkm->fd = ret;
203 lkm->conf = conf;
204 ret = asprintf(&lkm->pathname, "%s/metadata", DEFAULT_TRACE_OUTPUT);
205 if (ret < 0) {
206 perror("asprintf kernel metadata");
207 goto error;
208 }
209 session->metadata = lkm;
210
211 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
212
213 return 0;
214
215 error:
216 return ret;
217 }
218
219 /*
220 * kernel_start_session
221 *
222 * Start tracing session.
223 */
224 int kernel_start_session(struct ltt_kernel_session *session)
225 {
226 int ret;
227
228 ret = kernctl_start_session(session->fd);
229 if (ret < 0) {
230 goto error;
231 }
232
233 DBG("Kernel session started");
234
235 return 0;
236
237 error:
238 return ret;
239 }
240
241 /*
242 * kernel_stop_session
243 *
244 * Stop tracing session.
245 */
246 int kernel_stop_session(struct ltt_kernel_session *session)
247 {
248 int ret;
249
250 ret = kernctl_stop_session(session->fd);
251 if (ret < 0) {
252 goto error;
253 }
254
255 DBG("Kernel session stopped");
256
257 return 0;
258
259 error:
260 return ret;
261 }
262
263 /*
264 * kernel_create_channel_stream
265 *
266 * Create a stream for a channel.
267 *
268 * Return the number of created stream. Else, a negative value.
269 */
270 int kernel_create_channel_stream(struct ltt_kernel_channel *channel)
271 {
272 int ret;
273 struct ltt_kernel_stream *lks;
274
275 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
276 lks = malloc(sizeof(struct ltt_kernel_stream));
277 if (lks == NULL) {
278 perror("kernel create stream malloc");
279 ret = -errno;
280 goto error;
281 }
282
283 lks->fd = ret;
284 ret = asprintf(&lks->pathname, "%s/trace_%d",
285 channel->pathname, channel->stream_count);
286 if (ret < 0) {
287 perror("asprintf kernel create stream");
288 goto error;
289 }
290 lks->state = 0;
291
292 cds_list_add(&lks->list, &channel->stream_list.head);
293 channel->stream_count++;
294 }
295
296 DBG("Kernel channel stream created (num: %d)", channel->stream_count);
297
298 return channel->stream_count;
299
300 error:
301 return ret;
302 }
303
304 /*
305 * kernel_create_metadata_stream
306 *
307 * Create the metadata stream.
308 */
309 int kernel_create_metadata_stream(struct ltt_kernel_session *session)
310 {
311 int ret;
312
313 ret = kernctl_create_stream(session->metadata->fd);
314 if (ret < 0) {
315 perror("kernel create metadata stream");
316 ret = -errno;
317 goto error;
318 }
319
320 DBG("Kernel metadata stream created (fd: %d)", ret);
321 session->metadata_stream_fd = ret;
322
323 return 0;
324
325 error:
326 return ret;
327 }
This page took 0.036643 seconds and 5 git commands to generate.