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