Commit | Line | Data |
---|---|---|
20fe2104 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
20fe2104 DG |
7 | * |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
20fe2104 DG |
16 | */ |
17 | ||
8c0faa1d | 18 | #define _GNU_SOURCE |
20fe2104 | 19 | #include <errno.h> |
7b395890 | 20 | #include <fcntl.h> |
20fe2104 DG |
21 | #include <stdlib.h> |
22 | #include <stdio.h> | |
f34daff7 | 23 | #include <string.h> |
8c0faa1d | 24 | #include <unistd.h> |
77c7c900 | 25 | #include <inttypes.h> |
20fe2104 | 26 | |
990570ed | 27 | #include <common/common.h> |
db758600 | 28 | #include <common/kernel-ctl/kernel-ctl.h> |
42224349 | 29 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 30 | |
2f77fc4b | 31 | #include "consumer.h" |
4771f025 | 32 | #include "kernel.h" |
6dc3064a | 33 | #include "kernel-consumer.h" |
096102bd | 34 | #include "kern-modules.h" |
20fe2104 | 35 | |
d65106b1 | 36 | /* |
050349bb | 37 | * Add context on a kernel channel. |
d65106b1 DG |
38 | */ |
39 | int kernel_add_channel_context(struct ltt_kernel_channel *chan, | |
40 | struct lttng_kernel_context *ctx) | |
41 | { | |
42 | int ret; | |
43 | ||
0525e9ae DG |
44 | assert(chan); |
45 | assert(ctx); | |
46 | ||
d65106b1 DG |
47 | DBG("Adding context to channel %s", chan->channel->name); |
48 | ret = kernctl_add_context(chan->fd, ctx); | |
49 | if (ret < 0) { | |
b579acd9 | 50 | if (errno != EEXIST) { |
df0f840b | 51 | PERROR("add context ioctl"); |
b579acd9 DG |
52 | } else { |
53 | /* If EEXIST, we just ignore the error */ | |
54 | ret = 0; | |
55 | } | |
d65106b1 DG |
56 | goto error; |
57 | } | |
58 | ||
ba7f0ae5 | 59 | chan->ctx = zmalloc(sizeof(struct lttng_kernel_context)); |
d65106b1 | 60 | if (chan->ctx == NULL) { |
df0f840b | 61 | PERROR("zmalloc event context"); |
d65106b1 DG |
62 | goto error; |
63 | } | |
64 | ||
65 | memcpy(chan->ctx, ctx, sizeof(struct lttng_kernel_context)); | |
66 | ||
67 | return 0; | |
68 | ||
69 | error: | |
70 | return ret; | |
71 | } | |
72 | ||
20fe2104 | 73 | /* |
050349bb DG |
74 | * Create a new kernel session, register it to the kernel tracer and add it to |
75 | * the session daemon session. | |
20fe2104 | 76 | */ |
8c0faa1d | 77 | int kernel_create_session(struct ltt_session *session, int tracer_fd) |
20fe2104 DG |
78 | { |
79 | int ret; | |
80 | struct ltt_kernel_session *lks; | |
81 | ||
0525e9ae DG |
82 | assert(session); |
83 | ||
54012638 | 84 | /* Allocate data structure */ |
dec56f6c | 85 | lks = trace_kernel_create_session(); |
20fe2104 | 86 | if (lks == NULL) { |
54012638 | 87 | ret = -1; |
20fe2104 DG |
88 | goto error; |
89 | } | |
90 | ||
54012638 | 91 | /* Kernel tracer session creation */ |
20fe2104 DG |
92 | ret = kernctl_create_session(tracer_fd); |
93 | if (ret < 0) { | |
df0f840b | 94 | PERROR("ioctl kernel create session"); |
20fe2104 DG |
95 | goto error; |
96 | } | |
97 | ||
20fe2104 | 98 | lks->fd = ret; |
7b395890 DG |
99 | /* Prevent fd duplication after execlp() */ |
100 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
101 | if (ret < 0) { | |
df0f840b | 102 | PERROR("fcntl session fd"); |
7b395890 DG |
103 | } |
104 | ||
53632229 | 105 | lks->id = session->id; |
3bd1e081 | 106 | lks->consumer_fds_sent = 0; |
8c0faa1d | 107 | session->kernel_session = lks; |
8c0faa1d DG |
108 | |
109 | DBG("Kernel session created (fd: %d)", lks->fd); | |
20fe2104 DG |
110 | |
111 | return 0; | |
112 | ||
113 | error: | |
5f62c685 DG |
114 | if (lks) { |
115 | trace_kernel_destroy_session(lks); | |
116 | } | |
20fe2104 DG |
117 | return ret; |
118 | } | |
119 | ||
120 | /* | |
050349bb DG |
121 | * Create a kernel channel, register it to the kernel tracer and add it to the |
122 | * kernel session. | |
20fe2104 | 123 | */ |
050349bb | 124 | int kernel_create_channel(struct ltt_kernel_session *session, |
fdd9eb17 | 125 | struct lttng_channel *chan) |
20fe2104 DG |
126 | { |
127 | int ret; | |
128 | struct ltt_kernel_channel *lkc; | |
20fe2104 | 129 | |
0525e9ae DG |
130 | assert(session); |
131 | assert(chan); | |
0525e9ae | 132 | |
54012638 | 133 | /* Allocate kernel channel */ |
fdd9eb17 | 134 | lkc = trace_kernel_create_channel(chan); |
54012638 | 135 | if (lkc == NULL) { |
20fe2104 DG |
136 | goto error; |
137 | } | |
138 | ||
ecc48a90 | 139 | DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d", |
fdd9eb17 | 140 | chan->name, lkc->channel->attr.overwrite, |
173af62f DG |
141 | lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf, |
142 | lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval, | |
ecc48a90 | 143 | lkc->channel->attr.live_timer_interval, lkc->channel->attr.output); |
173af62f | 144 | |
54012638 | 145 | /* Kernel tracer channel creation */ |
f3ed775e | 146 | ret = kernctl_create_channel(session->fd, &lkc->channel->attr); |
20fe2104 | 147 | if (ret < 0) { |
df0f840b | 148 | PERROR("ioctl kernel create channel"); |
20fe2104 DG |
149 | goto error; |
150 | } | |
151 | ||
54012638 | 152 | /* Setup the channel fd */ |
20fe2104 | 153 | lkc->fd = ret; |
7b395890 DG |
154 | /* Prevent fd duplication after execlp() */ |
155 | ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC); | |
156 | if (ret < 0) { | |
df0f840b | 157 | PERROR("fcntl session fd"); |
7b395890 DG |
158 | } |
159 | ||
54012638 | 160 | /* Add channel to session */ |
8c0faa1d DG |
161 | cds_list_add(&lkc->list, &session->channel_list.head); |
162 | session->channel_count++; | |
fb5f35b6 | 163 | lkc->session = session; |
20fe2104 | 164 | |
00e2e675 | 165 | DBG("Kernel channel %s created (fd: %d)", lkc->channel->name, lkc->fd); |
20fe2104 DG |
166 | |
167 | return 0; | |
168 | ||
169 | error: | |
5f62c685 DG |
170 | if (lkc) { |
171 | free(lkc->channel); | |
172 | free(lkc); | |
173 | } | |
54012638 | 174 | return -1; |
20fe2104 | 175 | } |
f34daff7 DG |
176 | |
177 | /* | |
050349bb DG |
178 | * Create a kernel event, enable it to the kernel tracer and add it to the |
179 | * channel event list of the kernel session. | |
f34daff7 | 180 | */ |
050349bb DG |
181 | int kernel_create_event(struct lttng_event *ev, |
182 | struct ltt_kernel_channel *channel) | |
f34daff7 DG |
183 | { |
184 | int ret; | |
185 | struct ltt_kernel_event *event; | |
f34daff7 | 186 | |
0525e9ae DG |
187 | assert(ev); |
188 | assert(channel); | |
189 | ||
62499ad6 | 190 | event = trace_kernel_create_event(ev); |
54012638 | 191 | if (event == NULL) { |
d87bfb32 | 192 | ret = -1; |
f34daff7 DG |
193 | goto error; |
194 | } | |
195 | ||
f3ed775e DG |
196 | ret = kernctl_create_event(channel->fd, event->event); |
197 | if (ret < 0) { | |
bd29c13d DG |
198 | switch (errno) { |
199 | case EEXIST: | |
200 | break; | |
201 | case ENOSYS: | |
202 | WARN("Event type not implemented"); | |
203 | break; | |
8d1f6cfc DG |
204 | case ENOENT: |
205 | WARN("Event %s not found!", ev->name); | |
206 | break; | |
bd29c13d | 207 | default: |
d87bfb32 DG |
208 | PERROR("create event ioctl"); |
209 | } | |
210 | ret = -errno; | |
e953ef25 | 211 | goto free_event; |
8c0faa1d | 212 | } |
f34daff7 | 213 | |
5f822d0a | 214 | /* |
2c425ff7 | 215 | * LTTNG_KERNEL_SYSCALL event creation will return 0 on success. |
5f822d0a DG |
216 | */ |
217 | if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) { | |
218 | DBG2("Kernel event syscall creation success"); | |
87eb4ab8 MD |
219 | /* |
220 | * We use fd == -1 to ensure that we never trigger a close of fd | |
221 | * 0. | |
222 | */ | |
223 | event->fd = -1; | |
2c425ff7 | 224 | goto add_list; |
5f822d0a DG |
225 | } |
226 | ||
f3ed775e | 227 | event->fd = ret; |
7b395890 DG |
228 | /* Prevent fd duplication after execlp() */ |
229 | ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC); | |
230 | if (ret < 0) { | |
df0f840b | 231 | PERROR("fcntl session fd"); |
7b395890 DG |
232 | } |
233 | ||
2c425ff7 | 234 | add_list: |
f3ed775e DG |
235 | /* Add event to event list */ |
236 | cds_list_add(&event->list, &channel->events_list.head); | |
cbbbb275 DG |
237 | channel->event_count++; |
238 | ||
e953ef25 DG |
239 | DBG("Event %s created (fd: %d)", ev->name, event->fd); |
240 | ||
241 | return 0; | |
242 | ||
243 | free_event: | |
244 | free(event); | |
245 | error: | |
d87bfb32 | 246 | return ret; |
e953ef25 DG |
247 | } |
248 | ||
26cc6b4e | 249 | /* |
050349bb | 250 | * Disable a kernel channel. |
26cc6b4e DG |
251 | */ |
252 | int kernel_disable_channel(struct ltt_kernel_channel *chan) | |
253 | { | |
254 | int ret; | |
255 | ||
0525e9ae DG |
256 | assert(chan); |
257 | ||
26cc6b4e DG |
258 | ret = kernctl_disable(chan->fd); |
259 | if (ret < 0) { | |
df0f840b | 260 | PERROR("disable chan ioctl"); |
26cc6b4e DG |
261 | ret = errno; |
262 | goto error; | |
263 | } | |
264 | ||
265 | chan->enabled = 0; | |
266 | DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd); | |
267 | ||
268 | return 0; | |
269 | ||
270 | error: | |
271 | return ret; | |
272 | } | |
273 | ||
d36b8583 | 274 | /* |
050349bb | 275 | * Enable a kernel channel. |
d36b8583 DG |
276 | */ |
277 | int kernel_enable_channel(struct ltt_kernel_channel *chan) | |
278 | { | |
279 | int ret; | |
280 | ||
0525e9ae DG |
281 | assert(chan); |
282 | ||
d36b8583 | 283 | ret = kernctl_enable(chan->fd); |
54d01ffb | 284 | if (ret < 0 && errno != EEXIST) { |
df0f840b | 285 | PERROR("Enable kernel chan"); |
d36b8583 DG |
286 | goto error; |
287 | } | |
288 | ||
289 | chan->enabled = 1; | |
290 | DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd); | |
291 | ||
292 | return 0; | |
293 | ||
294 | error: | |
295 | return ret; | |
296 | } | |
297 | ||
19e70852 | 298 | /* |
050349bb | 299 | * Enable a kernel event. |
19e70852 DG |
300 | */ |
301 | int kernel_enable_event(struct ltt_kernel_event *event) | |
302 | { | |
303 | int ret; | |
304 | ||
0525e9ae DG |
305 | assert(event); |
306 | ||
19e70852 | 307 | ret = kernctl_enable(event->fd); |
42224349 DG |
308 | if (ret < 0) { |
309 | switch (errno) { | |
310 | case EEXIST: | |
f73fabfd | 311 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
42224349 DG |
312 | break; |
313 | default: | |
314 | PERROR("enable kernel event"); | |
315 | break; | |
316 | } | |
19e70852 DG |
317 | goto error; |
318 | } | |
319 | ||
320 | event->enabled = 1; | |
321 | DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd); | |
322 | ||
323 | return 0; | |
324 | ||
325 | error: | |
d36b8583 | 326 | return ret; |
19e70852 DG |
327 | } |
328 | ||
e953ef25 | 329 | /* |
050349bb | 330 | * Disable a kernel event. |
e953ef25 | 331 | */ |
19e70852 | 332 | int kernel_disable_event(struct ltt_kernel_event *event) |
e953ef25 DG |
333 | { |
334 | int ret; | |
19e70852 | 335 | |
0525e9ae DG |
336 | assert(event); |
337 | ||
19e70852 | 338 | ret = kernctl_disable(event->fd); |
42224349 DG |
339 | if (ret < 0) { |
340 | switch (errno) { | |
341 | case EEXIST: | |
f73fabfd | 342 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
42224349 DG |
343 | break; |
344 | default: | |
345 | PERROR("disable kernel event"); | |
346 | break; | |
347 | } | |
19e70852 | 348 | goto error; |
e953ef25 | 349 | } |
f3ed775e | 350 | |
19e70852 DG |
351 | event->enabled = 0; |
352 | DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd); | |
353 | ||
f34daff7 DG |
354 | return 0; |
355 | ||
356 | error: | |
d36b8583 | 357 | return ret; |
f34daff7 | 358 | } |
aaf26714 DG |
359 | |
360 | /* | |
050349bb DG |
361 | * Create kernel metadata, open from the kernel tracer and add it to the |
362 | * kernel session. | |
aaf26714 | 363 | */ |
a4b92340 | 364 | int kernel_open_metadata(struct ltt_kernel_session *session) |
aaf26714 DG |
365 | { |
366 | int ret; | |
74024a21 | 367 | struct ltt_kernel_metadata *lkm = NULL; |
aaf26714 | 368 | |
0525e9ae DG |
369 | assert(session); |
370 | ||
54012638 | 371 | /* Allocate kernel metadata */ |
a4b92340 | 372 | lkm = trace_kernel_create_metadata(); |
54012638 | 373 | if (lkm == NULL) { |
aaf26714 DG |
374 | goto error; |
375 | } | |
376 | ||
54012638 | 377 | /* Kernel tracer metadata creation */ |
f3ed775e | 378 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
aaf26714 | 379 | if (ret < 0) { |
74024a21 | 380 | goto error_open; |
aaf26714 DG |
381 | } |
382 | ||
8c0faa1d | 383 | lkm->fd = ret; |
7b395890 DG |
384 | /* Prevent fd duplication after execlp() */ |
385 | ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); | |
386 | if (ret < 0) { | |
df0f840b | 387 | PERROR("fcntl session fd"); |
7b395890 DG |
388 | } |
389 | ||
aaf26714 | 390 | session->metadata = lkm; |
8c0faa1d | 391 | |
00e2e675 | 392 | DBG("Kernel metadata opened (fd: %d)", lkm->fd); |
8c0faa1d DG |
393 | |
394 | return 0; | |
395 | ||
74024a21 DG |
396 | error_open: |
397 | trace_kernel_destroy_metadata(lkm); | |
8c0faa1d | 398 | error: |
54012638 | 399 | return -1; |
8c0faa1d DG |
400 | } |
401 | ||
402 | /* | |
050349bb | 403 | * Start tracing session. |
8c0faa1d DG |
404 | */ |
405 | int kernel_start_session(struct ltt_kernel_session *session) | |
406 | { | |
407 | int ret; | |
408 | ||
0525e9ae DG |
409 | assert(session); |
410 | ||
8c0faa1d DG |
411 | ret = kernctl_start_session(session->fd); |
412 | if (ret < 0) { | |
df0f840b | 413 | PERROR("ioctl start session"); |
8c0faa1d DG |
414 | goto error; |
415 | } | |
416 | ||
417 | DBG("Kernel session started"); | |
418 | ||
419 | return 0; | |
420 | ||
421 | error: | |
422 | return ret; | |
423 | } | |
424 | ||
f3ed775e | 425 | /* |
050349bb | 426 | * Make a kernel wait to make sure in-flight probe have completed. |
f3ed775e DG |
427 | */ |
428 | void kernel_wait_quiescent(int fd) | |
429 | { | |
430 | int ret; | |
431 | ||
432 | DBG("Kernel quiescent wait on %d", fd); | |
433 | ||
434 | ret = kernctl_wait_quiescent(fd); | |
435 | if (ret < 0) { | |
df0f840b | 436 | PERROR("wait quiescent ioctl"); |
f3ed775e DG |
437 | ERR("Kernel quiescent wait failed"); |
438 | } | |
439 | } | |
440 | ||
d0254c7c | 441 | /* |
050349bb | 442 | * Kernel calibrate |
d0254c7c MD |
443 | */ |
444 | int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate) | |
445 | { | |
446 | int ret; | |
447 | ||
0525e9ae DG |
448 | assert(calibrate); |
449 | ||
d0254c7c MD |
450 | ret = kernctl_calibrate(fd, calibrate); |
451 | if (ret < 0) { | |
df0f840b | 452 | PERROR("calibrate ioctl"); |
d0254c7c MD |
453 | return -1; |
454 | } | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
459 | ||
f3ed775e | 460 | /* |
f3ed775e DG |
461 | * Force flush buffer of metadata. |
462 | */ | |
463 | int kernel_metadata_flush_buffer(int fd) | |
464 | { | |
465 | int ret; | |
466 | ||
169d2cb7 DG |
467 | DBG("Kernel flushing metadata buffer on fd %d", fd); |
468 | ||
f3ed775e DG |
469 | ret = kernctl_buffer_flush(fd); |
470 | if (ret < 0) { | |
00e2e675 | 471 | ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret); |
f3ed775e DG |
472 | } |
473 | ||
474 | return 0; | |
475 | } | |
476 | ||
477 | /* | |
050349bb | 478 | * Force flush buffer for channel. |
f3ed775e DG |
479 | */ |
480 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) | |
481 | { | |
482 | int ret; | |
483 | struct ltt_kernel_stream *stream; | |
484 | ||
0525e9ae DG |
485 | assert(channel); |
486 | ||
f3ed775e DG |
487 | DBG("Flush buffer for channel %s", channel->channel->name); |
488 | ||
489 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
490 | DBG("Flushing channel stream %d", stream->fd); | |
491 | ret = kernctl_buffer_flush(stream->fd); | |
492 | if (ret < 0) { | |
df0f840b | 493 | PERROR("ioctl"); |
f3ed775e DG |
494 | ERR("Fail to flush buffer for stream %d (ret: %d)", |
495 | stream->fd, ret); | |
496 | } | |
497 | } | |
498 | ||
499 | return 0; | |
500 | } | |
501 | ||
8c0faa1d | 502 | /* |
050349bb | 503 | * Stop tracing session. |
8c0faa1d DG |
504 | */ |
505 | int kernel_stop_session(struct ltt_kernel_session *session) | |
506 | { | |
507 | int ret; | |
508 | ||
0525e9ae DG |
509 | assert(session); |
510 | ||
8c0faa1d DG |
511 | ret = kernctl_stop_session(session->fd); |
512 | if (ret < 0) { | |
513 | goto error; | |
514 | } | |
515 | ||
516 | DBG("Kernel session stopped"); | |
517 | ||
518 | return 0; | |
519 | ||
520 | error: | |
521 | return ret; | |
522 | } | |
523 | ||
524 | /* | |
050349bb DG |
525 | * Open stream of channel, register it to the kernel tracer and add it |
526 | * to the stream list of the channel. | |
8c0faa1d | 527 | * |
050349bb | 528 | * Return the number of created stream. Else, a negative value. |
8c0faa1d | 529 | */ |
f3ed775e | 530 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
8c0faa1d | 531 | { |
00e2e675 | 532 | int ret, count = 0; |
8c0faa1d DG |
533 | struct ltt_kernel_stream *lks; |
534 | ||
0525e9ae DG |
535 | assert(channel); |
536 | ||
5a47c6a2 | 537 | while ((ret = kernctl_create_stream(channel->fd)) >= 0) { |
00e2e675 | 538 | lks = trace_kernel_create_stream(channel->channel->name, count); |
8c0faa1d | 539 | if (lks == NULL) { |
799e2c4f MD |
540 | ret = close(ret); |
541 | if (ret) { | |
542 | PERROR("close"); | |
543 | } | |
8c0faa1d DG |
544 | goto error; |
545 | } | |
546 | ||
547 | lks->fd = ret; | |
7b395890 DG |
548 | /* Prevent fd duplication after execlp() */ |
549 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
550 | if (ret < 0) { | |
df0f840b | 551 | PERROR("fcntl session fd"); |
7b395890 DG |
552 | } |
553 | ||
1624d5b7 JD |
554 | lks->tracefile_size = channel->channel->attr.tracefile_size; |
555 | lks->tracefile_count = channel->channel->attr.tracefile_count; | |
556 | ||
54012638 | 557 | /* Add stream to channe stream list */ |
8c0faa1d DG |
558 | cds_list_add(&lks->list, &channel->stream_list.head); |
559 | channel->stream_count++; | |
8c0faa1d | 560 | |
00e2e675 DG |
561 | /* Increment counter which represent CPU number. */ |
562 | count++; | |
563 | ||
564 | DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd, | |
565 | lks->state); | |
54012638 | 566 | } |
8c0faa1d DG |
567 | |
568 | return channel->stream_count; | |
569 | ||
570 | error: | |
54012638 | 571 | return -1; |
8c0faa1d DG |
572 | } |
573 | ||
574 | /* | |
050349bb | 575 | * Open the metadata stream and set it to the kernel session. |
8c0faa1d | 576 | */ |
f3ed775e | 577 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
8c0faa1d DG |
578 | { |
579 | int ret; | |
580 | ||
0525e9ae DG |
581 | assert(session); |
582 | ||
8c0faa1d DG |
583 | ret = kernctl_create_stream(session->metadata->fd); |
584 | if (ret < 0) { | |
df0f840b | 585 | PERROR("kernel create metadata stream"); |
8c0faa1d DG |
586 | goto error; |
587 | } | |
588 | ||
589 | DBG("Kernel metadata stream created (fd: %d)", ret); | |
590 | session->metadata_stream_fd = ret; | |
7b395890 DG |
591 | /* Prevent fd duplication after execlp() */ |
592 | ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); | |
593 | if (ret < 0) { | |
df0f840b | 594 | PERROR("fcntl session fd"); |
7b395890 | 595 | } |
aaf26714 DG |
596 | |
597 | return 0; | |
598 | ||
599 | error: | |
54012638 | 600 | return -1; |
aaf26714 | 601 | } |
2ef84c95 DG |
602 | |
603 | /* | |
9f19cc17 | 604 | * Get the event list from the kernel tracer and return the number of elements. |
2ef84c95 | 605 | */ |
9f19cc17 | 606 | ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) |
2ef84c95 | 607 | { |
239f3aec | 608 | int fd, ret; |
9f19cc17 DG |
609 | char *event; |
610 | size_t nbmem, count = 0; | |
2ef84c95 | 611 | FILE *fp; |
9f19cc17 | 612 | struct lttng_event *elist; |
2ef84c95 | 613 | |
0525e9ae DG |
614 | assert(events); |
615 | ||
2ef84c95 DG |
616 | fd = kernctl_tracepoint_list(tracer_fd); |
617 | if (fd < 0) { | |
df0f840b | 618 | PERROR("kernel tracepoint list"); |
2ef84c95 DG |
619 | goto error; |
620 | } | |
621 | ||
622 | fp = fdopen(fd, "r"); | |
623 | if (fp == NULL) { | |
df0f840b | 624 | PERROR("kernel tracepoint list fdopen"); |
61b73b12 | 625 | goto error_fp; |
2ef84c95 DG |
626 | } |
627 | ||
628 | /* | |
629 | * Init memory size counter | |
630 | * See kernel-ctl.h for explanation of this value | |
631 | */ | |
6725fe19 | 632 | nbmem = KERNEL_EVENT_INIT_LIST_SIZE; |
ba7f0ae5 | 633 | elist = zmalloc(sizeof(struct lttng_event) * nbmem); |
3b870559 MD |
634 | if (elist == NULL) { |
635 | PERROR("alloc list events"); | |
636 | count = -ENOMEM; | |
637 | goto end; | |
638 | } | |
2ef84c95 | 639 | |
239f3aec | 640 | while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) { |
6725fe19 | 641 | if (count >= nbmem) { |
3b870559 | 642 | struct lttng_event *new_elist; |
239f3aec | 643 | size_t new_nbmem; |
3b870559 | 644 | |
239f3aec MD |
645 | new_nbmem = nbmem << 1; |
646 | DBG("Reallocating event list from %zu to %zu bytes", | |
647 | nbmem, new_nbmem); | |
648 | new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event)); | |
3b870559 | 649 | if (new_elist == NULL) { |
df0f840b | 650 | PERROR("realloc list events"); |
3b870559 MD |
651 | free(event); |
652 | free(elist); | |
61b73b12 MD |
653 | count = -ENOMEM; |
654 | goto end; | |
2ef84c95 | 655 | } |
239f3aec MD |
656 | /* Zero the new memory */ |
657 | memset(new_elist + nbmem, 0, | |
658 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); | |
659 | nbmem = new_nbmem; | |
3b870559 | 660 | elist = new_elist; |
2ef84c95 | 661 | } |
99497cd0 MD |
662 | strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN); |
663 | elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
67b9d018 | 664 | elist[count].enabled = -1; |
9f19cc17 | 665 | count++; |
3b870559 | 666 | free(event); |
2ef84c95 DG |
667 | } |
668 | ||
9f19cc17 | 669 | *events = elist; |
ced2f820 | 670 | DBG("Kernel list events done (%zu events)", count); |
61b73b12 | 671 | end: |
799e2c4f MD |
672 | ret = fclose(fp); /* closes both fp and fd */ |
673 | if (ret) { | |
674 | PERROR("fclose"); | |
675 | } | |
9f19cc17 | 676 | return count; |
2ef84c95 | 677 | |
61b73b12 | 678 | error_fp: |
799e2c4f MD |
679 | ret = close(fd); |
680 | if (ret) { | |
681 | PERROR("close"); | |
682 | } | |
2ef84c95 DG |
683 | error: |
684 | return -1; | |
685 | } | |
096102bd DG |
686 | |
687 | /* | |
688 | * Get kernel version and validate it. | |
689 | */ | |
690 | int kernel_validate_version(int tracer_fd) | |
691 | { | |
692 | int ret; | |
693 | struct lttng_kernel_tracer_version version; | |
694 | ||
695 | ret = kernctl_tracer_version(tracer_fd, &version); | |
696 | if (ret < 0) { | |
697 | ERR("Failed at getting the lttng-modules version"); | |
698 | goto error; | |
699 | } | |
700 | ||
701 | /* Validate version */ | |
a62a6556 MD |
702 | if (version.major != KERN_MODULES_PRE_MAJOR |
703 | && version.major != KERN_MODULES_MAJOR) { | |
096102bd | 704 | goto error_version; |
096102bd DG |
705 | } |
706 | ||
a62a6556 | 707 | DBG2("Kernel tracer version validated (major version %d)", version.major); |
096102bd DG |
708 | return 0; |
709 | ||
710 | error_version: | |
5df0f285 | 711 | ERR("Kernel major version %d is not compatible (supporting <= %d)", |
a62a6556 | 712 | version.major, KERN_MODULES_MAJOR) |
096102bd DG |
713 | ret = -1; |
714 | ||
715 | error: | |
716 | return ret; | |
717 | } | |
335a95b7 MD |
718 | |
719 | /* | |
720 | * Kernel work-arounds called at the start of sessiond main(). | |
721 | */ | |
722 | int init_kernel_workarounds(void) | |
723 | { | |
8936c33a | 724 | int ret; |
335a95b7 MD |
725 | FILE *fp; |
726 | ||
727 | /* | |
728 | * boot_id needs to be read once before being used concurrently | |
729 | * to deal with a Linux kernel race. A fix is proposed for | |
730 | * upstream, but the work-around is needed for older kernels. | |
731 | */ | |
732 | fp = fopen("/proc/sys/kernel/random/boot_id", "r"); | |
733 | if (!fp) { | |
734 | goto end_boot_id; | |
735 | } | |
736 | while (!feof(fp)) { | |
737 | char buf[37] = ""; | |
738 | ||
8936c33a DG |
739 | ret = fread(buf, 1, sizeof(buf), fp); |
740 | if (ret < 0) { | |
741 | /* Ignore error, we don't really care */ | |
742 | } | |
335a95b7 | 743 | } |
799e2c4f MD |
744 | ret = fclose(fp); |
745 | if (ret) { | |
746 | PERROR("fclose"); | |
747 | } | |
335a95b7 | 748 | end_boot_id: |
335a95b7 MD |
749 | return 0; |
750 | } | |
2f77fc4b DG |
751 | |
752 | /* | |
753 | * Complete teardown of a kernel session. | |
754 | */ | |
755 | void kernel_destroy_session(struct ltt_kernel_session *ksess) | |
756 | { | |
757 | if (ksess == NULL) { | |
758 | DBG3("No kernel session when tearing down session"); | |
759 | return; | |
760 | } | |
761 | ||
762 | DBG("Tearing down kernel session"); | |
763 | ||
07b86b52 | 764 | /* |
9000e568 DG |
765 | * Destroy channels on the consumer if at least one FD has been sent and we |
766 | * are in no output mode because the streams are in *no* monitor mode so we | |
767 | * have to send a command to clean them up or else they leaked. | |
07b86b52 | 768 | */ |
9000e568 | 769 | if (!ksess->output_traces && ksess->consumer_fds_sent) { |
07b86b52 JD |
770 | int ret; |
771 | struct consumer_socket *socket; | |
772 | struct lttng_ht_iter iter; | |
773 | ||
774 | /* For each consumer socket. */ | |
c7390073 | 775 | rcu_read_lock(); |
07b86b52 JD |
776 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, |
777 | socket, node.node) { | |
778 | struct ltt_kernel_channel *chan; | |
779 | ||
780 | /* For each channel, ask the consumer to destroy it. */ | |
781 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { | |
782 | ret = kernel_consumer_destroy_channel(socket, chan); | |
783 | if (ret < 0) { | |
784 | /* Consumer is probably dead. Use next socket. */ | |
785 | continue; | |
786 | } | |
787 | } | |
788 | } | |
c7390073 | 789 | rcu_read_unlock(); |
07b86b52 JD |
790 | } |
791 | ||
2f77fc4b DG |
792 | /* Close any relayd session */ |
793 | consumer_output_send_destroy_relayd(ksess->consumer); | |
794 | ||
795 | trace_kernel_destroy_session(ksess); | |
796 | } | |
fb5f35b6 DG |
797 | |
798 | /* | |
799 | * Destroy a kernel channel object. It does not do anything on the tracer side. | |
800 | */ | |
801 | void kernel_destroy_channel(struct ltt_kernel_channel *kchan) | |
802 | { | |
803 | struct ltt_kernel_session *ksess = NULL; | |
804 | ||
805 | assert(kchan); | |
806 | assert(kchan->channel); | |
807 | ||
808 | DBG3("Kernel destroy channel %s", kchan->channel->name); | |
809 | ||
810 | /* Update channel count of associated session. */ | |
811 | if (kchan->session) { | |
812 | /* Keep pointer reference so we can update it after the destroy. */ | |
813 | ksess = kchan->session; | |
814 | } | |
815 | ||
816 | trace_kernel_destroy_channel(kchan); | |
817 | ||
818 | /* | |
819 | * At this point the kernel channel is not visible anymore. This is safe | |
820 | * since in order to work on a visible kernel session, the tracing session | |
821 | * lock (ltt_session.lock) MUST be acquired. | |
822 | */ | |
823 | if (ksess) { | |
824 | ksess->channel_count--; | |
825 | } | |
826 | } | |
6dc3064a DG |
827 | |
828 | /* | |
829 | * Take a snapshot for a given kernel session. | |
830 | * | |
2a06df8d | 831 | * Return 0 on success or else return a LTTNG_ERR code. |
6dc3064a DG |
832 | */ |
833 | int kernel_snapshot_record(struct ltt_kernel_session *ksess, | |
5c786ded | 834 | struct snapshot_output *output, int wait, unsigned int nb_streams) |
6dc3064a | 835 | { |
2a06df8d | 836 | int err, ret, saved_metadata_fd; |
6dc3064a DG |
837 | struct consumer_socket *socket; |
838 | struct lttng_ht_iter iter; | |
839 | struct ltt_kernel_metadata *saved_metadata; | |
5c786ded | 840 | uint64_t max_size_per_stream = 0; |
6dc3064a DG |
841 | |
842 | assert(ksess); | |
843 | assert(ksess->consumer); | |
844 | assert(output); | |
845 | ||
846 | DBG("Kernel snapshot record started"); | |
847 | ||
848 | /* Save current metadata since the following calls will change it. */ | |
849 | saved_metadata = ksess->metadata; | |
850 | saved_metadata_fd = ksess->metadata_stream_fd; | |
851 | ||
852 | rcu_read_lock(); | |
853 | ||
854 | ret = kernel_open_metadata(ksess); | |
855 | if (ret < 0) { | |
856 | ret = LTTNG_ERR_KERN_META_FAIL; | |
857 | goto error; | |
858 | } | |
859 | ||
860 | ret = kernel_open_metadata_stream(ksess); | |
861 | if (ret < 0) { | |
862 | ret = LTTNG_ERR_KERN_META_FAIL; | |
863 | goto error_open_stream; | |
864 | } | |
865 | ||
5c786ded JD |
866 | if (output->max_size > 0 && nb_streams > 0) { |
867 | max_size_per_stream = output->max_size / nb_streams; | |
868 | } | |
869 | ||
6dc3064a DG |
870 | /* Send metadata to consumer and snapshot everything. */ |
871 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
872 | socket, node.node) { | |
873 | struct consumer_output *saved_output; | |
874 | struct ltt_kernel_channel *chan; | |
6dc3064a DG |
875 | |
876 | /* | |
877 | * Temporarly switch consumer output for our snapshot output. As long | |
878 | * as the session lock is taken, this is safe. | |
879 | */ | |
880 | saved_output = ksess->consumer; | |
881 | ksess->consumer = output->consumer; | |
882 | ||
883 | pthread_mutex_lock(socket->lock); | |
884 | /* This stream must not be monitored by the consumer. */ | |
07b86b52 | 885 | ret = kernel_consumer_add_metadata(socket, ksess, 0); |
6dc3064a | 886 | pthread_mutex_unlock(socket->lock); |
07b86b52 | 887 | /* Put back the saved consumer output into the session. */ |
6dc3064a DG |
888 | ksess->consumer = saved_output; |
889 | if (ret < 0) { | |
890 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; | |
891 | goto error_consumer; | |
892 | } | |
893 | ||
894 | /* For each channel, ask the consumer to snapshot it. */ | |
895 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { | |
5c786ded JD |
896 | if (max_size_per_stream && |
897 | chan->channel->attr.subbuf_size > max_size_per_stream) { | |
898 | ret = LTTNG_ERR_INVALID; | |
899 | DBG3("Kernel snapshot record maximum stream size %" PRIu64 | |
900 | " is smaller than subbuffer size of %" PRIu64, | |
901 | max_size_per_stream, chan->channel->attr.subbuf_size); | |
2a06df8d DG |
902 | (void) kernel_consumer_destroy_metadata(socket, |
903 | ksess->metadata); | |
5c786ded JD |
904 | goto error_consumer; |
905 | } | |
906 | ||
07b86b52 | 907 | pthread_mutex_lock(socket->lock); |
6dc3064a | 908 | ret = consumer_snapshot_channel(socket, chan->fd, output, 0, |
5c786ded JD |
909 | ksess->uid, ksess->gid, |
910 | DEFAULT_KERNEL_TRACE_DIR, wait, | |
911 | max_size_per_stream); | |
07b86b52 | 912 | pthread_mutex_unlock(socket->lock); |
6dc3064a DG |
913 | if (ret < 0) { |
914 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; | |
2a06df8d DG |
915 | (void) kernel_consumer_destroy_metadata(socket, |
916 | ksess->metadata); | |
6dc3064a DG |
917 | goto error_consumer; |
918 | } | |
919 | } | |
920 | ||
921 | /* Snapshot metadata, */ | |
07b86b52 | 922 | pthread_mutex_lock(socket->lock); |
6dc3064a | 923 | ret = consumer_snapshot_channel(socket, ksess->metadata->fd, output, |
5c786ded JD |
924 | 1, ksess->uid, ksess->gid, |
925 | DEFAULT_KERNEL_TRACE_DIR, wait, max_size_per_stream); | |
07b86b52 | 926 | pthread_mutex_unlock(socket->lock); |
6dc3064a DG |
927 | if (ret < 0) { |
928 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; | |
929 | goto error_consumer; | |
930 | } | |
07b86b52 JD |
931 | |
932 | /* | |
933 | * The metadata snapshot is done, ask the consumer to destroy it since | |
934 | * it's not monitored on the consumer side. | |
935 | */ | |
936 | (void) kernel_consumer_destroy_metadata(socket, ksess->metadata); | |
6dc3064a DG |
937 | } |
938 | ||
fac41e72 DG |
939 | ret = LTTNG_OK; |
940 | ||
6dc3064a DG |
941 | error_consumer: |
942 | /* Close newly opened metadata stream. It's now on the consumer side. */ | |
2a06df8d DG |
943 | err = close(ksess->metadata_stream_fd); |
944 | if (err < 0) { | |
6dc3064a DG |
945 | PERROR("close snapshot kernel"); |
946 | } | |
947 | ||
948 | error_open_stream: | |
949 | trace_kernel_destroy_metadata(ksess->metadata); | |
950 | error: | |
951 | /* Restore metadata state.*/ | |
952 | ksess->metadata = saved_metadata; | |
953 | ksess->metadata_stream_fd = saved_metadata_fd; | |
954 | ||
955 | rcu_read_unlock(); | |
956 | return ret; | |
957 | } |