Add support for star globbing patterns in event names
[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>
4993071a 54#include <lttng-string-utils.h>
241ae9a8 55#include <wrapper/vzalloc.h>
64af2437
MD
56#include <wrapper/ringbuffer/backend.h>
57#include <wrapper/ringbuffer/frontend.h>
4e3c1b9b 58
d83004aa
JD
59#define METADATA_CACHE_DEFAULT_SIZE 4096
60
4e3c1b9b 61static LIST_HEAD(sessions);
a90917c3 62static LIST_HEAD(lttng_transport_list);
d83004aa
JD
63/*
64 * Protect the sessions and metadata caches.
65 */
4e3c1b9b
MD
66static DEFINE_MUTEX(sessions_mutex);
67static struct kmem_cache *event_cache;
68
3c997079
MD
69static void lttng_session_lazy_sync_enablers(struct lttng_session *session);
70static void lttng_session_sync_enablers(struct lttng_session *session);
71static void lttng_enabler_destroy(struct lttng_enabler *enabler);
72
a90917c3
MD
73static void _lttng_event_destroy(struct lttng_event *event);
74static void _lttng_channel_destroy(struct lttng_channel *chan);
75static int _lttng_event_unregister(struct lttng_event *event);
c099397a 76static
a90917c3
MD
77int _lttng_event_metadata_statedump(struct lttng_session *session,
78 struct lttng_channel *chan,
79 struct lttng_event *event);
c099397a 80static
a90917c3 81int _lttng_session_metadata_statedump(struct lttng_session *session);
d83004aa
JD
82static
83void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
f513b2bf
MD
84static
85int _lttng_field_statedump(struct lttng_session *session,
86 const struct lttng_event_field *field,
87 size_t nesting);
c099397a 88
c099397a 89void synchronize_trace(void)
abcca994
MD
90{
91 synchronize_sched();
b8590f40
MD
92#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
93#ifdef CONFIG_PREEMPT_RT_FULL
94 synchronize_rcu();
95#endif
96#else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
abcca994
MD
97#ifdef CONFIG_PREEMPT_RT
98 synchronize_rcu();
99#endif
b8590f40 100#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
abcca994
MD
101}
102
3c997079
MD
103void lttng_lock_sessions(void)
104{
105 mutex_lock(&sessions_mutex);
106}
107
108void lttng_unlock_sessions(void)
109{
110 mutex_unlock(&sessions_mutex);
111}
112
113/*
114 * Called with sessions lock held.
115 */
116int lttng_session_active(void)
117{
118 struct lttng_session *iter;
119
120 list_for_each_entry(iter, &sessions, list) {
121 if (iter->active)
122 return 1;
123 }
124 return 0;
125}
126
a90917c3 127struct lttng_session *lttng_session_create(void)
4e3c1b9b 128{
a90917c3 129 struct lttng_session *session;
d83004aa 130 struct lttng_metadata_cache *metadata_cache;
3c997079 131 int i;
4e3c1b9b
MD
132
133 mutex_lock(&sessions_mutex);
a90917c3 134 session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL);
4e3c1b9b 135 if (!session)
d83004aa 136 goto err;
4e3c1b9b 137 INIT_LIST_HEAD(&session->chan);
f3d01b96 138 INIT_LIST_HEAD(&session->events);
d793d5e1 139 uuid_le_gen(&session->uuid);
d83004aa
JD
140
141 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
142 GFP_KERNEL);
143 if (!metadata_cache)
144 goto err_free_session;
f6da044f 145 metadata_cache->data = lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE);
d83004aa
JD
146 if (!metadata_cache->data)
147 goto err_free_cache;
148 metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
149 kref_init(&metadata_cache->refcount);
92d9f5e6 150 mutex_init(&metadata_cache->lock);
d83004aa
JD
151 session->metadata_cache = metadata_cache;
152 INIT_LIST_HEAD(&metadata_cache->metadata_stream);
a36580d5
MD
153 memcpy(&metadata_cache->uuid, &session->uuid,
154 sizeof(metadata_cache->uuid));
3c997079
MD
155 INIT_LIST_HEAD(&session->enablers_head);
156 for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++)
157 INIT_HLIST_HEAD(&session->events_ht.table[i]);
4e3c1b9b
MD
158 list_add(&session->list, &sessions);
159 mutex_unlock(&sessions_mutex);
160 return session;
d83004aa
JD
161
162err_free_cache:
163 kfree(metadata_cache);
164err_free_session:
165 kfree(session);
166err:
167 mutex_unlock(&sessions_mutex);
168 return NULL;
169}
170
171void metadata_cache_destroy(struct kref *kref)
172{
173 struct lttng_metadata_cache *cache =
174 container_of(kref, struct lttng_metadata_cache, refcount);
a606b6e8 175 vfree(cache->data);
d83004aa 176 kfree(cache);
4e3c1b9b
MD
177}
178
a90917c3 179void lttng_session_destroy(struct lttng_session *session)
4e3c1b9b 180{
a90917c3
MD
181 struct lttng_channel *chan, *tmpchan;
182 struct lttng_event *event, *tmpevent;
d83004aa 183 struct lttng_metadata_stream *metadata_stream;
3c997079 184 struct lttng_enabler *enabler, *tmpenabler;
dda6a249 185 int ret;
4e3c1b9b
MD
186
187 mutex_lock(&sessions_mutex);
52fc2e1f 188 ACCESS_ONCE(session->active) = 0;
1ec65de1
MD
189 list_for_each_entry(chan, &session->chan, list) {
190 ret = lttng_syscalls_unregister(chan);
191 WARN_ON(ret);
192 }
dda6a249 193 list_for_each_entry(event, &session->events, list) {
a90917c3 194 ret = _lttng_event_unregister(event);
dda6a249
MD
195 WARN_ON(ret);
196 }
abcca994 197 synchronize_trace(); /* Wait for in-flight events to complete */
3c997079
MD
198 list_for_each_entry_safe(enabler, tmpenabler,
199 &session->enablers_head, node)
200 lttng_enabler_destroy(enabler);
4e3c1b9b 201 list_for_each_entry_safe(event, tmpevent, &session->events, list)
a90917c3 202 _lttng_event_destroy(event);
d83004aa
JD
203 list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
204 BUG_ON(chan->channel_type == METADATA_CHANNEL);
a90917c3 205 _lttng_channel_destroy(chan);
d83004aa
JD
206 }
207 list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
208 _lttng_metadata_channel_hangup(metadata_stream);
e0130fab
MD
209 if (session->pid_tracker)
210 lttng_pid_tracker_destroy(session->pid_tracker);
d83004aa 211 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
4e3c1b9b
MD
212 list_del(&session->list);
213 mutex_unlock(&sessions_mutex);
214 kfree(session);
215}
216
601252cf
MD
217int lttng_session_statedump(struct lttng_session *session)
218{
219 int ret;
220
221 mutex_lock(&sessions_mutex);
222 ret = lttng_statedump_start(session);
223 mutex_unlock(&sessions_mutex);
224 return ret;
225}
226
a90917c3 227int lttng_session_enable(struct lttng_session *session)
c0e31d2e
MD
228{
229 int ret = 0;
a90917c3 230 struct lttng_channel *chan;
c0e31d2e
MD
231
232 mutex_lock(&sessions_mutex);
233 if (session->active) {
234 ret = -EBUSY;
235 goto end;
236 }
c099397a 237
3c997079
MD
238 /* Set transient enabler state to "enabled" */
239 session->tstate = 1;
240
c099397a
MD
241 /*
242 * Snapshot the number of events per channel to know the type of header
243 * we need to use.
244 */
245 list_for_each_entry(chan, &session->chan, list) {
246 if (chan->header_type)
247 continue; /* don't change it if session stop/restart */
248 if (chan->free_event_id < 31)
249 chan->header_type = 1; /* compact */
250 else
251 chan->header_type = 2; /* large */
252 }
253
3c997079
MD
254 /* We need to sync enablers with session before activation. */
255 lttng_session_sync_enablers(session);
256
64af2437 257 /* Clear each stream's quiescent state. */
09b93db9
MD
258 list_for_each_entry(chan, &session->chan, list) {
259 if (chan->channel_type != METADATA_CHANNEL)
260 lib_ring_buffer_clear_quiescent_channel(chan->chan);
261 }
64af2437 262
52fc2e1f 263 ACCESS_ONCE(session->active) = 1;
8070f5c0 264 ACCESS_ONCE(session->been_active) = 1;
a90917c3 265 ret = _lttng_session_metadata_statedump(session);
c337ddc2
MD
266 if (ret) {
267 ACCESS_ONCE(session->active) = 0;
268 goto end;
269 }
270 ret = lttng_statedump_start(session);
360f38ea 271 if (ret)
c099397a 272 ACCESS_ONCE(session->active) = 0;
c0e31d2e
MD
273end:
274 mutex_unlock(&sessions_mutex);
11b5a3c2 275 return ret;
c0e31d2e
MD
276}
277
a90917c3 278int lttng_session_disable(struct lttng_session *session)
c0e31d2e
MD
279{
280 int ret = 0;
64af2437 281 struct lttng_channel *chan;
c0e31d2e
MD
282
283 mutex_lock(&sessions_mutex);
284 if (!session->active) {
285 ret = -EBUSY;
286 goto end;
287 }
52fc2e1f 288 ACCESS_ONCE(session->active) = 0;
3c997079
MD
289
290 /* Set transient enabler state to "disabled" */
291 session->tstate = 0;
292 lttng_session_sync_enablers(session);
64af2437
MD
293
294 /* Set each stream's quiescent state. */
09b93db9
MD
295 list_for_each_entry(chan, &session->chan, list) {
296 if (chan->channel_type != METADATA_CHANNEL)
297 lib_ring_buffer_set_quiescent_channel(chan->chan);
298 }
c0e31d2e
MD
299end:
300 mutex_unlock(&sessions_mutex);
11b5a3c2 301 return ret;
c0e31d2e
MD
302}
303
9616f0bf
JD
304int lttng_session_metadata_regenerate(struct lttng_session *session)
305{
306 int ret = 0;
307 struct lttng_channel *chan;
308 struct lttng_event *event;
309 struct lttng_metadata_cache *cache = session->metadata_cache;
310 struct lttng_metadata_stream *stream;
311
312 mutex_lock(&sessions_mutex);
313 if (!session->active) {
314 ret = -EBUSY;
315 goto end;
316 }
317
318 mutex_lock(&cache->lock);
319 memset(cache->data, 0, cache->cache_alloc);
320 cache->metadata_written = 0;
321 cache->version++;
322 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) {
323 stream->metadata_out = 0;
324 stream->metadata_in = 0;
325 }
326 mutex_unlock(&cache->lock);
327
328 session->metadata_dumped = 0;
329 list_for_each_entry(chan, &session->chan, list) {
330 chan->metadata_dumped = 0;
331 }
332
333 list_for_each_entry(event, &session->events, list) {
334 event->metadata_dumped = 0;
335 }
336
337 ret = _lttng_session_metadata_statedump(session);
338
339end:
340 mutex_unlock(&sessions_mutex);
341 return ret;
342}
343
344
345
a90917c3 346int lttng_channel_enable(struct lttng_channel *channel)
e64957da 347{
3c997079 348 int ret = 0;
e64957da 349
3c997079
MD
350 mutex_lock(&sessions_mutex);
351 if (channel->channel_type == METADATA_CHANNEL) {
352 ret = -EPERM;
353 goto end;
354 }
355 if (channel->enabled) {
356 ret = -EEXIST;
357 goto end;
358 }
359 /* Set transient enabler state to "enabled" */
360 channel->tstate = 1;
361 lttng_session_sync_enablers(channel->session);
362 /* Set atomically the state to "enabled" */
363 ACCESS_ONCE(channel->enabled) = 1;
364end:
365 mutex_unlock(&sessions_mutex);
366 return ret;
e64957da
MD
367}
368
a90917c3 369int lttng_channel_disable(struct lttng_channel *channel)
e64957da 370{
3c997079 371 int ret = 0;
e64957da 372
3c997079
MD
373 mutex_lock(&sessions_mutex);
374 if (channel->channel_type == METADATA_CHANNEL) {
375 ret = -EPERM;
376 goto end;
377 }
378 if (!channel->enabled) {
379 ret = -EEXIST;
380 goto end;
381 }
382 /* Set atomically the state to "disabled" */
383 ACCESS_ONCE(channel->enabled) = 0;
384 /* Set transient enabler state to "enabled" */
385 channel->tstate = 0;
386 lttng_session_sync_enablers(channel->session);
387end:
388 mutex_unlock(&sessions_mutex);
389 return ret;
e64957da
MD
390}
391
a90917c3 392int lttng_event_enable(struct lttng_event *event)
e64957da 393{
3c997079 394 int ret = 0;
e64957da 395
3c997079
MD
396 mutex_lock(&sessions_mutex);
397 if (event->chan->channel_type == METADATA_CHANNEL) {
398 ret = -EPERM;
399 goto end;
400 }
401 if (event->enabled) {
402 ret = -EEXIST;
403 goto end;
404 }
a0493bef
MD
405 switch (event->instrumentation) {
406 case LTTNG_KERNEL_TRACEPOINT:
407 case LTTNG_KERNEL_SYSCALL:
408 ret = -EINVAL;
409 break;
410 case LTTNG_KERNEL_KPROBE:
411 case LTTNG_KERNEL_FUNCTION:
412 case LTTNG_KERNEL_NOOP:
413 ACCESS_ONCE(event->enabled) = 1;
414 break;
415 case LTTNG_KERNEL_KRETPROBE:
416 ret = lttng_kretprobes_event_enable_state(event, 1);
417 break;
418 default:
419 WARN_ON_ONCE(1);
420 ret = -EINVAL;
421 }
3c997079
MD
422end:
423 mutex_unlock(&sessions_mutex);
424 return ret;
e64957da
MD
425}
426
a90917c3 427int lttng_event_disable(struct lttng_event *event)
e64957da 428{
3c997079 429 int ret = 0;
e64957da 430
3c997079
MD
431 mutex_lock(&sessions_mutex);
432 if (event->chan->channel_type == METADATA_CHANNEL) {
433 ret = -EPERM;
434 goto end;
435 }
436 if (!event->enabled) {
437 ret = -EEXIST;
438 goto end;
439 }
a0493bef
MD
440 switch (event->instrumentation) {
441 case LTTNG_KERNEL_TRACEPOINT:
442 case LTTNG_KERNEL_SYSCALL:
443 ret = -EINVAL;
444 break;
445 case LTTNG_KERNEL_KPROBE:
446 case LTTNG_KERNEL_FUNCTION:
447 case LTTNG_KERNEL_NOOP:
448 ACCESS_ONCE(event->enabled) = 0;
449 break;
450 case LTTNG_KERNEL_KRETPROBE:
451 ret = lttng_kretprobes_event_enable_state(event, 0);
452 break;
453 default:
454 WARN_ON_ONCE(1);
455 ret = -EINVAL;
456 }
3c997079
MD
457end:
458 mutex_unlock(&sessions_mutex);
459 return ret;
e64957da
MD
460}
461
a90917c3 462static struct lttng_transport *lttng_transport_find(const char *name)
f3d01b96 463{
a90917c3 464 struct lttng_transport *transport;
f3d01b96 465
a90917c3 466 list_for_each_entry(transport, &lttng_transport_list, node) {
f3d01b96
MD
467 if (!strcmp(transport->name, name))
468 return transport;
469 }
470 return NULL;
471}
472
a90917c3 473struct lttng_channel *lttng_channel_create(struct lttng_session *session,
5dbbdb43
MD
474 const char *transport_name,
475 void *buf_addr,
4e3c1b9b
MD
476 size_t subbuf_size, size_t num_subbuf,
477 unsigned int switch_timer_interval,
d83004aa
JD
478 unsigned int read_timer_interval,
479 enum channel_type channel_type)
4e3c1b9b 480{
a90917c3
MD
481 struct lttng_channel *chan;
482 struct lttng_transport *transport = NULL;
4e3c1b9b
MD
483
484 mutex_lock(&sessions_mutex);
d83004aa 485 if (session->been_active && channel_type != METADATA_CHANNEL)
e5382b6d 486 goto active; /* Refuse to add channel to active session */
a90917c3 487 transport = lttng_transport_find(transport_name);
f3d01b96
MD
488 if (!transport) {
489 printk(KERN_WARNING "LTTng transport %s not found\n",
490 transport_name);
c0e31d2e 491 goto notransport;
f3d01b96 492 }
a33c9927
MD
493 if (!try_module_get(transport->owner)) {
494 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
495 goto notransport;
496 }
a90917c3 497 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
4e3c1b9b 498 if (!chan)
c0e31d2e 499 goto nomem;
4e3c1b9b 500 chan->session = session;
05d32c64 501 chan->id = session->free_chan_id++;
3b731ab1 502 chan->ops = &transport->ops;
125b4df4
MD
503 /*
504 * Note: the channel creation op already writes into the packet
505 * headers. Therefore the "chan" information used as input
506 * should be already accessible.
507 */
5f5ddf01
MD
508 chan->chan = transport->ops.channel_create(transport_name,
509 chan, buf_addr, subbuf_size, num_subbuf,
510 switch_timer_interval, read_timer_interval);
f3d01b96
MD
511 if (!chan->chan)
512 goto create_error;
3c997079 513 chan->tstate = 1;
e64957da 514 chan->enabled = 1;
a33c9927 515 chan->transport = transport;
d83004aa 516 chan->channel_type = channel_type;
4e3c1b9b
MD
517 list_add(&chan->list, &session->chan);
518 mutex_unlock(&sessions_mutex);
519 return chan;
520
f3d01b96
MD
521create_error:
522 kfree(chan);
c0e31d2e 523nomem:
a33c9927
MD
524 if (transport)
525 module_put(transport->owner);
c0e31d2e 526notransport:
e5382b6d 527active:
4e3c1b9b
MD
528 mutex_unlock(&sessions_mutex);
529 return NULL;
530}
531
532/*
d83004aa
JD
533 * Only used internally at session destruction for per-cpu channels, and
534 * when metadata channel is released.
535 * Needs to be called with sessions mutex held.
4e3c1b9b 536 */
aa7c23a9 537static
a90917c3 538void _lttng_channel_destroy(struct lttng_channel *chan)
4e3c1b9b 539{
11b5a3c2 540 chan->ops->channel_destroy(chan->chan);
a33c9927 541 module_put(chan->transport->owner);
4e3c1b9b 542 list_del(&chan->list);
8070f5c0 543 lttng_destroy_context(chan->ctx);
4e3c1b9b
MD
544 kfree(chan);
545}
546
d83004aa
JD
547void lttng_metadata_channel_destroy(struct lttng_channel *chan)
548{
549 BUG_ON(chan->channel_type != METADATA_CHANNEL);
550
551 /* Protect the metadata cache with the sessions_mutex. */
552 mutex_lock(&sessions_mutex);
553 _lttng_channel_destroy(chan);
554 mutex_unlock(&sessions_mutex);
555}
556EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
557
558static
559void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
560{
561 stream->finalized = 1;
562 wake_up_interruptible(&stream->read_wait);
563}
564
e5382b6d
MD
565/*
566 * Supports event creation while tracing session is active.
3c997079 567 * Needs to be called with sessions mutex held.
e5382b6d 568 */
3c997079
MD
569struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
570 struct lttng_kernel_event *event_param,
571 void *filter,
572 const struct lttng_event_desc *event_desc,
573 enum lttng_kernel_instrumentation itype)
4e3c1b9b 574{
3c997079 575 struct lttng_session *session = chan->session;
a90917c3 576 struct lttng_event *event;
3c997079
MD
577 const char *event_name;
578 struct hlist_head *head;
579 size_t name_len;
580 uint32_t hash;
3d084699 581 int ret;
4e3c1b9b 582
abc0446a 583 if (chan->free_event_id == -1U) {
4cf0bf51 584 ret = -EMFILE;
e5382b6d 585 goto full;
abc0446a 586 }
3c997079
MD
587
588 switch (itype) {
589 case LTTNG_KERNEL_TRACEPOINT:
590 event_name = event_desc->name;
591 break;
592 case LTTNG_KERNEL_KPROBE:
593 case LTTNG_KERNEL_KRETPROBE:
594 case LTTNG_KERNEL_FUNCTION:
595 case LTTNG_KERNEL_NOOP:
33a39a3c 596 case LTTNG_KERNEL_SYSCALL:
3c997079
MD
597 event_name = event_param->name;
598 break;
599 default:
600 WARN_ON_ONCE(1);
601 ret = -EINVAL;
602 goto type_error;
603 }
604 name_len = strlen(event_name);
605 hash = jhash(event_name, name_len, 0);
606 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
d216ecae 607 lttng_hlist_for_each_entry(event, head, hlist) {
3c997079
MD
608 WARN_ON_ONCE(!event->desc);
609 if (!strncmp(event->desc->name, event_name,
610 LTTNG_KERNEL_SYM_NAME_LEN - 1)
611 && chan == event->chan) {
612 ret = -EEXIST;
613 goto exist;
abc0446a
MD
614 }
615 }
3c997079 616
11b5a3c2 617 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
abc0446a 618 if (!event) {
4cf0bf51 619 ret = -ENOMEM;
4e3c1b9b 620 goto cache_error;
abc0446a 621 }
4e3c1b9b
MD
622 event->chan = chan;
623 event->filter = filter;
e5382b6d 624 event->id = chan->free_event_id++;
3c997079
MD
625 event->instrumentation = itype;
626 event->evtype = LTTNG_TYPE_EVENT;
07dfc1d0 627 INIT_LIST_HEAD(&event->bytecode_runtime_head);
3c997079
MD
628 INIT_LIST_HEAD(&event->enablers_ref_head);
629
630 switch (itype) {
ab2277d6 631 case LTTNG_KERNEL_TRACEPOINT:
3c997079
MD
632 /* Event will be enabled by enabler sync. */
633 event->enabled = 0;
634 event->registered = 0;
635 event->desc = lttng_event_get(event_name);
abc0446a 636 if (!event->desc) {
4cf0bf51 637 ret = -ENOENT;
d3dbe23c 638 goto register_error;
abc0446a 639 }
3c997079
MD
640 /* Populate lttng_event structure before event registration. */
641 smp_wmb();
baf20995 642 break;
ab2277d6 643 case LTTNG_KERNEL_KPROBE:
a0493bef
MD
644 /*
645 * Needs to be explicitly enabled after creation, since
646 * we may want to apply filters.
647 */
648 event->enabled = 0;
3c997079
MD
649 event->registered = 1;
650 /*
651 * Populate lttng_event structure before event
652 * registration.
653 */
654 smp_wmb();
655 ret = lttng_kprobes_register(event_name,
f17701fb
MD
656 event_param->u.kprobe.symbol_name,
657 event_param->u.kprobe.offset,
658 event_param->u.kprobe.addr,
659 event);
abc0446a 660 if (ret) {
4cf0bf51 661 ret = -EINVAL;
d6d808f3 662 goto register_error;
abc0446a 663 }
edeb3137
MD
664 ret = try_module_get(event->desc->owner);
665 WARN_ON_ONCE(!ret);
d6d808f3 666 break;
7371f44c
MD
667 case LTTNG_KERNEL_KRETPROBE:
668 {
a90917c3 669 struct lttng_event *event_return;
7371f44c
MD
670
671 /* kretprobe defines 2 events */
a0493bef
MD
672 /*
673 * Needs to be explicitly enabled after creation, since
674 * we may want to apply filters.
675 */
676 event->enabled = 0;
3c997079 677 event->registered = 1;
7371f44c
MD
678 event_return =
679 kmem_cache_zalloc(event_cache, GFP_KERNEL);
abc0446a 680 if (!event_return) {
4cf0bf51 681 ret = -ENOMEM;
7371f44c 682 goto register_error;
abc0446a 683 }
7371f44c
MD
684 event_return->chan = chan;
685 event_return->filter = filter;
686 event_return->id = chan->free_event_id++;
a0493bef 687 event_return->enabled = 0;
3c997079
MD
688 event_return->registered = 1;
689 event_return->instrumentation = itype;
7371f44c 690 /*
a90917c3 691 * Populate lttng_event structure before kretprobe registration.
7371f44c
MD
692 */
693 smp_wmb();
3c997079 694 ret = lttng_kretprobes_register(event_name,
7371f44c
MD
695 event_param->u.kretprobe.symbol_name,
696 event_param->u.kretprobe.offset,
697 event_param->u.kretprobe.addr,
698 event, event_return);
699 if (ret) {
700 kmem_cache_free(event_cache, event_return);
4cf0bf51 701 ret = -EINVAL;
7371f44c
MD
702 goto register_error;
703 }
704 /* Take 2 refs on the module: one per event. */
705 ret = try_module_get(event->desc->owner);
706 WARN_ON_ONCE(!ret);
707 ret = try_module_get(event->desc->owner);
708 WARN_ON_ONCE(!ret);
a90917c3 709 ret = _lttng_event_metadata_statedump(chan->session, chan,
7371f44c 710 event_return);
abc0446a 711 WARN_ON_ONCE(ret > 0);
7371f44c
MD
712 if (ret) {
713 kmem_cache_free(event_cache, event_return);
714 module_put(event->desc->owner);
715 module_put(event->desc->owner);
716 goto statedump_error;
717 }
718 list_add(&event_return->list, &chan->session->events);
719 break;
720 }
ab2277d6 721 case LTTNG_KERNEL_FUNCTION:
a0493bef
MD
722 /*
723 * Needs to be explicitly enabled after creation, since
724 * we may want to apply filters.
725 */
726 event->enabled = 0;
3c997079
MD
727 event->registered = 1;
728 /*
729 * Populate lttng_event structure before event
730 * registration.
731 */
732 smp_wmb();
733 ret = lttng_ftrace_register(event_name,
e0a7a7c4
MD
734 event_param->u.ftrace.symbol_name,
735 event);
abc0446a 736 if (ret) {
e0a7a7c4 737 goto register_error;
abc0446a 738 }
edeb3137
MD
739 ret = try_module_get(event->desc->owner);
740 WARN_ON_ONCE(!ret);
e0a7a7c4 741 break;
1ec65de1 742 case LTTNG_KERNEL_NOOP:
33a39a3c 743 case LTTNG_KERNEL_SYSCALL:
a0493bef
MD
744 /*
745 * Needs to be explicitly enabled after creation, since
746 * we may want to apply filters.
747 */
748 event->enabled = 0;
3c997079
MD
749 event->registered = 0;
750 event->desc = event_desc;
abc0446a 751 if (!event->desc) {
4cf0bf51 752 ret = -EINVAL;
259b6cb3 753 goto register_error;
abc0446a 754 }
1ec65de1 755 break;
baf20995
MD
756 default:
757 WARN_ON_ONCE(1);
4cf0bf51 758 ret = -EINVAL;
df07930b 759 goto register_error;
baf20995 760 }
a90917c3 761 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
abc0446a
MD
762 WARN_ON_ONCE(ret > 0);
763 if (ret) {
c099397a 764 goto statedump_error;
abc0446a 765 }
3c997079 766 hlist_add_head(&event->hlist, head);
dda6a249 767 list_add(&event->list, &chan->session->events);
4e3c1b9b
MD
768 return event;
769
c099397a 770statedump_error:
259b6cb3 771 /* If a statedump error occurs, events will not be readable. */
11b5a3c2 772register_error:
11b5a3c2 773 kmem_cache_free(event_cache, event);
4e3c1b9b
MD
774cache_error:
775exist:
3c997079 776type_error:
e5382b6d 777full:
4cf0bf51 778 return ERR_PTR(ret);
4e3c1b9b
MD
779}
780
3c997079
MD
781struct lttng_event *lttng_event_create(struct lttng_channel *chan,
782 struct lttng_kernel_event *event_param,
783 void *filter,
784 const struct lttng_event_desc *event_desc,
785 enum lttng_kernel_instrumentation itype)
786{
787 struct lttng_event *event;
788
789 mutex_lock(&sessions_mutex);
790 event = _lttng_event_create(chan, event_param, filter, event_desc,
791 itype);
792 mutex_unlock(&sessions_mutex);
793 return event;
794}
795
796/* Only used for tracepoints for now. */
797static
798void register_event(struct lttng_event *event)
799{
800 const struct lttng_event_desc *desc;
f127e61e 801 int ret = -EINVAL;
3c997079 802
3c997079
MD
803 if (event->registered)
804 return;
4ecb5ad5 805
3c997079 806 desc = event->desc;
4ecb5ad5
MD
807 switch (event->instrumentation) {
808 case LTTNG_KERNEL_TRACEPOINT:
809 ret = lttng_wrapper_tracepoint_probe_register(desc->kname,
810 desc->probe_callback,
811 event);
812 break;
813 case LTTNG_KERNEL_SYSCALL:
814 ret = lttng_syscall_filter_enable(event->chan,
815 desc->name);
816 break;
817 case LTTNG_KERNEL_KPROBE:
818 case LTTNG_KERNEL_KRETPROBE:
819 case LTTNG_KERNEL_FUNCTION:
820 case LTTNG_KERNEL_NOOP:
821 ret = 0;
822 break;
823 default:
824 WARN_ON_ONCE(1);
825 }
3c997079
MD
826 if (!ret)
827 event->registered = 1;
828}
829
4e3c1b9b
MD
830/*
831 * Only used internally at session destruction.
832 */
a90917c3 833int _lttng_event_unregister(struct lttng_event *event)
4e3c1b9b 834{
4ecb5ad5 835 const struct lttng_event_desc *desc;
11b5a3c2
MD
836 int ret = -EINVAL;
837
3c997079
MD
838 if (!event->registered)
839 return 0;
840
4ecb5ad5 841 desc = event->desc;
38d024ae 842 switch (event->instrumentation) {
ab2277d6 843 case LTTNG_KERNEL_TRACEPOINT:
20591cf7 844 ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
85a9ca7f 845 event->desc->probe_callback,
11b5a3c2 846 event);
baf20995 847 break;
ab2277d6 848 case LTTNG_KERNEL_KPROBE:
f17701fb 849 lttng_kprobes_unregister(event);
d6d808f3
MD
850 ret = 0;
851 break;
7371f44c
MD
852 case LTTNG_KERNEL_KRETPROBE:
853 lttng_kretprobes_unregister(event);
854 ret = 0;
855 break;
ab2277d6 856 case LTTNG_KERNEL_FUNCTION:
e0a7a7c4
MD
857 lttng_ftrace_unregister(event);
858 ret = 0;
859 break;
33a39a3c 860 case LTTNG_KERNEL_SYSCALL:
4ecb5ad5
MD
861 ret = lttng_syscall_filter_disable(event->chan,
862 desc->name);
863 break;
864 case LTTNG_KERNEL_NOOP:
259b6cb3
MD
865 ret = 0;
866 break;
baf20995
MD
867 default:
868 WARN_ON_ONCE(1);
869 }
3c997079
MD
870 if (!ret)
871 event->registered = 0;
dda6a249
MD
872 return ret;
873}
874
875/*
876 * Only used internally at session destruction.
877 */
be066e6c 878static
a90917c3 879void _lttng_event_destroy(struct lttng_event *event)
dda6a249 880{
edeb3137
MD
881 switch (event->instrumentation) {
882 case LTTNG_KERNEL_TRACEPOINT:
a90917c3 883 lttng_event_put(event->desc);
edeb3137
MD
884 break;
885 case LTTNG_KERNEL_KPROBE:
886 module_put(event->desc->owner);
887 lttng_kprobes_destroy_private(event);
888 break;
7371f44c
MD
889 case LTTNG_KERNEL_KRETPROBE:
890 module_put(event->desc->owner);
891 lttng_kretprobes_destroy_private(event);
892 break;
edeb3137
MD
893 case LTTNG_KERNEL_FUNCTION:
894 module_put(event->desc->owner);
895 lttng_ftrace_destroy_private(event);
896 break;
259b6cb3 897 case LTTNG_KERNEL_NOOP:
33a39a3c 898 case LTTNG_KERNEL_SYSCALL:
259b6cb3 899 break;
edeb3137
MD
900 default:
901 WARN_ON_ONCE(1);
902 }
dda6a249 903 list_del(&event->list);
8070f5c0 904 lttng_destroy_context(event->ctx);
11b5a3c2 905 kmem_cache_free(event_cache, event);
4e3c1b9b
MD
906}
907
e0130fab
MD
908int lttng_session_track_pid(struct lttng_session *session, int pid)
909{
910 int ret;
911
912 if (pid < -1)
913 return -EINVAL;
914 mutex_lock(&sessions_mutex);
915 if (pid == -1) {
916 /* track all pids: destroy tracker. */
917 if (session->pid_tracker) {
918 struct lttng_pid_tracker *lpf;
919
920 lpf = session->pid_tracker;
921 rcu_assign_pointer(session->pid_tracker, NULL);
922 synchronize_trace();
923 lttng_pid_tracker_destroy(lpf);
924 }
925 ret = 0;
926 } else {
927 if (!session->pid_tracker) {
928 struct lttng_pid_tracker *lpf;
929
930 lpf = lttng_pid_tracker_create();
931 if (!lpf) {
932 ret = -ENOMEM;
933 goto unlock;
934 }
935 ret = lttng_pid_tracker_add(lpf, pid);
936 rcu_assign_pointer(session->pid_tracker, lpf);
937 } else {
938 ret = lttng_pid_tracker_add(session->pid_tracker, pid);
939 }
940 }
941unlock:
942 mutex_unlock(&sessions_mutex);
943 return ret;
944}
945
946int lttng_session_untrack_pid(struct lttng_session *session, int pid)
947{
948 int ret;
949
950 if (pid < -1)
951 return -EINVAL;
952 mutex_lock(&sessions_mutex);
953 if (pid == -1) {
954 /* untrack all pids: replace by empty tracker. */
955 struct lttng_pid_tracker *old_lpf = session->pid_tracker;
956 struct lttng_pid_tracker *lpf;
957
958 lpf = lttng_pid_tracker_create();
959 if (!lpf) {
960 ret = -ENOMEM;
961 goto unlock;
962 }
963 rcu_assign_pointer(session->pid_tracker, lpf);
964 synchronize_trace();
965 if (old_lpf)
966 lttng_pid_tracker_destroy(old_lpf);
967 ret = 0;
968 } else {
969 if (!session->pid_tracker) {
970 ret = -ENOENT;
971 goto unlock;
972 }
973 ret = lttng_pid_tracker_del(session->pid_tracker, pid);
974 }
975unlock:
976 mutex_unlock(&sessions_mutex);
977 return ret;
978}
979
7e6f9ef6
MD
980static
981void *pid_list_start(struct seq_file *m, loff_t *pos)
982{
983 struct lttng_session *session = m->private;
984 struct lttng_pid_tracker *lpf;
985 struct lttng_pid_hash_node *e;
986 int iter = 0, i;
987
988 mutex_lock(&sessions_mutex);
989 lpf = session->pid_tracker;
990 if (lpf) {
991 for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
992 struct hlist_head *head = &lpf->pid_hash[i];
993
f934e302 994 lttng_hlist_for_each_entry(e, head, hlist) {
7e6f9ef6
MD
995 if (iter++ >= *pos)
996 return e;
997 }
998 }
999 } else {
1000 /* PID tracker disabled. */
1001 if (iter >= *pos && iter == 0) {
1002 return session; /* empty tracker */
1003 }
1004 iter++;
1005 }
1006 /* End of list */
1007 return NULL;
1008}
1009
1010/* Called with sessions_mutex held. */
1011static
1012void *pid_list_next(struct seq_file *m, void *p, loff_t *ppos)
1013{
1014 struct lttng_session *session = m->private;
1015 struct lttng_pid_tracker *lpf;
1016 struct lttng_pid_hash_node *e;
1017 int iter = 0, i;
1018
1019 (*ppos)++;
1020 lpf = session->pid_tracker;
1021 if (lpf) {
1022 for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
1023 struct hlist_head *head = &lpf->pid_hash[i];
1024
f934e302 1025 lttng_hlist_for_each_entry(e, head, hlist) {
7e6f9ef6
MD
1026 if (iter++ >= *ppos)
1027 return e;
1028 }
1029 }
1030 } else {
1031 /* PID tracker disabled. */
1032 if (iter >= *ppos && iter == 0)
1033 return session; /* empty tracker */
1034 iter++;
1035 }
1036
1037 /* End of list */
1038 return NULL;
1039}
1040
1041static
1042void pid_list_stop(struct seq_file *m, void *p)
1043{
1044 mutex_unlock(&sessions_mutex);
1045}
1046
1047static
1048int pid_list_show(struct seq_file *m, void *p)
1049{
1050 int pid;
1051
1052 if (p == m->private) {
1053 /* Tracker disabled. */
1054 pid = -1;
1055 } else {
1056 const struct lttng_pid_hash_node *e = p;
1057
1058 pid = lttng_pid_tracker_get_node_pid(e);
1059 }
1060 seq_printf(m, "process { pid = %d; };\n", pid);
1061 return 0;
1062}
1063
1064static
1065const struct seq_operations lttng_tracker_pids_list_seq_ops = {
1066 .start = pid_list_start,
1067 .next = pid_list_next,
1068 .stop = pid_list_stop,
1069 .show = pid_list_show,
1070};
1071
1072static
1073int lttng_tracker_pids_list_open(struct inode *inode, struct file *file)
1074{
1075 return seq_open(file, &lttng_tracker_pids_list_seq_ops);
1076}
1077
1078static
1079int lttng_tracker_pids_list_release(struct inode *inode, struct file *file)
1080{
1081 struct seq_file *m = file->private_data;
1082 struct lttng_session *session = m->private;
1083 int ret;
1084
1085 WARN_ON_ONCE(!session);
1086 ret = seq_release(inode, file);
1087 if (!ret && session)
1088 fput(session->file);
1089 return ret;
1090}
1091
1092const struct file_operations lttng_tracker_pids_list_fops = {
1093 .owner = THIS_MODULE,
1094 .open = lttng_tracker_pids_list_open,
1095 .read = seq_read,
1096 .llseek = seq_lseek,
1097 .release = lttng_tracker_pids_list_release,
1098};
1099
1100int lttng_session_list_tracker_pids(struct lttng_session *session)
1101{
1102 struct file *tracker_pids_list_file;
1103 struct seq_file *m;
1104 int file_fd, ret;
1105
1106 file_fd = lttng_get_unused_fd();
1107 if (file_fd < 0) {
1108 ret = file_fd;
1109 goto fd_error;
1110 }
1111
1112 tracker_pids_list_file = anon_inode_getfile("[lttng_tracker_pids_list]",
1113 &lttng_tracker_pids_list_fops,
1114 NULL, O_RDWR);
1115 if (IS_ERR(tracker_pids_list_file)) {
1116 ret = PTR_ERR(tracker_pids_list_file);
1117 goto file_error;
1118 }
9c1f4643
MD
1119 if (atomic_long_add_unless(&session->file->f_count,
1120 1, INT_MAX) == INT_MAX) {
1121 goto refcount_error;
1122 }
7e6f9ef6
MD
1123 ret = lttng_tracker_pids_list_fops.open(NULL, tracker_pids_list_file);
1124 if (ret < 0)
1125 goto open_error;
1126 m = tracker_pids_list_file->private_data;
1127 m->private = session;
1128 fd_install(file_fd, tracker_pids_list_file);
7e6f9ef6
MD
1129
1130 return file_fd;
1131
1132open_error:
9c1f4643
MD
1133 atomic_long_dec(&session->file->f_count);
1134refcount_error:
7e6f9ef6
MD
1135 fput(tracker_pids_list_file);
1136file_error:
1137 put_unused_fd(file_fd);
1138fd_error:
1139 return ret;
1140}
1141
3c997079
MD
1142/*
1143 * Enabler management.
1144 */
1145static
4993071a
PP
1146int lttng_match_enabler_star_glob(const char *desc_name,
1147 const char *pattern)
3c997079 1148{
4993071a 1149 if (!strutils_star_glob_match(pattern, -1ULL, desc_name, -1ULL))
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 1193 switch (enabler->type) {
4993071a
PP
1194 case LTTNG_ENABLER_STAR_GLOB:
1195 return lttng_match_enabler_star_glob(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,
578e68ec 1912 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
141ddf28
MD
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",
578e68ec
GB
1921 container_type->base,
1922#if __BYTE_ORDER == __BIG_ENDIAN
1923 container_type->reverse_byte_order ? " byte_order = le;" : ""
1924#else
1925 container_type->reverse_byte_order ? " byte_order = be;" : ""
1926#endif
1927 );
141ddf28
MD
1928 if (ret)
1929 goto end;
1930 /* Dump all entries */
1931 for (i = 0; i < nr_entries; i++) {
1932 const struct lttng_enum_entry *entry = &enum_desc->entries[i];
1933 int j, len;
1934
1935 ret = print_tabs(session, nesting + 1);
1936 if (ret)
1937 goto end;
1938 ret = lttng_metadata_printf(session,
1939 "\"");
1940 if (ret)
1941 goto end;
1942 len = strlen(entry->string);
1943 /* Escape the character '"' */
1944 for (j = 0; j < len; j++) {
1945 char c = entry->string[j];
1946
1947 switch (c) {
1948 case '"':
1949 ret = lttng_metadata_printf(session,
1950 "\\\"");
1951 break;
1952 case '\\':
1953 ret = lttng_metadata_printf(session,
1954 "\\\\");
1955 break;
1956 default:
1957 ret = lttng_metadata_printf(session,
1958 "%c", c);
1959 break;
1960 }
1961 if (ret)
1962 goto end;
1963 }
08ad1061
PP
1964 ret = lttng_metadata_printf(session, "\"");
1965 if (ret)
1966 goto end;
1967
1968 if (entry->options.is_auto) {
1969 ret = lttng_metadata_printf(session, ",\n");
1970 if (ret)
1971 goto end;
141ddf28 1972 } else {
7c559490
MD
1973 ret = lttng_metadata_printf(session,
1974 " = ");
1975 if (ret)
1976 goto end;
1977 if (entry->start.signedness)
141ddf28 1978 ret = lttng_metadata_printf(session,
7c559490
MD
1979 "%lld", (long long) entry->start.value);
1980 else
1981 ret = lttng_metadata_printf(session,
1982 "%llu", entry->start.value);
1983 if (ret)
1984 goto end;
1985 if (entry->start.signedness == entry->end.signedness &&
1986 entry->start.value
1987 == entry->end.value) {
141ddf28 1988 ret = lttng_metadata_printf(session,
7c559490
MD
1989 ",\n");
1990 } else {
1991 if (entry->end.signedness) {
1992 ret = lttng_metadata_printf(session,
1993 " ... %lld,\n",
1994 (long long) entry->end.value);
1995 } else {
1996 ret = lttng_metadata_printf(session,
1997 " ... %llu,\n",
1998 entry->end.value);
1999 }
141ddf28 2000 }
7c559490
MD
2001 if (ret)
2002 goto end;
141ddf28 2003 }
141ddf28
MD
2004 }
2005 ret = print_tabs(session, nesting);
2006 if (ret)
2007 goto end;
2008 ret = lttng_metadata_printf(session, "} _%s;\n",
2009 field->name);
2010end:
2011 return ret;
2012}
2013
d83004aa
JD
2014/*
2015 * Must be called with sessions_mutex held.
2016 */
c099397a 2017static
a90917c3 2018int _lttng_field_statedump(struct lttng_session *session,
f513b2bf
MD
2019 const struct lttng_event_field *field,
2020 size_t nesting)
c099397a 2021{
c099397a 2022 int ret = 0;
c099397a 2023
8070f5c0
MD
2024 switch (field->type.atype) {
2025 case atype_integer:
f513b2bf
MD
2026 ret = print_tabs(session, nesting);
2027 if (ret)
2028 return ret;
8070f5c0 2029 ret = lttng_metadata_printf(session,
f513b2bf 2030 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
8070f5c0
MD
2031 field->type.u.basic.integer.size,
2032 field->type.u.basic.integer.alignment,
2033 field->type.u.basic.integer.signedness,
2034 (field->type.u.basic.integer.encoding == lttng_encode_none)
2035 ? "none"
2036 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
2037 ? "UTF8"
2038 : "ASCII",
2039 field->type.u.basic.integer.base,
5214fa50 2040#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2041 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
c099397a 2042#else
8070f5c0 2043 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
c099397a 2044#endif
8070f5c0
MD
2045 field->name);
2046 break;
2047 case atype_enum:
141ddf28 2048 ret = _lttng_enum_statedump(session, field, nesting);
8070f5c0
MD
2049 break;
2050 case atype_array:
2051 {
2052 const struct lttng_basic_type *elem_type;
2053
2054 elem_type = &field->type.u.array.elem_type;
43803cf2 2055 if (field->type.u.array.elem_alignment) {
f513b2bf
MD
2056 ret = print_tabs(session, nesting);
2057 if (ret)
2058 return ret;
43803cf2 2059 ret = lttng_metadata_printf(session,
f513b2bf 2060 "struct { } align(%u) _%s_padding;\n",
43803cf2
MD
2061 field->type.u.array.elem_alignment * CHAR_BIT,
2062 field->name);
2063 if (ret)
2064 return ret;
2065 }
f513b2bf
MD
2066 ret = print_tabs(session, nesting);
2067 if (ret)
2068 return ret;
8070f5c0 2069 ret = lttng_metadata_printf(session,
f513b2bf 2070 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
8070f5c0
MD
2071 elem_type->u.basic.integer.size,
2072 elem_type->u.basic.integer.alignment,
2073 elem_type->u.basic.integer.signedness,
2074 (elem_type->u.basic.integer.encoding == lttng_encode_none)
2075 ? "none"
2076 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
2077 ? "UTF8"
2078 : "ASCII",
2079 elem_type->u.basic.integer.base,
5214fa50 2080#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2081 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
8fa75f34 2082#else
8070f5c0 2083 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
8fa75f34 2084#endif
8070f5c0
MD
2085 field->name, field->type.u.array.length);
2086 break;
2087 }
2088 case atype_sequence:
2089 {
2090 const struct lttng_basic_type *elem_type;
2091 const struct lttng_basic_type *length_type;
2092
2093 elem_type = &field->type.u.sequence.elem_type;
2094 length_type = &field->type.u.sequence.length_type;
f513b2bf
MD
2095 ret = print_tabs(session, nesting);
2096 if (ret)
2097 return ret;
8070f5c0 2098 ret = lttng_metadata_printf(session,
f513b2bf 2099 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
8070f5c0 2100 length_type->u.basic.integer.size,
32257272 2101 (unsigned int) length_type->u.basic.integer.alignment,
8070f5c0
MD
2102 length_type->u.basic.integer.signedness,
2103 (length_type->u.basic.integer.encoding == lttng_encode_none)
2104 ? "none"
0635cdd4 2105 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
8070f5c0 2106 ? "UTF8"
0635cdd4 2107 : "ASCII"),
8070f5c0 2108 length_type->u.basic.integer.base,
5214fa50 2109#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2110 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
e0a7a7c4 2111#else
0635cdd4 2112 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
e0a7a7c4 2113#endif
27d2368e
MD
2114 field->name);
2115 if (ret)
2116 return ret;
2117
43803cf2 2118 if (field->type.u.sequence.elem_alignment) {
f513b2bf
MD
2119 ret = print_tabs(session, nesting);
2120 if (ret)
2121 return ret;
43803cf2 2122 ret = lttng_metadata_printf(session,
f513b2bf 2123 "struct { } align(%u) _%s_padding;\n",
43803cf2
MD
2124 field->type.u.sequence.elem_alignment * CHAR_BIT,
2125 field->name);
2126 if (ret)
2127 return ret;
2128 }
f513b2bf
MD
2129 ret = print_tabs(session, nesting);
2130 if (ret)
2131 return ret;
27d2368e 2132 ret = lttng_metadata_printf(session,
f513b2bf 2133 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
8070f5c0 2134 elem_type->u.basic.integer.size,
32257272 2135 (unsigned int) elem_type->u.basic.integer.alignment,
8070f5c0
MD
2136 elem_type->u.basic.integer.signedness,
2137 (elem_type->u.basic.integer.encoding == lttng_encode_none)
2138 ? "none"
0635cdd4 2139 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
8070f5c0 2140 ? "UTF8"
0635cdd4 2141 : "ASCII"),
8070f5c0 2142 elem_type->u.basic.integer.base,
5214fa50 2143#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2144 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
8fa75f34 2145#else
8070f5c0 2146 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
8fa75f34 2147#endif
8070f5c0 2148 field->name,
27d2368e 2149 field->name);
8070f5c0
MD
2150 break;
2151 }
c099397a 2152
8070f5c0
MD
2153 case atype_string:
2154 /* Default encoding is UTF8 */
f513b2bf
MD
2155 ret = print_tabs(session, nesting);
2156 if (ret)
2157 return ret;
8070f5c0 2158 ret = lttng_metadata_printf(session,
f513b2bf 2159 "string%s _%s;\n",
8070f5c0
MD
2160 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
2161 " { encoding = ASCII; }" : "",
2162 field->name);
2163 break;
f513b2bf
MD
2164 case atype_struct:
2165 ret = _lttng_struct_statedump(session, field, nesting);
2166 break;
2167 case atype_array_compound:
2168 ret = _lttng_array_compound_statedump(session, field, nesting);
2169 break;
2170 case atype_sequence_compound:
2171 ret = _lttng_sequence_compound_statedump(session, field, nesting);
2172 break;
65c85aa6
MD
2173 case atype_variant:
2174 ret = _lttng_variant_statedump(session, field, nesting);
2175 break;
f513b2bf 2176
8070f5c0
MD
2177 default:
2178 WARN_ON_ONCE(1);
2179 return -EINVAL;
2180 }
2181 return ret;
2182}
2183
2184static
a90917c3 2185int _lttng_context_metadata_statedump(struct lttng_session *session,
8070f5c0
MD
2186 struct lttng_ctx *ctx)
2187{
2188 int ret = 0;
2189 int i;
2190
2191 if (!ctx)
2192 return 0;
2193 for (i = 0; i < ctx->nr_fields; i++) {
2194 const struct lttng_ctx_field *field = &ctx->fields[i];
2195
f513b2bf 2196 ret = _lttng_field_statedump(session, &field->event_field, 2);
8070f5c0
MD
2197 if (ret)
2198 return ret;
2199 }
2200 return ret;
2201}
2202
2203static
a90917c3
MD
2204int _lttng_fields_metadata_statedump(struct lttng_session *session,
2205 struct lttng_event *event)
8070f5c0
MD
2206{
2207 const struct lttng_event_desc *desc = event->desc;
2208 int ret = 0;
2209 int i;
2210
2211 for (i = 0; i < desc->nr_fields; i++) {
2212 const struct lttng_event_field *field = &desc->fields[i];
2213
f513b2bf 2214 ret = _lttng_field_statedump(session, field, 2);
8070f5c0
MD
2215 if (ret)
2216 return ret;
c099397a
MD
2217 }
2218 return ret;
2219}
2220
d83004aa
JD
2221/*
2222 * Must be called with sessions_mutex held.
2223 */
c099397a 2224static
a90917c3
MD
2225int _lttng_event_metadata_statedump(struct lttng_session *session,
2226 struct lttng_channel *chan,
2227 struct lttng_event *event)
c099397a
MD
2228{
2229 int ret = 0;
2230
2231 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
2232 return 0;
d83004aa 2233 if (chan->channel_type == METADATA_CHANNEL)
c099397a
MD
2234 return 0;
2235
2236 ret = lttng_metadata_printf(session,
2237 "event {\n"
ae734547 2238 " name = \"%s\";\n"
c099397a 2239 " id = %u;\n"
b9074a1b 2240 " stream_id = %u;\n",
c099397a
MD
2241 event->desc->name,
2242 event->id,
2243 event->chan->id);
2244 if (ret)
2245 goto end;
2246
b9074a1b
MD
2247 if (event->ctx) {
2248 ret = lttng_metadata_printf(session,
2249 " context := struct {\n");
2250 if (ret)
2251 goto end;
2252 }
a90917c3 2253 ret = _lttng_context_metadata_statedump(session, event->ctx);
8070f5c0
MD
2254 if (ret)
2255 goto end;
b9074a1b
MD
2256 if (event->ctx) {
2257 ret = lttng_metadata_printf(session,
2258 " };\n");
2259 if (ret)
2260 goto end;
2261 }
8070f5c0
MD
2262
2263 ret = lttng_metadata_printf(session,
8070f5c0
MD
2264 " fields := struct {\n"
2265 );
2266 if (ret)
2267 goto end;
2268
a90917c3 2269 ret = _lttng_fields_metadata_statedump(session, event);
c099397a
MD
2270 if (ret)
2271 goto end;
2272
2273 /*
2274 * LTTng space reservation can only reserve multiples of the
2275 * byte size.
2276 */
2277 ret = lttng_metadata_printf(session,
9115fbdc
MD
2278 " };\n"
2279 "};\n\n");
c099397a
MD
2280 if (ret)
2281 goto end;
2282
c099397a
MD
2283 event->metadata_dumped = 1;
2284end:
2285 return ret;
2286
2287}
2288
d83004aa
JD
2289/*
2290 * Must be called with sessions_mutex held.
2291 */
c099397a 2292static
a90917c3
MD
2293int _lttng_channel_metadata_statedump(struct lttng_session *session,
2294 struct lttng_channel *chan)
c099397a
MD
2295{
2296 int ret = 0;
2297
2298 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
2299 return 0;
d83004aa
JD
2300
2301 if (chan->channel_type == METADATA_CHANNEL)
c099397a
MD
2302 return 0;
2303
2304 WARN_ON_ONCE(!chan->header_type);
2305 ret = lttng_metadata_printf(session,
2306 "stream {\n"
2307 " id = %u;\n"
9115fbdc 2308 " event.header := %s;\n"
b9074a1b 2309 " packet.context := struct packet_context;\n",
c099397a
MD
2310 chan->id,
2311 chan->header_type == 1 ? "struct event_header_compact" :
2312 "struct event_header_large");
2313 if (ret)
2314 goto end;
2315
b9074a1b
MD
2316 if (chan->ctx) {
2317 ret = lttng_metadata_printf(session,
2318 " event.context := struct {\n");
2319 if (ret)
2320 goto end;
2321 }
a90917c3 2322 ret = _lttng_context_metadata_statedump(session, chan->ctx);
8070f5c0
MD
2323 if (ret)
2324 goto end;
b9074a1b
MD
2325 if (chan->ctx) {
2326 ret = lttng_metadata_printf(session,
2327 " };\n");
2328 if (ret)
2329 goto end;
2330 }
8070f5c0
MD
2331
2332 ret = lttng_metadata_printf(session,
b9074a1b 2333 "};\n\n");
8070f5c0 2334
c099397a
MD
2335 chan->metadata_dumped = 1;
2336end:
2337 return ret;
2338}
2339
d83004aa
JD
2340/*
2341 * Must be called with sessions_mutex held.
2342 */
9115fbdc 2343static
a90917c3 2344int _lttng_stream_packet_context_declare(struct lttng_session *session)
9115fbdc
MD
2345{
2346 return lttng_metadata_printf(session,
2347 "struct packet_context {\n"
a3ccff4f
MD
2348 " uint64_clock_monotonic_t timestamp_begin;\n"
2349 " uint64_clock_monotonic_t timestamp_end;\n"
576ca06a
MD
2350 " uint64_t content_size;\n"
2351 " uint64_t packet_size;\n"
5b3cf4f9 2352 " uint64_t packet_seq_num;\n"
a9afe705 2353 " unsigned long events_discarded;\n"
9115fbdc 2354 " uint32_t cpu_id;\n"
c6dfdf6f 2355 "};\n\n"
9115fbdc
MD
2356 );
2357}
2358
2359/*
2360 * Compact header:
2361 * id: range: 0 - 30.
2362 * id 31 is reserved to indicate an extended header.
2363 *
2364 * Large header:
2365 * id: range: 0 - 65534.
2366 * id 65535 is reserved to indicate an extended header.
d83004aa
JD
2367 *
2368 * Must be called with sessions_mutex held.
9115fbdc
MD
2369 */
2370static
a90917c3 2371int _lttng_event_header_declare(struct lttng_session *session)
9115fbdc
MD
2372{
2373 return lttng_metadata_printf(session,
2374 "struct event_header_compact {\n"
2375 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2376 " variant <id> {\n"
2377 " struct {\n"
a3ccff4f 2378 " uint27_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2379 " } compact;\n"
2380 " struct {\n"
2381 " uint32_t id;\n"
a3ccff4f 2382 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2383 " } extended;\n"
2384 " } v;\n"
2385 "} align(%u);\n"
2386 "\n"
2387 "struct event_header_large {\n"
2388 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2389 " variant <id> {\n"
2390 " struct {\n"
a3ccff4f 2391 " uint32_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2392 " } compact;\n"
2393 " struct {\n"
2394 " uint32_t id;\n"
a3ccff4f 2395 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2396 " } extended;\n"
2397 " } v;\n"
2398 "} align(%u);\n\n",
a90917c3
MD
2399 lttng_alignof(uint32_t) * CHAR_BIT,
2400 lttng_alignof(uint16_t) * CHAR_BIT
9115fbdc
MD
2401 );
2402}
2403
a3ccff4f
MD
2404 /*
2405 * Approximation of NTP time of day to clock monotonic correlation,
2406 * taken at start of trace.
2407 * Yes, this is only an approximation. Yes, we can (and will) do better
2408 * in future versions.
5283350b
MD
2409 * This function may return a negative offset. It may happen if the
2410 * system sets the REALTIME clock to 0 after boot.
a3ccff4f
MD
2411 */
2412static
8967b2a3 2413int64_t measure_clock_offset(void)
a3ccff4f 2414{
dcb93448 2415 uint64_t monotonic_avg, monotonic[2], realtime;
2754583e 2416 uint64_t tcf = trace_clock_freq();
dcb93448 2417 int64_t offset;
a3ccff4f
MD
2418 struct timespec rts = { 0, 0 };
2419 unsigned long flags;
2420
2421 /* Disable interrupts to increase correlation precision. */
2422 local_irq_save(flags);
2423 monotonic[0] = trace_clock_read64();
216b6baa 2424 getnstimeofday(&rts);
a3ccff4f
MD
2425 monotonic[1] = trace_clock_read64();
2426 local_irq_restore(flags);
2427
dcb93448 2428 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
2754583e
MD
2429 realtime = (uint64_t) rts.tv_sec * tcf;
2430 if (tcf == NSEC_PER_SEC) {
2431 realtime += rts.tv_nsec;
2432 } else {
2433 uint64_t n = rts.tv_nsec * tcf;
2434
2435 do_div(n, NSEC_PER_SEC);
2436 realtime += n;
2437 }
dcb93448 2438 offset = (int64_t) realtime - monotonic_avg;
a3ccff4f
MD
2439 return offset;
2440}
2441
c099397a
MD
2442/*
2443 * Output metadata into this session's metadata buffers.
d83004aa 2444 * Must be called with sessions_mutex held.
c099397a
MD
2445 */
2446static
a90917c3 2447int _lttng_session_metadata_statedump(struct lttng_session *session)
c099397a 2448{
30bdb6e4 2449 unsigned char *uuid_c = session->uuid.b;
a82c63f1 2450 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
a90917c3
MD
2451 struct lttng_channel *chan;
2452 struct lttng_event *event;
c099397a
MD
2453 int ret = 0;
2454
2455 if (!ACCESS_ONCE(session->active))
2456 return 0;
2457 if (session->metadata_dumped)
2458 goto skip_session;
c099397a 2459
d793d5e1 2460 snprintf(uuid_s, sizeof(uuid_s),
30bdb6e4
MD
2461 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2462 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
2463 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
2464 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
2465 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
d793d5e1
MD
2466
2467 ret = lttng_metadata_printf(session,
9115fbdc
MD
2468 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2469 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2470 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2471 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
a9afe705 2472 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
9115fbdc
MD
2473 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2474 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
d793d5e1
MD
2475 "\n"
2476 "trace {\n"
2477 " major = %u;\n"
2478 " minor = %u;\n"
30bdb6e4 2479 " uuid = \"%s\";\n"
d793d5e1
MD
2480 " byte_order = %s;\n"
2481 " packet.header := struct {\n"
2482 " uint32_t magic;\n"
1ec3f75a 2483 " uint8_t uuid[16];\n"
d793d5e1 2484 " uint32_t stream_id;\n"
5594698f 2485 " uint64_t stream_instance_id;\n"
0eb25f58 2486 " };\n"
d793d5e1 2487 "};\n\n",
a90917c3
MD
2488 lttng_alignof(uint8_t) * CHAR_BIT,
2489 lttng_alignof(uint16_t) * CHAR_BIT,
2490 lttng_alignof(uint32_t) * CHAR_BIT,
2491 lttng_alignof(uint64_t) * CHAR_BIT,
a9afe705
MD
2492 sizeof(unsigned long) * CHAR_BIT,
2493 lttng_alignof(unsigned long) * CHAR_BIT,
c6c9e10f
MD
2494 CTF_SPEC_MAJOR,
2495 CTF_SPEC_MINOR,
d793d5e1 2496 uuid_s,
5214fa50 2497#if __BYTE_ORDER == __BIG_ENDIAN
d793d5e1
MD
2498 "be"
2499#else
2500 "le"
2501#endif
2502 );
2503 if (ret)
2504 goto end;
2505
99dc9597
MD
2506 ret = lttng_metadata_printf(session,
2507 "env {\n"
a6058143 2508 " hostname = \"%s\";\n"
c6c9e10f 2509 " domain = \"kernel\";\n"
99dc9597 2510 " sysname = \"%s\";\n"
c6c9e10f
MD
2511 " kernel_release = \"%s\";\n"
2512 " kernel_version = \"%s\";\n"
2513 " tracer_name = \"lttng-modules\";\n"
2514 " tracer_major = %d;\n"
2515 " tracer_minor = %d;\n"
2516 " tracer_patchlevel = %d;\n"
99dc9597 2517 "};\n\n",
3d0d43db 2518 current->nsproxy->uts_ns->name.nodename,
99dc9597
MD
2519 utsname()->sysname,
2520 utsname()->release,
c6c9e10f
MD
2521 utsname()->version,
2522 LTTNG_MODULES_MAJOR_VERSION,
2523 LTTNG_MODULES_MINOR_VERSION,
2524 LTTNG_MODULES_PATCHLEVEL_VERSION
99dc9597
MD
2525 );
2526 if (ret)
2527 goto end;
2528
a3ccff4f
MD
2529 ret = lttng_metadata_printf(session,
2530 "clock {\n"
2754583e
MD
2531 " name = \"%s\";\n",
2532 trace_clock_name()
a82c63f1
MD
2533 );
2534 if (ret)
2535 goto end;
2536
2537 if (!trace_clock_uuid(clock_uuid_s)) {
2538 ret = lttng_metadata_printf(session,
7c27cb17 2539 " uuid = \"%s\";\n",
a82c63f1
MD
2540 clock_uuid_s
2541 );
2542 if (ret)
2543 goto end;
2544 }
2545
2546 ret = lttng_metadata_printf(session,
2754583e 2547 " description = \"%s\";\n"
a3ccff4f
MD
2548 " freq = %llu; /* Frequency, in Hz */\n"
2549 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
8967b2a3 2550 " offset = %lld;\n"
a3ccff4f 2551 "};\n\n",
2754583e 2552 trace_clock_description(),
a3ccff4f 2553 (unsigned long long) trace_clock_freq(),
8967b2a3 2554 (long long) measure_clock_offset()
a3ccff4f
MD
2555 );
2556 if (ret)
2557 goto end;
2558
2559 ret = lttng_metadata_printf(session,
2560 "typealias integer {\n"
2561 " size = 27; align = 1; signed = false;\n"
2754583e 2562 " map = clock.%s.value;\n"
a3ccff4f
MD
2563 "} := uint27_clock_monotonic_t;\n"
2564 "\n"
2565 "typealias integer {\n"
2566 " size = 32; align = %u; signed = false;\n"
2754583e 2567 " map = clock.%s.value;\n"
a3ccff4f
MD
2568 "} := uint32_clock_monotonic_t;\n"
2569 "\n"
2570 "typealias integer {\n"
2571 " size = 64; align = %u; signed = false;\n"
2754583e 2572 " map = clock.%s.value;\n"
a3ccff4f 2573 "} := uint64_clock_monotonic_t;\n\n",
2754583e 2574 trace_clock_name(),
a3ccff4f 2575 lttng_alignof(uint32_t) * CHAR_BIT,
2754583e
MD
2576 trace_clock_name(),
2577 lttng_alignof(uint64_t) * CHAR_BIT,
2578 trace_clock_name()
a3ccff4f
MD
2579 );
2580 if (ret)
2581 goto end;
2582
a90917c3 2583 ret = _lttng_stream_packet_context_declare(session);
9115fbdc
MD
2584 if (ret)
2585 goto end;
2586
a90917c3 2587 ret = _lttng_event_header_declare(session);
9115fbdc
MD
2588 if (ret)
2589 goto end;
2590
c099397a
MD
2591skip_session:
2592 list_for_each_entry(chan, &session->chan, list) {
a90917c3 2593 ret = _lttng_channel_metadata_statedump(session, chan);
c099397a
MD
2594 if (ret)
2595 goto end;
2596 }
2597
2598 list_for_each_entry(event, &session->events, list) {
a90917c3 2599 ret = _lttng_event_metadata_statedump(session, event->chan, event);
c099397a
MD
2600 if (ret)
2601 goto end;
2602 }
2603 session->metadata_dumped = 1;
2604end:
2605 return ret;
2606}
2607
c0e31d2e 2608/**
a90917c3 2609 * lttng_transport_register - LTT transport registration
c0e31d2e
MD
2610 * @transport: transport structure
2611 *
2612 * Registers a transport which can be used as output to extract the data out of
2613 * LTTng. The module calling this registration function must ensure that no
2614 * trap-inducing code will be executed by the transport functions. E.g.
2615 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2616 * is made visible to the transport function. This registration acts as a
2617 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2618 * after its registration must it synchronize the TLBs.
2619 */
a90917c3 2620void lttng_transport_register(struct lttng_transport *transport)
c0e31d2e
MD
2621{
2622 /*
2623 * Make sure no page fault can be triggered by the module about to be
2624 * registered. We deal with this here so we don't have to call
2625 * vmalloc_sync_all() in each module's init.
2626 */
6d2a620c 2627 wrapper_vmalloc_sync_all();
c0e31d2e
MD
2628
2629 mutex_lock(&sessions_mutex);
a90917c3 2630 list_add_tail(&transport->node, &lttng_transport_list);
c0e31d2e
MD
2631 mutex_unlock(&sessions_mutex);
2632}
a90917c3 2633EXPORT_SYMBOL_GPL(lttng_transport_register);
c0e31d2e
MD
2634
2635/**
a90917c3 2636 * lttng_transport_unregister - LTT transport unregistration
c0e31d2e
MD
2637 * @transport: transport structure
2638 */
a90917c3 2639void lttng_transport_unregister(struct lttng_transport *transport)
c0e31d2e
MD
2640{
2641 mutex_lock(&sessions_mutex);
2642 list_del(&transport->node);
2643 mutex_unlock(&sessions_mutex);
2644}
a90917c3 2645EXPORT_SYMBOL_GPL(lttng_transport_unregister);
c0e31d2e 2646
1e367326
MD
2647#if (defined(CONFIG_HOTPLUG_CPU) && (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)))
2648
2649enum cpuhp_state lttng_hp_prepare;
2650enum cpuhp_state lttng_hp_online;
2651
2652static int lttng_hotplug_prepare(unsigned int cpu, struct hlist_node *node)
2653{
2654 struct lttng_cpuhp_node *lttng_node;
2655
2656 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2657 switch (lttng_node->component) {
2658 case LTTNG_RING_BUFFER_FRONTEND:
2659 return 0;
2660 case LTTNG_RING_BUFFER_BACKEND:
2661 return lttng_cpuhp_rb_backend_prepare(cpu, lttng_node);
2662 case LTTNG_RING_BUFFER_ITER:
2663 return 0;
2664 case LTTNG_CONTEXT_PERF_COUNTERS:
2665 return 0;
2666 default:
2667 return -EINVAL;
2668 }
2669}
2670
2671static int lttng_hotplug_dead(unsigned int cpu, struct hlist_node *node)
2672{
2673 struct lttng_cpuhp_node *lttng_node;
2674
2675 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2676 switch (lttng_node->component) {
2677 case LTTNG_RING_BUFFER_FRONTEND:
2678 return lttng_cpuhp_rb_frontend_dead(cpu, lttng_node);
2679 case LTTNG_RING_BUFFER_BACKEND:
2680 return 0;
2681 case LTTNG_RING_BUFFER_ITER:
2682 return 0;
2683 case LTTNG_CONTEXT_PERF_COUNTERS:
2684 return lttng_cpuhp_perf_counter_dead(cpu, lttng_node);
2685 default:
2686 return -EINVAL;
2687 }
2688}
2689
2690static int lttng_hotplug_online(unsigned int cpu, struct hlist_node *node)
2691{
2692 struct lttng_cpuhp_node *lttng_node;
2693
2694 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2695 switch (lttng_node->component) {
2696 case LTTNG_RING_BUFFER_FRONTEND:
2697 return lttng_cpuhp_rb_frontend_online(cpu, lttng_node);
2698 case LTTNG_RING_BUFFER_BACKEND:
2699 return 0;
2700 case LTTNG_RING_BUFFER_ITER:
2701 return lttng_cpuhp_rb_iter_online(cpu, lttng_node);
2702 case LTTNG_CONTEXT_PERF_COUNTERS:
2703 return lttng_cpuhp_perf_counter_online(cpu, lttng_node);
2704 default:
2705 return -EINVAL;
2706 }
2707}
2708
2709static int lttng_hotplug_offline(unsigned int cpu, struct hlist_node *node)
2710{
2711 struct lttng_cpuhp_node *lttng_node;
2712
2713 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2714 switch (lttng_node->component) {
2715 case LTTNG_RING_BUFFER_FRONTEND:
2716 return lttng_cpuhp_rb_frontend_offline(cpu, lttng_node);
2717 case LTTNG_RING_BUFFER_BACKEND:
2718 return 0;
2719 case LTTNG_RING_BUFFER_ITER:
2720 return 0;
2721 case LTTNG_CONTEXT_PERF_COUNTERS:
2722 return 0;
2723 default:
2724 return -EINVAL;
2725 }
2726}
2727
2728static int __init lttng_init_cpu_hotplug(void)
2729{
2730 int ret;
2731
2732 ret = cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN, "lttng:prepare",
2733 lttng_hotplug_prepare,
2734 lttng_hotplug_dead);
2735 if (ret < 0) {
2736 return ret;
2737 }
2738 lttng_hp_prepare = ret;
2739 lttng_rb_set_hp_prepare(ret);
2740
2741 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "lttng:online",
2742 lttng_hotplug_online,
2743 lttng_hotplug_offline);
2744 if (ret < 0) {
2745 cpuhp_remove_multi_state(lttng_hp_prepare);
2746 lttng_hp_prepare = 0;
2747 return ret;
2748 }
2749 lttng_hp_online = ret;
2750 lttng_rb_set_hp_online(ret);
2751
2752 return 0;
2753}
2754
2755static void __exit lttng_exit_cpu_hotplug(void)
2756{
2757 lttng_rb_set_hp_online(0);
2758 cpuhp_remove_multi_state(lttng_hp_online);
2759 lttng_rb_set_hp_prepare(0);
2760 cpuhp_remove_multi_state(lttng_hp_prepare);
2761}
2762
2763#else /* #if (CONFIG_HOTPLUG_CPU && (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))) */
2764static int lttng_init_cpu_hotplug(void)
2765{
2766 return 0;
2767}
2768static void lttng_exit_cpu_hotplug(void)
2769{
2770}
2771#endif /* #else #if (CONFIG_HOTPLUG_CPU && (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))) */
2772
2773
a90917c3 2774static int __init lttng_events_init(void)
4e3c1b9b 2775{
1c25284c
MD
2776 int ret;
2777
453b2495
JD
2778 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
2779 if (ret)
2780 return ret;
389d7070 2781 ret = wrapper_get_pfnblock_flags_mask_init();
36561775
MD
2782 if (ret)
2783 return ret;
2784 ret = wrapper_get_pageblock_flags_mask_init();
114667d5
MD
2785 if (ret)
2786 return ret;
2787 ret = lttng_probes_init();
389d7070
MD
2788 if (ret)
2789 return ret;
07dfc1d0 2790 ret = lttng_context_init();
20591cf7
MD
2791 if (ret)
2792 return ret;
07dfc1d0
MD
2793 ret = lttng_tracepoint_init();
2794 if (ret)
2795 goto error_tp;
a90917c3 2796 event_cache = KMEM_CACHE(lttng_event, 0);
20591cf7
MD
2797 if (!event_cache) {
2798 ret = -ENOMEM;
2799 goto error_kmem;
2800 }
80996790 2801 ret = lttng_abi_init();
02119ee5
MD
2802 if (ret)
2803 goto error_abi;
0c956676
MD
2804 ret = lttng_logger_init();
2805 if (ret)
2806 goto error_logger;
1e367326
MD
2807 ret = lttng_init_cpu_hotplug();
2808 if (ret)
2809 goto error_hotplug;
489de24b
MJ
2810 printk(KERN_NOTICE "LTTng: Loaded modules v%s.%s.%s%s (%s)\n",
2811 __stringify(LTTNG_MODULES_MAJOR_VERSION),
2812 __stringify(LTTNG_MODULES_MINOR_VERSION),
2813 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
2814 LTTNG_MODULES_EXTRAVERSION,
2815 LTTNG_VERSION_NAME);
4e3c1b9b 2816 return 0;
0c956676 2817
1e367326
MD
2818error_hotplug:
2819 lttng_logger_exit();
0c956676
MD
2820error_logger:
2821 lttng_abi_exit();
02119ee5 2822error_abi:
1c25284c 2823 kmem_cache_destroy(event_cache);
20591cf7
MD
2824error_kmem:
2825 lttng_tracepoint_exit();
07dfc1d0
MD
2826error_tp:
2827 lttng_context_exit();
489de24b
MJ
2828 printk(KERN_NOTICE "LTTng: Failed to load modules v%s.%s.%s%s (%s)\n",
2829 __stringify(LTTNG_MODULES_MAJOR_VERSION),
2830 __stringify(LTTNG_MODULES_MINOR_VERSION),
2831 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
2832 LTTNG_MODULES_EXTRAVERSION,
2833 LTTNG_VERSION_NAME);
1c25284c 2834 return ret;
4e3c1b9b
MD
2835}
2836
a90917c3 2837module_init(lttng_events_init);
11b5a3c2 2838
a90917c3 2839static void __exit lttng_events_exit(void)
4e3c1b9b 2840{
a90917c3 2841 struct lttng_session *session, *tmpsession;
92e94819 2842
1e367326 2843 lttng_exit_cpu_hotplug();
0c956676 2844 lttng_logger_exit();
80996790 2845 lttng_abi_exit();
92e94819 2846 list_for_each_entry_safe(session, tmpsession, &sessions, list)
a90917c3 2847 lttng_session_destroy(session);
11b5a3c2 2848 kmem_cache_destroy(event_cache);
20591cf7 2849 lttng_tracepoint_exit();
07dfc1d0 2850 lttng_context_exit();
489de24b
MJ
2851 printk(KERN_NOTICE "LTTng: Unloaded modules v%s.%s.%s%s (%s)\n",
2852 __stringify(LTTNG_MODULES_MAJOR_VERSION),
2853 __stringify(LTTNG_MODULES_MINOR_VERSION),
2854 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
2855 LTTNG_MODULES_EXTRAVERSION,
2856 LTTNG_VERSION_NAME);
4e3c1b9b 2857}
92e94819 2858
a90917c3 2859module_exit(lttng_events_exit);
11b5a3c2 2860
92e94819
MD
2861MODULE_LICENSE("GPL and additional rights");
2862MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2863MODULE_DESCRIPTION("LTTng Events");
9a9973ef
MD
2864MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
2865 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
309370e2
MD
2866 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
2867 LTTNG_MODULES_EXTRAVERSION);
This page took 0.180113 seconds and 5 git commands to generate.