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