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