| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | #include <inttypes.h> |
| 15 | |
| 16 | #include <common/common.h> |
| 17 | #include <common/defaults.h> |
| 18 | #include <common/trace-chunk.h> |
| 19 | |
| 20 | #include "buffer-registry.h" |
| 21 | #include "trace-ust.h" |
| 22 | #include "utils.h" |
| 23 | #include "ust-app.h" |
| 24 | #include "agent.h" |
| 25 | |
| 26 | /* |
| 27 | * Match function for the events hash table lookup. |
| 28 | * |
| 29 | * Matches by name only. Used by the disable command. |
| 30 | */ |
| 31 | int trace_ust_ht_match_event_by_name(struct cds_lfht_node *node, |
| 32 | const void *_key) |
| 33 | { |
| 34 | struct ltt_ust_event *event; |
| 35 | const char *name; |
| 36 | |
| 37 | assert(node); |
| 38 | assert(_key); |
| 39 | |
| 40 | event = caa_container_of(node, struct ltt_ust_event, node.node); |
| 41 | name = _key; |
| 42 | |
| 43 | /* Event name */ |
| 44 | if (strncmp(event->attr.name, name, sizeof(event->attr.name)) != 0) { |
| 45 | goto no_match; |
| 46 | } |
| 47 | |
| 48 | /* Match */ |
| 49 | return 1; |
| 50 | |
| 51 | no_match: |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Match function for the hash table lookup. |
| 57 | * |
| 58 | * It matches an ust event based on three attributes which are the event name, |
| 59 | * the filter bytecode and the loglevel. |
| 60 | */ |
| 61 | int trace_ust_ht_match_event(struct cds_lfht_node *node, const void *_key) |
| 62 | { |
| 63 | struct ltt_ust_event *event; |
| 64 | const struct ltt_ust_ht_key *key; |
| 65 | int ev_loglevel_value; |
| 66 | int ll_match; |
| 67 | |
| 68 | assert(node); |
| 69 | assert(_key); |
| 70 | |
| 71 | event = caa_container_of(node, struct ltt_ust_event, node.node); |
| 72 | key = _key; |
| 73 | ev_loglevel_value = event->attr.loglevel; |
| 74 | |
| 75 | /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */ |
| 76 | |
| 77 | /* Event name */ |
| 78 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { |
| 79 | goto no_match; |
| 80 | } |
| 81 | |
| 82 | /* Event loglevel value and type. */ |
| 83 | ll_match = loglevels_match(event->attr.loglevel_type, |
| 84 | ev_loglevel_value, key->loglevel_type, |
| 85 | key->loglevel_value, LTTNG_UST_LOGLEVEL_ALL); |
| 86 | |
| 87 | if (!ll_match) { |
| 88 | goto no_match; |
| 89 | } |
| 90 | |
| 91 | /* Only one of the filters is NULL, fail. */ |
| 92 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { |
| 93 | goto no_match; |
| 94 | } |
| 95 | |
| 96 | if (key->filter && event->filter) { |
| 97 | /* Both filters exists, check length followed by the bytecode. */ |
| 98 | if (event->filter->len != key->filter->len || |
| 99 | memcmp(event->filter->data, key->filter->data, |
| 100 | event->filter->len) != 0) { |
| 101 | goto no_match; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* If only one of the exclusions is NULL, fail. */ |
| 106 | if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) { |
| 107 | goto no_match; |
| 108 | } |
| 109 | |
| 110 | if (key->exclusion && event->exclusion) { |
| 111 | size_t i; |
| 112 | |
| 113 | /* Check exclusion counts first. */ |
| 114 | if (event->exclusion->count != key->exclusion->count) { |
| 115 | goto no_match; |
| 116 | } |
| 117 | |
| 118 | /* Compare names individually. */ |
| 119 | for (i = 0; i < event->exclusion->count; ++i) { |
| 120 | size_t j; |
| 121 | bool found = false; |
| 122 | const char *name_ev = |
| 123 | LTTNG_EVENT_EXCLUSION_NAME_AT( |
| 124 | event->exclusion, i); |
| 125 | |
| 126 | /* |
| 127 | * Compare this exclusion name to all the key's |
| 128 | * exclusion names. |
| 129 | */ |
| 130 | for (j = 0; j < key->exclusion->count; ++j) { |
| 131 | const char *name_key = |
| 132 | LTTNG_EVENT_EXCLUSION_NAME_AT( |
| 133 | key->exclusion, j); |
| 134 | |
| 135 | if (!strncmp(name_ev, name_key, |
| 136 | LTTNG_SYMBOL_NAME_LEN)) { |
| 137 | /* Names match! */ |
| 138 | found = true; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * If the current exclusion name was not found amongst |
| 145 | * the key's exclusion names, then there's no match. |
| 146 | */ |
| 147 | if (!found) { |
| 148 | goto no_match; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | /* Match. */ |
| 153 | return 1; |
| 154 | |
| 155 | no_match: |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Find the channel in the hashtable and return channel pointer. RCU read side |
| 161 | * lock MUST be acquired before calling this. |
| 162 | */ |
| 163 | struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht, |
| 164 | const char *name) |
| 165 | { |
| 166 | struct lttng_ht_node_str *node; |
| 167 | struct lttng_ht_iter iter; |
| 168 | |
| 169 | /* |
| 170 | * If we receive an empty string for channel name, it means the |
| 171 | * default channel name is requested. |
| 172 | */ |
| 173 | if (name[0] == '\0') |
| 174 | name = DEFAULT_CHANNEL_NAME; |
| 175 | |
| 176 | lttng_ht_lookup(ht, (void *)name, &iter); |
| 177 | node = lttng_ht_iter_get_node_str(&iter); |
| 178 | if (node == NULL) { |
| 179 | goto error; |
| 180 | } |
| 181 | |
| 182 | DBG2("Trace UST channel %s found by name", name); |
| 183 | |
| 184 | return caa_container_of(node, struct ltt_ust_channel, node); |
| 185 | |
| 186 | error: |
| 187 | DBG2("Trace UST channel %s not found by name", name); |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Find the event in the hashtable and return event pointer. RCU read side lock |
| 193 | * MUST be acquired before calling this. |
| 194 | */ |
| 195 | struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht, |
| 196 | char *name, struct lttng_filter_bytecode *filter, |
| 197 | enum lttng_ust_loglevel_type loglevel_type, int loglevel_value, |
| 198 | struct lttng_event_exclusion *exclusion) |
| 199 | { |
| 200 | struct lttng_ht_node_str *node; |
| 201 | struct lttng_ht_iter iter; |
| 202 | struct ltt_ust_ht_key key; |
| 203 | |
| 204 | assert(name); |
| 205 | assert(ht); |
| 206 | |
| 207 | key.name = name; |
| 208 | key.filter = filter; |
| 209 | key.loglevel_type = loglevel_type; |
| 210 | key.loglevel_value = loglevel_value; |
| 211 | key.exclusion = exclusion; |
| 212 | |
| 213 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), |
| 214 | trace_ust_ht_match_event, &key, &iter.iter); |
| 215 | node = lttng_ht_iter_get_node_str(&iter); |
| 216 | if (node == NULL) { |
| 217 | goto error; |
| 218 | } |
| 219 | |
| 220 | DBG2("Trace UST event %s found", key.name); |
| 221 | |
| 222 | return caa_container_of(node, struct ltt_ust_event, node); |
| 223 | |
| 224 | error: |
| 225 | DBG2("Trace UST event %s NOT found", key.name); |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Lookup an agent in the session agents hash table by domain type and return |
| 231 | * the object if found else NULL. |
| 232 | * |
| 233 | * RCU read side lock must be acquired before calling and only released |
| 234 | * once the agent is no longer in scope or being used. |
| 235 | */ |
| 236 | struct agent *trace_ust_find_agent(struct ltt_ust_session *session, |
| 237 | enum lttng_domain_type domain_type) |
| 238 | { |
| 239 | struct agent *agt = NULL; |
| 240 | struct lttng_ht_node_u64 *node; |
| 241 | struct lttng_ht_iter iter; |
| 242 | uint64_t key; |
| 243 | |
| 244 | assert(session); |
| 245 | |
| 246 | DBG3("Trace ust agent lookup for domain %d", domain_type); |
| 247 | |
| 248 | key = domain_type; |
| 249 | |
| 250 | lttng_ht_lookup(session->agents, &key, &iter); |
| 251 | node = lttng_ht_iter_get_node_u64(&iter); |
| 252 | if (!node) { |
| 253 | goto end; |
| 254 | } |
| 255 | agt = caa_container_of(node, struct agent, node); |
| 256 | |
| 257 | end: |
| 258 | return agt; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Allocate and initialize a ust session data structure. |
| 263 | * |
| 264 | * Return pointer to structure or NULL. |
| 265 | */ |
| 266 | struct ltt_ust_session *trace_ust_create_session(uint64_t session_id) |
| 267 | { |
| 268 | struct ltt_ust_session *lus; |
| 269 | |
| 270 | /* Allocate a new ltt ust session */ |
| 271 | lus = zmalloc(sizeof(struct ltt_ust_session)); |
| 272 | if (lus == NULL) { |
| 273 | PERROR("create ust session zmalloc"); |
| 274 | goto error_alloc; |
| 275 | } |
| 276 | |
| 277 | /* Init data structure */ |
| 278 | lus->id = session_id; |
| 279 | lus->active = 0; |
| 280 | |
| 281 | /* Set default metadata channel attribute. */ |
| 282 | lus->metadata_attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 283 | lus->metadata_attr.subbuf_size = default_get_metadata_subbuf_size(); |
| 284 | lus->metadata_attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; |
| 285 | lus->metadata_attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER; |
| 286 | lus->metadata_attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER; |
| 287 | lus->metadata_attr.output = LTTNG_UST_MMAP; |
| 288 | |
| 289 | /* |
| 290 | * Default buffer type. This can be changed through an enable channel |
| 291 | * requesting a different type. Note that this can only be changed once |
| 292 | * during the session lifetime which is at the first enable channel and |
| 293 | * only before start. The flag buffer_type_changed indicates the status. |
| 294 | */ |
| 295 | lus->buffer_type = LTTNG_BUFFER_PER_UID; |
| 296 | /* Once set to 1, the buffer_type is immutable for the session. */ |
| 297 | lus->buffer_type_changed = 0; |
| 298 | /* Init it in case it get used after allocation. */ |
| 299 | CDS_INIT_LIST_HEAD(&lus->buffer_reg_uid_list); |
| 300 | |
| 301 | /* Alloc UST global domain channels' HT */ |
| 302 | lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 303 | /* Alloc agent hash table. */ |
| 304 | lus->agents = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 305 | |
| 306 | lus->tracker_list_vpid = lttng_tracker_list_create(); |
| 307 | if (!lus->tracker_list_vpid) { |
| 308 | goto error; |
| 309 | } |
| 310 | lus->tracker_list_vuid = lttng_tracker_list_create(); |
| 311 | if (!lus->tracker_list_vuid) { |
| 312 | goto error; |
| 313 | } |
| 314 | lus->tracker_list_vgid = lttng_tracker_list_create(); |
| 315 | if (!lus->tracker_list_vgid) { |
| 316 | goto error; |
| 317 | } |
| 318 | lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
| 319 | if (lus->consumer == NULL) { |
| 320 | goto error; |
| 321 | } |
| 322 | |
| 323 | DBG2("UST trace session create successful"); |
| 324 | |
| 325 | return lus; |
| 326 | |
| 327 | error: |
| 328 | lttng_tracker_list_destroy(lus->tracker_list_vpid); |
| 329 | lttng_tracker_list_destroy(lus->tracker_list_vuid); |
| 330 | lttng_tracker_list_destroy(lus->tracker_list_vgid); |
| 331 | ht_cleanup_push(lus->domain_global.channels); |
| 332 | ht_cleanup_push(lus->agents); |
| 333 | free(lus); |
| 334 | error_alloc: |
| 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Allocate and initialize a ust channel data structure. |
| 340 | * |
| 341 | * Return pointer to structure or NULL. |
| 342 | */ |
| 343 | struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan, |
| 344 | enum lttng_domain_type domain) |
| 345 | { |
| 346 | struct ltt_ust_channel *luc; |
| 347 | |
| 348 | assert(chan); |
| 349 | |
| 350 | luc = zmalloc(sizeof(struct ltt_ust_channel)); |
| 351 | if (luc == NULL) { |
| 352 | PERROR("ltt_ust_channel zmalloc"); |
| 353 | goto error; |
| 354 | } |
| 355 | |
| 356 | luc->domain = domain; |
| 357 | |
| 358 | /* Copy UST channel attributes */ |
| 359 | luc->attr.overwrite = chan->attr.overwrite; |
| 360 | luc->attr.subbuf_size = chan->attr.subbuf_size; |
| 361 | luc->attr.num_subbuf = chan->attr.num_subbuf; |
| 362 | luc->attr.switch_timer_interval = chan->attr.switch_timer_interval; |
| 363 | luc->attr.read_timer_interval = chan->attr.read_timer_interval; |
| 364 | luc->attr.output = (enum lttng_ust_output) chan->attr.output; |
| 365 | luc->monitor_timer_interval = ((struct lttng_channel_extended *) |
| 366 | chan->attr.extended.ptr)->monitor_timer_interval; |
| 367 | luc->attr.u.s.blocking_timeout = ((struct lttng_channel_extended *) |
| 368 | chan->attr.extended.ptr)->blocking_timeout; |
| 369 | |
| 370 | /* Translate to UST output enum */ |
| 371 | switch (luc->attr.output) { |
| 372 | default: |
| 373 | luc->attr.output = LTTNG_UST_MMAP; |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * If we receive an empty string for channel name, it means the |
| 379 | * default channel name is requested. |
| 380 | */ |
| 381 | if (chan->name[0] == '\0') { |
| 382 | strncpy(luc->name, DEFAULT_CHANNEL_NAME, sizeof(luc->name)); |
| 383 | } else { |
| 384 | /* Copy channel name */ |
| 385 | strncpy(luc->name, chan->name, sizeof(luc->name)); |
| 386 | } |
| 387 | luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 388 | |
| 389 | /* Init node */ |
| 390 | lttng_ht_node_init_str(&luc->node, luc->name); |
| 391 | CDS_INIT_LIST_HEAD(&luc->ctx_list); |
| 392 | |
| 393 | /* Alloc hash tables */ |
| 394 | luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 395 | luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 396 | |
| 397 | /* On-disk circular buffer parameters */ |
| 398 | luc->tracefile_size = chan->attr.tracefile_size; |
| 399 | luc->tracefile_count = chan->attr.tracefile_count; |
| 400 | |
| 401 | DBG2("Trace UST channel %s created", luc->name); |
| 402 | |
| 403 | error: |
| 404 | return luc; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Validates an exclusion list. |
| 409 | * |
| 410 | * Returns 0 if valid, negative value if invalid. |
| 411 | */ |
| 412 | static int validate_exclusion(struct lttng_event_exclusion *exclusion) |
| 413 | { |
| 414 | size_t i; |
| 415 | int ret = 0; |
| 416 | |
| 417 | assert(exclusion); |
| 418 | |
| 419 | for (i = 0; i < exclusion->count; ++i) { |
| 420 | size_t j; |
| 421 | const char *name_a = |
| 422 | LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i); |
| 423 | |
| 424 | for (j = 0; j < i; ++j) { |
| 425 | const char *name_b = |
| 426 | LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, j); |
| 427 | |
| 428 | if (!strncmp(name_a, name_b, LTTNG_SYMBOL_NAME_LEN)) { |
| 429 | /* Match! */ |
| 430 | ret = -1; |
| 431 | goto end; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | end: |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * Allocate and initialize a ust event. Set name and event type. |
| 442 | * We own filter_expression, filter, and exclusion. |
| 443 | * |
| 444 | * Return an lttng_error_code |
| 445 | */ |
| 446 | enum lttng_error_code trace_ust_create_event(struct lttng_event *ev, |
| 447 | char *filter_expression, |
| 448 | struct lttng_filter_bytecode *filter, |
| 449 | struct lttng_event_exclusion *exclusion, |
| 450 | bool internal_event, |
| 451 | struct ltt_ust_event **ust_event) |
| 452 | { |
| 453 | struct ltt_ust_event *local_ust_event; |
| 454 | enum lttng_error_code ret = LTTNG_OK; |
| 455 | |
| 456 | assert(ev); |
| 457 | |
| 458 | if (exclusion && validate_exclusion(exclusion)) { |
| 459 | ret = LTTNG_ERR_INVALID; |
| 460 | goto error; |
| 461 | } |
| 462 | |
| 463 | local_ust_event = zmalloc(sizeof(struct ltt_ust_event)); |
| 464 | if (local_ust_event == NULL) { |
| 465 | PERROR("ust event zmalloc"); |
| 466 | ret = LTTNG_ERR_NOMEM; |
| 467 | goto error; |
| 468 | } |
| 469 | |
| 470 | local_ust_event->internal = internal_event; |
| 471 | |
| 472 | switch (ev->type) { |
| 473 | case LTTNG_EVENT_PROBE: |
| 474 | local_ust_event->attr.instrumentation = LTTNG_UST_PROBE; |
| 475 | break; |
| 476 | case LTTNG_EVENT_FUNCTION: |
| 477 | local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION; |
| 478 | break; |
| 479 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 480 | local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION; |
| 481 | break; |
| 482 | case LTTNG_EVENT_TRACEPOINT: |
| 483 | local_ust_event->attr.instrumentation = LTTNG_UST_TRACEPOINT; |
| 484 | break; |
| 485 | default: |
| 486 | ERR("Unknown ust instrumentation type (%d)", ev->type); |
| 487 | ret = LTTNG_ERR_INVALID; |
| 488 | goto error_free_event; |
| 489 | } |
| 490 | |
| 491 | /* Copy event name */ |
| 492 | strncpy(local_ust_event->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN); |
| 493 | local_ust_event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 494 | |
| 495 | switch (ev->loglevel_type) { |
| 496 | case LTTNG_EVENT_LOGLEVEL_ALL: |
| 497 | local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL; |
| 498 | local_ust_event->attr.loglevel = -1; /* Force to -1 */ |
| 499 | break; |
| 500 | case LTTNG_EVENT_LOGLEVEL_RANGE: |
| 501 | local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE; |
| 502 | local_ust_event->attr.loglevel = ev->loglevel; |
| 503 | break; |
| 504 | case LTTNG_EVENT_LOGLEVEL_SINGLE: |
| 505 | local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE; |
| 506 | local_ust_event->attr.loglevel = ev->loglevel; |
| 507 | break; |
| 508 | default: |
| 509 | ERR("Unknown ust loglevel type (%d)", ev->loglevel_type); |
| 510 | ret = LTTNG_ERR_INVALID; |
| 511 | goto error_free_event; |
| 512 | } |
| 513 | |
| 514 | /* Same layout. */ |
| 515 | local_ust_event->filter_expression = filter_expression; |
| 516 | local_ust_event->filter = filter; |
| 517 | local_ust_event->exclusion = exclusion; |
| 518 | |
| 519 | /* Init node */ |
| 520 | lttng_ht_node_init_str(&local_ust_event->node, local_ust_event->attr.name); |
| 521 | |
| 522 | DBG2("Trace UST event %s, loglevel (%d,%d) created", |
| 523 | local_ust_event->attr.name, local_ust_event->attr.loglevel_type, |
| 524 | local_ust_event->attr.loglevel); |
| 525 | |
| 526 | *ust_event = local_ust_event; |
| 527 | |
| 528 | return ret; |
| 529 | |
| 530 | error_free_event: |
| 531 | free(local_ust_event); |
| 532 | error: |
| 533 | free(filter_expression); |
| 534 | free(filter); |
| 535 | free(exclusion); |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | static |
| 540 | int trace_ust_context_type_event_to_ust( |
| 541 | enum lttng_event_context_type type) |
| 542 | { |
| 543 | int utype; |
| 544 | |
| 545 | switch (type) { |
| 546 | case LTTNG_EVENT_CONTEXT_VTID: |
| 547 | utype = LTTNG_UST_CONTEXT_VTID; |
| 548 | break; |
| 549 | case LTTNG_EVENT_CONTEXT_VPID: |
| 550 | utype = LTTNG_UST_CONTEXT_VPID; |
| 551 | break; |
| 552 | case LTTNG_EVENT_CONTEXT_PTHREAD_ID: |
| 553 | utype = LTTNG_UST_CONTEXT_PTHREAD_ID; |
| 554 | break; |
| 555 | case LTTNG_EVENT_CONTEXT_PROCNAME: |
| 556 | utype = LTTNG_UST_CONTEXT_PROCNAME; |
| 557 | break; |
| 558 | case LTTNG_EVENT_CONTEXT_IP: |
| 559 | utype = LTTNG_UST_CONTEXT_IP; |
| 560 | break; |
| 561 | case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER: |
| 562 | if (!ustctl_has_perf_counters()) { |
| 563 | utype = -1; |
| 564 | WARN("Perf counters not implemented in UST"); |
| 565 | } else { |
| 566 | utype = LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER; |
| 567 | } |
| 568 | break; |
| 569 | case LTTNG_EVENT_CONTEXT_APP_CONTEXT: |
| 570 | utype = LTTNG_UST_CONTEXT_APP_CONTEXT; |
| 571 | break; |
| 572 | case LTTNG_EVENT_CONTEXT_CGROUP_NS: |
| 573 | utype = LTTNG_UST_CONTEXT_CGROUP_NS; |
| 574 | break; |
| 575 | case LTTNG_EVENT_CONTEXT_IPC_NS: |
| 576 | utype = LTTNG_UST_CONTEXT_IPC_NS; |
| 577 | break; |
| 578 | case LTTNG_EVENT_CONTEXT_MNT_NS: |
| 579 | utype = LTTNG_UST_CONTEXT_MNT_NS; |
| 580 | break; |
| 581 | case LTTNG_EVENT_CONTEXT_NET_NS: |
| 582 | utype = LTTNG_UST_CONTEXT_NET_NS; |
| 583 | break; |
| 584 | case LTTNG_EVENT_CONTEXT_PID_NS: |
| 585 | utype = LTTNG_UST_CONTEXT_PID_NS; |
| 586 | break; |
| 587 | case LTTNG_EVENT_CONTEXT_USER_NS: |
| 588 | utype = LTTNG_UST_CONTEXT_USER_NS; |
| 589 | break; |
| 590 | case LTTNG_EVENT_CONTEXT_UTS_NS: |
| 591 | utype = LTTNG_UST_CONTEXT_UTS_NS; |
| 592 | break; |
| 593 | case LTTNG_EVENT_CONTEXT_VUID: |
| 594 | utype = LTTNG_UST_CONTEXT_VUID; |
| 595 | break; |
| 596 | case LTTNG_EVENT_CONTEXT_VEUID: |
| 597 | utype = LTTNG_UST_CONTEXT_VEUID; |
| 598 | break; |
| 599 | case LTTNG_EVENT_CONTEXT_VSUID: |
| 600 | utype = LTTNG_UST_CONTEXT_VSUID; |
| 601 | break; |
| 602 | case LTTNG_EVENT_CONTEXT_VGID: |
| 603 | utype = LTTNG_UST_CONTEXT_VGID; |
| 604 | break; |
| 605 | case LTTNG_EVENT_CONTEXT_VEGID: |
| 606 | utype = LTTNG_UST_CONTEXT_VEGID; |
| 607 | break; |
| 608 | case LTTNG_EVENT_CONTEXT_VSGID: |
| 609 | utype = LTTNG_UST_CONTEXT_VSGID; |
| 610 | break; |
| 611 | default: |
| 612 | utype = -1; |
| 613 | break; |
| 614 | } |
| 615 | return utype; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Return 1 if contexts match, 0 otherwise. |
| 620 | */ |
| 621 | int trace_ust_match_context(const struct ltt_ust_context *uctx, |
| 622 | const struct lttng_event_context *ctx) |
| 623 | { |
| 624 | int utype; |
| 625 | |
| 626 | utype = trace_ust_context_type_event_to_ust(ctx->ctx); |
| 627 | if (utype < 0) { |
| 628 | return 0; |
| 629 | } |
| 630 | if (uctx->ctx.ctx != utype) { |
| 631 | return 0; |
| 632 | } |
| 633 | switch (utype) { |
| 634 | case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: |
| 635 | if (uctx->ctx.u.perf_counter.type |
| 636 | != ctx->u.perf_counter.type) { |
| 637 | return 0; |
| 638 | } |
| 639 | if (uctx->ctx.u.perf_counter.config |
| 640 | != ctx->u.perf_counter.config) { |
| 641 | return 0; |
| 642 | } |
| 643 | if (strncmp(uctx->ctx.u.perf_counter.name, |
| 644 | ctx->u.perf_counter.name, |
| 645 | LTTNG_UST_SYM_NAME_LEN)) { |
| 646 | return 0; |
| 647 | } |
| 648 | break; |
| 649 | case LTTNG_UST_CONTEXT_APP_CONTEXT: |
| 650 | assert(uctx->ctx.u.app_ctx.provider_name); |
| 651 | assert(uctx->ctx.u.app_ctx.ctx_name); |
| 652 | if (strcmp(uctx->ctx.u.app_ctx.provider_name, |
| 653 | ctx->u.app_ctx.provider_name) || |
| 654 | strcmp(uctx->ctx.u.app_ctx.ctx_name, |
| 655 | ctx->u.app_ctx.ctx_name)) { |
| 656 | return 0; |
| 657 | } |
| 658 | default: |
| 659 | break; |
| 660 | |
| 661 | } |
| 662 | return 1; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Allocate and initialize an UST context. |
| 667 | * |
| 668 | * Return pointer to structure or NULL. |
| 669 | */ |
| 670 | struct ltt_ust_context *trace_ust_create_context( |
| 671 | const struct lttng_event_context *ctx) |
| 672 | { |
| 673 | struct ltt_ust_context *uctx = NULL; |
| 674 | int utype; |
| 675 | |
| 676 | assert(ctx); |
| 677 | |
| 678 | utype = trace_ust_context_type_event_to_ust(ctx->ctx); |
| 679 | if (utype < 0) { |
| 680 | ERR("Invalid UST context"); |
| 681 | goto end; |
| 682 | } |
| 683 | |
| 684 | uctx = zmalloc(sizeof(struct ltt_ust_context)); |
| 685 | if (!uctx) { |
| 686 | PERROR("zmalloc ltt_ust_context"); |
| 687 | goto end; |
| 688 | } |
| 689 | |
| 690 | uctx->ctx.ctx = (enum lttng_ust_context_type) utype; |
| 691 | switch (utype) { |
| 692 | case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: |
| 693 | uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type; |
| 694 | uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config; |
| 695 | strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name, |
| 696 | LTTNG_UST_SYM_NAME_LEN); |
| 697 | uctx->ctx.u.perf_counter.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 698 | break; |
| 699 | case LTTNG_UST_CONTEXT_APP_CONTEXT: |
| 700 | { |
| 701 | char *provider_name = NULL, *ctx_name = NULL; |
| 702 | |
| 703 | provider_name = strdup(ctx->u.app_ctx.provider_name); |
| 704 | if (!provider_name) { |
| 705 | goto error; |
| 706 | } |
| 707 | uctx->ctx.u.app_ctx.provider_name = provider_name; |
| 708 | |
| 709 | ctx_name = strdup(ctx->u.app_ctx.ctx_name); |
| 710 | if (!ctx_name) { |
| 711 | goto error; |
| 712 | } |
| 713 | uctx->ctx.u.app_ctx.ctx_name = ctx_name; |
| 714 | break; |
| 715 | } |
| 716 | default: |
| 717 | break; |
| 718 | } |
| 719 | lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx); |
| 720 | end: |
| 721 | return uctx; |
| 722 | error: |
| 723 | trace_ust_destroy_context(uctx); |
| 724 | return NULL; |
| 725 | } |
| 726 | |
| 727 | static void destroy_id_tracker_node_rcu(struct rcu_head *head) |
| 728 | { |
| 729 | struct ust_id_tracker_node *tracker_node = caa_container_of( |
| 730 | head, struct ust_id_tracker_node, node.head); |
| 731 | free(tracker_node); |
| 732 | } |
| 733 | |
| 734 | static void destroy_id_tracker_node(struct ust_id_tracker_node *tracker_node) |
| 735 | { |
| 736 | call_rcu(&tracker_node->node.head, destroy_id_tracker_node_rcu); |
| 737 | } |
| 738 | |
| 739 | static int init_id_tracker(struct ust_id_tracker *id_tracker) |
| 740 | { |
| 741 | int ret = LTTNG_OK; |
| 742 | |
| 743 | id_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 744 | if (!id_tracker->ht) { |
| 745 | ret = LTTNG_ERR_NOMEM; |
| 746 | goto end; |
| 747 | } |
| 748 | |
| 749 | end: |
| 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Teardown id tracker content, but don't free id_tracker object. |
| 755 | */ |
| 756 | static void fini_id_tracker(struct ust_id_tracker *id_tracker) |
| 757 | { |
| 758 | struct ust_id_tracker_node *tracker_node; |
| 759 | struct lttng_ht_iter iter; |
| 760 | |
| 761 | if (!id_tracker->ht) { |
| 762 | return; |
| 763 | } |
| 764 | rcu_read_lock(); |
| 765 | cds_lfht_for_each_entry (id_tracker->ht->ht, &iter.iter, tracker_node, |
| 766 | node.node) { |
| 767 | int ret = lttng_ht_del(id_tracker->ht, &iter); |
| 768 | |
| 769 | assert(!ret); |
| 770 | destroy_id_tracker_node(tracker_node); |
| 771 | } |
| 772 | rcu_read_unlock(); |
| 773 | ht_cleanup_push(id_tracker->ht); |
| 774 | id_tracker->ht = NULL; |
| 775 | } |
| 776 | |
| 777 | static struct ust_id_tracker_node *id_tracker_lookup( |
| 778 | struct ust_id_tracker *id_tracker, |
| 779 | int id, |
| 780 | struct lttng_ht_iter *iter) |
| 781 | { |
| 782 | unsigned long _id = (unsigned long) id; |
| 783 | struct lttng_ht_node_ulong *node; |
| 784 | |
| 785 | lttng_ht_lookup(id_tracker->ht, (void *) _id, iter); |
| 786 | node = lttng_ht_iter_get_node_ulong(iter); |
| 787 | if (node) { |
| 788 | return caa_container_of(node, struct ust_id_tracker_node, node); |
| 789 | } else { |
| 790 | return NULL; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | static int id_tracker_add_id(struct ust_id_tracker *id_tracker, int id) |
| 795 | { |
| 796 | int retval = LTTNG_OK; |
| 797 | struct ust_id_tracker_node *tracker_node; |
| 798 | struct lttng_ht_iter iter; |
| 799 | |
| 800 | if (id < 0) { |
| 801 | retval = LTTNG_ERR_INVALID; |
| 802 | goto end; |
| 803 | } |
| 804 | tracker_node = id_tracker_lookup(id_tracker, id, &iter); |
| 805 | if (tracker_node) { |
| 806 | /* Already exists. */ |
| 807 | retval = LTTNG_ERR_ID_TRACKED; |
| 808 | goto end; |
| 809 | } |
| 810 | tracker_node = zmalloc(sizeof(*tracker_node)); |
| 811 | if (!tracker_node) { |
| 812 | retval = LTTNG_ERR_NOMEM; |
| 813 | goto end; |
| 814 | } |
| 815 | lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) id); |
| 816 | lttng_ht_add_unique_ulong(id_tracker->ht, &tracker_node->node); |
| 817 | end: |
| 818 | return retval; |
| 819 | } |
| 820 | |
| 821 | static int id_tracker_del_id(struct ust_id_tracker *id_tracker, int id) |
| 822 | { |
| 823 | int retval = LTTNG_OK, ret; |
| 824 | struct ust_id_tracker_node *tracker_node; |
| 825 | struct lttng_ht_iter iter; |
| 826 | |
| 827 | if (id < 0) { |
| 828 | retval = LTTNG_ERR_INVALID; |
| 829 | goto end; |
| 830 | } |
| 831 | tracker_node = id_tracker_lookup(id_tracker, id, &iter); |
| 832 | if (!tracker_node) { |
| 833 | /* Not found */ |
| 834 | retval = LTTNG_ERR_ID_NOT_TRACKED; |
| 835 | goto end; |
| 836 | } |
| 837 | ret = lttng_ht_del(id_tracker->ht, &iter); |
| 838 | assert(!ret); |
| 839 | |
| 840 | destroy_id_tracker_node(tracker_node); |
| 841 | end: |
| 842 | return retval; |
| 843 | } |
| 844 | |
| 845 | static struct ust_id_tracker *get_id_tracker(struct ltt_ust_session *session, |
| 846 | enum lttng_tracker_type tracker_type) |
| 847 | { |
| 848 | switch (tracker_type) { |
| 849 | case LTTNG_TRACKER_VPID: |
| 850 | return &session->vpid_tracker; |
| 851 | case LTTNG_TRACKER_VUID: |
| 852 | return &session->vuid_tracker; |
| 853 | case LTTNG_TRACKER_VGID: |
| 854 | return &session->vgid_tracker; |
| 855 | default: |
| 856 | return NULL; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | static struct lttng_tracker_list *get_id_tracker_list( |
| 861 | struct ltt_ust_session *session, |
| 862 | enum lttng_tracker_type tracker_type) |
| 863 | { |
| 864 | switch (tracker_type) { |
| 865 | case LTTNG_TRACKER_VPID: |
| 866 | return session->tracker_list_vpid; |
| 867 | case LTTNG_TRACKER_VUID: |
| 868 | return session->tracker_list_vuid; |
| 869 | case LTTNG_TRACKER_VGID: |
| 870 | return session->tracker_list_vgid; |
| 871 | default: |
| 872 | return NULL; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * The session lock is held when calling this function. |
| 878 | */ |
| 879 | int trace_ust_id_tracker_lookup(enum lttng_tracker_type tracker_type, |
| 880 | struct ltt_ust_session *session, |
| 881 | int id) |
| 882 | { |
| 883 | struct lttng_ht_iter iter; |
| 884 | struct ust_id_tracker *id_tracker; |
| 885 | |
| 886 | id_tracker = get_id_tracker(session, tracker_type); |
| 887 | if (!id_tracker) { |
| 888 | abort(); |
| 889 | } |
| 890 | if (!id_tracker->ht) { |
| 891 | return 1; |
| 892 | } |
| 893 | if (id_tracker_lookup(id_tracker, id, &iter)) { |
| 894 | return 1; |
| 895 | } |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * Called with the session lock held. |
| 901 | */ |
| 902 | int trace_ust_track_id(enum lttng_tracker_type tracker_type, |
| 903 | struct ltt_ust_session *session, |
| 904 | const struct lttng_tracker_id *id) |
| 905 | { |
| 906 | int retval = LTTNG_OK; |
| 907 | bool should_update_apps = false; |
| 908 | struct ust_id_tracker *id_tracker; |
| 909 | struct lttng_tracker_list *tracker_list; |
| 910 | int value; |
| 911 | struct lttng_tracker_ids *saved_ids; |
| 912 | |
| 913 | if (tracker_type == LTTNG_TRACKER_PID) { |
| 914 | DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain."); |
| 915 | tracker_type = LTTNG_TRACKER_VPID; |
| 916 | } |
| 917 | |
| 918 | retval = lttng_tracker_id_lookup_string(tracker_type, id, &value); |
| 919 | if (retval != LTTNG_OK) { |
| 920 | return retval; |
| 921 | } |
| 922 | tracker_list = get_id_tracker_list(session, tracker_type); |
| 923 | if (!tracker_list) { |
| 924 | return LTTNG_ERR_INVALID; |
| 925 | } |
| 926 | /* Save list for restore on error. */ |
| 927 | retval = lttng_tracker_id_get_list(tracker_list, &saved_ids); |
| 928 | if (retval != LTTNG_OK) { |
| 929 | return LTTNG_ERR_INVALID; |
| 930 | } |
| 931 | /* Add to list. */ |
| 932 | retval = lttng_tracker_list_add(tracker_list, id); |
| 933 | if (retval != LTTNG_OK) { |
| 934 | goto end; |
| 935 | } |
| 936 | |
| 937 | id_tracker = get_id_tracker(session, tracker_type); |
| 938 | if (!id_tracker) { |
| 939 | abort(); |
| 940 | } |
| 941 | if (value == -1) { |
| 942 | /* Track all ids: destroy tracker if exists. */ |
| 943 | if (id_tracker->ht) { |
| 944 | fini_id_tracker(id_tracker); |
| 945 | /* Ensure all apps have session. */ |
| 946 | should_update_apps = true; |
| 947 | } |
| 948 | } else { |
| 949 | if (!id_tracker->ht) { |
| 950 | /* Create tracker. */ |
| 951 | retval = init_id_tracker(id_tracker); |
| 952 | if (retval != LTTNG_OK) { |
| 953 | ERR("Error initializing ID tracker"); |
| 954 | goto end_restore; |
| 955 | } |
| 956 | retval = id_tracker_add_id(id_tracker, value); |
| 957 | if (retval != LTTNG_OK) { |
| 958 | fini_id_tracker(id_tracker); |
| 959 | goto end_restore; |
| 960 | } |
| 961 | /* Remove all apps from session except pid. */ |
| 962 | should_update_apps = true; |
| 963 | } else { |
| 964 | struct ust_app *app; |
| 965 | |
| 966 | retval = id_tracker_add_id(id_tracker, value); |
| 967 | if (retval != LTTNG_OK) { |
| 968 | goto end_restore; |
| 969 | } |
| 970 | /* Add session to application */ |
| 971 | switch (tracker_type) { |
| 972 | case LTTNG_TRACKER_VPID: |
| 973 | app = ust_app_find_by_pid(value); |
| 974 | if (app) { |
| 975 | should_update_apps = true; |
| 976 | } |
| 977 | break; |
| 978 | default: |
| 979 | should_update_apps = true; |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | if (should_update_apps && session->active) { |
| 984 | ust_app_global_update_all(session); |
| 985 | } |
| 986 | goto end; |
| 987 | |
| 988 | end_restore: |
| 989 | if (lttng_tracker_id_set_list(tracker_list, saved_ids) != LTTNG_OK) { |
| 990 | ERR("Error on tracker add error handling.\n"); |
| 991 | } |
| 992 | end: |
| 993 | lttng_tracker_ids_destroy(saved_ids); |
| 994 | return retval; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Called with the session lock held. |
| 999 | */ |
| 1000 | int trace_ust_untrack_id(enum lttng_tracker_type tracker_type, |
| 1001 | struct ltt_ust_session *session, |
| 1002 | const struct lttng_tracker_id *id) |
| 1003 | { |
| 1004 | int retval = LTTNG_OK; |
| 1005 | bool should_update_apps = false; |
| 1006 | struct ust_id_tracker *id_tracker; |
| 1007 | struct lttng_tracker_list *tracker_list; |
| 1008 | int value; |
| 1009 | struct lttng_tracker_ids *saved_ids; |
| 1010 | |
| 1011 | if (tracker_type == LTTNG_TRACKER_PID) { |
| 1012 | DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain."); |
| 1013 | tracker_type = LTTNG_TRACKER_VPID; |
| 1014 | } |
| 1015 | |
| 1016 | retval = lttng_tracker_id_lookup_string(tracker_type, id, &value); |
| 1017 | if (retval != LTTNG_OK) { |
| 1018 | return retval; |
| 1019 | } |
| 1020 | |
| 1021 | tracker_list = get_id_tracker_list(session, tracker_type); |
| 1022 | if (!tracker_list) { |
| 1023 | return LTTNG_ERR_INVALID; |
| 1024 | } |
| 1025 | /* Save list for restore on error. */ |
| 1026 | retval = lttng_tracker_id_get_list(tracker_list, &saved_ids); |
| 1027 | if (retval != LTTNG_OK) { |
| 1028 | return LTTNG_ERR_INVALID; |
| 1029 | } |
| 1030 | /* Remove from list. */ |
| 1031 | retval = lttng_tracker_list_remove(tracker_list, id); |
| 1032 | if (retval != LTTNG_OK) { |
| 1033 | goto end; |
| 1034 | } |
| 1035 | |
| 1036 | id_tracker = get_id_tracker(session, tracker_type); |
| 1037 | if (!id_tracker) { |
| 1038 | abort(); |
| 1039 | } |
| 1040 | |
| 1041 | if (value == -1) { |
| 1042 | /* Create empty tracker, replace old tracker. */ |
| 1043 | struct ust_id_tracker tmp_tracker; |
| 1044 | |
| 1045 | tmp_tracker = *id_tracker; |
| 1046 | retval = init_id_tracker(id_tracker); |
| 1047 | if (retval != LTTNG_OK) { |
| 1048 | ERR("Error initializing ID tracker"); |
| 1049 | /* Rollback operation. */ |
| 1050 | *id_tracker = tmp_tracker; |
| 1051 | goto end_restore; |
| 1052 | } |
| 1053 | fini_id_tracker(&tmp_tracker); |
| 1054 | |
| 1055 | /* Remove session from all applications */ |
| 1056 | should_update_apps = true; |
| 1057 | } else { |
| 1058 | struct ust_app *app; |
| 1059 | |
| 1060 | if (!id_tracker->ht) { |
| 1061 | /* No ID being tracked. */ |
| 1062 | retval = LTTNG_ERR_ID_NOT_TRACKED; |
| 1063 | goto end_restore; |
| 1064 | } |
| 1065 | /* Remove ID from tracker */ |
| 1066 | retval = id_tracker_del_id(id_tracker, value); |
| 1067 | if (retval != LTTNG_OK) { |
| 1068 | goto end_restore; |
| 1069 | } |
| 1070 | switch (tracker_type) { |
| 1071 | case LTTNG_TRACKER_VPID: |
| 1072 | /* Remove session from application. */ |
| 1073 | app = ust_app_find_by_pid(value); |
| 1074 | if (app) { |
| 1075 | should_update_apps = true; |
| 1076 | } |
| 1077 | break; |
| 1078 | default: |
| 1079 | /* Keep only apps matching ID. */ |
| 1080 | should_update_apps = true; |
| 1081 | } |
| 1082 | } |
| 1083 | if (should_update_apps && session->active) { |
| 1084 | ust_app_global_update_all(session); |
| 1085 | } |
| 1086 | goto end; |
| 1087 | |
| 1088 | end_restore: |
| 1089 | if (lttng_tracker_id_set_list(tracker_list, saved_ids) != LTTNG_OK) { |
| 1090 | ERR("Error on tracker remove error handling.\n"); |
| 1091 | } |
| 1092 | end: |
| 1093 | lttng_tracker_ids_destroy(saved_ids); |
| 1094 | return retval; |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * Called with session lock held. |
| 1099 | */ |
| 1100 | int trace_ust_list_tracker_ids(enum lttng_tracker_type tracker_type, |
| 1101 | struct ltt_ust_session *session, |
| 1102 | struct lttng_tracker_ids **_ids) |
| 1103 | { |
| 1104 | int ret = LTTNG_OK; |
| 1105 | struct lttng_tracker_list *tracker_list; |
| 1106 | |
| 1107 | if (tracker_type == LTTNG_TRACKER_PID) { |
| 1108 | DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain."); |
| 1109 | tracker_type = LTTNG_TRACKER_VPID; |
| 1110 | } |
| 1111 | |
| 1112 | tracker_list = get_id_tracker_list(session, tracker_type); |
| 1113 | if (!tracker_list) { |
| 1114 | ret = -LTTNG_ERR_INVALID; |
| 1115 | goto end; |
| 1116 | } |
| 1117 | |
| 1118 | ret = lttng_tracker_id_get_list(tracker_list, _ids); |
| 1119 | if (ret != LTTNG_OK) { |
| 1120 | ret = -LTTNG_ERR_INVALID; |
| 1121 | goto end; |
| 1122 | } |
| 1123 | end: |
| 1124 | return ret; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * RCU safe free context structure. |
| 1129 | */ |
| 1130 | static void destroy_context_rcu(struct rcu_head *head) |
| 1131 | { |
| 1132 | struct lttng_ht_node_ulong *node = |
| 1133 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 1134 | struct ltt_ust_context *ctx = |
| 1135 | caa_container_of(node, struct ltt_ust_context, node); |
| 1136 | |
| 1137 | trace_ust_destroy_context(ctx); |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * Cleanup UST context hash table. |
| 1142 | */ |
| 1143 | static void destroy_contexts(struct lttng_ht *ht) |
| 1144 | { |
| 1145 | int ret; |
| 1146 | struct lttng_ht_node_ulong *node; |
| 1147 | struct lttng_ht_iter iter; |
| 1148 | struct ltt_ust_context *ctx; |
| 1149 | |
| 1150 | assert(ht); |
| 1151 | |
| 1152 | rcu_read_lock(); |
| 1153 | cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) { |
| 1154 | /* Remove from ordered list. */ |
| 1155 | ctx = caa_container_of(node, struct ltt_ust_context, node); |
| 1156 | cds_list_del(&ctx->list); |
| 1157 | /* Remove from channel's hash table. */ |
| 1158 | ret = lttng_ht_del(ht, &iter); |
| 1159 | if (!ret) { |
| 1160 | call_rcu(&node->head, destroy_context_rcu); |
| 1161 | } |
| 1162 | } |
| 1163 | rcu_read_unlock(); |
| 1164 | |
| 1165 | ht_cleanup_push(ht); |
| 1166 | } |
| 1167 | |
| 1168 | /* |
| 1169 | * Cleanup ust event structure. |
| 1170 | */ |
| 1171 | void trace_ust_destroy_event(struct ltt_ust_event *event) |
| 1172 | { |
| 1173 | assert(event); |
| 1174 | |
| 1175 | DBG2("Trace destroy UST event %s", event->attr.name); |
| 1176 | free(event->filter_expression); |
| 1177 | free(event->filter); |
| 1178 | free(event->exclusion); |
| 1179 | free(event); |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | * Cleanup ust context structure. |
| 1184 | */ |
| 1185 | void trace_ust_destroy_context(struct ltt_ust_context *ctx) |
| 1186 | { |
| 1187 | assert(ctx); |
| 1188 | |
| 1189 | if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) { |
| 1190 | free(ctx->ctx.u.app_ctx.provider_name); |
| 1191 | free(ctx->ctx.u.app_ctx.ctx_name); |
| 1192 | } |
| 1193 | free(ctx); |
| 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * URCU intermediate call to complete destroy event. |
| 1198 | */ |
| 1199 | static void destroy_event_rcu(struct rcu_head *head) |
| 1200 | { |
| 1201 | struct lttng_ht_node_str *node = |
| 1202 | caa_container_of(head, struct lttng_ht_node_str, head); |
| 1203 | struct ltt_ust_event *event = |
| 1204 | caa_container_of(node, struct ltt_ust_event, node); |
| 1205 | |
| 1206 | trace_ust_destroy_event(event); |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * Cleanup UST events hashtable. |
| 1211 | */ |
| 1212 | static void destroy_events(struct lttng_ht *events) |
| 1213 | { |
| 1214 | int ret; |
| 1215 | struct lttng_ht_node_str *node; |
| 1216 | struct lttng_ht_iter iter; |
| 1217 | |
| 1218 | assert(events); |
| 1219 | |
| 1220 | rcu_read_lock(); |
| 1221 | cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) { |
| 1222 | ret = lttng_ht_del(events, &iter); |
| 1223 | assert(!ret); |
| 1224 | call_rcu(&node->head, destroy_event_rcu); |
| 1225 | } |
| 1226 | rcu_read_unlock(); |
| 1227 | |
| 1228 | ht_cleanup_push(events); |
| 1229 | } |
| 1230 | |
| 1231 | /* |
| 1232 | * Cleanup ust channel structure. |
| 1233 | * |
| 1234 | * Should _NOT_ be called with RCU read lock held. |
| 1235 | */ |
| 1236 | static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel) |
| 1237 | { |
| 1238 | assert(channel); |
| 1239 | |
| 1240 | DBG2("Trace destroy UST channel %s", channel->name); |
| 1241 | |
| 1242 | free(channel); |
| 1243 | } |
| 1244 | |
| 1245 | /* |
| 1246 | * URCU intermediate call to complete destroy channel. |
| 1247 | */ |
| 1248 | static void destroy_channel_rcu(struct rcu_head *head) |
| 1249 | { |
| 1250 | struct lttng_ht_node_str *node = |
| 1251 | caa_container_of(head, struct lttng_ht_node_str, head); |
| 1252 | struct ltt_ust_channel *channel = |
| 1253 | caa_container_of(node, struct ltt_ust_channel, node); |
| 1254 | |
| 1255 | _trace_ust_destroy_channel(channel); |
| 1256 | } |
| 1257 | |
| 1258 | void trace_ust_destroy_channel(struct ltt_ust_channel *channel) |
| 1259 | { |
| 1260 | /* Destroying all events of the channel */ |
| 1261 | destroy_events(channel->events); |
| 1262 | /* Destroying all context of the channel */ |
| 1263 | destroy_contexts(channel->ctx); |
| 1264 | |
| 1265 | call_rcu(&channel->node.head, destroy_channel_rcu); |
| 1266 | } |
| 1267 | |
| 1268 | /* |
| 1269 | * Remove an UST channel from a channel HT. |
| 1270 | */ |
| 1271 | void trace_ust_delete_channel(struct lttng_ht *ht, |
| 1272 | struct ltt_ust_channel *channel) |
| 1273 | { |
| 1274 | int ret; |
| 1275 | struct lttng_ht_iter iter; |
| 1276 | |
| 1277 | assert(ht); |
| 1278 | assert(channel); |
| 1279 | |
| 1280 | iter.iter.node = &channel->node.node; |
| 1281 | ret = lttng_ht_del(ht, &iter); |
| 1282 | assert(!ret); |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Iterate over a hash table containing channels and cleanup safely. |
| 1287 | */ |
| 1288 | static void destroy_channels(struct lttng_ht *channels) |
| 1289 | { |
| 1290 | struct lttng_ht_node_str *node; |
| 1291 | struct lttng_ht_iter iter; |
| 1292 | |
| 1293 | assert(channels); |
| 1294 | |
| 1295 | rcu_read_lock(); |
| 1296 | cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) { |
| 1297 | struct ltt_ust_channel *chan = |
| 1298 | caa_container_of(node, struct ltt_ust_channel, node); |
| 1299 | |
| 1300 | trace_ust_delete_channel(channels, chan); |
| 1301 | trace_ust_destroy_channel(chan); |
| 1302 | } |
| 1303 | rcu_read_unlock(); |
| 1304 | |
| 1305 | ht_cleanup_push(channels); |
| 1306 | } |
| 1307 | |
| 1308 | /* |
| 1309 | * Cleanup UST global domain. |
| 1310 | */ |
| 1311 | static void destroy_domain_global(struct ltt_ust_domain_global *dom) |
| 1312 | { |
| 1313 | assert(dom); |
| 1314 | |
| 1315 | destroy_channels(dom->channels); |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * Cleanup ust session structure, keeping data required by |
| 1320 | * destroy notifier. |
| 1321 | * |
| 1322 | * Should *NOT* be called with RCU read-side lock held. |
| 1323 | */ |
| 1324 | void trace_ust_destroy_session(struct ltt_ust_session *session) |
| 1325 | { |
| 1326 | struct agent *agt; |
| 1327 | struct buffer_reg_uid *reg, *sreg; |
| 1328 | struct lttng_ht_iter iter; |
| 1329 | |
| 1330 | assert(session); |
| 1331 | |
| 1332 | DBG2("Trace UST destroy session %" PRIu64, session->id); |
| 1333 | |
| 1334 | /* Cleaning up UST domain */ |
| 1335 | destroy_domain_global(&session->domain_global); |
| 1336 | |
| 1337 | rcu_read_lock(); |
| 1338 | cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) { |
| 1339 | int ret = lttng_ht_del(session->agents, &iter); |
| 1340 | |
| 1341 | assert(!ret); |
| 1342 | agent_destroy(agt); |
| 1343 | } |
| 1344 | rcu_read_unlock(); |
| 1345 | |
| 1346 | ht_cleanup_push(session->agents); |
| 1347 | |
| 1348 | /* Cleanup UID buffer registry object(s). */ |
| 1349 | cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list, |
| 1350 | lnode) { |
| 1351 | cds_list_del(®->lnode); |
| 1352 | buffer_reg_uid_remove(reg); |
| 1353 | buffer_reg_uid_destroy(reg, session->consumer); |
| 1354 | } |
| 1355 | |
| 1356 | lttng_tracker_list_destroy(session->tracker_list_vpid); |
| 1357 | lttng_tracker_list_destroy(session->tracker_list_vuid); |
| 1358 | lttng_tracker_list_destroy(session->tracker_list_vgid); |
| 1359 | |
| 1360 | fini_id_tracker(&session->vpid_tracker); |
| 1361 | fini_id_tracker(&session->vuid_tracker); |
| 1362 | fini_id_tracker(&session->vgid_tracker); |
| 1363 | lttng_trace_chunk_put(session->current_trace_chunk); |
| 1364 | } |
| 1365 | |
| 1366 | /* Free elements needed by destroy notifiers. */ |
| 1367 | void trace_ust_free_session(struct ltt_ust_session *session) |
| 1368 | { |
| 1369 | consumer_output_put(session->consumer); |
| 1370 | free(session); |
| 1371 | } |