| 1 | /* |
| 2 | * ltt-events.c |
| 3 | * |
| 4 | * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * Holds LTTng per-session event registry. |
| 7 | * |
| 8 | * Dual LGPL v2.1/GPL v2 license. |
| 9 | */ |
| 10 | |
| 11 | #define _GNU_SOURCE |
| 12 | #include <stdio.h> |
| 13 | #include <endian.h> |
| 14 | #include <urcu/list.h> |
| 15 | #include <urcu/hlist.h> |
| 16 | #include <pthread.h> |
| 17 | #include <uuid/uuid.h> |
| 18 | #include <errno.h> |
| 19 | #include <sys/shm.h> |
| 20 | #include <sys/ipc.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stddef.h> |
| 23 | |
| 24 | #include <urcu-bp.h> |
| 25 | #include <urcu/compiler.h> |
| 26 | #include <urcu/uatomic.h> |
| 27 | #include <urcu/arch.h> |
| 28 | |
| 29 | #include <lttng/tracepoint.h> |
| 30 | #include <lttng/ust-events.h> |
| 31 | |
| 32 | #include <usterr-signal-safe.h> |
| 33 | #include <helper.h> |
| 34 | #include "error.h" |
| 35 | |
| 36 | #include "ltt-tracer.h" |
| 37 | #include "ltt-tracer-core.h" |
| 38 | #include "wait.h" |
| 39 | #include "../libringbuffer/shm.h" |
| 40 | #include "jhash.h" |
| 41 | |
| 42 | /* |
| 43 | * The sessions mutex is the centralized mutex across UST tracing |
| 44 | * control and probe registration. All operations within this file are |
| 45 | * called by the communication thread, under ust_lock protection. |
| 46 | */ |
| 47 | static pthread_mutex_t sessions_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 48 | |
| 49 | void ust_lock(void) |
| 50 | { |
| 51 | pthread_mutex_lock(&sessions_mutex); |
| 52 | } |
| 53 | |
| 54 | void ust_unlock(void) |
| 55 | { |
| 56 | pthread_mutex_unlock(&sessions_mutex); |
| 57 | } |
| 58 | |
| 59 | static CDS_LIST_HEAD(sessions); |
| 60 | |
| 61 | /* |
| 62 | * Pending probes hash table, containing the registered ltt events for |
| 63 | * which tracepoint probes are still missing. Protected by the sessions |
| 64 | * mutex. |
| 65 | */ |
| 66 | #define PENDING_PROBE_HASH_BITS 6 |
| 67 | #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS) |
| 68 | static struct cds_hlist_head pending_probe_table[PENDING_PROBE_HASH_SIZE]; |
| 69 | |
| 70 | struct ust_pending_probe { |
| 71 | struct ltt_event *event; |
| 72 | struct cds_hlist_node node; |
| 73 | char name[]; |
| 74 | }; |
| 75 | |
| 76 | static void _ltt_event_destroy(struct ltt_event *event); |
| 77 | static void _ltt_loglevel_destroy(struct session_loglevel *sl); |
| 78 | static void _ltt_wildcard_destroy(struct session_wildcard *sw); |
| 79 | static void _ltt_channel_destroy(struct ltt_channel *chan); |
| 80 | static int _ltt_event_unregister(struct ltt_event *event); |
| 81 | static |
| 82 | int _ltt_event_metadata_statedump(struct ltt_session *session, |
| 83 | struct ltt_channel *chan, |
| 84 | struct ltt_event *event); |
| 85 | static |
| 86 | int _ltt_session_metadata_statedump(struct ltt_session *session); |
| 87 | |
| 88 | /* |
| 89 | * called at event creation if probe is missing. |
| 90 | * called with session mutex held. |
| 91 | */ |
| 92 | static |
| 93 | int add_pending_probe(struct ltt_event *event, const char *name) |
| 94 | { |
| 95 | struct cds_hlist_head *head; |
| 96 | struct ust_pending_probe *e; |
| 97 | size_t name_len = strlen(name); |
| 98 | uint32_t hash; |
| 99 | |
| 100 | if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) { |
| 101 | WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1); |
| 102 | name_len = LTTNG_UST_SYM_NAME_LEN - 1; |
| 103 | } |
| 104 | hash = jhash(name, name_len, 0); |
| 105 | head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)]; |
| 106 | e = zmalloc(sizeof(struct ust_pending_probe) + name_len); |
| 107 | if (!e) |
| 108 | return -ENOMEM; |
| 109 | memcpy(&e->name[0], name, name_len + 1); |
| 110 | e->name[name_len] = '\0'; |
| 111 | cds_hlist_add_head(&e->node, head); |
| 112 | e->event = event; |
| 113 | event->pending_probe = e; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * remove a pending probe. called when at event teardown and when an |
| 119 | * event is fixed (probe is loaded). |
| 120 | * called with session mutex held. |
| 121 | */ |
| 122 | static |
| 123 | void remove_pending_probe(struct ust_pending_probe *e) |
| 124 | { |
| 125 | if (!e) |
| 126 | return; |
| 127 | cds_hlist_del(&e->node); |
| 128 | free(e); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Called at library load: connect the probe on the events pending on |
| 133 | * probe load. |
| 134 | * called with session mutex held. |
| 135 | */ |
| 136 | int pending_probe_fix_events(const struct lttng_event_desc *desc) |
| 137 | { |
| 138 | struct cds_hlist_head *head; |
| 139 | struct cds_hlist_node *node, *p; |
| 140 | struct ust_pending_probe *e; |
| 141 | const char *name = desc->name; |
| 142 | int ret = 0; |
| 143 | struct lttng_ust_event event_param; |
| 144 | size_t name_len = strlen(name); |
| 145 | uint32_t hash; |
| 146 | |
| 147 | /* |
| 148 | * For this event, we need to lookup the loglevel. If active (in |
| 149 | * the active loglevels hash table), we must create the event. |
| 150 | */ |
| 151 | if (desc->loglevel) { |
| 152 | const struct tracepoint_loglevel_entry *ev_ll; |
| 153 | struct loglevel_entry *loglevel; |
| 154 | |
| 155 | ev_ll = *desc->loglevel; |
| 156 | loglevel = get_loglevel(ev_ll->identifier); |
| 157 | if (!loglevel) |
| 158 | loglevel = get_loglevel_value(ev_ll->value); |
| 159 | if (loglevel) { |
| 160 | struct session_loglevel *sl; |
| 161 | |
| 162 | cds_list_for_each_entry(sl, &loglevel->session_list, |
| 163 | session_list) { |
| 164 | struct ltt_event *ev; |
| 165 | int ret; |
| 166 | |
| 167 | memcpy(&event_param, &sl->event_param, |
| 168 | sizeof(event_param)); |
| 169 | memcpy(event_param.name, |
| 170 | desc->name, |
| 171 | sizeof(event_param.name)); |
| 172 | /* create event */ |
| 173 | ret = ltt_event_create(sl->chan, |
| 174 | &event_param, NULL, |
| 175 | &ev); |
| 176 | if (ret) { |
| 177 | DBG("Error creating event"); |
| 178 | continue; |
| 179 | } |
| 180 | cds_list_add(&ev->loglevel_list, |
| 181 | &sl->events); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /* Wildcard */ |
| 187 | { |
| 188 | struct wildcard_entry *wildcard; |
| 189 | |
| 190 | wildcard = match_wildcard(desc->name); |
| 191 | if (strcmp(desc->name, "lttng_ust:metadata") && wildcard) { |
| 192 | struct session_wildcard *sw; |
| 193 | |
| 194 | cds_list_for_each_entry(sw, &wildcard->session_list, |
| 195 | session_list) { |
| 196 | struct ltt_event *ev; |
| 197 | int ret; |
| 198 | |
| 199 | memcpy(&event_param, &sw->event_param, |
| 200 | sizeof(event_param)); |
| 201 | memcpy(event_param.name, |
| 202 | desc->name, |
| 203 | sizeof(event_param.name)); |
| 204 | /* create event */ |
| 205 | ret = ltt_event_create(sw->chan, |
| 206 | &event_param, NULL, |
| 207 | &ev); |
| 208 | if (ret) { |
| 209 | DBG("Error creating event"); |
| 210 | continue; |
| 211 | } |
| 212 | cds_list_add(&ev->wildcard_list, |
| 213 | &sw->events); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) { |
| 219 | WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1); |
| 220 | name_len = LTTNG_UST_SYM_NAME_LEN - 1; |
| 221 | } |
| 222 | hash = jhash(name, name_len, 0); |
| 223 | head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)]; |
| 224 | cds_hlist_for_each_entry_safe(e, node, p, head, node) { |
| 225 | struct ltt_event *event; |
| 226 | struct ltt_channel *chan; |
| 227 | |
| 228 | if (strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) |
| 229 | continue; |
| 230 | event = e->event; |
| 231 | chan = event->chan; |
| 232 | assert(!event->desc); |
| 233 | event->desc = desc; |
| 234 | event->pending_probe = NULL; |
| 235 | remove_pending_probe(e); |
| 236 | ret |= __tracepoint_probe_register(name, |
| 237 | event->desc->probe_callback, |
| 238 | event); |
| 239 | if (ret) |
| 240 | continue; |
| 241 | event->id = chan->free_event_id++; |
| 242 | ret |= _ltt_event_metadata_statedump(chan->session, chan, |
| 243 | event); |
| 244 | } |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | void synchronize_trace(void) |
| 249 | { |
| 250 | synchronize_rcu(); |
| 251 | } |
| 252 | |
| 253 | struct ltt_session *ltt_session_create(void) |
| 254 | { |
| 255 | struct ltt_session *session; |
| 256 | |
| 257 | session = zmalloc(sizeof(struct ltt_session)); |
| 258 | if (!session) |
| 259 | return NULL; |
| 260 | CDS_INIT_LIST_HEAD(&session->chan); |
| 261 | CDS_INIT_LIST_HEAD(&session->events); |
| 262 | CDS_INIT_LIST_HEAD(&session->loglevels); |
| 263 | CDS_INIT_LIST_HEAD(&session->wildcards); |
| 264 | uuid_generate(session->uuid); |
| 265 | cds_list_add(&session->list, &sessions); |
| 266 | return session; |
| 267 | } |
| 268 | |
| 269 | void ltt_session_destroy(struct ltt_session *session) |
| 270 | { |
| 271 | struct ltt_channel *chan, *tmpchan; |
| 272 | struct ltt_event *event, *tmpevent; |
| 273 | struct session_loglevel *loglevel, *tmploglevel; |
| 274 | struct session_wildcard *wildcard, *tmpwildcard; |
| 275 | int ret; |
| 276 | |
| 277 | CMM_ACCESS_ONCE(session->active) = 0; |
| 278 | cds_list_for_each_entry(event, &session->events, list) { |
| 279 | ret = _ltt_event_unregister(event); |
| 280 | WARN_ON(ret); |
| 281 | } |
| 282 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 283 | cds_list_for_each_entry_safe(loglevel, tmploglevel, &session->loglevels, list) |
| 284 | _ltt_loglevel_destroy(loglevel); |
| 285 | cds_list_for_each_entry_safe(wildcard, tmpwildcard, &session->wildcards, list) |
| 286 | _ltt_wildcard_destroy(wildcard); |
| 287 | cds_list_for_each_entry_safe(event, tmpevent, &session->events, list) |
| 288 | _ltt_event_destroy(event); |
| 289 | cds_list_for_each_entry_safe(chan, tmpchan, &session->chan, list) |
| 290 | _ltt_channel_destroy(chan); |
| 291 | cds_list_del(&session->list); |
| 292 | free(session); |
| 293 | } |
| 294 | |
| 295 | int ltt_session_enable(struct ltt_session *session) |
| 296 | { |
| 297 | int ret = 0; |
| 298 | struct ltt_channel *chan; |
| 299 | |
| 300 | if (session->active) { |
| 301 | ret = -EBUSY; |
| 302 | goto end; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Snapshot the number of events per channel to know the type of header |
| 307 | * we need to use. |
| 308 | */ |
| 309 | cds_list_for_each_entry(chan, &session->chan, list) { |
| 310 | if (chan->header_type) |
| 311 | continue; /* don't change it if session stop/restart */ |
| 312 | /* |
| 313 | * Because we don't use any timer in the application, we |
| 314 | * currently cannot guarantee that we have frequent |
| 315 | * events that let us detect 27-bit overflows. |
| 316 | * Therefore, for now, we force large event headers, |
| 317 | * which contain 64-bit timestamps. |
| 318 | */ |
| 319 | chan->header_type = 2; /* large */ |
| 320 | #if 0 |
| 321 | if (chan->free_event_id < 31) |
| 322 | chan->header_type = 1; /* compact */ |
| 323 | else |
| 324 | chan->header_type = 2; /* large */ |
| 325 | #endif //0 |
| 326 | } |
| 327 | |
| 328 | CMM_ACCESS_ONCE(session->active) = 1; |
| 329 | CMM_ACCESS_ONCE(session->been_active) = 1; |
| 330 | ret = _ltt_session_metadata_statedump(session); |
| 331 | if (ret) |
| 332 | CMM_ACCESS_ONCE(session->active) = 0; |
| 333 | end: |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | int ltt_session_disable(struct ltt_session *session) |
| 338 | { |
| 339 | int ret = 0; |
| 340 | |
| 341 | if (!session->active) { |
| 342 | ret = -EBUSY; |
| 343 | goto end; |
| 344 | } |
| 345 | CMM_ACCESS_ONCE(session->active) = 0; |
| 346 | end: |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | int ltt_channel_enable(struct ltt_channel *channel) |
| 351 | { |
| 352 | int old; |
| 353 | |
| 354 | if (channel == channel->session->metadata) |
| 355 | return -EPERM; |
| 356 | old = uatomic_xchg(&channel->enabled, 1); |
| 357 | if (old) |
| 358 | return -EEXIST; |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | int ltt_channel_disable(struct ltt_channel *channel) |
| 363 | { |
| 364 | int old; |
| 365 | |
| 366 | if (channel == channel->session->metadata) |
| 367 | return -EPERM; |
| 368 | old = uatomic_xchg(&channel->enabled, 0); |
| 369 | if (!old) |
| 370 | return -EEXIST; |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | int ltt_event_enable(struct ltt_event *event) |
| 375 | { |
| 376 | int old; |
| 377 | |
| 378 | if (event->chan == event->chan->session->metadata) |
| 379 | return -EPERM; |
| 380 | old = uatomic_xchg(&event->enabled, 1); |
| 381 | if (old) |
| 382 | return -EEXIST; |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | int ltt_event_disable(struct ltt_event *event) |
| 387 | { |
| 388 | int old; |
| 389 | |
| 390 | if (event->chan == event->chan->session->metadata) |
| 391 | return -EPERM; |
| 392 | old = uatomic_xchg(&event->enabled, 0); |
| 393 | if (!old) |
| 394 | return -EEXIST; |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | struct ltt_channel *ltt_channel_create(struct ltt_session *session, |
| 399 | const char *transport_name, |
| 400 | void *buf_addr, |
| 401 | size_t subbuf_size, size_t num_subbuf, |
| 402 | unsigned int switch_timer_interval, |
| 403 | unsigned int read_timer_interval, |
| 404 | int *shm_fd, int *wait_fd, |
| 405 | uint64_t *memory_map_size, |
| 406 | struct ltt_channel *chan_priv_init) |
| 407 | { |
| 408 | struct ltt_channel *chan = NULL; |
| 409 | struct ltt_transport *transport; |
| 410 | |
| 411 | if (session->been_active) |
| 412 | goto active; /* Refuse to add channel to active session */ |
| 413 | transport = ltt_transport_find(transport_name); |
| 414 | if (!transport) { |
| 415 | DBG("LTTng transport %s not found\n", |
| 416 | transport_name); |
| 417 | goto notransport; |
| 418 | } |
| 419 | chan_priv_init->id = session->free_chan_id++; |
| 420 | chan_priv_init->session = session; |
| 421 | /* |
| 422 | * Note: the channel creation op already writes into the packet |
| 423 | * headers. Therefore the "chan" information used as input |
| 424 | * should be already accessible. |
| 425 | */ |
| 426 | chan = transport->ops.channel_create("[lttng]", buf_addr, |
| 427 | subbuf_size, num_subbuf, switch_timer_interval, |
| 428 | read_timer_interval, shm_fd, wait_fd, |
| 429 | memory_map_size, chan_priv_init); |
| 430 | if (!chan) |
| 431 | goto create_error; |
| 432 | chan->enabled = 1; |
| 433 | chan->ops = &transport->ops; |
| 434 | cds_list_add(&chan->list, &session->chan); |
| 435 | return chan; |
| 436 | |
| 437 | create_error: |
| 438 | notransport: |
| 439 | active: |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Only used internally at session destruction. |
| 445 | */ |
| 446 | static |
| 447 | void _ltt_channel_destroy(struct ltt_channel *chan) |
| 448 | { |
| 449 | cds_list_del(&chan->list); |
| 450 | lttng_destroy_context(chan->ctx); |
| 451 | chan->ops->channel_destroy(chan); |
| 452 | } |
| 453 | |
| 454 | int ltt_loglevel_create(struct ltt_channel *chan, |
| 455 | struct lttng_ust_event *event_param, |
| 456 | struct session_loglevel **_sl) |
| 457 | { |
| 458 | struct session_loglevel *sl; |
| 459 | |
| 460 | sl = add_loglevel(event_param->name, chan, event_param); |
| 461 | if (!sl || IS_ERR(sl)) { |
| 462 | return PTR_ERR(sl); |
| 463 | } |
| 464 | *_sl = sl; |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | static |
| 469 | void _ltt_loglevel_destroy(struct session_loglevel *sl) |
| 470 | { |
| 471 | _remove_loglevel(sl); |
| 472 | } |
| 473 | |
| 474 | int ltt_wildcard_create(struct ltt_channel *chan, |
| 475 | struct lttng_ust_event *event_param, |
| 476 | struct session_wildcard **_sw) |
| 477 | { |
| 478 | struct session_wildcard *sw; |
| 479 | |
| 480 | sw = add_wildcard(event_param->name, chan, event_param); |
| 481 | if (!sw || IS_ERR(sw)) { |
| 482 | return PTR_ERR(sw); |
| 483 | } |
| 484 | *_sw = sw; |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static |
| 489 | void _ltt_wildcard_destroy(struct session_wildcard *sw) |
| 490 | { |
| 491 | _remove_wildcard(sw); |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Supports event creation while tracing session is active. |
| 496 | */ |
| 497 | int ltt_event_create(struct ltt_channel *chan, |
| 498 | struct lttng_ust_event *event_param, |
| 499 | void *filter, |
| 500 | struct ltt_event **_event) |
| 501 | { |
| 502 | struct ltt_event *event; |
| 503 | int ret = 0; |
| 504 | |
| 505 | if (chan->used_event_id == -1UL) { |
| 506 | ret = -ENOMEM; |
| 507 | goto full; |
| 508 | } |
| 509 | /* |
| 510 | * This is O(n^2) (for each event, the loop is called at event |
| 511 | * creation). Might require a hash if we have lots of events. |
| 512 | */ |
| 513 | cds_list_for_each_entry(event, &chan->session->events, list) { |
| 514 | if (event->desc && !strncmp(event->desc->name, |
| 515 | event_param->name, |
| 516 | LTTNG_UST_SYM_NAME_LEN - 1)) { |
| 517 | ret = -EEXIST; |
| 518 | goto exist; |
| 519 | } |
| 520 | } |
| 521 | event = zmalloc(sizeof(struct ltt_event)); |
| 522 | if (!event) { |
| 523 | ret = -ENOMEM; |
| 524 | goto cache_error; |
| 525 | } |
| 526 | event->chan = chan; |
| 527 | event->filter = filter; |
| 528 | /* |
| 529 | * used_event_id counts the maximum number of event IDs that can |
| 530 | * register if all probes register. |
| 531 | */ |
| 532 | chan->used_event_id++; |
| 533 | event->enabled = 1; |
| 534 | event->instrumentation = event_param->instrumentation; |
| 535 | /* Populate ltt_event structure before tracepoint registration. */ |
| 536 | cmm_smp_wmb(); |
| 537 | switch (event_param->instrumentation) { |
| 538 | case LTTNG_UST_TRACEPOINT: |
| 539 | event->desc = ltt_event_get(event_param->name); |
| 540 | if (event->desc) { |
| 541 | ret = __tracepoint_probe_register(event_param->name, |
| 542 | event->desc->probe_callback, |
| 543 | event); |
| 544 | if (ret) |
| 545 | goto register_error; |
| 546 | event->id = chan->free_event_id++; |
| 547 | } else { |
| 548 | /* |
| 549 | * If the probe is not present, event->desc stays NULL, |
| 550 | * waiting for the probe to register, and the event->id |
| 551 | * stays unallocated. |
| 552 | */ |
| 553 | ret = add_pending_probe(event, event_param->name); |
| 554 | if (ret) |
| 555 | goto add_pending_error; |
| 556 | } |
| 557 | break; |
| 558 | case LTTNG_UST_TRACEPOINT_LOGLEVEL: |
| 559 | assert(0); |
| 560 | break; |
| 561 | default: |
| 562 | WARN_ON_ONCE(1); |
| 563 | } |
| 564 | if (event->desc) { |
| 565 | ret = _ltt_event_metadata_statedump(chan->session, chan, event); |
| 566 | if (ret) |
| 567 | goto statedump_error; |
| 568 | } |
| 569 | cds_list_add(&event->list, &chan->session->events); |
| 570 | *_event = event; |
| 571 | return 0; |
| 572 | |
| 573 | statedump_error: |
| 574 | if (event->desc) { |
| 575 | WARN_ON_ONCE(__tracepoint_probe_unregister(event_param->name, |
| 576 | event->desc->probe_callback, |
| 577 | event)); |
| 578 | ltt_event_put(event->desc); |
| 579 | } |
| 580 | add_pending_error: |
| 581 | register_error: |
| 582 | free(event); |
| 583 | cache_error: |
| 584 | exist: |
| 585 | full: |
| 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Only used internally at session destruction. |
| 591 | */ |
| 592 | int _ltt_event_unregister(struct ltt_event *event) |
| 593 | { |
| 594 | int ret = -EINVAL; |
| 595 | |
| 596 | switch (event->instrumentation) { |
| 597 | case LTTNG_UST_TRACEPOINT: |
| 598 | if (event->desc) { |
| 599 | ret = __tracepoint_probe_unregister(event->desc->name, |
| 600 | event->desc->probe_callback, |
| 601 | event); |
| 602 | if (ret) |
| 603 | return ret; |
| 604 | } else { |
| 605 | remove_pending_probe(event->pending_probe); |
| 606 | ret = 0; |
| 607 | } |
| 608 | break; |
| 609 | default: |
| 610 | WARN_ON_ONCE(1); |
| 611 | } |
| 612 | return ret; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * Only used internally at session destruction. |
| 617 | */ |
| 618 | static |
| 619 | void _ltt_event_destroy(struct ltt_event *event) |
| 620 | { |
| 621 | switch (event->instrumentation) { |
| 622 | case LTTNG_UST_TRACEPOINT: |
| 623 | if (event->desc) { |
| 624 | ltt_event_put(event->desc); |
| 625 | } |
| 626 | break; |
| 627 | default: |
| 628 | WARN_ON_ONCE(1); |
| 629 | } |
| 630 | cds_list_del(&event->list); |
| 631 | lttng_destroy_context(event->ctx); |
| 632 | free(event); |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * We have exclusive access to our metadata buffer (protected by the |
| 637 | * ust_lock), so we can do racy operations such as looking for |
| 638 | * remaining space left in packet and write, since mutual exclusion |
| 639 | * protects us from concurrent writes. |
| 640 | */ |
| 641 | int lttng_metadata_printf(struct ltt_session *session, |
| 642 | const char *fmt, ...) |
| 643 | { |
| 644 | struct lttng_ust_lib_ring_buffer_ctx ctx; |
| 645 | struct ltt_channel *chan = session->metadata; |
| 646 | char *str = NULL; |
| 647 | int ret = 0, waitret; |
| 648 | size_t len, reserve_len, pos; |
| 649 | va_list ap; |
| 650 | |
| 651 | WARN_ON_ONCE(!CMM_ACCESS_ONCE(session->active)); |
| 652 | |
| 653 | va_start(ap, fmt); |
| 654 | ret = vasprintf(&str, fmt, ap); |
| 655 | va_end(ap); |
| 656 | if (ret < 0) |
| 657 | return -ENOMEM; |
| 658 | |
| 659 | len = strlen(str); |
| 660 | pos = 0; |
| 661 | |
| 662 | for (pos = 0; pos < len; pos += reserve_len) { |
| 663 | reserve_len = min_t(size_t, |
| 664 | chan->ops->packet_avail_size(chan->chan, chan->handle), |
| 665 | len - pos); |
| 666 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, |
| 667 | sizeof(char), -1, chan->handle); |
| 668 | /* |
| 669 | * We don't care about metadata buffer's records lost |
| 670 | * count, because we always retry here. Report error if |
| 671 | * we need to bail out after timeout or being |
| 672 | * interrupted. |
| 673 | */ |
| 674 | waitret = wait_cond_interruptible_timeout( |
| 675 | ({ |
| 676 | ret = chan->ops->event_reserve(&ctx, 0); |
| 677 | ret != -ENOBUFS || !ret; |
| 678 | }), |
| 679 | LTTNG_METADATA_TIMEOUT_MSEC); |
| 680 | if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) { |
| 681 | DBG("LTTng: Failure to write metadata to buffers (%s)\n", |
| 682 | waitret == -EINTR ? "interrupted" : |
| 683 | (ret == -ENOBUFS ? "timeout" : "I/O error")); |
| 684 | if (waitret == -EINTR) |
| 685 | ret = waitret; |
| 686 | goto end; |
| 687 | } |
| 688 | chan->ops->event_write(&ctx, &str[pos], reserve_len); |
| 689 | chan->ops->event_commit(&ctx); |
| 690 | } |
| 691 | end: |
| 692 | free(str); |
| 693 | return ret; |
| 694 | } |
| 695 | |
| 696 | static |
| 697 | int _ltt_field_statedump(struct ltt_session *session, |
| 698 | const struct lttng_event_field *field) |
| 699 | { |
| 700 | int ret = 0; |
| 701 | |
| 702 | switch (field->type.atype) { |
| 703 | case atype_integer: |
| 704 | ret = lttng_metadata_printf(session, |
| 705 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
| 706 | field->type.u.basic.integer.size, |
| 707 | field->type.u.basic.integer.alignment, |
| 708 | field->type.u.basic.integer.signedness, |
| 709 | (field->type.u.basic.integer.encoding == lttng_encode_none) |
| 710 | ? "none" |
| 711 | : (field->type.u.basic.integer.encoding == lttng_encode_UTF8) |
| 712 | ? "UTF8" |
| 713 | : "ASCII", |
| 714 | field->type.u.basic.integer.base, |
| 715 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 716 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 717 | #else |
| 718 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 719 | #endif |
| 720 | field->name); |
| 721 | break; |
| 722 | case atype_float: |
| 723 | ret = lttng_metadata_printf(session, |
| 724 | " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n", |
| 725 | field->type.u.basic._float.exp_dig, |
| 726 | field->type.u.basic._float.mant_dig, |
| 727 | field->type.u.basic._float.alignment, |
| 728 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 729 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 730 | #else |
| 731 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 732 | #endif |
| 733 | field->name); |
| 734 | break; |
| 735 | case atype_enum: |
| 736 | ret = lttng_metadata_printf(session, |
| 737 | " %s %s;\n", |
| 738 | field->type.u.basic.enumeration.name, |
| 739 | field->name); |
| 740 | break; |
| 741 | case atype_array: |
| 742 | { |
| 743 | const struct lttng_basic_type *elem_type; |
| 744 | |
| 745 | elem_type = &field->type.u.array.elem_type; |
| 746 | ret = lttng_metadata_printf(session, |
| 747 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
| 748 | elem_type->u.basic.integer.size, |
| 749 | elem_type->u.basic.integer.alignment, |
| 750 | elem_type->u.basic.integer.signedness, |
| 751 | (elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 752 | ? "none" |
| 753 | : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 754 | ? "UTF8" |
| 755 | : "ASCII", |
| 756 | elem_type->u.basic.integer.base, |
| 757 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 758 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 759 | #else |
| 760 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 761 | #endif |
| 762 | field->name, field->type.u.array.length); |
| 763 | break; |
| 764 | } |
| 765 | case atype_sequence: |
| 766 | { |
| 767 | const struct lttng_basic_type *elem_type; |
| 768 | const struct lttng_basic_type *length_type; |
| 769 | |
| 770 | elem_type = &field->type.u.sequence.elem_type; |
| 771 | length_type = &field->type.u.sequence.length_type; |
| 772 | ret = lttng_metadata_printf(session, |
| 773 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", |
| 774 | length_type->u.basic.integer.size, |
| 775 | (unsigned int) length_type->u.basic.integer.alignment, |
| 776 | length_type->u.basic.integer.signedness, |
| 777 | (length_type->u.basic.integer.encoding == lttng_encode_none) |
| 778 | ? "none" |
| 779 | : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 780 | ? "UTF8" |
| 781 | : "ASCII"), |
| 782 | length_type->u.basic.integer.base, |
| 783 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 784 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 785 | #else |
| 786 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 787 | #endif |
| 788 | field->name); |
| 789 | if (ret) |
| 790 | return ret; |
| 791 | |
| 792 | ret = lttng_metadata_printf(session, |
| 793 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
| 794 | elem_type->u.basic.integer.size, |
| 795 | (unsigned int) elem_type->u.basic.integer.alignment, |
| 796 | elem_type->u.basic.integer.signedness, |
| 797 | (elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 798 | ? "none" |
| 799 | : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 800 | ? "UTF8" |
| 801 | : "ASCII"), |
| 802 | elem_type->u.basic.integer.base, |
| 803 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 804 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 805 | #else |
| 806 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 807 | #endif |
| 808 | field->name, |
| 809 | field->name); |
| 810 | break; |
| 811 | } |
| 812 | |
| 813 | case atype_string: |
| 814 | /* Default encoding is UTF8 */ |
| 815 | ret = lttng_metadata_printf(session, |
| 816 | " string%s _%s;\n", |
| 817 | field->type.u.basic.string.encoding == lttng_encode_ASCII ? |
| 818 | " { encoding = ASCII; }" : "", |
| 819 | field->name); |
| 820 | break; |
| 821 | default: |
| 822 | WARN_ON_ONCE(1); |
| 823 | return -EINVAL; |
| 824 | } |
| 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | static |
| 829 | int _ltt_context_metadata_statedump(struct ltt_session *session, |
| 830 | struct lttng_ctx *ctx) |
| 831 | { |
| 832 | int ret = 0; |
| 833 | int i; |
| 834 | |
| 835 | if (!ctx) |
| 836 | return 0; |
| 837 | for (i = 0; i < ctx->nr_fields; i++) { |
| 838 | const struct lttng_ctx_field *field = &ctx->fields[i]; |
| 839 | |
| 840 | ret = _ltt_field_statedump(session, &field->event_field); |
| 841 | if (ret) |
| 842 | return ret; |
| 843 | } |
| 844 | return ret; |
| 845 | } |
| 846 | |
| 847 | static |
| 848 | int _ltt_fields_metadata_statedump(struct ltt_session *session, |
| 849 | struct ltt_event *event) |
| 850 | { |
| 851 | const struct lttng_event_desc *desc = event->desc; |
| 852 | int ret = 0; |
| 853 | int i; |
| 854 | |
| 855 | for (i = 0; i < desc->nr_fields; i++) { |
| 856 | const struct lttng_event_field *field = &desc->fields[i]; |
| 857 | |
| 858 | ret = _ltt_field_statedump(session, field); |
| 859 | if (ret) |
| 860 | return ret; |
| 861 | } |
| 862 | return ret; |
| 863 | } |
| 864 | |
| 865 | static |
| 866 | int _ltt_event_metadata_statedump(struct ltt_session *session, |
| 867 | struct ltt_channel *chan, |
| 868 | struct ltt_event *event) |
| 869 | { |
| 870 | int ret = 0; |
| 871 | |
| 872 | if (event->metadata_dumped || !CMM_ACCESS_ONCE(session->active)) |
| 873 | return 0; |
| 874 | if (chan == session->metadata) |
| 875 | return 0; |
| 876 | /* |
| 877 | * Don't print events for which probe load is pending. |
| 878 | */ |
| 879 | if (!event->desc) |
| 880 | return 0; |
| 881 | |
| 882 | ret = lttng_metadata_printf(session, |
| 883 | "event {\n" |
| 884 | " name = \"%s\";\n" |
| 885 | " id = %u;\n" |
| 886 | " stream_id = %u;\n", |
| 887 | event->desc->name, |
| 888 | event->id, |
| 889 | event->chan->id); |
| 890 | if (ret) |
| 891 | goto end; |
| 892 | |
| 893 | if (event->desc->loglevel) { |
| 894 | const struct tracepoint_loglevel_entry *ll_entry; |
| 895 | |
| 896 | ll_entry = *event->desc->loglevel; |
| 897 | ret = lttng_metadata_printf(session, |
| 898 | " loglevel.identifier = \"%s\";\n" |
| 899 | " loglevel.value = %lld;\n", |
| 900 | ll_entry->identifier, |
| 901 | (long long) ll_entry->value); |
| 902 | if (ret) |
| 903 | goto end; |
| 904 | } |
| 905 | |
| 906 | if (event->ctx) { |
| 907 | ret = lttng_metadata_printf(session, |
| 908 | " context := struct {\n"); |
| 909 | if (ret) |
| 910 | goto end; |
| 911 | } |
| 912 | ret = _ltt_context_metadata_statedump(session, event->ctx); |
| 913 | if (ret) |
| 914 | goto end; |
| 915 | if (event->ctx) { |
| 916 | ret = lttng_metadata_printf(session, |
| 917 | " };\n"); |
| 918 | if (ret) |
| 919 | goto end; |
| 920 | } |
| 921 | |
| 922 | ret = lttng_metadata_printf(session, |
| 923 | " fields := struct {\n" |
| 924 | ); |
| 925 | if (ret) |
| 926 | goto end; |
| 927 | |
| 928 | ret = _ltt_fields_metadata_statedump(session, event); |
| 929 | if (ret) |
| 930 | goto end; |
| 931 | |
| 932 | /* |
| 933 | * LTTng space reservation can only reserve multiples of the |
| 934 | * byte size. |
| 935 | */ |
| 936 | ret = lttng_metadata_printf(session, |
| 937 | " };\n" |
| 938 | "};\n\n"); |
| 939 | if (ret) |
| 940 | goto end; |
| 941 | |
| 942 | event->metadata_dumped = 1; |
| 943 | end: |
| 944 | return ret; |
| 945 | |
| 946 | } |
| 947 | |
| 948 | static |
| 949 | int _ltt_channel_metadata_statedump(struct ltt_session *session, |
| 950 | struct ltt_channel *chan) |
| 951 | { |
| 952 | int ret = 0; |
| 953 | |
| 954 | if (chan->metadata_dumped || !CMM_ACCESS_ONCE(session->active)) |
| 955 | return 0; |
| 956 | if (chan == session->metadata) |
| 957 | return 0; |
| 958 | |
| 959 | WARN_ON_ONCE(!chan->header_type); |
| 960 | ret = lttng_metadata_printf(session, |
| 961 | "stream {\n" |
| 962 | " id = %u;\n" |
| 963 | " event.header := %s;\n" |
| 964 | " packet.context := struct packet_context;\n", |
| 965 | chan->id, |
| 966 | chan->header_type == 1 ? "struct event_header_compact" : |
| 967 | "struct event_header_large"); |
| 968 | if (ret) |
| 969 | goto end; |
| 970 | |
| 971 | if (chan->ctx) { |
| 972 | ret = lttng_metadata_printf(session, |
| 973 | " event.context := struct {\n"); |
| 974 | if (ret) |
| 975 | goto end; |
| 976 | } |
| 977 | ret = _ltt_context_metadata_statedump(session, chan->ctx); |
| 978 | if (ret) |
| 979 | goto end; |
| 980 | if (chan->ctx) { |
| 981 | ret = lttng_metadata_printf(session, |
| 982 | " };\n"); |
| 983 | if (ret) |
| 984 | goto end; |
| 985 | } |
| 986 | |
| 987 | ret = lttng_metadata_printf(session, |
| 988 | "};\n\n"); |
| 989 | |
| 990 | chan->metadata_dumped = 1; |
| 991 | end: |
| 992 | return ret; |
| 993 | } |
| 994 | |
| 995 | static |
| 996 | int _ltt_stream_packet_context_declare(struct ltt_session *session) |
| 997 | { |
| 998 | return lttng_metadata_printf(session, |
| 999 | "struct packet_context {\n" |
| 1000 | " uint64_t timestamp_begin;\n" |
| 1001 | " uint64_t timestamp_end;\n" |
| 1002 | " uint32_t events_discarded;\n" |
| 1003 | " uint32_t content_size;\n" |
| 1004 | " uint32_t packet_size;\n" |
| 1005 | " uint32_t cpu_id;\n" |
| 1006 | "};\n\n" |
| 1007 | ); |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Compact header: |
| 1012 | * id: range: 0 - 30. |
| 1013 | * id 31 is reserved to indicate an extended header. |
| 1014 | * |
| 1015 | * Large header: |
| 1016 | * id: range: 0 - 65534. |
| 1017 | * id 65535 is reserved to indicate an extended header. |
| 1018 | */ |
| 1019 | static |
| 1020 | int _ltt_event_header_declare(struct ltt_session *session) |
| 1021 | { |
| 1022 | return lttng_metadata_printf(session, |
| 1023 | "struct event_header_compact {\n" |
| 1024 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" |
| 1025 | " variant <id> {\n" |
| 1026 | " struct {\n" |
| 1027 | " uint27_t timestamp;\n" |
| 1028 | " } compact;\n" |
| 1029 | " struct {\n" |
| 1030 | " uint32_t id;\n" |
| 1031 | " uint64_t timestamp;\n" |
| 1032 | " } extended;\n" |
| 1033 | " } v;\n" |
| 1034 | "} align(%u);\n" |
| 1035 | "\n" |
| 1036 | "struct event_header_large {\n" |
| 1037 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" |
| 1038 | " variant <id> {\n" |
| 1039 | " struct {\n" |
| 1040 | " uint32_t timestamp;\n" |
| 1041 | " } compact;\n" |
| 1042 | " struct {\n" |
| 1043 | " uint32_t id;\n" |
| 1044 | " uint64_t timestamp;\n" |
| 1045 | " } extended;\n" |
| 1046 | " } v;\n" |
| 1047 | "} align(%u);\n\n", |
| 1048 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 1049 | lttng_alignof(uint16_t) * CHAR_BIT |
| 1050 | ); |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * Output metadata into this session's metadata buffers. |
| 1055 | */ |
| 1056 | static |
| 1057 | int _ltt_session_metadata_statedump(struct ltt_session *session) |
| 1058 | { |
| 1059 | unsigned char *uuid_c = session->uuid; |
| 1060 | char uuid_s[37]; |
| 1061 | struct ltt_channel *chan; |
| 1062 | struct ltt_event *event; |
| 1063 | int ret = 0; |
| 1064 | |
| 1065 | if (!CMM_ACCESS_ONCE(session->active)) |
| 1066 | return 0; |
| 1067 | if (session->metadata_dumped) |
| 1068 | goto skip_session; |
| 1069 | if (!session->metadata) { |
| 1070 | DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n"); |
| 1071 | return -EPERM; |
| 1072 | } |
| 1073 | |
| 1074 | snprintf(uuid_s, sizeof(uuid_s), |
| 1075 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 1076 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], |
| 1077 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], |
| 1078 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], |
| 1079 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); |
| 1080 | |
| 1081 | ret = lttng_metadata_printf(session, |
| 1082 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
| 1083 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" |
| 1084 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" |
| 1085 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" |
| 1086 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" |
| 1087 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" |
| 1088 | "\n" |
| 1089 | "trace {\n" |
| 1090 | " major = %u;\n" |
| 1091 | " minor = %u;\n" |
| 1092 | " uuid = \"%s\";\n" |
| 1093 | " byte_order = %s;\n" |
| 1094 | " packet.header := struct {\n" |
| 1095 | " uint32_t magic;\n" |
| 1096 | " uint8_t uuid[16];\n" |
| 1097 | " uint32_t stream_id;\n" |
| 1098 | " };\n" |
| 1099 | "};\n\n", |
| 1100 | lttng_alignof(uint8_t) * CHAR_BIT, |
| 1101 | lttng_alignof(uint16_t) * CHAR_BIT, |
| 1102 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 1103 | lttng_alignof(uint64_t) * CHAR_BIT, |
| 1104 | CTF_VERSION_MAJOR, |
| 1105 | CTF_VERSION_MINOR, |
| 1106 | uuid_s, |
| 1107 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 1108 | "be" |
| 1109 | #else |
| 1110 | "le" |
| 1111 | #endif |
| 1112 | ); |
| 1113 | if (ret) |
| 1114 | goto end; |
| 1115 | |
| 1116 | ret = _ltt_stream_packet_context_declare(session); |
| 1117 | if (ret) |
| 1118 | goto end; |
| 1119 | |
| 1120 | ret = _ltt_event_header_declare(session); |
| 1121 | if (ret) |
| 1122 | goto end; |
| 1123 | |
| 1124 | skip_session: |
| 1125 | cds_list_for_each_entry(chan, &session->chan, list) { |
| 1126 | ret = _ltt_channel_metadata_statedump(session, chan); |
| 1127 | if (ret) |
| 1128 | goto end; |
| 1129 | } |
| 1130 | |
| 1131 | cds_list_for_each_entry(event, &session->events, list) { |
| 1132 | ret = _ltt_event_metadata_statedump(session, event->chan, event); |
| 1133 | if (ret) |
| 1134 | goto end; |
| 1135 | } |
| 1136 | session->metadata_dumped = 1; |
| 1137 | end: |
| 1138 | return ret; |
| 1139 | } |
| 1140 | |
| 1141 | void lttng_ust_events_exit(void) |
| 1142 | { |
| 1143 | struct ltt_session *session, *tmpsession; |
| 1144 | |
| 1145 | cds_list_for_each_entry_safe(session, tmpsession, &sessions, list) |
| 1146 | ltt_session_destroy(session); |
| 1147 | } |