Commit | Line | Data |
---|---|---|
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 | ||
165eee70 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/list.h> |
31 | #include <linux/mutex.h> | |
32 | #include <linux/sched.h> | |
11b5a3c2 | 33 | #include <linux/slab.h> |
c099397a | 34 | #include <linux/jiffies.h> |
99dc9597 | 35 | #include <linux/utsname.h> |
abc0446a | 36 | #include <linux/err.h> |
30913c47 ML |
37 | #include <linux/vmalloc.h> |
38 | ||
a864fb02 | 39 | #include "wrapper/uuid.h" |
b13f3ebe | 40 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
a82c63f1 | 41 | #include "wrapper/random.h" |
3a523f5b | 42 | #include "wrapper/tracepoint.h" |
b8590f40 | 43 | #include "lttng-kernel-version.h" |
a90917c3 MD |
44 | #include "lttng-events.h" |
45 | #include "lttng-tracer.h" | |
6dccd6c1 | 46 | #include "lttng-abi-old.h" |
4e3c1b9b | 47 | |
d83004aa JD |
48 | #define METADATA_CACHE_DEFAULT_SIZE 4096 |
49 | ||
4e3c1b9b | 50 | static LIST_HEAD(sessions); |
a90917c3 | 51 | static LIST_HEAD(lttng_transport_list); |
d83004aa JD |
52 | /* |
53 | * Protect the sessions and metadata caches. | |
54 | */ | |
4e3c1b9b MD |
55 | static DEFINE_MUTEX(sessions_mutex); |
56 | static struct kmem_cache *event_cache; | |
57 | ||
a90917c3 MD |
58 | static void _lttng_event_destroy(struct lttng_event *event); |
59 | static void _lttng_channel_destroy(struct lttng_channel *chan); | |
60 | static int _lttng_event_unregister(struct lttng_event *event); | |
c099397a | 61 | static |
a90917c3 MD |
62 | int _lttng_event_metadata_statedump(struct lttng_session *session, |
63 | struct lttng_channel *chan, | |
64 | struct lttng_event *event); | |
c099397a | 65 | static |
a90917c3 | 66 | int _lttng_session_metadata_statedump(struct lttng_session *session); |
d83004aa JD |
67 | static |
68 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream); | |
c099397a | 69 | |
c099397a | 70 | void synchronize_trace(void) |
abcca994 MD |
71 | { |
72 | synchronize_sched(); | |
b8590f40 MD |
73 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) |
74 | #ifdef CONFIG_PREEMPT_RT_FULL | |
75 | synchronize_rcu(); | |
76 | #endif | |
77 | #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */ | |
abcca994 MD |
78 | #ifdef CONFIG_PREEMPT_RT |
79 | synchronize_rcu(); | |
80 | #endif | |
b8590f40 | 81 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */ |
abcca994 MD |
82 | } |
83 | ||
a90917c3 | 84 | struct lttng_session *lttng_session_create(void) |
4e3c1b9b | 85 | { |
a90917c3 | 86 | struct lttng_session *session; |
d83004aa | 87 | struct lttng_metadata_cache *metadata_cache; |
4e3c1b9b MD |
88 | |
89 | mutex_lock(&sessions_mutex); | |
a90917c3 | 90 | session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL); |
4e3c1b9b | 91 | if (!session) |
d83004aa | 92 | goto err; |
4e3c1b9b | 93 | INIT_LIST_HEAD(&session->chan); |
f3d01b96 | 94 | INIT_LIST_HEAD(&session->events); |
d793d5e1 | 95 | uuid_le_gen(&session->uuid); |
d83004aa JD |
96 | |
97 | metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache), | |
98 | GFP_KERNEL); | |
99 | if (!metadata_cache) | |
100 | goto err_free_session; | |
30913c47 | 101 | metadata_cache->data = vzalloc(METADATA_CACHE_DEFAULT_SIZE); |
d83004aa JD |
102 | if (!metadata_cache->data) |
103 | goto err_free_cache; | |
104 | metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE; | |
105 | kref_init(&metadata_cache->refcount); | |
9a0aa3b1 | 106 | mutex_init(&metadata_cache->lock); |
d83004aa JD |
107 | session->metadata_cache = metadata_cache; |
108 | INIT_LIST_HEAD(&metadata_cache->metadata_stream); | |
a36580d5 MD |
109 | memcpy(&metadata_cache->uuid, &session->uuid, |
110 | sizeof(metadata_cache->uuid)); | |
4e3c1b9b MD |
111 | list_add(&session->list, &sessions); |
112 | mutex_unlock(&sessions_mutex); | |
113 | return session; | |
d83004aa JD |
114 | |
115 | err_free_cache: | |
116 | kfree(metadata_cache); | |
117 | err_free_session: | |
118 | kfree(session); | |
119 | err: | |
120 | mutex_unlock(&sessions_mutex); | |
121 | return NULL; | |
122 | } | |
123 | ||
124 | void metadata_cache_destroy(struct kref *kref) | |
125 | { | |
126 | struct lttng_metadata_cache *cache = | |
127 | container_of(kref, struct lttng_metadata_cache, refcount); | |
30913c47 | 128 | vfree(cache->data); |
d83004aa | 129 | kfree(cache); |
4e3c1b9b MD |
130 | } |
131 | ||
a90917c3 | 132 | void lttng_session_destroy(struct lttng_session *session) |
4e3c1b9b | 133 | { |
a90917c3 MD |
134 | struct lttng_channel *chan, *tmpchan; |
135 | struct lttng_event *event, *tmpevent; | |
d83004aa | 136 | struct lttng_metadata_stream *metadata_stream; |
dda6a249 | 137 | int ret; |
4e3c1b9b MD |
138 | |
139 | mutex_lock(&sessions_mutex); | |
52fc2e1f | 140 | ACCESS_ONCE(session->active) = 0; |
1ec65de1 MD |
141 | list_for_each_entry(chan, &session->chan, list) { |
142 | ret = lttng_syscalls_unregister(chan); | |
143 | WARN_ON(ret); | |
144 | } | |
dda6a249 | 145 | list_for_each_entry(event, &session->events, list) { |
a90917c3 | 146 | ret = _lttng_event_unregister(event); |
dda6a249 MD |
147 | WARN_ON(ret); |
148 | } | |
abcca994 | 149 | synchronize_trace(); /* Wait for in-flight events to complete */ |
4e3c1b9b | 150 | list_for_each_entry_safe(event, tmpevent, &session->events, list) |
a90917c3 | 151 | _lttng_event_destroy(event); |
d83004aa JD |
152 | list_for_each_entry_safe(chan, tmpchan, &session->chan, list) { |
153 | BUG_ON(chan->channel_type == METADATA_CHANNEL); | |
a90917c3 | 154 | _lttng_channel_destroy(chan); |
d83004aa JD |
155 | } |
156 | list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list) | |
157 | _lttng_metadata_channel_hangup(metadata_stream); | |
158 | kref_put(&session->metadata_cache->refcount, metadata_cache_destroy); | |
4e3c1b9b MD |
159 | list_del(&session->list); |
160 | mutex_unlock(&sessions_mutex); | |
161 | kfree(session); | |
162 | } | |
163 | ||
a90917c3 | 164 | int lttng_session_enable(struct lttng_session *session) |
c0e31d2e MD |
165 | { |
166 | int ret = 0; | |
a90917c3 | 167 | struct lttng_channel *chan; |
c0e31d2e MD |
168 | |
169 | mutex_lock(&sessions_mutex); | |
170 | if (session->active) { | |
171 | ret = -EBUSY; | |
172 | goto end; | |
173 | } | |
c099397a MD |
174 | |
175 | /* | |
176 | * Snapshot the number of events per channel to know the type of header | |
177 | * we need to use. | |
178 | */ | |
179 | list_for_each_entry(chan, &session->chan, list) { | |
180 | if (chan->header_type) | |
181 | continue; /* don't change it if session stop/restart */ | |
182 | if (chan->free_event_id < 31) | |
183 | chan->header_type = 1; /* compact */ | |
184 | else | |
185 | chan->header_type = 2; /* large */ | |
186 | } | |
187 | ||
52fc2e1f | 188 | ACCESS_ONCE(session->active) = 1; |
8070f5c0 | 189 | ACCESS_ONCE(session->been_active) = 1; |
a90917c3 | 190 | ret = _lttng_session_metadata_statedump(session); |
c337ddc2 MD |
191 | if (ret) { |
192 | ACCESS_ONCE(session->active) = 0; | |
193 | goto end; | |
194 | } | |
195 | ret = lttng_statedump_start(session); | |
360f38ea | 196 | if (ret) |
c099397a | 197 | ACCESS_ONCE(session->active) = 0; |
c0e31d2e MD |
198 | end: |
199 | mutex_unlock(&sessions_mutex); | |
11b5a3c2 | 200 | return ret; |
c0e31d2e MD |
201 | } |
202 | ||
a90917c3 | 203 | int lttng_session_disable(struct lttng_session *session) |
c0e31d2e MD |
204 | { |
205 | int ret = 0; | |
206 | ||
207 | mutex_lock(&sessions_mutex); | |
208 | if (!session->active) { | |
209 | ret = -EBUSY; | |
210 | goto end; | |
211 | } | |
52fc2e1f | 212 | ACCESS_ONCE(session->active) = 0; |
c0e31d2e MD |
213 | end: |
214 | mutex_unlock(&sessions_mutex); | |
11b5a3c2 | 215 | return ret; |
c0e31d2e MD |
216 | } |
217 | ||
a90917c3 | 218 | int lttng_channel_enable(struct lttng_channel *channel) |
e64957da MD |
219 | { |
220 | int old; | |
221 | ||
d83004aa | 222 | if (channel->channel_type == METADATA_CHANNEL) |
c6485006 | 223 | return -EPERM; |
e64957da MD |
224 | old = xchg(&channel->enabled, 1); |
225 | if (old) | |
226 | return -EEXIST; | |
227 | return 0; | |
228 | } | |
229 | ||
a90917c3 | 230 | int lttng_channel_disable(struct lttng_channel *channel) |
e64957da MD |
231 | { |
232 | int old; | |
233 | ||
d83004aa | 234 | if (channel->channel_type == METADATA_CHANNEL) |
c6485006 | 235 | return -EPERM; |
e64957da MD |
236 | old = xchg(&channel->enabled, 0); |
237 | if (!old) | |
238 | return -EEXIST; | |
239 | return 0; | |
240 | } | |
241 | ||
a90917c3 | 242 | int lttng_event_enable(struct lttng_event *event) |
e64957da MD |
243 | { |
244 | int old; | |
245 | ||
d83004aa | 246 | if (event->chan->channel_type == METADATA_CHANNEL) |
c6485006 | 247 | return -EPERM; |
e64957da MD |
248 | old = xchg(&event->enabled, 1); |
249 | if (old) | |
250 | return -EEXIST; | |
251 | return 0; | |
252 | } | |
253 | ||
a90917c3 | 254 | int lttng_event_disable(struct lttng_event *event) |
e64957da MD |
255 | { |
256 | int old; | |
257 | ||
d83004aa | 258 | if (event->chan->channel_type == METADATA_CHANNEL) |
c6485006 | 259 | return -EPERM; |
e64957da MD |
260 | old = xchg(&event->enabled, 0); |
261 | if (!old) | |
262 | return -EEXIST; | |
263 | return 0; | |
264 | } | |
265 | ||
a90917c3 | 266 | static struct lttng_transport *lttng_transport_find(const char *name) |
f3d01b96 | 267 | { |
a90917c3 | 268 | struct lttng_transport *transport; |
f3d01b96 | 269 | |
a90917c3 | 270 | list_for_each_entry(transport, <tng_transport_list, node) { |
f3d01b96 MD |
271 | if (!strcmp(transport->name, name)) |
272 | return transport; | |
273 | } | |
274 | return NULL; | |
275 | } | |
276 | ||
a90917c3 | 277 | struct lttng_channel *lttng_channel_create(struct lttng_session *session, |
5dbbdb43 MD |
278 | const char *transport_name, |
279 | void *buf_addr, | |
4e3c1b9b MD |
280 | size_t subbuf_size, size_t num_subbuf, |
281 | unsigned int switch_timer_interval, | |
d83004aa JD |
282 | unsigned int read_timer_interval, |
283 | enum channel_type channel_type) | |
4e3c1b9b | 284 | { |
a90917c3 MD |
285 | struct lttng_channel *chan; |
286 | struct lttng_transport *transport = NULL; | |
4e3c1b9b MD |
287 | |
288 | mutex_lock(&sessions_mutex); | |
d83004aa | 289 | if (session->been_active && channel_type != METADATA_CHANNEL) |
e5382b6d | 290 | goto active; /* Refuse to add channel to active session */ |
a90917c3 | 291 | transport = lttng_transport_find(transport_name); |
f3d01b96 MD |
292 | if (!transport) { |
293 | printk(KERN_WARNING "LTTng transport %s not found\n", | |
294 | transport_name); | |
c0e31d2e | 295 | goto notransport; |
f3d01b96 | 296 | } |
a33c9927 MD |
297 | if (!try_module_get(transport->owner)) { |
298 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); | |
299 | goto notransport; | |
300 | } | |
a90917c3 | 301 | chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL); |
4e3c1b9b | 302 | if (!chan) |
c0e31d2e | 303 | goto nomem; |
4e3c1b9b | 304 | chan->session = session; |
05d32c64 | 305 | chan->id = session->free_chan_id++; |
3b731ab1 | 306 | chan->ops = &transport->ops; |
125b4df4 MD |
307 | /* |
308 | * Note: the channel creation op already writes into the packet | |
309 | * headers. Therefore the "chan" information used as input | |
310 | * should be already accessible. | |
311 | */ | |
5f5ddf01 MD |
312 | chan->chan = transport->ops.channel_create(transport_name, |
313 | chan, buf_addr, subbuf_size, num_subbuf, | |
314 | switch_timer_interval, read_timer_interval); | |
f3d01b96 MD |
315 | if (!chan->chan) |
316 | goto create_error; | |
e64957da | 317 | chan->enabled = 1; |
a33c9927 | 318 | chan->transport = transport; |
d83004aa | 319 | chan->channel_type = channel_type; |
4e3c1b9b MD |
320 | list_add(&chan->list, &session->chan); |
321 | mutex_unlock(&sessions_mutex); | |
322 | return chan; | |
323 | ||
f3d01b96 MD |
324 | create_error: |
325 | kfree(chan); | |
c0e31d2e | 326 | nomem: |
a33c9927 MD |
327 | if (transport) |
328 | module_put(transport->owner); | |
c0e31d2e | 329 | notransport: |
e5382b6d | 330 | active: |
4e3c1b9b MD |
331 | mutex_unlock(&sessions_mutex); |
332 | return NULL; | |
333 | } | |
334 | ||
335 | /* | |
d83004aa JD |
336 | * Only used internally at session destruction for per-cpu channels, and |
337 | * when metadata channel is released. | |
338 | * Needs to be called with sessions mutex held. | |
4e3c1b9b | 339 | */ |
aa7c23a9 | 340 | static |
a90917c3 | 341 | void _lttng_channel_destroy(struct lttng_channel *chan) |
4e3c1b9b | 342 | { |
11b5a3c2 | 343 | chan->ops->channel_destroy(chan->chan); |
a33c9927 | 344 | module_put(chan->transport->owner); |
4e3c1b9b | 345 | list_del(&chan->list); |
8070f5c0 | 346 | lttng_destroy_context(chan->ctx); |
4e3c1b9b MD |
347 | kfree(chan); |
348 | } | |
349 | ||
d83004aa JD |
350 | void lttng_metadata_channel_destroy(struct lttng_channel *chan) |
351 | { | |
352 | BUG_ON(chan->channel_type != METADATA_CHANNEL); | |
353 | ||
354 | /* Protect the metadata cache with the sessions_mutex. */ | |
355 | mutex_lock(&sessions_mutex); | |
356 | _lttng_channel_destroy(chan); | |
357 | mutex_unlock(&sessions_mutex); | |
358 | } | |
359 | EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy); | |
360 | ||
361 | static | |
362 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream) | |
363 | { | |
364 | stream->finalized = 1; | |
365 | wake_up_interruptible(&stream->read_wait); | |
366 | } | |
367 | ||
e5382b6d MD |
368 | /* |
369 | * Supports event creation while tracing session is active. | |
370 | */ | |
a90917c3 | 371 | struct lttng_event *lttng_event_create(struct lttng_channel *chan, |
d6d808f3 | 372 | struct lttng_kernel_event *event_param, |
259b6cb3 MD |
373 | void *filter, |
374 | const struct lttng_event_desc *internal_desc) | |
4e3c1b9b | 375 | { |
a90917c3 | 376 | struct lttng_event *event; |
3d084699 | 377 | int ret; |
4e3c1b9b MD |
378 | |
379 | mutex_lock(&sessions_mutex); | |
abc0446a | 380 | if (chan->free_event_id == -1U) { |
4cf0bf51 | 381 | ret = -EMFILE; |
e5382b6d | 382 | goto full; |
abc0446a | 383 | } |
4e3c1b9b | 384 | /* |
dda6a249 MD |
385 | * This is O(n^2) (for each event, the loop is called at event |
386 | * creation). Might require a hash if we have lots of events. | |
4e3c1b9b | 387 | */ |
abc0446a MD |
388 | list_for_each_entry(event, &chan->session->events, list) { |
389 | if (!strcmp(event->desc->name, event_param->name)) { | |
f046a95d MD |
390 | /* |
391 | * Allow events with the same name to appear in | |
392 | * different channels. | |
393 | */ | |
394 | if (event->chan == chan) { | |
395 | ret = -EEXIST; | |
396 | goto exist; | |
397 | } | |
abc0446a MD |
398 | } |
399 | } | |
11b5a3c2 | 400 | event = kmem_cache_zalloc(event_cache, GFP_KERNEL); |
abc0446a | 401 | if (!event) { |
4cf0bf51 | 402 | ret = -ENOMEM; |
4e3c1b9b | 403 | goto cache_error; |
abc0446a | 404 | } |
4e3c1b9b MD |
405 | event->chan = chan; |
406 | event->filter = filter; | |
e5382b6d | 407 | event->id = chan->free_event_id++; |
e64957da | 408 | event->enabled = 1; |
d6d808f3 | 409 | event->instrumentation = event_param->instrumentation; |
a90917c3 | 410 | /* Populate lttng_event structure before tracepoint registration. */ |
e5382b6d | 411 | smp_wmb(); |
d6d808f3 | 412 | switch (event_param->instrumentation) { |
ab2277d6 | 413 | case LTTNG_KERNEL_TRACEPOINT: |
a90917c3 | 414 | event->desc = lttng_event_get(event_param->name); |
abc0446a | 415 | if (!event->desc) { |
4cf0bf51 | 416 | ret = -ENOENT; |
d3dbe23c | 417 | goto register_error; |
abc0446a | 418 | } |
20591cf7 | 419 | ret = lttng_wrapper_tracepoint_probe_register(event->desc->kname, |
d3dbe23c MD |
420 | event->desc->probe_callback, |
421 | event); | |
abc0446a | 422 | if (ret) { |
4cf0bf51 | 423 | ret = -EINVAL; |
11b5a3c2 | 424 | goto register_error; |
abc0446a | 425 | } |
baf20995 | 426 | break; |
ab2277d6 | 427 | case LTTNG_KERNEL_KPROBE: |
30bdb6e4 | 428 | ret = lttng_kprobes_register(event_param->name, |
f17701fb MD |
429 | event_param->u.kprobe.symbol_name, |
430 | event_param->u.kprobe.offset, | |
431 | event_param->u.kprobe.addr, | |
432 | event); | |
abc0446a | 433 | if (ret) { |
4cf0bf51 | 434 | ret = -EINVAL; |
d6d808f3 | 435 | goto register_error; |
abc0446a | 436 | } |
edeb3137 MD |
437 | ret = try_module_get(event->desc->owner); |
438 | WARN_ON_ONCE(!ret); | |
d6d808f3 | 439 | break; |
7371f44c MD |
440 | case LTTNG_KERNEL_KRETPROBE: |
441 | { | |
a90917c3 | 442 | struct lttng_event *event_return; |
7371f44c MD |
443 | |
444 | /* kretprobe defines 2 events */ | |
445 | event_return = | |
446 | kmem_cache_zalloc(event_cache, GFP_KERNEL); | |
abc0446a | 447 | if (!event_return) { |
4cf0bf51 | 448 | ret = -ENOMEM; |
7371f44c | 449 | goto register_error; |
abc0446a | 450 | } |
7371f44c MD |
451 | event_return->chan = chan; |
452 | event_return->filter = filter; | |
453 | event_return->id = chan->free_event_id++; | |
454 | event_return->enabled = 1; | |
455 | event_return->instrumentation = event_param->instrumentation; | |
456 | /* | |
a90917c3 | 457 | * Populate lttng_event structure before kretprobe registration. |
7371f44c MD |
458 | */ |
459 | smp_wmb(); | |
460 | ret = lttng_kretprobes_register(event_param->name, | |
461 | event_param->u.kretprobe.symbol_name, | |
462 | event_param->u.kretprobe.offset, | |
463 | event_param->u.kretprobe.addr, | |
464 | event, event_return); | |
465 | if (ret) { | |
466 | kmem_cache_free(event_cache, event_return); | |
4cf0bf51 | 467 | ret = -EINVAL; |
7371f44c MD |
468 | goto register_error; |
469 | } | |
470 | /* Take 2 refs on the module: one per event. */ | |
471 | ret = try_module_get(event->desc->owner); | |
472 | WARN_ON_ONCE(!ret); | |
473 | ret = try_module_get(event->desc->owner); | |
474 | WARN_ON_ONCE(!ret); | |
a90917c3 | 475 | ret = _lttng_event_metadata_statedump(chan->session, chan, |
7371f44c | 476 | event_return); |
abc0446a | 477 | WARN_ON_ONCE(ret > 0); |
7371f44c MD |
478 | if (ret) { |
479 | kmem_cache_free(event_cache, event_return); | |
480 | module_put(event->desc->owner); | |
481 | module_put(event->desc->owner); | |
482 | goto statedump_error; | |
483 | } | |
484 | list_add(&event_return->list, &chan->session->events); | |
485 | break; | |
486 | } | |
ab2277d6 | 487 | case LTTNG_KERNEL_FUNCTION: |
30bdb6e4 | 488 | ret = lttng_ftrace_register(event_param->name, |
e0a7a7c4 MD |
489 | event_param->u.ftrace.symbol_name, |
490 | event); | |
abc0446a | 491 | if (ret) { |
e0a7a7c4 | 492 | goto register_error; |
abc0446a | 493 | } |
edeb3137 MD |
494 | ret = try_module_get(event->desc->owner); |
495 | WARN_ON_ONCE(!ret); | |
e0a7a7c4 | 496 | break; |
1ec65de1 | 497 | case LTTNG_KERNEL_NOOP: |
259b6cb3 | 498 | event->desc = internal_desc; |
abc0446a | 499 | if (!event->desc) { |
4cf0bf51 | 500 | ret = -EINVAL; |
259b6cb3 | 501 | goto register_error; |
abc0446a | 502 | } |
1ec65de1 | 503 | break; |
baf20995 MD |
504 | default: |
505 | WARN_ON_ONCE(1); | |
4cf0bf51 | 506 | ret = -EINVAL; |
df07930b | 507 | goto register_error; |
baf20995 | 508 | } |
a90917c3 | 509 | ret = _lttng_event_metadata_statedump(chan->session, chan, event); |
abc0446a MD |
510 | WARN_ON_ONCE(ret > 0); |
511 | if (ret) { | |
c099397a | 512 | goto statedump_error; |
abc0446a | 513 | } |
dda6a249 | 514 | list_add(&event->list, &chan->session->events); |
11b5a3c2 | 515 | mutex_unlock(&sessions_mutex); |
4e3c1b9b MD |
516 | return event; |
517 | ||
c099397a | 518 | statedump_error: |
259b6cb3 | 519 | /* If a statedump error occurs, events will not be readable. */ |
11b5a3c2 | 520 | register_error: |
11b5a3c2 | 521 | kmem_cache_free(event_cache, event); |
4e3c1b9b MD |
522 | cache_error: |
523 | exist: | |
e5382b6d | 524 | full: |
4e3c1b9b | 525 | mutex_unlock(&sessions_mutex); |
4cf0bf51 | 526 | return ERR_PTR(ret); |
4e3c1b9b MD |
527 | } |
528 | ||
529 | /* | |
530 | * Only used internally at session destruction. | |
531 | */ | |
a90917c3 | 532 | int _lttng_event_unregister(struct lttng_event *event) |
4e3c1b9b | 533 | { |
11b5a3c2 MD |
534 | int ret = -EINVAL; |
535 | ||
38d024ae | 536 | switch (event->instrumentation) { |
ab2277d6 | 537 | case LTTNG_KERNEL_TRACEPOINT: |
20591cf7 | 538 | ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname, |
85a9ca7f | 539 | event->desc->probe_callback, |
11b5a3c2 MD |
540 | event); |
541 | if (ret) | |
542 | return ret; | |
baf20995 | 543 | break; |
ab2277d6 | 544 | case LTTNG_KERNEL_KPROBE: |
f17701fb | 545 | lttng_kprobes_unregister(event); |
d6d808f3 MD |
546 | ret = 0; |
547 | break; | |
7371f44c MD |
548 | case LTTNG_KERNEL_KRETPROBE: |
549 | lttng_kretprobes_unregister(event); | |
550 | ret = 0; | |
551 | break; | |
ab2277d6 | 552 | case LTTNG_KERNEL_FUNCTION: |
e0a7a7c4 MD |
553 | lttng_ftrace_unregister(event); |
554 | ret = 0; | |
555 | break; | |
259b6cb3 MD |
556 | case LTTNG_KERNEL_NOOP: |
557 | ret = 0; | |
558 | break; | |
baf20995 MD |
559 | default: |
560 | WARN_ON_ONCE(1); | |
561 | } | |
dda6a249 MD |
562 | return ret; |
563 | } | |
564 | ||
565 | /* | |
566 | * Only used internally at session destruction. | |
567 | */ | |
be066e6c | 568 | static |
a90917c3 | 569 | void _lttng_event_destroy(struct lttng_event *event) |
dda6a249 | 570 | { |
edeb3137 MD |
571 | switch (event->instrumentation) { |
572 | case LTTNG_KERNEL_TRACEPOINT: | |
a90917c3 | 573 | lttng_event_put(event->desc); |
edeb3137 MD |
574 | break; |
575 | case LTTNG_KERNEL_KPROBE: | |
576 | module_put(event->desc->owner); | |
577 | lttng_kprobes_destroy_private(event); | |
578 | break; | |
7371f44c MD |
579 | case LTTNG_KERNEL_KRETPROBE: |
580 | module_put(event->desc->owner); | |
581 | lttng_kretprobes_destroy_private(event); | |
582 | break; | |
edeb3137 MD |
583 | case LTTNG_KERNEL_FUNCTION: |
584 | module_put(event->desc->owner); | |
585 | lttng_ftrace_destroy_private(event); | |
586 | break; | |
259b6cb3 MD |
587 | case LTTNG_KERNEL_NOOP: |
588 | break; | |
edeb3137 MD |
589 | default: |
590 | WARN_ON_ONCE(1); | |
591 | } | |
dda6a249 | 592 | list_del(&event->list); |
8070f5c0 | 593 | lttng_destroy_context(event->ctx); |
11b5a3c2 | 594 | kmem_cache_free(event_cache, event); |
4e3c1b9b MD |
595 | } |
596 | ||
1ec3f75a | 597 | /* |
d83004aa JD |
598 | * Serialize at most one packet worth of metadata into a metadata |
599 | * channel. | |
9a0aa3b1 MD |
600 | * We grab the metadata cache mutex to get exclusive access to our metadata |
601 | * buffer and to the metadata cache. Exclusive access to the metadata buffer | |
602 | * allows us to do racy operations such as looking for remaining space left in | |
603 | * packet and write, since mutual exclusion protects us from concurrent writes. | |
604 | * Mutual exclusion on the metadata cache allow us to read the cache content | |
605 | * without racing against reallocation of the cache by updates. | |
35097f36 JD |
606 | * Returns the number of bytes written in the channel, 0 if no data |
607 | * was written and a negative value on error. | |
1ec3f75a | 608 | */ |
b3b8072b MD |
609 | int lttng_metadata_output_channel(struct lttng_metadata_stream *stream, |
610 | struct channel *chan) | |
d83004aa JD |
611 | { |
612 | struct lib_ring_buffer_ctx ctx; | |
613 | int ret = 0; | |
614 | size_t len, reserve_len; | |
615 | ||
f613e3e6 | 616 | /* |
9a0aa3b1 MD |
617 | * Ensure we support mutiple get_next / put sequences followed by |
618 | * put_next. The metadata cache lock protects reading the metadata | |
619 | * cache. It can indeed be read concurrently by "get_next_subbuf" and | |
620 | * "flush" operations on the buffer invoked by different processes. | |
621 | * Moreover, since the metadata cache memory can be reallocated, we | |
622 | * need to have exclusive access against updates even though we only | |
623 | * read it. | |
f613e3e6 | 624 | */ |
9a0aa3b1 | 625 | mutex_lock(&stream->metadata_cache->lock); |
f613e3e6 MD |
626 | WARN_ON(stream->metadata_in < stream->metadata_out); |
627 | if (stream->metadata_in != stream->metadata_out) | |
de23d59d | 628 | goto end; |
f613e3e6 | 629 | |
d83004aa | 630 | len = stream->metadata_cache->metadata_written - |
f613e3e6 | 631 | stream->metadata_in; |
9de2c215 | 632 | if (!len) |
de23d59d | 633 | goto end; |
d83004aa | 634 | reserve_len = min_t(size_t, |
b3b8072b | 635 | stream->transport->ops.packet_avail_size(chan), |
d83004aa | 636 | len); |
b3b8072b | 637 | lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len, |
d83004aa JD |
638 | sizeof(char), -1); |
639 | /* | |
640 | * If reservation failed, return an error to the caller. | |
641 | */ | |
b3b8072b | 642 | ret = stream->transport->ops.event_reserve(&ctx, 0); |
d83004aa JD |
643 | if (ret != 0) { |
644 | printk(KERN_WARNING "LTTng: Metadata event reservation failed\n"); | |
645 | goto end; | |
646 | } | |
b3b8072b | 647 | stream->transport->ops.event_write(&ctx, |
f613e3e6 | 648 | stream->metadata_cache->data + stream->metadata_in, |
d83004aa | 649 | reserve_len); |
b3b8072b | 650 | stream->transport->ops.event_commit(&ctx); |
f613e3e6 | 651 | stream->metadata_in += reserve_len; |
d83004aa JD |
652 | ret = reserve_len; |
653 | ||
654 | end: | |
9a0aa3b1 | 655 | mutex_unlock(&stream->metadata_cache->lock); |
d83004aa JD |
656 | return ret; |
657 | } | |
658 | ||
659 | /* | |
660 | * Write the metadata to the metadata cache. | |
661 | * Must be called with sessions_mutex held. | |
9a0aa3b1 MD |
662 | * The metadata cache lock protects us from concurrent read access from |
663 | * thread outputting metadata content to ring buffer. | |
d83004aa | 664 | */ |
a90917c3 | 665 | int lttng_metadata_printf(struct lttng_session *session, |
c099397a MD |
666 | const char *fmt, ...) |
667 | { | |
c099397a | 668 | char *str; |
d83004aa | 669 | size_t len; |
c099397a | 670 | va_list ap; |
d83004aa | 671 | struct lttng_metadata_stream *stream; |
c099397a MD |
672 | |
673 | WARN_ON_ONCE(!ACCESS_ONCE(session->active)); | |
674 | ||
675 | va_start(ap, fmt); | |
676 | str = kvasprintf(GFP_KERNEL, fmt, ap); | |
677 | va_end(ap); | |
678 | if (!str) | |
679 | return -ENOMEM; | |
680 | ||
1ec3f75a | 681 | len = strlen(str); |
9a0aa3b1 | 682 | mutex_lock(&session->metadata_cache->lock); |
d83004aa JD |
683 | if (session->metadata_cache->metadata_written + len > |
684 | session->metadata_cache->cache_alloc) { | |
685 | char *tmp_cache_realloc; | |
686 | unsigned int tmp_cache_alloc_size; | |
687 | ||
688 | tmp_cache_alloc_size = max_t(unsigned int, | |
689 | session->metadata_cache->cache_alloc + len, | |
690 | session->metadata_cache->cache_alloc << 1); | |
30913c47 | 691 | tmp_cache_realloc = vzalloc(tmp_cache_alloc_size); |
d83004aa JD |
692 | if (!tmp_cache_realloc) |
693 | goto err; | |
30913c47 ML |
694 | if (session->metadata_cache->data) { |
695 | memcpy(tmp_cache_realloc, | |
696 | session->metadata_cache->data, | |
697 | session->metadata_cache->cache_alloc); | |
698 | vfree(session->metadata_cache->data); | |
699 | } | |
700 | ||
d83004aa JD |
701 | session->metadata_cache->cache_alloc = tmp_cache_alloc_size; |
702 | session->metadata_cache->data = tmp_cache_realloc; | |
c099397a | 703 | } |
d83004aa JD |
704 | memcpy(session->metadata_cache->data + |
705 | session->metadata_cache->metadata_written, | |
706 | str, len); | |
707 | session->metadata_cache->metadata_written += len; | |
9a0aa3b1 | 708 | mutex_unlock(&session->metadata_cache->lock); |
c099397a | 709 | kfree(str); |
d83004aa JD |
710 | |
711 | list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) | |
712 | wake_up_interruptible(&stream->read_wait); | |
713 | ||
714 | return 0; | |
715 | ||
716 | err: | |
9a0aa3b1 | 717 | mutex_unlock(&session->metadata_cache->lock); |
d83004aa JD |
718 | kfree(str); |
719 | return -ENOMEM; | |
c099397a MD |
720 | } |
721 | ||
d83004aa JD |
722 | /* |
723 | * Must be called with sessions_mutex held. | |
724 | */ | |
c099397a | 725 | static |
a90917c3 | 726 | int _lttng_field_statedump(struct lttng_session *session, |
8070f5c0 | 727 | const struct lttng_event_field *field) |
c099397a | 728 | { |
c099397a | 729 | int ret = 0; |
c099397a | 730 | |
8070f5c0 MD |
731 | switch (field->type.atype) { |
732 | case atype_integer: | |
733 | ret = lttng_metadata_printf(session, | |
244465ab | 734 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
8070f5c0 MD |
735 | field->type.u.basic.integer.size, |
736 | field->type.u.basic.integer.alignment, | |
737 | field->type.u.basic.integer.signedness, | |
738 | (field->type.u.basic.integer.encoding == lttng_encode_none) | |
739 | ? "none" | |
740 | : (field->type.u.basic.integer.encoding == lttng_encode_UTF8) | |
741 | ? "UTF8" | |
742 | : "ASCII", | |
743 | field->type.u.basic.integer.base, | |
c099397a | 744 | #ifdef __BIG_ENDIAN |
8070f5c0 | 745 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
c099397a | 746 | #else |
8070f5c0 | 747 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
c099397a | 748 | #endif |
8070f5c0 MD |
749 | field->name); |
750 | break; | |
751 | case atype_enum: | |
752 | ret = lttng_metadata_printf(session, | |
244465ab | 753 | " %s _%s;\n", |
8070f5c0 MD |
754 | field->type.u.basic.enumeration.name, |
755 | field->name); | |
756 | break; | |
757 | case atype_array: | |
758 | { | |
759 | const struct lttng_basic_type *elem_type; | |
760 | ||
761 | elem_type = &field->type.u.array.elem_type; | |
762 | ret = lttng_metadata_printf(session, | |
244465ab | 763 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
8070f5c0 MD |
764 | elem_type->u.basic.integer.size, |
765 | elem_type->u.basic.integer.alignment, | |
766 | elem_type->u.basic.integer.signedness, | |
767 | (elem_type->u.basic.integer.encoding == lttng_encode_none) | |
768 | ? "none" | |
769 | : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8) | |
770 | ? "UTF8" | |
771 | : "ASCII", | |
772 | elem_type->u.basic.integer.base, | |
8fa75f34 | 773 | #ifdef __BIG_ENDIAN |
8070f5c0 | 774 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
8fa75f34 | 775 | #else |
8070f5c0 | 776 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
8fa75f34 | 777 | #endif |
8070f5c0 MD |
778 | field->name, field->type.u.array.length); |
779 | break; | |
780 | } | |
781 | case atype_sequence: | |
782 | { | |
783 | const struct lttng_basic_type *elem_type; | |
784 | const struct lttng_basic_type *length_type; | |
785 | ||
786 | elem_type = &field->type.u.sequence.elem_type; | |
787 | length_type = &field->type.u.sequence.length_type; | |
788 | ret = lttng_metadata_printf(session, | |
789 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", | |
8070f5c0 | 790 | length_type->u.basic.integer.size, |
32257272 | 791 | (unsigned int) length_type->u.basic.integer.alignment, |
8070f5c0 MD |
792 | length_type->u.basic.integer.signedness, |
793 | (length_type->u.basic.integer.encoding == lttng_encode_none) | |
794 | ? "none" | |
0635cdd4 | 795 | : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8) |
8070f5c0 | 796 | ? "UTF8" |
0635cdd4 | 797 | : "ASCII"), |
8070f5c0 | 798 | length_type->u.basic.integer.base, |
e0a7a7c4 | 799 | #ifdef __BIG_ENDIAN |
8070f5c0 | 800 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
e0a7a7c4 | 801 | #else |
0635cdd4 | 802 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
e0a7a7c4 | 803 | #endif |
27d2368e MD |
804 | field->name); |
805 | if (ret) | |
806 | return ret; | |
807 | ||
808 | ret = lttng_metadata_printf(session, | |
244465ab | 809 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
8070f5c0 | 810 | elem_type->u.basic.integer.size, |
32257272 | 811 | (unsigned int) elem_type->u.basic.integer.alignment, |
8070f5c0 MD |
812 | elem_type->u.basic.integer.signedness, |
813 | (elem_type->u.basic.integer.encoding == lttng_encode_none) | |
814 | ? "none" | |
0635cdd4 | 815 | : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
8070f5c0 | 816 | ? "UTF8" |
0635cdd4 | 817 | : "ASCII"), |
8070f5c0 | 818 | elem_type->u.basic.integer.base, |
8fa75f34 | 819 | #ifdef __BIG_ENDIAN |
8070f5c0 | 820 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
8fa75f34 | 821 | #else |
8070f5c0 | 822 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
8fa75f34 | 823 | #endif |
8070f5c0 | 824 | field->name, |
27d2368e | 825 | field->name); |
8070f5c0 MD |
826 | break; |
827 | } | |
c099397a | 828 | |
8070f5c0 MD |
829 | case atype_string: |
830 | /* Default encoding is UTF8 */ | |
831 | ret = lttng_metadata_printf(session, | |
244465ab | 832 | " string%s _%s;\n", |
8070f5c0 MD |
833 | field->type.u.basic.string.encoding == lttng_encode_ASCII ? |
834 | " { encoding = ASCII; }" : "", | |
835 | field->name); | |
836 | break; | |
837 | default: | |
838 | WARN_ON_ONCE(1); | |
839 | return -EINVAL; | |
840 | } | |
841 | return ret; | |
842 | } | |
843 | ||
844 | static | |
a90917c3 | 845 | int _lttng_context_metadata_statedump(struct lttng_session *session, |
8070f5c0 MD |
846 | struct lttng_ctx *ctx) |
847 | { | |
848 | int ret = 0; | |
849 | int i; | |
850 | ||
851 | if (!ctx) | |
852 | return 0; | |
853 | for (i = 0; i < ctx->nr_fields; i++) { | |
854 | const struct lttng_ctx_field *field = &ctx->fields[i]; | |
855 | ||
a90917c3 | 856 | ret = _lttng_field_statedump(session, &field->event_field); |
8070f5c0 MD |
857 | if (ret) |
858 | return ret; | |
859 | } | |
860 | return ret; | |
861 | } | |
862 | ||
863 | static | |
a90917c3 MD |
864 | int _lttng_fields_metadata_statedump(struct lttng_session *session, |
865 | struct lttng_event *event) | |
8070f5c0 MD |
866 | { |
867 | const struct lttng_event_desc *desc = event->desc; | |
868 | int ret = 0; | |
869 | int i; | |
870 | ||
871 | for (i = 0; i < desc->nr_fields; i++) { | |
872 | const struct lttng_event_field *field = &desc->fields[i]; | |
873 | ||
a90917c3 | 874 | ret = _lttng_field_statedump(session, field); |
8070f5c0 MD |
875 | if (ret) |
876 | return ret; | |
c099397a MD |
877 | } |
878 | return ret; | |
879 | } | |
880 | ||
d83004aa JD |
881 | /* |
882 | * Must be called with sessions_mutex held. | |
883 | */ | |
c099397a | 884 | static |
a90917c3 MD |
885 | int _lttng_event_metadata_statedump(struct lttng_session *session, |
886 | struct lttng_channel *chan, | |
887 | struct lttng_event *event) | |
c099397a MD |
888 | { |
889 | int ret = 0; | |
890 | ||
891 | if (event->metadata_dumped || !ACCESS_ONCE(session->active)) | |
892 | return 0; | |
d83004aa | 893 | if (chan->channel_type == METADATA_CHANNEL) |
c099397a MD |
894 | return 0; |
895 | ||
896 | ret = lttng_metadata_printf(session, | |
897 | "event {\n" | |
ae734547 | 898 | " name = \"%s\";\n" |
c099397a | 899 | " id = %u;\n" |
b9074a1b | 900 | " stream_id = %u;\n", |
c099397a MD |
901 | event->desc->name, |
902 | event->id, | |
903 | event->chan->id); | |
904 | if (ret) | |
905 | goto end; | |
906 | ||
b9074a1b MD |
907 | if (event->ctx) { |
908 | ret = lttng_metadata_printf(session, | |
909 | " context := struct {\n"); | |
910 | if (ret) | |
911 | goto end; | |
912 | } | |
a90917c3 | 913 | ret = _lttng_context_metadata_statedump(session, event->ctx); |
8070f5c0 MD |
914 | if (ret) |
915 | goto end; | |
b9074a1b MD |
916 | if (event->ctx) { |
917 | ret = lttng_metadata_printf(session, | |
918 | " };\n"); | |
919 | if (ret) | |
920 | goto end; | |
921 | } | |
8070f5c0 MD |
922 | |
923 | ret = lttng_metadata_printf(session, | |
8070f5c0 MD |
924 | " fields := struct {\n" |
925 | ); | |
926 | if (ret) | |
927 | goto end; | |
928 | ||
a90917c3 | 929 | ret = _lttng_fields_metadata_statedump(session, event); |
c099397a MD |
930 | if (ret) |
931 | goto end; | |
932 | ||
933 | /* | |
934 | * LTTng space reservation can only reserve multiples of the | |
935 | * byte size. | |
936 | */ | |
937 | ret = lttng_metadata_printf(session, | |
9115fbdc MD |
938 | " };\n" |
939 | "};\n\n"); | |
c099397a MD |
940 | if (ret) |
941 | goto end; | |
942 | ||
c099397a MD |
943 | event->metadata_dumped = 1; |
944 | end: | |
945 | return ret; | |
946 | ||
947 | } | |
948 | ||
d83004aa JD |
949 | /* |
950 | * Must be called with sessions_mutex held. | |
951 | */ | |
c099397a | 952 | static |
a90917c3 MD |
953 | int _lttng_channel_metadata_statedump(struct lttng_session *session, |
954 | struct lttng_channel *chan) | |
c099397a MD |
955 | { |
956 | int ret = 0; | |
957 | ||
958 | if (chan->metadata_dumped || !ACCESS_ONCE(session->active)) | |
959 | return 0; | |
d83004aa JD |
960 | |
961 | if (chan->channel_type == METADATA_CHANNEL) | |
c099397a MD |
962 | return 0; |
963 | ||
964 | WARN_ON_ONCE(!chan->header_type); | |
965 | ret = lttng_metadata_printf(session, | |
966 | "stream {\n" | |
967 | " id = %u;\n" | |
9115fbdc | 968 | " event.header := %s;\n" |
b9074a1b | 969 | " packet.context := struct packet_context;\n", |
c099397a MD |
970 | chan->id, |
971 | chan->header_type == 1 ? "struct event_header_compact" : | |
972 | "struct event_header_large"); | |
973 | if (ret) | |
974 | goto end; | |
975 | ||
b9074a1b MD |
976 | if (chan->ctx) { |
977 | ret = lttng_metadata_printf(session, | |
978 | " event.context := struct {\n"); | |
979 | if (ret) | |
980 | goto end; | |
981 | } | |
a90917c3 | 982 | ret = _lttng_context_metadata_statedump(session, chan->ctx); |
8070f5c0 MD |
983 | if (ret) |
984 | goto end; | |
b9074a1b MD |
985 | if (chan->ctx) { |
986 | ret = lttng_metadata_printf(session, | |
987 | " };\n"); | |
988 | if (ret) | |
989 | goto end; | |
990 | } | |
8070f5c0 MD |
991 | |
992 | ret = lttng_metadata_printf(session, | |
b9074a1b | 993 | "};\n\n"); |
8070f5c0 | 994 | |
c099397a MD |
995 | chan->metadata_dumped = 1; |
996 | end: | |
997 | return ret; | |
998 | } | |
999 | ||
d83004aa JD |
1000 | /* |
1001 | * Must be called with sessions_mutex held. | |
1002 | */ | |
9115fbdc | 1003 | static |
a90917c3 | 1004 | int _lttng_stream_packet_context_declare(struct lttng_session *session) |
9115fbdc MD |
1005 | { |
1006 | return lttng_metadata_printf(session, | |
1007 | "struct packet_context {\n" | |
a3ccff4f MD |
1008 | " uint64_clock_monotonic_t timestamp_begin;\n" |
1009 | " uint64_clock_monotonic_t timestamp_end;\n" | |
576ca06a MD |
1010 | " uint64_t content_size;\n" |
1011 | " uint64_t packet_size;\n" | |
a9afe705 | 1012 | " unsigned long events_discarded;\n" |
9115fbdc | 1013 | " uint32_t cpu_id;\n" |
c6dfdf6f | 1014 | "};\n\n" |
9115fbdc MD |
1015 | ); |
1016 | } | |
1017 | ||
1018 | /* | |
1019 | * Compact header: | |
1020 | * id: range: 0 - 30. | |
1021 | * id 31 is reserved to indicate an extended header. | |
1022 | * | |
1023 | * Large header: | |
1024 | * id: range: 0 - 65534. | |
1025 | * id 65535 is reserved to indicate an extended header. | |
d83004aa JD |
1026 | * |
1027 | * Must be called with sessions_mutex held. | |
9115fbdc MD |
1028 | */ |
1029 | static | |
a90917c3 | 1030 | int _lttng_event_header_declare(struct lttng_session *session) |
9115fbdc MD |
1031 | { |
1032 | return lttng_metadata_printf(session, | |
1033 | "struct event_header_compact {\n" | |
1034 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" | |
1035 | " variant <id> {\n" | |
1036 | " struct {\n" | |
a3ccff4f | 1037 | " uint27_clock_monotonic_t timestamp;\n" |
9115fbdc MD |
1038 | " } compact;\n" |
1039 | " struct {\n" | |
1040 | " uint32_t id;\n" | |
a3ccff4f | 1041 | " uint64_clock_monotonic_t timestamp;\n" |
9115fbdc MD |
1042 | " } extended;\n" |
1043 | " } v;\n" | |
1044 | "} align(%u);\n" | |
1045 | "\n" | |
1046 | "struct event_header_large {\n" | |
1047 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" | |
1048 | " variant <id> {\n" | |
1049 | " struct {\n" | |
a3ccff4f | 1050 | " uint32_clock_monotonic_t timestamp;\n" |
9115fbdc MD |
1051 | " } compact;\n" |
1052 | " struct {\n" | |
1053 | " uint32_t id;\n" | |
a3ccff4f | 1054 | " uint64_clock_monotonic_t timestamp;\n" |
9115fbdc MD |
1055 | " } extended;\n" |
1056 | " } v;\n" | |
1057 | "} align(%u);\n\n", | |
a90917c3 MD |
1058 | lttng_alignof(uint32_t) * CHAR_BIT, |
1059 | lttng_alignof(uint16_t) * CHAR_BIT | |
9115fbdc MD |
1060 | ); |
1061 | } | |
1062 | ||
a3ccff4f MD |
1063 | /* |
1064 | * Approximation of NTP time of day to clock monotonic correlation, | |
1065 | * taken at start of trace. | |
1066 | * Yes, this is only an approximation. Yes, we can (and will) do better | |
1067 | * in future versions. | |
1068 | */ | |
1069 | static | |
1070 | uint64_t measure_clock_offset(void) | |
1071 | { | |
1072 | uint64_t offset, monotonic[2], realtime; | |
1073 | struct timespec rts = { 0, 0 }; | |
1074 | unsigned long flags; | |
1075 | ||
1076 | /* Disable interrupts to increase correlation precision. */ | |
1077 | local_irq_save(flags); | |
1078 | monotonic[0] = trace_clock_read64(); | |
1079 | getnstimeofday(&rts); | |
1080 | monotonic[1] = trace_clock_read64(); | |
1081 | local_irq_restore(flags); | |
1082 | ||
1083 | offset = (monotonic[0] + monotonic[1]) >> 1; | |
57be518d | 1084 | realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC; |
a3ccff4f MD |
1085 | realtime += rts.tv_nsec; |
1086 | offset = realtime - offset; | |
1087 | return offset; | |
1088 | } | |
1089 | ||
c099397a MD |
1090 | /* |
1091 | * Output metadata into this session's metadata buffers. | |
d83004aa | 1092 | * Must be called with sessions_mutex held. |
c099397a MD |
1093 | */ |
1094 | static | |
a90917c3 | 1095 | int _lttng_session_metadata_statedump(struct lttng_session *session) |
c099397a | 1096 | { |
30bdb6e4 | 1097 | unsigned char *uuid_c = session->uuid.b; |
a82c63f1 | 1098 | unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN]; |
a90917c3 MD |
1099 | struct lttng_channel *chan; |
1100 | struct lttng_event *event; | |
c099397a MD |
1101 | int ret = 0; |
1102 | ||
1103 | if (!ACCESS_ONCE(session->active)) | |
1104 | return 0; | |
1105 | if (session->metadata_dumped) | |
1106 | goto skip_session; | |
c099397a | 1107 | |
d793d5e1 | 1108 | snprintf(uuid_s, sizeof(uuid_s), |
30bdb6e4 MD |
1109 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
1110 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], | |
1111 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], | |
1112 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], | |
1113 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); | |
d793d5e1 MD |
1114 | |
1115 | ret = lttng_metadata_printf(session, | |
9115fbdc MD |
1116 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
1117 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" | |
1118 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" | |
1119 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" | |
a9afe705 | 1120 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" |
9115fbdc MD |
1121 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" |
1122 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" | |
d793d5e1 MD |
1123 | "\n" |
1124 | "trace {\n" | |
1125 | " major = %u;\n" | |
1126 | " minor = %u;\n" | |
30bdb6e4 | 1127 | " uuid = \"%s\";\n" |
d793d5e1 MD |
1128 | " byte_order = %s;\n" |
1129 | " packet.header := struct {\n" | |
1130 | " uint32_t magic;\n" | |
1ec3f75a | 1131 | " uint8_t uuid[16];\n" |
d793d5e1 | 1132 | " uint32_t stream_id;\n" |
0eb25f58 | 1133 | " };\n" |
d793d5e1 | 1134 | "};\n\n", |
a90917c3 MD |
1135 | lttng_alignof(uint8_t) * CHAR_BIT, |
1136 | lttng_alignof(uint16_t) * CHAR_BIT, | |
1137 | lttng_alignof(uint32_t) * CHAR_BIT, | |
1138 | lttng_alignof(uint64_t) * CHAR_BIT, | |
a9afe705 MD |
1139 | sizeof(unsigned long) * CHAR_BIT, |
1140 | lttng_alignof(unsigned long) * CHAR_BIT, | |
c6c9e10f MD |
1141 | CTF_SPEC_MAJOR, |
1142 | CTF_SPEC_MINOR, | |
d793d5e1 MD |
1143 | uuid_s, |
1144 | #ifdef __BIG_ENDIAN | |
1145 | "be" | |
1146 | #else | |
1147 | "le" | |
1148 | #endif | |
1149 | ); | |
1150 | if (ret) | |
1151 | goto end; | |
1152 | ||
99dc9597 MD |
1153 | ret = lttng_metadata_printf(session, |
1154 | "env {\n" | |
a6058143 | 1155 | " hostname = \"%s\";\n" |
c6c9e10f | 1156 | " domain = \"kernel\";\n" |
99dc9597 | 1157 | " sysname = \"%s\";\n" |
c6c9e10f MD |
1158 | " kernel_release = \"%s\";\n" |
1159 | " kernel_version = \"%s\";\n" | |
1160 | " tracer_name = \"lttng-modules\";\n" | |
1161 | " tracer_major = %d;\n" | |
1162 | " tracer_minor = %d;\n" | |
1163 | " tracer_patchlevel = %d;\n" | |
99dc9597 | 1164 | "};\n\n", |
3d0d43db | 1165 | current->nsproxy->uts_ns->name.nodename, |
99dc9597 MD |
1166 | utsname()->sysname, |
1167 | utsname()->release, | |
c6c9e10f MD |
1168 | utsname()->version, |
1169 | LTTNG_MODULES_MAJOR_VERSION, | |
1170 | LTTNG_MODULES_MINOR_VERSION, | |
1171 | LTTNG_MODULES_PATCHLEVEL_VERSION | |
99dc9597 MD |
1172 | ); |
1173 | if (ret) | |
1174 | goto end; | |
1175 | ||
a3ccff4f MD |
1176 | ret = lttng_metadata_printf(session, |
1177 | "clock {\n" | |
a82c63f1 MD |
1178 | " name = %s;\n", |
1179 | "monotonic" | |
1180 | ); | |
1181 | if (ret) | |
1182 | goto end; | |
1183 | ||
1184 | if (!trace_clock_uuid(clock_uuid_s)) { | |
1185 | ret = lttng_metadata_printf(session, | |
7c27cb17 | 1186 | " uuid = \"%s\";\n", |
a82c63f1 MD |
1187 | clock_uuid_s |
1188 | ); | |
1189 | if (ret) | |
1190 | goto end; | |
1191 | } | |
1192 | ||
1193 | ret = lttng_metadata_printf(session, | |
a3ccff4f MD |
1194 | " description = \"Monotonic Clock\";\n" |
1195 | " freq = %llu; /* Frequency, in Hz */\n" | |
1196 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" | |
1197 | " offset = %llu;\n" | |
1198 | "};\n\n", | |
a3ccff4f MD |
1199 | (unsigned long long) trace_clock_freq(), |
1200 | (unsigned long long) measure_clock_offset() | |
1201 | ); | |
1202 | if (ret) | |
1203 | goto end; | |
1204 | ||
1205 | ret = lttng_metadata_printf(session, | |
1206 | "typealias integer {\n" | |
1207 | " size = 27; align = 1; signed = false;\n" | |
1208 | " map = clock.monotonic.value;\n" | |
1209 | "} := uint27_clock_monotonic_t;\n" | |
1210 | "\n" | |
1211 | "typealias integer {\n" | |
1212 | " size = 32; align = %u; signed = false;\n" | |
1213 | " map = clock.monotonic.value;\n" | |
1214 | "} := uint32_clock_monotonic_t;\n" | |
1215 | "\n" | |
1216 | "typealias integer {\n" | |
1217 | " size = 64; align = %u; signed = false;\n" | |
1218 | " map = clock.monotonic.value;\n" | |
1219 | "} := uint64_clock_monotonic_t;\n\n", | |
1220 | lttng_alignof(uint32_t) * CHAR_BIT, | |
1221 | lttng_alignof(uint64_t) * CHAR_BIT | |
1222 | ); | |
1223 | if (ret) | |
1224 | goto end; | |
1225 | ||
a90917c3 | 1226 | ret = _lttng_stream_packet_context_declare(session); |
9115fbdc MD |
1227 | if (ret) |
1228 | goto end; | |
1229 | ||
a90917c3 | 1230 | ret = _lttng_event_header_declare(session); |
9115fbdc MD |
1231 | if (ret) |
1232 | goto end; | |
1233 | ||
c099397a MD |
1234 | skip_session: |
1235 | list_for_each_entry(chan, &session->chan, list) { | |
a90917c3 | 1236 | ret = _lttng_channel_metadata_statedump(session, chan); |
c099397a MD |
1237 | if (ret) |
1238 | goto end; | |
1239 | } | |
1240 | ||
1241 | list_for_each_entry(event, &session->events, list) { | |
a90917c3 | 1242 | ret = _lttng_event_metadata_statedump(session, event->chan, event); |
c099397a MD |
1243 | if (ret) |
1244 | goto end; | |
1245 | } | |
1246 | session->metadata_dumped = 1; | |
1247 | end: | |
1248 | return ret; | |
1249 | } | |
1250 | ||
c0e31d2e | 1251 | /** |
a90917c3 | 1252 | * lttng_transport_register - LTT transport registration |
c0e31d2e MD |
1253 | * @transport: transport structure |
1254 | * | |
1255 | * Registers a transport which can be used as output to extract the data out of | |
1256 | * LTTng. The module calling this registration function must ensure that no | |
1257 | * trap-inducing code will be executed by the transport functions. E.g. | |
1258 | * vmalloc_sync_all() must be called between a vmalloc and the moment the memory | |
1259 | * is made visible to the transport function. This registration acts as a | |
1260 | * vmalloc_sync_all. Therefore, only if the module allocates virtual memory | |
1261 | * after its registration must it synchronize the TLBs. | |
1262 | */ | |
a90917c3 | 1263 | void lttng_transport_register(struct lttng_transport *transport) |
c0e31d2e MD |
1264 | { |
1265 | /* | |
1266 | * Make sure no page fault can be triggered by the module about to be | |
1267 | * registered. We deal with this here so we don't have to call | |
1268 | * vmalloc_sync_all() in each module's init. | |
1269 | */ | |
6d2a620c | 1270 | wrapper_vmalloc_sync_all(); |
c0e31d2e MD |
1271 | |
1272 | mutex_lock(&sessions_mutex); | |
a90917c3 | 1273 | list_add_tail(&transport->node, <tng_transport_list); |
c0e31d2e MD |
1274 | mutex_unlock(&sessions_mutex); |
1275 | } | |
a90917c3 | 1276 | EXPORT_SYMBOL_GPL(lttng_transport_register); |
c0e31d2e MD |
1277 | |
1278 | /** | |
a90917c3 | 1279 | * lttng_transport_unregister - LTT transport unregistration |
c0e31d2e MD |
1280 | * @transport: transport structure |
1281 | */ | |
a90917c3 | 1282 | void lttng_transport_unregister(struct lttng_transport *transport) |
c0e31d2e MD |
1283 | { |
1284 | mutex_lock(&sessions_mutex); | |
1285 | list_del(&transport->node); | |
1286 | mutex_unlock(&sessions_mutex); | |
1287 | } | |
a90917c3 | 1288 | EXPORT_SYMBOL_GPL(lttng_transport_unregister); |
c0e31d2e | 1289 | |
a90917c3 | 1290 | static int __init lttng_events_init(void) |
4e3c1b9b | 1291 | { |
1c25284c MD |
1292 | int ret; |
1293 | ||
453b2495 JD |
1294 | ret = wrapper_lttng_fixup_sig(THIS_MODULE); |
1295 | if (ret) | |
1296 | return ret; | |
165eee70 MD |
1297 | ret = wrapper_get_pfnblock_flags_mask_init(); |
1298 | if (ret) | |
1299 | return ret; | |
20591cf7 MD |
1300 | ret = lttng_tracepoint_init(); |
1301 | if (ret) | |
1302 | return ret; | |
a90917c3 | 1303 | event_cache = KMEM_CACHE(lttng_event, 0); |
20591cf7 MD |
1304 | if (!event_cache) { |
1305 | ret = -ENOMEM; | |
1306 | goto error_kmem; | |
1307 | } | |
80996790 | 1308 | ret = lttng_abi_init(); |
02119ee5 MD |
1309 | if (ret) |
1310 | goto error_abi; | |
0c956676 MD |
1311 | ret = lttng_logger_init(); |
1312 | if (ret) | |
1313 | goto error_logger; | |
4e3c1b9b | 1314 | return 0; |
0c956676 MD |
1315 | |
1316 | error_logger: | |
1317 | lttng_abi_exit(); | |
02119ee5 | 1318 | error_abi: |
1c25284c | 1319 | kmem_cache_destroy(event_cache); |
20591cf7 MD |
1320 | error_kmem: |
1321 | lttng_tracepoint_exit(); | |
1c25284c | 1322 | return ret; |
4e3c1b9b MD |
1323 | } |
1324 | ||
a90917c3 | 1325 | module_init(lttng_events_init); |
11b5a3c2 | 1326 | |
a90917c3 | 1327 | static void __exit lttng_events_exit(void) |
4e3c1b9b | 1328 | { |
a90917c3 | 1329 | struct lttng_session *session, *tmpsession; |
92e94819 | 1330 | |
0c956676 | 1331 | lttng_logger_exit(); |
80996790 | 1332 | lttng_abi_exit(); |
92e94819 | 1333 | list_for_each_entry_safe(session, tmpsession, &sessions, list) |
a90917c3 | 1334 | lttng_session_destroy(session); |
11b5a3c2 | 1335 | kmem_cache_destroy(event_cache); |
20591cf7 | 1336 | lttng_tracepoint_exit(); |
4e3c1b9b | 1337 | } |
92e94819 | 1338 | |
a90917c3 | 1339 | module_exit(lttng_events_exit); |
11b5a3c2 | 1340 | |
92e94819 MD |
1341 | MODULE_LICENSE("GPL and additional rights"); |
1342 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); | |
1343 | MODULE_DESCRIPTION("LTTng Events"); | |
9a9973ef MD |
1344 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
1345 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." | |
309370e2 MD |
1346 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) |
1347 | LTTNG_MODULES_EXTRAVERSION); |