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 |
6c1c0768 | 19 | #define _LGPL_SOURCE |
20fe2104 | 20 | #include <errno.h> |
7b395890 | 21 | #include <fcntl.h> |
20fe2104 DG |
22 | #include <stdlib.h> |
23 | #include <stdio.h> | |
f34daff7 | 24 | #include <string.h> |
8c0faa1d | 25 | #include <unistd.h> |
77c7c900 | 26 | #include <inttypes.h> |
20fe2104 | 27 | |
990570ed | 28 | #include <common/common.h> |
db758600 | 29 | #include <common/kernel-ctl/kernel-ctl.h> |
c052142c | 30 | #include <common/kernel-ctl/kernel-ioctl.h> |
42224349 | 31 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 32 | |
2f77fc4b | 33 | #include "consumer.h" |
4771f025 | 34 | #include "kernel.h" |
6dc3064a | 35 | #include "kernel-consumer.h" |
096102bd | 36 | #include "kern-modules.h" |
834978fd | 37 | #include "utils.h" |
20fe2104 | 38 | |
d65106b1 | 39 | /* |
050349bb | 40 | * Add context on a kernel channel. |
d65106b1 DG |
41 | */ |
42 | int kernel_add_channel_context(struct ltt_kernel_channel *chan, | |
645328ae | 43 | struct ltt_kernel_context *ctx) |
d65106b1 DG |
44 | { |
45 | int ret; | |
46 | ||
0525e9ae DG |
47 | assert(chan); |
48 | assert(ctx); | |
49 | ||
d65106b1 | 50 | DBG("Adding context to channel %s", chan->channel->name); |
645328ae | 51 | ret = kernctl_add_context(chan->fd, &ctx->ctx); |
d65106b1 | 52 | if (ret < 0) { |
b579acd9 | 53 | if (errno != EEXIST) { |
df0f840b | 54 | PERROR("add context ioctl"); |
b579acd9 DG |
55 | } else { |
56 | /* If EEXIST, we just ignore the error */ | |
57 | ret = 0; | |
58 | } | |
d65106b1 DG |
59 | goto error; |
60 | } | |
61 | ||
645328ae | 62 | cds_list_add_tail(&ctx->list, &chan->ctx_list); |
d65106b1 DG |
63 | |
64 | return 0; | |
65 | ||
66 | error: | |
67 | return ret; | |
68 | } | |
69 | ||
20fe2104 | 70 | /* |
050349bb DG |
71 | * Create a new kernel session, register it to the kernel tracer and add it to |
72 | * the session daemon session. | |
20fe2104 | 73 | */ |
8c0faa1d | 74 | int kernel_create_session(struct ltt_session *session, int tracer_fd) |
20fe2104 DG |
75 | { |
76 | int ret; | |
77 | struct ltt_kernel_session *lks; | |
78 | ||
0525e9ae DG |
79 | assert(session); |
80 | ||
54012638 | 81 | /* Allocate data structure */ |
dec56f6c | 82 | lks = trace_kernel_create_session(); |
20fe2104 | 83 | if (lks == NULL) { |
54012638 | 84 | ret = -1; |
20fe2104 DG |
85 | goto error; |
86 | } | |
87 | ||
54012638 | 88 | /* Kernel tracer session creation */ |
20fe2104 DG |
89 | ret = kernctl_create_session(tracer_fd); |
90 | if (ret < 0) { | |
df0f840b | 91 | PERROR("ioctl kernel create session"); |
20fe2104 DG |
92 | goto error; |
93 | } | |
94 | ||
20fe2104 | 95 | lks->fd = ret; |
7b395890 DG |
96 | /* Prevent fd duplication after execlp() */ |
97 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
98 | if (ret < 0) { | |
df0f840b | 99 | PERROR("fcntl session fd"); |
7b395890 DG |
100 | } |
101 | ||
53632229 | 102 | lks->id = session->id; |
3bd1e081 | 103 | lks->consumer_fds_sent = 0; |
8c0faa1d | 104 | session->kernel_session = lks; |
8c0faa1d DG |
105 | |
106 | DBG("Kernel session created (fd: %d)", lks->fd); | |
20fe2104 DG |
107 | |
108 | return 0; | |
109 | ||
110 | error: | |
5f62c685 DG |
111 | if (lks) { |
112 | trace_kernel_destroy_session(lks); | |
113 | } | |
20fe2104 DG |
114 | return ret; |
115 | } | |
116 | ||
117 | /* | |
050349bb DG |
118 | * Create a kernel channel, register it to the kernel tracer and add it to the |
119 | * kernel session. | |
20fe2104 | 120 | */ |
050349bb | 121 | int kernel_create_channel(struct ltt_kernel_session *session, |
fdd9eb17 | 122 | struct lttng_channel *chan) |
20fe2104 DG |
123 | { |
124 | int ret; | |
125 | struct ltt_kernel_channel *lkc; | |
20fe2104 | 126 | |
0525e9ae DG |
127 | assert(session); |
128 | assert(chan); | |
0525e9ae | 129 | |
54012638 | 130 | /* Allocate kernel channel */ |
fdd9eb17 | 131 | lkc = trace_kernel_create_channel(chan); |
54012638 | 132 | if (lkc == NULL) { |
20fe2104 DG |
133 | goto error; |
134 | } | |
135 | ||
ecc48a90 | 136 | DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d", |
fdd9eb17 | 137 | chan->name, lkc->channel->attr.overwrite, |
173af62f DG |
138 | lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf, |
139 | lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval, | |
ecc48a90 | 140 | lkc->channel->attr.live_timer_interval, lkc->channel->attr.output); |
173af62f | 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: | |
5f62c685 DG |
167 | if (lkc) { |
168 | free(lkc->channel); | |
169 | free(lkc); | |
170 | } | |
54012638 | 171 | return -1; |
20fe2104 | 172 | } |
f34daff7 DG |
173 | |
174 | /* | |
050349bb DG |
175 | * Create a kernel event, enable it to the kernel tracer and add it to the |
176 | * channel event list of the kernel session. | |
49d21f93 | 177 | * We own filter_expression and filter. |
f34daff7 | 178 | */ |
050349bb DG |
179 | int kernel_create_event(struct lttng_event *ev, |
180 | struct ltt_kernel_channel *channel) | |
f34daff7 DG |
181 | { |
182 | int ret; | |
183 | struct ltt_kernel_event *event; | |
f34daff7 | 184 | |
0525e9ae DG |
185 | assert(ev); |
186 | assert(channel); | |
187 | ||
62499ad6 | 188 | event = trace_kernel_create_event(ev); |
54012638 | 189 | if (event == NULL) { |
d87bfb32 | 190 | ret = -1; |
f34daff7 DG |
191 | goto error; |
192 | } | |
193 | ||
f3ed775e DG |
194 | ret = kernctl_create_event(channel->fd, event->event); |
195 | if (ret < 0) { | |
bd29c13d DG |
196 | switch (errno) { |
197 | case EEXIST: | |
198 | break; | |
199 | case ENOSYS: | |
200 | WARN("Event type not implemented"); | |
201 | break; | |
8197a339 DG |
202 | case ENOENT: |
203 | WARN("Event %s not found!", ev->name); | |
204 | break; | |
bd29c13d | 205 | default: |
d87bfb32 DG |
206 | PERROR("create event ioctl"); |
207 | } | |
208 | ret = -errno; | |
e953ef25 | 209 | goto free_event; |
8c0faa1d | 210 | } |
f34daff7 | 211 | |
d0ae4ea8 | 212 | event->type = ev->type; |
f3ed775e | 213 | event->fd = ret; |
7b395890 DG |
214 | /* Prevent fd duplication after execlp() */ |
215 | ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC); | |
216 | if (ret < 0) { | |
df0f840b | 217 | PERROR("fcntl session fd"); |
7b395890 DG |
218 | } |
219 | ||
f3ed775e DG |
220 | /* Add event to event list */ |
221 | cds_list_add(&event->list, &channel->events_list.head); | |
cbbbb275 DG |
222 | channel->event_count++; |
223 | ||
e953ef25 DG |
224 | DBG("Event %s created (fd: %d)", ev->name, event->fd); |
225 | ||
226 | return 0; | |
227 | ||
228 | free_event: | |
229 | free(event); | |
230 | error: | |
d87bfb32 | 231 | return ret; |
e953ef25 DG |
232 | } |
233 | ||
26cc6b4e | 234 | /* |
050349bb | 235 | * Disable a kernel channel. |
26cc6b4e DG |
236 | */ |
237 | int kernel_disable_channel(struct ltt_kernel_channel *chan) | |
238 | { | |
239 | int ret; | |
240 | ||
0525e9ae DG |
241 | assert(chan); |
242 | ||
26cc6b4e DG |
243 | ret = kernctl_disable(chan->fd); |
244 | if (ret < 0) { | |
df0f840b | 245 | PERROR("disable chan ioctl"); |
26cc6b4e DG |
246 | ret = errno; |
247 | goto error; | |
248 | } | |
249 | ||
250 | chan->enabled = 0; | |
251 | DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd); | |
252 | ||
253 | return 0; | |
254 | ||
255 | error: | |
256 | return ret; | |
257 | } | |
258 | ||
d36b8583 | 259 | /* |
050349bb | 260 | * Enable a kernel channel. |
d36b8583 DG |
261 | */ |
262 | int kernel_enable_channel(struct ltt_kernel_channel *chan) | |
263 | { | |
264 | int ret; | |
265 | ||
0525e9ae DG |
266 | assert(chan); |
267 | ||
d36b8583 | 268 | ret = kernctl_enable(chan->fd); |
54d01ffb | 269 | if (ret < 0 && errno != EEXIST) { |
df0f840b | 270 | PERROR("Enable kernel chan"); |
d36b8583 DG |
271 | goto error; |
272 | } | |
273 | ||
274 | chan->enabled = 1; | |
275 | DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd); | |
276 | ||
277 | return 0; | |
278 | ||
279 | error: | |
280 | return ret; | |
281 | } | |
282 | ||
19e70852 | 283 | /* |
050349bb | 284 | * Enable a kernel event. |
19e70852 DG |
285 | */ |
286 | int kernel_enable_event(struct ltt_kernel_event *event) | |
287 | { | |
288 | int ret; | |
289 | ||
0525e9ae DG |
290 | assert(event); |
291 | ||
19e70852 | 292 | ret = kernctl_enable(event->fd); |
42224349 DG |
293 | if (ret < 0) { |
294 | switch (errno) { | |
295 | case EEXIST: | |
f73fabfd | 296 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
42224349 DG |
297 | break; |
298 | default: | |
299 | PERROR("enable kernel event"); | |
300 | break; | |
301 | } | |
19e70852 DG |
302 | goto error; |
303 | } | |
304 | ||
305 | event->enabled = 1; | |
306 | DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd); | |
307 | ||
308 | return 0; | |
309 | ||
310 | error: | |
d36b8583 | 311 | return ret; |
19e70852 DG |
312 | } |
313 | ||
e953ef25 | 314 | /* |
050349bb | 315 | * Disable a kernel event. |
e953ef25 | 316 | */ |
19e70852 | 317 | int kernel_disable_event(struct ltt_kernel_event *event) |
e953ef25 DG |
318 | { |
319 | int ret; | |
19e70852 | 320 | |
0525e9ae DG |
321 | assert(event); |
322 | ||
19e70852 | 323 | ret = kernctl_disable(event->fd); |
42224349 DG |
324 | if (ret < 0) { |
325 | switch (errno) { | |
326 | case EEXIST: | |
f73fabfd | 327 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
42224349 DG |
328 | break; |
329 | default: | |
330 | PERROR("disable kernel event"); | |
331 | break; | |
332 | } | |
19e70852 | 333 | goto error; |
e953ef25 | 334 | } |
f3ed775e | 335 | |
19e70852 DG |
336 | event->enabled = 0; |
337 | DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd); | |
338 | ||
f34daff7 DG |
339 | return 0; |
340 | ||
341 | error: | |
d36b8583 | 342 | return ret; |
f34daff7 | 343 | } |
aaf26714 | 344 | |
6e911cad | 345 | |
ccf10263 MD |
346 | int kernel_track_pid(struct ltt_kernel_session *session, int pid) |
347 | { | |
348 | DBG("Kernel track PID %d for session id %" PRIu64 ".", | |
349 | pid, session->id); | |
350 | return kernctl_track_pid(session->fd, pid); | |
351 | } | |
352 | ||
353 | int kernel_untrack_pid(struct ltt_kernel_session *session, int pid) | |
354 | { | |
355 | DBG("Kernel untrack PID %d for session id %" PRIu64 ".", | |
356 | pid, session->id); | |
357 | return kernctl_untrack_pid(session->fd, pid); | |
358 | } | |
359 | ||
a5dfbb9d MD |
360 | ssize_t kernel_list_tracker_pids(struct ltt_kernel_session *session, |
361 | int **_pids) | |
362 | { | |
363 | int fd, ret; | |
364 | int pid; | |
365 | ssize_t nbmem, count = 0; | |
366 | FILE *fp; | |
367 | int *pids; | |
368 | ||
369 | fd = kernctl_list_tracker_pids(session->fd); | |
370 | if (fd < 0) { | |
371 | PERROR("kernel tracker pids list"); | |
372 | goto error; | |
373 | } | |
374 | ||
375 | fp = fdopen(fd, "r"); | |
376 | if (fp == NULL) { | |
377 | PERROR("kernel tracker pids list fdopen"); | |
378 | goto error_fp; | |
379 | } | |
380 | ||
381 | nbmem = KERNEL_TRACKER_PIDS_INIT_LIST_SIZE; | |
382 | pids = zmalloc(sizeof(*pids) * nbmem); | |
383 | if (pids == NULL) { | |
384 | PERROR("alloc list pids"); | |
385 | count = -ENOMEM; | |
386 | goto end; | |
387 | } | |
388 | ||
389 | while (fscanf(fp, "process { pid = %u; };\n", &pid) == 1) { | |
390 | if (count >= nbmem) { | |
391 | int *new_pids; | |
392 | size_t new_nbmem; | |
393 | ||
394 | new_nbmem = nbmem << 1; | |
395 | DBG("Reallocating pids list from %zu to %zu entries", | |
396 | nbmem, new_nbmem); | |
397 | new_pids = realloc(pids, new_nbmem * sizeof(*new_pids)); | |
398 | if (new_pids == NULL) { | |
399 | PERROR("realloc list events"); | |
400 | free(pids); | |
401 | count = -ENOMEM; | |
402 | goto end; | |
403 | } | |
404 | /* Zero the new memory */ | |
405 | memset(new_pids + nbmem, 0, | |
406 | (new_nbmem - nbmem) * sizeof(*new_pids)); | |
407 | nbmem = new_nbmem; | |
408 | pids = new_pids; | |
409 | } | |
410 | pids[count++] = pid; | |
411 | } | |
412 | ||
413 | *_pids = pids; | |
414 | DBG("Kernel list tracker pids done (%zd pids)", count); | |
415 | end: | |
416 | ret = fclose(fp); /* closes both fp and fd */ | |
417 | if (ret) { | |
418 | PERROR("fclose"); | |
419 | } | |
420 | return count; | |
421 | ||
422 | error_fp: | |
423 | ret = close(fd); | |
424 | if (ret) { | |
425 | PERROR("close"); | |
426 | } | |
427 | error: | |
428 | return -1; | |
429 | } | |
430 | ||
aaf26714 | 431 | /* |
050349bb DG |
432 | * Create kernel metadata, open from the kernel tracer and add it to the |
433 | * kernel session. | |
aaf26714 | 434 | */ |
a4b92340 | 435 | int kernel_open_metadata(struct ltt_kernel_session *session) |
aaf26714 DG |
436 | { |
437 | int ret; | |
74024a21 | 438 | struct ltt_kernel_metadata *lkm = NULL; |
aaf26714 | 439 | |
0525e9ae DG |
440 | assert(session); |
441 | ||
54012638 | 442 | /* Allocate kernel metadata */ |
a4b92340 | 443 | lkm = trace_kernel_create_metadata(); |
54012638 | 444 | if (lkm == NULL) { |
aaf26714 DG |
445 | goto error; |
446 | } | |
447 | ||
54012638 | 448 | /* Kernel tracer metadata creation */ |
f3ed775e | 449 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
aaf26714 | 450 | if (ret < 0) { |
74024a21 | 451 | goto error_open; |
aaf26714 DG |
452 | } |
453 | ||
8c0faa1d | 454 | lkm->fd = ret; |
7b395890 DG |
455 | /* Prevent fd duplication after execlp() */ |
456 | ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); | |
457 | if (ret < 0) { | |
df0f840b | 458 | PERROR("fcntl session fd"); |
7b395890 DG |
459 | } |
460 | ||
aaf26714 | 461 | session->metadata = lkm; |
8c0faa1d | 462 | |
00e2e675 | 463 | DBG("Kernel metadata opened (fd: %d)", lkm->fd); |
8c0faa1d DG |
464 | |
465 | return 0; | |
466 | ||
74024a21 DG |
467 | error_open: |
468 | trace_kernel_destroy_metadata(lkm); | |
8c0faa1d | 469 | error: |
54012638 | 470 | return -1; |
8c0faa1d DG |
471 | } |
472 | ||
473 | /* | |
050349bb | 474 | * Start tracing session. |
8c0faa1d DG |
475 | */ |
476 | int kernel_start_session(struct ltt_kernel_session *session) | |
477 | { | |
478 | int ret; | |
479 | ||
0525e9ae DG |
480 | assert(session); |
481 | ||
8c0faa1d DG |
482 | ret = kernctl_start_session(session->fd); |
483 | if (ret < 0) { | |
df0f840b | 484 | PERROR("ioctl start session"); |
8c0faa1d DG |
485 | goto error; |
486 | } | |
487 | ||
488 | DBG("Kernel session started"); | |
489 | ||
490 | return 0; | |
491 | ||
492 | error: | |
493 | return ret; | |
494 | } | |
495 | ||
f3ed775e | 496 | /* |
050349bb | 497 | * Make a kernel wait to make sure in-flight probe have completed. |
f3ed775e DG |
498 | */ |
499 | void kernel_wait_quiescent(int fd) | |
500 | { | |
501 | int ret; | |
502 | ||
503 | DBG("Kernel quiescent wait on %d", fd); | |
504 | ||
505 | ret = kernctl_wait_quiescent(fd); | |
506 | if (ret < 0) { | |
df0f840b | 507 | PERROR("wait quiescent ioctl"); |
f3ed775e DG |
508 | ERR("Kernel quiescent wait failed"); |
509 | } | |
510 | } | |
511 | ||
d0254c7c | 512 | /* |
050349bb | 513 | * Kernel calibrate |
d0254c7c MD |
514 | */ |
515 | int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate) | |
516 | { | |
517 | int ret; | |
518 | ||
0525e9ae DG |
519 | assert(calibrate); |
520 | ||
d0254c7c MD |
521 | ret = kernctl_calibrate(fd, calibrate); |
522 | if (ret < 0) { | |
df0f840b | 523 | PERROR("calibrate ioctl"); |
d0254c7c MD |
524 | return -1; |
525 | } | |
526 | ||
527 | return 0; | |
528 | } | |
529 | ||
530 | ||
f3ed775e | 531 | /* |
f3ed775e DG |
532 | * Force flush buffer of metadata. |
533 | */ | |
534 | int kernel_metadata_flush_buffer(int fd) | |
535 | { | |
536 | int ret; | |
537 | ||
169d2cb7 DG |
538 | DBG("Kernel flushing metadata buffer on fd %d", fd); |
539 | ||
f3ed775e DG |
540 | ret = kernctl_buffer_flush(fd); |
541 | if (ret < 0) { | |
00e2e675 | 542 | ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret); |
f3ed775e DG |
543 | } |
544 | ||
545 | return 0; | |
546 | } | |
547 | ||
548 | /* | |
050349bb | 549 | * Force flush buffer for channel. |
f3ed775e DG |
550 | */ |
551 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) | |
552 | { | |
553 | int ret; | |
554 | struct ltt_kernel_stream *stream; | |
555 | ||
0525e9ae DG |
556 | assert(channel); |
557 | ||
f3ed775e DG |
558 | DBG("Flush buffer for channel %s", channel->channel->name); |
559 | ||
560 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
561 | DBG("Flushing channel stream %d", stream->fd); | |
562 | ret = kernctl_buffer_flush(stream->fd); | |
563 | if (ret < 0) { | |
df0f840b | 564 | PERROR("ioctl"); |
f3ed775e DG |
565 | ERR("Fail to flush buffer for stream %d (ret: %d)", |
566 | stream->fd, ret); | |
567 | } | |
568 | } | |
569 | ||
570 | return 0; | |
571 | } | |
572 | ||
8c0faa1d | 573 | /* |
050349bb | 574 | * Stop tracing session. |
8c0faa1d DG |
575 | */ |
576 | int kernel_stop_session(struct ltt_kernel_session *session) | |
577 | { | |
578 | int ret; | |
579 | ||
0525e9ae DG |
580 | assert(session); |
581 | ||
8c0faa1d DG |
582 | ret = kernctl_stop_session(session->fd); |
583 | if (ret < 0) { | |
584 | goto error; | |
585 | } | |
586 | ||
587 | DBG("Kernel session stopped"); | |
588 | ||
589 | return 0; | |
590 | ||
591 | error: | |
592 | return ret; | |
593 | } | |
594 | ||
595 | /* | |
050349bb DG |
596 | * Open stream of channel, register it to the kernel tracer and add it |
597 | * to the stream list of the channel. | |
8c0faa1d | 598 | * |
050349bb | 599 | * Return the number of created stream. Else, a negative value. |
8c0faa1d | 600 | */ |
f3ed775e | 601 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
8c0faa1d | 602 | { |
00e2e675 | 603 | int ret, count = 0; |
8c0faa1d DG |
604 | struct ltt_kernel_stream *lks; |
605 | ||
0525e9ae DG |
606 | assert(channel); |
607 | ||
5a47c6a2 | 608 | while ((ret = kernctl_create_stream(channel->fd)) >= 0) { |
00e2e675 | 609 | lks = trace_kernel_create_stream(channel->channel->name, count); |
8c0faa1d | 610 | if (lks == NULL) { |
799e2c4f MD |
611 | ret = close(ret); |
612 | if (ret) { | |
613 | PERROR("close"); | |
614 | } | |
8c0faa1d DG |
615 | goto error; |
616 | } | |
617 | ||
618 | lks->fd = ret; | |
7b395890 DG |
619 | /* Prevent fd duplication after execlp() */ |
620 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
621 | if (ret < 0) { | |
df0f840b | 622 | PERROR("fcntl session fd"); |
7b395890 DG |
623 | } |
624 | ||
1624d5b7 JD |
625 | lks->tracefile_size = channel->channel->attr.tracefile_size; |
626 | lks->tracefile_count = channel->channel->attr.tracefile_count; | |
627 | ||
54012638 | 628 | /* Add stream to channe stream list */ |
8c0faa1d DG |
629 | cds_list_add(&lks->list, &channel->stream_list.head); |
630 | channel->stream_count++; | |
8c0faa1d | 631 | |
00e2e675 DG |
632 | /* Increment counter which represent CPU number. */ |
633 | count++; | |
634 | ||
635 | DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd, | |
636 | lks->state); | |
54012638 | 637 | } |
8c0faa1d DG |
638 | |
639 | return channel->stream_count; | |
640 | ||
641 | error: | |
54012638 | 642 | return -1; |
8c0faa1d DG |
643 | } |
644 | ||
645 | /* | |
050349bb | 646 | * Open the metadata stream and set it to the kernel session. |
8c0faa1d | 647 | */ |
f3ed775e | 648 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
8c0faa1d DG |
649 | { |
650 | int ret; | |
651 | ||
0525e9ae DG |
652 | assert(session); |
653 | ||
8c0faa1d DG |
654 | ret = kernctl_create_stream(session->metadata->fd); |
655 | if (ret < 0) { | |
df0f840b | 656 | PERROR("kernel create metadata stream"); |
8c0faa1d DG |
657 | goto error; |
658 | } | |
659 | ||
660 | DBG("Kernel metadata stream created (fd: %d)", ret); | |
661 | session->metadata_stream_fd = ret; | |
7b395890 DG |
662 | /* Prevent fd duplication after execlp() */ |
663 | ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); | |
664 | if (ret < 0) { | |
df0f840b | 665 | PERROR("fcntl session fd"); |
7b395890 | 666 | } |
aaf26714 DG |
667 | |
668 | return 0; | |
669 | ||
670 | error: | |
54012638 | 671 | return -1; |
aaf26714 | 672 | } |
2ef84c95 DG |
673 | |
674 | /* | |
9f19cc17 | 675 | * Get the event list from the kernel tracer and return the number of elements. |
2ef84c95 | 676 | */ |
9f19cc17 | 677 | ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) |
2ef84c95 | 678 | { |
53efb85a | 679 | int fd, ret; |
9f19cc17 DG |
680 | char *event; |
681 | size_t nbmem, count = 0; | |
2ef84c95 | 682 | FILE *fp; |
9f19cc17 | 683 | struct lttng_event *elist; |
2ef84c95 | 684 | |
0525e9ae DG |
685 | assert(events); |
686 | ||
2ef84c95 DG |
687 | fd = kernctl_tracepoint_list(tracer_fd); |
688 | if (fd < 0) { | |
df0f840b | 689 | PERROR("kernel tracepoint list"); |
2ef84c95 DG |
690 | goto error; |
691 | } | |
692 | ||
693 | fp = fdopen(fd, "r"); | |
694 | if (fp == NULL) { | |
df0f840b | 695 | PERROR("kernel tracepoint list fdopen"); |
61b73b12 | 696 | goto error_fp; |
2ef84c95 DG |
697 | } |
698 | ||
699 | /* | |
700 | * Init memory size counter | |
701 | * See kernel-ctl.h for explanation of this value | |
702 | */ | |
6725fe19 | 703 | nbmem = KERNEL_EVENT_INIT_LIST_SIZE; |
ba7f0ae5 | 704 | elist = zmalloc(sizeof(struct lttng_event) * nbmem); |
3b870559 MD |
705 | if (elist == NULL) { |
706 | PERROR("alloc list events"); | |
707 | count = -ENOMEM; | |
708 | goto end; | |
709 | } | |
2ef84c95 | 710 | |
53efb85a | 711 | while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) { |
6725fe19 | 712 | if (count >= nbmem) { |
3b870559 | 713 | struct lttng_event *new_elist; |
53efb85a | 714 | size_t new_nbmem; |
3b870559 | 715 | |
53efb85a MD |
716 | new_nbmem = nbmem << 1; |
717 | DBG("Reallocating event list from %zu to %zu bytes", | |
718 | nbmem, new_nbmem); | |
719 | new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event)); | |
3b870559 | 720 | if (new_elist == NULL) { |
df0f840b | 721 | PERROR("realloc list events"); |
3b870559 MD |
722 | free(event); |
723 | free(elist); | |
61b73b12 MD |
724 | count = -ENOMEM; |
725 | goto end; | |
2ef84c95 | 726 | } |
53efb85a MD |
727 | /* Zero the new memory */ |
728 | memset(new_elist + nbmem, 0, | |
729 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); | |
730 | nbmem = new_nbmem; | |
3b870559 | 731 | elist = new_elist; |
2ef84c95 | 732 | } |
99497cd0 MD |
733 | strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN); |
734 | elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
67b9d018 | 735 | elist[count].enabled = -1; |
9f19cc17 | 736 | count++; |
3b870559 | 737 | free(event); |
2ef84c95 DG |
738 | } |
739 | ||
9f19cc17 | 740 | *events = elist; |
ced2f820 | 741 | DBG("Kernel list events done (%zu events)", count); |
61b73b12 | 742 | end: |
799e2c4f MD |
743 | ret = fclose(fp); /* closes both fp and fd */ |
744 | if (ret) { | |
745 | PERROR("fclose"); | |
746 | } | |
9f19cc17 | 747 | return count; |
2ef84c95 | 748 | |
61b73b12 | 749 | error_fp: |
799e2c4f MD |
750 | ret = close(fd); |
751 | if (ret) { | |
752 | PERROR("close"); | |
753 | } | |
2ef84c95 DG |
754 | error: |
755 | return -1; | |
756 | } | |
096102bd DG |
757 | |
758 | /* | |
759 | * Get kernel version and validate it. | |
760 | */ | |
761 | int kernel_validate_version(int tracer_fd) | |
762 | { | |
763 | int ret; | |
764 | struct lttng_kernel_tracer_version version; | |
c052142c | 765 | struct lttng_kernel_tracer_abi_version abi_version; |
096102bd DG |
766 | |
767 | ret = kernctl_tracer_version(tracer_fd, &version); | |
768 | if (ret < 0) { | |
769 | ERR("Failed at getting the lttng-modules version"); | |
770 | goto error; | |
771 | } | |
772 | ||
773 | /* Validate version */ | |
c052142c MD |
774 | if (version.major != VERSION_MAJOR) { |
775 | ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)", | |
776 | version.major, VERSION_MAJOR); | |
096102bd | 777 | goto error_version; |
096102bd | 778 | } |
c052142c MD |
779 | ret = kernctl_tracer_abi_version(tracer_fd, &abi_version); |
780 | if (ret < 0) { | |
781 | ERR("Failed at getting lttng-modules ABI version"); | |
782 | goto error; | |
783 | } | |
784 | if (abi_version.major != LTTNG_MODULES_ABI_MAJOR_VERSION) { | |
785 | ERR("Kernel tracer ABI version (%d.%d) is not compatible with expected ABI major version (%d.*)", | |
786 | abi_version.major, abi_version.minor, | |
787 | LTTNG_MODULES_ABI_MAJOR_VERSION); | |
788 | goto error; | |
789 | } | |
790 | DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)", | |
791 | version.major, version.minor, | |
792 | abi_version.major, abi_version.minor); | |
096102bd DG |
793 | return 0; |
794 | ||
795 | error_version: | |
096102bd DG |
796 | ret = -1; |
797 | ||
798 | error: | |
799 | return ret; | |
800 | } | |
335a95b7 MD |
801 | |
802 | /* | |
803 | * Kernel work-arounds called at the start of sessiond main(). | |
804 | */ | |
805 | int init_kernel_workarounds(void) | |
806 | { | |
8936c33a | 807 | int ret; |
335a95b7 MD |
808 | FILE *fp; |
809 | ||
810 | /* | |
811 | * boot_id needs to be read once before being used concurrently | |
812 | * to deal with a Linux kernel race. A fix is proposed for | |
813 | * upstream, but the work-around is needed for older kernels. | |
814 | */ | |
815 | fp = fopen("/proc/sys/kernel/random/boot_id", "r"); | |
816 | if (!fp) { | |
817 | goto end_boot_id; | |
818 | } | |
819 | while (!feof(fp)) { | |
820 | char buf[37] = ""; | |
821 | ||
8936c33a DG |
822 | ret = fread(buf, 1, sizeof(buf), fp); |
823 | if (ret < 0) { | |
824 | /* Ignore error, we don't really care */ | |
825 | } | |
335a95b7 | 826 | } |
799e2c4f MD |
827 | ret = fclose(fp); |
828 | if (ret) { | |
829 | PERROR("fclose"); | |
830 | } | |
335a95b7 | 831 | end_boot_id: |
335a95b7 MD |
832 | return 0; |
833 | } | |
2f77fc4b DG |
834 | |
835 | /* | |
836 | * Complete teardown of a kernel session. | |
837 | */ | |
838 | void kernel_destroy_session(struct ltt_kernel_session *ksess) | |
839 | { | |
840 | if (ksess == NULL) { | |
841 | DBG3("No kernel session when tearing down session"); | |
842 | return; | |
843 | } | |
844 | ||
845 | DBG("Tearing down kernel session"); | |
846 | ||
07b86b52 | 847 | /* |
15dc512a DG |
848 | * Destroy channels on the consumer if at least one FD has been sent and we |
849 | * are in no output mode because the streams are in *no* monitor mode so we | |
850 | * have to send a command to clean them up or else they leaked. | |
07b86b52 | 851 | */ |
15dc512a | 852 | if (!ksess->output_traces && ksess->consumer_fds_sent) { |
07b86b52 JD |
853 | int ret; |
854 | struct consumer_socket *socket; | |
855 | struct lttng_ht_iter iter; | |
856 | ||
857 | /* For each consumer socket. */ | |
d069d577 | 858 | rcu_read_lock(); |
07b86b52 JD |
859 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, |
860 | socket, node.node) { | |
861 | struct ltt_kernel_channel *chan; | |
862 | ||
863 | /* For each channel, ask the consumer to destroy it. */ | |
864 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { | |
865 | ret = kernel_consumer_destroy_channel(socket, chan); | |
866 | if (ret < 0) { | |
867 | /* Consumer is probably dead. Use next socket. */ | |
868 | continue; | |
869 | } | |
870 | } | |
871 | } | |
d069d577 | 872 | rcu_read_unlock(); |
07b86b52 JD |
873 | } |
874 | ||
2f77fc4b DG |
875 | /* Close any relayd session */ |
876 | consumer_output_send_destroy_relayd(ksess->consumer); | |
877 | ||
878 | trace_kernel_destroy_session(ksess); | |
879 | } | |
fb5f35b6 DG |
880 | |
881 | /* | |
882 | * Destroy a kernel channel object. It does not do anything on the tracer side. | |
883 | */ | |
884 | void kernel_destroy_channel(struct ltt_kernel_channel *kchan) | |
885 | { | |
886 | struct ltt_kernel_session *ksess = NULL; | |
887 | ||
888 | assert(kchan); | |
889 | assert(kchan->channel); | |
890 | ||
891 | DBG3("Kernel destroy channel %s", kchan->channel->name); | |
892 | ||
893 | /* Update channel count of associated session. */ | |
894 | if (kchan->session) { | |
895 | /* Keep pointer reference so we can update it after the destroy. */ | |
896 | ksess = kchan->session; | |
897 | } | |
898 | ||
899 | trace_kernel_destroy_channel(kchan); | |
900 | ||
901 | /* | |
902 | * At this point the kernel channel is not visible anymore. This is safe | |
903 | * since in order to work on a visible kernel session, the tracing session | |
904 | * lock (ltt_session.lock) MUST be acquired. | |
905 | */ | |
906 | if (ksess) { | |
907 | ksess->channel_count--; | |
908 | } | |
909 | } | |
6dc3064a DG |
910 | |
911 | /* | |
912 | * Take a snapshot for a given kernel session. | |
913 | * | |
2a06df8d | 914 | * Return 0 on success or else return a LTTNG_ERR code. |
6dc3064a DG |
915 | */ |
916 | int kernel_snapshot_record(struct ltt_kernel_session *ksess, | |
d07ceecd MD |
917 | struct snapshot_output *output, int wait, |
918 | uint64_t nb_packets_per_stream) | |
6dc3064a | 919 | { |
2a06df8d | 920 | int err, ret, saved_metadata_fd; |
6dc3064a DG |
921 | struct consumer_socket *socket; |
922 | struct lttng_ht_iter iter; | |
923 | struct ltt_kernel_metadata *saved_metadata; | |
924 | ||
925 | assert(ksess); | |
926 | assert(ksess->consumer); | |
927 | assert(output); | |
928 | ||
929 | DBG("Kernel snapshot record started"); | |
930 | ||
931 | /* Save current metadata since the following calls will change it. */ | |
932 | saved_metadata = ksess->metadata; | |
933 | saved_metadata_fd = ksess->metadata_stream_fd; | |
934 | ||
935 | rcu_read_lock(); | |
936 | ||
937 | ret = kernel_open_metadata(ksess); | |
938 | if (ret < 0) { | |
939 | ret = LTTNG_ERR_KERN_META_FAIL; | |
940 | goto error; | |
941 | } | |
942 | ||
943 | ret = kernel_open_metadata_stream(ksess); | |
944 | if (ret < 0) { | |
945 | ret = LTTNG_ERR_KERN_META_FAIL; | |
946 | goto error_open_stream; | |
947 | } | |
948 | ||
949 | /* Send metadata to consumer and snapshot everything. */ | |
950 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
951 | socket, node.node) { | |
952 | struct consumer_output *saved_output; | |
953 | struct ltt_kernel_channel *chan; | |
6dc3064a DG |
954 | |
955 | /* | |
956 | * Temporarly switch consumer output for our snapshot output. As long | |
957 | * as the session lock is taken, this is safe. | |
958 | */ | |
959 | saved_output = ksess->consumer; | |
960 | ksess->consumer = output->consumer; | |
961 | ||
962 | pthread_mutex_lock(socket->lock); | |
963 | /* This stream must not be monitored by the consumer. */ | |
07b86b52 | 964 | ret = kernel_consumer_add_metadata(socket, ksess, 0); |
6dc3064a | 965 | pthread_mutex_unlock(socket->lock); |
07b86b52 | 966 | /* Put back the saved consumer output into the session. */ |
6dc3064a DG |
967 | ksess->consumer = saved_output; |
968 | if (ret < 0) { | |
969 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; | |
970 | goto error_consumer; | |
971 | } | |
972 | ||
973 | /* For each channel, ask the consumer to snapshot it. */ | |
974 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { | |
07b86b52 | 975 | pthread_mutex_lock(socket->lock); |
6dc3064a | 976 | ret = consumer_snapshot_channel(socket, chan->fd, output, 0, |
5c786ded JD |
977 | ksess->uid, ksess->gid, |
978 | DEFAULT_KERNEL_TRACE_DIR, wait, | |
d07ceecd | 979 | nb_packets_per_stream); |
07b86b52 | 980 | pthread_mutex_unlock(socket->lock); |
6dc3064a DG |
981 | if (ret < 0) { |
982 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; | |
2a06df8d DG |
983 | (void) kernel_consumer_destroy_metadata(socket, |
984 | ksess->metadata); | |
6dc3064a DG |
985 | goto error_consumer; |
986 | } | |
987 | } | |
988 | ||
989 | /* Snapshot metadata, */ | |
07b86b52 | 990 | pthread_mutex_lock(socket->lock); |
6dc3064a | 991 | ret = consumer_snapshot_channel(socket, ksess->metadata->fd, output, |
5c786ded | 992 | 1, ksess->uid, ksess->gid, |
d07ceecd | 993 | DEFAULT_KERNEL_TRACE_DIR, wait, 0); |
07b86b52 | 994 | pthread_mutex_unlock(socket->lock); |
6dc3064a DG |
995 | if (ret < 0) { |
996 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; | |
997 | goto error_consumer; | |
998 | } | |
07b86b52 JD |
999 | |
1000 | /* | |
1001 | * The metadata snapshot is done, ask the consumer to destroy it since | |
1002 | * it's not monitored on the consumer side. | |
1003 | */ | |
1004 | (void) kernel_consumer_destroy_metadata(socket, ksess->metadata); | |
6dc3064a DG |
1005 | } |
1006 | ||
fac41e72 DG |
1007 | ret = LTTNG_OK; |
1008 | ||
6dc3064a DG |
1009 | error_consumer: |
1010 | /* Close newly opened metadata stream. It's now on the consumer side. */ | |
2a06df8d DG |
1011 | err = close(ksess->metadata_stream_fd); |
1012 | if (err < 0) { | |
6dc3064a DG |
1013 | PERROR("close snapshot kernel"); |
1014 | } | |
1015 | ||
1016 | error_open_stream: | |
1017 | trace_kernel_destroy_metadata(ksess->metadata); | |
1018 | error: | |
1019 | /* Restore metadata state.*/ | |
1020 | ksess->metadata = saved_metadata; | |
1021 | ksess->metadata_stream_fd = saved_metadata_fd; | |
1022 | ||
1023 | rcu_read_unlock(); | |
1024 | return ret; | |
1025 | } | |
834978fd DG |
1026 | |
1027 | /* | |
1028 | * Get the syscall mask array from the kernel tracer. | |
1029 | * | |
1030 | * Return 0 on success else a negative value. In both case, syscall_mask should | |
1031 | * be freed. | |
1032 | */ | |
1033 | int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits) | |
1034 | { | |
1035 | assert(syscall_mask); | |
1036 | assert(nr_bits); | |
1037 | ||
1038 | return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits); | |
1039 | } |