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