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