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