| 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/compiler_attributes.h> |
| 32 | #include <wrapper/uuid.h> |
| 33 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */ |
| 34 | #include <wrapper/random.h> |
| 35 | #include <wrapper/tracepoint.h> |
| 36 | #include <wrapper/list.h> |
| 37 | #include <wrapper/types.h> |
| 38 | #include <lttng/kernel-version.h> |
| 39 | #include <lttng/events.h> |
| 40 | #include <lttng/events-internal.h> |
| 41 | #include <lttng/lttng-bytecode.h> |
| 42 | #include <lttng/tracer.h> |
| 43 | #include <lttng/event-notifier-notification.h> |
| 44 | #include <lttng/abi-old.h> |
| 45 | #include <lttng/endian.h> |
| 46 | #include <lttng/string-utils.h> |
| 47 | #include <lttng/utils.h> |
| 48 | #include <ringbuffer/backend.h> |
| 49 | #include <ringbuffer/frontend.h> |
| 50 | #include <wrapper/time.h> |
| 51 | |
| 52 | #define METADATA_CACHE_DEFAULT_SIZE 4096 |
| 53 | |
| 54 | static LIST_HEAD(sessions); |
| 55 | static LIST_HEAD(event_notifier_groups); |
| 56 | static LIST_HEAD(lttng_transport_list); |
| 57 | static LIST_HEAD(lttng_counter_transport_list); |
| 58 | /* |
| 59 | * Protect the sessions and metadata caches. |
| 60 | */ |
| 61 | static DEFINE_MUTEX(sessions_mutex); |
| 62 | static struct kmem_cache *event_recorder_cache; |
| 63 | static struct kmem_cache *event_recorder_private_cache; |
| 64 | static struct kmem_cache *event_notifier_cache; |
| 65 | static struct kmem_cache *event_notifier_private_cache; |
| 66 | |
| 67 | static void lttng_session_lazy_sync_event_enablers(struct lttng_kernel_session *session); |
| 68 | static void lttng_session_sync_event_enablers(struct lttng_kernel_session *session); |
| 69 | static void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group *event_notifier_group); |
| 70 | static void lttng_event_enabler_sync(struct lttng_event_enabler_common *event_enabler); |
| 71 | |
| 72 | static void _lttng_event_destroy(struct lttng_kernel_event_common *event); |
| 73 | static void _lttng_channel_destroy(struct lttng_kernel_channel_buffer *chan); |
| 74 | static int _lttng_event_unregister(struct lttng_kernel_event_recorder *event); |
| 75 | static int _lttng_event_notifier_unregister(struct lttng_kernel_event_notifier *event_notifier); |
| 76 | static |
| 77 | int _lttng_event_metadata_statedump(struct lttng_kernel_session *session, |
| 78 | struct lttng_kernel_channel_buffer *chan, |
| 79 | struct lttng_kernel_event_recorder *event); |
| 80 | static |
| 81 | int _lttng_session_metadata_statedump(struct lttng_kernel_session *session); |
| 82 | static |
| 83 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream); |
| 84 | static |
| 85 | int _lttng_type_statedump(struct lttng_kernel_session *session, |
| 86 | const struct lttng_kernel_type_common *type, |
| 87 | enum lttng_kernel_string_encoding parent_encoding, |
| 88 | size_t nesting); |
| 89 | static |
| 90 | int _lttng_field_statedump(struct lttng_kernel_session *session, |
| 91 | const struct lttng_kernel_event_field *field, |
| 92 | size_t nesting, const char **prev_field_name_p); |
| 93 | |
| 94 | void synchronize_trace(void) |
| 95 | { |
| 96 | #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,1,0) || \ |
| 97 | LTTNG_RHEL_KERNEL_RANGE(4,18,0,193,0,0, 4,19,0,0,0,0)) |
| 98 | synchronize_rcu(); |
| 99 | #else |
| 100 | synchronize_sched(); |
| 101 | #endif |
| 102 | |
| 103 | #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0)) |
| 104 | #ifdef CONFIG_PREEMPT_RT_FULL |
| 105 | synchronize_rcu(); |
| 106 | #endif |
| 107 | #else /* (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0)) */ |
| 108 | #ifdef CONFIG_PREEMPT_RT |
| 109 | synchronize_rcu(); |
| 110 | #endif |
| 111 | #endif /* (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,4,0)) */ |
| 112 | } |
| 113 | |
| 114 | void lttng_lock_sessions(void) |
| 115 | { |
| 116 | mutex_lock(&sessions_mutex); |
| 117 | } |
| 118 | |
| 119 | void lttng_unlock_sessions(void) |
| 120 | { |
| 121 | mutex_unlock(&sessions_mutex); |
| 122 | } |
| 123 | |
| 124 | static struct lttng_transport *lttng_transport_find(const char *name) |
| 125 | { |
| 126 | struct lttng_transport *transport; |
| 127 | |
| 128 | list_for_each_entry(transport, <tng_transport_list, node) { |
| 129 | if (!strcmp(transport->name, name)) |
| 130 | return transport; |
| 131 | } |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Called with sessions lock held. |
| 137 | */ |
| 138 | int lttng_session_active(void) |
| 139 | { |
| 140 | struct lttng_kernel_session_private *iter; |
| 141 | |
| 142 | list_for_each_entry(iter, &sessions, list) { |
| 143 | if (iter->pub->active) |
| 144 | return 1; |
| 145 | } |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | struct lttng_kernel_session *lttng_session_create(void) |
| 150 | { |
| 151 | struct lttng_kernel_session *session; |
| 152 | struct lttng_kernel_session_private *session_priv; |
| 153 | struct lttng_metadata_cache *metadata_cache; |
| 154 | int i; |
| 155 | |
| 156 | mutex_lock(&sessions_mutex); |
| 157 | session = lttng_kvzalloc(sizeof(*session), GFP_KERNEL); |
| 158 | if (!session) |
| 159 | goto err; |
| 160 | session_priv = lttng_kvzalloc(sizeof(*session_priv), GFP_KERNEL); |
| 161 | if (!session_priv) |
| 162 | goto err_free_session; |
| 163 | session->priv = session_priv; |
| 164 | session_priv->pub = session; |
| 165 | |
| 166 | INIT_LIST_HEAD(&session_priv->chan); |
| 167 | INIT_LIST_HEAD(&session_priv->events); |
| 168 | lttng_guid_gen(&session_priv->uuid); |
| 169 | |
| 170 | metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache), |
| 171 | GFP_KERNEL); |
| 172 | if (!metadata_cache) |
| 173 | goto err_free_session_private; |
| 174 | metadata_cache->data = vzalloc(METADATA_CACHE_DEFAULT_SIZE); |
| 175 | if (!metadata_cache->data) |
| 176 | goto err_free_cache; |
| 177 | metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE; |
| 178 | kref_init(&metadata_cache->refcount); |
| 179 | mutex_init(&metadata_cache->lock); |
| 180 | session_priv->metadata_cache = metadata_cache; |
| 181 | INIT_LIST_HEAD(&metadata_cache->metadata_stream); |
| 182 | memcpy(&metadata_cache->uuid, &session_priv->uuid, |
| 183 | sizeof(metadata_cache->uuid)); |
| 184 | INIT_LIST_HEAD(&session_priv->enablers_head); |
| 185 | for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++) |
| 186 | INIT_HLIST_HEAD(&session_priv->events_ht.table[i]); |
| 187 | list_add(&session_priv->list, &sessions); |
| 188 | |
| 189 | if (lttng_id_tracker_init(&session->pid_tracker, session, TRACKER_PID)) |
| 190 | goto tracker_alloc_error; |
| 191 | if (lttng_id_tracker_init(&session->vpid_tracker, session, TRACKER_VPID)) |
| 192 | goto tracker_alloc_error; |
| 193 | if (lttng_id_tracker_init(&session->uid_tracker, session, TRACKER_UID)) |
| 194 | goto tracker_alloc_error; |
| 195 | if (lttng_id_tracker_init(&session->vuid_tracker, session, TRACKER_VUID)) |
| 196 | goto tracker_alloc_error; |
| 197 | if (lttng_id_tracker_init(&session->gid_tracker, session, TRACKER_GID)) |
| 198 | goto tracker_alloc_error; |
| 199 | if (lttng_id_tracker_init(&session->vgid_tracker, session, TRACKER_VGID)) |
| 200 | goto tracker_alloc_error; |
| 201 | |
| 202 | mutex_unlock(&sessions_mutex); |
| 203 | |
| 204 | return session; |
| 205 | |
| 206 | tracker_alloc_error: |
| 207 | lttng_id_tracker_fini(&session->pid_tracker); |
| 208 | lttng_id_tracker_fini(&session->vpid_tracker); |
| 209 | lttng_id_tracker_fini(&session->uid_tracker); |
| 210 | lttng_id_tracker_fini(&session->vuid_tracker); |
| 211 | lttng_id_tracker_fini(&session->gid_tracker); |
| 212 | lttng_id_tracker_fini(&session->vgid_tracker); |
| 213 | err_free_cache: |
| 214 | kfree(metadata_cache); |
| 215 | err_free_session_private: |
| 216 | lttng_kvfree(session_priv); |
| 217 | err_free_session: |
| 218 | lttng_kvfree(session); |
| 219 | err: |
| 220 | mutex_unlock(&sessions_mutex); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | static |
| 225 | struct lttng_counter_transport *lttng_counter_transport_find(const char *name) |
| 226 | { |
| 227 | struct lttng_counter_transport *transport; |
| 228 | |
| 229 | list_for_each_entry(transport, <tng_counter_transport_list, node) { |
| 230 | if (!strcmp(transport->name, name)) |
| 231 | return transport; |
| 232 | } |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | struct lttng_counter *lttng_kernel_counter_create( |
| 237 | const char *counter_transport_name, |
| 238 | size_t number_dimensions, const size_t *dimensions_sizes) |
| 239 | { |
| 240 | struct lttng_counter *counter = NULL; |
| 241 | struct lttng_counter_transport *counter_transport = NULL; |
| 242 | |
| 243 | counter_transport = lttng_counter_transport_find(counter_transport_name); |
| 244 | if (!counter_transport) { |
| 245 | printk(KERN_WARNING "LTTng: counter transport %s not found.\n", |
| 246 | counter_transport_name); |
| 247 | goto notransport; |
| 248 | } |
| 249 | if (!try_module_get(counter_transport->owner)) { |
| 250 | printk(KERN_WARNING "LTTng: Can't lock counter transport module.\n"); |
| 251 | goto notransport; |
| 252 | } |
| 253 | |
| 254 | counter = lttng_kvzalloc(sizeof(struct lttng_counter), GFP_KERNEL); |
| 255 | if (!counter) |
| 256 | goto nomem; |
| 257 | |
| 258 | /* Create event notifier error counter. */ |
| 259 | counter->ops = &counter_transport->ops; |
| 260 | counter->transport = counter_transport; |
| 261 | |
| 262 | counter->counter = counter->ops->counter_create( |
| 263 | number_dimensions, dimensions_sizes, 0); |
| 264 | if (!counter->counter) { |
| 265 | goto create_error; |
| 266 | } |
| 267 | |
| 268 | return counter; |
| 269 | |
| 270 | create_error: |
| 271 | lttng_kvfree(counter); |
| 272 | nomem: |
| 273 | if (counter_transport) |
| 274 | module_put(counter_transport->owner); |
| 275 | notransport: |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | struct lttng_event_notifier_group *lttng_event_notifier_group_create(void) |
| 280 | { |
| 281 | struct lttng_transport *transport = NULL; |
| 282 | struct lttng_event_notifier_group *event_notifier_group; |
| 283 | const char *transport_name = "relay-event-notifier"; |
| 284 | size_t subbuf_size = 4096; //TODO |
| 285 | size_t num_subbuf = 16; //TODO |
| 286 | unsigned int switch_timer_interval = 0; |
| 287 | unsigned int read_timer_interval = 0; |
| 288 | int i; |
| 289 | |
| 290 | mutex_lock(&sessions_mutex); |
| 291 | |
| 292 | transport = lttng_transport_find(transport_name); |
| 293 | if (!transport) { |
| 294 | printk(KERN_WARNING "LTTng: transport %s not found\n", |
| 295 | transport_name); |
| 296 | goto notransport; |
| 297 | } |
| 298 | if (!try_module_get(transport->owner)) { |
| 299 | printk(KERN_WARNING "LTTng: Can't lock transport %s module.\n", |
| 300 | transport_name); |
| 301 | goto notransport; |
| 302 | } |
| 303 | |
| 304 | event_notifier_group = lttng_kvzalloc(sizeof(struct lttng_event_notifier_group), |
| 305 | GFP_KERNEL); |
| 306 | if (!event_notifier_group) |
| 307 | goto nomem; |
| 308 | |
| 309 | /* |
| 310 | * Initialize the ring buffer used to store event notifier |
| 311 | * notifications. |
| 312 | */ |
| 313 | event_notifier_group->ops = &transport->ops; |
| 314 | event_notifier_group->chan = transport->ops.priv->channel_create( |
| 315 | transport_name, event_notifier_group, NULL, |
| 316 | subbuf_size, num_subbuf, switch_timer_interval, |
| 317 | read_timer_interval); |
| 318 | if (!event_notifier_group->chan) |
| 319 | goto create_error; |
| 320 | |
| 321 | event_notifier_group->transport = transport; |
| 322 | |
| 323 | INIT_LIST_HEAD(&event_notifier_group->enablers_head); |
| 324 | INIT_LIST_HEAD(&event_notifier_group->event_notifiers_head); |
| 325 | for (i = 0; i < LTTNG_EVENT_NOTIFIER_HT_SIZE; i++) |
| 326 | INIT_HLIST_HEAD(&event_notifier_group->event_notifiers_ht.table[i]); |
| 327 | |
| 328 | list_add(&event_notifier_group->node, &event_notifier_groups); |
| 329 | |
| 330 | mutex_unlock(&sessions_mutex); |
| 331 | |
| 332 | return event_notifier_group; |
| 333 | |
| 334 | create_error: |
| 335 | lttng_kvfree(event_notifier_group); |
| 336 | nomem: |
| 337 | if (transport) |
| 338 | module_put(transport->owner); |
| 339 | notransport: |
| 340 | mutex_unlock(&sessions_mutex); |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | void metadata_cache_destroy(struct kref *kref) |
| 345 | { |
| 346 | struct lttng_metadata_cache *cache = |
| 347 | container_of(kref, struct lttng_metadata_cache, refcount); |
| 348 | vfree(cache->data); |
| 349 | kfree(cache); |
| 350 | } |
| 351 | |
| 352 | void lttng_session_destroy(struct lttng_kernel_session *session) |
| 353 | { |
| 354 | struct lttng_kernel_channel_buffer_private *chan_priv, *tmpchan_priv; |
| 355 | struct lttng_kernel_event_recorder_private *event_recorder_priv, *tmpevent_recorder_priv; |
| 356 | struct lttng_metadata_stream *metadata_stream; |
| 357 | struct lttng_event_recorder_enabler *event_recorder_enabler, *tmp_event_recorder_enabler; |
| 358 | int ret; |
| 359 | |
| 360 | mutex_lock(&sessions_mutex); |
| 361 | WRITE_ONCE(session->active, 0); |
| 362 | list_for_each_entry(chan_priv, &session->priv->chan, node) { |
| 363 | ret = lttng_syscalls_unregister_channel(chan_priv->pub); |
| 364 | WARN_ON(ret); |
| 365 | } |
| 366 | list_for_each_entry(event_recorder_priv, &session->priv->events, node) { |
| 367 | ret = _lttng_event_unregister(event_recorder_priv->pub); |
| 368 | WARN_ON(ret); |
| 369 | } |
| 370 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 371 | list_for_each_entry(chan_priv, &session->priv->chan, node) { |
| 372 | ret = lttng_syscalls_destroy_event(chan_priv->pub); |
| 373 | WARN_ON(ret); |
| 374 | } |
| 375 | list_for_each_entry_safe(event_recorder_enabler, tmp_event_recorder_enabler, |
| 376 | &session->priv->enablers_head, node) |
| 377 | lttng_event_enabler_destroy(&event_recorder_enabler->parent); |
| 378 | list_for_each_entry_safe(event_recorder_priv, tmpevent_recorder_priv, &session->priv->events, node) |
| 379 | _lttng_event_destroy(&event_recorder_priv->pub->parent); |
| 380 | list_for_each_entry_safe(chan_priv, tmpchan_priv, &session->priv->chan, node) { |
| 381 | BUG_ON(chan_priv->channel_type == METADATA_CHANNEL); |
| 382 | _lttng_channel_destroy(chan_priv->pub); |
| 383 | } |
| 384 | mutex_lock(&session->priv->metadata_cache->lock); |
| 385 | list_for_each_entry(metadata_stream, &session->priv->metadata_cache->metadata_stream, list) |
| 386 | _lttng_metadata_channel_hangup(metadata_stream); |
| 387 | mutex_unlock(&session->priv->metadata_cache->lock); |
| 388 | lttng_id_tracker_fini(&session->pid_tracker); |
| 389 | lttng_id_tracker_fini(&session->vpid_tracker); |
| 390 | lttng_id_tracker_fini(&session->uid_tracker); |
| 391 | lttng_id_tracker_fini(&session->vuid_tracker); |
| 392 | lttng_id_tracker_fini(&session->gid_tracker); |
| 393 | lttng_id_tracker_fini(&session->vgid_tracker); |
| 394 | kref_put(&session->priv->metadata_cache->refcount, metadata_cache_destroy); |
| 395 | list_del(&session->priv->list); |
| 396 | mutex_unlock(&sessions_mutex); |
| 397 | lttng_kvfree(session->priv); |
| 398 | lttng_kvfree(session); |
| 399 | } |
| 400 | |
| 401 | void lttng_event_notifier_group_destroy( |
| 402 | struct lttng_event_notifier_group *event_notifier_group) |
| 403 | { |
| 404 | struct lttng_event_notifier_enabler *event_notifier_enabler, *tmp_event_notifier_enabler; |
| 405 | struct lttng_kernel_event_notifier_private *event_notifier_priv, *tmpevent_notifier_priv; |
| 406 | int ret; |
| 407 | |
| 408 | if (!event_notifier_group) |
| 409 | return; |
| 410 | |
| 411 | mutex_lock(&sessions_mutex); |
| 412 | |
| 413 | ret = lttng_syscalls_unregister_event_notifier_group(event_notifier_group); |
| 414 | WARN_ON(ret); |
| 415 | |
| 416 | list_for_each_entry_safe(event_notifier_priv, tmpevent_notifier_priv, |
| 417 | &event_notifier_group->event_notifiers_head, node) { |
| 418 | ret = _lttng_event_notifier_unregister(event_notifier_priv->pub); |
| 419 | WARN_ON(ret); |
| 420 | } |
| 421 | |
| 422 | /* Wait for in-flight event notifier to complete */ |
| 423 | synchronize_trace(); |
| 424 | |
| 425 | irq_work_sync(&event_notifier_group->wakeup_pending); |
| 426 | |
| 427 | kfree(event_notifier_group->syscall_table.sc_filter); |
| 428 | |
| 429 | list_for_each_entry_safe(event_notifier_enabler, tmp_event_notifier_enabler, |
| 430 | &event_notifier_group->enablers_head, node) |
| 431 | lttng_event_enabler_destroy(&event_notifier_enabler->parent); |
| 432 | |
| 433 | list_for_each_entry_safe(event_notifier_priv, tmpevent_notifier_priv, |
| 434 | &event_notifier_group->event_notifiers_head, node) |
| 435 | _lttng_event_destroy(&event_notifier_priv->pub->parent); |
| 436 | |
| 437 | if (event_notifier_group->error_counter) { |
| 438 | struct lttng_counter *error_counter = event_notifier_group->error_counter; |
| 439 | |
| 440 | error_counter->ops->counter_destroy(error_counter->counter); |
| 441 | module_put(error_counter->transport->owner); |
| 442 | lttng_kvfree(error_counter); |
| 443 | event_notifier_group->error_counter = NULL; |
| 444 | } |
| 445 | |
| 446 | event_notifier_group->ops->priv->channel_destroy(event_notifier_group->chan); |
| 447 | module_put(event_notifier_group->transport->owner); |
| 448 | list_del(&event_notifier_group->node); |
| 449 | |
| 450 | mutex_unlock(&sessions_mutex); |
| 451 | lttng_kvfree(event_notifier_group); |
| 452 | } |
| 453 | |
| 454 | int lttng_session_statedump(struct lttng_kernel_session *session) |
| 455 | { |
| 456 | int ret; |
| 457 | |
| 458 | mutex_lock(&sessions_mutex); |
| 459 | ret = lttng_statedump_start(session); |
| 460 | mutex_unlock(&sessions_mutex); |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | int lttng_session_enable(struct lttng_kernel_session *session) |
| 465 | { |
| 466 | int ret = 0; |
| 467 | struct lttng_kernel_channel_buffer_private *chan_priv; |
| 468 | |
| 469 | mutex_lock(&sessions_mutex); |
| 470 | if (session->active) { |
| 471 | ret = -EBUSY; |
| 472 | goto end; |
| 473 | } |
| 474 | |
| 475 | /* Set transient enabler state to "enabled" */ |
| 476 | session->priv->tstate = 1; |
| 477 | |
| 478 | /* We need to sync enablers with session before activation. */ |
| 479 | lttng_session_sync_event_enablers(session); |
| 480 | |
| 481 | /* |
| 482 | * Snapshot the number of events per channel to know the type of header |
| 483 | * we need to use. |
| 484 | */ |
| 485 | list_for_each_entry(chan_priv, &session->priv->chan, node) { |
| 486 | if (chan_priv->header_type) |
| 487 | continue; /* don't change it if session stop/restart */ |
| 488 | if (chan_priv->free_event_id < 31) |
| 489 | chan_priv->header_type = 1; /* compact */ |
| 490 | else |
| 491 | chan_priv->header_type = 2; /* large */ |
| 492 | } |
| 493 | |
| 494 | /* Clear each stream's quiescent state. */ |
| 495 | list_for_each_entry(chan_priv, &session->priv->chan, node) { |
| 496 | if (chan_priv->channel_type != METADATA_CHANNEL) |
| 497 | lib_ring_buffer_clear_quiescent_channel(chan_priv->rb_chan); |
| 498 | } |
| 499 | |
| 500 | WRITE_ONCE(session->active, 1); |
| 501 | WRITE_ONCE(session->priv->been_active, 1); |
| 502 | ret = _lttng_session_metadata_statedump(session); |
| 503 | if (ret) { |
| 504 | WRITE_ONCE(session->active, 0); |
| 505 | goto end; |
| 506 | } |
| 507 | ret = lttng_statedump_start(session); |
| 508 | if (ret) |
| 509 | WRITE_ONCE(session->active, 0); |
| 510 | end: |
| 511 | mutex_unlock(&sessions_mutex); |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | int lttng_session_disable(struct lttng_kernel_session *session) |
| 516 | { |
| 517 | int ret = 0; |
| 518 | struct lttng_kernel_channel_buffer_private *chan_priv; |
| 519 | |
| 520 | mutex_lock(&sessions_mutex); |
| 521 | if (!session->active) { |
| 522 | ret = -EBUSY; |
| 523 | goto end; |
| 524 | } |
| 525 | WRITE_ONCE(session->active, 0); |
| 526 | |
| 527 | /* Set transient enabler state to "disabled" */ |
| 528 | session->priv->tstate = 0; |
| 529 | lttng_session_sync_event_enablers(session); |
| 530 | |
| 531 | /* Set each stream's quiescent state. */ |
| 532 | list_for_each_entry(chan_priv, &session->priv->chan, node) { |
| 533 | if (chan_priv->channel_type != METADATA_CHANNEL) |
| 534 | lib_ring_buffer_set_quiescent_channel(chan_priv->rb_chan); |
| 535 | } |
| 536 | end: |
| 537 | mutex_unlock(&sessions_mutex); |
| 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | int lttng_session_metadata_regenerate(struct lttng_kernel_session *session) |
| 542 | { |
| 543 | int ret = 0; |
| 544 | struct lttng_kernel_channel_buffer_private *chan_priv; |
| 545 | struct lttng_kernel_event_recorder_private *event_recorder_priv; |
| 546 | struct lttng_metadata_cache *cache = session->priv->metadata_cache; |
| 547 | struct lttng_metadata_stream *stream; |
| 548 | |
| 549 | mutex_lock(&sessions_mutex); |
| 550 | if (!session->active) { |
| 551 | ret = -EBUSY; |
| 552 | goto end; |
| 553 | } |
| 554 | |
| 555 | mutex_lock(&cache->lock); |
| 556 | memset(cache->data, 0, cache->cache_alloc); |
| 557 | cache->metadata_written = 0; |
| 558 | cache->version++; |
| 559 | list_for_each_entry(stream, &session->priv->metadata_cache->metadata_stream, list) { |
| 560 | stream->metadata_out = 0; |
| 561 | stream->metadata_in = 0; |
| 562 | } |
| 563 | mutex_unlock(&cache->lock); |
| 564 | |
| 565 | session->priv->metadata_dumped = 0; |
| 566 | list_for_each_entry(chan_priv, &session->priv->chan, node) { |
| 567 | chan_priv->metadata_dumped = 0; |
| 568 | } |
| 569 | |
| 570 | list_for_each_entry(event_recorder_priv, &session->priv->events, node) { |
| 571 | event_recorder_priv->metadata_dumped = 0; |
| 572 | } |
| 573 | |
| 574 | ret = _lttng_session_metadata_statedump(session); |
| 575 | |
| 576 | end: |
| 577 | mutex_unlock(&sessions_mutex); |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | int lttng_channel_enable(struct lttng_kernel_channel_buffer *channel) |
| 582 | { |
| 583 | int ret = 0; |
| 584 | |
| 585 | mutex_lock(&sessions_mutex); |
| 586 | if (channel->priv->channel_type == METADATA_CHANNEL) { |
| 587 | ret = -EPERM; |
| 588 | goto end; |
| 589 | } |
| 590 | if (channel->parent.enabled) { |
| 591 | ret = -EEXIST; |
| 592 | goto end; |
| 593 | } |
| 594 | /* Set transient enabler state to "enabled" */ |
| 595 | channel->priv->parent.tstate = 1; |
| 596 | lttng_session_sync_event_enablers(channel->parent.session); |
| 597 | /* Set atomically the state to "enabled" */ |
| 598 | WRITE_ONCE(channel->parent.enabled, 1); |
| 599 | end: |
| 600 | mutex_unlock(&sessions_mutex); |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | int lttng_channel_disable(struct lttng_kernel_channel_buffer *channel) |
| 605 | { |
| 606 | int ret = 0; |
| 607 | |
| 608 | mutex_lock(&sessions_mutex); |
| 609 | if (channel->priv->channel_type == METADATA_CHANNEL) { |
| 610 | ret = -EPERM; |
| 611 | goto end; |
| 612 | } |
| 613 | if (!channel->parent.enabled) { |
| 614 | ret = -EEXIST; |
| 615 | goto end; |
| 616 | } |
| 617 | /* Set atomically the state to "disabled" */ |
| 618 | WRITE_ONCE(channel->parent.enabled, 0); |
| 619 | /* Set transient enabler state to "enabled" */ |
| 620 | channel->priv->parent.tstate = 0; |
| 621 | lttng_session_sync_event_enablers(channel->parent.session); |
| 622 | end: |
| 623 | mutex_unlock(&sessions_mutex); |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | int lttng_event_enable(struct lttng_kernel_event_common *event) |
| 628 | { |
| 629 | int ret = 0; |
| 630 | |
| 631 | mutex_lock(&sessions_mutex); |
| 632 | switch (event->type) { |
| 633 | case LTTNG_KERNEL_EVENT_TYPE_RECORDER: |
| 634 | { |
| 635 | struct lttng_kernel_event_recorder *event_recorder = |
| 636 | container_of(event, struct lttng_kernel_event_recorder, parent); |
| 637 | |
| 638 | if (event_recorder->chan->priv->channel_type == METADATA_CHANNEL) { |
| 639 | ret = -EPERM; |
| 640 | goto end; |
| 641 | } |
| 642 | break; |
| 643 | } |
| 644 | case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER: |
| 645 | switch (event->priv->instrumentation) { |
| 646 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 647 | ret = -EINVAL; |
| 648 | goto end; |
| 649 | default: |
| 650 | break; |
| 651 | } |
| 652 | break; |
| 653 | default: |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | if (event->enabled) { |
| 658 | ret = -EEXIST; |
| 659 | goto end; |
| 660 | } |
| 661 | switch (event->priv->instrumentation) { |
| 662 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 663 | lttng_fallthrough; |
| 664 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 665 | ret = -EINVAL; |
| 666 | break; |
| 667 | |
| 668 | case LTTNG_KERNEL_ABI_KPROBE: |
| 669 | lttng_fallthrough; |
| 670 | case LTTNG_KERNEL_ABI_UPROBE: |
| 671 | WRITE_ONCE(event->enabled, 1); |
| 672 | break; |
| 673 | |
| 674 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 675 | ret = lttng_kretprobes_event_enable_state(event, 1); |
| 676 | break; |
| 677 | |
| 678 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 679 | lttng_fallthrough; |
| 680 | case LTTNG_KERNEL_ABI_NOOP: |
| 681 | lttng_fallthrough; |
| 682 | default: |
| 683 | WARN_ON_ONCE(1); |
| 684 | ret = -EINVAL; |
| 685 | } |
| 686 | end: |
| 687 | mutex_unlock(&sessions_mutex); |
| 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | int lttng_event_disable(struct lttng_kernel_event_common *event) |
| 692 | { |
| 693 | int ret = 0; |
| 694 | |
| 695 | mutex_lock(&sessions_mutex); |
| 696 | switch (event->type) { |
| 697 | case LTTNG_KERNEL_EVENT_TYPE_RECORDER: |
| 698 | { |
| 699 | struct lttng_kernel_event_recorder *event_recorder = |
| 700 | container_of(event, struct lttng_kernel_event_recorder, parent); |
| 701 | |
| 702 | if (event_recorder->chan->priv->channel_type == METADATA_CHANNEL) { |
| 703 | ret = -EPERM; |
| 704 | goto end; |
| 705 | } |
| 706 | break; |
| 707 | } |
| 708 | case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER: |
| 709 | switch (event->priv->instrumentation) { |
| 710 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 711 | ret = -EINVAL; |
| 712 | goto end; |
| 713 | default: |
| 714 | break; |
| 715 | } |
| 716 | break; |
| 717 | default: |
| 718 | break; |
| 719 | } |
| 720 | |
| 721 | if (!event->enabled) { |
| 722 | ret = -EEXIST; |
| 723 | goto end; |
| 724 | } |
| 725 | switch (event->priv->instrumentation) { |
| 726 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 727 | lttng_fallthrough; |
| 728 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 729 | ret = -EINVAL; |
| 730 | break; |
| 731 | |
| 732 | case LTTNG_KERNEL_ABI_KPROBE: |
| 733 | lttng_fallthrough; |
| 734 | case LTTNG_KERNEL_ABI_UPROBE: |
| 735 | WRITE_ONCE(event->enabled, 0); |
| 736 | break; |
| 737 | |
| 738 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 739 | ret = lttng_kretprobes_event_enable_state(event, 0); |
| 740 | break; |
| 741 | |
| 742 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 743 | lttng_fallthrough; |
| 744 | case LTTNG_KERNEL_ABI_NOOP: |
| 745 | lttng_fallthrough; |
| 746 | default: |
| 747 | WARN_ON_ONCE(1); |
| 748 | ret = -EINVAL; |
| 749 | } |
| 750 | end: |
| 751 | mutex_unlock(&sessions_mutex); |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | struct lttng_kernel_channel_buffer *lttng_channel_create(struct lttng_kernel_session *session, |
| 756 | const char *transport_name, |
| 757 | void *buf_addr, |
| 758 | size_t subbuf_size, size_t num_subbuf, |
| 759 | unsigned int switch_timer_interval, |
| 760 | unsigned int read_timer_interval, |
| 761 | enum channel_type channel_type) |
| 762 | { |
| 763 | struct lttng_kernel_channel_buffer *chan; |
| 764 | struct lttng_kernel_channel_buffer_private *chan_priv; |
| 765 | struct lttng_transport *transport = NULL; |
| 766 | |
| 767 | mutex_lock(&sessions_mutex); |
| 768 | if (session->priv->been_active && channel_type != METADATA_CHANNEL) |
| 769 | goto active; /* Refuse to add channel to active session */ |
| 770 | transport = lttng_transport_find(transport_name); |
| 771 | if (!transport) { |
| 772 | printk(KERN_WARNING "LTTng: transport %s not found\n", |
| 773 | transport_name); |
| 774 | goto notransport; |
| 775 | } |
| 776 | if (!try_module_get(transport->owner)) { |
| 777 | printk(KERN_WARNING "LTTng: Can't lock transport module.\n"); |
| 778 | goto notransport; |
| 779 | } |
| 780 | chan = kzalloc(sizeof(struct lttng_kernel_channel_buffer), GFP_KERNEL); |
| 781 | if (!chan) |
| 782 | goto nomem; |
| 783 | chan_priv = kzalloc(sizeof(struct lttng_kernel_channel_buffer_private), GFP_KERNEL); |
| 784 | if (!chan_priv) |
| 785 | goto nomem_priv; |
| 786 | chan->priv = chan_priv; |
| 787 | chan_priv->pub = chan; |
| 788 | chan->parent.type = LTTNG_KERNEL_CHANNEL_TYPE_BUFFER; |
| 789 | chan->parent.session = session; |
| 790 | chan->priv->id = session->priv->free_chan_id++; |
| 791 | chan->ops = &transport->ops; |
| 792 | /* |
| 793 | * Note: the channel creation op already writes into the packet |
| 794 | * headers. Therefore the "chan" information used as input |
| 795 | * should be already accessible. |
| 796 | */ |
| 797 | chan->priv->rb_chan = transport->ops.priv->channel_create(transport_name, |
| 798 | chan, buf_addr, subbuf_size, num_subbuf, |
| 799 | switch_timer_interval, read_timer_interval); |
| 800 | if (!chan->priv->rb_chan) |
| 801 | goto create_error; |
| 802 | chan->priv->parent.tstate = 1; |
| 803 | chan->parent.enabled = 1; |
| 804 | chan->priv->transport = transport; |
| 805 | chan->priv->channel_type = channel_type; |
| 806 | list_add(&chan->priv->node, &session->priv->chan); |
| 807 | mutex_unlock(&sessions_mutex); |
| 808 | return chan; |
| 809 | |
| 810 | create_error: |
| 811 | kfree(chan_priv); |
| 812 | nomem_priv: |
| 813 | kfree(chan); |
| 814 | nomem: |
| 815 | if (transport) |
| 816 | module_put(transport->owner); |
| 817 | notransport: |
| 818 | active: |
| 819 | mutex_unlock(&sessions_mutex); |
| 820 | return NULL; |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * Only used internally at session destruction for per-cpu channels, and |
| 825 | * when metadata channel is released. |
| 826 | * Needs to be called with sessions mutex held. |
| 827 | */ |
| 828 | static |
| 829 | void _lttng_channel_destroy(struct lttng_kernel_channel_buffer *chan) |
| 830 | { |
| 831 | chan->ops->priv->channel_destroy(chan->priv->rb_chan); |
| 832 | module_put(chan->priv->transport->owner); |
| 833 | list_del(&chan->priv->node); |
| 834 | lttng_kernel_destroy_context(chan->priv->ctx); |
| 835 | kfree(chan->priv); |
| 836 | kfree(chan); |
| 837 | } |
| 838 | |
| 839 | void lttng_metadata_channel_destroy(struct lttng_kernel_channel_buffer *chan) |
| 840 | { |
| 841 | BUG_ON(chan->priv->channel_type != METADATA_CHANNEL); |
| 842 | |
| 843 | /* Protect the metadata cache with the sessions_mutex. */ |
| 844 | mutex_lock(&sessions_mutex); |
| 845 | _lttng_channel_destroy(chan); |
| 846 | mutex_unlock(&sessions_mutex); |
| 847 | } |
| 848 | EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy); |
| 849 | |
| 850 | static |
| 851 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream) |
| 852 | { |
| 853 | stream->finalized = 1; |
| 854 | wake_up_interruptible(&stream->read_wait); |
| 855 | } |
| 856 | |
| 857 | |
| 858 | /* |
| 859 | * Supports event creation while tracing session is active. |
| 860 | * Needs to be called with sessions mutex held. |
| 861 | */ |
| 862 | struct lttng_kernel_event_recorder *_lttng_kernel_event_recorder_create(struct lttng_event_recorder_enabler *event_enabler, |
| 863 | const struct lttng_kernel_event_desc *event_desc) |
| 864 | { |
| 865 | struct lttng_kernel_channel_buffer *chan = event_enabler->chan; |
| 866 | struct lttng_kernel_abi_event *event_param = &event_enabler->parent.event_param; |
| 867 | enum lttng_kernel_abi_instrumentation itype = event_param->instrumentation; |
| 868 | struct lttng_kernel_session *session = chan->parent.session; |
| 869 | struct lttng_kernel_event_recorder *event_recorder; |
| 870 | struct lttng_kernel_event_recorder_private *event_recorder_priv; |
| 871 | const char *event_name; |
| 872 | struct hlist_head *head; |
| 873 | int ret; |
| 874 | |
| 875 | if (chan->priv->free_event_id == -1U) { |
| 876 | ret = -EMFILE; |
| 877 | goto full; |
| 878 | } |
| 879 | |
| 880 | switch (itype) { |
| 881 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 882 | event_name = event_desc->event_name; |
| 883 | break; |
| 884 | |
| 885 | case LTTNG_KERNEL_ABI_KPROBE: |
| 886 | lttng_fallthrough; |
| 887 | case LTTNG_KERNEL_ABI_UPROBE: |
| 888 | lttng_fallthrough; |
| 889 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 890 | lttng_fallthrough; |
| 891 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 892 | event_name = event_param->name; |
| 893 | break; |
| 894 | |
| 895 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 896 | lttng_fallthrough; |
| 897 | case LTTNG_KERNEL_ABI_NOOP: |
| 898 | lttng_fallthrough; |
| 899 | default: |
| 900 | WARN_ON_ONCE(1); |
| 901 | ret = -EINVAL; |
| 902 | goto type_error; |
| 903 | } |
| 904 | |
| 905 | head = utils_borrow_hash_table_bucket(session->priv->events_ht.table, |
| 906 | LTTNG_EVENT_HT_SIZE, event_name); |
| 907 | lttng_hlist_for_each_entry(event_recorder_priv, head, hlist) { |
| 908 | WARN_ON_ONCE(!event_recorder_priv->parent.desc); |
| 909 | if (!strncmp(event_recorder_priv->parent.desc->event_name, event_name, |
| 910 | LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1) |
| 911 | && chan == event_recorder_priv->pub->chan) { |
| 912 | ret = -EEXIST; |
| 913 | goto exist; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | event_recorder = kmem_cache_zalloc(event_recorder_cache, GFP_KERNEL); |
| 918 | if (!event_recorder) { |
| 919 | ret = -ENOMEM; |
| 920 | goto cache_error; |
| 921 | } |
| 922 | event_recorder_priv = kmem_cache_zalloc(event_recorder_private_cache, GFP_KERNEL); |
| 923 | if (!event_recorder_priv) { |
| 924 | ret = -ENOMEM; |
| 925 | goto cache_private_error; |
| 926 | } |
| 927 | event_recorder_priv->pub = event_recorder; |
| 928 | event_recorder_priv->parent.pub = &event_recorder->parent; |
| 929 | event_recorder->priv = event_recorder_priv; |
| 930 | event_recorder->parent.priv = &event_recorder_priv->parent; |
| 931 | event_recorder->parent.type = LTTNG_KERNEL_EVENT_TYPE_RECORDER; |
| 932 | |
| 933 | event_recorder->parent.run_filter = lttng_kernel_interpret_event_filter; |
| 934 | event_recorder->chan = chan; |
| 935 | event_recorder->priv->id = chan->priv->free_event_id++; |
| 936 | event_recorder->priv->parent.instrumentation = itype; |
| 937 | INIT_LIST_HEAD(&event_recorder->priv->parent.filter_bytecode_runtime_head); |
| 938 | INIT_LIST_HEAD(&event_recorder->priv->parent.enablers_ref_head); |
| 939 | |
| 940 | switch (itype) { |
| 941 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 942 | /* Event will be enabled by enabler sync. */ |
| 943 | event_recorder->parent.enabled = 0; |
| 944 | event_recorder->priv->parent.registered = 0; |
| 945 | event_recorder->priv->parent.desc = lttng_event_desc_get(event_name); |
| 946 | if (!event_recorder->priv->parent.desc) { |
| 947 | ret = -ENOENT; |
| 948 | goto register_error; |
| 949 | } |
| 950 | /* Populate lttng_event structure before event registration. */ |
| 951 | smp_wmb(); |
| 952 | break; |
| 953 | |
| 954 | case LTTNG_KERNEL_ABI_KPROBE: |
| 955 | /* |
| 956 | * Needs to be explicitly enabled after creation, since |
| 957 | * we may want to apply filters. |
| 958 | */ |
| 959 | event_recorder->parent.enabled = 0; |
| 960 | event_recorder->priv->parent.registered = 1; |
| 961 | /* |
| 962 | * Populate lttng_event structure before event |
| 963 | * registration. |
| 964 | */ |
| 965 | smp_wmb(); |
| 966 | ret = lttng_kprobes_register_event(event_name, |
| 967 | event_param->u.kprobe.symbol_name, |
| 968 | event_param->u.kprobe.offset, |
| 969 | event_param->u.kprobe.addr, |
| 970 | event_recorder); |
| 971 | if (ret) { |
| 972 | ret = -EINVAL; |
| 973 | goto register_error; |
| 974 | } |
| 975 | ret = try_module_get(event_recorder->priv->parent.desc->owner); |
| 976 | WARN_ON_ONCE(!ret); |
| 977 | break; |
| 978 | |
| 979 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 980 | { |
| 981 | struct lttng_kernel_event_recorder *event_recorder_return; |
| 982 | struct lttng_kernel_event_recorder_private *event_recorder_return_priv; |
| 983 | |
| 984 | /* kretprobe defines 2 events */ |
| 985 | /* |
| 986 | * Needs to be explicitly enabled after creation, since |
| 987 | * we may want to apply filters. |
| 988 | */ |
| 989 | event_recorder->parent.enabled = 0; |
| 990 | event_recorder->priv->parent.registered = 1; |
| 991 | |
| 992 | event_recorder_return = kmem_cache_zalloc(event_recorder_cache, GFP_KERNEL); |
| 993 | if (!event_recorder_return) { |
| 994 | ret = -ENOMEM; |
| 995 | goto register_error; |
| 996 | } |
| 997 | event_recorder_return_priv = kmem_cache_zalloc(event_recorder_private_cache, GFP_KERNEL); |
| 998 | if (!event_recorder_return_priv) { |
| 999 | kmem_cache_free(event_recorder_cache, event_recorder_return); |
| 1000 | ret = -ENOMEM; |
| 1001 | goto register_error; |
| 1002 | } |
| 1003 | event_recorder_return_priv->pub = event_recorder_return; |
| 1004 | event_recorder_return_priv->parent.pub = &event_recorder_return->parent; |
| 1005 | event_recorder_return->priv = event_recorder_return_priv; |
| 1006 | event_recorder_return->parent.priv = &event_recorder_return_priv->parent; |
| 1007 | event_recorder_return->parent.type = LTTNG_KERNEL_EVENT_TYPE_RECORDER; |
| 1008 | |
| 1009 | event_recorder_return->parent.run_filter = lttng_kernel_interpret_event_filter; |
| 1010 | event_recorder_return->chan = chan; |
| 1011 | event_recorder_return->priv->id = chan->priv->free_event_id++; |
| 1012 | event_recorder_return->priv->parent.instrumentation = itype; |
| 1013 | event_recorder_return->parent.enabled = 0; |
| 1014 | event_recorder_return->priv->parent.registered = 1; |
| 1015 | INIT_LIST_HEAD(&event_recorder_return->priv->parent.filter_bytecode_runtime_head); |
| 1016 | INIT_LIST_HEAD(&event_recorder_return->priv->parent.enablers_ref_head); |
| 1017 | /* |
| 1018 | * Populate lttng_event structure before kretprobe registration. |
| 1019 | */ |
| 1020 | smp_wmb(); |
| 1021 | ret = lttng_kretprobes_register(event_name, |
| 1022 | event_param->u.kretprobe.symbol_name, |
| 1023 | event_param->u.kretprobe.offset, |
| 1024 | event_param->u.kretprobe.addr, |
| 1025 | event_recorder, event_recorder_return); |
| 1026 | if (ret) { |
| 1027 | kmem_cache_free(event_recorder_private_cache, event_recorder_return_priv); |
| 1028 | kmem_cache_free(event_recorder_cache, event_recorder_return); |
| 1029 | ret = -EINVAL; |
| 1030 | goto register_error; |
| 1031 | } |
| 1032 | /* Take 2 refs on the module: one per event. */ |
| 1033 | ret = try_module_get(event_recorder->priv->parent.desc->owner); |
| 1034 | WARN_ON_ONCE(!ret); |
| 1035 | ret = try_module_get(event_recorder_return->priv->parent.desc->owner); |
| 1036 | WARN_ON_ONCE(!ret); |
| 1037 | ret = _lttng_event_metadata_statedump(chan->parent.session, chan, |
| 1038 | event_recorder_return); |
| 1039 | WARN_ON_ONCE(ret > 0); |
| 1040 | if (ret) { |
| 1041 | kmem_cache_free(event_recorder_private_cache, event_recorder_return_priv); |
| 1042 | kmem_cache_free(event_recorder_cache, event_recorder_return); |
| 1043 | module_put(event_recorder_return->priv->parent.desc->owner); |
| 1044 | module_put(event_recorder->priv->parent.desc->owner); |
| 1045 | goto statedump_error; |
| 1046 | } |
| 1047 | list_add(&event_recorder_return->priv->node, &chan->parent.session->priv->events); |
| 1048 | break; |
| 1049 | } |
| 1050 | |
| 1051 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 1052 | /* |
| 1053 | * Needs to be explicitly enabled after creation, since |
| 1054 | * we may want to apply filters. |
| 1055 | */ |
| 1056 | event_recorder->parent.enabled = 0; |
| 1057 | event_recorder->priv->parent.registered = 0; |
| 1058 | event_recorder->priv->parent.desc = event_desc; |
| 1059 | switch (event_param->u.syscall.entryexit) { |
| 1060 | case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT: |
| 1061 | ret = -EINVAL; |
| 1062 | goto register_error; |
| 1063 | case LTTNG_KERNEL_ABI_SYSCALL_ENTRY: |
| 1064 | event_recorder->priv->parent.u.syscall.entryexit = LTTNG_SYSCALL_ENTRY; |
| 1065 | break; |
| 1066 | case LTTNG_KERNEL_ABI_SYSCALL_EXIT: |
| 1067 | event_recorder->priv->parent.u.syscall.entryexit = LTTNG_SYSCALL_EXIT; |
| 1068 | break; |
| 1069 | } |
| 1070 | switch (event_param->u.syscall.abi) { |
| 1071 | case LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL: |
| 1072 | ret = -EINVAL; |
| 1073 | goto register_error; |
| 1074 | case LTTNG_KERNEL_ABI_SYSCALL_ABI_NATIVE: |
| 1075 | event_recorder->priv->parent.u.syscall.abi = LTTNG_SYSCALL_ABI_NATIVE; |
| 1076 | break; |
| 1077 | case LTTNG_KERNEL_ABI_SYSCALL_ABI_COMPAT: |
| 1078 | event_recorder->priv->parent.u.syscall.abi = LTTNG_SYSCALL_ABI_COMPAT; |
| 1079 | break; |
| 1080 | } |
| 1081 | if (!event_recorder->priv->parent.desc) { |
| 1082 | ret = -EINVAL; |
| 1083 | goto register_error; |
| 1084 | } |
| 1085 | break; |
| 1086 | |
| 1087 | case LTTNG_KERNEL_ABI_UPROBE: |
| 1088 | /* |
| 1089 | * Needs to be explicitly enabled after creation, since |
| 1090 | * we may want to apply filters. |
| 1091 | */ |
| 1092 | event_recorder->parent.enabled = 0; |
| 1093 | event_recorder->priv->parent.registered = 1; |
| 1094 | |
| 1095 | /* |
| 1096 | * Populate lttng_event structure before event |
| 1097 | * registration. |
| 1098 | */ |
| 1099 | smp_wmb(); |
| 1100 | |
| 1101 | ret = lttng_uprobes_register_event(event_param->name, |
| 1102 | event_param->u.uprobe.fd, |
| 1103 | event_recorder); |
| 1104 | if (ret) |
| 1105 | goto register_error; |
| 1106 | ret = try_module_get(event_recorder->priv->parent.desc->owner); |
| 1107 | WARN_ON_ONCE(!ret); |
| 1108 | break; |
| 1109 | |
| 1110 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 1111 | lttng_fallthrough; |
| 1112 | case LTTNG_KERNEL_ABI_NOOP: |
| 1113 | lttng_fallthrough; |
| 1114 | default: |
| 1115 | WARN_ON_ONCE(1); |
| 1116 | ret = -EINVAL; |
| 1117 | goto register_error; |
| 1118 | } |
| 1119 | ret = _lttng_event_metadata_statedump(chan->parent.session, chan, event_recorder); |
| 1120 | WARN_ON_ONCE(ret > 0); |
| 1121 | if (ret) { |
| 1122 | goto statedump_error; |
| 1123 | } |
| 1124 | hlist_add_head(&event_recorder->priv->hlist, head); |
| 1125 | list_add(&event_recorder->priv->node, &chan->parent.session->priv->events); |
| 1126 | return event_recorder; |
| 1127 | |
| 1128 | statedump_error: |
| 1129 | /* If a statedump error occurs, events will not be readable. */ |
| 1130 | register_error: |
| 1131 | kmem_cache_free(event_recorder_private_cache, event_recorder_priv); |
| 1132 | cache_private_error: |
| 1133 | kmem_cache_free(event_recorder_cache, event_recorder); |
| 1134 | cache_error: |
| 1135 | exist: |
| 1136 | type_error: |
| 1137 | full: |
| 1138 | return ERR_PTR(ret); |
| 1139 | } |
| 1140 | |
| 1141 | struct lttng_kernel_event_notifier *_lttng_event_notifier_create( |
| 1142 | const struct lttng_kernel_event_desc *event_desc, |
| 1143 | uint64_t token, uint64_t error_counter_index, |
| 1144 | struct lttng_event_notifier_group *event_notifier_group, |
| 1145 | struct lttng_kernel_abi_event_notifier *event_notifier_param, |
| 1146 | enum lttng_kernel_abi_instrumentation itype) |
| 1147 | { |
| 1148 | struct lttng_kernel_event_notifier *event_notifier; |
| 1149 | struct lttng_kernel_event_notifier_private *event_notifier_priv; |
| 1150 | struct lttng_counter *error_counter; |
| 1151 | const char *event_name; |
| 1152 | struct hlist_head *head; |
| 1153 | int ret; |
| 1154 | |
| 1155 | switch (itype) { |
| 1156 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 1157 | event_name = event_desc->event_name; |
| 1158 | break; |
| 1159 | |
| 1160 | case LTTNG_KERNEL_ABI_KPROBE: |
| 1161 | lttng_fallthrough; |
| 1162 | case LTTNG_KERNEL_ABI_UPROBE: |
| 1163 | lttng_fallthrough; |
| 1164 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 1165 | event_name = event_notifier_param->event.name; |
| 1166 | break; |
| 1167 | |
| 1168 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 1169 | lttng_fallthrough; |
| 1170 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 1171 | lttng_fallthrough; |
| 1172 | case LTTNG_KERNEL_ABI_NOOP: |
| 1173 | lttng_fallthrough; |
| 1174 | default: |
| 1175 | WARN_ON_ONCE(1); |
| 1176 | ret = -EINVAL; |
| 1177 | goto type_error; |
| 1178 | } |
| 1179 | |
| 1180 | head = utils_borrow_hash_table_bucket(event_notifier_group->event_notifiers_ht.table, |
| 1181 | LTTNG_EVENT_NOTIFIER_HT_SIZE, event_name); |
| 1182 | lttng_hlist_for_each_entry(event_notifier_priv, head, hlist) { |
| 1183 | WARN_ON_ONCE(!event_notifier_priv->parent.desc); |
| 1184 | if (!strncmp(event_notifier_priv->parent.desc->event_name, event_name, |
| 1185 | LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1) |
| 1186 | && event_notifier_group == event_notifier_priv->group |
| 1187 | && token == event_notifier_priv->parent.user_token) { |
| 1188 | ret = -EEXIST; |
| 1189 | goto exist; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | event_notifier = kmem_cache_zalloc(event_notifier_cache, GFP_KERNEL); |
| 1194 | if (!event_notifier) { |
| 1195 | ret = -ENOMEM; |
| 1196 | goto cache_error; |
| 1197 | } |
| 1198 | event_notifier_priv = kmem_cache_zalloc(event_notifier_private_cache, GFP_KERNEL); |
| 1199 | if (!event_notifier_priv) { |
| 1200 | ret = -ENOMEM; |
| 1201 | goto cache_private_error; |
| 1202 | } |
| 1203 | event_notifier_priv->pub = event_notifier; |
| 1204 | event_notifier_priv->parent.pub = &event_notifier->parent; |
| 1205 | event_notifier->priv = event_notifier_priv; |
| 1206 | event_notifier->parent.priv = &event_notifier_priv->parent; |
| 1207 | event_notifier->parent.type = LTTNG_KERNEL_EVENT_TYPE_NOTIFIER; |
| 1208 | |
| 1209 | event_notifier->priv->group = event_notifier_group; |
| 1210 | event_notifier->priv->parent.user_token = token; |
| 1211 | event_notifier->priv->error_counter_index = error_counter_index; |
| 1212 | event_notifier->priv->num_captures = 0; |
| 1213 | event_notifier->priv->parent.instrumentation = itype; |
| 1214 | event_notifier->notification_send = lttng_event_notifier_notification_send; |
| 1215 | INIT_LIST_HEAD(&event_notifier->priv->parent.filter_bytecode_runtime_head); |
| 1216 | INIT_LIST_HEAD(&event_notifier->priv->parent.enablers_ref_head); |
| 1217 | INIT_LIST_HEAD(&event_notifier->priv->capture_bytecode_runtime_head); |
| 1218 | event_notifier->parent.run_filter = lttng_kernel_interpret_event_filter; |
| 1219 | |
| 1220 | switch (itype) { |
| 1221 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 1222 | /* Event will be enabled by enabler sync. */ |
| 1223 | event_notifier->parent.enabled = 0; |
| 1224 | event_notifier->priv->parent.registered = 0; |
| 1225 | event_notifier->priv->parent.desc = lttng_event_desc_get(event_name); |
| 1226 | if (!event_notifier->priv->parent.desc) { |
| 1227 | ret = -ENOENT; |
| 1228 | goto register_error; |
| 1229 | } |
| 1230 | /* Populate lttng_event_notifier structure before event registration. */ |
| 1231 | smp_wmb(); |
| 1232 | break; |
| 1233 | |
| 1234 | case LTTNG_KERNEL_ABI_KPROBE: |
| 1235 | /* |
| 1236 | * Needs to be explicitly enabled after creation, since |
| 1237 | * we may want to apply filters. |
| 1238 | */ |
| 1239 | event_notifier->parent.enabled = 0; |
| 1240 | event_notifier->priv->parent.registered = 1; |
| 1241 | /* |
| 1242 | * Populate lttng_event_notifier structure before event |
| 1243 | * registration. |
| 1244 | */ |
| 1245 | smp_wmb(); |
| 1246 | ret = lttng_kprobes_register_event_notifier( |
| 1247 | event_notifier_param->event.u.kprobe.symbol_name, |
| 1248 | event_notifier_param->event.u.kprobe.offset, |
| 1249 | event_notifier_param->event.u.kprobe.addr, |
| 1250 | event_notifier); |
| 1251 | if (ret) { |
| 1252 | ret = -EINVAL; |
| 1253 | goto register_error; |
| 1254 | } |
| 1255 | ret = try_module_get(event_notifier->priv->parent.desc->owner); |
| 1256 | WARN_ON_ONCE(!ret); |
| 1257 | break; |
| 1258 | |
| 1259 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 1260 | /* |
| 1261 | * Needs to be explicitly enabled after creation, since |
| 1262 | * we may want to apply filters. |
| 1263 | */ |
| 1264 | event_notifier->parent.enabled = 0; |
| 1265 | event_notifier->priv->parent.registered = 0; |
| 1266 | event_notifier->priv->parent.desc = event_desc; |
| 1267 | switch (event_notifier_param->event.u.syscall.entryexit) { |
| 1268 | case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT: |
| 1269 | ret = -EINVAL; |
| 1270 | goto register_error; |
| 1271 | case LTTNG_KERNEL_ABI_SYSCALL_ENTRY: |
| 1272 | event_notifier->priv->parent.u.syscall.entryexit = LTTNG_SYSCALL_ENTRY; |
| 1273 | break; |
| 1274 | case LTTNG_KERNEL_ABI_SYSCALL_EXIT: |
| 1275 | event_notifier->priv->parent.u.syscall.entryexit = LTTNG_SYSCALL_EXIT; |
| 1276 | break; |
| 1277 | } |
| 1278 | switch (event_notifier_param->event.u.syscall.abi) { |
| 1279 | case LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL: |
| 1280 | ret = -EINVAL; |
| 1281 | goto register_error; |
| 1282 | case LTTNG_KERNEL_ABI_SYSCALL_ABI_NATIVE: |
| 1283 | event_notifier->priv->parent.u.syscall.abi = LTTNG_SYSCALL_ABI_NATIVE; |
| 1284 | break; |
| 1285 | case LTTNG_KERNEL_ABI_SYSCALL_ABI_COMPAT: |
| 1286 | event_notifier->priv->parent.u.syscall.abi = LTTNG_SYSCALL_ABI_COMPAT; |
| 1287 | break; |
| 1288 | } |
| 1289 | |
| 1290 | if (!event_notifier->priv->parent.desc) { |
| 1291 | ret = -EINVAL; |
| 1292 | goto register_error; |
| 1293 | } |
| 1294 | break; |
| 1295 | |
| 1296 | case LTTNG_KERNEL_ABI_UPROBE: |
| 1297 | /* |
| 1298 | * Needs to be explicitly enabled after creation, since |
| 1299 | * we may want to apply filters. |
| 1300 | */ |
| 1301 | event_notifier->parent.enabled = 0; |
| 1302 | event_notifier->priv->parent.registered = 1; |
| 1303 | |
| 1304 | /* |
| 1305 | * Populate lttng_event_notifier structure before |
| 1306 | * event_notifier registration. |
| 1307 | */ |
| 1308 | smp_wmb(); |
| 1309 | |
| 1310 | ret = lttng_uprobes_register_event_notifier( |
| 1311 | event_notifier_param->event.name, |
| 1312 | event_notifier_param->event.u.uprobe.fd, |
| 1313 | event_notifier); |
| 1314 | if (ret) |
| 1315 | goto register_error; |
| 1316 | ret = try_module_get(event_notifier->priv->parent.desc->owner); |
| 1317 | WARN_ON_ONCE(!ret); |
| 1318 | break; |
| 1319 | |
| 1320 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 1321 | lttng_fallthrough; |
| 1322 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 1323 | lttng_fallthrough; |
| 1324 | case LTTNG_KERNEL_ABI_NOOP: |
| 1325 | lttng_fallthrough; |
| 1326 | default: |
| 1327 | WARN_ON_ONCE(1); |
| 1328 | ret = -EINVAL; |
| 1329 | goto register_error; |
| 1330 | } |
| 1331 | |
| 1332 | list_add(&event_notifier->priv->node, &event_notifier_group->event_notifiers_head); |
| 1333 | hlist_add_head(&event_notifier->priv->hlist, head); |
| 1334 | |
| 1335 | /* |
| 1336 | * Clear the error counter bucket. The sessiond keeps track of which |
| 1337 | * bucket is currently in use. We trust it. The session lock |
| 1338 | * synchronizes against concurrent creation of the error |
| 1339 | * counter. |
| 1340 | */ |
| 1341 | error_counter = event_notifier_group->error_counter; |
| 1342 | if (error_counter) { |
| 1343 | size_t dimension_index[1]; |
| 1344 | |
| 1345 | /* |
| 1346 | * Check that the index is within the boundary of the counter. |
| 1347 | */ |
| 1348 | if (event_notifier->priv->error_counter_index >= event_notifier_group->error_counter_len) { |
| 1349 | printk(KERN_INFO "LTTng: event_notifier: Error counter index out-of-bound: counter-len=%zu, index=%llu\n", |
| 1350 | event_notifier_group->error_counter_len, event_notifier->priv->error_counter_index); |
| 1351 | ret = -EINVAL; |
| 1352 | goto register_error; |
| 1353 | } |
| 1354 | |
| 1355 | dimension_index[0] = event_notifier->priv->error_counter_index; |
| 1356 | ret = error_counter->ops->counter_clear(error_counter->counter, dimension_index); |
| 1357 | if (ret) { |
| 1358 | printk(KERN_INFO "LTTng: event_notifier: Unable to clear error counter bucket %llu\n", |
| 1359 | event_notifier->priv->error_counter_index); |
| 1360 | goto register_error; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | return event_notifier; |
| 1365 | |
| 1366 | register_error: |
| 1367 | kmem_cache_free(event_notifier_private_cache, event_notifier_priv); |
| 1368 | cache_private_error: |
| 1369 | kmem_cache_free(event_notifier_cache, event_notifier); |
| 1370 | cache_error: |
| 1371 | exist: |
| 1372 | type_error: |
| 1373 | return ERR_PTR(ret); |
| 1374 | } |
| 1375 | |
| 1376 | int lttng_kernel_counter_read(struct lttng_counter *counter, |
| 1377 | const size_t *dim_indexes, int32_t cpu, |
| 1378 | int64_t *val, bool *overflow, bool *underflow) |
| 1379 | { |
| 1380 | return counter->ops->counter_read(counter->counter, dim_indexes, |
| 1381 | cpu, val, overflow, underflow); |
| 1382 | } |
| 1383 | |
| 1384 | int lttng_kernel_counter_aggregate(struct lttng_counter *counter, |
| 1385 | const size_t *dim_indexes, int64_t *val, |
| 1386 | bool *overflow, bool *underflow) |
| 1387 | { |
| 1388 | return counter->ops->counter_aggregate(counter->counter, dim_indexes, |
| 1389 | val, overflow, underflow); |
| 1390 | } |
| 1391 | |
| 1392 | int lttng_kernel_counter_clear(struct lttng_counter *counter, |
| 1393 | const size_t *dim_indexes) |
| 1394 | { |
| 1395 | return counter->ops->counter_clear(counter->counter, dim_indexes); |
| 1396 | } |
| 1397 | |
| 1398 | struct lttng_kernel_event_recorder *lttng_kernel_event_recorder_create(struct lttng_event_recorder_enabler *event_enabler, |
| 1399 | const struct lttng_kernel_event_desc *event_desc) |
| 1400 | { |
| 1401 | struct lttng_kernel_event_recorder *event; |
| 1402 | |
| 1403 | mutex_lock(&sessions_mutex); |
| 1404 | event = _lttng_kernel_event_recorder_create(event_enabler, event_desc); |
| 1405 | mutex_unlock(&sessions_mutex); |
| 1406 | return event; |
| 1407 | } |
| 1408 | |
| 1409 | struct lttng_kernel_event_notifier *lttng_event_notifier_create( |
| 1410 | const struct lttng_kernel_event_desc *event_desc, |
| 1411 | uint64_t id, uint64_t error_counter_index, |
| 1412 | struct lttng_event_notifier_group *event_notifier_group, |
| 1413 | struct lttng_kernel_abi_event_notifier *event_notifier_param, |
| 1414 | enum lttng_kernel_abi_instrumentation itype) |
| 1415 | { |
| 1416 | struct lttng_kernel_event_notifier *event_notifier; |
| 1417 | |
| 1418 | mutex_lock(&sessions_mutex); |
| 1419 | event_notifier = _lttng_event_notifier_create(event_desc, id, |
| 1420 | error_counter_index, event_notifier_group, |
| 1421 | event_notifier_param, itype); |
| 1422 | mutex_unlock(&sessions_mutex); |
| 1423 | return event_notifier; |
| 1424 | } |
| 1425 | |
| 1426 | /* Only used for tracepoints for now. */ |
| 1427 | static |
| 1428 | void register_event(struct lttng_kernel_event_recorder *event_recorder) |
| 1429 | { |
| 1430 | const struct lttng_kernel_event_desc *desc; |
| 1431 | int ret = -EINVAL; |
| 1432 | |
| 1433 | if (event_recorder->priv->parent.registered) |
| 1434 | return; |
| 1435 | |
| 1436 | desc = event_recorder->priv->parent.desc; |
| 1437 | switch (event_recorder->priv->parent.instrumentation) { |
| 1438 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 1439 | ret = lttng_wrapper_tracepoint_probe_register(desc->event_kname, |
| 1440 | desc->tp_class->probe_callback, |
| 1441 | event_recorder); |
| 1442 | break; |
| 1443 | |
| 1444 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 1445 | ret = lttng_syscall_filter_enable_event(event_recorder->chan, event_recorder); |
| 1446 | break; |
| 1447 | |
| 1448 | case LTTNG_KERNEL_ABI_KPROBE: |
| 1449 | lttng_fallthrough; |
| 1450 | case LTTNG_KERNEL_ABI_UPROBE: |
| 1451 | lttng_fallthrough; |
| 1452 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 1453 | ret = 0; |
| 1454 | break; |
| 1455 | |
| 1456 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 1457 | lttng_fallthrough; |
| 1458 | case LTTNG_KERNEL_ABI_NOOP: |
| 1459 | lttng_fallthrough; |
| 1460 | default: |
| 1461 | WARN_ON_ONCE(1); |
| 1462 | } |
| 1463 | if (!ret) |
| 1464 | event_recorder->priv->parent.registered = 1; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * Only used internally at session destruction. |
| 1469 | */ |
| 1470 | int _lttng_event_unregister(struct lttng_kernel_event_recorder *event_recorder) |
| 1471 | { |
| 1472 | struct lttng_kernel_event_common_private *event_priv = &event_recorder->priv->parent; |
| 1473 | const struct lttng_kernel_event_desc *desc; |
| 1474 | int ret = -EINVAL; |
| 1475 | |
| 1476 | if (!event_priv->registered) |
| 1477 | return 0; |
| 1478 | |
| 1479 | desc = event_priv->desc; |
| 1480 | switch (event_priv->instrumentation) { |
| 1481 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 1482 | ret = lttng_wrapper_tracepoint_probe_unregister(event_priv->desc->event_kname, |
| 1483 | event_priv->desc->tp_class->probe_callback, |
| 1484 | event_recorder); |
| 1485 | break; |
| 1486 | |
| 1487 | case LTTNG_KERNEL_ABI_KPROBE: |
| 1488 | lttng_kprobes_unregister_event(event_recorder); |
| 1489 | ret = 0; |
| 1490 | break; |
| 1491 | |
| 1492 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 1493 | lttng_kretprobes_unregister(event_recorder); |
| 1494 | ret = 0; |
| 1495 | break; |
| 1496 | |
| 1497 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 1498 | ret = lttng_syscall_filter_disable_event(event_recorder->chan, event_recorder); |
| 1499 | break; |
| 1500 | |
| 1501 | case LTTNG_KERNEL_ABI_NOOP: |
| 1502 | ret = 0; |
| 1503 | break; |
| 1504 | |
| 1505 | case LTTNG_KERNEL_ABI_UPROBE: |
| 1506 | lttng_uprobes_unregister_event(event_recorder); |
| 1507 | ret = 0; |
| 1508 | break; |
| 1509 | |
| 1510 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 1511 | lttng_fallthrough; |
| 1512 | default: |
| 1513 | WARN_ON_ONCE(1); |
| 1514 | } |
| 1515 | if (!ret) |
| 1516 | event_priv->registered = 0; |
| 1517 | return ret; |
| 1518 | } |
| 1519 | |
| 1520 | /* Only used for tracepoints for now. */ |
| 1521 | static |
| 1522 | void register_event_notifier(struct lttng_kernel_event_notifier *event_notifier) |
| 1523 | { |
| 1524 | const struct lttng_kernel_event_desc *desc; |
| 1525 | int ret = -EINVAL; |
| 1526 | |
| 1527 | if (event_notifier->priv->parent.registered) |
| 1528 | return; |
| 1529 | |
| 1530 | desc = event_notifier->priv->parent.desc; |
| 1531 | switch (event_notifier->priv->parent.instrumentation) { |
| 1532 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 1533 | ret = lttng_wrapper_tracepoint_probe_register(desc->event_kname, |
| 1534 | desc->tp_class->probe_callback, |
| 1535 | event_notifier); |
| 1536 | break; |
| 1537 | |
| 1538 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 1539 | ret = lttng_syscall_filter_enable_event_notifier(event_notifier); |
| 1540 | break; |
| 1541 | |
| 1542 | case LTTNG_KERNEL_ABI_KPROBE: |
| 1543 | lttng_fallthrough; |
| 1544 | case LTTNG_KERNEL_ABI_UPROBE: |
| 1545 | ret = 0; |
| 1546 | break; |
| 1547 | |
| 1548 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 1549 | lttng_fallthrough; |
| 1550 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 1551 | lttng_fallthrough; |
| 1552 | case LTTNG_KERNEL_ABI_NOOP: |
| 1553 | lttng_fallthrough; |
| 1554 | default: |
| 1555 | WARN_ON_ONCE(1); |
| 1556 | } |
| 1557 | if (!ret) |
| 1558 | event_notifier->priv->parent.registered = 1; |
| 1559 | } |
| 1560 | |
| 1561 | static |
| 1562 | int _lttng_event_notifier_unregister( |
| 1563 | struct lttng_kernel_event_notifier *event_notifier) |
| 1564 | { |
| 1565 | const struct lttng_kernel_event_desc *desc; |
| 1566 | int ret = -EINVAL; |
| 1567 | |
| 1568 | if (!event_notifier->priv->parent.registered) |
| 1569 | return 0; |
| 1570 | |
| 1571 | desc = event_notifier->priv->parent.desc; |
| 1572 | switch (event_notifier->priv->parent.instrumentation) { |
| 1573 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 1574 | ret = lttng_wrapper_tracepoint_probe_unregister(event_notifier->priv->parent.desc->event_kname, |
| 1575 | event_notifier->priv->parent.desc->tp_class->probe_callback, |
| 1576 | event_notifier); |
| 1577 | break; |
| 1578 | |
| 1579 | case LTTNG_KERNEL_ABI_KPROBE: |
| 1580 | lttng_kprobes_unregister_event_notifier(event_notifier); |
| 1581 | ret = 0; |
| 1582 | break; |
| 1583 | |
| 1584 | case LTTNG_KERNEL_ABI_UPROBE: |
| 1585 | lttng_uprobes_unregister_event_notifier(event_notifier); |
| 1586 | ret = 0; |
| 1587 | break; |
| 1588 | |
| 1589 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 1590 | ret = lttng_syscall_filter_disable_event_notifier(event_notifier); |
| 1591 | break; |
| 1592 | |
| 1593 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 1594 | lttng_fallthrough; |
| 1595 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 1596 | lttng_fallthrough; |
| 1597 | case LTTNG_KERNEL_ABI_NOOP: |
| 1598 | lttng_fallthrough; |
| 1599 | default: |
| 1600 | WARN_ON_ONCE(1); |
| 1601 | } |
| 1602 | if (!ret) |
| 1603 | event_notifier->priv->parent.registered = 0; |
| 1604 | return ret; |
| 1605 | } |
| 1606 | |
| 1607 | /* |
| 1608 | * Only used internally at session destruction. |
| 1609 | */ |
| 1610 | static |
| 1611 | void _lttng_event_destroy(struct lttng_kernel_event_common *event) |
| 1612 | { |
| 1613 | struct lttng_kernel_event_common_private *event_priv = event->priv; |
| 1614 | struct lttng_enabler_ref *enabler_ref, *tmp_enabler_ref; |
| 1615 | |
| 1616 | lttng_free_event_filter_runtime(event); |
| 1617 | /* Free event enabler refs */ |
| 1618 | list_for_each_entry_safe(enabler_ref, tmp_enabler_ref, |
| 1619 | &event_priv->enablers_ref_head, node) |
| 1620 | kfree(enabler_ref); |
| 1621 | |
| 1622 | switch (event->type) { |
| 1623 | case LTTNG_KERNEL_EVENT_TYPE_RECORDER: |
| 1624 | { |
| 1625 | struct lttng_kernel_event_recorder *event_recorder = |
| 1626 | container_of(event, struct lttng_kernel_event_recorder, parent); |
| 1627 | |
| 1628 | switch (event_priv->instrumentation) { |
| 1629 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 1630 | lttng_event_desc_put(event_priv->desc); |
| 1631 | break; |
| 1632 | |
| 1633 | case LTTNG_KERNEL_ABI_KPROBE: |
| 1634 | module_put(event_priv->desc->owner); |
| 1635 | lttng_kprobes_destroy_event_private(event_recorder); |
| 1636 | break; |
| 1637 | |
| 1638 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 1639 | module_put(event_priv->desc->owner); |
| 1640 | lttng_kretprobes_destroy_private(event_recorder); |
| 1641 | break; |
| 1642 | |
| 1643 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 1644 | break; |
| 1645 | |
| 1646 | case LTTNG_KERNEL_ABI_UPROBE: |
| 1647 | module_put(event_priv->desc->owner); |
| 1648 | lttng_uprobes_destroy_event_private(event_recorder); |
| 1649 | break; |
| 1650 | |
| 1651 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 1652 | lttng_fallthrough; |
| 1653 | case LTTNG_KERNEL_ABI_NOOP: |
| 1654 | lttng_fallthrough; |
| 1655 | default: |
| 1656 | WARN_ON_ONCE(1); |
| 1657 | } |
| 1658 | list_del(&event_recorder->priv->node); |
| 1659 | kmem_cache_free(event_recorder_private_cache, event_recorder->priv); |
| 1660 | kmem_cache_free(event_recorder_cache, event_recorder); |
| 1661 | break; |
| 1662 | } |
| 1663 | case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER: |
| 1664 | { |
| 1665 | struct lttng_kernel_event_notifier *event_notifier = |
| 1666 | container_of(event, struct lttng_kernel_event_notifier, parent); |
| 1667 | |
| 1668 | switch (event_notifier->priv->parent.instrumentation) { |
| 1669 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 1670 | lttng_event_desc_put(event_notifier->priv->parent.desc); |
| 1671 | break; |
| 1672 | |
| 1673 | case LTTNG_KERNEL_ABI_KPROBE: |
| 1674 | module_put(event_notifier->priv->parent.desc->owner); |
| 1675 | lttng_kprobes_destroy_event_notifier_private(event_notifier); |
| 1676 | break; |
| 1677 | |
| 1678 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 1679 | break; |
| 1680 | |
| 1681 | case LTTNG_KERNEL_ABI_UPROBE: |
| 1682 | module_put(event_notifier->priv->parent.desc->owner); |
| 1683 | lttng_uprobes_destroy_event_notifier_private(event_notifier); |
| 1684 | break; |
| 1685 | |
| 1686 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 1687 | lttng_fallthrough; |
| 1688 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 1689 | lttng_fallthrough; |
| 1690 | case LTTNG_KERNEL_ABI_NOOP: |
| 1691 | lttng_fallthrough; |
| 1692 | default: |
| 1693 | WARN_ON_ONCE(1); |
| 1694 | } |
| 1695 | list_del(&event_notifier->priv->node); |
| 1696 | kmem_cache_free(event_notifier_private_cache, event_notifier->priv); |
| 1697 | kmem_cache_free(event_notifier_cache, event_notifier); |
| 1698 | break; |
| 1699 | } |
| 1700 | default: |
| 1701 | WARN_ON_ONCE(1); |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | struct lttng_kernel_id_tracker *get_tracker(struct lttng_kernel_session *session, |
| 1706 | enum tracker_type tracker_type) |
| 1707 | { |
| 1708 | switch (tracker_type) { |
| 1709 | case TRACKER_PID: |
| 1710 | return &session->pid_tracker; |
| 1711 | case TRACKER_VPID: |
| 1712 | return &session->vpid_tracker; |
| 1713 | case TRACKER_UID: |
| 1714 | return &session->uid_tracker; |
| 1715 | case TRACKER_VUID: |
| 1716 | return &session->vuid_tracker; |
| 1717 | case TRACKER_GID: |
| 1718 | return &session->gid_tracker; |
| 1719 | case TRACKER_VGID: |
| 1720 | return &session->vgid_tracker; |
| 1721 | default: |
| 1722 | WARN_ON_ONCE(1); |
| 1723 | return NULL; |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | int lttng_session_track_id(struct lttng_kernel_session *session, |
| 1728 | enum tracker_type tracker_type, int id) |
| 1729 | { |
| 1730 | struct lttng_kernel_id_tracker *tracker; |
| 1731 | int ret; |
| 1732 | |
| 1733 | tracker = get_tracker(session, tracker_type); |
| 1734 | if (!tracker) |
| 1735 | return -EINVAL; |
| 1736 | if (id < -1) |
| 1737 | return -EINVAL; |
| 1738 | mutex_lock(&sessions_mutex); |
| 1739 | if (id == -1) { |
| 1740 | /* track all ids: destroy tracker. */ |
| 1741 | lttng_id_tracker_destroy(tracker, true); |
| 1742 | ret = 0; |
| 1743 | } else { |
| 1744 | ret = lttng_id_tracker_add(tracker, id); |
| 1745 | } |
| 1746 | mutex_unlock(&sessions_mutex); |
| 1747 | return ret; |
| 1748 | } |
| 1749 | |
| 1750 | int lttng_session_untrack_id(struct lttng_kernel_session *session, |
| 1751 | enum tracker_type tracker_type, int id) |
| 1752 | { |
| 1753 | struct lttng_kernel_id_tracker *tracker; |
| 1754 | int ret; |
| 1755 | |
| 1756 | tracker = get_tracker(session, tracker_type); |
| 1757 | if (!tracker) |
| 1758 | return -EINVAL; |
| 1759 | if (id < -1) |
| 1760 | return -EINVAL; |
| 1761 | mutex_lock(&sessions_mutex); |
| 1762 | if (id == -1) { |
| 1763 | /* untrack all ids: replace by empty tracker. */ |
| 1764 | ret = lttng_id_tracker_empty_set(tracker); |
| 1765 | } else { |
| 1766 | ret = lttng_id_tracker_del(tracker, id); |
| 1767 | } |
| 1768 | mutex_unlock(&sessions_mutex); |
| 1769 | return ret; |
| 1770 | } |
| 1771 | |
| 1772 | static |
| 1773 | void *id_list_start(struct seq_file *m, loff_t *pos) |
| 1774 | { |
| 1775 | struct lttng_kernel_id_tracker *id_tracker = m->private; |
| 1776 | struct lttng_kernel_id_tracker_rcu *id_tracker_p = id_tracker->p; |
| 1777 | struct lttng_id_hash_node *e; |
| 1778 | int iter = 0, i; |
| 1779 | |
| 1780 | mutex_lock(&sessions_mutex); |
| 1781 | if (id_tracker_p) { |
| 1782 | for (i = 0; i < LTTNG_ID_TABLE_SIZE; i++) { |
| 1783 | struct hlist_head *head = &id_tracker_p->id_hash[i]; |
| 1784 | |
| 1785 | lttng_hlist_for_each_entry(e, head, hlist) { |
| 1786 | if (iter++ >= *pos) |
| 1787 | return e; |
| 1788 | } |
| 1789 | } |
| 1790 | } else { |
| 1791 | /* ID tracker disabled. */ |
| 1792 | if (iter >= *pos && iter == 0) { |
| 1793 | return id_tracker_p; /* empty tracker */ |
| 1794 | } |
| 1795 | iter++; |
| 1796 | } |
| 1797 | /* End of list */ |
| 1798 | return NULL; |
| 1799 | } |
| 1800 | |
| 1801 | /* Called with sessions_mutex held. */ |
| 1802 | static |
| 1803 | void *id_list_next(struct seq_file *m, void *p, loff_t *ppos) |
| 1804 | { |
| 1805 | struct lttng_kernel_id_tracker *id_tracker = m->private; |
| 1806 | struct lttng_kernel_id_tracker_rcu *id_tracker_p = id_tracker->p; |
| 1807 | struct lttng_id_hash_node *e; |
| 1808 | int iter = 0, i; |
| 1809 | |
| 1810 | (*ppos)++; |
| 1811 | if (id_tracker_p) { |
| 1812 | for (i = 0; i < LTTNG_ID_TABLE_SIZE; i++) { |
| 1813 | struct hlist_head *head = &id_tracker_p->id_hash[i]; |
| 1814 | |
| 1815 | lttng_hlist_for_each_entry(e, head, hlist) { |
| 1816 | if (iter++ >= *ppos) |
| 1817 | return e; |
| 1818 | } |
| 1819 | } |
| 1820 | } else { |
| 1821 | /* ID tracker disabled. */ |
| 1822 | if (iter >= *ppos && iter == 0) |
| 1823 | return p; /* empty tracker */ |
| 1824 | iter++; |
| 1825 | } |
| 1826 | |
| 1827 | /* End of list */ |
| 1828 | return NULL; |
| 1829 | } |
| 1830 | |
| 1831 | static |
| 1832 | void id_list_stop(struct seq_file *m, void *p) |
| 1833 | { |
| 1834 | mutex_unlock(&sessions_mutex); |
| 1835 | } |
| 1836 | |
| 1837 | static |
| 1838 | int id_list_show(struct seq_file *m, void *p) |
| 1839 | { |
| 1840 | struct lttng_kernel_id_tracker *id_tracker = m->private; |
| 1841 | struct lttng_kernel_id_tracker_rcu *id_tracker_p = id_tracker->p; |
| 1842 | int id; |
| 1843 | |
| 1844 | if (p == id_tracker_p) { |
| 1845 | /* Tracker disabled. */ |
| 1846 | id = -1; |
| 1847 | } else { |
| 1848 | const struct lttng_id_hash_node *e = p; |
| 1849 | |
| 1850 | id = lttng_id_tracker_get_node_id(e); |
| 1851 | } |
| 1852 | switch (id_tracker->priv->tracker_type) { |
| 1853 | case TRACKER_PID: |
| 1854 | seq_printf(m, "process { pid = %d; };\n", id); |
| 1855 | break; |
| 1856 | case TRACKER_VPID: |
| 1857 | seq_printf(m, "process { vpid = %d; };\n", id); |
| 1858 | break; |
| 1859 | case TRACKER_UID: |
| 1860 | seq_printf(m, "user { uid = %d; };\n", id); |
| 1861 | break; |
| 1862 | case TRACKER_VUID: |
| 1863 | seq_printf(m, "user { vuid = %d; };\n", id); |
| 1864 | break; |
| 1865 | case TRACKER_GID: |
| 1866 | seq_printf(m, "group { gid = %d; };\n", id); |
| 1867 | break; |
| 1868 | case TRACKER_VGID: |
| 1869 | seq_printf(m, "group { vgid = %d; };\n", id); |
| 1870 | break; |
| 1871 | default: |
| 1872 | seq_printf(m, "UNKNOWN { field = %d };\n", id); |
| 1873 | } |
| 1874 | return 0; |
| 1875 | } |
| 1876 | |
| 1877 | static |
| 1878 | const struct seq_operations lttng_tracker_ids_list_seq_ops = { |
| 1879 | .start = id_list_start, |
| 1880 | .next = id_list_next, |
| 1881 | .stop = id_list_stop, |
| 1882 | .show = id_list_show, |
| 1883 | }; |
| 1884 | |
| 1885 | static |
| 1886 | int lttng_tracker_ids_list_open(struct inode *inode, struct file *file) |
| 1887 | { |
| 1888 | return seq_open(file, <tng_tracker_ids_list_seq_ops); |
| 1889 | } |
| 1890 | |
| 1891 | static |
| 1892 | int lttng_tracker_ids_list_release(struct inode *inode, struct file *file) |
| 1893 | { |
| 1894 | struct seq_file *m = file->private_data; |
| 1895 | struct lttng_kernel_id_tracker *id_tracker = m->private; |
| 1896 | int ret; |
| 1897 | |
| 1898 | WARN_ON_ONCE(!id_tracker); |
| 1899 | ret = seq_release(inode, file); |
| 1900 | if (!ret) |
| 1901 | fput(id_tracker->priv->session->priv->file); |
| 1902 | return ret; |
| 1903 | } |
| 1904 | |
| 1905 | const struct file_operations lttng_tracker_ids_list_fops = { |
| 1906 | .owner = THIS_MODULE, |
| 1907 | .open = lttng_tracker_ids_list_open, |
| 1908 | .read = seq_read, |
| 1909 | .llseek = seq_lseek, |
| 1910 | .release = lttng_tracker_ids_list_release, |
| 1911 | }; |
| 1912 | |
| 1913 | int lttng_session_list_tracker_ids(struct lttng_kernel_session *session, |
| 1914 | enum tracker_type tracker_type) |
| 1915 | { |
| 1916 | struct file *tracker_ids_list_file; |
| 1917 | struct seq_file *m; |
| 1918 | int file_fd, ret; |
| 1919 | |
| 1920 | file_fd = lttng_get_unused_fd(); |
| 1921 | if (file_fd < 0) { |
| 1922 | ret = file_fd; |
| 1923 | goto fd_error; |
| 1924 | } |
| 1925 | |
| 1926 | tracker_ids_list_file = anon_inode_getfile("[lttng_tracker_ids_list]", |
| 1927 | <tng_tracker_ids_list_fops, |
| 1928 | NULL, O_RDWR); |
| 1929 | if (IS_ERR(tracker_ids_list_file)) { |
| 1930 | ret = PTR_ERR(tracker_ids_list_file); |
| 1931 | goto file_error; |
| 1932 | } |
| 1933 | if (!atomic_long_add_unless(&session->priv->file->f_count, 1, LONG_MAX)) { |
| 1934 | ret = -EOVERFLOW; |
| 1935 | goto refcount_error; |
| 1936 | } |
| 1937 | ret = lttng_tracker_ids_list_fops.open(NULL, tracker_ids_list_file); |
| 1938 | if (ret < 0) |
| 1939 | goto open_error; |
| 1940 | m = tracker_ids_list_file->private_data; |
| 1941 | |
| 1942 | m->private = get_tracker(session, tracker_type); |
| 1943 | BUG_ON(!m->private); |
| 1944 | fd_install(file_fd, tracker_ids_list_file); |
| 1945 | |
| 1946 | return file_fd; |
| 1947 | |
| 1948 | open_error: |
| 1949 | atomic_long_dec(&session->priv->file->f_count); |
| 1950 | refcount_error: |
| 1951 | fput(tracker_ids_list_file); |
| 1952 | file_error: |
| 1953 | put_unused_fd(file_fd); |
| 1954 | fd_error: |
| 1955 | return ret; |
| 1956 | } |
| 1957 | |
| 1958 | /* |
| 1959 | * Enabler management. |
| 1960 | */ |
| 1961 | static |
| 1962 | int lttng_match_enabler_star_glob(const char *desc_name, |
| 1963 | const char *pattern) |
| 1964 | { |
| 1965 | if (!strutils_star_glob_match(pattern, LTTNG_SIZE_MAX, |
| 1966 | desc_name, LTTNG_SIZE_MAX)) |
| 1967 | return 0; |
| 1968 | return 1; |
| 1969 | } |
| 1970 | |
| 1971 | static |
| 1972 | int lttng_match_enabler_name(const char *desc_name, |
| 1973 | const char *name) |
| 1974 | { |
| 1975 | if (strcmp(desc_name, name)) |
| 1976 | return 0; |
| 1977 | return 1; |
| 1978 | } |
| 1979 | |
| 1980 | int lttng_desc_match_enabler(const struct lttng_kernel_event_desc *desc, |
| 1981 | struct lttng_event_enabler_common *enabler) |
| 1982 | { |
| 1983 | const char *desc_name, *enabler_name; |
| 1984 | bool compat = false, entry = false; |
| 1985 | |
| 1986 | enabler_name = enabler->event_param.name; |
| 1987 | switch (enabler->event_param.instrumentation) { |
| 1988 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 1989 | desc_name = desc->event_name; |
| 1990 | switch (enabler->format_type) { |
| 1991 | case LTTNG_ENABLER_FORMAT_STAR_GLOB: |
| 1992 | return lttng_match_enabler_star_glob(desc_name, enabler_name); |
| 1993 | case LTTNG_ENABLER_FORMAT_NAME: |
| 1994 | return lttng_match_enabler_name(desc_name, enabler_name); |
| 1995 | default: |
| 1996 | return -EINVAL; |
| 1997 | } |
| 1998 | break; |
| 1999 | |
| 2000 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 2001 | desc_name = desc->event_name; |
| 2002 | if (!strncmp(desc_name, "compat_", strlen("compat_"))) { |
| 2003 | desc_name += strlen("compat_"); |
| 2004 | compat = true; |
| 2005 | } |
| 2006 | if (!strncmp(desc_name, "syscall_exit_", |
| 2007 | strlen("syscall_exit_"))) { |
| 2008 | desc_name += strlen("syscall_exit_"); |
| 2009 | } else if (!strncmp(desc_name, "syscall_entry_", |
| 2010 | strlen("syscall_entry_"))) { |
| 2011 | desc_name += strlen("syscall_entry_"); |
| 2012 | entry = true; |
| 2013 | } else { |
| 2014 | WARN_ON_ONCE(1); |
| 2015 | return -EINVAL; |
| 2016 | } |
| 2017 | switch (enabler->event_param.u.syscall.entryexit) { |
| 2018 | case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT: |
| 2019 | break; |
| 2020 | case LTTNG_KERNEL_ABI_SYSCALL_ENTRY: |
| 2021 | if (!entry) |
| 2022 | return 0; |
| 2023 | break; |
| 2024 | case LTTNG_KERNEL_ABI_SYSCALL_EXIT: |
| 2025 | if (entry) |
| 2026 | return 0; |
| 2027 | break; |
| 2028 | default: |
| 2029 | return -EINVAL; |
| 2030 | } |
| 2031 | switch (enabler->event_param.u.syscall.abi) { |
| 2032 | case LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL: |
| 2033 | break; |
| 2034 | case LTTNG_KERNEL_ABI_SYSCALL_ABI_NATIVE: |
| 2035 | if (compat) |
| 2036 | return 0; |
| 2037 | break; |
| 2038 | case LTTNG_KERNEL_ABI_SYSCALL_ABI_COMPAT: |
| 2039 | if (!compat) |
| 2040 | return 0; |
| 2041 | break; |
| 2042 | default: |
| 2043 | return -EINVAL; |
| 2044 | } |
| 2045 | switch (enabler->event_param.u.syscall.match) { |
| 2046 | case LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME: |
| 2047 | switch (enabler->format_type) { |
| 2048 | case LTTNG_ENABLER_FORMAT_STAR_GLOB: |
| 2049 | return lttng_match_enabler_star_glob(desc_name, enabler_name); |
| 2050 | case LTTNG_ENABLER_FORMAT_NAME: |
| 2051 | return lttng_match_enabler_name(desc_name, enabler_name); |
| 2052 | default: |
| 2053 | return -EINVAL; |
| 2054 | } |
| 2055 | break; |
| 2056 | case LTTNG_KERNEL_ABI_SYSCALL_MATCH_NR: |
| 2057 | return -EINVAL; /* Not implemented. */ |
| 2058 | default: |
| 2059 | return -EINVAL; |
| 2060 | } |
| 2061 | break; |
| 2062 | |
| 2063 | default: |
| 2064 | WARN_ON_ONCE(1); |
| 2065 | return -EINVAL; |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | static |
| 2070 | int lttng_event_enabler_match_event(struct lttng_event_recorder_enabler *event_enabler, |
| 2071 | struct lttng_kernel_event_recorder *event_recorder) |
| 2072 | { |
| 2073 | struct lttng_event_enabler_common *base_enabler = lttng_event_recorder_enabler_as_enabler( |
| 2074 | event_enabler); |
| 2075 | |
| 2076 | if (base_enabler->event_param.instrumentation != event_recorder->priv->parent.instrumentation) |
| 2077 | return 0; |
| 2078 | if (lttng_desc_match_enabler(event_recorder->priv->parent.desc, base_enabler) |
| 2079 | && event_recorder->chan == event_enabler->chan) |
| 2080 | return 1; |
| 2081 | else |
| 2082 | return 0; |
| 2083 | } |
| 2084 | |
| 2085 | static |
| 2086 | int lttng_event_notifier_enabler_match_event_notifier(struct lttng_event_notifier_enabler *event_notifier_enabler, |
| 2087 | struct lttng_kernel_event_notifier *event_notifier) |
| 2088 | { |
| 2089 | struct lttng_event_enabler_common *base_enabler = lttng_event_notifier_enabler_as_enabler( |
| 2090 | event_notifier_enabler); |
| 2091 | |
| 2092 | if (base_enabler->event_param.instrumentation != event_notifier->priv->parent.instrumentation) |
| 2093 | return 0; |
| 2094 | if (lttng_desc_match_enabler(event_notifier->priv->parent.desc, base_enabler) |
| 2095 | && event_notifier->priv->group == event_notifier_enabler->group |
| 2096 | && event_notifier->priv->parent.user_token == event_notifier_enabler->parent.user_token) |
| 2097 | return 1; |
| 2098 | else |
| 2099 | return 0; |
| 2100 | } |
| 2101 | |
| 2102 | static |
| 2103 | struct lttng_enabler_ref *lttng_enabler_ref( |
| 2104 | struct list_head *enablers_ref_list, |
| 2105 | struct lttng_event_enabler_common *enabler) |
| 2106 | { |
| 2107 | struct lttng_enabler_ref *enabler_ref; |
| 2108 | |
| 2109 | list_for_each_entry(enabler_ref, enablers_ref_list, node) { |
| 2110 | if (enabler_ref->ref == enabler) |
| 2111 | return enabler_ref; |
| 2112 | } |
| 2113 | return NULL; |
| 2114 | } |
| 2115 | |
| 2116 | static |
| 2117 | void lttng_create_tracepoint_event_if_missing(struct lttng_event_recorder_enabler *event_enabler) |
| 2118 | { |
| 2119 | struct lttng_kernel_session *session = event_enabler->chan->parent.session; |
| 2120 | struct lttng_kernel_probe_desc *probe_desc; |
| 2121 | const struct lttng_kernel_event_desc *desc; |
| 2122 | int i; |
| 2123 | struct list_head *probe_list; |
| 2124 | |
| 2125 | probe_list = lttng_get_probe_list_head(); |
| 2126 | /* |
| 2127 | * For each probe event, if we find that a probe event matches |
| 2128 | * our enabler, create an associated lttng_event if not |
| 2129 | * already present. |
| 2130 | */ |
| 2131 | list_for_each_entry(probe_desc, probe_list, head) { |
| 2132 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 2133 | int found = 0; |
| 2134 | struct hlist_head *head; |
| 2135 | struct lttng_kernel_event_recorder_private *event_recorder_private; |
| 2136 | struct lttng_kernel_event_recorder *event_recorder; |
| 2137 | |
| 2138 | desc = probe_desc->event_desc[i]; |
| 2139 | if (!lttng_desc_match_enabler(desc, |
| 2140 | lttng_event_recorder_enabler_as_enabler(event_enabler))) |
| 2141 | continue; |
| 2142 | |
| 2143 | /* |
| 2144 | * Check if already created. |
| 2145 | */ |
| 2146 | head = utils_borrow_hash_table_bucket( |
| 2147 | session->priv->events_ht.table, LTTNG_EVENT_HT_SIZE, |
| 2148 | desc->event_name); |
| 2149 | lttng_hlist_for_each_entry(event_recorder_private, head, hlist) { |
| 2150 | if (event_recorder_private->parent.desc == desc |
| 2151 | && event_recorder_private->pub->chan == event_enabler->chan) |
| 2152 | found = 1; |
| 2153 | } |
| 2154 | if (found) |
| 2155 | continue; |
| 2156 | |
| 2157 | /* |
| 2158 | * We need to create an event for this |
| 2159 | * event probe. |
| 2160 | */ |
| 2161 | event_recorder = _lttng_kernel_event_recorder_create(event_enabler, desc); |
| 2162 | if (!event_recorder) { |
| 2163 | printk(KERN_INFO "LTTng: Unable to create event %s\n", |
| 2164 | probe_desc->event_desc[i]->event_name); |
| 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | static |
| 2171 | void lttng_create_tracepoint_event_notifier_if_missing(struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 2172 | { |
| 2173 | struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group; |
| 2174 | struct lttng_kernel_probe_desc *probe_desc; |
| 2175 | const struct lttng_kernel_event_desc *desc; |
| 2176 | int i; |
| 2177 | struct list_head *probe_list; |
| 2178 | |
| 2179 | probe_list = lttng_get_probe_list_head(); |
| 2180 | /* |
| 2181 | * For each probe event, if we find that a probe event matches |
| 2182 | * our enabler, create an associated lttng_event_notifier if not |
| 2183 | * already present. |
| 2184 | */ |
| 2185 | list_for_each_entry(probe_desc, probe_list, head) { |
| 2186 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 2187 | int found = 0; |
| 2188 | struct hlist_head *head; |
| 2189 | struct lttng_kernel_event_notifier_private *event_notifier_priv; |
| 2190 | struct lttng_kernel_event_notifier *event_notifier; |
| 2191 | |
| 2192 | desc = probe_desc->event_desc[i]; |
| 2193 | if (!lttng_desc_match_enabler(desc, |
| 2194 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler))) |
| 2195 | continue; |
| 2196 | |
| 2197 | /* |
| 2198 | * Check if already created. |
| 2199 | */ |
| 2200 | head = utils_borrow_hash_table_bucket( |
| 2201 | event_notifier_group->event_notifiers_ht.table, |
| 2202 | LTTNG_EVENT_NOTIFIER_HT_SIZE, desc->event_name); |
| 2203 | lttng_hlist_for_each_entry(event_notifier_priv, head, hlist) { |
| 2204 | if (event_notifier_priv->parent.desc == desc |
| 2205 | && event_notifier_priv->parent.user_token == event_notifier_enabler->parent.user_token) |
| 2206 | found = 1; |
| 2207 | } |
| 2208 | if (found) |
| 2209 | continue; |
| 2210 | |
| 2211 | /* |
| 2212 | * We need to create a event_notifier for this event probe. |
| 2213 | */ |
| 2214 | event_notifier = _lttng_event_notifier_create(desc, |
| 2215 | event_notifier_enabler->parent.user_token, |
| 2216 | event_notifier_enabler->error_counter_index, |
| 2217 | event_notifier_group, NULL, |
| 2218 | LTTNG_KERNEL_ABI_TRACEPOINT); |
| 2219 | if (IS_ERR(event_notifier)) { |
| 2220 | printk(KERN_INFO "Unable to create event_notifier %s\n", |
| 2221 | probe_desc->event_desc[i]->event_name); |
| 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | static |
| 2228 | void lttng_create_syscall_event_if_missing(struct lttng_event_recorder_enabler *event_enabler) |
| 2229 | { |
| 2230 | int ret; |
| 2231 | |
| 2232 | ret = lttng_syscalls_register_event(event_enabler); |
| 2233 | WARN_ON_ONCE(ret); |
| 2234 | } |
| 2235 | |
| 2236 | static |
| 2237 | void lttng_create_syscall_event_notifier_if_missing(struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 2238 | { |
| 2239 | int ret; |
| 2240 | |
| 2241 | ret = lttng_syscalls_register_event_notifier(event_notifier_enabler); |
| 2242 | WARN_ON_ONCE(ret); |
| 2243 | ret = lttng_syscalls_create_matching_event_notifiers(event_notifier_enabler); |
| 2244 | WARN_ON_ONCE(ret); |
| 2245 | } |
| 2246 | |
| 2247 | /* |
| 2248 | * Create struct lttng_kernel_event_recorder if it is missing and present in the list of |
| 2249 | * tracepoint probes. |
| 2250 | * Should be called with sessions mutex held. |
| 2251 | */ |
| 2252 | static |
| 2253 | void lttng_create_event_if_missing(struct lttng_event_recorder_enabler *event_enabler) |
| 2254 | { |
| 2255 | switch (event_enabler->parent.event_param.instrumentation) { |
| 2256 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 2257 | lttng_create_tracepoint_event_if_missing(event_enabler); |
| 2258 | break; |
| 2259 | |
| 2260 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 2261 | lttng_create_syscall_event_if_missing(event_enabler); |
| 2262 | break; |
| 2263 | |
| 2264 | default: |
| 2265 | WARN_ON_ONCE(1); |
| 2266 | break; |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | /* |
| 2271 | * Create events associated with an event_enabler (if not already present), |
| 2272 | * and add backward reference from the event to the enabler. |
| 2273 | * Should be called with sessions mutex held. |
| 2274 | */ |
| 2275 | static |
| 2276 | int lttng_event_enabler_ref_events(struct lttng_event_recorder_enabler *event_enabler) |
| 2277 | { |
| 2278 | struct lttng_kernel_channel_buffer *chan = event_enabler->chan; |
| 2279 | struct lttng_kernel_session *session = event_enabler->chan->parent.session; |
| 2280 | struct lttng_event_enabler_common *base_enabler = lttng_event_recorder_enabler_as_enabler(event_enabler); |
| 2281 | struct lttng_kernel_event_recorder_private *event_recorder_priv; |
| 2282 | |
| 2283 | if (base_enabler->event_param.instrumentation == LTTNG_KERNEL_ABI_SYSCALL && |
| 2284 | base_enabler->event_param.u.syscall.abi == LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL && |
| 2285 | base_enabler->event_param.u.syscall.match == LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME && |
| 2286 | !strcmp(base_enabler->event_param.name, "*")) { |
| 2287 | int enabled = base_enabler->enabled; |
| 2288 | enum lttng_kernel_abi_syscall_entryexit entryexit = base_enabler->event_param.u.syscall.entryexit; |
| 2289 | |
| 2290 | if (entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRY || entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT) |
| 2291 | WRITE_ONCE(chan->priv->parent.syscall_table.syscall_all_entry, enabled); |
| 2292 | |
| 2293 | if (entryexit == LTTNG_KERNEL_ABI_SYSCALL_EXIT || entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT) |
| 2294 | WRITE_ONCE(chan->priv->parent.syscall_table.syscall_all_exit, enabled); |
| 2295 | } |
| 2296 | |
| 2297 | /* First ensure that probe events are created for this enabler. */ |
| 2298 | lttng_create_event_if_missing(event_enabler); |
| 2299 | |
| 2300 | /* For each event matching event_enabler in session event list. */ |
| 2301 | list_for_each_entry(event_recorder_priv, &session->priv->events, node) { |
| 2302 | struct lttng_kernel_event_recorder *event_recorder = event_recorder_priv->pub; |
| 2303 | struct lttng_enabler_ref *enabler_ref; |
| 2304 | |
| 2305 | if (!lttng_event_enabler_match_event(event_enabler, event_recorder)) |
| 2306 | continue; |
| 2307 | enabler_ref = lttng_enabler_ref(&event_recorder_priv->parent.enablers_ref_head, |
| 2308 | lttng_event_recorder_enabler_as_enabler(event_enabler)); |
| 2309 | if (!enabler_ref) { |
| 2310 | /* |
| 2311 | * If no backward ref, create it. |
| 2312 | * Add backward ref from event to event_enabler. |
| 2313 | */ |
| 2314 | enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL); |
| 2315 | if (!enabler_ref) |
| 2316 | return -ENOMEM; |
| 2317 | enabler_ref->ref = lttng_event_recorder_enabler_as_enabler(event_enabler); |
| 2318 | list_add(&enabler_ref->node, |
| 2319 | &event_recorder_priv->parent.enablers_ref_head); |
| 2320 | } |
| 2321 | |
| 2322 | /* |
| 2323 | * Link filter bytecodes if not linked yet. |
| 2324 | */ |
| 2325 | lttng_enabler_link_bytecode(event_recorder_priv->parent.desc, |
| 2326 | lttng_static_ctx, |
| 2327 | &event_recorder_priv->parent.filter_bytecode_runtime_head, |
| 2328 | <tng_event_recorder_enabler_as_enabler(event_enabler)->filter_bytecode_head); |
| 2329 | } |
| 2330 | return 0; |
| 2331 | } |
| 2332 | |
| 2333 | /* |
| 2334 | * Create struct lttng_kernel_event_notifier if it is missing and present in the list of |
| 2335 | * tracepoint probes. |
| 2336 | * Should be called with sessions mutex held. |
| 2337 | */ |
| 2338 | static |
| 2339 | void lttng_create_event_notifier_if_missing(struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 2340 | { |
| 2341 | switch (event_notifier_enabler->parent.event_param.instrumentation) { |
| 2342 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 2343 | lttng_create_tracepoint_event_notifier_if_missing(event_notifier_enabler); |
| 2344 | break; |
| 2345 | |
| 2346 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 2347 | lttng_create_syscall_event_notifier_if_missing(event_notifier_enabler); |
| 2348 | break; |
| 2349 | |
| 2350 | default: |
| 2351 | WARN_ON_ONCE(1); |
| 2352 | break; |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | /* |
| 2357 | * Create event_notifiers associated with a event_notifier enabler (if not already present). |
| 2358 | */ |
| 2359 | static |
| 2360 | int lttng_event_notifier_enabler_ref_event_notifiers( |
| 2361 | struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 2362 | { |
| 2363 | struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group; |
| 2364 | struct lttng_event_enabler_common *base_enabler = lttng_event_notifier_enabler_as_enabler(event_notifier_enabler); |
| 2365 | struct lttng_kernel_event_notifier_private *event_notifier_priv; |
| 2366 | |
| 2367 | if (base_enabler->event_param.instrumentation == LTTNG_KERNEL_ABI_SYSCALL && |
| 2368 | base_enabler->event_param.u.syscall.abi == LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL && |
| 2369 | base_enabler->event_param.u.syscall.match == LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME && |
| 2370 | !strcmp(base_enabler->event_param.name, "*")) { |
| 2371 | |
| 2372 | int enabled = base_enabler->enabled; |
| 2373 | enum lttng_kernel_abi_syscall_entryexit entryexit = base_enabler->event_param.u.syscall.entryexit; |
| 2374 | |
| 2375 | if (entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRY || entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT) |
| 2376 | WRITE_ONCE(event_notifier_group->syscall_table.syscall_all_entry, enabled); |
| 2377 | |
| 2378 | if (entryexit == LTTNG_KERNEL_ABI_SYSCALL_EXIT || entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT) |
| 2379 | WRITE_ONCE(event_notifier_group->syscall_table.syscall_all_exit, enabled); |
| 2380 | |
| 2381 | } |
| 2382 | |
| 2383 | /* First ensure that probe event_notifiers are created for this enabler. */ |
| 2384 | lttng_create_event_notifier_if_missing(event_notifier_enabler); |
| 2385 | |
| 2386 | /* Link the created event_notifier with its associated enabler. */ |
| 2387 | list_for_each_entry(event_notifier_priv, &event_notifier_group->event_notifiers_head, node) { |
| 2388 | struct lttng_kernel_event_notifier *event_notifier = event_notifier_priv->pub; |
| 2389 | struct lttng_enabler_ref *enabler_ref; |
| 2390 | |
| 2391 | if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler, event_notifier)) |
| 2392 | continue; |
| 2393 | |
| 2394 | enabler_ref = lttng_enabler_ref(&event_notifier_priv->parent.enablers_ref_head, |
| 2395 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)); |
| 2396 | if (!enabler_ref) { |
| 2397 | /* |
| 2398 | * If no backward ref, create it. |
| 2399 | * Add backward ref from event_notifier to enabler. |
| 2400 | */ |
| 2401 | enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL); |
| 2402 | if (!enabler_ref) |
| 2403 | return -ENOMEM; |
| 2404 | |
| 2405 | enabler_ref->ref = lttng_event_notifier_enabler_as_enabler( |
| 2406 | event_notifier_enabler); |
| 2407 | list_add(&enabler_ref->node, |
| 2408 | &event_notifier_priv->parent.enablers_ref_head); |
| 2409 | } |
| 2410 | |
| 2411 | /* |
| 2412 | * Link filter bytecodes if not linked yet. |
| 2413 | */ |
| 2414 | lttng_enabler_link_bytecode(event_notifier_priv->parent.desc, |
| 2415 | lttng_static_ctx, &event_notifier_priv->parent.filter_bytecode_runtime_head, |
| 2416 | <tng_event_notifier_enabler_as_enabler(event_notifier_enabler)->filter_bytecode_head); |
| 2417 | |
| 2418 | /* Link capture bytecodes if not linked yet. */ |
| 2419 | lttng_enabler_link_bytecode(event_notifier_priv->parent.desc, |
| 2420 | lttng_static_ctx, &event_notifier_priv->capture_bytecode_runtime_head, |
| 2421 | &event_notifier_enabler->capture_bytecode_head); |
| 2422 | |
| 2423 | event_notifier_priv->num_captures = event_notifier_enabler->num_captures; |
| 2424 | } |
| 2425 | return 0; |
| 2426 | } |
| 2427 | |
| 2428 | /* |
| 2429 | * Called at module load: connect the probe on all enablers matching |
| 2430 | * this event. |
| 2431 | * Called with sessions lock held. |
| 2432 | */ |
| 2433 | int lttng_fix_pending_events(void) |
| 2434 | { |
| 2435 | struct lttng_kernel_session_private *session_priv; |
| 2436 | |
| 2437 | list_for_each_entry(session_priv, &sessions, list) |
| 2438 | lttng_session_lazy_sync_event_enablers(session_priv->pub); |
| 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | static bool lttng_event_notifier_group_has_active_event_notifiers( |
| 2443 | struct lttng_event_notifier_group *event_notifier_group) |
| 2444 | { |
| 2445 | struct lttng_event_notifier_enabler *event_notifier_enabler; |
| 2446 | |
| 2447 | list_for_each_entry(event_notifier_enabler, &event_notifier_group->enablers_head, |
| 2448 | node) { |
| 2449 | if (event_notifier_enabler->parent.enabled) |
| 2450 | return true; |
| 2451 | } |
| 2452 | return false; |
| 2453 | } |
| 2454 | |
| 2455 | bool lttng_event_notifier_active(void) |
| 2456 | { |
| 2457 | struct lttng_event_notifier_group *event_notifier_group; |
| 2458 | |
| 2459 | list_for_each_entry(event_notifier_group, &event_notifier_groups, node) { |
| 2460 | if (lttng_event_notifier_group_has_active_event_notifiers(event_notifier_group)) |
| 2461 | return true; |
| 2462 | } |
| 2463 | return false; |
| 2464 | } |
| 2465 | |
| 2466 | int lttng_fix_pending_event_notifiers(void) |
| 2467 | { |
| 2468 | struct lttng_event_notifier_group *event_notifier_group; |
| 2469 | |
| 2470 | list_for_each_entry(event_notifier_group, &event_notifier_groups, node) |
| 2471 | lttng_event_notifier_group_sync_enablers(event_notifier_group); |
| 2472 | return 0; |
| 2473 | } |
| 2474 | |
| 2475 | struct lttng_event_recorder_enabler *lttng_event_recorder_enabler_create( |
| 2476 | enum lttng_enabler_format_type format_type, |
| 2477 | struct lttng_kernel_abi_event *event_param, |
| 2478 | struct lttng_kernel_channel_buffer *chan) |
| 2479 | { |
| 2480 | struct lttng_event_recorder_enabler *event_enabler; |
| 2481 | |
| 2482 | event_enabler = kzalloc(sizeof(*event_enabler), GFP_KERNEL); |
| 2483 | if (!event_enabler) |
| 2484 | return NULL; |
| 2485 | event_enabler->parent.enabler_type = LTTNG_EVENT_ENABLER_TYPE_RECORDER; |
| 2486 | event_enabler->parent.format_type = format_type; |
| 2487 | INIT_LIST_HEAD(&event_enabler->parent.filter_bytecode_head); |
| 2488 | memcpy(&event_enabler->parent.event_param, event_param, |
| 2489 | sizeof(event_enabler->parent.event_param)); |
| 2490 | event_enabler->chan = chan; |
| 2491 | /* ctx left NULL */ |
| 2492 | event_enabler->parent.enabled = 0; |
| 2493 | return event_enabler; |
| 2494 | } |
| 2495 | |
| 2496 | void lttng_event_enabler_session_add(struct lttng_kernel_session *session, |
| 2497 | struct lttng_event_recorder_enabler *event_enabler) |
| 2498 | { |
| 2499 | mutex_lock(&sessions_mutex); |
| 2500 | list_add(&event_enabler->node, &session->priv->enablers_head); |
| 2501 | event_enabler->published = true; |
| 2502 | lttng_session_lazy_sync_event_enablers(session); |
| 2503 | mutex_unlock(&sessions_mutex); |
| 2504 | } |
| 2505 | |
| 2506 | int lttng_event_enabler_enable(struct lttng_event_enabler_common *event_enabler) |
| 2507 | { |
| 2508 | mutex_lock(&sessions_mutex); |
| 2509 | event_enabler->enabled = 1; |
| 2510 | lttng_event_enabler_sync(event_enabler); |
| 2511 | mutex_unlock(&sessions_mutex); |
| 2512 | return 0; |
| 2513 | } |
| 2514 | |
| 2515 | int lttng_event_enabler_disable(struct lttng_event_enabler_common *event_enabler) |
| 2516 | { |
| 2517 | mutex_lock(&sessions_mutex); |
| 2518 | event_enabler->enabled = 0; |
| 2519 | lttng_event_enabler_sync(event_enabler); |
| 2520 | mutex_unlock(&sessions_mutex); |
| 2521 | return 0; |
| 2522 | } |
| 2523 | |
| 2524 | static |
| 2525 | int lttng_enabler_attach_filter_bytecode(struct lttng_event_enabler_common *enabler, |
| 2526 | struct lttng_kernel_abi_filter_bytecode __user *bytecode) |
| 2527 | { |
| 2528 | struct lttng_kernel_bytecode_node *bytecode_node; |
| 2529 | uint32_t bytecode_len; |
| 2530 | int ret; |
| 2531 | |
| 2532 | ret = get_user(bytecode_len, &bytecode->len); |
| 2533 | if (ret) |
| 2534 | return ret; |
| 2535 | bytecode_node = lttng_kvzalloc(sizeof(*bytecode_node) + bytecode_len, |
| 2536 | GFP_KERNEL); |
| 2537 | if (!bytecode_node) |
| 2538 | return -ENOMEM; |
| 2539 | ret = copy_from_user(&bytecode_node->bc, bytecode, |
| 2540 | sizeof(*bytecode) + bytecode_len); |
| 2541 | if (ret) |
| 2542 | goto error_free; |
| 2543 | |
| 2544 | bytecode_node->type = LTTNG_KERNEL_BYTECODE_TYPE_FILTER; |
| 2545 | bytecode_node->enabler = enabler; |
| 2546 | /* Enforce length based on allocated size */ |
| 2547 | bytecode_node->bc.len = bytecode_len; |
| 2548 | list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head); |
| 2549 | |
| 2550 | return 0; |
| 2551 | |
| 2552 | error_free: |
| 2553 | lttng_kvfree(bytecode_node); |
| 2554 | return ret; |
| 2555 | } |
| 2556 | |
| 2557 | int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler_common *event_enabler, |
| 2558 | struct lttng_kernel_abi_filter_bytecode __user *bytecode) |
| 2559 | { |
| 2560 | int ret; |
| 2561 | ret = lttng_enabler_attach_filter_bytecode(event_enabler, bytecode); |
| 2562 | if (ret) |
| 2563 | goto error; |
| 2564 | lttng_event_enabler_sync(event_enabler); |
| 2565 | return 0; |
| 2566 | |
| 2567 | error: |
| 2568 | return ret; |
| 2569 | } |
| 2570 | |
| 2571 | int lttng_event_add_callsite(struct lttng_kernel_event_common *event, |
| 2572 | struct lttng_kernel_abi_event_callsite __user *callsite) |
| 2573 | { |
| 2574 | |
| 2575 | switch (event->priv->instrumentation) { |
| 2576 | case LTTNG_KERNEL_ABI_UPROBE: |
| 2577 | return lttng_uprobes_event_add_callsite(event, callsite); |
| 2578 | default: |
| 2579 | return -EINVAL; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | static |
| 2584 | void lttng_enabler_destroy(struct lttng_event_enabler_common *enabler) |
| 2585 | { |
| 2586 | struct lttng_kernel_bytecode_node *filter_node, *tmp_filter_node; |
| 2587 | |
| 2588 | /* Destroy filter bytecode */ |
| 2589 | list_for_each_entry_safe(filter_node, tmp_filter_node, |
| 2590 | &enabler->filter_bytecode_head, node) { |
| 2591 | lttng_kvfree(filter_node); |
| 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | void lttng_event_enabler_destroy(struct lttng_event_enabler_common *event_enabler) |
| 2596 | { |
| 2597 | switch (event_enabler->enabler_type) { |
| 2598 | case LTTNG_EVENT_ENABLER_TYPE_RECORDER: |
| 2599 | { |
| 2600 | struct lttng_event_recorder_enabler *event_recorder_enabler = |
| 2601 | container_of(event_enabler, struct lttng_event_recorder_enabler, parent); |
| 2602 | |
| 2603 | lttng_enabler_destroy(event_enabler); |
| 2604 | if (event_recorder_enabler->published) |
| 2605 | list_del(&event_recorder_enabler->node); |
| 2606 | kfree(event_recorder_enabler); |
| 2607 | break; |
| 2608 | } |
| 2609 | case LTTNG_EVENT_ENABLER_TYPE_NOTIFIER: |
| 2610 | { |
| 2611 | struct lttng_event_notifier_enabler *event_notifier_enabler = |
| 2612 | container_of(event_enabler, struct lttng_event_notifier_enabler, parent); |
| 2613 | |
| 2614 | list_del(&event_notifier_enabler->node); |
| 2615 | lttng_enabler_destroy(event_enabler); |
| 2616 | kfree(event_notifier_enabler); |
| 2617 | break; |
| 2618 | } |
| 2619 | default: |
| 2620 | WARN_ON_ONCE(1); |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | struct lttng_event_notifier_enabler *lttng_event_notifier_enabler_create( |
| 2625 | struct lttng_event_notifier_group *event_notifier_group, |
| 2626 | enum lttng_enabler_format_type format_type, |
| 2627 | struct lttng_kernel_abi_event_notifier *event_notifier_param) |
| 2628 | { |
| 2629 | struct lttng_event_notifier_enabler *event_notifier_enabler; |
| 2630 | |
| 2631 | event_notifier_enabler = kzalloc(sizeof(*event_notifier_enabler), GFP_KERNEL); |
| 2632 | if (!event_notifier_enabler) |
| 2633 | return NULL; |
| 2634 | |
| 2635 | event_notifier_enabler->parent.enabler_type = LTTNG_EVENT_ENABLER_TYPE_NOTIFIER; |
| 2636 | event_notifier_enabler->parent.format_type = format_type; |
| 2637 | INIT_LIST_HEAD(&event_notifier_enabler->parent.filter_bytecode_head); |
| 2638 | INIT_LIST_HEAD(&event_notifier_enabler->capture_bytecode_head); |
| 2639 | |
| 2640 | event_notifier_enabler->error_counter_index = event_notifier_param->error_counter_index; |
| 2641 | event_notifier_enabler->num_captures = 0; |
| 2642 | |
| 2643 | memcpy(&event_notifier_enabler->parent.event_param, &event_notifier_param->event, |
| 2644 | sizeof(event_notifier_enabler->parent.event_param)); |
| 2645 | |
| 2646 | event_notifier_enabler->parent.enabled = 0; |
| 2647 | event_notifier_enabler->parent.user_token = event_notifier_param->event.token; |
| 2648 | event_notifier_enabler->group = event_notifier_group; |
| 2649 | |
| 2650 | mutex_lock(&sessions_mutex); |
| 2651 | list_add(&event_notifier_enabler->node, &event_notifier_enabler->group->enablers_head); |
| 2652 | lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group); |
| 2653 | |
| 2654 | mutex_unlock(&sessions_mutex); |
| 2655 | |
| 2656 | return event_notifier_enabler; |
| 2657 | } |
| 2658 | |
| 2659 | int lttng_event_notifier_enabler_enable( |
| 2660 | struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 2661 | { |
| 2662 | mutex_lock(&sessions_mutex); |
| 2663 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled = 1; |
| 2664 | lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group); |
| 2665 | mutex_unlock(&sessions_mutex); |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
| 2669 | int lttng_event_notifier_enabler_disable( |
| 2670 | struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 2671 | { |
| 2672 | mutex_lock(&sessions_mutex); |
| 2673 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled = 0; |
| 2674 | lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group); |
| 2675 | mutex_unlock(&sessions_mutex); |
| 2676 | return 0; |
| 2677 | } |
| 2678 | |
| 2679 | int lttng_event_notifier_enabler_attach_capture_bytecode( |
| 2680 | struct lttng_event_notifier_enabler *event_notifier_enabler, |
| 2681 | struct lttng_kernel_abi_capture_bytecode __user *bytecode) |
| 2682 | { |
| 2683 | struct lttng_kernel_bytecode_node *bytecode_node; |
| 2684 | struct lttng_event_enabler_common *enabler = |
| 2685 | lttng_event_notifier_enabler_as_enabler(event_notifier_enabler); |
| 2686 | uint32_t bytecode_len; |
| 2687 | int ret; |
| 2688 | |
| 2689 | ret = get_user(bytecode_len, &bytecode->len); |
| 2690 | if (ret) |
| 2691 | return ret; |
| 2692 | |
| 2693 | bytecode_node = lttng_kvzalloc(sizeof(*bytecode_node) + bytecode_len, |
| 2694 | GFP_KERNEL); |
| 2695 | if (!bytecode_node) |
| 2696 | return -ENOMEM; |
| 2697 | |
| 2698 | ret = copy_from_user(&bytecode_node->bc, bytecode, |
| 2699 | sizeof(*bytecode) + bytecode_len); |
| 2700 | if (ret) |
| 2701 | goto error_free; |
| 2702 | |
| 2703 | bytecode_node->type = LTTNG_KERNEL_BYTECODE_TYPE_CAPTURE; |
| 2704 | bytecode_node->enabler = enabler; |
| 2705 | |
| 2706 | /* Enforce length based on allocated size */ |
| 2707 | bytecode_node->bc.len = bytecode_len; |
| 2708 | list_add_tail(&bytecode_node->node, &event_notifier_enabler->capture_bytecode_head); |
| 2709 | |
| 2710 | event_notifier_enabler->num_captures++; |
| 2711 | |
| 2712 | lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group); |
| 2713 | goto end; |
| 2714 | |
| 2715 | error_free: |
| 2716 | lttng_kvfree(bytecode_node); |
| 2717 | end: |
| 2718 | return ret; |
| 2719 | } |
| 2720 | |
| 2721 | /* |
| 2722 | * lttng_session_sync_event_enablers should be called just before starting a |
| 2723 | * session. |
| 2724 | * Should be called with sessions mutex held. |
| 2725 | */ |
| 2726 | static |
| 2727 | void lttng_session_sync_event_enablers(struct lttng_kernel_session *session) |
| 2728 | { |
| 2729 | struct lttng_event_recorder_enabler *event_enabler; |
| 2730 | struct lttng_kernel_event_recorder_private *event_recorder_priv; |
| 2731 | |
| 2732 | list_for_each_entry(event_enabler, &session->priv->enablers_head, node) |
| 2733 | lttng_event_enabler_ref_events(event_enabler); |
| 2734 | /* |
| 2735 | * For each event, if at least one of its enablers is enabled, |
| 2736 | * and its channel and session transient states are enabled, we |
| 2737 | * enable the event, else we disable it. |
| 2738 | */ |
| 2739 | list_for_each_entry(event_recorder_priv, &session->priv->events, node) { |
| 2740 | struct lttng_kernel_event_recorder *event_recorder = event_recorder_priv->pub; |
| 2741 | struct lttng_enabler_ref *enabler_ref; |
| 2742 | struct lttng_kernel_bytecode_runtime *runtime; |
| 2743 | int enabled = 0, has_enablers_without_filter_bytecode = 0; |
| 2744 | int nr_filters = 0; |
| 2745 | |
| 2746 | switch (event_recorder_priv->parent.instrumentation) { |
| 2747 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 2748 | lttng_fallthrough; |
| 2749 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 2750 | /* Enable events */ |
| 2751 | list_for_each_entry(enabler_ref, |
| 2752 | &event_recorder_priv->parent.enablers_ref_head, node) { |
| 2753 | if (enabler_ref->ref->enabled) { |
| 2754 | enabled = 1; |
| 2755 | break; |
| 2756 | } |
| 2757 | } |
| 2758 | break; |
| 2759 | |
| 2760 | default: |
| 2761 | /* Not handled with lazy sync. */ |
| 2762 | continue; |
| 2763 | } |
| 2764 | /* |
| 2765 | * Enabled state is based on union of enablers, with |
| 2766 | * intesection of session and channel transient enable |
| 2767 | * states. |
| 2768 | */ |
| 2769 | enabled = enabled && session->priv->tstate && event_recorder->chan->priv->parent.tstate; |
| 2770 | |
| 2771 | WRITE_ONCE(event_recorder->parent.enabled, enabled); |
| 2772 | /* |
| 2773 | * Sync tracepoint registration with event enabled |
| 2774 | * state. |
| 2775 | */ |
| 2776 | if (enabled) { |
| 2777 | register_event(event_recorder); |
| 2778 | } else { |
| 2779 | _lttng_event_unregister(event_recorder); |
| 2780 | } |
| 2781 | |
| 2782 | /* Check if has enablers without bytecode enabled */ |
| 2783 | list_for_each_entry(enabler_ref, |
| 2784 | &event_recorder_priv->parent.enablers_ref_head, node) { |
| 2785 | if (enabler_ref->ref->enabled |
| 2786 | && list_empty(&enabler_ref->ref->filter_bytecode_head)) { |
| 2787 | has_enablers_without_filter_bytecode = 1; |
| 2788 | break; |
| 2789 | } |
| 2790 | } |
| 2791 | event_recorder_priv->parent.has_enablers_without_filter_bytecode = |
| 2792 | has_enablers_without_filter_bytecode; |
| 2793 | |
| 2794 | /* Enable filters */ |
| 2795 | list_for_each_entry(runtime, |
| 2796 | &event_recorder_priv->parent.filter_bytecode_runtime_head, node) { |
| 2797 | lttng_bytecode_sync_state(runtime); |
| 2798 | nr_filters++; |
| 2799 | } |
| 2800 | WRITE_ONCE(event_recorder_priv->parent.pub->eval_filter, |
| 2801 | !(has_enablers_without_filter_bytecode || !nr_filters)); |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | /* |
| 2806 | * Apply enablers to session events, adding events to session if need |
| 2807 | * be. It is required after each modification applied to an active |
| 2808 | * session, and right before session "start". |
| 2809 | * "lazy" sync means we only sync if required. |
| 2810 | * Should be called with sessions mutex held. |
| 2811 | */ |
| 2812 | static |
| 2813 | void lttng_session_lazy_sync_event_enablers(struct lttng_kernel_session *session) |
| 2814 | { |
| 2815 | /* We can skip if session is not active */ |
| 2816 | if (!session->active) |
| 2817 | return; |
| 2818 | lttng_session_sync_event_enablers(session); |
| 2819 | } |
| 2820 | |
| 2821 | static |
| 2822 | void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group *event_notifier_group) |
| 2823 | { |
| 2824 | struct lttng_event_notifier_enabler *event_notifier_enabler; |
| 2825 | struct lttng_kernel_event_notifier_private *event_notifier_priv; |
| 2826 | |
| 2827 | list_for_each_entry(event_notifier_enabler, &event_notifier_group->enablers_head, node) |
| 2828 | lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler); |
| 2829 | |
| 2830 | /* |
| 2831 | * For each event_notifier, if at least one of its enablers is enabled, |
| 2832 | * we enable the event_notifier, else we disable it. |
| 2833 | */ |
| 2834 | list_for_each_entry(event_notifier_priv, &event_notifier_group->event_notifiers_head, node) { |
| 2835 | struct lttng_kernel_event_notifier *event_notifier = event_notifier_priv->pub; |
| 2836 | struct lttng_enabler_ref *enabler_ref; |
| 2837 | struct lttng_kernel_bytecode_runtime *runtime; |
| 2838 | int enabled = 0, has_enablers_without_filter_bytecode = 0; |
| 2839 | int nr_filters = 0, nr_captures = 0; |
| 2840 | |
| 2841 | switch (event_notifier_priv->parent.instrumentation) { |
| 2842 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 2843 | lttng_fallthrough; |
| 2844 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 2845 | /* Enable event_notifiers */ |
| 2846 | list_for_each_entry(enabler_ref, |
| 2847 | &event_notifier_priv->parent.enablers_ref_head, node) { |
| 2848 | if (enabler_ref->ref->enabled) { |
| 2849 | enabled = 1; |
| 2850 | break; |
| 2851 | } |
| 2852 | } |
| 2853 | break; |
| 2854 | |
| 2855 | default: |
| 2856 | /* Not handled with sync. */ |
| 2857 | continue; |
| 2858 | } |
| 2859 | |
| 2860 | WRITE_ONCE(event_notifier->parent.enabled, enabled); |
| 2861 | /* |
| 2862 | * Sync tracepoint registration with event_notifier enabled |
| 2863 | * state. |
| 2864 | */ |
| 2865 | if (enabled) { |
| 2866 | if (!event_notifier_priv->parent.registered) |
| 2867 | register_event_notifier(event_notifier); |
| 2868 | } else { |
| 2869 | if (event_notifier_priv->parent.registered) |
| 2870 | _lttng_event_notifier_unregister(event_notifier); |
| 2871 | } |
| 2872 | |
| 2873 | /* Check if has enablers without bytecode enabled */ |
| 2874 | list_for_each_entry(enabler_ref, |
| 2875 | &event_notifier_priv->parent.enablers_ref_head, node) { |
| 2876 | if (enabler_ref->ref->enabled |
| 2877 | && list_empty(&enabler_ref->ref->filter_bytecode_head)) { |
| 2878 | has_enablers_without_filter_bytecode = 1; |
| 2879 | break; |
| 2880 | } |
| 2881 | } |
| 2882 | event_notifier_priv->parent.has_enablers_without_filter_bytecode = |
| 2883 | has_enablers_without_filter_bytecode; |
| 2884 | |
| 2885 | /* Enable filters */ |
| 2886 | list_for_each_entry(runtime, |
| 2887 | &event_notifier_priv->parent.filter_bytecode_runtime_head, node) { |
| 2888 | lttng_bytecode_sync_state(runtime); |
| 2889 | nr_filters++; |
| 2890 | } |
| 2891 | WRITE_ONCE(event_notifier_priv->parent.pub->eval_filter, |
| 2892 | !(has_enablers_without_filter_bytecode || !nr_filters)); |
| 2893 | |
| 2894 | /* Enable captures */ |
| 2895 | list_for_each_entry(runtime, |
| 2896 | &event_notifier_priv->capture_bytecode_runtime_head, node) { |
| 2897 | lttng_bytecode_sync_state(runtime); |
| 2898 | nr_captures++; |
| 2899 | } |
| 2900 | WRITE_ONCE(event_notifier->eval_capture, !!nr_captures); |
| 2901 | } |
| 2902 | } |
| 2903 | |
| 2904 | static |
| 2905 | void lttng_event_enabler_sync(struct lttng_event_enabler_common *event_enabler) |
| 2906 | { |
| 2907 | switch (event_enabler->enabler_type) { |
| 2908 | case LTTNG_EVENT_ENABLER_TYPE_RECORDER: |
| 2909 | { |
| 2910 | struct lttng_event_recorder_enabler *event_recorder_enabler = |
| 2911 | container_of(event_enabler, struct lttng_event_recorder_enabler, parent); |
| 2912 | lttng_session_lazy_sync_event_enablers(event_recorder_enabler->chan->parent.session); |
| 2913 | break; |
| 2914 | } |
| 2915 | case LTTNG_EVENT_ENABLER_TYPE_NOTIFIER: |
| 2916 | { |
| 2917 | struct lttng_event_notifier_enabler *event_notifier_enabler = |
| 2918 | container_of(event_enabler, struct lttng_event_notifier_enabler, parent); |
| 2919 | lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group); |
| 2920 | break; |
| 2921 | } |
| 2922 | default: |
| 2923 | WARN_ON_ONCE(1); |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | /* |
| 2928 | * Serialize at most one packet worth of metadata into a metadata |
| 2929 | * channel. |
| 2930 | * We grab the metadata cache mutex to get exclusive access to our metadata |
| 2931 | * buffer and to the metadata cache. Exclusive access to the metadata buffer |
| 2932 | * allows us to do racy operations such as looking for remaining space left in |
| 2933 | * packet and write, since mutual exclusion protects us from concurrent writes. |
| 2934 | * Mutual exclusion on the metadata cache allow us to read the cache content |
| 2935 | * without racing against reallocation of the cache by updates. |
| 2936 | * Returns the number of bytes written in the channel, 0 if no data |
| 2937 | * was written and a negative value on error. |
| 2938 | */ |
| 2939 | int lttng_metadata_output_channel(struct lttng_metadata_stream *stream, |
| 2940 | struct lttng_kernel_ring_buffer_channel *chan, bool *coherent) |
| 2941 | { |
| 2942 | struct lttng_kernel_ring_buffer_ctx ctx; |
| 2943 | int ret = 0; |
| 2944 | size_t len, reserve_len; |
| 2945 | |
| 2946 | /* |
| 2947 | * Ensure we support mutiple get_next / put sequences followed by |
| 2948 | * put_next. The metadata cache lock protects reading the metadata |
| 2949 | * cache. It can indeed be read concurrently by "get_next_subbuf" and |
| 2950 | * "flush" operations on the buffer invoked by different processes. |
| 2951 | * Moreover, since the metadata cache memory can be reallocated, we |
| 2952 | * need to have exclusive access against updates even though we only |
| 2953 | * read it. |
| 2954 | */ |
| 2955 | mutex_lock(&stream->metadata_cache->lock); |
| 2956 | WARN_ON(stream->metadata_in < stream->metadata_out); |
| 2957 | if (stream->metadata_in != stream->metadata_out) |
| 2958 | goto end; |
| 2959 | |
| 2960 | /* Metadata regenerated, change the version. */ |
| 2961 | if (stream->metadata_cache->version != stream->version) |
| 2962 | stream->version = stream->metadata_cache->version; |
| 2963 | |
| 2964 | len = stream->metadata_cache->metadata_written - |
| 2965 | stream->metadata_in; |
| 2966 | if (!len) |
| 2967 | goto end; |
| 2968 | reserve_len = min_t(size_t, |
| 2969 | stream->transport->ops.priv->packet_avail_size(chan), |
| 2970 | len); |
| 2971 | lib_ring_buffer_ctx_init(&ctx, chan, reserve_len, |
| 2972 | sizeof(char), NULL); |
| 2973 | /* |
| 2974 | * If reservation failed, return an error to the caller. |
| 2975 | */ |
| 2976 | ret = stream->transport->ops.event_reserve(&ctx); |
| 2977 | if (ret != 0) { |
| 2978 | printk(KERN_WARNING "LTTng: Metadata event reservation failed\n"); |
| 2979 | stream->coherent = false; |
| 2980 | goto end; |
| 2981 | } |
| 2982 | stream->transport->ops.event_write(&ctx, |
| 2983 | stream->metadata_cache->data + stream->metadata_in, |
| 2984 | reserve_len, 1); |
| 2985 | stream->transport->ops.event_commit(&ctx); |
| 2986 | stream->metadata_in += reserve_len; |
| 2987 | if (reserve_len < len) |
| 2988 | stream->coherent = false; |
| 2989 | else |
| 2990 | stream->coherent = true; |
| 2991 | ret = reserve_len; |
| 2992 | |
| 2993 | end: |
| 2994 | if (coherent) |
| 2995 | *coherent = stream->coherent; |
| 2996 | mutex_unlock(&stream->metadata_cache->lock); |
| 2997 | return ret; |
| 2998 | } |
| 2999 | |
| 3000 | static |
| 3001 | void lttng_metadata_begin(struct lttng_kernel_session *session) |
| 3002 | { |
| 3003 | if (atomic_inc_return(&session->priv->metadata_cache->producing) == 1) |
| 3004 | mutex_lock(&session->priv->metadata_cache->lock); |
| 3005 | } |
| 3006 | |
| 3007 | static |
| 3008 | void lttng_metadata_end(struct lttng_kernel_session *session) |
| 3009 | { |
| 3010 | WARN_ON_ONCE(!atomic_read(&session->priv->metadata_cache->producing)); |
| 3011 | if (atomic_dec_return(&session->priv->metadata_cache->producing) == 0) { |
| 3012 | struct lttng_metadata_stream *stream; |
| 3013 | |
| 3014 | list_for_each_entry(stream, &session->priv->metadata_cache->metadata_stream, list) |
| 3015 | wake_up_interruptible(&stream->read_wait); |
| 3016 | mutex_unlock(&session->priv->metadata_cache->lock); |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | /* |
| 3021 | * Write the metadata to the metadata cache. |
| 3022 | * Must be called with sessions_mutex held. |
| 3023 | * The metadata cache lock protects us from concurrent read access from |
| 3024 | * thread outputting metadata content to ring buffer. |
| 3025 | * The content of the printf is printed as a single atomic metadata |
| 3026 | * transaction. |
| 3027 | */ |
| 3028 | int lttng_metadata_printf(struct lttng_kernel_session *session, |
| 3029 | const char *fmt, ...) |
| 3030 | { |
| 3031 | char *str; |
| 3032 | size_t len; |
| 3033 | va_list ap; |
| 3034 | |
| 3035 | WARN_ON_ONCE(!LTTNG_READ_ONCE(session->active)); |
| 3036 | |
| 3037 | va_start(ap, fmt); |
| 3038 | str = kvasprintf(GFP_KERNEL, fmt, ap); |
| 3039 | va_end(ap); |
| 3040 | if (!str) |
| 3041 | return -ENOMEM; |
| 3042 | |
| 3043 | len = strlen(str); |
| 3044 | WARN_ON_ONCE(!atomic_read(&session->priv->metadata_cache->producing)); |
| 3045 | if (session->priv->metadata_cache->metadata_written + len > |
| 3046 | session->priv->metadata_cache->cache_alloc) { |
| 3047 | char *tmp_cache_realloc; |
| 3048 | unsigned int tmp_cache_alloc_size; |
| 3049 | |
| 3050 | tmp_cache_alloc_size = max_t(unsigned int, |
| 3051 | session->priv->metadata_cache->cache_alloc + len, |
| 3052 | session->priv->metadata_cache->cache_alloc << 1); |
| 3053 | tmp_cache_realloc = vzalloc(tmp_cache_alloc_size); |
| 3054 | if (!tmp_cache_realloc) |
| 3055 | goto err; |
| 3056 | if (session->priv->metadata_cache->data) { |
| 3057 | memcpy(tmp_cache_realloc, |
| 3058 | session->priv->metadata_cache->data, |
| 3059 | session->priv->metadata_cache->cache_alloc); |
| 3060 | vfree(session->priv->metadata_cache->data); |
| 3061 | } |
| 3062 | |
| 3063 | session->priv->metadata_cache->cache_alloc = tmp_cache_alloc_size; |
| 3064 | session->priv->metadata_cache->data = tmp_cache_realloc; |
| 3065 | } |
| 3066 | memcpy(session->priv->metadata_cache->data + |
| 3067 | session->priv->metadata_cache->metadata_written, |
| 3068 | str, len); |
| 3069 | session->priv->metadata_cache->metadata_written += len; |
| 3070 | kfree(str); |
| 3071 | |
| 3072 | return 0; |
| 3073 | |
| 3074 | err: |
| 3075 | kfree(str); |
| 3076 | return -ENOMEM; |
| 3077 | } |
| 3078 | |
| 3079 | static |
| 3080 | int print_tabs(struct lttng_kernel_session *session, size_t nesting) |
| 3081 | { |
| 3082 | size_t i; |
| 3083 | |
| 3084 | for (i = 0; i < nesting; i++) { |
| 3085 | int ret; |
| 3086 | |
| 3087 | ret = lttng_metadata_printf(session, " "); |
| 3088 | if (ret) { |
| 3089 | return ret; |
| 3090 | } |
| 3091 | } |
| 3092 | return 0; |
| 3093 | } |
| 3094 | |
| 3095 | static |
| 3096 | int lttng_field_name_statedump(struct lttng_kernel_session *session, |
| 3097 | const struct lttng_kernel_event_field *field, |
| 3098 | size_t nesting) |
| 3099 | { |
| 3100 | return lttng_metadata_printf(session, " _%s;\n", field->name); |
| 3101 | } |
| 3102 | |
| 3103 | static |
| 3104 | int _lttng_integer_type_statedump(struct lttng_kernel_session *session, |
| 3105 | const struct lttng_kernel_type_integer *type, |
| 3106 | enum lttng_kernel_string_encoding parent_encoding, |
| 3107 | size_t nesting) |
| 3108 | { |
| 3109 | int ret; |
| 3110 | |
| 3111 | ret = print_tabs(session, nesting); |
| 3112 | if (ret) |
| 3113 | return ret; |
| 3114 | ret = lttng_metadata_printf(session, |
| 3115 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s }", |
| 3116 | type->size, |
| 3117 | type->alignment, |
| 3118 | type->signedness, |
| 3119 | (parent_encoding == lttng_kernel_string_encoding_none) |
| 3120 | ? "none" |
| 3121 | : (parent_encoding == lttng_kernel_string_encoding_UTF8) |
| 3122 | ? "UTF8" |
| 3123 | : "ASCII", |
| 3124 | type->base, |
| 3125 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 3126 | type->reverse_byte_order ? " byte_order = le;" : "" |
| 3127 | #else |
| 3128 | type->reverse_byte_order ? " byte_order = be;" : "" |
| 3129 | #endif |
| 3130 | ); |
| 3131 | return ret; |
| 3132 | } |
| 3133 | |
| 3134 | /* |
| 3135 | * Must be called with sessions_mutex held. |
| 3136 | */ |
| 3137 | static |
| 3138 | int _lttng_struct_type_statedump(struct lttng_kernel_session *session, |
| 3139 | const struct lttng_kernel_type_struct *type, |
| 3140 | size_t nesting) |
| 3141 | { |
| 3142 | const char *prev_field_name = NULL; |
| 3143 | int ret; |
| 3144 | uint32_t i, nr_fields; |
| 3145 | unsigned int alignment; |
| 3146 | |
| 3147 | ret = print_tabs(session, nesting); |
| 3148 | if (ret) |
| 3149 | return ret; |
| 3150 | ret = lttng_metadata_printf(session, |
| 3151 | "struct {\n"); |
| 3152 | if (ret) |
| 3153 | return ret; |
| 3154 | nr_fields = type->nr_fields; |
| 3155 | for (i = 0; i < nr_fields; i++) { |
| 3156 | const struct lttng_kernel_event_field *iter_field; |
| 3157 | |
| 3158 | iter_field = type->fields[i]; |
| 3159 | ret = _lttng_field_statedump(session, iter_field, nesting + 1, &prev_field_name); |
| 3160 | if (ret) |
| 3161 | return ret; |
| 3162 | } |
| 3163 | ret = print_tabs(session, nesting); |
| 3164 | if (ret) |
| 3165 | return ret; |
| 3166 | alignment = type->alignment; |
| 3167 | if (alignment) { |
| 3168 | ret = lttng_metadata_printf(session, |
| 3169 | "} align(%u)", |
| 3170 | alignment); |
| 3171 | } else { |
| 3172 | ret = lttng_metadata_printf(session, |
| 3173 | "}"); |
| 3174 | } |
| 3175 | return ret; |
| 3176 | } |
| 3177 | |
| 3178 | /* |
| 3179 | * Must be called with sessions_mutex held. |
| 3180 | */ |
| 3181 | static |
| 3182 | int _lttng_struct_field_statedump(struct lttng_kernel_session *session, |
| 3183 | const struct lttng_kernel_event_field *field, |
| 3184 | size_t nesting) |
| 3185 | { |
| 3186 | int ret; |
| 3187 | |
| 3188 | ret = _lttng_struct_type_statedump(session, |
| 3189 | lttng_kernel_get_type_struct(field->type), nesting); |
| 3190 | if (ret) |
| 3191 | return ret; |
| 3192 | return lttng_field_name_statedump(session, field, nesting); |
| 3193 | } |
| 3194 | |
| 3195 | /* |
| 3196 | * Must be called with sessions_mutex held. |
| 3197 | */ |
| 3198 | static |
| 3199 | int _lttng_variant_type_statedump(struct lttng_kernel_session *session, |
| 3200 | const struct lttng_kernel_type_variant *type, |
| 3201 | size_t nesting, |
| 3202 | const char *prev_field_name) |
| 3203 | { |
| 3204 | const char *tag_name; |
| 3205 | int ret; |
| 3206 | uint32_t i, nr_choices; |
| 3207 | |
| 3208 | tag_name = type->tag_name; |
| 3209 | if (!tag_name) |
| 3210 | tag_name = prev_field_name; |
| 3211 | if (!tag_name) |
| 3212 | return -EINVAL; |
| 3213 | /* |
| 3214 | * CTF 1.8 does not allow expressing nonzero variant alignment in a nestable way. |
| 3215 | */ |
| 3216 | if (type->alignment != 0) |
| 3217 | return -EINVAL; |
| 3218 | ret = print_tabs(session, nesting); |
| 3219 | if (ret) |
| 3220 | return ret; |
| 3221 | ret = lttng_metadata_printf(session, |
| 3222 | "variant <_%s> {\n", |
| 3223 | tag_name); |
| 3224 | if (ret) |
| 3225 | return ret; |
| 3226 | nr_choices = type->nr_choices; |
| 3227 | for (i = 0; i < nr_choices; i++) { |
| 3228 | const struct lttng_kernel_event_field *iter_field; |
| 3229 | |
| 3230 | iter_field = type->choices[i]; |
| 3231 | ret = _lttng_field_statedump(session, iter_field, nesting + 1, NULL); |
| 3232 | if (ret) |
| 3233 | return ret; |
| 3234 | } |
| 3235 | ret = print_tabs(session, nesting); |
| 3236 | if (ret) |
| 3237 | return ret; |
| 3238 | ret = lttng_metadata_printf(session, |
| 3239 | "}"); |
| 3240 | return ret; |
| 3241 | } |
| 3242 | |
| 3243 | /* |
| 3244 | * Must be called with sessions_mutex held. |
| 3245 | */ |
| 3246 | static |
| 3247 | int _lttng_variant_field_statedump(struct lttng_kernel_session *session, |
| 3248 | const struct lttng_kernel_event_field *field, |
| 3249 | size_t nesting, |
| 3250 | const char *prev_field_name) |
| 3251 | { |
| 3252 | int ret; |
| 3253 | |
| 3254 | ret = _lttng_variant_type_statedump(session, |
| 3255 | lttng_kernel_get_type_variant(field->type), nesting, |
| 3256 | prev_field_name); |
| 3257 | if (ret) |
| 3258 | return ret; |
| 3259 | return lttng_field_name_statedump(session, field, nesting); |
| 3260 | } |
| 3261 | |
| 3262 | /* |
| 3263 | * Must be called with sessions_mutex held. |
| 3264 | */ |
| 3265 | static |
| 3266 | int _lttng_array_field_statedump(struct lttng_kernel_session *session, |
| 3267 | const struct lttng_kernel_event_field *field, |
| 3268 | size_t nesting) |
| 3269 | { |
| 3270 | int ret; |
| 3271 | const struct lttng_kernel_type_array *array_type; |
| 3272 | const struct lttng_kernel_type_common *elem_type; |
| 3273 | |
| 3274 | array_type = lttng_kernel_get_type_array(field->type); |
| 3275 | WARN_ON_ONCE(!array_type); |
| 3276 | |
| 3277 | if (array_type->alignment) { |
| 3278 | ret = print_tabs(session, nesting); |
| 3279 | if (ret) |
| 3280 | return ret; |
| 3281 | ret = lttng_metadata_printf(session, |
| 3282 | "struct { } align(%u) _%s_padding;\n", |
| 3283 | array_type->alignment * CHAR_BIT, |
| 3284 | field->name); |
| 3285 | if (ret) |
| 3286 | return ret; |
| 3287 | } |
| 3288 | /* |
| 3289 | * Nested compound types: Only array of structures and variants are |
| 3290 | * currently supported. |
| 3291 | */ |
| 3292 | elem_type = array_type->elem_type; |
| 3293 | switch (elem_type->type) { |
| 3294 | case lttng_kernel_type_integer: |
| 3295 | case lttng_kernel_type_struct: |
| 3296 | case lttng_kernel_type_variant: |
| 3297 | ret = _lttng_type_statedump(session, elem_type, |
| 3298 | array_type->encoding, nesting); |
| 3299 | if (ret) |
| 3300 | return ret; |
| 3301 | break; |
| 3302 | |
| 3303 | default: |
| 3304 | return -EINVAL; |
| 3305 | } |
| 3306 | ret = lttng_metadata_printf(session, |
| 3307 | " _%s[%u];\n", |
| 3308 | field->name, |
| 3309 | array_type->length); |
| 3310 | return ret; |
| 3311 | } |
| 3312 | |
| 3313 | /* |
| 3314 | * Must be called with sessions_mutex held. |
| 3315 | */ |
| 3316 | static |
| 3317 | int _lttng_sequence_field_statedump(struct lttng_kernel_session *session, |
| 3318 | const struct lttng_kernel_event_field *field, |
| 3319 | size_t nesting, |
| 3320 | const char *prev_field_name) |
| 3321 | { |
| 3322 | int ret; |
| 3323 | const char *length_name; |
| 3324 | const struct lttng_kernel_type_sequence *sequence_type; |
| 3325 | const struct lttng_kernel_type_common *elem_type; |
| 3326 | |
| 3327 | sequence_type = lttng_kernel_get_type_sequence(field->type); |
| 3328 | WARN_ON_ONCE(!sequence_type); |
| 3329 | |
| 3330 | length_name = sequence_type->length_name; |
| 3331 | if (!length_name) |
| 3332 | length_name = prev_field_name; |
| 3333 | if (!length_name) |
| 3334 | return -EINVAL; |
| 3335 | |
| 3336 | if (sequence_type->alignment) { |
| 3337 | ret = print_tabs(session, nesting); |
| 3338 | if (ret) |
| 3339 | return ret; |
| 3340 | ret = lttng_metadata_printf(session, |
| 3341 | "struct { } align(%u) _%s_padding;\n", |
| 3342 | sequence_type->alignment * CHAR_BIT, |
| 3343 | field->name); |
| 3344 | if (ret) |
| 3345 | return ret; |
| 3346 | } |
| 3347 | |
| 3348 | /* |
| 3349 | * Nested compound types: Only array of structures and variants are |
| 3350 | * currently supported. |
| 3351 | */ |
| 3352 | elem_type = sequence_type->elem_type; |
| 3353 | switch (elem_type->type) { |
| 3354 | case lttng_kernel_type_integer: |
| 3355 | case lttng_kernel_type_struct: |
| 3356 | case lttng_kernel_type_variant: |
| 3357 | ret = _lttng_type_statedump(session, elem_type, |
| 3358 | sequence_type->encoding, nesting); |
| 3359 | if (ret) |
| 3360 | return ret; |
| 3361 | break; |
| 3362 | |
| 3363 | default: |
| 3364 | return -EINVAL; |
| 3365 | } |
| 3366 | ret = lttng_metadata_printf(session, |
| 3367 | " _%s[ _%s ];\n", |
| 3368 | field->name, |
| 3369 | length_name); |
| 3370 | return ret; |
| 3371 | } |
| 3372 | |
| 3373 | /* |
| 3374 | * Must be called with sessions_mutex held. |
| 3375 | */ |
| 3376 | static |
| 3377 | int _lttng_enum_type_statedump(struct lttng_kernel_session *session, |
| 3378 | const struct lttng_kernel_type_enum *type, |
| 3379 | size_t nesting) |
| 3380 | { |
| 3381 | const struct lttng_kernel_enum_desc *enum_desc; |
| 3382 | const struct lttng_kernel_type_common *container_type; |
| 3383 | int ret; |
| 3384 | unsigned int i, nr_entries; |
| 3385 | |
| 3386 | container_type = type->container_type; |
| 3387 | if (container_type->type != lttng_kernel_type_integer) { |
| 3388 | ret = -EINVAL; |
| 3389 | goto end; |
| 3390 | } |
| 3391 | enum_desc = type->desc; |
| 3392 | nr_entries = enum_desc->nr_entries; |
| 3393 | |
| 3394 | ret = print_tabs(session, nesting); |
| 3395 | if (ret) |
| 3396 | goto end; |
| 3397 | ret = lttng_metadata_printf(session, "enum : "); |
| 3398 | if (ret) |
| 3399 | goto end; |
| 3400 | ret = _lttng_integer_type_statedump(session, lttng_kernel_get_type_integer(container_type), |
| 3401 | lttng_kernel_string_encoding_none, 0); |
| 3402 | if (ret) |
| 3403 | goto end; |
| 3404 | ret = lttng_metadata_printf(session, " {\n"); |
| 3405 | if (ret) |
| 3406 | goto end; |
| 3407 | /* Dump all entries */ |
| 3408 | for (i = 0; i < nr_entries; i++) { |
| 3409 | const struct lttng_kernel_enum_entry *entry = enum_desc->entries[i]; |
| 3410 | int j, len; |
| 3411 | |
| 3412 | ret = print_tabs(session, nesting + 1); |
| 3413 | if (ret) |
| 3414 | goto end; |
| 3415 | ret = lttng_metadata_printf(session, |
| 3416 | "\""); |
| 3417 | if (ret) |
| 3418 | goto end; |
| 3419 | len = strlen(entry->string); |
| 3420 | /* Escape the character '"' */ |
| 3421 | for (j = 0; j < len; j++) { |
| 3422 | char c = entry->string[j]; |
| 3423 | |
| 3424 | switch (c) { |
| 3425 | case '"': |
| 3426 | ret = lttng_metadata_printf(session, |
| 3427 | "\\\""); |
| 3428 | break; |
| 3429 | case '\\': |
| 3430 | ret = lttng_metadata_printf(session, |
| 3431 | "\\\\"); |
| 3432 | break; |
| 3433 | default: |
| 3434 | ret = lttng_metadata_printf(session, |
| 3435 | "%c", c); |
| 3436 | break; |
| 3437 | } |
| 3438 | if (ret) |
| 3439 | goto end; |
| 3440 | } |
| 3441 | ret = lttng_metadata_printf(session, "\""); |
| 3442 | if (ret) |
| 3443 | goto end; |
| 3444 | |
| 3445 | if (entry->options.is_auto) { |
| 3446 | ret = lttng_metadata_printf(session, ",\n"); |
| 3447 | if (ret) |
| 3448 | goto end; |
| 3449 | } else { |
| 3450 | ret = lttng_metadata_printf(session, |
| 3451 | " = "); |
| 3452 | if (ret) |
| 3453 | goto end; |
| 3454 | if (entry->start.signedness) |
| 3455 | ret = lttng_metadata_printf(session, |
| 3456 | "%lld", (long long) entry->start.value); |
| 3457 | else |
| 3458 | ret = lttng_metadata_printf(session, |
| 3459 | "%llu", entry->start.value); |
| 3460 | if (ret) |
| 3461 | goto end; |
| 3462 | if (entry->start.signedness == entry->end.signedness && |
| 3463 | entry->start.value |
| 3464 | == entry->end.value) { |
| 3465 | ret = lttng_metadata_printf(session, |
| 3466 | ",\n"); |
| 3467 | } else { |
| 3468 | if (entry->end.signedness) { |
| 3469 | ret = lttng_metadata_printf(session, |
| 3470 | " ... %lld,\n", |
| 3471 | (long long) entry->end.value); |
| 3472 | } else { |
| 3473 | ret = lttng_metadata_printf(session, |
| 3474 | " ... %llu,\n", |
| 3475 | entry->end.value); |
| 3476 | } |
| 3477 | } |
| 3478 | if (ret) |
| 3479 | goto end; |
| 3480 | } |
| 3481 | } |
| 3482 | ret = print_tabs(session, nesting); |
| 3483 | if (ret) |
| 3484 | goto end; |
| 3485 | ret = lttng_metadata_printf(session, "}"); |
| 3486 | end: |
| 3487 | return ret; |
| 3488 | } |
| 3489 | |
| 3490 | /* |
| 3491 | * Must be called with sessions_mutex held. |
| 3492 | */ |
| 3493 | static |
| 3494 | int _lttng_enum_field_statedump(struct lttng_kernel_session *session, |
| 3495 | const struct lttng_kernel_event_field *field, |
| 3496 | size_t nesting) |
| 3497 | { |
| 3498 | int ret; |
| 3499 | const struct lttng_kernel_type_enum *enum_type; |
| 3500 | |
| 3501 | enum_type = lttng_kernel_get_type_enum(field->type); |
| 3502 | WARN_ON_ONCE(!enum_type); |
| 3503 | ret = _lttng_enum_type_statedump(session, enum_type, nesting); |
| 3504 | if (ret) |
| 3505 | return ret; |
| 3506 | return lttng_field_name_statedump(session, field, nesting); |
| 3507 | } |
| 3508 | |
| 3509 | static |
| 3510 | int _lttng_integer_field_statedump(struct lttng_kernel_session *session, |
| 3511 | const struct lttng_kernel_event_field *field, |
| 3512 | size_t nesting) |
| 3513 | { |
| 3514 | int ret; |
| 3515 | |
| 3516 | ret = _lttng_integer_type_statedump(session, lttng_kernel_get_type_integer(field->type), |
| 3517 | lttng_kernel_string_encoding_none, nesting); |
| 3518 | if (ret) |
| 3519 | return ret; |
| 3520 | return lttng_field_name_statedump(session, field, nesting); |
| 3521 | } |
| 3522 | |
| 3523 | static |
| 3524 | int _lttng_string_type_statedump(struct lttng_kernel_session *session, |
| 3525 | const struct lttng_kernel_type_string *type, |
| 3526 | size_t nesting) |
| 3527 | { |
| 3528 | int ret; |
| 3529 | |
| 3530 | /* Default encoding is UTF8 */ |
| 3531 | ret = print_tabs(session, nesting); |
| 3532 | if (ret) |
| 3533 | return ret; |
| 3534 | ret = lttng_metadata_printf(session, |
| 3535 | "string%s", |
| 3536 | type->encoding == lttng_kernel_string_encoding_ASCII ? |
| 3537 | " { encoding = ASCII; }" : ""); |
| 3538 | return ret; |
| 3539 | } |
| 3540 | |
| 3541 | static |
| 3542 | int _lttng_string_field_statedump(struct lttng_kernel_session *session, |
| 3543 | const struct lttng_kernel_event_field *field, |
| 3544 | size_t nesting) |
| 3545 | { |
| 3546 | const struct lttng_kernel_type_string *string_type; |
| 3547 | int ret; |
| 3548 | |
| 3549 | string_type = lttng_kernel_get_type_string(field->type); |
| 3550 | WARN_ON_ONCE(!string_type); |
| 3551 | ret = _lttng_string_type_statedump(session, string_type, nesting); |
| 3552 | if (ret) |
| 3553 | return ret; |
| 3554 | return lttng_field_name_statedump(session, field, nesting); |
| 3555 | } |
| 3556 | |
| 3557 | /* |
| 3558 | * Must be called with sessions_mutex held. |
| 3559 | */ |
| 3560 | static |
| 3561 | int _lttng_type_statedump(struct lttng_kernel_session *session, |
| 3562 | const struct lttng_kernel_type_common *type, |
| 3563 | enum lttng_kernel_string_encoding parent_encoding, |
| 3564 | size_t nesting) |
| 3565 | { |
| 3566 | int ret = 0; |
| 3567 | |
| 3568 | switch (type->type) { |
| 3569 | case lttng_kernel_type_integer: |
| 3570 | ret = _lttng_integer_type_statedump(session, |
| 3571 | lttng_kernel_get_type_integer(type), |
| 3572 | parent_encoding, nesting); |
| 3573 | break; |
| 3574 | case lttng_kernel_type_enum: |
| 3575 | ret = _lttng_enum_type_statedump(session, |
| 3576 | lttng_kernel_get_type_enum(type), |
| 3577 | nesting); |
| 3578 | break; |
| 3579 | case lttng_kernel_type_string: |
| 3580 | ret = _lttng_string_type_statedump(session, |
| 3581 | lttng_kernel_get_type_string(type), |
| 3582 | nesting); |
| 3583 | break; |
| 3584 | case lttng_kernel_type_struct: |
| 3585 | ret = _lttng_struct_type_statedump(session, |
| 3586 | lttng_kernel_get_type_struct(type), |
| 3587 | nesting); |
| 3588 | break; |
| 3589 | case lttng_kernel_type_variant: |
| 3590 | ret = _lttng_variant_type_statedump(session, |
| 3591 | lttng_kernel_get_type_variant(type), |
| 3592 | nesting, NULL); |
| 3593 | break; |
| 3594 | |
| 3595 | /* Nested arrays and sequences are not supported yet. */ |
| 3596 | case lttng_kernel_type_array: |
| 3597 | case lttng_kernel_type_sequence: |
| 3598 | default: |
| 3599 | WARN_ON_ONCE(1); |
| 3600 | return -EINVAL; |
| 3601 | } |
| 3602 | return ret; |
| 3603 | } |
| 3604 | |
| 3605 | /* |
| 3606 | * Must be called with sessions_mutex held. |
| 3607 | */ |
| 3608 | static |
| 3609 | int _lttng_field_statedump(struct lttng_kernel_session *session, |
| 3610 | const struct lttng_kernel_event_field *field, |
| 3611 | size_t nesting, |
| 3612 | const char **prev_field_name_p) |
| 3613 | { |
| 3614 | const char *prev_field_name = NULL; |
| 3615 | int ret = 0; |
| 3616 | |
| 3617 | if (prev_field_name_p) |
| 3618 | prev_field_name = *prev_field_name_p; |
| 3619 | switch (field->type->type) { |
| 3620 | case lttng_kernel_type_integer: |
| 3621 | ret = _lttng_integer_field_statedump(session, field, nesting); |
| 3622 | break; |
| 3623 | case lttng_kernel_type_enum: |
| 3624 | ret = _lttng_enum_field_statedump(session, field, nesting); |
| 3625 | break; |
| 3626 | case lttng_kernel_type_string: |
| 3627 | ret = _lttng_string_field_statedump(session, field, nesting); |
| 3628 | break; |
| 3629 | case lttng_kernel_type_struct: |
| 3630 | ret = _lttng_struct_field_statedump(session, field, nesting); |
| 3631 | break; |
| 3632 | case lttng_kernel_type_array: |
| 3633 | ret = _lttng_array_field_statedump(session, field, nesting); |
| 3634 | break; |
| 3635 | case lttng_kernel_type_sequence: |
| 3636 | ret = _lttng_sequence_field_statedump(session, field, nesting, prev_field_name); |
| 3637 | break; |
| 3638 | case lttng_kernel_type_variant: |
| 3639 | ret = _lttng_variant_field_statedump(session, field, nesting, prev_field_name); |
| 3640 | break; |
| 3641 | |
| 3642 | default: |
| 3643 | WARN_ON_ONCE(1); |
| 3644 | return -EINVAL; |
| 3645 | } |
| 3646 | if (prev_field_name_p) |
| 3647 | *prev_field_name_p = field->name; |
| 3648 | return ret; |
| 3649 | } |
| 3650 | |
| 3651 | static |
| 3652 | int _lttng_context_metadata_statedump(struct lttng_kernel_session *session, |
| 3653 | struct lttng_kernel_ctx *ctx) |
| 3654 | { |
| 3655 | const char *prev_field_name = NULL; |
| 3656 | int ret = 0; |
| 3657 | int i; |
| 3658 | |
| 3659 | if (!ctx) |
| 3660 | return 0; |
| 3661 | for (i = 0; i < ctx->nr_fields; i++) { |
| 3662 | const struct lttng_kernel_ctx_field *field = &ctx->fields[i]; |
| 3663 | |
| 3664 | ret = _lttng_field_statedump(session, field->event_field, 2, &prev_field_name); |
| 3665 | if (ret) |
| 3666 | return ret; |
| 3667 | } |
| 3668 | return ret; |
| 3669 | } |
| 3670 | |
| 3671 | static |
| 3672 | int _lttng_fields_metadata_statedump(struct lttng_kernel_session *session, |
| 3673 | struct lttng_kernel_event_recorder *event_recorder) |
| 3674 | { |
| 3675 | const char *prev_field_name = NULL; |
| 3676 | const struct lttng_kernel_event_desc *desc = event_recorder->priv->parent.desc; |
| 3677 | int ret = 0; |
| 3678 | int i; |
| 3679 | |
| 3680 | for (i = 0; i < desc->tp_class->nr_fields; i++) { |
| 3681 | const struct lttng_kernel_event_field *field = desc->tp_class->fields[i]; |
| 3682 | |
| 3683 | ret = _lttng_field_statedump(session, field, 2, &prev_field_name); |
| 3684 | if (ret) |
| 3685 | return ret; |
| 3686 | } |
| 3687 | return ret; |
| 3688 | } |
| 3689 | |
| 3690 | /* |
| 3691 | * Must be called with sessions_mutex held. |
| 3692 | * The entire event metadata is printed as a single atomic metadata |
| 3693 | * transaction. |
| 3694 | */ |
| 3695 | static |
| 3696 | int _lttng_event_metadata_statedump(struct lttng_kernel_session *session, |
| 3697 | struct lttng_kernel_channel_buffer *chan, |
| 3698 | struct lttng_kernel_event_recorder *event_recorder) |
| 3699 | { |
| 3700 | int ret = 0; |
| 3701 | |
| 3702 | if (event_recorder->priv->metadata_dumped || !LTTNG_READ_ONCE(session->active)) |
| 3703 | return 0; |
| 3704 | if (chan->priv->channel_type == METADATA_CHANNEL) |
| 3705 | return 0; |
| 3706 | |
| 3707 | lttng_metadata_begin(session); |
| 3708 | |
| 3709 | ret = lttng_metadata_printf(session, |
| 3710 | "event {\n" |
| 3711 | " name = \"%s\";\n" |
| 3712 | " id = %u;\n" |
| 3713 | " stream_id = %u;\n", |
| 3714 | event_recorder->priv->parent.desc->event_name, |
| 3715 | event_recorder->priv->id, |
| 3716 | event_recorder->chan->priv->id); |
| 3717 | if (ret) |
| 3718 | goto end; |
| 3719 | |
| 3720 | ret = lttng_metadata_printf(session, |
| 3721 | " fields := struct {\n" |
| 3722 | ); |
| 3723 | if (ret) |
| 3724 | goto end; |
| 3725 | |
| 3726 | ret = _lttng_fields_metadata_statedump(session, event_recorder); |
| 3727 | if (ret) |
| 3728 | goto end; |
| 3729 | |
| 3730 | /* |
| 3731 | * LTTng space reservation can only reserve multiples of the |
| 3732 | * byte size. |
| 3733 | */ |
| 3734 | ret = lttng_metadata_printf(session, |
| 3735 | " };\n" |
| 3736 | "};\n\n"); |
| 3737 | if (ret) |
| 3738 | goto end; |
| 3739 | |
| 3740 | event_recorder->priv->metadata_dumped = 1; |
| 3741 | end: |
| 3742 | lttng_metadata_end(session); |
| 3743 | return ret; |
| 3744 | |
| 3745 | } |
| 3746 | |
| 3747 | /* |
| 3748 | * Must be called with sessions_mutex held. |
| 3749 | * The entire channel metadata is printed as a single atomic metadata |
| 3750 | * transaction. |
| 3751 | */ |
| 3752 | static |
| 3753 | int _lttng_channel_metadata_statedump(struct lttng_kernel_session *session, |
| 3754 | struct lttng_kernel_channel_buffer *chan) |
| 3755 | { |
| 3756 | int ret = 0; |
| 3757 | |
| 3758 | if (chan->priv->metadata_dumped || !LTTNG_READ_ONCE(session->active)) |
| 3759 | return 0; |
| 3760 | |
| 3761 | if (chan->priv->channel_type == METADATA_CHANNEL) |
| 3762 | return 0; |
| 3763 | |
| 3764 | lttng_metadata_begin(session); |
| 3765 | |
| 3766 | WARN_ON_ONCE(!chan->priv->header_type); |
| 3767 | ret = lttng_metadata_printf(session, |
| 3768 | "stream {\n" |
| 3769 | " id = %u;\n" |
| 3770 | " event.header := %s;\n" |
| 3771 | " packet.context := struct packet_context;\n", |
| 3772 | chan->priv->id, |
| 3773 | chan->priv->header_type == 1 ? "struct event_header_compact" : |
| 3774 | "struct event_header_large"); |
| 3775 | if (ret) |
| 3776 | goto end; |
| 3777 | |
| 3778 | if (chan->priv->ctx) { |
| 3779 | ret = lttng_metadata_printf(session, |
| 3780 | " event.context := struct {\n"); |
| 3781 | if (ret) |
| 3782 | goto end; |
| 3783 | } |
| 3784 | ret = _lttng_context_metadata_statedump(session, chan->priv->ctx); |
| 3785 | if (ret) |
| 3786 | goto end; |
| 3787 | if (chan->priv->ctx) { |
| 3788 | ret = lttng_metadata_printf(session, |
| 3789 | " };\n"); |
| 3790 | if (ret) |
| 3791 | goto end; |
| 3792 | } |
| 3793 | |
| 3794 | ret = lttng_metadata_printf(session, |
| 3795 | "};\n\n"); |
| 3796 | |
| 3797 | chan->priv->metadata_dumped = 1; |
| 3798 | end: |
| 3799 | lttng_metadata_end(session); |
| 3800 | return ret; |
| 3801 | } |
| 3802 | |
| 3803 | /* |
| 3804 | * Must be called with sessions_mutex held. |
| 3805 | */ |
| 3806 | static |
| 3807 | int _lttng_stream_packet_context_declare(struct lttng_kernel_session *session) |
| 3808 | { |
| 3809 | return lttng_metadata_printf(session, |
| 3810 | "struct packet_context {\n" |
| 3811 | " uint64_clock_monotonic_t timestamp_begin;\n" |
| 3812 | " uint64_clock_monotonic_t timestamp_end;\n" |
| 3813 | " uint64_t content_size;\n" |
| 3814 | " uint64_t packet_size;\n" |
| 3815 | " uint64_t packet_seq_num;\n" |
| 3816 | " unsigned long events_discarded;\n" |
| 3817 | " uint32_t cpu_id;\n" |
| 3818 | "};\n\n" |
| 3819 | ); |
| 3820 | } |
| 3821 | |
| 3822 | /* |
| 3823 | * Compact header: |
| 3824 | * id: range: 0 - 30. |
| 3825 | * id 31 is reserved to indicate an extended header. |
| 3826 | * |
| 3827 | * Large header: |
| 3828 | * id: range: 0 - 65534. |
| 3829 | * id 65535 is reserved to indicate an extended header. |
| 3830 | * |
| 3831 | * Must be called with sessions_mutex held. |
| 3832 | */ |
| 3833 | static |
| 3834 | int _lttng_event_header_declare(struct lttng_kernel_session *session) |
| 3835 | { |
| 3836 | return lttng_metadata_printf(session, |
| 3837 | "struct event_header_compact {\n" |
| 3838 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" |
| 3839 | " variant <id> {\n" |
| 3840 | " struct {\n" |
| 3841 | " uint27_clock_monotonic_t timestamp;\n" |
| 3842 | " } compact;\n" |
| 3843 | " struct {\n" |
| 3844 | " uint32_t id;\n" |
| 3845 | " uint64_clock_monotonic_t timestamp;\n" |
| 3846 | " } extended;\n" |
| 3847 | " } v;\n" |
| 3848 | "} align(%u);\n" |
| 3849 | "\n" |
| 3850 | "struct event_header_large {\n" |
| 3851 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" |
| 3852 | " variant <id> {\n" |
| 3853 | " struct {\n" |
| 3854 | " uint32_clock_monotonic_t timestamp;\n" |
| 3855 | " } compact;\n" |
| 3856 | " struct {\n" |
| 3857 | " uint32_t id;\n" |
| 3858 | " uint64_clock_monotonic_t timestamp;\n" |
| 3859 | " } extended;\n" |
| 3860 | " } v;\n" |
| 3861 | "} align(%u);\n\n", |
| 3862 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 3863 | lttng_alignof(uint16_t) * CHAR_BIT |
| 3864 | ); |
| 3865 | } |
| 3866 | |
| 3867 | /* |
| 3868 | * Approximation of NTP time of day to clock monotonic correlation, |
| 3869 | * taken at start of trace. |
| 3870 | * Yes, this is only an approximation. Yes, we can (and will) do better |
| 3871 | * in future versions. |
| 3872 | * This function may return a negative offset. It may happen if the |
| 3873 | * system sets the REALTIME clock to 0 after boot. |
| 3874 | * |
| 3875 | * Use 64bit timespec on kernels that have it, this makes 32bit arch |
| 3876 | * y2038 compliant. |
| 3877 | */ |
| 3878 | static |
| 3879 | int64_t measure_clock_offset(void) |
| 3880 | { |
| 3881 | uint64_t monotonic_avg, monotonic[2], realtime; |
| 3882 | uint64_t tcf = trace_clock_freq(); |
| 3883 | int64_t offset; |
| 3884 | unsigned long flags; |
| 3885 | #ifdef LTTNG_KERNEL_HAS_TIMESPEC64 |
| 3886 | struct timespec64 rts = { 0, 0 }; |
| 3887 | #else |
| 3888 | struct timespec rts = { 0, 0 }; |
| 3889 | #endif |
| 3890 | |
| 3891 | /* Disable interrupts to increase correlation precision. */ |
| 3892 | local_irq_save(flags); |
| 3893 | monotonic[0] = trace_clock_read64(); |
| 3894 | #ifdef LTTNG_KERNEL_HAS_TIMESPEC64 |
| 3895 | ktime_get_real_ts64(&rts); |
| 3896 | #else |
| 3897 | getnstimeofday(&rts); |
| 3898 | #endif |
| 3899 | monotonic[1] = trace_clock_read64(); |
| 3900 | local_irq_restore(flags); |
| 3901 | |
| 3902 | monotonic_avg = (monotonic[0] + monotonic[1]) >> 1; |
| 3903 | realtime = (uint64_t) rts.tv_sec * tcf; |
| 3904 | if (tcf == NSEC_PER_SEC) { |
| 3905 | realtime += rts.tv_nsec; |
| 3906 | } else { |
| 3907 | uint64_t n = rts.tv_nsec * tcf; |
| 3908 | |
| 3909 | do_div(n, NSEC_PER_SEC); |
| 3910 | realtime += n; |
| 3911 | } |
| 3912 | offset = (int64_t) realtime - monotonic_avg; |
| 3913 | return offset; |
| 3914 | } |
| 3915 | |
| 3916 | static |
| 3917 | int print_escaped_ctf_string(struct lttng_kernel_session *session, const char *string) |
| 3918 | { |
| 3919 | int ret = 0; |
| 3920 | size_t i; |
| 3921 | char cur; |
| 3922 | |
| 3923 | i = 0; |
| 3924 | cur = string[i]; |
| 3925 | while (cur != '\0') { |
| 3926 | switch (cur) { |
| 3927 | case '\n': |
| 3928 | ret = lttng_metadata_printf(session, "%s", "\\n"); |
| 3929 | break; |
| 3930 | case '\\': |
| 3931 | case '"': |
| 3932 | ret = lttng_metadata_printf(session, "%c", '\\'); |
| 3933 | if (ret) |
| 3934 | goto error; |
| 3935 | /* We still print the current char */ |
| 3936 | lttng_fallthrough; |
| 3937 | default: |
| 3938 | ret = lttng_metadata_printf(session, "%c", cur); |
| 3939 | break; |
| 3940 | } |
| 3941 | |
| 3942 | if (ret) |
| 3943 | goto error; |
| 3944 | |
| 3945 | cur = string[++i]; |
| 3946 | } |
| 3947 | error: |
| 3948 | return ret; |
| 3949 | } |
| 3950 | |
| 3951 | static |
| 3952 | int print_metadata_escaped_field(struct lttng_kernel_session *session, const char *field, |
| 3953 | const char *field_value) |
| 3954 | { |
| 3955 | int ret; |
| 3956 | |
| 3957 | ret = lttng_metadata_printf(session, " %s = \"", field); |
| 3958 | if (ret) |
| 3959 | goto error; |
| 3960 | |
| 3961 | ret = print_escaped_ctf_string(session, field_value); |
| 3962 | if (ret) |
| 3963 | goto error; |
| 3964 | |
| 3965 | ret = lttng_metadata_printf(session, "\";\n"); |
| 3966 | |
| 3967 | error: |
| 3968 | return ret; |
| 3969 | } |
| 3970 | |
| 3971 | /* |
| 3972 | * Output metadata into this session's metadata buffers. |
| 3973 | * Must be called with sessions_mutex held. |
| 3974 | */ |
| 3975 | static |
| 3976 | int _lttng_session_metadata_statedump(struct lttng_kernel_session *session) |
| 3977 | { |
| 3978 | unsigned char *uuid_c = session->priv->uuid.b; |
| 3979 | unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN]; |
| 3980 | const char *product_uuid; |
| 3981 | struct lttng_kernel_channel_buffer_private *chan_priv; |
| 3982 | struct lttng_kernel_event_recorder_private *event_recorder_priv; |
| 3983 | int ret = 0; |
| 3984 | |
| 3985 | if (!LTTNG_READ_ONCE(session->active)) |
| 3986 | return 0; |
| 3987 | |
| 3988 | lttng_metadata_begin(session); |
| 3989 | |
| 3990 | if (session->priv->metadata_dumped) |
| 3991 | goto skip_session; |
| 3992 | |
| 3993 | snprintf(uuid_s, sizeof(uuid_s), |
| 3994 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 3995 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], |
| 3996 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], |
| 3997 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], |
| 3998 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); |
| 3999 | |
| 4000 | ret = lttng_metadata_printf(session, |
| 4001 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
| 4002 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" |
| 4003 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" |
| 4004 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" |
| 4005 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" |
| 4006 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" |
| 4007 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" |
| 4008 | "\n" |
| 4009 | "trace {\n" |
| 4010 | " major = %u;\n" |
| 4011 | " minor = %u;\n" |
| 4012 | " uuid = \"%s\";\n" |
| 4013 | " byte_order = %s;\n" |
| 4014 | " packet.header := struct {\n" |
| 4015 | " uint32_t magic;\n" |
| 4016 | " uint8_t uuid[16];\n" |
| 4017 | " uint32_t stream_id;\n" |
| 4018 | " uint64_t stream_instance_id;\n" |
| 4019 | " };\n" |
| 4020 | "};\n\n", |
| 4021 | lttng_alignof(uint8_t) * CHAR_BIT, |
| 4022 | lttng_alignof(uint16_t) * CHAR_BIT, |
| 4023 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 4024 | lttng_alignof(uint64_t) * CHAR_BIT, |
| 4025 | sizeof(unsigned long) * CHAR_BIT, |
| 4026 | lttng_alignof(unsigned long) * CHAR_BIT, |
| 4027 | CTF_SPEC_MAJOR, |
| 4028 | CTF_SPEC_MINOR, |
| 4029 | uuid_s, |
| 4030 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 4031 | "be" |
| 4032 | #else |
| 4033 | "le" |
| 4034 | #endif |
| 4035 | ); |
| 4036 | if (ret) |
| 4037 | goto end; |
| 4038 | |
| 4039 | ret = lttng_metadata_printf(session, |
| 4040 | "env {\n" |
| 4041 | " hostname = \"%s\";\n" |
| 4042 | " domain = \"kernel\";\n" |
| 4043 | " sysname = \"%s\";\n" |
| 4044 | " kernel_release = \"%s\";\n" |
| 4045 | " kernel_version = \"%s\";\n" |
| 4046 | " tracer_name = \"lttng-modules\";\n" |
| 4047 | " tracer_major = %d;\n" |
| 4048 | " tracer_minor = %d;\n" |
| 4049 | " tracer_patchlevel = %d;\n" |
| 4050 | " trace_buffering_scheme = \"global\";\n", |
| 4051 | current->nsproxy->uts_ns->name.nodename, |
| 4052 | utsname()->sysname, |
| 4053 | utsname()->release, |
| 4054 | utsname()->version, |
| 4055 | LTTNG_MODULES_MAJOR_VERSION, |
| 4056 | LTTNG_MODULES_MINOR_VERSION, |
| 4057 | LTTNG_MODULES_PATCHLEVEL_VERSION |
| 4058 | ); |
| 4059 | if (ret) |
| 4060 | goto end; |
| 4061 | |
| 4062 | ret = print_metadata_escaped_field(session, "trace_name", session->priv->name); |
| 4063 | if (ret) |
| 4064 | goto end; |
| 4065 | ret = print_metadata_escaped_field(session, "trace_creation_datetime", |
| 4066 | session->priv->creation_time); |
| 4067 | if (ret) |
| 4068 | goto end; |
| 4069 | |
| 4070 | /* Add the product UUID to the 'env' section */ |
| 4071 | product_uuid = dmi_get_system_info(DMI_PRODUCT_UUID); |
| 4072 | if (product_uuid) { |
| 4073 | ret = lttng_metadata_printf(session, |
| 4074 | " product_uuid = \"%s\";\n", |
| 4075 | product_uuid |
| 4076 | ); |
| 4077 | if (ret) |
| 4078 | goto end; |
| 4079 | } |
| 4080 | |
| 4081 | /* Close the 'env' section */ |
| 4082 | ret = lttng_metadata_printf(session, "};\n\n"); |
| 4083 | if (ret) |
| 4084 | goto end; |
| 4085 | |
| 4086 | ret = lttng_metadata_printf(session, |
| 4087 | "clock {\n" |
| 4088 | " name = \"%s\";\n", |
| 4089 | trace_clock_name() |
| 4090 | ); |
| 4091 | if (ret) |
| 4092 | goto end; |
| 4093 | |
| 4094 | if (!trace_clock_uuid(clock_uuid_s)) { |
| 4095 | ret = lttng_metadata_printf(session, |
| 4096 | " uuid = \"%s\";\n", |
| 4097 | clock_uuid_s |
| 4098 | ); |
| 4099 | if (ret) |
| 4100 | goto end; |
| 4101 | } |
| 4102 | |
| 4103 | ret = lttng_metadata_printf(session, |
| 4104 | " description = \"%s\";\n" |
| 4105 | " freq = %llu; /* Frequency, in Hz */\n" |
| 4106 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" |
| 4107 | " offset = %lld;\n" |
| 4108 | "};\n\n", |
| 4109 | trace_clock_description(), |
| 4110 | (unsigned long long) trace_clock_freq(), |
| 4111 | (long long) measure_clock_offset() |
| 4112 | ); |
| 4113 | if (ret) |
| 4114 | goto end; |
| 4115 | |
| 4116 | ret = lttng_metadata_printf(session, |
| 4117 | "typealias integer {\n" |
| 4118 | " size = 27; align = 1; signed = false;\n" |
| 4119 | " map = clock.%s.value;\n" |
| 4120 | "} := uint27_clock_monotonic_t;\n" |
| 4121 | "\n" |
| 4122 | "typealias integer {\n" |
| 4123 | " size = 32; align = %u; signed = false;\n" |
| 4124 | " map = clock.%s.value;\n" |
| 4125 | "} := uint32_clock_monotonic_t;\n" |
| 4126 | "\n" |
| 4127 | "typealias integer {\n" |
| 4128 | " size = 64; align = %u; signed = false;\n" |
| 4129 | " map = clock.%s.value;\n" |
| 4130 | "} := uint64_clock_monotonic_t;\n\n", |
| 4131 | trace_clock_name(), |
| 4132 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 4133 | trace_clock_name(), |
| 4134 | lttng_alignof(uint64_t) * CHAR_BIT, |
| 4135 | trace_clock_name() |
| 4136 | ); |
| 4137 | if (ret) |
| 4138 | goto end; |
| 4139 | |
| 4140 | ret = _lttng_stream_packet_context_declare(session); |
| 4141 | if (ret) |
| 4142 | goto end; |
| 4143 | |
| 4144 | ret = _lttng_event_header_declare(session); |
| 4145 | if (ret) |
| 4146 | goto end; |
| 4147 | |
| 4148 | skip_session: |
| 4149 | list_for_each_entry(chan_priv, &session->priv->chan, node) { |
| 4150 | ret = _lttng_channel_metadata_statedump(session, chan_priv->pub); |
| 4151 | if (ret) |
| 4152 | goto end; |
| 4153 | } |
| 4154 | |
| 4155 | list_for_each_entry(event_recorder_priv, &session->priv->events, node) { |
| 4156 | ret = _lttng_event_metadata_statedump(session, event_recorder_priv->pub->chan, |
| 4157 | event_recorder_priv->pub); |
| 4158 | if (ret) |
| 4159 | goto end; |
| 4160 | } |
| 4161 | session->priv->metadata_dumped = 1; |
| 4162 | end: |
| 4163 | lttng_metadata_end(session); |
| 4164 | return ret; |
| 4165 | } |
| 4166 | |
| 4167 | /** |
| 4168 | * lttng_transport_register - LTT transport registration |
| 4169 | * @transport: transport structure |
| 4170 | * |
| 4171 | * Registers a transport which can be used as output to extract the data out of |
| 4172 | * LTTng. The module calling this registration function must ensure that no |
| 4173 | * trap-inducing code will be executed by the transport functions. E.g. |
| 4174 | * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory |
| 4175 | * is made visible to the transport function. This registration acts as a |
| 4176 | * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory |
| 4177 | * after its registration must it synchronize the TLBs. |
| 4178 | */ |
| 4179 | void lttng_transport_register(struct lttng_transport *transport) |
| 4180 | { |
| 4181 | /* |
| 4182 | * Make sure no page fault can be triggered by the module about to be |
| 4183 | * registered. We deal with this here so we don't have to call |
| 4184 | * vmalloc_sync_mappings() in each module's init. |
| 4185 | */ |
| 4186 | wrapper_vmalloc_sync_mappings(); |
| 4187 | |
| 4188 | mutex_lock(&sessions_mutex); |
| 4189 | list_add_tail(&transport->node, <tng_transport_list); |
| 4190 | mutex_unlock(&sessions_mutex); |
| 4191 | } |
| 4192 | EXPORT_SYMBOL_GPL(lttng_transport_register); |
| 4193 | |
| 4194 | /** |
| 4195 | * lttng_transport_unregister - LTT transport unregistration |
| 4196 | * @transport: transport structure |
| 4197 | */ |
| 4198 | void lttng_transport_unregister(struct lttng_transport *transport) |
| 4199 | { |
| 4200 | mutex_lock(&sessions_mutex); |
| 4201 | list_del(&transport->node); |
| 4202 | mutex_unlock(&sessions_mutex); |
| 4203 | } |
| 4204 | EXPORT_SYMBOL_GPL(lttng_transport_unregister); |
| 4205 | |
| 4206 | void lttng_counter_transport_register(struct lttng_counter_transport *transport) |
| 4207 | { |
| 4208 | /* |
| 4209 | * Make sure no page fault can be triggered by the module about to be |
| 4210 | * registered. We deal with this here so we don't have to call |
| 4211 | * vmalloc_sync_mappings() in each module's init. |
| 4212 | */ |
| 4213 | wrapper_vmalloc_sync_mappings(); |
| 4214 | |
| 4215 | mutex_lock(&sessions_mutex); |
| 4216 | list_add_tail(&transport->node, <tng_counter_transport_list); |
| 4217 | mutex_unlock(&sessions_mutex); |
| 4218 | } |
| 4219 | EXPORT_SYMBOL_GPL(lttng_counter_transport_register); |
| 4220 | |
| 4221 | void lttng_counter_transport_unregister(struct lttng_counter_transport *transport) |
| 4222 | { |
| 4223 | mutex_lock(&sessions_mutex); |
| 4224 | list_del(&transport->node); |
| 4225 | mutex_unlock(&sessions_mutex); |
| 4226 | } |
| 4227 | EXPORT_SYMBOL_GPL(lttng_counter_transport_unregister); |
| 4228 | |
| 4229 | #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) |
| 4230 | |
| 4231 | enum cpuhp_state lttng_hp_prepare; |
| 4232 | enum cpuhp_state lttng_hp_online; |
| 4233 | |
| 4234 | static int lttng_hotplug_prepare(unsigned int cpu, struct hlist_node *node) |
| 4235 | { |
| 4236 | struct lttng_cpuhp_node *lttng_node; |
| 4237 | |
| 4238 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); |
| 4239 | switch (lttng_node->component) { |
| 4240 | case LTTNG_RING_BUFFER_FRONTEND: |
| 4241 | return 0; |
| 4242 | case LTTNG_RING_BUFFER_BACKEND: |
| 4243 | return lttng_cpuhp_rb_backend_prepare(cpu, lttng_node); |
| 4244 | case LTTNG_RING_BUFFER_ITER: |
| 4245 | return 0; |
| 4246 | case LTTNG_CONTEXT_PERF_COUNTERS: |
| 4247 | return 0; |
| 4248 | default: |
| 4249 | return -EINVAL; |
| 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | static int lttng_hotplug_dead(unsigned int cpu, struct hlist_node *node) |
| 4254 | { |
| 4255 | struct lttng_cpuhp_node *lttng_node; |
| 4256 | |
| 4257 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); |
| 4258 | switch (lttng_node->component) { |
| 4259 | case LTTNG_RING_BUFFER_FRONTEND: |
| 4260 | return lttng_cpuhp_rb_frontend_dead(cpu, lttng_node); |
| 4261 | case LTTNG_RING_BUFFER_BACKEND: |
| 4262 | return 0; |
| 4263 | case LTTNG_RING_BUFFER_ITER: |
| 4264 | return 0; |
| 4265 | case LTTNG_CONTEXT_PERF_COUNTERS: |
| 4266 | return lttng_cpuhp_perf_counter_dead(cpu, lttng_node); |
| 4267 | default: |
| 4268 | return -EINVAL; |
| 4269 | } |
| 4270 | } |
| 4271 | |
| 4272 | static int lttng_hotplug_online(unsigned int cpu, struct hlist_node *node) |
| 4273 | { |
| 4274 | struct lttng_cpuhp_node *lttng_node; |
| 4275 | |
| 4276 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); |
| 4277 | switch (lttng_node->component) { |
| 4278 | case LTTNG_RING_BUFFER_FRONTEND: |
| 4279 | return lttng_cpuhp_rb_frontend_online(cpu, lttng_node); |
| 4280 | case LTTNG_RING_BUFFER_BACKEND: |
| 4281 | return 0; |
| 4282 | case LTTNG_RING_BUFFER_ITER: |
| 4283 | return lttng_cpuhp_rb_iter_online(cpu, lttng_node); |
| 4284 | case LTTNG_CONTEXT_PERF_COUNTERS: |
| 4285 | return lttng_cpuhp_perf_counter_online(cpu, lttng_node); |
| 4286 | default: |
| 4287 | return -EINVAL; |
| 4288 | } |
| 4289 | } |
| 4290 | |
| 4291 | static int lttng_hotplug_offline(unsigned int cpu, struct hlist_node *node) |
| 4292 | { |
| 4293 | struct lttng_cpuhp_node *lttng_node; |
| 4294 | |
| 4295 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); |
| 4296 | switch (lttng_node->component) { |
| 4297 | case LTTNG_RING_BUFFER_FRONTEND: |
| 4298 | return lttng_cpuhp_rb_frontend_offline(cpu, lttng_node); |
| 4299 | case LTTNG_RING_BUFFER_BACKEND: |
| 4300 | return 0; |
| 4301 | case LTTNG_RING_BUFFER_ITER: |
| 4302 | return 0; |
| 4303 | case LTTNG_CONTEXT_PERF_COUNTERS: |
| 4304 | return 0; |
| 4305 | default: |
| 4306 | return -EINVAL; |
| 4307 | } |
| 4308 | } |
| 4309 | |
| 4310 | static int __init lttng_init_cpu_hotplug(void) |
| 4311 | { |
| 4312 | int ret; |
| 4313 | |
| 4314 | ret = cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN, "lttng:prepare", |
| 4315 | lttng_hotplug_prepare, |
| 4316 | lttng_hotplug_dead); |
| 4317 | if (ret < 0) { |
| 4318 | return ret; |
| 4319 | } |
| 4320 | lttng_hp_prepare = ret; |
| 4321 | lttng_rb_set_hp_prepare(ret); |
| 4322 | |
| 4323 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "lttng:online", |
| 4324 | lttng_hotplug_online, |
| 4325 | lttng_hotplug_offline); |
| 4326 | if (ret < 0) { |
| 4327 | cpuhp_remove_multi_state(lttng_hp_prepare); |
| 4328 | lttng_hp_prepare = 0; |
| 4329 | return ret; |
| 4330 | } |
| 4331 | lttng_hp_online = ret; |
| 4332 | lttng_rb_set_hp_online(ret); |
| 4333 | |
| 4334 | return 0; |
| 4335 | } |
| 4336 | |
| 4337 | static void __exit lttng_exit_cpu_hotplug(void) |
| 4338 | { |
| 4339 | lttng_rb_set_hp_online(0); |
| 4340 | cpuhp_remove_multi_state(lttng_hp_online); |
| 4341 | lttng_rb_set_hp_prepare(0); |
| 4342 | cpuhp_remove_multi_state(lttng_hp_prepare); |
| 4343 | } |
| 4344 | |
| 4345 | #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */ |
| 4346 | static int lttng_init_cpu_hotplug(void) |
| 4347 | { |
| 4348 | return 0; |
| 4349 | } |
| 4350 | static void lttng_exit_cpu_hotplug(void) |
| 4351 | { |
| 4352 | } |
| 4353 | #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */ |
| 4354 | |
| 4355 | static int __init lttng_events_init(void) |
| 4356 | { |
| 4357 | int ret; |
| 4358 | |
| 4359 | ret = wrapper_lttng_fixup_sig(THIS_MODULE); |
| 4360 | if (ret) |
| 4361 | return ret; |
| 4362 | ret = wrapper_get_pfnblock_flags_mask_init(); |
| 4363 | if (ret) |
| 4364 | return ret; |
| 4365 | ret = wrapper_get_pageblock_flags_mask_init(); |
| 4366 | if (ret) |
| 4367 | return ret; |
| 4368 | ret = lttng_probes_init(); |
| 4369 | if (ret) |
| 4370 | return ret; |
| 4371 | ret = lttng_context_init(); |
| 4372 | if (ret) |
| 4373 | return ret; |
| 4374 | ret = lttng_tracepoint_init(); |
| 4375 | if (ret) |
| 4376 | goto error_tp; |
| 4377 | event_recorder_cache = KMEM_CACHE(lttng_kernel_event_recorder, 0); |
| 4378 | if (!event_recorder_cache) { |
| 4379 | ret = -ENOMEM; |
| 4380 | goto error_kmem_event_recorder; |
| 4381 | } |
| 4382 | event_recorder_private_cache = KMEM_CACHE(lttng_kernel_event_recorder_private, 0); |
| 4383 | if (!event_recorder_private_cache) { |
| 4384 | ret = -ENOMEM; |
| 4385 | goto error_kmem_event_recorder_private; |
| 4386 | } |
| 4387 | event_notifier_cache = KMEM_CACHE(lttng_kernel_event_notifier, 0); |
| 4388 | if (!event_notifier_cache) { |
| 4389 | ret = -ENOMEM; |
| 4390 | goto error_kmem_event_notifier; |
| 4391 | } |
| 4392 | event_notifier_private_cache = KMEM_CACHE(lttng_kernel_event_notifier_private, 0); |
| 4393 | if (!event_notifier_private_cache) { |
| 4394 | ret = -ENOMEM; |
| 4395 | goto error_kmem_event_notifier_private; |
| 4396 | } |
| 4397 | ret = lttng_abi_init(); |
| 4398 | if (ret) |
| 4399 | goto error_abi; |
| 4400 | ret = lttng_logger_init(); |
| 4401 | if (ret) |
| 4402 | goto error_logger; |
| 4403 | ret = lttng_init_cpu_hotplug(); |
| 4404 | if (ret) |
| 4405 | goto error_hotplug; |
| 4406 | printk(KERN_NOTICE "LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n", |
| 4407 | __stringify(LTTNG_MODULES_MAJOR_VERSION), |
| 4408 | __stringify(LTTNG_MODULES_MINOR_VERSION), |
| 4409 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION), |
| 4410 | LTTNG_MODULES_EXTRAVERSION, |
| 4411 | LTTNG_VERSION_NAME, |
| 4412 | #ifdef LTTNG_EXTRA_VERSION_GIT |
| 4413 | LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT, |
| 4414 | #else |
| 4415 | "", |
| 4416 | #endif |
| 4417 | #ifdef LTTNG_EXTRA_VERSION_NAME |
| 4418 | LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME); |
| 4419 | #else |
| 4420 | ""); |
| 4421 | #endif |
| 4422 | #ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM |
| 4423 | printk(KERN_NOTICE "LTTng: Experimental bitwise enum enabled.\n"); |
| 4424 | #endif /* CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM */ |
| 4425 | return 0; |
| 4426 | |
| 4427 | error_hotplug: |
| 4428 | lttng_logger_exit(); |
| 4429 | error_logger: |
| 4430 | lttng_abi_exit(); |
| 4431 | error_abi: |
| 4432 | kmem_cache_destroy(event_notifier_private_cache); |
| 4433 | error_kmem_event_notifier_private: |
| 4434 | kmem_cache_destroy(event_notifier_cache); |
| 4435 | error_kmem_event_notifier: |
| 4436 | kmem_cache_destroy(event_recorder_private_cache); |
| 4437 | error_kmem_event_recorder_private: |
| 4438 | kmem_cache_destroy(event_recorder_cache); |
| 4439 | error_kmem_event_recorder: |
| 4440 | lttng_tracepoint_exit(); |
| 4441 | error_tp: |
| 4442 | lttng_context_exit(); |
| 4443 | printk(KERN_NOTICE "LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n", |
| 4444 | __stringify(LTTNG_MODULES_MAJOR_VERSION), |
| 4445 | __stringify(LTTNG_MODULES_MINOR_VERSION), |
| 4446 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION), |
| 4447 | LTTNG_MODULES_EXTRAVERSION, |
| 4448 | LTTNG_VERSION_NAME, |
| 4449 | #ifdef LTTNG_EXTRA_VERSION_GIT |
| 4450 | LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT, |
| 4451 | #else |
| 4452 | "", |
| 4453 | #endif |
| 4454 | #ifdef LTTNG_EXTRA_VERSION_NAME |
| 4455 | LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME); |
| 4456 | #else |
| 4457 | ""); |
| 4458 | #endif |
| 4459 | return ret; |
| 4460 | } |
| 4461 | |
| 4462 | module_init(lttng_events_init); |
| 4463 | |
| 4464 | static void __exit lttng_events_exit(void) |
| 4465 | { |
| 4466 | struct lttng_kernel_session_private *session_priv, *tmpsession_priv; |
| 4467 | |
| 4468 | lttng_exit_cpu_hotplug(); |
| 4469 | lttng_logger_exit(); |
| 4470 | lttng_abi_exit(); |
| 4471 | list_for_each_entry_safe(session_priv, tmpsession_priv, &sessions, list) |
| 4472 | lttng_session_destroy(session_priv->pub); |
| 4473 | kmem_cache_destroy(event_recorder_cache); |
| 4474 | kmem_cache_destroy(event_recorder_private_cache); |
| 4475 | kmem_cache_destroy(event_notifier_cache); |
| 4476 | kmem_cache_destroy(event_notifier_private_cache); |
| 4477 | lttng_tracepoint_exit(); |
| 4478 | lttng_context_exit(); |
| 4479 | printk(KERN_NOTICE "LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n", |
| 4480 | __stringify(LTTNG_MODULES_MAJOR_VERSION), |
| 4481 | __stringify(LTTNG_MODULES_MINOR_VERSION), |
| 4482 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION), |
| 4483 | LTTNG_MODULES_EXTRAVERSION, |
| 4484 | LTTNG_VERSION_NAME, |
| 4485 | #ifdef LTTNG_EXTRA_VERSION_GIT |
| 4486 | LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT, |
| 4487 | #else |
| 4488 | "", |
| 4489 | #endif |
| 4490 | #ifdef LTTNG_EXTRA_VERSION_NAME |
| 4491 | LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME); |
| 4492 | #else |
| 4493 | ""); |
| 4494 | #endif |
| 4495 | } |
| 4496 | |
| 4497 | module_exit(lttng_events_exit); |
| 4498 | |
| 4499 | #include <generated/patches.h> |
| 4500 | #ifdef LTTNG_EXTRA_VERSION_GIT |
| 4501 | MODULE_INFO(extra_version_git, LTTNG_EXTRA_VERSION_GIT); |
| 4502 | #endif |
| 4503 | #ifdef LTTNG_EXTRA_VERSION_NAME |
| 4504 | MODULE_INFO(extra_version_name, LTTNG_EXTRA_VERSION_NAME); |
| 4505 | #endif |
| 4506 | MODULE_LICENSE("GPL and additional rights"); |
| 4507 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); |
| 4508 | MODULE_DESCRIPTION("LTTng tracer"); |
| 4509 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
| 4510 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." |
| 4511 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) |
| 4512 | LTTNG_MODULES_EXTRAVERSION); |