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