| 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-function.h> |
| 17 | #include <lttng/kernel-probe.h> |
| 18 | #include <lttng/userspace-probe.h> |
| 19 | #include <lttng/userspace-probe-internal.h> |
| 20 | #include <lttng/event-rule/event-rule.h> |
| 21 | #include <lttng/event-rule/event-rule-internal.h> |
| 22 | #include <lttng/event-rule/kernel-function.h> |
| 23 | #include <lttng/event-rule/kernel-function-internal.h> |
| 24 | #include <lttng/event-rule/kernel-probe.h> |
| 25 | #include <lttng/event-rule/kernel-probe-internal.h> |
| 26 | #include <lttng/event-rule/syscall.h> |
| 27 | #include <lttng/event-rule/syscall-internal.h> |
| 28 | #include <lttng/event-rule/tracepoint.h> |
| 29 | #include <lttng/event-rule/tracepoint-internal.h> |
| 30 | #include <lttng/event-rule/userspace-probe-internal.h> |
| 31 | #include <lttng/map/map.h> |
| 32 | #include <lttng/map-key-internal.h> |
| 33 | #include <common/common.h> |
| 34 | #include <common/defaults.h> |
| 35 | #include <common/trace-chunk.h> |
| 36 | #include <common/macros.h> |
| 37 | |
| 38 | #include "consumer.h" |
| 39 | #include "trace-kernel.h" |
| 40 | #include "lttng-sessiond.h" |
| 41 | #include "notification-thread-commands.h" |
| 42 | |
| 43 | /* Next available map key. Access under next_map_key_lock. */ |
| 44 | static uint64_t _next_map_key; |
| 45 | static pthread_mutex_t next_map_key_lock = PTHREAD_MUTEX_INITIALIZER; |
| 46 | |
| 47 | /* |
| 48 | * Return the incremented value of next_map_key. |
| 49 | */ |
| 50 | static uint64_t get_next_map_key(void) |
| 51 | { |
| 52 | uint64_t ret; |
| 53 | |
| 54 | pthread_mutex_lock(&next_map_key_lock); |
| 55 | ret = ++_next_map_key; |
| 56 | pthread_mutex_unlock(&next_map_key_lock); |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Find the channel name for the given kernel session. |
| 62 | */ |
| 63 | struct ltt_kernel_channel *trace_kernel_get_channel_by_name( |
| 64 | const char *name, struct ltt_kernel_session *session) |
| 65 | { |
| 66 | struct ltt_kernel_channel *chan; |
| 67 | |
| 68 | assert(session); |
| 69 | assert(name); |
| 70 | |
| 71 | /* |
| 72 | * If we receive an empty string for channel name, it means the |
| 73 | * default channel name is requested. |
| 74 | */ |
| 75 | if (name[0] == '\0') |
| 76 | name = DEFAULT_CHANNEL_NAME; |
| 77 | |
| 78 | DBG("Trying to find channel %s", name); |
| 79 | |
| 80 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
| 81 | if (strcmp(name, chan->channel->name) == 0) { |
| 82 | DBG("Found channel by name %s", name); |
| 83 | return chan; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Find the map name for the given kernel session. |
| 92 | */ |
| 93 | struct ltt_kernel_map *trace_kernel_get_map_by_name( |
| 94 | const char *name, struct ltt_kernel_session *session) |
| 95 | { |
| 96 | struct ltt_kernel_map *map; |
| 97 | |
| 98 | assert(session); |
| 99 | assert(name); |
| 100 | |
| 101 | DBG("Trying to find map %s", name); |
| 102 | |
| 103 | cds_list_for_each_entry(map, &session->map_list.head, list) { |
| 104 | enum lttng_map_status status; |
| 105 | const char *cur_map_name; |
| 106 | |
| 107 | status = lttng_map_get_name(map->map, &cur_map_name); |
| 108 | assert(status == LTTNG_MAP_STATUS_OK); |
| 109 | |
| 110 | if (strcmp(name, cur_map_name) == 0) { |
| 111 | DBG("Found map by name %s", name); |
| 112 | return map; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Find the event for the given channel or map. |
| 121 | */ |
| 122 | struct ltt_kernel_event *trace_kernel_find_event( |
| 123 | struct ltt_kernel_event_list *events_list, |
| 124 | uint64_t tracer_token, char *name, |
| 125 | enum lttng_event_type type, |
| 126 | struct lttng_bytecode *filter) |
| 127 | { |
| 128 | struct ltt_kernel_event *ev; |
| 129 | int found = 0; |
| 130 | |
| 131 | assert(name); |
| 132 | |
| 133 | cds_list_for_each_entry(ev, &events_list->head, list) { |
| 134 | if (ev->event->token != tracer_token) { |
| 135 | continue; |
| 136 | } |
| 137 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
| 138 | continue; |
| 139 | } |
| 140 | if (strcmp(name, ev->event->name)) { |
| 141 | continue; |
| 142 | } |
| 143 | if ((ev->filter && !filter) || (!ev->filter && filter)) { |
| 144 | continue; |
| 145 | } |
| 146 | if (ev->filter && filter) { |
| 147 | if (ev->filter->len != filter->len || |
| 148 | memcmp(ev->filter->data, filter->data, |
| 149 | filter->len) != 0) { |
| 150 | continue; |
| 151 | } |
| 152 | } |
| 153 | found = 1; |
| 154 | break; |
| 155 | } |
| 156 | if (found) { |
| 157 | DBG("Kernel event %s found", name); |
| 158 | return ev; |
| 159 | } else { |
| 160 | DBG("Kernel event %s not found", name); |
| 161 | return NULL; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Find the event name for the given channel. |
| 167 | */ |
| 168 | struct ltt_kernel_event *trace_kernel_get_event_by_name( |
| 169 | char *name, struct ltt_kernel_channel *channel, |
| 170 | enum lttng_event_type type) |
| 171 | { |
| 172 | struct ltt_kernel_event *ev; |
| 173 | int found = 0; |
| 174 | |
| 175 | assert(name); |
| 176 | assert(channel); |
| 177 | |
| 178 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { |
| 179 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
| 180 | continue; |
| 181 | } |
| 182 | if (strcmp(name, ev->event->name)) { |
| 183 | continue; |
| 184 | } |
| 185 | found = 1; |
| 186 | break; |
| 187 | } |
| 188 | if (found) { |
| 189 | DBG("Found event %s for channel %s", name, |
| 190 | channel->channel->name); |
| 191 | return ev; |
| 192 | } else { |
| 193 | return NULL; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Allocate and initialize a kernel session data structure. |
| 199 | * |
| 200 | * Return pointer to structure or NULL. |
| 201 | */ |
| 202 | struct ltt_kernel_session *trace_kernel_create_session(void) |
| 203 | { |
| 204 | struct ltt_kernel_session *lks = NULL; |
| 205 | |
| 206 | /* Allocate a new ltt kernel session */ |
| 207 | lks = zmalloc(sizeof(struct ltt_kernel_session)); |
| 208 | if (lks == NULL) { |
| 209 | PERROR("create kernel session zmalloc"); |
| 210 | goto alloc_error; |
| 211 | } |
| 212 | |
| 213 | /* Init data structure */ |
| 214 | lks->fd = -1; |
| 215 | lks->metadata_stream_fd = -1; |
| 216 | lks->channel_count = 0; |
| 217 | lks->map_count = 0; |
| 218 | lks->stream_count_global = 0; |
| 219 | lks->metadata = NULL; |
| 220 | CDS_INIT_LIST_HEAD(&lks->channel_list.head); |
| 221 | CDS_INIT_LIST_HEAD(&lks->map_list.head); |
| 222 | |
| 223 | lks->tracker_pid = process_attr_tracker_create(); |
| 224 | if (!lks->tracker_pid) { |
| 225 | goto error; |
| 226 | } |
| 227 | lks->tracker_vpid = process_attr_tracker_create(); |
| 228 | if (!lks->tracker_vpid) { |
| 229 | goto error; |
| 230 | } |
| 231 | lks->tracker_uid = process_attr_tracker_create(); |
| 232 | if (!lks->tracker_uid) { |
| 233 | goto error; |
| 234 | } |
| 235 | lks->tracker_vuid = process_attr_tracker_create(); |
| 236 | if (!lks->tracker_vuid) { |
| 237 | goto error; |
| 238 | } |
| 239 | lks->tracker_gid = process_attr_tracker_create(); |
| 240 | if (!lks->tracker_gid) { |
| 241 | goto error; |
| 242 | } |
| 243 | lks->tracker_vgid = process_attr_tracker_create(); |
| 244 | if (!lks->tracker_vgid) { |
| 245 | goto error; |
| 246 | } |
| 247 | lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
| 248 | if (lks->consumer == NULL) { |
| 249 | goto error; |
| 250 | } |
| 251 | |
| 252 | return lks; |
| 253 | |
| 254 | error: |
| 255 | process_attr_tracker_destroy(lks->tracker_pid); |
| 256 | process_attr_tracker_destroy(lks->tracker_vpid); |
| 257 | process_attr_tracker_destroy(lks->tracker_uid); |
| 258 | process_attr_tracker_destroy(lks->tracker_vuid); |
| 259 | process_attr_tracker_destroy(lks->tracker_gid); |
| 260 | process_attr_tracker_destroy(lks->tracker_vgid); |
| 261 | free(lks); |
| 262 | |
| 263 | alloc_error: |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Allocate and initialize a kernel channel data structure. |
| 269 | * |
| 270 | * Return pointer to structure or NULL. |
| 271 | */ |
| 272 | struct ltt_kernel_channel *trace_kernel_create_channel( |
| 273 | struct lttng_channel *chan) |
| 274 | { |
| 275 | struct ltt_kernel_channel *lkc; |
| 276 | struct lttng_channel_extended *extended = NULL; |
| 277 | |
| 278 | assert(chan); |
| 279 | |
| 280 | lkc = zmalloc(sizeof(struct ltt_kernel_channel)); |
| 281 | if (lkc == NULL) { |
| 282 | PERROR("ltt_kernel_channel zmalloc"); |
| 283 | goto error; |
| 284 | } |
| 285 | |
| 286 | lkc->channel = zmalloc(sizeof(struct lttng_channel)); |
| 287 | if (lkc->channel == NULL) { |
| 288 | PERROR("lttng_channel zmalloc"); |
| 289 | goto error; |
| 290 | } |
| 291 | |
| 292 | extended = zmalloc(sizeof(struct lttng_channel_extended)); |
| 293 | if (!extended) { |
| 294 | PERROR("lttng_channel_channel zmalloc"); |
| 295 | goto error; |
| 296 | } |
| 297 | memcpy(lkc->channel, chan, sizeof(struct lttng_channel)); |
| 298 | memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended)); |
| 299 | lkc->channel->attr.extended.ptr = extended; |
| 300 | extended = NULL; |
| 301 | |
| 302 | /* |
| 303 | * If we receive an empty string for channel name, it means the |
| 304 | * default channel name is requested. |
| 305 | */ |
| 306 | if (chan->name[0] == '\0') { |
| 307 | strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME, |
| 308 | sizeof(lkc->channel->name)); |
| 309 | } |
| 310 | lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 311 | |
| 312 | lkc->fd = -1; |
| 313 | lkc->stream_count = 0; |
| 314 | lkc->event_count = 0; |
| 315 | lkc->enabled = 1; |
| 316 | lkc->published_to_notification_thread = false; |
| 317 | /* Init linked list */ |
| 318 | CDS_INIT_LIST_HEAD(&lkc->events_list.head); |
| 319 | CDS_INIT_LIST_HEAD(&lkc->stream_list.head); |
| 320 | CDS_INIT_LIST_HEAD(&lkc->ctx_list); |
| 321 | |
| 322 | return lkc; |
| 323 | |
| 324 | error: |
| 325 | if (lkc) { |
| 326 | free(lkc->channel); |
| 327 | } |
| 328 | free(extended); |
| 329 | free(lkc); |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | struct ltt_kernel_map *trace_kernel_create_map( |
| 334 | const struct lttng_map *map) |
| 335 | { |
| 336 | struct ltt_kernel_map *kernel_map = NULL; |
| 337 | unsigned int i, number_dimensions; |
| 338 | |
| 339 | kernel_map = zmalloc(sizeof(*kernel_map)); |
| 340 | if (!kernel_map) { |
| 341 | PERROR("ltt_kernel_map zmalloc"); |
| 342 | goto end; |
| 343 | } |
| 344 | |
| 345 | switch (lttng_map_get_boundary_policy(map)) { |
| 346 | case LTTNG_MAP_BOUNDARY_POLICY_OVERFLOW: |
| 347 | kernel_map->counter_conf.arithmetic = LTTNG_KERNEL_COUNTER_ARITHMETIC_MODULAR; |
| 348 | break; |
| 349 | default: |
| 350 | abort(); |
| 351 | } |
| 352 | |
| 353 | switch (lttng_map_get_bitness(map)) { |
| 354 | case LTTNG_MAP_BITNESS_32BITS: |
| 355 | kernel_map->counter_conf.bitness = LTTNG_KERNEL_COUNTER_BITNESS_32; |
| 356 | break; |
| 357 | case LTTNG_MAP_BITNESS_64BITS: |
| 358 | kernel_map->counter_conf.bitness = LTTNG_KERNEL_COUNTER_BITNESS_64; |
| 359 | break; |
| 360 | default: |
| 361 | abort(); |
| 362 | } |
| 363 | |
| 364 | kernel_map->counter_conf.coalesce_hits = lttng_map_get_coalesce_hits(map); |
| 365 | |
| 366 | number_dimensions = lttng_map_get_dimension_count(map); |
| 367 | assert(number_dimensions <= LTTNG_KERNEL_COUNTER_DIMENSION_MAX); |
| 368 | |
| 369 | kernel_map->counter_conf.number_dimensions = number_dimensions; |
| 370 | |
| 371 | for (i = 0; i < kernel_map->counter_conf.number_dimensions; i++) { |
| 372 | enum lttng_map_status map_status; |
| 373 | uint64_t dimension_length; |
| 374 | |
| 375 | map_status = lttng_map_get_dimension_length(map, i, |
| 376 | &dimension_length); |
| 377 | assert(map_status == LTTNG_MAP_STATUS_OK); |
| 378 | |
| 379 | kernel_map->counter_conf.dimensions[i].size = dimension_length; |
| 380 | |
| 381 | //FIXME: We need to turn on overflow and underflow |
| 382 | kernel_map->counter_conf.dimensions[i].has_overflow = false; |
| 383 | kernel_map->counter_conf.dimensions[i].has_underflow = false; |
| 384 | } |
| 385 | |
| 386 | kernel_map->fd = -1; |
| 387 | kernel_map->enabled = 1; |
| 388 | kernel_map->key = get_next_map_key(); |
| 389 | |
| 390 | kernel_map->event_counters_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 391 | end: |
| 392 | return kernel_map; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Allocate and init a kernel context object. |
| 397 | * |
| 398 | * Return the allocated object or NULL on error. |
| 399 | */ |
| 400 | struct ltt_kernel_context *trace_kernel_create_context( |
| 401 | struct lttng_kernel_context *ctx) |
| 402 | { |
| 403 | struct ltt_kernel_context *kctx; |
| 404 | |
| 405 | kctx = zmalloc(sizeof(*kctx)); |
| 406 | if (!kctx) { |
| 407 | PERROR("zmalloc kernel context"); |
| 408 | goto error; |
| 409 | } |
| 410 | |
| 411 | if (ctx) { |
| 412 | memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx)); |
| 413 | } |
| 414 | error: |
| 415 | return kctx; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Allocate and init a kernel context object from an existing kernel context |
| 420 | * object. |
| 421 | * |
| 422 | * Return the allocated object or NULL on error. |
| 423 | */ |
| 424 | struct ltt_kernel_context *trace_kernel_copy_context( |
| 425 | struct ltt_kernel_context *kctx) |
| 426 | { |
| 427 | struct ltt_kernel_context *kctx_copy; |
| 428 | |
| 429 | assert(kctx); |
| 430 | kctx_copy = zmalloc(sizeof(*kctx_copy)); |
| 431 | if (!kctx_copy) { |
| 432 | PERROR("zmalloc ltt_kernel_context"); |
| 433 | goto error; |
| 434 | } |
| 435 | |
| 436 | memcpy(kctx_copy, kctx, sizeof(*kctx_copy)); |
| 437 | memset(&kctx_copy->list, 0, sizeof(kctx_copy->list)); |
| 438 | |
| 439 | error: |
| 440 | return kctx_copy; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Allocate and initialize a kernel event. Set name and event type. |
| 445 | * We own filter_expression, and filter. |
| 446 | * |
| 447 | * Return pointer to structure or NULL. |
| 448 | */ |
| 449 | enum lttng_error_code trace_kernel_create_event( |
| 450 | struct lttng_event *ev, char *filter_expression, |
| 451 | struct lttng_bytecode *filter, |
| 452 | struct ltt_kernel_event **kernel_event) |
| 453 | { |
| 454 | enum lttng_error_code ret; |
| 455 | struct lttng_kernel_event *attr; |
| 456 | struct ltt_kernel_event *local_kernel_event; |
| 457 | struct lttng_userspace_probe_location *userspace_probe_location = NULL; |
| 458 | |
| 459 | assert(ev); |
| 460 | |
| 461 | local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event)); |
| 462 | attr = zmalloc(sizeof(struct lttng_kernel_event)); |
| 463 | if (local_kernel_event == NULL || attr == NULL) { |
| 464 | PERROR("kernel event zmalloc"); |
| 465 | ret = LTTNG_ERR_NOMEM; |
| 466 | goto error; |
| 467 | } |
| 468 | |
| 469 | switch (ev->type) { |
| 470 | case LTTNG_EVENT_PROBE: |
| 471 | attr->instrumentation = LTTNG_KERNEL_KPROBE; |
| 472 | attr->u.kprobe.addr = ev->attr.probe.addr; |
| 473 | attr->u.kprobe.offset = ev->attr.probe.offset; |
| 474 | strncpy(attr->u.kprobe.symbol_name, |
| 475 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 476 | attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 477 | break; |
| 478 | case LTTNG_EVENT_USERSPACE_PROBE: |
| 479 | { |
| 480 | const struct lttng_userspace_probe_location* location = NULL; |
| 481 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; |
| 482 | |
| 483 | location = lttng_event_get_userspace_probe_location(ev); |
| 484 | if (!location) { |
| 485 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 486 | goto error; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * From this point on, the specific term 'uprobe' is used |
| 491 | * instead of the generic 'userspace probe' because it's the |
| 492 | * technology used at the moment for this instrumentation. |
| 493 | * LTTng currently implements userspace probes using uprobes. |
| 494 | * In the interactions with the kernel tracer, we use the |
| 495 | * uprobe term. |
| 496 | */ |
| 497 | attr->instrumentation = LTTNG_KERNEL_UPROBE; |
| 498 | |
| 499 | lookup = lttng_userspace_probe_location_get_lookup_method( |
| 500 | location); |
| 501 | if (!lookup) { |
| 502 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 503 | goto error; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * From the kernel tracer's perspective, all userspace probe |
| 508 | * event types are all the same: a file and an offset. |
| 509 | */ |
| 510 | switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) { |
| 511 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: |
| 512 | /* Get the file descriptor on the target binary. */ |
| 513 | attr->u.uprobe.fd = |
| 514 | lttng_userspace_probe_location_function_get_binary_fd(location); |
| 515 | |
| 516 | /* |
| 517 | * Save a reference to the probe location used during |
| 518 | * the listing of events. |
| 519 | */ |
| 520 | userspace_probe_location = |
| 521 | lttng_userspace_probe_location_copy(location); |
| 522 | break; |
| 523 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: |
| 524 | /* Get the file descriptor on the target binary. */ |
| 525 | attr->u.uprobe.fd = |
| 526 | lttng_userspace_probe_location_tracepoint_get_binary_fd(location); |
| 527 | |
| 528 | /* |
| 529 | * Save a reference to the probe location used during the listing of |
| 530 | * events. |
| 531 | */ |
| 532 | userspace_probe_location = |
| 533 | lttng_userspace_probe_location_copy(location); |
| 534 | break; |
| 535 | default: |
| 536 | DBG("Unsupported lookup method type"); |
| 537 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 538 | goto error; |
| 539 | } |
| 540 | break; |
| 541 | } |
| 542 | case LTTNG_EVENT_FUNCTION: |
| 543 | attr->instrumentation = LTTNG_KERNEL_KRETPROBE; |
| 544 | attr->u.kretprobe.addr = ev->attr.probe.addr; |
| 545 | attr->u.kretprobe.offset = ev->attr.probe.offset; |
| 546 | strncpy(attr->u.kretprobe.symbol_name, |
| 547 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 548 | attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 549 | break; |
| 550 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 551 | attr->instrumentation = LTTNG_KERNEL_FUNCTION; |
| 552 | strncpy(attr->u.ftrace.symbol_name, |
| 553 | ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 554 | attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 555 | break; |
| 556 | case LTTNG_EVENT_TRACEPOINT: |
| 557 | attr->instrumentation = LTTNG_KERNEL_TRACEPOINT; |
| 558 | break; |
| 559 | case LTTNG_EVENT_SYSCALL: |
| 560 | attr->instrumentation = LTTNG_KERNEL_SYSCALL; |
| 561 | attr->u.syscall.abi = LTTNG_KERNEL_SYSCALL_ABI_ALL; |
| 562 | attr->u.syscall.entryexit = LTTNG_KERNEL_SYSCALL_ENTRYEXIT; |
| 563 | attr->u.syscall.match = LTTNG_KERNEL_SYSCALL_MATCH_NAME; |
| 564 | break; |
| 565 | case LTTNG_EVENT_ALL: |
| 566 | attr->instrumentation = LTTNG_KERNEL_ALL; |
| 567 | break; |
| 568 | default: |
| 569 | ERR("Unknown kernel instrumentation type (%d)", ev->type); |
| 570 | ret = LTTNG_ERR_INVALID; |
| 571 | goto error; |
| 572 | } |
| 573 | |
| 574 | /* Copy event name */ |
| 575 | strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 576 | attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 577 | |
| 578 | /* Setting up a kernel event */ |
| 579 | local_kernel_event->fd = -1; |
| 580 | local_kernel_event->event = attr; |
| 581 | local_kernel_event->enabled = 1; |
| 582 | local_kernel_event->filter_expression = filter_expression; |
| 583 | local_kernel_event->filter = filter; |
| 584 | local_kernel_event->userspace_probe_location = userspace_probe_location; |
| 585 | |
| 586 | *kernel_event = local_kernel_event; |
| 587 | |
| 588 | return LTTNG_OK; |
| 589 | |
| 590 | error: |
| 591 | free(filter_expression); |
| 592 | free(filter); |
| 593 | free(local_kernel_event); |
| 594 | free(attr); |
| 595 | return ret; |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | * Allocate and initialize a kernel token event rule. |
| 600 | * |
| 601 | * Return pointer to structure or NULL. |
| 602 | */ |
| 603 | enum lttng_error_code trace_kernel_create_event_notifier_rule( |
| 604 | struct lttng_trigger *trigger, |
| 605 | uint64_t token, |
| 606 | uint64_t error_counter_index, |
| 607 | struct ltt_kernel_event_notifier_rule **event_notifier_rule) |
| 608 | { |
| 609 | enum lttng_error_code ret = LTTNG_OK; |
| 610 | enum lttng_condition_type condition_type; |
| 611 | enum lttng_event_rule_type event_rule_type; |
| 612 | enum lttng_condition_status condition_status; |
| 613 | struct ltt_kernel_event_notifier_rule *local_kernel_token_event_rule; |
| 614 | const struct lttng_condition *condition = NULL; |
| 615 | const struct lttng_event_rule *event_rule = NULL; |
| 616 | |
| 617 | assert(event_notifier_rule); |
| 618 | |
| 619 | condition = lttng_trigger_get_condition(trigger); |
| 620 | assert(condition); |
| 621 | |
| 622 | condition_type = lttng_condition_get_type(condition); |
| 623 | assert(condition_type == LTTNG_CONDITION_TYPE_ON_EVENT); |
| 624 | |
| 625 | condition_status = lttng_condition_on_event_get_rule( |
| 626 | condition, &event_rule); |
| 627 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); |
| 628 | assert(event_rule); |
| 629 | |
| 630 | event_rule_type = lttng_event_rule_get_type(event_rule); |
| 631 | assert(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN); |
| 632 | |
| 633 | local_kernel_token_event_rule = |
| 634 | zmalloc(sizeof(struct ltt_kernel_event_notifier_rule)); |
| 635 | if (local_kernel_token_event_rule == NULL) { |
| 636 | PERROR("Failed to allocate ltt_kernel_token_event_rule structure"); |
| 637 | ret = LTTNG_ERR_NOMEM; |
| 638 | goto error; |
| 639 | } |
| 640 | |
| 641 | local_kernel_token_event_rule->fd = -1; |
| 642 | local_kernel_token_event_rule->enabled = 1; |
| 643 | local_kernel_token_event_rule->token = token; |
| 644 | local_kernel_token_event_rule->error_counter_index = error_counter_index; |
| 645 | |
| 646 | /* Get the reference of the event rule. */ |
| 647 | lttng_trigger_get(trigger); |
| 648 | |
| 649 | local_kernel_token_event_rule->trigger = trigger; |
| 650 | /* The event rule still owns the filter and bytecode. */ |
| 651 | local_kernel_token_event_rule->filter = |
| 652 | lttng_event_rule_get_filter_bytecode(event_rule); |
| 653 | |
| 654 | DBG3("Created kernel event notifier rule: token = %" PRIu64, |
| 655 | local_kernel_token_event_rule->token); |
| 656 | error: |
| 657 | *event_notifier_rule = local_kernel_token_event_rule; |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | enum trace_kernel_event_type { |
| 662 | TRACE_KERNEL_EVENT_TYPE_NOTIFIER, |
| 663 | TRACE_KERNEL_EVENT_TYPE_COUNTER, |
| 664 | }; |
| 665 | |
| 666 | /* |
| 667 | * Initialize a kernel event from an event rule. |
| 668 | */ |
| 669 | static |
| 670 | enum lttng_error_code trace_kernel_init_event_from_event_rule( |
| 671 | const struct lttng_event_rule *rule, |
| 672 | struct lttng_kernel_event *kernel_event, |
| 673 | enum trace_kernel_event_type event_type) |
| 674 | { |
| 675 | enum lttng_error_code ret_code; |
| 676 | const char *name; |
| 677 | int strncpy_ret; |
| 678 | |
| 679 | switch (lttng_event_rule_get_type(rule)) { |
| 680 | case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE: |
| 681 | { |
| 682 | uint64_t address = 0, offset = 0; |
| 683 | const char *symbol_name = NULL; |
| 684 | const struct lttng_kernel_probe_location *location = NULL; |
| 685 | enum lttng_kernel_probe_location_status k_status; |
| 686 | enum lttng_event_rule_status status; |
| 687 | |
| 688 | status = lttng_event_rule_kernel_probe_get_location(rule, &location); |
| 689 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
| 690 | ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 691 | goto error; |
| 692 | } |
| 693 | |
| 694 | switch (lttng_kernel_probe_location_get_type(location)) { |
| 695 | case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS: |
| 696 | { |
| 697 | k_status = lttng_kernel_probe_location_address_get_address( |
| 698 | location, &address); |
| 699 | assert(k_status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK); |
| 700 | break; |
| 701 | } |
| 702 | case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET: |
| 703 | { |
| 704 | k_status = lttng_kernel_probe_location_symbol_get_offset( |
| 705 | location, &offset); |
| 706 | assert(k_status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK); |
| 707 | symbol_name = lttng_kernel_probe_location_symbol_get_name( |
| 708 | location); |
| 709 | break; |
| 710 | } |
| 711 | default: |
| 712 | abort(); |
| 713 | } |
| 714 | |
| 715 | kernel_event->instrumentation = LTTNG_KERNEL_KPROBE; |
| 716 | kernel_event->u.kprobe.addr = address; |
| 717 | kernel_event->u.kprobe.offset = offset; |
| 718 | if (symbol_name) { |
| 719 | strncpy_ret = lttng_strncpy( |
| 720 | kernel_event->u.kprobe.symbol_name, |
| 721 | symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 722 | |
| 723 | if (strncpy_ret) { |
| 724 | ret_code = LTTNG_ERR_INVALID; |
| 725 | goto error; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | kernel_event->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 730 | |
| 731 | status = lttng_event_rule_kernel_probe_get_event_name(rule, &name); |
| 732 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 733 | ret_code = LTTNG_OK; |
| 734 | break; |
| 735 | } |
| 736 | case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION: |
| 737 | { |
| 738 | uint64_t address = 0, offset = 0; |
| 739 | const char *symbol_name = NULL; |
| 740 | const struct lttng_kernel_function_location *location = NULL; |
| 741 | enum lttng_kernel_function_location_status k_status; |
| 742 | enum lttng_event_rule_status status; |
| 743 | |
| 744 | status = lttng_event_rule_kernel_function_get_location(rule, &location); |
| 745 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
| 746 | ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 747 | goto error; |
| 748 | } |
| 749 | |
| 750 | switch (lttng_kernel_function_location_get_type(location)) { |
| 751 | case LTTNG_KERNEL_FUNCTION_LOCATION_TYPE_ADDRESS: |
| 752 | { |
| 753 | k_status = lttng_kernel_function_location_address_get_address( |
| 754 | location, &address); |
| 755 | assert(k_status == LTTNG_KERNEL_FUNCTION_LOCATION_STATUS_OK); |
| 756 | break; |
| 757 | } |
| 758 | case LTTNG_KERNEL_FUNCTION_LOCATION_TYPE_SYMBOL_OFFSET: |
| 759 | { |
| 760 | k_status = lttng_kernel_function_location_symbol_get_offset( |
| 761 | location, &offset); |
| 762 | assert(k_status == LTTNG_KERNEL_FUNCTION_LOCATION_STATUS_OK); |
| 763 | symbol_name = lttng_kernel_function_location_symbol_get_name( |
| 764 | location); |
| 765 | break; |
| 766 | } |
| 767 | default: |
| 768 | abort(); |
| 769 | } |
| 770 | |
| 771 | kernel_event->instrumentation = LTTNG_KERNEL_KRETPROBE; |
| 772 | kernel_event->u.kretprobe.addr = address; |
| 773 | kernel_event->u.kretprobe.offset = offset; |
| 774 | if (symbol_name) { |
| 775 | strncpy_ret = lttng_strncpy( |
| 776 | kernel_event->u.kretprobe.symbol_name, |
| 777 | symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 778 | |
| 779 | if (strncpy_ret) { |
| 780 | ret_code = LTTNG_ERR_INVALID; |
| 781 | goto error; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | kernel_event->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 786 | |
| 787 | status = lttng_event_rule_kernel_function_get_event_name(rule, &name); |
| 788 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 789 | ret_code = LTTNG_OK; |
| 790 | break; |
| 791 | } |
| 792 | case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE: |
| 793 | { |
| 794 | const struct lttng_userspace_probe_location* location = NULL; |
| 795 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; |
| 796 | enum lttng_event_rule_status status; |
| 797 | |
| 798 | status = lttng_event_rule_userspace_probe_get_location(rule, &location); |
| 799 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
| 800 | ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 801 | goto error; |
| 802 | } |
| 803 | |
| 804 | kernel_event->instrumentation = LTTNG_KERNEL_UPROBE; |
| 805 | |
| 806 | lookup = lttng_userspace_probe_location_get_lookup_method( |
| 807 | location); |
| 808 | if (!lookup) { |
| 809 | ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 810 | goto error; |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * From the kernel tracer's perspective, all userspace probe |
| 815 | * event types are all the same: a file and an offset. |
| 816 | */ |
| 817 | switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) { |
| 818 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: |
| 819 | /* Get the file descriptor on the target binary. */ |
| 820 | kernel_event->u.uprobe.fd = |
| 821 | lttng_userspace_probe_location_function_get_binary_fd(location); |
| 822 | |
| 823 | break; |
| 824 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: |
| 825 | /* Get the file descriptor on the target binary. */ |
| 826 | kernel_event->u.uprobe.fd = |
| 827 | lttng_userspace_probe_location_tracepoint_get_binary_fd(location); |
| 828 | break; |
| 829 | default: |
| 830 | abort(); |
| 831 | } |
| 832 | |
| 833 | status = lttng_event_rule_userspace_probe_get_event_name(rule, &name); |
| 834 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 835 | ret_code = LTTNG_OK; |
| 836 | break; |
| 837 | } |
| 838 | case LTTNG_EVENT_RULE_TYPE_TRACEPOINT: |
| 839 | { |
| 840 | const enum lttng_domain_type domain = |
| 841 | lttng_event_rule_get_domain_type(rule); |
| 842 | const enum lttng_event_rule_status status = |
| 843 | lttng_event_rule_tracepoint_get_pattern( |
| 844 | rule, &name); |
| 845 | |
| 846 | assert(domain == LTTNG_DOMAIN_KERNEL); |
| 847 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 848 | kernel_event->instrumentation = |
| 849 | LTTNG_KERNEL_TRACEPOINT; |
| 850 | |
| 851 | ret_code = LTTNG_OK; |
| 852 | break; |
| 853 | } |
| 854 | case LTTNG_EVENT_RULE_TYPE_SYSCALL: |
| 855 | { |
| 856 | const enum lttng_event_rule_status status = |
| 857 | lttng_event_rule_syscall_get_pattern( |
| 858 | rule, &name); |
| 859 | |
| 860 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 861 | |
| 862 | kernel_event->instrumentation = |
| 863 | LTTNG_KERNEL_SYSCALL; |
| 864 | kernel_event->u.syscall.abi = |
| 865 | LTTNG_KERNEL_SYSCALL_ABI_ALL; |
| 866 | kernel_event->u.syscall.match = |
| 867 | LTTNG_KERNEL_SYSCALL_MATCH_NAME; |
| 868 | switch (event_type) { |
| 869 | case TRACE_KERNEL_EVENT_TYPE_COUNTER: |
| 870 | kernel_event->u.syscall.entryexit = |
| 871 | LTTNG_KERNEL_SYSCALL_ENTRYEXIT; |
| 872 | break; |
| 873 | case TRACE_KERNEL_EVENT_TYPE_NOTIFIER: |
| 874 | kernel_event->u.syscall.entryexit = |
| 875 | LTTNG_KERNEL_SYSCALL_ENTRY; |
| 876 | break; |
| 877 | } |
| 878 | ret_code = LTTNG_OK; |
| 879 | break; |
| 880 | } |
| 881 | default: |
| 882 | abort(); |
| 883 | break; |
| 884 | } |
| 885 | |
| 886 | strncpy_ret = lttng_strncpy(kernel_event->name, name, |
| 887 | LTTNG_KERNEL_SYM_NAME_LEN); |
| 888 | if (strncpy_ret) { |
| 889 | ret_code = LTTNG_ERR_INVALID; |
| 890 | goto error; |
| 891 | } |
| 892 | |
| 893 | error: |
| 894 | return ret_code; |
| 895 | } |
| 896 | |
| 897 | enum lttng_error_code trace_kernel_init_event_notifier_from_event_rule( |
| 898 | const struct lttng_event_rule *rule, |
| 899 | struct lttng_kernel_event_notifier *kernel_event_notifier) |
| 900 | { |
| 901 | return trace_kernel_init_event_from_event_rule(rule, |
| 902 | &kernel_event_notifier->event, |
| 903 | TRACE_KERNEL_EVENT_TYPE_NOTIFIER); |
| 904 | } |
| 905 | |
| 906 | enum lttng_error_code trace_kernel_init_event_counter_from_event_rule( |
| 907 | const struct lttng_event_rule *rule, |
| 908 | struct lttng_kernel_counter_event *kernel_counter_event) |
| 909 | { |
| 910 | return trace_kernel_init_event_from_event_rule(rule, |
| 911 | &kernel_counter_event->event, |
| 912 | TRACE_KERNEL_EVENT_TYPE_COUNTER); |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | * Allocate and initialize a kernel metadata. |
| 917 | * |
| 918 | * Return pointer to structure or NULL. |
| 919 | */ |
| 920 | struct ltt_kernel_metadata *trace_kernel_create_metadata(void) |
| 921 | { |
| 922 | int ret; |
| 923 | struct ltt_kernel_metadata *lkm; |
| 924 | struct lttng_channel *chan; |
| 925 | |
| 926 | lkm = zmalloc(sizeof(struct ltt_kernel_metadata)); |
| 927 | chan = zmalloc(sizeof(struct lttng_channel)); |
| 928 | if (lkm == NULL || chan == NULL) { |
| 929 | PERROR("kernel metadata zmalloc"); |
| 930 | goto error; |
| 931 | } |
| 932 | |
| 933 | ret = lttng_strncpy( |
| 934 | chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name)); |
| 935 | if (ret) { |
| 936 | ERR("Failed to initialize metadata channel name to `%s`", |
| 937 | DEFAULT_METADATA_NAME); |
| 938 | goto error; |
| 939 | } |
| 940 | |
| 941 | /* Set default attributes */ |
| 942 | chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE; |
| 943 | chan->attr.subbuf_size = default_get_metadata_subbuf_size(); |
| 944 | chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; |
| 945 | chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER; |
| 946 | chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;; |
| 947 | |
| 948 | |
| 949 | /* |
| 950 | * The metadata channel of kernel sessions must use the "mmap" |
| 951 | * back-end since the consumer daemon accumulates complete |
| 952 | * metadata units before sending them to the relay daemon in |
| 953 | * live mode. The consumer daemon also needs to extract the contents |
| 954 | * of the metadata cache when computing a rotation position. |
| 955 | * |
| 956 | * In both cases, it is not possible to rely on the splice |
| 957 | * back-end as the consumer daemon may need to accumulate more |
| 958 | * content than can be backed by the ring buffer's underlying |
| 959 | * pages. |
| 960 | */ |
| 961 | chan->attr.output = LTTNG_EVENT_MMAP; |
| 962 | chan->attr.tracefile_size = 0; |
| 963 | chan->attr.tracefile_count = 0; |
| 964 | chan->attr.live_timer_interval = 0; |
| 965 | |
| 966 | /* Init metadata */ |
| 967 | lkm->fd = -1; |
| 968 | lkm->conf = chan; |
| 969 | |
| 970 | return lkm; |
| 971 | |
| 972 | error: |
| 973 | free(lkm); |
| 974 | free(chan); |
| 975 | return NULL; |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by |
| 980 | * default. |
| 981 | * |
| 982 | * Return pointer to structure or NULL. |
| 983 | */ |
| 984 | struct ltt_kernel_stream *trace_kernel_create_stream(const char *name, |
| 985 | unsigned int count) |
| 986 | { |
| 987 | int ret; |
| 988 | struct ltt_kernel_stream *lks; |
| 989 | |
| 990 | assert(name); |
| 991 | |
| 992 | lks = zmalloc(sizeof(struct ltt_kernel_stream)); |
| 993 | if (lks == NULL) { |
| 994 | PERROR("kernel stream zmalloc"); |
| 995 | goto error; |
| 996 | } |
| 997 | |
| 998 | /* Set name */ |
| 999 | ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count); |
| 1000 | if (ret < 0) { |
| 1001 | PERROR("snprintf stream name"); |
| 1002 | goto error; |
| 1003 | } |
| 1004 | lks->name[sizeof(lks->name) - 1] = '\0'; |
| 1005 | |
| 1006 | /* Init stream */ |
| 1007 | lks->fd = -1; |
| 1008 | lks->state = 0; |
| 1009 | lks->cpu = count; |
| 1010 | |
| 1011 | return lks; |
| 1012 | |
| 1013 | error: |
| 1014 | return NULL; |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * Cleanup kernel stream structure. |
| 1019 | */ |
| 1020 | void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream) |
| 1021 | { |
| 1022 | assert(stream); |
| 1023 | |
| 1024 | DBG("[trace] Closing stream fd %d", stream->fd); |
| 1025 | /* Close kernel fd */ |
| 1026 | if (stream->fd >= 0) { |
| 1027 | int ret; |
| 1028 | |
| 1029 | ret = close(stream->fd); |
| 1030 | if (ret) { |
| 1031 | PERROR("close"); |
| 1032 | } |
| 1033 | } |
| 1034 | /* Remove from stream list */ |
| 1035 | cds_list_del(&stream->list); |
| 1036 | |
| 1037 | free(stream); |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * Cleanup kernel event structure. |
| 1042 | */ |
| 1043 | void trace_kernel_destroy_event(struct ltt_kernel_event *event) |
| 1044 | { |
| 1045 | assert(event); |
| 1046 | |
| 1047 | if (event->fd >= 0) { |
| 1048 | int ret; |
| 1049 | |
| 1050 | DBG("[trace] Closing event fd %d", event->fd); |
| 1051 | /* Close kernel fd */ |
| 1052 | ret = close(event->fd); |
| 1053 | if (ret) { |
| 1054 | PERROR("close"); |
| 1055 | } |
| 1056 | } else { |
| 1057 | DBG("[trace] Tearing down event (no associated file descriptor)"); |
| 1058 | } |
| 1059 | |
| 1060 | /* Remove from event list */ |
| 1061 | cds_list_del(&event->list); |
| 1062 | |
| 1063 | free(event->filter_expression); |
| 1064 | free(event->filter); |
| 1065 | |
| 1066 | free(event->event); |
| 1067 | free(event); |
| 1068 | } |
| 1069 | |
| 1070 | /* |
| 1071 | * Free kernel event counter structure from RCU context |
| 1072 | */ |
| 1073 | static void free_event_counter_rcu(struct rcu_head *rcu_node) |
| 1074 | { |
| 1075 | struct ltt_kernel_event_counter *event_counter = caa_container_of(rcu_node, |
| 1076 | struct ltt_kernel_event_counter, rcu_node); |
| 1077 | |
| 1078 | free(event_counter); |
| 1079 | } |
| 1080 | /* |
| 1081 | * Cleanup kernel event counter structure. |
| 1082 | */ |
| 1083 | void trace_kernel_destroy_event_counter(struct ltt_kernel_event_counter *event_counter) |
| 1084 | { |
| 1085 | assert(event_counter); |
| 1086 | |
| 1087 | if (event_counter->fd >= 0) { |
| 1088 | int ret; |
| 1089 | |
| 1090 | DBG("[trace] Closing event counter fd %d", event_counter->fd); |
| 1091 | /* Close kernel fd */ |
| 1092 | ret = close(event_counter->fd); |
| 1093 | if (ret) { |
| 1094 | PERROR("close"); |
| 1095 | } |
| 1096 | } else { |
| 1097 | DBG("[trace] Tearing down event counter (no associated file descriptor)"); |
| 1098 | } |
| 1099 | |
| 1100 | lttng_map_key_put(event_counter->key); |
| 1101 | call_rcu(&event_counter->rcu_node, free_event_counter_rcu); |
| 1102 | } |
| 1103 | |
| 1104 | |
| 1105 | /* |
| 1106 | * Cleanup kernel event structure. |
| 1107 | */ |
| 1108 | static void free_token_event_rule_rcu(struct rcu_head *rcu_node) |
| 1109 | { |
| 1110 | struct ltt_kernel_event_notifier_rule *rule = caa_container_of(rcu_node, |
| 1111 | struct ltt_kernel_event_notifier_rule, rcu_node); |
| 1112 | |
| 1113 | free(rule); |
| 1114 | } |
| 1115 | |
| 1116 | void trace_kernel_destroy_event_notifier_rule( |
| 1117 | struct ltt_kernel_event_notifier_rule *event) |
| 1118 | { |
| 1119 | assert(event); |
| 1120 | |
| 1121 | if (event->fd >= 0) { |
| 1122 | const int ret = close(event->fd); |
| 1123 | |
| 1124 | DBG("Closing kernel event notifier rule file descriptor: fd = %d", |
| 1125 | event->fd); |
| 1126 | if (ret) { |
| 1127 | PERROR("Failed to close kernel event notifier file descriptor: fd = %d", |
| 1128 | event->fd); |
| 1129 | } |
| 1130 | } else { |
| 1131 | DBG("Destroying kernel event notifier rule (no associated file descriptor)"); |
| 1132 | } |
| 1133 | |
| 1134 | lttng_trigger_put(event->trigger); |
| 1135 | call_rcu(&event->rcu_node, free_token_event_rule_rcu); |
| 1136 | } |
| 1137 | /* |
| 1138 | * Cleanup kernel context structure. |
| 1139 | */ |
| 1140 | void trace_kernel_destroy_context(struct ltt_kernel_context *ctx) |
| 1141 | { |
| 1142 | assert(ctx); |
| 1143 | |
| 1144 | if (ctx->in_list) { |
| 1145 | cds_list_del(&ctx->list); |
| 1146 | } |
| 1147 | free(ctx); |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * Cleanup kernel channel structure. |
| 1152 | */ |
| 1153 | void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel) |
| 1154 | { |
| 1155 | struct ltt_kernel_stream *stream, *stmp; |
| 1156 | struct ltt_kernel_event *event, *etmp; |
| 1157 | struct ltt_kernel_context *ctx, *ctmp; |
| 1158 | int ret; |
| 1159 | enum lttng_error_code status; |
| 1160 | |
| 1161 | assert(channel); |
| 1162 | |
| 1163 | DBG("[trace] Closing channel fd %d", channel->fd); |
| 1164 | /* Close kernel fd */ |
| 1165 | if (channel->fd >= 0) { |
| 1166 | ret = close(channel->fd); |
| 1167 | if (ret) { |
| 1168 | PERROR("close"); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | /* For each stream in the channel list */ |
| 1173 | cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) { |
| 1174 | trace_kernel_destroy_stream(stream); |
| 1175 | } |
| 1176 | |
| 1177 | /* For each event in the channel list */ |
| 1178 | cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) { |
| 1179 | trace_kernel_destroy_event(event); |
| 1180 | } |
| 1181 | |
| 1182 | /* For each context in the channel list */ |
| 1183 | cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) { |
| 1184 | trace_kernel_destroy_context(ctx); |
| 1185 | } |
| 1186 | |
| 1187 | /* Remove from channel list */ |
| 1188 | cds_list_del(&channel->list); |
| 1189 | |
| 1190 | if (notification_thread_handle |
| 1191 | && channel->published_to_notification_thread) { |
| 1192 | status = notification_thread_command_remove_channel( |
| 1193 | notification_thread_handle, |
| 1194 | channel->key, LTTNG_DOMAIN_KERNEL); |
| 1195 | assert(status == LTTNG_OK); |
| 1196 | } |
| 1197 | free(channel->channel->attr.extended.ptr); |
| 1198 | free(channel->channel); |
| 1199 | free(channel); |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * Cleanup kernel map structure. |
| 1204 | */ |
| 1205 | void trace_kernel_destroy_map(struct ltt_kernel_map *map) |
| 1206 | { |
| 1207 | int ret; |
| 1208 | struct ltt_kernel_event_counter *event_counter; |
| 1209 | struct lttng_ht_iter iter; |
| 1210 | |
| 1211 | assert(map); |
| 1212 | |
| 1213 | DBG("[trace] Closing map fd %d", map->fd); |
| 1214 | /* Close kernel fd */ |
| 1215 | if (map->fd >= 0) { |
| 1216 | ret = close(map->fd); |
| 1217 | if (ret) { |
| 1218 | PERROR("close"); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /* For each event counter in the map hashtable */ |
| 1223 | cds_lfht_for_each_entry(map->event_counters_ht->ht, &iter.iter, |
| 1224 | event_counter, ht_node.node) { |
| 1225 | trace_kernel_destroy_event_counter(event_counter); |
| 1226 | } |
| 1227 | |
| 1228 | /* Remove from map list */ |
| 1229 | cds_list_del(&map->list); |
| 1230 | |
| 1231 | free(map); |
| 1232 | } |
| 1233 | |
| 1234 | /* |
| 1235 | * Cleanup kernel metadata structure. |
| 1236 | */ |
| 1237 | void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata) |
| 1238 | { |
| 1239 | assert(metadata); |
| 1240 | |
| 1241 | DBG("[trace] Closing metadata fd %d", metadata->fd); |
| 1242 | /* Close kernel fd */ |
| 1243 | if (metadata->fd >= 0) { |
| 1244 | int ret; |
| 1245 | |
| 1246 | ret = close(metadata->fd); |
| 1247 | if (ret) { |
| 1248 | PERROR("close"); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | free(metadata->conf); |
| 1253 | free(metadata); |
| 1254 | } |
| 1255 | |
| 1256 | /* |
| 1257 | * Cleanup kernel session structure |
| 1258 | * |
| 1259 | * Should *NOT* be called with RCU read-side lock held. |
| 1260 | */ |
| 1261 | void trace_kernel_destroy_session(struct ltt_kernel_session *session) |
| 1262 | { |
| 1263 | struct ltt_kernel_channel *channel, *ctmp; |
| 1264 | struct ltt_kernel_map *map, *map_tmp; |
| 1265 | int ret; |
| 1266 | |
| 1267 | assert(session); |
| 1268 | |
| 1269 | DBG("[trace] Closing session fd %d", session->fd); |
| 1270 | /* Close kernel fds */ |
| 1271 | if (session->fd >= 0) { |
| 1272 | ret = close(session->fd); |
| 1273 | if (ret) { |
| 1274 | PERROR("close"); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | if (session->metadata_stream_fd >= 0) { |
| 1279 | DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd); |
| 1280 | ret = close(session->metadata_stream_fd); |
| 1281 | if (ret) { |
| 1282 | PERROR("close"); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | if (session->metadata != NULL) { |
| 1287 | trace_kernel_destroy_metadata(session->metadata); |
| 1288 | } |
| 1289 | |
| 1290 | cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) { |
| 1291 | trace_kernel_destroy_channel(channel); |
| 1292 | } |
| 1293 | |
| 1294 | cds_list_for_each_entry_safe(map, map_tmp, &session->map_list.head, list) { |
| 1295 | trace_kernel_destroy_map(map); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | /* Free elements needed by destroy notifiers. */ |
| 1300 | void trace_kernel_free_session(struct ltt_kernel_session *session) |
| 1301 | { |
| 1302 | /* Wipe consumer output object */ |
| 1303 | consumer_output_put(session->consumer); |
| 1304 | |
| 1305 | process_attr_tracker_destroy(session->tracker_pid); |
| 1306 | process_attr_tracker_destroy(session->tracker_vpid); |
| 1307 | process_attr_tracker_destroy(session->tracker_uid); |
| 1308 | process_attr_tracker_destroy(session->tracker_vuid); |
| 1309 | process_attr_tracker_destroy(session->tracker_gid); |
| 1310 | process_attr_tracker_destroy(session->tracker_vgid); |
| 1311 | |
| 1312 | free(session); |
| 1313 | } |