| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #include <lttng/event.h> |
| 15 | #include <lttng/lttng-error.h> |
| 16 | #include <lttng/kernel-probe.h> |
| 17 | #include <lttng/userspace-probe.h> |
| 18 | #include <lttng/userspace-probe-internal.h> |
| 19 | #include <lttng/event-rule/event-rule.h> |
| 20 | #include <lttng/event-rule/event-rule-internal.h> |
| 21 | #include <lttng/event-rule/kernel-kprobe.h> |
| 22 | #include <lttng/event-rule/kernel-kprobe-internal.h> |
| 23 | #include <lttng/event-rule/kernel-syscall.h> |
| 24 | #include <lttng/event-rule/kernel-syscall-internal.h> |
| 25 | #include <lttng/event-rule/kernel-tracepoint.h> |
| 26 | #include <lttng/event-rule/kernel-tracepoint-internal.h> |
| 27 | #include <lttng/event-rule/kernel-uprobe.h> |
| 28 | #include <lttng/event-rule/kernel-uprobe-internal.h> |
| 29 | #include <common/common.h> |
| 30 | #include <common/defaults.h> |
| 31 | #include <common/trace-chunk.h> |
| 32 | #include <common/macros.h> |
| 33 | |
| 34 | #include "consumer.h" |
| 35 | #include "trace-kernel.h" |
| 36 | #include "lttng-sessiond.h" |
| 37 | #include "notification-thread-commands.h" |
| 38 | |
| 39 | /* |
| 40 | * Find the channel name for the given kernel session. |
| 41 | */ |
| 42 | struct ltt_kernel_channel *trace_kernel_get_channel_by_name( |
| 43 | const char *name, struct ltt_kernel_session *session) |
| 44 | { |
| 45 | struct ltt_kernel_channel *chan; |
| 46 | |
| 47 | assert(session); |
| 48 | assert(name); |
| 49 | |
| 50 | /* |
| 51 | * If we receive an empty string for channel name, it means the |
| 52 | * default channel name is requested. |
| 53 | */ |
| 54 | if (name[0] == '\0') |
| 55 | name = DEFAULT_CHANNEL_NAME; |
| 56 | |
| 57 | DBG("Trying to find channel %s", name); |
| 58 | |
| 59 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
| 60 | if (strcmp(name, chan->channel->name) == 0) { |
| 61 | DBG("Found channel by name %s", name); |
| 62 | return chan; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Find the event for the given channel. |
| 71 | */ |
| 72 | struct ltt_kernel_event *trace_kernel_find_event( |
| 73 | char *name, struct ltt_kernel_channel *channel, |
| 74 | enum lttng_event_type type, |
| 75 | struct lttng_bytecode *filter) |
| 76 | { |
| 77 | struct ltt_kernel_event *ev; |
| 78 | int found = 0; |
| 79 | |
| 80 | assert(name); |
| 81 | assert(channel); |
| 82 | |
| 83 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { |
| 84 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
| 85 | continue; |
| 86 | } |
| 87 | if (strcmp(name, ev->event->name)) { |
| 88 | continue; |
| 89 | } |
| 90 | if ((ev->filter && !filter) || (!ev->filter && filter)) { |
| 91 | continue; |
| 92 | } |
| 93 | if (ev->filter && filter) { |
| 94 | if (ev->filter->len != filter->len || |
| 95 | memcmp(ev->filter->data, filter->data, |
| 96 | filter->len) != 0) { |
| 97 | continue; |
| 98 | } |
| 99 | } |
| 100 | found = 1; |
| 101 | break; |
| 102 | } |
| 103 | if (found) { |
| 104 | DBG("Found event %s for channel %s", name, |
| 105 | channel->channel->name); |
| 106 | return ev; |
| 107 | } else { |
| 108 | return NULL; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Find the event name for the given channel. |
| 114 | */ |
| 115 | struct ltt_kernel_event *trace_kernel_get_event_by_name( |
| 116 | char *name, struct ltt_kernel_channel *channel, |
| 117 | enum lttng_event_type type) |
| 118 | { |
| 119 | struct ltt_kernel_event *ev; |
| 120 | int found = 0; |
| 121 | |
| 122 | assert(name); |
| 123 | assert(channel); |
| 124 | |
| 125 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { |
| 126 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
| 127 | continue; |
| 128 | } |
| 129 | if (strcmp(name, ev->event->name)) { |
| 130 | continue; |
| 131 | } |
| 132 | found = 1; |
| 133 | break; |
| 134 | } |
| 135 | if (found) { |
| 136 | DBG("Found event %s for channel %s", name, |
| 137 | channel->channel->name); |
| 138 | return ev; |
| 139 | } else { |
| 140 | return NULL; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Allocate and initialize a kernel session data structure. |
| 146 | * |
| 147 | * Return pointer to structure or NULL. |
| 148 | */ |
| 149 | struct ltt_kernel_session *trace_kernel_create_session(void) |
| 150 | { |
| 151 | struct ltt_kernel_session *lks = NULL; |
| 152 | |
| 153 | /* Allocate a new ltt kernel session */ |
| 154 | lks = zmalloc(sizeof(struct ltt_kernel_session)); |
| 155 | if (lks == NULL) { |
| 156 | PERROR("create kernel session zmalloc"); |
| 157 | goto alloc_error; |
| 158 | } |
| 159 | |
| 160 | /* Init data structure */ |
| 161 | lks->fd = -1; |
| 162 | lks->metadata_stream_fd = -1; |
| 163 | lks->channel_count = 0; |
| 164 | lks->stream_count_global = 0; |
| 165 | lks->metadata = NULL; |
| 166 | CDS_INIT_LIST_HEAD(&lks->channel_list.head); |
| 167 | |
| 168 | lks->tracker_pid = process_attr_tracker_create(); |
| 169 | if (!lks->tracker_pid) { |
| 170 | goto error; |
| 171 | } |
| 172 | lks->tracker_vpid = process_attr_tracker_create(); |
| 173 | if (!lks->tracker_vpid) { |
| 174 | goto error; |
| 175 | } |
| 176 | lks->tracker_uid = process_attr_tracker_create(); |
| 177 | if (!lks->tracker_uid) { |
| 178 | goto error; |
| 179 | } |
| 180 | lks->tracker_vuid = process_attr_tracker_create(); |
| 181 | if (!lks->tracker_vuid) { |
| 182 | goto error; |
| 183 | } |
| 184 | lks->tracker_gid = process_attr_tracker_create(); |
| 185 | if (!lks->tracker_gid) { |
| 186 | goto error; |
| 187 | } |
| 188 | lks->tracker_vgid = process_attr_tracker_create(); |
| 189 | if (!lks->tracker_vgid) { |
| 190 | goto error; |
| 191 | } |
| 192 | lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
| 193 | if (lks->consumer == NULL) { |
| 194 | goto error; |
| 195 | } |
| 196 | |
| 197 | return lks; |
| 198 | |
| 199 | error: |
| 200 | process_attr_tracker_destroy(lks->tracker_pid); |
| 201 | process_attr_tracker_destroy(lks->tracker_vpid); |
| 202 | process_attr_tracker_destroy(lks->tracker_uid); |
| 203 | process_attr_tracker_destroy(lks->tracker_vuid); |
| 204 | process_attr_tracker_destroy(lks->tracker_gid); |
| 205 | process_attr_tracker_destroy(lks->tracker_vgid); |
| 206 | free(lks); |
| 207 | |
| 208 | alloc_error: |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Allocate and initialize a kernel channel data structure. |
| 214 | * |
| 215 | * Return pointer to structure or NULL. |
| 216 | */ |
| 217 | struct ltt_kernel_channel *trace_kernel_create_channel( |
| 218 | struct lttng_channel *chan) |
| 219 | { |
| 220 | struct ltt_kernel_channel *lkc; |
| 221 | struct lttng_channel_extended *extended = NULL; |
| 222 | |
| 223 | assert(chan); |
| 224 | |
| 225 | lkc = zmalloc(sizeof(struct ltt_kernel_channel)); |
| 226 | if (lkc == NULL) { |
| 227 | PERROR("ltt_kernel_channel zmalloc"); |
| 228 | goto error; |
| 229 | } |
| 230 | |
| 231 | lkc->channel = zmalloc(sizeof(struct lttng_channel)); |
| 232 | if (lkc->channel == NULL) { |
| 233 | PERROR("lttng_channel zmalloc"); |
| 234 | goto error; |
| 235 | } |
| 236 | |
| 237 | extended = zmalloc(sizeof(struct lttng_channel_extended)); |
| 238 | if (!extended) { |
| 239 | PERROR("lttng_channel_channel zmalloc"); |
| 240 | goto error; |
| 241 | } |
| 242 | memcpy(lkc->channel, chan, sizeof(struct lttng_channel)); |
| 243 | memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended)); |
| 244 | lkc->channel->attr.extended.ptr = extended; |
| 245 | extended = NULL; |
| 246 | |
| 247 | /* |
| 248 | * If we receive an empty string for channel name, it means the |
| 249 | * default channel name is requested. |
| 250 | */ |
| 251 | if (chan->name[0] == '\0') { |
| 252 | strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME, |
| 253 | sizeof(lkc->channel->name)); |
| 254 | } |
| 255 | lkc->channel->name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; |
| 256 | |
| 257 | lkc->fd = -1; |
| 258 | lkc->stream_count = 0; |
| 259 | lkc->event_count = 0; |
| 260 | lkc->enabled = 1; |
| 261 | lkc->published_to_notification_thread = false; |
| 262 | /* Init linked list */ |
| 263 | CDS_INIT_LIST_HEAD(&lkc->events_list.head); |
| 264 | CDS_INIT_LIST_HEAD(&lkc->stream_list.head); |
| 265 | CDS_INIT_LIST_HEAD(&lkc->ctx_list); |
| 266 | |
| 267 | return lkc; |
| 268 | |
| 269 | error: |
| 270 | if (lkc) { |
| 271 | free(lkc->channel); |
| 272 | } |
| 273 | free(extended); |
| 274 | free(lkc); |
| 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Allocate and init a kernel context object. |
| 280 | * |
| 281 | * Return the allocated object or NULL on error. |
| 282 | */ |
| 283 | struct ltt_kernel_context *trace_kernel_create_context( |
| 284 | struct lttng_kernel_abi_context *ctx) |
| 285 | { |
| 286 | struct ltt_kernel_context *kctx; |
| 287 | |
| 288 | kctx = zmalloc(sizeof(*kctx)); |
| 289 | if (!kctx) { |
| 290 | PERROR("zmalloc kernel context"); |
| 291 | goto error; |
| 292 | } |
| 293 | |
| 294 | if (ctx) { |
| 295 | memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx)); |
| 296 | } |
| 297 | error: |
| 298 | return kctx; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Allocate and init a kernel context object from an existing kernel context |
| 303 | * object. |
| 304 | * |
| 305 | * Return the allocated object or NULL on error. |
| 306 | */ |
| 307 | struct ltt_kernel_context *trace_kernel_copy_context( |
| 308 | struct ltt_kernel_context *kctx) |
| 309 | { |
| 310 | struct ltt_kernel_context *kctx_copy; |
| 311 | |
| 312 | assert(kctx); |
| 313 | kctx_copy = zmalloc(sizeof(*kctx_copy)); |
| 314 | if (!kctx_copy) { |
| 315 | PERROR("zmalloc ltt_kernel_context"); |
| 316 | goto error; |
| 317 | } |
| 318 | |
| 319 | memcpy(kctx_copy, kctx, sizeof(*kctx_copy)); |
| 320 | memset(&kctx_copy->list, 0, sizeof(kctx_copy->list)); |
| 321 | |
| 322 | error: |
| 323 | return kctx_copy; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Allocate and initialize a kernel event. Set name and event type. |
| 328 | * We own filter_expression, and filter. |
| 329 | * |
| 330 | * Return pointer to structure or NULL. |
| 331 | */ |
| 332 | enum lttng_error_code trace_kernel_create_event( |
| 333 | struct lttng_event *ev, char *filter_expression, |
| 334 | struct lttng_bytecode *filter, |
| 335 | struct ltt_kernel_event **kernel_event) |
| 336 | { |
| 337 | enum lttng_error_code ret; |
| 338 | struct lttng_kernel_abi_event *attr; |
| 339 | struct ltt_kernel_event *local_kernel_event; |
| 340 | struct lttng_userspace_probe_location *userspace_probe_location = NULL; |
| 341 | |
| 342 | assert(ev); |
| 343 | |
| 344 | local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event)); |
| 345 | attr = zmalloc(sizeof(struct lttng_kernel_abi_event)); |
| 346 | if (local_kernel_event == NULL || attr == NULL) { |
| 347 | PERROR("kernel event zmalloc"); |
| 348 | ret = LTTNG_ERR_NOMEM; |
| 349 | goto error; |
| 350 | } |
| 351 | |
| 352 | switch (ev->type) { |
| 353 | case LTTNG_EVENT_PROBE: |
| 354 | attr->instrumentation = LTTNG_KERNEL_ABI_KPROBE; |
| 355 | attr->u.kprobe.addr = ev->attr.probe.addr; |
| 356 | attr->u.kprobe.offset = ev->attr.probe.offset; |
| 357 | strncpy(attr->u.kprobe.symbol_name, |
| 358 | ev->attr.probe.symbol_name, LTTNG_KERNEL_ABI_SYM_NAME_LEN); |
| 359 | attr->u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; |
| 360 | break; |
| 361 | case LTTNG_EVENT_USERSPACE_PROBE: |
| 362 | { |
| 363 | const struct lttng_userspace_probe_location* location = NULL; |
| 364 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; |
| 365 | |
| 366 | location = lttng_event_get_userspace_probe_location(ev); |
| 367 | if (!location) { |
| 368 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 369 | goto error; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * From this point on, the specific term 'uprobe' is used |
| 374 | * instead of the generic 'userspace probe' because it's the |
| 375 | * technology used at the moment for this instrumentation. |
| 376 | * LTTng currently implements userspace probes using uprobes. |
| 377 | * In the interactions with the kernel tracer, we use the |
| 378 | * uprobe term. |
| 379 | */ |
| 380 | attr->instrumentation = LTTNG_KERNEL_ABI_UPROBE; |
| 381 | |
| 382 | lookup = lttng_userspace_probe_location_get_lookup_method( |
| 383 | location); |
| 384 | if (!lookup) { |
| 385 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 386 | goto error; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * From the kernel tracer's perspective, all userspace probe |
| 391 | * event types are all the same: a file and an offset. |
| 392 | */ |
| 393 | switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) { |
| 394 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: |
| 395 | /* Get the file descriptor on the target binary. */ |
| 396 | attr->u.uprobe.fd = |
| 397 | lttng_userspace_probe_location_function_get_binary_fd(location); |
| 398 | |
| 399 | /* |
| 400 | * Save a reference to the probe location used during |
| 401 | * the listing of events. |
| 402 | */ |
| 403 | userspace_probe_location = |
| 404 | lttng_userspace_probe_location_copy(location); |
| 405 | break; |
| 406 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: |
| 407 | /* Get the file descriptor on the target binary. */ |
| 408 | attr->u.uprobe.fd = |
| 409 | lttng_userspace_probe_location_tracepoint_get_binary_fd(location); |
| 410 | |
| 411 | /* |
| 412 | * Save a reference to the probe location used during the listing of |
| 413 | * events. |
| 414 | */ |
| 415 | userspace_probe_location = |
| 416 | lttng_userspace_probe_location_copy(location); |
| 417 | break; |
| 418 | default: |
| 419 | DBG("Unsupported lookup method type"); |
| 420 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 421 | goto error; |
| 422 | } |
| 423 | break; |
| 424 | } |
| 425 | case LTTNG_EVENT_FUNCTION: |
| 426 | attr->instrumentation = LTTNG_KERNEL_ABI_KRETPROBE; |
| 427 | attr->u.kretprobe.addr = ev->attr.probe.addr; |
| 428 | attr->u.kretprobe.offset = ev->attr.probe.offset; |
| 429 | strncpy(attr->u.kretprobe.symbol_name, |
| 430 | ev->attr.probe.symbol_name, LTTNG_KERNEL_ABI_SYM_NAME_LEN); |
| 431 | attr->u.kretprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; |
| 432 | break; |
| 433 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 434 | attr->instrumentation = LTTNG_KERNEL_ABI_FUNCTION; |
| 435 | strncpy(attr->u.ftrace.symbol_name, |
| 436 | ev->attr.ftrace.symbol_name, LTTNG_KERNEL_ABI_SYM_NAME_LEN); |
| 437 | attr->u.ftrace.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; |
| 438 | break; |
| 439 | case LTTNG_EVENT_TRACEPOINT: |
| 440 | attr->instrumentation = LTTNG_KERNEL_ABI_TRACEPOINT; |
| 441 | break; |
| 442 | case LTTNG_EVENT_SYSCALL: |
| 443 | attr->instrumentation = LTTNG_KERNEL_ABI_SYSCALL; |
| 444 | attr->u.syscall.abi = LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL; |
| 445 | attr->u.syscall.entryexit = LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT; |
| 446 | attr->u.syscall.match = LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME; |
| 447 | break; |
| 448 | case LTTNG_EVENT_ALL: |
| 449 | attr->instrumentation = LTTNG_KERNEL_ABI_ALL; |
| 450 | break; |
| 451 | default: |
| 452 | ERR("Unknown kernel instrumentation type (%d)", ev->type); |
| 453 | ret = LTTNG_ERR_INVALID; |
| 454 | goto error; |
| 455 | } |
| 456 | |
| 457 | /* Copy event name */ |
| 458 | strncpy(attr->name, ev->name, LTTNG_KERNEL_ABI_SYM_NAME_LEN); |
| 459 | attr->name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; |
| 460 | |
| 461 | /* Setting up a kernel event */ |
| 462 | local_kernel_event->fd = -1; |
| 463 | local_kernel_event->event = attr; |
| 464 | local_kernel_event->enabled = 1; |
| 465 | local_kernel_event->filter_expression = filter_expression; |
| 466 | local_kernel_event->filter = filter; |
| 467 | local_kernel_event->userspace_probe_location = userspace_probe_location; |
| 468 | |
| 469 | *kernel_event = local_kernel_event; |
| 470 | |
| 471 | return LTTNG_OK; |
| 472 | |
| 473 | error: |
| 474 | free(filter_expression); |
| 475 | free(filter); |
| 476 | free(local_kernel_event); |
| 477 | free(attr); |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Allocate and initialize a kernel token event rule. |
| 483 | * |
| 484 | * Return pointer to structure or NULL. |
| 485 | */ |
| 486 | enum lttng_error_code trace_kernel_create_event_notifier_rule( |
| 487 | struct lttng_trigger *trigger, |
| 488 | uint64_t token, |
| 489 | uint64_t error_counter_index, |
| 490 | struct ltt_kernel_event_notifier_rule **event_notifier_rule) |
| 491 | { |
| 492 | enum lttng_error_code ret = LTTNG_OK; |
| 493 | enum lttng_condition_type condition_type; |
| 494 | enum lttng_event_rule_type event_rule_type; |
| 495 | enum lttng_condition_status condition_status; |
| 496 | struct ltt_kernel_event_notifier_rule *local_kernel_token_event_rule; |
| 497 | const struct lttng_condition *condition = NULL; |
| 498 | const struct lttng_event_rule *event_rule = NULL; |
| 499 | |
| 500 | assert(event_notifier_rule); |
| 501 | |
| 502 | condition = lttng_trigger_get_const_condition(trigger); |
| 503 | assert(condition); |
| 504 | |
| 505 | condition_type = lttng_condition_get_type(condition); |
| 506 | assert(condition_type == LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
| 507 | |
| 508 | condition_status = lttng_condition_event_rule_matches_get_rule( |
| 509 | condition, &event_rule); |
| 510 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); |
| 511 | assert(event_rule); |
| 512 | |
| 513 | event_rule_type = lttng_event_rule_get_type(event_rule); |
| 514 | assert(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN); |
| 515 | |
| 516 | local_kernel_token_event_rule = |
| 517 | zmalloc(sizeof(struct ltt_kernel_event_notifier_rule)); |
| 518 | if (local_kernel_token_event_rule == NULL) { |
| 519 | PERROR("Failed to allocate ltt_kernel_token_event_rule structure"); |
| 520 | ret = LTTNG_ERR_NOMEM; |
| 521 | goto error; |
| 522 | } |
| 523 | |
| 524 | local_kernel_token_event_rule->fd = -1; |
| 525 | local_kernel_token_event_rule->enabled = 1; |
| 526 | local_kernel_token_event_rule->token = token; |
| 527 | local_kernel_token_event_rule->error_counter_index = error_counter_index; |
| 528 | |
| 529 | /* Get the reference of the event rule. */ |
| 530 | lttng_trigger_get(trigger); |
| 531 | |
| 532 | local_kernel_token_event_rule->trigger = trigger; |
| 533 | /* The event rule still owns the filter and bytecode. */ |
| 534 | local_kernel_token_event_rule->filter = |
| 535 | lttng_event_rule_get_filter_bytecode(event_rule); |
| 536 | |
| 537 | DBG3("Created kernel event notifier rule: token = %" PRIu64, |
| 538 | local_kernel_token_event_rule->token); |
| 539 | error: |
| 540 | *event_notifier_rule = local_kernel_token_event_rule; |
| 541 | return ret; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * Initialize a kernel trigger from an event rule. |
| 546 | */ |
| 547 | enum lttng_error_code trace_kernel_init_event_notifier_from_event_rule( |
| 548 | const struct lttng_event_rule *rule, |
| 549 | struct lttng_kernel_abi_event_notifier *kernel_event_notifier) |
| 550 | { |
| 551 | enum lttng_error_code ret_code; |
| 552 | const char *name; |
| 553 | int strncpy_ret; |
| 554 | |
| 555 | switch (lttng_event_rule_get_type(rule)) { |
| 556 | case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE: |
| 557 | { |
| 558 | uint64_t address = 0, offset = 0; |
| 559 | const char *symbol_name = NULL; |
| 560 | const struct lttng_kernel_probe_location *location = NULL; |
| 561 | enum lttng_kernel_probe_location_status k_status; |
| 562 | enum lttng_event_rule_status status; |
| 563 | |
| 564 | status = lttng_event_rule_kernel_kprobe_get_location(rule, &location); |
| 565 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
| 566 | ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 567 | goto error; |
| 568 | } |
| 569 | |
| 570 | switch (lttng_kernel_probe_location_get_type(location)) { |
| 571 | case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS: |
| 572 | { |
| 573 | k_status = lttng_kernel_probe_location_address_get_address( |
| 574 | location, &address); |
| 575 | assert(k_status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK); |
| 576 | break; |
| 577 | } |
| 578 | case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET: |
| 579 | { |
| 580 | k_status = lttng_kernel_probe_location_symbol_get_offset( |
| 581 | location, &offset); |
| 582 | assert(k_status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK); |
| 583 | symbol_name = lttng_kernel_probe_location_symbol_get_name( |
| 584 | location); |
| 585 | break; |
| 586 | } |
| 587 | default: |
| 588 | abort(); |
| 589 | } |
| 590 | |
| 591 | kernel_event_notifier->event.instrumentation = LTTNG_KERNEL_ABI_KPROBE; |
| 592 | kernel_event_notifier->event.u.kprobe.addr = address; |
| 593 | kernel_event_notifier->event.u.kprobe.offset = offset; |
| 594 | if (symbol_name) { |
| 595 | strncpy_ret = lttng_strncpy( |
| 596 | kernel_event_notifier->event.u.kprobe.symbol_name, |
| 597 | symbol_name, LTTNG_KERNEL_ABI_SYM_NAME_LEN); |
| 598 | |
| 599 | if (strncpy_ret) { |
| 600 | ret_code = LTTNG_ERR_INVALID; |
| 601 | goto error; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | kernel_event_notifier->event.u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; |
| 606 | |
| 607 | status = lttng_event_rule_kernel_kprobe_get_event_name(rule, &name); |
| 608 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 609 | ret_code = LTTNG_OK; |
| 610 | break; |
| 611 | } |
| 612 | case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE: |
| 613 | { |
| 614 | const struct lttng_userspace_probe_location* location = NULL; |
| 615 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; |
| 616 | enum lttng_event_rule_status status; |
| 617 | |
| 618 | status = lttng_event_rule_kernel_uprobe_get_location(rule, &location); |
| 619 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
| 620 | ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 621 | goto error; |
| 622 | } |
| 623 | |
| 624 | kernel_event_notifier->event.instrumentation = LTTNG_KERNEL_ABI_UPROBE; |
| 625 | |
| 626 | lookup = lttng_userspace_probe_location_get_lookup_method( |
| 627 | location); |
| 628 | if (!lookup) { |
| 629 | ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 630 | goto error; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * From the kernel tracer's perspective, all userspace probe |
| 635 | * event types are all the same: a file and an offset. |
| 636 | */ |
| 637 | switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) { |
| 638 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: |
| 639 | /* Get the file descriptor on the target binary. */ |
| 640 | kernel_event_notifier->event.u.uprobe.fd = |
| 641 | lttng_userspace_probe_location_function_get_binary_fd(location); |
| 642 | |
| 643 | break; |
| 644 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: |
| 645 | /* Get the file descriptor on the target binary. */ |
| 646 | kernel_event_notifier->event.u.uprobe.fd = |
| 647 | lttng_userspace_probe_location_tracepoint_get_binary_fd(location); |
| 648 | break; |
| 649 | default: |
| 650 | abort(); |
| 651 | } |
| 652 | |
| 653 | status = lttng_event_rule_kernel_uprobe_get_event_name( |
| 654 | rule, &name); |
| 655 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 656 | ret_code = LTTNG_OK; |
| 657 | break; |
| 658 | } |
| 659 | case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT: |
| 660 | { |
| 661 | const enum lttng_event_rule_status status = |
| 662 | lttng_event_rule_kernel_tracepoint_get_name_pattern( |
| 663 | rule, &name); |
| 664 | |
| 665 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 666 | kernel_event_notifier->event.instrumentation = |
| 667 | LTTNG_KERNEL_ABI_TRACEPOINT; |
| 668 | |
| 669 | ret_code = LTTNG_OK; |
| 670 | break; |
| 671 | } |
| 672 | case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL: |
| 673 | { |
| 674 | const enum lttng_event_rule_status status = |
| 675 | lttng_event_rule_kernel_syscall_get_name_pattern( |
| 676 | rule, &name); |
| 677 | const enum lttng_event_rule_kernel_syscall_emission_site |
| 678 | emission_site = |
| 679 | lttng_event_rule_kernel_syscall_get_emission_site(rule); |
| 680 | enum lttng_kernel_abi_syscall_entryexit entryexit; |
| 681 | |
| 682 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 683 | assert(emission_site != LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_UNKNOWN); |
| 684 | |
| 685 | switch(emission_site) { |
| 686 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY: |
| 687 | entryexit = LTTNG_KERNEL_ABI_SYSCALL_ENTRY; |
| 688 | break; |
| 689 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT: |
| 690 | entryexit = LTTNG_KERNEL_ABI_SYSCALL_EXIT; |
| 691 | break; |
| 692 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT: |
| 693 | entryexit = LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT; |
| 694 | break; |
| 695 | default: |
| 696 | abort(); |
| 697 | break; |
| 698 | } |
| 699 | |
| 700 | kernel_event_notifier->event.instrumentation = |
| 701 | LTTNG_KERNEL_ABI_SYSCALL; |
| 702 | kernel_event_notifier->event.u.syscall.abi = |
| 703 | LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL; |
| 704 | kernel_event_notifier->event.u.syscall.entryexit = |
| 705 | entryexit; |
| 706 | kernel_event_notifier->event.u.syscall.match = |
| 707 | LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME; |
| 708 | ret_code = LTTNG_OK; |
| 709 | break; |
| 710 | } |
| 711 | default: |
| 712 | abort(); |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | strncpy_ret = lttng_strncpy(kernel_event_notifier->event.name, name, |
| 717 | LTTNG_KERNEL_ABI_SYM_NAME_LEN); |
| 718 | if (strncpy_ret) { |
| 719 | ret_code = LTTNG_ERR_INVALID; |
| 720 | goto error; |
| 721 | } |
| 722 | |
| 723 | error: |
| 724 | return ret_code; |
| 725 | } |
| 726 | /* |
| 727 | * Allocate and initialize a kernel metadata. |
| 728 | * |
| 729 | * Return pointer to structure or NULL. |
| 730 | */ |
| 731 | struct ltt_kernel_metadata *trace_kernel_create_metadata(void) |
| 732 | { |
| 733 | int ret; |
| 734 | struct ltt_kernel_metadata *lkm; |
| 735 | struct lttng_channel *chan; |
| 736 | |
| 737 | lkm = zmalloc(sizeof(struct ltt_kernel_metadata)); |
| 738 | chan = zmalloc(sizeof(struct lttng_channel)); |
| 739 | if (lkm == NULL || chan == NULL) { |
| 740 | PERROR("kernel metadata zmalloc"); |
| 741 | goto error; |
| 742 | } |
| 743 | |
| 744 | ret = lttng_strncpy( |
| 745 | chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name)); |
| 746 | if (ret) { |
| 747 | ERR("Failed to initialize metadata channel name to `%s`", |
| 748 | DEFAULT_METADATA_NAME); |
| 749 | goto error; |
| 750 | } |
| 751 | |
| 752 | /* Set default attributes */ |
| 753 | chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE; |
| 754 | chan->attr.subbuf_size = default_get_metadata_subbuf_size(); |
| 755 | chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; |
| 756 | chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER; |
| 757 | chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;; |
| 758 | |
| 759 | |
| 760 | /* |
| 761 | * The metadata channel of kernel sessions must use the "mmap" |
| 762 | * back-end since the consumer daemon accumulates complete |
| 763 | * metadata units before sending them to the relay daemon in |
| 764 | * live mode. The consumer daemon also needs to extract the contents |
| 765 | * of the metadata cache when computing a rotation position. |
| 766 | * |
| 767 | * In both cases, it is not possible to rely on the splice |
| 768 | * back-end as the consumer daemon may need to accumulate more |
| 769 | * content than can be backed by the ring buffer's underlying |
| 770 | * pages. |
| 771 | */ |
| 772 | chan->attr.output = LTTNG_EVENT_MMAP; |
| 773 | chan->attr.tracefile_size = 0; |
| 774 | chan->attr.tracefile_count = 0; |
| 775 | chan->attr.live_timer_interval = 0; |
| 776 | |
| 777 | /* Init metadata */ |
| 778 | lkm->fd = -1; |
| 779 | lkm->conf = chan; |
| 780 | |
| 781 | return lkm; |
| 782 | |
| 783 | error: |
| 784 | free(lkm); |
| 785 | free(chan); |
| 786 | return NULL; |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by |
| 791 | * default. |
| 792 | * |
| 793 | * Return pointer to structure or NULL. |
| 794 | */ |
| 795 | struct ltt_kernel_stream *trace_kernel_create_stream(const char *name, |
| 796 | unsigned int count) |
| 797 | { |
| 798 | int ret; |
| 799 | struct ltt_kernel_stream *lks; |
| 800 | |
| 801 | assert(name); |
| 802 | |
| 803 | lks = zmalloc(sizeof(struct ltt_kernel_stream)); |
| 804 | if (lks == NULL) { |
| 805 | PERROR("kernel stream zmalloc"); |
| 806 | goto error; |
| 807 | } |
| 808 | |
| 809 | /* Set name */ |
| 810 | ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count); |
| 811 | if (ret < 0) { |
| 812 | PERROR("snprintf stream name"); |
| 813 | goto error; |
| 814 | } |
| 815 | lks->name[sizeof(lks->name) - 1] = '\0'; |
| 816 | |
| 817 | /* Init stream */ |
| 818 | lks->fd = -1; |
| 819 | lks->state = 0; |
| 820 | lks->cpu = count; |
| 821 | |
| 822 | return lks; |
| 823 | |
| 824 | error: |
| 825 | return NULL; |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * Cleanup kernel stream structure. |
| 830 | */ |
| 831 | void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream) |
| 832 | { |
| 833 | assert(stream); |
| 834 | |
| 835 | DBG("[trace] Closing stream fd %d", stream->fd); |
| 836 | /* Close kernel fd */ |
| 837 | if (stream->fd >= 0) { |
| 838 | int ret; |
| 839 | |
| 840 | ret = close(stream->fd); |
| 841 | if (ret) { |
| 842 | PERROR("close"); |
| 843 | } |
| 844 | } |
| 845 | /* Remove from stream list */ |
| 846 | cds_list_del(&stream->list); |
| 847 | |
| 848 | free(stream); |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Cleanup kernel event structure. |
| 853 | */ |
| 854 | void trace_kernel_destroy_event(struct ltt_kernel_event *event) |
| 855 | { |
| 856 | assert(event); |
| 857 | |
| 858 | if (event->fd >= 0) { |
| 859 | int ret; |
| 860 | |
| 861 | DBG("[trace] Closing event fd %d", event->fd); |
| 862 | /* Close kernel fd */ |
| 863 | ret = close(event->fd); |
| 864 | if (ret) { |
| 865 | PERROR("close"); |
| 866 | } |
| 867 | } else { |
| 868 | DBG("[trace] Tearing down event (no associated file descriptor)"); |
| 869 | } |
| 870 | |
| 871 | /* Remove from event list */ |
| 872 | cds_list_del(&event->list); |
| 873 | |
| 874 | free(event->filter_expression); |
| 875 | free(event->filter); |
| 876 | |
| 877 | free(event->event); |
| 878 | free(event); |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Cleanup kernel event structure. |
| 883 | */ |
| 884 | static void free_token_event_rule_rcu(struct rcu_head *rcu_node) |
| 885 | { |
| 886 | struct ltt_kernel_event_notifier_rule *rule = caa_container_of(rcu_node, |
| 887 | struct ltt_kernel_event_notifier_rule, rcu_node); |
| 888 | |
| 889 | free(rule); |
| 890 | } |
| 891 | |
| 892 | void trace_kernel_destroy_event_notifier_rule( |
| 893 | struct ltt_kernel_event_notifier_rule *event) |
| 894 | { |
| 895 | assert(event); |
| 896 | |
| 897 | if (event->fd >= 0) { |
| 898 | const int ret = close(event->fd); |
| 899 | |
| 900 | DBG("Closing kernel event notifier rule file descriptor: fd = %d", |
| 901 | event->fd); |
| 902 | if (ret) { |
| 903 | PERROR("Failed to close kernel event notifier file descriptor: fd = %d", |
| 904 | event->fd); |
| 905 | } |
| 906 | } else { |
| 907 | DBG("Destroying kernel event notifier rule (no associated file descriptor)"); |
| 908 | } |
| 909 | |
| 910 | lttng_trigger_put(event->trigger); |
| 911 | call_rcu(&event->rcu_node, free_token_event_rule_rcu); |
| 912 | } |
| 913 | /* |
| 914 | * Cleanup kernel context structure. |
| 915 | */ |
| 916 | void trace_kernel_destroy_context(struct ltt_kernel_context *ctx) |
| 917 | { |
| 918 | assert(ctx); |
| 919 | |
| 920 | if (ctx->in_list) { |
| 921 | cds_list_del(&ctx->list); |
| 922 | } |
| 923 | free(ctx); |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Cleanup kernel channel structure. |
| 928 | */ |
| 929 | void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel) |
| 930 | { |
| 931 | struct ltt_kernel_stream *stream, *stmp; |
| 932 | struct ltt_kernel_event *event, *etmp; |
| 933 | struct ltt_kernel_context *ctx, *ctmp; |
| 934 | int ret; |
| 935 | enum lttng_error_code status; |
| 936 | |
| 937 | assert(channel); |
| 938 | |
| 939 | DBG("[trace] Closing channel fd %d", channel->fd); |
| 940 | /* Close kernel fd */ |
| 941 | if (channel->fd >= 0) { |
| 942 | ret = close(channel->fd); |
| 943 | if (ret) { |
| 944 | PERROR("close"); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | /* For each stream in the channel list */ |
| 949 | cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) { |
| 950 | trace_kernel_destroy_stream(stream); |
| 951 | } |
| 952 | |
| 953 | /* For each event in the channel list */ |
| 954 | cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) { |
| 955 | trace_kernel_destroy_event(event); |
| 956 | } |
| 957 | |
| 958 | /* For each context in the channel list */ |
| 959 | cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) { |
| 960 | trace_kernel_destroy_context(ctx); |
| 961 | } |
| 962 | |
| 963 | /* Remove from channel list */ |
| 964 | cds_list_del(&channel->list); |
| 965 | |
| 966 | if (the_notification_thread_handle && |
| 967 | channel->published_to_notification_thread) { |
| 968 | status = notification_thread_command_remove_channel( |
| 969 | the_notification_thread_handle, channel->key, |
| 970 | LTTNG_DOMAIN_KERNEL); |
| 971 | assert(status == LTTNG_OK); |
| 972 | } |
| 973 | free(channel->channel->attr.extended.ptr); |
| 974 | free(channel->channel); |
| 975 | free(channel); |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Cleanup kernel metadata structure. |
| 980 | */ |
| 981 | void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata) |
| 982 | { |
| 983 | assert(metadata); |
| 984 | |
| 985 | DBG("[trace] Closing metadata fd %d", metadata->fd); |
| 986 | /* Close kernel fd */ |
| 987 | if (metadata->fd >= 0) { |
| 988 | int ret; |
| 989 | |
| 990 | ret = close(metadata->fd); |
| 991 | if (ret) { |
| 992 | PERROR("close"); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | free(metadata->conf); |
| 997 | free(metadata); |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * Cleanup kernel session structure |
| 1002 | * |
| 1003 | * Should *NOT* be called with RCU read-side lock held. |
| 1004 | */ |
| 1005 | void trace_kernel_destroy_session(struct ltt_kernel_session *session) |
| 1006 | { |
| 1007 | struct ltt_kernel_channel *channel, *ctmp; |
| 1008 | int ret; |
| 1009 | |
| 1010 | assert(session); |
| 1011 | |
| 1012 | DBG("[trace] Closing session fd %d", session->fd); |
| 1013 | /* Close kernel fds */ |
| 1014 | if (session->fd >= 0) { |
| 1015 | ret = close(session->fd); |
| 1016 | if (ret) { |
| 1017 | PERROR("close"); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | if (session->metadata_stream_fd >= 0) { |
| 1022 | DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd); |
| 1023 | ret = close(session->metadata_stream_fd); |
| 1024 | if (ret) { |
| 1025 | PERROR("close"); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | if (session->metadata != NULL) { |
| 1030 | trace_kernel_destroy_metadata(session->metadata); |
| 1031 | } |
| 1032 | |
| 1033 | cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) { |
| 1034 | trace_kernel_destroy_channel(channel); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /* Free elements needed by destroy notifiers. */ |
| 1039 | void trace_kernel_free_session(struct ltt_kernel_session *session) |
| 1040 | { |
| 1041 | /* Wipe consumer output object */ |
| 1042 | consumer_output_put(session->consumer); |
| 1043 | |
| 1044 | process_attr_tracker_destroy(session->tracker_pid); |
| 1045 | process_attr_tracker_destroy(session->tracker_vpid); |
| 1046 | process_attr_tracker_destroy(session->tracker_uid); |
| 1047 | process_attr_tracker_destroy(session->tracker_vuid); |
| 1048 | process_attr_tracker_destroy(session->tracker_gid); |
| 1049 | process_attr_tracker_destroy(session->tracker_vgid); |
| 1050 | |
| 1051 | free(session); |
| 1052 | } |