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