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