| 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 | /* |
| 24 | * This page_alloc.h wrapper needs to be included before gfpflags.h because it |
| 25 | * overrides a function with a define. |
| 26 | */ |
| 27 | #include "wrapper/page_alloc.h" |
| 28 | |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/mutex.h> |
| 31 | #include <linux/sched.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/jiffies.h> |
| 34 | #include <linux/utsname.h> |
| 35 | #include <linux/err.h> |
| 36 | #include <linux/seq_file.h> |
| 37 | #include <linux/file.h> |
| 38 | #include <linux/anon_inodes.h> |
| 39 | #include "wrapper/file.h" |
| 40 | #include <linux/jhash.h> |
| 41 | #include <linux/uaccess.h> |
| 42 | #include <linux/vmalloc.h> |
| 43 | |
| 44 | #include "wrapper/uuid.h" |
| 45 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
| 46 | #include "wrapper/random.h" |
| 47 | #include "wrapper/tracepoint.h" |
| 48 | #include "wrapper/list.h" |
| 49 | #include "lttng-kernel-version.h" |
| 50 | #include "lttng-events.h" |
| 51 | #include "lttng-tracer.h" |
| 52 | #include "lttng-abi-old.h" |
| 53 | |
| 54 | #define METADATA_CACHE_DEFAULT_SIZE 4096 |
| 55 | |
| 56 | static LIST_HEAD(sessions); |
| 57 | static LIST_HEAD(lttng_transport_list); |
| 58 | /* |
| 59 | * Protect the sessions and metadata caches. |
| 60 | */ |
| 61 | static DEFINE_MUTEX(sessions_mutex); |
| 62 | static struct kmem_cache *event_cache; |
| 63 | |
| 64 | static void lttng_session_lazy_sync_enablers(struct lttng_session *session); |
| 65 | static void lttng_session_sync_enablers(struct lttng_session *session); |
| 66 | static void lttng_enabler_destroy(struct lttng_enabler *enabler); |
| 67 | |
| 68 | static void _lttng_event_destroy(struct lttng_event *event); |
| 69 | static void _lttng_channel_destroy(struct lttng_channel *chan); |
| 70 | static int _lttng_event_unregister(struct lttng_event *event); |
| 71 | static |
| 72 | int _lttng_event_metadata_statedump(struct lttng_session *session, |
| 73 | struct lttng_channel *chan, |
| 74 | struct lttng_event *event); |
| 75 | static |
| 76 | int _lttng_session_metadata_statedump(struct lttng_session *session); |
| 77 | static |
| 78 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream); |
| 79 | |
| 80 | void synchronize_trace(void) |
| 81 | { |
| 82 | synchronize_sched(); |
| 83 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) |
| 84 | #ifdef CONFIG_PREEMPT_RT_FULL |
| 85 | synchronize_rcu(); |
| 86 | #endif |
| 87 | #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */ |
| 88 | #ifdef CONFIG_PREEMPT_RT |
| 89 | synchronize_rcu(); |
| 90 | #endif |
| 91 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */ |
| 92 | } |
| 93 | |
| 94 | void lttng_lock_sessions(void) |
| 95 | { |
| 96 | mutex_lock(&sessions_mutex); |
| 97 | } |
| 98 | |
| 99 | void lttng_unlock_sessions(void) |
| 100 | { |
| 101 | mutex_unlock(&sessions_mutex); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Called with sessions lock held. |
| 106 | */ |
| 107 | int lttng_session_active(void) |
| 108 | { |
| 109 | struct lttng_session *iter; |
| 110 | |
| 111 | list_for_each_entry(iter, &sessions, list) { |
| 112 | if (iter->active) |
| 113 | return 1; |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | struct lttng_session *lttng_session_create(void) |
| 119 | { |
| 120 | struct lttng_session *session; |
| 121 | struct lttng_metadata_cache *metadata_cache; |
| 122 | int i; |
| 123 | |
| 124 | mutex_lock(&sessions_mutex); |
| 125 | session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL); |
| 126 | if (!session) |
| 127 | goto err; |
| 128 | INIT_LIST_HEAD(&session->chan); |
| 129 | INIT_LIST_HEAD(&session->events); |
| 130 | uuid_le_gen(&session->uuid); |
| 131 | |
| 132 | metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache), |
| 133 | GFP_KERNEL); |
| 134 | if (!metadata_cache) |
| 135 | goto err_free_session; |
| 136 | metadata_cache->data = vzalloc(METADATA_CACHE_DEFAULT_SIZE); |
| 137 | if (!metadata_cache->data) |
| 138 | goto err_free_cache; |
| 139 | metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE; |
| 140 | kref_init(&metadata_cache->refcount); |
| 141 | mutex_init(&metadata_cache->lock); |
| 142 | session->metadata_cache = metadata_cache; |
| 143 | INIT_LIST_HEAD(&metadata_cache->metadata_stream); |
| 144 | memcpy(&metadata_cache->uuid, &session->uuid, |
| 145 | sizeof(metadata_cache->uuid)); |
| 146 | INIT_LIST_HEAD(&session->enablers_head); |
| 147 | for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++) |
| 148 | INIT_HLIST_HEAD(&session->events_ht.table[i]); |
| 149 | list_add(&session->list, &sessions); |
| 150 | mutex_unlock(&sessions_mutex); |
| 151 | return session; |
| 152 | |
| 153 | err_free_cache: |
| 154 | kfree(metadata_cache); |
| 155 | err_free_session: |
| 156 | kfree(session); |
| 157 | err: |
| 158 | mutex_unlock(&sessions_mutex); |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | void metadata_cache_destroy(struct kref *kref) |
| 163 | { |
| 164 | struct lttng_metadata_cache *cache = |
| 165 | container_of(kref, struct lttng_metadata_cache, refcount); |
| 166 | vfree(cache->data); |
| 167 | kfree(cache); |
| 168 | } |
| 169 | |
| 170 | void lttng_session_destroy(struct lttng_session *session) |
| 171 | { |
| 172 | struct lttng_channel *chan, *tmpchan; |
| 173 | struct lttng_event *event, *tmpevent; |
| 174 | struct lttng_metadata_stream *metadata_stream; |
| 175 | struct lttng_enabler *enabler, *tmpenabler; |
| 176 | int ret; |
| 177 | |
| 178 | mutex_lock(&sessions_mutex); |
| 179 | ACCESS_ONCE(session->active) = 0; |
| 180 | list_for_each_entry(chan, &session->chan, list) { |
| 181 | ret = lttng_syscalls_unregister(chan); |
| 182 | WARN_ON(ret); |
| 183 | } |
| 184 | list_for_each_entry(event, &session->events, list) { |
| 185 | ret = _lttng_event_unregister(event); |
| 186 | WARN_ON(ret); |
| 187 | } |
| 188 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 189 | list_for_each_entry_safe(enabler, tmpenabler, |
| 190 | &session->enablers_head, node) |
| 191 | lttng_enabler_destroy(enabler); |
| 192 | list_for_each_entry_safe(event, tmpevent, &session->events, list) |
| 193 | _lttng_event_destroy(event); |
| 194 | list_for_each_entry_safe(chan, tmpchan, &session->chan, list) { |
| 195 | BUG_ON(chan->channel_type == METADATA_CHANNEL); |
| 196 | _lttng_channel_destroy(chan); |
| 197 | } |
| 198 | list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list) |
| 199 | _lttng_metadata_channel_hangup(metadata_stream); |
| 200 | if (session->pid_tracker) |
| 201 | lttng_pid_tracker_destroy(session->pid_tracker); |
| 202 | kref_put(&session->metadata_cache->refcount, metadata_cache_destroy); |
| 203 | list_del(&session->list); |
| 204 | mutex_unlock(&sessions_mutex); |
| 205 | kfree(session); |
| 206 | } |
| 207 | |
| 208 | int lttng_session_enable(struct lttng_session *session) |
| 209 | { |
| 210 | int ret = 0; |
| 211 | struct lttng_channel *chan; |
| 212 | |
| 213 | mutex_lock(&sessions_mutex); |
| 214 | if (session->active) { |
| 215 | ret = -EBUSY; |
| 216 | goto end; |
| 217 | } |
| 218 | |
| 219 | /* Set transient enabler state to "enabled" */ |
| 220 | session->tstate = 1; |
| 221 | |
| 222 | /* |
| 223 | * Snapshot the number of events per channel to know the type of header |
| 224 | * we need to use. |
| 225 | */ |
| 226 | list_for_each_entry(chan, &session->chan, list) { |
| 227 | if (chan->header_type) |
| 228 | continue; /* don't change it if session stop/restart */ |
| 229 | if (chan->free_event_id < 31) |
| 230 | chan->header_type = 1; /* compact */ |
| 231 | else |
| 232 | chan->header_type = 2; /* large */ |
| 233 | } |
| 234 | |
| 235 | /* We need to sync enablers with session before activation. */ |
| 236 | lttng_session_sync_enablers(session); |
| 237 | |
| 238 | ACCESS_ONCE(session->active) = 1; |
| 239 | ACCESS_ONCE(session->been_active) = 1; |
| 240 | ret = _lttng_session_metadata_statedump(session); |
| 241 | if (ret) { |
| 242 | ACCESS_ONCE(session->active) = 0; |
| 243 | goto end; |
| 244 | } |
| 245 | ret = lttng_statedump_start(session); |
| 246 | if (ret) |
| 247 | ACCESS_ONCE(session->active) = 0; |
| 248 | end: |
| 249 | mutex_unlock(&sessions_mutex); |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | int lttng_session_disable(struct lttng_session *session) |
| 254 | { |
| 255 | int ret = 0; |
| 256 | |
| 257 | mutex_lock(&sessions_mutex); |
| 258 | if (!session->active) { |
| 259 | ret = -EBUSY; |
| 260 | goto end; |
| 261 | } |
| 262 | ACCESS_ONCE(session->active) = 0; |
| 263 | |
| 264 | /* Set transient enabler state to "disabled" */ |
| 265 | session->tstate = 0; |
| 266 | lttng_session_sync_enablers(session); |
| 267 | end: |
| 268 | mutex_unlock(&sessions_mutex); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | int lttng_channel_enable(struct lttng_channel *channel) |
| 273 | { |
| 274 | int ret = 0; |
| 275 | |
| 276 | mutex_lock(&sessions_mutex); |
| 277 | if (channel->channel_type == METADATA_CHANNEL) { |
| 278 | ret = -EPERM; |
| 279 | goto end; |
| 280 | } |
| 281 | if (channel->enabled) { |
| 282 | ret = -EEXIST; |
| 283 | goto end; |
| 284 | } |
| 285 | /* Set transient enabler state to "enabled" */ |
| 286 | channel->tstate = 1; |
| 287 | lttng_session_sync_enablers(channel->session); |
| 288 | /* Set atomically the state to "enabled" */ |
| 289 | ACCESS_ONCE(channel->enabled) = 1; |
| 290 | end: |
| 291 | mutex_unlock(&sessions_mutex); |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | int lttng_channel_disable(struct lttng_channel *channel) |
| 296 | { |
| 297 | int ret = 0; |
| 298 | |
| 299 | mutex_lock(&sessions_mutex); |
| 300 | if (channel->channel_type == METADATA_CHANNEL) { |
| 301 | ret = -EPERM; |
| 302 | goto end; |
| 303 | } |
| 304 | if (!channel->enabled) { |
| 305 | ret = -EEXIST; |
| 306 | goto end; |
| 307 | } |
| 308 | /* Set atomically the state to "disabled" */ |
| 309 | ACCESS_ONCE(channel->enabled) = 0; |
| 310 | /* Set transient enabler state to "enabled" */ |
| 311 | channel->tstate = 0; |
| 312 | lttng_session_sync_enablers(channel->session); |
| 313 | end: |
| 314 | mutex_unlock(&sessions_mutex); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | int lttng_event_enable(struct lttng_event *event) |
| 319 | { |
| 320 | int ret = 0; |
| 321 | |
| 322 | mutex_lock(&sessions_mutex); |
| 323 | if (event->chan->channel_type == METADATA_CHANNEL) { |
| 324 | ret = -EPERM; |
| 325 | goto end; |
| 326 | } |
| 327 | if (event->enabled) { |
| 328 | ret = -EEXIST; |
| 329 | goto end; |
| 330 | } |
| 331 | ACCESS_ONCE(event->enabled) = 1; |
| 332 | lttng_session_sync_enablers(event->chan->session); |
| 333 | end: |
| 334 | mutex_unlock(&sessions_mutex); |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | int lttng_event_disable(struct lttng_event *event) |
| 339 | { |
| 340 | int ret = 0; |
| 341 | |
| 342 | mutex_lock(&sessions_mutex); |
| 343 | if (event->chan->channel_type == METADATA_CHANNEL) { |
| 344 | ret = -EPERM; |
| 345 | goto end; |
| 346 | } |
| 347 | if (!event->enabled) { |
| 348 | ret = -EEXIST; |
| 349 | goto end; |
| 350 | } |
| 351 | ACCESS_ONCE(event->enabled) = 0; |
| 352 | lttng_session_sync_enablers(event->chan->session); |
| 353 | end: |
| 354 | mutex_unlock(&sessions_mutex); |
| 355 | return ret; |
| 356 | } |
| 357 | |
| 358 | static struct lttng_transport *lttng_transport_find(const char *name) |
| 359 | { |
| 360 | struct lttng_transport *transport; |
| 361 | |
| 362 | list_for_each_entry(transport, <tng_transport_list, node) { |
| 363 | if (!strcmp(transport->name, name)) |
| 364 | return transport; |
| 365 | } |
| 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | struct lttng_channel *lttng_channel_create(struct lttng_session *session, |
| 370 | const char *transport_name, |
| 371 | void *buf_addr, |
| 372 | size_t subbuf_size, size_t num_subbuf, |
| 373 | unsigned int switch_timer_interval, |
| 374 | unsigned int read_timer_interval, |
| 375 | enum channel_type channel_type) |
| 376 | { |
| 377 | struct lttng_channel *chan; |
| 378 | struct lttng_transport *transport = NULL; |
| 379 | |
| 380 | mutex_lock(&sessions_mutex); |
| 381 | if (session->been_active && channel_type != METADATA_CHANNEL) |
| 382 | goto active; /* Refuse to add channel to active session */ |
| 383 | transport = lttng_transport_find(transport_name); |
| 384 | if (!transport) { |
| 385 | printk(KERN_WARNING "LTTng transport %s not found\n", |
| 386 | transport_name); |
| 387 | goto notransport; |
| 388 | } |
| 389 | if (!try_module_get(transport->owner)) { |
| 390 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); |
| 391 | goto notransport; |
| 392 | } |
| 393 | chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL); |
| 394 | if (!chan) |
| 395 | goto nomem; |
| 396 | chan->session = session; |
| 397 | chan->id = session->free_chan_id++; |
| 398 | chan->ops = &transport->ops; |
| 399 | /* |
| 400 | * Note: the channel creation op already writes into the packet |
| 401 | * headers. Therefore the "chan" information used as input |
| 402 | * should be already accessible. |
| 403 | */ |
| 404 | chan->chan = transport->ops.channel_create(transport_name, |
| 405 | chan, buf_addr, subbuf_size, num_subbuf, |
| 406 | switch_timer_interval, read_timer_interval); |
| 407 | if (!chan->chan) |
| 408 | goto create_error; |
| 409 | chan->tstate = 1; |
| 410 | chan->enabled = 1; |
| 411 | chan->transport = transport; |
| 412 | chan->channel_type = channel_type; |
| 413 | list_add(&chan->list, &session->chan); |
| 414 | mutex_unlock(&sessions_mutex); |
| 415 | return chan; |
| 416 | |
| 417 | create_error: |
| 418 | kfree(chan); |
| 419 | nomem: |
| 420 | if (transport) |
| 421 | module_put(transport->owner); |
| 422 | notransport: |
| 423 | active: |
| 424 | mutex_unlock(&sessions_mutex); |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Only used internally at session destruction for per-cpu channels, and |
| 430 | * when metadata channel is released. |
| 431 | * Needs to be called with sessions mutex held. |
| 432 | */ |
| 433 | static |
| 434 | void _lttng_channel_destroy(struct lttng_channel *chan) |
| 435 | { |
| 436 | chan->ops->channel_destroy(chan->chan); |
| 437 | module_put(chan->transport->owner); |
| 438 | list_del(&chan->list); |
| 439 | lttng_destroy_context(chan->ctx); |
| 440 | kfree(chan); |
| 441 | } |
| 442 | |
| 443 | void lttng_metadata_channel_destroy(struct lttng_channel *chan) |
| 444 | { |
| 445 | BUG_ON(chan->channel_type != METADATA_CHANNEL); |
| 446 | |
| 447 | /* Protect the metadata cache with the sessions_mutex. */ |
| 448 | mutex_lock(&sessions_mutex); |
| 449 | _lttng_channel_destroy(chan); |
| 450 | mutex_unlock(&sessions_mutex); |
| 451 | } |
| 452 | EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy); |
| 453 | |
| 454 | static |
| 455 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream) |
| 456 | { |
| 457 | stream->finalized = 1; |
| 458 | wake_up_interruptible(&stream->read_wait); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Supports event creation while tracing session is active. |
| 463 | * Needs to be called with sessions mutex held. |
| 464 | */ |
| 465 | struct lttng_event *_lttng_event_create(struct lttng_channel *chan, |
| 466 | struct lttng_kernel_event *event_param, |
| 467 | void *filter, |
| 468 | const struct lttng_event_desc *event_desc, |
| 469 | enum lttng_kernel_instrumentation itype) |
| 470 | { |
| 471 | struct lttng_session *session = chan->session; |
| 472 | struct lttng_event *event; |
| 473 | const char *event_name; |
| 474 | struct hlist_head *head; |
| 475 | size_t name_len; |
| 476 | uint32_t hash; |
| 477 | int ret; |
| 478 | |
| 479 | if (chan->free_event_id == -1U) { |
| 480 | ret = -EMFILE; |
| 481 | goto full; |
| 482 | } |
| 483 | |
| 484 | switch (itype) { |
| 485 | case LTTNG_KERNEL_TRACEPOINT: |
| 486 | event_name = event_desc->name; |
| 487 | break; |
| 488 | case LTTNG_KERNEL_KPROBE: |
| 489 | case LTTNG_KERNEL_KRETPROBE: |
| 490 | case LTTNG_KERNEL_FUNCTION: |
| 491 | case LTTNG_KERNEL_NOOP: |
| 492 | case LTTNG_KERNEL_SYSCALL: |
| 493 | event_name = event_param->name; |
| 494 | break; |
| 495 | default: |
| 496 | WARN_ON_ONCE(1); |
| 497 | ret = -EINVAL; |
| 498 | goto type_error; |
| 499 | } |
| 500 | name_len = strlen(event_name); |
| 501 | hash = jhash(event_name, name_len, 0); |
| 502 | head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)]; |
| 503 | lttng_hlist_for_each_entry(event, head, hlist) { |
| 504 | WARN_ON_ONCE(!event->desc); |
| 505 | if (!strncmp(event->desc->name, event_name, |
| 506 | LTTNG_KERNEL_SYM_NAME_LEN - 1) |
| 507 | && chan == event->chan) { |
| 508 | ret = -EEXIST; |
| 509 | goto exist; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | event = kmem_cache_zalloc(event_cache, GFP_KERNEL); |
| 514 | if (!event) { |
| 515 | ret = -ENOMEM; |
| 516 | goto cache_error; |
| 517 | } |
| 518 | event->chan = chan; |
| 519 | event->filter = filter; |
| 520 | event->id = chan->free_event_id++; |
| 521 | event->instrumentation = itype; |
| 522 | event->evtype = LTTNG_TYPE_EVENT; |
| 523 | INIT_LIST_HEAD(&event->bytecode_runtime_head); |
| 524 | INIT_LIST_HEAD(&event->enablers_ref_head); |
| 525 | |
| 526 | switch (itype) { |
| 527 | case LTTNG_KERNEL_TRACEPOINT: |
| 528 | /* Event will be enabled by enabler sync. */ |
| 529 | event->enabled = 0; |
| 530 | event->registered = 0; |
| 531 | event->desc = lttng_event_get(event_name); |
| 532 | if (!event->desc) { |
| 533 | ret = -ENOENT; |
| 534 | goto register_error; |
| 535 | } |
| 536 | /* Populate lttng_event structure before event registration. */ |
| 537 | smp_wmb(); |
| 538 | break; |
| 539 | case LTTNG_KERNEL_KPROBE: |
| 540 | event->enabled = 1; |
| 541 | event->registered = 1; |
| 542 | /* |
| 543 | * Populate lttng_event structure before event |
| 544 | * registration. |
| 545 | */ |
| 546 | smp_wmb(); |
| 547 | ret = lttng_kprobes_register(event_name, |
| 548 | event_param->u.kprobe.symbol_name, |
| 549 | event_param->u.kprobe.offset, |
| 550 | event_param->u.kprobe.addr, |
| 551 | event); |
| 552 | if (ret) { |
| 553 | ret = -EINVAL; |
| 554 | goto register_error; |
| 555 | } |
| 556 | ret = try_module_get(event->desc->owner); |
| 557 | WARN_ON_ONCE(!ret); |
| 558 | break; |
| 559 | case LTTNG_KERNEL_KRETPROBE: |
| 560 | { |
| 561 | struct lttng_event *event_return; |
| 562 | |
| 563 | /* kretprobe defines 2 events */ |
| 564 | event->enabled = 1; |
| 565 | event->registered = 1; |
| 566 | event_return = |
| 567 | kmem_cache_zalloc(event_cache, GFP_KERNEL); |
| 568 | if (!event_return) { |
| 569 | ret = -ENOMEM; |
| 570 | goto register_error; |
| 571 | } |
| 572 | event_return->chan = chan; |
| 573 | event_return->filter = filter; |
| 574 | event_return->id = chan->free_event_id++; |
| 575 | event_return->enabled = 1; |
| 576 | event_return->registered = 1; |
| 577 | event_return->instrumentation = itype; |
| 578 | /* |
| 579 | * Populate lttng_event structure before kretprobe registration. |
| 580 | */ |
| 581 | smp_wmb(); |
| 582 | ret = lttng_kretprobes_register(event_name, |
| 583 | event_param->u.kretprobe.symbol_name, |
| 584 | event_param->u.kretprobe.offset, |
| 585 | event_param->u.kretprobe.addr, |
| 586 | event, event_return); |
| 587 | if (ret) { |
| 588 | kmem_cache_free(event_cache, event_return); |
| 589 | ret = -EINVAL; |
| 590 | goto register_error; |
| 591 | } |
| 592 | /* Take 2 refs on the module: one per event. */ |
| 593 | ret = try_module_get(event->desc->owner); |
| 594 | WARN_ON_ONCE(!ret); |
| 595 | ret = try_module_get(event->desc->owner); |
| 596 | WARN_ON_ONCE(!ret); |
| 597 | ret = _lttng_event_metadata_statedump(chan->session, chan, |
| 598 | event_return); |
| 599 | WARN_ON_ONCE(ret > 0); |
| 600 | if (ret) { |
| 601 | kmem_cache_free(event_cache, event_return); |
| 602 | module_put(event->desc->owner); |
| 603 | module_put(event->desc->owner); |
| 604 | goto statedump_error; |
| 605 | } |
| 606 | list_add(&event_return->list, &chan->session->events); |
| 607 | break; |
| 608 | } |
| 609 | case LTTNG_KERNEL_FUNCTION: |
| 610 | event->enabled = 1; |
| 611 | event->registered = 1; |
| 612 | /* |
| 613 | * Populate lttng_event structure before event |
| 614 | * registration. |
| 615 | */ |
| 616 | smp_wmb(); |
| 617 | ret = lttng_ftrace_register(event_name, |
| 618 | event_param->u.ftrace.symbol_name, |
| 619 | event); |
| 620 | if (ret) { |
| 621 | goto register_error; |
| 622 | } |
| 623 | ret = try_module_get(event->desc->owner); |
| 624 | WARN_ON_ONCE(!ret); |
| 625 | break; |
| 626 | case LTTNG_KERNEL_NOOP: |
| 627 | case LTTNG_KERNEL_SYSCALL: |
| 628 | event->enabled = 1; |
| 629 | event->registered = 0; |
| 630 | event->desc = event_desc; |
| 631 | if (!event->desc) { |
| 632 | ret = -EINVAL; |
| 633 | goto register_error; |
| 634 | } |
| 635 | break; |
| 636 | default: |
| 637 | WARN_ON_ONCE(1); |
| 638 | ret = -EINVAL; |
| 639 | goto register_error; |
| 640 | } |
| 641 | ret = _lttng_event_metadata_statedump(chan->session, chan, event); |
| 642 | WARN_ON_ONCE(ret > 0); |
| 643 | if (ret) { |
| 644 | goto statedump_error; |
| 645 | } |
| 646 | hlist_add_head(&event->hlist, head); |
| 647 | list_add(&event->list, &chan->session->events); |
| 648 | return event; |
| 649 | |
| 650 | statedump_error: |
| 651 | /* If a statedump error occurs, events will not be readable. */ |
| 652 | register_error: |
| 653 | kmem_cache_free(event_cache, event); |
| 654 | cache_error: |
| 655 | exist: |
| 656 | type_error: |
| 657 | full: |
| 658 | return ERR_PTR(ret); |
| 659 | } |
| 660 | |
| 661 | struct lttng_event *lttng_event_create(struct lttng_channel *chan, |
| 662 | struct lttng_kernel_event *event_param, |
| 663 | void *filter, |
| 664 | const struct lttng_event_desc *event_desc, |
| 665 | enum lttng_kernel_instrumentation itype) |
| 666 | { |
| 667 | struct lttng_event *event; |
| 668 | |
| 669 | mutex_lock(&sessions_mutex); |
| 670 | event = _lttng_event_create(chan, event_param, filter, event_desc, |
| 671 | itype); |
| 672 | mutex_unlock(&sessions_mutex); |
| 673 | return event; |
| 674 | } |
| 675 | |
| 676 | /* Only used for tracepoints for now. */ |
| 677 | static |
| 678 | void register_event(struct lttng_event *event) |
| 679 | { |
| 680 | const struct lttng_event_desc *desc; |
| 681 | int ret = -EINVAL; |
| 682 | |
| 683 | if (event->registered) |
| 684 | return; |
| 685 | |
| 686 | desc = event->desc; |
| 687 | switch (event->instrumentation) { |
| 688 | case LTTNG_KERNEL_TRACEPOINT: |
| 689 | ret = lttng_wrapper_tracepoint_probe_register(desc->kname, |
| 690 | desc->probe_callback, |
| 691 | event); |
| 692 | break; |
| 693 | case LTTNG_KERNEL_SYSCALL: |
| 694 | ret = lttng_syscall_filter_enable(event->chan, |
| 695 | desc->name); |
| 696 | break; |
| 697 | case LTTNG_KERNEL_KPROBE: |
| 698 | case LTTNG_KERNEL_KRETPROBE: |
| 699 | case LTTNG_KERNEL_FUNCTION: |
| 700 | case LTTNG_KERNEL_NOOP: |
| 701 | ret = 0; |
| 702 | break; |
| 703 | default: |
| 704 | WARN_ON_ONCE(1); |
| 705 | } |
| 706 | if (!ret) |
| 707 | event->registered = 1; |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Only used internally at session destruction. |
| 712 | */ |
| 713 | int _lttng_event_unregister(struct lttng_event *event) |
| 714 | { |
| 715 | const struct lttng_event_desc *desc; |
| 716 | int ret = -EINVAL; |
| 717 | |
| 718 | if (!event->registered) |
| 719 | return 0; |
| 720 | |
| 721 | desc = event->desc; |
| 722 | switch (event->instrumentation) { |
| 723 | case LTTNG_KERNEL_TRACEPOINT: |
| 724 | ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname, |
| 725 | event->desc->probe_callback, |
| 726 | event); |
| 727 | break; |
| 728 | case LTTNG_KERNEL_KPROBE: |
| 729 | lttng_kprobes_unregister(event); |
| 730 | ret = 0; |
| 731 | break; |
| 732 | case LTTNG_KERNEL_KRETPROBE: |
| 733 | lttng_kretprobes_unregister(event); |
| 734 | ret = 0; |
| 735 | break; |
| 736 | case LTTNG_KERNEL_FUNCTION: |
| 737 | lttng_ftrace_unregister(event); |
| 738 | ret = 0; |
| 739 | break; |
| 740 | case LTTNG_KERNEL_SYSCALL: |
| 741 | ret = lttng_syscall_filter_disable(event->chan, |
| 742 | desc->name); |
| 743 | break; |
| 744 | case LTTNG_KERNEL_NOOP: |
| 745 | ret = 0; |
| 746 | break; |
| 747 | default: |
| 748 | WARN_ON_ONCE(1); |
| 749 | } |
| 750 | if (!ret) |
| 751 | event->registered = 0; |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Only used internally at session destruction. |
| 757 | */ |
| 758 | static |
| 759 | void _lttng_event_destroy(struct lttng_event *event) |
| 760 | { |
| 761 | switch (event->instrumentation) { |
| 762 | case LTTNG_KERNEL_TRACEPOINT: |
| 763 | lttng_event_put(event->desc); |
| 764 | break; |
| 765 | case LTTNG_KERNEL_KPROBE: |
| 766 | module_put(event->desc->owner); |
| 767 | lttng_kprobes_destroy_private(event); |
| 768 | break; |
| 769 | case LTTNG_KERNEL_KRETPROBE: |
| 770 | module_put(event->desc->owner); |
| 771 | lttng_kretprobes_destroy_private(event); |
| 772 | break; |
| 773 | case LTTNG_KERNEL_FUNCTION: |
| 774 | module_put(event->desc->owner); |
| 775 | lttng_ftrace_destroy_private(event); |
| 776 | break; |
| 777 | case LTTNG_KERNEL_NOOP: |
| 778 | case LTTNG_KERNEL_SYSCALL: |
| 779 | break; |
| 780 | default: |
| 781 | WARN_ON_ONCE(1); |
| 782 | } |
| 783 | list_del(&event->list); |
| 784 | lttng_destroy_context(event->ctx); |
| 785 | kmem_cache_free(event_cache, event); |
| 786 | } |
| 787 | |
| 788 | int lttng_session_track_pid(struct lttng_session *session, int pid) |
| 789 | { |
| 790 | int ret; |
| 791 | |
| 792 | if (pid < -1) |
| 793 | return -EINVAL; |
| 794 | mutex_lock(&sessions_mutex); |
| 795 | if (pid == -1) { |
| 796 | /* track all pids: destroy tracker. */ |
| 797 | if (session->pid_tracker) { |
| 798 | struct lttng_pid_tracker *lpf; |
| 799 | |
| 800 | lpf = session->pid_tracker; |
| 801 | rcu_assign_pointer(session->pid_tracker, NULL); |
| 802 | synchronize_trace(); |
| 803 | lttng_pid_tracker_destroy(lpf); |
| 804 | } |
| 805 | ret = 0; |
| 806 | } else { |
| 807 | if (!session->pid_tracker) { |
| 808 | struct lttng_pid_tracker *lpf; |
| 809 | |
| 810 | lpf = lttng_pid_tracker_create(); |
| 811 | if (!lpf) { |
| 812 | ret = -ENOMEM; |
| 813 | goto unlock; |
| 814 | } |
| 815 | ret = lttng_pid_tracker_add(lpf, pid); |
| 816 | rcu_assign_pointer(session->pid_tracker, lpf); |
| 817 | } else { |
| 818 | ret = lttng_pid_tracker_add(session->pid_tracker, pid); |
| 819 | } |
| 820 | } |
| 821 | unlock: |
| 822 | mutex_unlock(&sessions_mutex); |
| 823 | return ret; |
| 824 | } |
| 825 | |
| 826 | int lttng_session_untrack_pid(struct lttng_session *session, int pid) |
| 827 | { |
| 828 | int ret; |
| 829 | |
| 830 | if (pid < -1) |
| 831 | return -EINVAL; |
| 832 | mutex_lock(&sessions_mutex); |
| 833 | if (pid == -1) { |
| 834 | /* untrack all pids: replace by empty tracker. */ |
| 835 | struct lttng_pid_tracker *old_lpf = session->pid_tracker; |
| 836 | struct lttng_pid_tracker *lpf; |
| 837 | |
| 838 | lpf = lttng_pid_tracker_create(); |
| 839 | if (!lpf) { |
| 840 | ret = -ENOMEM; |
| 841 | goto unlock; |
| 842 | } |
| 843 | rcu_assign_pointer(session->pid_tracker, lpf); |
| 844 | synchronize_trace(); |
| 845 | if (old_lpf) |
| 846 | lttng_pid_tracker_destroy(old_lpf); |
| 847 | ret = 0; |
| 848 | } else { |
| 849 | if (!session->pid_tracker) { |
| 850 | ret = -ENOENT; |
| 851 | goto unlock; |
| 852 | } |
| 853 | ret = lttng_pid_tracker_del(session->pid_tracker, pid); |
| 854 | } |
| 855 | unlock: |
| 856 | mutex_unlock(&sessions_mutex); |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | static |
| 861 | void *pid_list_start(struct seq_file *m, loff_t *pos) |
| 862 | { |
| 863 | struct lttng_session *session = m->private; |
| 864 | struct lttng_pid_tracker *lpf; |
| 865 | struct lttng_pid_hash_node *e; |
| 866 | int iter = 0, i; |
| 867 | |
| 868 | mutex_lock(&sessions_mutex); |
| 869 | lpf = session->pid_tracker; |
| 870 | if (lpf) { |
| 871 | for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) { |
| 872 | struct hlist_head *head = &lpf->pid_hash[i]; |
| 873 | |
| 874 | lttng_hlist_for_each_entry(e, head, hlist) { |
| 875 | if (iter++ >= *pos) |
| 876 | return e; |
| 877 | } |
| 878 | } |
| 879 | } else { |
| 880 | /* PID tracker disabled. */ |
| 881 | if (iter >= *pos && iter == 0) { |
| 882 | return session; /* empty tracker */ |
| 883 | } |
| 884 | iter++; |
| 885 | } |
| 886 | /* End of list */ |
| 887 | return NULL; |
| 888 | } |
| 889 | |
| 890 | /* Called with sessions_mutex held. */ |
| 891 | static |
| 892 | void *pid_list_next(struct seq_file *m, void *p, loff_t *ppos) |
| 893 | { |
| 894 | struct lttng_session *session = m->private; |
| 895 | struct lttng_pid_tracker *lpf; |
| 896 | struct lttng_pid_hash_node *e; |
| 897 | int iter = 0, i; |
| 898 | |
| 899 | (*ppos)++; |
| 900 | lpf = session->pid_tracker; |
| 901 | if (lpf) { |
| 902 | for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) { |
| 903 | struct hlist_head *head = &lpf->pid_hash[i]; |
| 904 | |
| 905 | lttng_hlist_for_each_entry(e, head, hlist) { |
| 906 | if (iter++ >= *ppos) |
| 907 | return e; |
| 908 | } |
| 909 | } |
| 910 | } else { |
| 911 | /* PID tracker disabled. */ |
| 912 | if (iter >= *ppos && iter == 0) |
| 913 | return session; /* empty tracker */ |
| 914 | iter++; |
| 915 | } |
| 916 | |
| 917 | /* End of list */ |
| 918 | return NULL; |
| 919 | } |
| 920 | |
| 921 | static |
| 922 | void pid_list_stop(struct seq_file *m, void *p) |
| 923 | { |
| 924 | mutex_unlock(&sessions_mutex); |
| 925 | } |
| 926 | |
| 927 | static |
| 928 | int pid_list_show(struct seq_file *m, void *p) |
| 929 | { |
| 930 | int pid; |
| 931 | |
| 932 | if (p == m->private) { |
| 933 | /* Tracker disabled. */ |
| 934 | pid = -1; |
| 935 | } else { |
| 936 | const struct lttng_pid_hash_node *e = p; |
| 937 | |
| 938 | pid = lttng_pid_tracker_get_node_pid(e); |
| 939 | } |
| 940 | seq_printf(m, "process { pid = %d; };\n", pid); |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static |
| 945 | const struct seq_operations lttng_tracker_pids_list_seq_ops = { |
| 946 | .start = pid_list_start, |
| 947 | .next = pid_list_next, |
| 948 | .stop = pid_list_stop, |
| 949 | .show = pid_list_show, |
| 950 | }; |
| 951 | |
| 952 | static |
| 953 | int lttng_tracker_pids_list_open(struct inode *inode, struct file *file) |
| 954 | { |
| 955 | return seq_open(file, <tng_tracker_pids_list_seq_ops); |
| 956 | } |
| 957 | |
| 958 | static |
| 959 | int lttng_tracker_pids_list_release(struct inode *inode, struct file *file) |
| 960 | { |
| 961 | struct seq_file *m = file->private_data; |
| 962 | struct lttng_session *session = m->private; |
| 963 | int ret; |
| 964 | |
| 965 | WARN_ON_ONCE(!session); |
| 966 | ret = seq_release(inode, file); |
| 967 | if (!ret && session) |
| 968 | fput(session->file); |
| 969 | return ret; |
| 970 | } |
| 971 | |
| 972 | const struct file_operations lttng_tracker_pids_list_fops = { |
| 973 | .owner = THIS_MODULE, |
| 974 | .open = lttng_tracker_pids_list_open, |
| 975 | .read = seq_read, |
| 976 | .llseek = seq_lseek, |
| 977 | .release = lttng_tracker_pids_list_release, |
| 978 | }; |
| 979 | |
| 980 | int lttng_session_list_tracker_pids(struct lttng_session *session) |
| 981 | { |
| 982 | struct file *tracker_pids_list_file; |
| 983 | struct seq_file *m; |
| 984 | int file_fd, ret; |
| 985 | |
| 986 | file_fd = lttng_get_unused_fd(); |
| 987 | if (file_fd < 0) { |
| 988 | ret = file_fd; |
| 989 | goto fd_error; |
| 990 | } |
| 991 | |
| 992 | tracker_pids_list_file = anon_inode_getfile("[lttng_tracker_pids_list]", |
| 993 | <tng_tracker_pids_list_fops, |
| 994 | NULL, O_RDWR); |
| 995 | if (IS_ERR(tracker_pids_list_file)) { |
| 996 | ret = PTR_ERR(tracker_pids_list_file); |
| 997 | goto file_error; |
| 998 | } |
| 999 | ret = lttng_tracker_pids_list_fops.open(NULL, tracker_pids_list_file); |
| 1000 | if (ret < 0) |
| 1001 | goto open_error; |
| 1002 | m = tracker_pids_list_file->private_data; |
| 1003 | m->private = session; |
| 1004 | fd_install(file_fd, tracker_pids_list_file); |
| 1005 | atomic_long_inc(&session->file->f_count); |
| 1006 | |
| 1007 | return file_fd; |
| 1008 | |
| 1009 | open_error: |
| 1010 | fput(tracker_pids_list_file); |
| 1011 | file_error: |
| 1012 | put_unused_fd(file_fd); |
| 1013 | fd_error: |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * Enabler management. |
| 1019 | */ |
| 1020 | static |
| 1021 | int lttng_match_enabler_wildcard(const char *desc_name, |
| 1022 | const char *name) |
| 1023 | { |
| 1024 | /* Compare excluding final '*' */ |
| 1025 | if (strncmp(desc_name, name, strlen(name) - 1)) |
| 1026 | return 0; |
| 1027 | return 1; |
| 1028 | } |
| 1029 | |
| 1030 | static |
| 1031 | int lttng_match_enabler_name(const char *desc_name, |
| 1032 | const char *name) |
| 1033 | { |
| 1034 | if (strcmp(desc_name, name)) |
| 1035 | return 0; |
| 1036 | return 1; |
| 1037 | } |
| 1038 | |
| 1039 | static |
| 1040 | int lttng_desc_match_enabler(const struct lttng_event_desc *desc, |
| 1041 | struct lttng_enabler *enabler) |
| 1042 | { |
| 1043 | const char *desc_name, *enabler_name; |
| 1044 | |
| 1045 | enabler_name = enabler->event_param.name; |
| 1046 | switch (enabler->event_param.instrumentation) { |
| 1047 | case LTTNG_KERNEL_TRACEPOINT: |
| 1048 | desc_name = desc->name; |
| 1049 | break; |
| 1050 | case LTTNG_KERNEL_SYSCALL: |
| 1051 | desc_name = desc->name; |
| 1052 | if (!strncmp(desc_name, "compat_", strlen("compat_"))) |
| 1053 | desc_name += strlen("compat_"); |
| 1054 | if (!strncmp(desc_name, "syscall_exit_", |
| 1055 | strlen("syscall_exit_"))) { |
| 1056 | desc_name += strlen("syscall_exit_"); |
| 1057 | } else if (!strncmp(desc_name, "syscall_entry_", |
| 1058 | strlen("syscall_entry_"))) { |
| 1059 | desc_name += strlen("syscall_entry_"); |
| 1060 | } else { |
| 1061 | WARN_ON_ONCE(1); |
| 1062 | return -EINVAL; |
| 1063 | } |
| 1064 | break; |
| 1065 | default: |
| 1066 | WARN_ON_ONCE(1); |
| 1067 | return -EINVAL; |
| 1068 | } |
| 1069 | switch (enabler->type) { |
| 1070 | case LTTNG_ENABLER_WILDCARD: |
| 1071 | return lttng_match_enabler_wildcard(desc_name, enabler_name); |
| 1072 | case LTTNG_ENABLER_NAME: |
| 1073 | return lttng_match_enabler_name(desc_name, enabler_name); |
| 1074 | default: |
| 1075 | return -EINVAL; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | static |
| 1080 | int lttng_event_match_enabler(struct lttng_event *event, |
| 1081 | struct lttng_enabler *enabler) |
| 1082 | { |
| 1083 | if (enabler->event_param.instrumentation != event->instrumentation) |
| 1084 | return 0; |
| 1085 | if (lttng_desc_match_enabler(event->desc, enabler) |
| 1086 | && event->chan == enabler->chan) |
| 1087 | return 1; |
| 1088 | else |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | static |
| 1093 | struct lttng_enabler_ref *lttng_event_enabler_ref(struct lttng_event *event, |
| 1094 | struct lttng_enabler *enabler) |
| 1095 | { |
| 1096 | struct lttng_enabler_ref *enabler_ref; |
| 1097 | |
| 1098 | list_for_each_entry(enabler_ref, |
| 1099 | &event->enablers_ref_head, node) { |
| 1100 | if (enabler_ref->ref == enabler) |
| 1101 | return enabler_ref; |
| 1102 | } |
| 1103 | return NULL; |
| 1104 | } |
| 1105 | |
| 1106 | static |
| 1107 | void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler) |
| 1108 | { |
| 1109 | struct lttng_session *session = enabler->chan->session; |
| 1110 | struct lttng_probe_desc *probe_desc; |
| 1111 | const struct lttng_event_desc *desc; |
| 1112 | int i; |
| 1113 | struct list_head *probe_list; |
| 1114 | |
| 1115 | probe_list = lttng_get_probe_list_head(); |
| 1116 | /* |
| 1117 | * For each probe event, if we find that a probe event matches |
| 1118 | * our enabler, create an associated lttng_event if not |
| 1119 | * already present. |
| 1120 | */ |
| 1121 | list_for_each_entry(probe_desc, probe_list, head) { |
| 1122 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 1123 | int found = 0; |
| 1124 | struct hlist_head *head; |
| 1125 | const char *event_name; |
| 1126 | size_t name_len; |
| 1127 | uint32_t hash; |
| 1128 | struct lttng_event *event; |
| 1129 | |
| 1130 | desc = probe_desc->event_desc[i]; |
| 1131 | if (!lttng_desc_match_enabler(desc, enabler)) |
| 1132 | continue; |
| 1133 | event_name = desc->name; |
| 1134 | name_len = strlen(event_name); |
| 1135 | |
| 1136 | /* |
| 1137 | * Check if already created. |
| 1138 | */ |
| 1139 | hash = jhash(event_name, name_len, 0); |
| 1140 | head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)]; |
| 1141 | lttng_hlist_for_each_entry(event, head, hlist) { |
| 1142 | if (event->desc == desc |
| 1143 | && event->chan == enabler->chan) |
| 1144 | found = 1; |
| 1145 | } |
| 1146 | if (found) |
| 1147 | continue; |
| 1148 | |
| 1149 | /* |
| 1150 | * We need to create an event for this |
| 1151 | * event probe. |
| 1152 | */ |
| 1153 | event = _lttng_event_create(enabler->chan, |
| 1154 | NULL, NULL, desc, |
| 1155 | LTTNG_KERNEL_TRACEPOINT); |
| 1156 | if (!event) { |
| 1157 | printk(KERN_INFO "Unable to create event %s\n", |
| 1158 | probe_desc->event_desc[i]->name); |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | static |
| 1165 | void lttng_create_syscall_if_missing(struct lttng_enabler *enabler) |
| 1166 | { |
| 1167 | int ret; |
| 1168 | |
| 1169 | ret = lttng_syscalls_register(enabler->chan, NULL); |
| 1170 | WARN_ON_ONCE(ret); |
| 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | * Create struct lttng_event if it is missing and present in the list of |
| 1175 | * tracepoint probes. |
| 1176 | * Should be called with sessions mutex held. |
| 1177 | */ |
| 1178 | static |
| 1179 | void lttng_create_event_if_missing(struct lttng_enabler *enabler) |
| 1180 | { |
| 1181 | switch (enabler->event_param.instrumentation) { |
| 1182 | case LTTNG_KERNEL_TRACEPOINT: |
| 1183 | lttng_create_tracepoint_if_missing(enabler); |
| 1184 | break; |
| 1185 | case LTTNG_KERNEL_SYSCALL: |
| 1186 | lttng_create_syscall_if_missing(enabler); |
| 1187 | break; |
| 1188 | default: |
| 1189 | WARN_ON_ONCE(1); |
| 1190 | break; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * Create events associated with an enabler (if not already present), |
| 1196 | * and add backward reference from the event to the enabler. |
| 1197 | * Should be called with sessions mutex held. |
| 1198 | */ |
| 1199 | static |
| 1200 | int lttng_enabler_ref_events(struct lttng_enabler *enabler) |
| 1201 | { |
| 1202 | struct lttng_session *session = enabler->chan->session; |
| 1203 | struct lttng_event *event; |
| 1204 | |
| 1205 | /* First ensure that probe events are created for this enabler. */ |
| 1206 | lttng_create_event_if_missing(enabler); |
| 1207 | |
| 1208 | /* For each event matching enabler in session event list. */ |
| 1209 | list_for_each_entry(event, &session->events, list) { |
| 1210 | struct lttng_enabler_ref *enabler_ref; |
| 1211 | |
| 1212 | if (!lttng_event_match_enabler(event, enabler)) |
| 1213 | continue; |
| 1214 | enabler_ref = lttng_event_enabler_ref(event, enabler); |
| 1215 | if (!enabler_ref) { |
| 1216 | /* |
| 1217 | * If no backward ref, create it. |
| 1218 | * Add backward ref from event to enabler. |
| 1219 | */ |
| 1220 | enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL); |
| 1221 | if (!enabler_ref) |
| 1222 | return -ENOMEM; |
| 1223 | enabler_ref->ref = enabler; |
| 1224 | list_add(&enabler_ref->node, |
| 1225 | &event->enablers_ref_head); |
| 1226 | } |
| 1227 | |
| 1228 | /* |
| 1229 | * Link filter bytecodes if not linked yet. |
| 1230 | */ |
| 1231 | lttng_enabler_event_link_bytecode(event, enabler); |
| 1232 | |
| 1233 | /* TODO: merge event context. */ |
| 1234 | } |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * Called at module load: connect the probe on all enablers matching |
| 1240 | * this event. |
| 1241 | * Called with sessions lock held. |
| 1242 | */ |
| 1243 | int lttng_fix_pending_events(void) |
| 1244 | { |
| 1245 | struct lttng_session *session; |
| 1246 | |
| 1247 | list_for_each_entry(session, &sessions, list) |
| 1248 | lttng_session_lazy_sync_enablers(session); |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type, |
| 1253 | struct lttng_kernel_event *event_param, |
| 1254 | struct lttng_channel *chan) |
| 1255 | { |
| 1256 | struct lttng_enabler *enabler; |
| 1257 | |
| 1258 | enabler = kzalloc(sizeof(*enabler), GFP_KERNEL); |
| 1259 | if (!enabler) |
| 1260 | return NULL; |
| 1261 | enabler->type = type; |
| 1262 | INIT_LIST_HEAD(&enabler->filter_bytecode_head); |
| 1263 | memcpy(&enabler->event_param, event_param, |
| 1264 | sizeof(enabler->event_param)); |
| 1265 | enabler->chan = chan; |
| 1266 | /* ctx left NULL */ |
| 1267 | enabler->enabled = 0; |
| 1268 | enabler->evtype = LTTNG_TYPE_ENABLER; |
| 1269 | mutex_lock(&sessions_mutex); |
| 1270 | list_add(&enabler->node, &enabler->chan->session->enablers_head); |
| 1271 | lttng_session_lazy_sync_enablers(enabler->chan->session); |
| 1272 | mutex_unlock(&sessions_mutex); |
| 1273 | return enabler; |
| 1274 | } |
| 1275 | |
| 1276 | int lttng_enabler_enable(struct lttng_enabler *enabler) |
| 1277 | { |
| 1278 | mutex_lock(&sessions_mutex); |
| 1279 | enabler->enabled = 1; |
| 1280 | lttng_session_lazy_sync_enablers(enabler->chan->session); |
| 1281 | mutex_unlock(&sessions_mutex); |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | int lttng_enabler_disable(struct lttng_enabler *enabler) |
| 1286 | { |
| 1287 | mutex_lock(&sessions_mutex); |
| 1288 | enabler->enabled = 0; |
| 1289 | lttng_session_lazy_sync_enablers(enabler->chan->session); |
| 1290 | mutex_unlock(&sessions_mutex); |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler, |
| 1295 | struct lttng_kernel_filter_bytecode __user *bytecode) |
| 1296 | { |
| 1297 | struct lttng_filter_bytecode_node *bytecode_node; |
| 1298 | uint32_t bytecode_len; |
| 1299 | int ret; |
| 1300 | |
| 1301 | ret = get_user(bytecode_len, &bytecode->len); |
| 1302 | if (ret) |
| 1303 | return ret; |
| 1304 | bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len, |
| 1305 | GFP_KERNEL); |
| 1306 | if (!bytecode_node) |
| 1307 | return -ENOMEM; |
| 1308 | ret = copy_from_user(&bytecode_node->bc, bytecode, |
| 1309 | sizeof(*bytecode) + bytecode_len); |
| 1310 | if (ret) |
| 1311 | goto error_free; |
| 1312 | bytecode_node->enabler = enabler; |
| 1313 | /* Enforce length based on allocated size */ |
| 1314 | bytecode_node->bc.len = bytecode_len; |
| 1315 | list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head); |
| 1316 | lttng_session_lazy_sync_enablers(enabler->chan->session); |
| 1317 | return 0; |
| 1318 | |
| 1319 | error_free: |
| 1320 | kfree(bytecode_node); |
| 1321 | return ret; |
| 1322 | } |
| 1323 | |
| 1324 | int lttng_enabler_attach_context(struct lttng_enabler *enabler, |
| 1325 | struct lttng_kernel_context *context_param) |
| 1326 | { |
| 1327 | return -ENOSYS; |
| 1328 | } |
| 1329 | |
| 1330 | static |
| 1331 | void lttng_enabler_destroy(struct lttng_enabler *enabler) |
| 1332 | { |
| 1333 | struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node; |
| 1334 | |
| 1335 | /* Destroy filter bytecode */ |
| 1336 | list_for_each_entry_safe(filter_node, tmp_filter_node, |
| 1337 | &enabler->filter_bytecode_head, node) { |
| 1338 | kfree(filter_node); |
| 1339 | } |
| 1340 | |
| 1341 | /* Destroy contexts */ |
| 1342 | lttng_destroy_context(enabler->ctx); |
| 1343 | |
| 1344 | list_del(&enabler->node); |
| 1345 | kfree(enabler); |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * lttng_session_sync_enablers should be called just before starting a |
| 1350 | * session. |
| 1351 | * Should be called with sessions mutex held. |
| 1352 | */ |
| 1353 | static |
| 1354 | void lttng_session_sync_enablers(struct lttng_session *session) |
| 1355 | { |
| 1356 | struct lttng_enabler *enabler; |
| 1357 | struct lttng_event *event; |
| 1358 | |
| 1359 | list_for_each_entry(enabler, &session->enablers_head, node) |
| 1360 | lttng_enabler_ref_events(enabler); |
| 1361 | /* |
| 1362 | * For each event, if at least one of its enablers is enabled, |
| 1363 | * and its channel and session transient states are enabled, we |
| 1364 | * enable the event, else we disable it. |
| 1365 | */ |
| 1366 | list_for_each_entry(event, &session->events, list) { |
| 1367 | struct lttng_enabler_ref *enabler_ref; |
| 1368 | struct lttng_bytecode_runtime *runtime; |
| 1369 | int enabled = 0, has_enablers_without_bytecode = 0; |
| 1370 | |
| 1371 | switch (event->instrumentation) { |
| 1372 | case LTTNG_KERNEL_TRACEPOINT: |
| 1373 | case LTTNG_KERNEL_SYSCALL: |
| 1374 | /* Enable events */ |
| 1375 | list_for_each_entry(enabler_ref, |
| 1376 | &event->enablers_ref_head, node) { |
| 1377 | if (enabler_ref->ref->enabled) { |
| 1378 | enabled = 1; |
| 1379 | break; |
| 1380 | } |
| 1381 | } |
| 1382 | break; |
| 1383 | default: |
| 1384 | /* Not handled with lazy sync. */ |
| 1385 | continue; |
| 1386 | } |
| 1387 | /* |
| 1388 | * Enabled state is based on union of enablers, with |
| 1389 | * intesection of session and channel transient enable |
| 1390 | * states. |
| 1391 | */ |
| 1392 | enabled = enabled && session->tstate && event->chan->tstate; |
| 1393 | |
| 1394 | ACCESS_ONCE(event->enabled) = enabled; |
| 1395 | /* |
| 1396 | * Sync tracepoint registration with event enabled |
| 1397 | * state. |
| 1398 | */ |
| 1399 | if (enabled) { |
| 1400 | register_event(event); |
| 1401 | } else { |
| 1402 | _lttng_event_unregister(event); |
| 1403 | } |
| 1404 | |
| 1405 | /* Check if has enablers without bytecode enabled */ |
| 1406 | list_for_each_entry(enabler_ref, |
| 1407 | &event->enablers_ref_head, node) { |
| 1408 | if (enabler_ref->ref->enabled |
| 1409 | && list_empty(&enabler_ref->ref->filter_bytecode_head)) { |
| 1410 | has_enablers_without_bytecode = 1; |
| 1411 | break; |
| 1412 | } |
| 1413 | } |
| 1414 | event->has_enablers_without_bytecode = |
| 1415 | has_enablers_without_bytecode; |
| 1416 | |
| 1417 | /* Enable filters */ |
| 1418 | list_for_each_entry(runtime, |
| 1419 | &event->bytecode_runtime_head, node) |
| 1420 | lttng_filter_sync_state(runtime); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Apply enablers to session events, adding events to session if need |
| 1426 | * be. It is required after each modification applied to an active |
| 1427 | * session, and right before session "start". |
| 1428 | * "lazy" sync means we only sync if required. |
| 1429 | * Should be called with sessions mutex held. |
| 1430 | */ |
| 1431 | static |
| 1432 | void lttng_session_lazy_sync_enablers(struct lttng_session *session) |
| 1433 | { |
| 1434 | /* We can skip if session is not active */ |
| 1435 | if (!session->active) |
| 1436 | return; |
| 1437 | lttng_session_sync_enablers(session); |
| 1438 | } |
| 1439 | |
| 1440 | /* |
| 1441 | * Serialize at most one packet worth of metadata into a metadata |
| 1442 | * channel. |
| 1443 | * We grab the metadata cache mutex to get exclusive access to our metadata |
| 1444 | * buffer and to the metadata cache. Exclusive access to the metadata buffer |
| 1445 | * allows us to do racy operations such as looking for remaining space left in |
| 1446 | * packet and write, since mutual exclusion protects us from concurrent writes. |
| 1447 | * Mutual exclusion on the metadata cache allow us to read the cache content |
| 1448 | * without racing against reallocation of the cache by updates. |
| 1449 | * Returns the number of bytes written in the channel, 0 if no data |
| 1450 | * was written and a negative value on error. |
| 1451 | */ |
| 1452 | int lttng_metadata_output_channel(struct lttng_metadata_stream *stream, |
| 1453 | struct channel *chan) |
| 1454 | { |
| 1455 | struct lib_ring_buffer_ctx ctx; |
| 1456 | int ret = 0; |
| 1457 | size_t len, reserve_len; |
| 1458 | |
| 1459 | /* |
| 1460 | * Ensure we support mutiple get_next / put sequences followed by |
| 1461 | * put_next. The metadata cache lock protects reading the metadata |
| 1462 | * cache. It can indeed be read concurrently by "get_next_subbuf" and |
| 1463 | * "flush" operations on the buffer invoked by different processes. |
| 1464 | * Moreover, since the metadata cache memory can be reallocated, we |
| 1465 | * need to have exclusive access against updates even though we only |
| 1466 | * read it. |
| 1467 | */ |
| 1468 | mutex_lock(&stream->metadata_cache->lock); |
| 1469 | WARN_ON(stream->metadata_in < stream->metadata_out); |
| 1470 | if (stream->metadata_in != stream->metadata_out) |
| 1471 | goto end; |
| 1472 | |
| 1473 | len = stream->metadata_cache->metadata_written - |
| 1474 | stream->metadata_in; |
| 1475 | if (!len) |
| 1476 | goto end; |
| 1477 | reserve_len = min_t(size_t, |
| 1478 | stream->transport->ops.packet_avail_size(chan), |
| 1479 | len); |
| 1480 | lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len, |
| 1481 | sizeof(char), -1); |
| 1482 | /* |
| 1483 | * If reservation failed, return an error to the caller. |
| 1484 | */ |
| 1485 | ret = stream->transport->ops.event_reserve(&ctx, 0); |
| 1486 | if (ret != 0) { |
| 1487 | printk(KERN_WARNING "LTTng: Metadata event reservation failed\n"); |
| 1488 | goto end; |
| 1489 | } |
| 1490 | stream->transport->ops.event_write(&ctx, |
| 1491 | stream->metadata_cache->data + stream->metadata_in, |
| 1492 | reserve_len); |
| 1493 | stream->transport->ops.event_commit(&ctx); |
| 1494 | stream->metadata_in += reserve_len; |
| 1495 | ret = reserve_len; |
| 1496 | |
| 1497 | end: |
| 1498 | mutex_unlock(&stream->metadata_cache->lock); |
| 1499 | return ret; |
| 1500 | } |
| 1501 | |
| 1502 | /* |
| 1503 | * Write the metadata to the metadata cache. |
| 1504 | * Must be called with sessions_mutex held. |
| 1505 | * The metadata cache lock protects us from concurrent read access from |
| 1506 | * thread outputting metadata content to ring buffer. |
| 1507 | */ |
| 1508 | int lttng_metadata_printf(struct lttng_session *session, |
| 1509 | const char *fmt, ...) |
| 1510 | { |
| 1511 | char *str; |
| 1512 | size_t len; |
| 1513 | va_list ap; |
| 1514 | struct lttng_metadata_stream *stream; |
| 1515 | |
| 1516 | WARN_ON_ONCE(!ACCESS_ONCE(session->active)); |
| 1517 | |
| 1518 | va_start(ap, fmt); |
| 1519 | str = kvasprintf(GFP_KERNEL, fmt, ap); |
| 1520 | va_end(ap); |
| 1521 | if (!str) |
| 1522 | return -ENOMEM; |
| 1523 | |
| 1524 | len = strlen(str); |
| 1525 | mutex_lock(&session->metadata_cache->lock); |
| 1526 | if (session->metadata_cache->metadata_written + len > |
| 1527 | session->metadata_cache->cache_alloc) { |
| 1528 | char *tmp_cache_realloc; |
| 1529 | unsigned int tmp_cache_alloc_size; |
| 1530 | |
| 1531 | tmp_cache_alloc_size = max_t(unsigned int, |
| 1532 | session->metadata_cache->cache_alloc + len, |
| 1533 | session->metadata_cache->cache_alloc << 1); |
| 1534 | tmp_cache_realloc = vzalloc(tmp_cache_alloc_size); |
| 1535 | if (!tmp_cache_realloc) |
| 1536 | goto err; |
| 1537 | if (session->metadata_cache->data) { |
| 1538 | memcpy(tmp_cache_realloc, |
| 1539 | session->metadata_cache->data, |
| 1540 | session->metadata_cache->cache_alloc); |
| 1541 | vfree(session->metadata_cache->data); |
| 1542 | } |
| 1543 | |
| 1544 | session->metadata_cache->cache_alloc = tmp_cache_alloc_size; |
| 1545 | session->metadata_cache->data = tmp_cache_realloc; |
| 1546 | } |
| 1547 | memcpy(session->metadata_cache->data + |
| 1548 | session->metadata_cache->metadata_written, |
| 1549 | str, len); |
| 1550 | session->metadata_cache->metadata_written += len; |
| 1551 | mutex_unlock(&session->metadata_cache->lock); |
| 1552 | kfree(str); |
| 1553 | |
| 1554 | list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) |
| 1555 | wake_up_interruptible(&stream->read_wait); |
| 1556 | |
| 1557 | return 0; |
| 1558 | |
| 1559 | err: |
| 1560 | mutex_unlock(&session->metadata_cache->lock); |
| 1561 | kfree(str); |
| 1562 | return -ENOMEM; |
| 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * Must be called with sessions_mutex held. |
| 1567 | */ |
| 1568 | static |
| 1569 | int _lttng_field_statedump(struct lttng_session *session, |
| 1570 | const struct lttng_event_field *field) |
| 1571 | { |
| 1572 | int ret = 0; |
| 1573 | |
| 1574 | switch (field->type.atype) { |
| 1575 | case atype_integer: |
| 1576 | ret = lttng_metadata_printf(session, |
| 1577 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
| 1578 | field->type.u.basic.integer.size, |
| 1579 | field->type.u.basic.integer.alignment, |
| 1580 | field->type.u.basic.integer.signedness, |
| 1581 | (field->type.u.basic.integer.encoding == lttng_encode_none) |
| 1582 | ? "none" |
| 1583 | : (field->type.u.basic.integer.encoding == lttng_encode_UTF8) |
| 1584 | ? "UTF8" |
| 1585 | : "ASCII", |
| 1586 | field->type.u.basic.integer.base, |
| 1587 | #ifdef __BIG_ENDIAN |
| 1588 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 1589 | #else |
| 1590 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 1591 | #endif |
| 1592 | field->name); |
| 1593 | break; |
| 1594 | case atype_enum: |
| 1595 | ret = lttng_metadata_printf(session, |
| 1596 | " %s _%s;\n", |
| 1597 | field->type.u.basic.enumeration.name, |
| 1598 | field->name); |
| 1599 | break; |
| 1600 | case atype_array: |
| 1601 | { |
| 1602 | const struct lttng_basic_type *elem_type; |
| 1603 | |
| 1604 | elem_type = &field->type.u.array.elem_type; |
| 1605 | ret = lttng_metadata_printf(session, |
| 1606 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
| 1607 | elem_type->u.basic.integer.size, |
| 1608 | elem_type->u.basic.integer.alignment, |
| 1609 | elem_type->u.basic.integer.signedness, |
| 1610 | (elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 1611 | ? "none" |
| 1612 | : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 1613 | ? "UTF8" |
| 1614 | : "ASCII", |
| 1615 | elem_type->u.basic.integer.base, |
| 1616 | #ifdef __BIG_ENDIAN |
| 1617 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 1618 | #else |
| 1619 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 1620 | #endif |
| 1621 | field->name, field->type.u.array.length); |
| 1622 | break; |
| 1623 | } |
| 1624 | case atype_sequence: |
| 1625 | { |
| 1626 | const struct lttng_basic_type *elem_type; |
| 1627 | const struct lttng_basic_type *length_type; |
| 1628 | |
| 1629 | elem_type = &field->type.u.sequence.elem_type; |
| 1630 | length_type = &field->type.u.sequence.length_type; |
| 1631 | ret = lttng_metadata_printf(session, |
| 1632 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", |
| 1633 | length_type->u.basic.integer.size, |
| 1634 | (unsigned int) length_type->u.basic.integer.alignment, |
| 1635 | length_type->u.basic.integer.signedness, |
| 1636 | (length_type->u.basic.integer.encoding == lttng_encode_none) |
| 1637 | ? "none" |
| 1638 | : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 1639 | ? "UTF8" |
| 1640 | : "ASCII"), |
| 1641 | length_type->u.basic.integer.base, |
| 1642 | #ifdef __BIG_ENDIAN |
| 1643 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 1644 | #else |
| 1645 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 1646 | #endif |
| 1647 | field->name); |
| 1648 | if (ret) |
| 1649 | return ret; |
| 1650 | |
| 1651 | ret = lttng_metadata_printf(session, |
| 1652 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
| 1653 | elem_type->u.basic.integer.size, |
| 1654 | (unsigned int) elem_type->u.basic.integer.alignment, |
| 1655 | elem_type->u.basic.integer.signedness, |
| 1656 | (elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 1657 | ? "none" |
| 1658 | : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
| 1659 | ? "UTF8" |
| 1660 | : "ASCII"), |
| 1661 | elem_type->u.basic.integer.base, |
| 1662 | #ifdef __BIG_ENDIAN |
| 1663 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
| 1664 | #else |
| 1665 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
| 1666 | #endif |
| 1667 | field->name, |
| 1668 | field->name); |
| 1669 | break; |
| 1670 | } |
| 1671 | |
| 1672 | case atype_string: |
| 1673 | /* Default encoding is UTF8 */ |
| 1674 | ret = lttng_metadata_printf(session, |
| 1675 | " string%s _%s;\n", |
| 1676 | field->type.u.basic.string.encoding == lttng_encode_ASCII ? |
| 1677 | " { encoding = ASCII; }" : "", |
| 1678 | field->name); |
| 1679 | break; |
| 1680 | default: |
| 1681 | WARN_ON_ONCE(1); |
| 1682 | return -EINVAL; |
| 1683 | } |
| 1684 | return ret; |
| 1685 | } |
| 1686 | |
| 1687 | static |
| 1688 | int _lttng_context_metadata_statedump(struct lttng_session *session, |
| 1689 | struct lttng_ctx *ctx) |
| 1690 | { |
| 1691 | int ret = 0; |
| 1692 | int i; |
| 1693 | |
| 1694 | if (!ctx) |
| 1695 | return 0; |
| 1696 | for (i = 0; i < ctx->nr_fields; i++) { |
| 1697 | const struct lttng_ctx_field *field = &ctx->fields[i]; |
| 1698 | |
| 1699 | ret = _lttng_field_statedump(session, &field->event_field); |
| 1700 | if (ret) |
| 1701 | return ret; |
| 1702 | } |
| 1703 | return ret; |
| 1704 | } |
| 1705 | |
| 1706 | static |
| 1707 | int _lttng_fields_metadata_statedump(struct lttng_session *session, |
| 1708 | struct lttng_event *event) |
| 1709 | { |
| 1710 | const struct lttng_event_desc *desc = event->desc; |
| 1711 | int ret = 0; |
| 1712 | int i; |
| 1713 | |
| 1714 | for (i = 0; i < desc->nr_fields; i++) { |
| 1715 | const struct lttng_event_field *field = &desc->fields[i]; |
| 1716 | |
| 1717 | ret = _lttng_field_statedump(session, field); |
| 1718 | if (ret) |
| 1719 | return ret; |
| 1720 | } |
| 1721 | return ret; |
| 1722 | } |
| 1723 | |
| 1724 | /* |
| 1725 | * Must be called with sessions_mutex held. |
| 1726 | */ |
| 1727 | static |
| 1728 | int _lttng_event_metadata_statedump(struct lttng_session *session, |
| 1729 | struct lttng_channel *chan, |
| 1730 | struct lttng_event *event) |
| 1731 | { |
| 1732 | int ret = 0; |
| 1733 | |
| 1734 | if (event->metadata_dumped || !ACCESS_ONCE(session->active)) |
| 1735 | return 0; |
| 1736 | if (chan->channel_type == METADATA_CHANNEL) |
| 1737 | return 0; |
| 1738 | |
| 1739 | ret = lttng_metadata_printf(session, |
| 1740 | "event {\n" |
| 1741 | " name = \"%s\";\n" |
| 1742 | " id = %u;\n" |
| 1743 | " stream_id = %u;\n", |
| 1744 | event->desc->name, |
| 1745 | event->id, |
| 1746 | event->chan->id); |
| 1747 | if (ret) |
| 1748 | goto end; |
| 1749 | |
| 1750 | if (event->ctx) { |
| 1751 | ret = lttng_metadata_printf(session, |
| 1752 | " context := struct {\n"); |
| 1753 | if (ret) |
| 1754 | goto end; |
| 1755 | } |
| 1756 | ret = _lttng_context_metadata_statedump(session, event->ctx); |
| 1757 | if (ret) |
| 1758 | goto end; |
| 1759 | if (event->ctx) { |
| 1760 | ret = lttng_metadata_printf(session, |
| 1761 | " };\n"); |
| 1762 | if (ret) |
| 1763 | goto end; |
| 1764 | } |
| 1765 | |
| 1766 | ret = lttng_metadata_printf(session, |
| 1767 | " fields := struct {\n" |
| 1768 | ); |
| 1769 | if (ret) |
| 1770 | goto end; |
| 1771 | |
| 1772 | ret = _lttng_fields_metadata_statedump(session, event); |
| 1773 | if (ret) |
| 1774 | goto end; |
| 1775 | |
| 1776 | /* |
| 1777 | * LTTng space reservation can only reserve multiples of the |
| 1778 | * byte size. |
| 1779 | */ |
| 1780 | ret = lttng_metadata_printf(session, |
| 1781 | " };\n" |
| 1782 | "};\n\n"); |
| 1783 | if (ret) |
| 1784 | goto end; |
| 1785 | |
| 1786 | event->metadata_dumped = 1; |
| 1787 | end: |
| 1788 | return ret; |
| 1789 | |
| 1790 | } |
| 1791 | |
| 1792 | /* |
| 1793 | * Must be called with sessions_mutex held. |
| 1794 | */ |
| 1795 | static |
| 1796 | int _lttng_channel_metadata_statedump(struct lttng_session *session, |
| 1797 | struct lttng_channel *chan) |
| 1798 | { |
| 1799 | int ret = 0; |
| 1800 | |
| 1801 | if (chan->metadata_dumped || !ACCESS_ONCE(session->active)) |
| 1802 | return 0; |
| 1803 | |
| 1804 | if (chan->channel_type == METADATA_CHANNEL) |
| 1805 | return 0; |
| 1806 | |
| 1807 | WARN_ON_ONCE(!chan->header_type); |
| 1808 | ret = lttng_metadata_printf(session, |
| 1809 | "stream {\n" |
| 1810 | " id = %u;\n" |
| 1811 | " event.header := %s;\n" |
| 1812 | " packet.context := struct packet_context;\n", |
| 1813 | chan->id, |
| 1814 | chan->header_type == 1 ? "struct event_header_compact" : |
| 1815 | "struct event_header_large"); |
| 1816 | if (ret) |
| 1817 | goto end; |
| 1818 | |
| 1819 | if (chan->ctx) { |
| 1820 | ret = lttng_metadata_printf(session, |
| 1821 | " event.context := struct {\n"); |
| 1822 | if (ret) |
| 1823 | goto end; |
| 1824 | } |
| 1825 | ret = _lttng_context_metadata_statedump(session, chan->ctx); |
| 1826 | if (ret) |
| 1827 | goto end; |
| 1828 | if (chan->ctx) { |
| 1829 | ret = lttng_metadata_printf(session, |
| 1830 | " };\n"); |
| 1831 | if (ret) |
| 1832 | goto end; |
| 1833 | } |
| 1834 | |
| 1835 | ret = lttng_metadata_printf(session, |
| 1836 | "};\n\n"); |
| 1837 | |
| 1838 | chan->metadata_dumped = 1; |
| 1839 | end: |
| 1840 | return ret; |
| 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | * Must be called with sessions_mutex held. |
| 1845 | */ |
| 1846 | static |
| 1847 | int _lttng_stream_packet_context_declare(struct lttng_session *session) |
| 1848 | { |
| 1849 | return lttng_metadata_printf(session, |
| 1850 | "struct packet_context {\n" |
| 1851 | " uint64_clock_monotonic_t timestamp_begin;\n" |
| 1852 | " uint64_clock_monotonic_t timestamp_end;\n" |
| 1853 | " uint64_t content_size;\n" |
| 1854 | " uint64_t packet_size;\n" |
| 1855 | " uint64_t packet_seq_num;\n" |
| 1856 | " unsigned long events_discarded;\n" |
| 1857 | " uint32_t cpu_id;\n" |
| 1858 | "};\n\n" |
| 1859 | ); |
| 1860 | } |
| 1861 | |
| 1862 | /* |
| 1863 | * Compact header: |
| 1864 | * id: range: 0 - 30. |
| 1865 | * id 31 is reserved to indicate an extended header. |
| 1866 | * |
| 1867 | * Large header: |
| 1868 | * id: range: 0 - 65534. |
| 1869 | * id 65535 is reserved to indicate an extended header. |
| 1870 | * |
| 1871 | * Must be called with sessions_mutex held. |
| 1872 | */ |
| 1873 | static |
| 1874 | int _lttng_event_header_declare(struct lttng_session *session) |
| 1875 | { |
| 1876 | return lttng_metadata_printf(session, |
| 1877 | "struct event_header_compact {\n" |
| 1878 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" |
| 1879 | " variant <id> {\n" |
| 1880 | " struct {\n" |
| 1881 | " uint27_clock_monotonic_t timestamp;\n" |
| 1882 | " } compact;\n" |
| 1883 | " struct {\n" |
| 1884 | " uint32_t id;\n" |
| 1885 | " uint64_clock_monotonic_t timestamp;\n" |
| 1886 | " } extended;\n" |
| 1887 | " } v;\n" |
| 1888 | "} align(%u);\n" |
| 1889 | "\n" |
| 1890 | "struct event_header_large {\n" |
| 1891 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" |
| 1892 | " variant <id> {\n" |
| 1893 | " struct {\n" |
| 1894 | " uint32_clock_monotonic_t timestamp;\n" |
| 1895 | " } compact;\n" |
| 1896 | " struct {\n" |
| 1897 | " uint32_t id;\n" |
| 1898 | " uint64_clock_monotonic_t timestamp;\n" |
| 1899 | " } extended;\n" |
| 1900 | " } v;\n" |
| 1901 | "} align(%u);\n\n", |
| 1902 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 1903 | lttng_alignof(uint16_t) * CHAR_BIT |
| 1904 | ); |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Approximation of NTP time of day to clock monotonic correlation, |
| 1909 | * taken at start of trace. |
| 1910 | * Yes, this is only an approximation. Yes, we can (and will) do better |
| 1911 | * in future versions. |
| 1912 | */ |
| 1913 | static |
| 1914 | uint64_t measure_clock_offset(void) |
| 1915 | { |
| 1916 | uint64_t offset, monotonic[2], realtime; |
| 1917 | struct timespec rts = { 0, 0 }; |
| 1918 | unsigned long flags; |
| 1919 | |
| 1920 | /* Disable interrupts to increase correlation precision. */ |
| 1921 | local_irq_save(flags); |
| 1922 | monotonic[0] = trace_clock_read64(); |
| 1923 | getnstimeofday(&rts); |
| 1924 | monotonic[1] = trace_clock_read64(); |
| 1925 | local_irq_restore(flags); |
| 1926 | |
| 1927 | offset = (monotonic[0] + monotonic[1]) >> 1; |
| 1928 | realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC; |
| 1929 | realtime += rts.tv_nsec; |
| 1930 | offset = realtime - offset; |
| 1931 | return offset; |
| 1932 | } |
| 1933 | |
| 1934 | /* |
| 1935 | * Output metadata into this session's metadata buffers. |
| 1936 | * Must be called with sessions_mutex held. |
| 1937 | */ |
| 1938 | static |
| 1939 | int _lttng_session_metadata_statedump(struct lttng_session *session) |
| 1940 | { |
| 1941 | unsigned char *uuid_c = session->uuid.b; |
| 1942 | unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN]; |
| 1943 | struct lttng_channel *chan; |
| 1944 | struct lttng_event *event; |
| 1945 | int ret = 0; |
| 1946 | |
| 1947 | if (!ACCESS_ONCE(session->active)) |
| 1948 | return 0; |
| 1949 | if (session->metadata_dumped) |
| 1950 | goto skip_session; |
| 1951 | |
| 1952 | snprintf(uuid_s, sizeof(uuid_s), |
| 1953 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 1954 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], |
| 1955 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], |
| 1956 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], |
| 1957 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); |
| 1958 | |
| 1959 | ret = lttng_metadata_printf(session, |
| 1960 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
| 1961 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" |
| 1962 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" |
| 1963 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" |
| 1964 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" |
| 1965 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" |
| 1966 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" |
| 1967 | "\n" |
| 1968 | "trace {\n" |
| 1969 | " major = %u;\n" |
| 1970 | " minor = %u;\n" |
| 1971 | " uuid = \"%s\";\n" |
| 1972 | " byte_order = %s;\n" |
| 1973 | " packet.header := struct {\n" |
| 1974 | " uint32_t magic;\n" |
| 1975 | " uint8_t uuid[16];\n" |
| 1976 | " uint32_t stream_id;\n" |
| 1977 | " uint64_t stream_instance_id;\n" |
| 1978 | " };\n" |
| 1979 | "};\n\n", |
| 1980 | lttng_alignof(uint8_t) * CHAR_BIT, |
| 1981 | lttng_alignof(uint16_t) * CHAR_BIT, |
| 1982 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 1983 | lttng_alignof(uint64_t) * CHAR_BIT, |
| 1984 | sizeof(unsigned long) * CHAR_BIT, |
| 1985 | lttng_alignof(unsigned long) * CHAR_BIT, |
| 1986 | CTF_SPEC_MAJOR, |
| 1987 | CTF_SPEC_MINOR, |
| 1988 | uuid_s, |
| 1989 | #ifdef __BIG_ENDIAN |
| 1990 | "be" |
| 1991 | #else |
| 1992 | "le" |
| 1993 | #endif |
| 1994 | ); |
| 1995 | if (ret) |
| 1996 | goto end; |
| 1997 | |
| 1998 | ret = lttng_metadata_printf(session, |
| 1999 | "env {\n" |
| 2000 | " hostname = \"%s\";\n" |
| 2001 | " domain = \"kernel\";\n" |
| 2002 | " sysname = \"%s\";\n" |
| 2003 | " kernel_release = \"%s\";\n" |
| 2004 | " kernel_version = \"%s\";\n" |
| 2005 | " tracer_name = \"lttng-modules\";\n" |
| 2006 | " tracer_major = %d;\n" |
| 2007 | " tracer_minor = %d;\n" |
| 2008 | " tracer_patchlevel = %d;\n" |
| 2009 | "};\n\n", |
| 2010 | current->nsproxy->uts_ns->name.nodename, |
| 2011 | utsname()->sysname, |
| 2012 | utsname()->release, |
| 2013 | utsname()->version, |
| 2014 | LTTNG_MODULES_MAJOR_VERSION, |
| 2015 | LTTNG_MODULES_MINOR_VERSION, |
| 2016 | LTTNG_MODULES_PATCHLEVEL_VERSION |
| 2017 | ); |
| 2018 | if (ret) |
| 2019 | goto end; |
| 2020 | |
| 2021 | ret = lttng_metadata_printf(session, |
| 2022 | "clock {\n" |
| 2023 | " name = %s;\n", |
| 2024 | "monotonic" |
| 2025 | ); |
| 2026 | if (ret) |
| 2027 | goto end; |
| 2028 | |
| 2029 | if (!trace_clock_uuid(clock_uuid_s)) { |
| 2030 | ret = lttng_metadata_printf(session, |
| 2031 | " uuid = \"%s\";\n", |
| 2032 | clock_uuid_s |
| 2033 | ); |
| 2034 | if (ret) |
| 2035 | goto end; |
| 2036 | } |
| 2037 | |
| 2038 | ret = lttng_metadata_printf(session, |
| 2039 | " description = \"Monotonic Clock\";\n" |
| 2040 | " freq = %llu; /* Frequency, in Hz */\n" |
| 2041 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" |
| 2042 | " offset = %llu;\n" |
| 2043 | "};\n\n", |
| 2044 | (unsigned long long) trace_clock_freq(), |
| 2045 | (unsigned long long) measure_clock_offset() |
| 2046 | ); |
| 2047 | if (ret) |
| 2048 | goto end; |
| 2049 | |
| 2050 | ret = lttng_metadata_printf(session, |
| 2051 | "typealias integer {\n" |
| 2052 | " size = 27; align = 1; signed = false;\n" |
| 2053 | " map = clock.monotonic.value;\n" |
| 2054 | "} := uint27_clock_monotonic_t;\n" |
| 2055 | "\n" |
| 2056 | "typealias integer {\n" |
| 2057 | " size = 32; align = %u; signed = false;\n" |
| 2058 | " map = clock.monotonic.value;\n" |
| 2059 | "} := uint32_clock_monotonic_t;\n" |
| 2060 | "\n" |
| 2061 | "typealias integer {\n" |
| 2062 | " size = 64; align = %u; signed = false;\n" |
| 2063 | " map = clock.monotonic.value;\n" |
| 2064 | "} := uint64_clock_monotonic_t;\n\n", |
| 2065 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 2066 | lttng_alignof(uint64_t) * CHAR_BIT |
| 2067 | ); |
| 2068 | if (ret) |
| 2069 | goto end; |
| 2070 | |
| 2071 | ret = _lttng_stream_packet_context_declare(session); |
| 2072 | if (ret) |
| 2073 | goto end; |
| 2074 | |
| 2075 | ret = _lttng_event_header_declare(session); |
| 2076 | if (ret) |
| 2077 | goto end; |
| 2078 | |
| 2079 | skip_session: |
| 2080 | list_for_each_entry(chan, &session->chan, list) { |
| 2081 | ret = _lttng_channel_metadata_statedump(session, chan); |
| 2082 | if (ret) |
| 2083 | goto end; |
| 2084 | } |
| 2085 | |
| 2086 | list_for_each_entry(event, &session->events, list) { |
| 2087 | ret = _lttng_event_metadata_statedump(session, event->chan, event); |
| 2088 | if (ret) |
| 2089 | goto end; |
| 2090 | } |
| 2091 | session->metadata_dumped = 1; |
| 2092 | end: |
| 2093 | return ret; |
| 2094 | } |
| 2095 | |
| 2096 | /** |
| 2097 | * lttng_transport_register - LTT transport registration |
| 2098 | * @transport: transport structure |
| 2099 | * |
| 2100 | * Registers a transport which can be used as output to extract the data out of |
| 2101 | * LTTng. The module calling this registration function must ensure that no |
| 2102 | * trap-inducing code will be executed by the transport functions. E.g. |
| 2103 | * vmalloc_sync_all() must be called between a vmalloc and the moment the memory |
| 2104 | * is made visible to the transport function. This registration acts as a |
| 2105 | * vmalloc_sync_all. Therefore, only if the module allocates virtual memory |
| 2106 | * after its registration must it synchronize the TLBs. |
| 2107 | */ |
| 2108 | void lttng_transport_register(struct lttng_transport *transport) |
| 2109 | { |
| 2110 | /* |
| 2111 | * Make sure no page fault can be triggered by the module about to be |
| 2112 | * registered. We deal with this here so we don't have to call |
| 2113 | * vmalloc_sync_all() in each module's init. |
| 2114 | */ |
| 2115 | wrapper_vmalloc_sync_all(); |
| 2116 | |
| 2117 | mutex_lock(&sessions_mutex); |
| 2118 | list_add_tail(&transport->node, <tng_transport_list); |
| 2119 | mutex_unlock(&sessions_mutex); |
| 2120 | } |
| 2121 | EXPORT_SYMBOL_GPL(lttng_transport_register); |
| 2122 | |
| 2123 | /** |
| 2124 | * lttng_transport_unregister - LTT transport unregistration |
| 2125 | * @transport: transport structure |
| 2126 | */ |
| 2127 | void lttng_transport_unregister(struct lttng_transport *transport) |
| 2128 | { |
| 2129 | mutex_lock(&sessions_mutex); |
| 2130 | list_del(&transport->node); |
| 2131 | mutex_unlock(&sessions_mutex); |
| 2132 | } |
| 2133 | EXPORT_SYMBOL_GPL(lttng_transport_unregister); |
| 2134 | |
| 2135 | static int __init lttng_events_init(void) |
| 2136 | { |
| 2137 | int ret; |
| 2138 | |
| 2139 | ret = wrapper_lttng_fixup_sig(THIS_MODULE); |
| 2140 | if (ret) |
| 2141 | return ret; |
| 2142 | ret = wrapper_get_pfnblock_flags_mask_init(); |
| 2143 | if (ret) |
| 2144 | return ret; |
| 2145 | ret = lttng_context_init(); |
| 2146 | if (ret) |
| 2147 | return ret; |
| 2148 | ret = lttng_tracepoint_init(); |
| 2149 | if (ret) |
| 2150 | goto error_tp; |
| 2151 | event_cache = KMEM_CACHE(lttng_event, 0); |
| 2152 | if (!event_cache) { |
| 2153 | ret = -ENOMEM; |
| 2154 | goto error_kmem; |
| 2155 | } |
| 2156 | ret = lttng_abi_init(); |
| 2157 | if (ret) |
| 2158 | goto error_abi; |
| 2159 | ret = lttng_logger_init(); |
| 2160 | if (ret) |
| 2161 | goto error_logger; |
| 2162 | return 0; |
| 2163 | |
| 2164 | error_logger: |
| 2165 | lttng_abi_exit(); |
| 2166 | error_abi: |
| 2167 | kmem_cache_destroy(event_cache); |
| 2168 | error_kmem: |
| 2169 | lttng_tracepoint_exit(); |
| 2170 | error_tp: |
| 2171 | lttng_context_exit(); |
| 2172 | return ret; |
| 2173 | } |
| 2174 | |
| 2175 | module_init(lttng_events_init); |
| 2176 | |
| 2177 | static void __exit lttng_events_exit(void) |
| 2178 | { |
| 2179 | struct lttng_session *session, *tmpsession; |
| 2180 | |
| 2181 | lttng_logger_exit(); |
| 2182 | lttng_abi_exit(); |
| 2183 | list_for_each_entry_safe(session, tmpsession, &sessions, list) |
| 2184 | lttng_session_destroy(session); |
| 2185 | kmem_cache_destroy(event_cache); |
| 2186 | lttng_tracepoint_exit(); |
| 2187 | lttng_context_exit(); |
| 2188 | } |
| 2189 | |
| 2190 | module_exit(lttng_events_exit); |
| 2191 | |
| 2192 | MODULE_LICENSE("GPL and additional rights"); |
| 2193 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); |
| 2194 | MODULE_DESCRIPTION("LTTng Events"); |
| 2195 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
| 2196 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." |
| 2197 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) |
| 2198 | LTTNG_MODULES_EXTRAVERSION); |