| 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/trace-chunk.h> |
| 28 | #include <common/kernel-ctl/kernel-ctl.h> |
| 29 | #include <common/kernel-ctl/kernel-ioctl.h> |
| 30 | #include <common/sessiond-comm/sessiond-comm.h> |
| 31 | |
| 32 | #include "consumer.h" |
| 33 | #include "kernel.h" |
| 34 | #include "kernel-consumer.h" |
| 35 | #include "kern-modules.h" |
| 36 | #include "utils.h" |
| 37 | #include "rotate.h" |
| 38 | |
| 39 | /* |
| 40 | * Key used to reference a channel between the sessiond and the consumer. This |
| 41 | * is only read and updated with the session_list lock held. |
| 42 | */ |
| 43 | static uint64_t next_kernel_channel_key; |
| 44 | |
| 45 | #include <lttng/userspace-probe.h> |
| 46 | #include <lttng/userspace-probe-internal.h> |
| 47 | /* |
| 48 | * Add context on a kernel channel. |
| 49 | * |
| 50 | * Assumes the ownership of ctx. |
| 51 | */ |
| 52 | int kernel_add_channel_context(struct ltt_kernel_channel *chan, |
| 53 | struct ltt_kernel_context *ctx) |
| 54 | { |
| 55 | int ret; |
| 56 | |
| 57 | assert(chan); |
| 58 | assert(ctx); |
| 59 | |
| 60 | DBG("Adding context to channel %s", chan->channel->name); |
| 61 | ret = kernctl_add_context(chan->fd, &ctx->ctx); |
| 62 | if (ret < 0) { |
| 63 | switch (-ret) { |
| 64 | case ENOSYS: |
| 65 | /* Exists but not available for this kernel */ |
| 66 | ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE; |
| 67 | goto error; |
| 68 | case EEXIST: |
| 69 | /* If EEXIST, we just ignore the error */ |
| 70 | ret = 0; |
| 71 | goto end; |
| 72 | default: |
| 73 | PERROR("add context ioctl"); |
| 74 | ret = LTTNG_ERR_KERN_CONTEXT_FAIL; |
| 75 | goto error; |
| 76 | } |
| 77 | } |
| 78 | ret = 0; |
| 79 | |
| 80 | end: |
| 81 | cds_list_add_tail(&ctx->list, &chan->ctx_list); |
| 82 | ctx->in_list = true; |
| 83 | ctx = NULL; |
| 84 | error: |
| 85 | if (ctx) { |
| 86 | trace_kernel_destroy_context(ctx); |
| 87 | } |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Create a new kernel session, register it to the kernel tracer and add it to |
| 93 | * the session daemon session. |
| 94 | */ |
| 95 | int kernel_create_session(struct ltt_session *session, int tracer_fd) |
| 96 | { |
| 97 | int ret; |
| 98 | struct ltt_kernel_session *lks; |
| 99 | |
| 100 | assert(session); |
| 101 | |
| 102 | /* Allocate data structure */ |
| 103 | lks = trace_kernel_create_session(); |
| 104 | if (lks == NULL) { |
| 105 | ret = -1; |
| 106 | goto error; |
| 107 | } |
| 108 | |
| 109 | /* Kernel tracer session creation */ |
| 110 | ret = kernctl_create_session(tracer_fd); |
| 111 | if (ret < 0) { |
| 112 | PERROR("ioctl kernel create session"); |
| 113 | goto error; |
| 114 | } |
| 115 | |
| 116 | lks->fd = ret; |
| 117 | /* Prevent fd duplication after execlp() */ |
| 118 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); |
| 119 | if (ret < 0) { |
| 120 | PERROR("fcntl session fd"); |
| 121 | } |
| 122 | |
| 123 | lks->id = session->id; |
| 124 | lks->consumer_fds_sent = 0; |
| 125 | session->kernel_session = lks; |
| 126 | |
| 127 | DBG("Kernel session created (fd: %d)", lks->fd); |
| 128 | |
| 129 | return 0; |
| 130 | |
| 131 | error: |
| 132 | if (lks) { |
| 133 | trace_kernel_destroy_session(lks); |
| 134 | } |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Create a kernel channel, register it to the kernel tracer and add it to the |
| 140 | * kernel session. |
| 141 | */ |
| 142 | int kernel_create_channel(struct ltt_kernel_session *session, |
| 143 | struct lttng_channel *chan) |
| 144 | { |
| 145 | int ret; |
| 146 | struct ltt_kernel_channel *lkc; |
| 147 | |
| 148 | assert(session); |
| 149 | assert(chan); |
| 150 | |
| 151 | /* Allocate kernel channel */ |
| 152 | lkc = trace_kernel_create_channel(chan); |
| 153 | if (lkc == NULL) { |
| 154 | goto error; |
| 155 | } |
| 156 | |
| 157 | DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d", |
| 158 | chan->name, lkc->channel->attr.overwrite, |
| 159 | lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf, |
| 160 | lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval, |
| 161 | lkc->channel->attr.live_timer_interval, lkc->channel->attr.output); |
| 162 | |
| 163 | /* Kernel tracer channel creation */ |
| 164 | ret = kernctl_create_channel(session->fd, &lkc->channel->attr); |
| 165 | if (ret < 0) { |
| 166 | PERROR("ioctl kernel create channel"); |
| 167 | goto error; |
| 168 | } |
| 169 | |
| 170 | /* Setup the channel fd */ |
| 171 | lkc->fd = ret; |
| 172 | /* Prevent fd duplication after execlp() */ |
| 173 | ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC); |
| 174 | if (ret < 0) { |
| 175 | PERROR("fcntl session fd"); |
| 176 | } |
| 177 | |
| 178 | /* Add channel to session */ |
| 179 | cds_list_add(&lkc->list, &session->channel_list.head); |
| 180 | session->channel_count++; |
| 181 | lkc->session = session; |
| 182 | lkc->key = ++next_kernel_channel_key; |
| 183 | |
| 184 | DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")", |
| 185 | lkc->channel->name, lkc->fd, lkc->key); |
| 186 | |
| 187 | return 0; |
| 188 | |
| 189 | error: |
| 190 | if (lkc) { |
| 191 | free(lkc->channel); |
| 192 | free(lkc); |
| 193 | } |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Compute the offset of the instrumentation byte in the binary based on the |
| 199 | * function probe location using the ELF lookup method. |
| 200 | * |
| 201 | * Returns 0 on success and set the offset out parameter to the offset of the |
| 202 | * elf symbol |
| 203 | * Returns -1 on error |
| 204 | */ |
| 205 | static |
| 206 | int extract_userspace_probe_offset_function_elf( |
| 207 | const struct lttng_userspace_probe_location *probe_location, |
| 208 | struct ltt_kernel_session *session, uint64_t *offset) |
| 209 | { |
| 210 | int fd; |
| 211 | int ret = 0; |
| 212 | const char *symbol = NULL; |
| 213 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; |
| 214 | enum lttng_userspace_probe_location_lookup_method_type lookup_method_type; |
| 215 | |
| 216 | assert(lttng_userspace_probe_location_get_type(probe_location) == |
| 217 | LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION); |
| 218 | |
| 219 | lookup = lttng_userspace_probe_location_get_lookup_method( |
| 220 | probe_location); |
| 221 | if (!lookup) { |
| 222 | ret = -1; |
| 223 | goto end; |
| 224 | } |
| 225 | |
| 226 | lookup_method_type = |
| 227 | lttng_userspace_probe_location_lookup_method_get_type(lookup); |
| 228 | |
| 229 | assert(lookup_method_type == |
| 230 | LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF); |
| 231 | |
| 232 | symbol = lttng_userspace_probe_location_function_get_function_name( |
| 233 | probe_location); |
| 234 | if (!symbol) { |
| 235 | ret = -1; |
| 236 | goto end; |
| 237 | } |
| 238 | |
| 239 | fd = lttng_userspace_probe_location_function_get_binary_fd(probe_location); |
| 240 | if (fd < 0) { |
| 241 | ret = -1; |
| 242 | goto end; |
| 243 | } |
| 244 | |
| 245 | ret = run_as_extract_elf_symbol_offset(fd, symbol, session->uid, |
| 246 | session->gid, offset); |
| 247 | if (ret < 0) { |
| 248 | DBG("userspace probe offset calculation failed for " |
| 249 | "function %s", symbol); |
| 250 | goto end; |
| 251 | } |
| 252 | |
| 253 | DBG("userspace probe elf offset for %s is 0x%jd", symbol, (intmax_t)(*offset)); |
| 254 | end: |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Compute the offsets of the instrumentation bytes in the binary based on the |
| 260 | * tracepoint probe location using the SDT lookup method. This function |
| 261 | * allocates the offsets buffer, the caller must free it. |
| 262 | * |
| 263 | * Returns 0 on success and set the offset out parameter to the offsets of the |
| 264 | * SDT tracepoint. |
| 265 | * Returns -1 on error. |
| 266 | */ |
| 267 | static |
| 268 | int extract_userspace_probe_offset_tracepoint_sdt( |
| 269 | const struct lttng_userspace_probe_location *probe_location, |
| 270 | struct ltt_kernel_session *session, uint64_t **offsets, |
| 271 | uint32_t *offsets_count) |
| 272 | { |
| 273 | enum lttng_userspace_probe_location_lookup_method_type lookup_method_type; |
| 274 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; |
| 275 | const char *probe_name = NULL, *provider_name = NULL; |
| 276 | int ret = 0; |
| 277 | int fd, i; |
| 278 | |
| 279 | assert(lttng_userspace_probe_location_get_type(probe_location) == |
| 280 | LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT); |
| 281 | |
| 282 | lookup = lttng_userspace_probe_location_get_lookup_method(probe_location); |
| 283 | if (!lookup) { |
| 284 | ret = -1; |
| 285 | goto end; |
| 286 | } |
| 287 | |
| 288 | lookup_method_type = |
| 289 | lttng_userspace_probe_location_lookup_method_get_type(lookup); |
| 290 | |
| 291 | assert(lookup_method_type == |
| 292 | LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT); |
| 293 | |
| 294 | |
| 295 | probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name( |
| 296 | probe_location); |
| 297 | if (!probe_name) { |
| 298 | ret = -1; |
| 299 | goto end; |
| 300 | } |
| 301 | |
| 302 | provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name( |
| 303 | probe_location); |
| 304 | if (!provider_name) { |
| 305 | ret = -1; |
| 306 | goto end; |
| 307 | } |
| 308 | |
| 309 | fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location); |
| 310 | if (fd < 0) { |
| 311 | ret = -1; |
| 312 | goto end; |
| 313 | } |
| 314 | |
| 315 | ret = run_as_extract_sdt_probe_offsets(fd, provider_name, probe_name, |
| 316 | session->uid, session->gid, offsets, offsets_count); |
| 317 | if (ret < 0) { |
| 318 | DBG("userspace probe offset calculation failed for sdt " |
| 319 | "probe %s:%s", provider_name, probe_name); |
| 320 | goto end; |
| 321 | } |
| 322 | |
| 323 | if (*offsets_count == 0) { |
| 324 | DBG("no userspace probe offset found"); |
| 325 | goto end; |
| 326 | } |
| 327 | |
| 328 | DBG("%u userspace probe SDT offsets found for %s:%s at:", |
| 329 | *offsets_count, provider_name, probe_name); |
| 330 | for (i = 0; i < *offsets_count; i++) { |
| 331 | DBG("\t0x%jd", (intmax_t)((*offsets)[i])); |
| 332 | } |
| 333 | end: |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Extract the offsets of the instrumentation point for the different lookup |
| 339 | * methods. |
| 340 | */ |
| 341 | static |
| 342 | int userspace_probe_add_callsites(struct lttng_event *ev, |
| 343 | struct ltt_kernel_session *session, int fd) |
| 344 | { |
| 345 | const struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL; |
| 346 | enum lttng_userspace_probe_location_lookup_method_type type; |
| 347 | const struct lttng_userspace_probe_location *location = NULL; |
| 348 | int ret; |
| 349 | |
| 350 | assert(ev); |
| 351 | assert(ev->type == LTTNG_EVENT_USERSPACE_PROBE); |
| 352 | |
| 353 | location = lttng_event_get_userspace_probe_location(ev); |
| 354 | if (!location) { |
| 355 | ret = -1; |
| 356 | goto end; |
| 357 | } |
| 358 | lookup_method = |
| 359 | lttng_userspace_probe_location_get_lookup_method(location); |
| 360 | if (!lookup_method) { |
| 361 | ret = -1; |
| 362 | goto end; |
| 363 | } |
| 364 | |
| 365 | type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method); |
| 366 | switch (type) { |
| 367 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: |
| 368 | { |
| 369 | struct lttng_kernel_event_callsite callsite; |
| 370 | uint64_t offset; |
| 371 | |
| 372 | ret = extract_userspace_probe_offset_function_elf(location, session, &offset); |
| 373 | if (ret) { |
| 374 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 375 | goto end; |
| 376 | } |
| 377 | |
| 378 | callsite.u.uprobe.offset = offset; |
| 379 | ret = kernctl_add_callsite(fd, &callsite); |
| 380 | if (ret) { |
| 381 | WARN("Adding callsite to userspace probe " |
| 382 | "event %s failed.", ev->name); |
| 383 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 384 | goto end; |
| 385 | } |
| 386 | break; |
| 387 | } |
| 388 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: |
| 389 | { |
| 390 | int i; |
| 391 | uint64_t *offsets = NULL; |
| 392 | uint32_t offsets_count; |
| 393 | struct lttng_kernel_event_callsite callsite; |
| 394 | |
| 395 | /* |
| 396 | * This call allocates the offsets buffer. This buffer must be freed |
| 397 | * by the caller |
| 398 | */ |
| 399 | ret = extract_userspace_probe_offset_tracepoint_sdt(location, session, |
| 400 | &offsets, &offsets_count); |
| 401 | if (ret) { |
| 402 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 403 | goto end; |
| 404 | } |
| 405 | for (i = 0; i < offsets_count; i++) { |
| 406 | callsite.u.uprobe.offset = offsets[i]; |
| 407 | ret = kernctl_add_callsite(fd, &callsite); |
| 408 | if (ret) { |
| 409 | WARN("Adding callsite to userspace probe " |
| 410 | "event %s failed.", ev->name); |
| 411 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 412 | free(offsets); |
| 413 | goto end; |
| 414 | } |
| 415 | } |
| 416 | free(offsets); |
| 417 | break; |
| 418 | } |
| 419 | default: |
| 420 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 421 | goto end; |
| 422 | } |
| 423 | end: |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Create a kernel event, enable it to the kernel tracer and add it to the |
| 429 | * channel event list of the kernel session. |
| 430 | * We own filter_expression and filter. |
| 431 | */ |
| 432 | int kernel_create_event(struct lttng_event *ev, |
| 433 | struct ltt_kernel_channel *channel, |
| 434 | char *filter_expression, |
| 435 | struct lttng_filter_bytecode *filter) |
| 436 | { |
| 437 | int err, fd; |
| 438 | enum lttng_error_code ret; |
| 439 | struct ltt_kernel_event *event; |
| 440 | |
| 441 | assert(ev); |
| 442 | assert(channel); |
| 443 | |
| 444 | /* We pass ownership of filter_expression and filter */ |
| 445 | ret = trace_kernel_create_event(ev, filter_expression, |
| 446 | filter, &event); |
| 447 | if (ret != LTTNG_OK) { |
| 448 | goto error; |
| 449 | } |
| 450 | |
| 451 | fd = kernctl_create_event(channel->fd, event->event); |
| 452 | if (fd < 0) { |
| 453 | switch (-fd) { |
| 454 | case EEXIST: |
| 455 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
| 456 | break; |
| 457 | case ENOSYS: |
| 458 | WARN("Event type not implemented"); |
| 459 | ret = LTTNG_ERR_KERN_EVENT_ENOSYS; |
| 460 | break; |
| 461 | case ENOENT: |
| 462 | WARN("Event %s not found!", ev->name); |
| 463 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 464 | break; |
| 465 | default: |
| 466 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 467 | PERROR("create event ioctl"); |
| 468 | } |
| 469 | goto free_event; |
| 470 | } |
| 471 | |
| 472 | event->type = ev->type; |
| 473 | event->fd = fd; |
| 474 | /* Prevent fd duplication after execlp() */ |
| 475 | err = fcntl(event->fd, F_SETFD, FD_CLOEXEC); |
| 476 | if (err < 0) { |
| 477 | PERROR("fcntl session fd"); |
| 478 | } |
| 479 | |
| 480 | if (filter) { |
| 481 | err = kernctl_filter(event->fd, filter); |
| 482 | if (err < 0) { |
| 483 | switch (-err) { |
| 484 | case ENOMEM: |
| 485 | ret = LTTNG_ERR_FILTER_NOMEM; |
| 486 | break; |
| 487 | default: |
| 488 | ret = LTTNG_ERR_FILTER_INVAL; |
| 489 | break; |
| 490 | } |
| 491 | goto filter_error; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if (ev->type == LTTNG_EVENT_USERSPACE_PROBE) { |
| 496 | ret = userspace_probe_add_callsites(ev, channel->session, event->fd); |
| 497 | if (ret) { |
| 498 | goto add_callsite_error; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | err = kernctl_enable(event->fd); |
| 503 | if (err < 0) { |
| 504 | switch (-err) { |
| 505 | case EEXIST: |
| 506 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
| 507 | break; |
| 508 | default: |
| 509 | PERROR("enable kernel event"); |
| 510 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 511 | break; |
| 512 | } |
| 513 | goto enable_error; |
| 514 | } |
| 515 | |
| 516 | /* Add event to event list */ |
| 517 | cds_list_add(&event->list, &channel->events_list.head); |
| 518 | channel->event_count++; |
| 519 | |
| 520 | DBG("Event %s created (fd: %d)", ev->name, event->fd); |
| 521 | |
| 522 | return 0; |
| 523 | |
| 524 | add_callsite_error: |
| 525 | enable_error: |
| 526 | filter_error: |
| 527 | { |
| 528 | int closeret; |
| 529 | |
| 530 | closeret = close(event->fd); |
| 531 | if (closeret) { |
| 532 | PERROR("close event fd"); |
| 533 | } |
| 534 | } |
| 535 | free_event: |
| 536 | free(event); |
| 537 | error: |
| 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Disable a kernel channel. |
| 543 | */ |
| 544 | int kernel_disable_channel(struct ltt_kernel_channel *chan) |
| 545 | { |
| 546 | int ret; |
| 547 | |
| 548 | assert(chan); |
| 549 | |
| 550 | ret = kernctl_disable(chan->fd); |
| 551 | if (ret < 0) { |
| 552 | PERROR("disable chan ioctl"); |
| 553 | goto error; |
| 554 | } |
| 555 | |
| 556 | chan->enabled = 0; |
| 557 | DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")", |
| 558 | chan->channel->name, chan->fd, chan->key); |
| 559 | |
| 560 | return 0; |
| 561 | |
| 562 | error: |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Enable a kernel channel. |
| 568 | */ |
| 569 | int kernel_enable_channel(struct ltt_kernel_channel *chan) |
| 570 | { |
| 571 | int ret; |
| 572 | |
| 573 | assert(chan); |
| 574 | |
| 575 | ret = kernctl_enable(chan->fd); |
| 576 | if (ret < 0 && ret != -EEXIST) { |
| 577 | PERROR("Enable kernel chan"); |
| 578 | goto error; |
| 579 | } |
| 580 | |
| 581 | chan->enabled = 1; |
| 582 | DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")", |
| 583 | chan->channel->name, chan->fd, chan->key); |
| 584 | |
| 585 | return 0; |
| 586 | |
| 587 | error: |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Enable a kernel event. |
| 593 | */ |
| 594 | int kernel_enable_event(struct ltt_kernel_event *event) |
| 595 | { |
| 596 | int ret; |
| 597 | |
| 598 | assert(event); |
| 599 | |
| 600 | ret = kernctl_enable(event->fd); |
| 601 | if (ret < 0) { |
| 602 | switch (-ret) { |
| 603 | case EEXIST: |
| 604 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
| 605 | break; |
| 606 | default: |
| 607 | PERROR("enable kernel event"); |
| 608 | break; |
| 609 | } |
| 610 | goto error; |
| 611 | } |
| 612 | |
| 613 | event->enabled = 1; |
| 614 | DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd); |
| 615 | |
| 616 | return 0; |
| 617 | |
| 618 | error: |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Disable a kernel event. |
| 624 | */ |
| 625 | int kernel_disable_event(struct ltt_kernel_event *event) |
| 626 | { |
| 627 | int ret; |
| 628 | |
| 629 | assert(event); |
| 630 | |
| 631 | ret = kernctl_disable(event->fd); |
| 632 | if (ret < 0) { |
| 633 | switch (-ret) { |
| 634 | case EEXIST: |
| 635 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
| 636 | break; |
| 637 | default: |
| 638 | PERROR("disable kernel event"); |
| 639 | break; |
| 640 | } |
| 641 | goto error; |
| 642 | } |
| 643 | |
| 644 | event->enabled = 0; |
| 645 | DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd); |
| 646 | |
| 647 | return 0; |
| 648 | |
| 649 | error: |
| 650 | return ret; |
| 651 | } |
| 652 | |
| 653 | |
| 654 | int kernel_track_pid(struct ltt_kernel_session *session, int pid) |
| 655 | { |
| 656 | int ret; |
| 657 | |
| 658 | DBG("Kernel track PID %d for session id %" PRIu64 ".", |
| 659 | pid, session->id); |
| 660 | ret = kernctl_track_pid(session->fd, pid); |
| 661 | if (!ret) { |
| 662 | return LTTNG_OK; |
| 663 | } |
| 664 | switch (-ret) { |
| 665 | case EINVAL: |
| 666 | return LTTNG_ERR_INVALID; |
| 667 | case ENOMEM: |
| 668 | return LTTNG_ERR_NOMEM; |
| 669 | case EEXIST: |
| 670 | return LTTNG_ERR_PID_TRACKED; |
| 671 | default: |
| 672 | return LTTNG_ERR_UNK; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | int kernel_untrack_pid(struct ltt_kernel_session *session, int pid) |
| 677 | { |
| 678 | int ret; |
| 679 | |
| 680 | DBG("Kernel untrack PID %d for session id %" PRIu64 ".", |
| 681 | pid, session->id); |
| 682 | ret = kernctl_untrack_pid(session->fd, pid); |
| 683 | if (!ret) { |
| 684 | return LTTNG_OK; |
| 685 | } |
| 686 | switch (-ret) { |
| 687 | case EINVAL: |
| 688 | return LTTNG_ERR_INVALID; |
| 689 | case ENOMEM: |
| 690 | return LTTNG_ERR_NOMEM; |
| 691 | case ENOENT: |
| 692 | return LTTNG_ERR_PID_NOT_TRACKED; |
| 693 | default: |
| 694 | return LTTNG_ERR_UNK; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | ssize_t kernel_list_tracker_pids(struct ltt_kernel_session *session, |
| 699 | int **_pids) |
| 700 | { |
| 701 | int fd, ret; |
| 702 | int pid; |
| 703 | ssize_t nbmem, count = 0; |
| 704 | FILE *fp; |
| 705 | int *pids; |
| 706 | |
| 707 | fd = kernctl_list_tracker_pids(session->fd); |
| 708 | if (fd < 0) { |
| 709 | PERROR("kernel tracker pids list"); |
| 710 | goto error; |
| 711 | } |
| 712 | |
| 713 | fp = fdopen(fd, "r"); |
| 714 | if (fp == NULL) { |
| 715 | PERROR("kernel tracker pids list fdopen"); |
| 716 | goto error_fp; |
| 717 | } |
| 718 | |
| 719 | nbmem = KERNEL_TRACKER_PIDS_INIT_LIST_SIZE; |
| 720 | pids = zmalloc(sizeof(*pids) * nbmem); |
| 721 | if (pids == NULL) { |
| 722 | PERROR("alloc list pids"); |
| 723 | count = -ENOMEM; |
| 724 | goto end; |
| 725 | } |
| 726 | |
| 727 | while (fscanf(fp, "process { pid = %u; };\n", &pid) == 1) { |
| 728 | if (count >= nbmem) { |
| 729 | int *new_pids; |
| 730 | size_t new_nbmem; |
| 731 | |
| 732 | new_nbmem = nbmem << 1; |
| 733 | DBG("Reallocating pids list from %zu to %zu entries", |
| 734 | nbmem, new_nbmem); |
| 735 | new_pids = realloc(pids, new_nbmem * sizeof(*new_pids)); |
| 736 | if (new_pids == NULL) { |
| 737 | PERROR("realloc list events"); |
| 738 | free(pids); |
| 739 | count = -ENOMEM; |
| 740 | goto end; |
| 741 | } |
| 742 | /* Zero the new memory */ |
| 743 | memset(new_pids + nbmem, 0, |
| 744 | (new_nbmem - nbmem) * sizeof(*new_pids)); |
| 745 | nbmem = new_nbmem; |
| 746 | pids = new_pids; |
| 747 | } |
| 748 | pids[count++] = pid; |
| 749 | } |
| 750 | |
| 751 | *_pids = pids; |
| 752 | DBG("Kernel list tracker pids done (%zd pids)", count); |
| 753 | end: |
| 754 | ret = fclose(fp); /* closes both fp and fd */ |
| 755 | if (ret) { |
| 756 | PERROR("fclose"); |
| 757 | } |
| 758 | return count; |
| 759 | |
| 760 | error_fp: |
| 761 | ret = close(fd); |
| 762 | if (ret) { |
| 763 | PERROR("close"); |
| 764 | } |
| 765 | error: |
| 766 | return -1; |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * Create kernel metadata, open from the kernel tracer and add it to the |
| 771 | * kernel session. |
| 772 | */ |
| 773 | int kernel_open_metadata(struct ltt_kernel_session *session) |
| 774 | { |
| 775 | int ret; |
| 776 | struct ltt_kernel_metadata *lkm = NULL; |
| 777 | |
| 778 | assert(session); |
| 779 | |
| 780 | /* Allocate kernel metadata */ |
| 781 | lkm = trace_kernel_create_metadata(); |
| 782 | if (lkm == NULL) { |
| 783 | goto error; |
| 784 | } |
| 785 | |
| 786 | /* Kernel tracer metadata creation */ |
| 787 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
| 788 | if (ret < 0) { |
| 789 | goto error_open; |
| 790 | } |
| 791 | |
| 792 | lkm->fd = ret; |
| 793 | lkm->key = ++next_kernel_channel_key; |
| 794 | /* Prevent fd duplication after execlp() */ |
| 795 | ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); |
| 796 | if (ret < 0) { |
| 797 | PERROR("fcntl session fd"); |
| 798 | } |
| 799 | |
| 800 | session->metadata = lkm; |
| 801 | |
| 802 | DBG("Kernel metadata opened (fd: %d)", lkm->fd); |
| 803 | |
| 804 | return 0; |
| 805 | |
| 806 | error_open: |
| 807 | trace_kernel_destroy_metadata(lkm); |
| 808 | error: |
| 809 | return -1; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Start tracing session. |
| 814 | */ |
| 815 | int kernel_start_session(struct ltt_kernel_session *session) |
| 816 | { |
| 817 | int ret; |
| 818 | |
| 819 | assert(session); |
| 820 | |
| 821 | ret = kernctl_start_session(session->fd); |
| 822 | if (ret < 0) { |
| 823 | PERROR("ioctl start session"); |
| 824 | goto error; |
| 825 | } |
| 826 | |
| 827 | DBG("Kernel session started"); |
| 828 | |
| 829 | return 0; |
| 830 | |
| 831 | error: |
| 832 | return ret; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Make a kernel wait to make sure in-flight probe have completed. |
| 837 | */ |
| 838 | void kernel_wait_quiescent(int fd) |
| 839 | { |
| 840 | int ret; |
| 841 | |
| 842 | DBG("Kernel quiescent wait on %d", fd); |
| 843 | |
| 844 | ret = kernctl_wait_quiescent(fd); |
| 845 | if (ret < 0) { |
| 846 | PERROR("wait quiescent ioctl"); |
| 847 | ERR("Kernel quiescent wait failed"); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Force flush buffer of metadata. |
| 853 | */ |
| 854 | int kernel_metadata_flush_buffer(int fd) |
| 855 | { |
| 856 | int ret; |
| 857 | |
| 858 | DBG("Kernel flushing metadata buffer on fd %d", fd); |
| 859 | |
| 860 | ret = kernctl_buffer_flush(fd); |
| 861 | if (ret < 0) { |
| 862 | ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret); |
| 863 | } |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | /* |
| 869 | * Force flush buffer for channel. |
| 870 | */ |
| 871 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) |
| 872 | { |
| 873 | int ret; |
| 874 | struct ltt_kernel_stream *stream; |
| 875 | |
| 876 | assert(channel); |
| 877 | |
| 878 | DBG("Flush buffer for channel %s", channel->channel->name); |
| 879 | |
| 880 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { |
| 881 | DBG("Flushing channel stream %d", stream->fd); |
| 882 | ret = kernctl_buffer_flush(stream->fd); |
| 883 | if (ret < 0) { |
| 884 | PERROR("ioctl"); |
| 885 | ERR("Fail to flush buffer for stream %d (ret: %d)", |
| 886 | stream->fd, ret); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * Stop tracing session. |
| 895 | */ |
| 896 | int kernel_stop_session(struct ltt_kernel_session *session) |
| 897 | { |
| 898 | int ret; |
| 899 | |
| 900 | assert(session); |
| 901 | |
| 902 | ret = kernctl_stop_session(session->fd); |
| 903 | if (ret < 0) { |
| 904 | goto error; |
| 905 | } |
| 906 | |
| 907 | DBG("Kernel session stopped"); |
| 908 | |
| 909 | return 0; |
| 910 | |
| 911 | error: |
| 912 | return ret; |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | * Open stream of channel, register it to the kernel tracer and add it |
| 917 | * to the stream list of the channel. |
| 918 | * |
| 919 | * Note: given that the streams may appear in random order wrt CPU |
| 920 | * number (e.g. cpu hotplug), the index value of the stream number in |
| 921 | * the stream name is not necessarily linked to the CPU number. |
| 922 | * |
| 923 | * Return the number of created stream. Else, a negative value. |
| 924 | */ |
| 925 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
| 926 | { |
| 927 | int ret; |
| 928 | struct ltt_kernel_stream *lks; |
| 929 | |
| 930 | assert(channel); |
| 931 | |
| 932 | while ((ret = kernctl_create_stream(channel->fd)) >= 0) { |
| 933 | lks = trace_kernel_create_stream(channel->channel->name, |
| 934 | channel->stream_count); |
| 935 | if (lks == NULL) { |
| 936 | ret = close(ret); |
| 937 | if (ret) { |
| 938 | PERROR("close"); |
| 939 | } |
| 940 | goto error; |
| 941 | } |
| 942 | |
| 943 | lks->fd = ret; |
| 944 | /* Prevent fd duplication after execlp() */ |
| 945 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); |
| 946 | if (ret < 0) { |
| 947 | PERROR("fcntl session fd"); |
| 948 | } |
| 949 | |
| 950 | lks->tracefile_size = channel->channel->attr.tracefile_size; |
| 951 | lks->tracefile_count = channel->channel->attr.tracefile_count; |
| 952 | |
| 953 | /* Add stream to channel stream list */ |
| 954 | cds_list_add(&lks->list, &channel->stream_list.head); |
| 955 | channel->stream_count++; |
| 956 | |
| 957 | DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd, |
| 958 | lks->state); |
| 959 | } |
| 960 | |
| 961 | return channel->stream_count; |
| 962 | |
| 963 | error: |
| 964 | return -1; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Open the metadata stream and set it to the kernel session. |
| 969 | */ |
| 970 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
| 971 | { |
| 972 | int ret; |
| 973 | |
| 974 | assert(session); |
| 975 | |
| 976 | ret = kernctl_create_stream(session->metadata->fd); |
| 977 | if (ret < 0) { |
| 978 | PERROR("kernel create metadata stream"); |
| 979 | goto error; |
| 980 | } |
| 981 | |
| 982 | DBG("Kernel metadata stream created (fd: %d)", ret); |
| 983 | session->metadata_stream_fd = ret; |
| 984 | /* Prevent fd duplication after execlp() */ |
| 985 | ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); |
| 986 | if (ret < 0) { |
| 987 | PERROR("fcntl session fd"); |
| 988 | } |
| 989 | |
| 990 | return 0; |
| 991 | |
| 992 | error: |
| 993 | return -1; |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Get the event list from the kernel tracer and return the number of elements. |
| 998 | */ |
| 999 | ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) |
| 1000 | { |
| 1001 | int fd, ret; |
| 1002 | char *event; |
| 1003 | size_t nbmem, count = 0; |
| 1004 | FILE *fp; |
| 1005 | struct lttng_event *elist; |
| 1006 | |
| 1007 | assert(events); |
| 1008 | |
| 1009 | fd = kernctl_tracepoint_list(tracer_fd); |
| 1010 | if (fd < 0) { |
| 1011 | PERROR("kernel tracepoint list"); |
| 1012 | goto error; |
| 1013 | } |
| 1014 | |
| 1015 | fp = fdopen(fd, "r"); |
| 1016 | if (fp == NULL) { |
| 1017 | PERROR("kernel tracepoint list fdopen"); |
| 1018 | goto error_fp; |
| 1019 | } |
| 1020 | |
| 1021 | /* |
| 1022 | * Init memory size counter |
| 1023 | * See kernel-ctl.h for explanation of this value |
| 1024 | */ |
| 1025 | nbmem = KERNEL_EVENT_INIT_LIST_SIZE; |
| 1026 | elist = zmalloc(sizeof(struct lttng_event) * nbmem); |
| 1027 | if (elist == NULL) { |
| 1028 | PERROR("alloc list events"); |
| 1029 | count = -ENOMEM; |
| 1030 | goto end; |
| 1031 | } |
| 1032 | |
| 1033 | while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) { |
| 1034 | if (count >= nbmem) { |
| 1035 | struct lttng_event *new_elist; |
| 1036 | size_t new_nbmem; |
| 1037 | |
| 1038 | new_nbmem = nbmem << 1; |
| 1039 | DBG("Reallocating event list from %zu to %zu bytes", |
| 1040 | nbmem, new_nbmem); |
| 1041 | new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event)); |
| 1042 | if (new_elist == NULL) { |
| 1043 | PERROR("realloc list events"); |
| 1044 | free(event); |
| 1045 | free(elist); |
| 1046 | count = -ENOMEM; |
| 1047 | goto end; |
| 1048 | } |
| 1049 | /* Zero the new memory */ |
| 1050 | memset(new_elist + nbmem, 0, |
| 1051 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); |
| 1052 | nbmem = new_nbmem; |
| 1053 | elist = new_elist; |
| 1054 | } |
| 1055 | strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN); |
| 1056 | elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 1057 | elist[count].enabled = -1; |
| 1058 | count++; |
| 1059 | free(event); |
| 1060 | } |
| 1061 | |
| 1062 | *events = elist; |
| 1063 | DBG("Kernel list events done (%zu events)", count); |
| 1064 | end: |
| 1065 | ret = fclose(fp); /* closes both fp and fd */ |
| 1066 | if (ret) { |
| 1067 | PERROR("fclose"); |
| 1068 | } |
| 1069 | return count; |
| 1070 | |
| 1071 | error_fp: |
| 1072 | ret = close(fd); |
| 1073 | if (ret) { |
| 1074 | PERROR("close"); |
| 1075 | } |
| 1076 | error: |
| 1077 | return -1; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * Get kernel version and validate it. |
| 1082 | */ |
| 1083 | int kernel_validate_version(int tracer_fd, |
| 1084 | struct lttng_kernel_tracer_version *version, |
| 1085 | struct lttng_kernel_tracer_abi_version *abi_version) |
| 1086 | { |
| 1087 | int ret; |
| 1088 | |
| 1089 | ret = kernctl_tracer_version(tracer_fd, version); |
| 1090 | if (ret < 0) { |
| 1091 | ERR("Failed to retrieve the lttng-modules version"); |
| 1092 | goto error; |
| 1093 | } |
| 1094 | |
| 1095 | /* Validate version */ |
| 1096 | if (version->major != VERSION_MAJOR) { |
| 1097 | ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)", |
| 1098 | version->major, VERSION_MAJOR); |
| 1099 | goto error_version; |
| 1100 | } |
| 1101 | ret = kernctl_tracer_abi_version(tracer_fd, abi_version); |
| 1102 | if (ret < 0) { |
| 1103 | ERR("Failed to retrieve lttng-modules ABI version"); |
| 1104 | goto error; |
| 1105 | } |
| 1106 | if (abi_version->major != LTTNG_MODULES_ABI_MAJOR_VERSION) { |
| 1107 | ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)", |
| 1108 | abi_version->major, abi_version->minor, |
| 1109 | LTTNG_MODULES_ABI_MAJOR_VERSION); |
| 1110 | goto error; |
| 1111 | } |
| 1112 | DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)", |
| 1113 | version->major, version->minor, |
| 1114 | abi_version->major, abi_version->minor); |
| 1115 | return 0; |
| 1116 | |
| 1117 | error_version: |
| 1118 | ret = -1; |
| 1119 | |
| 1120 | error: |
| 1121 | ERR("Kernel tracer version check failed; kernel tracing will not be available"); |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * Kernel work-arounds called at the start of sessiond main(). |
| 1127 | */ |
| 1128 | int init_kernel_workarounds(void) |
| 1129 | { |
| 1130 | int ret; |
| 1131 | FILE *fp; |
| 1132 | |
| 1133 | /* |
| 1134 | * boot_id needs to be read once before being used concurrently |
| 1135 | * to deal with a Linux kernel race. A fix is proposed for |
| 1136 | * upstream, but the work-around is needed for older kernels. |
| 1137 | */ |
| 1138 | fp = fopen("/proc/sys/kernel/random/boot_id", "r"); |
| 1139 | if (!fp) { |
| 1140 | goto end_boot_id; |
| 1141 | } |
| 1142 | while (!feof(fp)) { |
| 1143 | char buf[37] = ""; |
| 1144 | |
| 1145 | ret = fread(buf, 1, sizeof(buf), fp); |
| 1146 | if (ret < 0) { |
| 1147 | /* Ignore error, we don't really care */ |
| 1148 | } |
| 1149 | } |
| 1150 | ret = fclose(fp); |
| 1151 | if (ret) { |
| 1152 | PERROR("fclose"); |
| 1153 | } |
| 1154 | end_boot_id: |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * Complete teardown of a kernel session. |
| 1160 | */ |
| 1161 | void kernel_destroy_session(struct ltt_kernel_session *ksess) |
| 1162 | { |
| 1163 | struct lttng_trace_chunk *trace_chunk; |
| 1164 | |
| 1165 | if (ksess == NULL) { |
| 1166 | DBG3("No kernel session when tearing down session"); |
| 1167 | return; |
| 1168 | } |
| 1169 | |
| 1170 | DBG("Tearing down kernel session"); |
| 1171 | trace_chunk = ksess->current_trace_chunk; |
| 1172 | |
| 1173 | /* |
| 1174 | * Destroy channels on the consumer if at least one FD has been sent and we |
| 1175 | * are in no output mode because the streams are in *no* monitor mode so we |
| 1176 | * have to send a command to clean them up or else they leaked. |
| 1177 | */ |
| 1178 | if (!ksess->output_traces && ksess->consumer_fds_sent) { |
| 1179 | int ret; |
| 1180 | struct consumer_socket *socket; |
| 1181 | struct lttng_ht_iter iter; |
| 1182 | |
| 1183 | /* For each consumer socket. */ |
| 1184 | rcu_read_lock(); |
| 1185 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, |
| 1186 | socket, node.node) { |
| 1187 | struct ltt_kernel_channel *chan; |
| 1188 | |
| 1189 | /* For each channel, ask the consumer to destroy it. */ |
| 1190 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
| 1191 | ret = kernel_consumer_destroy_channel(socket, chan); |
| 1192 | if (ret < 0) { |
| 1193 | /* Consumer is probably dead. Use next socket. */ |
| 1194 | continue; |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | rcu_read_unlock(); |
| 1199 | } |
| 1200 | |
| 1201 | /* Close any relayd session */ |
| 1202 | consumer_output_send_destroy_relayd(ksess->consumer); |
| 1203 | |
| 1204 | trace_kernel_destroy_session(ksess); |
| 1205 | lttng_trace_chunk_put(trace_chunk); |
| 1206 | } |
| 1207 | |
| 1208 | /* |
| 1209 | * Destroy a kernel channel object. It does not do anything on the tracer side. |
| 1210 | */ |
| 1211 | void kernel_destroy_channel(struct ltt_kernel_channel *kchan) |
| 1212 | { |
| 1213 | struct ltt_kernel_session *ksess = NULL; |
| 1214 | |
| 1215 | assert(kchan); |
| 1216 | assert(kchan->channel); |
| 1217 | |
| 1218 | DBG3("Kernel destroy channel %s", kchan->channel->name); |
| 1219 | |
| 1220 | /* Update channel count of associated session. */ |
| 1221 | if (kchan->session) { |
| 1222 | /* Keep pointer reference so we can update it after the destroy. */ |
| 1223 | ksess = kchan->session; |
| 1224 | } |
| 1225 | |
| 1226 | trace_kernel_destroy_channel(kchan); |
| 1227 | |
| 1228 | /* |
| 1229 | * At this point the kernel channel is not visible anymore. This is safe |
| 1230 | * since in order to work on a visible kernel session, the tracing session |
| 1231 | * lock (ltt_session.lock) MUST be acquired. |
| 1232 | */ |
| 1233 | if (ksess) { |
| 1234 | ksess->channel_count--; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * Take a snapshot for a given kernel session. |
| 1240 | * |
| 1241 | * Return LTTNG_OK on success or else return a LTTNG_ERR code. |
| 1242 | */ |
| 1243 | enum lttng_error_code kernel_snapshot_record( |
| 1244 | struct ltt_kernel_session *ksess, |
| 1245 | const struct consumer_output *output, int wait, |
| 1246 | uint64_t nb_packets_per_stream) |
| 1247 | { |
| 1248 | int err, ret, saved_metadata_fd; |
| 1249 | enum lttng_error_code status = LTTNG_OK; |
| 1250 | struct consumer_socket *socket; |
| 1251 | struct lttng_ht_iter iter; |
| 1252 | struct ltt_kernel_metadata *saved_metadata; |
| 1253 | |
| 1254 | assert(ksess); |
| 1255 | assert(ksess->consumer); |
| 1256 | assert(output); |
| 1257 | |
| 1258 | DBG("Kernel snapshot record started"); |
| 1259 | |
| 1260 | /* Save current metadata since the following calls will change it. */ |
| 1261 | saved_metadata = ksess->metadata; |
| 1262 | saved_metadata_fd = ksess->metadata_stream_fd; |
| 1263 | |
| 1264 | rcu_read_lock(); |
| 1265 | |
| 1266 | ret = kernel_open_metadata(ksess); |
| 1267 | if (ret < 0) { |
| 1268 | status = LTTNG_ERR_KERN_META_FAIL; |
| 1269 | goto error; |
| 1270 | } |
| 1271 | |
| 1272 | ret = kernel_open_metadata_stream(ksess); |
| 1273 | if (ret < 0) { |
| 1274 | status = LTTNG_ERR_KERN_META_FAIL; |
| 1275 | goto error_open_stream; |
| 1276 | } |
| 1277 | |
| 1278 | /* Send metadata to consumer and snapshot everything. */ |
| 1279 | cds_lfht_for_each_entry(output->socks->ht, &iter.iter, |
| 1280 | socket, node.node) { |
| 1281 | struct ltt_kernel_channel *chan; |
| 1282 | |
| 1283 | pthread_mutex_lock(socket->lock); |
| 1284 | /* This stream must not be monitored by the consumer. */ |
| 1285 | ret = kernel_consumer_add_metadata(socket, ksess, 0); |
| 1286 | pthread_mutex_unlock(socket->lock); |
| 1287 | if (ret < 0) { |
| 1288 | status = LTTNG_ERR_KERN_META_FAIL; |
| 1289 | goto error_consumer; |
| 1290 | } |
| 1291 | |
| 1292 | /* For each channel, ask the consumer to snapshot it. */ |
| 1293 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
| 1294 | status = consumer_snapshot_channel(socket, chan->key, output, 0, |
| 1295 | ksess->uid, ksess->gid, |
| 1296 | DEFAULT_KERNEL_TRACE_DIR, wait, |
| 1297 | nb_packets_per_stream); |
| 1298 | if (status != LTTNG_OK) { |
| 1299 | (void) kernel_consumer_destroy_metadata(socket, |
| 1300 | ksess->metadata); |
| 1301 | goto error_consumer; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | /* Snapshot metadata, */ |
| 1306 | status = consumer_snapshot_channel(socket, ksess->metadata->key, output, |
| 1307 | 1, ksess->uid, ksess->gid, |
| 1308 | DEFAULT_KERNEL_TRACE_DIR, wait, 0); |
| 1309 | if (status != LTTNG_OK) { |
| 1310 | goto error_consumer; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | * The metadata snapshot is done, ask the consumer to destroy it since |
| 1315 | * it's not monitored on the consumer side. |
| 1316 | */ |
| 1317 | (void) kernel_consumer_destroy_metadata(socket, ksess->metadata); |
| 1318 | } |
| 1319 | |
| 1320 | error_consumer: |
| 1321 | /* Close newly opened metadata stream. It's now on the consumer side. */ |
| 1322 | err = close(ksess->metadata_stream_fd); |
| 1323 | if (err < 0) { |
| 1324 | PERROR("close snapshot kernel"); |
| 1325 | } |
| 1326 | |
| 1327 | error_open_stream: |
| 1328 | trace_kernel_destroy_metadata(ksess->metadata); |
| 1329 | error: |
| 1330 | /* Restore metadata state.*/ |
| 1331 | ksess->metadata = saved_metadata; |
| 1332 | ksess->metadata_stream_fd = saved_metadata_fd; |
| 1333 | rcu_read_unlock(); |
| 1334 | return status; |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * Get the syscall mask array from the kernel tracer. |
| 1339 | * |
| 1340 | * Return 0 on success else a negative value. In both case, syscall_mask should |
| 1341 | * be freed. |
| 1342 | */ |
| 1343 | int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits) |
| 1344 | { |
| 1345 | assert(syscall_mask); |
| 1346 | assert(nr_bits); |
| 1347 | |
| 1348 | return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits); |
| 1349 | } |
| 1350 | |
| 1351 | /* |
| 1352 | * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi |
| 1353 | * version number. |
| 1354 | * |
| 1355 | * Return 1 on success, 0 when feature is not supported, negative value in case |
| 1356 | * of errors. |
| 1357 | */ |
| 1358 | int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd) |
| 1359 | { |
| 1360 | int ret = 0; // Not supported by default |
| 1361 | struct lttng_kernel_tracer_abi_version abi; |
| 1362 | |
| 1363 | ret = kernctl_tracer_abi_version(tracer_fd, &abi); |
| 1364 | if (ret < 0) { |
| 1365 | ERR("Failed to retrieve lttng-modules ABI version"); |
| 1366 | goto error; |
| 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3 |
| 1371 | */ |
| 1372 | if (abi.major >= 2 && abi.minor >= 3) { |
| 1373 | /* Supported */ |
| 1374 | ret = 1; |
| 1375 | } else { |
| 1376 | /* Not supported */ |
| 1377 | ret = 0; |
| 1378 | } |
| 1379 | error: |
| 1380 | return ret; |
| 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * Rotate a kernel session. |
| 1385 | * |
| 1386 | * Return LTTNG_OK on success or else an LTTng error code. |
| 1387 | */ |
| 1388 | enum lttng_error_code kernel_rotate_session(struct ltt_session *session) |
| 1389 | { |
| 1390 | int ret; |
| 1391 | enum lttng_error_code status = LTTNG_OK; |
| 1392 | struct consumer_socket *socket; |
| 1393 | struct lttng_ht_iter iter; |
| 1394 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 1395 | |
| 1396 | assert(ksess); |
| 1397 | assert(ksess->consumer); |
| 1398 | |
| 1399 | DBG("Rotate kernel session %s started (session %" PRIu64 ")", |
| 1400 | session->name, session->id); |
| 1401 | |
| 1402 | rcu_read_lock(); |
| 1403 | |
| 1404 | /* |
| 1405 | * Note that this loop will end after one iteration given that there is |
| 1406 | * only one kernel consumer. |
| 1407 | */ |
| 1408 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, |
| 1409 | socket, node.node) { |
| 1410 | struct ltt_kernel_channel *chan; |
| 1411 | |
| 1412 | /* For each channel, ask the consumer to rotate it. */ |
| 1413 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
| 1414 | DBG("Rotate kernel channel %" PRIu64 ", session %s", |
| 1415 | chan->key, session->name); |
| 1416 | ret = consumer_rotate_channel(socket, chan->key, |
| 1417 | ksess->uid, ksess->gid, ksess->consumer, |
| 1418 | /* is_metadata_channel */ false); |
| 1419 | if (ret < 0) { |
| 1420 | status = LTTNG_ERR_KERN_CONSUMER_FAIL; |
| 1421 | goto error; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /* |
| 1426 | * Rotate the metadata channel. |
| 1427 | */ |
| 1428 | ret = consumer_rotate_channel(socket, ksess->metadata->key, |
| 1429 | ksess->uid, ksess->gid, ksess->consumer, |
| 1430 | /* is_metadata_channel */ true); |
| 1431 | if (ret < 0) { |
| 1432 | status = LTTNG_ERR_KERN_CONSUMER_FAIL; |
| 1433 | goto error; |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | error: |
| 1438 | rcu_read_unlock(); |
| 1439 | return status; |
| 1440 | } |
| 1441 | |
| 1442 | enum lttng_error_code kernel_create_channel_subdirectories( |
| 1443 | const struct ltt_kernel_session *ksess) |
| 1444 | { |
| 1445 | enum lttng_error_code ret = LTTNG_OK; |
| 1446 | enum lttng_trace_chunk_status chunk_status; |
| 1447 | |
| 1448 | rcu_read_lock(); |
| 1449 | assert(ksess->current_trace_chunk); |
| 1450 | |
| 1451 | /* |
| 1452 | * Create the index subdirectory which will take care |
| 1453 | * of implicitly creating the channel's path. |
| 1454 | */ |
| 1455 | chunk_status = lttng_trace_chunk_create_subdirectory( |
| 1456 | ksess->current_trace_chunk, |
| 1457 | DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR); |
| 1458 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1459 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 1460 | goto error; |
| 1461 | } |
| 1462 | error: |
| 1463 | rcu_read_unlock(); |
| 1464 | return ret; |
| 1465 | } |