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