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