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