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