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