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