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" |
096102bd | 33 | #include "kern-modules.h" |
20fe2104 | 34 | |
d65106b1 | 35 | /* |
050349bb | 36 | * Add context on a kernel channel. |
d65106b1 DG |
37 | */ |
38 | int kernel_add_channel_context(struct ltt_kernel_channel *chan, | |
39 | struct lttng_kernel_context *ctx) | |
40 | { | |
41 | int ret; | |
42 | ||
0525e9ae DG |
43 | assert(chan); |
44 | assert(ctx); | |
45 | ||
d65106b1 DG |
46 | DBG("Adding context to channel %s", chan->channel->name); |
47 | ret = kernctl_add_context(chan->fd, ctx); | |
48 | if (ret < 0) { | |
b579acd9 | 49 | if (errno != EEXIST) { |
df0f840b | 50 | PERROR("add context ioctl"); |
b579acd9 DG |
51 | } else { |
52 | /* If EEXIST, we just ignore the error */ | |
53 | ret = 0; | |
54 | } | |
d65106b1 DG |
55 | goto error; |
56 | } | |
57 | ||
ba7f0ae5 | 58 | chan->ctx = zmalloc(sizeof(struct lttng_kernel_context)); |
d65106b1 | 59 | if (chan->ctx == NULL) { |
df0f840b | 60 | PERROR("zmalloc event context"); |
d65106b1 DG |
61 | goto error; |
62 | } | |
63 | ||
64 | memcpy(chan->ctx, ctx, sizeof(struct lttng_kernel_context)); | |
65 | ||
66 | return 0; | |
67 | ||
68 | error: | |
69 | return ret; | |
70 | } | |
71 | ||
20fe2104 | 72 | /* |
050349bb DG |
73 | * Create a new kernel session, register it to the kernel tracer and add it to |
74 | * the session daemon session. | |
20fe2104 | 75 | */ |
8c0faa1d | 76 | int kernel_create_session(struct ltt_session *session, int tracer_fd) |
20fe2104 DG |
77 | { |
78 | int ret; | |
79 | struct ltt_kernel_session *lks; | |
80 | ||
0525e9ae DG |
81 | assert(session); |
82 | ||
54012638 | 83 | /* Allocate data structure */ |
dec56f6c | 84 | lks = trace_kernel_create_session(); |
20fe2104 | 85 | if (lks == NULL) { |
54012638 | 86 | ret = -1; |
20fe2104 DG |
87 | goto error; |
88 | } | |
89 | ||
54012638 | 90 | /* Kernel tracer session creation */ |
20fe2104 DG |
91 | ret = kernctl_create_session(tracer_fd); |
92 | if (ret < 0) { | |
df0f840b | 93 | PERROR("ioctl kernel create session"); |
20fe2104 DG |
94 | goto error; |
95 | } | |
96 | ||
20fe2104 | 97 | lks->fd = ret; |
7b395890 DG |
98 | /* Prevent fd duplication after execlp() */ |
99 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
100 | if (ret < 0) { | |
df0f840b | 101 | PERROR("fcntl session fd"); |
7b395890 DG |
102 | } |
103 | ||
53632229 | 104 | lks->id = session->id; |
3bd1e081 | 105 | lks->consumer_fds_sent = 0; |
8c0faa1d | 106 | session->kernel_session = lks; |
8c0faa1d DG |
107 | |
108 | DBG("Kernel session created (fd: %d)", lks->fd); | |
20fe2104 DG |
109 | |
110 | return 0; | |
111 | ||
112 | error: | |
5f62c685 DG |
113 | if (lks) { |
114 | trace_kernel_destroy_session(lks); | |
115 | } | |
20fe2104 DG |
116 | return ret; |
117 | } | |
118 | ||
119 | /* | |
050349bb DG |
120 | * Create a kernel channel, register it to the kernel tracer and add it to the |
121 | * kernel session. | |
20fe2104 | 122 | */ |
050349bb | 123 | int kernel_create_channel(struct ltt_kernel_session *session, |
fdd9eb17 | 124 | struct lttng_channel *chan) |
20fe2104 DG |
125 | { |
126 | int ret; | |
127 | struct ltt_kernel_channel *lkc; | |
20fe2104 | 128 | |
0525e9ae DG |
129 | assert(session); |
130 | assert(chan); | |
0525e9ae | 131 | |
54012638 | 132 | /* Allocate kernel channel */ |
fdd9eb17 | 133 | lkc = trace_kernel_create_channel(chan); |
54012638 | 134 | if (lkc == NULL) { |
20fe2104 DG |
135 | goto error; |
136 | } | |
137 | ||
fdd9eb17 DG |
138 | DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d", |
139 | chan->name, lkc->channel->attr.overwrite, | |
173af62f DG |
140 | lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf, |
141 | lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval, | |
142 | lkc->channel->attr.output); | |
143 | ||
54012638 | 144 | /* Kernel tracer channel creation */ |
f3ed775e | 145 | ret = kernctl_create_channel(session->fd, &lkc->channel->attr); |
20fe2104 | 146 | if (ret < 0) { |
df0f840b | 147 | PERROR("ioctl kernel create channel"); |
20fe2104 DG |
148 | goto error; |
149 | } | |
150 | ||
54012638 | 151 | /* Setup the channel fd */ |
20fe2104 | 152 | lkc->fd = ret; |
7b395890 DG |
153 | /* Prevent fd duplication after execlp() */ |
154 | ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC); | |
155 | if (ret < 0) { | |
df0f840b | 156 | PERROR("fcntl session fd"); |
7b395890 DG |
157 | } |
158 | ||
54012638 | 159 | /* Add channel to session */ |
8c0faa1d DG |
160 | cds_list_add(&lkc->list, &session->channel_list.head); |
161 | session->channel_count++; | |
fb5f35b6 | 162 | lkc->session = session; |
20fe2104 | 163 | |
00e2e675 | 164 | DBG("Kernel channel %s created (fd: %d)", lkc->channel->name, lkc->fd); |
20fe2104 DG |
165 | |
166 | return 0; | |
167 | ||
168 | error: | |
5f62c685 DG |
169 | if (lkc) { |
170 | free(lkc->channel); | |
171 | free(lkc); | |
172 | } | |
54012638 | 173 | return -1; |
20fe2104 | 174 | } |
f34daff7 DG |
175 | |
176 | /* | |
050349bb DG |
177 | * Create a kernel event, enable it to the kernel tracer and add it to the |
178 | * channel event list of the kernel session. | |
f34daff7 | 179 | */ |
050349bb DG |
180 | int kernel_create_event(struct lttng_event *ev, |
181 | struct ltt_kernel_channel *channel) | |
f34daff7 DG |
182 | { |
183 | int ret; | |
184 | struct ltt_kernel_event *event; | |
f34daff7 | 185 | |
0525e9ae DG |
186 | assert(ev); |
187 | assert(channel); | |
188 | ||
62499ad6 | 189 | event = trace_kernel_create_event(ev); |
54012638 | 190 | if (event == NULL) { |
d87bfb32 | 191 | ret = -1; |
f34daff7 DG |
192 | goto error; |
193 | } | |
194 | ||
f3ed775e DG |
195 | ret = kernctl_create_event(channel->fd, event->event); |
196 | if (ret < 0) { | |
bd29c13d DG |
197 | switch (errno) { |
198 | case EEXIST: | |
199 | break; | |
200 | case ENOSYS: | |
201 | WARN("Event type not implemented"); | |
202 | break; | |
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 DG |
355 | |
356 | /* | |
050349bb DG |
357 | * Create kernel metadata, open from the kernel tracer and add it to the |
358 | * kernel session. | |
aaf26714 | 359 | */ |
a4b92340 | 360 | int kernel_open_metadata(struct ltt_kernel_session *session) |
aaf26714 DG |
361 | { |
362 | int ret; | |
74024a21 | 363 | struct ltt_kernel_metadata *lkm = NULL; |
aaf26714 | 364 | |
0525e9ae DG |
365 | assert(session); |
366 | ||
54012638 | 367 | /* Allocate kernel metadata */ |
a4b92340 | 368 | lkm = trace_kernel_create_metadata(); |
54012638 | 369 | if (lkm == NULL) { |
aaf26714 DG |
370 | goto error; |
371 | } | |
372 | ||
54012638 | 373 | /* Kernel tracer metadata creation */ |
f3ed775e | 374 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
aaf26714 | 375 | if (ret < 0) { |
74024a21 | 376 | goto error_open; |
aaf26714 DG |
377 | } |
378 | ||
8c0faa1d | 379 | lkm->fd = ret; |
7b395890 DG |
380 | /* Prevent fd duplication after execlp() */ |
381 | ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); | |
382 | if (ret < 0) { | |
df0f840b | 383 | PERROR("fcntl session fd"); |
7b395890 DG |
384 | } |
385 | ||
aaf26714 | 386 | session->metadata = lkm; |
8c0faa1d | 387 | |
00e2e675 | 388 | DBG("Kernel metadata opened (fd: %d)", lkm->fd); |
8c0faa1d DG |
389 | |
390 | return 0; | |
391 | ||
74024a21 DG |
392 | error_open: |
393 | trace_kernel_destroy_metadata(lkm); | |
8c0faa1d | 394 | error: |
54012638 | 395 | return -1; |
8c0faa1d DG |
396 | } |
397 | ||
398 | /* | |
050349bb | 399 | * Start tracing session. |
8c0faa1d DG |
400 | */ |
401 | int kernel_start_session(struct ltt_kernel_session *session) | |
402 | { | |
403 | int ret; | |
404 | ||
0525e9ae DG |
405 | assert(session); |
406 | ||
8c0faa1d DG |
407 | ret = kernctl_start_session(session->fd); |
408 | if (ret < 0) { | |
df0f840b | 409 | PERROR("ioctl start session"); |
8c0faa1d DG |
410 | goto error; |
411 | } | |
412 | ||
413 | DBG("Kernel session started"); | |
414 | ||
415 | return 0; | |
416 | ||
417 | error: | |
418 | return ret; | |
419 | } | |
420 | ||
f3ed775e | 421 | /* |
050349bb | 422 | * Make a kernel wait to make sure in-flight probe have completed. |
f3ed775e DG |
423 | */ |
424 | void kernel_wait_quiescent(int fd) | |
425 | { | |
426 | int ret; | |
427 | ||
428 | DBG("Kernel quiescent wait on %d", fd); | |
429 | ||
430 | ret = kernctl_wait_quiescent(fd); | |
431 | if (ret < 0) { | |
df0f840b | 432 | PERROR("wait quiescent ioctl"); |
f3ed775e DG |
433 | ERR("Kernel quiescent wait failed"); |
434 | } | |
435 | } | |
436 | ||
d0254c7c | 437 | /* |
050349bb | 438 | * Kernel calibrate |
d0254c7c MD |
439 | */ |
440 | int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate) | |
441 | { | |
442 | int ret; | |
443 | ||
0525e9ae DG |
444 | assert(calibrate); |
445 | ||
d0254c7c MD |
446 | ret = kernctl_calibrate(fd, calibrate); |
447 | if (ret < 0) { | |
df0f840b | 448 | PERROR("calibrate ioctl"); |
d0254c7c MD |
449 | return -1; |
450 | } | |
451 | ||
452 | return 0; | |
453 | } | |
454 | ||
455 | ||
f3ed775e | 456 | /* |
f3ed775e DG |
457 | * Force flush buffer of metadata. |
458 | */ | |
459 | int kernel_metadata_flush_buffer(int fd) | |
460 | { | |
461 | int ret; | |
462 | ||
169d2cb7 DG |
463 | DBG("Kernel flushing metadata buffer on fd %d", fd); |
464 | ||
f3ed775e DG |
465 | ret = kernctl_buffer_flush(fd); |
466 | if (ret < 0) { | |
00e2e675 | 467 | ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret); |
f3ed775e DG |
468 | } |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
473 | /* | |
050349bb | 474 | * Force flush buffer for channel. |
f3ed775e DG |
475 | */ |
476 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) | |
477 | { | |
478 | int ret; | |
479 | struct ltt_kernel_stream *stream; | |
480 | ||
0525e9ae DG |
481 | assert(channel); |
482 | ||
f3ed775e DG |
483 | DBG("Flush buffer for channel %s", channel->channel->name); |
484 | ||
485 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
486 | DBG("Flushing channel stream %d", stream->fd); | |
487 | ret = kernctl_buffer_flush(stream->fd); | |
488 | if (ret < 0) { | |
df0f840b | 489 | PERROR("ioctl"); |
f3ed775e DG |
490 | ERR("Fail to flush buffer for stream %d (ret: %d)", |
491 | stream->fd, ret); | |
492 | } | |
493 | } | |
494 | ||
495 | return 0; | |
496 | } | |
497 | ||
8c0faa1d | 498 | /* |
050349bb | 499 | * Stop tracing session. |
8c0faa1d DG |
500 | */ |
501 | int kernel_stop_session(struct ltt_kernel_session *session) | |
502 | { | |
503 | int ret; | |
504 | ||
0525e9ae DG |
505 | assert(session); |
506 | ||
8c0faa1d DG |
507 | ret = kernctl_stop_session(session->fd); |
508 | if (ret < 0) { | |
509 | goto error; | |
510 | } | |
511 | ||
512 | DBG("Kernel session stopped"); | |
513 | ||
514 | return 0; | |
515 | ||
516 | error: | |
517 | return ret; | |
518 | } | |
519 | ||
520 | /* | |
050349bb DG |
521 | * Open stream of channel, register it to the kernel tracer and add it |
522 | * to the stream list of the channel. | |
8c0faa1d | 523 | * |
050349bb | 524 | * Return the number of created stream. Else, a negative value. |
8c0faa1d | 525 | */ |
f3ed775e | 526 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
8c0faa1d | 527 | { |
00e2e675 | 528 | int ret, count = 0; |
8c0faa1d DG |
529 | struct ltt_kernel_stream *lks; |
530 | ||
0525e9ae DG |
531 | assert(channel); |
532 | ||
5a47c6a2 | 533 | while ((ret = kernctl_create_stream(channel->fd)) >= 0) { |
00e2e675 | 534 | lks = trace_kernel_create_stream(channel->channel->name, count); |
8c0faa1d | 535 | if (lks == NULL) { |
799e2c4f MD |
536 | ret = close(ret); |
537 | if (ret) { | |
538 | PERROR("close"); | |
539 | } | |
8c0faa1d DG |
540 | goto error; |
541 | } | |
542 | ||
543 | lks->fd = ret; | |
7b395890 DG |
544 | /* Prevent fd duplication after execlp() */ |
545 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
546 | if (ret < 0) { | |
df0f840b | 547 | PERROR("fcntl session fd"); |
7b395890 DG |
548 | } |
549 | ||
1624d5b7 JD |
550 | lks->tracefile_size = channel->channel->attr.tracefile_size; |
551 | lks->tracefile_count = channel->channel->attr.tracefile_count; | |
552 | ||
54012638 | 553 | /* Add stream to channe stream list */ |
8c0faa1d DG |
554 | cds_list_add(&lks->list, &channel->stream_list.head); |
555 | channel->stream_count++; | |
8c0faa1d | 556 | |
00e2e675 DG |
557 | /* Increment counter which represent CPU number. */ |
558 | count++; | |
559 | ||
560 | DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd, | |
561 | lks->state); | |
54012638 | 562 | } |
8c0faa1d DG |
563 | |
564 | return channel->stream_count; | |
565 | ||
566 | error: | |
54012638 | 567 | return -1; |
8c0faa1d DG |
568 | } |
569 | ||
570 | /* | |
050349bb | 571 | * Open the metadata stream and set it to the kernel session. |
8c0faa1d | 572 | */ |
f3ed775e | 573 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
8c0faa1d DG |
574 | { |
575 | int ret; | |
576 | ||
0525e9ae DG |
577 | assert(session); |
578 | ||
8c0faa1d DG |
579 | ret = kernctl_create_stream(session->metadata->fd); |
580 | if (ret < 0) { | |
df0f840b | 581 | PERROR("kernel create metadata stream"); |
8c0faa1d DG |
582 | goto error; |
583 | } | |
584 | ||
585 | DBG("Kernel metadata stream created (fd: %d)", ret); | |
586 | session->metadata_stream_fd = ret; | |
7b395890 DG |
587 | /* Prevent fd duplication after execlp() */ |
588 | ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); | |
589 | if (ret < 0) { | |
df0f840b | 590 | PERROR("fcntl session fd"); |
7b395890 | 591 | } |
aaf26714 DG |
592 | |
593 | return 0; | |
594 | ||
595 | error: | |
54012638 | 596 | return -1; |
aaf26714 | 597 | } |
2ef84c95 DG |
598 | |
599 | /* | |
9f19cc17 | 600 | * Get the event list from the kernel tracer and return the number of elements. |
2ef84c95 | 601 | */ |
9f19cc17 | 602 | ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) |
2ef84c95 | 603 | { |
799e2c4f | 604 | int fd, pos, ret; |
9f19cc17 DG |
605 | char *event; |
606 | size_t nbmem, count = 0; | |
2ef84c95 | 607 | FILE *fp; |
9f19cc17 | 608 | struct lttng_event *elist; |
2ef84c95 | 609 | |
0525e9ae DG |
610 | assert(events); |
611 | ||
2ef84c95 DG |
612 | fd = kernctl_tracepoint_list(tracer_fd); |
613 | if (fd < 0) { | |
df0f840b | 614 | PERROR("kernel tracepoint list"); |
2ef84c95 DG |
615 | goto error; |
616 | } | |
617 | ||
618 | fp = fdopen(fd, "r"); | |
619 | if (fp == NULL) { | |
df0f840b | 620 | PERROR("kernel tracepoint list fdopen"); |
61b73b12 | 621 | goto error_fp; |
2ef84c95 DG |
622 | } |
623 | ||
624 | /* | |
625 | * Init memory size counter | |
626 | * See kernel-ctl.h for explanation of this value | |
627 | */ | |
6725fe19 | 628 | nbmem = KERNEL_EVENT_INIT_LIST_SIZE; |
ba7f0ae5 | 629 | elist = zmalloc(sizeof(struct lttng_event) * nbmem); |
3b870559 MD |
630 | if (elist == NULL) { |
631 | PERROR("alloc list events"); | |
632 | count = -ENOMEM; | |
633 | goto end; | |
634 | } | |
2ef84c95 | 635 | |
c617c0c6 | 636 | while (fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos) == 1) { |
6725fe19 | 637 | if (count >= nbmem) { |
3b870559 MD |
638 | struct lttng_event *new_elist; |
639 | ||
ced2f820 | 640 | DBG("Reallocating event list from %zu to %zu bytes", nbmem, |
6725fe19 DG |
641 | nbmem * 2); |
642 | /* Double the size */ | |
643 | nbmem <<= 1; | |
3b870559 MD |
644 | new_elist = realloc(elist, nbmem * sizeof(struct lttng_event)); |
645 | if (new_elist == NULL) { | |
df0f840b | 646 | PERROR("realloc list events"); |
3b870559 MD |
647 | free(event); |
648 | free(elist); | |
61b73b12 MD |
649 | count = -ENOMEM; |
650 | goto end; | |
2ef84c95 | 651 | } |
3b870559 | 652 | elist = new_elist; |
2ef84c95 | 653 | } |
99497cd0 MD |
654 | strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN); |
655 | elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
67b9d018 | 656 | elist[count].enabled = -1; |
9f19cc17 | 657 | count++; |
3b870559 | 658 | free(event); |
2ef84c95 DG |
659 | } |
660 | ||
9f19cc17 | 661 | *events = elist; |
ced2f820 | 662 | DBG("Kernel list events done (%zu events)", count); |
61b73b12 | 663 | end: |
799e2c4f MD |
664 | ret = fclose(fp); /* closes both fp and fd */ |
665 | if (ret) { | |
666 | PERROR("fclose"); | |
667 | } | |
9f19cc17 | 668 | return count; |
2ef84c95 | 669 | |
61b73b12 | 670 | error_fp: |
799e2c4f MD |
671 | ret = close(fd); |
672 | if (ret) { | |
673 | PERROR("close"); | |
674 | } | |
2ef84c95 DG |
675 | error: |
676 | return -1; | |
677 | } | |
096102bd DG |
678 | |
679 | /* | |
680 | * Get kernel version and validate it. | |
681 | */ | |
682 | int kernel_validate_version(int tracer_fd) | |
683 | { | |
684 | int ret; | |
685 | struct lttng_kernel_tracer_version version; | |
686 | ||
687 | ret = kernctl_tracer_version(tracer_fd, &version); | |
688 | if (ret < 0) { | |
689 | ERR("Failed at getting the lttng-modules version"); | |
690 | goto error; | |
691 | } | |
692 | ||
693 | /* Validate version */ | |
a62a6556 MD |
694 | if (version.major != KERN_MODULES_PRE_MAJOR |
695 | && version.major != KERN_MODULES_MAJOR) { | |
096102bd | 696 | goto error_version; |
096102bd DG |
697 | } |
698 | ||
a62a6556 | 699 | DBG2("Kernel tracer version validated (major version %d)", version.major); |
096102bd DG |
700 | return 0; |
701 | ||
702 | error_version: | |
5df0f285 | 703 | ERR("Kernel major version %d is not compatible (supporting <= %d)", |
a62a6556 | 704 | version.major, KERN_MODULES_MAJOR) |
096102bd DG |
705 | ret = -1; |
706 | ||
707 | error: | |
708 | return ret; | |
709 | } | |
335a95b7 MD |
710 | |
711 | /* | |
712 | * Kernel work-arounds called at the start of sessiond main(). | |
713 | */ | |
714 | int init_kernel_workarounds(void) | |
715 | { | |
8936c33a | 716 | int ret; |
335a95b7 MD |
717 | FILE *fp; |
718 | ||
719 | /* | |
720 | * boot_id needs to be read once before being used concurrently | |
721 | * to deal with a Linux kernel race. A fix is proposed for | |
722 | * upstream, but the work-around is needed for older kernels. | |
723 | */ | |
724 | fp = fopen("/proc/sys/kernel/random/boot_id", "r"); | |
725 | if (!fp) { | |
726 | goto end_boot_id; | |
727 | } | |
728 | while (!feof(fp)) { | |
729 | char buf[37] = ""; | |
730 | ||
8936c33a DG |
731 | ret = fread(buf, 1, sizeof(buf), fp); |
732 | if (ret < 0) { | |
733 | /* Ignore error, we don't really care */ | |
734 | } | |
335a95b7 | 735 | } |
799e2c4f MD |
736 | ret = fclose(fp); |
737 | if (ret) { | |
738 | PERROR("fclose"); | |
739 | } | |
335a95b7 | 740 | end_boot_id: |
335a95b7 MD |
741 | return 0; |
742 | } | |
2f77fc4b DG |
743 | |
744 | /* | |
745 | * Complete teardown of a kernel session. | |
746 | */ | |
747 | void kernel_destroy_session(struct ltt_kernel_session *ksess) | |
748 | { | |
749 | if (ksess == NULL) { | |
750 | DBG3("No kernel session when tearing down session"); | |
751 | return; | |
752 | } | |
753 | ||
754 | DBG("Tearing down kernel session"); | |
755 | ||
756 | /* Close any relayd session */ | |
757 | consumer_output_send_destroy_relayd(ksess->consumer); | |
758 | ||
759 | trace_kernel_destroy_session(ksess); | |
760 | } | |
fb5f35b6 DG |
761 | |
762 | /* | |
763 | * Destroy a kernel channel object. It does not do anything on the tracer side. | |
764 | */ | |
765 | void kernel_destroy_channel(struct ltt_kernel_channel *kchan) | |
766 | { | |
767 | struct ltt_kernel_session *ksess = NULL; | |
768 | ||
769 | assert(kchan); | |
770 | assert(kchan->channel); | |
771 | ||
772 | DBG3("Kernel destroy channel %s", kchan->channel->name); | |
773 | ||
774 | /* Update channel count of associated session. */ | |
775 | if (kchan->session) { | |
776 | /* Keep pointer reference so we can update it after the destroy. */ | |
777 | ksess = kchan->session; | |
778 | } | |
779 | ||
780 | trace_kernel_destroy_channel(kchan); | |
781 | ||
782 | /* | |
783 | * At this point the kernel channel is not visible anymore. This is safe | |
784 | * since in order to work on a visible kernel session, the tracing session | |
785 | * lock (ltt_session.lock) MUST be acquired. | |
786 | */ | |
787 | if (ksess) { | |
788 | ksess->channel_count--; | |
789 | } | |
790 | } |