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 | 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 | |
8c0faa1d | 27 | #include "lttngerr.h" |
20fe2104 DG |
28 | #include "ltt-sessiond.h" |
29 | #include "libkernelctl.h" | |
30 | #include "kernel-ctl.h" | |
20fe2104 DG |
31 | |
32 | /* | |
33 | * kernel_create_session | |
34 | * | |
54012638 DG |
35 | * Create a new kernel session, register it to the kernel tracer and add it to |
36 | * the session daemon session. | |
20fe2104 | 37 | */ |
8c0faa1d | 38 | int kernel_create_session(struct ltt_session *session, int tracer_fd) |
20fe2104 DG |
39 | { |
40 | int ret; | |
41 | struct ltt_kernel_session *lks; | |
42 | ||
54012638 DG |
43 | /* Allocate data structure */ |
44 | lks = trace_create_kernel_session(); | |
20fe2104 | 45 | if (lks == NULL) { |
54012638 | 46 | ret = -1; |
20fe2104 DG |
47 | goto error; |
48 | } | |
49 | ||
54012638 | 50 | /* Kernel tracer session creation */ |
20fe2104 DG |
51 | ret = kernctl_create_session(tracer_fd); |
52 | if (ret < 0) { | |
54012638 | 53 | perror("ioctl kernel create session"); |
20fe2104 DG |
54 | goto error; |
55 | } | |
56 | ||
20fe2104 | 57 | lks->fd = ret; |
7b395890 DG |
58 | /* Prevent fd duplication after execlp() */ |
59 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
60 | if (ret < 0) { | |
61 | perror("fcntl session fd"); | |
62 | } | |
63 | ||
f3ed775e | 64 | lks->kconsumer_fds_sent = 0; |
8c0faa1d DG |
65 | session->kernel_session = lks; |
66 | session->kern_session_count++; | |
8c0faa1d DG |
67 | |
68 | DBG("Kernel session created (fd: %d)", lks->fd); | |
20fe2104 DG |
69 | |
70 | return 0; | |
71 | ||
72 | error: | |
73 | return ret; | |
74 | } | |
75 | ||
76 | /* | |
77 | * kernel_create_channel | |
78 | * | |
54012638 DG |
79 | * Create a kernel channel, register it to the kernel tracer and add it to the |
80 | * kernel session. | |
20fe2104 | 81 | */ |
f3ed775e | 82 | int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_channel *chan) |
20fe2104 DG |
83 | { |
84 | int ret; | |
85 | struct ltt_kernel_channel *lkc; | |
20fe2104 | 86 | |
54012638 | 87 | /* Allocate kernel channel */ |
f3ed775e | 88 | lkc = trace_create_kernel_channel(chan); |
54012638 | 89 | if (lkc == NULL) { |
20fe2104 DG |
90 | goto error; |
91 | } | |
92 | ||
54012638 | 93 | /* Kernel tracer channel creation */ |
f3ed775e | 94 | ret = kernctl_create_channel(session->fd, &lkc->channel->attr); |
20fe2104 | 95 | if (ret < 0) { |
54012638 | 96 | perror("ioctl kernel create channel"); |
20fe2104 DG |
97 | goto error; |
98 | } | |
99 | ||
54012638 | 100 | /* Setup the channel fd */ |
20fe2104 | 101 | lkc->fd = ret; |
7b395890 DG |
102 | /* Prevent fd duplication after execlp() */ |
103 | ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC); | |
104 | if (ret < 0) { | |
105 | perror("fcntl session fd"); | |
106 | } | |
107 | ||
54012638 | 108 | /* Add channel to session */ |
8c0faa1d DG |
109 | cds_list_add(&lkc->list, &session->channel_list.head); |
110 | session->channel_count++; | |
20fe2104 | 111 | |
f3ed775e DG |
112 | DBG("Kernel channel %s created (fd: %d and path: %s)", |
113 | lkc->channel->name, lkc->fd, lkc->pathname); | |
20fe2104 DG |
114 | |
115 | return 0; | |
116 | ||
117 | error: | |
54012638 | 118 | return -1; |
20fe2104 | 119 | } |
f34daff7 DG |
120 | |
121 | /* | |
f3ed775e | 122 | * kernel_create_event |
f34daff7 | 123 | * |
54012638 DG |
124 | * Create a kernel event, enable it to the kernel tracer and add it to the |
125 | * channel event list of the kernel session. | |
f34daff7 | 126 | */ |
19e70852 | 127 | int kernel_create_event(struct lttng_event *ev, struct ltt_kernel_channel *channel) |
f34daff7 DG |
128 | { |
129 | int ret; | |
130 | struct ltt_kernel_event *event; | |
f34daff7 | 131 | |
f3ed775e | 132 | event = trace_create_kernel_event(ev); |
54012638 | 133 | if (event == NULL) { |
f34daff7 DG |
134 | goto error; |
135 | } | |
136 | ||
f3ed775e DG |
137 | ret = kernctl_create_event(channel->fd, event->event); |
138 | if (ret < 0) { | |
e953ef25 DG |
139 | perror("create event ioctl"); |
140 | goto free_event; | |
8c0faa1d | 141 | } |
f34daff7 | 142 | |
f3ed775e | 143 | event->fd = ret; |
7b395890 DG |
144 | /* Prevent fd duplication after execlp() */ |
145 | ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC); | |
146 | if (ret < 0) { | |
147 | perror("fcntl session fd"); | |
148 | } | |
149 | ||
f3ed775e DG |
150 | /* Add event to event list */ |
151 | cds_list_add(&event->list, &channel->events_list.head); | |
e953ef25 DG |
152 | DBG("Event %s created (fd: %d)", ev->name, event->fd); |
153 | ||
154 | return 0; | |
155 | ||
156 | free_event: | |
157 | free(event); | |
158 | error: | |
159 | return -1; | |
160 | } | |
161 | ||
26cc6b4e DG |
162 | /* |
163 | * kernel_disable_channel | |
164 | * | |
165 | * Disable a kernel channel. | |
166 | */ | |
167 | int kernel_disable_channel(struct ltt_kernel_channel *chan) | |
168 | { | |
169 | int ret; | |
170 | ||
171 | ret = kernctl_disable(chan->fd); | |
172 | if (ret < 0) { | |
173 | perror("disable chan ioctl"); | |
174 | ret = errno; | |
175 | goto error; | |
176 | } | |
177 | ||
178 | chan->enabled = 0; | |
179 | DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd); | |
180 | ||
181 | return 0; | |
182 | ||
183 | error: | |
184 | return ret; | |
185 | } | |
186 | ||
d36b8583 DG |
187 | /* |
188 | * kernel_enable_channel | |
189 | * | |
190 | * Enable a kernel channel. | |
191 | */ | |
192 | int kernel_enable_channel(struct ltt_kernel_channel *chan) | |
193 | { | |
194 | int ret; | |
195 | ||
196 | ret = kernctl_enable(chan->fd); | |
197 | if (ret < 0) { | |
198 | perror("enable chan ioctl"); | |
199 | ret = errno; | |
200 | goto error; | |
201 | } | |
202 | ||
203 | chan->enabled = 1; | |
204 | DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd); | |
205 | ||
206 | return 0; | |
207 | ||
208 | error: | |
209 | return ret; | |
210 | } | |
211 | ||
19e70852 DG |
212 | /* |
213 | * kernel_enable_event | |
214 | * | |
215 | * Enable a kernel event. | |
216 | */ | |
217 | int kernel_enable_event(struct ltt_kernel_event *event) | |
218 | { | |
219 | int ret; | |
220 | ||
221 | ret = kernctl_enable(event->fd); | |
222 | if (ret < 0) { | |
223 | perror("enable event ioctl"); | |
224 | goto error; | |
225 | } | |
226 | ||
227 | event->enabled = 1; | |
228 | DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd); | |
229 | ||
230 | return 0; | |
231 | ||
232 | error: | |
d36b8583 | 233 | return ret; |
19e70852 DG |
234 | } |
235 | ||
e953ef25 DG |
236 | /* |
237 | * kernel_disable_event | |
238 | * | |
19e70852 | 239 | * Disable a kernel event. |
e953ef25 | 240 | */ |
19e70852 | 241 | int kernel_disable_event(struct ltt_kernel_event *event) |
e953ef25 DG |
242 | { |
243 | int ret; | |
19e70852 DG |
244 | |
245 | ret = kernctl_disable(event->fd); | |
246 | if (ret < 0) { | |
247 | perror("disable event ioctl"); | |
248 | goto error; | |
e953ef25 | 249 | } |
f3ed775e | 250 | |
19e70852 DG |
251 | event->enabled = 0; |
252 | DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd); | |
253 | ||
f34daff7 DG |
254 | return 0; |
255 | ||
256 | error: | |
d36b8583 | 257 | return ret; |
f34daff7 | 258 | } |
aaf26714 DG |
259 | |
260 | /* | |
261 | * kernel_open_metadata | |
262 | * | |
54012638 DG |
263 | * Create kernel metadata, open from the kernel tracer and add it to the |
264 | * kernel session. | |
aaf26714 DG |
265 | */ |
266 | int kernel_open_metadata(struct ltt_kernel_session *session) | |
267 | { | |
268 | int ret; | |
269 | struct ltt_kernel_metadata *lkm; | |
aaf26714 | 270 | |
54012638 DG |
271 | /* Allocate kernel metadata */ |
272 | lkm = trace_create_kernel_metadata(); | |
273 | if (lkm == NULL) { | |
aaf26714 DG |
274 | goto error; |
275 | } | |
276 | ||
54012638 | 277 | /* Kernel tracer metadata creation */ |
f3ed775e | 278 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
aaf26714 DG |
279 | if (ret < 0) { |
280 | goto error; | |
281 | } | |
282 | ||
8c0faa1d | 283 | lkm->fd = ret; |
7b395890 DG |
284 | /* Prevent fd duplication after execlp() */ |
285 | ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); | |
286 | if (ret < 0) { | |
287 | perror("fcntl session fd"); | |
288 | } | |
289 | ||
aaf26714 | 290 | session->metadata = lkm; |
8c0faa1d | 291 | |
54012638 | 292 | DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname); |
8c0faa1d DG |
293 | |
294 | return 0; | |
295 | ||
296 | error: | |
54012638 | 297 | return -1; |
8c0faa1d DG |
298 | } |
299 | ||
300 | /* | |
301 | * kernel_start_session | |
302 | * | |
303 | * Start tracing session. | |
304 | */ | |
305 | int kernel_start_session(struct ltt_kernel_session *session) | |
306 | { | |
307 | int ret; | |
308 | ||
309 | ret = kernctl_start_session(session->fd); | |
310 | if (ret < 0) { | |
f3ed775e | 311 | perror("ioctl start session"); |
8c0faa1d DG |
312 | goto error; |
313 | } | |
314 | ||
315 | DBG("Kernel session started"); | |
316 | ||
317 | return 0; | |
318 | ||
319 | error: | |
320 | return ret; | |
321 | } | |
322 | ||
f3ed775e DG |
323 | /* |
324 | * kernel_wait_quiescent | |
325 | * | |
326 | * Make a kernel wait to make sure in-flight probe have completed. | |
327 | */ | |
328 | void kernel_wait_quiescent(int fd) | |
329 | { | |
330 | int ret; | |
331 | ||
332 | DBG("Kernel quiescent wait on %d", fd); | |
333 | ||
334 | ret = kernctl_wait_quiescent(fd); | |
335 | if (ret < 0) { | |
336 | perror("wait quiescent ioctl"); | |
337 | ERR("Kernel quiescent wait failed"); | |
338 | } | |
339 | } | |
340 | ||
341 | /* | |
342 | * kernel_metadata_flush_buffer | |
343 | * | |
344 | * Force flush buffer of metadata. | |
345 | */ | |
346 | int kernel_metadata_flush_buffer(int fd) | |
347 | { | |
348 | int ret; | |
349 | ||
350 | ret = kernctl_buffer_flush(fd); | |
351 | if (ret < 0) { | |
352 | ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret); | |
353 | } | |
354 | ||
355 | return 0; | |
356 | } | |
357 | ||
358 | /* | |
359 | * kernel_flush_buffer | |
360 | * | |
361 | * Force flush buffer for channel. | |
362 | */ | |
363 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) | |
364 | { | |
365 | int ret; | |
366 | struct ltt_kernel_stream *stream; | |
367 | ||
368 | DBG("Flush buffer for channel %s", channel->channel->name); | |
369 | ||
370 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
371 | DBG("Flushing channel stream %d", stream->fd); | |
372 | ret = kernctl_buffer_flush(stream->fd); | |
373 | if (ret < 0) { | |
374 | perror("ioctl"); | |
375 | ERR("Fail to flush buffer for stream %d (ret: %d)", | |
376 | stream->fd, ret); | |
377 | } | |
378 | } | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
8c0faa1d DG |
383 | /* |
384 | * kernel_stop_session | |
385 | * | |
386 | * Stop tracing session. | |
387 | */ | |
388 | int kernel_stop_session(struct ltt_kernel_session *session) | |
389 | { | |
390 | int ret; | |
391 | ||
392 | ret = kernctl_stop_session(session->fd); | |
393 | if (ret < 0) { | |
394 | goto error; | |
395 | } | |
396 | ||
397 | DBG("Kernel session stopped"); | |
398 | ||
399 | return 0; | |
400 | ||
401 | error: | |
402 | return ret; | |
403 | } | |
404 | ||
405 | /* | |
f3ed775e | 406 | * kernel_open_channel_stream |
8c0faa1d | 407 | * |
f3ed775e | 408 | * Open stream of channel, register it to the kernel tracer and add it |
54012638 | 409 | * to the stream list of the channel. |
8c0faa1d DG |
410 | * |
411 | * Return the number of created stream. Else, a negative value. | |
412 | */ | |
f3ed775e | 413 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
8c0faa1d DG |
414 | { |
415 | int ret; | |
416 | struct ltt_kernel_stream *lks; | |
417 | ||
418 | while ((ret = kernctl_create_stream(channel->fd)) > 0) { | |
54012638 | 419 | lks = trace_create_kernel_stream(); |
8c0faa1d | 420 | if (lks == NULL) { |
54012638 | 421 | close(ret); |
8c0faa1d DG |
422 | goto error; |
423 | } | |
424 | ||
425 | lks->fd = ret; | |
7b395890 DG |
426 | /* Prevent fd duplication after execlp() */ |
427 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
428 | if (ret < 0) { | |
429 | perror("fcntl session fd"); | |
430 | } | |
431 | ||
8c0faa1d DG |
432 | ret = asprintf(&lks->pathname, "%s/trace_%d", |
433 | channel->pathname, channel->stream_count); | |
434 | if (ret < 0) { | |
435 | perror("asprintf kernel create stream"); | |
436 | goto error; | |
437 | } | |
8c0faa1d | 438 | |
54012638 | 439 | /* Add stream to channe stream list */ |
8c0faa1d DG |
440 | cds_list_add(&lks->list, &channel->stream_list.head); |
441 | channel->stream_count++; | |
8c0faa1d | 442 | |
54012638 DG |
443 | DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)", |
444 | channel->stream_count, lks->fd, lks->state, lks->pathname); | |
445 | } | |
8c0faa1d DG |
446 | |
447 | return channel->stream_count; | |
448 | ||
449 | error: | |
54012638 | 450 | return -1; |
8c0faa1d DG |
451 | } |
452 | ||
453 | /* | |
f3ed775e | 454 | * kernel_open_metadata_stream |
8c0faa1d | 455 | * |
f3ed775e | 456 | * Open the metadata stream and set it to the kernel session. |
8c0faa1d | 457 | */ |
f3ed775e | 458 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
8c0faa1d DG |
459 | { |
460 | int ret; | |
461 | ||
462 | ret = kernctl_create_stream(session->metadata->fd); | |
463 | if (ret < 0) { | |
464 | perror("kernel create metadata stream"); | |
8c0faa1d DG |
465 | goto error; |
466 | } | |
467 | ||
468 | DBG("Kernel metadata stream created (fd: %d)", ret); | |
469 | session->metadata_stream_fd = ret; | |
7b395890 DG |
470 | /* Prevent fd duplication after execlp() */ |
471 | ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); | |
472 | if (ret < 0) { | |
473 | perror("fcntl session fd"); | |
474 | } | |
aaf26714 DG |
475 | |
476 | return 0; | |
477 | ||
478 | error: | |
54012638 | 479 | return -1; |
aaf26714 | 480 | } |
2ef84c95 DG |
481 | |
482 | /* | |
483 | * kernel_list_events | |
484 | * | |
485 | * Get the event list from the kernel tracer and return that list in the CTF | |
486 | * format. | |
487 | */ | |
488 | ssize_t kernel_list_events(int tracer_fd, char **list) | |
489 | { | |
490 | int fd; | |
491 | char *buf, *line = NULL; | |
492 | size_t nb, nbmem, total = 0; | |
493 | ssize_t size; | |
494 | FILE *fp; | |
495 | ||
496 | fd = kernctl_tracepoint_list(tracer_fd); | |
497 | if (fd < 0) { | |
498 | perror("kernel tracepoint list"); | |
499 | goto error; | |
500 | } | |
501 | ||
502 | fp = fdopen(fd, "r"); | |
503 | if (fp == NULL) { | |
504 | perror("kernel tracepoint list fdopen"); | |
505 | goto error; | |
506 | } | |
507 | ||
508 | /* | |
509 | * Init memory size counter | |
510 | * See kernel-ctl.h for explanation of this value | |
511 | */ | |
512 | nbmem = KERNEL_EVENT_LIST_SIZE; | |
513 | buf = malloc(nbmem); | |
514 | ||
515 | while ((size = getline(&line, &nb, fp)) != -1) { | |
516 | if (total + size > nbmem) { | |
d686b40f | 517 | DBG("Reallocating event list from %zd to %zd bytes", nbmem, |
2ef84c95 DG |
518 | total + size + KERNEL_EVENT_LIST_SIZE); |
519 | /* Adding the default size again */ | |
520 | nbmem = total + size + KERNEL_EVENT_LIST_SIZE; | |
521 | buf = realloc(buf, nbmem); | |
522 | if (buf == NULL) { | |
523 | perror("realloc list events"); | |
524 | goto error; | |
525 | } | |
526 | } | |
527 | memcpy(buf + total, line, size); | |
528 | total += size; | |
529 | } | |
530 | ||
531 | *list = buf; | |
532 | ||
533 | DBG("Kernel list events done"); | |
534 | ||
535 | return total; | |
536 | ||
537 | error: | |
538 | return -1; | |
539 | } |