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