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: | |
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 | ||
fdd9eb17 DG |
135 | DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d", |
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, | |
139 | lkc->channel->attr.output); | |
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: | |
54012638 | 166 | return -1; |
20fe2104 | 167 | } |
f34daff7 DG |
168 | |
169 | /* | |
050349bb DG |
170 | * Create a kernel event, enable it to the kernel tracer and add it to the |
171 | * channel event list of the kernel session. | |
f34daff7 | 172 | */ |
050349bb DG |
173 | int kernel_create_event(struct lttng_event *ev, |
174 | struct ltt_kernel_channel *channel) | |
f34daff7 DG |
175 | { |
176 | int ret; | |
177 | struct ltt_kernel_event *event; | |
f34daff7 | 178 | |
0525e9ae DG |
179 | assert(ev); |
180 | assert(channel); | |
181 | ||
62499ad6 | 182 | event = trace_kernel_create_event(ev); |
54012638 | 183 | if (event == NULL) { |
d87bfb32 | 184 | ret = -1; |
f34daff7 DG |
185 | goto error; |
186 | } | |
187 | ||
f3ed775e DG |
188 | ret = kernctl_create_event(channel->fd, event->event); |
189 | if (ret < 0) { | |
bd29c13d DG |
190 | switch (errno) { |
191 | case EEXIST: | |
192 | break; | |
193 | case ENOSYS: | |
194 | WARN("Event type not implemented"); | |
195 | break; | |
196 | default: | |
d87bfb32 DG |
197 | PERROR("create event ioctl"); |
198 | } | |
199 | ret = -errno; | |
e953ef25 | 200 | goto free_event; |
8c0faa1d | 201 | } |
f34daff7 | 202 | |
5f822d0a | 203 | /* |
2c425ff7 | 204 | * LTTNG_KERNEL_SYSCALL event creation will return 0 on success. |
5f822d0a DG |
205 | */ |
206 | if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) { | |
207 | DBG2("Kernel event syscall creation success"); | |
87eb4ab8 MD |
208 | /* |
209 | * We use fd == -1 to ensure that we never trigger a close of fd | |
210 | * 0. | |
211 | */ | |
212 | event->fd = -1; | |
2c425ff7 | 213 | goto add_list; |
5f822d0a DG |
214 | } |
215 | ||
f3ed775e | 216 | event->fd = ret; |
7b395890 DG |
217 | /* Prevent fd duplication after execlp() */ |
218 | ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC); | |
219 | if (ret < 0) { | |
df0f840b | 220 | PERROR("fcntl session fd"); |
7b395890 DG |
221 | } |
222 | ||
2c425ff7 | 223 | add_list: |
f3ed775e DG |
224 | /* Add event to event list */ |
225 | cds_list_add(&event->list, &channel->events_list.head); | |
cbbbb275 DG |
226 | channel->event_count++; |
227 | ||
e953ef25 DG |
228 | DBG("Event %s created (fd: %d)", ev->name, event->fd); |
229 | ||
230 | return 0; | |
231 | ||
232 | free_event: | |
233 | free(event); | |
234 | error: | |
d87bfb32 | 235 | return ret; |
e953ef25 DG |
236 | } |
237 | ||
26cc6b4e | 238 | /* |
050349bb | 239 | * Disable a kernel channel. |
26cc6b4e DG |
240 | */ |
241 | int kernel_disable_channel(struct ltt_kernel_channel *chan) | |
242 | { | |
243 | int ret; | |
244 | ||
0525e9ae DG |
245 | assert(chan); |
246 | ||
26cc6b4e DG |
247 | ret = kernctl_disable(chan->fd); |
248 | if (ret < 0) { | |
df0f840b | 249 | PERROR("disable chan ioctl"); |
26cc6b4e DG |
250 | ret = errno; |
251 | goto error; | |
252 | } | |
253 | ||
254 | chan->enabled = 0; | |
255 | DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd); | |
256 | ||
257 | return 0; | |
258 | ||
259 | error: | |
260 | return ret; | |
261 | } | |
262 | ||
d36b8583 | 263 | /* |
050349bb | 264 | * Enable a kernel channel. |
d36b8583 DG |
265 | */ |
266 | int kernel_enable_channel(struct ltt_kernel_channel *chan) | |
267 | { | |
268 | int ret; | |
269 | ||
0525e9ae DG |
270 | assert(chan); |
271 | ||
d36b8583 | 272 | ret = kernctl_enable(chan->fd); |
54d01ffb | 273 | if (ret < 0 && errno != EEXIST) { |
df0f840b | 274 | PERROR("Enable kernel chan"); |
d36b8583 DG |
275 | goto error; |
276 | } | |
277 | ||
278 | chan->enabled = 1; | |
279 | DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd); | |
280 | ||
281 | return 0; | |
282 | ||
283 | error: | |
284 | return ret; | |
285 | } | |
286 | ||
19e70852 | 287 | /* |
050349bb | 288 | * Enable a kernel event. |
19e70852 DG |
289 | */ |
290 | int kernel_enable_event(struct ltt_kernel_event *event) | |
291 | { | |
292 | int ret; | |
293 | ||
0525e9ae DG |
294 | assert(event); |
295 | ||
19e70852 | 296 | ret = kernctl_enable(event->fd); |
42224349 DG |
297 | if (ret < 0) { |
298 | switch (errno) { | |
299 | case EEXIST: | |
f73fabfd | 300 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
42224349 DG |
301 | break; |
302 | default: | |
303 | PERROR("enable kernel event"); | |
304 | break; | |
305 | } | |
19e70852 DG |
306 | goto error; |
307 | } | |
308 | ||
309 | event->enabled = 1; | |
310 | DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd); | |
311 | ||
312 | return 0; | |
313 | ||
314 | error: | |
d36b8583 | 315 | return ret; |
19e70852 DG |
316 | } |
317 | ||
e953ef25 | 318 | /* |
050349bb | 319 | * Disable a kernel event. |
e953ef25 | 320 | */ |
19e70852 | 321 | int kernel_disable_event(struct ltt_kernel_event *event) |
e953ef25 DG |
322 | { |
323 | int ret; | |
19e70852 | 324 | |
0525e9ae DG |
325 | assert(event); |
326 | ||
19e70852 | 327 | ret = kernctl_disable(event->fd); |
42224349 DG |
328 | if (ret < 0) { |
329 | switch (errno) { | |
330 | case EEXIST: | |
f73fabfd | 331 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
42224349 DG |
332 | break; |
333 | default: | |
334 | PERROR("disable kernel event"); | |
335 | break; | |
336 | } | |
19e70852 | 337 | goto error; |
e953ef25 | 338 | } |
f3ed775e | 339 | |
19e70852 DG |
340 | event->enabled = 0; |
341 | DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd); | |
342 | ||
f34daff7 DG |
343 | return 0; |
344 | ||
345 | error: | |
d36b8583 | 346 | return ret; |
f34daff7 | 347 | } |
aaf26714 DG |
348 | |
349 | /* | |
050349bb DG |
350 | * Create kernel metadata, open from the kernel tracer and add it to the |
351 | * kernel session. | |
aaf26714 | 352 | */ |
a4b92340 | 353 | int kernel_open_metadata(struct ltt_kernel_session *session) |
aaf26714 DG |
354 | { |
355 | int ret; | |
74024a21 | 356 | struct ltt_kernel_metadata *lkm = NULL; |
aaf26714 | 357 | |
0525e9ae DG |
358 | assert(session); |
359 | ||
54012638 | 360 | /* Allocate kernel metadata */ |
a4b92340 | 361 | lkm = trace_kernel_create_metadata(); |
54012638 | 362 | if (lkm == NULL) { |
aaf26714 DG |
363 | goto error; |
364 | } | |
365 | ||
54012638 | 366 | /* Kernel tracer metadata creation */ |
f3ed775e | 367 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
aaf26714 | 368 | if (ret < 0) { |
74024a21 | 369 | goto error_open; |
aaf26714 DG |
370 | } |
371 | ||
8c0faa1d | 372 | lkm->fd = ret; |
7b395890 DG |
373 | /* Prevent fd duplication after execlp() */ |
374 | ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); | |
375 | if (ret < 0) { | |
df0f840b | 376 | PERROR("fcntl session fd"); |
7b395890 DG |
377 | } |
378 | ||
aaf26714 | 379 | session->metadata = lkm; |
8c0faa1d | 380 | |
00e2e675 | 381 | DBG("Kernel metadata opened (fd: %d)", lkm->fd); |
8c0faa1d DG |
382 | |
383 | return 0; | |
384 | ||
74024a21 DG |
385 | error_open: |
386 | trace_kernel_destroy_metadata(lkm); | |
8c0faa1d | 387 | error: |
54012638 | 388 | return -1; |
8c0faa1d DG |
389 | } |
390 | ||
391 | /* | |
050349bb | 392 | * Start tracing session. |
8c0faa1d DG |
393 | */ |
394 | int kernel_start_session(struct ltt_kernel_session *session) | |
395 | { | |
396 | int ret; | |
397 | ||
0525e9ae DG |
398 | assert(session); |
399 | ||
8c0faa1d DG |
400 | ret = kernctl_start_session(session->fd); |
401 | if (ret < 0) { | |
df0f840b | 402 | PERROR("ioctl start session"); |
8c0faa1d DG |
403 | goto error; |
404 | } | |
405 | ||
406 | DBG("Kernel session started"); | |
407 | ||
408 | return 0; | |
409 | ||
410 | error: | |
411 | return ret; | |
412 | } | |
413 | ||
f3ed775e | 414 | /* |
050349bb | 415 | * Make a kernel wait to make sure in-flight probe have completed. |
f3ed775e DG |
416 | */ |
417 | void kernel_wait_quiescent(int fd) | |
418 | { | |
419 | int ret; | |
420 | ||
421 | DBG("Kernel quiescent wait on %d", fd); | |
422 | ||
423 | ret = kernctl_wait_quiescent(fd); | |
424 | if (ret < 0) { | |
df0f840b | 425 | PERROR("wait quiescent ioctl"); |
f3ed775e DG |
426 | ERR("Kernel quiescent wait failed"); |
427 | } | |
428 | } | |
429 | ||
d0254c7c | 430 | /* |
050349bb | 431 | * Kernel calibrate |
d0254c7c MD |
432 | */ |
433 | int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate) | |
434 | { | |
435 | int ret; | |
436 | ||
0525e9ae DG |
437 | assert(calibrate); |
438 | ||
d0254c7c MD |
439 | ret = kernctl_calibrate(fd, calibrate); |
440 | if (ret < 0) { | |
df0f840b | 441 | PERROR("calibrate ioctl"); |
d0254c7c MD |
442 | return -1; |
443 | } | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
448 | ||
f3ed775e | 449 | /* |
f3ed775e DG |
450 | * Force flush buffer of metadata. |
451 | */ | |
452 | int kernel_metadata_flush_buffer(int fd) | |
453 | { | |
454 | int ret; | |
455 | ||
456 | ret = kernctl_buffer_flush(fd); | |
457 | if (ret < 0) { | |
00e2e675 | 458 | ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret); |
f3ed775e DG |
459 | } |
460 | ||
461 | return 0; | |
462 | } | |
463 | ||
464 | /* | |
050349bb | 465 | * Force flush buffer for channel. |
f3ed775e DG |
466 | */ |
467 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) | |
468 | { | |
469 | int ret; | |
470 | struct ltt_kernel_stream *stream; | |
471 | ||
0525e9ae DG |
472 | assert(channel); |
473 | ||
f3ed775e DG |
474 | DBG("Flush buffer for channel %s", channel->channel->name); |
475 | ||
476 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
477 | DBG("Flushing channel stream %d", stream->fd); | |
478 | ret = kernctl_buffer_flush(stream->fd); | |
479 | if (ret < 0) { | |
df0f840b | 480 | PERROR("ioctl"); |
f3ed775e DG |
481 | ERR("Fail to flush buffer for stream %d (ret: %d)", |
482 | stream->fd, ret); | |
483 | } | |
484 | } | |
485 | ||
486 | return 0; | |
487 | } | |
488 | ||
8c0faa1d | 489 | /* |
050349bb | 490 | * Stop tracing session. |
8c0faa1d DG |
491 | */ |
492 | int kernel_stop_session(struct ltt_kernel_session *session) | |
493 | { | |
494 | int ret; | |
495 | ||
0525e9ae DG |
496 | assert(session); |
497 | ||
8c0faa1d DG |
498 | ret = kernctl_stop_session(session->fd); |
499 | if (ret < 0) { | |
500 | goto error; | |
501 | } | |
502 | ||
503 | DBG("Kernel session stopped"); | |
504 | ||
505 | return 0; | |
506 | ||
507 | error: | |
508 | return ret; | |
509 | } | |
510 | ||
511 | /* | |
050349bb DG |
512 | * Open stream of channel, register it to the kernel tracer and add it |
513 | * to the stream list of the channel. | |
8c0faa1d | 514 | * |
050349bb | 515 | * Return the number of created stream. Else, a negative value. |
8c0faa1d | 516 | */ |
f3ed775e | 517 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
8c0faa1d | 518 | { |
00e2e675 | 519 | int ret, count = 0; |
8c0faa1d DG |
520 | struct ltt_kernel_stream *lks; |
521 | ||
0525e9ae DG |
522 | assert(channel); |
523 | ||
5a47c6a2 | 524 | while ((ret = kernctl_create_stream(channel->fd)) >= 0) { |
00e2e675 | 525 | lks = trace_kernel_create_stream(channel->channel->name, count); |
8c0faa1d | 526 | if (lks == NULL) { |
799e2c4f MD |
527 | ret = close(ret); |
528 | if (ret) { | |
529 | PERROR("close"); | |
530 | } | |
8c0faa1d DG |
531 | goto error; |
532 | } | |
533 | ||
534 | lks->fd = ret; | |
7b395890 DG |
535 | /* Prevent fd duplication after execlp() */ |
536 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
537 | if (ret < 0) { | |
df0f840b | 538 | PERROR("fcntl session fd"); |
7b395890 DG |
539 | } |
540 | ||
1624d5b7 JD |
541 | lks->tracefile_size = channel->channel->attr.tracefile_size; |
542 | lks->tracefile_count = channel->channel->attr.tracefile_count; | |
543 | ||
54012638 | 544 | /* Add stream to channe stream list */ |
8c0faa1d DG |
545 | cds_list_add(&lks->list, &channel->stream_list.head); |
546 | channel->stream_count++; | |
8c0faa1d | 547 | |
00e2e675 DG |
548 | /* Increment counter which represent CPU number. */ |
549 | count++; | |
550 | ||
551 | DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd, | |
552 | lks->state); | |
54012638 | 553 | } |
8c0faa1d DG |
554 | |
555 | return channel->stream_count; | |
556 | ||
557 | error: | |
54012638 | 558 | return -1; |
8c0faa1d DG |
559 | } |
560 | ||
561 | /* | |
050349bb | 562 | * Open the metadata stream and set it to the kernel session. |
8c0faa1d | 563 | */ |
f3ed775e | 564 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
8c0faa1d DG |
565 | { |
566 | int ret; | |
567 | ||
0525e9ae DG |
568 | assert(session); |
569 | ||
8c0faa1d DG |
570 | ret = kernctl_create_stream(session->metadata->fd); |
571 | if (ret < 0) { | |
df0f840b | 572 | PERROR("kernel create metadata stream"); |
8c0faa1d DG |
573 | goto error; |
574 | } | |
575 | ||
576 | DBG("Kernel metadata stream created (fd: %d)", ret); | |
577 | session->metadata_stream_fd = ret; | |
7b395890 DG |
578 | /* Prevent fd duplication after execlp() */ |
579 | ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); | |
580 | if (ret < 0) { | |
df0f840b | 581 | PERROR("fcntl session fd"); |
7b395890 | 582 | } |
aaf26714 DG |
583 | |
584 | return 0; | |
585 | ||
586 | error: | |
54012638 | 587 | return -1; |
aaf26714 | 588 | } |
2ef84c95 DG |
589 | |
590 | /* | |
9f19cc17 | 591 | * Get the event list from the kernel tracer and return the number of elements. |
2ef84c95 | 592 | */ |
9f19cc17 | 593 | ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) |
2ef84c95 | 594 | { |
799e2c4f | 595 | int fd, pos, ret; |
9f19cc17 DG |
596 | char *event; |
597 | size_t nbmem, count = 0; | |
2ef84c95 | 598 | FILE *fp; |
9f19cc17 | 599 | struct lttng_event *elist; |
2ef84c95 | 600 | |
0525e9ae DG |
601 | assert(events); |
602 | ||
2ef84c95 DG |
603 | fd = kernctl_tracepoint_list(tracer_fd); |
604 | if (fd < 0) { | |
df0f840b | 605 | PERROR("kernel tracepoint list"); |
2ef84c95 DG |
606 | goto error; |
607 | } | |
608 | ||
609 | fp = fdopen(fd, "r"); | |
610 | if (fp == NULL) { | |
df0f840b | 611 | PERROR("kernel tracepoint list fdopen"); |
61b73b12 | 612 | goto error_fp; |
2ef84c95 DG |
613 | } |
614 | ||
615 | /* | |
616 | * Init memory size counter | |
617 | * See kernel-ctl.h for explanation of this value | |
618 | */ | |
6725fe19 | 619 | nbmem = KERNEL_EVENT_INIT_LIST_SIZE; |
ba7f0ae5 | 620 | elist = zmalloc(sizeof(struct lttng_event) * nbmem); |
3b870559 MD |
621 | if (elist == NULL) { |
622 | PERROR("alloc list events"); | |
623 | count = -ENOMEM; | |
624 | goto end; | |
625 | } | |
2ef84c95 | 626 | |
c617c0c6 | 627 | while (fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos) == 1) { |
6725fe19 | 628 | if (count >= nbmem) { |
3b870559 MD |
629 | struct lttng_event *new_elist; |
630 | ||
ced2f820 | 631 | DBG("Reallocating event list from %zu to %zu bytes", nbmem, |
6725fe19 DG |
632 | nbmem * 2); |
633 | /* Double the size */ | |
634 | nbmem <<= 1; | |
3b870559 MD |
635 | new_elist = realloc(elist, nbmem * sizeof(struct lttng_event)); |
636 | if (new_elist == NULL) { | |
df0f840b | 637 | PERROR("realloc list events"); |
3b870559 MD |
638 | free(event); |
639 | free(elist); | |
61b73b12 MD |
640 | count = -ENOMEM; |
641 | goto end; | |
2ef84c95 | 642 | } |
3b870559 | 643 | elist = new_elist; |
2ef84c95 | 644 | } |
99497cd0 MD |
645 | strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN); |
646 | elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
67b9d018 | 647 | elist[count].enabled = -1; |
9f19cc17 | 648 | count++; |
3b870559 | 649 | free(event); |
2ef84c95 DG |
650 | } |
651 | ||
9f19cc17 | 652 | *events = elist; |
ced2f820 | 653 | DBG("Kernel list events done (%zu events)", count); |
61b73b12 | 654 | end: |
799e2c4f MD |
655 | ret = fclose(fp); /* closes both fp and fd */ |
656 | if (ret) { | |
657 | PERROR("fclose"); | |
658 | } | |
9f19cc17 | 659 | return count; |
2ef84c95 | 660 | |
61b73b12 | 661 | error_fp: |
799e2c4f MD |
662 | ret = close(fd); |
663 | if (ret) { | |
664 | PERROR("close"); | |
665 | } | |
2ef84c95 DG |
666 | error: |
667 | return -1; | |
668 | } | |
096102bd DG |
669 | |
670 | /* | |
671 | * Get kernel version and validate it. | |
672 | */ | |
673 | int kernel_validate_version(int tracer_fd) | |
674 | { | |
675 | int ret; | |
676 | struct lttng_kernel_tracer_version version; | |
677 | ||
678 | ret = kernctl_tracer_version(tracer_fd, &version); | |
679 | if (ret < 0) { | |
680 | ERR("Failed at getting the lttng-modules version"); | |
681 | goto error; | |
682 | } | |
683 | ||
684 | /* Validate version */ | |
a62a6556 MD |
685 | if (version.major != KERN_MODULES_PRE_MAJOR |
686 | && version.major != KERN_MODULES_MAJOR) { | |
096102bd | 687 | goto error_version; |
096102bd DG |
688 | } |
689 | ||
a62a6556 | 690 | DBG2("Kernel tracer version validated (major version %d)", version.major); |
096102bd DG |
691 | return 0; |
692 | ||
693 | error_version: | |
5df0f285 | 694 | ERR("Kernel major version %d is not compatible (supporting <= %d)", |
a62a6556 | 695 | version.major, KERN_MODULES_MAJOR) |
096102bd DG |
696 | ret = -1; |
697 | ||
698 | error: | |
699 | return ret; | |
700 | } | |
335a95b7 MD |
701 | |
702 | /* | |
703 | * Kernel work-arounds called at the start of sessiond main(). | |
704 | */ | |
705 | int init_kernel_workarounds(void) | |
706 | { | |
8936c33a | 707 | int ret; |
335a95b7 MD |
708 | FILE *fp; |
709 | ||
710 | /* | |
711 | * boot_id needs to be read once before being used concurrently | |
712 | * to deal with a Linux kernel race. A fix is proposed for | |
713 | * upstream, but the work-around is needed for older kernels. | |
714 | */ | |
715 | fp = fopen("/proc/sys/kernel/random/boot_id", "r"); | |
716 | if (!fp) { | |
717 | goto end_boot_id; | |
718 | } | |
719 | while (!feof(fp)) { | |
720 | char buf[37] = ""; | |
721 | ||
8936c33a DG |
722 | ret = fread(buf, 1, sizeof(buf), fp); |
723 | if (ret < 0) { | |
724 | /* Ignore error, we don't really care */ | |
725 | } | |
335a95b7 | 726 | } |
799e2c4f MD |
727 | ret = fclose(fp); |
728 | if (ret) { | |
729 | PERROR("fclose"); | |
730 | } | |
335a95b7 | 731 | end_boot_id: |
335a95b7 MD |
732 | return 0; |
733 | } | |
2f77fc4b DG |
734 | |
735 | /* | |
736 | * Complete teardown of a kernel session. | |
737 | */ | |
738 | void kernel_destroy_session(struct ltt_kernel_session *ksess) | |
739 | { | |
740 | if (ksess == NULL) { | |
741 | DBG3("No kernel session when tearing down session"); | |
742 | return; | |
743 | } | |
744 | ||
745 | DBG("Tearing down kernel session"); | |
746 | ||
747 | /* Close any relayd session */ | |
748 | consumer_output_send_destroy_relayd(ksess->consumer); | |
749 | ||
750 | trace_kernel_destroy_session(ksess); | |
751 | } | |
fb5f35b6 DG |
752 | |
753 | /* | |
754 | * Destroy a kernel channel object. It does not do anything on the tracer side. | |
755 | */ | |
756 | void kernel_destroy_channel(struct ltt_kernel_channel *kchan) | |
757 | { | |
758 | struct ltt_kernel_session *ksess = NULL; | |
759 | ||
760 | assert(kchan); | |
761 | assert(kchan->channel); | |
762 | ||
763 | DBG3("Kernel destroy channel %s", kchan->channel->name); | |
764 | ||
765 | /* Update channel count of associated session. */ | |
766 | if (kchan->session) { | |
767 | /* Keep pointer reference so we can update it after the destroy. */ | |
768 | ksess = kchan->session; | |
769 | } | |
770 | ||
771 | trace_kernel_destroy_channel(kchan); | |
772 | ||
773 | /* | |
774 | * At this point the kernel channel is not visible anymore. This is safe | |
775 | * since in order to work on a visible kernel session, the tracing session | |
776 | * lock (ltt_session.lock) MUST be acquired. | |
777 | */ | |
778 | if (ksess) { | |
779 | ksess->channel_count--; | |
780 | } | |
781 | } |