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