| 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 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/mutex.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/jiffies.h> |
| 15 | #include <linux/uuid.h> |
| 16 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
| 17 | #include "ltt-events.h" |
| 18 | #include "ltt-tracer.h" |
| 19 | |
| 20 | static LIST_HEAD(sessions); |
| 21 | static LIST_HEAD(ltt_transport_list); |
| 22 | static DEFINE_MUTEX(sessions_mutex); |
| 23 | static struct kmem_cache *event_cache; |
| 24 | |
| 25 | static void _ltt_event_destroy(struct ltt_event *event); |
| 26 | static int _ltt_event_unregister(struct ltt_event *event); |
| 27 | static |
| 28 | int _ltt_event_metadata_statedump(struct ltt_session *session, |
| 29 | struct ltt_channel *chan, |
| 30 | struct ltt_event *event); |
| 31 | static |
| 32 | int _ltt_session_metadata_statedump(struct ltt_session *session); |
| 33 | |
| 34 | |
| 35 | static |
| 36 | void synchronize_trace(void) |
| 37 | { |
| 38 | synchronize_sched(); |
| 39 | #ifdef CONFIG_PREEMPT_RT |
| 40 | synchronize_rcu(); |
| 41 | #endif |
| 42 | } |
| 43 | |
| 44 | struct ltt_session *ltt_session_create(void) |
| 45 | { |
| 46 | struct ltt_session *session; |
| 47 | |
| 48 | mutex_lock(&sessions_mutex); |
| 49 | session = kzalloc(sizeof(struct ltt_session), GFP_KERNEL); |
| 50 | if (!session) |
| 51 | return NULL; |
| 52 | INIT_LIST_HEAD(&session->chan); |
| 53 | INIT_LIST_HEAD(&session->events); |
| 54 | uuid_le_gen(&session->uuid); |
| 55 | list_add(&session->list, &sessions); |
| 56 | mutex_unlock(&sessions_mutex); |
| 57 | return session; |
| 58 | } |
| 59 | |
| 60 | void ltt_session_destroy(struct ltt_session *session) |
| 61 | { |
| 62 | struct ltt_channel *chan, *tmpchan; |
| 63 | struct ltt_event *event, *tmpevent; |
| 64 | int ret; |
| 65 | |
| 66 | mutex_lock(&sessions_mutex); |
| 67 | ACCESS_ONCE(session->active) = 0; |
| 68 | list_for_each_entry(event, &session->events, list) { |
| 69 | ret = _ltt_event_unregister(event); |
| 70 | WARN_ON(ret); |
| 71 | } |
| 72 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 73 | list_for_each_entry_safe(event, tmpevent, &session->events, list) |
| 74 | _ltt_event_destroy(event); |
| 75 | list_for_each_entry_safe(chan, tmpchan, &session->chan, list) |
| 76 | _ltt_channel_destroy(chan); |
| 77 | list_del(&session->list); |
| 78 | mutex_unlock(&sessions_mutex); |
| 79 | kfree(session); |
| 80 | } |
| 81 | |
| 82 | int ltt_session_start(struct ltt_session *session) |
| 83 | { |
| 84 | int ret = 0; |
| 85 | struct ltt_channel *chan; |
| 86 | |
| 87 | mutex_lock(&sessions_mutex); |
| 88 | if (session->active) { |
| 89 | ret = -EBUSY; |
| 90 | goto end; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Snapshot the number of events per channel to know the type of header |
| 95 | * we need to use. |
| 96 | */ |
| 97 | list_for_each_entry(chan, &session->chan, list) { |
| 98 | if (chan->header_type) |
| 99 | continue; /* don't change it if session stop/restart */ |
| 100 | if (chan->free_event_id < 31) |
| 101 | chan->header_type = 1; /* compact */ |
| 102 | else |
| 103 | chan->header_type = 2; /* large */ |
| 104 | } |
| 105 | |
| 106 | ACCESS_ONCE(session->active) = 1; |
| 107 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 108 | ret = _ltt_session_metadata_statedump(session); |
| 109 | if (ret) { |
| 110 | ACCESS_ONCE(session->active) = 0; |
| 111 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 112 | } |
| 113 | end: |
| 114 | mutex_unlock(&sessions_mutex); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | int ltt_session_stop(struct ltt_session *session) |
| 119 | { |
| 120 | int ret = 0; |
| 121 | |
| 122 | mutex_lock(&sessions_mutex); |
| 123 | if (!session->active) { |
| 124 | ret = -EBUSY; |
| 125 | goto end; |
| 126 | } |
| 127 | ACCESS_ONCE(session->active) = 0; |
| 128 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 129 | end: |
| 130 | mutex_unlock(&sessions_mutex); |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | static struct ltt_transport *ltt_transport_find(const char *name) |
| 135 | { |
| 136 | struct ltt_transport *transport; |
| 137 | |
| 138 | list_for_each_entry(transport, <t_transport_list, node) { |
| 139 | if (!strcmp(transport->name, name)) |
| 140 | return transport; |
| 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | struct ltt_channel *ltt_channel_create(struct ltt_session *session, |
| 146 | const char *transport_name, |
| 147 | void *buf_addr, |
| 148 | size_t subbuf_size, size_t num_subbuf, |
| 149 | unsigned int switch_timer_interval, |
| 150 | unsigned int read_timer_interval) |
| 151 | { |
| 152 | struct ltt_channel *chan; |
| 153 | struct ltt_transport *transport; |
| 154 | |
| 155 | mutex_lock(&sessions_mutex); |
| 156 | if (session->active) { |
| 157 | printk(KERN_WARNING "LTTng refusing to add channel to active session\n"); |
| 158 | goto active; /* Refuse to add channel to active session */ |
| 159 | } |
| 160 | transport = ltt_transport_find(transport_name); |
| 161 | if (!transport) { |
| 162 | printk(KERN_WARNING "LTTng transport %s not found\n", |
| 163 | transport_name); |
| 164 | goto notransport; |
| 165 | } |
| 166 | chan = kzalloc(sizeof(struct ltt_channel), GFP_KERNEL); |
| 167 | if (!chan) |
| 168 | goto nomem; |
| 169 | chan->session = session; |
| 170 | init_waitqueue_head(&chan->notify_wait); |
| 171 | chan->chan = transport->ops.channel_create("[lttng]", chan, buf_addr, |
| 172 | subbuf_size, num_subbuf, switch_timer_interval, |
| 173 | read_timer_interval); |
| 174 | if (!chan->chan) |
| 175 | goto create_error; |
| 176 | chan->id = session->free_chan_id++; |
| 177 | chan->ops = &transport->ops; |
| 178 | list_add(&chan->list, &session->chan); |
| 179 | mutex_unlock(&sessions_mutex); |
| 180 | return chan; |
| 181 | |
| 182 | create_error: |
| 183 | kfree(chan); |
| 184 | nomem: |
| 185 | notransport: |
| 186 | active: |
| 187 | mutex_unlock(&sessions_mutex); |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Only used internally at session destruction. |
| 193 | */ |
| 194 | void _ltt_channel_destroy(struct ltt_channel *chan) |
| 195 | { |
| 196 | chan->ops->channel_destroy(chan->chan); |
| 197 | list_del(&chan->list); |
| 198 | kfree(chan); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Supports event creation while tracing session is active. |
| 203 | */ |
| 204 | struct ltt_event *ltt_event_create(struct ltt_channel *chan, char *name, |
| 205 | struct lttng_kernel_event *event_param, |
| 206 | const struct lttng_event_desc *event_desc, |
| 207 | void *filter) |
| 208 | { |
| 209 | struct ltt_event *event; |
| 210 | int ret; |
| 211 | |
| 212 | mutex_lock(&sessions_mutex); |
| 213 | if (chan->free_event_id == -1UL) |
| 214 | goto full; |
| 215 | /* |
| 216 | * This is O(n^2) (for each event, the loop is called at event |
| 217 | * creation). Might require a hash if we have lots of events. |
| 218 | */ |
| 219 | list_for_each_entry(event, &chan->session->events, list) |
| 220 | if (!strcmp(event->desc->name, name)) |
| 221 | goto exist; |
| 222 | event = kmem_cache_zalloc(event_cache, GFP_KERNEL); |
| 223 | if (!event) |
| 224 | goto cache_error; |
| 225 | event->chan = chan; |
| 226 | event->desc = event_desc; |
| 227 | event->filter = filter; |
| 228 | event->id = chan->free_event_id++; |
| 229 | event->instrumentation = event_param->instrumentation; |
| 230 | /* Populate ltt_event structure before tracepoint registration. */ |
| 231 | smp_wmb(); |
| 232 | switch (event_param->instrumentation) { |
| 233 | case LTTNG_KERNEL_TRACEPOINTS: |
| 234 | ret = tracepoint_probe_register(name, event_desc->probe_callback, |
| 235 | event); |
| 236 | if (ret) |
| 237 | goto register_error; |
| 238 | break; |
| 239 | case LTTNG_KERNEL_KPROBES: |
| 240 | ret = lttng_kprobes_register(name, |
| 241 | event_param->u.kprobe.symbol_name, |
| 242 | event_param->u.kprobe.offset, |
| 243 | event_param->u.kprobe.addr, |
| 244 | event); |
| 245 | if (ret) |
| 246 | goto register_error; |
| 247 | break; |
| 248 | default: |
| 249 | WARN_ON_ONCE(1); |
| 250 | } |
| 251 | ret = _ltt_event_metadata_statedump(chan->session, chan, event); |
| 252 | if (ret) |
| 253 | goto statedump_error; |
| 254 | list_add(&event->list, &chan->session->events); |
| 255 | mutex_unlock(&sessions_mutex); |
| 256 | return event; |
| 257 | |
| 258 | statedump_error: |
| 259 | WARN_ON_ONCE(tracepoint_probe_unregister(name, event_desc->probe_callback, |
| 260 | event)); |
| 261 | register_error: |
| 262 | kmem_cache_free(event_cache, event); |
| 263 | cache_error: |
| 264 | exist: |
| 265 | full: |
| 266 | mutex_unlock(&sessions_mutex); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Only used internally at session destruction. |
| 272 | */ |
| 273 | int _ltt_event_unregister(struct ltt_event *event) |
| 274 | { |
| 275 | int ret = -EINVAL; |
| 276 | |
| 277 | switch (event->instrumentation) { |
| 278 | case LTTNG_KERNEL_TRACEPOINTS: |
| 279 | ret = tracepoint_probe_unregister(event->desc->name, |
| 280 | event->desc->probe_callback, |
| 281 | event); |
| 282 | if (ret) |
| 283 | return ret; |
| 284 | break; |
| 285 | case LTTNG_KERNEL_KPROBES: |
| 286 | lttng_kprobes_unregister(event); |
| 287 | ret = 0; |
| 288 | break; |
| 289 | default: |
| 290 | WARN_ON_ONCE(1); |
| 291 | } |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Used when an event FD is released. |
| 297 | */ |
| 298 | int ltt_event_unregister(struct ltt_event *event) |
| 299 | { |
| 300 | int ret; |
| 301 | |
| 302 | mutex_lock(&sessions_mutex); |
| 303 | ret = ltt_event_unregister(event); |
| 304 | mutex_unlock(&sessions_mutex); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Only used internally at session destruction. |
| 310 | */ |
| 311 | static |
| 312 | void _ltt_event_destroy(struct ltt_event *event) |
| 313 | { |
| 314 | ltt_event_put(event->desc); |
| 315 | list_del(&event->list); |
| 316 | kmem_cache_free(event_cache, event); |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * We have exclusive access to our metadata buffer (protected by the |
| 321 | * sessions_mutex), so we can do racy operations such as looking for |
| 322 | * remaining space left in packet and write, since mutual exclusion |
| 323 | * protects us from concurrent writes. |
| 324 | */ |
| 325 | int lttng_metadata_printf(struct ltt_session *session, |
| 326 | const char *fmt, ...) |
| 327 | { |
| 328 | struct lib_ring_buffer_ctx ctx; |
| 329 | struct ltt_channel *chan = session->metadata; |
| 330 | char *str; |
| 331 | int ret = 0, waitret; |
| 332 | size_t len, reserve_len, pos; |
| 333 | va_list ap; |
| 334 | |
| 335 | WARN_ON_ONCE(!ACCESS_ONCE(session->active)); |
| 336 | |
| 337 | va_start(ap, fmt); |
| 338 | str = kvasprintf(GFP_KERNEL, fmt, ap); |
| 339 | va_end(ap); |
| 340 | if (!str) |
| 341 | return -ENOMEM; |
| 342 | |
| 343 | len = strlen(str); |
| 344 | pos = 0; |
| 345 | |
| 346 | for (pos = 0; pos < len; pos += reserve_len) { |
| 347 | reserve_len = min_t(size_t, |
| 348 | chan->ops->packet_avail_size(chan->chan), |
| 349 | len - pos); |
| 350 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, |
| 351 | sizeof(char), -1); |
| 352 | /* |
| 353 | * We don't care about metadata buffer's records lost |
| 354 | * count, because we always retry here. Report error if |
| 355 | * we need to bail out after timeout or being |
| 356 | * interrupted. |
| 357 | */ |
| 358 | waitret = wait_event_interruptible_timeout(*chan->ops->get_reader_wait_queue(chan), |
| 359 | ({ |
| 360 | ret = chan->ops->event_reserve(&ctx); |
| 361 | ret != -ENOBUFS || !ret; |
| 362 | }), |
| 363 | msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC)); |
| 364 | if (!waitret || waitret == -ERESTARTSYS || ret) { |
| 365 | printk(KERN_WARNING "LTTng: Failure to write metadata to buffers (%s)\n", |
| 366 | waitret == -ERESTARTSYS ? "interrupted" : |
| 367 | (ret == -ENOBUFS ? "timeout" : "I/O error")); |
| 368 | if (waitret == -ERESTARTSYS) |
| 369 | ret = waitret; |
| 370 | goto end; |
| 371 | } |
| 372 | chan->ops->event_write(&ctx, &str[pos], len); |
| 373 | chan->ops->event_commit(&ctx); |
| 374 | } |
| 375 | end: |
| 376 | kfree(str); |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | static |
| 381 | int _ltt_fields_metadata_statedump(struct ltt_session *session, |
| 382 | struct ltt_event *event) |
| 383 | { |
| 384 | const struct lttng_event_desc *desc = event->desc; |
| 385 | int ret = 0; |
| 386 | int i; |
| 387 | |
| 388 | for (i = 0; i < desc->nr_fields; i++) { |
| 389 | const struct lttng_event_field *field = &desc->fields[i]; |
| 390 | |
| 391 | switch (field->type.atype) { |
| 392 | case atype_integer: |
| 393 | ret = lttng_metadata_printf(session, |
| 394 | " integer { size = %u; align = %u; signed = %u;%s } %s;\n", |
| 395 | field->type.u.basic.integer.size, |
| 396 | field->type.u.basic.integer.alignment, |
| 397 | field->type.u.basic.integer.signedness, |
| 398 | #ifdef __BIG_ENDIAN |
| 399 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 400 | #else |
| 401 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 402 | #endif |
| 403 | field->name); |
| 404 | break; |
| 405 | case atype_enum: |
| 406 | ret = lttng_metadata_printf(session, |
| 407 | " %s %s;\n", |
| 408 | field->type.u.basic.enumeration.name, |
| 409 | field->name); |
| 410 | break; |
| 411 | case atype_array: |
| 412 | { |
| 413 | const struct lttng_basic_type *elem_type; |
| 414 | |
| 415 | elem_type = &field->type.u.array.elem_type; |
| 416 | ret = lttng_metadata_printf(session, |
| 417 | " integer { size = %u; align = %u; signed = %u;%s } %s[%u];\n", |
| 418 | elem_type->u.basic.integer.size, |
| 419 | elem_type->u.basic.integer.alignment, |
| 420 | elem_type->u.basic.integer.signedness, |
| 421 | #ifdef __BIG_ENDIAN |
| 422 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 423 | #else |
| 424 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 425 | #endif |
| 426 | field->name, field->type.u.array.length); |
| 427 | break; |
| 428 | } |
| 429 | case atype_sequence: |
| 430 | { |
| 431 | const struct lttng_basic_type *elem_type; |
| 432 | const struct lttng_basic_type *length_type; |
| 433 | |
| 434 | elem_type = &field->type.u.sequence.elem_type; |
| 435 | length_type = &field->type.u.sequence.length_type; |
| 436 | ret = lttng_metadata_printf(session, |
| 437 | " integer { size = %u; align = %u; signed = %u;%s } %s[ integer { size = %u; align = %u; signed = %u;%s } ];\n", |
| 438 | elem_type->u.basic.integer.size, |
| 439 | elem_type->u.basic.integer.alignment, |
| 440 | elem_type->u.basic.integer.signedness, |
| 441 | #ifdef __BIG_ENDIAN |
| 442 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 443 | #else |
| 444 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 445 | #endif |
| 446 | field->name, |
| 447 | length_type->u.basic.integer.size, |
| 448 | length_type->u.basic.integer.alignment, |
| 449 | length_type->u.basic.integer.signedness, |
| 450 | #ifdef __BIG_ENDIAN |
| 451 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 452 | #else |
| 453 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "" |
| 454 | #endif |
| 455 | ); |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | case atype_string: |
| 460 | ret = lttng_metadata_printf(session, |
| 461 | " string%s %s;\n", |
| 462 | field->type.u.basic.string.encoding == lttng_encode_ASCII ? |
| 463 | " { encoding = ASCII; }" : "", |
| 464 | field->name); |
| 465 | break; |
| 466 | default: |
| 467 | WARN_ON_ONCE(1); |
| 468 | return -EINVAL; |
| 469 | } |
| 470 | } |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | static |
| 475 | int _ltt_event_metadata_statedump(struct ltt_session *session, |
| 476 | struct ltt_channel *chan, |
| 477 | struct ltt_event *event) |
| 478 | { |
| 479 | int ret = 0; |
| 480 | |
| 481 | if (event->metadata_dumped || !ACCESS_ONCE(session->active)) |
| 482 | return 0; |
| 483 | if (chan == session->metadata) |
| 484 | return 0; |
| 485 | |
| 486 | ret = lttng_metadata_printf(session, |
| 487 | "event {\n" |
| 488 | " name = %s;\n" |
| 489 | " id = %u;\n" |
| 490 | " stream_id = %u;\n" |
| 491 | " event.fields := struct {\n", |
| 492 | event->desc->name, |
| 493 | event->id, |
| 494 | event->chan->id); |
| 495 | if (ret) |
| 496 | goto end; |
| 497 | |
| 498 | ret = _ltt_fields_metadata_statedump(session, event); |
| 499 | if (ret) |
| 500 | goto end; |
| 501 | |
| 502 | /* |
| 503 | * LTTng space reservation can only reserve multiples of the |
| 504 | * byte size. |
| 505 | */ |
| 506 | ret = lttng_metadata_printf(session, |
| 507 | " };\n" |
| 508 | "};\n\n"); |
| 509 | if (ret) |
| 510 | goto end; |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | event->metadata_dumped = 1; |
| 516 | end: |
| 517 | return ret; |
| 518 | |
| 519 | } |
| 520 | |
| 521 | static |
| 522 | int _ltt_channel_metadata_statedump(struct ltt_session *session, |
| 523 | struct ltt_channel *chan) |
| 524 | { |
| 525 | int ret = 0; |
| 526 | |
| 527 | if (chan->metadata_dumped || !ACCESS_ONCE(session->active)) |
| 528 | return 0; |
| 529 | if (chan == session->metadata) |
| 530 | return 0; |
| 531 | |
| 532 | WARN_ON_ONCE(!chan->header_type); |
| 533 | ret = lttng_metadata_printf(session, |
| 534 | "stream {\n" |
| 535 | " id = %u;\n" |
| 536 | " event.header := %s;\n" |
| 537 | " packet.context := struct packet_context;\n" |
| 538 | "};\n\n", |
| 539 | chan->id, |
| 540 | chan->header_type == 1 ? "struct event_header_compact" : |
| 541 | "struct event_header_large"); |
| 542 | if (ret) |
| 543 | goto end; |
| 544 | |
| 545 | chan->metadata_dumped = 1; |
| 546 | end: |
| 547 | return ret; |
| 548 | } |
| 549 | |
| 550 | static |
| 551 | int _ltt_stream_packet_context_declare(struct ltt_session *session) |
| 552 | { |
| 553 | return lttng_metadata_printf(session, |
| 554 | "struct packet_context {\n" |
| 555 | " uint64_t timestamp_begin;\n" |
| 556 | " uint64_t timestamp_end;\n" |
| 557 | " uint32_t events_discarded;\n" |
| 558 | " uint32_t content_size;\n" |
| 559 | " uint32_t packet_size;\n" |
| 560 | " uint32_t cpu_id;\n" |
| 561 | "};\n" |
| 562 | ); |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Compact header: |
| 567 | * id: range: 0 - 30. |
| 568 | * id 31 is reserved to indicate an extended header. |
| 569 | * |
| 570 | * Large header: |
| 571 | * id: range: 0 - 65534. |
| 572 | * id 65535 is reserved to indicate an extended header. |
| 573 | */ |
| 574 | static |
| 575 | int _ltt_event_header_declare(struct ltt_session *session) |
| 576 | { |
| 577 | return lttng_metadata_printf(session, |
| 578 | "struct event_header_compact {\n" |
| 579 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" |
| 580 | " variant <id> {\n" |
| 581 | " struct {\n" |
| 582 | " uint27_t timestamp;\n" |
| 583 | " } compact;\n" |
| 584 | " struct {\n" |
| 585 | " uint32_t id;\n" |
| 586 | " uint64_t timestamp;\n" |
| 587 | " } extended;\n" |
| 588 | " } v;\n" |
| 589 | "} align(%u);\n" |
| 590 | "\n" |
| 591 | "struct event_header_large {\n" |
| 592 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" |
| 593 | " variant <id> {\n" |
| 594 | " struct {\n" |
| 595 | " uint32_t timestamp;\n" |
| 596 | " } compact;\n" |
| 597 | " struct {\n" |
| 598 | " uint32_t id;\n" |
| 599 | " uint64_t timestamp;\n" |
| 600 | " } extended;\n" |
| 601 | " } v;\n" |
| 602 | "} align(%u);\n\n", |
| 603 | ltt_alignof(uint32_t) * CHAR_BIT, |
| 604 | ltt_alignof(uint16_t) * CHAR_BIT |
| 605 | ); |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * Output metadata into this session's metadata buffers. |
| 610 | */ |
| 611 | static |
| 612 | int _ltt_session_metadata_statedump(struct ltt_session *session) |
| 613 | { |
| 614 | char uuid_s[37]; |
| 615 | struct ltt_channel *chan; |
| 616 | struct ltt_event *event; |
| 617 | int ret = 0; |
| 618 | |
| 619 | if (!ACCESS_ONCE(session->active)) |
| 620 | return 0; |
| 621 | if (session->metadata_dumped) |
| 622 | goto skip_session; |
| 623 | if (!session->metadata) { |
| 624 | printk(KERN_WARNING "LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n"); |
| 625 | return -EPERM; |
| 626 | } |
| 627 | |
| 628 | snprintf(uuid_s, sizeof(uuid_s), |
| 629 | "%x%x%x%x-%x%x-%x%x-%x%x-%x%x%x%x%x%x", |
| 630 | uuid_s[0], uuid_s[1], uuid_s[2], uuid_s[3], |
| 631 | uuid_s[4], uuid_s[5], uuid_s[6], uuid_s[7], |
| 632 | uuid_s[8], uuid_s[9], uuid_s[10], uuid_s[11], |
| 633 | uuid_s[12], uuid_s[13], uuid_s[14], uuid_s[15]); |
| 634 | |
| 635 | ret = lttng_metadata_printf(session, |
| 636 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
| 637 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" |
| 638 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" |
| 639 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" |
| 640 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" |
| 641 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" |
| 642 | "\n" |
| 643 | "trace {\n" |
| 644 | " major = %u;\n" |
| 645 | " minor = %u;\n" |
| 646 | " uuid = %s;\n" |
| 647 | " byte_order = %s;\n" |
| 648 | " packet.header := struct {\n" |
| 649 | " uint32_t magic;\n" |
| 650 | " uint8_t uuid[16];\n" |
| 651 | " uint32_t stream_id;\n" |
| 652 | " };\n", |
| 653 | "};\n\n", |
| 654 | ltt_alignof(uint8_t) * CHAR_BIT, |
| 655 | ltt_alignof(uint32_t) * CHAR_BIT, |
| 656 | ltt_alignof(uint64_t) * CHAR_BIT, |
| 657 | CTF_VERSION_MAJOR, |
| 658 | CTF_VERSION_MINOR, |
| 659 | uuid_s, |
| 660 | #ifdef __BIG_ENDIAN |
| 661 | "be" |
| 662 | #else |
| 663 | "le" |
| 664 | #endif |
| 665 | ); |
| 666 | if (ret) |
| 667 | goto end; |
| 668 | |
| 669 | ret = _ltt_stream_packet_context_declare(session); |
| 670 | if (ret) |
| 671 | goto end; |
| 672 | |
| 673 | ret = _ltt_event_header_declare(session); |
| 674 | if (ret) |
| 675 | goto end; |
| 676 | |
| 677 | skip_session: |
| 678 | list_for_each_entry(chan, &session->chan, list) { |
| 679 | ret = _ltt_channel_metadata_statedump(session, chan); |
| 680 | if (ret) |
| 681 | goto end; |
| 682 | } |
| 683 | |
| 684 | list_for_each_entry(event, &session->events, list) { |
| 685 | ret = _ltt_event_metadata_statedump(session, chan, event); |
| 686 | if (ret) |
| 687 | goto end; |
| 688 | } |
| 689 | session->metadata_dumped = 1; |
| 690 | end: |
| 691 | return ret; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * ltt_transport_register - LTT transport registration |
| 696 | * @transport: transport structure |
| 697 | * |
| 698 | * Registers a transport which can be used as output to extract the data out of |
| 699 | * LTTng. The module calling this registration function must ensure that no |
| 700 | * trap-inducing code will be executed by the transport functions. E.g. |
| 701 | * vmalloc_sync_all() must be called between a vmalloc and the moment the memory |
| 702 | * is made visible to the transport function. This registration acts as a |
| 703 | * vmalloc_sync_all. Therefore, only if the module allocates virtual memory |
| 704 | * after its registration must it synchronize the TLBs. |
| 705 | */ |
| 706 | void ltt_transport_register(struct ltt_transport *transport) |
| 707 | { |
| 708 | /* |
| 709 | * Make sure no page fault can be triggered by the module about to be |
| 710 | * registered. We deal with this here so we don't have to call |
| 711 | * vmalloc_sync_all() in each module's init. |
| 712 | */ |
| 713 | wrapper_vmalloc_sync_all(); |
| 714 | |
| 715 | mutex_lock(&sessions_mutex); |
| 716 | list_add_tail(&transport->node, <t_transport_list); |
| 717 | mutex_unlock(&sessions_mutex); |
| 718 | } |
| 719 | EXPORT_SYMBOL_GPL(ltt_transport_register); |
| 720 | |
| 721 | /** |
| 722 | * ltt_transport_unregister - LTT transport unregistration |
| 723 | * @transport: transport structure |
| 724 | */ |
| 725 | void ltt_transport_unregister(struct ltt_transport *transport) |
| 726 | { |
| 727 | mutex_lock(&sessions_mutex); |
| 728 | list_del(&transport->node); |
| 729 | mutex_unlock(&sessions_mutex); |
| 730 | } |
| 731 | EXPORT_SYMBOL_GPL(ltt_transport_unregister); |
| 732 | |
| 733 | static int __init ltt_events_init(void) |
| 734 | { |
| 735 | int ret; |
| 736 | |
| 737 | event_cache = KMEM_CACHE(ltt_event, 0); |
| 738 | if (!event_cache) |
| 739 | return -ENOMEM; |
| 740 | ret = ltt_debugfs_abi_init(); |
| 741 | if (ret) |
| 742 | goto error_abi; |
| 743 | return 0; |
| 744 | error_abi: |
| 745 | kmem_cache_destroy(event_cache); |
| 746 | return ret; |
| 747 | } |
| 748 | |
| 749 | module_init(ltt_events_init); |
| 750 | |
| 751 | static void __exit ltt_events_exit(void) |
| 752 | { |
| 753 | struct ltt_session *session, *tmpsession; |
| 754 | |
| 755 | ltt_debugfs_abi_exit(); |
| 756 | list_for_each_entry_safe(session, tmpsession, &sessions, list) |
| 757 | ltt_session_destroy(session); |
| 758 | kmem_cache_destroy(event_cache); |
| 759 | } |
| 760 | |
| 761 | module_exit(ltt_events_exit); |
| 762 | |
| 763 | MODULE_LICENSE("GPL and additional rights"); |
| 764 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); |
| 765 | MODULE_DESCRIPTION("LTTng Events"); |