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