Refactoring: combine identical functions into lttng_create_tracepoint_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/*
a67ba386 2204 * Create struct lttng_kernel_event_recorder if it is missing and present in the list of
33a39a3c
MD
2205 * tracepoint probes.
2206 * Should be called with sessions mutex held.
2207 */
2208static
1ae083ba 2209void lttng_create_event_if_missing(struct lttng_event_recorder_enabler *event_enabler)
33a39a3c 2210{
5b9817ae 2211 switch (event_enabler->parent.event_param.instrumentation) {
606828e4 2212 case LTTNG_KERNEL_ABI_TRACEPOINT:
c8e14380 2213 lttng_create_tracepoint_event_if_missing(&event_enabler->parent);
33a39a3c 2214 break;
b4a75708 2215
606828e4 2216 case LTTNG_KERNEL_ABI_SYSCALL:
053554ae 2217 lttng_create_syscall_event_if_missing(&event_enabler->parent);
33a39a3c 2218 break;
b4a75708 2219
33a39a3c
MD
2220 default:
2221 WARN_ON_ONCE(1);
2222 break;
2223 }
2224}
2225
3c997079 2226/*
b2bc0bc8 2227 * Create events associated with an event_enabler (if not already present),
3c997079
MD
2228 * and add backward reference from the event to the enabler.
2229 * Should be called with sessions mutex held.
2230 */
2231static
1ae083ba 2232int lttng_event_enabler_ref_events(struct lttng_event_recorder_enabler *event_enabler)
3c997079 2233{
f7d06400
MD
2234 struct lttng_kernel_channel_buffer *chan = event_enabler->chan;
2235 struct lttng_kernel_session *session = event_enabler->chan->parent.session;
1ae083ba 2236 struct lttng_event_enabler_common *base_enabler = lttng_event_recorder_enabler_as_enabler(event_enabler);
a67ba386 2237 struct lttng_kernel_event_recorder_private *event_recorder_priv;
3c997079 2238
606828e4
MD
2239 if (base_enabler->event_param.instrumentation == LTTNG_KERNEL_ABI_SYSCALL &&
2240 base_enabler->event_param.u.syscall.abi == LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL &&
2241 base_enabler->event_param.u.syscall.match == LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME &&
b2bc0bc8 2242 !strcmp(base_enabler->event_param.name, "*")) {
3b82c4e1 2243 int enabled = base_enabler->enabled;
606828e4 2244 enum lttng_kernel_abi_syscall_entryexit entryexit = base_enabler->event_param.u.syscall.entryexit;
3b82c4e1 2245
606828e4 2246 if (entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRY || entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT)
0bb716a8 2247 WRITE_ONCE(chan->priv->parent.syscall_table.syscall_all_entry, enabled);
3b82c4e1 2248
606828e4 2249 if (entryexit == LTTNG_KERNEL_ABI_SYSCALL_EXIT || entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT)
0bb716a8 2250 WRITE_ONCE(chan->priv->parent.syscall_table.syscall_all_exit, enabled);
badfe9f5
MD
2251 }
2252
3c997079 2253 /* First ensure that probe events are created for this enabler. */
b2bc0bc8 2254 lttng_create_event_if_missing(event_enabler);
3c997079 2255
b2bc0bc8 2256 /* For each event matching event_enabler in session event list. */
8cdc1a81 2257 list_for_each_entry(event_recorder_priv, &session->priv->events, node) {
a67ba386 2258 struct lttng_kernel_event_recorder *event_recorder = event_recorder_priv->pub;
3c997079
MD
2259 struct lttng_enabler_ref *enabler_ref;
2260
5c6ec66a 2261 if (!lttng_event_enabler_match_event(&event_enabler->parent, &event_recorder->parent))
3c997079 2262 continue;
a67ba386 2263 enabler_ref = lttng_enabler_ref(&event_recorder_priv->parent.enablers_ref_head,
1ae083ba 2264 lttng_event_recorder_enabler_as_enabler(event_enabler));
3c997079
MD
2265 if (!enabler_ref) {
2266 /*
2267 * If no backward ref, create it.
b2bc0bc8 2268 * Add backward ref from event to event_enabler.
3c997079
MD
2269 */
2270 enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL);
2271 if (!enabler_ref)
2272 return -ENOMEM;
1ae083ba 2273 enabler_ref->ref = lttng_event_recorder_enabler_as_enabler(event_enabler);
3c997079 2274 list_add(&enabler_ref->node,
a67ba386 2275 &event_recorder_priv->parent.enablers_ref_head);
3c997079
MD
2276 }
2277
f127e61e
MD
2278 /*
2279 * Link filter bytecodes if not linked yet.
2280 */
a67ba386 2281 lttng_enabler_link_bytecode(event_recorder_priv->parent.desc,
2dfda770 2282 lttng_static_ctx,
a67ba386 2283 &event_recorder_priv->parent.filter_bytecode_runtime_head,
1ae083ba 2284 &lttng_event_recorder_enabler_as_enabler(event_enabler)->filter_bytecode_head);
3c997079
MD
2285 }
2286 return 0;
2287}
2288
b01155ba 2289/*
a67ba386 2290 * Create struct lttng_kernel_event_notifier if it is missing and present in the list of
b01155ba
FD
2291 * tracepoint probes.
2292 * Should be called with sessions mutex held.
2293 */
2294static
2295void lttng_create_event_notifier_if_missing(struct lttng_event_notifier_enabler *event_notifier_enabler)
2296{
5b9817ae 2297 switch (event_notifier_enabler->parent.event_param.instrumentation) {
606828e4 2298 case LTTNG_KERNEL_ABI_TRACEPOINT:
c8e14380 2299 lttng_create_tracepoint_event_if_missing(&event_notifier_enabler->parent);
b01155ba 2300 break;
b4a75708 2301
606828e4 2302 case LTTNG_KERNEL_ABI_SYSCALL:
053554ae 2303 lttng_create_syscall_event_if_missing(&event_notifier_enabler->parent);
8a8ac9a8 2304 break;
b4a75708 2305
b01155ba
FD
2306 default:
2307 WARN_ON_ONCE(1);
2308 break;
2309 }
2310}
2311
2312/*
2313 * Create event_notifiers associated with a event_notifier enabler (if not already present).
2314 */
2315static
8a8ac9a8
FD
2316int lttng_event_notifier_enabler_ref_event_notifiers(
2317 struct lttng_event_notifier_enabler *event_notifier_enabler)
b01155ba
FD
2318{
2319 struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group;
8d554755 2320 struct lttng_event_enabler_common *base_enabler = lttng_event_notifier_enabler_as_enabler(event_notifier_enabler);
a67ba386 2321 struct lttng_kernel_event_notifier_private *event_notifier_priv;
b01155ba 2322
606828e4
MD
2323 if (base_enabler->event_param.instrumentation == LTTNG_KERNEL_ABI_SYSCALL &&
2324 base_enabler->event_param.u.syscall.abi == LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL &&
2325 base_enabler->event_param.u.syscall.match == LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME &&
8a8ac9a8
FD
2326 !strcmp(base_enabler->event_param.name, "*")) {
2327
2328 int enabled = base_enabler->enabled;
606828e4 2329 enum lttng_kernel_abi_syscall_entryexit entryexit = base_enabler->event_param.u.syscall.entryexit;
8a8ac9a8 2330
606828e4 2331 if (entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRY || entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT)
0bb716a8 2332 WRITE_ONCE(event_notifier_group->syscall_table.syscall_all_entry, enabled);
8a8ac9a8 2333
606828e4 2334 if (entryexit == LTTNG_KERNEL_ABI_SYSCALL_EXIT || entryexit == LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT)
0bb716a8 2335 WRITE_ONCE(event_notifier_group->syscall_table.syscall_all_exit, enabled);
8a8ac9a8
FD
2336
2337 }
2338
b01155ba
FD
2339 /* First ensure that probe event_notifiers are created for this enabler. */
2340 lttng_create_event_notifier_if_missing(event_notifier_enabler);
2341
2342 /* Link the created event_notifier with its associated enabler. */
a67ba386
MD
2343 list_for_each_entry(event_notifier_priv, &event_notifier_group->event_notifiers_head, node) {
2344 struct lttng_kernel_event_notifier *event_notifier = event_notifier_priv->pub;
b01155ba
FD
2345 struct lttng_enabler_ref *enabler_ref;
2346
5c6ec66a 2347 if (!lttng_event_enabler_match_event(&event_notifier_enabler->parent, &event_notifier->parent))
b01155ba
FD
2348 continue;
2349
a67ba386 2350 enabler_ref = lttng_enabler_ref(&event_notifier_priv->parent.enablers_ref_head,
b01155ba
FD
2351 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler));
2352 if (!enabler_ref) {
2353 /*
2354 * If no backward ref, create it.
2355 * Add backward ref from event_notifier to enabler.
2356 */
2357 enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL);
2358 if (!enabler_ref)
2359 return -ENOMEM;
2360
2361 enabler_ref->ref = lttng_event_notifier_enabler_as_enabler(
2362 event_notifier_enabler);
2363 list_add(&enabler_ref->node,
a67ba386 2364 &event_notifier_priv->parent.enablers_ref_head);
b01155ba
FD
2365 }
2366
2367 /*
2368 * Link filter bytecodes if not linked yet.
2369 */
a67ba386
MD
2370 lttng_enabler_link_bytecode(event_notifier_priv->parent.desc,
2371 lttng_static_ctx, &event_notifier_priv->parent.filter_bytecode_runtime_head,
60206944 2372 &lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->filter_bytecode_head);
99d223ad
FD
2373
2374 /* Link capture bytecodes if not linked yet. */
a67ba386
MD
2375 lttng_enabler_link_bytecode(event_notifier_priv->parent.desc,
2376 lttng_static_ctx, &event_notifier_priv->capture_bytecode_runtime_head,
99d223ad
FD
2377 &event_notifier_enabler->capture_bytecode_head);
2378
a67ba386 2379 event_notifier_priv->num_captures = event_notifier_enabler->num_captures;
b01155ba
FD
2380 }
2381 return 0;
2382}
2383
3c997079
MD
2384/*
2385 * Called at module load: connect the probe on all enablers matching
2386 * this event.
2387 * Called with sessions lock held.
2388 */
2389int lttng_fix_pending_events(void)
2390{
8cdc1a81 2391 struct lttng_kernel_session_private *session_priv;
3c997079 2392
8cdc1a81
MD
2393 list_for_each_entry(session_priv, &sessions, list)
2394 lttng_session_lazy_sync_event_enablers(session_priv->pub);
3c997079
MD
2395 return 0;
2396}
2397
b01155ba
FD
2398static bool lttng_event_notifier_group_has_active_event_notifiers(
2399 struct lttng_event_notifier_group *event_notifier_group)
2400{
2401 struct lttng_event_notifier_enabler *event_notifier_enabler;
2402
2403 list_for_each_entry(event_notifier_enabler, &event_notifier_group->enablers_head,
2404 node) {
5b9817ae 2405 if (event_notifier_enabler->parent.enabled)
b01155ba
FD
2406 return true;
2407 }
2408 return false;
2409}
2410
2411bool lttng_event_notifier_active(void)
2412{
2413 struct lttng_event_notifier_group *event_notifier_group;
2414
2415 list_for_each_entry(event_notifier_group, &event_notifier_groups, node) {
2416 if (lttng_event_notifier_group_has_active_event_notifiers(event_notifier_group))
2417 return true;
2418 }
2419 return false;
2420}
2421
2422int lttng_fix_pending_event_notifiers(void)
2423{
2424 struct lttng_event_notifier_group *event_notifier_group;
2425
2426 list_for_each_entry(event_notifier_group, &event_notifier_groups, node)
2427 lttng_event_notifier_group_sync_enablers(event_notifier_group);
2428 return 0;
2429}
2430
1ae083ba 2431struct lttng_event_recorder_enabler *lttng_event_recorder_enabler_create(
b2bc0bc8 2432 enum lttng_enabler_format_type format_type,
606828e4 2433 struct lttng_kernel_abi_event *event_param,
f7d06400 2434 struct lttng_kernel_channel_buffer *chan)
3c997079 2435{
1ae083ba 2436 struct lttng_event_recorder_enabler *event_enabler;
3c997079 2437
b2bc0bc8
FD
2438 event_enabler = kzalloc(sizeof(*event_enabler), GFP_KERNEL);
2439 if (!event_enabler)
3c997079 2440 return NULL;
0bb716a8 2441 event_enabler->parent.enabler_type = LTTNG_EVENT_ENABLER_TYPE_RECORDER;
5b9817ae
MD
2442 event_enabler->parent.format_type = format_type;
2443 INIT_LIST_HEAD(&event_enabler->parent.filter_bytecode_head);
2444 memcpy(&event_enabler->parent.event_param, event_param,
2445 sizeof(event_enabler->parent.event_param));
b2bc0bc8 2446 event_enabler->chan = chan;
3c997079 2447 /* ctx left NULL */
5b9817ae 2448 event_enabler->parent.enabled = 0;
def1e304
MD
2449 return event_enabler;
2450}
2451
2452void lttng_event_enabler_session_add(struct lttng_kernel_session *session,
1ae083ba 2453 struct lttng_event_recorder_enabler *event_enabler)
def1e304 2454{
3c997079 2455 mutex_lock(&sessions_mutex);
def1e304
MD
2456 list_add(&event_enabler->node, &session->priv->enablers_head);
2457 event_enabler->published = true;
2458 lttng_session_lazy_sync_event_enablers(session);
3c997079 2459 mutex_unlock(&sessions_mutex);
3c997079
MD
2460}
2461
7b25ab0e 2462int lttng_event_enabler_enable(struct lttng_event_enabler_common *event_enabler)
3c997079
MD
2463{
2464 mutex_lock(&sessions_mutex);
7b25ab0e
MD
2465 event_enabler->enabled = 1;
2466 lttng_event_enabler_sync(event_enabler);
3c997079
MD
2467 mutex_unlock(&sessions_mutex);
2468 return 0;
2469}
2470
7b25ab0e 2471int lttng_event_enabler_disable(struct lttng_event_enabler_common *event_enabler)
3c997079
MD
2472{
2473 mutex_lock(&sessions_mutex);
7b25ab0e
MD
2474 event_enabler->enabled = 0;
2475 lttng_event_enabler_sync(event_enabler);
3c997079
MD
2476 mutex_unlock(&sessions_mutex);
2477 return 0;
2478}
2479
b2bc0bc8 2480static
8d554755 2481int lttng_enabler_attach_filter_bytecode(struct lttng_event_enabler_common *enabler,
606828e4 2482 struct lttng_kernel_abi_filter_bytecode __user *bytecode)
07dfc1d0 2483{
d64ca521 2484 struct lttng_kernel_bytecode_node *bytecode_node;
07dfc1d0
MD
2485 uint32_t bytecode_len;
2486 int ret;
2487
2488 ret = get_user(bytecode_len, &bytecode->len);
2489 if (ret)
2490 return ret;
52cfcdf3 2491 bytecode_node = lttng_kvzalloc(sizeof(*bytecode_node) + bytecode_len,
07dfc1d0
MD
2492 GFP_KERNEL);
2493 if (!bytecode_node)
2494 return -ENOMEM;
2495 ret = copy_from_user(&bytecode_node->bc, bytecode,
2496 sizeof(*bytecode) + bytecode_len);
2497 if (ret)
2498 goto error_free;
b2bc0bc8 2499
8a445457 2500 bytecode_node->type = LTTNG_KERNEL_BYTECODE_TYPE_FILTER;
07dfc1d0
MD
2501 bytecode_node->enabler = enabler;
2502 /* Enforce length based on allocated size */
2503 bytecode_node->bc.len = bytecode_len;
2504 list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head);
b2bc0bc8 2505
07dfc1d0
MD
2506 return 0;
2507
2508error_free:
52cfcdf3 2509 lttng_kvfree(bytecode_node);
07dfc1d0
MD
2510 return ret;
2511}
2512
6048044f 2513int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler_common *event_enabler,
606828e4 2514 struct lttng_kernel_abi_filter_bytecode __user *bytecode)
b2bc0bc8
FD
2515{
2516 int ret;
6048044f 2517 ret = lttng_enabler_attach_filter_bytecode(event_enabler, bytecode);
b2bc0bc8
FD
2518 if (ret)
2519 goto error;
7b25ab0e 2520 lttng_event_enabler_sync(event_enabler);
b2bc0bc8
FD
2521 return 0;
2522
2523error:
2524 return ret;
2525}
2526
e2d5dbc7 2527int lttng_event_add_callsite(struct lttng_kernel_event_common *event,
606828e4 2528 struct lttng_kernel_abi_event_callsite __user *callsite)
3aed4dca
FD
2529{
2530
e2d5dbc7 2531 switch (event->priv->instrumentation) {
606828e4 2532 case LTTNG_KERNEL_ABI_UPROBE:
e2d5dbc7 2533 return lttng_uprobes_event_add_callsite(event, callsite);
3aed4dca
FD
2534 default:
2535 return -EINVAL;
2536 }
2537}
2538
3c997079 2539static
8d554755 2540void lttng_enabler_destroy(struct lttng_event_enabler_common *enabler)
3c997079 2541{
d64ca521 2542 struct lttng_kernel_bytecode_node *filter_node, *tmp_filter_node;
07dfc1d0
MD
2543
2544 /* Destroy filter bytecode */
2545 list_for_each_entry_safe(filter_node, tmp_filter_node,
2546 &enabler->filter_bytecode_head, node) {
52cfcdf3 2547 lttng_kvfree(filter_node);
07dfc1d0 2548 }
b2bc0bc8
FD
2549}
2550
44b1abdc 2551void lttng_event_enabler_destroy(struct lttng_event_enabler_common *event_enabler)
b2bc0bc8 2552{
44b1abdc
MD
2553 switch (event_enabler->enabler_type) {
2554 case LTTNG_EVENT_ENABLER_TYPE_RECORDER:
2555 {
2556 struct lttng_event_recorder_enabler *event_recorder_enabler =
2557 container_of(event_enabler, struct lttng_event_recorder_enabler, parent);
2558
2559 lttng_enabler_destroy(event_enabler);
2560 if (event_recorder_enabler->published)
2561 list_del(&event_recorder_enabler->node);
2562 kfree(event_recorder_enabler);
2563 break;
2564 }
2565 case LTTNG_EVENT_ENABLER_TYPE_NOTIFIER:
2566 {
2567 struct lttng_event_notifier_enabler *event_notifier_enabler =
2568 container_of(event_enabler, struct lttng_event_notifier_enabler, parent);
07dfc1d0 2569
44b1abdc 2570 lttng_enabler_destroy(event_enabler);
1bbb5b2d
MD
2571 if (event_notifier_enabler->published)
2572 list_del(&event_notifier_enabler->node);
44b1abdc
MD
2573 kfree(event_notifier_enabler);
2574 break;
2575 }
2576 default:
2577 WARN_ON_ONCE(1);
2578 }
3c997079
MD
2579}
2580
dffef45d 2581struct lttng_event_notifier_enabler *lttng_event_notifier_enabler_create(
dffef45d 2582 enum lttng_enabler_format_type format_type,
1bbb5b2d
MD
2583 struct lttng_kernel_abi_event_notifier *event_notifier_param,
2584 struct lttng_event_notifier_group *event_notifier_group)
dffef45d
FD
2585{
2586 struct lttng_event_notifier_enabler *event_notifier_enabler;
2587
2588 event_notifier_enabler = kzalloc(sizeof(*event_notifier_enabler), GFP_KERNEL);
2589 if (!event_notifier_enabler)
2590 return NULL;
2591
0bb716a8 2592 event_notifier_enabler->parent.enabler_type = LTTNG_EVENT_ENABLER_TYPE_NOTIFIER;
5b9817ae
MD
2593 event_notifier_enabler->parent.format_type = format_type;
2594 INIT_LIST_HEAD(&event_notifier_enabler->parent.filter_bytecode_head);
99d223ad
FD
2595 INIT_LIST_HEAD(&event_notifier_enabler->capture_bytecode_head);
2596
99f52fcc 2597 event_notifier_enabler->error_counter_index = event_notifier_param->error_counter_index;
99d223ad 2598 event_notifier_enabler->num_captures = 0;
dffef45d 2599
5b9817ae
MD
2600 memcpy(&event_notifier_enabler->parent.event_param, &event_notifier_param->event,
2601 sizeof(event_notifier_enabler->parent.event_param));
dffef45d 2602
5b9817ae
MD
2603 event_notifier_enabler->parent.enabled = 0;
2604 event_notifier_enabler->parent.user_token = event_notifier_param->event.token;
dffef45d 2605 event_notifier_enabler->group = event_notifier_group;
1bbb5b2d
MD
2606 return event_notifier_enabler;
2607}
dffef45d 2608
1bbb5b2d
MD
2609void lttng_event_notifier_enabler_group_add(struct lttng_event_notifier_group *event_notifier_group,
2610 struct lttng_event_notifier_enabler *event_notifier_enabler)
2611{
dffef45d
FD
2612 mutex_lock(&sessions_mutex);
2613 list_add(&event_notifier_enabler->node, &event_notifier_enabler->group->enablers_head);
1bbb5b2d 2614 event_notifier_enabler->published = true;
b01155ba 2615 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
dffef45d 2616 mutex_unlock(&sessions_mutex);
dffef45d
FD
2617}
2618
2619int lttng_event_notifier_enabler_enable(
2620 struct lttng_event_notifier_enabler *event_notifier_enabler)
2621{
2622 mutex_lock(&sessions_mutex);
2623 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled = 1;
b01155ba 2624 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
dffef45d
FD
2625 mutex_unlock(&sessions_mutex);
2626 return 0;
2627}
2628
2629int lttng_event_notifier_enabler_disable(
2630 struct lttng_event_notifier_enabler *event_notifier_enabler)
2631{
2632 mutex_lock(&sessions_mutex);
2633 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled = 0;
b01155ba 2634 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
dffef45d
FD
2635 mutex_unlock(&sessions_mutex);
2636 return 0;
2637}
2638
99d223ad
FD
2639int lttng_event_notifier_enabler_attach_capture_bytecode(
2640 struct lttng_event_notifier_enabler *event_notifier_enabler,
606828e4 2641 struct lttng_kernel_abi_capture_bytecode __user *bytecode)
99d223ad 2642{
d64ca521 2643 struct lttng_kernel_bytecode_node *bytecode_node;
8d554755 2644 struct lttng_event_enabler_common *enabler =
99d223ad
FD
2645 lttng_event_notifier_enabler_as_enabler(event_notifier_enabler);
2646 uint32_t bytecode_len;
2647 int ret;
2648
2649 ret = get_user(bytecode_len, &bytecode->len);
2650 if (ret)
2651 return ret;
2652
2653 bytecode_node = lttng_kvzalloc(sizeof(*bytecode_node) + bytecode_len,
2654 GFP_KERNEL);
2655 if (!bytecode_node)
2656 return -ENOMEM;
2657
2658 ret = copy_from_user(&bytecode_node->bc, bytecode,
2659 sizeof(*bytecode) + bytecode_len);
2660 if (ret)
2661 goto error_free;
2662
8a445457 2663 bytecode_node->type = LTTNG_KERNEL_BYTECODE_TYPE_CAPTURE;
99d223ad
FD
2664 bytecode_node->enabler = enabler;
2665
2666 /* Enforce length based on allocated size */
2667 bytecode_node->bc.len = bytecode_len;
2668 list_add_tail(&bytecode_node->node, &event_notifier_enabler->capture_bytecode_head);
2669
2670 event_notifier_enabler->num_captures++;
2671
2672 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
2673 goto end;
2674
2675error_free:
2676 lttng_kvfree(bytecode_node);
2677end:
2678 return ret;
2679}
2680
3c997079 2681/*
b2bc0bc8 2682 * lttng_session_sync_event_enablers should be called just before starting a
3c997079
MD
2683 * session.
2684 * Should be called with sessions mutex held.
2685 */
2686static
8cdc1a81 2687void lttng_session_sync_event_enablers(struct lttng_kernel_session *session)
3c997079 2688{
1ae083ba 2689 struct lttng_event_recorder_enabler *event_enabler;
a67ba386 2690 struct lttng_kernel_event_recorder_private *event_recorder_priv;
3c997079 2691
8cdc1a81 2692 list_for_each_entry(event_enabler, &session->priv->enablers_head, node)
b2bc0bc8 2693 lttng_event_enabler_ref_events(event_enabler);
3c997079
MD
2694 /*
2695 * For each event, if at least one of its enablers is enabled,
2696 * and its channel and session transient states are enabled, we
2697 * enable the event, else we disable it.
2698 */
8cdc1a81 2699 list_for_each_entry(event_recorder_priv, &session->priv->events, node) {
a67ba386 2700 struct lttng_kernel_event_recorder *event_recorder = event_recorder_priv->pub;
3c997079 2701 struct lttng_enabler_ref *enabler_ref;
218585b9 2702 struct lttng_kernel_bytecode_runtime *runtime;
e2d5dbc7
MD
2703 int enabled = 0, has_enablers_without_filter_bytecode = 0;
2704 int nr_filters = 0;
3c997079 2705
a67ba386 2706 switch (event_recorder_priv->parent.instrumentation) {
c190d76e
MJ
2707 case LTTNG_KERNEL_ABI_TRACEPOINT:
2708 lttng_fallthrough;
606828e4 2709 case LTTNG_KERNEL_ABI_SYSCALL:
3c997079
MD
2710 /* Enable events */
2711 list_for_each_entry(enabler_ref,
a67ba386 2712 &event_recorder_priv->parent.enablers_ref_head, node) {
3c997079
MD
2713 if (enabler_ref->ref->enabled) {
2714 enabled = 1;
2715 break;
2716 }
2717 }
4ecb5ad5 2718 break;
b4a75708 2719
4ecb5ad5 2720 default:
3c997079
MD
2721 /* Not handled with lazy sync. */
2722 continue;
2723 }
2724 /*
2725 * Enabled state is based on union of enablers, with
2726 * intesection of session and channel transient enable
2727 * states.
2728 */
f7d06400 2729 enabled = enabled && session->priv->tstate && event_recorder->chan->priv->parent.tstate;
3c997079 2730
a67ba386 2731 WRITE_ONCE(event_recorder->parent.enabled, enabled);
3c997079
MD
2732 /*
2733 * Sync tracepoint registration with event enabled
2734 * state.
2735 */
2736 if (enabled) {
0c851fa0 2737 register_event(&event_recorder->parent);
3c997079 2738 } else {
f0bcc14c 2739 _lttng_event_unregister(&event_recorder->parent);
3c997079 2740 }
07dfc1d0
MD
2741
2742 /* Check if has enablers without bytecode enabled */
2743 list_for_each_entry(enabler_ref,
a67ba386 2744 &event_recorder_priv->parent.enablers_ref_head, node) {
07dfc1d0
MD
2745 if (enabler_ref->ref->enabled
2746 && list_empty(&enabler_ref->ref->filter_bytecode_head)) {
e2d5dbc7 2747 has_enablers_without_filter_bytecode = 1;
07dfc1d0
MD
2748 break;
2749 }
2750 }
a67ba386 2751 event_recorder_priv->parent.has_enablers_without_filter_bytecode =
e2d5dbc7 2752 has_enablers_without_filter_bytecode;
07dfc1d0
MD
2753
2754 /* Enable filters */
2755 list_for_each_entry(runtime,
e2d5dbc7 2756 &event_recorder_priv->parent.filter_bytecode_runtime_head, node) {
8a445457 2757 lttng_bytecode_sync_state(runtime);
e2d5dbc7
MD
2758 nr_filters++;
2759 }
2760 WRITE_ONCE(event_recorder_priv->parent.pub->eval_filter,
2761 !(has_enablers_without_filter_bytecode || !nr_filters));
3c997079
MD
2762 }
2763}
2764
2765/*
2766 * Apply enablers to session events, adding events to session if need
2767 * be. It is required after each modification applied to an active
2768 * session, and right before session "start".
2769 * "lazy" sync means we only sync if required.
2770 * Should be called with sessions mutex held.
2771 */
2772static
8cdc1a81 2773void lttng_session_lazy_sync_event_enablers(struct lttng_kernel_session *session)
3c997079
MD
2774{
2775 /* We can skip if session is not active */
2776 if (!session->active)
2777 return;
b2bc0bc8 2778 lttng_session_sync_event_enablers(session);
3c997079
MD
2779}
2780
b01155ba
FD
2781static
2782void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group *event_notifier_group)
2783{
2784 struct lttng_event_notifier_enabler *event_notifier_enabler;
a67ba386 2785 struct lttng_kernel_event_notifier_private *event_notifier_priv;
b01155ba
FD
2786
2787 list_for_each_entry(event_notifier_enabler, &event_notifier_group->enablers_head, node)
2788 lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler);
2789
2790 /*
2791 * For each event_notifier, if at least one of its enablers is enabled,
2792 * we enable the event_notifier, else we disable it.
2793 */
a67ba386
MD
2794 list_for_each_entry(event_notifier_priv, &event_notifier_group->event_notifiers_head, node) {
2795 struct lttng_kernel_event_notifier *event_notifier = event_notifier_priv->pub;
b01155ba 2796 struct lttng_enabler_ref *enabler_ref;
218585b9 2797 struct lttng_kernel_bytecode_runtime *runtime;
e2d5dbc7
MD
2798 int enabled = 0, has_enablers_without_filter_bytecode = 0;
2799 int nr_filters = 0, nr_captures = 0;
b01155ba 2800
a67ba386 2801 switch (event_notifier_priv->parent.instrumentation) {
c190d76e
MJ
2802 case LTTNG_KERNEL_ABI_TRACEPOINT:
2803 lttng_fallthrough;
606828e4 2804 case LTTNG_KERNEL_ABI_SYSCALL:
b01155ba
FD
2805 /* Enable event_notifiers */
2806 list_for_each_entry(enabler_ref,
a67ba386 2807 &event_notifier_priv->parent.enablers_ref_head, node) {
b01155ba
FD
2808 if (enabler_ref->ref->enabled) {
2809 enabled = 1;
2810 break;
2811 }
2812 }
2813 break;
b4a75708 2814
b01155ba
FD
2815 default:
2816 /* Not handled with sync. */
2817 continue;
2818 }
2819
a67ba386 2820 WRITE_ONCE(event_notifier->parent.enabled, enabled);
b01155ba
FD
2821 /*
2822 * Sync tracepoint registration with event_notifier enabled
2823 * state.
2824 */
2825 if (enabled) {
a67ba386 2826 if (!event_notifier_priv->parent.registered)
0c851fa0 2827 register_event(&event_notifier->parent);
b01155ba 2828 } else {
a67ba386 2829 if (event_notifier_priv->parent.registered)
f0bcc14c 2830 _lttng_event_unregister(&event_notifier->parent);
b01155ba
FD
2831 }
2832
2833 /* Check if has enablers without bytecode enabled */
2834 list_for_each_entry(enabler_ref,
a67ba386 2835 &event_notifier_priv->parent.enablers_ref_head, node) {
b01155ba
FD
2836 if (enabler_ref->ref->enabled
2837 && list_empty(&enabler_ref->ref->filter_bytecode_head)) {
e2d5dbc7 2838 has_enablers_without_filter_bytecode = 1;
b01155ba
FD
2839 break;
2840 }
2841 }
a67ba386 2842 event_notifier_priv->parent.has_enablers_without_filter_bytecode =
e2d5dbc7 2843 has_enablers_without_filter_bytecode;
b01155ba
FD
2844
2845 /* Enable filters */
2846 list_for_each_entry(runtime,
e2d5dbc7 2847 &event_notifier_priv->parent.filter_bytecode_runtime_head, node) {
8a445457 2848 lttng_bytecode_sync_state(runtime);
e2d5dbc7
MD
2849 nr_filters++;
2850 }
2851 WRITE_ONCE(event_notifier_priv->parent.pub->eval_filter,
2852 !(has_enablers_without_filter_bytecode || !nr_filters));
99d223ad
FD
2853
2854 /* Enable captures */
2855 list_for_each_entry(runtime,
e2d5dbc7 2856 &event_notifier_priv->capture_bytecode_runtime_head, node) {
8a445457 2857 lttng_bytecode_sync_state(runtime);
e2d5dbc7
MD
2858 nr_captures++;
2859 }
2860 WRITE_ONCE(event_notifier->eval_capture, !!nr_captures);
b01155ba
FD
2861 }
2862}
2863
7b25ab0e
MD
2864static
2865void lttng_event_enabler_sync(struct lttng_event_enabler_common *event_enabler)
2866{
2867 switch (event_enabler->enabler_type) {
2868 case LTTNG_EVENT_ENABLER_TYPE_RECORDER:
2869 {
2870 struct lttng_event_recorder_enabler *event_recorder_enabler =
2871 container_of(event_enabler, struct lttng_event_recorder_enabler, parent);
2872 lttng_session_lazy_sync_event_enablers(event_recorder_enabler->chan->parent.session);
2873 break;
2874 }
2875 case LTTNG_EVENT_ENABLER_TYPE_NOTIFIER:
2876 {
2877 struct lttng_event_notifier_enabler *event_notifier_enabler =
2878 container_of(event_enabler, struct lttng_event_notifier_enabler, parent);
2879 lttng_event_notifier_group_sync_enablers(event_notifier_enabler->group);
2880 break;
2881 }
2882 default:
2883 WARN_ON_ONCE(1);
2884 }
2885}
2886
1ec3f75a 2887/*
d83004aa
JD
2888 * Serialize at most one packet worth of metadata into a metadata
2889 * channel.
92d9f5e6
MD
2890 * We grab the metadata cache mutex to get exclusive access to our metadata
2891 * buffer and to the metadata cache. Exclusive access to the metadata buffer
2892 * allows us to do racy operations such as looking for remaining space left in
2893 * packet and write, since mutual exclusion protects us from concurrent writes.
2894 * Mutual exclusion on the metadata cache allow us to read the cache content
2895 * without racing against reallocation of the cache by updates.
35097f36
JD
2896 * Returns the number of bytes written in the channel, 0 if no data
2897 * was written and a negative value on error.
1ec3f75a 2898 */
b3b8072b 2899int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
860c213b 2900 struct lttng_kernel_ring_buffer_channel *chan, bool *coherent)
d83004aa 2901{
8a57ec02 2902 struct lttng_kernel_ring_buffer_ctx ctx;
d83004aa
JD
2903 int ret = 0;
2904 size_t len, reserve_len;
2905
f613e3e6 2906 /*
92d9f5e6
MD
2907 * Ensure we support mutiple get_next / put sequences followed by
2908 * put_next. The metadata cache lock protects reading the metadata
2909 * cache. It can indeed be read concurrently by "get_next_subbuf" and
2910 * "flush" operations on the buffer invoked by different processes.
2911 * Moreover, since the metadata cache memory can be reallocated, we
2912 * need to have exclusive access against updates even though we only
2913 * read it.
f613e3e6 2914 */
92d9f5e6 2915 mutex_lock(&stream->metadata_cache->lock);
f613e3e6
MD
2916 WARN_ON(stream->metadata_in < stream->metadata_out);
2917 if (stream->metadata_in != stream->metadata_out)
de23d59d 2918 goto end;
f613e3e6 2919
9616f0bf
JD
2920 /* Metadata regenerated, change the version. */
2921 if (stream->metadata_cache->version != stream->version)
2922 stream->version = stream->metadata_cache->version;
2923
d83004aa 2924 len = stream->metadata_cache->metadata_written -
f613e3e6 2925 stream->metadata_in;
9de2c215 2926 if (!len)
de23d59d 2927 goto end;
d83004aa 2928 reserve_len = min_t(size_t,
4a399b76 2929 stream->transport->ops.priv->packet_avail_size(chan),
d83004aa 2930 len);
a775608d 2931 lib_ring_buffer_ctx_init(&ctx, chan, reserve_len,
b1199bd3 2932 sizeof(char), NULL);
d83004aa
JD
2933 /*
2934 * If reservation failed, return an error to the caller.
2935 */
c2fb9c1c 2936 ret = stream->transport->ops.event_reserve(&ctx);
d83004aa
JD
2937 if (ret != 0) {
2938 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
8b97fd42 2939 stream->coherent = false;
d83004aa
JD
2940 goto end;
2941 }
b3b8072b 2942 stream->transport->ops.event_write(&ctx,
f613e3e6 2943 stream->metadata_cache->data + stream->metadata_in,
f5ffbd77 2944 reserve_len, 1);
b3b8072b 2945 stream->transport->ops.event_commit(&ctx);
f613e3e6 2946 stream->metadata_in += reserve_len;
3e75e2a7 2947 if (reserve_len < len)
8b97fd42
MD
2948 stream->coherent = false;
2949 else
2950 stream->coherent = true;
d83004aa
JD
2951 ret = reserve_len;
2952
2953end:
8b97fd42
MD
2954 if (coherent)
2955 *coherent = stream->coherent;
92d9f5e6 2956 mutex_unlock(&stream->metadata_cache->lock);
d83004aa
JD
2957 return ret;
2958}
2959
8b97fd42 2960static
8cdc1a81 2961void lttng_metadata_begin(struct lttng_kernel_session *session)
8b97fd42 2962{
8cdc1a81
MD
2963 if (atomic_inc_return(&session->priv->metadata_cache->producing) == 1)
2964 mutex_lock(&session->priv->metadata_cache->lock);
8b97fd42
MD
2965}
2966
2967static
8cdc1a81 2968void lttng_metadata_end(struct lttng_kernel_session *session)
8b97fd42 2969{
8cdc1a81
MD
2970 WARN_ON_ONCE(!atomic_read(&session->priv->metadata_cache->producing));
2971 if (atomic_dec_return(&session->priv->metadata_cache->producing) == 0) {
3e75e2a7
MD
2972 struct lttng_metadata_stream *stream;
2973
8cdc1a81 2974 list_for_each_entry(stream, &session->priv->metadata_cache->metadata_stream, list)
3e75e2a7 2975 wake_up_interruptible(&stream->read_wait);
8cdc1a81 2976 mutex_unlock(&session->priv->metadata_cache->lock);
3e75e2a7 2977 }
8b97fd42
MD
2978}
2979
d83004aa
JD
2980/*
2981 * Write the metadata to the metadata cache.
2982 * Must be called with sessions_mutex held.
92d9f5e6
MD
2983 * The metadata cache lock protects us from concurrent read access from
2984 * thread outputting metadata content to ring buffer.
8b97fd42
MD
2985 * The content of the printf is printed as a single atomic metadata
2986 * transaction.
d83004aa 2987 */
8cdc1a81 2988int lttng_metadata_printf(struct lttng_kernel_session *session,
c099397a
MD
2989 const char *fmt, ...)
2990{
c099397a 2991 char *str;
d83004aa 2992 size_t len;
c099397a
MD
2993 va_list ap;
2994
585e5dcc 2995 WARN_ON_ONCE(!LTTNG_READ_ONCE(session->active));
c099397a
MD
2996
2997 va_start(ap, fmt);
2998 str = kvasprintf(GFP_KERNEL, fmt, ap);
2999 va_end(ap);
3000 if (!str)
3001 return -ENOMEM;
3002
1ec3f75a 3003 len = strlen(str);
8cdc1a81
MD
3004 WARN_ON_ONCE(!atomic_read(&session->priv->metadata_cache->producing));
3005 if (session->priv->metadata_cache->metadata_written + len >
3006 session->priv->metadata_cache->cache_alloc) {
d83004aa
JD
3007 char *tmp_cache_realloc;
3008 unsigned int tmp_cache_alloc_size;
3009
3010 tmp_cache_alloc_size = max_t(unsigned int,
8cdc1a81
MD
3011 session->priv->metadata_cache->cache_alloc + len,
3012 session->priv->metadata_cache->cache_alloc << 1);
0ca42eb7 3013 tmp_cache_realloc = vzalloc(tmp_cache_alloc_size);
d83004aa
JD
3014 if (!tmp_cache_realloc)
3015 goto err;
8cdc1a81 3016 if (session->priv->metadata_cache->data) {
a606b6e8 3017 memcpy(tmp_cache_realloc,
8cdc1a81
MD
3018 session->priv->metadata_cache->data,
3019 session->priv->metadata_cache->cache_alloc);
3020 vfree(session->priv->metadata_cache->data);
a606b6e8
ML
3021 }
3022
8cdc1a81
MD
3023 session->priv->metadata_cache->cache_alloc = tmp_cache_alloc_size;
3024 session->priv->metadata_cache->data = tmp_cache_realloc;
c099397a 3025 }
8cdc1a81
MD
3026 memcpy(session->priv->metadata_cache->data +
3027 session->priv->metadata_cache->metadata_written,
d83004aa 3028 str, len);
8cdc1a81 3029 session->priv->metadata_cache->metadata_written += len;
c099397a 3030 kfree(str);
d83004aa 3031
d83004aa
JD
3032 return 0;
3033
3034err:
3035 kfree(str);
3036 return -ENOMEM;
c099397a
MD
3037}
3038
f513b2bf 3039static
8cdc1a81 3040int print_tabs(struct lttng_kernel_session *session, size_t nesting)
f513b2bf
MD
3041{
3042 size_t i;
3043
3044 for (i = 0; i < nesting; i++) {
3045 int ret;
3046
3047 ret = lttng_metadata_printf(session, " ");
3048 if (ret) {
3049 return ret;
3050 }
3051 }
3052 return 0;
3053}
3054
ceabb767 3055static
8cdc1a81 3056int lttng_field_name_statedump(struct lttng_kernel_session *session,
437d5aa5 3057 const struct lttng_kernel_event_field *field,
ceabb767
MD
3058 size_t nesting)
3059{
3060 return lttng_metadata_printf(session, " _%s;\n", field->name);
3061}
3062
3063static
8cdc1a81 3064int _lttng_integer_type_statedump(struct lttng_kernel_session *session,
437d5aa5
MD
3065 const struct lttng_kernel_type_integer *type,
3066 enum lttng_kernel_string_encoding parent_encoding,
ceabb767
MD
3067 size_t nesting)
3068{
3069 int ret;
3070
ceabb767
MD
3071 ret = print_tabs(session, nesting);
3072 if (ret)
3073 return ret;
3074 ret = lttng_metadata_printf(session,
3075 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s }",
437d5aa5
MD
3076 type->size,
3077 type->alignment,
3078 type->signedness,
3079 (parent_encoding == lttng_kernel_string_encoding_none)
ceabb767 3080 ? "none"
437d5aa5 3081 : (parent_encoding == lttng_kernel_string_encoding_UTF8)
ceabb767
MD
3082 ? "UTF8"
3083 : "ASCII",
437d5aa5 3084 type->base,
ceabb767 3085#if __BYTE_ORDER == __BIG_ENDIAN
437d5aa5 3086 type->reverse_byte_order ? " byte_order = le;" : ""
ceabb767 3087#else
437d5aa5 3088 type->reverse_byte_order ? " byte_order = be;" : ""
ceabb767
MD
3089#endif
3090 );
3091 return ret;
3092}
3093
f513b2bf
MD
3094/*
3095 * Must be called with sessions_mutex held.
3096 */
3097static
8cdc1a81 3098int _lttng_struct_type_statedump(struct lttng_kernel_session *session,
437d5aa5 3099 const struct lttng_kernel_type_struct *type,
f513b2bf
MD
3100 size_t nesting)
3101{
51ef4536 3102 const char *prev_field_name = NULL;
f513b2bf
MD
3103 int ret;
3104 uint32_t i, nr_fields;
ceabb767
MD
3105 unsigned int alignment;
3106
f513b2bf
MD
3107 ret = print_tabs(session, nesting);
3108 if (ret)
3109 return ret;
3110 ret = lttng_metadata_printf(session,
3111 "struct {\n");
3112 if (ret)
3113 return ret;
437d5aa5 3114 nr_fields = type->nr_fields;
f513b2bf 3115 for (i = 0; i < nr_fields; i++) {
437d5aa5 3116 const struct lttng_kernel_event_field *iter_field;
f513b2bf 3117
437d5aa5 3118 iter_field = type->fields[i];
51ef4536 3119 ret = _lttng_field_statedump(session, iter_field, nesting + 1, &prev_field_name);
f513b2bf
MD
3120 if (ret)
3121 return ret;
3122 }
3123 ret = print_tabs(session, nesting);
3124 if (ret)
3125 return ret;
437d5aa5 3126 alignment = type->alignment;
ceabb767
MD
3127 if (alignment) {
3128 ret = lttng_metadata_printf(session,
3129 "} align(%u)",
3130 alignment);
3131 } else {
3132 ret = lttng_metadata_printf(session,
3133 "}");
3134 }
f513b2bf
MD
3135 return ret;
3136}
3137
3138/*
3139 * Must be called with sessions_mutex held.
3140 */
3141static
8cdc1a81 3142int _lttng_struct_field_statedump(struct lttng_kernel_session *session,
437d5aa5 3143 const struct lttng_kernel_event_field *field,
f513b2bf
MD
3144 size_t nesting)
3145{
3146 int ret;
3147
3148 ret = _lttng_struct_type_statedump(session,
437d5aa5 3149 lttng_kernel_get_type_struct(field->type), nesting);
f513b2bf
MD
3150 if (ret)
3151 return ret;
ceabb767 3152 return lttng_field_name_statedump(session, field, nesting);
f513b2bf
MD
3153}
3154
65c85aa6
MD
3155/*
3156 * Must be called with sessions_mutex held.
3157 */
3158static
8cdc1a81 3159int _lttng_variant_type_statedump(struct lttng_kernel_session *session,
437d5aa5 3160 const struct lttng_kernel_type_variant *type,
51ef4536
MD
3161 size_t nesting,
3162 const char *prev_field_name)
65c85aa6 3163{
51ef4536 3164 const char *tag_name;
65c85aa6
MD
3165 int ret;
3166 uint32_t i, nr_choices;
3167
51ef4536
MD
3168 tag_name = type->tag_name;
3169 if (!tag_name)
3170 tag_name = prev_field_name;
3171 if (!tag_name)
3172 return -EINVAL;
ceabb767
MD
3173 /*
3174 * CTF 1.8 does not allow expressing nonzero variant alignment in a nestable way.
3175 */
437d5aa5 3176 if (type->alignment != 0)
ceabb767 3177 return -EINVAL;
65c85aa6
MD
3178 ret = print_tabs(session, nesting);
3179 if (ret)
3180 return ret;
3181 ret = lttng_metadata_printf(session,
3182 "variant <_%s> {\n",
51ef4536 3183 tag_name);
65c85aa6
MD
3184 if (ret)
3185 return ret;
437d5aa5 3186 nr_choices = type->nr_choices;
65c85aa6 3187 for (i = 0; i < nr_choices; i++) {
437d5aa5 3188 const struct lttng_kernel_event_field *iter_field;
65c85aa6 3189
437d5aa5 3190 iter_field = type->choices[i];
51ef4536 3191 ret = _lttng_field_statedump(session, iter_field, nesting + 1, NULL);
65c85aa6
MD
3192 if (ret)
3193 return ret;
3194 }
3195 ret = print_tabs(session, nesting);
3196 if (ret)
3197 return ret;
3198 ret = lttng_metadata_printf(session,
3199 "}");
3200 return ret;
3201}
3202
3203/*
3204 * Must be called with sessions_mutex held.
3205 */
3206static
8cdc1a81 3207int _lttng_variant_field_statedump(struct lttng_kernel_session *session,
437d5aa5 3208 const struct lttng_kernel_event_field *field,
51ef4536
MD
3209 size_t nesting,
3210 const char *prev_field_name)
65c85aa6
MD
3211{
3212 int ret;
3213
3214 ret = _lttng_variant_type_statedump(session,
51ef4536
MD
3215 lttng_kernel_get_type_variant(field->type), nesting,
3216 prev_field_name);
65c85aa6
MD
3217 if (ret)
3218 return ret;
ceabb767 3219 return lttng_field_name_statedump(session, field, nesting);
65c85aa6
MD
3220}
3221
f513b2bf
MD
3222/*
3223 * Must be called with sessions_mutex held.
3224 */
3225static
8cdc1a81 3226int _lttng_array_field_statedump(struct lttng_kernel_session *session,
437d5aa5 3227 const struct lttng_kernel_event_field *field,
f513b2bf
MD
3228 size_t nesting)
3229{
3230 int ret;
437d5aa5
MD
3231 const struct lttng_kernel_type_array *array_type;
3232 const struct lttng_kernel_type_common *elem_type;
f513b2bf 3233
437d5aa5
MD
3234 array_type = lttng_kernel_get_type_array(field->type);
3235 WARN_ON_ONCE(!array_type);
ceabb767 3236
437d5aa5 3237 if (array_type->alignment) {
ceabb767 3238 ret = print_tabs(session, nesting);
f513b2bf
MD
3239 if (ret)
3240 return ret;
ceabb767
MD
3241 ret = lttng_metadata_printf(session,
3242 "struct { } align(%u) _%s_padding;\n",
437d5aa5 3243 array_type->alignment * CHAR_BIT,
ceabb767
MD
3244 field->name);
3245 if (ret)
3246 return ret;
3247 }
3248 /*
3249 * Nested compound types: Only array of structures and variants are
3250 * currently supported.
3251 */
437d5aa5 3252 elem_type = array_type->elem_type;
12bb2edb
MD
3253 switch (elem_type->type) {
3254 case lttng_kernel_type_integer:
437d5aa5
MD
3255 case lttng_kernel_type_struct:
3256 case lttng_kernel_type_variant:
3257 ret = _lttng_type_statedump(session, elem_type,
3258 array_type->encoding, nesting);
65c85aa6
MD
3259 if (ret)
3260 return ret;
3261 break;
ceabb767 3262
f513b2bf
MD
3263 default:
3264 return -EINVAL;
3265 }
3266 ret = lttng_metadata_printf(session,
3267 " _%s[%u];\n",
3268 field->name,
437d5aa5 3269 array_type->length);
f513b2bf
MD
3270 return ret;
3271}
3272
3273/*
3274 * Must be called with sessions_mutex held.
3275 */
3276static
8cdc1a81 3277int _lttng_sequence_field_statedump(struct lttng_kernel_session *session,
437d5aa5 3278 const struct lttng_kernel_event_field *field,
51ef4536
MD
3279 size_t nesting,
3280 const char *prev_field_name)
f513b2bf
MD
3281{
3282 int ret;
3283 const char *length_name;
437d5aa5
MD
3284 const struct lttng_kernel_type_sequence *sequence_type;
3285 const struct lttng_kernel_type_common *elem_type;
f513b2bf 3286
437d5aa5
MD
3287 sequence_type = lttng_kernel_get_type_sequence(field->type);
3288 WARN_ON_ONCE(!sequence_type);
f513b2bf 3289
437d5aa5 3290 length_name = sequence_type->length_name;
51ef4536
MD
3291 if (!length_name)
3292 length_name = prev_field_name;
3293 if (!length_name)
3294 return -EINVAL;
ceabb767 3295
437d5aa5 3296 if (sequence_type->alignment) {
ceabb767 3297 ret = print_tabs(session, nesting);
f513b2bf
MD
3298 if (ret)
3299 return ret;
ceabb767
MD
3300 ret = lttng_metadata_printf(session,
3301 "struct { } align(%u) _%s_padding;\n",
437d5aa5 3302 sequence_type->alignment * CHAR_BIT,
ceabb767
MD
3303 field->name);
3304 if (ret)
3305 return ret;
3306 }
3307
3308 /*
3309 * Nested compound types: Only array of structures and variants are
3310 * currently supported.
3311 */
437d5aa5 3312 elem_type = sequence_type->elem_type;
12bb2edb
MD
3313 switch (elem_type->type) {
3314 case lttng_kernel_type_integer:
437d5aa5
MD
3315 case lttng_kernel_type_struct:
3316 case lttng_kernel_type_variant:
3317 ret = _lttng_type_statedump(session, elem_type,
3318 sequence_type->encoding, nesting);
65c85aa6
MD
3319 if (ret)
3320 return ret;
3321 break;
ceabb767 3322
f513b2bf
MD
3323 default:
3324 return -EINVAL;
3325 }
3326 ret = lttng_metadata_printf(session,
3327 " _%s[ _%s ];\n",
3328 field->name,
51ef4536 3329 length_name);
f513b2bf
MD
3330 return ret;
3331}
3332
141ddf28
MD
3333/*
3334 * Must be called with sessions_mutex held.
3335 */
3336static
8cdc1a81 3337int _lttng_enum_type_statedump(struct lttng_kernel_session *session,
437d5aa5 3338 const struct lttng_kernel_type_enum *type,
141ddf28
MD
3339 size_t nesting)
3340{
d96a4a7a 3341 const struct lttng_kernel_enum_desc *enum_desc;
437d5aa5 3342 const struct lttng_kernel_type_common *container_type;
141ddf28
MD
3343 int ret;
3344 unsigned int i, nr_entries;
3345
437d5aa5 3346 container_type = type->container_type;
12bb2edb 3347 if (container_type->type != lttng_kernel_type_integer) {
ceabb767
MD
3348 ret = -EINVAL;
3349 goto end;
3350 }
437d5aa5 3351 enum_desc = type->desc;
141ddf28
MD
3352 nr_entries = enum_desc->nr_entries;
3353
3354 ret = print_tabs(session, nesting);
3355 if (ret)
3356 goto end;
ceabb767
MD
3357 ret = lttng_metadata_printf(session, "enum : ");
3358 if (ret)
3359 goto end;
437d5aa5
MD
3360 ret = _lttng_integer_type_statedump(session, lttng_kernel_get_type_integer(container_type),
3361 lttng_kernel_string_encoding_none, 0);
141ddf28 3362 if (ret)
ceabb767
MD
3363 goto end;
3364 ret = lttng_metadata_printf(session, " {\n");
3365 if (ret)
3366 goto end;
141ddf28
MD
3367 /* Dump all entries */
3368 for (i = 0; i < nr_entries; i++) {
437d5aa5 3369 const struct lttng_kernel_enum_entry *entry = enum_desc->entries[i];
141ddf28
MD
3370 int j, len;
3371
3372 ret = print_tabs(session, nesting + 1);
3373 if (ret)
3374 goto end;
3375 ret = lttng_metadata_printf(session,
3376 "\"");
3377 if (ret)
3378 goto end;
3379 len = strlen(entry->string);
3380 /* Escape the character '"' */
3381 for (j = 0; j < len; j++) {
3382 char c = entry->string[j];
3383
3384 switch (c) {
3385 case '"':
3386 ret = lttng_metadata_printf(session,
3387 "\\\"");
3388 break;
3389 case '\\':
3390 ret = lttng_metadata_printf(session,
3391 "\\\\");
3392 break;
3393 default:
3394 ret = lttng_metadata_printf(session,
3395 "%c", c);
3396 break;
3397 }
3398 if (ret)
3399 goto end;
3400 }
08ad1061
PP
3401 ret = lttng_metadata_printf(session, "\"");
3402 if (ret)
3403 goto end;
3404
3405 if (entry->options.is_auto) {
3406 ret = lttng_metadata_printf(session, ",\n");
3407 if (ret)
3408 goto end;
141ddf28 3409 } else {
7c559490
MD
3410 ret = lttng_metadata_printf(session,
3411 " = ");
3412 if (ret)
3413 goto end;
3414 if (entry->start.signedness)
141ddf28 3415 ret = lttng_metadata_printf(session,
7c559490
MD
3416 "%lld", (long long) entry->start.value);
3417 else
3418 ret = lttng_metadata_printf(session,
3419 "%llu", entry->start.value);
3420 if (ret)
3421 goto end;
3422 if (entry->start.signedness == entry->end.signedness &&
3423 entry->start.value
3424 == entry->end.value) {
141ddf28 3425 ret = lttng_metadata_printf(session,
7c559490
MD
3426 ",\n");
3427 } else {
3428 if (entry->end.signedness) {
3429 ret = lttng_metadata_printf(session,
3430 " ... %lld,\n",
3431 (long long) entry->end.value);
3432 } else {
3433 ret = lttng_metadata_printf(session,
3434 " ... %llu,\n",
3435 entry->end.value);
3436 }
141ddf28 3437 }
7c559490
MD
3438 if (ret)
3439 goto end;
141ddf28 3440 }
141ddf28
MD
3441 }
3442 ret = print_tabs(session, nesting);
3443 if (ret)
3444 goto end;
ceabb767 3445 ret = lttng_metadata_printf(session, "}");
141ddf28
MD
3446end:
3447 return ret;
3448}
3449
d83004aa
JD
3450/*
3451 * Must be called with sessions_mutex held.
3452 */
c099397a 3453static
8cdc1a81 3454int _lttng_enum_field_statedump(struct lttng_kernel_session *session,
437d5aa5 3455 const struct lttng_kernel_event_field *field,
ceabb767
MD
3456 size_t nesting)
3457{
3458 int ret;
437d5aa5 3459 const struct lttng_kernel_type_enum *enum_type;
ceabb767 3460
437d5aa5
MD
3461 enum_type = lttng_kernel_get_type_enum(field->type);
3462 WARN_ON_ONCE(!enum_type);
3463 ret = _lttng_enum_type_statedump(session, enum_type, nesting);
ceabb767
MD
3464 if (ret)
3465 return ret;
3466 return lttng_field_name_statedump(session, field, nesting);
3467}
3468
3469static
8cdc1a81 3470int _lttng_integer_field_statedump(struct lttng_kernel_session *session,
437d5aa5 3471 const struct lttng_kernel_event_field *field,
ceabb767
MD
3472 size_t nesting)
3473{
3474 int ret;
3475
437d5aa5
MD
3476 ret = _lttng_integer_type_statedump(session, lttng_kernel_get_type_integer(field->type),
3477 lttng_kernel_string_encoding_none, nesting);
ceabb767
MD
3478 if (ret)
3479 return ret;
3480 return lttng_field_name_statedump(session, field, nesting);
3481}
3482
3483static
8cdc1a81 3484int _lttng_string_type_statedump(struct lttng_kernel_session *session,
437d5aa5 3485 const struct lttng_kernel_type_string *type,
ceabb767
MD
3486 size_t nesting)
3487{
3488 int ret;
3489
ceabb767
MD
3490 /* Default encoding is UTF8 */
3491 ret = print_tabs(session, nesting);
3492 if (ret)
3493 return ret;
3494 ret = lttng_metadata_printf(session,
3495 "string%s",
437d5aa5 3496 type->encoding == lttng_kernel_string_encoding_ASCII ?
ceabb767
MD
3497 " { encoding = ASCII; }" : "");
3498 return ret;
3499}
3500
3501static
8cdc1a81 3502int _lttng_string_field_statedump(struct lttng_kernel_session *session,
437d5aa5 3503 const struct lttng_kernel_event_field *field,
f513b2bf 3504 size_t nesting)
ceabb767 3505{
437d5aa5 3506 const struct lttng_kernel_type_string *string_type;
ceabb767
MD
3507 int ret;
3508
437d5aa5
MD
3509 string_type = lttng_kernel_get_type_string(field->type);
3510 WARN_ON_ONCE(!string_type);
3511 ret = _lttng_string_type_statedump(session, string_type, nesting);
ceabb767
MD
3512 if (ret)
3513 return ret;
3514 return lttng_field_name_statedump(session, field, nesting);
3515}
3516
3517/*
3518 * Must be called with sessions_mutex held.
3519 */
3520static
8cdc1a81 3521int _lttng_type_statedump(struct lttng_kernel_session *session,
437d5aa5
MD
3522 const struct lttng_kernel_type_common *type,
3523 enum lttng_kernel_string_encoding parent_encoding,
ceabb767 3524 size_t nesting)
c099397a 3525{
c099397a 3526 int ret = 0;
c099397a 3527
12bb2edb
MD
3528 switch (type->type) {
3529 case lttng_kernel_type_integer:
437d5aa5
MD
3530 ret = _lttng_integer_type_statedump(session,
3531 lttng_kernel_get_type_integer(type),
3532 parent_encoding, nesting);
8070f5c0 3533 break;
437d5aa5
MD
3534 case lttng_kernel_type_enum:
3535 ret = _lttng_enum_type_statedump(session,
3536 lttng_kernel_get_type_enum(type),
3537 nesting);
8070f5c0 3538 break;
12bb2edb 3539 case lttng_kernel_type_string:
437d5aa5
MD
3540 ret = _lttng_string_type_statedump(session,
3541 lttng_kernel_get_type_string(type),
3542 nesting);
ceabb767 3543 break;
437d5aa5
MD
3544 case lttng_kernel_type_struct:
3545 ret = _lttng_struct_type_statedump(session,
3546 lttng_kernel_get_type_struct(type),
3547 nesting);
8070f5c0 3548 break;
437d5aa5
MD
3549 case lttng_kernel_type_variant:
3550 ret = _lttng_variant_type_statedump(session,
3551 lttng_kernel_get_type_variant(type),
51ef4536 3552 nesting, NULL);
ceabb767
MD
3553 break;
3554
3555 /* Nested arrays and sequences are not supported yet. */
437d5aa5
MD
3556 case lttng_kernel_type_array:
3557 case lttng_kernel_type_sequence:
ceabb767
MD
3558 default:
3559 WARN_ON_ONCE(1);
3560 return -EINVAL;
8070f5c0 3561 }
ceabb767
MD
3562 return ret;
3563}
8070f5c0 3564
ceabb767
MD
3565/*
3566 * Must be called with sessions_mutex held.
3567 */
3568static
8cdc1a81 3569int _lttng_field_statedump(struct lttng_kernel_session *session,
437d5aa5 3570 const struct lttng_kernel_event_field *field,
51ef4536
MD
3571 size_t nesting,
3572 const char **prev_field_name_p)
ceabb767 3573{
51ef4536 3574 const char *prev_field_name = NULL;
ceabb767 3575 int ret = 0;
27d2368e 3576
51ef4536
MD
3577 if (prev_field_name_p)
3578 prev_field_name = *prev_field_name_p;
437d5aa5 3579 switch (field->type->type) {
12bb2edb 3580 case lttng_kernel_type_integer:
ceabb767
MD
3581 ret = _lttng_integer_field_statedump(session, field, nesting);
3582 break;
437d5aa5 3583 case lttng_kernel_type_enum:
ceabb767 3584 ret = _lttng_enum_field_statedump(session, field, nesting);
8070f5c0 3585 break;
12bb2edb 3586 case lttng_kernel_type_string:
ceabb767 3587 ret = _lttng_string_field_statedump(session, field, nesting);
8070f5c0 3588 break;
437d5aa5 3589 case lttng_kernel_type_struct:
ceabb767 3590 ret = _lttng_struct_field_statedump(session, field, nesting);
f513b2bf 3591 break;
437d5aa5 3592 case lttng_kernel_type_array:
ceabb767 3593 ret = _lttng_array_field_statedump(session, field, nesting);
f513b2bf 3594 break;
437d5aa5 3595 case lttng_kernel_type_sequence:
51ef4536 3596 ret = _lttng_sequence_field_statedump(session, field, nesting, prev_field_name);
f513b2bf 3597 break;
437d5aa5 3598 case lttng_kernel_type_variant:
51ef4536 3599 ret = _lttng_variant_field_statedump(session, field, nesting, prev_field_name);
65c85aa6 3600 break;
f513b2bf 3601
8070f5c0
MD
3602 default:
3603 WARN_ON_ONCE(1);
3604 return -EINVAL;
3605 }
51ef4536
MD
3606 if (prev_field_name_p)
3607 *prev_field_name_p = field->name;
8070f5c0
MD
3608 return ret;
3609}
3610
3611static
8cdc1a81 3612int _lttng_context_metadata_statedump(struct lttng_kernel_session *session,
437d5aa5 3613 struct lttng_kernel_ctx *ctx)
8070f5c0 3614{
51ef4536 3615 const char *prev_field_name = NULL;
8070f5c0
MD
3616 int ret = 0;
3617 int i;
3618
3619 if (!ctx)
3620 return 0;
3621 for (i = 0; i < ctx->nr_fields; i++) {
437d5aa5 3622 const struct lttng_kernel_ctx_field *field = &ctx->fields[i];
8070f5c0 3623
51ef4536 3624 ret = _lttng_field_statedump(session, field->event_field, 2, &prev_field_name);
8070f5c0
MD
3625 if (ret)
3626 return ret;
3627 }
3628 return ret;
3629}
3630
3631static
8cdc1a81 3632int _lttng_fields_metadata_statedump(struct lttng_kernel_session *session,
a67ba386 3633 struct lttng_kernel_event_recorder *event_recorder)
8070f5c0 3634{
51ef4536 3635 const char *prev_field_name = NULL;
a67ba386 3636 const struct lttng_kernel_event_desc *desc = event_recorder->priv->parent.desc;
8070f5c0
MD
3637 int ret = 0;
3638 int i;
3639
6c8c025b
MD
3640 for (i = 0; i < desc->tp_class->nr_fields; i++) {
3641 const struct lttng_kernel_event_field *field = desc->tp_class->fields[i];
8070f5c0 3642
51ef4536 3643 ret = _lttng_field_statedump(session, field, 2, &prev_field_name);
8070f5c0
MD
3644 if (ret)
3645 return ret;
c099397a
MD
3646 }
3647 return ret;
3648}
3649
d83004aa
JD
3650/*
3651 * Must be called with sessions_mutex held.
8b97fd42
MD
3652 * The entire event metadata is printed as a single atomic metadata
3653 * transaction.
d83004aa 3654 */
c099397a 3655static
8cdc1a81 3656int _lttng_event_metadata_statedump(struct lttng_kernel_session *session,
f7d06400 3657 struct lttng_kernel_channel_buffer *chan,
a67ba386 3658 struct lttng_kernel_event_recorder *event_recorder)
c099397a
MD
3659{
3660 int ret = 0;
3661
a67ba386 3662 if (event_recorder->priv->metadata_dumped || !LTTNG_READ_ONCE(session->active))
c099397a 3663 return 0;
f7d06400 3664 if (chan->priv->channel_type == METADATA_CHANNEL)
c099397a
MD
3665 return 0;
3666
8b97fd42
MD
3667 lttng_metadata_begin(session);
3668
c099397a
MD
3669 ret = lttng_metadata_printf(session,
3670 "event {\n"
ae734547 3671 " name = \"%s\";\n"
c099397a 3672 " id = %u;\n"
b9074a1b 3673 " stream_id = %u;\n",
a67ba386
MD
3674 event_recorder->priv->parent.desc->event_name,
3675 event_recorder->priv->id,
f7d06400 3676 event_recorder->chan->priv->id);
c099397a
MD
3677 if (ret)
3678 goto end;
3679
8070f5c0 3680 ret = lttng_metadata_printf(session,
8070f5c0
MD
3681 " fields := struct {\n"
3682 );
3683 if (ret)
3684 goto end;
3685
a67ba386 3686 ret = _lttng_fields_metadata_statedump(session, event_recorder);
c099397a
MD
3687 if (ret)
3688 goto end;
3689
3690 /*
3691 * LTTng space reservation can only reserve multiples of the
3692 * byte size.
3693 */
3694 ret = lttng_metadata_printf(session,
9115fbdc
MD
3695 " };\n"
3696 "};\n\n");
c099397a
MD
3697 if (ret)
3698 goto end;
3699
a67ba386 3700 event_recorder->priv->metadata_dumped = 1;
c099397a 3701end:
8b97fd42 3702 lttng_metadata_end(session);
c099397a
MD
3703 return ret;
3704
3705}
3706
d83004aa
JD
3707/*
3708 * Must be called with sessions_mutex held.
8b97fd42
MD
3709 * The entire channel metadata is printed as a single atomic metadata
3710 * transaction.
d83004aa 3711 */
c099397a 3712static
8cdc1a81 3713int _lttng_channel_metadata_statedump(struct lttng_kernel_session *session,
f7d06400 3714 struct lttng_kernel_channel_buffer *chan)
c099397a
MD
3715{
3716 int ret = 0;
3717
f7d06400 3718 if (chan->priv->metadata_dumped || !LTTNG_READ_ONCE(session->active))
c099397a 3719 return 0;
d83004aa 3720
f7d06400 3721 if (chan->priv->channel_type == METADATA_CHANNEL)
c099397a
MD
3722 return 0;
3723
8b97fd42
MD
3724 lttng_metadata_begin(session);
3725
f7d06400 3726 WARN_ON_ONCE(!chan->priv->header_type);
c099397a
MD
3727 ret = lttng_metadata_printf(session,
3728 "stream {\n"
3729 " id = %u;\n"
9115fbdc 3730 " event.header := %s;\n"
b9074a1b 3731 " packet.context := struct packet_context;\n",
f7d06400
MD
3732 chan->priv->id,
3733 chan->priv->header_type == 1 ? "struct event_header_compact" :
c099397a
MD
3734 "struct event_header_large");
3735 if (ret)
3736 goto end;
3737
f7d06400 3738 if (chan->priv->ctx) {
b9074a1b
MD
3739 ret = lttng_metadata_printf(session,
3740 " event.context := struct {\n");
3741 if (ret)
3742 goto end;
3743 }
f7d06400 3744 ret = _lttng_context_metadata_statedump(session, chan->priv->ctx);
8070f5c0
MD
3745 if (ret)
3746 goto end;
f7d06400 3747 if (chan->priv->ctx) {
b9074a1b
MD
3748 ret = lttng_metadata_printf(session,
3749 " };\n");
3750 if (ret)
3751 goto end;
3752 }
8070f5c0
MD
3753
3754 ret = lttng_metadata_printf(session,
b9074a1b 3755 "};\n\n");
8070f5c0 3756
f7d06400 3757 chan->priv->metadata_dumped = 1;
c099397a 3758end:
8b97fd42 3759 lttng_metadata_end(session);
c099397a
MD
3760 return ret;
3761}
3762
d83004aa
JD
3763/*
3764 * Must be called with sessions_mutex held.
3765 */
9115fbdc 3766static
8cdc1a81 3767int _lttng_stream_packet_context_declare(struct lttng_kernel_session *session)
9115fbdc
MD
3768{
3769 return lttng_metadata_printf(session,
3770 "struct packet_context {\n"
a3ccff4f
MD
3771 " uint64_clock_monotonic_t timestamp_begin;\n"
3772 " uint64_clock_monotonic_t timestamp_end;\n"
576ca06a
MD
3773 " uint64_t content_size;\n"
3774 " uint64_t packet_size;\n"
5b3cf4f9 3775 " uint64_t packet_seq_num;\n"
a9afe705 3776 " unsigned long events_discarded;\n"
9115fbdc 3777 " uint32_t cpu_id;\n"
c6dfdf6f 3778 "};\n\n"
9115fbdc
MD
3779 );
3780}
3781
3782/*
3783 * Compact header:
3784 * id: range: 0 - 30.
3785 * id 31 is reserved to indicate an extended header.
3786 *
3787 * Large header:
3788 * id: range: 0 - 65534.
3789 * id 65535 is reserved to indicate an extended header.
d83004aa
JD
3790 *
3791 * Must be called with sessions_mutex held.
9115fbdc
MD
3792 */
3793static
8cdc1a81 3794int _lttng_event_header_declare(struct lttng_kernel_session *session)
9115fbdc
MD
3795{
3796 return lttng_metadata_printf(session,
3797 "struct event_header_compact {\n"
3798 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
3799 " variant <id> {\n"
3800 " struct {\n"
a3ccff4f 3801 " uint27_clock_monotonic_t timestamp;\n"
9115fbdc
MD
3802 " } compact;\n"
3803 " struct {\n"
3804 " uint32_t id;\n"
a3ccff4f 3805 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
3806 " } extended;\n"
3807 " } v;\n"
3808 "} align(%u);\n"
3809 "\n"
3810 "struct event_header_large {\n"
3811 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
3812 " variant <id> {\n"
3813 " struct {\n"
a3ccff4f 3814 " uint32_clock_monotonic_t timestamp;\n"
9115fbdc
MD
3815 " } compact;\n"
3816 " struct {\n"
3817 " uint32_t id;\n"
a3ccff4f 3818 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
3819 " } extended;\n"
3820 " } v;\n"
3821 "} align(%u);\n\n",
a90917c3
MD
3822 lttng_alignof(uint32_t) * CHAR_BIT,
3823 lttng_alignof(uint16_t) * CHAR_BIT
9115fbdc
MD
3824 );
3825}
3826
a3ccff4f
MD
3827 /*
3828 * Approximation of NTP time of day to clock monotonic correlation,
3829 * taken at start of trace.
3830 * Yes, this is only an approximation. Yes, we can (and will) do better
3831 * in future versions.
5283350b
MD
3832 * This function may return a negative offset. It may happen if the
3833 * system sets the REALTIME clock to 0 after boot.
3636085f
MJ
3834 *
3835 * Use 64bit timespec on kernels that have it, this makes 32bit arch
3836 * y2038 compliant.
a3ccff4f
MD
3837 */
3838static
8967b2a3 3839int64_t measure_clock_offset(void)
a3ccff4f 3840{
dcb93448 3841 uint64_t monotonic_avg, monotonic[2], realtime;
2754583e 3842 uint64_t tcf = trace_clock_freq();
dcb93448 3843 int64_t offset;
a3ccff4f 3844 unsigned long flags;
3636085f
MJ
3845#ifdef LTTNG_KERNEL_HAS_TIMESPEC64
3846 struct timespec64 rts = { 0, 0 };
3847#else
3848 struct timespec rts = { 0, 0 };
3849#endif
a3ccff4f
MD
3850
3851 /* Disable interrupts to increase correlation precision. */
3852 local_irq_save(flags);
3853 monotonic[0] = trace_clock_read64();
3636085f
MJ
3854#ifdef LTTNG_KERNEL_HAS_TIMESPEC64
3855 ktime_get_real_ts64(&rts);
3856#else
216b6baa 3857 getnstimeofday(&rts);
3636085f 3858#endif
a3ccff4f
MD
3859 monotonic[1] = trace_clock_read64();
3860 local_irq_restore(flags);
3861
dcb93448 3862 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
2754583e
MD
3863 realtime = (uint64_t) rts.tv_sec * tcf;
3864 if (tcf == NSEC_PER_SEC) {
3865 realtime += rts.tv_nsec;
3866 } else {
3867 uint64_t n = rts.tv_nsec * tcf;
3868
3869 do_div(n, NSEC_PER_SEC);
3870 realtime += n;
3871 }
dcb93448 3872 offset = (int64_t) realtime - monotonic_avg;
a3ccff4f
MD
3873 return offset;
3874}
3875
3eb95be4 3876static
8cdc1a81 3877int print_escaped_ctf_string(struct lttng_kernel_session *session, const char *string)
3eb95be4 3878{
0f3eaad4 3879 int ret = 0;
3eb95be4
JR
3880 size_t i;
3881 char cur;
3882
3883 i = 0;
3884 cur = string[i];
3885 while (cur != '\0') {
3886 switch (cur) {
3887 case '\n':
3888 ret = lttng_metadata_printf(session, "%s", "\\n");
3889 break;
3890 case '\\':
3891 case '"':
3892 ret = lttng_metadata_printf(session, "%c", '\\');
3893 if (ret)
3894 goto error;
3895 /* We still print the current char */
c190d76e 3896 lttng_fallthrough;
3eb95be4
JR
3897 default:
3898 ret = lttng_metadata_printf(session, "%c", cur);
3899 break;
3900 }
3901
3902 if (ret)
3903 goto error;
3904
3905 cur = string[++i];
3906 }
3907error:
3908 return ret;
3909}
3910
3911static
8cdc1a81 3912int print_metadata_escaped_field(struct lttng_kernel_session *session, const char *field,
1c88f269 3913 const char *field_value)
3eb95be4
JR
3914{
3915 int ret;
3916
1c88f269 3917 ret = lttng_metadata_printf(session, " %s = \"", field);
3eb95be4
JR
3918 if (ret)
3919 goto error;
3920
1c88f269 3921 ret = print_escaped_ctf_string(session, field_value);
3eb95be4
JR
3922 if (ret)
3923 goto error;
3924
3925 ret = lttng_metadata_printf(session, "\";\n");
3926
3927error:
3928 return ret;
3929}
3930
c099397a
MD
3931/*
3932 * Output metadata into this session's metadata buffers.
d83004aa 3933 * Must be called with sessions_mutex held.
c099397a
MD
3934 */
3935static
8cdc1a81 3936int _lttng_session_metadata_statedump(struct lttng_kernel_session *session)
c099397a 3937{
8cdc1a81 3938 unsigned char *uuid_c = session->priv->uuid.b;
a82c63f1 3939 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
c88f762e 3940 const char *product_uuid;
f7d06400 3941 struct lttng_kernel_channel_buffer_private *chan_priv;
a67ba386 3942 struct lttng_kernel_event_recorder_private *event_recorder_priv;
c099397a
MD
3943 int ret = 0;
3944
585e5dcc 3945 if (!LTTNG_READ_ONCE(session->active))
c099397a 3946 return 0;
8b97fd42
MD
3947
3948 lttng_metadata_begin(session);
3949
8cdc1a81 3950 if (session->priv->metadata_dumped)
c099397a 3951 goto skip_session;
c099397a 3952
d793d5e1 3953 snprintf(uuid_s, sizeof(uuid_s),
30bdb6e4
MD
3954 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
3955 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
3956 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
3957 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
3958 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
d793d5e1
MD
3959
3960 ret = lttng_metadata_printf(session,
9115fbdc
MD
3961 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
3962 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
3963 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
3964 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
a9afe705 3965 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
9115fbdc
MD
3966 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
3967 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
d793d5e1
MD
3968 "\n"
3969 "trace {\n"
3970 " major = %u;\n"
3971 " minor = %u;\n"
30bdb6e4 3972 " uuid = \"%s\";\n"
d793d5e1
MD
3973 " byte_order = %s;\n"
3974 " packet.header := struct {\n"
3975 " uint32_t magic;\n"
1ec3f75a 3976 " uint8_t uuid[16];\n"
d793d5e1 3977 " uint32_t stream_id;\n"
5594698f 3978 " uint64_t stream_instance_id;\n"
0eb25f58 3979 " };\n"
d793d5e1 3980 "};\n\n",
a90917c3
MD
3981 lttng_alignof(uint8_t) * CHAR_BIT,
3982 lttng_alignof(uint16_t) * CHAR_BIT,
3983 lttng_alignof(uint32_t) * CHAR_BIT,
3984 lttng_alignof(uint64_t) * CHAR_BIT,
a9afe705
MD
3985 sizeof(unsigned long) * CHAR_BIT,
3986 lttng_alignof(unsigned long) * CHAR_BIT,
c6c9e10f
MD
3987 CTF_SPEC_MAJOR,
3988 CTF_SPEC_MINOR,
d793d5e1 3989 uuid_s,
5214fa50 3990#if __BYTE_ORDER == __BIG_ENDIAN
d793d5e1
MD
3991 "be"
3992#else
3993 "le"
3994#endif
3995 );
3996 if (ret)
3997 goto end;
3998
99dc9597
MD
3999 ret = lttng_metadata_printf(session,
4000 "env {\n"
a6058143 4001 " hostname = \"%s\";\n"
c6c9e10f 4002 " domain = \"kernel\";\n"
99dc9597 4003 " sysname = \"%s\";\n"
c6c9e10f
MD
4004 " kernel_release = \"%s\";\n"
4005 " kernel_version = \"%s\";\n"
4006 " tracer_name = \"lttng-modules\";\n"
4007 " tracer_major = %d;\n"
4008 " tracer_minor = %d;\n"
4009 " tracer_patchlevel = %d;\n"
3eb95be4 4010 " trace_buffering_scheme = \"global\";\n",
3d0d43db 4011 current->nsproxy->uts_ns->name.nodename,
99dc9597
MD
4012 utsname()->sysname,
4013 utsname()->release,
c6c9e10f
MD
4014 utsname()->version,
4015 LTTNG_MODULES_MAJOR_VERSION,
4016 LTTNG_MODULES_MINOR_VERSION,
4017 LTTNG_MODULES_PATCHLEVEL_VERSION
99dc9597
MD
4018 );
4019 if (ret)
4020 goto end;
4021
8cdc1a81 4022 ret = print_metadata_escaped_field(session, "trace_name", session->priv->name);
1c88f269
JR
4023 if (ret)
4024 goto end;
4025 ret = print_metadata_escaped_field(session, "trace_creation_datetime",
8cdc1a81 4026 session->priv->creation_time);
3eb95be4
JR
4027 if (ret)
4028 goto end;
4029
c88f762e
GB
4030 /* Add the product UUID to the 'env' section */
4031 product_uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
4032 if (product_uuid) {
4033 ret = lttng_metadata_printf(session,
4034 " product_uuid = \"%s\";\n",
4035 product_uuid
4036 );
4037 if (ret)
4038 goto end;
4039 }
4040
4041 /* Close the 'env' section */
3eb95be4
JR
4042 ret = lttng_metadata_printf(session, "};\n\n");
4043 if (ret)
4044 goto end;
4045
a3ccff4f
MD
4046 ret = lttng_metadata_printf(session,
4047 "clock {\n"
2754583e
MD
4048 " name = \"%s\";\n",
4049 trace_clock_name()
a82c63f1
MD
4050 );
4051 if (ret)
4052 goto end;
4053
4054 if (!trace_clock_uuid(clock_uuid_s)) {
4055 ret = lttng_metadata_printf(session,
7c27cb17 4056 " uuid = \"%s\";\n",
a82c63f1
MD
4057 clock_uuid_s
4058 );
4059 if (ret)
4060 goto end;
4061 }
4062
4063 ret = lttng_metadata_printf(session,
2754583e 4064 " description = \"%s\";\n"
a3ccff4f
MD
4065 " freq = %llu; /* Frequency, in Hz */\n"
4066 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
8967b2a3 4067 " offset = %lld;\n"
a3ccff4f 4068 "};\n\n",
2754583e 4069 trace_clock_description(),
a3ccff4f 4070 (unsigned long long) trace_clock_freq(),
8967b2a3 4071 (long long) measure_clock_offset()
a3ccff4f
MD
4072 );
4073 if (ret)
4074 goto end;
4075
4076 ret = lttng_metadata_printf(session,
4077 "typealias integer {\n"
4078 " size = 27; align = 1; signed = false;\n"
2754583e 4079 " map = clock.%s.value;\n"
a3ccff4f
MD
4080 "} := uint27_clock_monotonic_t;\n"
4081 "\n"
4082 "typealias integer {\n"
4083 " size = 32; align = %u; signed = false;\n"
2754583e 4084 " map = clock.%s.value;\n"
a3ccff4f
MD
4085 "} := uint32_clock_monotonic_t;\n"
4086 "\n"
4087 "typealias integer {\n"
4088 " size = 64; align = %u; signed = false;\n"
2754583e 4089 " map = clock.%s.value;\n"
a3ccff4f 4090 "} := uint64_clock_monotonic_t;\n\n",
2754583e 4091 trace_clock_name(),
a3ccff4f 4092 lttng_alignof(uint32_t) * CHAR_BIT,
2754583e
MD
4093 trace_clock_name(),
4094 lttng_alignof(uint64_t) * CHAR_BIT,
4095 trace_clock_name()
a3ccff4f
MD
4096 );
4097 if (ret)
4098 goto end;
4099
a90917c3 4100 ret = _lttng_stream_packet_context_declare(session);
9115fbdc
MD
4101 if (ret)
4102 goto end;
4103
a90917c3 4104 ret = _lttng_event_header_declare(session);
9115fbdc
MD
4105 if (ret)
4106 goto end;
4107
c099397a 4108skip_session:
f7d06400
MD
4109 list_for_each_entry(chan_priv, &session->priv->chan, node) {
4110 ret = _lttng_channel_metadata_statedump(session, chan_priv->pub);
c099397a
MD
4111 if (ret)
4112 goto end;
4113 }
4114
8cdc1a81 4115 list_for_each_entry(event_recorder_priv, &session->priv->events, node) {
a67ba386
MD
4116 ret = _lttng_event_metadata_statedump(session, event_recorder_priv->pub->chan,
4117 event_recorder_priv->pub);
c099397a
MD
4118 if (ret)
4119 goto end;
4120 }
8cdc1a81 4121 session->priv->metadata_dumped = 1;
c099397a 4122end:
8b97fd42 4123 lttng_metadata_end(session);
c099397a
MD
4124 return ret;
4125}
4126
c0e31d2e 4127/**
a90917c3 4128 * lttng_transport_register - LTT transport registration
c0e31d2e
MD
4129 * @transport: transport structure
4130 *
4131 * Registers a transport which can be used as output to extract the data out of
4132 * LTTng. The module calling this registration function must ensure that no
4133 * trap-inducing code will be executed by the transport functions. E.g.
263b6c88 4134 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
c0e31d2e 4135 * is made visible to the transport function. This registration acts as a
263b6c88 4136 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
c0e31d2e
MD
4137 * after its registration must it synchronize the TLBs.
4138 */
a90917c3 4139void lttng_transport_register(struct lttng_transport *transport)
c0e31d2e
MD
4140{
4141 /*
4142 * Make sure no page fault can be triggered by the module about to be
4143 * registered. We deal with this here so we don't have to call
263b6c88 4144 * vmalloc_sync_mappings() in each module's init.
c0e31d2e 4145 */
263b6c88 4146 wrapper_vmalloc_sync_mappings();
c0e31d2e
MD
4147
4148 mutex_lock(&sessions_mutex);
a90917c3 4149 list_add_tail(&transport->node, &lttng_transport_list);
c0e31d2e
MD
4150 mutex_unlock(&sessions_mutex);
4151}
a90917c3 4152EXPORT_SYMBOL_GPL(lttng_transport_register);
c0e31d2e
MD
4153
4154/**
a90917c3 4155 * lttng_transport_unregister - LTT transport unregistration
c0e31d2e
MD
4156 * @transport: transport structure
4157 */
a90917c3 4158void lttng_transport_unregister(struct lttng_transport *transport)
c0e31d2e
MD
4159{
4160 mutex_lock(&sessions_mutex);
4161 list_del(&transport->node);
4162 mutex_unlock(&sessions_mutex);
4163}
a90917c3 4164EXPORT_SYMBOL_GPL(lttng_transport_unregister);
c0e31d2e 4165
a101fa10
MD
4166void lttng_counter_transport_register(struct lttng_counter_transport *transport)
4167{
4168 /*
4169 * Make sure no page fault can be triggered by the module about to be
4170 * registered. We deal with this here so we don't have to call
4171 * vmalloc_sync_mappings() in each module's init.
4172 */
4173 wrapper_vmalloc_sync_mappings();
4174
4175 mutex_lock(&sessions_mutex);
4176 list_add_tail(&transport->node, &lttng_counter_transport_list);
4177 mutex_unlock(&sessions_mutex);
4178}
4179EXPORT_SYMBOL_GPL(lttng_counter_transport_register);
4180
4181void lttng_counter_transport_unregister(struct lttng_counter_transport *transport)
4182{
4183 mutex_lock(&sessions_mutex);
4184 list_del(&transport->node);
4185 mutex_unlock(&sessions_mutex);
4186}
4187EXPORT_SYMBOL_GPL(lttng_counter_transport_unregister);
4188
5f4c791e 4189#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
1e367326
MD
4190
4191enum cpuhp_state lttng_hp_prepare;
4192enum cpuhp_state lttng_hp_online;
4193
4194static int lttng_hotplug_prepare(unsigned int cpu, struct hlist_node *node)
4195{
4196 struct lttng_cpuhp_node *lttng_node;
4197
4198 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
4199 switch (lttng_node->component) {
4200 case LTTNG_RING_BUFFER_FRONTEND:
4201 return 0;
4202 case LTTNG_RING_BUFFER_BACKEND:
4203 return lttng_cpuhp_rb_backend_prepare(cpu, lttng_node);
4204 case LTTNG_RING_BUFFER_ITER:
4205 return 0;
4206 case LTTNG_CONTEXT_PERF_COUNTERS:
4207 return 0;
4208 default:
4209 return -EINVAL;
4210 }
4211}
4212
4213static int lttng_hotplug_dead(unsigned int cpu, struct hlist_node *node)
4214{
4215 struct lttng_cpuhp_node *lttng_node;
4216
4217 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
4218 switch (lttng_node->component) {
4219 case LTTNG_RING_BUFFER_FRONTEND:
4220 return lttng_cpuhp_rb_frontend_dead(cpu, lttng_node);
4221 case LTTNG_RING_BUFFER_BACKEND:
4222 return 0;
4223 case LTTNG_RING_BUFFER_ITER:
4224 return 0;
4225 case LTTNG_CONTEXT_PERF_COUNTERS:
4226 return lttng_cpuhp_perf_counter_dead(cpu, lttng_node);
4227 default:
4228 return -EINVAL;
4229 }
4230}
4231
4232static int lttng_hotplug_online(unsigned int cpu, struct hlist_node *node)
4233{
4234 struct lttng_cpuhp_node *lttng_node;
4235
4236 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
4237 switch (lttng_node->component) {
4238 case LTTNG_RING_BUFFER_FRONTEND:
4239 return lttng_cpuhp_rb_frontend_online(cpu, lttng_node);
4240 case LTTNG_RING_BUFFER_BACKEND:
4241 return 0;
4242 case LTTNG_RING_BUFFER_ITER:
4243 return lttng_cpuhp_rb_iter_online(cpu, lttng_node);
4244 case LTTNG_CONTEXT_PERF_COUNTERS:
4245 return lttng_cpuhp_perf_counter_online(cpu, lttng_node);
4246 default:
4247 return -EINVAL;
4248 }
4249}
4250
4251static int lttng_hotplug_offline(unsigned int cpu, struct hlist_node *node)
4252{
4253 struct lttng_cpuhp_node *lttng_node;
4254
4255 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
4256 switch (lttng_node->component) {
4257 case LTTNG_RING_BUFFER_FRONTEND:
4258 return lttng_cpuhp_rb_frontend_offline(cpu, lttng_node);
4259 case LTTNG_RING_BUFFER_BACKEND:
4260 return 0;
4261 case LTTNG_RING_BUFFER_ITER:
4262 return 0;
4263 case LTTNG_CONTEXT_PERF_COUNTERS:
4264 return 0;
4265 default:
4266 return -EINVAL;
4267 }
4268}
4269
4270static int __init lttng_init_cpu_hotplug(void)
4271{
4272 int ret;
4273
4274 ret = cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN, "lttng:prepare",
4275 lttng_hotplug_prepare,
4276 lttng_hotplug_dead);
4277 if (ret < 0) {
4278 return ret;
4279 }
4280 lttng_hp_prepare = ret;
4281 lttng_rb_set_hp_prepare(ret);
4282
4283 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "lttng:online",
4284 lttng_hotplug_online,
4285 lttng_hotplug_offline);
4286 if (ret < 0) {
4287 cpuhp_remove_multi_state(lttng_hp_prepare);
4288 lttng_hp_prepare = 0;
4289 return ret;
4290 }
4291 lttng_hp_online = ret;
4292 lttng_rb_set_hp_online(ret);
4293
4294 return 0;
4295}
4296
4297static void __exit lttng_exit_cpu_hotplug(void)
4298{
4299 lttng_rb_set_hp_online(0);
4300 cpuhp_remove_multi_state(lttng_hp_online);
4301 lttng_rb_set_hp_prepare(0);
4302 cpuhp_remove_multi_state(lttng_hp_prepare);
4303}
4304
5f4c791e 4305#else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
1e367326
MD
4306static int lttng_init_cpu_hotplug(void)
4307{
4308 return 0;
4309}
4310static void lttng_exit_cpu_hotplug(void)
4311{
4312}
5f4c791e 4313#endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
1e367326 4314
a90917c3 4315static int __init lttng_events_init(void)
4e3c1b9b 4316{
1c25284c
MD
4317 int ret;
4318
453b2495
JD
4319 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
4320 if (ret)
4321 return ret;
389d7070 4322 ret = wrapper_get_pfnblock_flags_mask_init();
36561775
MD
4323 if (ret)
4324 return ret;
4325 ret = wrapper_get_pageblock_flags_mask_init();
114667d5
MD
4326 if (ret)
4327 return ret;
4328 ret = lttng_probes_init();
389d7070
MD
4329 if (ret)
4330 return ret;
07dfc1d0 4331 ret = lttng_context_init();
20591cf7
MD
4332 if (ret)
4333 return ret;
07dfc1d0
MD
4334 ret = lttng_tracepoint_init();
4335 if (ret)
4336 goto error_tp;
a67ba386
MD
4337 event_recorder_cache = KMEM_CACHE(lttng_kernel_event_recorder, 0);
4338 if (!event_recorder_cache) {
20591cf7 4339 ret = -ENOMEM;
a67ba386 4340 goto error_kmem_event_recorder;
dffef45d 4341 }
a67ba386
MD
4342 event_recorder_private_cache = KMEM_CACHE(lttng_kernel_event_recorder_private, 0);
4343 if (!event_recorder_private_cache) {
4344 ret = -ENOMEM;
4345 goto error_kmem_event_recorder_private;
4346 }
4347 event_notifier_cache = KMEM_CACHE(lttng_kernel_event_notifier, 0);
dffef45d
FD
4348 if (!event_notifier_cache) {
4349 ret = -ENOMEM;
4350 goto error_kmem_event_notifier;
20591cf7 4351 }
a67ba386
MD
4352 event_notifier_private_cache = KMEM_CACHE(lttng_kernel_event_notifier_private, 0);
4353 if (!event_notifier_private_cache) {
4354 ret = -ENOMEM;
4355 goto error_kmem_event_notifier_private;
4356 }
80996790 4357 ret = lttng_abi_init();
02119ee5
MD
4358 if (ret)
4359 goto error_abi;
0c956676
MD
4360 ret = lttng_logger_init();
4361 if (ret)
4362 goto error_logger;
1e367326
MD
4363 ret = lttng_init_cpu_hotplug();
4364 if (ret)
4365 goto error_hotplug;
101215b7 4366 printk(KERN_NOTICE "LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
489de24b
MJ
4367 __stringify(LTTNG_MODULES_MAJOR_VERSION),
4368 __stringify(LTTNG_MODULES_MINOR_VERSION),
4369 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
4370 LTTNG_MODULES_EXTRAVERSION,
101215b7
MJ
4371 LTTNG_VERSION_NAME,
4372#ifdef LTTNG_EXTRA_VERSION_GIT
4373 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
4374#else
4375 "",
4376#endif
4377#ifdef LTTNG_EXTRA_VERSION_NAME
4378 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
4379#else
4380 "");
4381#endif
c1551e57
MJ
4382#ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
4383 printk(KERN_NOTICE "LTTng: Experimental bitwise enum enabled.\n");
4384#endif /* CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM */
4e3c1b9b 4385 return 0;
0c956676 4386
1e367326
MD
4387error_hotplug:
4388 lttng_logger_exit();
0c956676
MD
4389error_logger:
4390 lttng_abi_exit();
02119ee5 4391error_abi:
a67ba386
MD
4392 kmem_cache_destroy(event_notifier_private_cache);
4393error_kmem_event_notifier_private:
dffef45d
FD
4394 kmem_cache_destroy(event_notifier_cache);
4395error_kmem_event_notifier:
a67ba386
MD
4396 kmem_cache_destroy(event_recorder_private_cache);
4397error_kmem_event_recorder_private:
4398 kmem_cache_destroy(event_recorder_cache);
4399error_kmem_event_recorder:
20591cf7 4400 lttng_tracepoint_exit();
07dfc1d0
MD
4401error_tp:
4402 lttng_context_exit();
101215b7 4403 printk(KERN_NOTICE "LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
489de24b
MJ
4404 __stringify(LTTNG_MODULES_MAJOR_VERSION),
4405 __stringify(LTTNG_MODULES_MINOR_VERSION),
4406 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
4407 LTTNG_MODULES_EXTRAVERSION,
101215b7
MJ
4408 LTTNG_VERSION_NAME,
4409#ifdef LTTNG_EXTRA_VERSION_GIT
4410 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
4411#else
4412 "",
4413#endif
4414#ifdef LTTNG_EXTRA_VERSION_NAME
4415 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
4416#else
4417 "");
4418#endif
1c25284c 4419 return ret;
4e3c1b9b
MD
4420}
4421
a90917c3 4422module_init(lttng_events_init);
11b5a3c2 4423
a90917c3 4424static void __exit lttng_events_exit(void)
4e3c1b9b 4425{
8cdc1a81 4426 struct lttng_kernel_session_private *session_priv, *tmpsession_priv;
92e94819 4427
1e367326 4428 lttng_exit_cpu_hotplug();
0c956676 4429 lttng_logger_exit();
80996790 4430 lttng_abi_exit();
8cdc1a81
MD
4431 list_for_each_entry_safe(session_priv, tmpsession_priv, &sessions, list)
4432 lttng_session_destroy(session_priv->pub);
a67ba386
MD
4433 kmem_cache_destroy(event_recorder_cache);
4434 kmem_cache_destroy(event_recorder_private_cache);
dffef45d 4435 kmem_cache_destroy(event_notifier_cache);
a67ba386 4436 kmem_cache_destroy(event_notifier_private_cache);
20591cf7 4437 lttng_tracepoint_exit();
07dfc1d0 4438 lttng_context_exit();
101215b7 4439 printk(KERN_NOTICE "LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
489de24b
MJ
4440 __stringify(LTTNG_MODULES_MAJOR_VERSION),
4441 __stringify(LTTNG_MODULES_MINOR_VERSION),
4442 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
4443 LTTNG_MODULES_EXTRAVERSION,
101215b7
MJ
4444 LTTNG_VERSION_NAME,
4445#ifdef LTTNG_EXTRA_VERSION_GIT
4446 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
4447#else
4448 "",
4449#endif
4450#ifdef LTTNG_EXTRA_VERSION_NAME
4451 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
4452#else
4453 "");
4454#endif
4e3c1b9b 4455}
92e94819 4456
a90917c3 4457module_exit(lttng_events_exit);
11b5a3c2 4458
7ca11e77 4459#include <generated/patches.h>
101215b7
MJ
4460#ifdef LTTNG_EXTRA_VERSION_GIT
4461MODULE_INFO(extra_version_git, LTTNG_EXTRA_VERSION_GIT);
4462#endif
4463#ifdef LTTNG_EXTRA_VERSION_NAME
4464MODULE_INFO(extra_version_name, LTTNG_EXTRA_VERSION_NAME);
4465#endif
92e94819
MD
4466MODULE_LICENSE("GPL and additional rights");
4467MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1c124020 4468MODULE_DESCRIPTION("LTTng tracer");
9a9973ef
MD
4469MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
4470 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
309370e2
MD
4471 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
4472 LTTNG_MODULES_EXTRAVERSION);
This page took 0.389262 seconds and 4 git commands to generate.