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