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