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