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