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