enumeration autoincrement: use if/else logic
[deliverable/lttng-modules.git] / lttng-events.c
CommitLineData
4e3c1b9b 1/*
a90917c3 2 * lttng-events.c
4e3c1b9b 3 *
4e3c1b9b 4 * Holds LTTng per-session event registry.
17baffe2 5 *
886d51a3
MD
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4e3c1b9b
MD
21 */
22
389d7070
MD
23/*
24 * This page_alloc.h wrapper needs to be included before gfpflags.h because it
25 * overrides a function with a define.
26 */
27#include "wrapper/page_alloc.h"
28
4e3c1b9b 29#include <linux/module.h>
c0e31d2e
MD
30#include <linux/mutex.h>
31#include <linux/sched.h>
11b5a3c2 32#include <linux/slab.h>
c099397a 33#include <linux/jiffies.h>
99dc9597 34#include <linux/utsname.h>
abc0446a 35#include <linux/err.h>
7e6f9ef6
MD
36#include <linux/seq_file.h>
37#include <linux/file.h>
38#include <linux/anon_inodes.h>
241ae9a8 39#include <wrapper/file.h>
3c997079 40#include <linux/jhash.h>
8c6e7f13 41#include <linux/uaccess.h>
a606b6e8 42#include <linux/vmalloc.h>
3c997079 43
241ae9a8
MD
44#include <wrapper/uuid.h>
45#include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */
46#include <wrapper/random.h>
47#include <wrapper/tracepoint.h>
48#include <wrapper/list.h>
49#include <lttng-kernel-version.h>
50#include <lttng-events.h>
51#include <lttng-tracer.h>
52#include <lttng-abi-old.h>
5214fa50 53#include <lttng-endian.h>
241ae9a8 54#include <wrapper/vzalloc.h>
64af2437
MD
55#include <wrapper/ringbuffer/backend.h>
56#include <wrapper/ringbuffer/frontend.h>
4e3c1b9b 57
d83004aa
JD
58#define METADATA_CACHE_DEFAULT_SIZE 4096
59
4e3c1b9b 60static LIST_HEAD(sessions);
a90917c3 61static LIST_HEAD(lttng_transport_list);
d83004aa
JD
62/*
63 * Protect the sessions and metadata caches.
64 */
4e3c1b9b
MD
65static DEFINE_MUTEX(sessions_mutex);
66static struct kmem_cache *event_cache;
67
3c997079
MD
68static void lttng_session_lazy_sync_enablers(struct lttng_session *session);
69static void lttng_session_sync_enablers(struct lttng_session *session);
70static void lttng_enabler_destroy(struct lttng_enabler *enabler);
71
a90917c3
MD
72static void _lttng_event_destroy(struct lttng_event *event);
73static void _lttng_channel_destroy(struct lttng_channel *chan);
74static int _lttng_event_unregister(struct lttng_event *event);
c099397a 75static
a90917c3
MD
76int _lttng_event_metadata_statedump(struct lttng_session *session,
77 struct lttng_channel *chan,
78 struct lttng_event *event);
c099397a 79static
a90917c3 80int _lttng_session_metadata_statedump(struct lttng_session *session);
d83004aa
JD
81static
82void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
f513b2bf
MD
83static
84int _lttng_field_statedump(struct lttng_session *session,
85 const struct lttng_event_field *field,
86 size_t nesting);
c099397a 87
c099397a 88void synchronize_trace(void)
abcca994
MD
89{
90 synchronize_sched();
b8590f40
MD
91#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
92#ifdef CONFIG_PREEMPT_RT_FULL
93 synchronize_rcu();
94#endif
95#else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
abcca994
MD
96#ifdef CONFIG_PREEMPT_RT
97 synchronize_rcu();
98#endif
b8590f40 99#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
abcca994
MD
100}
101
3c997079
MD
102void lttng_lock_sessions(void)
103{
104 mutex_lock(&sessions_mutex);
105}
106
107void lttng_unlock_sessions(void)
108{
109 mutex_unlock(&sessions_mutex);
110}
111
112/*
113 * Called with sessions lock held.
114 */
115int lttng_session_active(void)
116{
117 struct lttng_session *iter;
118
119 list_for_each_entry(iter, &sessions, list) {
120 if (iter->active)
121 return 1;
122 }
123 return 0;
124}
125
a90917c3 126struct lttng_session *lttng_session_create(void)
4e3c1b9b 127{
a90917c3 128 struct lttng_session *session;
d83004aa 129 struct lttng_metadata_cache *metadata_cache;
3c997079 130 int i;
4e3c1b9b
MD
131
132 mutex_lock(&sessions_mutex);
a90917c3 133 session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL);
4e3c1b9b 134 if (!session)
d83004aa 135 goto err;
4e3c1b9b 136 INIT_LIST_HEAD(&session->chan);
f3d01b96 137 INIT_LIST_HEAD(&session->events);
d793d5e1 138 uuid_le_gen(&session->uuid);
d83004aa
JD
139
140 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
141 GFP_KERNEL);
142 if (!metadata_cache)
143 goto err_free_session;
f6da044f 144 metadata_cache->data = lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE);
d83004aa
JD
145 if (!metadata_cache->data)
146 goto err_free_cache;
147 metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
148 kref_init(&metadata_cache->refcount);
92d9f5e6 149 mutex_init(&metadata_cache->lock);
d83004aa
JD
150 session->metadata_cache = metadata_cache;
151 INIT_LIST_HEAD(&metadata_cache->metadata_stream);
a36580d5
MD
152 memcpy(&metadata_cache->uuid, &session->uuid,
153 sizeof(metadata_cache->uuid));
3c997079
MD
154 INIT_LIST_HEAD(&session->enablers_head);
155 for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++)
156 INIT_HLIST_HEAD(&session->events_ht.table[i]);
4e3c1b9b
MD
157 list_add(&session->list, &sessions);
158 mutex_unlock(&sessions_mutex);
159 return session;
d83004aa
JD
160
161err_free_cache:
162 kfree(metadata_cache);
163err_free_session:
164 kfree(session);
165err:
166 mutex_unlock(&sessions_mutex);
167 return NULL;
168}
169
170void metadata_cache_destroy(struct kref *kref)
171{
172 struct lttng_metadata_cache *cache =
173 container_of(kref, struct lttng_metadata_cache, refcount);
a606b6e8 174 vfree(cache->data);
d83004aa 175 kfree(cache);
4e3c1b9b
MD
176}
177
a90917c3 178void lttng_session_destroy(struct lttng_session *session)
4e3c1b9b 179{
a90917c3
MD
180 struct lttng_channel *chan, *tmpchan;
181 struct lttng_event *event, *tmpevent;
d83004aa 182 struct lttng_metadata_stream *metadata_stream;
3c997079 183 struct lttng_enabler *enabler, *tmpenabler;
dda6a249 184 int ret;
4e3c1b9b
MD
185
186 mutex_lock(&sessions_mutex);
52fc2e1f 187 ACCESS_ONCE(session->active) = 0;
1ec65de1
MD
188 list_for_each_entry(chan, &session->chan, list) {
189 ret = lttng_syscalls_unregister(chan);
190 WARN_ON(ret);
191 }
dda6a249 192 list_for_each_entry(event, &session->events, list) {
a90917c3 193 ret = _lttng_event_unregister(event);
dda6a249
MD
194 WARN_ON(ret);
195 }
abcca994 196 synchronize_trace(); /* Wait for in-flight events to complete */
3c997079
MD
197 list_for_each_entry_safe(enabler, tmpenabler,
198 &session->enablers_head, node)
199 lttng_enabler_destroy(enabler);
4e3c1b9b 200 list_for_each_entry_safe(event, tmpevent, &session->events, list)
a90917c3 201 _lttng_event_destroy(event);
d83004aa
JD
202 list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
203 BUG_ON(chan->channel_type == METADATA_CHANNEL);
a90917c3 204 _lttng_channel_destroy(chan);
d83004aa
JD
205 }
206 list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
207 _lttng_metadata_channel_hangup(metadata_stream);
e0130fab
MD
208 if (session->pid_tracker)
209 lttng_pid_tracker_destroy(session->pid_tracker);
d83004aa 210 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
4e3c1b9b
MD
211 list_del(&session->list);
212 mutex_unlock(&sessions_mutex);
213 kfree(session);
214}
215
601252cf
MD
216int lttng_session_statedump(struct lttng_session *session)
217{
218 int ret;
219
220 mutex_lock(&sessions_mutex);
221 ret = lttng_statedump_start(session);
222 mutex_unlock(&sessions_mutex);
223 return ret;
224}
225
a90917c3 226int lttng_session_enable(struct lttng_session *session)
c0e31d2e
MD
227{
228 int ret = 0;
a90917c3 229 struct lttng_channel *chan;
c0e31d2e
MD
230
231 mutex_lock(&sessions_mutex);
232 if (session->active) {
233 ret = -EBUSY;
234 goto end;
235 }
c099397a 236
3c997079
MD
237 /* Set transient enabler state to "enabled" */
238 session->tstate = 1;
239
c099397a
MD
240 /*
241 * Snapshot the number of events per channel to know the type of header
242 * we need to use.
243 */
244 list_for_each_entry(chan, &session->chan, list) {
245 if (chan->header_type)
246 continue; /* don't change it if session stop/restart */
247 if (chan->free_event_id < 31)
248 chan->header_type = 1; /* compact */
249 else
250 chan->header_type = 2; /* large */
251 }
252
3c997079
MD
253 /* We need to sync enablers with session before activation. */
254 lttng_session_sync_enablers(session);
255
64af2437 256 /* Clear each stream's quiescent state. */
09b93db9
MD
257 list_for_each_entry(chan, &session->chan, list) {
258 if (chan->channel_type != METADATA_CHANNEL)
259 lib_ring_buffer_clear_quiescent_channel(chan->chan);
260 }
64af2437 261
52fc2e1f 262 ACCESS_ONCE(session->active) = 1;
8070f5c0 263 ACCESS_ONCE(session->been_active) = 1;
a90917c3 264 ret = _lttng_session_metadata_statedump(session);
c337ddc2
MD
265 if (ret) {
266 ACCESS_ONCE(session->active) = 0;
267 goto end;
268 }
269 ret = lttng_statedump_start(session);
360f38ea 270 if (ret)
c099397a 271 ACCESS_ONCE(session->active) = 0;
c0e31d2e
MD
272end:
273 mutex_unlock(&sessions_mutex);
11b5a3c2 274 return ret;
c0e31d2e
MD
275}
276
a90917c3 277int lttng_session_disable(struct lttng_session *session)
c0e31d2e
MD
278{
279 int ret = 0;
64af2437 280 struct lttng_channel *chan;
c0e31d2e
MD
281
282 mutex_lock(&sessions_mutex);
283 if (!session->active) {
284 ret = -EBUSY;
285 goto end;
286 }
52fc2e1f 287 ACCESS_ONCE(session->active) = 0;
3c997079
MD
288
289 /* Set transient enabler state to "disabled" */
290 session->tstate = 0;
291 lttng_session_sync_enablers(session);
64af2437
MD
292
293 /* Set each stream's quiescent state. */
09b93db9
MD
294 list_for_each_entry(chan, &session->chan, list) {
295 if (chan->channel_type != METADATA_CHANNEL)
296 lib_ring_buffer_set_quiescent_channel(chan->chan);
297 }
c0e31d2e
MD
298end:
299 mutex_unlock(&sessions_mutex);
11b5a3c2 300 return ret;
c0e31d2e
MD
301}
302
9616f0bf
JD
303int lttng_session_metadata_regenerate(struct lttng_session *session)
304{
305 int ret = 0;
306 struct lttng_channel *chan;
307 struct lttng_event *event;
308 struct lttng_metadata_cache *cache = session->metadata_cache;
309 struct lttng_metadata_stream *stream;
310
311 mutex_lock(&sessions_mutex);
312 if (!session->active) {
313 ret = -EBUSY;
314 goto end;
315 }
316
317 mutex_lock(&cache->lock);
318 memset(cache->data, 0, cache->cache_alloc);
319 cache->metadata_written = 0;
320 cache->version++;
321 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) {
322 stream->metadata_out = 0;
323 stream->metadata_in = 0;
324 }
325 mutex_unlock(&cache->lock);
326
327 session->metadata_dumped = 0;
328 list_for_each_entry(chan, &session->chan, list) {
329 chan->metadata_dumped = 0;
330 }
331
332 list_for_each_entry(event, &session->events, list) {
333 event->metadata_dumped = 0;
334 }
335
336 ret = _lttng_session_metadata_statedump(session);
337
338end:
339 mutex_unlock(&sessions_mutex);
340 return ret;
341}
342
343
344
a90917c3 345int lttng_channel_enable(struct lttng_channel *channel)
e64957da 346{
3c997079 347 int ret = 0;
e64957da 348
3c997079
MD
349 mutex_lock(&sessions_mutex);
350 if (channel->channel_type == METADATA_CHANNEL) {
351 ret = -EPERM;
352 goto end;
353 }
354 if (channel->enabled) {
355 ret = -EEXIST;
356 goto end;
357 }
358 /* Set transient enabler state to "enabled" */
359 channel->tstate = 1;
360 lttng_session_sync_enablers(channel->session);
361 /* Set atomically the state to "enabled" */
362 ACCESS_ONCE(channel->enabled) = 1;
363end:
364 mutex_unlock(&sessions_mutex);
365 return ret;
e64957da
MD
366}
367
a90917c3 368int lttng_channel_disable(struct lttng_channel *channel)
e64957da 369{
3c997079 370 int ret = 0;
e64957da 371
3c997079
MD
372 mutex_lock(&sessions_mutex);
373 if (channel->channel_type == METADATA_CHANNEL) {
374 ret = -EPERM;
375 goto end;
376 }
377 if (!channel->enabled) {
378 ret = -EEXIST;
379 goto end;
380 }
381 /* Set atomically the state to "disabled" */
382 ACCESS_ONCE(channel->enabled) = 0;
383 /* Set transient enabler state to "enabled" */
384 channel->tstate = 0;
385 lttng_session_sync_enablers(channel->session);
386end:
387 mutex_unlock(&sessions_mutex);
388 return ret;
e64957da
MD
389}
390
a90917c3 391int lttng_event_enable(struct lttng_event *event)
e64957da 392{
3c997079 393 int ret = 0;
e64957da 394
3c997079
MD
395 mutex_lock(&sessions_mutex);
396 if (event->chan->channel_type == METADATA_CHANNEL) {
397 ret = -EPERM;
398 goto end;
399 }
400 if (event->enabled) {
401 ret = -EEXIST;
402 goto end;
403 }
a0493bef
MD
404 switch (event->instrumentation) {
405 case LTTNG_KERNEL_TRACEPOINT:
406 case LTTNG_KERNEL_SYSCALL:
407 ret = -EINVAL;
408 break;
409 case LTTNG_KERNEL_KPROBE:
410 case LTTNG_KERNEL_FUNCTION:
411 case LTTNG_KERNEL_NOOP:
412 ACCESS_ONCE(event->enabled) = 1;
413 break;
414 case LTTNG_KERNEL_KRETPROBE:
415 ret = lttng_kretprobes_event_enable_state(event, 1);
416 break;
417 default:
418 WARN_ON_ONCE(1);
419 ret = -EINVAL;
420 }
3c997079
MD
421end:
422 mutex_unlock(&sessions_mutex);
423 return ret;
e64957da
MD
424}
425
a90917c3 426int lttng_event_disable(struct lttng_event *event)
e64957da 427{
3c997079 428 int ret = 0;
e64957da 429
3c997079
MD
430 mutex_lock(&sessions_mutex);
431 if (event->chan->channel_type == METADATA_CHANNEL) {
432 ret = -EPERM;
433 goto end;
434 }
435 if (!event->enabled) {
436 ret = -EEXIST;
437 goto end;
438 }
a0493bef
MD
439 switch (event->instrumentation) {
440 case LTTNG_KERNEL_TRACEPOINT:
441 case LTTNG_KERNEL_SYSCALL:
442 ret = -EINVAL;
443 break;
444 case LTTNG_KERNEL_KPROBE:
445 case LTTNG_KERNEL_FUNCTION:
446 case LTTNG_KERNEL_NOOP:
447 ACCESS_ONCE(event->enabled) = 0;
448 break;
449 case LTTNG_KERNEL_KRETPROBE:
450 ret = lttng_kretprobes_event_enable_state(event, 0);
451 break;
452 default:
453 WARN_ON_ONCE(1);
454 ret = -EINVAL;
455 }
3c997079
MD
456end:
457 mutex_unlock(&sessions_mutex);
458 return ret;
e64957da
MD
459}
460
a90917c3 461static struct lttng_transport *lttng_transport_find(const char *name)
f3d01b96 462{
a90917c3 463 struct lttng_transport *transport;
f3d01b96 464
a90917c3 465 list_for_each_entry(transport, &lttng_transport_list, node) {
f3d01b96
MD
466 if (!strcmp(transport->name, name))
467 return transport;
468 }
469 return NULL;
470}
471
a90917c3 472struct lttng_channel *lttng_channel_create(struct lttng_session *session,
5dbbdb43
MD
473 const char *transport_name,
474 void *buf_addr,
4e3c1b9b
MD
475 size_t subbuf_size, size_t num_subbuf,
476 unsigned int switch_timer_interval,
d83004aa
JD
477 unsigned int read_timer_interval,
478 enum channel_type channel_type)
4e3c1b9b 479{
a90917c3
MD
480 struct lttng_channel *chan;
481 struct lttng_transport *transport = NULL;
4e3c1b9b
MD
482
483 mutex_lock(&sessions_mutex);
d83004aa 484 if (session->been_active && channel_type != METADATA_CHANNEL)
e5382b6d 485 goto active; /* Refuse to add channel to active session */
a90917c3 486 transport = lttng_transport_find(transport_name);
f3d01b96
MD
487 if (!transport) {
488 printk(KERN_WARNING "LTTng transport %s not found\n",
489 transport_name);
c0e31d2e 490 goto notransport;
f3d01b96 491 }
a33c9927
MD
492 if (!try_module_get(transport->owner)) {
493 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
494 goto notransport;
495 }
a90917c3 496 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
4e3c1b9b 497 if (!chan)
c0e31d2e 498 goto nomem;
4e3c1b9b 499 chan->session = session;
05d32c64 500 chan->id = session->free_chan_id++;
3b731ab1 501 chan->ops = &transport->ops;
125b4df4
MD
502 /*
503 * Note: the channel creation op already writes into the packet
504 * headers. Therefore the "chan" information used as input
505 * should be already accessible.
506 */
5f5ddf01
MD
507 chan->chan = transport->ops.channel_create(transport_name,
508 chan, buf_addr, subbuf_size, num_subbuf,
509 switch_timer_interval, read_timer_interval);
f3d01b96
MD
510 if (!chan->chan)
511 goto create_error;
3c997079 512 chan->tstate = 1;
e64957da 513 chan->enabled = 1;
a33c9927 514 chan->transport = transport;
d83004aa 515 chan->channel_type = channel_type;
4e3c1b9b
MD
516 list_add(&chan->list, &session->chan);
517 mutex_unlock(&sessions_mutex);
518 return chan;
519
f3d01b96
MD
520create_error:
521 kfree(chan);
c0e31d2e 522nomem:
a33c9927
MD
523 if (transport)
524 module_put(transport->owner);
c0e31d2e 525notransport:
e5382b6d 526active:
4e3c1b9b
MD
527 mutex_unlock(&sessions_mutex);
528 return NULL;
529}
530
531/*
d83004aa
JD
532 * Only used internally at session destruction for per-cpu channels, and
533 * when metadata channel is released.
534 * Needs to be called with sessions mutex held.
4e3c1b9b 535 */
aa7c23a9 536static
a90917c3 537void _lttng_channel_destroy(struct lttng_channel *chan)
4e3c1b9b 538{
11b5a3c2 539 chan->ops->channel_destroy(chan->chan);
a33c9927 540 module_put(chan->transport->owner);
4e3c1b9b 541 list_del(&chan->list);
8070f5c0 542 lttng_destroy_context(chan->ctx);
4e3c1b9b
MD
543 kfree(chan);
544}
545
d83004aa
JD
546void lttng_metadata_channel_destroy(struct lttng_channel *chan)
547{
548 BUG_ON(chan->channel_type != METADATA_CHANNEL);
549
550 /* Protect the metadata cache with the sessions_mutex. */
551 mutex_lock(&sessions_mutex);
552 _lttng_channel_destroy(chan);
553 mutex_unlock(&sessions_mutex);
554}
555EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
556
557static
558void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
559{
560 stream->finalized = 1;
561 wake_up_interruptible(&stream->read_wait);
562}
563
e5382b6d
MD
564/*
565 * Supports event creation while tracing session is active.
3c997079 566 * Needs to be called with sessions mutex held.
e5382b6d 567 */
3c997079
MD
568struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
569 struct lttng_kernel_event *event_param,
570 void *filter,
571 const struct lttng_event_desc *event_desc,
572 enum lttng_kernel_instrumentation itype)
4e3c1b9b 573{
3c997079 574 struct lttng_session *session = chan->session;
a90917c3 575 struct lttng_event *event;
3c997079
MD
576 const char *event_name;
577 struct hlist_head *head;
578 size_t name_len;
579 uint32_t hash;
3d084699 580 int ret;
4e3c1b9b 581
abc0446a 582 if (chan->free_event_id == -1U) {
4cf0bf51 583 ret = -EMFILE;
e5382b6d 584 goto full;
abc0446a 585 }
3c997079
MD
586
587 switch (itype) {
588 case LTTNG_KERNEL_TRACEPOINT:
589 event_name = event_desc->name;
590 break;
591 case LTTNG_KERNEL_KPROBE:
592 case LTTNG_KERNEL_KRETPROBE:
593 case LTTNG_KERNEL_FUNCTION:
594 case LTTNG_KERNEL_NOOP:
33a39a3c 595 case LTTNG_KERNEL_SYSCALL:
3c997079
MD
596 event_name = event_param->name;
597 break;
598 default:
599 WARN_ON_ONCE(1);
600 ret = -EINVAL;
601 goto type_error;
602 }
603 name_len = strlen(event_name);
604 hash = jhash(event_name, name_len, 0);
605 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
d216ecae 606 lttng_hlist_for_each_entry(event, head, hlist) {
3c997079
MD
607 WARN_ON_ONCE(!event->desc);
608 if (!strncmp(event->desc->name, event_name,
609 LTTNG_KERNEL_SYM_NAME_LEN - 1)
610 && chan == event->chan) {
611 ret = -EEXIST;
612 goto exist;
abc0446a
MD
613 }
614 }
3c997079 615
11b5a3c2 616 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
abc0446a 617 if (!event) {
4cf0bf51 618 ret = -ENOMEM;
4e3c1b9b 619 goto cache_error;
abc0446a 620 }
4e3c1b9b
MD
621 event->chan = chan;
622 event->filter = filter;
e5382b6d 623 event->id = chan->free_event_id++;
3c997079
MD
624 event->instrumentation = itype;
625 event->evtype = LTTNG_TYPE_EVENT;
07dfc1d0 626 INIT_LIST_HEAD(&event->bytecode_runtime_head);
3c997079
MD
627 INIT_LIST_HEAD(&event->enablers_ref_head);
628
629 switch (itype) {
ab2277d6 630 case LTTNG_KERNEL_TRACEPOINT:
3c997079
MD
631 /* Event will be enabled by enabler sync. */
632 event->enabled = 0;
633 event->registered = 0;
634 event->desc = lttng_event_get(event_name);
abc0446a 635 if (!event->desc) {
4cf0bf51 636 ret = -ENOENT;
d3dbe23c 637 goto register_error;
abc0446a 638 }
3c997079
MD
639 /* Populate lttng_event structure before event registration. */
640 smp_wmb();
baf20995 641 break;
ab2277d6 642 case LTTNG_KERNEL_KPROBE:
a0493bef
MD
643 /*
644 * Needs to be explicitly enabled after creation, since
645 * we may want to apply filters.
646 */
647 event->enabled = 0;
3c997079
MD
648 event->registered = 1;
649 /*
650 * Populate lttng_event structure before event
651 * registration.
652 */
653 smp_wmb();
654 ret = lttng_kprobes_register(event_name,
f17701fb
MD
655 event_param->u.kprobe.symbol_name,
656 event_param->u.kprobe.offset,
657 event_param->u.kprobe.addr,
658 event);
abc0446a 659 if (ret) {
4cf0bf51 660 ret = -EINVAL;
d6d808f3 661 goto register_error;
abc0446a 662 }
edeb3137
MD
663 ret = try_module_get(event->desc->owner);
664 WARN_ON_ONCE(!ret);
d6d808f3 665 break;
7371f44c
MD
666 case LTTNG_KERNEL_KRETPROBE:
667 {
a90917c3 668 struct lttng_event *event_return;
7371f44c
MD
669
670 /* kretprobe defines 2 events */
a0493bef
MD
671 /*
672 * Needs to be explicitly enabled after creation, since
673 * we may want to apply filters.
674 */
675 event->enabled = 0;
3c997079 676 event->registered = 1;
7371f44c
MD
677 event_return =
678 kmem_cache_zalloc(event_cache, GFP_KERNEL);
abc0446a 679 if (!event_return) {
4cf0bf51 680 ret = -ENOMEM;
7371f44c 681 goto register_error;
abc0446a 682 }
7371f44c
MD
683 event_return->chan = chan;
684 event_return->filter = filter;
685 event_return->id = chan->free_event_id++;
a0493bef 686 event_return->enabled = 0;
3c997079
MD
687 event_return->registered = 1;
688 event_return->instrumentation = itype;
7371f44c 689 /*
a90917c3 690 * Populate lttng_event structure before kretprobe registration.
7371f44c
MD
691 */
692 smp_wmb();
3c997079 693 ret = lttng_kretprobes_register(event_name,
7371f44c
MD
694 event_param->u.kretprobe.symbol_name,
695 event_param->u.kretprobe.offset,
696 event_param->u.kretprobe.addr,
697 event, event_return);
698 if (ret) {
699 kmem_cache_free(event_cache, event_return);
4cf0bf51 700 ret = -EINVAL;
7371f44c
MD
701 goto register_error;
702 }
703 /* Take 2 refs on the module: one per event. */
704 ret = try_module_get(event->desc->owner);
705 WARN_ON_ONCE(!ret);
706 ret = try_module_get(event->desc->owner);
707 WARN_ON_ONCE(!ret);
a90917c3 708 ret = _lttng_event_metadata_statedump(chan->session, chan,
7371f44c 709 event_return);
abc0446a 710 WARN_ON_ONCE(ret > 0);
7371f44c
MD
711 if (ret) {
712 kmem_cache_free(event_cache, event_return);
713 module_put(event->desc->owner);
714 module_put(event->desc->owner);
715 goto statedump_error;
716 }
717 list_add(&event_return->list, &chan->session->events);
718 break;
719 }
ab2277d6 720 case LTTNG_KERNEL_FUNCTION:
a0493bef
MD
721 /*
722 * Needs to be explicitly enabled after creation, since
723 * we may want to apply filters.
724 */
725 event->enabled = 0;
3c997079
MD
726 event->registered = 1;
727 /*
728 * Populate lttng_event structure before event
729 * registration.
730 */
731 smp_wmb();
732 ret = lttng_ftrace_register(event_name,
e0a7a7c4
MD
733 event_param->u.ftrace.symbol_name,
734 event);
abc0446a 735 if (ret) {
e0a7a7c4 736 goto register_error;
abc0446a 737 }
edeb3137
MD
738 ret = try_module_get(event->desc->owner);
739 WARN_ON_ONCE(!ret);
e0a7a7c4 740 break;
1ec65de1 741 case LTTNG_KERNEL_NOOP:
33a39a3c 742 case LTTNG_KERNEL_SYSCALL:
a0493bef
MD
743 /*
744 * Needs to be explicitly enabled after creation, since
745 * we may want to apply filters.
746 */
747 event->enabled = 0;
3c997079
MD
748 event->registered = 0;
749 event->desc = event_desc;
abc0446a 750 if (!event->desc) {
4cf0bf51 751 ret = -EINVAL;
259b6cb3 752 goto register_error;
abc0446a 753 }
1ec65de1 754 break;
baf20995
MD
755 default:
756 WARN_ON_ONCE(1);
4cf0bf51 757 ret = -EINVAL;
df07930b 758 goto register_error;
baf20995 759 }
a90917c3 760 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
abc0446a
MD
761 WARN_ON_ONCE(ret > 0);
762 if (ret) {
c099397a 763 goto statedump_error;
abc0446a 764 }
3c997079 765 hlist_add_head(&event->hlist, head);
dda6a249 766 list_add(&event->list, &chan->session->events);
4e3c1b9b
MD
767 return event;
768
c099397a 769statedump_error:
259b6cb3 770 /* If a statedump error occurs, events will not be readable. */
11b5a3c2 771register_error:
11b5a3c2 772 kmem_cache_free(event_cache, event);
4e3c1b9b
MD
773cache_error:
774exist:
3c997079 775type_error:
e5382b6d 776full:
4cf0bf51 777 return ERR_PTR(ret);
4e3c1b9b
MD
778}
779
3c997079
MD
780struct lttng_event *lttng_event_create(struct lttng_channel *chan,
781 struct lttng_kernel_event *event_param,
782 void *filter,
783 const struct lttng_event_desc *event_desc,
784 enum lttng_kernel_instrumentation itype)
785{
786 struct lttng_event *event;
787
788 mutex_lock(&sessions_mutex);
789 event = _lttng_event_create(chan, event_param, filter, event_desc,
790 itype);
791 mutex_unlock(&sessions_mutex);
792 return event;
793}
794
795/* Only used for tracepoints for now. */
796static
797void register_event(struct lttng_event *event)
798{
799 const struct lttng_event_desc *desc;
f127e61e 800 int ret = -EINVAL;
3c997079 801
3c997079
MD
802 if (event->registered)
803 return;
4ecb5ad5 804
3c997079 805 desc = event->desc;
4ecb5ad5
MD
806 switch (event->instrumentation) {
807 case LTTNG_KERNEL_TRACEPOINT:
808 ret = lttng_wrapper_tracepoint_probe_register(desc->kname,
809 desc->probe_callback,
810 event);
811 break;
812 case LTTNG_KERNEL_SYSCALL:
813 ret = lttng_syscall_filter_enable(event->chan,
814 desc->name);
815 break;
816 case LTTNG_KERNEL_KPROBE:
817 case LTTNG_KERNEL_KRETPROBE:
818 case LTTNG_KERNEL_FUNCTION:
819 case LTTNG_KERNEL_NOOP:
820 ret = 0;
821 break;
822 default:
823 WARN_ON_ONCE(1);
824 }
3c997079
MD
825 if (!ret)
826 event->registered = 1;
827}
828
4e3c1b9b
MD
829/*
830 * Only used internally at session destruction.
831 */
a90917c3 832int _lttng_event_unregister(struct lttng_event *event)
4e3c1b9b 833{
4ecb5ad5 834 const struct lttng_event_desc *desc;
11b5a3c2
MD
835 int ret = -EINVAL;
836
3c997079
MD
837 if (!event->registered)
838 return 0;
839
4ecb5ad5 840 desc = event->desc;
38d024ae 841 switch (event->instrumentation) {
ab2277d6 842 case LTTNG_KERNEL_TRACEPOINT:
20591cf7 843 ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
85a9ca7f 844 event->desc->probe_callback,
11b5a3c2 845 event);
baf20995 846 break;
ab2277d6 847 case LTTNG_KERNEL_KPROBE:
f17701fb 848 lttng_kprobes_unregister(event);
d6d808f3
MD
849 ret = 0;
850 break;
7371f44c
MD
851 case LTTNG_KERNEL_KRETPROBE:
852 lttng_kretprobes_unregister(event);
853 ret = 0;
854 break;
ab2277d6 855 case LTTNG_KERNEL_FUNCTION:
e0a7a7c4
MD
856 lttng_ftrace_unregister(event);
857 ret = 0;
858 break;
33a39a3c 859 case LTTNG_KERNEL_SYSCALL:
4ecb5ad5
MD
860 ret = lttng_syscall_filter_disable(event->chan,
861 desc->name);
862 break;
863 case LTTNG_KERNEL_NOOP:
259b6cb3
MD
864 ret = 0;
865 break;
baf20995
MD
866 default:
867 WARN_ON_ONCE(1);
868 }
3c997079
MD
869 if (!ret)
870 event->registered = 0;
dda6a249
MD
871 return ret;
872}
873
874/*
875 * Only used internally at session destruction.
876 */
be066e6c 877static
a90917c3 878void _lttng_event_destroy(struct lttng_event *event)
dda6a249 879{
edeb3137
MD
880 switch (event->instrumentation) {
881 case LTTNG_KERNEL_TRACEPOINT:
a90917c3 882 lttng_event_put(event->desc);
edeb3137
MD
883 break;
884 case LTTNG_KERNEL_KPROBE:
885 module_put(event->desc->owner);
886 lttng_kprobes_destroy_private(event);
887 break;
7371f44c
MD
888 case LTTNG_KERNEL_KRETPROBE:
889 module_put(event->desc->owner);
890 lttng_kretprobes_destroy_private(event);
891 break;
edeb3137
MD
892 case LTTNG_KERNEL_FUNCTION:
893 module_put(event->desc->owner);
894 lttng_ftrace_destroy_private(event);
895 break;
259b6cb3 896 case LTTNG_KERNEL_NOOP:
33a39a3c 897 case LTTNG_KERNEL_SYSCALL:
259b6cb3 898 break;
edeb3137
MD
899 default:
900 WARN_ON_ONCE(1);
901 }
dda6a249 902 list_del(&event->list);
8070f5c0 903 lttng_destroy_context(event->ctx);
11b5a3c2 904 kmem_cache_free(event_cache, event);
4e3c1b9b
MD
905}
906
e0130fab
MD
907int lttng_session_track_pid(struct lttng_session *session, int pid)
908{
909 int ret;
910
911 if (pid < -1)
912 return -EINVAL;
913 mutex_lock(&sessions_mutex);
914 if (pid == -1) {
915 /* track all pids: destroy tracker. */
916 if (session->pid_tracker) {
917 struct lttng_pid_tracker *lpf;
918
919 lpf = session->pid_tracker;
920 rcu_assign_pointer(session->pid_tracker, NULL);
921 synchronize_trace();
922 lttng_pid_tracker_destroy(lpf);
923 }
924 ret = 0;
925 } else {
926 if (!session->pid_tracker) {
927 struct lttng_pid_tracker *lpf;
928
929 lpf = lttng_pid_tracker_create();
930 if (!lpf) {
931 ret = -ENOMEM;
932 goto unlock;
933 }
934 ret = lttng_pid_tracker_add(lpf, pid);
935 rcu_assign_pointer(session->pid_tracker, lpf);
936 } else {
937 ret = lttng_pid_tracker_add(session->pid_tracker, pid);
938 }
939 }
940unlock:
941 mutex_unlock(&sessions_mutex);
942 return ret;
943}
944
945int lttng_session_untrack_pid(struct lttng_session *session, int pid)
946{
947 int ret;
948
949 if (pid < -1)
950 return -EINVAL;
951 mutex_lock(&sessions_mutex);
952 if (pid == -1) {
953 /* untrack all pids: replace by empty tracker. */
954 struct lttng_pid_tracker *old_lpf = session->pid_tracker;
955 struct lttng_pid_tracker *lpf;
956
957 lpf = lttng_pid_tracker_create();
958 if (!lpf) {
959 ret = -ENOMEM;
960 goto unlock;
961 }
962 rcu_assign_pointer(session->pid_tracker, lpf);
963 synchronize_trace();
964 if (old_lpf)
965 lttng_pid_tracker_destroy(old_lpf);
966 ret = 0;
967 } else {
968 if (!session->pid_tracker) {
969 ret = -ENOENT;
970 goto unlock;
971 }
972 ret = lttng_pid_tracker_del(session->pid_tracker, pid);
973 }
974unlock:
975 mutex_unlock(&sessions_mutex);
976 return ret;
977}
978
7e6f9ef6
MD
979static
980void *pid_list_start(struct seq_file *m, loff_t *pos)
981{
982 struct lttng_session *session = m->private;
983 struct lttng_pid_tracker *lpf;
984 struct lttng_pid_hash_node *e;
985 int iter = 0, i;
986
987 mutex_lock(&sessions_mutex);
988 lpf = session->pid_tracker;
989 if (lpf) {
990 for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
991 struct hlist_head *head = &lpf->pid_hash[i];
992
f934e302 993 lttng_hlist_for_each_entry(e, head, hlist) {
7e6f9ef6
MD
994 if (iter++ >= *pos)
995 return e;
996 }
997 }
998 } else {
999 /* PID tracker disabled. */
1000 if (iter >= *pos && iter == 0) {
1001 return session; /* empty tracker */
1002 }
1003 iter++;
1004 }
1005 /* End of list */
1006 return NULL;
1007}
1008
1009/* Called with sessions_mutex held. */
1010static
1011void *pid_list_next(struct seq_file *m, void *p, loff_t *ppos)
1012{
1013 struct lttng_session *session = m->private;
1014 struct lttng_pid_tracker *lpf;
1015 struct lttng_pid_hash_node *e;
1016 int iter = 0, i;
1017
1018 (*ppos)++;
1019 lpf = session->pid_tracker;
1020 if (lpf) {
1021 for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
1022 struct hlist_head *head = &lpf->pid_hash[i];
1023
f934e302 1024 lttng_hlist_for_each_entry(e, head, hlist) {
7e6f9ef6
MD
1025 if (iter++ >= *ppos)
1026 return e;
1027 }
1028 }
1029 } else {
1030 /* PID tracker disabled. */
1031 if (iter >= *ppos && iter == 0)
1032 return session; /* empty tracker */
1033 iter++;
1034 }
1035
1036 /* End of list */
1037 return NULL;
1038}
1039
1040static
1041void pid_list_stop(struct seq_file *m, void *p)
1042{
1043 mutex_unlock(&sessions_mutex);
1044}
1045
1046static
1047int pid_list_show(struct seq_file *m, void *p)
1048{
1049 int pid;
1050
1051 if (p == m->private) {
1052 /* Tracker disabled. */
1053 pid = -1;
1054 } else {
1055 const struct lttng_pid_hash_node *e = p;
1056
1057 pid = lttng_pid_tracker_get_node_pid(e);
1058 }
1059 seq_printf(m, "process { pid = %d; };\n", pid);
1060 return 0;
1061}
1062
1063static
1064const struct seq_operations lttng_tracker_pids_list_seq_ops = {
1065 .start = pid_list_start,
1066 .next = pid_list_next,
1067 .stop = pid_list_stop,
1068 .show = pid_list_show,
1069};
1070
1071static
1072int lttng_tracker_pids_list_open(struct inode *inode, struct file *file)
1073{
1074 return seq_open(file, &lttng_tracker_pids_list_seq_ops);
1075}
1076
1077static
1078int lttng_tracker_pids_list_release(struct inode *inode, struct file *file)
1079{
1080 struct seq_file *m = file->private_data;
1081 struct lttng_session *session = m->private;
1082 int ret;
1083
1084 WARN_ON_ONCE(!session);
1085 ret = seq_release(inode, file);
1086 if (!ret && session)
1087 fput(session->file);
1088 return ret;
1089}
1090
1091const struct file_operations lttng_tracker_pids_list_fops = {
1092 .owner = THIS_MODULE,
1093 .open = lttng_tracker_pids_list_open,
1094 .read = seq_read,
1095 .llseek = seq_lseek,
1096 .release = lttng_tracker_pids_list_release,
1097};
1098
1099int lttng_session_list_tracker_pids(struct lttng_session *session)
1100{
1101 struct file *tracker_pids_list_file;
1102 struct seq_file *m;
1103 int file_fd, ret;
1104
1105 file_fd = lttng_get_unused_fd();
1106 if (file_fd < 0) {
1107 ret = file_fd;
1108 goto fd_error;
1109 }
1110
1111 tracker_pids_list_file = anon_inode_getfile("[lttng_tracker_pids_list]",
1112 &lttng_tracker_pids_list_fops,
1113 NULL, O_RDWR);
1114 if (IS_ERR(tracker_pids_list_file)) {
1115 ret = PTR_ERR(tracker_pids_list_file);
1116 goto file_error;
1117 }
9c1f4643
MD
1118 if (atomic_long_add_unless(&session->file->f_count,
1119 1, INT_MAX) == INT_MAX) {
1120 goto refcount_error;
1121 }
7e6f9ef6
MD
1122 ret = lttng_tracker_pids_list_fops.open(NULL, tracker_pids_list_file);
1123 if (ret < 0)
1124 goto open_error;
1125 m = tracker_pids_list_file->private_data;
1126 m->private = session;
1127 fd_install(file_fd, tracker_pids_list_file);
7e6f9ef6
MD
1128
1129 return file_fd;
1130
1131open_error:
9c1f4643
MD
1132 atomic_long_dec(&session->file->f_count);
1133refcount_error:
7e6f9ef6
MD
1134 fput(tracker_pids_list_file);
1135file_error:
1136 put_unused_fd(file_fd);
1137fd_error:
1138 return ret;
1139}
1140
3c997079
MD
1141/*
1142 * Enabler management.
1143 */
1144static
4ecb5ad5
MD
1145int lttng_match_enabler_wildcard(const char *desc_name,
1146 const char *name)
3c997079 1147{
3c997079 1148 /* Compare excluding final '*' */
4ecb5ad5 1149 if (strncmp(desc_name, name, strlen(name) - 1))
3c997079
MD
1150 return 0;
1151 return 1;
1152}
1153
1154static
4ecb5ad5
MD
1155int lttng_match_enabler_name(const char *desc_name,
1156 const char *name)
3c997079 1157{
4ecb5ad5 1158 if (strcmp(desc_name, name))
3c997079
MD
1159 return 0;
1160 return 1;
1161}
1162
1163static
1164int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
1165 struct lttng_enabler *enabler)
1166{
4ecb5ad5
MD
1167 const char *desc_name, *enabler_name;
1168
1169 enabler_name = enabler->event_param.name;
1170 switch (enabler->event_param.instrumentation) {
1171 case LTTNG_KERNEL_TRACEPOINT:
1172 desc_name = desc->name;
1173 break;
1174 case LTTNG_KERNEL_SYSCALL:
1175 desc_name = desc->name;
1176 if (!strncmp(desc_name, "compat_", strlen("compat_")))
1177 desc_name += strlen("compat_");
1178 if (!strncmp(desc_name, "syscall_exit_",
1179 strlen("syscall_exit_"))) {
1180 desc_name += strlen("syscall_exit_");
1181 } else if (!strncmp(desc_name, "syscall_entry_",
1182 strlen("syscall_entry_"))) {
1183 desc_name += strlen("syscall_entry_");
1184 } else {
1185 WARN_ON_ONCE(1);
1186 return -EINVAL;
1187 }
1188 break;
1189 default:
1190 WARN_ON_ONCE(1);
1191 return -EINVAL;
1192 }
3c997079
MD
1193 switch (enabler->type) {
1194 case LTTNG_ENABLER_WILDCARD:
4ecb5ad5 1195 return lttng_match_enabler_wildcard(desc_name, enabler_name);
3c997079 1196 case LTTNG_ENABLER_NAME:
4ecb5ad5 1197 return lttng_match_enabler_name(desc_name, enabler_name);
3c997079
MD
1198 default:
1199 return -EINVAL;
1200 }
1201}
1202
1203static
1204int lttng_event_match_enabler(struct lttng_event *event,
1205 struct lttng_enabler *enabler)
1206{
33a39a3c
MD
1207 if (enabler->event_param.instrumentation != event->instrumentation)
1208 return 0;
3c997079
MD
1209 if (lttng_desc_match_enabler(event->desc, enabler)
1210 && event->chan == enabler->chan)
1211 return 1;
1212 else
1213 return 0;
1214}
1215
1216static
1217struct lttng_enabler_ref *lttng_event_enabler_ref(struct lttng_event *event,
1218 struct lttng_enabler *enabler)
1219{
1220 struct lttng_enabler_ref *enabler_ref;
1221
1222 list_for_each_entry(enabler_ref,
1223 &event->enablers_ref_head, node) {
1224 if (enabler_ref->ref == enabler)
1225 return enabler_ref;
1226 }
1227 return NULL;
1228}
1229
3c997079 1230static
33a39a3c 1231void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler)
3c997079
MD
1232{
1233 struct lttng_session *session = enabler->chan->session;
1234 struct lttng_probe_desc *probe_desc;
1235 const struct lttng_event_desc *desc;
1236 int i;
1237 struct list_head *probe_list;
1238
1239 probe_list = lttng_get_probe_list_head();
1240 /*
1241 * For each probe event, if we find that a probe event matches
1242 * our enabler, create an associated lttng_event if not
1243 * already present.
1244 */
1245 list_for_each_entry(probe_desc, probe_list, head) {
1246 for (i = 0; i < probe_desc->nr_events; i++) {
1247 int found = 0;
1248 struct hlist_head *head;
1249 const char *event_name;
1250 size_t name_len;
1251 uint32_t hash;
1252 struct lttng_event *event;
1253
1254 desc = probe_desc->event_desc[i];
1255 if (!lttng_desc_match_enabler(desc, enabler))
1256 continue;
1257 event_name = desc->name;
1258 name_len = strlen(event_name);
1259
1260 /*
1261 * Check if already created.
1262 */
1263 hash = jhash(event_name, name_len, 0);
1264 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
d216ecae 1265 lttng_hlist_for_each_entry(event, head, hlist) {
3c997079
MD
1266 if (event->desc == desc
1267 && event->chan == enabler->chan)
1268 found = 1;
1269 }
1270 if (found)
1271 continue;
1272
1273 /*
1274 * We need to create an event for this
1275 * event probe.
1276 */
1277 event = _lttng_event_create(enabler->chan,
1278 NULL, NULL, desc,
1279 LTTNG_KERNEL_TRACEPOINT);
1280 if (!event) {
1281 printk(KERN_INFO "Unable to create event %s\n",
1282 probe_desc->event_desc[i]->name);
1283 }
1284 }
1285 }
1286}
1287
33a39a3c
MD
1288static
1289void lttng_create_syscall_if_missing(struct lttng_enabler *enabler)
1290{
1291 int ret;
1292
1293 ret = lttng_syscalls_register(enabler->chan, NULL);
1294 WARN_ON_ONCE(ret);
1295}
1296
1297/*
1298 * Create struct lttng_event if it is missing and present in the list of
1299 * tracepoint probes.
1300 * Should be called with sessions mutex held.
1301 */
1302static
1303void lttng_create_event_if_missing(struct lttng_enabler *enabler)
1304{
1305 switch (enabler->event_param.instrumentation) {
1306 case LTTNG_KERNEL_TRACEPOINT:
1307 lttng_create_tracepoint_if_missing(enabler);
1308 break;
1309 case LTTNG_KERNEL_SYSCALL:
1310 lttng_create_syscall_if_missing(enabler);
1311 break;
1312 default:
1313 WARN_ON_ONCE(1);
1314 break;
1315 }
1316}
1317
3c997079
MD
1318/*
1319 * Create events associated with an enabler (if not already present),
1320 * and add backward reference from the event to the enabler.
1321 * Should be called with sessions mutex held.
1322 */
1323static
1324int lttng_enabler_ref_events(struct lttng_enabler *enabler)
1325{
1326 struct lttng_session *session = enabler->chan->session;
1327 struct lttng_event *event;
1328
1329 /* First ensure that probe events are created for this enabler. */
1330 lttng_create_event_if_missing(enabler);
1331
1332 /* For each event matching enabler in session event list. */
1333 list_for_each_entry(event, &session->events, list) {
1334 struct lttng_enabler_ref *enabler_ref;
1335
1336 if (!lttng_event_match_enabler(event, enabler))
1337 continue;
1338 enabler_ref = lttng_event_enabler_ref(event, enabler);
1339 if (!enabler_ref) {
1340 /*
1341 * If no backward ref, create it.
1342 * Add backward ref from event to enabler.
1343 */
1344 enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL);
1345 if (!enabler_ref)
1346 return -ENOMEM;
1347 enabler_ref->ref = enabler;
1348 list_add(&enabler_ref->node,
1349 &event->enablers_ref_head);
1350 }
1351
f127e61e
MD
1352 /*
1353 * Link filter bytecodes if not linked yet.
1354 */
1355 lttng_enabler_event_link_bytecode(event, enabler);
1356
3c997079
MD
1357 /* TODO: merge event context. */
1358 }
1359 return 0;
1360}
1361
1362/*
1363 * Called at module load: connect the probe on all enablers matching
1364 * this event.
1365 * Called with sessions lock held.
1366 */
1367int lttng_fix_pending_events(void)
1368{
1369 struct lttng_session *session;
1370
1371 list_for_each_entry(session, &sessions, list)
1372 lttng_session_lazy_sync_enablers(session);
1373 return 0;
1374}
1375
1376struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
1377 struct lttng_kernel_event *event_param,
1378 struct lttng_channel *chan)
1379{
1380 struct lttng_enabler *enabler;
1381
1382 enabler = kzalloc(sizeof(*enabler), GFP_KERNEL);
1383 if (!enabler)
1384 return NULL;
1385 enabler->type = type;
07dfc1d0 1386 INIT_LIST_HEAD(&enabler->filter_bytecode_head);
3c997079
MD
1387 memcpy(&enabler->event_param, event_param,
1388 sizeof(enabler->event_param));
1389 enabler->chan = chan;
1390 /* ctx left NULL */
4fd19d8a 1391 enabler->enabled = 0;
3c997079
MD
1392 enabler->evtype = LTTNG_TYPE_ENABLER;
1393 mutex_lock(&sessions_mutex);
1394 list_add(&enabler->node, &enabler->chan->session->enablers_head);
1395 lttng_session_lazy_sync_enablers(enabler->chan->session);
1396 mutex_unlock(&sessions_mutex);
1397 return enabler;
1398}
1399
1400int lttng_enabler_enable(struct lttng_enabler *enabler)
1401{
1402 mutex_lock(&sessions_mutex);
1403 enabler->enabled = 1;
1404 lttng_session_lazy_sync_enablers(enabler->chan->session);
1405 mutex_unlock(&sessions_mutex);
1406 return 0;
1407}
1408
1409int lttng_enabler_disable(struct lttng_enabler *enabler)
1410{
1411 mutex_lock(&sessions_mutex);
1412 enabler->enabled = 0;
1413 lttng_session_lazy_sync_enablers(enabler->chan->session);
1414 mutex_unlock(&sessions_mutex);
1415 return 0;
1416}
1417
07dfc1d0
MD
1418int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
1419 struct lttng_kernel_filter_bytecode __user *bytecode)
1420{
1421 struct lttng_filter_bytecode_node *bytecode_node;
1422 uint32_t bytecode_len;
1423 int ret;
1424
1425 ret = get_user(bytecode_len, &bytecode->len);
1426 if (ret)
1427 return ret;
1428 bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len,
1429 GFP_KERNEL);
1430 if (!bytecode_node)
1431 return -ENOMEM;
1432 ret = copy_from_user(&bytecode_node->bc, bytecode,
1433 sizeof(*bytecode) + bytecode_len);
1434 if (ret)
1435 goto error_free;
1436 bytecode_node->enabler = enabler;
1437 /* Enforce length based on allocated size */
1438 bytecode_node->bc.len = bytecode_len;
1439 list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head);
1440 lttng_session_lazy_sync_enablers(enabler->chan->session);
1441 return 0;
1442
1443error_free:
1444 kfree(bytecode_node);
1445 return ret;
1446}
1447
3c997079
MD
1448int lttng_enabler_attach_context(struct lttng_enabler *enabler,
1449 struct lttng_kernel_context *context_param)
1450{
1451 return -ENOSYS;
1452}
1453
1454static
1455void lttng_enabler_destroy(struct lttng_enabler *enabler)
1456{
07dfc1d0
MD
1457 struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node;
1458
1459 /* Destroy filter bytecode */
1460 list_for_each_entry_safe(filter_node, tmp_filter_node,
1461 &enabler->filter_bytecode_head, node) {
1462 kfree(filter_node);
1463 }
1464
3c997079
MD
1465 /* Destroy contexts */
1466 lttng_destroy_context(enabler->ctx);
1467
1468 list_del(&enabler->node);
1469 kfree(enabler);
1470}
1471
1472/*
1473 * lttng_session_sync_enablers should be called just before starting a
1474 * session.
1475 * Should be called with sessions mutex held.
1476 */
1477static
1478void lttng_session_sync_enablers(struct lttng_session *session)
1479{
1480 struct lttng_enabler *enabler;
1481 struct lttng_event *event;
1482
1483 list_for_each_entry(enabler, &session->enablers_head, node)
1484 lttng_enabler_ref_events(enabler);
1485 /*
1486 * For each event, if at least one of its enablers is enabled,
1487 * and its channel and session transient states are enabled, we
1488 * enable the event, else we disable it.
1489 */
1490 list_for_each_entry(event, &session->events, list) {
1491 struct lttng_enabler_ref *enabler_ref;
07dfc1d0
MD
1492 struct lttng_bytecode_runtime *runtime;
1493 int enabled = 0, has_enablers_without_bytecode = 0;
3c997079 1494
4ecb5ad5
MD
1495 switch (event->instrumentation) {
1496 case LTTNG_KERNEL_TRACEPOINT:
1497 case LTTNG_KERNEL_SYSCALL:
3c997079
MD
1498 /* Enable events */
1499 list_for_each_entry(enabler_ref,
1500 &event->enablers_ref_head, node) {
1501 if (enabler_ref->ref->enabled) {
1502 enabled = 1;
1503 break;
1504 }
1505 }
4ecb5ad5
MD
1506 break;
1507 default:
3c997079
MD
1508 /* Not handled with lazy sync. */
1509 continue;
1510 }
1511 /*
1512 * Enabled state is based on union of enablers, with
1513 * intesection of session and channel transient enable
1514 * states.
1515 */
1516 enabled = enabled && session->tstate && event->chan->tstate;
1517
1518 ACCESS_ONCE(event->enabled) = enabled;
1519 /*
1520 * Sync tracepoint registration with event enabled
1521 * state.
1522 */
1523 if (enabled) {
1524 register_event(event);
1525 } else {
1526 _lttng_event_unregister(event);
1527 }
07dfc1d0
MD
1528
1529 /* Check if has enablers without bytecode enabled */
1530 list_for_each_entry(enabler_ref,
1531 &event->enablers_ref_head, node) {
1532 if (enabler_ref->ref->enabled
1533 && list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1534 has_enablers_without_bytecode = 1;
1535 break;
1536 }
1537 }
1538 event->has_enablers_without_bytecode =
1539 has_enablers_without_bytecode;
1540
1541 /* Enable filters */
1542 list_for_each_entry(runtime,
f127e61e 1543 &event->bytecode_runtime_head, node)
07dfc1d0 1544 lttng_filter_sync_state(runtime);
3c997079
MD
1545 }
1546}
1547
1548/*
1549 * Apply enablers to session events, adding events to session if need
1550 * be. It is required after each modification applied to an active
1551 * session, and right before session "start".
1552 * "lazy" sync means we only sync if required.
1553 * Should be called with sessions mutex held.
1554 */
1555static
1556void lttng_session_lazy_sync_enablers(struct lttng_session *session)
1557{
1558 /* We can skip if session is not active */
1559 if (!session->active)
1560 return;
1561 lttng_session_sync_enablers(session);
1562}
1563
1ec3f75a 1564/*
d83004aa
JD
1565 * Serialize at most one packet worth of metadata into a metadata
1566 * channel.
92d9f5e6
MD
1567 * We grab the metadata cache mutex to get exclusive access to our metadata
1568 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1569 * allows us to do racy operations such as looking for remaining space left in
1570 * packet and write, since mutual exclusion protects us from concurrent writes.
1571 * Mutual exclusion on the metadata cache allow us to read the cache content
1572 * without racing against reallocation of the cache by updates.
35097f36
JD
1573 * Returns the number of bytes written in the channel, 0 if no data
1574 * was written and a negative value on error.
1ec3f75a 1575 */
b3b8072b
MD
1576int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
1577 struct channel *chan)
d83004aa
JD
1578{
1579 struct lib_ring_buffer_ctx ctx;
1580 int ret = 0;
1581 size_t len, reserve_len;
1582
f613e3e6 1583 /*
92d9f5e6
MD
1584 * Ensure we support mutiple get_next / put sequences followed by
1585 * put_next. The metadata cache lock protects reading the metadata
1586 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1587 * "flush" operations on the buffer invoked by different processes.
1588 * Moreover, since the metadata cache memory can be reallocated, we
1589 * need to have exclusive access against updates even though we only
1590 * read it.
f613e3e6 1591 */
92d9f5e6 1592 mutex_lock(&stream->metadata_cache->lock);
f613e3e6
MD
1593 WARN_ON(stream->metadata_in < stream->metadata_out);
1594 if (stream->metadata_in != stream->metadata_out)
de23d59d 1595 goto end;
f613e3e6 1596
9616f0bf
JD
1597 /* Metadata regenerated, change the version. */
1598 if (stream->metadata_cache->version != stream->version)
1599 stream->version = stream->metadata_cache->version;
1600
d83004aa 1601 len = stream->metadata_cache->metadata_written -
f613e3e6 1602 stream->metadata_in;
9de2c215 1603 if (!len)
de23d59d 1604 goto end;
d83004aa 1605 reserve_len = min_t(size_t,
b3b8072b 1606 stream->transport->ops.packet_avail_size(chan),
d83004aa 1607 len);
b3b8072b 1608 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
d83004aa
JD
1609 sizeof(char), -1);
1610 /*
1611 * If reservation failed, return an error to the caller.
1612 */
b3b8072b 1613 ret = stream->transport->ops.event_reserve(&ctx, 0);
d83004aa
JD
1614 if (ret != 0) {
1615 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
1616 goto end;
1617 }
b3b8072b 1618 stream->transport->ops.event_write(&ctx,
f613e3e6 1619 stream->metadata_cache->data + stream->metadata_in,
d83004aa 1620 reserve_len);
b3b8072b 1621 stream->transport->ops.event_commit(&ctx);
f613e3e6 1622 stream->metadata_in += reserve_len;
d83004aa
JD
1623 ret = reserve_len;
1624
1625end:
92d9f5e6 1626 mutex_unlock(&stream->metadata_cache->lock);
d83004aa
JD
1627 return ret;
1628}
1629
1630/*
1631 * Write the metadata to the metadata cache.
1632 * Must be called with sessions_mutex held.
92d9f5e6
MD
1633 * The metadata cache lock protects us from concurrent read access from
1634 * thread outputting metadata content to ring buffer.
d83004aa 1635 */
a90917c3 1636int lttng_metadata_printf(struct lttng_session *session,
c099397a
MD
1637 const char *fmt, ...)
1638{
c099397a 1639 char *str;
d83004aa 1640 size_t len;
c099397a 1641 va_list ap;
d83004aa 1642 struct lttng_metadata_stream *stream;
c099397a
MD
1643
1644 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
1645
1646 va_start(ap, fmt);
1647 str = kvasprintf(GFP_KERNEL, fmt, ap);
1648 va_end(ap);
1649 if (!str)
1650 return -ENOMEM;
1651
1ec3f75a 1652 len = strlen(str);
92d9f5e6 1653 mutex_lock(&session->metadata_cache->lock);
d83004aa
JD
1654 if (session->metadata_cache->metadata_written + len >
1655 session->metadata_cache->cache_alloc) {
1656 char *tmp_cache_realloc;
1657 unsigned int tmp_cache_alloc_size;
1658
1659 tmp_cache_alloc_size = max_t(unsigned int,
1660 session->metadata_cache->cache_alloc + len,
1661 session->metadata_cache->cache_alloc << 1);
f6da044f 1662 tmp_cache_realloc = lttng_vzalloc(tmp_cache_alloc_size);
d83004aa
JD
1663 if (!tmp_cache_realloc)
1664 goto err;
a606b6e8
ML
1665 if (session->metadata_cache->data) {
1666 memcpy(tmp_cache_realloc,
1667 session->metadata_cache->data,
1668 session->metadata_cache->cache_alloc);
1669 vfree(session->metadata_cache->data);
1670 }
1671
d83004aa
JD
1672 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
1673 session->metadata_cache->data = tmp_cache_realloc;
c099397a 1674 }
d83004aa
JD
1675 memcpy(session->metadata_cache->data +
1676 session->metadata_cache->metadata_written,
1677 str, len);
1678 session->metadata_cache->metadata_written += len;
92d9f5e6 1679 mutex_unlock(&session->metadata_cache->lock);
c099397a 1680 kfree(str);
d83004aa
JD
1681
1682 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
1683 wake_up_interruptible(&stream->read_wait);
1684
1685 return 0;
1686
1687err:
92d9f5e6 1688 mutex_unlock(&session->metadata_cache->lock);
d83004aa
JD
1689 kfree(str);
1690 return -ENOMEM;
c099397a
MD
1691}
1692
f513b2bf
MD
1693static
1694int print_tabs(struct lttng_session *session, size_t nesting)
1695{
1696 size_t i;
1697
1698 for (i = 0; i < nesting; i++) {
1699 int ret;
1700
1701 ret = lttng_metadata_printf(session, " ");
1702 if (ret) {
1703 return ret;
1704 }
1705 }
1706 return 0;
1707}
1708
1709/*
1710 * Must be called with sessions_mutex held.
1711 */
1712static
1713int _lttng_struct_type_statedump(struct lttng_session *session,
1714 const struct lttng_type *type,
1715 size_t nesting)
1716{
1717 int ret;
1718 uint32_t i, nr_fields;
1719
1720 ret = print_tabs(session, nesting);
1721 if (ret)
1722 return ret;
1723 ret = lttng_metadata_printf(session,
1724 "struct {\n");
1725 if (ret)
1726 return ret;
1727 nr_fields = type->u._struct.nr_fields;
1728 for (i = 0; i < nr_fields; i++) {
1729 const struct lttng_event_field *iter_field;
1730
1731 iter_field = &type->u._struct.fields[i];
1732 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
1733 if (ret)
1734 return ret;
1735 }
1736 ret = print_tabs(session, nesting);
1737 if (ret)
1738 return ret;
1739 ret = lttng_metadata_printf(session,
1740 "}");
1741 return ret;
1742}
1743
1744/*
1745 * Must be called with sessions_mutex held.
1746 */
1747static
1748int _lttng_struct_statedump(struct lttng_session *session,
1749 const struct lttng_event_field *field,
1750 size_t nesting)
1751{
1752 int ret;
1753
1754 ret = _lttng_struct_type_statedump(session,
1755 &field->type, nesting);
1756 if (ret)
1757 return ret;
1758 ret = lttng_metadata_printf(session,
1759 "_%s;\n",
1760 field->name);
1761 return ret;
1762}
1763
65c85aa6
MD
1764/*
1765 * Must be called with sessions_mutex held.
1766 */
1767static
1768int _lttng_variant_type_statedump(struct lttng_session *session,
1769 const struct lttng_type *type,
1770 size_t nesting)
1771{
1772 int ret;
1773 uint32_t i, nr_choices;
1774
1775 ret = print_tabs(session, nesting);
1776 if (ret)
1777 return ret;
1778 ret = lttng_metadata_printf(session,
1779 "variant <_%s> {\n",
1780 type->u.variant.tag_name);
1781 if (ret)
1782 return ret;
1783 nr_choices = type->u.variant.nr_choices;
1784 for (i = 0; i < nr_choices; i++) {
1785 const struct lttng_event_field *iter_field;
1786
1787 iter_field = &type->u.variant.choices[i];
1788 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
1789 if (ret)
1790 return ret;
1791 }
1792 ret = print_tabs(session, nesting);
1793 if (ret)
1794 return ret;
1795 ret = lttng_metadata_printf(session,
1796 "}");
1797 return ret;
1798}
1799
1800/*
1801 * Must be called with sessions_mutex held.
1802 */
1803static
1804int _lttng_variant_statedump(struct lttng_session *session,
1805 const struct lttng_event_field *field,
1806 size_t nesting)
1807{
1808 int ret;
1809
1810 ret = _lttng_variant_type_statedump(session,
1811 &field->type, nesting);
1812 if (ret)
1813 return ret;
1814 ret = lttng_metadata_printf(session,
1815 "_%s;\n",
1816 field->name);
1817 return ret;
1818}
1819
f513b2bf
MD
1820/*
1821 * Must be called with sessions_mutex held.
1822 */
1823static
1824int _lttng_array_compound_statedump(struct lttng_session *session,
1825 const struct lttng_event_field *field,
1826 size_t nesting)
1827{
1828 int ret;
1829 const struct lttng_type *elem_type;
1830
65c85aa6 1831 /* Only array of structures and variants are currently supported. */
f513b2bf
MD
1832 elem_type = field->type.u.array_compound.elem_type;
1833 switch (elem_type->atype) {
1834 case atype_struct:
1835 ret = _lttng_struct_type_statedump(session, elem_type, nesting);
1836 if (ret)
1837 return ret;
1838 break;
65c85aa6
MD
1839 case atype_variant:
1840 ret = _lttng_variant_type_statedump(session, elem_type, nesting);
1841 if (ret)
1842 return ret;
1843 break;
f513b2bf
MD
1844 default:
1845 return -EINVAL;
1846 }
1847 ret = lttng_metadata_printf(session,
1848 " _%s[%u];\n",
1849 field->name,
1850 field->type.u.array_compound.length);
1851 return ret;
1852}
1853
1854/*
1855 * Must be called with sessions_mutex held.
1856 */
1857static
1858int _lttng_sequence_compound_statedump(struct lttng_session *session,
1859 const struct lttng_event_field *field,
1860 size_t nesting)
1861{
1862 int ret;
1863 const char *length_name;
1864 const struct lttng_type *elem_type;
1865
1866 length_name = field->type.u.sequence_compound.length_name;
1867
65c85aa6 1868 /* Only array of structures and variants are currently supported. */
f513b2bf
MD
1869 elem_type = field->type.u.sequence_compound.elem_type;
1870 switch (elem_type->atype) {
1871 case atype_struct:
1872 ret = _lttng_struct_type_statedump(session, elem_type, nesting);
1873 if (ret)
1874 return ret;
1875 break;
65c85aa6
MD
1876 case atype_variant:
1877 ret = _lttng_variant_type_statedump(session, elem_type, nesting);
1878 if (ret)
1879 return ret;
1880 break;
f513b2bf
MD
1881 default:
1882 return -EINVAL;
1883 }
1884 ret = lttng_metadata_printf(session,
1885 " _%s[ _%s ];\n",
1886 field->name,
1887 length_name);
1888 return ret;
1889}
1890
141ddf28
MD
1891/*
1892 * Must be called with sessions_mutex held.
1893 */
1894static
1895int _lttng_enum_statedump(struct lttng_session *session,
1896 const struct lttng_event_field *field,
1897 size_t nesting)
1898{
1899 const struct lttng_enum_desc *enum_desc;
1900 const struct lttng_integer_type *container_type;
1901 int ret;
1902 unsigned int i, nr_entries;
1903
1904 enum_desc = field->type.u.basic.enumeration.desc;
1905 container_type = &field->type.u.basic.enumeration.container_type;
1906 nr_entries = enum_desc->nr_entries;
1907
1908 ret = print_tabs(session, nesting);
1909 if (ret)
1910 goto end;
1911 ret = lttng_metadata_printf(session,
1912 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
1913 container_type->size,
1914 container_type->alignment,
1915 container_type->signedness,
1916 (container_type->encoding == lttng_encode_none)
1917 ? "none"
1918 : (container_type->encoding == lttng_encode_UTF8)
1919 ? "UTF8"
1920 : "ASCII",
1921 container_type->base);
1922 if (ret)
1923 goto end;
1924 /* Dump all entries */
1925 for (i = 0; i < nr_entries; i++) {
1926 const struct lttng_enum_entry *entry = &enum_desc->entries[i];
1927 int j, len;
1928
1929 ret = print_tabs(session, nesting + 1);
1930 if (ret)
1931 goto end;
1932 ret = lttng_metadata_printf(session,
1933 "\"");
1934 if (ret)
1935 goto end;
1936 len = strlen(entry->string);
1937 /* Escape the character '"' */
1938 for (j = 0; j < len; j++) {
1939 char c = entry->string[j];
1940
1941 switch (c) {
1942 case '"':
1943 ret = lttng_metadata_printf(session,
1944 "\\\"");
1945 break;
1946 case '\\':
1947 ret = lttng_metadata_printf(session,
1948 "\\\\");
1949 break;
1950 default:
1951 ret = lttng_metadata_printf(session,
1952 "%c", c);
1953 break;
1954 }
1955 if (ret)
1956 goto end;
1957 }
08ad1061
PP
1958 ret = lttng_metadata_printf(session, "\"");
1959 if (ret)
1960 goto end;
1961
1962 if (entry->options.is_auto) {
1963 ret = lttng_metadata_printf(session, ",\n");
1964 if (ret)
1965 goto end;
141ddf28 1966 } else {
7c559490
MD
1967 ret = lttng_metadata_printf(session,
1968 " = ");
1969 if (ret)
1970 goto end;
1971 if (entry->start.signedness)
141ddf28 1972 ret = lttng_metadata_printf(session,
7c559490
MD
1973 "%lld", (long long) entry->start.value);
1974 else
1975 ret = lttng_metadata_printf(session,
1976 "%llu", entry->start.value);
1977 if (ret)
1978 goto end;
1979 if (entry->start.signedness == entry->end.signedness &&
1980 entry->start.value
1981 == entry->end.value) {
141ddf28 1982 ret = lttng_metadata_printf(session,
7c559490
MD
1983 ",\n");
1984 } else {
1985 if (entry->end.signedness) {
1986 ret = lttng_metadata_printf(session,
1987 " ... %lld,\n",
1988 (long long) entry->end.value);
1989 } else {
1990 ret = lttng_metadata_printf(session,
1991 " ... %llu,\n",
1992 entry->end.value);
1993 }
141ddf28 1994 }
7c559490
MD
1995 if (ret)
1996 goto end;
141ddf28 1997 }
141ddf28
MD
1998 }
1999 ret = print_tabs(session, nesting);
2000 if (ret)
2001 goto end;
2002 ret = lttng_metadata_printf(session, "} _%s;\n",
2003 field->name);
2004end:
2005 return ret;
2006}
2007
d83004aa
JD
2008/*
2009 * Must be called with sessions_mutex held.
2010 */
c099397a 2011static
a90917c3 2012int _lttng_field_statedump(struct lttng_session *session,
f513b2bf
MD
2013 const struct lttng_event_field *field,
2014 size_t nesting)
c099397a 2015{
c099397a 2016 int ret = 0;
c099397a 2017
8070f5c0
MD
2018 switch (field->type.atype) {
2019 case atype_integer:
f513b2bf
MD
2020 ret = print_tabs(session, nesting);
2021 if (ret)
2022 return ret;
8070f5c0 2023 ret = lttng_metadata_printf(session,
f513b2bf 2024 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
8070f5c0
MD
2025 field->type.u.basic.integer.size,
2026 field->type.u.basic.integer.alignment,
2027 field->type.u.basic.integer.signedness,
2028 (field->type.u.basic.integer.encoding == lttng_encode_none)
2029 ? "none"
2030 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
2031 ? "UTF8"
2032 : "ASCII",
2033 field->type.u.basic.integer.base,
5214fa50 2034#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2035 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
c099397a 2036#else
8070f5c0 2037 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
c099397a 2038#endif
8070f5c0
MD
2039 field->name);
2040 break;
2041 case atype_enum:
141ddf28 2042 ret = _lttng_enum_statedump(session, field, nesting);
8070f5c0
MD
2043 break;
2044 case atype_array:
2045 {
2046 const struct lttng_basic_type *elem_type;
2047
2048 elem_type = &field->type.u.array.elem_type;
43803cf2 2049 if (field->type.u.array.elem_alignment) {
f513b2bf
MD
2050 ret = print_tabs(session, nesting);
2051 if (ret)
2052 return ret;
43803cf2 2053 ret = lttng_metadata_printf(session,
f513b2bf 2054 "struct { } align(%u) _%s_padding;\n",
43803cf2
MD
2055 field->type.u.array.elem_alignment * CHAR_BIT,
2056 field->name);
2057 if (ret)
2058 return ret;
2059 }
f513b2bf
MD
2060 ret = print_tabs(session, nesting);
2061 if (ret)
2062 return ret;
8070f5c0 2063 ret = lttng_metadata_printf(session,
f513b2bf 2064 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
8070f5c0
MD
2065 elem_type->u.basic.integer.size,
2066 elem_type->u.basic.integer.alignment,
2067 elem_type->u.basic.integer.signedness,
2068 (elem_type->u.basic.integer.encoding == lttng_encode_none)
2069 ? "none"
2070 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
2071 ? "UTF8"
2072 : "ASCII",
2073 elem_type->u.basic.integer.base,
5214fa50 2074#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2075 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
8fa75f34 2076#else
8070f5c0 2077 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
8fa75f34 2078#endif
8070f5c0
MD
2079 field->name, field->type.u.array.length);
2080 break;
2081 }
2082 case atype_sequence:
2083 {
2084 const struct lttng_basic_type *elem_type;
2085 const struct lttng_basic_type *length_type;
2086
2087 elem_type = &field->type.u.sequence.elem_type;
2088 length_type = &field->type.u.sequence.length_type;
f513b2bf
MD
2089 ret = print_tabs(session, nesting);
2090 if (ret)
2091 return ret;
8070f5c0 2092 ret = lttng_metadata_printf(session,
f513b2bf 2093 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
8070f5c0 2094 length_type->u.basic.integer.size,
32257272 2095 (unsigned int) length_type->u.basic.integer.alignment,
8070f5c0
MD
2096 length_type->u.basic.integer.signedness,
2097 (length_type->u.basic.integer.encoding == lttng_encode_none)
2098 ? "none"
0635cdd4 2099 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
8070f5c0 2100 ? "UTF8"
0635cdd4 2101 : "ASCII"),
8070f5c0 2102 length_type->u.basic.integer.base,
5214fa50 2103#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2104 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
e0a7a7c4 2105#else
0635cdd4 2106 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
e0a7a7c4 2107#endif
27d2368e
MD
2108 field->name);
2109 if (ret)
2110 return ret;
2111
43803cf2 2112 if (field->type.u.sequence.elem_alignment) {
f513b2bf
MD
2113 ret = print_tabs(session, nesting);
2114 if (ret)
2115 return ret;
43803cf2 2116 ret = lttng_metadata_printf(session,
f513b2bf 2117 "struct { } align(%u) _%s_padding;\n",
43803cf2
MD
2118 field->type.u.sequence.elem_alignment * CHAR_BIT,
2119 field->name);
2120 if (ret)
2121 return ret;
2122 }
f513b2bf
MD
2123 ret = print_tabs(session, nesting);
2124 if (ret)
2125 return ret;
27d2368e 2126 ret = lttng_metadata_printf(session,
f513b2bf 2127 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
8070f5c0 2128 elem_type->u.basic.integer.size,
32257272 2129 (unsigned int) elem_type->u.basic.integer.alignment,
8070f5c0
MD
2130 elem_type->u.basic.integer.signedness,
2131 (elem_type->u.basic.integer.encoding == lttng_encode_none)
2132 ? "none"
0635cdd4 2133 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
8070f5c0 2134 ? "UTF8"
0635cdd4 2135 : "ASCII"),
8070f5c0 2136 elem_type->u.basic.integer.base,
5214fa50 2137#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2138 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
8fa75f34 2139#else
8070f5c0 2140 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
8fa75f34 2141#endif
8070f5c0 2142 field->name,
27d2368e 2143 field->name);
8070f5c0
MD
2144 break;
2145 }
c099397a 2146
8070f5c0
MD
2147 case atype_string:
2148 /* Default encoding is UTF8 */
f513b2bf
MD
2149 ret = print_tabs(session, nesting);
2150 if (ret)
2151 return ret;
8070f5c0 2152 ret = lttng_metadata_printf(session,
f513b2bf 2153 "string%s _%s;\n",
8070f5c0
MD
2154 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
2155 " { encoding = ASCII; }" : "",
2156 field->name);
2157 break;
f513b2bf
MD
2158 case atype_struct:
2159 ret = _lttng_struct_statedump(session, field, nesting);
2160 break;
2161 case atype_array_compound:
2162 ret = _lttng_array_compound_statedump(session, field, nesting);
2163 break;
2164 case atype_sequence_compound:
2165 ret = _lttng_sequence_compound_statedump(session, field, nesting);
2166 break;
65c85aa6
MD
2167 case atype_variant:
2168 ret = _lttng_variant_statedump(session, field, nesting);
2169 break;
f513b2bf 2170
8070f5c0
MD
2171 default:
2172 WARN_ON_ONCE(1);
2173 return -EINVAL;
2174 }
2175 return ret;
2176}
2177
2178static
a90917c3 2179int _lttng_context_metadata_statedump(struct lttng_session *session,
8070f5c0
MD
2180 struct lttng_ctx *ctx)
2181{
2182 int ret = 0;
2183 int i;
2184
2185 if (!ctx)
2186 return 0;
2187 for (i = 0; i < ctx->nr_fields; i++) {
2188 const struct lttng_ctx_field *field = &ctx->fields[i];
2189
f513b2bf 2190 ret = _lttng_field_statedump(session, &field->event_field, 2);
8070f5c0
MD
2191 if (ret)
2192 return ret;
2193 }
2194 return ret;
2195}
2196
2197static
a90917c3
MD
2198int _lttng_fields_metadata_statedump(struct lttng_session *session,
2199 struct lttng_event *event)
8070f5c0
MD
2200{
2201 const struct lttng_event_desc *desc = event->desc;
2202 int ret = 0;
2203 int i;
2204
2205 for (i = 0; i < desc->nr_fields; i++) {
2206 const struct lttng_event_field *field = &desc->fields[i];
2207
f513b2bf 2208 ret = _lttng_field_statedump(session, field, 2);
8070f5c0
MD
2209 if (ret)
2210 return ret;
c099397a
MD
2211 }
2212 return ret;
2213}
2214
d83004aa
JD
2215/*
2216 * Must be called with sessions_mutex held.
2217 */
c099397a 2218static
a90917c3
MD
2219int _lttng_event_metadata_statedump(struct lttng_session *session,
2220 struct lttng_channel *chan,
2221 struct lttng_event *event)
c099397a
MD
2222{
2223 int ret = 0;
2224
2225 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
2226 return 0;
d83004aa 2227 if (chan->channel_type == METADATA_CHANNEL)
c099397a
MD
2228 return 0;
2229
2230 ret = lttng_metadata_printf(session,
2231 "event {\n"
ae734547 2232 " name = \"%s\";\n"
c099397a 2233 " id = %u;\n"
b9074a1b 2234 " stream_id = %u;\n",
c099397a
MD
2235 event->desc->name,
2236 event->id,
2237 event->chan->id);
2238 if (ret)
2239 goto end;
2240
b9074a1b
MD
2241 if (event->ctx) {
2242 ret = lttng_metadata_printf(session,
2243 " context := struct {\n");
2244 if (ret)
2245 goto end;
2246 }
a90917c3 2247 ret = _lttng_context_metadata_statedump(session, event->ctx);
8070f5c0
MD
2248 if (ret)
2249 goto end;
b9074a1b
MD
2250 if (event->ctx) {
2251 ret = lttng_metadata_printf(session,
2252 " };\n");
2253 if (ret)
2254 goto end;
2255 }
8070f5c0
MD
2256
2257 ret = lttng_metadata_printf(session,
8070f5c0
MD
2258 " fields := struct {\n"
2259 );
2260 if (ret)
2261 goto end;
2262
a90917c3 2263 ret = _lttng_fields_metadata_statedump(session, event);
c099397a
MD
2264 if (ret)
2265 goto end;
2266
2267 /*
2268 * LTTng space reservation can only reserve multiples of the
2269 * byte size.
2270 */
2271 ret = lttng_metadata_printf(session,
9115fbdc
MD
2272 " };\n"
2273 "};\n\n");
c099397a
MD
2274 if (ret)
2275 goto end;
2276
c099397a
MD
2277 event->metadata_dumped = 1;
2278end:
2279 return ret;
2280
2281}
2282
d83004aa
JD
2283/*
2284 * Must be called with sessions_mutex held.
2285 */
c099397a 2286static
a90917c3
MD
2287int _lttng_channel_metadata_statedump(struct lttng_session *session,
2288 struct lttng_channel *chan)
c099397a
MD
2289{
2290 int ret = 0;
2291
2292 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
2293 return 0;
d83004aa
JD
2294
2295 if (chan->channel_type == METADATA_CHANNEL)
c099397a
MD
2296 return 0;
2297
2298 WARN_ON_ONCE(!chan->header_type);
2299 ret = lttng_metadata_printf(session,
2300 "stream {\n"
2301 " id = %u;\n"
9115fbdc 2302 " event.header := %s;\n"
b9074a1b 2303 " packet.context := struct packet_context;\n",
c099397a
MD
2304 chan->id,
2305 chan->header_type == 1 ? "struct event_header_compact" :
2306 "struct event_header_large");
2307 if (ret)
2308 goto end;
2309
b9074a1b
MD
2310 if (chan->ctx) {
2311 ret = lttng_metadata_printf(session,
2312 " event.context := struct {\n");
2313 if (ret)
2314 goto end;
2315 }
a90917c3 2316 ret = _lttng_context_metadata_statedump(session, chan->ctx);
8070f5c0
MD
2317 if (ret)
2318 goto end;
b9074a1b
MD
2319 if (chan->ctx) {
2320 ret = lttng_metadata_printf(session,
2321 " };\n");
2322 if (ret)
2323 goto end;
2324 }
8070f5c0
MD
2325
2326 ret = lttng_metadata_printf(session,
b9074a1b 2327 "};\n\n");
8070f5c0 2328
c099397a
MD
2329 chan->metadata_dumped = 1;
2330end:
2331 return ret;
2332}
2333
d83004aa
JD
2334/*
2335 * Must be called with sessions_mutex held.
2336 */
9115fbdc 2337static
a90917c3 2338int _lttng_stream_packet_context_declare(struct lttng_session *session)
9115fbdc
MD
2339{
2340 return lttng_metadata_printf(session,
2341 "struct packet_context {\n"
a3ccff4f
MD
2342 " uint64_clock_monotonic_t timestamp_begin;\n"
2343 " uint64_clock_monotonic_t timestamp_end;\n"
576ca06a
MD
2344 " uint64_t content_size;\n"
2345 " uint64_t packet_size;\n"
5b3cf4f9 2346 " uint64_t packet_seq_num;\n"
a9afe705 2347 " unsigned long events_discarded;\n"
9115fbdc 2348 " uint32_t cpu_id;\n"
c6dfdf6f 2349 "};\n\n"
9115fbdc
MD
2350 );
2351}
2352
2353/*
2354 * Compact header:
2355 * id: range: 0 - 30.
2356 * id 31 is reserved to indicate an extended header.
2357 *
2358 * Large header:
2359 * id: range: 0 - 65534.
2360 * id 65535 is reserved to indicate an extended header.
d83004aa
JD
2361 *
2362 * Must be called with sessions_mutex held.
9115fbdc
MD
2363 */
2364static
a90917c3 2365int _lttng_event_header_declare(struct lttng_session *session)
9115fbdc
MD
2366{
2367 return lttng_metadata_printf(session,
2368 "struct event_header_compact {\n"
2369 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2370 " variant <id> {\n"
2371 " struct {\n"
a3ccff4f 2372 " uint27_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2373 " } compact;\n"
2374 " struct {\n"
2375 " uint32_t id;\n"
a3ccff4f 2376 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2377 " } extended;\n"
2378 " } v;\n"
2379 "} align(%u);\n"
2380 "\n"
2381 "struct event_header_large {\n"
2382 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2383 " variant <id> {\n"
2384 " struct {\n"
a3ccff4f 2385 " uint32_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2386 " } compact;\n"
2387 " struct {\n"
2388 " uint32_t id;\n"
a3ccff4f 2389 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2390 " } extended;\n"
2391 " } v;\n"
2392 "} align(%u);\n\n",
a90917c3
MD
2393 lttng_alignof(uint32_t) * CHAR_BIT,
2394 lttng_alignof(uint16_t) * CHAR_BIT
9115fbdc
MD
2395 );
2396}
2397
a3ccff4f
MD
2398 /*
2399 * Approximation of NTP time of day to clock monotonic correlation,
2400 * taken at start of trace.
2401 * Yes, this is only an approximation. Yes, we can (and will) do better
2402 * in future versions.
5283350b
MD
2403 * This function may return a negative offset. It may happen if the
2404 * system sets the REALTIME clock to 0 after boot.
a3ccff4f
MD
2405 */
2406static
8967b2a3 2407int64_t measure_clock_offset(void)
a3ccff4f 2408{
dcb93448 2409 uint64_t monotonic_avg, monotonic[2], realtime;
2754583e 2410 uint64_t tcf = trace_clock_freq();
dcb93448 2411 int64_t offset;
a3ccff4f
MD
2412 struct timespec rts = { 0, 0 };
2413 unsigned long flags;
2414
2415 /* Disable interrupts to increase correlation precision. */
2416 local_irq_save(flags);
2417 monotonic[0] = trace_clock_read64();
216b6baa 2418 getnstimeofday(&rts);
a3ccff4f
MD
2419 monotonic[1] = trace_clock_read64();
2420 local_irq_restore(flags);
2421
dcb93448 2422 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
2754583e
MD
2423 realtime = (uint64_t) rts.tv_sec * tcf;
2424 if (tcf == NSEC_PER_SEC) {
2425 realtime += rts.tv_nsec;
2426 } else {
2427 uint64_t n = rts.tv_nsec * tcf;
2428
2429 do_div(n, NSEC_PER_SEC);
2430 realtime += n;
2431 }
dcb93448 2432 offset = (int64_t) realtime - monotonic_avg;
a3ccff4f
MD
2433 return offset;
2434}
2435
c099397a
MD
2436/*
2437 * Output metadata into this session's metadata buffers.
d83004aa 2438 * Must be called with sessions_mutex held.
c099397a
MD
2439 */
2440static
a90917c3 2441int _lttng_session_metadata_statedump(struct lttng_session *session)
c099397a 2442{
30bdb6e4 2443 unsigned char *uuid_c = session->uuid.b;
a82c63f1 2444 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
a90917c3
MD
2445 struct lttng_channel *chan;
2446 struct lttng_event *event;
c099397a
MD
2447 int ret = 0;
2448
2449 if (!ACCESS_ONCE(session->active))
2450 return 0;
2451 if (session->metadata_dumped)
2452 goto skip_session;
c099397a 2453
d793d5e1 2454 snprintf(uuid_s, sizeof(uuid_s),
30bdb6e4
MD
2455 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2456 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
2457 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
2458 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
2459 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
d793d5e1
MD
2460
2461 ret = lttng_metadata_printf(session,
9115fbdc
MD
2462 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2463 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2464 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2465 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
a9afe705 2466 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
9115fbdc
MD
2467 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2468 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
d793d5e1
MD
2469 "\n"
2470 "trace {\n"
2471 " major = %u;\n"
2472 " minor = %u;\n"
30bdb6e4 2473 " uuid = \"%s\";\n"
d793d5e1
MD
2474 " byte_order = %s;\n"
2475 " packet.header := struct {\n"
2476 " uint32_t magic;\n"
1ec3f75a 2477 " uint8_t uuid[16];\n"
d793d5e1 2478 " uint32_t stream_id;\n"
5594698f 2479 " uint64_t stream_instance_id;\n"
0eb25f58 2480 " };\n"
d793d5e1 2481 "};\n\n",
a90917c3
MD
2482 lttng_alignof(uint8_t) * CHAR_BIT,
2483 lttng_alignof(uint16_t) * CHAR_BIT,
2484 lttng_alignof(uint32_t) * CHAR_BIT,
2485 lttng_alignof(uint64_t) * CHAR_BIT,
a9afe705
MD
2486 sizeof(unsigned long) * CHAR_BIT,
2487 lttng_alignof(unsigned long) * CHAR_BIT,
c6c9e10f
MD
2488 CTF_SPEC_MAJOR,
2489 CTF_SPEC_MINOR,
d793d5e1 2490 uuid_s,
5214fa50 2491#if __BYTE_ORDER == __BIG_ENDIAN
d793d5e1
MD
2492 "be"
2493#else
2494 "le"
2495#endif
2496 );
2497 if (ret)
2498 goto end;
2499
99dc9597
MD
2500 ret = lttng_metadata_printf(session,
2501 "env {\n"
a6058143 2502 " hostname = \"%s\";\n"
c6c9e10f 2503 " domain = \"kernel\";\n"
99dc9597 2504 " sysname = \"%s\";\n"
c6c9e10f
MD
2505 " kernel_release = \"%s\";\n"
2506 " kernel_version = \"%s\";\n"
2507 " tracer_name = \"lttng-modules\";\n"
2508 " tracer_major = %d;\n"
2509 " tracer_minor = %d;\n"
2510 " tracer_patchlevel = %d;\n"
99dc9597 2511 "};\n\n",
3d0d43db 2512 current->nsproxy->uts_ns->name.nodename,
99dc9597
MD
2513 utsname()->sysname,
2514 utsname()->release,
c6c9e10f
MD
2515 utsname()->version,
2516 LTTNG_MODULES_MAJOR_VERSION,
2517 LTTNG_MODULES_MINOR_VERSION,
2518 LTTNG_MODULES_PATCHLEVEL_VERSION
99dc9597
MD
2519 );
2520 if (ret)
2521 goto end;
2522
a3ccff4f
MD
2523 ret = lttng_metadata_printf(session,
2524 "clock {\n"
2754583e
MD
2525 " name = \"%s\";\n",
2526 trace_clock_name()
a82c63f1
MD
2527 );
2528 if (ret)
2529 goto end;
2530
2531 if (!trace_clock_uuid(clock_uuid_s)) {
2532 ret = lttng_metadata_printf(session,
7c27cb17 2533 " uuid = \"%s\";\n",
a82c63f1
MD
2534 clock_uuid_s
2535 );
2536 if (ret)
2537 goto end;
2538 }
2539
2540 ret = lttng_metadata_printf(session,
2754583e 2541 " description = \"%s\";\n"
a3ccff4f
MD
2542 " freq = %llu; /* Frequency, in Hz */\n"
2543 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
8967b2a3 2544 " offset = %lld;\n"
a3ccff4f 2545 "};\n\n",
2754583e 2546 trace_clock_description(),
a3ccff4f 2547 (unsigned long long) trace_clock_freq(),
8967b2a3 2548 (long long) measure_clock_offset()
a3ccff4f
MD
2549 );
2550 if (ret)
2551 goto end;
2552
2553 ret = lttng_metadata_printf(session,
2554 "typealias integer {\n"
2555 " size = 27; align = 1; signed = false;\n"
2754583e 2556 " map = clock.%s.value;\n"
a3ccff4f
MD
2557 "} := uint27_clock_monotonic_t;\n"
2558 "\n"
2559 "typealias integer {\n"
2560 " size = 32; align = %u; signed = false;\n"
2754583e 2561 " map = clock.%s.value;\n"
a3ccff4f
MD
2562 "} := uint32_clock_monotonic_t;\n"
2563 "\n"
2564 "typealias integer {\n"
2565 " size = 64; align = %u; signed = false;\n"
2754583e 2566 " map = clock.%s.value;\n"
a3ccff4f 2567 "} := uint64_clock_monotonic_t;\n\n",
2754583e 2568 trace_clock_name(),
a3ccff4f 2569 lttng_alignof(uint32_t) * CHAR_BIT,
2754583e
MD
2570 trace_clock_name(),
2571 lttng_alignof(uint64_t) * CHAR_BIT,
2572 trace_clock_name()
a3ccff4f
MD
2573 );
2574 if (ret)
2575 goto end;
2576
a90917c3 2577 ret = _lttng_stream_packet_context_declare(session);
9115fbdc
MD
2578 if (ret)
2579 goto end;
2580
a90917c3 2581 ret = _lttng_event_header_declare(session);
9115fbdc
MD
2582 if (ret)
2583 goto end;
2584
c099397a
MD
2585skip_session:
2586 list_for_each_entry(chan, &session->chan, list) {
a90917c3 2587 ret = _lttng_channel_metadata_statedump(session, chan);
c099397a
MD
2588 if (ret)
2589 goto end;
2590 }
2591
2592 list_for_each_entry(event, &session->events, list) {
a90917c3 2593 ret = _lttng_event_metadata_statedump(session, event->chan, event);
c099397a
MD
2594 if (ret)
2595 goto end;
2596 }
2597 session->metadata_dumped = 1;
2598end:
2599 return ret;
2600}
2601
c0e31d2e 2602/**
a90917c3 2603 * lttng_transport_register - LTT transport registration
c0e31d2e
MD
2604 * @transport: transport structure
2605 *
2606 * Registers a transport which can be used as output to extract the data out of
2607 * LTTng. The module calling this registration function must ensure that no
2608 * trap-inducing code will be executed by the transport functions. E.g.
2609 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2610 * is made visible to the transport function. This registration acts as a
2611 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2612 * after its registration must it synchronize the TLBs.
2613 */
a90917c3 2614void lttng_transport_register(struct lttng_transport *transport)
c0e31d2e
MD
2615{
2616 /*
2617 * Make sure no page fault can be triggered by the module about to be
2618 * registered. We deal with this here so we don't have to call
2619 * vmalloc_sync_all() in each module's init.
2620 */
6d2a620c 2621 wrapper_vmalloc_sync_all();
c0e31d2e
MD
2622
2623 mutex_lock(&sessions_mutex);
a90917c3 2624 list_add_tail(&transport->node, &lttng_transport_list);
c0e31d2e
MD
2625 mutex_unlock(&sessions_mutex);
2626}
a90917c3 2627EXPORT_SYMBOL_GPL(lttng_transport_register);
c0e31d2e
MD
2628
2629/**
a90917c3 2630 * lttng_transport_unregister - LTT transport unregistration
c0e31d2e
MD
2631 * @transport: transport structure
2632 */
a90917c3 2633void lttng_transport_unregister(struct lttng_transport *transport)
c0e31d2e
MD
2634{
2635 mutex_lock(&sessions_mutex);
2636 list_del(&transport->node);
2637 mutex_unlock(&sessions_mutex);
2638}
a90917c3 2639EXPORT_SYMBOL_GPL(lttng_transport_unregister);
c0e31d2e 2640
a90917c3 2641static int __init lttng_events_init(void)
4e3c1b9b 2642{
1c25284c
MD
2643 int ret;
2644
453b2495
JD
2645 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
2646 if (ret)
2647 return ret;
389d7070 2648 ret = wrapper_get_pfnblock_flags_mask_init();
36561775
MD
2649 if (ret)
2650 return ret;
2651 ret = wrapper_get_pageblock_flags_mask_init();
114667d5
MD
2652 if (ret)
2653 return ret;
2654 ret = lttng_probes_init();
389d7070
MD
2655 if (ret)
2656 return ret;
07dfc1d0 2657 ret = lttng_context_init();
20591cf7
MD
2658 if (ret)
2659 return ret;
07dfc1d0
MD
2660 ret = lttng_tracepoint_init();
2661 if (ret)
2662 goto error_tp;
a90917c3 2663 event_cache = KMEM_CACHE(lttng_event, 0);
20591cf7
MD
2664 if (!event_cache) {
2665 ret = -ENOMEM;
2666 goto error_kmem;
2667 }
80996790 2668 ret = lttng_abi_init();
02119ee5
MD
2669 if (ret)
2670 goto error_abi;
0c956676
MD
2671 ret = lttng_logger_init();
2672 if (ret)
2673 goto error_logger;
4e3c1b9b 2674 return 0;
0c956676
MD
2675
2676error_logger:
2677 lttng_abi_exit();
02119ee5 2678error_abi:
1c25284c 2679 kmem_cache_destroy(event_cache);
20591cf7
MD
2680error_kmem:
2681 lttng_tracepoint_exit();
07dfc1d0
MD
2682error_tp:
2683 lttng_context_exit();
1c25284c 2684 return ret;
4e3c1b9b
MD
2685}
2686
a90917c3 2687module_init(lttng_events_init);
11b5a3c2 2688
a90917c3 2689static void __exit lttng_events_exit(void)
4e3c1b9b 2690{
a90917c3 2691 struct lttng_session *session, *tmpsession;
92e94819 2692
0c956676 2693 lttng_logger_exit();
80996790 2694 lttng_abi_exit();
92e94819 2695 list_for_each_entry_safe(session, tmpsession, &sessions, list)
a90917c3 2696 lttng_session_destroy(session);
11b5a3c2 2697 kmem_cache_destroy(event_cache);
20591cf7 2698 lttng_tracepoint_exit();
07dfc1d0 2699 lttng_context_exit();
4e3c1b9b 2700}
92e94819 2701
a90917c3 2702module_exit(lttng_events_exit);
11b5a3c2 2703
92e94819
MD
2704MODULE_LICENSE("GPL and additional rights");
2705MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2706MODULE_DESCRIPTION("LTTng Events");
9a9973ef
MD
2707MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
2708 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
309370e2
MD
2709 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
2710 LTTNG_MODULES_EXTRAVERSION);
This page took 0.178013 seconds and 5 git commands to generate.