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