Commit | Line | Data |
---|---|---|
4e3c1b9b MD |
1 | /* |
2 | * ltt-events.c | |
3 | * | |
4 | * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
6 | * Holds LTTng per-session event registry. | |
17baffe2 MD |
7 | * |
8 | * Dual LGPL v2.1/GPL v2 license. | |
4e3c1b9b MD |
9 | */ |
10 | ||
11 | #include <linux/module.h> | |
c0e31d2e MD |
12 | #include <linux/list.h> |
13 | #include <linux/mutex.h> | |
14 | #include <linux/sched.h> | |
11b5a3c2 | 15 | #include <linux/slab.h> |
c099397a | 16 | #include <linux/jiffies.h> |
d793d5e1 | 17 | #include <linux/uuid.h> |
b13f3ebe | 18 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
4e3c1b9b | 19 | #include "ltt-events.h" |
c099397a | 20 | #include "ltt-tracer.h" |
4e3c1b9b MD |
21 | |
22 | static LIST_HEAD(sessions); | |
c0e31d2e | 23 | static LIST_HEAD(ltt_transport_list); |
4e3c1b9b MD |
24 | static DEFINE_MUTEX(sessions_mutex); |
25 | static struct kmem_cache *event_cache; | |
26 | ||
be066e6c | 27 | static void _ltt_event_destroy(struct ltt_event *event); |
aa7c23a9 | 28 | static void _ltt_channel_destroy(struct ltt_channel *chan); |
be066e6c | 29 | static int _ltt_event_unregister(struct ltt_event *event); |
c099397a MD |
30 | static |
31 | int _ltt_event_metadata_statedump(struct ltt_session *session, | |
32 | struct ltt_channel *chan, | |
33 | struct ltt_event *event); | |
34 | static | |
35 | int _ltt_session_metadata_statedump(struct ltt_session *session); | |
36 | ||
c099397a | 37 | void synchronize_trace(void) |
abcca994 MD |
38 | { |
39 | synchronize_sched(); | |
40 | #ifdef CONFIG_PREEMPT_RT | |
41 | synchronize_rcu(); | |
42 | #endif | |
43 | } | |
44 | ||
baf20995 | 45 | struct ltt_session *ltt_session_create(void) |
4e3c1b9b MD |
46 | { |
47 | struct ltt_session *session; | |
48 | ||
49 | mutex_lock(&sessions_mutex); | |
f3d01b96 | 50 | session = kzalloc(sizeof(struct ltt_session), GFP_KERNEL); |
4e3c1b9b MD |
51 | if (!session) |
52 | return NULL; | |
4e3c1b9b | 53 | INIT_LIST_HEAD(&session->chan); |
f3d01b96 | 54 | INIT_LIST_HEAD(&session->events); |
d793d5e1 | 55 | uuid_le_gen(&session->uuid); |
4e3c1b9b MD |
56 | list_add(&session->list, &sessions); |
57 | mutex_unlock(&sessions_mutex); | |
58 | return session; | |
4e3c1b9b MD |
59 | } |
60 | ||
11b5a3c2 | 61 | void ltt_session_destroy(struct ltt_session *session) |
4e3c1b9b MD |
62 | { |
63 | struct ltt_channel *chan, *tmpchan; | |
64 | struct ltt_event *event, *tmpevent; | |
dda6a249 | 65 | int ret; |
4e3c1b9b MD |
66 | |
67 | mutex_lock(&sessions_mutex); | |
52fc2e1f | 68 | ACCESS_ONCE(session->active) = 0; |
dda6a249 MD |
69 | list_for_each_entry(event, &session->events, list) { |
70 | ret = _ltt_event_unregister(event); | |
71 | WARN_ON(ret); | |
72 | } | |
abcca994 | 73 | synchronize_trace(); /* Wait for in-flight events to complete */ |
4e3c1b9b MD |
74 | list_for_each_entry_safe(event, tmpevent, &session->events, list) |
75 | _ltt_event_destroy(event); | |
76 | list_for_each_entry_safe(chan, tmpchan, &session->chan, list) | |
77 | _ltt_channel_destroy(chan); | |
78 | list_del(&session->list); | |
79 | mutex_unlock(&sessions_mutex); | |
80 | kfree(session); | |
81 | } | |
82 | ||
e64957da | 83 | int ltt_session_enable(struct ltt_session *session) |
c0e31d2e MD |
84 | { |
85 | int ret = 0; | |
c099397a | 86 | struct ltt_channel *chan; |
c0e31d2e MD |
87 | |
88 | mutex_lock(&sessions_mutex); | |
89 | if (session->active) { | |
90 | ret = -EBUSY; | |
91 | goto end; | |
92 | } | |
c099397a MD |
93 | |
94 | /* | |
95 | * Snapshot the number of events per channel to know the type of header | |
96 | * we need to use. | |
97 | */ | |
98 | list_for_each_entry(chan, &session->chan, list) { | |
99 | if (chan->header_type) | |
100 | continue; /* don't change it if session stop/restart */ | |
101 | if (chan->free_event_id < 31) | |
102 | chan->header_type = 1; /* compact */ | |
103 | else | |
104 | chan->header_type = 2; /* large */ | |
105 | } | |
106 | ||
52fc2e1f | 107 | ACCESS_ONCE(session->active) = 1; |
8070f5c0 | 108 | ACCESS_ONCE(session->been_active) = 1; |
c099397a | 109 | ret = _ltt_session_metadata_statedump(session); |
360f38ea | 110 | if (ret) |
c099397a | 111 | ACCESS_ONCE(session->active) = 0; |
c0e31d2e MD |
112 | end: |
113 | mutex_unlock(&sessions_mutex); | |
11b5a3c2 | 114 | return ret; |
c0e31d2e MD |
115 | } |
116 | ||
e64957da | 117 | int ltt_session_disable(struct ltt_session *session) |
c0e31d2e MD |
118 | { |
119 | int ret = 0; | |
120 | ||
121 | mutex_lock(&sessions_mutex); | |
122 | if (!session->active) { | |
123 | ret = -EBUSY; | |
124 | goto end; | |
125 | } | |
52fc2e1f | 126 | ACCESS_ONCE(session->active) = 0; |
c0e31d2e MD |
127 | end: |
128 | mutex_unlock(&sessions_mutex); | |
11b5a3c2 | 129 | return ret; |
c0e31d2e MD |
130 | } |
131 | ||
e64957da MD |
132 | int ltt_channel_enable(struct ltt_channel *channel) |
133 | { | |
134 | int old; | |
135 | ||
c6485006 MD |
136 | if (channel == channel->session->metadata) |
137 | return -EPERM; | |
e64957da MD |
138 | old = xchg(&channel->enabled, 1); |
139 | if (old) | |
140 | return -EEXIST; | |
141 | return 0; | |
142 | } | |
143 | ||
144 | int ltt_channel_disable(struct ltt_channel *channel) | |
145 | { | |
146 | int old; | |
147 | ||
c6485006 MD |
148 | if (channel == channel->session->metadata) |
149 | return -EPERM; | |
e64957da MD |
150 | old = xchg(&channel->enabled, 0); |
151 | if (!old) | |
152 | return -EEXIST; | |
153 | return 0; | |
154 | } | |
155 | ||
156 | int ltt_event_enable(struct ltt_event *event) | |
157 | { | |
158 | int old; | |
159 | ||
c6485006 MD |
160 | if (event->chan == event->chan->session->metadata) |
161 | return -EPERM; | |
e64957da MD |
162 | old = xchg(&event->enabled, 1); |
163 | if (old) | |
164 | return -EEXIST; | |
165 | return 0; | |
166 | } | |
167 | ||
168 | int ltt_event_disable(struct ltt_event *event) | |
169 | { | |
170 | int old; | |
171 | ||
c6485006 MD |
172 | if (event->chan == event->chan->session->metadata) |
173 | return -EPERM; | |
e64957da MD |
174 | old = xchg(&event->enabled, 0); |
175 | if (!old) | |
176 | return -EEXIST; | |
177 | return 0; | |
178 | } | |
179 | ||
c535175e | 180 | static struct ltt_transport *ltt_transport_find(const char *name) |
f3d01b96 MD |
181 | { |
182 | struct ltt_transport *transport; | |
183 | ||
184 | list_for_each_entry(transport, <t_transport_list, node) { | |
185 | if (!strcmp(transport->name, name)) | |
186 | return transport; | |
187 | } | |
188 | return NULL; | |
189 | } | |
190 | ||
baf20995 | 191 | struct ltt_channel *ltt_channel_create(struct ltt_session *session, |
5dbbdb43 MD |
192 | const char *transport_name, |
193 | void *buf_addr, | |
4e3c1b9b MD |
194 | size_t subbuf_size, size_t num_subbuf, |
195 | unsigned int switch_timer_interval, | |
196 | unsigned int read_timer_interval) | |
197 | { | |
198 | struct ltt_channel *chan; | |
f3d01b96 | 199 | struct ltt_transport *transport; |
4e3c1b9b MD |
200 | |
201 | mutex_lock(&sessions_mutex); | |
8070f5c0 | 202 | if (session->been_active) |
e5382b6d | 203 | goto active; /* Refuse to add channel to active session */ |
f3d01b96 MD |
204 | transport = ltt_transport_find(transport_name); |
205 | if (!transport) { | |
206 | printk(KERN_WARNING "LTTng transport %s not found\n", | |
207 | transport_name); | |
c0e31d2e | 208 | goto notransport; |
f3d01b96 | 209 | } |
f3d01b96 | 210 | chan = kzalloc(sizeof(struct ltt_channel), GFP_KERNEL); |
4e3c1b9b | 211 | if (!chan) |
c0e31d2e | 212 | goto nomem; |
4e3c1b9b | 213 | chan->session = session; |
05d32c64 | 214 | chan->id = session->free_chan_id++; |
125b4df4 MD |
215 | /* |
216 | * Note: the channel creation op already writes into the packet | |
217 | * headers. Therefore the "chan" information used as input | |
218 | * should be already accessible. | |
219 | */ | |
9115fbdc | 220 | chan->chan = transport->ops.channel_create("[lttng]", chan, buf_addr, |
11b5a3c2 MD |
221 | subbuf_size, num_subbuf, switch_timer_interval, |
222 | read_timer_interval); | |
f3d01b96 MD |
223 | if (!chan->chan) |
224 | goto create_error; | |
e64957da | 225 | chan->enabled = 1; |
11b5a3c2 | 226 | chan->ops = &transport->ops; |
4e3c1b9b MD |
227 | list_add(&chan->list, &session->chan); |
228 | mutex_unlock(&sessions_mutex); | |
229 | return chan; | |
230 | ||
f3d01b96 MD |
231 | create_error: |
232 | kfree(chan); | |
c0e31d2e MD |
233 | nomem: |
234 | notransport: | |
e5382b6d | 235 | active: |
4e3c1b9b MD |
236 | mutex_unlock(&sessions_mutex); |
237 | return NULL; | |
238 | } | |
239 | ||
240 | /* | |
241 | * Only used internally at session destruction. | |
242 | */ | |
aa7c23a9 | 243 | static |
11b5a3c2 | 244 | void _ltt_channel_destroy(struct ltt_channel *chan) |
4e3c1b9b | 245 | { |
11b5a3c2 | 246 | chan->ops->channel_destroy(chan->chan); |
4e3c1b9b | 247 | list_del(&chan->list); |
8070f5c0 | 248 | lttng_destroy_context(chan->ctx); |
4e3c1b9b MD |
249 | kfree(chan); |
250 | } | |
251 | ||
e5382b6d MD |
252 | /* |
253 | * Supports event creation while tracing session is active. | |
254 | */ | |
30bdb6e4 | 255 | struct ltt_event *ltt_event_create(struct ltt_channel *chan, |
d6d808f3 | 256 | struct lttng_kernel_event *event_param, |
85a9ca7f | 257 | void *filter) |
4e3c1b9b MD |
258 | { |
259 | struct ltt_event *event; | |
3d084699 | 260 | int ret; |
4e3c1b9b MD |
261 | |
262 | mutex_lock(&sessions_mutex); | |
e5382b6d MD |
263 | if (chan->free_event_id == -1UL) |
264 | goto full; | |
4e3c1b9b | 265 | /* |
dda6a249 MD |
266 | * This is O(n^2) (for each event, the loop is called at event |
267 | * creation). Might require a hash if we have lots of events. | |
4e3c1b9b MD |
268 | */ |
269 | list_for_each_entry(event, &chan->session->events, list) | |
30bdb6e4 | 270 | if (!strcmp(event->desc->name, event_param->name)) |
4e3c1b9b | 271 | goto exist; |
11b5a3c2 | 272 | event = kmem_cache_zalloc(event_cache, GFP_KERNEL); |
4e3c1b9b MD |
273 | if (!event) |
274 | goto cache_error; | |
4e3c1b9b MD |
275 | event->chan = chan; |
276 | event->filter = filter; | |
e5382b6d | 277 | event->id = chan->free_event_id++; |
e64957da | 278 | event->enabled = 1; |
d6d808f3 | 279 | event->instrumentation = event_param->instrumentation; |
e5382b6d MD |
280 | /* Populate ltt_event structure before tracepoint registration. */ |
281 | smp_wmb(); | |
d6d808f3 | 282 | switch (event_param->instrumentation) { |
ab2277d6 | 283 | case LTTNG_KERNEL_TRACEPOINT: |
30bdb6e4 | 284 | event->desc = ltt_event_get(event_param->name); |
d3dbe23c MD |
285 | if (!event->desc) |
286 | goto register_error; | |
30bdb6e4 | 287 | ret = tracepoint_probe_register(event_param->name, |
d3dbe23c MD |
288 | event->desc->probe_callback, |
289 | event); | |
11b5a3c2 MD |
290 | if (ret) |
291 | goto register_error; | |
baf20995 | 292 | break; |
ab2277d6 | 293 | case LTTNG_KERNEL_KPROBE: |
30bdb6e4 | 294 | ret = lttng_kprobes_register(event_param->name, |
f17701fb MD |
295 | event_param->u.kprobe.symbol_name, |
296 | event_param->u.kprobe.offset, | |
297 | event_param->u.kprobe.addr, | |
298 | event); | |
299 | if (ret) | |
d6d808f3 | 300 | goto register_error; |
edeb3137 MD |
301 | ret = try_module_get(event->desc->owner); |
302 | WARN_ON_ONCE(!ret); | |
d6d808f3 | 303 | break; |
ab2277d6 | 304 | case LTTNG_KERNEL_FUNCTION: |
30bdb6e4 | 305 | ret = lttng_ftrace_register(event_param->name, |
e0a7a7c4 MD |
306 | event_param->u.ftrace.symbol_name, |
307 | event); | |
308 | if (ret) | |
309 | goto register_error; | |
edeb3137 MD |
310 | ret = try_module_get(event->desc->owner); |
311 | WARN_ON_ONCE(!ret); | |
e0a7a7c4 | 312 | break; |
baf20995 MD |
313 | default: |
314 | WARN_ON_ONCE(1); | |
315 | } | |
c099397a MD |
316 | ret = _ltt_event_metadata_statedump(chan->session, chan, event); |
317 | if (ret) | |
318 | goto statedump_error; | |
dda6a249 | 319 | list_add(&event->list, &chan->session->events); |
11b5a3c2 | 320 | mutex_unlock(&sessions_mutex); |
4e3c1b9b MD |
321 | return event; |
322 | ||
c099397a | 323 | statedump_error: |
30bdb6e4 | 324 | WARN_ON_ONCE(tracepoint_probe_unregister(event_param->name, |
d3dbe23c MD |
325 | event->desc->probe_callback, |
326 | event)); | |
327 | ltt_event_put(event->desc); | |
11b5a3c2 | 328 | register_error: |
11b5a3c2 | 329 | kmem_cache_free(event_cache, event); |
4e3c1b9b MD |
330 | cache_error: |
331 | exist: | |
e5382b6d | 332 | full: |
4e3c1b9b MD |
333 | mutex_unlock(&sessions_mutex); |
334 | return NULL; | |
335 | } | |
336 | ||
337 | /* | |
338 | * Only used internally at session destruction. | |
339 | */ | |
dda6a249 | 340 | int _ltt_event_unregister(struct ltt_event *event) |
4e3c1b9b | 341 | { |
11b5a3c2 MD |
342 | int ret = -EINVAL; |
343 | ||
38d024ae | 344 | switch (event->instrumentation) { |
ab2277d6 | 345 | case LTTNG_KERNEL_TRACEPOINT: |
85a9ca7f MD |
346 | ret = tracepoint_probe_unregister(event->desc->name, |
347 | event->desc->probe_callback, | |
11b5a3c2 MD |
348 | event); |
349 | if (ret) | |
350 | return ret; | |
baf20995 | 351 | break; |
ab2277d6 | 352 | case LTTNG_KERNEL_KPROBE: |
f17701fb | 353 | lttng_kprobes_unregister(event); |
d6d808f3 MD |
354 | ret = 0; |
355 | break; | |
ab2277d6 | 356 | case LTTNG_KERNEL_FUNCTION: |
e0a7a7c4 MD |
357 | lttng_ftrace_unregister(event); |
358 | ret = 0; | |
359 | break; | |
baf20995 MD |
360 | default: |
361 | WARN_ON_ONCE(1); | |
362 | } | |
dda6a249 MD |
363 | return ret; |
364 | } | |
365 | ||
366 | /* | |
367 | * Only used internally at session destruction. | |
368 | */ | |
be066e6c | 369 | static |
dda6a249 MD |
370 | void _ltt_event_destroy(struct ltt_event *event) |
371 | { | |
edeb3137 MD |
372 | switch (event->instrumentation) { |
373 | case LTTNG_KERNEL_TRACEPOINT: | |
374 | ltt_event_put(event->desc); | |
375 | break; | |
376 | case LTTNG_KERNEL_KPROBE: | |
377 | module_put(event->desc->owner); | |
378 | lttng_kprobes_destroy_private(event); | |
379 | break; | |
380 | case LTTNG_KERNEL_FUNCTION: | |
381 | module_put(event->desc->owner); | |
382 | lttng_ftrace_destroy_private(event); | |
383 | break; | |
384 | default: | |
385 | WARN_ON_ONCE(1); | |
386 | } | |
dda6a249 | 387 | list_del(&event->list); |
8070f5c0 | 388 | lttng_destroy_context(event->ctx); |
11b5a3c2 | 389 | kmem_cache_free(event_cache, event); |
4e3c1b9b MD |
390 | } |
391 | ||
1ec3f75a MD |
392 | /* |
393 | * We have exclusive access to our metadata buffer (protected by the | |
394 | * sessions_mutex), so we can do racy operations such as looking for | |
395 | * remaining space left in packet and write, since mutual exclusion | |
396 | * protects us from concurrent writes. | |
397 | */ | |
c099397a MD |
398 | int lttng_metadata_printf(struct ltt_session *session, |
399 | const char *fmt, ...) | |
400 | { | |
401 | struct lib_ring_buffer_ctx ctx; | |
402 | struct ltt_channel *chan = session->metadata; | |
403 | char *str; | |
404 | int ret = 0, waitret; | |
1ec3f75a | 405 | size_t len, reserve_len, pos; |
c099397a MD |
406 | va_list ap; |
407 | ||
408 | WARN_ON_ONCE(!ACCESS_ONCE(session->active)); | |
409 | ||
410 | va_start(ap, fmt); | |
411 | str = kvasprintf(GFP_KERNEL, fmt, ap); | |
412 | va_end(ap); | |
413 | if (!str) | |
414 | return -ENOMEM; | |
415 | ||
1ec3f75a MD |
416 | len = strlen(str); |
417 | pos = 0; | |
418 | ||
419 | for (pos = 0; pos < len; pos += reserve_len) { | |
420 | reserve_len = min_t(size_t, | |
421 | chan->ops->packet_avail_size(chan->chan), | |
422 | len - pos); | |
423 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, | |
424 | sizeof(char), -1); | |
425 | /* | |
426 | * We don't care about metadata buffer's records lost | |
427 | * count, because we always retry here. Report error if | |
428 | * we need to bail out after timeout or being | |
429 | * interrupted. | |
430 | */ | |
24cedcfe | 431 | waitret = wait_event_interruptible_timeout(*chan->ops->get_reader_wait_queue(chan->chan), |
1ec3f75a | 432 | ({ |
4e1f08f4 | 433 | ret = chan->ops->event_reserve(&ctx, 0); |
1ec3f75a MD |
434 | ret != -ENOBUFS || !ret; |
435 | }), | |
436 | msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC)); | |
437 | if (!waitret || waitret == -ERESTARTSYS || ret) { | |
438 | printk(KERN_WARNING "LTTng: Failure to write metadata to buffers (%s)\n", | |
439 | waitret == -ERESTARTSYS ? "interrupted" : | |
440 | (ret == -ENOBUFS ? "timeout" : "I/O error")); | |
441 | if (waitret == -ERESTARTSYS) | |
442 | ret = waitret; | |
443 | goto end; | |
444 | } | |
f5cf8eff | 445 | chan->ops->event_write(&ctx, &str[pos], reserve_len); |
1ec3f75a | 446 | chan->ops->event_commit(&ctx); |
c099397a | 447 | } |
c099397a MD |
448 | end: |
449 | kfree(str); | |
450 | return ret; | |
451 | } | |
452 | ||
453 | static | |
8070f5c0 MD |
454 | int _ltt_field_statedump(struct ltt_session *session, |
455 | const struct lttng_event_field *field) | |
c099397a | 456 | { |
c099397a | 457 | int ret = 0; |
c099397a | 458 | |
8070f5c0 MD |
459 | switch (field->type.atype) { |
460 | case atype_integer: | |
461 | ret = lttng_metadata_printf(session, | |
462 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s;\n", | |
463 | field->type.u.basic.integer.size, | |
464 | field->type.u.basic.integer.alignment, | |
465 | field->type.u.basic.integer.signedness, | |
466 | (field->type.u.basic.integer.encoding == lttng_encode_none) | |
467 | ? "none" | |
468 | : (field->type.u.basic.integer.encoding == lttng_encode_UTF8) | |
469 | ? "UTF8" | |
470 | : "ASCII", | |
471 | field->type.u.basic.integer.base, | |
c099397a | 472 | #ifdef __BIG_ENDIAN |
8070f5c0 | 473 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
c099397a | 474 | #else |
8070f5c0 | 475 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
c099397a | 476 | #endif |
8070f5c0 MD |
477 | field->name); |
478 | break; | |
479 | case atype_enum: | |
480 | ret = lttng_metadata_printf(session, | |
481 | " %s %s;\n", | |
482 | field->type.u.basic.enumeration.name, | |
483 | field->name); | |
484 | break; | |
485 | case atype_array: | |
486 | { | |
487 | const struct lttng_basic_type *elem_type; | |
488 | ||
489 | elem_type = &field->type.u.array.elem_type; | |
490 | ret = lttng_metadata_printf(session, | |
491 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[%u];\n", | |
492 | elem_type->u.basic.integer.size, | |
493 | elem_type->u.basic.integer.alignment, | |
494 | elem_type->u.basic.integer.signedness, | |
495 | (elem_type->u.basic.integer.encoding == lttng_encode_none) | |
496 | ? "none" | |
497 | : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8) | |
498 | ? "UTF8" | |
499 | : "ASCII", | |
500 | elem_type->u.basic.integer.base, | |
8fa75f34 | 501 | #ifdef __BIG_ENDIAN |
8070f5c0 | 502 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
8fa75f34 | 503 | #else |
8070f5c0 | 504 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
8fa75f34 | 505 | #endif |
8070f5c0 MD |
506 | field->name, field->type.u.array.length); |
507 | break; | |
508 | } | |
509 | case atype_sequence: | |
510 | { | |
511 | const struct lttng_basic_type *elem_type; | |
512 | const struct lttng_basic_type *length_type; | |
513 | ||
514 | elem_type = &field->type.u.sequence.elem_type; | |
515 | length_type = &field->type.u.sequence.length_type; | |
516 | ret = lttng_metadata_printf(session, | |
517 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", | |
8070f5c0 | 518 | length_type->u.basic.integer.size, |
32257272 | 519 | (unsigned int) length_type->u.basic.integer.alignment, |
8070f5c0 MD |
520 | length_type->u.basic.integer.signedness, |
521 | (length_type->u.basic.integer.encoding == lttng_encode_none) | |
522 | ? "none" | |
0635cdd4 | 523 | : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8) |
8070f5c0 | 524 | ? "UTF8" |
0635cdd4 | 525 | : "ASCII"), |
8070f5c0 | 526 | length_type->u.basic.integer.base, |
e0a7a7c4 | 527 | #ifdef __BIG_ENDIAN |
8070f5c0 | 528 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
e0a7a7c4 | 529 | #else |
0635cdd4 | 530 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
e0a7a7c4 | 531 | #endif |
27d2368e MD |
532 | field->name); |
533 | if (ret) | |
534 | return ret; | |
535 | ||
536 | ret = lttng_metadata_printf(session, | |
537 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[ __%s_length ];\n", | |
8070f5c0 | 538 | elem_type->u.basic.integer.size, |
32257272 | 539 | (unsigned int) elem_type->u.basic.integer.alignment, |
8070f5c0 MD |
540 | elem_type->u.basic.integer.signedness, |
541 | (elem_type->u.basic.integer.encoding == lttng_encode_none) | |
542 | ? "none" | |
0635cdd4 | 543 | : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
8070f5c0 | 544 | ? "UTF8" |
0635cdd4 | 545 | : "ASCII"), |
8070f5c0 | 546 | elem_type->u.basic.integer.base, |
8fa75f34 | 547 | #ifdef __BIG_ENDIAN |
8070f5c0 | 548 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
8fa75f34 | 549 | #else |
8070f5c0 | 550 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
8fa75f34 | 551 | #endif |
8070f5c0 | 552 | field->name, |
27d2368e | 553 | field->name); |
8070f5c0 MD |
554 | break; |
555 | } | |
c099397a | 556 | |
8070f5c0 MD |
557 | case atype_string: |
558 | /* Default encoding is UTF8 */ | |
559 | ret = lttng_metadata_printf(session, | |
560 | " string%s %s;\n", | |
561 | field->type.u.basic.string.encoding == lttng_encode_ASCII ? | |
562 | " { encoding = ASCII; }" : "", | |
563 | field->name); | |
564 | break; | |
565 | default: | |
566 | WARN_ON_ONCE(1); | |
567 | return -EINVAL; | |
568 | } | |
569 | return ret; | |
570 | } | |
571 | ||
572 | static | |
573 | int _ltt_context_metadata_statedump(struct ltt_session *session, | |
574 | struct lttng_ctx *ctx) | |
575 | { | |
576 | int ret = 0; | |
577 | int i; | |
578 | ||
579 | if (!ctx) | |
580 | return 0; | |
581 | for (i = 0; i < ctx->nr_fields; i++) { | |
582 | const struct lttng_ctx_field *field = &ctx->fields[i]; | |
583 | ||
584 | ret = _ltt_field_statedump(session, &field->event_field); | |
585 | if (ret) | |
586 | return ret; | |
587 | } | |
588 | return ret; | |
589 | } | |
590 | ||
591 | static | |
592 | int _ltt_fields_metadata_statedump(struct ltt_session *session, | |
593 | struct ltt_event *event) | |
594 | { | |
595 | const struct lttng_event_desc *desc = event->desc; | |
596 | int ret = 0; | |
597 | int i; | |
598 | ||
599 | for (i = 0; i < desc->nr_fields; i++) { | |
600 | const struct lttng_event_field *field = &desc->fields[i]; | |
601 | ||
602 | ret = _ltt_field_statedump(session, field); | |
603 | if (ret) | |
604 | return ret; | |
c099397a MD |
605 | } |
606 | return ret; | |
607 | } | |
608 | ||
609 | static | |
610 | int _ltt_event_metadata_statedump(struct ltt_session *session, | |
611 | struct ltt_channel *chan, | |
612 | struct ltt_event *event) | |
613 | { | |
614 | int ret = 0; | |
615 | ||
616 | if (event->metadata_dumped || !ACCESS_ONCE(session->active)) | |
617 | return 0; | |
618 | if (chan == session->metadata) | |
619 | return 0; | |
620 | ||
621 | ret = lttng_metadata_printf(session, | |
622 | "event {\n" | |
623 | " name = %s;\n" | |
624 | " id = %u;\n" | |
b9074a1b | 625 | " stream_id = %u;\n", |
c099397a MD |
626 | event->desc->name, |
627 | event->id, | |
628 | event->chan->id); | |
629 | if (ret) | |
630 | goto end; | |
631 | ||
b9074a1b MD |
632 | if (event->ctx) { |
633 | ret = lttng_metadata_printf(session, | |
634 | " context := struct {\n"); | |
635 | if (ret) | |
636 | goto end; | |
637 | } | |
8070f5c0 MD |
638 | ret = _ltt_context_metadata_statedump(session, event->ctx); |
639 | if (ret) | |
640 | goto end; | |
b9074a1b MD |
641 | if (event->ctx) { |
642 | ret = lttng_metadata_printf(session, | |
643 | " };\n"); | |
644 | if (ret) | |
645 | goto end; | |
646 | } | |
8070f5c0 MD |
647 | |
648 | ret = lttng_metadata_printf(session, | |
8070f5c0 MD |
649 | " fields := struct {\n" |
650 | ); | |
651 | if (ret) | |
652 | goto end; | |
653 | ||
c099397a MD |
654 | ret = _ltt_fields_metadata_statedump(session, event); |
655 | if (ret) | |
656 | goto end; | |
657 | ||
658 | /* | |
659 | * LTTng space reservation can only reserve multiples of the | |
660 | * byte size. | |
661 | */ | |
662 | ret = lttng_metadata_printf(session, | |
9115fbdc MD |
663 | " };\n" |
664 | "};\n\n"); | |
c099397a MD |
665 | if (ret) |
666 | goto end; | |
667 | ||
c099397a MD |
668 | event->metadata_dumped = 1; |
669 | end: | |
670 | return ret; | |
671 | ||
672 | } | |
673 | ||
674 | static | |
675 | int _ltt_channel_metadata_statedump(struct ltt_session *session, | |
676 | struct ltt_channel *chan) | |
677 | { | |
678 | int ret = 0; | |
679 | ||
680 | if (chan->metadata_dumped || !ACCESS_ONCE(session->active)) | |
681 | return 0; | |
682 | if (chan == session->metadata) | |
683 | return 0; | |
684 | ||
685 | WARN_ON_ONCE(!chan->header_type); | |
686 | ret = lttng_metadata_printf(session, | |
687 | "stream {\n" | |
688 | " id = %u;\n" | |
9115fbdc | 689 | " event.header := %s;\n" |
b9074a1b | 690 | " packet.context := struct packet_context;\n", |
c099397a MD |
691 | chan->id, |
692 | chan->header_type == 1 ? "struct event_header_compact" : | |
693 | "struct event_header_large"); | |
694 | if (ret) | |
695 | goto end; | |
696 | ||
b9074a1b MD |
697 | if (chan->ctx) { |
698 | ret = lttng_metadata_printf(session, | |
699 | " event.context := struct {\n"); | |
700 | if (ret) | |
701 | goto end; | |
702 | } | |
8070f5c0 MD |
703 | ret = _ltt_context_metadata_statedump(session, chan->ctx); |
704 | if (ret) | |
705 | goto end; | |
b9074a1b MD |
706 | if (chan->ctx) { |
707 | ret = lttng_metadata_printf(session, | |
708 | " };\n"); | |
709 | if (ret) | |
710 | goto end; | |
711 | } | |
8070f5c0 MD |
712 | |
713 | ret = lttng_metadata_printf(session, | |
b9074a1b | 714 | "};\n\n"); |
8070f5c0 | 715 | |
c099397a MD |
716 | chan->metadata_dumped = 1; |
717 | end: | |
718 | return ret; | |
719 | } | |
720 | ||
9115fbdc MD |
721 | static |
722 | int _ltt_stream_packet_context_declare(struct ltt_session *session) | |
723 | { | |
724 | return lttng_metadata_printf(session, | |
725 | "struct packet_context {\n" | |
726 | " uint64_t timestamp_begin;\n" | |
727 | " uint64_t timestamp_end;\n" | |
728 | " uint32_t events_discarded;\n" | |
729 | " uint32_t content_size;\n" | |
730 | " uint32_t packet_size;\n" | |
731 | " uint32_t cpu_id;\n" | |
c6dfdf6f | 732 | "};\n\n" |
9115fbdc MD |
733 | ); |
734 | } | |
735 | ||
736 | /* | |
737 | * Compact header: | |
738 | * id: range: 0 - 30. | |
739 | * id 31 is reserved to indicate an extended header. | |
740 | * | |
741 | * Large header: | |
742 | * id: range: 0 - 65534. | |
743 | * id 65535 is reserved to indicate an extended header. | |
744 | */ | |
745 | static | |
746 | int _ltt_event_header_declare(struct ltt_session *session) | |
747 | { | |
748 | return lttng_metadata_printf(session, | |
749 | "struct event_header_compact {\n" | |
750 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" | |
751 | " variant <id> {\n" | |
752 | " struct {\n" | |
753 | " uint27_t timestamp;\n" | |
754 | " } compact;\n" | |
755 | " struct {\n" | |
756 | " uint32_t id;\n" | |
757 | " uint64_t timestamp;\n" | |
758 | " } extended;\n" | |
759 | " } v;\n" | |
760 | "} align(%u);\n" | |
761 | "\n" | |
762 | "struct event_header_large {\n" | |
763 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" | |
764 | " variant <id> {\n" | |
765 | " struct {\n" | |
766 | " uint32_t timestamp;\n" | |
767 | " } compact;\n" | |
768 | " struct {\n" | |
769 | " uint32_t id;\n" | |
770 | " uint64_t timestamp;\n" | |
771 | " } extended;\n" | |
772 | " } v;\n" | |
773 | "} align(%u);\n\n", | |
774 | ltt_alignof(uint32_t) * CHAR_BIT, | |
775 | ltt_alignof(uint16_t) * CHAR_BIT | |
776 | ); | |
777 | } | |
778 | ||
c099397a MD |
779 | /* |
780 | * Output metadata into this session's metadata buffers. | |
781 | */ | |
782 | static | |
783 | int _ltt_session_metadata_statedump(struct ltt_session *session) | |
784 | { | |
30bdb6e4 MD |
785 | unsigned char *uuid_c = session->uuid.b; |
786 | unsigned char uuid_s[37]; | |
c099397a MD |
787 | struct ltt_channel *chan; |
788 | struct ltt_event *event; | |
789 | int ret = 0; | |
790 | ||
791 | if (!ACCESS_ONCE(session->active)) | |
792 | return 0; | |
793 | if (session->metadata_dumped) | |
794 | goto skip_session; | |
f40270ad | 795 | if (!session->metadata) { |
cd4bd11f | 796 | printk(KERN_WARNING "LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n"); |
f40270ad MD |
797 | return -EPERM; |
798 | } | |
c099397a | 799 | |
d793d5e1 | 800 | snprintf(uuid_s, sizeof(uuid_s), |
30bdb6e4 MD |
801 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
802 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], | |
803 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], | |
804 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], | |
805 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); | |
d793d5e1 MD |
806 | |
807 | ret = lttng_metadata_printf(session, | |
9115fbdc MD |
808 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
809 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" | |
810 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" | |
811 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" | |
812 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" | |
813 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" | |
d793d5e1 MD |
814 | "\n" |
815 | "trace {\n" | |
816 | " major = %u;\n" | |
817 | " minor = %u;\n" | |
30bdb6e4 | 818 | " uuid = \"%s\";\n" |
d793d5e1 MD |
819 | " byte_order = %s;\n" |
820 | " packet.header := struct {\n" | |
821 | " uint32_t magic;\n" | |
1ec3f75a | 822 | " uint8_t uuid[16];\n" |
d793d5e1 | 823 | " uint32_t stream_id;\n" |
0eb25f58 | 824 | " };\n" |
d793d5e1 MD |
825 | "};\n\n", |
826 | ltt_alignof(uint8_t) * CHAR_BIT, | |
0eb25f58 | 827 | ltt_alignof(uint16_t) * CHAR_BIT, |
d793d5e1 MD |
828 | ltt_alignof(uint32_t) * CHAR_BIT, |
829 | ltt_alignof(uint64_t) * CHAR_BIT, | |
830 | CTF_VERSION_MAJOR, | |
831 | CTF_VERSION_MINOR, | |
832 | uuid_s, | |
833 | #ifdef __BIG_ENDIAN | |
834 | "be" | |
835 | #else | |
836 | "le" | |
837 | #endif | |
838 | ); | |
839 | if (ret) | |
840 | goto end; | |
841 | ||
9115fbdc MD |
842 | ret = _ltt_stream_packet_context_declare(session); |
843 | if (ret) | |
844 | goto end; | |
845 | ||
846 | ret = _ltt_event_header_declare(session); | |
847 | if (ret) | |
848 | goto end; | |
849 | ||
c099397a MD |
850 | skip_session: |
851 | list_for_each_entry(chan, &session->chan, list) { | |
852 | ret = _ltt_channel_metadata_statedump(session, chan); | |
853 | if (ret) | |
854 | goto end; | |
855 | } | |
856 | ||
857 | list_for_each_entry(event, &session->events, list) { | |
91e0dcaa | 858 | ret = _ltt_event_metadata_statedump(session, event->chan, event); |
c099397a MD |
859 | if (ret) |
860 | goto end; | |
861 | } | |
862 | session->metadata_dumped = 1; | |
863 | end: | |
864 | return ret; | |
865 | } | |
866 | ||
c0e31d2e MD |
867 | /** |
868 | * ltt_transport_register - LTT transport registration | |
869 | * @transport: transport structure | |
870 | * | |
871 | * Registers a transport which can be used as output to extract the data out of | |
872 | * LTTng. The module calling this registration function must ensure that no | |
873 | * trap-inducing code will be executed by the transport functions. E.g. | |
874 | * vmalloc_sync_all() must be called between a vmalloc and the moment the memory | |
875 | * is made visible to the transport function. This registration acts as a | |
876 | * vmalloc_sync_all. Therefore, only if the module allocates virtual memory | |
877 | * after its registration must it synchronize the TLBs. | |
878 | */ | |
879 | void ltt_transport_register(struct ltt_transport *transport) | |
880 | { | |
881 | /* | |
882 | * Make sure no page fault can be triggered by the module about to be | |
883 | * registered. We deal with this here so we don't have to call | |
884 | * vmalloc_sync_all() in each module's init. | |
885 | */ | |
6d2a620c | 886 | wrapper_vmalloc_sync_all(); |
c0e31d2e MD |
887 | |
888 | mutex_lock(&sessions_mutex); | |
889 | list_add_tail(&transport->node, <t_transport_list); | |
890 | mutex_unlock(&sessions_mutex); | |
891 | } | |
892 | EXPORT_SYMBOL_GPL(ltt_transport_register); | |
893 | ||
894 | /** | |
895 | * ltt_transport_unregister - LTT transport unregistration | |
896 | * @transport: transport structure | |
897 | */ | |
898 | void ltt_transport_unregister(struct ltt_transport *transport) | |
899 | { | |
900 | mutex_lock(&sessions_mutex); | |
901 | list_del(&transport->node); | |
902 | mutex_unlock(&sessions_mutex); | |
903 | } | |
904 | EXPORT_SYMBOL_GPL(ltt_transport_unregister); | |
905 | ||
4e3c1b9b MD |
906 | static int __init ltt_events_init(void) |
907 | { | |
1c25284c MD |
908 | int ret; |
909 | ||
11b5a3c2 MD |
910 | event_cache = KMEM_CACHE(ltt_event, 0); |
911 | if (!event_cache) | |
4e3c1b9b | 912 | return -ENOMEM; |
02119ee5 MD |
913 | ret = ltt_debugfs_abi_init(); |
914 | if (ret) | |
915 | goto error_abi; | |
4e3c1b9b | 916 | return 0; |
02119ee5 | 917 | error_abi: |
1c25284c MD |
918 | kmem_cache_destroy(event_cache); |
919 | return ret; | |
4e3c1b9b MD |
920 | } |
921 | ||
11b5a3c2 MD |
922 | module_init(ltt_events_init); |
923 | ||
4e3c1b9b MD |
924 | static void __exit ltt_events_exit(void) |
925 | { | |
92e94819 MD |
926 | struct ltt_session *session, *tmpsession; |
927 | ||
1c25284c | 928 | ltt_debugfs_abi_exit(); |
92e94819 MD |
929 | list_for_each_entry_safe(session, tmpsession, &sessions, list) |
930 | ltt_session_destroy(session); | |
11b5a3c2 | 931 | kmem_cache_destroy(event_cache); |
4e3c1b9b | 932 | } |
92e94819 | 933 | |
11b5a3c2 MD |
934 | module_exit(ltt_events_exit); |
935 | ||
92e94819 MD |
936 | MODULE_LICENSE("GPL and additional rights"); |
937 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); | |
938 | MODULE_DESCRIPTION("LTTng Events"); |