| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * lttng-events.c |
| 4 | * |
| 5 | * Holds LTTng per-session event registry. |
| 6 | * |
| 7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * This page_alloc.h wrapper needs to be included before gfpflags.h because it |
| 12 | * overrides a function with a define. |
| 13 | */ |
| 14 | #include "wrapper/page_alloc.h" |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/jiffies.h> |
| 21 | #include <linux/utsname.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | #include <linux/file.h> |
| 25 | #include <linux/anon_inodes.h> |
| 26 | #include <wrapper/file.h> |
| 27 | #include <linux/uaccess.h> |
| 28 | #include <linux/vmalloc.h> |
| 29 | #include <linux/dmi.h> |
| 30 | |
| 31 | #include <wrapper/uuid.h> |
| 32 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */ |
| 33 | #include <wrapper/random.h> |
| 34 | #include <wrapper/tracepoint.h> |
| 35 | #include <wrapper/list.h> |
| 36 | #include <wrapper/types.h> |
| 37 | #include <lttng/kernel-version.h> |
| 38 | #include <lttng/events.h> |
| 39 | #include <lttng/tracer.h> |
| 40 | #include <lttng/abi-old.h> |
| 41 | #include <lttng/endian.h> |
| 42 | #include <lttng/string-utils.h> |
| 43 | #include <lttng/utils.h> |
| 44 | #include <ringbuffer/backend.h> |
| 45 | #include <ringbuffer/frontend.h> |
| 46 | #include <wrapper/time.h> |
| 47 | |
| 48 | #define METADATA_CACHE_DEFAULT_SIZE 4096 |
| 49 | |
| 50 | static LIST_HEAD(sessions); |
| 51 | static LIST_HEAD(lttng_transport_list); |
| 52 | /* |
| 53 | * Protect the sessions and metadata caches. |
| 54 | */ |
| 55 | static DEFINE_MUTEX(sessions_mutex); |
| 56 | static struct kmem_cache *event_cache; |
| 57 | |
| 58 | static void lttng_session_lazy_sync_event_enablers(struct lttng_session *session); |
| 59 | static void lttng_session_sync_event_enablers(struct lttng_session *session); |
| 60 | static void lttng_event_enabler_destroy(struct lttng_event_enabler *event_enabler); |
| 61 | |
| 62 | static void _lttng_event_destroy(struct lttng_event *event); |
| 63 | static void _lttng_channel_destroy(struct lttng_channel *chan); |
| 64 | static int _lttng_event_unregister(struct lttng_event *event); |
| 65 | static |
| 66 | int _lttng_event_metadata_statedump(struct lttng_session *session, |
| 67 | struct lttng_channel *chan, |
| 68 | struct lttng_event *event); |
| 69 | static |
| 70 | int _lttng_session_metadata_statedump(struct lttng_session *session); |
| 71 | static |
| 72 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream); |
| 73 | static |
| 74 | int _lttng_type_statedump(struct lttng_session *session, |
| 75 | const struct lttng_type *type, |
| 76 | size_t nesting); |
| 77 | static |
| 78 | int _lttng_field_statedump(struct lttng_session *session, |
| 79 | const struct lttng_event_field *field, |
| 80 | size_t nesting); |
| 81 | |
| 82 | void synchronize_trace(void) |
| 83 | { |
| 84 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0)) |
| 85 | synchronize_rcu(); |
| 86 | #else |
| 87 | synchronize_sched(); |
| 88 | #endif |
| 89 | |
| 90 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) |
| 91 | #ifdef CONFIG_PREEMPT_RT_FULL |
| 92 | synchronize_rcu(); |
| 93 | #endif |
| 94 | #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */ |
| 95 | #ifdef CONFIG_PREEMPT_RT |
| 96 | synchronize_rcu(); |
| 97 | #endif |
| 98 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */ |
| 99 | } |
| 100 | |
| 101 | void lttng_lock_sessions(void) |
| 102 | { |
| 103 | mutex_lock(&sessions_mutex); |
| 104 | } |
| 105 | |
| 106 | void lttng_unlock_sessions(void) |
| 107 | { |
| 108 | mutex_unlock(&sessions_mutex); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Called with sessions lock held. |
| 113 | */ |
| 114 | int lttng_session_active(void) |
| 115 | { |
| 116 | struct lttng_session *iter; |
| 117 | |
| 118 | list_for_each_entry(iter, &sessions, list) { |
| 119 | if (iter->active) |
| 120 | return 1; |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | struct lttng_session *lttng_session_create(void) |
| 126 | { |
| 127 | struct lttng_session *session; |
| 128 | struct lttng_metadata_cache *metadata_cache; |
| 129 | int i; |
| 130 | |
| 131 | mutex_lock(&sessions_mutex); |
| 132 | session = lttng_kvzalloc(sizeof(struct lttng_session), GFP_KERNEL); |
| 133 | if (!session) |
| 134 | goto err; |
| 135 | INIT_LIST_HEAD(&session->chan); |
| 136 | INIT_LIST_HEAD(&session->events); |
| 137 | lttng_guid_gen(&session->uuid); |
| 138 | |
| 139 | metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache), |
| 140 | GFP_KERNEL); |
| 141 | if (!metadata_cache) |
| 142 | goto err_free_session; |
| 143 | metadata_cache->data = vzalloc(METADATA_CACHE_DEFAULT_SIZE); |
| 144 | if (!metadata_cache->data) |
| 145 | goto err_free_cache; |
| 146 | metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE; |
| 147 | kref_init(&metadata_cache->refcount); |
| 148 | mutex_init(&metadata_cache->lock); |
| 149 | session->metadata_cache = metadata_cache; |
| 150 | INIT_LIST_HEAD(&metadata_cache->metadata_stream); |
| 151 | memcpy(&metadata_cache->uuid, &session->uuid, |
| 152 | sizeof(metadata_cache->uuid)); |
| 153 | INIT_LIST_HEAD(&session->enablers_head); |
| 154 | for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++) |
| 155 | INIT_HLIST_HEAD(&session->events_ht.table[i]); |
| 156 | list_add(&session->list, &sessions); |
| 157 | session->pid_tracker.session = session; |
| 158 | session->pid_tracker.tracker_type = TRACKER_PID; |
| 159 | session->vpid_tracker.session = session; |
| 160 | session->vpid_tracker.tracker_type = TRACKER_VPID; |
| 161 | session->uid_tracker.session = session; |
| 162 | session->uid_tracker.tracker_type = TRACKER_UID; |
| 163 | session->vuid_tracker.session = session; |
| 164 | session->vuid_tracker.tracker_type = TRACKER_VUID; |
| 165 | session->gid_tracker.session = session; |
| 166 | session->gid_tracker.tracker_type = TRACKER_GID; |
| 167 | session->vgid_tracker.session = session; |
| 168 | session->vgid_tracker.tracker_type = TRACKER_VGID; |
| 169 | mutex_unlock(&sessions_mutex); |
| 170 | return session; |
| 171 | |
| 172 | err_free_cache: |
| 173 | kfree(metadata_cache); |
| 174 | err_free_session: |
| 175 | lttng_kvfree(session); |
| 176 | err: |
| 177 | mutex_unlock(&sessions_mutex); |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | void metadata_cache_destroy(struct kref *kref) |
| 182 | { |
| 183 | struct lttng_metadata_cache *cache = |
| 184 | container_of(kref, struct lttng_metadata_cache, refcount); |
| 185 | vfree(cache->data); |
| 186 | kfree(cache); |
| 187 | } |
| 188 | |
| 189 | void lttng_session_destroy(struct lttng_session *session) |
| 190 | { |
| 191 | struct lttng_channel *chan, *tmpchan; |
| 192 | struct lttng_event *event, *tmpevent; |
| 193 | struct lttng_metadata_stream *metadata_stream; |
| 194 | struct lttng_event_enabler *event_enabler, *tmp_event_enabler; |
| 195 | int ret; |
| 196 | |
| 197 | mutex_lock(&sessions_mutex); |
| 198 | WRITE_ONCE(session->active, 0); |
| 199 | list_for_each_entry(chan, &session->chan, list) { |
| 200 | ret = lttng_syscalls_unregister(chan); |
| 201 | WARN_ON(ret); |
| 202 | } |
| 203 | list_for_each_entry(event, &session->events, list) { |
| 204 | ret = _lttng_event_unregister(event); |
| 205 | WARN_ON(ret); |
| 206 | } |
| 207 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 208 | list_for_each_entry(chan, &session->chan, list) { |
| 209 | ret = lttng_syscalls_destroy(chan); |
| 210 | WARN_ON(ret); |
| 211 | } |
| 212 | list_for_each_entry_safe(event_enabler, tmp_event_enabler, |
| 213 | &session->enablers_head, node) |
| 214 | lttng_event_enabler_destroy(event_enabler); |
| 215 | list_for_each_entry_safe(event, tmpevent, &session->events, list) |
| 216 | _lttng_event_destroy(event); |
| 217 | list_for_each_entry_safe(chan, tmpchan, &session->chan, list) { |
| 218 | BUG_ON(chan->channel_type == METADATA_CHANNEL); |
| 219 | _lttng_channel_destroy(chan); |
| 220 | } |
| 221 | mutex_lock(&session->metadata_cache->lock); |
| 222 | list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list) |
| 223 | _lttng_metadata_channel_hangup(metadata_stream); |
| 224 | mutex_unlock(&session->metadata_cache->lock); |
| 225 | lttng_id_tracker_destroy(&session->pid_tracker, false); |
| 226 | lttng_id_tracker_destroy(&session->vpid_tracker, false); |
| 227 | lttng_id_tracker_destroy(&session->uid_tracker, false); |
| 228 | lttng_id_tracker_destroy(&session->vuid_tracker, false); |
| 229 | lttng_id_tracker_destroy(&session->gid_tracker, false); |
| 230 | lttng_id_tracker_destroy(&session->vgid_tracker, false); |
| 231 | kref_put(&session->metadata_cache->refcount, metadata_cache_destroy); |
| 232 | list_del(&session->list); |
| 233 | mutex_unlock(&sessions_mutex); |
| 234 | lttng_kvfree(session); |
| 235 | } |
| 236 | |
| 237 | int lttng_session_statedump(struct lttng_session *session) |
| 238 | { |
| 239 | int ret; |
| 240 | |
| 241 | mutex_lock(&sessions_mutex); |
| 242 | ret = lttng_statedump_start(session); |
| 243 | mutex_unlock(&sessions_mutex); |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | int lttng_session_enable(struct lttng_session *session) |
| 248 | { |
| 249 | int ret = 0; |
| 250 | struct lttng_channel *chan; |
| 251 | |
| 252 | mutex_lock(&sessions_mutex); |
| 253 | if (session->active) { |
| 254 | ret = -EBUSY; |
| 255 | goto end; |
| 256 | } |
| 257 | |
| 258 | /* Set transient enabler state to "enabled" */ |
| 259 | session->tstate = 1; |
| 260 | |
| 261 | /* We need to sync enablers with session before activation. */ |
| 262 | lttng_session_sync_event_enablers(session); |
| 263 | |
| 264 | /* |
| 265 | * Snapshot the number of events per channel to know the type of header |
| 266 | * we need to use. |
| 267 | */ |
| 268 | list_for_each_entry(chan, &session->chan, list) { |
| 269 | if (chan->header_type) |
| 270 | continue; /* don't change it if session stop/restart */ |
| 271 | if (chan->free_event_id < 31) |
| 272 | chan->header_type = 1; /* compact */ |
| 273 | else |
| 274 | chan->header_type = 2; /* large */ |
| 275 | } |
| 276 | |
| 277 | /* Clear each stream's quiescent state. */ |
| 278 | list_for_each_entry(chan, &session->chan, list) { |
| 279 | if (chan->channel_type != METADATA_CHANNEL) |
| 280 | lib_ring_buffer_clear_quiescent_channel(chan->chan); |
| 281 | } |
| 282 | |
| 283 | WRITE_ONCE(session->active, 1); |
| 284 | WRITE_ONCE(session->been_active, 1); |
| 285 | ret = _lttng_session_metadata_statedump(session); |
| 286 | if (ret) { |
| 287 | WRITE_ONCE(session->active, 0); |
| 288 | goto end; |
| 289 | } |
| 290 | ret = lttng_statedump_start(session); |
| 291 | if (ret) |
| 292 | WRITE_ONCE(session->active, 0); |
| 293 | end: |
| 294 | mutex_unlock(&sessions_mutex); |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | int lttng_session_disable(struct lttng_session *session) |
| 299 | { |
| 300 | int ret = 0; |
| 301 | struct lttng_channel *chan; |
| 302 | |
| 303 | mutex_lock(&sessions_mutex); |
| 304 | if (!session->active) { |
| 305 | ret = -EBUSY; |
| 306 | goto end; |
| 307 | } |
| 308 | WRITE_ONCE(session->active, 0); |
| 309 | |
| 310 | /* Set transient enabler state to "disabled" */ |
| 311 | session->tstate = 0; |
| 312 | lttng_session_sync_event_enablers(session); |
| 313 | |
| 314 | /* Set each stream's quiescent state. */ |
| 315 | list_for_each_entry(chan, &session->chan, list) { |
| 316 | if (chan->channel_type != METADATA_CHANNEL) |
| 317 | lib_ring_buffer_set_quiescent_channel(chan->chan); |
| 318 | } |
| 319 | end: |
| 320 | mutex_unlock(&sessions_mutex); |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | int lttng_session_metadata_regenerate(struct lttng_session *session) |
| 325 | { |
| 326 | int ret = 0; |
| 327 | struct lttng_channel *chan; |
| 328 | struct lttng_event *event; |
| 329 | struct lttng_metadata_cache *cache = session->metadata_cache; |
| 330 | struct lttng_metadata_stream *stream; |
| 331 | |
| 332 | mutex_lock(&sessions_mutex); |
| 333 | if (!session->active) { |
| 334 | ret = -EBUSY; |
| 335 | goto end; |
| 336 | } |
| 337 | |
| 338 | mutex_lock(&cache->lock); |
| 339 | memset(cache->data, 0, cache->cache_alloc); |
| 340 | cache->metadata_written = 0; |
| 341 | cache->version++; |
| 342 | list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) { |
| 343 | stream->metadata_out = 0; |
| 344 | stream->metadata_in = 0; |
| 345 | } |
| 346 | mutex_unlock(&cache->lock); |
| 347 | |
| 348 | session->metadata_dumped = 0; |
| 349 | list_for_each_entry(chan, &session->chan, list) { |
| 350 | chan->metadata_dumped = 0; |
| 351 | } |
| 352 | |
| 353 | list_for_each_entry(event, &session->events, list) { |
| 354 | event->metadata_dumped = 0; |
| 355 | } |
| 356 | |
| 357 | ret = _lttng_session_metadata_statedump(session); |
| 358 | |
| 359 | end: |
| 360 | mutex_unlock(&sessions_mutex); |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | int lttng_channel_enable(struct lttng_channel *channel) |
| 365 | { |
| 366 | int ret = 0; |
| 367 | |
| 368 | mutex_lock(&sessions_mutex); |
| 369 | if (channel->channel_type == METADATA_CHANNEL) { |
| 370 | ret = -EPERM; |
| 371 | goto end; |
| 372 | } |
| 373 | if (channel->enabled) { |
| 374 | ret = -EEXIST; |
| 375 | goto end; |
| 376 | } |
| 377 | /* Set transient enabler state to "enabled" */ |
| 378 | channel->tstate = 1; |
| 379 | lttng_session_sync_event_enablers(channel->session); |
| 380 | /* Set atomically the state to "enabled" */ |
| 381 | WRITE_ONCE(channel->enabled, 1); |
| 382 | end: |
| 383 | mutex_unlock(&sessions_mutex); |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | int lttng_channel_disable(struct lttng_channel *channel) |
| 388 | { |
| 389 | int ret = 0; |
| 390 | |
| 391 | mutex_lock(&sessions_mutex); |
| 392 | if (channel->channel_type == METADATA_CHANNEL) { |
| 393 | ret = -EPERM; |
| 394 | goto end; |
| 395 | } |
| 396 | if (!channel->enabled) { |
| 397 | ret = -EEXIST; |
| 398 | goto end; |
| 399 | } |
| 400 | /* Set atomically the state to "disabled" */ |
| 401 | WRITE_ONCE(channel->enabled, 0); |
| 402 | /* Set transient enabler state to "enabled" */ |
| 403 | channel->tstate = 0; |
| 404 | lttng_session_sync_event_enablers(channel->session); |
| 405 | end: |
| 406 | mutex_unlock(&sessions_mutex); |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | int lttng_event_enable(struct lttng_event *event) |
| 411 | { |
| 412 | int ret = 0; |
| 413 | |
| 414 | mutex_lock(&sessions_mutex); |
| 415 | if (event->chan->channel_type == METADATA_CHANNEL) { |
| 416 | ret = -EPERM; |
| 417 | goto end; |
| 418 | } |
| 419 | if (event->enabled) { |
| 420 | ret = -EEXIST; |
| 421 | goto end; |
| 422 | } |
| 423 | switch (event->instrumentation) { |
| 424 | case LTTNG_KERNEL_TRACEPOINT: |
| 425 | case LTTNG_KERNEL_SYSCALL: |
| 426 | ret = -EINVAL; |
| 427 | break; |
| 428 | case LTTNG_KERNEL_KPROBE: |
| 429 | case LTTNG_KERNEL_UPROBE: |
| 430 | case LTTNG_KERNEL_NOOP: |
| 431 | WRITE_ONCE(event->enabled, 1); |
| 432 | break; |
| 433 | case LTTNG_KERNEL_KRETPROBE: |
| 434 | ret = lttng_kretprobes_event_enable_state(event, 1); |
| 435 | break; |
| 436 | case LTTNG_KERNEL_FUNCTION: /* Fall-through. */ |
| 437 | default: |
| 438 | WARN_ON_ONCE(1); |
| 439 | ret = -EINVAL; |
| 440 | } |
| 441 | end: |
| 442 | mutex_unlock(&sessions_mutex); |
| 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | int lttng_event_disable(struct lttng_event *event) |
| 447 | { |
| 448 | int ret = 0; |
| 449 | |
| 450 | mutex_lock(&sessions_mutex); |
| 451 | if (event->chan->channel_type == METADATA_CHANNEL) { |
| 452 | ret = -EPERM; |
| 453 | goto end; |
| 454 | } |
| 455 | if (!event->enabled) { |
| 456 | ret = -EEXIST; |
| 457 | goto end; |
| 458 | } |
| 459 | switch (event->instrumentation) { |
| 460 | case LTTNG_KERNEL_TRACEPOINT: |
| 461 | case LTTNG_KERNEL_SYSCALL: |
| 462 | ret = -EINVAL; |
| 463 | break; |
| 464 | case LTTNG_KERNEL_KPROBE: |
| 465 | case LTTNG_KERNEL_UPROBE: |
| 466 | case LTTNG_KERNEL_NOOP: |
| 467 | WRITE_ONCE(event->enabled, 0); |
| 468 | break; |
| 469 | case LTTNG_KERNEL_KRETPROBE: |
| 470 | ret = lttng_kretprobes_event_enable_state(event, 0); |
| 471 | break; |
| 472 | case LTTNG_KERNEL_FUNCTION: /* Fall-through. */ |
| 473 | default: |
| 474 | WARN_ON_ONCE(1); |
| 475 | ret = -EINVAL; |
| 476 | } |
| 477 | end: |
| 478 | mutex_unlock(&sessions_mutex); |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static struct lttng_transport *lttng_transport_find(const char *name) |
| 483 | { |
| 484 | struct lttng_transport *transport; |
| 485 | |
| 486 | list_for_each_entry(transport, <tng_transport_list, node) { |
| 487 | if (!strcmp(transport->name, name)) |
| 488 | return transport; |
| 489 | } |
| 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | struct lttng_channel *lttng_channel_create(struct lttng_session *session, |
| 494 | const char *transport_name, |
| 495 | void *buf_addr, |
| 496 | size_t subbuf_size, size_t num_subbuf, |
| 497 | unsigned int switch_timer_interval, |
| 498 | unsigned int read_timer_interval, |
| 499 | enum channel_type channel_type) |
| 500 | { |
| 501 | struct lttng_channel *chan; |
| 502 | struct lttng_transport *transport = NULL; |
| 503 | |
| 504 | mutex_lock(&sessions_mutex); |
| 505 | if (session->been_active && channel_type != METADATA_CHANNEL) |
| 506 | goto active; /* Refuse to add channel to active session */ |
| 507 | transport = lttng_transport_find(transport_name); |
| 508 | if (!transport) { |
| 509 | printk(KERN_WARNING "LTTng: transport %s not found\n", |
| 510 | transport_name); |
| 511 | goto notransport; |
| 512 | } |
| 513 | if (!try_module_get(transport->owner)) { |
| 514 | printk(KERN_WARNING "LTTng: Can't lock transport module.\n"); |
| 515 | goto notransport; |
| 516 | } |
| 517 | chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL); |
| 518 | if (!chan) |
| 519 | goto nomem; |
| 520 | chan->session = session; |
| 521 | chan->id = session->free_chan_id++; |
| 522 | chan->ops = &transport->ops; |
| 523 | /* |
| 524 | * Note: the channel creation op already writes into the packet |
| 525 | * headers. Therefore the "chan" information used as input |
| 526 | * should be already accessible. |
| 527 | */ |
| 528 | chan->chan = transport->ops.channel_create(transport_name, |
| 529 | chan, buf_addr, subbuf_size, num_subbuf, |
| 530 | switch_timer_interval, read_timer_interval); |
| 531 | if (!chan->chan) |
| 532 | goto create_error; |
| 533 | chan->tstate = 1; |
| 534 | chan->enabled = 1; |
| 535 | chan->transport = transport; |
| 536 | chan->channel_type = channel_type; |
| 537 | list_add(&chan->list, &session->chan); |
| 538 | mutex_unlock(&sessions_mutex); |
| 539 | return chan; |
| 540 | |
| 541 | create_error: |
| 542 | kfree(chan); |
| 543 | nomem: |
| 544 | if (transport) |
| 545 | module_put(transport->owner); |
| 546 | notransport: |
| 547 | active: |
| 548 | mutex_unlock(&sessions_mutex); |
| 549 | return NULL; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Only used internally at session destruction for per-cpu channels, and |
| 554 | * when metadata channel is released. |
| 555 | * Needs to be called with sessions mutex held. |
| 556 | */ |
| 557 | static |
| 558 | void _lttng_channel_destroy(struct lttng_channel *chan) |
| 559 | { |
| 560 | chan->ops->channel_destroy(chan->chan); |
| 561 | module_put(chan->transport->owner); |
| 562 | list_del(&chan->list); |
| 563 | lttng_destroy_context(chan->ctx); |
| 564 | kfree(chan); |
| 565 | } |
| 566 | |
| 567 | void lttng_metadata_channel_destroy(struct lttng_channel *chan) |
| 568 | { |
| 569 | BUG_ON(chan->channel_type != METADATA_CHANNEL); |
| 570 | |
| 571 | /* Protect the metadata cache with the sessions_mutex. */ |
| 572 | mutex_lock(&sessions_mutex); |
| 573 | _lttng_channel_destroy(chan); |
| 574 | mutex_unlock(&sessions_mutex); |
| 575 | } |
| 576 | EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy); |
| 577 | |
| 578 | static |
| 579 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream) |
| 580 | { |
| 581 | stream->finalized = 1; |
| 582 | wake_up_interruptible(&stream->read_wait); |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * Supports event creation while tracing session is active. |
| 587 | * Needs to be called with sessions mutex held. |
| 588 | */ |
| 589 | struct lttng_event *_lttng_event_create(struct lttng_channel *chan, |
| 590 | struct lttng_kernel_event *event_param, |
| 591 | void *filter, |
| 592 | const struct lttng_event_desc *event_desc, |
| 593 | enum lttng_kernel_instrumentation itype) |
| 594 | { |
| 595 | struct lttng_session *session = chan->session; |
| 596 | struct lttng_event *event; |
| 597 | const char *event_name; |
| 598 | struct hlist_head *head; |
| 599 | int ret; |
| 600 | |
| 601 | if (chan->free_event_id == -1U) { |
| 602 | ret = -EMFILE; |
| 603 | goto full; |
| 604 | } |
| 605 | |
| 606 | switch (itype) { |
| 607 | case LTTNG_KERNEL_TRACEPOINT: |
| 608 | event_name = event_desc->name; |
| 609 | break; |
| 610 | case LTTNG_KERNEL_KPROBE: |
| 611 | case LTTNG_KERNEL_UPROBE: |
| 612 | case LTTNG_KERNEL_KRETPROBE: |
| 613 | case LTTNG_KERNEL_NOOP: |
| 614 | case LTTNG_KERNEL_SYSCALL: |
| 615 | event_name = event_param->name; |
| 616 | break; |
| 617 | case LTTNG_KERNEL_FUNCTION: /* Fall-through. */ |
| 618 | default: |
| 619 | WARN_ON_ONCE(1); |
| 620 | ret = -EINVAL; |
| 621 | goto type_error; |
| 622 | } |
| 623 | |
| 624 | head = utils_borrow_hash_table_bucket(session->events_ht.table, |
| 625 | LTTNG_EVENT_HT_SIZE, event_name); |
| 626 | lttng_hlist_for_each_entry(event, head, hlist) { |
| 627 | WARN_ON_ONCE(!event->desc); |
| 628 | if (!strncmp(event->desc->name, event_name, |
| 629 | LTTNG_KERNEL_SYM_NAME_LEN - 1) |
| 630 | && chan == event->chan) { |
| 631 | ret = -EEXIST; |
| 632 | goto exist; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | event = kmem_cache_zalloc(event_cache, GFP_KERNEL); |
| 637 | if (!event) { |
| 638 | ret = -ENOMEM; |
| 639 | goto cache_error; |
| 640 | } |
| 641 | event->chan = chan; |
| 642 | event->filter = filter; |
| 643 | event->id = chan->free_event_id++; |
| 644 | event->instrumentation = itype; |
| 645 | event->evtype = LTTNG_TYPE_EVENT; |
| 646 | INIT_LIST_HEAD(&event->bytecode_runtime_head); |
| 647 | INIT_LIST_HEAD(&event->enablers_ref_head); |
| 648 | |
| 649 | switch (itype) { |
| 650 | case LTTNG_KERNEL_TRACEPOINT: |
| 651 | /* Event will be enabled by enabler sync. */ |
| 652 | event->enabled = 0; |
| 653 | event->registered = 0; |
| 654 | event->desc = lttng_event_desc_get(event_name); |
| 655 | if (!event->desc) { |
| 656 | ret = -ENOENT; |
| 657 | goto register_error; |
| 658 | } |
| 659 | /* Populate lttng_event structure before event registration. */ |
| 660 | smp_wmb(); |
| 661 | break; |
| 662 | case LTTNG_KERNEL_KPROBE: |
| 663 | /* |
| 664 | * Needs to be explicitly enabled after creation, since |
| 665 | * we may want to apply filters. |
| 666 | */ |
| 667 | event->enabled = 0; |
| 668 | event->registered = 1; |
| 669 | /* |
| 670 | * Populate lttng_event structure before event |
| 671 | * registration. |
| 672 | */ |
| 673 | smp_wmb(); |
| 674 | ret = lttng_kprobes_register(event_name, |
| 675 | event_param->u.kprobe.symbol_name, |
| 676 | event_param->u.kprobe.offset, |
| 677 | event_param->u.kprobe.addr, |
| 678 | event); |
| 679 | if (ret) { |
| 680 | ret = -EINVAL; |
| 681 | goto register_error; |
| 682 | } |
| 683 | ret = try_module_get(event->desc->owner); |
| 684 | WARN_ON_ONCE(!ret); |
| 685 | break; |
| 686 | case LTTNG_KERNEL_KRETPROBE: |
| 687 | { |
| 688 | struct lttng_event *event_return; |
| 689 | |
| 690 | /* kretprobe defines 2 events */ |
| 691 | /* |
| 692 | * Needs to be explicitly enabled after creation, since |
| 693 | * we may want to apply filters. |
| 694 | */ |
| 695 | event->enabled = 0; |
| 696 | event->registered = 1; |
| 697 | event_return = |
| 698 | kmem_cache_zalloc(event_cache, GFP_KERNEL); |
| 699 | if (!event_return) { |
| 700 | ret = -ENOMEM; |
| 701 | goto register_error; |
| 702 | } |
| 703 | event_return->chan = chan; |
| 704 | event_return->filter = filter; |
| 705 | event_return->id = chan->free_event_id++; |
| 706 | event_return->enabled = 0; |
| 707 | event_return->registered = 1; |
| 708 | event_return->instrumentation = itype; |
| 709 | /* |
| 710 | * Populate lttng_event structure before kretprobe registration. |
| 711 | */ |
| 712 | smp_wmb(); |
| 713 | ret = lttng_kretprobes_register(event_name, |
| 714 | event_param->u.kretprobe.symbol_name, |
| 715 | event_param->u.kretprobe.offset, |
| 716 | event_param->u.kretprobe.addr, |
| 717 | event, event_return); |
| 718 | if (ret) { |
| 719 | kmem_cache_free(event_cache, event_return); |
| 720 | ret = -EINVAL; |
| 721 | goto register_error; |
| 722 | } |
| 723 | /* Take 2 refs on the module: one per event. */ |
| 724 | ret = try_module_get(event->desc->owner); |
| 725 | WARN_ON_ONCE(!ret); |
| 726 | ret = try_module_get(event->desc->owner); |
| 727 | WARN_ON_ONCE(!ret); |
| 728 | ret = _lttng_event_metadata_statedump(chan->session, chan, |
| 729 | event_return); |
| 730 | WARN_ON_ONCE(ret > 0); |
| 731 | if (ret) { |
| 732 | kmem_cache_free(event_cache, event_return); |
| 733 | module_put(event->desc->owner); |
| 734 | module_put(event->desc->owner); |
| 735 | goto statedump_error; |
| 736 | } |
| 737 | list_add(&event_return->list, &chan->session->events); |
| 738 | break; |
| 739 | } |
| 740 | case LTTNG_KERNEL_NOOP: |
| 741 | case LTTNG_KERNEL_SYSCALL: |
| 742 | /* |
| 743 | * Needs to be explicitly enabled after creation, since |
| 744 | * we may want to apply filters. |
| 745 | */ |
| 746 | event->enabled = 0; |
| 747 | event->registered = 0; |
| 748 | event->desc = event_desc; |
| 749 | switch (event_param->u.syscall.entryexit) { |
| 750 | case LTTNG_KERNEL_SYSCALL_ENTRYEXIT: |
| 751 | ret = -EINVAL; |
| 752 | goto register_error; |
| 753 | case LTTNG_KERNEL_SYSCALL_ENTRY: |
| 754 | event->u.syscall.entryexit = LTTNG_SYSCALL_ENTRY; |
| 755 | break; |
| 756 | case LTTNG_KERNEL_SYSCALL_EXIT: |
| 757 | event->u.syscall.entryexit = LTTNG_SYSCALL_EXIT; |
| 758 | break; |
| 759 | } |
| 760 | switch (event_param->u.syscall.abi) { |
| 761 | case LTTNG_KERNEL_SYSCALL_ABI_ALL: |
| 762 | ret = -EINVAL; |
| 763 | goto register_error; |
| 764 | case LTTNG_KERNEL_SYSCALL_ABI_NATIVE: |
| 765 | event->u.syscall.abi = LTTNG_SYSCALL_ABI_NATIVE; |
| 766 | break; |
| 767 | case LTTNG_KERNEL_SYSCALL_ABI_COMPAT: |
| 768 | event->u.syscall.abi = LTTNG_SYSCALL_ABI_COMPAT; |
| 769 | break; |
| 770 | } |
| 771 | if (!event->desc) { |
| 772 | ret = -EINVAL; |
| 773 | goto register_error; |
| 774 | } |
| 775 | break; |
| 776 | case LTTNG_KERNEL_UPROBE: |
| 777 | /* |
| 778 | * Needs to be explicitly enabled after creation, since |
| 779 | * we may want to apply filters. |
| 780 | */ |
| 781 | event->enabled = 0; |
| 782 | event->registered = 1; |
| 783 | |
| 784 | /* |
| 785 | * Populate lttng_event structure before event |
| 786 | * registration. |
| 787 | */ |
| 788 | smp_wmb(); |
| 789 | |
| 790 | ret = lttng_uprobes_register(event_param->name, |
| 791 | event_param->u.uprobe.fd, |
| 792 | event); |
| 793 | if (ret) |
| 794 | goto register_error; |
| 795 | ret = try_module_get(event->desc->owner); |
| 796 | WARN_ON_ONCE(!ret); |
| 797 | break; |
| 798 | case LTTNG_KERNEL_FUNCTION: /* Fall-through */ |
| 799 | default: |
| 800 | WARN_ON_ONCE(1); |
| 801 | ret = -EINVAL; |
| 802 | goto register_error; |
| 803 | } |
| 804 | ret = _lttng_event_metadata_statedump(chan->session, chan, event); |
| 805 | WARN_ON_ONCE(ret > 0); |
| 806 | if (ret) { |
| 807 | goto statedump_error; |
| 808 | } |
| 809 | hlist_add_head(&event->hlist, head); |
| 810 | list_add(&event->list, &chan->session->events); |
| 811 | return event; |
| 812 | |
| 813 | statedump_error: |
| 814 | /* If a statedump error occurs, events will not be readable. */ |
| 815 | register_error: |
| 816 | kmem_cache_free(event_cache, event); |
| 817 | cache_error: |
| 818 | exist: |
| 819 | type_error: |
| 820 | full: |
| 821 | return ERR_PTR(ret); |
| 822 | } |
| 823 | |
| 824 | struct lttng_event *lttng_event_create(struct lttng_channel *chan, |
| 825 | struct lttng_kernel_event *event_param, |
| 826 | void *filter, |
| 827 | const struct lttng_event_desc *event_desc, |
| 828 | enum lttng_kernel_instrumentation itype) |
| 829 | { |
| 830 | struct lttng_event *event; |
| 831 | |
| 832 | mutex_lock(&sessions_mutex); |
| 833 | event = _lttng_event_create(chan, event_param, filter, event_desc, |
| 834 | itype); |
| 835 | mutex_unlock(&sessions_mutex); |
| 836 | return event; |
| 837 | } |
| 838 | |
| 839 | /* Only used for tracepoints for now. */ |
| 840 | static |
| 841 | void register_event(struct lttng_event *event) |
| 842 | { |
| 843 | const struct lttng_event_desc *desc; |
| 844 | int ret = -EINVAL; |
| 845 | |
| 846 | if (event->registered) |
| 847 | return; |
| 848 | |
| 849 | desc = event->desc; |
| 850 | switch (event->instrumentation) { |
| 851 | case LTTNG_KERNEL_TRACEPOINT: |
| 852 | ret = lttng_wrapper_tracepoint_probe_register(desc->kname, |
| 853 | desc->probe_callback, |
| 854 | event); |
| 855 | break; |
| 856 | case LTTNG_KERNEL_SYSCALL: |
| 857 | ret = lttng_syscall_filter_enable(event->chan, event); |
| 858 | break; |
| 859 | case LTTNG_KERNEL_KPROBE: |
| 860 | case LTTNG_KERNEL_UPROBE: |
| 861 | case LTTNG_KERNEL_KRETPROBE: |
| 862 | case LTTNG_KERNEL_NOOP: |
| 863 | ret = 0; |
| 864 | break; |
| 865 | case LTTNG_KERNEL_FUNCTION: /* Fall-through */ |
| 866 | default: |
| 867 | WARN_ON_ONCE(1); |
| 868 | } |
| 869 | if (!ret) |
| 870 | event->registered = 1; |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * Only used internally at session destruction. |
| 875 | */ |
| 876 | int _lttng_event_unregister(struct lttng_event *event) |
| 877 | { |
| 878 | const struct lttng_event_desc *desc; |
| 879 | int ret = -EINVAL; |
| 880 | |
| 881 | if (!event->registered) |
| 882 | return 0; |
| 883 | |
| 884 | desc = event->desc; |
| 885 | switch (event->instrumentation) { |
| 886 | case LTTNG_KERNEL_TRACEPOINT: |
| 887 | ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname, |
| 888 | event->desc->probe_callback, |
| 889 | event); |
| 890 | break; |
| 891 | case LTTNG_KERNEL_KPROBE: |
| 892 | lttng_kprobes_unregister(event); |
| 893 | ret = 0; |
| 894 | break; |
| 895 | case LTTNG_KERNEL_KRETPROBE: |
| 896 | lttng_kretprobes_unregister(event); |
| 897 | ret = 0; |
| 898 | break; |
| 899 | case LTTNG_KERNEL_SYSCALL: |
| 900 | ret = lttng_syscall_filter_disable(event->chan, event); |
| 901 | break; |
| 902 | case LTTNG_KERNEL_NOOP: |
| 903 | ret = 0; |
| 904 | break; |
| 905 | case LTTNG_KERNEL_UPROBE: |
| 906 | lttng_uprobes_unregister(event); |
| 907 | ret = 0; |
| 908 | break; |
| 909 | case LTTNG_KERNEL_FUNCTION: /* Fall-through */ |
| 910 | default: |
| 911 | WARN_ON_ONCE(1); |
| 912 | } |
| 913 | if (!ret) |
| 914 | event->registered = 0; |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * Only used internally at session destruction. |
| 920 | */ |
| 921 | static |
| 922 | void _lttng_event_destroy(struct lttng_event *event) |
| 923 | { |
| 924 | switch (event->instrumentation) { |
| 925 | case LTTNG_KERNEL_TRACEPOINT: |
| 926 | lttng_event_desc_put(event->desc); |
| 927 | break; |
| 928 | case LTTNG_KERNEL_KPROBE: |
| 929 | module_put(event->desc->owner); |
| 930 | lttng_kprobes_destroy_private(event); |
| 931 | break; |
| 932 | case LTTNG_KERNEL_KRETPROBE: |
| 933 | module_put(event->desc->owner); |
| 934 | lttng_kretprobes_destroy_private(event); |
| 935 | break; |
| 936 | case LTTNG_KERNEL_NOOP: |
| 937 | case LTTNG_KERNEL_SYSCALL: |
| 938 | break; |
| 939 | case LTTNG_KERNEL_UPROBE: |
| 940 | module_put(event->desc->owner); |
| 941 | lttng_uprobes_destroy_private(event); |
| 942 | break; |
| 943 | case LTTNG_KERNEL_FUNCTION: /* Fall-through */ |
| 944 | default: |
| 945 | WARN_ON_ONCE(1); |
| 946 | } |
| 947 | list_del(&event->list); |
| 948 | lttng_destroy_context(event->ctx); |
| 949 | kmem_cache_free(event_cache, event); |
| 950 | } |
| 951 | |
| 952 | struct lttng_id_tracker *get_tracker(struct lttng_session *session, |
| 953 | enum tracker_type tracker_type) |
| 954 | { |
| 955 | switch (tracker_type) { |
| 956 | case TRACKER_PID: |
| 957 | return &session->pid_tracker; |
| 958 | case TRACKER_VPID: |
| 959 | return &session->vpid_tracker; |
| 960 | case TRACKER_UID: |
| 961 | return &session->uid_tracker; |
| 962 | case TRACKER_VUID: |
| 963 | return &session->vuid_tracker; |
| 964 | case TRACKER_GID: |
| 965 | return &session->gid_tracker; |
| 966 | case TRACKER_VGID: |
| 967 | return &session->vgid_tracker; |
| 968 | default: |
| 969 | WARN_ON_ONCE(1); |
| 970 | return NULL; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | int lttng_session_track_id(struct lttng_session *session, |
| 975 | enum tracker_type tracker_type, int id) |
| 976 | { |
| 977 | struct lttng_id_tracker *tracker; |
| 978 | int ret; |
| 979 | |
| 980 | tracker = get_tracker(session, tracker_type); |
| 981 | if (!tracker) |
| 982 | return -EINVAL; |
| 983 | if (id < -1) |
| 984 | return -EINVAL; |
| 985 | mutex_lock(&sessions_mutex); |
| 986 | if (id == -1) { |
| 987 | /* track all ids: destroy tracker. */ |
| 988 | lttng_id_tracker_destroy(tracker, true); |
| 989 | ret = 0; |
| 990 | } else { |
| 991 | ret = lttng_id_tracker_add(tracker, id); |
| 992 | } |
| 993 | mutex_unlock(&sessions_mutex); |
| 994 | return ret; |
| 995 | } |
| 996 | |
| 997 | int lttng_session_untrack_id(struct lttng_session *session, |
| 998 | enum tracker_type tracker_type, int id) |
| 999 | { |
| 1000 | struct lttng_id_tracker *tracker; |
| 1001 | int ret; |
| 1002 | |
| 1003 | tracker = get_tracker(session, tracker_type); |
| 1004 | if (!tracker) |
| 1005 | return -EINVAL; |
| 1006 | if (id < -1) |
| 1007 | return -EINVAL; |
| 1008 | mutex_lock(&sessions_mutex); |
| 1009 | if (id == -1) { |
| 1010 | /* untrack all ids: replace by empty tracker. */ |
| 1011 | ret = lttng_id_tracker_empty_set(tracker); |
| 1012 | } else { |
| 1013 | ret = lttng_id_tracker_del(tracker, id); |
| 1014 | } |
| 1015 | mutex_unlock(&sessions_mutex); |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
| 1019 | static |
| 1020 | void *id_list_start(struct seq_file *m, loff_t *pos) |
| 1021 | { |
| 1022 | struct lttng_id_tracker *id_tracker = m->private; |
| 1023 | struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p; |
| 1024 | struct lttng_id_hash_node *e; |
| 1025 | int iter = 0, i; |
| 1026 | |
| 1027 | mutex_lock(&sessions_mutex); |
| 1028 | if (id_tracker_p) { |
| 1029 | for (i = 0; i < LTTNG_ID_TABLE_SIZE; i++) { |
| 1030 | struct hlist_head *head = &id_tracker_p->id_hash[i]; |
| 1031 | |
| 1032 | lttng_hlist_for_each_entry(e, head, hlist) { |
| 1033 | if (iter++ >= *pos) |
| 1034 | return e; |
| 1035 | } |
| 1036 | } |
| 1037 | } else { |
| 1038 | /* ID tracker disabled. */ |
| 1039 | if (iter >= *pos && iter == 0) { |
| 1040 | return id_tracker_p; /* empty tracker */ |
| 1041 | } |
| 1042 | iter++; |
| 1043 | } |
| 1044 | /* End of list */ |
| 1045 | return NULL; |
| 1046 | } |
| 1047 | |
| 1048 | /* Called with sessions_mutex held. */ |
| 1049 | static |
| 1050 | void *id_list_next(struct seq_file *m, void *p, loff_t *ppos) |
| 1051 | { |
| 1052 | struct lttng_id_tracker *id_tracker = m->private; |
| 1053 | struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p; |
| 1054 | struct lttng_id_hash_node *e; |
| 1055 | int iter = 0, i; |
| 1056 | |
| 1057 | (*ppos)++; |
| 1058 | if (id_tracker_p) { |
| 1059 | for (i = 0; i < LTTNG_ID_TABLE_SIZE; i++) { |
| 1060 | struct hlist_head *head = &id_tracker_p->id_hash[i]; |
| 1061 | |
| 1062 | lttng_hlist_for_each_entry(e, head, hlist) { |
| 1063 | if (iter++ >= *ppos) |
| 1064 | return e; |
| 1065 | } |
| 1066 | } |
| 1067 | } else { |
| 1068 | /* ID tracker disabled. */ |
| 1069 | if (iter >= *ppos && iter == 0) |
| 1070 | return p; /* empty tracker */ |
| 1071 | iter++; |
| 1072 | } |
| 1073 | |
| 1074 | /* End of list */ |
| 1075 | return NULL; |
| 1076 | } |
| 1077 | |
| 1078 | static |
| 1079 | void id_list_stop(struct seq_file *m, void *p) |
| 1080 | { |
| 1081 | mutex_unlock(&sessions_mutex); |
| 1082 | } |
| 1083 | |
| 1084 | static |
| 1085 | int id_list_show(struct seq_file *m, void *p) |
| 1086 | { |
| 1087 | struct lttng_id_tracker *id_tracker = m->private; |
| 1088 | struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p; |
| 1089 | int id; |
| 1090 | |
| 1091 | if (p == id_tracker_p) { |
| 1092 | /* Tracker disabled. */ |
| 1093 | id = -1; |
| 1094 | } else { |
| 1095 | const struct lttng_id_hash_node *e = p; |
| 1096 | |
| 1097 | id = lttng_id_tracker_get_node_id(e); |
| 1098 | } |
| 1099 | switch (id_tracker->tracker_type) { |
| 1100 | case TRACKER_PID: |
| 1101 | seq_printf(m, "process { pid = %d; };\n", id); |
| 1102 | break; |
| 1103 | case TRACKER_VPID: |
| 1104 | seq_printf(m, "process { vpid = %d; };\n", id); |
| 1105 | break; |
| 1106 | case TRACKER_UID: |
| 1107 | seq_printf(m, "user { uid = %d; };\n", id); |
| 1108 | break; |
| 1109 | case TRACKER_VUID: |
| 1110 | seq_printf(m, "user { vuid = %d; };\n", id); |
| 1111 | break; |
| 1112 | case TRACKER_GID: |
| 1113 | seq_printf(m, "group { gid = %d; };\n", id); |
| 1114 | break; |
| 1115 | case TRACKER_VGID: |
| 1116 | seq_printf(m, "group { vgid = %d; };\n", id); |
| 1117 | break; |
| 1118 | default: |
| 1119 | seq_printf(m, "UNKNOWN { field = %d };\n", id); |
| 1120 | } |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | static |
| 1125 | const struct seq_operations lttng_tracker_ids_list_seq_ops = { |
| 1126 | .start = id_list_start, |
| 1127 | .next = id_list_next, |
| 1128 | .stop = id_list_stop, |
| 1129 | .show = id_list_show, |
| 1130 | }; |
| 1131 | |
| 1132 | static |
| 1133 | int lttng_tracker_ids_list_open(struct inode *inode, struct file *file) |
| 1134 | { |
| 1135 | return seq_open(file, <tng_tracker_ids_list_seq_ops); |
| 1136 | } |
| 1137 | |
| 1138 | static |
| 1139 | int lttng_tracker_ids_list_release(struct inode *inode, struct file *file) |
| 1140 | { |
| 1141 | struct seq_file *m = file->private_data; |
| 1142 | struct lttng_id_tracker *id_tracker = m->private; |
| 1143 | int ret; |
| 1144 | |
| 1145 | WARN_ON_ONCE(!id_tracker); |
| 1146 | ret = seq_release(inode, file); |
| 1147 | if (!ret) |
| 1148 | fput(id_tracker->session->file); |
| 1149 | return ret; |
| 1150 | } |
| 1151 | |
| 1152 | const struct file_operations lttng_tracker_ids_list_fops = { |
| 1153 | .owner = THIS_MODULE, |
| 1154 | .open = lttng_tracker_ids_list_open, |
| 1155 | .read = seq_read, |
| 1156 | .llseek = seq_lseek, |
| 1157 | .release = lttng_tracker_ids_list_release, |
| 1158 | }; |
| 1159 | |
| 1160 | int lttng_session_list_tracker_ids(struct lttng_session *session, |
| 1161 | enum tracker_type tracker_type) |
| 1162 | { |
| 1163 | struct file *tracker_ids_list_file; |
| 1164 | struct seq_file *m; |
| 1165 | int file_fd, ret; |
| 1166 | |
| 1167 | file_fd = lttng_get_unused_fd(); |
| 1168 | if (file_fd < 0) { |
| 1169 | ret = file_fd; |
| 1170 | goto fd_error; |
| 1171 | } |
| 1172 | |
| 1173 | tracker_ids_list_file = anon_inode_getfile("[lttng_tracker_ids_list]", |
| 1174 | <tng_tracker_ids_list_fops, |
| 1175 | NULL, O_RDWR); |
| 1176 | if (IS_ERR(tracker_ids_list_file)) { |
| 1177 | ret = PTR_ERR(tracker_ids_list_file); |
| 1178 | goto file_error; |
| 1179 | } |
| 1180 | if (!atomic_long_add_unless(&session->file->f_count, 1, LONG_MAX)) { |
| 1181 | ret = -EOVERFLOW; |
| 1182 | goto refcount_error; |
| 1183 | } |
| 1184 | ret = lttng_tracker_ids_list_fops.open(NULL, tracker_ids_list_file); |
| 1185 | if (ret < 0) |
| 1186 | goto open_error; |
| 1187 | m = tracker_ids_list_file->private_data; |
| 1188 | |
| 1189 | m->private = get_tracker(session, tracker_type); |
| 1190 | BUG_ON(!m->private); |
| 1191 | fd_install(file_fd, tracker_ids_list_file); |
| 1192 | |
| 1193 | return file_fd; |
| 1194 | |
| 1195 | open_error: |
| 1196 | atomic_long_dec(&session->file->f_count); |
| 1197 | refcount_error: |
| 1198 | fput(tracker_ids_list_file); |
| 1199 | file_error: |
| 1200 | put_unused_fd(file_fd); |
| 1201 | fd_error: |
| 1202 | return ret; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * Enabler management. |
| 1207 | */ |
| 1208 | static |
| 1209 | int lttng_match_enabler_star_glob(const char *desc_name, |
| 1210 | const char *pattern) |
| 1211 | { |
| 1212 | if (!strutils_star_glob_match(pattern, LTTNG_SIZE_MAX, |
| 1213 | desc_name, LTTNG_SIZE_MAX)) |
| 1214 | return 0; |
| 1215 | return 1; |
| 1216 | } |
| 1217 | |
| 1218 | static |
| 1219 | int lttng_match_enabler_name(const char *desc_name, |
| 1220 | const char *name) |
| 1221 | { |
| 1222 | if (strcmp(desc_name, name)) |
| 1223 | return 0; |
| 1224 | return 1; |
| 1225 | } |
| 1226 | |
| 1227 | static |
| 1228 | int lttng_desc_match_enabler(const struct lttng_event_desc *desc, |
| 1229 | struct lttng_enabler *enabler) |
| 1230 | { |
| 1231 | const char *desc_name, *enabler_name; |
| 1232 | bool compat = false, entry = false; |
| 1233 | |
| 1234 | enabler_name = enabler->event_param.name; |
| 1235 | switch (enabler->event_param.instrumentation) { |
| 1236 | case LTTNG_KERNEL_TRACEPOINT: |
| 1237 | desc_name = desc->name; |
| 1238 | switch (enabler->format_type) { |
| 1239 | case LTTNG_ENABLER_FORMAT_STAR_GLOB: |
| 1240 | return lttng_match_enabler_star_glob(desc_name, enabler_name); |
| 1241 | case LTTNG_ENABLER_FORMAT_NAME: |
| 1242 | return lttng_match_enabler_name(desc_name, enabler_name); |
| 1243 | default: |
| 1244 | return -EINVAL; |
| 1245 | } |
| 1246 | break; |
| 1247 | case LTTNG_KERNEL_SYSCALL: |
| 1248 | desc_name = desc->name; |
| 1249 | if (!strncmp(desc_name, "compat_", strlen("compat_"))) { |
| 1250 | desc_name += strlen("compat_"); |
| 1251 | compat = true; |
| 1252 | } |
| 1253 | if (!strncmp(desc_name, "syscall_exit_", |
| 1254 | strlen("syscall_exit_"))) { |
| 1255 | desc_name += strlen("syscall_exit_"); |
| 1256 | } else if (!strncmp(desc_name, "syscall_entry_", |
| 1257 | strlen("syscall_entry_"))) { |
| 1258 | desc_name += strlen("syscall_entry_"); |
| 1259 | entry = true; |
| 1260 | } else { |
| 1261 | WARN_ON_ONCE(1); |
| 1262 | return -EINVAL; |
| 1263 | } |
| 1264 | switch (enabler->event_param.u.syscall.entryexit) { |
| 1265 | case LTTNG_KERNEL_SYSCALL_ENTRYEXIT: |
| 1266 | break; |
| 1267 | case LTTNG_KERNEL_SYSCALL_ENTRY: |
| 1268 | if (!entry) |
| 1269 | return 0; |
| 1270 | break; |
| 1271 | case LTTNG_KERNEL_SYSCALL_EXIT: |
| 1272 | if (entry) |
| 1273 | return 0; |
| 1274 | break; |
| 1275 | default: |
| 1276 | return -EINVAL; |
| 1277 | } |
| 1278 | switch (enabler->event_param.u.syscall.abi) { |
| 1279 | case LTTNG_KERNEL_SYSCALL_ABI_ALL: |
| 1280 | break; |
| 1281 | case LTTNG_KERNEL_SYSCALL_ABI_NATIVE: |
| 1282 | if (compat) |
| 1283 | return 0; |
| 1284 | break; |
| 1285 | case LTTNG_KERNEL_SYSCALL_ABI_COMPAT: |
| 1286 | if (!compat) |
| 1287 | return 0; |
| 1288 | break; |
| 1289 | default: |
| 1290 | return -EINVAL; |
| 1291 | } |
| 1292 | switch (enabler->event_param.u.syscall.match) { |
| 1293 | case LTTNG_SYSCALL_MATCH_NAME: |
| 1294 | switch (enabler->format_type) { |
| 1295 | case LTTNG_ENABLER_FORMAT_STAR_GLOB: |
| 1296 | return lttng_match_enabler_star_glob(desc_name, enabler_name); |
| 1297 | case LTTNG_ENABLER_FORMAT_NAME: |
| 1298 | return lttng_match_enabler_name(desc_name, enabler_name); |
| 1299 | default: |
| 1300 | return -EINVAL; |
| 1301 | } |
| 1302 | break; |
| 1303 | case LTTNG_SYSCALL_MATCH_NR: |
| 1304 | return -EINVAL; /* Not implemented. */ |
| 1305 | default: |
| 1306 | return -EINVAL; |
| 1307 | } |
| 1308 | break; |
| 1309 | default: |
| 1310 | WARN_ON_ONCE(1); |
| 1311 | return -EINVAL; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | static |
| 1316 | int lttng_event_enabler_match_event(struct lttng_event_enabler *event_enabler, |
| 1317 | struct lttng_event *event) |
| 1318 | { |
| 1319 | struct lttng_enabler *base_enabler = lttng_event_enabler_as_enabler( |
| 1320 | event_enabler); |
| 1321 | |
| 1322 | if (base_enabler->event_param.instrumentation != event->instrumentation) |
| 1323 | return 0; |
| 1324 | if (lttng_desc_match_enabler(event->desc, base_enabler) |
| 1325 | && event->chan == event_enabler->chan) |
| 1326 | return 1; |
| 1327 | else |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | static |
| 1332 | struct lttng_enabler_ref *lttng_enabler_ref( |
| 1333 | struct list_head *enablers_ref_list, |
| 1334 | struct lttng_enabler *enabler) |
| 1335 | { |
| 1336 | struct lttng_enabler_ref *enabler_ref; |
| 1337 | |
| 1338 | list_for_each_entry(enabler_ref, enablers_ref_list, node) { |
| 1339 | if (enabler_ref->ref == enabler) |
| 1340 | return enabler_ref; |
| 1341 | } |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | |
| 1345 | static |
| 1346 | void lttng_create_tracepoint_event_if_missing(struct lttng_event_enabler *event_enabler) |
| 1347 | { |
| 1348 | struct lttng_session *session = event_enabler->chan->session; |
| 1349 | struct lttng_probe_desc *probe_desc; |
| 1350 | const struct lttng_event_desc *desc; |
| 1351 | int i; |
| 1352 | struct list_head *probe_list; |
| 1353 | |
| 1354 | probe_list = lttng_get_probe_list_head(); |
| 1355 | /* |
| 1356 | * For each probe event, if we find that a probe event matches |
| 1357 | * our enabler, create an associated lttng_event if not |
| 1358 | * already present. |
| 1359 | */ |
| 1360 | list_for_each_entry(probe_desc, probe_list, head) { |
| 1361 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 1362 | int found = 0; |
| 1363 | struct hlist_head *head; |
| 1364 | struct lttng_event *event; |
| 1365 | |
| 1366 | desc = probe_desc->event_desc[i]; |
| 1367 | if (!lttng_desc_match_enabler(desc, |
| 1368 | lttng_event_enabler_as_enabler(event_enabler))) |
| 1369 | continue; |
| 1370 | |
| 1371 | /* |
| 1372 | * Check if already created. |
| 1373 | */ |
| 1374 | head = utils_borrow_hash_table_bucket( |
| 1375 | session->events_ht.table, LTTNG_EVENT_HT_SIZE, |
| 1376 | desc->name); |
| 1377 | lttng_hlist_for_each_entry(event, head, hlist) { |
| 1378 | if (event->desc == desc |
| 1379 | && event->chan == event_enabler->chan) |
| 1380 | found = 1; |
| 1381 | } |
| 1382 | if (found) |
| 1383 | continue; |
| 1384 | |
| 1385 | /* |
| 1386 | * We need to create an event for this |
| 1387 | * event probe. |
| 1388 | */ |
| 1389 | event = _lttng_event_create(event_enabler->chan, |
| 1390 | NULL, NULL, desc, |
| 1391 | LTTNG_KERNEL_TRACEPOINT); |
| 1392 | if (!event) { |
| 1393 | printk(KERN_INFO "LTTng: Unable to create event %s\n", |
| 1394 | probe_desc->event_desc[i]->name); |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | static |
| 1401 | void lttng_create_syscall_event_if_missing(struct lttng_event_enabler *event_enabler) |
| 1402 | { |
| 1403 | int ret; |
| 1404 | |
| 1405 | ret = lttng_syscalls_register(event_enabler->chan, NULL); |
| 1406 | WARN_ON_ONCE(ret); |
| 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * Create struct lttng_event if it is missing and present in the list of |
| 1411 | * tracepoint probes. |
| 1412 | * Should be called with sessions mutex held. |
| 1413 | */ |
| 1414 | static |
| 1415 | void lttng_create_event_if_missing(struct lttng_event_enabler *event_enabler) |
| 1416 | { |
| 1417 | switch (event_enabler->base.event_param.instrumentation) { |
| 1418 | case LTTNG_KERNEL_TRACEPOINT: |
| 1419 | lttng_create_tracepoint_event_if_missing(event_enabler); |
| 1420 | break; |
| 1421 | case LTTNG_KERNEL_SYSCALL: |
| 1422 | lttng_create_syscall_event_if_missing(event_enabler); |
| 1423 | break; |
| 1424 | default: |
| 1425 | WARN_ON_ONCE(1); |
| 1426 | break; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | /* |
| 1431 | * Create events associated with an event_enabler (if not already present), |
| 1432 | * and add backward reference from the event to the enabler. |
| 1433 | * Should be called with sessions mutex held. |
| 1434 | */ |
| 1435 | static |
| 1436 | int lttng_event_enabler_ref_events(struct lttng_event_enabler *event_enabler) |
| 1437 | { |
| 1438 | struct lttng_channel *chan = event_enabler->chan; |
| 1439 | struct lttng_session *session = event_enabler->chan->session; |
| 1440 | struct lttng_enabler *base_enabler = lttng_event_enabler_as_enabler(event_enabler); |
| 1441 | struct lttng_event *event; |
| 1442 | |
| 1443 | if (base_enabler->event_param.instrumentation == LTTNG_KERNEL_SYSCALL && |
| 1444 | base_enabler->event_param.u.syscall.entryexit == LTTNG_KERNEL_SYSCALL_ENTRYEXIT && |
| 1445 | base_enabler->event_param.u.syscall.abi == LTTNG_KERNEL_SYSCALL_ABI_ALL && |
| 1446 | base_enabler->event_param.u.syscall.match == LTTNG_SYSCALL_MATCH_NAME && |
| 1447 | !strcmp(base_enabler->event_param.name, "*")) { |
| 1448 | if (base_enabler->enabled) |
| 1449 | WRITE_ONCE(chan->syscall_all, 1); |
| 1450 | else |
| 1451 | WRITE_ONCE(chan->syscall_all, 0); |
| 1452 | } |
| 1453 | |
| 1454 | /* First ensure that probe events are created for this enabler. */ |
| 1455 | lttng_create_event_if_missing(event_enabler); |
| 1456 | |
| 1457 | /* For each event matching event_enabler in session event list. */ |
| 1458 | list_for_each_entry(event, &session->events, list) { |
| 1459 | struct lttng_enabler_ref *enabler_ref; |
| 1460 | |
| 1461 | if (!lttng_event_enabler_match_event(event_enabler, event)) |
| 1462 | continue; |
| 1463 | enabler_ref = lttng_enabler_ref(&event->enablers_ref_head, |
| 1464 | lttng_event_enabler_as_enabler(event_enabler)); |
| 1465 | if (!enabler_ref) { |
| 1466 | /* |
| 1467 | * If no backward ref, create it. |
| 1468 | * Add backward ref from event to event_enabler. |
| 1469 | */ |
| 1470 | enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL); |
| 1471 | if (!enabler_ref) |
| 1472 | return -ENOMEM; |
| 1473 | enabler_ref->ref = lttng_event_enabler_as_enabler(event_enabler); |
| 1474 | list_add(&enabler_ref->node, |
| 1475 | &event->enablers_ref_head); |
| 1476 | } |
| 1477 | |
| 1478 | /* |
| 1479 | * Link filter bytecodes if not linked yet. |
| 1480 | */ |
| 1481 | lttng_enabler_link_bytecode(event->desc, |
| 1482 | lttng_static_ctx, |
| 1483 | &event->bytecode_runtime_head, |
| 1484 | lttng_event_enabler_as_enabler(event_enabler)); |
| 1485 | |
| 1486 | /* TODO: merge event context. */ |
| 1487 | } |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | /* |
| 1492 | * Called at module load: connect the probe on all enablers matching |
| 1493 | * this event. |
| 1494 | * Called with sessions lock held. |
| 1495 | */ |
| 1496 | int lttng_fix_pending_events(void) |
| 1497 | { |
| 1498 | struct lttng_session *session; |
| 1499 | |
| 1500 | list_for_each_entry(session, &sessions, list) |
| 1501 | lttng_session_lazy_sync_event_enablers(session); |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
| 1505 | struct lttng_event_enabler *lttng_event_enabler_create( |
| 1506 | enum lttng_enabler_format_type format_type, |
| 1507 | struct lttng_kernel_event *event_param, |
| 1508 | struct lttng_channel *chan) |
| 1509 | { |
| 1510 | struct lttng_event_enabler *event_enabler; |
| 1511 | |
| 1512 | event_enabler = kzalloc(sizeof(*event_enabler), GFP_KERNEL); |
| 1513 | if (!event_enabler) |
| 1514 | return NULL; |
| 1515 | event_enabler->base.format_type = format_type; |
| 1516 | INIT_LIST_HEAD(&event_enabler->base.filter_bytecode_head); |
| 1517 | memcpy(&event_enabler->base.event_param, event_param, |
| 1518 | sizeof(event_enabler->base.event_param)); |
| 1519 | event_enabler->chan = chan; |
| 1520 | /* ctx left NULL */ |
| 1521 | event_enabler->base.enabled = 0; |
| 1522 | event_enabler->base.evtype = LTTNG_TYPE_ENABLER; |
| 1523 | mutex_lock(&sessions_mutex); |
| 1524 | list_add(&event_enabler->node, &event_enabler->chan->session->enablers_head); |
| 1525 | lttng_session_lazy_sync_event_enablers(event_enabler->chan->session); |
| 1526 | mutex_unlock(&sessions_mutex); |
| 1527 | return event_enabler; |
| 1528 | } |
| 1529 | |
| 1530 | int lttng_event_enabler_enable(struct lttng_event_enabler *event_enabler) |
| 1531 | { |
| 1532 | mutex_lock(&sessions_mutex); |
| 1533 | lttng_event_enabler_as_enabler(event_enabler)->enabled = 1; |
| 1534 | lttng_session_lazy_sync_event_enablers(event_enabler->chan->session); |
| 1535 | mutex_unlock(&sessions_mutex); |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | int lttng_event_enabler_disable(struct lttng_event_enabler *event_enabler) |
| 1540 | { |
| 1541 | mutex_lock(&sessions_mutex); |
| 1542 | lttng_event_enabler_as_enabler(event_enabler)->enabled = 0; |
| 1543 | lttng_session_lazy_sync_event_enablers(event_enabler->chan->session); |
| 1544 | mutex_unlock(&sessions_mutex); |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
| 1548 | static |
| 1549 | int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler, |
| 1550 | struct lttng_kernel_filter_bytecode __user *bytecode) |
| 1551 | { |
| 1552 | struct lttng_filter_bytecode_node *bytecode_node; |
| 1553 | uint32_t bytecode_len; |
| 1554 | int ret; |
| 1555 | |
| 1556 | ret = get_user(bytecode_len, &bytecode->len); |
| 1557 | if (ret) |
| 1558 | return ret; |
| 1559 | bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len, |
| 1560 | GFP_KERNEL); |
| 1561 | if (!bytecode_node) |
| 1562 | return -ENOMEM; |
| 1563 | ret = copy_from_user(&bytecode_node->bc, bytecode, |
| 1564 | sizeof(*bytecode) + bytecode_len); |
| 1565 | if (ret) |
| 1566 | goto error_free; |
| 1567 | |
| 1568 | bytecode_node->enabler = enabler; |
| 1569 | /* Enforce length based on allocated size */ |
| 1570 | bytecode_node->bc.len = bytecode_len; |
| 1571 | list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head); |
| 1572 | |
| 1573 | return 0; |
| 1574 | |
| 1575 | error_free: |
| 1576 | kfree(bytecode_node); |
| 1577 | return ret; |
| 1578 | } |
| 1579 | |
| 1580 | int lttng_event_enabler_attach_bytecode(struct lttng_event_enabler *event_enabler, |
| 1581 | struct lttng_kernel_filter_bytecode __user *bytecode) |
| 1582 | { |
| 1583 | int ret; |
| 1584 | ret = lttng_enabler_attach_bytecode( |
| 1585 | lttng_event_enabler_as_enabler(event_enabler), bytecode); |
| 1586 | if (ret) |
| 1587 | goto error; |
| 1588 | |
| 1589 | lttng_session_lazy_sync_event_enablers(event_enabler->chan->session); |
| 1590 | return 0; |
| 1591 | |
| 1592 | error: |
| 1593 | return ret; |
| 1594 | } |
| 1595 | |
| 1596 | int lttng_event_add_callsite(struct lttng_event *event, |
| 1597 | struct lttng_kernel_event_callsite __user *callsite) |
| 1598 | { |
| 1599 | |
| 1600 | switch (event->instrumentation) { |
| 1601 | case LTTNG_KERNEL_UPROBE: |
| 1602 | return lttng_uprobes_add_callsite(event, callsite); |
| 1603 | default: |
| 1604 | return -EINVAL; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | int lttng_event_enabler_attach_context(struct lttng_event_enabler *event_enabler, |
| 1609 | struct lttng_kernel_context *context_param) |
| 1610 | { |
| 1611 | return -ENOSYS; |
| 1612 | } |
| 1613 | |
| 1614 | static |
| 1615 | void lttng_enabler_destroy(struct lttng_enabler *enabler) |
| 1616 | { |
| 1617 | struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node; |
| 1618 | |
| 1619 | /* Destroy filter bytecode */ |
| 1620 | list_for_each_entry_safe(filter_node, tmp_filter_node, |
| 1621 | &enabler->filter_bytecode_head, node) { |
| 1622 | kfree(filter_node); |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | static |
| 1627 | void lttng_event_enabler_destroy(struct lttng_event_enabler *event_enabler) |
| 1628 | { |
| 1629 | lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler)); |
| 1630 | |
| 1631 | /* Destroy contexts */ |
| 1632 | lttng_destroy_context(event_enabler->ctx); |
| 1633 | |
| 1634 | list_del(&event_enabler->node); |
| 1635 | kfree(event_enabler); |
| 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | * lttng_session_sync_event_enablers should be called just before starting a |
| 1640 | * session. |
| 1641 | * Should be called with sessions mutex held. |
| 1642 | */ |
| 1643 | static |
| 1644 | void lttng_session_sync_event_enablers(struct lttng_session *session) |
| 1645 | { |
| 1646 | struct lttng_event_enabler *event_enabler; |
| 1647 | struct lttng_event *event; |
| 1648 | |
| 1649 | list_for_each_entry(event_enabler, &session->enablers_head, node) |
| 1650 | lttng_event_enabler_ref_events(event_enabler); |
| 1651 | /* |
| 1652 | * For each event, if at least one of its enablers is enabled, |
| 1653 | * and its channel and session transient states are enabled, we |
| 1654 | * enable the event, else we disable it. |
| 1655 | */ |
| 1656 | list_for_each_entry(event, &session->events, list) { |
| 1657 | struct lttng_enabler_ref *enabler_ref; |
| 1658 | struct lttng_bytecode_runtime *runtime; |
| 1659 | int enabled = 0, has_enablers_without_bytecode = 0; |
| 1660 | |
| 1661 | switch (event->instrumentation) { |
| 1662 | case LTTNG_KERNEL_TRACEPOINT: |
| 1663 | case LTTNG_KERNEL_SYSCALL: |
| 1664 | /* Enable events */ |
| 1665 | list_for_each_entry(enabler_ref, |
| 1666 | &event->enablers_ref_head, node) { |
| 1667 | if (enabler_ref->ref->enabled) { |
| 1668 | enabled = 1; |
| 1669 | break; |
| 1670 | } |
| 1671 | } |
| 1672 | break; |
| 1673 | default: |
| 1674 | /* Not handled with lazy sync. */ |
| 1675 | continue; |
| 1676 | } |
| 1677 | /* |
| 1678 | * Enabled state is based on union of enablers, with |
| 1679 | * intesection of session and channel transient enable |
| 1680 | * states. |
| 1681 | */ |
| 1682 | enabled = enabled && session->tstate && event->chan->tstate; |
| 1683 | |
| 1684 | WRITE_ONCE(event->enabled, enabled); |
| 1685 | /* |
| 1686 | * Sync tracepoint registration with event enabled |
| 1687 | * state. |
| 1688 | */ |
| 1689 | if (enabled) { |
| 1690 | register_event(event); |
| 1691 | } else { |
| 1692 | _lttng_event_unregister(event); |
| 1693 | } |
| 1694 | |
| 1695 | /* Check if has enablers without bytecode enabled */ |
| 1696 | list_for_each_entry(enabler_ref, |
| 1697 | &event->enablers_ref_head, node) { |
| 1698 | if (enabler_ref->ref->enabled |
| 1699 | && list_empty(&enabler_ref->ref->filter_bytecode_head)) { |
| 1700 | has_enablers_without_bytecode = 1; |
| 1701 | break; |
| 1702 | } |
| 1703 | } |
| 1704 | event->has_enablers_without_bytecode = |
| 1705 | has_enablers_without_bytecode; |
| 1706 | |
| 1707 | /* Enable filters */ |
| 1708 | list_for_each_entry(runtime, |
| 1709 | &event->bytecode_runtime_head, node) |
| 1710 | lttng_filter_sync_state(runtime); |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | /* |
| 1715 | * Apply enablers to session events, adding events to session if need |
| 1716 | * be. It is required after each modification applied to an active |
| 1717 | * session, and right before session "start". |
| 1718 | * "lazy" sync means we only sync if required. |
| 1719 | * Should be called with sessions mutex held. |
| 1720 | */ |
| 1721 | static |
| 1722 | void lttng_session_lazy_sync_event_enablers(struct lttng_session *session) |
| 1723 | { |
| 1724 | /* We can skip if session is not active */ |
| 1725 | if (!session->active) |
| 1726 | return; |
| 1727 | lttng_session_sync_event_enablers(session); |
| 1728 | } |
| 1729 | |
| 1730 | /* |
| 1731 | * Serialize at most one packet worth of metadata into a metadata |
| 1732 | * channel. |
| 1733 | * We grab the metadata cache mutex to get exclusive access to our metadata |
| 1734 | * buffer and to the metadata cache. Exclusive access to the metadata buffer |
| 1735 | * allows us to do racy operations such as looking for remaining space left in |
| 1736 | * packet and write, since mutual exclusion protects us from concurrent writes. |
| 1737 | * Mutual exclusion on the metadata cache allow us to read the cache content |
| 1738 | * without racing against reallocation of the cache by updates. |
| 1739 | * Returns the number of bytes written in the channel, 0 if no data |
| 1740 | * was written and a negative value on error. |
| 1741 | */ |
| 1742 | int lttng_metadata_output_channel(struct lttng_metadata_stream *stream, |
| 1743 | struct channel *chan, bool *coherent) |
| 1744 | { |
| 1745 | struct lib_ring_buffer_ctx ctx; |
| 1746 | int ret = 0; |
| 1747 | size_t len, reserve_len; |
| 1748 | |
| 1749 | /* |
| 1750 | * Ensure we support mutiple get_next / put sequences followed by |
| 1751 | * put_next. The metadata cache lock protects reading the metadata |
| 1752 | * cache. It can indeed be read concurrently by "get_next_subbuf" and |
| 1753 | * "flush" operations on the buffer invoked by different processes. |
| 1754 | * Moreover, since the metadata cache memory can be reallocated, we |
| 1755 | * need to have exclusive access against updates even though we only |
| 1756 | * read it. |
| 1757 | */ |
| 1758 | mutex_lock(&stream->metadata_cache->lock); |
| 1759 | WARN_ON(stream->metadata_in < stream->metadata_out); |
| 1760 | if (stream->metadata_in != stream->metadata_out) |
| 1761 | goto end; |
| 1762 | |
| 1763 | /* Metadata regenerated, change the version. */ |
| 1764 | if (stream->metadata_cache->version != stream->version) |
| 1765 | stream->version = stream->metadata_cache->version; |
| 1766 | |
| 1767 | len = stream->metadata_cache->metadata_written - |
| 1768 | stream->metadata_in; |
| 1769 | if (!len) |
| 1770 | goto end; |
| 1771 | reserve_len = min_t(size_t, |
| 1772 | stream->transport->ops.packet_avail_size(chan), |
| 1773 | len); |
| 1774 | lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len, |
| 1775 | sizeof(char), -1); |
| 1776 | /* |
| 1777 | * If reservation failed, return an error to the caller. |
| 1778 | */ |
| 1779 | ret = stream->transport->ops.event_reserve(&ctx, 0); |
| 1780 | if (ret != 0) { |
| 1781 | printk(KERN_WARNING "LTTng: Metadata event reservation failed\n"); |
| 1782 | stream->coherent = false; |
| 1783 | goto end; |
| 1784 | } |
| 1785 | stream->transport->ops.event_write(&ctx, |
| 1786 | stream->metadata_cache->data + stream->metadata_in, |
| 1787 | reserve_len); |
| 1788 | stream->transport->ops.event_commit(&ctx); |
| 1789 | stream->metadata_in += reserve_len; |
| 1790 | if (reserve_len < len) |
| 1791 | stream->coherent = false; |
| 1792 | else |
| 1793 | stream->coherent = true; |
| 1794 | ret = reserve_len; |
| 1795 | |
| 1796 | end: |
| 1797 | if (coherent) |
| 1798 | *coherent = stream->coherent; |
| 1799 | mutex_unlock(&stream->metadata_cache->lock); |
| 1800 | return ret; |
| 1801 | } |
| 1802 | |
| 1803 | static |
| 1804 | void lttng_metadata_begin(struct lttng_session *session) |
| 1805 | { |
| 1806 | if (atomic_inc_return(&session->metadata_cache->producing) == 1) |
| 1807 | mutex_lock(&session->metadata_cache->lock); |
| 1808 | } |
| 1809 | |
| 1810 | static |
| 1811 | void lttng_metadata_end(struct lttng_session *session) |
| 1812 | { |
| 1813 | WARN_ON_ONCE(!atomic_read(&session->metadata_cache->producing)); |
| 1814 | if (atomic_dec_return(&session->metadata_cache->producing) == 0) { |
| 1815 | struct lttng_metadata_stream *stream; |
| 1816 | |
| 1817 | list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) |
| 1818 | wake_up_interruptible(&stream->read_wait); |
| 1819 | mutex_unlock(&session->metadata_cache->lock); |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | /* |
| 1824 | * Write the metadata to the metadata cache. |
| 1825 | * Must be called with sessions_mutex held. |
| 1826 | * The metadata cache lock protects us from concurrent read access from |
| 1827 | * thread outputting metadata content to ring buffer. |
| 1828 | * The content of the printf is printed as a single atomic metadata |
| 1829 | * transaction. |
| 1830 | */ |
| 1831 | int lttng_metadata_printf(struct lttng_session *session, |
| 1832 | const char *fmt, ...) |
| 1833 | { |
| 1834 | char *str; |
| 1835 | size_t len; |
| 1836 | va_list ap; |
| 1837 | |
| 1838 | WARN_ON_ONCE(!LTTNG_READ_ONCE(session->active)); |
| 1839 | |
| 1840 | va_start(ap, fmt); |
| 1841 | str = kvasprintf(GFP_KERNEL, fmt, ap); |
| 1842 | va_end(ap); |
| 1843 | if (!str) |
| 1844 | return -ENOMEM; |
| 1845 | |
| 1846 | len = strlen(str); |
| 1847 | WARN_ON_ONCE(!atomic_read(&session->metadata_cache->producing)); |
| 1848 | if (session->metadata_cache->metadata_written + len > |
| 1849 | session->metadata_cache->cache_alloc) { |
| 1850 | char *tmp_cache_realloc; |
| 1851 | unsigned int tmp_cache_alloc_size; |
| 1852 | |
| 1853 | tmp_cache_alloc_size = max_t(unsigned int, |
| 1854 | session->metadata_cache->cache_alloc + len, |
| 1855 | session->metadata_cache->cache_alloc << 1); |
| 1856 | tmp_cache_realloc = vzalloc(tmp_cache_alloc_size); |
| 1857 | if (!tmp_cache_realloc) |
| 1858 | goto err; |
| 1859 | if (session->metadata_cache->data) { |
| 1860 | memcpy(tmp_cache_realloc, |
| 1861 | session->metadata_cache->data, |
| 1862 | session->metadata_cache->cache_alloc); |
| 1863 | vfree(session->metadata_cache->data); |
| 1864 | } |
| 1865 | |
| 1866 | session->metadata_cache->cache_alloc = tmp_cache_alloc_size; |
| 1867 | session->metadata_cache->data = tmp_cache_realloc; |
| 1868 | } |
| 1869 | memcpy(session->metadata_cache->data + |
| 1870 | session->metadata_cache->metadata_written, |
| 1871 | str, len); |
| 1872 | session->metadata_cache->metadata_written += len; |
| 1873 | kfree(str); |
| 1874 | |
| 1875 | return 0; |
| 1876 | |
| 1877 | err: |
| 1878 | kfree(str); |
| 1879 | return -ENOMEM; |
| 1880 | } |
| 1881 | |
| 1882 | static |
| 1883 | int print_tabs(struct lttng_session *session, size_t nesting) |
| 1884 | { |
| 1885 | size_t i; |
| 1886 | |
| 1887 | for (i = 0; i < nesting; i++) { |
| 1888 | int ret; |
| 1889 | |
| 1890 | ret = lttng_metadata_printf(session, " "); |
| 1891 | if (ret) { |
| 1892 | return ret; |
| 1893 | } |
| 1894 | } |
| 1895 | return 0; |
| 1896 | } |
| 1897 | |
| 1898 | static |
| 1899 | int lttng_field_name_statedump(struct lttng_session *session, |
| 1900 | const struct lttng_event_field *field, |
| 1901 | size_t nesting) |
| 1902 | { |
| 1903 | return lttng_metadata_printf(session, " _%s;\n", field->name); |
| 1904 | } |
| 1905 | |
| 1906 | static |
| 1907 | int _lttng_integer_type_statedump(struct lttng_session *session, |
| 1908 | const struct lttng_type *type, |
| 1909 | size_t nesting) |
| 1910 | { |
| 1911 | int ret; |
| 1912 | |
| 1913 | WARN_ON_ONCE(type->atype != atype_integer); |
| 1914 | ret = print_tabs(session, nesting); |
| 1915 | if (ret) |
| 1916 | return ret; |
| 1917 | ret = lttng_metadata_printf(session, |
| 1918 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s }", |
| 1919 | type->u.integer.size, |
| 1920 | type->u.integer.alignment, |
| 1921 | type->u.integer.signedness, |
| 1922 | (type->u.integer.encoding == lttng_encode_none) |
| 1923 | ? "none" |
| 1924 | : (type->u.integer.encoding == lttng_encode_UTF8) |
| 1925 | ? "UTF8" |
| 1926 | : "ASCII", |
| 1927 | type->u.integer.base, |
| 1928 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 1929 | type->u.integer.reverse_byte_order ? " byte_order = le;" : "" |
| 1930 | #else |
| 1931 | type->u.integer.reverse_byte_order ? " byte_order = be;" : "" |
| 1932 | #endif |
| 1933 | ); |
| 1934 | return ret; |
| 1935 | } |
| 1936 | |
| 1937 | /* |
| 1938 | * Must be called with sessions_mutex held. |
| 1939 | */ |
| 1940 | static |
| 1941 | int _lttng_struct_type_statedump(struct lttng_session *session, |
| 1942 | const struct lttng_type *type, |
| 1943 | size_t nesting) |
| 1944 | { |
| 1945 | int ret; |
| 1946 | uint32_t i, nr_fields; |
| 1947 | unsigned int alignment; |
| 1948 | |
| 1949 | WARN_ON_ONCE(type->atype != atype_struct_nestable); |
| 1950 | |
| 1951 | ret = print_tabs(session, nesting); |
| 1952 | if (ret) |
| 1953 | return ret; |
| 1954 | ret = lttng_metadata_printf(session, |
| 1955 | "struct {\n"); |
| 1956 | if (ret) |
| 1957 | return ret; |
| 1958 | nr_fields = type->u.struct_nestable.nr_fields; |
| 1959 | for (i = 0; i < nr_fields; i++) { |
| 1960 | const struct lttng_event_field *iter_field; |
| 1961 | |
| 1962 | iter_field = &type->u.struct_nestable.fields[i]; |
| 1963 | ret = _lttng_field_statedump(session, iter_field, nesting + 1); |
| 1964 | if (ret) |
| 1965 | return ret; |
| 1966 | } |
| 1967 | ret = print_tabs(session, nesting); |
| 1968 | if (ret) |
| 1969 | return ret; |
| 1970 | alignment = type->u.struct_nestable.alignment; |
| 1971 | if (alignment) { |
| 1972 | ret = lttng_metadata_printf(session, |
| 1973 | "} align(%u)", |
| 1974 | alignment); |
| 1975 | } else { |
| 1976 | ret = lttng_metadata_printf(session, |
| 1977 | "}"); |
| 1978 | } |
| 1979 | return ret; |
| 1980 | } |
| 1981 | |
| 1982 | /* |
| 1983 | * Must be called with sessions_mutex held. |
| 1984 | */ |
| 1985 | static |
| 1986 | int _lttng_struct_field_statedump(struct lttng_session *session, |
| 1987 | const struct lttng_event_field *field, |
| 1988 | size_t nesting) |
| 1989 | { |
| 1990 | int ret; |
| 1991 | |
| 1992 | ret = _lttng_struct_type_statedump(session, |
| 1993 | &field->type, nesting); |
| 1994 | if (ret) |
| 1995 | return ret; |
| 1996 | return lttng_field_name_statedump(session, field, nesting); |
| 1997 | } |
| 1998 | |
| 1999 | /* |
| 2000 | * Must be called with sessions_mutex held. |
| 2001 | */ |
| 2002 | static |
| 2003 | int _lttng_variant_type_statedump(struct lttng_session *session, |
| 2004 | const struct lttng_type *type, |
| 2005 | size_t nesting) |
| 2006 | { |
| 2007 | int ret; |
| 2008 | uint32_t i, nr_choices; |
| 2009 | |
| 2010 | WARN_ON_ONCE(type->atype != atype_variant_nestable); |
| 2011 | /* |
| 2012 | * CTF 1.8 does not allow expressing nonzero variant alignment in a nestable way. |
| 2013 | */ |
| 2014 | if (type->u.variant_nestable.alignment != 0) |
| 2015 | return -EINVAL; |
| 2016 | ret = print_tabs(session, nesting); |
| 2017 | if (ret) |
| 2018 | return ret; |
| 2019 | ret = lttng_metadata_printf(session, |
| 2020 | "variant <_%s> {\n", |
| 2021 | type->u.variant_nestable.tag_name); |
| 2022 | if (ret) |
| 2023 | return ret; |
| 2024 | nr_choices = type->u.variant_nestable.nr_choices; |
| 2025 | for (i = 0; i < nr_choices; i++) { |
| 2026 | const struct lttng_event_field *iter_field; |
| 2027 | |
| 2028 | iter_field = &type->u.variant_nestable.choices[i]; |
| 2029 | ret = _lttng_field_statedump(session, iter_field, nesting + 1); |
| 2030 | if (ret) |
| 2031 | return ret; |
| 2032 | } |
| 2033 | ret = print_tabs(session, nesting); |
| 2034 | if (ret) |
| 2035 | return ret; |
| 2036 | ret = lttng_metadata_printf(session, |
| 2037 | "}"); |
| 2038 | return ret; |
| 2039 | } |
| 2040 | |
| 2041 | /* |
| 2042 | * Must be called with sessions_mutex held. |
| 2043 | */ |
| 2044 | static |
| 2045 | int _lttng_variant_field_statedump(struct lttng_session *session, |
| 2046 | const struct lttng_event_field *field, |
| 2047 | size_t nesting) |
| 2048 | { |
| 2049 | int ret; |
| 2050 | |
| 2051 | ret = _lttng_variant_type_statedump(session, |
| 2052 | &field->type, nesting); |
| 2053 | if (ret) |
| 2054 | return ret; |
| 2055 | return lttng_field_name_statedump(session, field, nesting); |
| 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * Must be called with sessions_mutex held. |
| 2060 | */ |
| 2061 | static |
| 2062 | int _lttng_array_field_statedump(struct lttng_session *session, |
| 2063 | const struct lttng_event_field *field, |
| 2064 | size_t nesting) |
| 2065 | { |
| 2066 | int ret; |
| 2067 | const struct lttng_type *elem_type; |
| 2068 | |
| 2069 | WARN_ON_ONCE(field->type.atype != atype_array_nestable); |
| 2070 | |
| 2071 | if (field->type.u.array_nestable.alignment) { |
| 2072 | ret = print_tabs(session, nesting); |
| 2073 | if (ret) |
| 2074 | return ret; |
| 2075 | ret = lttng_metadata_printf(session, |
| 2076 | "struct { } align(%u) _%s_padding;\n", |
| 2077 | field->type.u.array_nestable.alignment * CHAR_BIT, |
| 2078 | field->name); |
| 2079 | if (ret) |
| 2080 | return ret; |
| 2081 | } |
| 2082 | /* |
| 2083 | * Nested compound types: Only array of structures and variants are |
| 2084 | * currently supported. |
| 2085 | */ |
| 2086 | elem_type = field->type.u.array_nestable.elem_type; |
| 2087 | switch (elem_type->atype) { |
| 2088 | case atype_integer: |
| 2089 | case atype_struct_nestable: |
| 2090 | case atype_variant_nestable: |
| 2091 | ret = _lttng_type_statedump(session, elem_type, nesting); |
| 2092 | if (ret) |
| 2093 | return ret; |
| 2094 | break; |
| 2095 | |
| 2096 | default: |
| 2097 | return -EINVAL; |
| 2098 | } |
| 2099 | ret = lttng_metadata_printf(session, |
| 2100 | " _%s[%u];\n", |
| 2101 | field->name, |
| 2102 | field->type.u.array_nestable.length); |
| 2103 | return ret; |
| 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * Must be called with sessions_mutex held. |
| 2108 | */ |
| 2109 | static |
| 2110 | int _lttng_sequence_field_statedump(struct lttng_session *session, |
| 2111 | const struct lttng_event_field *field, |
| 2112 | size_t nesting) |
| 2113 | { |
| 2114 | int ret; |
| 2115 | const char *length_name; |
| 2116 | const struct lttng_type *elem_type; |
| 2117 | |
| 2118 | WARN_ON_ONCE(field->type.atype != atype_sequence_nestable); |
| 2119 | |
| 2120 | length_name = field->type.u.sequence_nestable.length_name; |
| 2121 | |
| 2122 | if (field->type.u.sequence_nestable.alignment) { |
| 2123 | ret = print_tabs(session, nesting); |
| 2124 | if (ret) |
| 2125 | return ret; |
| 2126 | ret = lttng_metadata_printf(session, |
| 2127 | "struct { } align(%u) _%s_padding;\n", |
| 2128 | field->type.u.sequence_nestable.alignment * CHAR_BIT, |
| 2129 | field->name); |
| 2130 | if (ret) |
| 2131 | return ret; |
| 2132 | } |
| 2133 | |
| 2134 | /* |
| 2135 | * Nested compound types: Only array of structures and variants are |
| 2136 | * currently supported. |
| 2137 | */ |
| 2138 | elem_type = field->type.u.sequence_nestable.elem_type; |
| 2139 | switch (elem_type->atype) { |
| 2140 | case atype_integer: |
| 2141 | case atype_struct_nestable: |
| 2142 | case atype_variant_nestable: |
| 2143 | ret = _lttng_type_statedump(session, elem_type, nesting); |
| 2144 | if (ret) |
| 2145 | return ret; |
| 2146 | break; |
| 2147 | |
| 2148 | default: |
| 2149 | return -EINVAL; |
| 2150 | } |
| 2151 | ret = lttng_metadata_printf(session, |
| 2152 | " _%s[ _%s ];\n", |
| 2153 | field->name, |
| 2154 | field->type.u.sequence_nestable.length_name); |
| 2155 | return ret; |
| 2156 | } |
| 2157 | |
| 2158 | /* |
| 2159 | * Must be called with sessions_mutex held. |
| 2160 | */ |
| 2161 | static |
| 2162 | int _lttng_enum_type_statedump(struct lttng_session *session, |
| 2163 | const struct lttng_type *type, |
| 2164 | size_t nesting) |
| 2165 | { |
| 2166 | const struct lttng_enum_desc *enum_desc; |
| 2167 | const struct lttng_type *container_type; |
| 2168 | int ret; |
| 2169 | unsigned int i, nr_entries; |
| 2170 | |
| 2171 | container_type = type->u.enum_nestable.container_type; |
| 2172 | if (container_type->atype != atype_integer) { |
| 2173 | ret = -EINVAL; |
| 2174 | goto end; |
| 2175 | } |
| 2176 | enum_desc = type->u.enum_nestable.desc; |
| 2177 | nr_entries = enum_desc->nr_entries; |
| 2178 | |
| 2179 | ret = print_tabs(session, nesting); |
| 2180 | if (ret) |
| 2181 | goto end; |
| 2182 | ret = lttng_metadata_printf(session, "enum : "); |
| 2183 | if (ret) |
| 2184 | goto end; |
| 2185 | ret = _lttng_integer_type_statedump(session, container_type, 0); |
| 2186 | if (ret) |
| 2187 | goto end; |
| 2188 | ret = lttng_metadata_printf(session, " {\n"); |
| 2189 | if (ret) |
| 2190 | goto end; |
| 2191 | /* Dump all entries */ |
| 2192 | for (i = 0; i < nr_entries; i++) { |
| 2193 | const struct lttng_enum_entry *entry = &enum_desc->entries[i]; |
| 2194 | int j, len; |
| 2195 | |
| 2196 | ret = print_tabs(session, nesting + 1); |
| 2197 | if (ret) |
| 2198 | goto end; |
| 2199 | ret = lttng_metadata_printf(session, |
| 2200 | "\""); |
| 2201 | if (ret) |
| 2202 | goto end; |
| 2203 | len = strlen(entry->string); |
| 2204 | /* Escape the character '"' */ |
| 2205 | for (j = 0; j < len; j++) { |
| 2206 | char c = entry->string[j]; |
| 2207 | |
| 2208 | switch (c) { |
| 2209 | case '"': |
| 2210 | ret = lttng_metadata_printf(session, |
| 2211 | "\\\""); |
| 2212 | break; |
| 2213 | case '\\': |
| 2214 | ret = lttng_metadata_printf(session, |
| 2215 | "\\\\"); |
| 2216 | break; |
| 2217 | default: |
| 2218 | ret = lttng_metadata_printf(session, |
| 2219 | "%c", c); |
| 2220 | break; |
| 2221 | } |
| 2222 | if (ret) |
| 2223 | goto end; |
| 2224 | } |
| 2225 | ret = lttng_metadata_printf(session, "\""); |
| 2226 | if (ret) |
| 2227 | goto end; |
| 2228 | |
| 2229 | if (entry->options.is_auto) { |
| 2230 | ret = lttng_metadata_printf(session, ",\n"); |
| 2231 | if (ret) |
| 2232 | goto end; |
| 2233 | } else { |
| 2234 | ret = lttng_metadata_printf(session, |
| 2235 | " = "); |
| 2236 | if (ret) |
| 2237 | goto end; |
| 2238 | if (entry->start.signedness) |
| 2239 | ret = lttng_metadata_printf(session, |
| 2240 | "%lld", (long long) entry->start.value); |
| 2241 | else |
| 2242 | ret = lttng_metadata_printf(session, |
| 2243 | "%llu", entry->start.value); |
| 2244 | if (ret) |
| 2245 | goto end; |
| 2246 | if (entry->start.signedness == entry->end.signedness && |
| 2247 | entry->start.value |
| 2248 | == entry->end.value) { |
| 2249 | ret = lttng_metadata_printf(session, |
| 2250 | ",\n"); |
| 2251 | } else { |
| 2252 | if (entry->end.signedness) { |
| 2253 | ret = lttng_metadata_printf(session, |
| 2254 | " ... %lld,\n", |
| 2255 | (long long) entry->end.value); |
| 2256 | } else { |
| 2257 | ret = lttng_metadata_printf(session, |
| 2258 | " ... %llu,\n", |
| 2259 | entry->end.value); |
| 2260 | } |
| 2261 | } |
| 2262 | if (ret) |
| 2263 | goto end; |
| 2264 | } |
| 2265 | } |
| 2266 | ret = print_tabs(session, nesting); |
| 2267 | if (ret) |
| 2268 | goto end; |
| 2269 | ret = lttng_metadata_printf(session, "}"); |
| 2270 | end: |
| 2271 | return ret; |
| 2272 | } |
| 2273 | |
| 2274 | /* |
| 2275 | * Must be called with sessions_mutex held. |
| 2276 | */ |
| 2277 | static |
| 2278 | int _lttng_enum_field_statedump(struct lttng_session *session, |
| 2279 | const struct lttng_event_field *field, |
| 2280 | size_t nesting) |
| 2281 | { |
| 2282 | int ret; |
| 2283 | |
| 2284 | ret = _lttng_enum_type_statedump(session, &field->type, nesting); |
| 2285 | if (ret) |
| 2286 | return ret; |
| 2287 | return lttng_field_name_statedump(session, field, nesting); |
| 2288 | } |
| 2289 | |
| 2290 | static |
| 2291 | int _lttng_integer_field_statedump(struct lttng_session *session, |
| 2292 | const struct lttng_event_field *field, |
| 2293 | size_t nesting) |
| 2294 | { |
| 2295 | int ret; |
| 2296 | |
| 2297 | ret = _lttng_integer_type_statedump(session, &field->type, nesting); |
| 2298 | if (ret) |
| 2299 | return ret; |
| 2300 | return lttng_field_name_statedump(session, field, nesting); |
| 2301 | } |
| 2302 | |
| 2303 | static |
| 2304 | int _lttng_string_type_statedump(struct lttng_session *session, |
| 2305 | const struct lttng_type *type, |
| 2306 | size_t nesting) |
| 2307 | { |
| 2308 | int ret; |
| 2309 | |
| 2310 | WARN_ON_ONCE(type->atype != atype_string); |
| 2311 | /* Default encoding is UTF8 */ |
| 2312 | ret = print_tabs(session, nesting); |
| 2313 | if (ret) |
| 2314 | return ret; |
| 2315 | ret = lttng_metadata_printf(session, |
| 2316 | "string%s", |
| 2317 | type->u.string.encoding == lttng_encode_ASCII ? |
| 2318 | " { encoding = ASCII; }" : ""); |
| 2319 | return ret; |
| 2320 | } |
| 2321 | |
| 2322 | static |
| 2323 | int _lttng_string_field_statedump(struct lttng_session *session, |
| 2324 | const struct lttng_event_field *field, |
| 2325 | size_t nesting) |
| 2326 | { |
| 2327 | int ret; |
| 2328 | |
| 2329 | WARN_ON_ONCE(field->type.atype != atype_string); |
| 2330 | ret = _lttng_string_type_statedump(session, &field->type, nesting); |
| 2331 | if (ret) |
| 2332 | return ret; |
| 2333 | return lttng_field_name_statedump(session, field, nesting); |
| 2334 | } |
| 2335 | |
| 2336 | /* |
| 2337 | * Must be called with sessions_mutex held. |
| 2338 | */ |
| 2339 | static |
| 2340 | int _lttng_type_statedump(struct lttng_session *session, |
| 2341 | const struct lttng_type *type, |
| 2342 | size_t nesting) |
| 2343 | { |
| 2344 | int ret = 0; |
| 2345 | |
| 2346 | switch (type->atype) { |
| 2347 | case atype_integer: |
| 2348 | ret = _lttng_integer_type_statedump(session, type, nesting); |
| 2349 | break; |
| 2350 | case atype_enum_nestable: |
| 2351 | ret = _lttng_enum_type_statedump(session, type, nesting); |
| 2352 | break; |
| 2353 | case atype_string: |
| 2354 | ret = _lttng_string_type_statedump(session, type, nesting); |
| 2355 | break; |
| 2356 | case atype_struct_nestable: |
| 2357 | ret = _lttng_struct_type_statedump(session, type, nesting); |
| 2358 | break; |
| 2359 | case atype_variant_nestable: |
| 2360 | ret = _lttng_variant_type_statedump(session, type, nesting); |
| 2361 | break; |
| 2362 | |
| 2363 | /* Nested arrays and sequences are not supported yet. */ |
| 2364 | case atype_array_nestable: |
| 2365 | case atype_sequence_nestable: |
| 2366 | default: |
| 2367 | WARN_ON_ONCE(1); |
| 2368 | return -EINVAL; |
| 2369 | } |
| 2370 | return ret; |
| 2371 | } |
| 2372 | |
| 2373 | /* |
| 2374 | * Must be called with sessions_mutex held. |
| 2375 | */ |
| 2376 | static |
| 2377 | int _lttng_field_statedump(struct lttng_session *session, |
| 2378 | const struct lttng_event_field *field, |
| 2379 | size_t nesting) |
| 2380 | { |
| 2381 | int ret = 0; |
| 2382 | |
| 2383 | switch (field->type.atype) { |
| 2384 | case atype_integer: |
| 2385 | ret = _lttng_integer_field_statedump(session, field, nesting); |
| 2386 | break; |
| 2387 | case atype_enum_nestable: |
| 2388 | ret = _lttng_enum_field_statedump(session, field, nesting); |
| 2389 | break; |
| 2390 | case atype_string: |
| 2391 | ret = _lttng_string_field_statedump(session, field, nesting); |
| 2392 | break; |
| 2393 | case atype_struct_nestable: |
| 2394 | ret = _lttng_struct_field_statedump(session, field, nesting); |
| 2395 | break; |
| 2396 | case atype_array_nestable: |
| 2397 | ret = _lttng_array_field_statedump(session, field, nesting); |
| 2398 | break; |
| 2399 | case atype_sequence_nestable: |
| 2400 | ret = _lttng_sequence_field_statedump(session, field, nesting); |
| 2401 | break; |
| 2402 | case atype_variant_nestable: |
| 2403 | ret = _lttng_variant_field_statedump(session, field, nesting); |
| 2404 | break; |
| 2405 | |
| 2406 | default: |
| 2407 | WARN_ON_ONCE(1); |
| 2408 | return -EINVAL; |
| 2409 | } |
| 2410 | return ret; |
| 2411 | } |
| 2412 | |
| 2413 | static |
| 2414 | int _lttng_context_metadata_statedump(struct lttng_session *session, |
| 2415 | struct lttng_ctx *ctx) |
| 2416 | { |
| 2417 | int ret = 0; |
| 2418 | int i; |
| 2419 | |
| 2420 | if (!ctx) |
| 2421 | return 0; |
| 2422 | for (i = 0; i < ctx->nr_fields; i++) { |
| 2423 | const struct lttng_ctx_field *field = &ctx->fields[i]; |
| 2424 | |
| 2425 | ret = _lttng_field_statedump(session, &field->event_field, 2); |
| 2426 | if (ret) |
| 2427 | return ret; |
| 2428 | } |
| 2429 | return ret; |
| 2430 | } |
| 2431 | |
| 2432 | static |
| 2433 | int _lttng_fields_metadata_statedump(struct lttng_session *session, |
| 2434 | struct lttng_event *event) |
| 2435 | { |
| 2436 | const struct lttng_event_desc *desc = event->desc; |
| 2437 | int ret = 0; |
| 2438 | int i; |
| 2439 | |
| 2440 | for (i = 0; i < desc->nr_fields; i++) { |
| 2441 | const struct lttng_event_field *field = &desc->fields[i]; |
| 2442 | |
| 2443 | ret = _lttng_field_statedump(session, field, 2); |
| 2444 | if (ret) |
| 2445 | return ret; |
| 2446 | } |
| 2447 | return ret; |
| 2448 | } |
| 2449 | |
| 2450 | /* |
| 2451 | * Must be called with sessions_mutex held. |
| 2452 | * The entire event metadata is printed as a single atomic metadata |
| 2453 | * transaction. |
| 2454 | */ |
| 2455 | static |
| 2456 | int _lttng_event_metadata_statedump(struct lttng_session *session, |
| 2457 | struct lttng_channel *chan, |
| 2458 | struct lttng_event *event) |
| 2459 | { |
| 2460 | int ret = 0; |
| 2461 | |
| 2462 | if (event->metadata_dumped || !LTTNG_READ_ONCE(session->active)) |
| 2463 | return 0; |
| 2464 | if (chan->channel_type == METADATA_CHANNEL) |
| 2465 | return 0; |
| 2466 | |
| 2467 | lttng_metadata_begin(session); |
| 2468 | |
| 2469 | ret = lttng_metadata_printf(session, |
| 2470 | "event {\n" |
| 2471 | " name = \"%s\";\n" |
| 2472 | " id = %u;\n" |
| 2473 | " stream_id = %u;\n", |
| 2474 | event->desc->name, |
| 2475 | event->id, |
| 2476 | event->chan->id); |
| 2477 | if (ret) |
| 2478 | goto end; |
| 2479 | |
| 2480 | if (event->ctx) { |
| 2481 | ret = lttng_metadata_printf(session, |
| 2482 | " context := struct {\n"); |
| 2483 | if (ret) |
| 2484 | goto end; |
| 2485 | } |
| 2486 | ret = _lttng_context_metadata_statedump(session, event->ctx); |
| 2487 | if (ret) |
| 2488 | goto end; |
| 2489 | if (event->ctx) { |
| 2490 | ret = lttng_metadata_printf(session, |
| 2491 | " };\n"); |
| 2492 | if (ret) |
| 2493 | goto end; |
| 2494 | } |
| 2495 | |
| 2496 | ret = lttng_metadata_printf(session, |
| 2497 | " fields := struct {\n" |
| 2498 | ); |
| 2499 | if (ret) |
| 2500 | goto end; |
| 2501 | |
| 2502 | ret = _lttng_fields_metadata_statedump(session, event); |
| 2503 | if (ret) |
| 2504 | goto end; |
| 2505 | |
| 2506 | /* |
| 2507 | * LTTng space reservation can only reserve multiples of the |
| 2508 | * byte size. |
| 2509 | */ |
| 2510 | ret = lttng_metadata_printf(session, |
| 2511 | " };\n" |
| 2512 | "};\n\n"); |
| 2513 | if (ret) |
| 2514 | goto end; |
| 2515 | |
| 2516 | event->metadata_dumped = 1; |
| 2517 | end: |
| 2518 | lttng_metadata_end(session); |
| 2519 | return ret; |
| 2520 | |
| 2521 | } |
| 2522 | |
| 2523 | /* |
| 2524 | * Must be called with sessions_mutex held. |
| 2525 | * The entire channel metadata is printed as a single atomic metadata |
| 2526 | * transaction. |
| 2527 | */ |
| 2528 | static |
| 2529 | int _lttng_channel_metadata_statedump(struct lttng_session *session, |
| 2530 | struct lttng_channel *chan) |
| 2531 | { |
| 2532 | int ret = 0; |
| 2533 | |
| 2534 | if (chan->metadata_dumped || !LTTNG_READ_ONCE(session->active)) |
| 2535 | return 0; |
| 2536 | |
| 2537 | if (chan->channel_type == METADATA_CHANNEL) |
| 2538 | return 0; |
| 2539 | |
| 2540 | lttng_metadata_begin(session); |
| 2541 | |
| 2542 | WARN_ON_ONCE(!chan->header_type); |
| 2543 | ret = lttng_metadata_printf(session, |
| 2544 | "stream {\n" |
| 2545 | " id = %u;\n" |
| 2546 | " event.header := %s;\n" |
| 2547 | " packet.context := struct packet_context;\n", |
| 2548 | chan->id, |
| 2549 | chan->header_type == 1 ? "struct event_header_compact" : |
| 2550 | "struct event_header_large"); |
| 2551 | if (ret) |
| 2552 | goto end; |
| 2553 | |
| 2554 | if (chan->ctx) { |
| 2555 | ret = lttng_metadata_printf(session, |
| 2556 | " event.context := struct {\n"); |
| 2557 | if (ret) |
| 2558 | goto end; |
| 2559 | } |
| 2560 | ret = _lttng_context_metadata_statedump(session, chan->ctx); |
| 2561 | if (ret) |
| 2562 | goto end; |
| 2563 | if (chan->ctx) { |
| 2564 | ret = lttng_metadata_printf(session, |
| 2565 | " };\n"); |
| 2566 | if (ret) |
| 2567 | goto end; |
| 2568 | } |
| 2569 | |
| 2570 | ret = lttng_metadata_printf(session, |
| 2571 | "};\n\n"); |
| 2572 | |
| 2573 | chan->metadata_dumped = 1; |
| 2574 | end: |
| 2575 | lttng_metadata_end(session); |
| 2576 | return ret; |
| 2577 | } |
| 2578 | |
| 2579 | /* |
| 2580 | * Must be called with sessions_mutex held. |
| 2581 | */ |
| 2582 | static |
| 2583 | int _lttng_stream_packet_context_declare(struct lttng_session *session) |
| 2584 | { |
| 2585 | return lttng_metadata_printf(session, |
| 2586 | "struct packet_context {\n" |
| 2587 | " uint64_clock_monotonic_t timestamp_begin;\n" |
| 2588 | " uint64_clock_monotonic_t timestamp_end;\n" |
| 2589 | " uint64_t content_size;\n" |
| 2590 | " uint64_t packet_size;\n" |
| 2591 | " uint64_t packet_seq_num;\n" |
| 2592 | " unsigned long events_discarded;\n" |
| 2593 | " uint32_t cpu_id;\n" |
| 2594 | "};\n\n" |
| 2595 | ); |
| 2596 | } |
| 2597 | |
| 2598 | /* |
| 2599 | * Compact header: |
| 2600 | * id: range: 0 - 30. |
| 2601 | * id 31 is reserved to indicate an extended header. |
| 2602 | * |
| 2603 | * Large header: |
| 2604 | * id: range: 0 - 65534. |
| 2605 | * id 65535 is reserved to indicate an extended header. |
| 2606 | * |
| 2607 | * Must be called with sessions_mutex held. |
| 2608 | */ |
| 2609 | static |
| 2610 | int _lttng_event_header_declare(struct lttng_session *session) |
| 2611 | { |
| 2612 | return lttng_metadata_printf(session, |
| 2613 | "struct event_header_compact {\n" |
| 2614 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" |
| 2615 | " variant <id> {\n" |
| 2616 | " struct {\n" |
| 2617 | " uint27_clock_monotonic_t timestamp;\n" |
| 2618 | " } compact;\n" |
| 2619 | " struct {\n" |
| 2620 | " uint32_t id;\n" |
| 2621 | " uint64_clock_monotonic_t timestamp;\n" |
| 2622 | " } extended;\n" |
| 2623 | " } v;\n" |
| 2624 | "} align(%u);\n" |
| 2625 | "\n" |
| 2626 | "struct event_header_large {\n" |
| 2627 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" |
| 2628 | " variant <id> {\n" |
| 2629 | " struct {\n" |
| 2630 | " uint32_clock_monotonic_t timestamp;\n" |
| 2631 | " } compact;\n" |
| 2632 | " struct {\n" |
| 2633 | " uint32_t id;\n" |
| 2634 | " uint64_clock_monotonic_t timestamp;\n" |
| 2635 | " } extended;\n" |
| 2636 | " } v;\n" |
| 2637 | "} align(%u);\n\n", |
| 2638 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 2639 | lttng_alignof(uint16_t) * CHAR_BIT |
| 2640 | ); |
| 2641 | } |
| 2642 | |
| 2643 | /* |
| 2644 | * Approximation of NTP time of day to clock monotonic correlation, |
| 2645 | * taken at start of trace. |
| 2646 | * Yes, this is only an approximation. Yes, we can (and will) do better |
| 2647 | * in future versions. |
| 2648 | * This function may return a negative offset. It may happen if the |
| 2649 | * system sets the REALTIME clock to 0 after boot. |
| 2650 | * |
| 2651 | * Use 64bit timespec on kernels that have it, this makes 32bit arch |
| 2652 | * y2038 compliant. |
| 2653 | */ |
| 2654 | static |
| 2655 | int64_t measure_clock_offset(void) |
| 2656 | { |
| 2657 | uint64_t monotonic_avg, monotonic[2], realtime; |
| 2658 | uint64_t tcf = trace_clock_freq(); |
| 2659 | int64_t offset; |
| 2660 | unsigned long flags; |
| 2661 | #ifdef LTTNG_KERNEL_HAS_TIMESPEC64 |
| 2662 | struct timespec64 rts = { 0, 0 }; |
| 2663 | #else |
| 2664 | struct timespec rts = { 0, 0 }; |
| 2665 | #endif |
| 2666 | |
| 2667 | /* Disable interrupts to increase correlation precision. */ |
| 2668 | local_irq_save(flags); |
| 2669 | monotonic[0] = trace_clock_read64(); |
| 2670 | #ifdef LTTNG_KERNEL_HAS_TIMESPEC64 |
| 2671 | ktime_get_real_ts64(&rts); |
| 2672 | #else |
| 2673 | getnstimeofday(&rts); |
| 2674 | #endif |
| 2675 | monotonic[1] = trace_clock_read64(); |
| 2676 | local_irq_restore(flags); |
| 2677 | |
| 2678 | monotonic_avg = (monotonic[0] + monotonic[1]) >> 1; |
| 2679 | realtime = (uint64_t) rts.tv_sec * tcf; |
| 2680 | if (tcf == NSEC_PER_SEC) { |
| 2681 | realtime += rts.tv_nsec; |
| 2682 | } else { |
| 2683 | uint64_t n = rts.tv_nsec * tcf; |
| 2684 | |
| 2685 | do_div(n, NSEC_PER_SEC); |
| 2686 | realtime += n; |
| 2687 | } |
| 2688 | offset = (int64_t) realtime - monotonic_avg; |
| 2689 | return offset; |
| 2690 | } |
| 2691 | |
| 2692 | static |
| 2693 | int print_escaped_ctf_string(struct lttng_session *session, const char *string) |
| 2694 | { |
| 2695 | int ret = 0; |
| 2696 | size_t i; |
| 2697 | char cur; |
| 2698 | |
| 2699 | i = 0; |
| 2700 | cur = string[i]; |
| 2701 | while (cur != '\0') { |
| 2702 | switch (cur) { |
| 2703 | case '\n': |
| 2704 | ret = lttng_metadata_printf(session, "%s", "\\n"); |
| 2705 | break; |
| 2706 | case '\\': |
| 2707 | case '"': |
| 2708 | ret = lttng_metadata_printf(session, "%c", '\\'); |
| 2709 | if (ret) |
| 2710 | goto error; |
| 2711 | /* We still print the current char */ |
| 2712 | /* Fallthrough */ |
| 2713 | default: |
| 2714 | ret = lttng_metadata_printf(session, "%c", cur); |
| 2715 | break; |
| 2716 | } |
| 2717 | |
| 2718 | if (ret) |
| 2719 | goto error; |
| 2720 | |
| 2721 | cur = string[++i]; |
| 2722 | } |
| 2723 | error: |
| 2724 | return ret; |
| 2725 | } |
| 2726 | |
| 2727 | static |
| 2728 | int print_metadata_escaped_field(struct lttng_session *session, const char *field, |
| 2729 | const char *field_value) |
| 2730 | { |
| 2731 | int ret; |
| 2732 | |
| 2733 | ret = lttng_metadata_printf(session, " %s = \"", field); |
| 2734 | if (ret) |
| 2735 | goto error; |
| 2736 | |
| 2737 | ret = print_escaped_ctf_string(session, field_value); |
| 2738 | if (ret) |
| 2739 | goto error; |
| 2740 | |
| 2741 | ret = lttng_metadata_printf(session, "\";\n"); |
| 2742 | |
| 2743 | error: |
| 2744 | return ret; |
| 2745 | } |
| 2746 | |
| 2747 | /* |
| 2748 | * Output metadata into this session's metadata buffers. |
| 2749 | * Must be called with sessions_mutex held. |
| 2750 | */ |
| 2751 | static |
| 2752 | int _lttng_session_metadata_statedump(struct lttng_session *session) |
| 2753 | { |
| 2754 | unsigned char *uuid_c = session->uuid.b; |
| 2755 | unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN]; |
| 2756 | const char *product_uuid; |
| 2757 | struct lttng_channel *chan; |
| 2758 | struct lttng_event *event; |
| 2759 | int ret = 0; |
| 2760 | |
| 2761 | if (!LTTNG_READ_ONCE(session->active)) |
| 2762 | return 0; |
| 2763 | |
| 2764 | lttng_metadata_begin(session); |
| 2765 | |
| 2766 | if (session->metadata_dumped) |
| 2767 | goto skip_session; |
| 2768 | |
| 2769 | snprintf(uuid_s, sizeof(uuid_s), |
| 2770 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 2771 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], |
| 2772 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], |
| 2773 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], |
| 2774 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); |
| 2775 | |
| 2776 | ret = lttng_metadata_printf(session, |
| 2777 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
| 2778 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" |
| 2779 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" |
| 2780 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" |
| 2781 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" |
| 2782 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" |
| 2783 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" |
| 2784 | "\n" |
| 2785 | "trace {\n" |
| 2786 | " major = %u;\n" |
| 2787 | " minor = %u;\n" |
| 2788 | " uuid = \"%s\";\n" |
| 2789 | " byte_order = %s;\n" |
| 2790 | " packet.header := struct {\n" |
| 2791 | " uint32_t magic;\n" |
| 2792 | " uint8_t uuid[16];\n" |
| 2793 | " uint32_t stream_id;\n" |
| 2794 | " uint64_t stream_instance_id;\n" |
| 2795 | " };\n" |
| 2796 | "};\n\n", |
| 2797 | lttng_alignof(uint8_t) * CHAR_BIT, |
| 2798 | lttng_alignof(uint16_t) * CHAR_BIT, |
| 2799 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 2800 | lttng_alignof(uint64_t) * CHAR_BIT, |
| 2801 | sizeof(unsigned long) * CHAR_BIT, |
| 2802 | lttng_alignof(unsigned long) * CHAR_BIT, |
| 2803 | CTF_SPEC_MAJOR, |
| 2804 | CTF_SPEC_MINOR, |
| 2805 | uuid_s, |
| 2806 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 2807 | "be" |
| 2808 | #else |
| 2809 | "le" |
| 2810 | #endif |
| 2811 | ); |
| 2812 | if (ret) |
| 2813 | goto end; |
| 2814 | |
| 2815 | ret = lttng_metadata_printf(session, |
| 2816 | "env {\n" |
| 2817 | " hostname = \"%s\";\n" |
| 2818 | " domain = \"kernel\";\n" |
| 2819 | " sysname = \"%s\";\n" |
| 2820 | " kernel_release = \"%s\";\n" |
| 2821 | " kernel_version = \"%s\";\n" |
| 2822 | " tracer_name = \"lttng-modules\";\n" |
| 2823 | " tracer_major = %d;\n" |
| 2824 | " tracer_minor = %d;\n" |
| 2825 | " tracer_patchlevel = %d;\n" |
| 2826 | " trace_buffering_scheme = \"global\";\n", |
| 2827 | current->nsproxy->uts_ns->name.nodename, |
| 2828 | utsname()->sysname, |
| 2829 | utsname()->release, |
| 2830 | utsname()->version, |
| 2831 | LTTNG_MODULES_MAJOR_VERSION, |
| 2832 | LTTNG_MODULES_MINOR_VERSION, |
| 2833 | LTTNG_MODULES_PATCHLEVEL_VERSION |
| 2834 | ); |
| 2835 | if (ret) |
| 2836 | goto end; |
| 2837 | |
| 2838 | ret = print_metadata_escaped_field(session, "trace_name", session->name); |
| 2839 | if (ret) |
| 2840 | goto end; |
| 2841 | ret = print_metadata_escaped_field(session, "trace_creation_datetime", |
| 2842 | session->creation_time); |
| 2843 | if (ret) |
| 2844 | goto end; |
| 2845 | |
| 2846 | /* Add the product UUID to the 'env' section */ |
| 2847 | product_uuid = dmi_get_system_info(DMI_PRODUCT_UUID); |
| 2848 | if (product_uuid) { |
| 2849 | ret = lttng_metadata_printf(session, |
| 2850 | " product_uuid = \"%s\";\n", |
| 2851 | product_uuid |
| 2852 | ); |
| 2853 | if (ret) |
| 2854 | goto end; |
| 2855 | } |
| 2856 | |
| 2857 | /* Close the 'env' section */ |
| 2858 | ret = lttng_metadata_printf(session, "};\n\n"); |
| 2859 | if (ret) |
| 2860 | goto end; |
| 2861 | |
| 2862 | ret = lttng_metadata_printf(session, |
| 2863 | "clock {\n" |
| 2864 | " name = \"%s\";\n", |
| 2865 | trace_clock_name() |
| 2866 | ); |
| 2867 | if (ret) |
| 2868 | goto end; |
| 2869 | |
| 2870 | if (!trace_clock_uuid(clock_uuid_s)) { |
| 2871 | ret = lttng_metadata_printf(session, |
| 2872 | " uuid = \"%s\";\n", |
| 2873 | clock_uuid_s |
| 2874 | ); |
| 2875 | if (ret) |
| 2876 | goto end; |
| 2877 | } |
| 2878 | |
| 2879 | ret = lttng_metadata_printf(session, |
| 2880 | " description = \"%s\";\n" |
| 2881 | " freq = %llu; /* Frequency, in Hz */\n" |
| 2882 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" |
| 2883 | " offset = %lld;\n" |
| 2884 | "};\n\n", |
| 2885 | trace_clock_description(), |
| 2886 | (unsigned long long) trace_clock_freq(), |
| 2887 | (long long) measure_clock_offset() |
| 2888 | ); |
| 2889 | if (ret) |
| 2890 | goto end; |
| 2891 | |
| 2892 | ret = lttng_metadata_printf(session, |
| 2893 | "typealias integer {\n" |
| 2894 | " size = 27; align = 1; signed = false;\n" |
| 2895 | " map = clock.%s.value;\n" |
| 2896 | "} := uint27_clock_monotonic_t;\n" |
| 2897 | "\n" |
| 2898 | "typealias integer {\n" |
| 2899 | " size = 32; align = %u; signed = false;\n" |
| 2900 | " map = clock.%s.value;\n" |
| 2901 | "} := uint32_clock_monotonic_t;\n" |
| 2902 | "\n" |
| 2903 | "typealias integer {\n" |
| 2904 | " size = 64; align = %u; signed = false;\n" |
| 2905 | " map = clock.%s.value;\n" |
| 2906 | "} := uint64_clock_monotonic_t;\n\n", |
| 2907 | trace_clock_name(), |
| 2908 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 2909 | trace_clock_name(), |
| 2910 | lttng_alignof(uint64_t) * CHAR_BIT, |
| 2911 | trace_clock_name() |
| 2912 | ); |
| 2913 | if (ret) |
| 2914 | goto end; |
| 2915 | |
| 2916 | ret = _lttng_stream_packet_context_declare(session); |
| 2917 | if (ret) |
| 2918 | goto end; |
| 2919 | |
| 2920 | ret = _lttng_event_header_declare(session); |
| 2921 | if (ret) |
| 2922 | goto end; |
| 2923 | |
| 2924 | skip_session: |
| 2925 | list_for_each_entry(chan, &session->chan, list) { |
| 2926 | ret = _lttng_channel_metadata_statedump(session, chan); |
| 2927 | if (ret) |
| 2928 | goto end; |
| 2929 | } |
| 2930 | |
| 2931 | list_for_each_entry(event, &session->events, list) { |
| 2932 | ret = _lttng_event_metadata_statedump(session, event->chan, event); |
| 2933 | if (ret) |
| 2934 | goto end; |
| 2935 | } |
| 2936 | session->metadata_dumped = 1; |
| 2937 | end: |
| 2938 | lttng_metadata_end(session); |
| 2939 | return ret; |
| 2940 | } |
| 2941 | |
| 2942 | /** |
| 2943 | * lttng_transport_register - LTT transport registration |
| 2944 | * @transport: transport structure |
| 2945 | * |
| 2946 | * Registers a transport which can be used as output to extract the data out of |
| 2947 | * LTTng. The module calling this registration function must ensure that no |
| 2948 | * trap-inducing code will be executed by the transport functions. E.g. |
| 2949 | * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory |
| 2950 | * is made visible to the transport function. This registration acts as a |
| 2951 | * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory |
| 2952 | * after its registration must it synchronize the TLBs. |
| 2953 | */ |
| 2954 | void lttng_transport_register(struct lttng_transport *transport) |
| 2955 | { |
| 2956 | /* |
| 2957 | * Make sure no page fault can be triggered by the module about to be |
| 2958 | * registered. We deal with this here so we don't have to call |
| 2959 | * vmalloc_sync_mappings() in each module's init. |
| 2960 | */ |
| 2961 | wrapper_vmalloc_sync_mappings(); |
| 2962 | |
| 2963 | mutex_lock(&sessions_mutex); |
| 2964 | list_add_tail(&transport->node, <tng_transport_list); |
| 2965 | mutex_unlock(&sessions_mutex); |
| 2966 | } |
| 2967 | EXPORT_SYMBOL_GPL(lttng_transport_register); |
| 2968 | |
| 2969 | /** |
| 2970 | * lttng_transport_unregister - LTT transport unregistration |
| 2971 | * @transport: transport structure |
| 2972 | */ |
| 2973 | void lttng_transport_unregister(struct lttng_transport *transport) |
| 2974 | { |
| 2975 | mutex_lock(&sessions_mutex); |
| 2976 | list_del(&transport->node); |
| 2977 | mutex_unlock(&sessions_mutex); |
| 2978 | } |
| 2979 | EXPORT_SYMBOL_GPL(lttng_transport_unregister); |
| 2980 | |
| 2981 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
| 2982 | |
| 2983 | enum cpuhp_state lttng_hp_prepare; |
| 2984 | enum cpuhp_state lttng_hp_online; |
| 2985 | |
| 2986 | static int lttng_hotplug_prepare(unsigned int cpu, struct hlist_node *node) |
| 2987 | { |
| 2988 | struct lttng_cpuhp_node *lttng_node; |
| 2989 | |
| 2990 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); |
| 2991 | switch (lttng_node->component) { |
| 2992 | case LTTNG_RING_BUFFER_FRONTEND: |
| 2993 | return 0; |
| 2994 | case LTTNG_RING_BUFFER_BACKEND: |
| 2995 | return lttng_cpuhp_rb_backend_prepare(cpu, lttng_node); |
| 2996 | case LTTNG_RING_BUFFER_ITER: |
| 2997 | return 0; |
| 2998 | case LTTNG_CONTEXT_PERF_COUNTERS: |
| 2999 | return 0; |
| 3000 | default: |
| 3001 | return -EINVAL; |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | static int lttng_hotplug_dead(unsigned int cpu, struct hlist_node *node) |
| 3006 | { |
| 3007 | struct lttng_cpuhp_node *lttng_node; |
| 3008 | |
| 3009 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); |
| 3010 | switch (lttng_node->component) { |
| 3011 | case LTTNG_RING_BUFFER_FRONTEND: |
| 3012 | return lttng_cpuhp_rb_frontend_dead(cpu, lttng_node); |
| 3013 | case LTTNG_RING_BUFFER_BACKEND: |
| 3014 | return 0; |
| 3015 | case LTTNG_RING_BUFFER_ITER: |
| 3016 | return 0; |
| 3017 | case LTTNG_CONTEXT_PERF_COUNTERS: |
| 3018 | return lttng_cpuhp_perf_counter_dead(cpu, lttng_node); |
| 3019 | default: |
| 3020 | return -EINVAL; |
| 3021 | } |
| 3022 | } |
| 3023 | |
| 3024 | static int lttng_hotplug_online(unsigned int cpu, struct hlist_node *node) |
| 3025 | { |
| 3026 | struct lttng_cpuhp_node *lttng_node; |
| 3027 | |
| 3028 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); |
| 3029 | switch (lttng_node->component) { |
| 3030 | case LTTNG_RING_BUFFER_FRONTEND: |
| 3031 | return lttng_cpuhp_rb_frontend_online(cpu, lttng_node); |
| 3032 | case LTTNG_RING_BUFFER_BACKEND: |
| 3033 | return 0; |
| 3034 | case LTTNG_RING_BUFFER_ITER: |
| 3035 | return lttng_cpuhp_rb_iter_online(cpu, lttng_node); |
| 3036 | case LTTNG_CONTEXT_PERF_COUNTERS: |
| 3037 | return lttng_cpuhp_perf_counter_online(cpu, lttng_node); |
| 3038 | default: |
| 3039 | return -EINVAL; |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | static int lttng_hotplug_offline(unsigned int cpu, struct hlist_node *node) |
| 3044 | { |
| 3045 | struct lttng_cpuhp_node *lttng_node; |
| 3046 | |
| 3047 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); |
| 3048 | switch (lttng_node->component) { |
| 3049 | case LTTNG_RING_BUFFER_FRONTEND: |
| 3050 | return lttng_cpuhp_rb_frontend_offline(cpu, lttng_node); |
| 3051 | case LTTNG_RING_BUFFER_BACKEND: |
| 3052 | return 0; |
| 3053 | case LTTNG_RING_BUFFER_ITER: |
| 3054 | return 0; |
| 3055 | case LTTNG_CONTEXT_PERF_COUNTERS: |
| 3056 | return 0; |
| 3057 | default: |
| 3058 | return -EINVAL; |
| 3059 | } |
| 3060 | } |
| 3061 | |
| 3062 | static int __init lttng_init_cpu_hotplug(void) |
| 3063 | { |
| 3064 | int ret; |
| 3065 | |
| 3066 | ret = cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN, "lttng:prepare", |
| 3067 | lttng_hotplug_prepare, |
| 3068 | lttng_hotplug_dead); |
| 3069 | if (ret < 0) { |
| 3070 | return ret; |
| 3071 | } |
| 3072 | lttng_hp_prepare = ret; |
| 3073 | lttng_rb_set_hp_prepare(ret); |
| 3074 | |
| 3075 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "lttng:online", |
| 3076 | lttng_hotplug_online, |
| 3077 | lttng_hotplug_offline); |
| 3078 | if (ret < 0) { |
| 3079 | cpuhp_remove_multi_state(lttng_hp_prepare); |
| 3080 | lttng_hp_prepare = 0; |
| 3081 | return ret; |
| 3082 | } |
| 3083 | lttng_hp_online = ret; |
| 3084 | lttng_rb_set_hp_online(ret); |
| 3085 | |
| 3086 | return 0; |
| 3087 | } |
| 3088 | |
| 3089 | static void __exit lttng_exit_cpu_hotplug(void) |
| 3090 | { |
| 3091 | lttng_rb_set_hp_online(0); |
| 3092 | cpuhp_remove_multi_state(lttng_hp_online); |
| 3093 | lttng_rb_set_hp_prepare(0); |
| 3094 | cpuhp_remove_multi_state(lttng_hp_prepare); |
| 3095 | } |
| 3096 | |
| 3097 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
| 3098 | static int lttng_init_cpu_hotplug(void) |
| 3099 | { |
| 3100 | return 0; |
| 3101 | } |
| 3102 | static void lttng_exit_cpu_hotplug(void) |
| 3103 | { |
| 3104 | } |
| 3105 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
| 3106 | |
| 3107 | |
| 3108 | static int __init lttng_events_init(void) |
| 3109 | { |
| 3110 | int ret; |
| 3111 | |
| 3112 | ret = wrapper_lttng_fixup_sig(THIS_MODULE); |
| 3113 | if (ret) |
| 3114 | return ret; |
| 3115 | ret = wrapper_get_pfnblock_flags_mask_init(); |
| 3116 | if (ret) |
| 3117 | return ret; |
| 3118 | ret = wrapper_get_pageblock_flags_mask_init(); |
| 3119 | if (ret) |
| 3120 | return ret; |
| 3121 | ret = lttng_probes_init(); |
| 3122 | if (ret) |
| 3123 | return ret; |
| 3124 | ret = lttng_context_init(); |
| 3125 | if (ret) |
| 3126 | return ret; |
| 3127 | ret = lttng_tracepoint_init(); |
| 3128 | if (ret) |
| 3129 | goto error_tp; |
| 3130 | event_cache = KMEM_CACHE(lttng_event, 0); |
| 3131 | if (!event_cache) { |
| 3132 | ret = -ENOMEM; |
| 3133 | goto error_kmem; |
| 3134 | } |
| 3135 | ret = lttng_abi_init(); |
| 3136 | if (ret) |
| 3137 | goto error_abi; |
| 3138 | ret = lttng_logger_init(); |
| 3139 | if (ret) |
| 3140 | goto error_logger; |
| 3141 | ret = lttng_init_cpu_hotplug(); |
| 3142 | if (ret) |
| 3143 | goto error_hotplug; |
| 3144 | printk(KERN_NOTICE "LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n", |
| 3145 | __stringify(LTTNG_MODULES_MAJOR_VERSION), |
| 3146 | __stringify(LTTNG_MODULES_MINOR_VERSION), |
| 3147 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION), |
| 3148 | LTTNG_MODULES_EXTRAVERSION, |
| 3149 | LTTNG_VERSION_NAME, |
| 3150 | #ifdef LTTNG_EXTRA_VERSION_GIT |
| 3151 | LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT, |
| 3152 | #else |
| 3153 | "", |
| 3154 | #endif |
| 3155 | #ifdef LTTNG_EXTRA_VERSION_NAME |
| 3156 | LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME); |
| 3157 | #else |
| 3158 | ""); |
| 3159 | #endif |
| 3160 | return 0; |
| 3161 | |
| 3162 | error_hotplug: |
| 3163 | lttng_logger_exit(); |
| 3164 | error_logger: |
| 3165 | lttng_abi_exit(); |
| 3166 | error_abi: |
| 3167 | kmem_cache_destroy(event_cache); |
| 3168 | error_kmem: |
| 3169 | lttng_tracepoint_exit(); |
| 3170 | error_tp: |
| 3171 | lttng_context_exit(); |
| 3172 | printk(KERN_NOTICE "LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n", |
| 3173 | __stringify(LTTNG_MODULES_MAJOR_VERSION), |
| 3174 | __stringify(LTTNG_MODULES_MINOR_VERSION), |
| 3175 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION), |
| 3176 | LTTNG_MODULES_EXTRAVERSION, |
| 3177 | LTTNG_VERSION_NAME, |
| 3178 | #ifdef LTTNG_EXTRA_VERSION_GIT |
| 3179 | LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT, |
| 3180 | #else |
| 3181 | "", |
| 3182 | #endif |
| 3183 | #ifdef LTTNG_EXTRA_VERSION_NAME |
| 3184 | LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME); |
| 3185 | #else |
| 3186 | ""); |
| 3187 | #endif |
| 3188 | return ret; |
| 3189 | } |
| 3190 | |
| 3191 | module_init(lttng_events_init); |
| 3192 | |
| 3193 | static void __exit lttng_events_exit(void) |
| 3194 | { |
| 3195 | struct lttng_session *session, *tmpsession; |
| 3196 | |
| 3197 | lttng_exit_cpu_hotplug(); |
| 3198 | lttng_logger_exit(); |
| 3199 | lttng_abi_exit(); |
| 3200 | list_for_each_entry_safe(session, tmpsession, &sessions, list) |
| 3201 | lttng_session_destroy(session); |
| 3202 | kmem_cache_destroy(event_cache); |
| 3203 | lttng_tracepoint_exit(); |
| 3204 | lttng_context_exit(); |
| 3205 | printk(KERN_NOTICE "LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n", |
| 3206 | __stringify(LTTNG_MODULES_MAJOR_VERSION), |
| 3207 | __stringify(LTTNG_MODULES_MINOR_VERSION), |
| 3208 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION), |
| 3209 | LTTNG_MODULES_EXTRAVERSION, |
| 3210 | LTTNG_VERSION_NAME, |
| 3211 | #ifdef LTTNG_EXTRA_VERSION_GIT |
| 3212 | LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT, |
| 3213 | #else |
| 3214 | "", |
| 3215 | #endif |
| 3216 | #ifdef LTTNG_EXTRA_VERSION_NAME |
| 3217 | LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME); |
| 3218 | #else |
| 3219 | ""); |
| 3220 | #endif |
| 3221 | } |
| 3222 | |
| 3223 | module_exit(lttng_events_exit); |
| 3224 | |
| 3225 | #include <generated/patches.h> |
| 3226 | #ifdef LTTNG_EXTRA_VERSION_GIT |
| 3227 | MODULE_INFO(extra_version_git, LTTNG_EXTRA_VERSION_GIT); |
| 3228 | #endif |
| 3229 | #ifdef LTTNG_EXTRA_VERSION_NAME |
| 3230 | MODULE_INFO(extra_version_name, LTTNG_EXTRA_VERSION_NAME); |
| 3231 | #endif |
| 3232 | MODULE_LICENSE("GPL and additional rights"); |
| 3233 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); |
| 3234 | MODULE_DESCRIPTION("LTTng tracer"); |
| 3235 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
| 3236 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." |
| 3237 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) |
| 3238 | LTTNG_MODULES_EXTRAVERSION); |