| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | #include "condition-internal.hpp" |
| 10 | #include "consumer.hpp" |
| 11 | #include "event-notifier-error-accounting.hpp" |
| 12 | #include "kern-modules.hpp" |
| 13 | #include "kernel-consumer.hpp" |
| 14 | #include "kernel.hpp" |
| 15 | #include "lttng-sessiond.hpp" |
| 16 | #include "lttng-syscall.hpp" |
| 17 | #include "modprobe.hpp" |
| 18 | #include "notification-thread-commands.hpp" |
| 19 | #include "rotate.hpp" |
| 20 | #include "sessiond-config.hpp" |
| 21 | #include "tracker.hpp" |
| 22 | #include "utils.hpp" |
| 23 | |
| 24 | #include <common/common.hpp> |
| 25 | #include <common/hashtable/utils.hpp> |
| 26 | #include <common/kernel-ctl/kernel-ctl.hpp> |
| 27 | #include <common/kernel-ctl/kernel-ioctl.hpp> |
| 28 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 29 | #include <common/trace-chunk.hpp> |
| 30 | #include <common/tracker.hpp> |
| 31 | #include <common/urcu.hpp> |
| 32 | #include <common/utils.hpp> |
| 33 | |
| 34 | #include <lttng/condition/event-rule-matches-internal.hpp> |
| 35 | #include <lttng/condition/event-rule-matches.h> |
| 36 | #include <lttng/event-rule/event-rule-internal.hpp> |
| 37 | #include <lttng/event-rule/event-rule.h> |
| 38 | #include <lttng/event-rule/kernel-uprobe-internal.hpp> |
| 39 | #include <lttng/event.h> |
| 40 | #include <lttng/lttng-error.h> |
| 41 | #include <lttng/tracker.h> |
| 42 | #include <lttng/userspace-probe-internal.hpp> |
| 43 | #include <lttng/userspace-probe.h> |
| 44 | |
| 45 | #include <fcntl.h> |
| 46 | #include <inttypes.h> |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <string.h> |
| 50 | #include <sys/types.h> |
| 51 | #include <unistd.h> |
| 52 | |
| 53 | /* |
| 54 | * Key used to reference a channel between the sessiond and the consumer. This |
| 55 | * is only read and updated with the session_list lock held. |
| 56 | */ |
| 57 | static uint64_t next_kernel_channel_key; |
| 58 | |
| 59 | static const char *module_proc_lttng = "/proc/lttng"; |
| 60 | |
| 61 | static int kernel_tracer_fd = -1; |
| 62 | static int kernel_tracer_event_notifier_group_fd = -1; |
| 63 | static int kernel_tracer_event_notifier_group_notification_fd = -1; |
| 64 | static struct cds_lfht *kernel_token_to_event_notifier_rule_ht; |
| 65 | |
| 66 | /* |
| 67 | * Add context on a kernel channel. |
| 68 | * |
| 69 | * Assumes the ownership of ctx. |
| 70 | */ |
| 71 | int kernel_add_channel_context(struct ltt_kernel_channel *chan, struct ltt_kernel_context *ctx) |
| 72 | { |
| 73 | int ret; |
| 74 | |
| 75 | LTTNG_ASSERT(chan); |
| 76 | LTTNG_ASSERT(ctx); |
| 77 | |
| 78 | DBG("Adding context to channel %s", chan->channel->name); |
| 79 | ret = kernctl_add_context(chan->fd, &ctx->ctx); |
| 80 | if (ret < 0) { |
| 81 | switch (-ret) { |
| 82 | case ENOSYS: |
| 83 | /* Exists but not available for this kernel */ |
| 84 | ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE; |
| 85 | goto error; |
| 86 | case EEXIST: |
| 87 | /* If EEXIST, we just ignore the error */ |
| 88 | ret = 0; |
| 89 | goto end; |
| 90 | default: |
| 91 | PERROR("add context ioctl"); |
| 92 | ret = LTTNG_ERR_KERN_CONTEXT_FAIL; |
| 93 | goto error; |
| 94 | } |
| 95 | } |
| 96 | ret = 0; |
| 97 | |
| 98 | end: |
| 99 | cds_list_add_tail(&ctx->list, &chan->ctx_list); |
| 100 | ctx->in_list = true; |
| 101 | ctx = nullptr; |
| 102 | error: |
| 103 | if (ctx) { |
| 104 | trace_kernel_destroy_context(ctx); |
| 105 | } |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Create a new kernel session, register it to the kernel tracer and add it to |
| 111 | * the session daemon session. |
| 112 | */ |
| 113 | int kernel_create_session(struct ltt_session *session) |
| 114 | { |
| 115 | int ret; |
| 116 | struct ltt_kernel_session *lks; |
| 117 | |
| 118 | LTTNG_ASSERT(session); |
| 119 | |
| 120 | /* Allocate data structure */ |
| 121 | lks = trace_kernel_create_session(); |
| 122 | if (lks == nullptr) { |
| 123 | ret = -1; |
| 124 | goto error; |
| 125 | } |
| 126 | |
| 127 | /* Kernel tracer session creation */ |
| 128 | ret = kernctl_create_session(kernel_tracer_fd); |
| 129 | if (ret < 0) { |
| 130 | PERROR("ioctl kernel create session"); |
| 131 | goto error; |
| 132 | } |
| 133 | |
| 134 | lks->fd = ret; |
| 135 | /* Prevent fd duplication after execlp() */ |
| 136 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); |
| 137 | if (ret < 0) { |
| 138 | PERROR("fcntl session fd"); |
| 139 | } |
| 140 | |
| 141 | lks->id = session->id; |
| 142 | lks->consumer_fds_sent = 0; |
| 143 | session->kernel_session = lks; |
| 144 | |
| 145 | DBG("Kernel session created (fd: %d)", lks->fd); |
| 146 | |
| 147 | /* |
| 148 | * This is necessary since the creation time is present in the session |
| 149 | * name when it is generated. |
| 150 | */ |
| 151 | if (session->has_auto_generated_name) { |
| 152 | ret = kernctl_session_set_name(lks->fd, DEFAULT_SESSION_NAME); |
| 153 | } else { |
| 154 | ret = kernctl_session_set_name(lks->fd, session->name); |
| 155 | } |
| 156 | if (ret) { |
| 157 | WARN("Could not set kernel session name for session %" PRIu64 " name: %s", |
| 158 | session->id, |
| 159 | session->name); |
| 160 | } |
| 161 | |
| 162 | ret = kernctl_session_set_creation_time(lks->fd, session->creation_time); |
| 163 | if (ret) { |
| 164 | WARN("Could not set kernel session creation time for session %" PRIu64 " name: %s", |
| 165 | session->id, |
| 166 | session->name); |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | |
| 171 | error: |
| 172 | if (lks) { |
| 173 | trace_kernel_destroy_session(lks); |
| 174 | trace_kernel_free_session(lks); |
| 175 | } |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Create a kernel channel, register it to the kernel tracer and add it to the |
| 181 | * kernel session. |
| 182 | */ |
| 183 | int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_channel *chan) |
| 184 | { |
| 185 | int ret; |
| 186 | struct ltt_kernel_channel *lkc; |
| 187 | |
| 188 | LTTNG_ASSERT(session); |
| 189 | LTTNG_ASSERT(chan); |
| 190 | |
| 191 | /* Allocate kernel channel */ |
| 192 | lkc = trace_kernel_create_channel(chan); |
| 193 | if (lkc == nullptr) { |
| 194 | goto error; |
| 195 | } |
| 196 | |
| 197 | DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d", |
| 198 | chan->name, |
| 199 | lkc->channel->attr.overwrite, |
| 200 | lkc->channel->attr.subbuf_size, |
| 201 | lkc->channel->attr.num_subbuf, |
| 202 | lkc->channel->attr.switch_timer_interval, |
| 203 | lkc->channel->attr.read_timer_interval, |
| 204 | lkc->channel->attr.live_timer_interval, |
| 205 | lkc->channel->attr.output); |
| 206 | |
| 207 | /* Kernel tracer channel creation */ |
| 208 | ret = kernctl_create_channel(session->fd, &lkc->channel->attr); |
| 209 | if (ret < 0) { |
| 210 | PERROR("ioctl kernel create channel"); |
| 211 | goto error; |
| 212 | } |
| 213 | |
| 214 | /* Setup the channel fd */ |
| 215 | lkc->fd = ret; |
| 216 | /* Prevent fd duplication after execlp() */ |
| 217 | ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC); |
| 218 | if (ret < 0) { |
| 219 | PERROR("fcntl session fd"); |
| 220 | } |
| 221 | |
| 222 | /* Add channel to session */ |
| 223 | cds_list_add(&lkc->list, &session->channel_list.head); |
| 224 | session->channel_count++; |
| 225 | lkc->session = session; |
| 226 | lkc->key = ++next_kernel_channel_key; |
| 227 | |
| 228 | DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")", |
| 229 | lkc->channel->name, |
| 230 | lkc->fd, |
| 231 | lkc->key); |
| 232 | |
| 233 | return 0; |
| 234 | |
| 235 | error: |
| 236 | if (lkc) { |
| 237 | free(lkc->channel); |
| 238 | free(lkc); |
| 239 | } |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Create a kernel event notifier group, register it to the kernel tracer and |
| 245 | * add it to the kernel session. |
| 246 | */ |
| 247 | static int kernel_create_event_notifier_group(int *event_notifier_group_fd) |
| 248 | { |
| 249 | int ret; |
| 250 | int local_fd = -1; |
| 251 | |
| 252 | LTTNG_ASSERT(event_notifier_group_fd); |
| 253 | |
| 254 | /* Kernel event notifier group creation. */ |
| 255 | ret = kernctl_create_event_notifier_group(kernel_tracer_fd); |
| 256 | if (ret < 0) { |
| 257 | PERROR("Failed to create kernel event notifier group"); |
| 258 | ret = -1; |
| 259 | goto error; |
| 260 | } |
| 261 | |
| 262 | local_fd = ret; |
| 263 | |
| 264 | /* Prevent fd duplication after execlp(). */ |
| 265 | ret = fcntl(local_fd, F_SETFD, FD_CLOEXEC); |
| 266 | if (ret < 0) { |
| 267 | PERROR("Failed to set FD_CLOEXEC on kernel event notifier group file descriptor: fd = %d", |
| 268 | local_fd); |
| 269 | goto error; |
| 270 | } |
| 271 | |
| 272 | DBG("Created kernel event notifier group: fd = %d", local_fd); |
| 273 | *event_notifier_group_fd = local_fd; |
| 274 | local_fd = -1; |
| 275 | ret = 0; |
| 276 | error: |
| 277 | if (local_fd >= 0) { |
| 278 | ret = close(local_fd); |
| 279 | if (ret) { |
| 280 | PERROR("Failed to close kernel event notifier group file descriptor: fd = %d", |
| 281 | local_fd); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Compute the offset of the instrumentation byte in the binary based on the |
| 290 | * function probe location using the ELF lookup method. |
| 291 | * |
| 292 | * Returns 0 on success and set the offset out parameter to the offset of the |
| 293 | * elf symbol |
| 294 | * Returns -1 on error |
| 295 | */ |
| 296 | static int extract_userspace_probe_offset_function_elf( |
| 297 | const struct lttng_userspace_probe_location *probe_location, |
| 298 | uid_t uid, |
| 299 | gid_t gid, |
| 300 | uint64_t *offset) |
| 301 | { |
| 302 | int fd; |
| 303 | int ret = 0; |
| 304 | const char *symbol = nullptr; |
| 305 | const struct lttng_userspace_probe_location_lookup_method *lookup = nullptr; |
| 306 | enum lttng_userspace_probe_location_lookup_method_type lookup_method_type; |
| 307 | |
| 308 | LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location) == |
| 309 | LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION); |
| 310 | |
| 311 | lookup = lttng_userspace_probe_location_get_lookup_method(probe_location); |
| 312 | if (!lookup) { |
| 313 | ret = -1; |
| 314 | goto end; |
| 315 | } |
| 316 | |
| 317 | lookup_method_type = lttng_userspace_probe_location_lookup_method_get_type(lookup); |
| 318 | |
| 319 | LTTNG_ASSERT(lookup_method_type == |
| 320 | LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF); |
| 321 | |
| 322 | symbol = lttng_userspace_probe_location_function_get_function_name(probe_location); |
| 323 | if (!symbol) { |
| 324 | ret = -1; |
| 325 | goto end; |
| 326 | } |
| 327 | |
| 328 | fd = lttng_userspace_probe_location_function_get_binary_fd(probe_location); |
| 329 | if (fd < 0) { |
| 330 | ret = -1; |
| 331 | goto end; |
| 332 | } |
| 333 | |
| 334 | ret = run_as_extract_elf_symbol_offset(fd, symbol, uid, gid, offset); |
| 335 | if (ret < 0) { |
| 336 | DBG("userspace probe offset calculation failed for " |
| 337 | "function %s", |
| 338 | symbol); |
| 339 | goto end; |
| 340 | } |
| 341 | |
| 342 | DBG("userspace probe elf offset for %s is 0x%jd", symbol, (intmax_t) (*offset)); |
| 343 | end: |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Compute the offsets of the instrumentation bytes in the binary based on the |
| 349 | * tracepoint probe location using the SDT lookup method. This function |
| 350 | * allocates the offsets buffer, the caller must free it. |
| 351 | * |
| 352 | * Returns 0 on success and set the offset out parameter to the offsets of the |
| 353 | * SDT tracepoint. |
| 354 | * Returns -1 on error. |
| 355 | */ |
| 356 | static int extract_userspace_probe_offset_tracepoint_sdt( |
| 357 | const struct lttng_userspace_probe_location *probe_location, |
| 358 | uid_t uid, |
| 359 | gid_t gid, |
| 360 | uint64_t **offsets, |
| 361 | uint32_t *offsets_count) |
| 362 | { |
| 363 | enum lttng_userspace_probe_location_lookup_method_type lookup_method_type; |
| 364 | const struct lttng_userspace_probe_location_lookup_method *lookup = nullptr; |
| 365 | const char *probe_name = nullptr, *provider_name = nullptr; |
| 366 | int ret = 0; |
| 367 | int fd, i; |
| 368 | |
| 369 | LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location) == |
| 370 | LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT); |
| 371 | |
| 372 | lookup = lttng_userspace_probe_location_get_lookup_method(probe_location); |
| 373 | if (!lookup) { |
| 374 | ret = -1; |
| 375 | goto end; |
| 376 | } |
| 377 | |
| 378 | lookup_method_type = lttng_userspace_probe_location_lookup_method_get_type(lookup); |
| 379 | |
| 380 | LTTNG_ASSERT(lookup_method_type == |
| 381 | LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT); |
| 382 | |
| 383 | probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(probe_location); |
| 384 | if (!probe_name) { |
| 385 | ret = -1; |
| 386 | goto end; |
| 387 | } |
| 388 | |
| 389 | provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(probe_location); |
| 390 | if (!provider_name) { |
| 391 | ret = -1; |
| 392 | goto end; |
| 393 | } |
| 394 | |
| 395 | fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location); |
| 396 | if (fd < 0) { |
| 397 | ret = -1; |
| 398 | goto end; |
| 399 | } |
| 400 | |
| 401 | ret = run_as_extract_sdt_probe_offsets( |
| 402 | fd, provider_name, probe_name, uid, gid, offsets, offsets_count); |
| 403 | if (ret < 0) { |
| 404 | DBG("userspace probe offset calculation failed for sdt " |
| 405 | "probe %s:%s", |
| 406 | provider_name, |
| 407 | probe_name); |
| 408 | goto end; |
| 409 | } |
| 410 | |
| 411 | if (*offsets_count == 0) { |
| 412 | DBG("no userspace probe offset found"); |
| 413 | goto end; |
| 414 | } |
| 415 | |
| 416 | DBG("%u userspace probe SDT offsets found for %s:%s at:", |
| 417 | *offsets_count, |
| 418 | provider_name, |
| 419 | probe_name); |
| 420 | for (i = 0; i < *offsets_count; i++) { |
| 421 | DBG("\t0x%jd", (intmax_t) ((*offsets)[i])); |
| 422 | } |
| 423 | end: |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | static int userspace_probe_add_callsite(const struct lttng_userspace_probe_location *location, |
| 428 | uid_t uid, |
| 429 | gid_t gid, |
| 430 | int fd) |
| 431 | { |
| 432 | const struct lttng_userspace_probe_location_lookup_method *lookup_method = nullptr; |
| 433 | enum lttng_userspace_probe_location_lookup_method_type type; |
| 434 | int ret; |
| 435 | |
| 436 | lookup_method = lttng_userspace_probe_location_get_lookup_method(location); |
| 437 | if (!lookup_method) { |
| 438 | ret = -1; |
| 439 | goto end; |
| 440 | } |
| 441 | |
| 442 | type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method); |
| 443 | switch (type) { |
| 444 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: |
| 445 | { |
| 446 | struct lttng_kernel_abi_event_callsite callsite; |
| 447 | uint64_t offset; |
| 448 | |
| 449 | ret = extract_userspace_probe_offset_function_elf(location, uid, gid, &offset); |
| 450 | if (ret) { |
| 451 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 452 | goto end; |
| 453 | } |
| 454 | |
| 455 | callsite.u.uprobe.offset = offset; |
| 456 | ret = kernctl_add_callsite(fd, &callsite); |
| 457 | if (ret) { |
| 458 | WARN("Failed to add callsite to ELF userspace probe."); |
| 459 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 460 | goto end; |
| 461 | } |
| 462 | break; |
| 463 | } |
| 464 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: |
| 465 | { |
| 466 | int i; |
| 467 | uint64_t *offsets = nullptr; |
| 468 | uint32_t offsets_count; |
| 469 | struct lttng_kernel_abi_event_callsite callsite; |
| 470 | |
| 471 | /* |
| 472 | * This call allocates the offsets buffer. This buffer must be freed |
| 473 | * by the caller |
| 474 | */ |
| 475 | ret = extract_userspace_probe_offset_tracepoint_sdt( |
| 476 | location, uid, gid, &offsets, &offsets_count); |
| 477 | if (ret) { |
| 478 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 479 | goto end; |
| 480 | } |
| 481 | for (i = 0; i < offsets_count; i++) { |
| 482 | callsite.u.uprobe.offset = offsets[i]; |
| 483 | ret = kernctl_add_callsite(fd, &callsite); |
| 484 | if (ret) { |
| 485 | WARN("Failed to add callsite to SDT userspace probe"); |
| 486 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 487 | free(offsets); |
| 488 | goto end; |
| 489 | } |
| 490 | } |
| 491 | free(offsets); |
| 492 | break; |
| 493 | } |
| 494 | default: |
| 495 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 496 | goto end; |
| 497 | } |
| 498 | end: |
| 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Extract the offsets of the instrumentation point for the different lookup |
| 504 | * methods. |
| 505 | */ |
| 506 | static int userspace_probe_event_add_callsites(struct lttng_event *ev, |
| 507 | struct ltt_kernel_session *session, |
| 508 | int fd) |
| 509 | { |
| 510 | int ret; |
| 511 | const struct lttng_userspace_probe_location *location = nullptr; |
| 512 | |
| 513 | LTTNG_ASSERT(ev); |
| 514 | LTTNG_ASSERT(ev->type == LTTNG_EVENT_USERSPACE_PROBE); |
| 515 | |
| 516 | location = lttng_event_get_userspace_probe_location(ev); |
| 517 | if (!location) { |
| 518 | ret = -1; |
| 519 | goto end; |
| 520 | } |
| 521 | |
| 522 | ret = userspace_probe_add_callsite(location, session->uid, session->gid, fd); |
| 523 | if (ret) { |
| 524 | WARN("Failed to add callsite to userspace probe event '%s'", ev->name); |
| 525 | } |
| 526 | |
| 527 | end: |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Extract the offsets of the instrumentation point for the different look-up |
| 533 | * methods. |
| 534 | */ |
| 535 | static int userspace_probe_event_rule_add_callsites(const struct lttng_event_rule *rule, |
| 536 | const struct lttng_credentials *creds, |
| 537 | int fd) |
| 538 | { |
| 539 | int ret; |
| 540 | enum lttng_event_rule_status status; |
| 541 | enum lttng_event_rule_type event_rule_type; |
| 542 | const struct lttng_userspace_probe_location *location = nullptr; |
| 543 | |
| 544 | LTTNG_ASSERT(rule); |
| 545 | LTTNG_ASSERT(creds); |
| 546 | |
| 547 | event_rule_type = lttng_event_rule_get_type(rule); |
| 548 | LTTNG_ASSERT(event_rule_type == LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE); |
| 549 | |
| 550 | status = lttng_event_rule_kernel_uprobe_get_location(rule, &location); |
| 551 | if (status != LTTNG_EVENT_RULE_STATUS_OK || !location) { |
| 552 | ret = -1; |
| 553 | goto end; |
| 554 | } |
| 555 | |
| 556 | ret = userspace_probe_add_callsite( |
| 557 | location, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds), fd); |
| 558 | if (ret) { |
| 559 | WARN("Failed to add callsite to user space probe object: fd = %d", fd); |
| 560 | } |
| 561 | |
| 562 | end: |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Create a kernel event, enable it to the kernel tracer and add it to the |
| 568 | * channel event list of the kernel session. |
| 569 | * We own filter_expression and filter. |
| 570 | */ |
| 571 | int kernel_create_event(struct lttng_event *ev, |
| 572 | struct ltt_kernel_channel *channel, |
| 573 | char *filter_expression, |
| 574 | struct lttng_bytecode *filter) |
| 575 | { |
| 576 | int err, fd; |
| 577 | enum lttng_error_code ret; |
| 578 | struct ltt_kernel_event *event; |
| 579 | |
| 580 | LTTNG_ASSERT(ev); |
| 581 | LTTNG_ASSERT(channel); |
| 582 | |
| 583 | /* We pass ownership of filter_expression and filter */ |
| 584 | ret = trace_kernel_create_event(ev, filter_expression, filter, &event); |
| 585 | if (ret != LTTNG_OK) { |
| 586 | goto error; |
| 587 | } |
| 588 | |
| 589 | fd = kernctl_create_event(channel->fd, event->event); |
| 590 | if (fd < 0) { |
| 591 | switch (-fd) { |
| 592 | case EEXIST: |
| 593 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
| 594 | break; |
| 595 | case ENOSYS: |
| 596 | WARN("Event type not implemented"); |
| 597 | ret = LTTNG_ERR_KERN_EVENT_ENOSYS; |
| 598 | break; |
| 599 | case ENOENT: |
| 600 | WARN("Event %s not found!", ev->name); |
| 601 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 602 | break; |
| 603 | default: |
| 604 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 605 | PERROR("create event ioctl"); |
| 606 | } |
| 607 | goto free_event; |
| 608 | } |
| 609 | |
| 610 | event->type = ev->type; |
| 611 | event->fd = fd; |
| 612 | /* Prevent fd duplication after execlp() */ |
| 613 | err = fcntl(event->fd, F_SETFD, FD_CLOEXEC); |
| 614 | if (err < 0) { |
| 615 | PERROR("fcntl session fd"); |
| 616 | } |
| 617 | |
| 618 | if (filter) { |
| 619 | err = kernctl_filter(event->fd, filter); |
| 620 | if (err < 0) { |
| 621 | switch (-err) { |
| 622 | case ENOMEM: |
| 623 | ret = LTTNG_ERR_FILTER_NOMEM; |
| 624 | break; |
| 625 | default: |
| 626 | ret = LTTNG_ERR_FILTER_INVAL; |
| 627 | break; |
| 628 | } |
| 629 | goto filter_error; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | if (ev->type == LTTNG_EVENT_USERSPACE_PROBE) { |
| 634 | ret = (lttng_error_code) userspace_probe_event_add_callsites( |
| 635 | ev, channel->session, event->fd); |
| 636 | if (ret) { |
| 637 | goto add_callsite_error; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | err = kernctl_enable(event->fd); |
| 642 | if (err < 0) { |
| 643 | switch (-err) { |
| 644 | case EEXIST: |
| 645 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
| 646 | break; |
| 647 | default: |
| 648 | PERROR("enable kernel event"); |
| 649 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 650 | break; |
| 651 | } |
| 652 | goto enable_error; |
| 653 | } |
| 654 | |
| 655 | /* Add event to event list */ |
| 656 | cds_list_add(&event->list, &channel->events_list.head); |
| 657 | channel->event_count++; |
| 658 | |
| 659 | DBG("Event %s created (fd: %d)", ev->name, event->fd); |
| 660 | |
| 661 | return 0; |
| 662 | |
| 663 | add_callsite_error: |
| 664 | enable_error: |
| 665 | filter_error: |
| 666 | { |
| 667 | int closeret; |
| 668 | |
| 669 | closeret = close(event->fd); |
| 670 | if (closeret) { |
| 671 | PERROR("close event fd"); |
| 672 | } |
| 673 | } |
| 674 | free_event: |
| 675 | free(event); |
| 676 | error: |
| 677 | return ret; |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * Disable a kernel channel. |
| 682 | */ |
| 683 | int kernel_disable_channel(struct ltt_kernel_channel *chan) |
| 684 | { |
| 685 | int ret; |
| 686 | |
| 687 | LTTNG_ASSERT(chan); |
| 688 | |
| 689 | ret = kernctl_disable(chan->fd); |
| 690 | if (ret < 0) { |
| 691 | PERROR("disable chan ioctl"); |
| 692 | goto error; |
| 693 | } |
| 694 | |
| 695 | chan->enabled = false; |
| 696 | DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")", |
| 697 | chan->channel->name, |
| 698 | chan->fd, |
| 699 | chan->key); |
| 700 | |
| 701 | return 0; |
| 702 | |
| 703 | error: |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Enable a kernel channel. |
| 709 | */ |
| 710 | int kernel_enable_channel(struct ltt_kernel_channel *chan) |
| 711 | { |
| 712 | int ret; |
| 713 | |
| 714 | LTTNG_ASSERT(chan); |
| 715 | |
| 716 | ret = kernctl_enable(chan->fd); |
| 717 | if (ret < 0 && ret != -EEXIST) { |
| 718 | PERROR("Enable kernel chan"); |
| 719 | goto error; |
| 720 | } |
| 721 | |
| 722 | chan->enabled = true; |
| 723 | DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")", |
| 724 | chan->channel->name, |
| 725 | chan->fd, |
| 726 | chan->key); |
| 727 | |
| 728 | return 0; |
| 729 | |
| 730 | error: |
| 731 | return ret; |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Enable a kernel event. |
| 736 | */ |
| 737 | int kernel_enable_event(struct ltt_kernel_event *event) |
| 738 | { |
| 739 | int ret; |
| 740 | |
| 741 | LTTNG_ASSERT(event); |
| 742 | |
| 743 | ret = kernctl_enable(event->fd); |
| 744 | if (ret < 0) { |
| 745 | switch (-ret) { |
| 746 | case EEXIST: |
| 747 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
| 748 | break; |
| 749 | default: |
| 750 | PERROR("enable kernel event"); |
| 751 | break; |
| 752 | } |
| 753 | goto error; |
| 754 | } |
| 755 | |
| 756 | event->enabled = true; |
| 757 | DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd); |
| 758 | |
| 759 | return 0; |
| 760 | |
| 761 | error: |
| 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * Disable a kernel event. |
| 767 | */ |
| 768 | int kernel_disable_event(struct ltt_kernel_event *event) |
| 769 | { |
| 770 | int ret; |
| 771 | |
| 772 | LTTNG_ASSERT(event); |
| 773 | |
| 774 | ret = kernctl_disable(event->fd); |
| 775 | if (ret < 0) { |
| 776 | PERROR("Failed to disable kernel event: name = '%s', fd = %d", |
| 777 | event->event->name, |
| 778 | event->fd); |
| 779 | goto error; |
| 780 | } |
| 781 | |
| 782 | event->enabled = false; |
| 783 | DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd); |
| 784 | |
| 785 | return 0; |
| 786 | |
| 787 | error: |
| 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * Disable a kernel event notifier. |
| 793 | */ |
| 794 | static int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_rule *event) |
| 795 | { |
| 796 | int ret; |
| 797 | |
| 798 | LTTNG_ASSERT(event); |
| 799 | |
| 800 | lttng::urcu::read_lock_guard read_lock; |
| 801 | cds_lfht_del(kernel_token_to_event_notifier_rule_ht, &event->ht_node); |
| 802 | |
| 803 | ret = kernctl_disable(event->fd); |
| 804 | if (ret < 0) { |
| 805 | PERROR("Failed to disable kernel event notifier: fd = %d, token = %" PRIu64, |
| 806 | event->fd, |
| 807 | event->token); |
| 808 | goto error; |
| 809 | } |
| 810 | |
| 811 | event->enabled = false; |
| 812 | DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64, event->fd, event->token); |
| 813 | |
| 814 | error: |
| 815 | return ret; |
| 816 | } |
| 817 | |
| 818 | static struct process_attr_tracker * |
| 819 | _kernel_get_process_attr_tracker(struct ltt_kernel_session *session, |
| 820 | enum lttng_process_attr process_attr) |
| 821 | { |
| 822 | switch (process_attr) { |
| 823 | case LTTNG_PROCESS_ATTR_PROCESS_ID: |
| 824 | return session->tracker_pid; |
| 825 | case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID: |
| 826 | return session->tracker_vpid; |
| 827 | case LTTNG_PROCESS_ATTR_USER_ID: |
| 828 | return session->tracker_uid; |
| 829 | case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID: |
| 830 | return session->tracker_vuid; |
| 831 | case LTTNG_PROCESS_ATTR_GROUP_ID: |
| 832 | return session->tracker_gid; |
| 833 | case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID: |
| 834 | return session->tracker_vgid; |
| 835 | default: |
| 836 | return nullptr; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | const struct process_attr_tracker * |
| 841 | kernel_get_process_attr_tracker(struct ltt_kernel_session *session, |
| 842 | enum lttng_process_attr process_attr) |
| 843 | { |
| 844 | return (const struct process_attr_tracker *) _kernel_get_process_attr_tracker(session, |
| 845 | process_attr); |
| 846 | } |
| 847 | |
| 848 | enum lttng_error_code |
| 849 | kernel_process_attr_tracker_set_tracking_policy(struct ltt_kernel_session *session, |
| 850 | enum lttng_process_attr process_attr, |
| 851 | enum lttng_tracking_policy policy) |
| 852 | { |
| 853 | int ret; |
| 854 | enum lttng_error_code ret_code = LTTNG_OK; |
| 855 | struct process_attr_tracker *tracker = |
| 856 | _kernel_get_process_attr_tracker(session, process_attr); |
| 857 | enum lttng_tracking_policy previous_policy; |
| 858 | |
| 859 | if (!tracker) { |
| 860 | ret_code = LTTNG_ERR_INVALID; |
| 861 | goto end; |
| 862 | } |
| 863 | |
| 864 | previous_policy = process_attr_tracker_get_tracking_policy(tracker); |
| 865 | ret = process_attr_tracker_set_tracking_policy(tracker, policy); |
| 866 | if (ret) { |
| 867 | ret_code = LTTNG_ERR_UNK; |
| 868 | goto end; |
| 869 | } |
| 870 | |
| 871 | if (previous_policy == policy) { |
| 872 | goto end; |
| 873 | } |
| 874 | |
| 875 | switch (policy) { |
| 876 | case LTTNG_TRACKING_POLICY_INCLUDE_ALL: |
| 877 | if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) { |
| 878 | /* |
| 879 | * Maintain a special case for the process ID process |
| 880 | * attribute tracker as it was the only supported |
| 881 | * attribute prior to 2.12. |
| 882 | */ |
| 883 | ret = kernctl_track_pid(session->fd, -1); |
| 884 | } else { |
| 885 | ret = kernctl_track_id(session->fd, process_attr, -1); |
| 886 | } |
| 887 | break; |
| 888 | case LTTNG_TRACKING_POLICY_EXCLUDE_ALL: |
| 889 | case LTTNG_TRACKING_POLICY_INCLUDE_SET: |
| 890 | /* fall-through. */ |
| 891 | if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) { |
| 892 | /* |
| 893 | * Maintain a special case for the process ID process |
| 894 | * attribute tracker as it was the only supported |
| 895 | * attribute prior to 2.12. |
| 896 | */ |
| 897 | ret = kernctl_untrack_pid(session->fd, -1); |
| 898 | } else { |
| 899 | ret = kernctl_untrack_id(session->fd, process_attr, -1); |
| 900 | } |
| 901 | break; |
| 902 | default: |
| 903 | abort(); |
| 904 | } |
| 905 | /* kern-ctl error handling */ |
| 906 | switch (-ret) { |
| 907 | case 0: |
| 908 | ret_code = LTTNG_OK; |
| 909 | break; |
| 910 | case EINVAL: |
| 911 | ret_code = LTTNG_ERR_INVALID; |
| 912 | break; |
| 913 | case ENOMEM: |
| 914 | ret_code = LTTNG_ERR_NOMEM; |
| 915 | break; |
| 916 | case EEXIST: |
| 917 | ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS; |
| 918 | break; |
| 919 | default: |
| 920 | ret_code = LTTNG_ERR_UNK; |
| 921 | break; |
| 922 | } |
| 923 | end: |
| 924 | return ret_code; |
| 925 | } |
| 926 | |
| 927 | enum lttng_error_code |
| 928 | kernel_process_attr_tracker_inclusion_set_add_value(struct ltt_kernel_session *session, |
| 929 | enum lttng_process_attr process_attr, |
| 930 | const struct process_attr_value *value) |
| 931 | { |
| 932 | int ret, integral_value; |
| 933 | enum lttng_error_code ret_code; |
| 934 | struct process_attr_tracker *tracker; |
| 935 | enum process_attr_tracker_status status; |
| 936 | |
| 937 | /* |
| 938 | * Convert process attribute tracker value to the integral |
| 939 | * representation required by the kern-ctl API. |
| 940 | */ |
| 941 | switch (process_attr) { |
| 942 | case LTTNG_PROCESS_ATTR_PROCESS_ID: |
| 943 | case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID: |
| 944 | integral_value = (int) value->value.pid; |
| 945 | break; |
| 946 | case LTTNG_PROCESS_ATTR_USER_ID: |
| 947 | case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID: |
| 948 | if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) { |
| 949 | uid_t uid; |
| 950 | |
| 951 | ret_code = utils_user_id_from_name(value->value.user_name, &uid); |
| 952 | if (ret_code != LTTNG_OK) { |
| 953 | goto end; |
| 954 | } |
| 955 | integral_value = (int) uid; |
| 956 | } else { |
| 957 | integral_value = (int) value->value.uid; |
| 958 | } |
| 959 | break; |
| 960 | case LTTNG_PROCESS_ATTR_GROUP_ID: |
| 961 | case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID: |
| 962 | if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) { |
| 963 | gid_t gid; |
| 964 | |
| 965 | ret_code = utils_group_id_from_name(value->value.group_name, &gid); |
| 966 | if (ret_code != LTTNG_OK) { |
| 967 | goto end; |
| 968 | } |
| 969 | integral_value = (int) gid; |
| 970 | } else { |
| 971 | integral_value = (int) value->value.gid; |
| 972 | } |
| 973 | break; |
| 974 | default: |
| 975 | ret_code = LTTNG_ERR_INVALID; |
| 976 | goto end; |
| 977 | } |
| 978 | |
| 979 | tracker = _kernel_get_process_attr_tracker(session, process_attr); |
| 980 | if (!tracker) { |
| 981 | ret_code = LTTNG_ERR_INVALID; |
| 982 | goto end; |
| 983 | } |
| 984 | |
| 985 | status = process_attr_tracker_inclusion_set_add_value(tracker, value); |
| 986 | if (status != PROCESS_ATTR_TRACKER_STATUS_OK) { |
| 987 | switch (status) { |
| 988 | case PROCESS_ATTR_TRACKER_STATUS_EXISTS: |
| 989 | ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS; |
| 990 | break; |
| 991 | case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY: |
| 992 | ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY; |
| 993 | break; |
| 994 | case PROCESS_ATTR_TRACKER_STATUS_ERROR: |
| 995 | default: |
| 996 | ret_code = LTTNG_ERR_UNK; |
| 997 | break; |
| 998 | } |
| 999 | goto end; |
| 1000 | } |
| 1001 | |
| 1002 | DBG("Kernel track %s %d for session id %" PRIu64, |
| 1003 | lttng_process_attr_to_string(process_attr), |
| 1004 | integral_value, |
| 1005 | session->id); |
| 1006 | if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) { |
| 1007 | /* |
| 1008 | * Maintain a special case for the process ID process attribute |
| 1009 | * tracker as it was the only supported attribute prior to 2.12. |
| 1010 | */ |
| 1011 | ret = kernctl_track_pid(session->fd, integral_value); |
| 1012 | } else { |
| 1013 | ret = kernctl_track_id(session->fd, process_attr, integral_value); |
| 1014 | } |
| 1015 | if (ret == 0) { |
| 1016 | ret_code = LTTNG_OK; |
| 1017 | goto end; |
| 1018 | } |
| 1019 | |
| 1020 | kernel_wait_quiescent(); |
| 1021 | |
| 1022 | /* kern-ctl error handling */ |
| 1023 | switch (-ret) { |
| 1024 | case 0: |
| 1025 | ret_code = LTTNG_OK; |
| 1026 | break; |
| 1027 | case EINVAL: |
| 1028 | ret_code = LTTNG_ERR_INVALID; |
| 1029 | break; |
| 1030 | case ENOMEM: |
| 1031 | ret_code = LTTNG_ERR_NOMEM; |
| 1032 | break; |
| 1033 | case EEXIST: |
| 1034 | ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS; |
| 1035 | break; |
| 1036 | default: |
| 1037 | ret_code = LTTNG_ERR_UNK; |
| 1038 | break; |
| 1039 | } |
| 1040 | |
| 1041 | /* Attempt to remove the value from the tracker. */ |
| 1042 | status = process_attr_tracker_inclusion_set_remove_value(tracker, value); |
| 1043 | if (status != PROCESS_ATTR_TRACKER_STATUS_OK) { |
| 1044 | ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error", |
| 1045 | lttng_process_attr_to_string(process_attr), |
| 1046 | integral_value); |
| 1047 | } |
| 1048 | end: |
| 1049 | return ret_code; |
| 1050 | } |
| 1051 | |
| 1052 | enum lttng_error_code |
| 1053 | kernel_process_attr_tracker_inclusion_set_remove_value(struct ltt_kernel_session *session, |
| 1054 | enum lttng_process_attr process_attr, |
| 1055 | const struct process_attr_value *value) |
| 1056 | { |
| 1057 | int ret, integral_value; |
| 1058 | enum lttng_error_code ret_code; |
| 1059 | struct process_attr_tracker *tracker; |
| 1060 | enum process_attr_tracker_status status; |
| 1061 | |
| 1062 | /* |
| 1063 | * Convert process attribute tracker value to the integral |
| 1064 | * representation required by the kern-ctl API. |
| 1065 | */ |
| 1066 | switch (process_attr) { |
| 1067 | case LTTNG_PROCESS_ATTR_PROCESS_ID: |
| 1068 | case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID: |
| 1069 | integral_value = (int) value->value.pid; |
| 1070 | break; |
| 1071 | case LTTNG_PROCESS_ATTR_USER_ID: |
| 1072 | case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID: |
| 1073 | if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) { |
| 1074 | uid_t uid; |
| 1075 | |
| 1076 | ret_code = utils_user_id_from_name(value->value.user_name, &uid); |
| 1077 | if (ret_code != LTTNG_OK) { |
| 1078 | goto end; |
| 1079 | } |
| 1080 | integral_value = (int) uid; |
| 1081 | } else { |
| 1082 | integral_value = (int) value->value.uid; |
| 1083 | } |
| 1084 | break; |
| 1085 | case LTTNG_PROCESS_ATTR_GROUP_ID: |
| 1086 | case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID: |
| 1087 | if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) { |
| 1088 | gid_t gid; |
| 1089 | |
| 1090 | ret_code = utils_group_id_from_name(value->value.group_name, &gid); |
| 1091 | if (ret_code != LTTNG_OK) { |
| 1092 | goto end; |
| 1093 | } |
| 1094 | integral_value = (int) gid; |
| 1095 | } else { |
| 1096 | integral_value = (int) value->value.gid; |
| 1097 | } |
| 1098 | break; |
| 1099 | default: |
| 1100 | ret_code = LTTNG_ERR_INVALID; |
| 1101 | goto end; |
| 1102 | } |
| 1103 | |
| 1104 | tracker = _kernel_get_process_attr_tracker(session, process_attr); |
| 1105 | if (!tracker) { |
| 1106 | ret_code = LTTNG_ERR_INVALID; |
| 1107 | goto end; |
| 1108 | } |
| 1109 | |
| 1110 | status = process_attr_tracker_inclusion_set_remove_value(tracker, value); |
| 1111 | if (status != PROCESS_ATTR_TRACKER_STATUS_OK) { |
| 1112 | switch (status) { |
| 1113 | case PROCESS_ATTR_TRACKER_STATUS_MISSING: |
| 1114 | ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING; |
| 1115 | break; |
| 1116 | case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY: |
| 1117 | ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY; |
| 1118 | break; |
| 1119 | case PROCESS_ATTR_TRACKER_STATUS_ERROR: |
| 1120 | default: |
| 1121 | ret_code = LTTNG_ERR_UNK; |
| 1122 | break; |
| 1123 | } |
| 1124 | goto end; |
| 1125 | } |
| 1126 | |
| 1127 | DBG("Kernel track %s %d for session id %" PRIu64, |
| 1128 | lttng_process_attr_to_string(process_attr), |
| 1129 | integral_value, |
| 1130 | session->id); |
| 1131 | if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) { |
| 1132 | /* |
| 1133 | * Maintain a special case for the process ID process attribute |
| 1134 | * tracker as it was the only supported attribute prior to 2.12. |
| 1135 | */ |
| 1136 | ret = kernctl_untrack_pid(session->fd, integral_value); |
| 1137 | } else { |
| 1138 | ret = kernctl_untrack_id(session->fd, process_attr, integral_value); |
| 1139 | } |
| 1140 | if (ret == 0) { |
| 1141 | ret_code = LTTNG_OK; |
| 1142 | goto end; |
| 1143 | } |
| 1144 | kernel_wait_quiescent(); |
| 1145 | |
| 1146 | /* kern-ctl error handling */ |
| 1147 | switch (-ret) { |
| 1148 | case 0: |
| 1149 | ret_code = LTTNG_OK; |
| 1150 | break; |
| 1151 | case EINVAL: |
| 1152 | ret_code = LTTNG_ERR_INVALID; |
| 1153 | break; |
| 1154 | case ENOMEM: |
| 1155 | ret_code = LTTNG_ERR_NOMEM; |
| 1156 | break; |
| 1157 | case ENOENT: |
| 1158 | ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING; |
| 1159 | break; |
| 1160 | default: |
| 1161 | ret_code = LTTNG_ERR_UNK; |
| 1162 | break; |
| 1163 | } |
| 1164 | |
| 1165 | /* Attempt to add the value to the tracker. */ |
| 1166 | status = process_attr_tracker_inclusion_set_add_value(tracker, value); |
| 1167 | if (status != PROCESS_ATTR_TRACKER_STATUS_OK) { |
| 1168 | ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error", |
| 1169 | lttng_process_attr_to_string(process_attr), |
| 1170 | integral_value); |
| 1171 | } |
| 1172 | end: |
| 1173 | return ret_code; |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | * Create kernel metadata, open from the kernel tracer and add it to the |
| 1178 | * kernel session. |
| 1179 | */ |
| 1180 | int kernel_open_metadata(struct ltt_kernel_session *session) |
| 1181 | { |
| 1182 | int ret; |
| 1183 | struct ltt_kernel_metadata *lkm = nullptr; |
| 1184 | |
| 1185 | LTTNG_ASSERT(session); |
| 1186 | |
| 1187 | /* Allocate kernel metadata */ |
| 1188 | lkm = trace_kernel_create_metadata(); |
| 1189 | if (lkm == nullptr) { |
| 1190 | goto error; |
| 1191 | } |
| 1192 | |
| 1193 | /* Kernel tracer metadata creation */ |
| 1194 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
| 1195 | if (ret < 0) { |
| 1196 | goto error_open; |
| 1197 | } |
| 1198 | |
| 1199 | lkm->fd = ret; |
| 1200 | lkm->key = ++next_kernel_channel_key; |
| 1201 | /* Prevent fd duplication after execlp() */ |
| 1202 | ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); |
| 1203 | if (ret < 0) { |
| 1204 | PERROR("fcntl session fd"); |
| 1205 | } |
| 1206 | |
| 1207 | session->metadata = lkm; |
| 1208 | |
| 1209 | DBG("Kernel metadata opened (fd: %d)", lkm->fd); |
| 1210 | |
| 1211 | return 0; |
| 1212 | |
| 1213 | error_open: |
| 1214 | trace_kernel_destroy_metadata(lkm); |
| 1215 | error: |
| 1216 | return -1; |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * Start tracing session. |
| 1221 | */ |
| 1222 | int kernel_start_session(struct ltt_kernel_session *session) |
| 1223 | { |
| 1224 | int ret; |
| 1225 | |
| 1226 | LTTNG_ASSERT(session); |
| 1227 | |
| 1228 | ret = kernctl_start_session(session->fd); |
| 1229 | if (ret < 0) { |
| 1230 | PERROR("ioctl start session"); |
| 1231 | goto error; |
| 1232 | } |
| 1233 | |
| 1234 | DBG("Kernel session started"); |
| 1235 | |
| 1236 | return 0; |
| 1237 | |
| 1238 | error: |
| 1239 | return ret; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | * Make a kernel wait to make sure in-flight probe have completed. |
| 1244 | */ |
| 1245 | void kernel_wait_quiescent() |
| 1246 | { |
| 1247 | int ret; |
| 1248 | int fd = kernel_tracer_fd; |
| 1249 | |
| 1250 | DBG("Kernel quiescent wait on %d", fd); |
| 1251 | |
| 1252 | ret = kernctl_wait_quiescent(fd); |
| 1253 | if (ret < 0) { |
| 1254 | PERROR("wait quiescent ioctl"); |
| 1255 | ERR("Kernel quiescent wait failed"); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | * Force flush buffer of metadata. |
| 1261 | */ |
| 1262 | int kernel_metadata_flush_buffer(int fd) |
| 1263 | { |
| 1264 | int ret; |
| 1265 | |
| 1266 | DBG("Kernel flushing metadata buffer on fd %d", fd); |
| 1267 | |
| 1268 | ret = kernctl_buffer_flush(fd); |
| 1269 | if (ret < 0) { |
| 1270 | ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret); |
| 1271 | } |
| 1272 | |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * Force flush buffer for channel. |
| 1278 | */ |
| 1279 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) |
| 1280 | { |
| 1281 | int ret; |
| 1282 | struct ltt_kernel_stream *stream; |
| 1283 | |
| 1284 | LTTNG_ASSERT(channel); |
| 1285 | |
| 1286 | DBG("Flush buffer for channel %s", channel->channel->name); |
| 1287 | |
| 1288 | cds_list_for_each_entry (stream, &channel->stream_list.head, list) { |
| 1289 | DBG("Flushing channel stream %d", stream->fd); |
| 1290 | ret = kernctl_buffer_flush(stream->fd); |
| 1291 | if (ret < 0) { |
| 1292 | PERROR("ioctl"); |
| 1293 | ERR("Fail to flush buffer for stream %d (ret: %d)", stream->fd, ret); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * Stop tracing session. |
| 1302 | */ |
| 1303 | int kernel_stop_session(struct ltt_kernel_session *session) |
| 1304 | { |
| 1305 | int ret; |
| 1306 | |
| 1307 | LTTNG_ASSERT(session); |
| 1308 | |
| 1309 | ret = kernctl_stop_session(session->fd); |
| 1310 | if (ret < 0) { |
| 1311 | goto error; |
| 1312 | } |
| 1313 | |
| 1314 | DBG("Kernel session stopped"); |
| 1315 | |
| 1316 | return 0; |
| 1317 | |
| 1318 | error: |
| 1319 | return ret; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | * Open stream of channel, register it to the kernel tracer and add it |
| 1324 | * to the stream list of the channel. |
| 1325 | * |
| 1326 | * Note: given that the streams may appear in random order wrt CPU |
| 1327 | * number (e.g. cpu hotplug), the index value of the stream number in |
| 1328 | * the stream name is not necessarily linked to the CPU number. |
| 1329 | * |
| 1330 | * Return the number of created stream. Else, a negative value. |
| 1331 | */ |
| 1332 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
| 1333 | { |
| 1334 | int ret; |
| 1335 | struct ltt_kernel_stream *lks; |
| 1336 | |
| 1337 | LTTNG_ASSERT(channel); |
| 1338 | |
| 1339 | while ((ret = kernctl_create_stream(channel->fd)) >= 0) { |
| 1340 | lks = trace_kernel_create_stream(channel->channel->name, channel->stream_count); |
| 1341 | if (lks == nullptr) { |
| 1342 | ret = close(ret); |
| 1343 | if (ret) { |
| 1344 | PERROR("close"); |
| 1345 | } |
| 1346 | goto error; |
| 1347 | } |
| 1348 | |
| 1349 | lks->fd = ret; |
| 1350 | /* Prevent fd duplication after execlp() */ |
| 1351 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); |
| 1352 | if (ret < 0) { |
| 1353 | PERROR("fcntl session fd"); |
| 1354 | } |
| 1355 | |
| 1356 | lks->tracefile_size = channel->channel->attr.tracefile_size; |
| 1357 | lks->tracefile_count = channel->channel->attr.tracefile_count; |
| 1358 | |
| 1359 | /* Add stream to channel stream list */ |
| 1360 | cds_list_add(&lks->list, &channel->stream_list.head); |
| 1361 | channel->stream_count++; |
| 1362 | |
| 1363 | DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd, lks->state); |
| 1364 | } |
| 1365 | |
| 1366 | return channel->stream_count; |
| 1367 | |
| 1368 | error: |
| 1369 | return -1; |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * Open the metadata stream and set it to the kernel session. |
| 1374 | */ |
| 1375 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
| 1376 | { |
| 1377 | int ret; |
| 1378 | |
| 1379 | LTTNG_ASSERT(session); |
| 1380 | |
| 1381 | ret = kernctl_create_stream(session->metadata->fd); |
| 1382 | if (ret < 0) { |
| 1383 | PERROR("kernel create metadata stream"); |
| 1384 | goto error; |
| 1385 | } |
| 1386 | |
| 1387 | DBG("Kernel metadata stream created (fd: %d)", ret); |
| 1388 | session->metadata_stream_fd = ret; |
| 1389 | /* Prevent fd duplication after execlp() */ |
| 1390 | ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); |
| 1391 | if (ret < 0) { |
| 1392 | PERROR("fcntl session fd"); |
| 1393 | } |
| 1394 | |
| 1395 | return 0; |
| 1396 | |
| 1397 | error: |
| 1398 | return -1; |
| 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * Get the event list from the kernel tracer and return the number of elements. |
| 1403 | */ |
| 1404 | ssize_t kernel_list_events(struct lttng_event **events) |
| 1405 | { |
| 1406 | int fd, ret; |
| 1407 | char *event; |
| 1408 | size_t nbmem, count = 0; |
| 1409 | FILE *fp; |
| 1410 | struct lttng_event *elist; |
| 1411 | |
| 1412 | LTTNG_ASSERT(events); |
| 1413 | |
| 1414 | fd = kernctl_tracepoint_list(kernel_tracer_fd); |
| 1415 | if (fd < 0) { |
| 1416 | PERROR("kernel tracepoint list"); |
| 1417 | goto error; |
| 1418 | } |
| 1419 | |
| 1420 | fp = fdopen(fd, "r"); |
| 1421 | if (fp == nullptr) { |
| 1422 | PERROR("kernel tracepoint list fdopen"); |
| 1423 | goto error_fp; |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | * Init memory size counter |
| 1428 | * See kernel-ctl.h for explanation of this value |
| 1429 | */ |
| 1430 | nbmem = KERNEL_EVENT_INIT_LIST_SIZE; |
| 1431 | elist = calloc<lttng_event>(nbmem); |
| 1432 | if (elist == nullptr) { |
| 1433 | PERROR("alloc list events"); |
| 1434 | count = -ENOMEM; |
| 1435 | goto end; |
| 1436 | } |
| 1437 | |
| 1438 | while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) { |
| 1439 | if (count >= nbmem) { |
| 1440 | struct lttng_event *new_elist; |
| 1441 | size_t new_nbmem; |
| 1442 | |
| 1443 | new_nbmem = nbmem << 1; |
| 1444 | DBG("Reallocating event list from %zu to %zu bytes", nbmem, new_nbmem); |
| 1445 | new_elist = (lttng_event *) realloc(elist, |
| 1446 | new_nbmem * sizeof(struct lttng_event)); |
| 1447 | if (new_elist == nullptr) { |
| 1448 | PERROR("realloc list events"); |
| 1449 | free(event); |
| 1450 | free(elist); |
| 1451 | count = -ENOMEM; |
| 1452 | goto end; |
| 1453 | } |
| 1454 | /* Zero the new memory */ |
| 1455 | memset(new_elist + nbmem, |
| 1456 | 0, |
| 1457 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); |
| 1458 | nbmem = new_nbmem; |
| 1459 | elist = new_elist; |
| 1460 | } |
| 1461 | strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN); |
| 1462 | elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 1463 | elist[count].enabled = -1; |
| 1464 | count++; |
| 1465 | free(event); |
| 1466 | } |
| 1467 | |
| 1468 | *events = elist; |
| 1469 | DBG("Kernel list events done (%zu events)", count); |
| 1470 | end: |
| 1471 | ret = fclose(fp); /* closes both fp and fd */ |
| 1472 | if (ret) { |
| 1473 | PERROR("fclose"); |
| 1474 | } |
| 1475 | return count; |
| 1476 | |
| 1477 | error_fp: |
| 1478 | ret = close(fd); |
| 1479 | if (ret) { |
| 1480 | PERROR("close"); |
| 1481 | } |
| 1482 | error: |
| 1483 | return -1; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * Get kernel version and validate it. |
| 1488 | */ |
| 1489 | int kernel_validate_version(struct lttng_kernel_abi_tracer_version *version, |
| 1490 | struct lttng_kernel_abi_tracer_abi_version *abi_version) |
| 1491 | { |
| 1492 | int ret; |
| 1493 | |
| 1494 | ret = kernctl_tracer_version(kernel_tracer_fd, version); |
| 1495 | if (ret < 0) { |
| 1496 | ERR("Failed to retrieve the lttng-modules version"); |
| 1497 | goto error; |
| 1498 | } |
| 1499 | |
| 1500 | /* Validate version */ |
| 1501 | if (version->major != VERSION_MAJOR) { |
| 1502 | ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)", |
| 1503 | version->major, |
| 1504 | VERSION_MAJOR); |
| 1505 | goto error_version; |
| 1506 | } |
| 1507 | ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version); |
| 1508 | if (ret < 0) { |
| 1509 | ERR("Failed to retrieve lttng-modules ABI version"); |
| 1510 | goto error; |
| 1511 | } |
| 1512 | if (abi_version->major != LTTNG_KERNEL_ABI_MAJOR_VERSION) { |
| 1513 | ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)", |
| 1514 | abi_version->major, |
| 1515 | abi_version->minor, |
| 1516 | LTTNG_KERNEL_ABI_MAJOR_VERSION); |
| 1517 | goto error; |
| 1518 | } |
| 1519 | DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)", |
| 1520 | version->major, |
| 1521 | version->minor, |
| 1522 | abi_version->major, |
| 1523 | abi_version->minor); |
| 1524 | return 0; |
| 1525 | |
| 1526 | error_version: |
| 1527 | ret = -1; |
| 1528 | |
| 1529 | error: |
| 1530 | ERR("Kernel tracer version check failed; kernel tracing will not be available"); |
| 1531 | return ret; |
| 1532 | } |
| 1533 | |
| 1534 | /* |
| 1535 | * Kernel work-arounds called at the start of sessiond main(). |
| 1536 | */ |
| 1537 | int init_kernel_workarounds() |
| 1538 | { |
| 1539 | int ret; |
| 1540 | FILE *fp; |
| 1541 | |
| 1542 | /* |
| 1543 | * boot_id needs to be read once before being used concurrently |
| 1544 | * to deal with a Linux kernel race. A fix is proposed for |
| 1545 | * upstream, but the work-around is needed for older kernels. |
| 1546 | */ |
| 1547 | fp = fopen("/proc/sys/kernel/random/boot_id", "r"); |
| 1548 | if (!fp) { |
| 1549 | goto end_boot_id; |
| 1550 | } |
| 1551 | while (!feof(fp)) { |
| 1552 | char buf[37] = ""; |
| 1553 | |
| 1554 | ret = fread(buf, 1, sizeof(buf), fp); |
| 1555 | if (ret < 0) { |
| 1556 | /* Ignore error, we don't really care */ |
| 1557 | } |
| 1558 | } |
| 1559 | ret = fclose(fp); |
| 1560 | if (ret) { |
| 1561 | PERROR("fclose"); |
| 1562 | } |
| 1563 | end_boot_id: |
| 1564 | return 0; |
| 1565 | } |
| 1566 | |
| 1567 | /* |
| 1568 | * Teardown of a kernel session, keeping data required by destroy notifiers. |
| 1569 | */ |
| 1570 | void kernel_destroy_session(struct ltt_kernel_session *ksess) |
| 1571 | { |
| 1572 | struct lttng_trace_chunk *trace_chunk; |
| 1573 | |
| 1574 | if (ksess == nullptr) { |
| 1575 | DBG3("No kernel session when tearing down session"); |
| 1576 | return; |
| 1577 | } |
| 1578 | |
| 1579 | DBG("Tearing down kernel session"); |
| 1580 | trace_chunk = ksess->current_trace_chunk; |
| 1581 | |
| 1582 | /* |
| 1583 | * Destroy channels on the consumer if at least one FD has been sent and we |
| 1584 | * are in no output mode because the streams are in *no* monitor mode so we |
| 1585 | * have to send a command to clean them up or else they leaked. |
| 1586 | */ |
| 1587 | if (!ksess->output_traces && ksess->consumer_fds_sent) { |
| 1588 | int ret; |
| 1589 | struct consumer_socket *socket; |
| 1590 | struct lttng_ht_iter iter; |
| 1591 | |
| 1592 | /* For each consumer socket. */ |
| 1593 | lttng::urcu::read_lock_guard read_lock; |
| 1594 | |
| 1595 | cds_lfht_for_each_entry ( |
| 1596 | ksess->consumer->socks->ht, &iter.iter, socket, node.node) { |
| 1597 | struct ltt_kernel_channel *chan; |
| 1598 | |
| 1599 | /* For each channel, ask the consumer to destroy it. */ |
| 1600 | cds_list_for_each_entry (chan, &ksess->channel_list.head, list) { |
| 1601 | ret = kernel_consumer_destroy_channel(socket, chan); |
| 1602 | if (ret < 0) { |
| 1603 | /* Consumer is probably dead. Use next socket. */ |
| 1604 | continue; |
| 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | /* Close any relayd session */ |
| 1611 | consumer_output_send_destroy_relayd(ksess->consumer); |
| 1612 | |
| 1613 | trace_kernel_destroy_session(ksess); |
| 1614 | lttng_trace_chunk_put(trace_chunk); |
| 1615 | } |
| 1616 | |
| 1617 | /* Teardown of data required by destroy notifiers. */ |
| 1618 | void kernel_free_session(struct ltt_kernel_session *ksess) |
| 1619 | { |
| 1620 | if (ksess == nullptr) { |
| 1621 | return; |
| 1622 | } |
| 1623 | trace_kernel_free_session(ksess); |
| 1624 | } |
| 1625 | |
| 1626 | /* |
| 1627 | * Destroy a kernel channel object. It does not do anything on the tracer side. |
| 1628 | */ |
| 1629 | void kernel_destroy_channel(struct ltt_kernel_channel *kchan) |
| 1630 | { |
| 1631 | struct ltt_kernel_session *ksess = nullptr; |
| 1632 | |
| 1633 | LTTNG_ASSERT(kchan); |
| 1634 | LTTNG_ASSERT(kchan->channel); |
| 1635 | |
| 1636 | DBG3("Kernel destroy channel %s", kchan->channel->name); |
| 1637 | |
| 1638 | /* Update channel count of associated session. */ |
| 1639 | if (kchan->session) { |
| 1640 | /* Keep pointer reference so we can update it after the destroy. */ |
| 1641 | ksess = kchan->session; |
| 1642 | } |
| 1643 | |
| 1644 | trace_kernel_destroy_channel(kchan); |
| 1645 | |
| 1646 | /* |
| 1647 | * At this point the kernel channel is not visible anymore. This is safe |
| 1648 | * since in order to work on a visible kernel session, the tracing session |
| 1649 | * lock (ltt_session.lock) MUST be acquired. |
| 1650 | */ |
| 1651 | if (ksess) { |
| 1652 | ksess->channel_count--; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | /* |
| 1657 | * Take a snapshot for a given kernel session. |
| 1658 | * |
| 1659 | * Return LTTNG_OK on success or else return a LTTNG_ERR code. |
| 1660 | */ |
| 1661 | enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess, |
| 1662 | const struct consumer_output *output, |
| 1663 | uint64_t nb_packets_per_stream) |
| 1664 | { |
| 1665 | int err, ret, saved_metadata_fd; |
| 1666 | enum lttng_error_code status = LTTNG_OK; |
| 1667 | struct consumer_socket *socket; |
| 1668 | struct lttng_ht_iter iter; |
| 1669 | struct ltt_kernel_metadata *saved_metadata; |
| 1670 | char *trace_path = nullptr; |
| 1671 | size_t consumer_path_offset = 0; |
| 1672 | |
| 1673 | LTTNG_ASSERT(ksess); |
| 1674 | LTTNG_ASSERT(ksess->consumer); |
| 1675 | LTTNG_ASSERT(output); |
| 1676 | |
| 1677 | DBG("Kernel snapshot record started"); |
| 1678 | |
| 1679 | /* Save current metadata since the following calls will change it. */ |
| 1680 | saved_metadata = ksess->metadata; |
| 1681 | saved_metadata_fd = ksess->metadata_stream_fd; |
| 1682 | |
| 1683 | ret = kernel_open_metadata(ksess); |
| 1684 | if (ret < 0) { |
| 1685 | status = LTTNG_ERR_KERN_META_FAIL; |
| 1686 | goto error; |
| 1687 | } |
| 1688 | |
| 1689 | ret = kernel_open_metadata_stream(ksess); |
| 1690 | if (ret < 0) { |
| 1691 | status = LTTNG_ERR_KERN_META_FAIL; |
| 1692 | goto error_open_stream; |
| 1693 | } |
| 1694 | |
| 1695 | trace_path = setup_channel_trace_path(ksess->consumer, "", &consumer_path_offset); |
| 1696 | if (!trace_path) { |
| 1697 | status = LTTNG_ERR_INVALID; |
| 1698 | goto error; |
| 1699 | } |
| 1700 | |
| 1701 | { |
| 1702 | /* Send metadata to consumer and snapshot everything. */ |
| 1703 | lttng::urcu::read_lock_guard read_lock; |
| 1704 | |
| 1705 | cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) { |
| 1706 | struct ltt_kernel_channel *chan; |
| 1707 | |
| 1708 | pthread_mutex_lock(socket->lock); |
| 1709 | /* This stream must not be monitored by the consumer. */ |
| 1710 | ret = kernel_consumer_add_metadata(socket, ksess, 0); |
| 1711 | pthread_mutex_unlock(socket->lock); |
| 1712 | if (ret < 0) { |
| 1713 | status = LTTNG_ERR_KERN_META_FAIL; |
| 1714 | goto error_consumer; |
| 1715 | } |
| 1716 | |
| 1717 | /* For each channel, ask the consumer to snapshot it. */ |
| 1718 | cds_list_for_each_entry (chan, &ksess->channel_list.head, list) { |
| 1719 | status = |
| 1720 | consumer_snapshot_channel(socket, |
| 1721 | chan->key, |
| 1722 | output, |
| 1723 | 0, |
| 1724 | &trace_path[consumer_path_offset], |
| 1725 | nb_packets_per_stream); |
| 1726 | if (status != LTTNG_OK) { |
| 1727 | (void) kernel_consumer_destroy_metadata(socket, |
| 1728 | ksess->metadata); |
| 1729 | goto error_consumer; |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | /* Snapshot metadata, */ |
| 1734 | status = consumer_snapshot_channel(socket, |
| 1735 | ksess->metadata->key, |
| 1736 | output, |
| 1737 | 1, |
| 1738 | &trace_path[consumer_path_offset], |
| 1739 | 0); |
| 1740 | if (status != LTTNG_OK) { |
| 1741 | goto error_consumer; |
| 1742 | } |
| 1743 | |
| 1744 | /* |
| 1745 | * The metadata snapshot is done, ask the consumer to destroy it since |
| 1746 | * it's not monitored on the consumer side. |
| 1747 | */ |
| 1748 | (void) kernel_consumer_destroy_metadata(socket, ksess->metadata); |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | error_consumer: |
| 1753 | /* Close newly opened metadata stream. It's now on the consumer side. */ |
| 1754 | err = close(ksess->metadata_stream_fd); |
| 1755 | if (err < 0) { |
| 1756 | PERROR("close snapshot kernel"); |
| 1757 | } |
| 1758 | |
| 1759 | error_open_stream: |
| 1760 | trace_kernel_destroy_metadata(ksess->metadata); |
| 1761 | error: |
| 1762 | /* Restore metadata state.*/ |
| 1763 | ksess->metadata = saved_metadata; |
| 1764 | ksess->metadata_stream_fd = saved_metadata_fd; |
| 1765 | free(trace_path); |
| 1766 | return status; |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * Get the syscall mask array from the kernel tracer. |
| 1771 | * |
| 1772 | * Return 0 on success else a negative value. In both case, syscall_mask should |
| 1773 | * be freed. |
| 1774 | */ |
| 1775 | int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits) |
| 1776 | { |
| 1777 | LTTNG_ASSERT(syscall_mask); |
| 1778 | LTTNG_ASSERT(nr_bits); |
| 1779 | |
| 1780 | return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits); |
| 1781 | } |
| 1782 | |
| 1783 | static int kernel_tracer_abi_greater_or_equal(unsigned int major, unsigned int minor) |
| 1784 | { |
| 1785 | int ret; |
| 1786 | struct lttng_kernel_abi_tracer_abi_version abi; |
| 1787 | |
| 1788 | ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi); |
| 1789 | if (ret < 0) { |
| 1790 | ERR("Failed to retrieve lttng-modules ABI version"); |
| 1791 | goto error; |
| 1792 | } |
| 1793 | |
| 1794 | ret = abi.major > major || (abi.major == major && abi.minor >= minor); |
| 1795 | error: |
| 1796 | return ret; |
| 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi |
| 1801 | * version number. |
| 1802 | * |
| 1803 | * Return 1 on success, 0 when feature is not supported, negative value in case |
| 1804 | * of errors. |
| 1805 | */ |
| 1806 | int kernel_supports_ring_buffer_snapshot_sample_positions() |
| 1807 | { |
| 1808 | /* |
| 1809 | * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3 |
| 1810 | */ |
| 1811 | return kernel_tracer_abi_greater_or_equal(2, 3); |
| 1812 | } |
| 1813 | |
| 1814 | /* |
| 1815 | * Check for the support of the packet sequence number via abi version number. |
| 1816 | * |
| 1817 | * Return 1 on success, 0 when feature is not supported, negative value in case |
| 1818 | * of errors. |
| 1819 | */ |
| 1820 | int kernel_supports_ring_buffer_packet_sequence_number() |
| 1821 | { |
| 1822 | /* |
| 1823 | * Packet sequence number was introduced in LTTng 2.8, |
| 1824 | * lttng-modules ABI 2.1. |
| 1825 | */ |
| 1826 | return kernel_tracer_abi_greater_or_equal(2, 1); |
| 1827 | } |
| 1828 | |
| 1829 | /* |
| 1830 | * Check for the support of event notifiers via abi version number. |
| 1831 | * |
| 1832 | * Return 1 on success, 0 when feature is not supported, negative value in case |
| 1833 | * of errors. |
| 1834 | */ |
| 1835 | int kernel_supports_event_notifiers() |
| 1836 | { |
| 1837 | /* |
| 1838 | * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6. |
| 1839 | */ |
| 1840 | return kernel_tracer_abi_greater_or_equal(2, 6); |
| 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | * Rotate a kernel session. |
| 1845 | * |
| 1846 | * Return LTTNG_OK on success or else an LTTng error code. |
| 1847 | */ |
| 1848 | enum lttng_error_code kernel_rotate_session(struct ltt_session *session) |
| 1849 | { |
| 1850 | int ret; |
| 1851 | enum lttng_error_code status = LTTNG_OK; |
| 1852 | struct consumer_socket *socket; |
| 1853 | struct lttng_ht_iter iter; |
| 1854 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 1855 | |
| 1856 | LTTNG_ASSERT(ksess); |
| 1857 | LTTNG_ASSERT(ksess->consumer); |
| 1858 | |
| 1859 | DBG("Rotate kernel session %s started (session %" PRIu64 ")", session->name, session->id); |
| 1860 | |
| 1861 | { |
| 1862 | /* |
| 1863 | * Note that this loop will end after one iteration given that there is |
| 1864 | * only one kernel consumer. |
| 1865 | */ |
| 1866 | lttng::urcu::read_lock_guard read_lock; |
| 1867 | |
| 1868 | cds_lfht_for_each_entry ( |
| 1869 | ksess->consumer->socks->ht, &iter.iter, socket, node.node) { |
| 1870 | struct ltt_kernel_channel *chan; |
| 1871 | |
| 1872 | /* For each channel, ask the consumer to rotate it. */ |
| 1873 | cds_list_for_each_entry (chan, &ksess->channel_list.head, list) { |
| 1874 | DBG("Rotate kernel channel %" PRIu64 ", session %s", |
| 1875 | chan->key, |
| 1876 | session->name); |
| 1877 | ret = consumer_rotate_channel(socket, |
| 1878 | chan->key, |
| 1879 | ksess->consumer, |
| 1880 | /* is_metadata_channel */ false); |
| 1881 | if (ret < 0) { |
| 1882 | status = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 1883 | goto error; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | /* |
| 1888 | * Rotate the metadata channel. |
| 1889 | */ |
| 1890 | ret = consumer_rotate_channel(socket, |
| 1891 | ksess->metadata->key, |
| 1892 | ksess->consumer, |
| 1893 | /* is_metadata_channel */ true); |
| 1894 | if (ret < 0) { |
| 1895 | status = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 1896 | goto error; |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | error: |
| 1902 | return status; |
| 1903 | } |
| 1904 | |
| 1905 | enum lttng_error_code kernel_create_channel_subdirectories(const struct ltt_kernel_session *ksess) |
| 1906 | { |
| 1907 | enum lttng_error_code ret = LTTNG_OK; |
| 1908 | enum lttng_trace_chunk_status chunk_status; |
| 1909 | |
| 1910 | lttng::urcu::read_lock_guard read_lock; |
| 1911 | LTTNG_ASSERT(ksess->current_trace_chunk); |
| 1912 | |
| 1913 | /* |
| 1914 | * Create the index subdirectory which will take care |
| 1915 | * of implicitly creating the channel's path. |
| 1916 | */ |
| 1917 | chunk_status = lttng_trace_chunk_create_subdirectory( |
| 1918 | ksess->current_trace_chunk, DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR); |
| 1919 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1920 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 1921 | goto error; |
| 1922 | } |
| 1923 | error: |
| 1924 | return ret; |
| 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * Setup necessary data for kernel tracer action. |
| 1929 | */ |
| 1930 | int init_kernel_tracer() |
| 1931 | { |
| 1932 | int ret; |
| 1933 | bool is_root = !getuid(); |
| 1934 | |
| 1935 | /* Modprobe lttng kernel modules */ |
| 1936 | ret = modprobe_lttng_control(); |
| 1937 | if (ret < 0) { |
| 1938 | goto error; |
| 1939 | } |
| 1940 | |
| 1941 | /* Open debugfs lttng */ |
| 1942 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); |
| 1943 | if (kernel_tracer_fd < 0) { |
| 1944 | DBG("Failed to open %s", module_proc_lttng); |
| 1945 | goto error_open; |
| 1946 | } |
| 1947 | |
| 1948 | /* Validate kernel version */ |
| 1949 | ret = kernel_validate_version(&the_kernel_tracer_version, &the_kernel_tracer_abi_version); |
| 1950 | if (ret < 0) { |
| 1951 | goto error_version; |
| 1952 | } |
| 1953 | |
| 1954 | ret = modprobe_lttng_data(); |
| 1955 | if (ret < 0) { |
| 1956 | goto error_modules; |
| 1957 | } |
| 1958 | |
| 1959 | ret = kernel_supports_ring_buffer_snapshot_sample_positions(); |
| 1960 | if (ret < 0) { |
| 1961 | goto error_modules; |
| 1962 | } |
| 1963 | if (ret < 1) { |
| 1964 | WARN("Kernel tracer does not support buffer monitoring. " |
| 1965 | "The monitoring timer of channels in the kernel domain " |
| 1966 | "will be set to 0 (disabled)."); |
| 1967 | } |
| 1968 | |
| 1969 | ret = kernel_supports_event_notifiers(); |
| 1970 | if (ret < 0) { |
| 1971 | ERR("Failed to check for kernel tracer event notifier support"); |
| 1972 | goto error_modules; |
| 1973 | } |
| 1974 | ret = kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd); |
| 1975 | if (ret < 0) { |
| 1976 | /* This is not fatal. */ |
| 1977 | WARN("Failed to create kernel event notifier group"); |
| 1978 | kernel_tracer_event_notifier_group_fd = -1; |
| 1979 | } else { |
| 1980 | enum event_notifier_error_accounting_status error_accounting_status; |
| 1981 | enum lttng_error_code error_code_ret = |
| 1982 | kernel_create_event_notifier_group_notification_fd( |
| 1983 | &kernel_tracer_event_notifier_group_notification_fd); |
| 1984 | |
| 1985 | if (error_code_ret != LTTNG_OK) { |
| 1986 | goto error_modules; |
| 1987 | } |
| 1988 | |
| 1989 | error_accounting_status = event_notifier_error_accounting_register_kernel( |
| 1990 | kernel_tracer_event_notifier_group_fd); |
| 1991 | if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { |
| 1992 | ERR("Failed to initialize event notifier error accounting for kernel tracer"); |
| 1993 | error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING; |
| 1994 | goto error_modules; |
| 1995 | } |
| 1996 | |
| 1997 | kernel_token_to_event_notifier_rule_ht = cds_lfht_new( |
| 1998 | DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr); |
| 1999 | if (!kernel_token_to_event_notifier_rule_ht) { |
| 2000 | goto error_token_ht; |
| 2001 | } |
| 2002 | } |
| 2003 | |
| 2004 | DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d", |
| 2005 | kernel_tracer_fd, |
| 2006 | kernel_tracer_event_notifier_group_fd, |
| 2007 | kernel_tracer_event_notifier_group_notification_fd); |
| 2008 | |
| 2009 | ret = syscall_init_table(kernel_tracer_fd); |
| 2010 | if (ret < 0) { |
| 2011 | ERR("Unable to populate syscall table. Syscall tracing won't " |
| 2012 | "work for this session daemon."); |
| 2013 | } |
| 2014 | |
| 2015 | return 0; |
| 2016 | |
| 2017 | error_version: |
| 2018 | modprobe_remove_lttng_control(); |
| 2019 | ret = close(kernel_tracer_fd); |
| 2020 | if (ret) { |
| 2021 | PERROR("Failed to close kernel tracer file descriptor: fd = %d", kernel_tracer_fd); |
| 2022 | } |
| 2023 | |
| 2024 | kernel_tracer_fd = -1; |
| 2025 | return LTTNG_ERR_KERN_VERSION; |
| 2026 | |
| 2027 | error_token_ht: |
| 2028 | ret = close(kernel_tracer_event_notifier_group_notification_fd); |
| 2029 | if (ret) { |
| 2030 | PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d", |
| 2031 | kernel_tracer_event_notifier_group_notification_fd); |
| 2032 | } |
| 2033 | |
| 2034 | kernel_tracer_event_notifier_group_notification_fd = -1; |
| 2035 | |
| 2036 | error_modules: |
| 2037 | ret = close(kernel_tracer_event_notifier_group_fd); |
| 2038 | if (ret) { |
| 2039 | PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d", |
| 2040 | kernel_tracer_event_notifier_group_fd); |
| 2041 | } |
| 2042 | |
| 2043 | kernel_tracer_event_notifier_group_fd = -1; |
| 2044 | |
| 2045 | ret = close(kernel_tracer_fd); |
| 2046 | if (ret) { |
| 2047 | PERROR("Failed to close kernel tracer file descriptor: fd = %d", kernel_tracer_fd); |
| 2048 | } |
| 2049 | |
| 2050 | kernel_tracer_fd = -1; |
| 2051 | |
| 2052 | error_open: |
| 2053 | modprobe_remove_lttng_control(); |
| 2054 | |
| 2055 | error: |
| 2056 | WARN("No kernel tracer available"); |
| 2057 | kernel_tracer_fd = -1; |
| 2058 | if (!is_root) { |
| 2059 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
| 2060 | } else { |
| 2061 | return LTTNG_ERR_KERN_NA; |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | void cleanup_kernel_tracer() |
| 2066 | { |
| 2067 | DBG2("Closing kernel event notifier group notification file descriptor"); |
| 2068 | if (kernel_tracer_event_notifier_group_notification_fd >= 0) { |
| 2069 | int ret = notification_thread_command_remove_tracer_event_source( |
| 2070 | the_notification_thread_handle, |
| 2071 | kernel_tracer_event_notifier_group_notification_fd); |
| 2072 | if (ret != LTTNG_OK) { |
| 2073 | ERR("Failed to remove kernel event notifier notification from notification thread"); |
| 2074 | } |
| 2075 | |
| 2076 | ret = close(kernel_tracer_event_notifier_group_notification_fd); |
| 2077 | if (ret) { |
| 2078 | PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d", |
| 2079 | kernel_tracer_event_notifier_group_notification_fd); |
| 2080 | } |
| 2081 | |
| 2082 | kernel_tracer_event_notifier_group_notification_fd = -1; |
| 2083 | } |
| 2084 | |
| 2085 | if (kernel_token_to_event_notifier_rule_ht) { |
| 2086 | const int ret = cds_lfht_destroy(kernel_token_to_event_notifier_rule_ht, nullptr); |
| 2087 | LTTNG_ASSERT(ret == 0); |
| 2088 | } |
| 2089 | |
| 2090 | DBG2("Closing kernel event notifier group file descriptor"); |
| 2091 | if (kernel_tracer_event_notifier_group_fd >= 0) { |
| 2092 | const int ret = close(kernel_tracer_event_notifier_group_fd); |
| 2093 | |
| 2094 | if (ret) { |
| 2095 | PERROR("Failed to close kernel event notifier group file descriptor: fd = %d", |
| 2096 | kernel_tracer_event_notifier_group_fd); |
| 2097 | } |
| 2098 | |
| 2099 | kernel_tracer_event_notifier_group_fd = -1; |
| 2100 | } |
| 2101 | |
| 2102 | DBG2("Closing kernel fd"); |
| 2103 | if (kernel_tracer_fd >= 0) { |
| 2104 | const int ret = close(kernel_tracer_fd); |
| 2105 | |
| 2106 | if (ret) { |
| 2107 | PERROR("Failed to close kernel tracer file descriptor: fd = %d", |
| 2108 | kernel_tracer_fd); |
| 2109 | } |
| 2110 | |
| 2111 | kernel_tracer_fd = -1; |
| 2112 | } |
| 2113 | |
| 2114 | free(syscall_table); |
| 2115 | } |
| 2116 | |
| 2117 | bool kernel_tracer_is_initialized() |
| 2118 | { |
| 2119 | return kernel_tracer_fd >= 0; |
| 2120 | } |
| 2121 | |
| 2122 | /* |
| 2123 | * Clear a kernel session. |
| 2124 | * |
| 2125 | * Return LTTNG_OK on success or else an LTTng error code. |
| 2126 | */ |
| 2127 | enum lttng_error_code kernel_clear_session(struct ltt_session *session) |
| 2128 | { |
| 2129 | int ret; |
| 2130 | enum lttng_error_code status = LTTNG_OK; |
| 2131 | struct consumer_socket *socket; |
| 2132 | struct lttng_ht_iter iter; |
| 2133 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 2134 | |
| 2135 | LTTNG_ASSERT(ksess); |
| 2136 | LTTNG_ASSERT(ksess->consumer); |
| 2137 | |
| 2138 | DBG("Clear kernel session %s (session %" PRIu64 ")", session->name, session->id); |
| 2139 | |
| 2140 | if (ksess->active) { |
| 2141 | ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id); |
| 2142 | status = LTTNG_ERR_FATAL; |
| 2143 | goto end; |
| 2144 | } |
| 2145 | |
| 2146 | { |
| 2147 | /* |
| 2148 | * Note that this loop will end after one iteration given that there is |
| 2149 | * only one kernel consumer. |
| 2150 | */ |
| 2151 | lttng::urcu::read_lock_guard read_lock; |
| 2152 | |
| 2153 | cds_lfht_for_each_entry ( |
| 2154 | ksess->consumer->socks->ht, &iter.iter, socket, node.node) { |
| 2155 | struct ltt_kernel_channel *chan; |
| 2156 | |
| 2157 | /* For each channel, ask the consumer to clear it. */ |
| 2158 | cds_list_for_each_entry (chan, &ksess->channel_list.head, list) { |
| 2159 | DBG("Clear kernel channel %" PRIu64 ", session %s", |
| 2160 | chan->key, |
| 2161 | session->name); |
| 2162 | ret = consumer_clear_channel(socket, chan->key); |
| 2163 | if (ret < 0) { |
| 2164 | goto error; |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | if (!ksess->metadata) { |
| 2169 | /* |
| 2170 | * Nothing to do for the metadata. |
| 2171 | * This is a snapshot session. |
| 2172 | * The metadata is genererated on the fly. |
| 2173 | */ |
| 2174 | continue; |
| 2175 | } |
| 2176 | |
| 2177 | /* |
| 2178 | * Clear the metadata channel. |
| 2179 | * Metadata channel is not cleared per se but we still need to |
| 2180 | * perform a rotation operation on it behind the scene. |
| 2181 | */ |
| 2182 | ret = consumer_clear_channel(socket, ksess->metadata->key); |
| 2183 | if (ret < 0) { |
| 2184 | goto error; |
| 2185 | } |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | goto end; |
| 2190 | error: |
| 2191 | switch (-ret) { |
| 2192 | case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: |
| 2193 | status = LTTNG_ERR_CLEAR_RELAY_DISALLOWED; |
| 2194 | break; |
| 2195 | default: |
| 2196 | status = LTTNG_ERR_CLEAR_FAIL_CONSUMER; |
| 2197 | break; |
| 2198 | } |
| 2199 | end: |
| 2200 | return status; |
| 2201 | } |
| 2202 | |
| 2203 | enum lttng_error_code |
| 2204 | kernel_create_event_notifier_group_notification_fd(int *event_notifier_group_notification_fd) |
| 2205 | { |
| 2206 | int local_fd = -1, ret; |
| 2207 | enum lttng_error_code error_code_ret; |
| 2208 | |
| 2209 | LTTNG_ASSERT(event_notifier_group_notification_fd); |
| 2210 | |
| 2211 | ret = kernctl_create_event_notifier_group_notification_fd( |
| 2212 | kernel_tracer_event_notifier_group_fd); |
| 2213 | if (ret < 0) { |
| 2214 | PERROR("Failed to create kernel event notifier group notification file descriptor"); |
| 2215 | error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD; |
| 2216 | goto error; |
| 2217 | } |
| 2218 | |
| 2219 | local_fd = ret; |
| 2220 | |
| 2221 | /* Prevent fd duplication after execlp(). */ |
| 2222 | ret = fcntl(local_fd, F_SETFD, FD_CLOEXEC); |
| 2223 | if (ret < 0) { |
| 2224 | PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d", |
| 2225 | local_fd); |
| 2226 | error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD; |
| 2227 | goto error; |
| 2228 | } |
| 2229 | |
| 2230 | DBG("Created kernel notifier group notification file descriptor: fd = %d", local_fd); |
| 2231 | error_code_ret = LTTNG_OK; |
| 2232 | *event_notifier_group_notification_fd = local_fd; |
| 2233 | local_fd = -1; |
| 2234 | |
| 2235 | error: |
| 2236 | if (local_fd >= 0) { |
| 2237 | ret = close(local_fd); |
| 2238 | if (ret) { |
| 2239 | PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d", |
| 2240 | local_fd); |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | return error_code_ret; |
| 2245 | } |
| 2246 | |
| 2247 | enum lttng_error_code |
| 2248 | kernel_destroy_event_notifier_group_notification_fd(int event_notifier_group_notification_fd) |
| 2249 | { |
| 2250 | enum lttng_error_code ret_code = LTTNG_OK; |
| 2251 | |
| 2252 | DBG("Closing event notifier group notification file descriptor: fd = %d", |
| 2253 | event_notifier_group_notification_fd); |
| 2254 | if (event_notifier_group_notification_fd >= 0) { |
| 2255 | const int ret = close(event_notifier_group_notification_fd); |
| 2256 | if (ret) { |
| 2257 | PERROR("Failed to close event notifier group notification file descriptor: fd = %d", |
| 2258 | event_notifier_group_notification_fd); |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | return ret_code; |
| 2263 | } |
| 2264 | |
| 2265 | static unsigned long hash_trigger(const struct lttng_trigger *trigger) |
| 2266 | { |
| 2267 | const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger); |
| 2268 | |
| 2269 | return lttng_condition_hash(condition); |
| 2270 | } |
| 2271 | |
| 2272 | static int match_trigger(struct cds_lfht_node *node, const void *key) |
| 2273 | { |
| 2274 | const struct ltt_kernel_event_notifier_rule *event_notifier_rule; |
| 2275 | const struct lttng_trigger *trigger = (lttng_trigger *) key; |
| 2276 | |
| 2277 | event_notifier_rule = |
| 2278 | caa_container_of(node, const struct ltt_kernel_event_notifier_rule, ht_node); |
| 2279 | |
| 2280 | return lttng_trigger_is_equal(trigger, event_notifier_rule->trigger); |
| 2281 | } |
| 2282 | |
| 2283 | static enum lttng_error_code kernel_create_event_notifier_rule( |
| 2284 | struct lttng_trigger *trigger, const struct lttng_credentials *creds, uint64_t token) |
| 2285 | { |
| 2286 | int err, fd, ret = 0; |
| 2287 | enum lttng_error_code error_code_ret; |
| 2288 | enum lttng_condition_status condition_status; |
| 2289 | enum lttng_condition_type condition_type; |
| 2290 | enum lttng_event_rule_type event_rule_type; |
| 2291 | struct ltt_kernel_event_notifier_rule *event_notifier_rule; |
| 2292 | struct lttng_kernel_abi_event_notifier kernel_event_notifier = {}; |
| 2293 | unsigned int capture_bytecode_count = 0, i; |
| 2294 | const struct lttng_condition *condition = nullptr; |
| 2295 | const struct lttng_event_rule *event_rule = nullptr; |
| 2296 | enum lttng_condition_status cond_status; |
| 2297 | |
| 2298 | LTTNG_ASSERT(trigger); |
| 2299 | |
| 2300 | condition = lttng_trigger_get_const_condition(trigger); |
| 2301 | LTTNG_ASSERT(condition); |
| 2302 | |
| 2303 | condition_type = lttng_condition_get_type(condition); |
| 2304 | LTTNG_ASSERT(condition_type == LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
| 2305 | |
| 2306 | /* Does not acquire a reference. */ |
| 2307 | condition_status = lttng_condition_event_rule_matches_get_rule(condition, &event_rule); |
| 2308 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
| 2309 | LTTNG_ASSERT(event_rule); |
| 2310 | |
| 2311 | event_rule_type = lttng_event_rule_get_type(event_rule); |
| 2312 | LTTNG_ASSERT(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN); |
| 2313 | |
| 2314 | error_code_ret = trace_kernel_create_event_notifier_rule( |
| 2315 | trigger, |
| 2316 | token, |
| 2317 | lttng_condition_event_rule_matches_get_error_counter_index(condition), |
| 2318 | &event_notifier_rule); |
| 2319 | if (error_code_ret != LTTNG_OK) { |
| 2320 | goto error; |
| 2321 | } |
| 2322 | |
| 2323 | error_code_ret = trace_kernel_init_event_notifier_from_event_rule(event_rule, |
| 2324 | &kernel_event_notifier); |
| 2325 | if (error_code_ret != LTTNG_OK) { |
| 2326 | goto free_event; |
| 2327 | } |
| 2328 | |
| 2329 | kernel_event_notifier.event.token = event_notifier_rule->token; |
| 2330 | kernel_event_notifier.error_counter_idx = |
| 2331 | lttng_condition_event_rule_matches_get_error_counter_index(condition); |
| 2332 | |
| 2333 | fd = kernctl_create_event_notifier(kernel_tracer_event_notifier_group_fd, |
| 2334 | &kernel_event_notifier); |
| 2335 | if (fd < 0) { |
| 2336 | switch (-fd) { |
| 2337 | case EEXIST: |
| 2338 | error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST; |
| 2339 | break; |
| 2340 | case ENOSYS: |
| 2341 | WARN("Failed to create kernel event notifier: not notifier type not implemented"); |
| 2342 | error_code_ret = LTTNG_ERR_KERN_EVENT_ENOSYS; |
| 2343 | break; |
| 2344 | case ENOENT: |
| 2345 | WARN("Failed to create kernel event notifier: not found: name = '%s'", |
| 2346 | kernel_event_notifier.event.name); |
| 2347 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 2348 | break; |
| 2349 | default: |
| 2350 | PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'", |
| 2351 | fd, |
| 2352 | kernel_event_notifier.event.name); |
| 2353 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 2354 | } |
| 2355 | goto free_event; |
| 2356 | } |
| 2357 | |
| 2358 | event_notifier_rule->fd = fd; |
| 2359 | /* Prevent fd duplication after execlp(). */ |
| 2360 | err = fcntl(event_notifier_rule->fd, F_SETFD, FD_CLOEXEC); |
| 2361 | if (err < 0) { |
| 2362 | PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d", |
| 2363 | fd); |
| 2364 | error_code_ret = LTTNG_ERR_FATAL; |
| 2365 | goto set_cloexec_error; |
| 2366 | } |
| 2367 | |
| 2368 | if (event_notifier_rule->filter) { |
| 2369 | err = kernctl_filter(event_notifier_rule->fd, event_notifier_rule->filter); |
| 2370 | if (err < 0) { |
| 2371 | switch (-err) { |
| 2372 | case ENOMEM: |
| 2373 | error_code_ret = LTTNG_ERR_FILTER_NOMEM; |
| 2374 | break; |
| 2375 | default: |
| 2376 | error_code_ret = LTTNG_ERR_FILTER_INVAL; |
| 2377 | break; |
| 2378 | } |
| 2379 | goto filter_error; |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | if (lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE) { |
| 2384 | ret = userspace_probe_event_rule_add_callsites( |
| 2385 | event_rule, creds, event_notifier_rule->fd); |
| 2386 | if (ret) { |
| 2387 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 2388 | goto add_callsite_error; |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | /* Set the capture bytecode if any. */ |
| 2393 | cond_status = lttng_condition_event_rule_matches_get_capture_descriptor_count( |
| 2394 | condition, &capture_bytecode_count); |
| 2395 | LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK); |
| 2396 | |
| 2397 | for (i = 0; i < capture_bytecode_count; i++) { |
| 2398 | const struct lttng_bytecode *capture_bytecode = |
| 2399 | lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition, |
| 2400 | i); |
| 2401 | |
| 2402 | if (capture_bytecode == nullptr) { |
| 2403 | ERR("Unexpected NULL capture bytecode on condition"); |
| 2404 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 2405 | goto capture_error; |
| 2406 | } |
| 2407 | |
| 2408 | ret = kernctl_capture(event_notifier_rule->fd, capture_bytecode); |
| 2409 | if (ret < 0) { |
| 2410 | ERR("Failed to set capture bytecode on event notifier rule fd: fd = %d", |
| 2411 | event_notifier_rule->fd); |
| 2412 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 2413 | goto capture_error; |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | err = kernctl_enable(event_notifier_rule->fd); |
| 2418 | if (err < 0) { |
| 2419 | switch (-err) { |
| 2420 | case EEXIST: |
| 2421 | error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST; |
| 2422 | break; |
| 2423 | default: |
| 2424 | PERROR("enable kernel event notifier"); |
| 2425 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 2426 | break; |
| 2427 | } |
| 2428 | goto enable_error; |
| 2429 | } |
| 2430 | |
| 2431 | /* Add trigger to kernel token mapping in the hash table. */ |
| 2432 | { |
| 2433 | lttng::urcu::read_lock_guard read_lock; |
| 2434 | cds_lfht_add(kernel_token_to_event_notifier_rule_ht, |
| 2435 | hash_trigger(trigger), |
| 2436 | &event_notifier_rule->ht_node); |
| 2437 | } |
| 2438 | |
| 2439 | DBG("Created kernel event notifier: name = '%s', fd = %d", |
| 2440 | kernel_event_notifier.event.name, |
| 2441 | event_notifier_rule->fd); |
| 2442 | |
| 2443 | return LTTNG_OK; |
| 2444 | |
| 2445 | capture_error: |
| 2446 | add_callsite_error: |
| 2447 | enable_error: |
| 2448 | set_cloexec_error: |
| 2449 | filter_error: |
| 2450 | { |
| 2451 | const int close_ret = close(event_notifier_rule->fd); |
| 2452 | |
| 2453 | if (close_ret) { |
| 2454 | PERROR("Failed to close kernel event notifier file descriptor: fd = %d", |
| 2455 | event_notifier_rule->fd); |
| 2456 | } |
| 2457 | } |
| 2458 | free_event: |
| 2459 | free(event_notifier_rule); |
| 2460 | error: |
| 2461 | return error_code_ret; |
| 2462 | } |
| 2463 | |
| 2464 | enum lttng_error_code kernel_register_event_notifier(struct lttng_trigger *trigger, |
| 2465 | const struct lttng_credentials *cmd_creds) |
| 2466 | { |
| 2467 | enum lttng_error_code ret; |
| 2468 | enum lttng_condition_status status; |
| 2469 | enum lttng_domain_type domain_type; |
| 2470 | const struct lttng_event_rule *event_rule; |
| 2471 | const struct lttng_condition *const condition = lttng_trigger_get_const_condition(trigger); |
| 2472 | const uint64_t token = lttng_trigger_get_tracer_token(trigger); |
| 2473 | |
| 2474 | LTTNG_ASSERT(condition); |
| 2475 | |
| 2476 | /* Does not acquire a reference to the event rule. */ |
| 2477 | status = lttng_condition_event_rule_matches_get_rule(condition, &event_rule); |
| 2478 | LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK); |
| 2479 | |
| 2480 | domain_type = lttng_event_rule_get_domain_type(event_rule); |
| 2481 | LTTNG_ASSERT(domain_type == LTTNG_DOMAIN_KERNEL); |
| 2482 | |
| 2483 | ret = kernel_create_event_notifier_rule(trigger, cmd_creds, token); |
| 2484 | if (ret != LTTNG_OK) { |
| 2485 | ERR("Failed to create kernel event notifier rule"); |
| 2486 | } |
| 2487 | |
| 2488 | return ret; |
| 2489 | } |
| 2490 | |
| 2491 | enum lttng_error_code kernel_unregister_event_notifier(const struct lttng_trigger *trigger) |
| 2492 | { |
| 2493 | struct ltt_kernel_event_notifier_rule *token_event_rule_element; |
| 2494 | struct cds_lfht_node *node; |
| 2495 | struct cds_lfht_iter iter; |
| 2496 | enum lttng_error_code error_code_ret; |
| 2497 | int ret; |
| 2498 | |
| 2499 | lttng::urcu::read_lock_guard read_lock; |
| 2500 | |
| 2501 | cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht, |
| 2502 | hash_trigger(trigger), |
| 2503 | match_trigger, |
| 2504 | trigger, |
| 2505 | &iter); |
| 2506 | |
| 2507 | node = cds_lfht_iter_get_node(&iter); |
| 2508 | if (!node) { |
| 2509 | error_code_ret = LTTNG_ERR_TRIGGER_NOT_FOUND; |
| 2510 | goto error; |
| 2511 | } |
| 2512 | |
| 2513 | token_event_rule_element = |
| 2514 | caa_container_of(node, struct ltt_kernel_event_notifier_rule, ht_node); |
| 2515 | |
| 2516 | ret = kernel_disable_event_notifier_rule(token_event_rule_element); |
| 2517 | if (ret) { |
| 2518 | error_code_ret = LTTNG_ERR_FATAL; |
| 2519 | goto error; |
| 2520 | } |
| 2521 | |
| 2522 | trace_kernel_destroy_event_notifier_rule(token_event_rule_element); |
| 2523 | error_code_ret = LTTNG_OK; |
| 2524 | |
| 2525 | error: |
| 2526 | |
| 2527 | return error_code_ret; |
| 2528 | } |
| 2529 | |
| 2530 | int kernel_get_notification_fd() |
| 2531 | { |
| 2532 | return kernel_tracer_event_notifier_group_notification_fd; |
| 2533 | } |