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 | * | |
54012638 DG |
34 | * Create a new kernel session, register it to the kernel tracer and add it to |
35 | * the session daemon session. | |
20fe2104 | 36 | */ |
8c0faa1d | 37 | int kernel_create_session(struct ltt_session *session, int tracer_fd) |
20fe2104 DG |
38 | { |
39 | int ret; | |
40 | struct ltt_kernel_session *lks; | |
41 | ||
54012638 DG |
42 | /* Allocate data structure */ |
43 | lks = trace_create_kernel_session(); | |
20fe2104 | 44 | if (lks == NULL) { |
54012638 | 45 | ret = -1; |
20fe2104 DG |
46 | goto error; |
47 | } | |
48 | ||
54012638 | 49 | /* Kernel tracer session creation */ |
20fe2104 DG |
50 | ret = kernctl_create_session(tracer_fd); |
51 | if (ret < 0) { | |
54012638 | 52 | perror("ioctl kernel create session"); |
20fe2104 DG |
53 | goto error; |
54 | } | |
55 | ||
20fe2104 | 56 | lks->fd = ret; |
f3ed775e | 57 | lks->kconsumer_fds_sent = 0; |
8c0faa1d DG |
58 | session->kernel_session = lks; |
59 | session->kern_session_count++; | |
8c0faa1d DG |
60 | |
61 | DBG("Kernel session created (fd: %d)", lks->fd); | |
20fe2104 DG |
62 | |
63 | return 0; | |
64 | ||
65 | error: | |
66 | return ret; | |
67 | } | |
68 | ||
69 | /* | |
70 | * kernel_create_channel | |
71 | * | |
54012638 DG |
72 | * Create a kernel channel, register it to the kernel tracer and add it to the |
73 | * kernel session. | |
20fe2104 | 74 | */ |
f3ed775e | 75 | int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_channel *chan) |
20fe2104 DG |
76 | { |
77 | int ret; | |
78 | struct ltt_kernel_channel *lkc; | |
20fe2104 | 79 | |
54012638 | 80 | /* Allocate kernel channel */ |
f3ed775e | 81 | lkc = trace_create_kernel_channel(chan); |
54012638 | 82 | if (lkc == NULL) { |
20fe2104 DG |
83 | goto error; |
84 | } | |
85 | ||
54012638 | 86 | /* Kernel tracer channel creation */ |
f3ed775e | 87 | ret = kernctl_create_channel(session->fd, &lkc->channel->attr); |
20fe2104 | 88 | if (ret < 0) { |
54012638 | 89 | perror("ioctl kernel create channel"); |
20fe2104 DG |
90 | goto error; |
91 | } | |
92 | ||
54012638 | 93 | /* Setup the channel fd */ |
20fe2104 | 94 | lkc->fd = ret; |
54012638 | 95 | /* Add channel to session */ |
8c0faa1d DG |
96 | cds_list_add(&lkc->list, &session->channel_list.head); |
97 | session->channel_count++; | |
20fe2104 | 98 | |
f3ed775e DG |
99 | DBG("Kernel channel %s created (fd: %d and path: %s)", |
100 | lkc->channel->name, lkc->fd, lkc->pathname); | |
20fe2104 DG |
101 | |
102 | return 0; | |
103 | ||
104 | error: | |
54012638 | 105 | return -1; |
20fe2104 | 106 | } |
f34daff7 DG |
107 | |
108 | /* | |
f3ed775e | 109 | * kernel_create_event |
f34daff7 | 110 | * |
54012638 DG |
111 | * Create a kernel event, enable it to the kernel tracer and add it to the |
112 | * channel event list of the kernel session. | |
f34daff7 | 113 | */ |
f3ed775e | 114 | int kernel_create_event(struct ltt_kernel_channel *channel, struct lttng_event *ev) |
f34daff7 DG |
115 | { |
116 | int ret; | |
117 | struct ltt_kernel_event *event; | |
f34daff7 | 118 | |
f3ed775e | 119 | event = trace_create_kernel_event(ev); |
54012638 | 120 | if (event == NULL) { |
f34daff7 DG |
121 | goto error; |
122 | } | |
123 | ||
f3ed775e DG |
124 | ret = kernctl_create_event(channel->fd, event->event); |
125 | if (ret < 0) { | |
126 | ERR("Unable to enable event %s for channel %s", ev->name, channel->channel->name); | |
127 | goto error; | |
8c0faa1d | 128 | } |
f34daff7 | 129 | |
f3ed775e DG |
130 | event->fd = ret; |
131 | /* Add event to event list */ | |
132 | cds_list_add(&event->list, &channel->events_list.head); | |
133 | DBG("Event %s enabled (fd: %d)", ev->name, event->fd); | |
134 | ||
f34daff7 DG |
135 | return 0; |
136 | ||
137 | error: | |
54012638 | 138 | return -1; |
f34daff7 | 139 | } |
aaf26714 DG |
140 | |
141 | /* | |
142 | * kernel_open_metadata | |
143 | * | |
54012638 DG |
144 | * Create kernel metadata, open from the kernel tracer and add it to the |
145 | * kernel session. | |
aaf26714 DG |
146 | */ |
147 | int kernel_open_metadata(struct ltt_kernel_session *session) | |
148 | { | |
149 | int ret; | |
150 | struct ltt_kernel_metadata *lkm; | |
aaf26714 | 151 | |
54012638 DG |
152 | /* Allocate kernel metadata */ |
153 | lkm = trace_create_kernel_metadata(); | |
154 | if (lkm == NULL) { | |
aaf26714 DG |
155 | goto error; |
156 | } | |
157 | ||
54012638 | 158 | /* Kernel tracer metadata creation */ |
f3ed775e | 159 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
aaf26714 DG |
160 | if (ret < 0) { |
161 | goto error; | |
162 | } | |
163 | ||
8c0faa1d | 164 | lkm->fd = ret; |
aaf26714 | 165 | session->metadata = lkm; |
8c0faa1d | 166 | |
54012638 | 167 | DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname); |
8c0faa1d DG |
168 | |
169 | return 0; | |
170 | ||
171 | error: | |
54012638 | 172 | return -1; |
8c0faa1d DG |
173 | } |
174 | ||
175 | /* | |
176 | * kernel_start_session | |
177 | * | |
178 | * Start tracing session. | |
179 | */ | |
180 | int kernel_start_session(struct ltt_kernel_session *session) | |
181 | { | |
182 | int ret; | |
183 | ||
184 | ret = kernctl_start_session(session->fd); | |
185 | if (ret < 0) { | |
f3ed775e | 186 | perror("ioctl start session"); |
8c0faa1d DG |
187 | goto error; |
188 | } | |
189 | ||
190 | DBG("Kernel session started"); | |
191 | ||
192 | return 0; | |
193 | ||
194 | error: | |
195 | return ret; | |
196 | } | |
197 | ||
f3ed775e DG |
198 | /* |
199 | * kernel_wait_quiescent | |
200 | * | |
201 | * Make a kernel wait to make sure in-flight probe have completed. | |
202 | */ | |
203 | void kernel_wait_quiescent(int fd) | |
204 | { | |
205 | int ret; | |
206 | ||
207 | DBG("Kernel quiescent wait on %d", fd); | |
208 | ||
209 | ret = kernctl_wait_quiescent(fd); | |
210 | if (ret < 0) { | |
211 | perror("wait quiescent ioctl"); | |
212 | ERR("Kernel quiescent wait failed"); | |
213 | } | |
214 | } | |
215 | ||
216 | /* | |
217 | * kernel_metadata_flush_buffer | |
218 | * | |
219 | * Force flush buffer of metadata. | |
220 | */ | |
221 | int kernel_metadata_flush_buffer(int fd) | |
222 | { | |
223 | int ret; | |
224 | ||
225 | ret = kernctl_buffer_flush(fd); | |
226 | if (ret < 0) { | |
227 | ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret); | |
228 | } | |
229 | ||
230 | return 0; | |
231 | } | |
232 | ||
233 | /* | |
234 | * kernel_flush_buffer | |
235 | * | |
236 | * Force flush buffer for channel. | |
237 | */ | |
238 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) | |
239 | { | |
240 | int ret; | |
241 | struct ltt_kernel_stream *stream; | |
242 | ||
243 | DBG("Flush buffer for channel %s", channel->channel->name); | |
244 | ||
245 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
246 | DBG("Flushing channel stream %d", stream->fd); | |
247 | ret = kernctl_buffer_flush(stream->fd); | |
248 | if (ret < 0) { | |
249 | perror("ioctl"); | |
250 | ERR("Fail to flush buffer for stream %d (ret: %d)", | |
251 | stream->fd, ret); | |
252 | } | |
253 | } | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
8c0faa1d DG |
258 | /* |
259 | * kernel_stop_session | |
260 | * | |
261 | * Stop tracing session. | |
262 | */ | |
263 | int kernel_stop_session(struct ltt_kernel_session *session) | |
264 | { | |
265 | int ret; | |
266 | ||
267 | ret = kernctl_stop_session(session->fd); | |
268 | if (ret < 0) { | |
269 | goto error; | |
270 | } | |
271 | ||
272 | DBG("Kernel session stopped"); | |
273 | ||
274 | return 0; | |
275 | ||
276 | error: | |
277 | return ret; | |
278 | } | |
279 | ||
280 | /* | |
f3ed775e | 281 | * kernel_open_channel_stream |
8c0faa1d | 282 | * |
f3ed775e | 283 | * Open stream of channel, register it to the kernel tracer and add it |
54012638 | 284 | * to the stream list of the channel. |
8c0faa1d DG |
285 | * |
286 | * Return the number of created stream. Else, a negative value. | |
287 | */ | |
f3ed775e | 288 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
8c0faa1d DG |
289 | { |
290 | int ret; | |
291 | struct ltt_kernel_stream *lks; | |
292 | ||
293 | while ((ret = kernctl_create_stream(channel->fd)) > 0) { | |
54012638 | 294 | lks = trace_create_kernel_stream(); |
8c0faa1d | 295 | if (lks == NULL) { |
54012638 | 296 | close(ret); |
8c0faa1d DG |
297 | goto error; |
298 | } | |
299 | ||
300 | lks->fd = ret; | |
301 | ret = asprintf(&lks->pathname, "%s/trace_%d", | |
302 | channel->pathname, channel->stream_count); | |
303 | if (ret < 0) { | |
304 | perror("asprintf kernel create stream"); | |
305 | goto error; | |
306 | } | |
8c0faa1d | 307 | |
54012638 | 308 | /* Add stream to channe stream list */ |
8c0faa1d DG |
309 | cds_list_add(&lks->list, &channel->stream_list.head); |
310 | channel->stream_count++; | |
8c0faa1d | 311 | |
54012638 DG |
312 | DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)", |
313 | channel->stream_count, lks->fd, lks->state, lks->pathname); | |
314 | } | |
8c0faa1d DG |
315 | |
316 | return channel->stream_count; | |
317 | ||
318 | error: | |
54012638 | 319 | return -1; |
8c0faa1d DG |
320 | } |
321 | ||
322 | /* | |
f3ed775e | 323 | * kernel_open_metadata_stream |
8c0faa1d | 324 | * |
f3ed775e | 325 | * Open the metadata stream and set it to the kernel session. |
8c0faa1d | 326 | */ |
f3ed775e | 327 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
8c0faa1d DG |
328 | { |
329 | int ret; | |
330 | ||
331 | ret = kernctl_create_stream(session->metadata->fd); | |
332 | if (ret < 0) { | |
333 | perror("kernel create metadata stream"); | |
8c0faa1d DG |
334 | goto error; |
335 | } | |
336 | ||
337 | DBG("Kernel metadata stream created (fd: %d)", ret); | |
338 | session->metadata_stream_fd = ret; | |
aaf26714 DG |
339 | |
340 | return 0; | |
341 | ||
342 | error: | |
54012638 | 343 | return -1; |
aaf26714 | 344 | } |
2ef84c95 DG |
345 | |
346 | /* | |
347 | * kernel_list_events | |
348 | * | |
349 | * Get the event list from the kernel tracer and return that list in the CTF | |
350 | * format. | |
351 | */ | |
352 | ssize_t kernel_list_events(int tracer_fd, char **list) | |
353 | { | |
354 | int fd; | |
355 | char *buf, *line = NULL; | |
356 | size_t nb, nbmem, total = 0; | |
357 | ssize_t size; | |
358 | FILE *fp; | |
359 | ||
360 | fd = kernctl_tracepoint_list(tracer_fd); | |
361 | if (fd < 0) { | |
362 | perror("kernel tracepoint list"); | |
363 | goto error; | |
364 | } | |
365 | ||
366 | fp = fdopen(fd, "r"); | |
367 | if (fp == NULL) { | |
368 | perror("kernel tracepoint list fdopen"); | |
369 | goto error; | |
370 | } | |
371 | ||
372 | /* | |
373 | * Init memory size counter | |
374 | * See kernel-ctl.h for explanation of this value | |
375 | */ | |
376 | nbmem = KERNEL_EVENT_LIST_SIZE; | |
377 | buf = malloc(nbmem); | |
378 | ||
379 | while ((size = getline(&line, &nb, fp)) != -1) { | |
380 | if (total + size > nbmem) { | |
d686b40f | 381 | DBG("Reallocating event list from %zd to %zd bytes", nbmem, |
2ef84c95 DG |
382 | total + size + KERNEL_EVENT_LIST_SIZE); |
383 | /* Adding the default size again */ | |
384 | nbmem = total + size + KERNEL_EVENT_LIST_SIZE; | |
385 | buf = realloc(buf, nbmem); | |
386 | if (buf == NULL) { | |
387 | perror("realloc list events"); | |
388 | goto error; | |
389 | } | |
390 | } | |
391 | memcpy(buf + total, line, size); | |
392 | total += size; | |
393 | } | |
394 | ||
395 | *list = buf; | |
396 | ||
397 | DBG("Kernel list events done"); | |
398 | ||
399 | return total; | |
400 | ||
401 | error: | |
402 | return -1; | |
403 | } |