Commit | Line | Data |
---|---|---|
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 | ||
8c0faa1d | 19 | #define _GNU_SOURCE |
20fe2104 DG |
20 | #include <errno.h> |
21 | #include <stdlib.h> | |
22 | #include <stdio.h> | |
f34daff7 | 23 | #include <string.h> |
8c0faa1d | 24 | #include <unistd.h> |
20fe2104 | 25 | |
8c0faa1d | 26 | #include "lttngerr.h" |
20fe2104 DG |
27 | #include "ltt-sessiond.h" |
28 | #include "libkernelctl.h" | |
29 | #include "kernel-ctl.h" | |
20fe2104 DG |
30 | |
31 | /* | |
32 | * kernel_create_session | |
33 | * | |
34 | * Create a new kernel session using the command context session. | |
35 | */ | |
8c0faa1d | 36 | int kernel_create_session(struct ltt_session *session, int tracer_fd) |
20fe2104 DG |
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; | |
8c0faa1d DG |
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); | |
20fe2104 DG |
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 | */ | |
8c0faa1d | 75 | int kernel_create_channel(struct ltt_kernel_session *session) |
20fe2104 DG |
76 | { |
77 | int ret; | |
78 | struct ltt_kernel_channel *lkc; | |
9cb98350 | 79 | struct lttng_kernel_channel *chan; |
20fe2104 DG |
80 | |
81 | lkc = malloc(sizeof(struct ltt_kernel_channel)); | |
9cb98350 | 82 | chan = malloc(sizeof(struct lttng_kernel_channel)); |
20fe2104 DG |
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 | ||
8c0faa1d | 96 | ret = kernctl_create_channel(session->fd, chan); |
20fe2104 | 97 | if (ret < 0) { |
aaf26714 | 98 | perror("ioctl create channel"); |
20fe2104 DG |
99 | goto error; |
100 | } | |
101 | ||
8c0faa1d | 102 | /* Setup the channel */ |
20fe2104 DG |
103 | lkc->fd = ret; |
104 | lkc->channel = chan; | |
8c0faa1d DG |
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 | ||
20fe2104 | 114 | CDS_INIT_LIST_HEAD(&lkc->events_list.head); |
8c0faa1d DG |
115 | CDS_INIT_LIST_HEAD(&lkc->stream_list.head); |
116 | cds_list_add(&lkc->list, &session->channel_list.head); | |
117 | session->channel_count++; | |
20fe2104 | 118 | |
8c0faa1d | 119 | DBG("Kernel channel created (fd: %d)", lkc->fd); |
20fe2104 DG |
120 | |
121 | return 0; | |
122 | ||
123 | error: | |
124 | return ret; | |
125 | } | |
f34daff7 DG |
126 | |
127 | /* | |
128 | * kernel_enable_event | |
129 | * | |
130 | * Enable kernel event. | |
131 | */ | |
8c0faa1d | 132 | int kernel_enable_event(struct ltt_kernel_session *session, char *name) |
f34daff7 DG |
133 | { |
134 | int ret; | |
8c0faa1d | 135 | struct ltt_kernel_channel *chan; |
f34daff7 DG |
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 | ||
8c0faa1d DG |
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 | } | |
f34daff7 | 158 | |
8c0faa1d DG |
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 | } | |
f34daff7 DG |
164 | |
165 | return 0; | |
166 | ||
167 | error: | |
168 | return ret; | |
169 | } | |
aaf26714 DG |
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 | ||
8c0faa1d DG |
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 | } | |
aaf26714 | 209 | session->metadata = lkm; |
8c0faa1d DG |
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; | |
aaf26714 DG |
322 | |
323 | return 0; | |
324 | ||
325 | error: | |
326 | return ret; | |
327 | } |