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; | |
204 | default: | |
d87bfb32 DG |
205 | PERROR("create event ioctl"); |
206 | } | |
207 | ret = -errno; | |
e953ef25 | 208 | goto free_event; |
8c0faa1d | 209 | } |
f34daff7 | 210 | |
5f822d0a | 211 | /* |
2c425ff7 | 212 | * LTTNG_KERNEL_SYSCALL event creation will return 0 on success. |
5f822d0a DG |
213 | */ |
214 | if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) { | |
215 | DBG2("Kernel event syscall creation success"); | |
87eb4ab8 MD |
216 | /* |
217 | * We use fd == -1 to ensure that we never trigger a close of fd | |
218 | * 0. | |
219 | */ | |
220 | event->fd = -1; | |
2c425ff7 | 221 | goto add_list; |
5f822d0a DG |
222 | } |
223 | ||
f3ed775e | 224 | event->fd = ret; |
7b395890 DG |
225 | /* Prevent fd duplication after execlp() */ |
226 | ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC); | |
227 | if (ret < 0) { | |
df0f840b | 228 | PERROR("fcntl session fd"); |
7b395890 DG |
229 | } |
230 | ||
2c425ff7 | 231 | add_list: |
f3ed775e DG |
232 | /* Add event to event list */ |
233 | cds_list_add(&event->list, &channel->events_list.head); | |
cbbbb275 DG |
234 | channel->event_count++; |
235 | ||
e953ef25 DG |
236 | DBG("Event %s created (fd: %d)", ev->name, event->fd); |
237 | ||
238 | return 0; | |
239 | ||
240 | free_event: | |
241 | free(event); | |
242 | error: | |
d87bfb32 | 243 | return ret; |
e953ef25 DG |
244 | } |
245 | ||
26cc6b4e | 246 | /* |
050349bb | 247 | * Disable a kernel channel. |
26cc6b4e DG |
248 | */ |
249 | int kernel_disable_channel(struct ltt_kernel_channel *chan) | |
250 | { | |
251 | int ret; | |
252 | ||
0525e9ae DG |
253 | assert(chan); |
254 | ||
26cc6b4e DG |
255 | ret = kernctl_disable(chan->fd); |
256 | if (ret < 0) { | |
df0f840b | 257 | PERROR("disable chan ioctl"); |
26cc6b4e DG |
258 | ret = errno; |
259 | goto error; | |
260 | } | |
261 | ||
262 | chan->enabled = 0; | |
263 | DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd); | |
264 | ||
265 | return 0; | |
266 | ||
267 | error: | |
268 | return ret; | |
269 | } | |
270 | ||
d36b8583 | 271 | /* |
050349bb | 272 | * Enable a kernel channel. |
d36b8583 DG |
273 | */ |
274 | int kernel_enable_channel(struct ltt_kernel_channel *chan) | |
275 | { | |
276 | int ret; | |
277 | ||
0525e9ae DG |
278 | assert(chan); |
279 | ||
d36b8583 | 280 | ret = kernctl_enable(chan->fd); |
54d01ffb | 281 | if (ret < 0 && errno != EEXIST) { |
df0f840b | 282 | PERROR("Enable kernel chan"); |
d36b8583 DG |
283 | goto error; |
284 | } | |
285 | ||
286 | chan->enabled = 1; | |
287 | DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd); | |
288 | ||
289 | return 0; | |
290 | ||
291 | error: | |
292 | return ret; | |
293 | } | |
294 | ||
19e70852 | 295 | /* |
050349bb | 296 | * Enable a kernel event. |
19e70852 DG |
297 | */ |
298 | int kernel_enable_event(struct ltt_kernel_event *event) | |
299 | { | |
300 | int ret; | |
301 | ||
0525e9ae DG |
302 | assert(event); |
303 | ||
19e70852 | 304 | ret = kernctl_enable(event->fd); |
42224349 DG |
305 | if (ret < 0) { |
306 | switch (errno) { | |
307 | case EEXIST: | |
f73fabfd | 308 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
42224349 DG |
309 | break; |
310 | default: | |
311 | PERROR("enable kernel event"); | |
312 | break; | |
313 | } | |
19e70852 DG |
314 | goto error; |
315 | } | |
316 | ||
317 | event->enabled = 1; | |
318 | DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd); | |
319 | ||
320 | return 0; | |
321 | ||
322 | error: | |
d36b8583 | 323 | return ret; |
19e70852 DG |
324 | } |
325 | ||
e953ef25 | 326 | /* |
050349bb | 327 | * Disable a kernel event. |
e953ef25 | 328 | */ |
19e70852 | 329 | int kernel_disable_event(struct ltt_kernel_event *event) |
e953ef25 DG |
330 | { |
331 | int ret; | |
19e70852 | 332 | |
0525e9ae DG |
333 | assert(event); |
334 | ||
19e70852 | 335 | ret = kernctl_disable(event->fd); |
42224349 DG |
336 | if (ret < 0) { |
337 | switch (errno) { | |
338 | case EEXIST: | |
f73fabfd | 339 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
42224349 DG |
340 | break; |
341 | default: | |
342 | PERROR("disable kernel event"); | |
343 | break; | |
344 | } | |
19e70852 | 345 | goto error; |
e953ef25 | 346 | } |
f3ed775e | 347 | |
19e70852 DG |
348 | event->enabled = 0; |
349 | DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd); | |
350 | ||
f34daff7 DG |
351 | return 0; |
352 | ||
353 | error: | |
d36b8583 | 354 | return ret; |
f34daff7 | 355 | } |
aaf26714 DG |
356 | |
357 | /* | |
050349bb DG |
358 | * Create kernel metadata, open from the kernel tracer and add it to the |
359 | * kernel session. | |
aaf26714 | 360 | */ |
a4b92340 | 361 | int kernel_open_metadata(struct ltt_kernel_session *session) |
aaf26714 DG |
362 | { |
363 | int ret; | |
74024a21 | 364 | struct ltt_kernel_metadata *lkm = NULL; |
aaf26714 | 365 | |
0525e9ae DG |
366 | assert(session); |
367 | ||
54012638 | 368 | /* Allocate kernel metadata */ |
a4b92340 | 369 | lkm = trace_kernel_create_metadata(); |
54012638 | 370 | if (lkm == NULL) { |
aaf26714 DG |
371 | goto error; |
372 | } | |
373 | ||
54012638 | 374 | /* Kernel tracer metadata creation */ |
f3ed775e | 375 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
aaf26714 | 376 | if (ret < 0) { |
74024a21 | 377 | goto error_open; |
aaf26714 DG |
378 | } |
379 | ||
8c0faa1d | 380 | lkm->fd = ret; |
7b395890 DG |
381 | /* Prevent fd duplication after execlp() */ |
382 | ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); | |
383 | if (ret < 0) { | |
df0f840b | 384 | PERROR("fcntl session fd"); |
7b395890 DG |
385 | } |
386 | ||
aaf26714 | 387 | session->metadata = lkm; |
8c0faa1d | 388 | |
00e2e675 | 389 | DBG("Kernel metadata opened (fd: %d)", lkm->fd); |
8c0faa1d DG |
390 | |
391 | return 0; | |
392 | ||
74024a21 DG |
393 | error_open: |
394 | trace_kernel_destroy_metadata(lkm); | |
8c0faa1d | 395 | error: |
54012638 | 396 | return -1; |
8c0faa1d DG |
397 | } |
398 | ||
399 | /* | |
050349bb | 400 | * Start tracing session. |
8c0faa1d DG |
401 | */ |
402 | int kernel_start_session(struct ltt_kernel_session *session) | |
403 | { | |
404 | int ret; | |
405 | ||
0525e9ae DG |
406 | assert(session); |
407 | ||
8c0faa1d DG |
408 | ret = kernctl_start_session(session->fd); |
409 | if (ret < 0) { | |
df0f840b | 410 | PERROR("ioctl start session"); |
8c0faa1d DG |
411 | goto error; |
412 | } | |
413 | ||
414 | DBG("Kernel session started"); | |
415 | ||
416 | return 0; | |
417 | ||
418 | error: | |
419 | return ret; | |
420 | } | |
421 | ||
f3ed775e | 422 | /* |
050349bb | 423 | * Make a kernel wait to make sure in-flight probe have completed. |
f3ed775e DG |
424 | */ |
425 | void kernel_wait_quiescent(int fd) | |
426 | { | |
427 | int ret; | |
428 | ||
429 | DBG("Kernel quiescent wait on %d", fd); | |
430 | ||
431 | ret = kernctl_wait_quiescent(fd); | |
432 | if (ret < 0) { | |
df0f840b | 433 | PERROR("wait quiescent ioctl"); |
f3ed775e DG |
434 | ERR("Kernel quiescent wait failed"); |
435 | } | |
436 | } | |
437 | ||
d0254c7c | 438 | /* |
050349bb | 439 | * Kernel calibrate |
d0254c7c MD |
440 | */ |
441 | int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate) | |
442 | { | |
443 | int ret; | |
444 | ||
0525e9ae DG |
445 | assert(calibrate); |
446 | ||
d0254c7c MD |
447 | ret = kernctl_calibrate(fd, calibrate); |
448 | if (ret < 0) { | |
df0f840b | 449 | PERROR("calibrate ioctl"); |
d0254c7c MD |
450 | return -1; |
451 | } | |
452 | ||
453 | return 0; | |
454 | } | |
455 | ||
456 | ||
f3ed775e | 457 | /* |
f3ed775e DG |
458 | * Force flush buffer of metadata. |
459 | */ | |
460 | int kernel_metadata_flush_buffer(int fd) | |
461 | { | |
462 | int ret; | |
463 | ||
169d2cb7 DG |
464 | DBG("Kernel flushing metadata buffer on fd %d", fd); |
465 | ||
f3ed775e DG |
466 | ret = kernctl_buffer_flush(fd); |
467 | if (ret < 0) { | |
00e2e675 | 468 | ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret); |
f3ed775e DG |
469 | } |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
474 | /* | |
050349bb | 475 | * Force flush buffer for channel. |
f3ed775e DG |
476 | */ |
477 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) | |
478 | { | |
479 | int ret; | |
480 | struct ltt_kernel_stream *stream; | |
481 | ||
0525e9ae DG |
482 | assert(channel); |
483 | ||
f3ed775e DG |
484 | DBG("Flush buffer for channel %s", channel->channel->name); |
485 | ||
486 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
487 | DBG("Flushing channel stream %d", stream->fd); | |
488 | ret = kernctl_buffer_flush(stream->fd); | |
489 | if (ret < 0) { | |
df0f840b | 490 | PERROR("ioctl"); |
f3ed775e DG |
491 | ERR("Fail to flush buffer for stream %d (ret: %d)", |
492 | stream->fd, ret); | |
493 | } | |
494 | } | |
495 | ||
496 | return 0; | |
497 | } | |
498 | ||
8c0faa1d | 499 | /* |
050349bb | 500 | * Stop tracing session. |
8c0faa1d DG |
501 | */ |
502 | int kernel_stop_session(struct ltt_kernel_session *session) | |
503 | { | |
504 | int ret; | |
505 | ||
0525e9ae DG |
506 | assert(session); |
507 | ||
8c0faa1d DG |
508 | ret = kernctl_stop_session(session->fd); |
509 | if (ret < 0) { | |
510 | goto error; | |
511 | } | |
512 | ||
513 | DBG("Kernel session stopped"); | |
514 | ||
515 | return 0; | |
516 | ||
517 | error: | |
518 | return ret; | |
519 | } | |
520 | ||
521 | /* | |
050349bb DG |
522 | * Open stream of channel, register it to the kernel tracer and add it |
523 | * to the stream list of the channel. | |
8c0faa1d | 524 | * |
050349bb | 525 | * Return the number of created stream. Else, a negative value. |
8c0faa1d | 526 | */ |
f3ed775e | 527 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
8c0faa1d | 528 | { |
00e2e675 | 529 | int ret, count = 0; |
8c0faa1d DG |
530 | struct ltt_kernel_stream *lks; |
531 | ||
0525e9ae DG |
532 | assert(channel); |
533 | ||
5a47c6a2 | 534 | while ((ret = kernctl_create_stream(channel->fd)) >= 0) { |
00e2e675 | 535 | lks = trace_kernel_create_stream(channel->channel->name, count); |
8c0faa1d | 536 | if (lks == NULL) { |
799e2c4f MD |
537 | ret = close(ret); |
538 | if (ret) { | |
539 | PERROR("close"); | |
540 | } | |
8c0faa1d DG |
541 | goto error; |
542 | } | |
543 | ||
544 | lks->fd = ret; | |
7b395890 DG |
545 | /* Prevent fd duplication after execlp() */ |
546 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
547 | if (ret < 0) { | |
df0f840b | 548 | PERROR("fcntl session fd"); |
7b395890 DG |
549 | } |
550 | ||
1624d5b7 JD |
551 | lks->tracefile_size = channel->channel->attr.tracefile_size; |
552 | lks->tracefile_count = channel->channel->attr.tracefile_count; | |
553 | ||
54012638 | 554 | /* Add stream to channe stream list */ |
8c0faa1d DG |
555 | cds_list_add(&lks->list, &channel->stream_list.head); |
556 | channel->stream_count++; | |
8c0faa1d | 557 | |
00e2e675 DG |
558 | /* Increment counter which represent CPU number. */ |
559 | count++; | |
560 | ||
561 | DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd, | |
562 | lks->state); | |
54012638 | 563 | } |
8c0faa1d DG |
564 | |
565 | return channel->stream_count; | |
566 | ||
567 | error: | |
54012638 | 568 | return -1; |
8c0faa1d DG |
569 | } |
570 | ||
571 | /* | |
050349bb | 572 | * Open the metadata stream and set it to the kernel session. |
8c0faa1d | 573 | */ |
f3ed775e | 574 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
8c0faa1d DG |
575 | { |
576 | int ret; | |
577 | ||
0525e9ae DG |
578 | assert(session); |
579 | ||
8c0faa1d DG |
580 | ret = kernctl_create_stream(session->metadata->fd); |
581 | if (ret < 0) { | |
df0f840b | 582 | PERROR("kernel create metadata stream"); |
8c0faa1d DG |
583 | goto error; |
584 | } | |
585 | ||
586 | DBG("Kernel metadata stream created (fd: %d)", ret); | |
587 | session->metadata_stream_fd = ret; | |
7b395890 DG |
588 | /* Prevent fd duplication after execlp() */ |
589 | ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); | |
590 | if (ret < 0) { | |
df0f840b | 591 | PERROR("fcntl session fd"); |
7b395890 | 592 | } |
aaf26714 DG |
593 | |
594 | return 0; | |
595 | ||
596 | error: | |
54012638 | 597 | return -1; |
aaf26714 | 598 | } |
2ef84c95 DG |
599 | |
600 | /* | |
9f19cc17 | 601 | * Get the event list from the kernel tracer and return the number of elements. |
2ef84c95 | 602 | */ |
9f19cc17 | 603 | ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) |
2ef84c95 | 604 | { |
799e2c4f | 605 | int fd, pos, ret; |
9f19cc17 DG |
606 | char *event; |
607 | size_t nbmem, count = 0; | |
2ef84c95 | 608 | FILE *fp; |
9f19cc17 | 609 | struct lttng_event *elist; |
2ef84c95 | 610 | |
0525e9ae DG |
611 | assert(events); |
612 | ||
2ef84c95 DG |
613 | fd = kernctl_tracepoint_list(tracer_fd); |
614 | if (fd < 0) { | |
df0f840b | 615 | PERROR("kernel tracepoint list"); |
2ef84c95 DG |
616 | goto error; |
617 | } | |
618 | ||
619 | fp = fdopen(fd, "r"); | |
620 | if (fp == NULL) { | |
df0f840b | 621 | PERROR("kernel tracepoint list fdopen"); |
61b73b12 | 622 | goto error_fp; |
2ef84c95 DG |
623 | } |
624 | ||
625 | /* | |
626 | * Init memory size counter | |
627 | * See kernel-ctl.h for explanation of this value | |
628 | */ | |
6725fe19 | 629 | nbmem = KERNEL_EVENT_INIT_LIST_SIZE; |
ba7f0ae5 | 630 | elist = zmalloc(sizeof(struct lttng_event) * nbmem); |
3b870559 MD |
631 | if (elist == NULL) { |
632 | PERROR("alloc list events"); | |
633 | count = -ENOMEM; | |
634 | goto end; | |
635 | } | |
2ef84c95 | 636 | |
c617c0c6 | 637 | while (fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos) == 1) { |
6725fe19 | 638 | if (count >= nbmem) { |
3b870559 MD |
639 | struct lttng_event *new_elist; |
640 | ||
ced2f820 | 641 | DBG("Reallocating event list from %zu to %zu bytes", nbmem, |
6725fe19 DG |
642 | nbmem * 2); |
643 | /* Double the size */ | |
644 | nbmem <<= 1; | |
3b870559 MD |
645 | new_elist = realloc(elist, nbmem * sizeof(struct lttng_event)); |
646 | if (new_elist == NULL) { | |
df0f840b | 647 | PERROR("realloc list events"); |
3b870559 MD |
648 | free(event); |
649 | free(elist); | |
61b73b12 MD |
650 | count = -ENOMEM; |
651 | goto end; | |
2ef84c95 | 652 | } |
3b870559 | 653 | elist = new_elist; |
2ef84c95 | 654 | } |
99497cd0 MD |
655 | strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN); |
656 | elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
67b9d018 | 657 | elist[count].enabled = -1; |
9f19cc17 | 658 | count++; |
3b870559 | 659 | free(event); |
2ef84c95 DG |
660 | } |
661 | ||
9f19cc17 | 662 | *events = elist; |
ced2f820 | 663 | DBG("Kernel list events done (%zu events)", count); |
61b73b12 | 664 | end: |
799e2c4f MD |
665 | ret = fclose(fp); /* closes both fp and fd */ |
666 | if (ret) { | |
667 | PERROR("fclose"); | |
668 | } | |
9f19cc17 | 669 | return count; |
2ef84c95 | 670 | |
61b73b12 | 671 | error_fp: |
799e2c4f MD |
672 | ret = close(fd); |
673 | if (ret) { | |
674 | PERROR("close"); | |
675 | } | |
2ef84c95 DG |
676 | error: |
677 | return -1; | |
678 | } | |
096102bd DG |
679 | |
680 | /* | |
681 | * Get kernel version and validate it. | |
682 | */ | |
683 | int kernel_validate_version(int tracer_fd) | |
684 | { | |
685 | int ret; | |
686 | struct lttng_kernel_tracer_version version; | |
687 | ||
688 | ret = kernctl_tracer_version(tracer_fd, &version); | |
689 | if (ret < 0) { | |
690 | ERR("Failed at getting the lttng-modules version"); | |
691 | goto error; | |
692 | } | |
693 | ||
694 | /* Validate version */ | |
a62a6556 MD |
695 | if (version.major != KERN_MODULES_PRE_MAJOR |
696 | && version.major != KERN_MODULES_MAJOR) { | |
096102bd | 697 | goto error_version; |
096102bd DG |
698 | } |
699 | ||
a62a6556 | 700 | DBG2("Kernel tracer version validated (major version %d)", version.major); |
096102bd DG |
701 | return 0; |
702 | ||
703 | error_version: | |
5df0f285 | 704 | ERR("Kernel major version %d is not compatible (supporting <= %d)", |
a62a6556 | 705 | version.major, KERN_MODULES_MAJOR) |
096102bd DG |
706 | ret = -1; |
707 | ||
708 | error: | |
709 | return ret; | |
710 | } | |
335a95b7 MD |
711 | |
712 | /* | |
713 | * Kernel work-arounds called at the start of sessiond main(). | |
714 | */ | |
715 | int init_kernel_workarounds(void) | |
716 | { | |
8936c33a | 717 | int ret; |
335a95b7 MD |
718 | FILE *fp; |
719 | ||
720 | /* | |
721 | * boot_id needs to be read once before being used concurrently | |
722 | * to deal with a Linux kernel race. A fix is proposed for | |
723 | * upstream, but the work-around is needed for older kernels. | |
724 | */ | |
725 | fp = fopen("/proc/sys/kernel/random/boot_id", "r"); | |
726 | if (!fp) { | |
727 | goto end_boot_id; | |
728 | } | |
729 | while (!feof(fp)) { | |
730 | char buf[37] = ""; | |
731 | ||
8936c33a DG |
732 | ret = fread(buf, 1, sizeof(buf), fp); |
733 | if (ret < 0) { | |
734 | /* Ignore error, we don't really care */ | |
735 | } | |
335a95b7 | 736 | } |
799e2c4f MD |
737 | ret = fclose(fp); |
738 | if (ret) { | |
739 | PERROR("fclose"); | |
740 | } | |
335a95b7 | 741 | end_boot_id: |
335a95b7 MD |
742 | return 0; |
743 | } | |
2f77fc4b DG |
744 | |
745 | /* | |
746 | * Complete teardown of a kernel session. | |
747 | */ | |
748 | void kernel_destroy_session(struct ltt_kernel_session *ksess) | |
749 | { | |
750 | if (ksess == NULL) { | |
751 | DBG3("No kernel session when tearing down session"); | |
752 | return; | |
753 | } | |
754 | ||
755 | DBG("Tearing down kernel session"); | |
756 | ||
07b86b52 JD |
757 | /* |
758 | * Destroy channels on the consumer if in no output mode because the | |
759 | * streams are in *no* monitor mode so we have to send a command to clean | |
760 | * them up or else they leaked. | |
761 | */ | |
762 | if (!ksess->output_traces) { | |
763 | int ret; | |
764 | struct consumer_socket *socket; | |
765 | struct lttng_ht_iter iter; | |
766 | ||
767 | /* For each consumer socket. */ | |
768 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
769 | socket, node.node) { | |
770 | struct ltt_kernel_channel *chan; | |
771 | ||
772 | /* For each channel, ask the consumer to destroy it. */ | |
773 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { | |
774 | ret = kernel_consumer_destroy_channel(socket, chan); | |
775 | if (ret < 0) { | |
776 | /* Consumer is probably dead. Use next socket. */ | |
777 | continue; | |
778 | } | |
779 | } | |
780 | } | |
781 | } | |
782 | ||
2f77fc4b DG |
783 | /* Close any relayd session */ |
784 | consumer_output_send_destroy_relayd(ksess->consumer); | |
785 | ||
786 | trace_kernel_destroy_session(ksess); | |
787 | } | |
fb5f35b6 DG |
788 | |
789 | /* | |
790 | * Destroy a kernel channel object. It does not do anything on the tracer side. | |
791 | */ | |
792 | void kernel_destroy_channel(struct ltt_kernel_channel *kchan) | |
793 | { | |
794 | struct ltt_kernel_session *ksess = NULL; | |
795 | ||
796 | assert(kchan); | |
797 | assert(kchan->channel); | |
798 | ||
799 | DBG3("Kernel destroy channel %s", kchan->channel->name); | |
800 | ||
801 | /* Update channel count of associated session. */ | |
802 | if (kchan->session) { | |
803 | /* Keep pointer reference so we can update it after the destroy. */ | |
804 | ksess = kchan->session; | |
805 | } | |
806 | ||
807 | trace_kernel_destroy_channel(kchan); | |
808 | ||
809 | /* | |
810 | * At this point the kernel channel is not visible anymore. This is safe | |
811 | * since in order to work on a visible kernel session, the tracing session | |
812 | * lock (ltt_session.lock) MUST be acquired. | |
813 | */ | |
814 | if (ksess) { | |
815 | ksess->channel_count--; | |
816 | } | |
817 | } | |
6dc3064a DG |
818 | |
819 | /* | |
820 | * Take a snapshot for a given kernel session. | |
821 | * | |
2a06df8d | 822 | * Return 0 on success or else return a LTTNG_ERR code. |
6dc3064a DG |
823 | */ |
824 | int kernel_snapshot_record(struct ltt_kernel_session *ksess, | |
5c786ded | 825 | struct snapshot_output *output, int wait, unsigned int nb_streams) |
6dc3064a | 826 | { |
2a06df8d | 827 | int err, ret, saved_metadata_fd; |
6dc3064a DG |
828 | struct consumer_socket *socket; |
829 | struct lttng_ht_iter iter; | |
830 | struct ltt_kernel_metadata *saved_metadata; | |
5c786ded | 831 | uint64_t max_size_per_stream = 0; |
6dc3064a DG |
832 | |
833 | assert(ksess); | |
834 | assert(ksess->consumer); | |
835 | assert(output); | |
836 | ||
837 | DBG("Kernel snapshot record started"); | |
838 | ||
839 | /* Save current metadata since the following calls will change it. */ | |
840 | saved_metadata = ksess->metadata; | |
841 | saved_metadata_fd = ksess->metadata_stream_fd; | |
842 | ||
843 | rcu_read_lock(); | |
844 | ||
845 | ret = kernel_open_metadata(ksess); | |
846 | if (ret < 0) { | |
847 | ret = LTTNG_ERR_KERN_META_FAIL; | |
848 | goto error; | |
849 | } | |
850 | ||
851 | ret = kernel_open_metadata_stream(ksess); | |
852 | if (ret < 0) { | |
853 | ret = LTTNG_ERR_KERN_META_FAIL; | |
854 | goto error_open_stream; | |
855 | } | |
856 | ||
5c786ded JD |
857 | if (output->max_size > 0 && nb_streams > 0) { |
858 | max_size_per_stream = output->max_size / nb_streams; | |
859 | } | |
860 | ||
6dc3064a DG |
861 | /* Send metadata to consumer and snapshot everything. */ |
862 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
863 | socket, node.node) { | |
864 | struct consumer_output *saved_output; | |
865 | struct ltt_kernel_channel *chan; | |
6dc3064a DG |
866 | |
867 | /* | |
868 | * Temporarly switch consumer output for our snapshot output. As long | |
869 | * as the session lock is taken, this is safe. | |
870 | */ | |
871 | saved_output = ksess->consumer; | |
872 | ksess->consumer = output->consumer; | |
873 | ||
874 | pthread_mutex_lock(socket->lock); | |
875 | /* This stream must not be monitored by the consumer. */ | |
07b86b52 | 876 | ret = kernel_consumer_add_metadata(socket, ksess, 0); |
6dc3064a | 877 | pthread_mutex_unlock(socket->lock); |
07b86b52 | 878 | /* Put back the saved consumer output into the session. */ |
6dc3064a DG |
879 | ksess->consumer = saved_output; |
880 | if (ret < 0) { | |
881 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; | |
882 | goto error_consumer; | |
883 | } | |
884 | ||
885 | /* For each channel, ask the consumer to snapshot it. */ | |
886 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { | |
5c786ded JD |
887 | if (max_size_per_stream && |
888 | chan->channel->attr.subbuf_size > max_size_per_stream) { | |
889 | ret = LTTNG_ERR_INVALID; | |
890 | DBG3("Kernel snapshot record maximum stream size %" PRIu64 | |
891 | " is smaller than subbuffer size of %" PRIu64, | |
892 | max_size_per_stream, chan->channel->attr.subbuf_size); | |
2a06df8d DG |
893 | (void) kernel_consumer_destroy_metadata(socket, |
894 | ksess->metadata); | |
5c786ded JD |
895 | goto error_consumer; |
896 | } | |
897 | ||
07b86b52 | 898 | pthread_mutex_lock(socket->lock); |
6dc3064a | 899 | ret = consumer_snapshot_channel(socket, chan->fd, output, 0, |
5c786ded JD |
900 | ksess->uid, ksess->gid, |
901 | DEFAULT_KERNEL_TRACE_DIR, wait, | |
902 | max_size_per_stream); | |
07b86b52 | 903 | pthread_mutex_unlock(socket->lock); |
6dc3064a DG |
904 | if (ret < 0) { |
905 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; | |
2a06df8d DG |
906 | (void) kernel_consumer_destroy_metadata(socket, |
907 | ksess->metadata); | |
6dc3064a DG |
908 | goto error_consumer; |
909 | } | |
910 | } | |
911 | ||
912 | /* Snapshot metadata, */ | |
07b86b52 | 913 | pthread_mutex_lock(socket->lock); |
6dc3064a | 914 | ret = consumer_snapshot_channel(socket, ksess->metadata->fd, output, |
5c786ded JD |
915 | 1, ksess->uid, ksess->gid, |
916 | DEFAULT_KERNEL_TRACE_DIR, wait, max_size_per_stream); | |
07b86b52 | 917 | pthread_mutex_unlock(socket->lock); |
6dc3064a DG |
918 | if (ret < 0) { |
919 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; | |
920 | goto error_consumer; | |
921 | } | |
07b86b52 JD |
922 | |
923 | /* | |
924 | * The metadata snapshot is done, ask the consumer to destroy it since | |
925 | * it's not monitored on the consumer side. | |
926 | */ | |
927 | (void) kernel_consumer_destroy_metadata(socket, ksess->metadata); | |
6dc3064a DG |
928 | } |
929 | ||
fac41e72 DG |
930 | ret = LTTNG_OK; |
931 | ||
6dc3064a DG |
932 | error_consumer: |
933 | /* Close newly opened metadata stream. It's now on the consumer side. */ | |
2a06df8d DG |
934 | err = close(ksess->metadata_stream_fd); |
935 | if (err < 0) { | |
6dc3064a DG |
936 | PERROR("close snapshot kernel"); |
937 | } | |
938 | ||
939 | error_open_stream: | |
940 | trace_kernel_destroy_metadata(ksess->metadata); | |
941 | error: | |
942 | /* Restore metadata state.*/ | |
943 | ksess->metadata = saved_metadata; | |
944 | ksess->metadata_stream_fd = saved_metadata_fd; | |
945 | ||
946 | rcu_read_unlock(); | |
947 | return ret; | |
948 | } |