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