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