| 1 | /* |
| 2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | #define _GNU_SOURCE |
| 18 | #include <assert.h> |
| 19 | #include <inttypes.h> |
| 20 | |
| 21 | #include <common/common.h> |
| 22 | #include <common/hashtable/utils.h> |
| 23 | #include <lttng/lttng.h> |
| 24 | |
| 25 | #include "ust-registry.h" |
| 26 | |
| 27 | /* |
| 28 | * Hash table match function for event in the registry. |
| 29 | */ |
| 30 | static int ht_match_event(struct cds_lfht_node *node, const void *_key) |
| 31 | { |
| 32 | struct ust_registry_event *event; |
| 33 | const struct ust_registry_event *key; |
| 34 | |
| 35 | assert(node); |
| 36 | assert(_key); |
| 37 | |
| 38 | event = caa_container_of(node, struct ust_registry_event, node.node); |
| 39 | assert(event); |
| 40 | key = _key; |
| 41 | |
| 42 | /* It has to be a perfect match. */ |
| 43 | if (strncmp(event->name, key->name, sizeof(event->name)) != 0) { |
| 44 | goto no_match; |
| 45 | } |
| 46 | |
| 47 | /* It has to be a perfect match. */ |
| 48 | if (strncmp(event->signature, key->signature, |
| 49 | strlen(event->signature) != 0)) { |
| 50 | goto no_match; |
| 51 | } |
| 52 | |
| 53 | /* Match */ |
| 54 | return 1; |
| 55 | |
| 56 | no_match: |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static unsigned long ht_hash_event(void *_key, unsigned long seed) |
| 61 | { |
| 62 | uint64_t xored_key; |
| 63 | struct ust_registry_event *key = _key; |
| 64 | |
| 65 | assert(key); |
| 66 | |
| 67 | xored_key = (uint64_t) (hash_key_str(key->name, seed) ^ |
| 68 | hash_key_str(key->signature, seed)); |
| 69 | |
| 70 | return hash_key_u64(&xored_key, seed); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Allocate event and initialize it. This does NOT set a valid event id from a |
| 75 | * registry. |
| 76 | */ |
| 77 | static struct ust_registry_event *alloc_event(int session_objd, |
| 78 | int channel_objd, char *name, char *sig, size_t nr_fields, |
| 79 | struct ustctl_field *fields, int loglevel, char *model_emf_uri) |
| 80 | { |
| 81 | struct ust_registry_event *event = NULL; |
| 82 | |
| 83 | event = zmalloc(sizeof(*event)); |
| 84 | if (!event) { |
| 85 | PERROR("zmalloc ust registry event"); |
| 86 | goto error; |
| 87 | } |
| 88 | |
| 89 | event->session_objd = session_objd; |
| 90 | event->channel_objd = channel_objd; |
| 91 | /* Allocated by ustctl. */ |
| 92 | event->signature = sig; |
| 93 | event->nr_fields = nr_fields; |
| 94 | event->fields = fields; |
| 95 | event->loglevel = loglevel; |
| 96 | event->model_emf_uri = model_emf_uri; |
| 97 | if (name) { |
| 98 | /* Copy event name and force NULL byte. */ |
| 99 | strncpy(event->name, name, sizeof(event->name)); |
| 100 | event->name[sizeof(event->name) - 1] = '\0'; |
| 101 | } |
| 102 | cds_lfht_node_init(&event->node.node); |
| 103 | |
| 104 | error: |
| 105 | return event; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Free event data structure. This does NOT delete it from any hash table. It's |
| 110 | * safe to pass a NULL pointer. This shoudl be called inside a call RCU if the |
| 111 | * event is previously deleted from a rcu hash table. |
| 112 | */ |
| 113 | static void destroy_event(struct ust_registry_event *event) |
| 114 | { |
| 115 | if (!event) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | free(event->fields); |
| 120 | free(event->model_emf_uri); |
| 121 | free(event->signature); |
| 122 | free(event); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Destroy event function call of the call RCU. |
| 127 | */ |
| 128 | static void destroy_event_rcu(struct rcu_head *head) |
| 129 | { |
| 130 | struct lttng_ht_node_u64 *node = |
| 131 | caa_container_of(head, struct lttng_ht_node_u64, head); |
| 132 | struct ust_registry_event *event = |
| 133 | caa_container_of(node, struct ust_registry_event, node); |
| 134 | |
| 135 | destroy_event(event); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Find an event using the name and signature in the given registry. RCU read |
| 140 | * side lock MUST be acquired before calling this function and as long as the |
| 141 | * event reference is kept by the caller. |
| 142 | * |
| 143 | * On success, the event pointer is returned else NULL. |
| 144 | */ |
| 145 | struct ust_registry_event *ust_registry_find_event( |
| 146 | struct ust_registry_channel *chan, char *name, char *sig) |
| 147 | { |
| 148 | struct lttng_ht_node_u64 *node; |
| 149 | struct lttng_ht_iter iter; |
| 150 | struct ust_registry_event *event = NULL; |
| 151 | struct ust_registry_event key; |
| 152 | |
| 153 | assert(chan); |
| 154 | assert(name); |
| 155 | assert(sig); |
| 156 | |
| 157 | /* Setup key for the match function. */ |
| 158 | strncpy(key.name, name, sizeof(key.name)); |
| 159 | key.name[sizeof(key.name) - 1] = '\0'; |
| 160 | key.signature = sig; |
| 161 | |
| 162 | cds_lfht_lookup(chan->ht->ht, chan->ht->hash_fct(&key, lttng_ht_seed), |
| 163 | chan->ht->match_fct, &key, &iter.iter); |
| 164 | node = lttng_ht_iter_get_node_u64(&iter); |
| 165 | if (!node) { |
| 166 | goto end; |
| 167 | } |
| 168 | event = caa_container_of(node, struct ust_registry_event, node); |
| 169 | |
| 170 | end: |
| 171 | return event; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Create a ust_registry_event from the given parameters and add it to the |
| 176 | * registry hash table. If event_id is valid, it is set with the newly created |
| 177 | * event id. |
| 178 | * |
| 179 | * On success, return 0 else a negative value. The created event MUST be unique |
| 180 | * so on duplicate entry -EINVAL is returned. On error, event_id is untouched. |
| 181 | * |
| 182 | * Should be called with session registry mutex held. |
| 183 | */ |
| 184 | int ust_registry_create_event(struct ust_registry_session *session, |
| 185 | uint64_t chan_key, int session_objd, int channel_objd, char *name, |
| 186 | char *sig, size_t nr_fields, struct ustctl_field *fields, int loglevel, |
| 187 | char *model_emf_uri, int buffer_type, uint32_t *event_id_p) |
| 188 | { |
| 189 | int ret; |
| 190 | uint32_t event_id; |
| 191 | struct cds_lfht_node *nptr; |
| 192 | struct ust_registry_event *event = NULL; |
| 193 | struct ust_registry_channel *chan; |
| 194 | |
| 195 | assert(session); |
| 196 | assert(name); |
| 197 | assert(sig); |
| 198 | assert(event_id_p); |
| 199 | |
| 200 | /* |
| 201 | * This should not happen but since it comes from the UST tracer, an |
| 202 | * external party, don't assert and simply validate values. |
| 203 | */ |
| 204 | if (session_objd < 0 || channel_objd < 0) { |
| 205 | ret = -EINVAL; |
| 206 | goto error; |
| 207 | } |
| 208 | |
| 209 | rcu_read_lock(); |
| 210 | |
| 211 | chan = ust_registry_channel_find(session, chan_key); |
| 212 | if (!chan) { |
| 213 | ret = -EINVAL; |
| 214 | goto error_unlock; |
| 215 | } |
| 216 | |
| 217 | /* Check if we've reached the maximum possible id. */ |
| 218 | if (ust_registry_is_max_id(chan->used_event_id)) { |
| 219 | ret = -ENOENT; |
| 220 | goto error_unlock; |
| 221 | } |
| 222 | |
| 223 | event = alloc_event(session_objd, channel_objd, name, sig, nr_fields, |
| 224 | fields, loglevel, model_emf_uri); |
| 225 | if (!event) { |
| 226 | ret = -ENOMEM; |
| 227 | goto error_unlock; |
| 228 | } |
| 229 | |
| 230 | DBG3("UST registry creating event with event: %s, sig: %s, id: %u, " |
| 231 | "chan_objd: %u, sess_objd: %u, chan_id: %u", event->name, |
| 232 | event->signature, event->id, event->channel_objd, |
| 233 | event->session_objd, chan->chan_id); |
| 234 | |
| 235 | /* |
| 236 | * This is an add unique with a custom match function for event. The node |
| 237 | * are matched using the event name and signature. |
| 238 | */ |
| 239 | nptr = cds_lfht_add_unique(chan->ht->ht, chan->ht->hash_fct(event, |
| 240 | lttng_ht_seed), chan->ht->match_fct, event, &event->node.node); |
| 241 | if (nptr != &event->node.node) { |
| 242 | if (buffer_type == LTTNG_BUFFER_PER_UID) { |
| 243 | /* |
| 244 | * This is normal, we just have to send the event id of the |
| 245 | * returned node and make sure we destroy the previously allocated |
| 246 | * event object. |
| 247 | */ |
| 248 | destroy_event(event); |
| 249 | event = caa_container_of(nptr, struct ust_registry_event, |
| 250 | node.node); |
| 251 | assert(event); |
| 252 | event_id = event->id; |
| 253 | } else { |
| 254 | ERR("UST registry create event add unique failed for event: %s, " |
| 255 | "sig: %s, id: %u, chan_objd: %u, sess_objd: %u", |
| 256 | event->name, event->signature, event->id, |
| 257 | event->channel_objd, event->session_objd); |
| 258 | ret = -EINVAL; |
| 259 | goto error_unlock; |
| 260 | } |
| 261 | } else { |
| 262 | /* Request next event id if the node was successfully added. */ |
| 263 | event_id = event->id = ust_registry_get_next_event_id(chan); |
| 264 | } |
| 265 | |
| 266 | *event_id_p = event_id; |
| 267 | |
| 268 | if (!event->metadata_dumped) { |
| 269 | /* Append to metadata */ |
| 270 | ret = ust_metadata_event_statedump(session, chan, event); |
| 271 | if (ret) { |
| 272 | ERR("Error appending event metadata (errno = %d)", ret); |
| 273 | rcu_read_unlock(); |
| 274 | return ret; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | rcu_read_unlock(); |
| 279 | return 0; |
| 280 | |
| 281 | error_unlock: |
| 282 | rcu_read_unlock(); |
| 283 | error: |
| 284 | destroy_event(event); |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * For a given event in a registry, delete the entry and destroy the event. |
| 290 | * This MUST be called within a RCU read side lock section. |
| 291 | */ |
| 292 | void ust_registry_destroy_event(struct ust_registry_channel *chan, |
| 293 | struct ust_registry_event *event) |
| 294 | { |
| 295 | int ret; |
| 296 | struct lttng_ht_iter iter; |
| 297 | |
| 298 | assert(chan); |
| 299 | assert(event); |
| 300 | |
| 301 | /* Delete the node first. */ |
| 302 | iter.iter.node = &event->node.node; |
| 303 | ret = lttng_ht_del(chan->ht, &iter); |
| 304 | assert(!ret); |
| 305 | |
| 306 | call_rcu(&event->node.head, destroy_event_rcu); |
| 307 | |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * We need to execute ht_destroy outside of RCU read-side critical |
| 313 | * section, so we postpone its execution using call_rcu. It is simpler |
| 314 | * than to change the semantic of the many callers of |
| 315 | * destroy_channel(). |
| 316 | */ |
| 317 | static |
| 318 | void destroy_channel_rcu(struct rcu_head *head) |
| 319 | { |
| 320 | struct ust_registry_channel *chan = |
| 321 | caa_container_of(head, struct ust_registry_channel, rcu_head); |
| 322 | |
| 323 | lttng_ht_destroy(chan->ht); |
| 324 | free(chan); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Destroy every element of the registry and free the memory. This does NOT |
| 329 | * free the registry pointer since it might not have been allocated before so |
| 330 | * it's the caller responsability. |
| 331 | */ |
| 332 | static void destroy_channel(struct ust_registry_channel *chan) |
| 333 | { |
| 334 | struct lttng_ht_iter iter; |
| 335 | struct ust_registry_event *event; |
| 336 | |
| 337 | assert(chan); |
| 338 | |
| 339 | rcu_read_lock(); |
| 340 | /* Destroy all event associated with this registry. */ |
| 341 | cds_lfht_for_each_entry(chan->ht->ht, &iter.iter, event, node.node) { |
| 342 | /* Delete the node from the ht and free it. */ |
| 343 | ust_registry_destroy_event(chan, event); |
| 344 | } |
| 345 | rcu_read_unlock(); |
| 346 | call_rcu(&chan->rcu_head, destroy_channel_rcu); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Initialize registry with default values. |
| 351 | */ |
| 352 | int ust_registry_channel_add(struct ust_registry_session *session, |
| 353 | uint64_t key) |
| 354 | { |
| 355 | int ret = 0; |
| 356 | struct ust_registry_channel *chan; |
| 357 | |
| 358 | assert(session); |
| 359 | |
| 360 | chan = zmalloc(sizeof(*chan)); |
| 361 | if (!chan) { |
| 362 | PERROR("zmalloc ust registry channel"); |
| 363 | ret = -ENOMEM; |
| 364 | goto error; |
| 365 | } |
| 366 | |
| 367 | chan->ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 368 | if (!chan->ht) { |
| 369 | ret = -ENOMEM; |
| 370 | goto error; |
| 371 | } |
| 372 | |
| 373 | /* Set custom match function. */ |
| 374 | chan->ht->match_fct = ht_match_event; |
| 375 | chan->ht->hash_fct = ht_hash_event; |
| 376 | |
| 377 | /* |
| 378 | * Assign a channel ID right now since the event notification comes |
| 379 | * *before* the channel notify so the ID needs to be set at this point so |
| 380 | * the metadata can be dumped for that event. |
| 381 | */ |
| 382 | if (ust_registry_is_max_id(session->used_channel_id)) { |
| 383 | ret = -1; |
| 384 | goto error; |
| 385 | } |
| 386 | chan->chan_id = ust_registry_get_next_chan_id(session); |
| 387 | |
| 388 | rcu_read_lock(); |
| 389 | lttng_ht_node_init_u64(&chan->node, key); |
| 390 | lttng_ht_add_unique_u64(session->channels, &chan->node); |
| 391 | rcu_read_unlock(); |
| 392 | |
| 393 | error: |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Find a channel in the given registry. RCU read side lock MUST be acquired |
| 399 | * before calling this function and as long as the event reference is kept by |
| 400 | * the caller. |
| 401 | * |
| 402 | * On success, the pointer is returned else NULL. |
| 403 | */ |
| 404 | struct ust_registry_channel *ust_registry_channel_find( |
| 405 | struct ust_registry_session *session, uint64_t key) |
| 406 | { |
| 407 | struct lttng_ht_node_u64 *node; |
| 408 | struct lttng_ht_iter iter; |
| 409 | struct ust_registry_channel *chan = NULL; |
| 410 | |
| 411 | assert(session); |
| 412 | assert(session->channels); |
| 413 | |
| 414 | DBG3("UST registry channel finding key %" PRIu64, key); |
| 415 | |
| 416 | lttng_ht_lookup(session->channels, &key, &iter); |
| 417 | node = lttng_ht_iter_get_node_u64(&iter); |
| 418 | if (!node) { |
| 419 | goto end; |
| 420 | } |
| 421 | chan = caa_container_of(node, struct ust_registry_channel, node); |
| 422 | |
| 423 | end: |
| 424 | return chan; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Remove channel using key from registry and free memory. |
| 429 | */ |
| 430 | void ust_registry_channel_del_free(struct ust_registry_session *session, |
| 431 | uint64_t key) |
| 432 | { |
| 433 | struct lttng_ht_iter iter; |
| 434 | struct ust_registry_channel *chan; |
| 435 | int ret; |
| 436 | |
| 437 | assert(session); |
| 438 | |
| 439 | rcu_read_lock(); |
| 440 | chan = ust_registry_channel_find(session, key); |
| 441 | if (!chan) { |
| 442 | rcu_read_unlock(); |
| 443 | goto end; |
| 444 | } |
| 445 | |
| 446 | iter.iter.node = &chan->node.node; |
| 447 | ret = lttng_ht_del(session->channels, &iter); |
| 448 | assert(!ret); |
| 449 | rcu_read_unlock(); |
| 450 | destroy_channel(chan); |
| 451 | |
| 452 | end: |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Initialize registry with default values and set the newly allocated session |
| 458 | * pointer to sessionp. |
| 459 | * |
| 460 | * Return 0 on success and sessionp is set or else return -1 and sessionp is |
| 461 | * kept untouched. |
| 462 | */ |
| 463 | int ust_registry_session_init(struct ust_registry_session **sessionp, |
| 464 | struct ust_app *app, |
| 465 | uint32_t bits_per_long, |
| 466 | uint32_t uint8_t_alignment, |
| 467 | uint32_t uint16_t_alignment, |
| 468 | uint32_t uint32_t_alignment, |
| 469 | uint32_t uint64_t_alignment, |
| 470 | uint32_t long_alignment, |
| 471 | int byte_order, |
| 472 | uint32_t major, |
| 473 | uint32_t minor) |
| 474 | { |
| 475 | int ret; |
| 476 | struct ust_registry_session *session; |
| 477 | |
| 478 | assert(sessionp); |
| 479 | |
| 480 | session = zmalloc(sizeof(*session)); |
| 481 | if (!session) { |
| 482 | PERROR("zmalloc ust registry session"); |
| 483 | goto error; |
| 484 | } |
| 485 | |
| 486 | pthread_mutex_init(&session->lock, NULL); |
| 487 | session->bits_per_long = bits_per_long; |
| 488 | session->uint8_t_alignment = uint8_t_alignment; |
| 489 | session->uint16_t_alignment = uint16_t_alignment; |
| 490 | session->uint32_t_alignment = uint32_t_alignment; |
| 491 | session->uint64_t_alignment = uint64_t_alignment; |
| 492 | session->long_alignment = long_alignment; |
| 493 | session->byte_order = byte_order; |
| 494 | |
| 495 | session->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 496 | if (!session->channels) { |
| 497 | goto error; |
| 498 | } |
| 499 | |
| 500 | ret = lttng_uuid_generate(session->uuid); |
| 501 | if (ret) { |
| 502 | ERR("Failed to generate UST uuid (errno = %d)", ret); |
| 503 | goto error; |
| 504 | } |
| 505 | |
| 506 | pthread_mutex_lock(&session->lock); |
| 507 | ret = ust_metadata_session_statedump(session, app, major, minor); |
| 508 | pthread_mutex_unlock(&session->lock); |
| 509 | if (ret) { |
| 510 | ERR("Failed to generate session metadata (errno = %d)", ret); |
| 511 | goto error; |
| 512 | } |
| 513 | |
| 514 | *sessionp = session; |
| 515 | |
| 516 | return 0; |
| 517 | |
| 518 | error: |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Destroy session registry. This does NOT free the given pointer since it |
| 524 | * might get passed as a reference. The registry lock should NOT be acquired. |
| 525 | */ |
| 526 | void ust_registry_session_destroy(struct ust_registry_session *reg) |
| 527 | { |
| 528 | int ret; |
| 529 | struct lttng_ht_iter iter; |
| 530 | struct ust_registry_channel *chan; |
| 531 | |
| 532 | /* On error, EBUSY can be returned if lock. Code flow error. */ |
| 533 | ret = pthread_mutex_destroy(®->lock); |
| 534 | assert(!ret); |
| 535 | |
| 536 | rcu_read_lock(); |
| 537 | /* Destroy all event associated with this registry. */ |
| 538 | cds_lfht_for_each_entry(reg->channels->ht, &iter.iter, chan, node.node) { |
| 539 | /* Delete the node from the ht and free it. */ |
| 540 | ret = lttng_ht_del(reg->channels, &iter); |
| 541 | assert(!ret); |
| 542 | destroy_channel(chan); |
| 543 | } |
| 544 | rcu_read_unlock(); |
| 545 | |
| 546 | lttng_ht_destroy(reg->channels); |
| 547 | free(reg->metadata); |
| 548 | } |