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