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