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