| 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 | #include <inttypes.h> |
| 24 | #include <time.h> |
| 25 | #include "clock.h" |
| 26 | |
| 27 | #include <urcu-bp.h> |
| 28 | #include <urcu/compiler.h> |
| 29 | #include <urcu/uatomic.h> |
| 30 | #include <urcu/arch.h> |
| 31 | |
| 32 | #include <lttng/tracepoint.h> |
| 33 | #include <lttng/ust-events.h> |
| 34 | |
| 35 | #include <usterr-signal-safe.h> |
| 36 | #include <helper.h> |
| 37 | #include "error.h" |
| 38 | |
| 39 | #include "tracepoint-internal.h" |
| 40 | #include "ltt-tracer.h" |
| 41 | #include "ltt-tracer-core.h" |
| 42 | #include "wait.h" |
| 43 | #include "../libringbuffer/shm.h" |
| 44 | #include "jhash.h" |
| 45 | |
| 46 | /* |
| 47 | * The sessions mutex is the centralized mutex across UST tracing |
| 48 | * control and probe registration. All operations within this file are |
| 49 | * called by the communication thread, under ust_lock protection. |
| 50 | */ |
| 51 | static pthread_mutex_t sessions_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 52 | |
| 53 | void ust_lock(void) |
| 54 | { |
| 55 | pthread_mutex_lock(&sessions_mutex); |
| 56 | } |
| 57 | |
| 58 | void ust_unlock(void) |
| 59 | { |
| 60 | pthread_mutex_unlock(&sessions_mutex); |
| 61 | } |
| 62 | |
| 63 | static CDS_LIST_HEAD(sessions); |
| 64 | |
| 65 | /* |
| 66 | * Wildcard list, containing the active wildcards. |
| 67 | * Protected by ust lock. |
| 68 | */ |
| 69 | static CDS_LIST_HEAD(wildcard_list); |
| 70 | |
| 71 | /* |
| 72 | * Pending probes hash table, containing the registered ltt events for |
| 73 | * which tracepoint probes are still missing. Protected by the sessions |
| 74 | * mutex. |
| 75 | */ |
| 76 | #define PENDING_PROBE_HASH_BITS 6 |
| 77 | #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS) |
| 78 | static struct cds_hlist_head pending_probe_table[PENDING_PROBE_HASH_SIZE]; |
| 79 | |
| 80 | struct ust_pending_probe { |
| 81 | struct ltt_event *event; |
| 82 | struct cds_hlist_node node; |
| 83 | enum lttng_ust_loglevel_type loglevel_type; |
| 84 | int loglevel; |
| 85 | char name[]; |
| 86 | }; |
| 87 | |
| 88 | static void _ltt_event_destroy(struct ltt_event *event); |
| 89 | static void _ltt_wildcard_destroy(struct session_wildcard *sw); |
| 90 | static void _ltt_channel_destroy(struct ltt_channel *chan); |
| 91 | static int _ltt_event_unregister(struct ltt_event *event); |
| 92 | static |
| 93 | int _ltt_event_metadata_statedump(struct ltt_session *session, |
| 94 | struct ltt_channel *chan, |
| 95 | struct ltt_event *event); |
| 96 | static |
| 97 | int _ltt_session_metadata_statedump(struct ltt_session *session); |
| 98 | |
| 99 | int ltt_loglevel_match(const struct lttng_event_desc *desc, |
| 100 | enum lttng_ust_loglevel_type req_type, |
| 101 | int req_loglevel) |
| 102 | { |
| 103 | int ev_loglevel; |
| 104 | |
| 105 | if (req_type == LTTNG_UST_LOGLEVEL_ALL) |
| 106 | return 1; |
| 107 | if (!desc->loglevel) |
| 108 | ev_loglevel = TRACE_DEFAULT; |
| 109 | else |
| 110 | ev_loglevel = *(*desc->loglevel); |
| 111 | switch (req_type) { |
| 112 | case LTTNG_UST_LOGLEVEL_RANGE: |
| 113 | if (ev_loglevel <= req_loglevel || req_loglevel == -1) |
| 114 | return 1; |
| 115 | else |
| 116 | return 0; |
| 117 | case LTTNG_UST_LOGLEVEL_SINGLE: |
| 118 | if (ev_loglevel == req_loglevel || req_loglevel == -1) |
| 119 | return 1; |
| 120 | else |
| 121 | return 0; |
| 122 | case LTTNG_UST_LOGLEVEL_ALL: |
| 123 | default: |
| 124 | return 1; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Return wildcard for a given event name if the event name match the |
| 130 | * one of the wildcards. |
| 131 | * Must be called with ust lock held. |
| 132 | * Returns NULL if not present. |
| 133 | */ |
| 134 | static |
| 135 | struct wildcard_entry *match_wildcard(const struct lttng_event_desc *desc) |
| 136 | { |
| 137 | struct wildcard_entry *e; |
| 138 | |
| 139 | cds_list_for_each_entry(e, &wildcard_list, list) { |
| 140 | /* If only contain '*' */ |
| 141 | if (strlen(e->name) == 1) |
| 142 | goto possible_match; |
| 143 | /* Compare excluding final '*' */ |
| 144 | if (!strncmp(desc->name, e->name, strlen(e->name) - 1)) |
| 145 | goto possible_match; |
| 146 | continue; /* goto next, no match */ |
| 147 | possible_match: |
| 148 | if (ltt_loglevel_match(desc, |
| 149 | e->loglevel_type, |
| 150 | e->loglevel)) { |
| 151 | return e; |
| 152 | } |
| 153 | /* no match, loop to next */ |
| 154 | } |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * called at event creation if probe is missing. |
| 160 | * called with session mutex held. |
| 161 | */ |
| 162 | static |
| 163 | int add_pending_probe(struct ltt_event *event, const char *name, |
| 164 | enum lttng_ust_loglevel_type loglevel_type, |
| 165 | int loglevel) |
| 166 | { |
| 167 | struct cds_hlist_head *head; |
| 168 | struct ust_pending_probe *e; |
| 169 | size_t name_len = strlen(name); |
| 170 | uint32_t hash; |
| 171 | |
| 172 | if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) { |
| 173 | WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1); |
| 174 | name_len = LTTNG_UST_SYM_NAME_LEN - 1; |
| 175 | } |
| 176 | hash = jhash(name, name_len, 0); |
| 177 | head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)]; |
| 178 | e = zmalloc(sizeof(struct ust_pending_probe) + name_len); |
| 179 | if (!e) |
| 180 | return -ENOMEM; |
| 181 | memcpy(&e->name[0], name, name_len + 1); |
| 182 | e->name[name_len] = '\0'; |
| 183 | e->loglevel_type = loglevel_type; |
| 184 | e->loglevel = loglevel; |
| 185 | cds_hlist_add_head(&e->node, head); |
| 186 | e->event = event; |
| 187 | event->pending_probe = e; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * remove a pending probe. called when at event teardown and when an |
| 193 | * event is fixed (probe is loaded). |
| 194 | * called with session mutex held. |
| 195 | */ |
| 196 | static |
| 197 | void remove_pending_probe(struct ust_pending_probe *e) |
| 198 | { |
| 199 | if (!e) |
| 200 | return; |
| 201 | cds_hlist_del(&e->node); |
| 202 | free(e); |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Called at library load: connect the probe on the events pending on |
| 207 | * probe load. |
| 208 | * called with session mutex held. |
| 209 | */ |
| 210 | int pending_probe_fix_events(const struct lttng_event_desc *desc) |
| 211 | { |
| 212 | struct cds_hlist_head *head; |
| 213 | struct cds_hlist_node *node, *p; |
| 214 | struct ust_pending_probe *e; |
| 215 | const char *name = desc->name; |
| 216 | int ret = 0; |
| 217 | struct lttng_ust_event event_param; |
| 218 | size_t name_len = strlen(name); |
| 219 | uint32_t hash; |
| 220 | |
| 221 | /* Wildcard */ |
| 222 | { |
| 223 | struct wildcard_entry *wildcard; |
| 224 | |
| 225 | wildcard = match_wildcard(desc); |
| 226 | if (strcmp(desc->name, "lttng_ust:metadata") && wildcard) { |
| 227 | struct session_wildcard *sw; |
| 228 | |
| 229 | cds_list_for_each_entry(sw, &wildcard->session_list, |
| 230 | session_list) { |
| 231 | struct ltt_event *ev; |
| 232 | int ret; |
| 233 | |
| 234 | memcpy(&event_param, &sw->event_param, |
| 235 | sizeof(event_param)); |
| 236 | memcpy(event_param.name, |
| 237 | desc->name, |
| 238 | sizeof(event_param.name)); |
| 239 | /* create event */ |
| 240 | ret = ltt_event_create(sw->chan, |
| 241 | &event_param, NULL, |
| 242 | &ev); |
| 243 | if (ret) { |
| 244 | DBG("Error creating event"); |
| 245 | continue; |
| 246 | } |
| 247 | cds_list_add(&ev->wildcard_list, |
| 248 | &sw->events); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) { |
| 254 | WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1); |
| 255 | name_len = LTTNG_UST_SYM_NAME_LEN - 1; |
| 256 | } |
| 257 | hash = jhash(name, name_len, 0); |
| 258 | head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)]; |
| 259 | cds_hlist_for_each_entry_safe(e, node, p, head, node) { |
| 260 | struct ltt_event *event; |
| 261 | struct ltt_channel *chan; |
| 262 | |
| 263 | event = e->event; |
| 264 | if (!ltt_loglevel_match(event->desc, |
| 265 | e->loglevel_type, |
| 266 | e->loglevel)) { |
| 267 | continue; |
| 268 | } |
| 269 | if (strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) { |
| 270 | continue; |
| 271 | } |
| 272 | chan = event->chan; |
| 273 | assert(!event->desc); |
| 274 | event->desc = desc; |
| 275 | event->pending_probe = NULL; |
| 276 | remove_pending_probe(e); |
| 277 | ret |= __tracepoint_probe_register(name, |
| 278 | event->desc->probe_callback, |
| 279 | event); |
| 280 | if (ret) |
| 281 | continue; |
| 282 | event->id = chan->free_event_id++; |
| 283 | ret |= _ltt_event_metadata_statedump(chan->session, chan, |
| 284 | event); |
| 285 | } |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | void synchronize_trace(void) |
| 290 | { |
| 291 | synchronize_rcu(); |
| 292 | } |
| 293 | |
| 294 | struct ltt_session *ltt_session_create(void) |
| 295 | { |
| 296 | struct ltt_session *session; |
| 297 | |
| 298 | session = zmalloc(sizeof(struct ltt_session)); |
| 299 | if (!session) |
| 300 | return NULL; |
| 301 | CDS_INIT_LIST_HEAD(&session->chan); |
| 302 | CDS_INIT_LIST_HEAD(&session->events); |
| 303 | CDS_INIT_LIST_HEAD(&session->wildcards); |
| 304 | uuid_generate(session->uuid); |
| 305 | cds_list_add(&session->list, &sessions); |
| 306 | return session; |
| 307 | } |
| 308 | |
| 309 | void ltt_session_destroy(struct ltt_session *session) |
| 310 | { |
| 311 | struct ltt_channel *chan, *tmpchan; |
| 312 | struct ltt_event *event, *tmpevent; |
| 313 | struct session_wildcard *wildcard, *tmpwildcard; |
| 314 | int ret; |
| 315 | |
| 316 | CMM_ACCESS_ONCE(session->active) = 0; |
| 317 | cds_list_for_each_entry(event, &session->events, list) { |
| 318 | ret = _ltt_event_unregister(event); |
| 319 | WARN_ON(ret); |
| 320 | } |
| 321 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 322 | cds_list_for_each_entry_safe(wildcard, tmpwildcard, &session->wildcards, list) |
| 323 | _ltt_wildcard_destroy(wildcard); |
| 324 | cds_list_for_each_entry_safe(event, tmpevent, &session->events, list) |
| 325 | _ltt_event_destroy(event); |
| 326 | cds_list_for_each_entry_safe(chan, tmpchan, &session->chan, list) |
| 327 | _ltt_channel_destroy(chan); |
| 328 | cds_list_del(&session->list); |
| 329 | free(session); |
| 330 | } |
| 331 | |
| 332 | int ltt_session_enable(struct ltt_session *session) |
| 333 | { |
| 334 | int ret = 0; |
| 335 | struct ltt_channel *chan; |
| 336 | |
| 337 | if (session->active) { |
| 338 | ret = -EBUSY; |
| 339 | goto end; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Snapshot the number of events per channel to know the type of header |
| 344 | * we need to use. |
| 345 | */ |
| 346 | cds_list_for_each_entry(chan, &session->chan, list) { |
| 347 | if (chan->header_type) |
| 348 | continue; /* don't change it if session stop/restart */ |
| 349 | if (chan->free_event_id < 31) |
| 350 | chan->header_type = 1; /* compact */ |
| 351 | else |
| 352 | chan->header_type = 2; /* large */ |
| 353 | } |
| 354 | |
| 355 | CMM_ACCESS_ONCE(session->active) = 1; |
| 356 | CMM_ACCESS_ONCE(session->been_active) = 1; |
| 357 | ret = _ltt_session_metadata_statedump(session); |
| 358 | if (ret) |
| 359 | CMM_ACCESS_ONCE(session->active) = 0; |
| 360 | end: |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | int ltt_session_disable(struct ltt_session *session) |
| 365 | { |
| 366 | int ret = 0; |
| 367 | |
| 368 | if (!session->active) { |
| 369 | ret = -EBUSY; |
| 370 | goto end; |
| 371 | } |
| 372 | CMM_ACCESS_ONCE(session->active) = 0; |
| 373 | end: |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | int ltt_channel_enable(struct ltt_channel *channel) |
| 378 | { |
| 379 | int old; |
| 380 | |
| 381 | if (channel == channel->session->metadata) |
| 382 | return -EPERM; |
| 383 | old = uatomic_xchg(&channel->enabled, 1); |
| 384 | if (old) |
| 385 | return -EEXIST; |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | int ltt_channel_disable(struct ltt_channel *channel) |
| 390 | { |
| 391 | int old; |
| 392 | |
| 393 | if (channel == channel->session->metadata) |
| 394 | return -EPERM; |
| 395 | old = uatomic_xchg(&channel->enabled, 0); |
| 396 | if (!old) |
| 397 | return -EEXIST; |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | int ltt_event_enable(struct ltt_event *event) |
| 402 | { |
| 403 | int old; |
| 404 | |
| 405 | if (event->chan == event->chan->session->metadata) |
| 406 | return -EPERM; |
| 407 | old = uatomic_xchg(&event->enabled, 1); |
| 408 | if (old) |
| 409 | return -EEXIST; |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | int ltt_event_disable(struct ltt_event *event) |
| 414 | { |
| 415 | int old; |
| 416 | |
| 417 | if (event->chan == event->chan->session->metadata) |
| 418 | return -EPERM; |
| 419 | old = uatomic_xchg(&event->enabled, 0); |
| 420 | if (!old) |
| 421 | return -EEXIST; |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | struct ltt_channel *ltt_channel_create(struct ltt_session *session, |
| 426 | const char *transport_name, |
| 427 | void *buf_addr, |
| 428 | size_t subbuf_size, size_t num_subbuf, |
| 429 | unsigned int switch_timer_interval, |
| 430 | unsigned int read_timer_interval, |
| 431 | int **shm_fd, int **wait_fd, |
| 432 | uint64_t **memory_map_size, |
| 433 | struct ltt_channel *chan_priv_init) |
| 434 | { |
| 435 | struct ltt_channel *chan = NULL; |
| 436 | struct ltt_transport *transport; |
| 437 | |
| 438 | if (session->been_active) |
| 439 | goto active; /* Refuse to add channel to active session */ |
| 440 | transport = ltt_transport_find(transport_name); |
| 441 | if (!transport) { |
| 442 | DBG("LTTng transport %s not found\n", |
| 443 | transport_name); |
| 444 | goto notransport; |
| 445 | } |
| 446 | chan_priv_init->id = session->free_chan_id++; |
| 447 | chan_priv_init->session = session; |
| 448 | /* |
| 449 | * Note: the channel creation op already writes into the packet |
| 450 | * headers. Therefore the "chan" information used as input |
| 451 | * should be already accessible. |
| 452 | */ |
| 453 | chan = transport->ops.channel_create("[lttng]", buf_addr, |
| 454 | subbuf_size, num_subbuf, switch_timer_interval, |
| 455 | read_timer_interval, shm_fd, wait_fd, |
| 456 | memory_map_size, chan_priv_init); |
| 457 | if (!chan) |
| 458 | goto create_error; |
| 459 | chan->enabled = 1; |
| 460 | chan->ops = &transport->ops; |
| 461 | cds_list_add(&chan->list, &session->chan); |
| 462 | return chan; |
| 463 | |
| 464 | create_error: |
| 465 | notransport: |
| 466 | active: |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Only used internally at session destruction. |
| 472 | */ |
| 473 | static |
| 474 | void _ltt_channel_destroy(struct ltt_channel *chan) |
| 475 | { |
| 476 | cds_list_del(&chan->list); |
| 477 | lttng_destroy_context(chan->ctx); |
| 478 | chan->ops->channel_destroy(chan); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Supports event creation while tracing session is active. |
| 483 | */ |
| 484 | int ltt_event_create(struct ltt_channel *chan, |
| 485 | struct lttng_ust_event *event_param, |
| 486 | void *filter, |
| 487 | struct ltt_event **_event) |
| 488 | { |
| 489 | const struct lttng_event_desc *desc = NULL; /* silence gcc */ |
| 490 | struct ltt_event *event; |
| 491 | int ret = 0; |
| 492 | |
| 493 | if (chan->used_event_id == -1UL) { |
| 494 | ret = -ENOMEM; |
| 495 | goto full; |
| 496 | } |
| 497 | /* |
| 498 | * This is O(n^2) (for each event, the loop is called at event |
| 499 | * creation). Might require a hash if we have lots of events. |
| 500 | */ |
| 501 | cds_list_for_each_entry(event, &chan->session->events, list) { |
| 502 | if (event->desc && !strncmp(event->desc->name, |
| 503 | event_param->name, |
| 504 | LTTNG_UST_SYM_NAME_LEN - 1)) { |
| 505 | ret = -EEXIST; |
| 506 | goto exist; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Check if loglevel match. Refuse to connect event if not. |
| 512 | */ |
| 513 | if (event_param->instrumentation == LTTNG_UST_TRACEPOINT) { |
| 514 | desc = ltt_event_get(event_param->name); |
| 515 | if (!ltt_loglevel_match(desc, |
| 516 | event_param->loglevel_type, |
| 517 | event_param->loglevel)) { |
| 518 | ret = -EPERM; |
| 519 | goto no_loglevel_match; |
| 520 | } |
| 521 | } |
| 522 | event = zmalloc(sizeof(struct ltt_event)); |
| 523 | if (!event) { |
| 524 | ret = -ENOMEM; |
| 525 | goto cache_error; |
| 526 | } |
| 527 | event->chan = chan; |
| 528 | event->filter = filter; |
| 529 | /* |
| 530 | * used_event_id counts the maximum number of event IDs that can |
| 531 | * register if all probes register. |
| 532 | */ |
| 533 | chan->used_event_id++; |
| 534 | event->enabled = 1; |
| 535 | event->instrumentation = event_param->instrumentation; |
| 536 | /* Populate ltt_event structure before tracepoint registration. */ |
| 537 | cmm_smp_wmb(); |
| 538 | switch (event_param->instrumentation) { |
| 539 | case LTTNG_UST_TRACEPOINT: |
| 540 | event->desc = desc; |
| 541 | if (event->desc) { |
| 542 | ret = __tracepoint_probe_register(event_param->name, |
| 543 | event->desc->probe_callback, |
| 544 | event); |
| 545 | if (ret) |
| 546 | goto register_error; |
| 547 | event->id = chan->free_event_id++; |
| 548 | } else { |
| 549 | /* |
| 550 | * If the probe is not present, event->desc stays NULL, |
| 551 | * waiting for the probe to register, and the event->id |
| 552 | * stays unallocated. |
| 553 | */ |
| 554 | ret = add_pending_probe(event, event_param->name, |
| 555 | event_param->loglevel_type, |
| 556 | event_param->loglevel); |
| 557 | if (ret) |
| 558 | goto add_pending_error; |
| 559 | } |
| 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 | no_loglevel_match: |
| 585 | exist: |
| 586 | full: |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Only used internally at session destruction. |
| 592 | */ |
| 593 | int _ltt_event_unregister(struct ltt_event *event) |
| 594 | { |
| 595 | int ret = -EINVAL; |
| 596 | |
| 597 | switch (event->instrumentation) { |
| 598 | case LTTNG_UST_TRACEPOINT: |
| 599 | if (event->desc) { |
| 600 | ret = __tracepoint_probe_unregister(event->desc->name, |
| 601 | event->desc->probe_callback, |
| 602 | event); |
| 603 | if (ret) |
| 604 | return ret; |
| 605 | } else { |
| 606 | remove_pending_probe(event->pending_probe); |
| 607 | ret = 0; |
| 608 | } |
| 609 | break; |
| 610 | default: |
| 611 | WARN_ON_ONCE(1); |
| 612 | } |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Only used internally at session destruction. |
| 618 | */ |
| 619 | static |
| 620 | void _ltt_event_destroy(struct ltt_event *event) |
| 621 | { |
| 622 | switch (event->instrumentation) { |
| 623 | case LTTNG_UST_TRACEPOINT: |
| 624 | if (event->desc) { |
| 625 | ltt_event_put(event->desc); |
| 626 | } |
| 627 | break; |
| 628 | default: |
| 629 | WARN_ON_ONCE(1); |
| 630 | } |
| 631 | cds_list_del(&event->list); |
| 632 | lttng_destroy_context(event->ctx); |
| 633 | free(event); |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * We have exclusive access to our metadata buffer (protected by the |
| 638 | * ust_lock), so we can do racy operations such as looking for |
| 639 | * remaining space left in packet and write, since mutual exclusion |
| 640 | * protects us from concurrent writes. |
| 641 | */ |
| 642 | int lttng_metadata_printf(struct ltt_session *session, |
| 643 | const char *fmt, ...) |
| 644 | { |
| 645 | struct lttng_ust_lib_ring_buffer_ctx ctx; |
| 646 | struct ltt_channel *chan = session->metadata; |
| 647 | char *str = NULL; |
| 648 | int ret = 0, waitret; |
| 649 | size_t len, reserve_len, pos; |
| 650 | va_list ap; |
| 651 | |
| 652 | WARN_ON_ONCE(!CMM_ACCESS_ONCE(session->active)); |
| 653 | |
| 654 | va_start(ap, fmt); |
| 655 | ret = vasprintf(&str, fmt, ap); |
| 656 | va_end(ap); |
| 657 | if (ret < 0) |
| 658 | return -ENOMEM; |
| 659 | |
| 660 | len = strlen(str); |
| 661 | pos = 0; |
| 662 | |
| 663 | for (pos = 0; pos < len; pos += reserve_len) { |
| 664 | reserve_len = min_t(size_t, |
| 665 | chan->ops->packet_avail_size(chan->chan, chan->handle), |
| 666 | len - pos); |
| 667 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, |
| 668 | sizeof(char), -1, chan->handle); |
| 669 | /* |
| 670 | * We don't care about metadata buffer's records lost |
| 671 | * count, because we always retry here. Report error if |
| 672 | * we need to bail out after timeout or being |
| 673 | * interrupted. |
| 674 | */ |
| 675 | waitret = wait_cond_interruptible_timeout( |
| 676 | ({ |
| 677 | ret = chan->ops->event_reserve(&ctx, 0); |
| 678 | ret != -ENOBUFS || !ret; |
| 679 | }), |
| 680 | LTTNG_METADATA_TIMEOUT_MSEC); |
| 681 | if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) { |
| 682 | DBG("LTTng: Failure to write metadata to buffers (%s)\n", |
| 683 | waitret == -EINTR ? "interrupted" : |
| 684 | (ret == -ENOBUFS ? "timeout" : "I/O error")); |
| 685 | if (waitret == -EINTR) |
| 686 | ret = waitret; |
| 687 | goto end; |
| 688 | } |
| 689 | chan->ops->event_write(&ctx, &str[pos], reserve_len); |
| 690 | chan->ops->event_commit(&ctx); |
| 691 | } |
| 692 | end: |
| 693 | free(str); |
| 694 | return ret; |
| 695 | } |
| 696 | |
| 697 | static |
| 698 | int _ltt_field_statedump(struct ltt_session *session, |
| 699 | const struct lttng_event_field *field) |
| 700 | { |
| 701 | int ret = 0; |
| 702 | |
| 703 | switch (field->type.atype) { |
| 704 | case atype_integer: |
| 705 | ret = lttng_metadata_printf(session, |
| 706 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
| 707 | field->type.u.basic.integer.size, |
| 708 | field->type.u.basic.integer.alignment, |
| 709 | field->type.u.basic.integer.signedness, |
| 710 | (field->type.u.basic.integer.encoding == lttng_encode_none) |
| 711 | ? "none" |
| 712 | : (field->type.u.basic.integer.encoding == lttng_encode_UTF8) |
| 713 | ? "UTF8" |
| 714 | : "ASCII", |
| 715 | field->type.u.basic.integer.base, |
| 716 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 717 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 718 | #else |
| 719 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 720 | #endif |
| 721 | field->name); |
| 722 | break; |
| 723 | case atype_float: |
| 724 | ret = lttng_metadata_printf(session, |
| 725 | " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n", |
| 726 | field->type.u.basic._float.exp_dig, |
| 727 | field->type.u.basic._float.mant_dig, |
| 728 | field->type.u.basic._float.alignment, |
| 729 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 730 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 731 | #else |
| 732 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 733 | #endif |
| 734 | field->name); |
| 735 | break; |
| 736 | case atype_enum: |
| 737 | ret = lttng_metadata_printf(session, |
| 738 | " %s %s;\n", |
| 739 | field->type.u.basic.enumeration.name, |
| 740 | field->name); |
| 741 | break; |
| 742 | case atype_array: |
| 743 | { |
| 744 | const struct lttng_basic_type *elem_type; |
| 745 | |
| 746 | elem_type = &field->type.u.array.elem_type; |
| 747 | ret = lttng_metadata_printf(session, |
| 748 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
| 749 | elem_type->u.basic.integer.size, |
| 750 | elem_type->u.basic.integer.alignment, |
| 751 | elem_type->u.basic.integer.signedness, |
| 752 | (elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 753 | ? "none" |
| 754 | : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 755 | ? "UTF8" |
| 756 | : "ASCII", |
| 757 | elem_type->u.basic.integer.base, |
| 758 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 759 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 760 | #else |
| 761 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 762 | #endif |
| 763 | field->name, field->type.u.array.length); |
| 764 | break; |
| 765 | } |
| 766 | case atype_sequence: |
| 767 | { |
| 768 | const struct lttng_basic_type *elem_type; |
| 769 | const struct lttng_basic_type *length_type; |
| 770 | |
| 771 | elem_type = &field->type.u.sequence.elem_type; |
| 772 | length_type = &field->type.u.sequence.length_type; |
| 773 | ret = lttng_metadata_printf(session, |
| 774 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", |
| 775 | length_type->u.basic.integer.size, |
| 776 | (unsigned int) length_type->u.basic.integer.alignment, |
| 777 | length_type->u.basic.integer.signedness, |
| 778 | (length_type->u.basic.integer.encoding == lttng_encode_none) |
| 779 | ? "none" |
| 780 | : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 781 | ? "UTF8" |
| 782 | : "ASCII"), |
| 783 | length_type->u.basic.integer.base, |
| 784 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 785 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 786 | #else |
| 787 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 788 | #endif |
| 789 | field->name); |
| 790 | if (ret) |
| 791 | return ret; |
| 792 | |
| 793 | ret = lttng_metadata_printf(session, |
| 794 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
| 795 | elem_type->u.basic.integer.size, |
| 796 | (unsigned int) elem_type->u.basic.integer.alignment, |
| 797 | elem_type->u.basic.integer.signedness, |
| 798 | (elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 799 | ? "none" |
| 800 | : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 801 | ? "UTF8" |
| 802 | : "ASCII"), |
| 803 | elem_type->u.basic.integer.base, |
| 804 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 805 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 806 | #else |
| 807 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 808 | #endif |
| 809 | field->name, |
| 810 | field->name); |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | case atype_string: |
| 815 | /* Default encoding is UTF8 */ |
| 816 | ret = lttng_metadata_printf(session, |
| 817 | " string%s _%s;\n", |
| 818 | field->type.u.basic.string.encoding == lttng_encode_ASCII ? |
| 819 | " { encoding = ASCII; }" : "", |
| 820 | field->name); |
| 821 | break; |
| 822 | default: |
| 823 | WARN_ON_ONCE(1); |
| 824 | return -EINVAL; |
| 825 | } |
| 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | static |
| 830 | int _ltt_context_metadata_statedump(struct ltt_session *session, |
| 831 | struct lttng_ctx *ctx) |
| 832 | { |
| 833 | int ret = 0; |
| 834 | int i; |
| 835 | |
| 836 | if (!ctx) |
| 837 | return 0; |
| 838 | for (i = 0; i < ctx->nr_fields; i++) { |
| 839 | const struct lttng_ctx_field *field = &ctx->fields[i]; |
| 840 | |
| 841 | ret = _ltt_field_statedump(session, &field->event_field); |
| 842 | if (ret) |
| 843 | return ret; |
| 844 | } |
| 845 | return ret; |
| 846 | } |
| 847 | |
| 848 | static |
| 849 | int _ltt_fields_metadata_statedump(struct ltt_session *session, |
| 850 | struct ltt_event *event) |
| 851 | { |
| 852 | const struct lttng_event_desc *desc = event->desc; |
| 853 | int ret = 0; |
| 854 | int i; |
| 855 | |
| 856 | for (i = 0; i < desc->nr_fields; i++) { |
| 857 | const struct lttng_event_field *field = &desc->fields[i]; |
| 858 | |
| 859 | ret = _ltt_field_statedump(session, field); |
| 860 | if (ret) |
| 861 | return ret; |
| 862 | } |
| 863 | return ret; |
| 864 | } |
| 865 | |
| 866 | static |
| 867 | int _ltt_event_metadata_statedump(struct ltt_session *session, |
| 868 | struct ltt_channel *chan, |
| 869 | struct ltt_event *event) |
| 870 | { |
| 871 | int ret = 0; |
| 872 | |
| 873 | if (event->metadata_dumped || !CMM_ACCESS_ONCE(session->active)) |
| 874 | return 0; |
| 875 | if (chan == session->metadata) |
| 876 | return 0; |
| 877 | /* |
| 878 | * Don't print events for which probe load is pending. |
| 879 | */ |
| 880 | if (!event->desc) |
| 881 | return 0; |
| 882 | |
| 883 | ret = lttng_metadata_printf(session, |
| 884 | "event {\n" |
| 885 | " name = \"%s\";\n" |
| 886 | " id = %u;\n" |
| 887 | " stream_id = %u;\n", |
| 888 | event->desc->name, |
| 889 | event->id, |
| 890 | event->chan->id); |
| 891 | if (ret) |
| 892 | goto end; |
| 893 | |
| 894 | if (event->desc->loglevel) { |
| 895 | ret = lttng_metadata_printf(session, |
| 896 | " loglevel = %d;\n", |
| 897 | *(*event->desc->loglevel)); |
| 898 | if (ret) |
| 899 | goto end; |
| 900 | } |
| 901 | |
| 902 | if (event->ctx) { |
| 903 | ret = lttng_metadata_printf(session, |
| 904 | " context := struct {\n"); |
| 905 | if (ret) |
| 906 | goto end; |
| 907 | } |
| 908 | ret = _ltt_context_metadata_statedump(session, event->ctx); |
| 909 | if (ret) |
| 910 | goto end; |
| 911 | if (event->ctx) { |
| 912 | ret = lttng_metadata_printf(session, |
| 913 | " };\n"); |
| 914 | if (ret) |
| 915 | goto end; |
| 916 | } |
| 917 | |
| 918 | ret = lttng_metadata_printf(session, |
| 919 | " fields := struct {\n" |
| 920 | ); |
| 921 | if (ret) |
| 922 | goto end; |
| 923 | |
| 924 | ret = _ltt_fields_metadata_statedump(session, event); |
| 925 | if (ret) |
| 926 | goto end; |
| 927 | |
| 928 | /* |
| 929 | * LTTng space reservation can only reserve multiples of the |
| 930 | * byte size. |
| 931 | */ |
| 932 | ret = lttng_metadata_printf(session, |
| 933 | " };\n" |
| 934 | "};\n\n"); |
| 935 | if (ret) |
| 936 | goto end; |
| 937 | |
| 938 | event->metadata_dumped = 1; |
| 939 | end: |
| 940 | return ret; |
| 941 | |
| 942 | } |
| 943 | |
| 944 | static |
| 945 | int _ltt_channel_metadata_statedump(struct ltt_session *session, |
| 946 | struct ltt_channel *chan) |
| 947 | { |
| 948 | int ret = 0; |
| 949 | |
| 950 | if (chan->metadata_dumped || !CMM_ACCESS_ONCE(session->active)) |
| 951 | return 0; |
| 952 | if (chan == session->metadata) |
| 953 | return 0; |
| 954 | |
| 955 | WARN_ON_ONCE(!chan->header_type); |
| 956 | ret = lttng_metadata_printf(session, |
| 957 | "stream {\n" |
| 958 | " id = %u;\n" |
| 959 | " event.header := %s;\n" |
| 960 | " packet.context := struct packet_context;\n", |
| 961 | chan->id, |
| 962 | chan->header_type == 1 ? "struct event_header_compact" : |
| 963 | "struct event_header_large"); |
| 964 | if (ret) |
| 965 | goto end; |
| 966 | |
| 967 | if (chan->ctx) { |
| 968 | ret = lttng_metadata_printf(session, |
| 969 | " event.context := struct {\n"); |
| 970 | if (ret) |
| 971 | goto end; |
| 972 | } |
| 973 | ret = _ltt_context_metadata_statedump(session, chan->ctx); |
| 974 | if (ret) |
| 975 | goto end; |
| 976 | if (chan->ctx) { |
| 977 | ret = lttng_metadata_printf(session, |
| 978 | " };\n"); |
| 979 | if (ret) |
| 980 | goto end; |
| 981 | } |
| 982 | |
| 983 | ret = lttng_metadata_printf(session, |
| 984 | "};\n\n"); |
| 985 | |
| 986 | chan->metadata_dumped = 1; |
| 987 | end: |
| 988 | return ret; |
| 989 | } |
| 990 | |
| 991 | static |
| 992 | int _ltt_stream_packet_context_declare(struct ltt_session *session) |
| 993 | { |
| 994 | return lttng_metadata_printf(session, |
| 995 | "struct packet_context {\n" |
| 996 | " uint64_clock_monotonic_t timestamp_begin;\n" |
| 997 | " uint64_clock_monotonic_t timestamp_end;\n" |
| 998 | " uint32_t events_discarded;\n" |
| 999 | " uint32_t content_size;\n" |
| 1000 | " uint32_t packet_size;\n" |
| 1001 | " uint32_t cpu_id;\n" |
| 1002 | "};\n\n" |
| 1003 | ); |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * Compact header: |
| 1008 | * id: range: 0 - 30. |
| 1009 | * id 31 is reserved to indicate an extended header. |
| 1010 | * |
| 1011 | * Large header: |
| 1012 | * id: range: 0 - 65534. |
| 1013 | * id 65535 is reserved to indicate an extended header. |
| 1014 | */ |
| 1015 | static |
| 1016 | int _ltt_event_header_declare(struct ltt_session *session) |
| 1017 | { |
| 1018 | return lttng_metadata_printf(session, |
| 1019 | "struct event_header_compact {\n" |
| 1020 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" |
| 1021 | " variant <id> {\n" |
| 1022 | " struct {\n" |
| 1023 | " uint27_clock_monotonic_t timestamp;\n" |
| 1024 | " } compact;\n" |
| 1025 | " struct {\n" |
| 1026 | " uint32_t id;\n" |
| 1027 | " uint64_clock_monotonic_t timestamp;\n" |
| 1028 | " } extended;\n" |
| 1029 | " } v;\n" |
| 1030 | "} align(%u);\n" |
| 1031 | "\n" |
| 1032 | "struct event_header_large {\n" |
| 1033 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" |
| 1034 | " variant <id> {\n" |
| 1035 | " struct {\n" |
| 1036 | " uint32_clock_monotonic_t timestamp;\n" |
| 1037 | " } compact;\n" |
| 1038 | " struct {\n" |
| 1039 | " uint32_t id;\n" |
| 1040 | " uint64_clock_monotonic_t timestamp;\n" |
| 1041 | " } extended;\n" |
| 1042 | " } v;\n" |
| 1043 | "} align(%u);\n\n", |
| 1044 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 1045 | lttng_alignof(uint16_t) * CHAR_BIT |
| 1046 | ); |
| 1047 | } |
| 1048 | |
| 1049 | /* |
| 1050 | * Approximation of NTP time of day to clock monotonic correlation, |
| 1051 | * taken at start of trace. |
| 1052 | * Yes, this is only an approximation. Yes, we can (and will) do better |
| 1053 | * in future versions. |
| 1054 | */ |
| 1055 | static |
| 1056 | uint64_t measure_clock_offset(void) |
| 1057 | { |
| 1058 | uint64_t offset, monotonic[2], realtime; |
| 1059 | struct timespec rts = { 0, 0 }; |
| 1060 | int ret; |
| 1061 | |
| 1062 | monotonic[0] = trace_clock_read64(); |
| 1063 | ret = clock_gettime(CLOCK_REALTIME, &rts); |
| 1064 | if (ret < 0) |
| 1065 | return 0; |
| 1066 | monotonic[1] = trace_clock_read64(); |
| 1067 | offset = (monotonic[0] + monotonic[1]) >> 1; |
| 1068 | realtime = rts.tv_sec * 1000000000ULL; |
| 1069 | realtime += rts.tv_nsec; |
| 1070 | offset = realtime - offset; |
| 1071 | return offset; |
| 1072 | } |
| 1073 | |
| 1074 | /* |
| 1075 | * Output metadata into this session's metadata buffers. |
| 1076 | */ |
| 1077 | static |
| 1078 | int _ltt_session_metadata_statedump(struct ltt_session *session) |
| 1079 | { |
| 1080 | unsigned char *uuid_c = session->uuid; |
| 1081 | char uuid_s[37], clock_uuid_s[CLOCK_UUID_LEN]; |
| 1082 | struct ltt_channel *chan; |
| 1083 | struct ltt_event *event; |
| 1084 | int ret = 0; |
| 1085 | |
| 1086 | if (!CMM_ACCESS_ONCE(session->active)) |
| 1087 | return 0; |
| 1088 | if (session->metadata_dumped) |
| 1089 | goto skip_session; |
| 1090 | if (!session->metadata) { |
| 1091 | DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n"); |
| 1092 | return -EPERM; |
| 1093 | } |
| 1094 | |
| 1095 | snprintf(uuid_s, sizeof(uuid_s), |
| 1096 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 1097 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], |
| 1098 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], |
| 1099 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], |
| 1100 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); |
| 1101 | |
| 1102 | ret = lttng_metadata_printf(session, |
| 1103 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
| 1104 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" |
| 1105 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" |
| 1106 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" |
| 1107 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" |
| 1108 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" |
| 1109 | "\n" |
| 1110 | "trace {\n" |
| 1111 | " major = %u;\n" |
| 1112 | " minor = %u;\n" |
| 1113 | " uuid = \"%s\";\n" |
| 1114 | " byte_order = %s;\n" |
| 1115 | " packet.header := struct {\n" |
| 1116 | " uint32_t magic;\n" |
| 1117 | " uint8_t uuid[16];\n" |
| 1118 | " uint32_t stream_id;\n" |
| 1119 | " };\n" |
| 1120 | "};\n\n", |
| 1121 | lttng_alignof(uint8_t) * CHAR_BIT, |
| 1122 | lttng_alignof(uint16_t) * CHAR_BIT, |
| 1123 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 1124 | lttng_alignof(uint64_t) * CHAR_BIT, |
| 1125 | CTF_VERSION_MAJOR, |
| 1126 | CTF_VERSION_MINOR, |
| 1127 | uuid_s, |
| 1128 | #if (BYTE_ORDER == BIG_ENDIAN) |
| 1129 | "be" |
| 1130 | #else |
| 1131 | "le" |
| 1132 | #endif |
| 1133 | ); |
| 1134 | if (ret) |
| 1135 | goto end; |
| 1136 | |
| 1137 | ret = lttng_metadata_printf(session, |
| 1138 | "clock {\n" |
| 1139 | " name = %s;\n", |
| 1140 | "monotonic" |
| 1141 | ); |
| 1142 | if (ret) |
| 1143 | goto end; |
| 1144 | |
| 1145 | if (!trace_clock_uuid(clock_uuid_s)) { |
| 1146 | ret = lttng_metadata_printf(session, |
| 1147 | " uuid = \"%s\";\n", |
| 1148 | clock_uuid_s |
| 1149 | ); |
| 1150 | if (ret) |
| 1151 | goto end; |
| 1152 | } |
| 1153 | |
| 1154 | ret = lttng_metadata_printf(session, |
| 1155 | " description = \"Monotonic Clock\";\n" |
| 1156 | " freq = %" PRIu64 "; /* Frequency, in Hz */\n" |
| 1157 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" |
| 1158 | " offset = %" PRIu64 ";\n" |
| 1159 | "};\n\n", |
| 1160 | trace_clock_freq(), |
| 1161 | measure_clock_offset() |
| 1162 | ); |
| 1163 | if (ret) |
| 1164 | goto end; |
| 1165 | |
| 1166 | ret = lttng_metadata_printf(session, |
| 1167 | "typealias integer {\n" |
| 1168 | " size = 27; align = 1; signed = false;\n" |
| 1169 | " map = clock.monotonic.value;\n" |
| 1170 | "} := uint27_clock_monotonic_t;\n" |
| 1171 | "\n" |
| 1172 | "typealias integer {\n" |
| 1173 | " size = 32; align = %u; signed = false;\n" |
| 1174 | " map = clock.monotonic.value;\n" |
| 1175 | "} := uint32_clock_monotonic_t;\n" |
| 1176 | "\n" |
| 1177 | "typealias integer {\n" |
| 1178 | " size = 64; align = %u; signed = false;\n" |
| 1179 | " map = clock.monotonic.value;\n" |
| 1180 | "} := uint64_clock_monotonic_t;\n\n", |
| 1181 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 1182 | lttng_alignof(uint64_t) * CHAR_BIT |
| 1183 | ); |
| 1184 | if (ret) |
| 1185 | goto end; |
| 1186 | |
| 1187 | ret = _ltt_stream_packet_context_declare(session); |
| 1188 | if (ret) |
| 1189 | goto end; |
| 1190 | |
| 1191 | ret = _ltt_event_header_declare(session); |
| 1192 | if (ret) |
| 1193 | goto end; |
| 1194 | |
| 1195 | skip_session: |
| 1196 | cds_list_for_each_entry(chan, &session->chan, list) { |
| 1197 | ret = _ltt_channel_metadata_statedump(session, chan); |
| 1198 | if (ret) |
| 1199 | goto end; |
| 1200 | } |
| 1201 | |
| 1202 | cds_list_for_each_entry(event, &session->events, list) { |
| 1203 | ret = _ltt_event_metadata_statedump(session, event->chan, event); |
| 1204 | if (ret) |
| 1205 | goto end; |
| 1206 | } |
| 1207 | session->metadata_dumped = 1; |
| 1208 | end: |
| 1209 | return ret; |
| 1210 | } |
| 1211 | |
| 1212 | void lttng_ust_events_exit(void) |
| 1213 | { |
| 1214 | struct ltt_session *session, *tmpsession; |
| 1215 | |
| 1216 | cds_list_for_each_entry_safe(session, tmpsession, &sessions, list) |
| 1217 | ltt_session_destroy(session); |
| 1218 | } |
| 1219 | |
| 1220 | /* WILDCARDS */ |
| 1221 | |
| 1222 | static |
| 1223 | int wildcard_same_loglevel(struct wildcard_entry *e, |
| 1224 | enum lttng_ust_loglevel_type loglevel_type, |
| 1225 | int loglevel) |
| 1226 | { |
| 1227 | if (e->loglevel_type == loglevel_type && e->loglevel == loglevel) |
| 1228 | return 1; |
| 1229 | else |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | #if 0 |
| 1234 | static |
| 1235 | int wildcard_is_within(struct wildcard_entry *e, |
| 1236 | enum lttng_ust_loglevel_type loglevel_type, |
| 1237 | int loglevel) |
| 1238 | { |
| 1239 | if (e->loglevel_type == LTTNG_UST_LOGLEVEL_ALL |
| 1240 | || e->loglevel == -1) |
| 1241 | return 1; |
| 1242 | switch (e->loglevel_type) { |
| 1243 | case LTTNG_UST_LOGLEVEL_RANGE: |
| 1244 | switch (loglevel_type) { |
| 1245 | case LTTNG_UST_LOGLEVEL_RANGE: |
| 1246 | if (e->loglevel >= loglevel) |
| 1247 | return 1; |
| 1248 | else |
| 1249 | return 0; |
| 1250 | case LTTNG_UST_LOGLEVEL_SINGLE: |
| 1251 | if (e->loglevel <= 0 && loglevel == 0) |
| 1252 | return 1; |
| 1253 | else |
| 1254 | return 0; |
| 1255 | } |
| 1256 | case LTTNG_UST_LOGLEVEL_SINGLE: |
| 1257 | switch (loglevel_type) { |
| 1258 | case LTTNG_UST_LOGLEVEL_RANGE: |
| 1259 | if (loglevel <= 0) |
| 1260 | return 1; |
| 1261 | else |
| 1262 | return 0; |
| 1263 | case LTTNG_UST_LOGLEVEL_SINGLE: |
| 1264 | if (e->loglevel == loglevel) |
| 1265 | return 1; |
| 1266 | else |
| 1267 | return 0; |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | #endif |
| 1272 | |
| 1273 | /* |
| 1274 | * Add the wildcard to the wildcard list. Must be called with |
| 1275 | * ust lock held. |
| 1276 | */ |
| 1277 | static |
| 1278 | struct session_wildcard *add_wildcard(struct ltt_channel *chan, |
| 1279 | struct lttng_ust_event *event_param) |
| 1280 | { |
| 1281 | struct wildcard_entry *e; |
| 1282 | struct session_wildcard *sw; |
| 1283 | size_t name_len = strlen(event_param->name) + 1; |
| 1284 | int found = 0; |
| 1285 | |
| 1286 | /* |
| 1287 | * Try to find global wildcard entry. Given that this is shared |
| 1288 | * across all sessions, we need to check for exact loglevel |
| 1289 | * match, not just whether contained within the existing ones. |
| 1290 | */ |
| 1291 | cds_list_for_each_entry(e, &wildcard_list, list) { |
| 1292 | if (!strncmp(event_param->name, e->name, |
| 1293 | LTTNG_UST_SYM_NAME_LEN - 1)) { |
| 1294 | if (wildcard_same_loglevel(e, |
| 1295 | event_param->loglevel_type, |
| 1296 | event_param->loglevel)) { |
| 1297 | found = 1; |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | if (!found) { |
| 1304 | /* |
| 1305 | * Create global wildcard entry if not found. Using |
| 1306 | * zmalloc here to allocate a variable length element. |
| 1307 | * Could cause some memory fragmentation if overused. |
| 1308 | */ |
| 1309 | e = zmalloc(sizeof(struct wildcard_entry) + name_len); |
| 1310 | if (!e) |
| 1311 | return ERR_PTR(-ENOMEM); |
| 1312 | memcpy(&e->name[0], event_param->name, name_len); |
| 1313 | cds_list_add(&e->list, &wildcard_list); |
| 1314 | CDS_INIT_LIST_HEAD(&e->session_list); |
| 1315 | } |
| 1316 | |
| 1317 | /* session wildcard */ |
| 1318 | cds_list_for_each_entry(sw, &e->session_list, session_list) { |
| 1319 | if (chan == sw->chan) { |
| 1320 | DBG("wildcard %s busy for this channel", |
| 1321 | event_param->name); |
| 1322 | return ERR_PTR(-EEXIST); /* Already there */ |
| 1323 | } |
| 1324 | } |
| 1325 | sw = zmalloc(sizeof(struct session_wildcard)); |
| 1326 | if (!sw) |
| 1327 | return ERR_PTR(-ENOMEM); |
| 1328 | sw->chan = chan; |
| 1329 | sw->enabled = 1; |
| 1330 | memcpy(&sw->event_param, event_param, sizeof(sw->event_param)); |
| 1331 | sw->event_param.instrumentation = LTTNG_UST_TRACEPOINT; |
| 1332 | sw->event_param.loglevel_type = event_param->loglevel_type; |
| 1333 | sw->event_param.loglevel = event_param->loglevel; |
| 1334 | CDS_INIT_LIST_HEAD(&sw->events); |
| 1335 | cds_list_add(&sw->list, &chan->session->wildcards); |
| 1336 | cds_list_add(&sw->session_list, &e->session_list); |
| 1337 | sw->entry = e; |
| 1338 | ltt_probes_create_wildcard_events(e, sw); |
| 1339 | return sw; |
| 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * Remove the wildcard from the wildcard list. Must be called with |
| 1344 | * ust_lock held. Only called at session teardown. |
| 1345 | */ |
| 1346 | static |
| 1347 | void _remove_wildcard(struct session_wildcard *wildcard) |
| 1348 | { |
| 1349 | struct ltt_event *ev, *tmp; |
| 1350 | |
| 1351 | /* |
| 1352 | * Just remove the events owned (for enable/disable) by this |
| 1353 | * wildcard from the list. The session teardown will take care |
| 1354 | * of freeing the event memory. |
| 1355 | */ |
| 1356 | cds_list_for_each_entry_safe(ev, tmp, &wildcard->events, |
| 1357 | wildcard_list) { |
| 1358 | cds_list_del(&ev->wildcard_list); |
| 1359 | } |
| 1360 | cds_list_del(&wildcard->session_list); |
| 1361 | cds_list_del(&wildcard->list); |
| 1362 | if (cds_list_empty(&wildcard->entry->session_list)) { |
| 1363 | cds_list_del(&wildcard->entry->list); |
| 1364 | free(wildcard->entry); |
| 1365 | } |
| 1366 | free(wildcard); |
| 1367 | } |
| 1368 | |
| 1369 | int ltt_wildcard_create(struct ltt_channel *chan, |
| 1370 | struct lttng_ust_event *event_param, |
| 1371 | struct session_wildcard **_sw) |
| 1372 | { |
| 1373 | struct session_wildcard *sw; |
| 1374 | |
| 1375 | sw = add_wildcard(chan, event_param); |
| 1376 | if (!sw || IS_ERR(sw)) { |
| 1377 | return PTR_ERR(sw); |
| 1378 | } |
| 1379 | *_sw = sw; |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
| 1383 | static |
| 1384 | void _ltt_wildcard_destroy(struct session_wildcard *sw) |
| 1385 | { |
| 1386 | _remove_wildcard(sw); |
| 1387 | } |
| 1388 | |
| 1389 | int ltt_wildcard_enable(struct session_wildcard *wildcard) |
| 1390 | { |
| 1391 | struct ltt_event *ev; |
| 1392 | int ret; |
| 1393 | |
| 1394 | if (wildcard->enabled) |
| 1395 | return -EEXIST; |
| 1396 | cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) { |
| 1397 | ret = ltt_event_enable(ev); |
| 1398 | if (ret) { |
| 1399 | DBG("Error: enable error.\n"); |
| 1400 | return ret; |
| 1401 | } |
| 1402 | } |
| 1403 | wildcard->enabled = 1; |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | int ltt_wildcard_disable(struct session_wildcard *wildcard) |
| 1408 | { |
| 1409 | struct ltt_event *ev; |
| 1410 | int ret; |
| 1411 | |
| 1412 | if (!wildcard->enabled) |
| 1413 | return -EEXIST; |
| 1414 | cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) { |
| 1415 | ret = ltt_event_disable(ev); |
| 1416 | if (ret) { |
| 1417 | DBG("Error: disable error.\n"); |
| 1418 | return ret; |
| 1419 | } |
| 1420 | } |
| 1421 | wildcard->enabled = 0; |
| 1422 | return 0; |
| 1423 | } |