| 1 | /* |
| 2 | * lttng-events.c |
| 3 | * |
| 4 | * Holds LTTng per-session event registry. |
| 5 | * |
| 6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; only |
| 11 | * version 2.1 of the License. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/jiffies.h> |
| 29 | #include <linux/utsname.h> |
| 30 | #include <linux/err.h> |
| 31 | #include <linux/vmalloc.h> |
| 32 | |
| 33 | #include "wrapper/uuid.h" |
| 34 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
| 35 | #include "wrapper/random.h" |
| 36 | #include "wrapper/tracepoint.h" |
| 37 | #include "lttng-kernel-version.h" |
| 38 | #include "lttng-events.h" |
| 39 | #include "lttng-tracer.h" |
| 40 | #include "lttng-abi-old.h" |
| 41 | |
| 42 | #define METADATA_CACHE_DEFAULT_SIZE 4096 |
| 43 | |
| 44 | static LIST_HEAD(sessions); |
| 45 | static LIST_HEAD(lttng_transport_list); |
| 46 | /* |
| 47 | * Protect the sessions and metadata caches. |
| 48 | */ |
| 49 | static DEFINE_MUTEX(sessions_mutex); |
| 50 | static struct kmem_cache *event_cache; |
| 51 | |
| 52 | static void _lttng_event_destroy(struct lttng_event *event); |
| 53 | static void _lttng_channel_destroy(struct lttng_channel *chan); |
| 54 | static int _lttng_event_unregister(struct lttng_event *event); |
| 55 | static |
| 56 | int _lttng_event_metadata_statedump(struct lttng_session *session, |
| 57 | struct lttng_channel *chan, |
| 58 | struct lttng_event *event); |
| 59 | static |
| 60 | int _lttng_session_metadata_statedump(struct lttng_session *session); |
| 61 | static |
| 62 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream); |
| 63 | |
| 64 | void synchronize_trace(void) |
| 65 | { |
| 66 | synchronize_sched(); |
| 67 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) |
| 68 | #ifdef CONFIG_PREEMPT_RT_FULL |
| 69 | synchronize_rcu(); |
| 70 | #endif |
| 71 | #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */ |
| 72 | #ifdef CONFIG_PREEMPT_RT |
| 73 | synchronize_rcu(); |
| 74 | #endif |
| 75 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */ |
| 76 | } |
| 77 | |
| 78 | struct lttng_session *lttng_session_create(void) |
| 79 | { |
| 80 | struct lttng_session *session; |
| 81 | struct lttng_metadata_cache *metadata_cache; |
| 82 | |
| 83 | mutex_lock(&sessions_mutex); |
| 84 | session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL); |
| 85 | if (!session) |
| 86 | goto err; |
| 87 | INIT_LIST_HEAD(&session->chan); |
| 88 | INIT_LIST_HEAD(&session->events); |
| 89 | uuid_le_gen(&session->uuid); |
| 90 | |
| 91 | metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache), |
| 92 | GFP_KERNEL); |
| 93 | if (!metadata_cache) |
| 94 | goto err_free_session; |
| 95 | metadata_cache->data = vzalloc(METADATA_CACHE_DEFAULT_SIZE); |
| 96 | if (!metadata_cache->data) |
| 97 | goto err_free_cache; |
| 98 | metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE; |
| 99 | kref_init(&metadata_cache->refcount); |
| 100 | mutex_init(&metadata_cache->lock); |
| 101 | session->metadata_cache = metadata_cache; |
| 102 | INIT_LIST_HEAD(&metadata_cache->metadata_stream); |
| 103 | memcpy(&metadata_cache->uuid, &session->uuid, |
| 104 | sizeof(metadata_cache->uuid)); |
| 105 | list_add(&session->list, &sessions); |
| 106 | mutex_unlock(&sessions_mutex); |
| 107 | return session; |
| 108 | |
| 109 | err_free_cache: |
| 110 | kfree(metadata_cache); |
| 111 | err_free_session: |
| 112 | kfree(session); |
| 113 | err: |
| 114 | mutex_unlock(&sessions_mutex); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | void metadata_cache_destroy(struct kref *kref) |
| 119 | { |
| 120 | struct lttng_metadata_cache *cache = |
| 121 | container_of(kref, struct lttng_metadata_cache, refcount); |
| 122 | vfree(cache->data); |
| 123 | kfree(cache); |
| 124 | } |
| 125 | |
| 126 | void lttng_session_destroy(struct lttng_session *session) |
| 127 | { |
| 128 | struct lttng_channel *chan, *tmpchan; |
| 129 | struct lttng_event *event, *tmpevent; |
| 130 | struct lttng_metadata_stream *metadata_stream; |
| 131 | int ret; |
| 132 | |
| 133 | mutex_lock(&sessions_mutex); |
| 134 | ACCESS_ONCE(session->active) = 0; |
| 135 | list_for_each_entry(chan, &session->chan, list) { |
| 136 | ret = lttng_syscalls_unregister(chan); |
| 137 | WARN_ON(ret); |
| 138 | } |
| 139 | list_for_each_entry(event, &session->events, list) { |
| 140 | ret = _lttng_event_unregister(event); |
| 141 | WARN_ON(ret); |
| 142 | } |
| 143 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 144 | list_for_each_entry_safe(event, tmpevent, &session->events, list) |
| 145 | _lttng_event_destroy(event); |
| 146 | list_for_each_entry_safe(chan, tmpchan, &session->chan, list) { |
| 147 | BUG_ON(chan->channel_type == METADATA_CHANNEL); |
| 148 | _lttng_channel_destroy(chan); |
| 149 | } |
| 150 | list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list) |
| 151 | _lttng_metadata_channel_hangup(metadata_stream); |
| 152 | kref_put(&session->metadata_cache->refcount, metadata_cache_destroy); |
| 153 | list_del(&session->list); |
| 154 | mutex_unlock(&sessions_mutex); |
| 155 | kfree(session); |
| 156 | } |
| 157 | |
| 158 | int lttng_session_enable(struct lttng_session *session) |
| 159 | { |
| 160 | int ret = 0; |
| 161 | struct lttng_channel *chan; |
| 162 | |
| 163 | mutex_lock(&sessions_mutex); |
| 164 | if (session->active) { |
| 165 | ret = -EBUSY; |
| 166 | goto end; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Snapshot the number of events per channel to know the type of header |
| 171 | * we need to use. |
| 172 | */ |
| 173 | list_for_each_entry(chan, &session->chan, list) { |
| 174 | if (chan->header_type) |
| 175 | continue; /* don't change it if session stop/restart */ |
| 176 | if (chan->free_event_id < 31) |
| 177 | chan->header_type = 1; /* compact */ |
| 178 | else |
| 179 | chan->header_type = 2; /* large */ |
| 180 | } |
| 181 | |
| 182 | ACCESS_ONCE(session->active) = 1; |
| 183 | ACCESS_ONCE(session->been_active) = 1; |
| 184 | ret = _lttng_session_metadata_statedump(session); |
| 185 | if (ret) { |
| 186 | ACCESS_ONCE(session->active) = 0; |
| 187 | goto end; |
| 188 | } |
| 189 | ret = lttng_statedump_start(session); |
| 190 | if (ret) |
| 191 | ACCESS_ONCE(session->active) = 0; |
| 192 | end: |
| 193 | mutex_unlock(&sessions_mutex); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | int lttng_session_disable(struct lttng_session *session) |
| 198 | { |
| 199 | int ret = 0; |
| 200 | |
| 201 | mutex_lock(&sessions_mutex); |
| 202 | if (!session->active) { |
| 203 | ret = -EBUSY; |
| 204 | goto end; |
| 205 | } |
| 206 | ACCESS_ONCE(session->active) = 0; |
| 207 | end: |
| 208 | mutex_unlock(&sessions_mutex); |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | int lttng_channel_enable(struct lttng_channel *channel) |
| 213 | { |
| 214 | int old; |
| 215 | |
| 216 | if (channel->channel_type == METADATA_CHANNEL) |
| 217 | return -EPERM; |
| 218 | old = xchg(&channel->enabled, 1); |
| 219 | if (old) |
| 220 | return -EEXIST; |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | int lttng_channel_disable(struct lttng_channel *channel) |
| 225 | { |
| 226 | int old; |
| 227 | |
| 228 | if (channel->channel_type == METADATA_CHANNEL) |
| 229 | return -EPERM; |
| 230 | old = xchg(&channel->enabled, 0); |
| 231 | if (!old) |
| 232 | return -EEXIST; |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | int lttng_event_enable(struct lttng_event *event) |
| 237 | { |
| 238 | int old; |
| 239 | |
| 240 | if (event->chan->channel_type == METADATA_CHANNEL) |
| 241 | return -EPERM; |
| 242 | old = xchg(&event->enabled, 1); |
| 243 | if (old) |
| 244 | return -EEXIST; |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | int lttng_event_disable(struct lttng_event *event) |
| 249 | { |
| 250 | int old; |
| 251 | |
| 252 | if (event->chan->channel_type == METADATA_CHANNEL) |
| 253 | return -EPERM; |
| 254 | old = xchg(&event->enabled, 0); |
| 255 | if (!old) |
| 256 | return -EEXIST; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static struct lttng_transport *lttng_transport_find(const char *name) |
| 261 | { |
| 262 | struct lttng_transport *transport; |
| 263 | |
| 264 | list_for_each_entry(transport, <tng_transport_list, node) { |
| 265 | if (!strcmp(transport->name, name)) |
| 266 | return transport; |
| 267 | } |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | struct lttng_channel *lttng_channel_create(struct lttng_session *session, |
| 272 | const char *transport_name, |
| 273 | void *buf_addr, |
| 274 | size_t subbuf_size, size_t num_subbuf, |
| 275 | unsigned int switch_timer_interval, |
| 276 | unsigned int read_timer_interval, |
| 277 | enum channel_type channel_type) |
| 278 | { |
| 279 | struct lttng_channel *chan; |
| 280 | struct lttng_transport *transport = NULL; |
| 281 | |
| 282 | mutex_lock(&sessions_mutex); |
| 283 | if (session->been_active && channel_type != METADATA_CHANNEL) |
| 284 | goto active; /* Refuse to add channel to active session */ |
| 285 | transport = lttng_transport_find(transport_name); |
| 286 | if (!transport) { |
| 287 | printk(KERN_WARNING "LTTng transport %s not found\n", |
| 288 | transport_name); |
| 289 | goto notransport; |
| 290 | } |
| 291 | if (!try_module_get(transport->owner)) { |
| 292 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); |
| 293 | goto notransport; |
| 294 | } |
| 295 | chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL); |
| 296 | if (!chan) |
| 297 | goto nomem; |
| 298 | chan->session = session; |
| 299 | chan->id = session->free_chan_id++; |
| 300 | chan->ops = &transport->ops; |
| 301 | /* |
| 302 | * Note: the channel creation op already writes into the packet |
| 303 | * headers. Therefore the "chan" information used as input |
| 304 | * should be already accessible. |
| 305 | */ |
| 306 | chan->chan = transport->ops.channel_create(transport_name, |
| 307 | chan, buf_addr, subbuf_size, num_subbuf, |
| 308 | switch_timer_interval, read_timer_interval); |
| 309 | if (!chan->chan) |
| 310 | goto create_error; |
| 311 | chan->enabled = 1; |
| 312 | chan->transport = transport; |
| 313 | chan->channel_type = channel_type; |
| 314 | list_add(&chan->list, &session->chan); |
| 315 | mutex_unlock(&sessions_mutex); |
| 316 | return chan; |
| 317 | |
| 318 | create_error: |
| 319 | kfree(chan); |
| 320 | nomem: |
| 321 | if (transport) |
| 322 | module_put(transport->owner); |
| 323 | notransport: |
| 324 | active: |
| 325 | mutex_unlock(&sessions_mutex); |
| 326 | return NULL; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Only used internally at session destruction for per-cpu channels, and |
| 331 | * when metadata channel is released. |
| 332 | * Needs to be called with sessions mutex held. |
| 333 | */ |
| 334 | static |
| 335 | void _lttng_channel_destroy(struct lttng_channel *chan) |
| 336 | { |
| 337 | chan->ops->channel_destroy(chan->chan); |
| 338 | module_put(chan->transport->owner); |
| 339 | list_del(&chan->list); |
| 340 | lttng_destroy_context(chan->ctx); |
| 341 | kfree(chan); |
| 342 | } |
| 343 | |
| 344 | void lttng_metadata_channel_destroy(struct lttng_channel *chan) |
| 345 | { |
| 346 | BUG_ON(chan->channel_type != METADATA_CHANNEL); |
| 347 | |
| 348 | /* Protect the metadata cache with the sessions_mutex. */ |
| 349 | mutex_lock(&sessions_mutex); |
| 350 | _lttng_channel_destroy(chan); |
| 351 | mutex_unlock(&sessions_mutex); |
| 352 | } |
| 353 | EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy); |
| 354 | |
| 355 | static |
| 356 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream) |
| 357 | { |
| 358 | stream->finalized = 1; |
| 359 | wake_up_interruptible(&stream->read_wait); |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Supports event creation while tracing session is active. |
| 364 | */ |
| 365 | struct lttng_event *lttng_event_create(struct lttng_channel *chan, |
| 366 | struct lttng_kernel_event *event_param, |
| 367 | void *filter, |
| 368 | const struct lttng_event_desc *internal_desc) |
| 369 | { |
| 370 | struct lttng_event *event; |
| 371 | int ret; |
| 372 | |
| 373 | mutex_lock(&sessions_mutex); |
| 374 | if (chan->free_event_id == -1U) { |
| 375 | ret = -EMFILE; |
| 376 | goto full; |
| 377 | } |
| 378 | /* |
| 379 | * This is O(n^2) (for each event, the loop is called at event |
| 380 | * creation). Might require a hash if we have lots of events. |
| 381 | */ |
| 382 | list_for_each_entry(event, &chan->session->events, list) { |
| 383 | if (!strcmp(event->desc->name, event_param->name)) { |
| 384 | ret = -EEXIST; |
| 385 | goto exist; |
| 386 | } |
| 387 | } |
| 388 | event = kmem_cache_zalloc(event_cache, GFP_KERNEL); |
| 389 | if (!event) { |
| 390 | ret = -ENOMEM; |
| 391 | goto cache_error; |
| 392 | } |
| 393 | event->chan = chan; |
| 394 | event->filter = filter; |
| 395 | event->id = chan->free_event_id++; |
| 396 | event->enabled = 1; |
| 397 | event->instrumentation = event_param->instrumentation; |
| 398 | /* Populate lttng_event structure before tracepoint registration. */ |
| 399 | smp_wmb(); |
| 400 | switch (event_param->instrumentation) { |
| 401 | case LTTNG_KERNEL_TRACEPOINT: |
| 402 | event->desc = lttng_event_get(event_param->name); |
| 403 | if (!event->desc) { |
| 404 | ret = -ENOENT; |
| 405 | goto register_error; |
| 406 | } |
| 407 | ret = lttng_wrapper_tracepoint_probe_register(event->desc->kname, |
| 408 | event->desc->probe_callback, |
| 409 | event); |
| 410 | if (ret) { |
| 411 | ret = -EINVAL; |
| 412 | goto register_error; |
| 413 | } |
| 414 | break; |
| 415 | case LTTNG_KERNEL_KPROBE: |
| 416 | ret = lttng_kprobes_register(event_param->name, |
| 417 | event_param->u.kprobe.symbol_name, |
| 418 | event_param->u.kprobe.offset, |
| 419 | event_param->u.kprobe.addr, |
| 420 | event); |
| 421 | if (ret) { |
| 422 | ret = -EINVAL; |
| 423 | goto register_error; |
| 424 | } |
| 425 | ret = try_module_get(event->desc->owner); |
| 426 | WARN_ON_ONCE(!ret); |
| 427 | break; |
| 428 | case LTTNG_KERNEL_KRETPROBE: |
| 429 | { |
| 430 | struct lttng_event *event_return; |
| 431 | |
| 432 | /* kretprobe defines 2 events */ |
| 433 | event_return = |
| 434 | kmem_cache_zalloc(event_cache, GFP_KERNEL); |
| 435 | if (!event_return) { |
| 436 | ret = -ENOMEM; |
| 437 | goto register_error; |
| 438 | } |
| 439 | event_return->chan = chan; |
| 440 | event_return->filter = filter; |
| 441 | event_return->id = chan->free_event_id++; |
| 442 | event_return->enabled = 1; |
| 443 | event_return->instrumentation = event_param->instrumentation; |
| 444 | /* |
| 445 | * Populate lttng_event structure before kretprobe registration. |
| 446 | */ |
| 447 | smp_wmb(); |
| 448 | ret = lttng_kretprobes_register(event_param->name, |
| 449 | event_param->u.kretprobe.symbol_name, |
| 450 | event_param->u.kretprobe.offset, |
| 451 | event_param->u.kretprobe.addr, |
| 452 | event, event_return); |
| 453 | if (ret) { |
| 454 | kmem_cache_free(event_cache, event_return); |
| 455 | ret = -EINVAL; |
| 456 | goto register_error; |
| 457 | } |
| 458 | /* Take 2 refs on the module: one per event. */ |
| 459 | ret = try_module_get(event->desc->owner); |
| 460 | WARN_ON_ONCE(!ret); |
| 461 | ret = try_module_get(event->desc->owner); |
| 462 | WARN_ON_ONCE(!ret); |
| 463 | ret = _lttng_event_metadata_statedump(chan->session, chan, |
| 464 | event_return); |
| 465 | WARN_ON_ONCE(ret > 0); |
| 466 | if (ret) { |
| 467 | kmem_cache_free(event_cache, event_return); |
| 468 | module_put(event->desc->owner); |
| 469 | module_put(event->desc->owner); |
| 470 | goto statedump_error; |
| 471 | } |
| 472 | list_add(&event_return->list, &chan->session->events); |
| 473 | break; |
| 474 | } |
| 475 | case LTTNG_KERNEL_FUNCTION: |
| 476 | ret = lttng_ftrace_register(event_param->name, |
| 477 | event_param->u.ftrace.symbol_name, |
| 478 | event); |
| 479 | if (ret) { |
| 480 | goto register_error; |
| 481 | } |
| 482 | ret = try_module_get(event->desc->owner); |
| 483 | WARN_ON_ONCE(!ret); |
| 484 | break; |
| 485 | case LTTNG_KERNEL_NOOP: |
| 486 | event->desc = internal_desc; |
| 487 | if (!event->desc) { |
| 488 | ret = -EINVAL; |
| 489 | goto register_error; |
| 490 | } |
| 491 | break; |
| 492 | default: |
| 493 | WARN_ON_ONCE(1); |
| 494 | ret = -EINVAL; |
| 495 | goto register_error; |
| 496 | } |
| 497 | ret = _lttng_event_metadata_statedump(chan->session, chan, event); |
| 498 | WARN_ON_ONCE(ret > 0); |
| 499 | if (ret) { |
| 500 | goto statedump_error; |
| 501 | } |
| 502 | list_add(&event->list, &chan->session->events); |
| 503 | mutex_unlock(&sessions_mutex); |
| 504 | return event; |
| 505 | |
| 506 | statedump_error: |
| 507 | /* If a statedump error occurs, events will not be readable. */ |
| 508 | register_error: |
| 509 | kmem_cache_free(event_cache, event); |
| 510 | cache_error: |
| 511 | exist: |
| 512 | full: |
| 513 | mutex_unlock(&sessions_mutex); |
| 514 | return ERR_PTR(ret); |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Only used internally at session destruction. |
| 519 | */ |
| 520 | int _lttng_event_unregister(struct lttng_event *event) |
| 521 | { |
| 522 | int ret = -EINVAL; |
| 523 | |
| 524 | switch (event->instrumentation) { |
| 525 | case LTTNG_KERNEL_TRACEPOINT: |
| 526 | ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname, |
| 527 | event->desc->probe_callback, |
| 528 | event); |
| 529 | if (ret) |
| 530 | return ret; |
| 531 | break; |
| 532 | case LTTNG_KERNEL_KPROBE: |
| 533 | lttng_kprobes_unregister(event); |
| 534 | ret = 0; |
| 535 | break; |
| 536 | case LTTNG_KERNEL_KRETPROBE: |
| 537 | lttng_kretprobes_unregister(event); |
| 538 | ret = 0; |
| 539 | break; |
| 540 | case LTTNG_KERNEL_FUNCTION: |
| 541 | lttng_ftrace_unregister(event); |
| 542 | ret = 0; |
| 543 | break; |
| 544 | case LTTNG_KERNEL_NOOP: |
| 545 | ret = 0; |
| 546 | break; |
| 547 | default: |
| 548 | WARN_ON_ONCE(1); |
| 549 | } |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Only used internally at session destruction. |
| 555 | */ |
| 556 | static |
| 557 | void _lttng_event_destroy(struct lttng_event *event) |
| 558 | { |
| 559 | switch (event->instrumentation) { |
| 560 | case LTTNG_KERNEL_TRACEPOINT: |
| 561 | lttng_event_put(event->desc); |
| 562 | break; |
| 563 | case LTTNG_KERNEL_KPROBE: |
| 564 | module_put(event->desc->owner); |
| 565 | lttng_kprobes_destroy_private(event); |
| 566 | break; |
| 567 | case LTTNG_KERNEL_KRETPROBE: |
| 568 | module_put(event->desc->owner); |
| 569 | lttng_kretprobes_destroy_private(event); |
| 570 | break; |
| 571 | case LTTNG_KERNEL_FUNCTION: |
| 572 | module_put(event->desc->owner); |
| 573 | lttng_ftrace_destroy_private(event); |
| 574 | break; |
| 575 | case LTTNG_KERNEL_NOOP: |
| 576 | break; |
| 577 | default: |
| 578 | WARN_ON_ONCE(1); |
| 579 | } |
| 580 | list_del(&event->list); |
| 581 | lttng_destroy_context(event->ctx); |
| 582 | kmem_cache_free(event_cache, event); |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * Serialize at most one packet worth of metadata into a metadata |
| 587 | * channel. |
| 588 | * We grab the metadata cache mutex to get exclusive access to our metadata |
| 589 | * buffer and to the metadata cache. Exclusive access to the metadata buffer |
| 590 | * allows us to do racy operations such as looking for remaining space left in |
| 591 | * packet and write, since mutual exclusion protects us from concurrent writes. |
| 592 | * Mutual exclusion on the metadata cache allow us to read the cache content |
| 593 | * without racing against reallocation of the cache by updates. |
| 594 | * Returns the number of bytes written in the channel, 0 if no data |
| 595 | * was written and a negative value on error. |
| 596 | */ |
| 597 | int lttng_metadata_output_channel(struct lttng_metadata_stream *stream, |
| 598 | struct channel *chan) |
| 599 | { |
| 600 | struct lib_ring_buffer_ctx ctx; |
| 601 | int ret = 0; |
| 602 | size_t len, reserve_len; |
| 603 | |
| 604 | /* |
| 605 | * Ensure we support mutiple get_next / put sequences followed by |
| 606 | * put_next. The metadata cache lock protects reading the metadata |
| 607 | * cache. It can indeed be read concurrently by "get_next_subbuf" and |
| 608 | * "flush" operations on the buffer invoked by different processes. |
| 609 | * Moreover, since the metadata cache memory can be reallocated, we |
| 610 | * need to have exclusive access against updates even though we only |
| 611 | * read it. |
| 612 | */ |
| 613 | mutex_lock(&stream->metadata_cache->lock); |
| 614 | WARN_ON(stream->metadata_in < stream->metadata_out); |
| 615 | if (stream->metadata_in != stream->metadata_out) |
| 616 | goto end; |
| 617 | |
| 618 | len = stream->metadata_cache->metadata_written - |
| 619 | stream->metadata_in; |
| 620 | if (!len) |
| 621 | goto end; |
| 622 | reserve_len = min_t(size_t, |
| 623 | stream->transport->ops.packet_avail_size(chan), |
| 624 | len); |
| 625 | lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len, |
| 626 | sizeof(char), -1); |
| 627 | /* |
| 628 | * If reservation failed, return an error to the caller. |
| 629 | */ |
| 630 | ret = stream->transport->ops.event_reserve(&ctx, 0); |
| 631 | if (ret != 0) { |
| 632 | printk(KERN_WARNING "LTTng: Metadata event reservation failed\n"); |
| 633 | goto end; |
| 634 | } |
| 635 | stream->transport->ops.event_write(&ctx, |
| 636 | stream->metadata_cache->data + stream->metadata_in, |
| 637 | reserve_len); |
| 638 | stream->transport->ops.event_commit(&ctx); |
| 639 | stream->metadata_in += reserve_len; |
| 640 | ret = reserve_len; |
| 641 | |
| 642 | end: |
| 643 | mutex_unlock(&stream->metadata_cache->lock); |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * Write the metadata to the metadata cache. |
| 649 | * Must be called with sessions_mutex held. |
| 650 | * The metadata cache lock protects us from concurrent read access from |
| 651 | * thread outputting metadata content to ring buffer. |
| 652 | */ |
| 653 | int lttng_metadata_printf(struct lttng_session *session, |
| 654 | const char *fmt, ...) |
| 655 | { |
| 656 | char *str; |
| 657 | size_t len; |
| 658 | va_list ap; |
| 659 | struct lttng_metadata_stream *stream; |
| 660 | |
| 661 | WARN_ON_ONCE(!ACCESS_ONCE(session->active)); |
| 662 | |
| 663 | va_start(ap, fmt); |
| 664 | str = kvasprintf(GFP_KERNEL, fmt, ap); |
| 665 | va_end(ap); |
| 666 | if (!str) |
| 667 | return -ENOMEM; |
| 668 | |
| 669 | len = strlen(str); |
| 670 | mutex_lock(&session->metadata_cache->lock); |
| 671 | if (session->metadata_cache->metadata_written + len > |
| 672 | session->metadata_cache->cache_alloc) { |
| 673 | char *tmp_cache_realloc; |
| 674 | unsigned int tmp_cache_alloc_size; |
| 675 | |
| 676 | tmp_cache_alloc_size = max_t(unsigned int, |
| 677 | session->metadata_cache->cache_alloc + len, |
| 678 | session->metadata_cache->cache_alloc << 1); |
| 679 | tmp_cache_realloc = vzalloc(tmp_cache_alloc_size); |
| 680 | if (!tmp_cache_realloc) |
| 681 | goto err; |
| 682 | if (session->metadata_cache->data) { |
| 683 | memcpy(tmp_cache_realloc, |
| 684 | session->metadata_cache->data, |
| 685 | session->metadata_cache->cache_alloc); |
| 686 | vfree(session->metadata_cache->data); |
| 687 | } |
| 688 | |
| 689 | session->metadata_cache->cache_alloc = tmp_cache_alloc_size; |
| 690 | session->metadata_cache->data = tmp_cache_realloc; |
| 691 | } |
| 692 | memcpy(session->metadata_cache->data + |
| 693 | session->metadata_cache->metadata_written, |
| 694 | str, len); |
| 695 | session->metadata_cache->metadata_written += len; |
| 696 | mutex_unlock(&session->metadata_cache->lock); |
| 697 | kfree(str); |
| 698 | |
| 699 | list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) |
| 700 | wake_up_interruptible(&stream->read_wait); |
| 701 | |
| 702 | return 0; |
| 703 | |
| 704 | err: |
| 705 | mutex_unlock(&session->metadata_cache->lock); |
| 706 | kfree(str); |
| 707 | return -ENOMEM; |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Must be called with sessions_mutex held. |
| 712 | */ |
| 713 | static |
| 714 | int _lttng_field_statedump(struct lttng_session *session, |
| 715 | const struct lttng_event_field *field) |
| 716 | { |
| 717 | int ret = 0; |
| 718 | |
| 719 | switch (field->type.atype) { |
| 720 | case atype_integer: |
| 721 | ret = lttng_metadata_printf(session, |
| 722 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
| 723 | field->type.u.basic.integer.size, |
| 724 | field->type.u.basic.integer.alignment, |
| 725 | field->type.u.basic.integer.signedness, |
| 726 | (field->type.u.basic.integer.encoding == lttng_encode_none) |
| 727 | ? "none" |
| 728 | : (field->type.u.basic.integer.encoding == lttng_encode_UTF8) |
| 729 | ? "UTF8" |
| 730 | : "ASCII", |
| 731 | field->type.u.basic.integer.base, |
| 732 | #ifdef __BIG_ENDIAN |
| 733 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 734 | #else |
| 735 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 736 | #endif |
| 737 | field->name); |
| 738 | break; |
| 739 | case atype_enum: |
| 740 | ret = lttng_metadata_printf(session, |
| 741 | " %s _%s;\n", |
| 742 | field->type.u.basic.enumeration.name, |
| 743 | field->name); |
| 744 | break; |
| 745 | case atype_array: |
| 746 | { |
| 747 | const struct lttng_basic_type *elem_type; |
| 748 | |
| 749 | elem_type = &field->type.u.array.elem_type; |
| 750 | ret = lttng_metadata_printf(session, |
| 751 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
| 752 | elem_type->u.basic.integer.size, |
| 753 | elem_type->u.basic.integer.alignment, |
| 754 | elem_type->u.basic.integer.signedness, |
| 755 | (elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 756 | ? "none" |
| 757 | : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 758 | ? "UTF8" |
| 759 | : "ASCII", |
| 760 | elem_type->u.basic.integer.base, |
| 761 | #ifdef __BIG_ENDIAN |
| 762 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 763 | #else |
| 764 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 765 | #endif |
| 766 | field->name, field->type.u.array.length); |
| 767 | break; |
| 768 | } |
| 769 | case atype_sequence: |
| 770 | { |
| 771 | const struct lttng_basic_type *elem_type; |
| 772 | const struct lttng_basic_type *length_type; |
| 773 | |
| 774 | elem_type = &field->type.u.sequence.elem_type; |
| 775 | length_type = &field->type.u.sequence.length_type; |
| 776 | ret = lttng_metadata_printf(session, |
| 777 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", |
| 778 | length_type->u.basic.integer.size, |
| 779 | (unsigned int) length_type->u.basic.integer.alignment, |
| 780 | length_type->u.basic.integer.signedness, |
| 781 | (length_type->u.basic.integer.encoding == lttng_encode_none) |
| 782 | ? "none" |
| 783 | : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 784 | ? "UTF8" |
| 785 | : "ASCII"), |
| 786 | length_type->u.basic.integer.base, |
| 787 | #ifdef __BIG_ENDIAN |
| 788 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 789 | #else |
| 790 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 791 | #endif |
| 792 | field->name); |
| 793 | if (ret) |
| 794 | return ret; |
| 795 | |
| 796 | ret = lttng_metadata_printf(session, |
| 797 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
| 798 | elem_type->u.basic.integer.size, |
| 799 | (unsigned int) elem_type->u.basic.integer.alignment, |
| 800 | elem_type->u.basic.integer.signedness, |
| 801 | (elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 802 | ? "none" |
| 803 | : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 804 | ? "UTF8" |
| 805 | : "ASCII"), |
| 806 | elem_type->u.basic.integer.base, |
| 807 | #ifdef __BIG_ENDIAN |
| 808 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 809 | #else |
| 810 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 811 | #endif |
| 812 | field->name, |
| 813 | field->name); |
| 814 | break; |
| 815 | } |
| 816 | |
| 817 | case atype_string: |
| 818 | /* Default encoding is UTF8 */ |
| 819 | ret = lttng_metadata_printf(session, |
| 820 | " string%s _%s;\n", |
| 821 | field->type.u.basic.string.encoding == lttng_encode_ASCII ? |
| 822 | " { encoding = ASCII; }" : "", |
| 823 | field->name); |
| 824 | break; |
| 825 | default: |
| 826 | WARN_ON_ONCE(1); |
| 827 | return -EINVAL; |
| 828 | } |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | static |
| 833 | int _lttng_context_metadata_statedump(struct lttng_session *session, |
| 834 | struct lttng_ctx *ctx) |
| 835 | { |
| 836 | int ret = 0; |
| 837 | int i; |
| 838 | |
| 839 | if (!ctx) |
| 840 | return 0; |
| 841 | for (i = 0; i < ctx->nr_fields; i++) { |
| 842 | const struct lttng_ctx_field *field = &ctx->fields[i]; |
| 843 | |
| 844 | ret = _lttng_field_statedump(session, &field->event_field); |
| 845 | if (ret) |
| 846 | return ret; |
| 847 | } |
| 848 | return ret; |
| 849 | } |
| 850 | |
| 851 | static |
| 852 | int _lttng_fields_metadata_statedump(struct lttng_session *session, |
| 853 | struct lttng_event *event) |
| 854 | { |
| 855 | const struct lttng_event_desc *desc = event->desc; |
| 856 | int ret = 0; |
| 857 | int i; |
| 858 | |
| 859 | for (i = 0; i < desc->nr_fields; i++) { |
| 860 | const struct lttng_event_field *field = &desc->fields[i]; |
| 861 | |
| 862 | ret = _lttng_field_statedump(session, field); |
| 863 | if (ret) |
| 864 | return ret; |
| 865 | } |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Must be called with sessions_mutex held. |
| 871 | */ |
| 872 | static |
| 873 | int _lttng_event_metadata_statedump(struct lttng_session *session, |
| 874 | struct lttng_channel *chan, |
| 875 | struct lttng_event *event) |
| 876 | { |
| 877 | int ret = 0; |
| 878 | |
| 879 | if (event->metadata_dumped || !ACCESS_ONCE(session->active)) |
| 880 | return 0; |
| 881 | if (chan->channel_type == METADATA_CHANNEL) |
| 882 | return 0; |
| 883 | |
| 884 | ret = lttng_metadata_printf(session, |
| 885 | "event {\n" |
| 886 | " name = \"%s\";\n" |
| 887 | " id = %u;\n" |
| 888 | " stream_id = %u;\n", |
| 889 | event->desc->name, |
| 890 | event->id, |
| 891 | event->chan->id); |
| 892 | if (ret) |
| 893 | goto end; |
| 894 | |
| 895 | if (event->ctx) { |
| 896 | ret = lttng_metadata_printf(session, |
| 897 | " context := struct {\n"); |
| 898 | if (ret) |
| 899 | goto end; |
| 900 | } |
| 901 | ret = _lttng_context_metadata_statedump(session, event->ctx); |
| 902 | if (ret) |
| 903 | goto end; |
| 904 | if (event->ctx) { |
| 905 | ret = lttng_metadata_printf(session, |
| 906 | " };\n"); |
| 907 | if (ret) |
| 908 | goto end; |
| 909 | } |
| 910 | |
| 911 | ret = lttng_metadata_printf(session, |
| 912 | " fields := struct {\n" |
| 913 | ); |
| 914 | if (ret) |
| 915 | goto end; |
| 916 | |
| 917 | ret = _lttng_fields_metadata_statedump(session, event); |
| 918 | if (ret) |
| 919 | goto end; |
| 920 | |
| 921 | /* |
| 922 | * LTTng space reservation can only reserve multiples of the |
| 923 | * byte size. |
| 924 | */ |
| 925 | ret = lttng_metadata_printf(session, |
| 926 | " };\n" |
| 927 | "};\n\n"); |
| 928 | if (ret) |
| 929 | goto end; |
| 930 | |
| 931 | event->metadata_dumped = 1; |
| 932 | end: |
| 933 | return ret; |
| 934 | |
| 935 | } |
| 936 | |
| 937 | /* |
| 938 | * Must be called with sessions_mutex held. |
| 939 | */ |
| 940 | static |
| 941 | int _lttng_channel_metadata_statedump(struct lttng_session *session, |
| 942 | struct lttng_channel *chan) |
| 943 | { |
| 944 | int ret = 0; |
| 945 | |
| 946 | if (chan->metadata_dumped || !ACCESS_ONCE(session->active)) |
| 947 | return 0; |
| 948 | |
| 949 | if (chan->channel_type == METADATA_CHANNEL) |
| 950 | return 0; |
| 951 | |
| 952 | WARN_ON_ONCE(!chan->header_type); |
| 953 | ret = lttng_metadata_printf(session, |
| 954 | "stream {\n" |
| 955 | " id = %u;\n" |
| 956 | " event.header := %s;\n" |
| 957 | " packet.context := struct packet_context;\n", |
| 958 | chan->id, |
| 959 | chan->header_type == 1 ? "struct event_header_compact" : |
| 960 | "struct event_header_large"); |
| 961 | if (ret) |
| 962 | goto end; |
| 963 | |
| 964 | if (chan->ctx) { |
| 965 | ret = lttng_metadata_printf(session, |
| 966 | " event.context := struct {\n"); |
| 967 | if (ret) |
| 968 | goto end; |
| 969 | } |
| 970 | ret = _lttng_context_metadata_statedump(session, chan->ctx); |
| 971 | if (ret) |
| 972 | goto end; |
| 973 | if (chan->ctx) { |
| 974 | ret = lttng_metadata_printf(session, |
| 975 | " };\n"); |
| 976 | if (ret) |
| 977 | goto end; |
| 978 | } |
| 979 | |
| 980 | ret = lttng_metadata_printf(session, |
| 981 | "};\n\n"); |
| 982 | |
| 983 | chan->metadata_dumped = 1; |
| 984 | end: |
| 985 | return ret; |
| 986 | } |
| 987 | |
| 988 | /* |
| 989 | * Must be called with sessions_mutex held. |
| 990 | */ |
| 991 | static |
| 992 | int _lttng_stream_packet_context_declare(struct lttng_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 | " uint64_t content_size;\n" |
| 999 | " uint64_t packet_size;\n" |
| 1000 | " unsigned long events_discarded;\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 | * Must be called with sessions_mutex held. |
| 1016 | */ |
| 1017 | static |
| 1018 | int _lttng_event_header_declare(struct lttng_session *session) |
| 1019 | { |
| 1020 | return lttng_metadata_printf(session, |
| 1021 | "struct event_header_compact {\n" |
| 1022 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" |
| 1023 | " variant <id> {\n" |
| 1024 | " struct {\n" |
| 1025 | " uint27_clock_monotonic_t timestamp;\n" |
| 1026 | " } compact;\n" |
| 1027 | " struct {\n" |
| 1028 | " uint32_t id;\n" |
| 1029 | " uint64_clock_monotonic_t timestamp;\n" |
| 1030 | " } extended;\n" |
| 1031 | " } v;\n" |
| 1032 | "} align(%u);\n" |
| 1033 | "\n" |
| 1034 | "struct event_header_large {\n" |
| 1035 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" |
| 1036 | " variant <id> {\n" |
| 1037 | " struct {\n" |
| 1038 | " uint32_clock_monotonic_t timestamp;\n" |
| 1039 | " } compact;\n" |
| 1040 | " struct {\n" |
| 1041 | " uint32_t id;\n" |
| 1042 | " uint64_clock_monotonic_t timestamp;\n" |
| 1043 | " } extended;\n" |
| 1044 | " } v;\n" |
| 1045 | "} align(%u);\n\n", |
| 1046 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 1047 | lttng_alignof(uint16_t) * CHAR_BIT |
| 1048 | ); |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * Approximation of NTP time of day to clock monotonic correlation, |
| 1053 | * taken at start of trace. |
| 1054 | * Yes, this is only an approximation. Yes, we can (and will) do better |
| 1055 | * in future versions. |
| 1056 | */ |
| 1057 | static |
| 1058 | uint64_t measure_clock_offset(void) |
| 1059 | { |
| 1060 | uint64_t offset, monotonic[2], realtime; |
| 1061 | struct timespec rts = { 0, 0 }; |
| 1062 | unsigned long flags; |
| 1063 | |
| 1064 | /* Disable interrupts to increase correlation precision. */ |
| 1065 | local_irq_save(flags); |
| 1066 | monotonic[0] = trace_clock_read64(); |
| 1067 | getnstimeofday(&rts); |
| 1068 | monotonic[1] = trace_clock_read64(); |
| 1069 | local_irq_restore(flags); |
| 1070 | |
| 1071 | offset = (monotonic[0] + monotonic[1]) >> 1; |
| 1072 | realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC; |
| 1073 | realtime += rts.tv_nsec; |
| 1074 | offset = realtime - offset; |
| 1075 | return offset; |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * Output metadata into this session's metadata buffers. |
| 1080 | * Must be called with sessions_mutex held. |
| 1081 | */ |
| 1082 | static |
| 1083 | int _lttng_session_metadata_statedump(struct lttng_session *session) |
| 1084 | { |
| 1085 | unsigned char *uuid_c = session->uuid.b; |
| 1086 | unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN]; |
| 1087 | struct lttng_channel *chan; |
| 1088 | struct lttng_event *event; |
| 1089 | int ret = 0; |
| 1090 | |
| 1091 | if (!ACCESS_ONCE(session->active)) |
| 1092 | return 0; |
| 1093 | if (session->metadata_dumped) |
| 1094 | goto skip_session; |
| 1095 | |
| 1096 | snprintf(uuid_s, sizeof(uuid_s), |
| 1097 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 1098 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], |
| 1099 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], |
| 1100 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], |
| 1101 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); |
| 1102 | |
| 1103 | ret = lttng_metadata_printf(session, |
| 1104 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
| 1105 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" |
| 1106 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" |
| 1107 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" |
| 1108 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" |
| 1109 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" |
| 1110 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" |
| 1111 | "\n" |
| 1112 | "trace {\n" |
| 1113 | " major = %u;\n" |
| 1114 | " minor = %u;\n" |
| 1115 | " uuid = \"%s\";\n" |
| 1116 | " byte_order = %s;\n" |
| 1117 | " packet.header := struct {\n" |
| 1118 | " uint32_t magic;\n" |
| 1119 | " uint8_t uuid[16];\n" |
| 1120 | " uint32_t stream_id;\n" |
| 1121 | " };\n" |
| 1122 | "};\n\n", |
| 1123 | lttng_alignof(uint8_t) * CHAR_BIT, |
| 1124 | lttng_alignof(uint16_t) * CHAR_BIT, |
| 1125 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 1126 | lttng_alignof(uint64_t) * CHAR_BIT, |
| 1127 | sizeof(unsigned long) * CHAR_BIT, |
| 1128 | lttng_alignof(unsigned long) * CHAR_BIT, |
| 1129 | CTF_SPEC_MAJOR, |
| 1130 | CTF_SPEC_MINOR, |
| 1131 | uuid_s, |
| 1132 | #ifdef __BIG_ENDIAN |
| 1133 | "be" |
| 1134 | #else |
| 1135 | "le" |
| 1136 | #endif |
| 1137 | ); |
| 1138 | if (ret) |
| 1139 | goto end; |
| 1140 | |
| 1141 | ret = lttng_metadata_printf(session, |
| 1142 | "env {\n" |
| 1143 | " hostname = \"%s\";\n" |
| 1144 | " domain = \"kernel\";\n" |
| 1145 | " sysname = \"%s\";\n" |
| 1146 | " kernel_release = \"%s\";\n" |
| 1147 | " kernel_version = \"%s\";\n" |
| 1148 | " tracer_name = \"lttng-modules\";\n" |
| 1149 | " tracer_major = %d;\n" |
| 1150 | " tracer_minor = %d;\n" |
| 1151 | " tracer_patchlevel = %d;\n" |
| 1152 | "};\n\n", |
| 1153 | current->nsproxy->uts_ns->name.nodename, |
| 1154 | utsname()->sysname, |
| 1155 | utsname()->release, |
| 1156 | utsname()->version, |
| 1157 | LTTNG_MODULES_MAJOR_VERSION, |
| 1158 | LTTNG_MODULES_MINOR_VERSION, |
| 1159 | LTTNG_MODULES_PATCHLEVEL_VERSION |
| 1160 | ); |
| 1161 | if (ret) |
| 1162 | goto end; |
| 1163 | |
| 1164 | ret = lttng_metadata_printf(session, |
| 1165 | "clock {\n" |
| 1166 | " name = %s;\n", |
| 1167 | "monotonic" |
| 1168 | ); |
| 1169 | if (ret) |
| 1170 | goto end; |
| 1171 | |
| 1172 | if (!trace_clock_uuid(clock_uuid_s)) { |
| 1173 | ret = lttng_metadata_printf(session, |
| 1174 | " uuid = \"%s\";\n", |
| 1175 | clock_uuid_s |
| 1176 | ); |
| 1177 | if (ret) |
| 1178 | goto end; |
| 1179 | } |
| 1180 | |
| 1181 | ret = lttng_metadata_printf(session, |
| 1182 | " description = \"Monotonic Clock\";\n" |
| 1183 | " freq = %llu; /* Frequency, in Hz */\n" |
| 1184 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" |
| 1185 | " offset = %llu;\n" |
| 1186 | "};\n\n", |
| 1187 | (unsigned long long) trace_clock_freq(), |
| 1188 | (unsigned long long) measure_clock_offset() |
| 1189 | ); |
| 1190 | if (ret) |
| 1191 | goto end; |
| 1192 | |
| 1193 | ret = lttng_metadata_printf(session, |
| 1194 | "typealias integer {\n" |
| 1195 | " size = 27; align = 1; signed = false;\n" |
| 1196 | " map = clock.monotonic.value;\n" |
| 1197 | "} := uint27_clock_monotonic_t;\n" |
| 1198 | "\n" |
| 1199 | "typealias integer {\n" |
| 1200 | " size = 32; align = %u; signed = false;\n" |
| 1201 | " map = clock.monotonic.value;\n" |
| 1202 | "} := uint32_clock_monotonic_t;\n" |
| 1203 | "\n" |
| 1204 | "typealias integer {\n" |
| 1205 | " size = 64; align = %u; signed = false;\n" |
| 1206 | " map = clock.monotonic.value;\n" |
| 1207 | "} := uint64_clock_monotonic_t;\n\n", |
| 1208 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 1209 | lttng_alignof(uint64_t) * CHAR_BIT |
| 1210 | ); |
| 1211 | if (ret) |
| 1212 | goto end; |
| 1213 | |
| 1214 | ret = _lttng_stream_packet_context_declare(session); |
| 1215 | if (ret) |
| 1216 | goto end; |
| 1217 | |
| 1218 | ret = _lttng_event_header_declare(session); |
| 1219 | if (ret) |
| 1220 | goto end; |
| 1221 | |
| 1222 | skip_session: |
| 1223 | list_for_each_entry(chan, &session->chan, list) { |
| 1224 | ret = _lttng_channel_metadata_statedump(session, chan); |
| 1225 | if (ret) |
| 1226 | goto end; |
| 1227 | } |
| 1228 | |
| 1229 | list_for_each_entry(event, &session->events, list) { |
| 1230 | ret = _lttng_event_metadata_statedump(session, event->chan, event); |
| 1231 | if (ret) |
| 1232 | goto end; |
| 1233 | } |
| 1234 | session->metadata_dumped = 1; |
| 1235 | end: |
| 1236 | return ret; |
| 1237 | } |
| 1238 | |
| 1239 | /** |
| 1240 | * lttng_transport_register - LTT transport registration |
| 1241 | * @transport: transport structure |
| 1242 | * |
| 1243 | * Registers a transport which can be used as output to extract the data out of |
| 1244 | * LTTng. The module calling this registration function must ensure that no |
| 1245 | * trap-inducing code will be executed by the transport functions. E.g. |
| 1246 | * vmalloc_sync_all() must be called between a vmalloc and the moment the memory |
| 1247 | * is made visible to the transport function. This registration acts as a |
| 1248 | * vmalloc_sync_all. Therefore, only if the module allocates virtual memory |
| 1249 | * after its registration must it synchronize the TLBs. |
| 1250 | */ |
| 1251 | void lttng_transport_register(struct lttng_transport *transport) |
| 1252 | { |
| 1253 | /* |
| 1254 | * Make sure no page fault can be triggered by the module about to be |
| 1255 | * registered. We deal with this here so we don't have to call |
| 1256 | * vmalloc_sync_all() in each module's init. |
| 1257 | */ |
| 1258 | wrapper_vmalloc_sync_all(); |
| 1259 | |
| 1260 | mutex_lock(&sessions_mutex); |
| 1261 | list_add_tail(&transport->node, <tng_transport_list); |
| 1262 | mutex_unlock(&sessions_mutex); |
| 1263 | } |
| 1264 | EXPORT_SYMBOL_GPL(lttng_transport_register); |
| 1265 | |
| 1266 | /** |
| 1267 | * lttng_transport_unregister - LTT transport unregistration |
| 1268 | * @transport: transport structure |
| 1269 | */ |
| 1270 | void lttng_transport_unregister(struct lttng_transport *transport) |
| 1271 | { |
| 1272 | mutex_lock(&sessions_mutex); |
| 1273 | list_del(&transport->node); |
| 1274 | mutex_unlock(&sessions_mutex); |
| 1275 | } |
| 1276 | EXPORT_SYMBOL_GPL(lttng_transport_unregister); |
| 1277 | |
| 1278 | static int __init lttng_events_init(void) |
| 1279 | { |
| 1280 | int ret; |
| 1281 | |
| 1282 | ret = wrapper_lttng_fixup_sig(THIS_MODULE); |
| 1283 | if (ret) |
| 1284 | return ret; |
| 1285 | |
| 1286 | ret = lttng_tracepoint_init(); |
| 1287 | if (ret) |
| 1288 | return ret; |
| 1289 | event_cache = KMEM_CACHE(lttng_event, 0); |
| 1290 | if (!event_cache) { |
| 1291 | ret = -ENOMEM; |
| 1292 | goto error_kmem; |
| 1293 | } |
| 1294 | ret = lttng_abi_init(); |
| 1295 | if (ret) |
| 1296 | goto error_abi; |
| 1297 | ret = lttng_logger_init(); |
| 1298 | if (ret) |
| 1299 | goto error_logger; |
| 1300 | return 0; |
| 1301 | |
| 1302 | error_logger: |
| 1303 | lttng_abi_exit(); |
| 1304 | error_abi: |
| 1305 | kmem_cache_destroy(event_cache); |
| 1306 | error_kmem: |
| 1307 | lttng_tracepoint_exit(); |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
| 1311 | module_init(lttng_events_init); |
| 1312 | |
| 1313 | static void __exit lttng_events_exit(void) |
| 1314 | { |
| 1315 | struct lttng_session *session, *tmpsession; |
| 1316 | |
| 1317 | lttng_logger_exit(); |
| 1318 | lttng_abi_exit(); |
| 1319 | list_for_each_entry_safe(session, tmpsession, &sessions, list) |
| 1320 | lttng_session_destroy(session); |
| 1321 | kmem_cache_destroy(event_cache); |
| 1322 | lttng_tracepoint_exit(); |
| 1323 | } |
| 1324 | |
| 1325 | module_exit(lttng_events_exit); |
| 1326 | |
| 1327 | MODULE_LICENSE("GPL and additional rights"); |
| 1328 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); |
| 1329 | MODULE_DESCRIPTION("LTTng Events"); |
| 1330 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
| 1331 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." |
| 1332 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) |
| 1333 | LTTNG_MODULES_EXTRAVERSION); |