Commit | Line | Data |
---|---|---|
9f36eaed MJ |
1 | /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1) |
2 | * | |
a90917c3 | 3 | * lttng-events.c |
4e3c1b9b | 4 | * |
4e3c1b9b | 5 | * Holds LTTng per-session event registry. |
17baffe2 | 6 | * |
886d51a3 | 7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
4e3c1b9b MD |
8 | */ |
9 | ||
389d7070 MD |
10 | /* |
11 | * This page_alloc.h wrapper needs to be included before gfpflags.h because it | |
12 | * overrides a function with a define. | |
13 | */ | |
14 | #include "wrapper/page_alloc.h" | |
15 | ||
4e3c1b9b | 16 | #include <linux/module.h> |
c0e31d2e MD |
17 | #include <linux/mutex.h> |
18 | #include <linux/sched.h> | |
11b5a3c2 | 19 | #include <linux/slab.h> |
c099397a | 20 | #include <linux/jiffies.h> |
99dc9597 | 21 | #include <linux/utsname.h> |
abc0446a | 22 | #include <linux/err.h> |
7e6f9ef6 MD |
23 | #include <linux/seq_file.h> |
24 | #include <linux/file.h> | |
25 | #include <linux/anon_inodes.h> | |
241ae9a8 | 26 | #include <wrapper/file.h> |
3c997079 | 27 | #include <linux/jhash.h> |
8c6e7f13 | 28 | #include <linux/uaccess.h> |
a606b6e8 | 29 | #include <linux/vmalloc.h> |
3c997079 | 30 | |
241ae9a8 MD |
31 | #include <wrapper/uuid.h> |
32 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */ | |
33 | #include <wrapper/random.h> | |
34 | #include <wrapper/tracepoint.h> | |
35 | #include <wrapper/list.h> | |
cbc19040 | 36 | #include <wrapper/types.h> |
241ae9a8 MD |
37 | #include <lttng-kernel-version.h> |
38 | #include <lttng-events.h> | |
39 | #include <lttng-tracer.h> | |
40 | #include <lttng-abi-old.h> | |
5214fa50 | 41 | #include <lttng-endian.h> |
4993071a | 42 | #include <lttng-string-utils.h> |
241ae9a8 | 43 | #include <wrapper/vzalloc.h> |
64af2437 MD |
44 | #include <wrapper/ringbuffer/backend.h> |
45 | #include <wrapper/ringbuffer/frontend.h> | |
25cc7051 | 46 | #include <wrapper/time.h> |
4e3c1b9b | 47 | |
d83004aa JD |
48 | #define METADATA_CACHE_DEFAULT_SIZE 4096 |
49 | ||
4e3c1b9b | 50 | static LIST_HEAD(sessions); |
a90917c3 | 51 | static LIST_HEAD(lttng_transport_list); |
d83004aa JD |
52 | /* |
53 | * Protect the sessions and metadata caches. | |
54 | */ | |
4e3c1b9b MD |
55 | static DEFINE_MUTEX(sessions_mutex); |
56 | static struct kmem_cache *event_cache; | |
57 | ||
3c997079 MD |
58 | static void lttng_session_lazy_sync_enablers(struct lttng_session *session); |
59 | static void lttng_session_sync_enablers(struct lttng_session *session); | |
60 | static void lttng_enabler_destroy(struct lttng_enabler *enabler); | |
61 | ||
a90917c3 MD |
62 | static void _lttng_event_destroy(struct lttng_event *event); |
63 | static void _lttng_channel_destroy(struct lttng_channel *chan); | |
64 | static int _lttng_event_unregister(struct lttng_event *event); | |
c099397a | 65 | static |
a90917c3 MD |
66 | int _lttng_event_metadata_statedump(struct lttng_session *session, |
67 | struct lttng_channel *chan, | |
68 | struct lttng_event *event); | |
c099397a | 69 | static |
a90917c3 | 70 | int _lttng_session_metadata_statedump(struct lttng_session *session); |
d83004aa JD |
71 | static |
72 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream); | |
f513b2bf MD |
73 | static |
74 | int _lttng_field_statedump(struct lttng_session *session, | |
75 | const struct lttng_event_field *field, | |
76 | size_t nesting); | |
c099397a | 77 | |
c099397a | 78 | void synchronize_trace(void) |
abcca994 | 79 | { |
f4a8ec4d MJ |
80 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0)) |
81 | synchronize_rcu(); | |
82 | #else | |
abcca994 | 83 | synchronize_sched(); |
f4a8ec4d MJ |
84 | #endif |
85 | ||
b8590f40 MD |
86 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) |
87 | #ifdef CONFIG_PREEMPT_RT_FULL | |
88 | synchronize_rcu(); | |
89 | #endif | |
90 | #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */ | |
abcca994 MD |
91 | #ifdef CONFIG_PREEMPT_RT |
92 | synchronize_rcu(); | |
93 | #endif | |
b8590f40 | 94 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */ |
abcca994 MD |
95 | } |
96 | ||
3c997079 MD |
97 | void lttng_lock_sessions(void) |
98 | { | |
99 | mutex_lock(&sessions_mutex); | |
100 | } | |
101 | ||
102 | void lttng_unlock_sessions(void) | |
103 | { | |
104 | mutex_unlock(&sessions_mutex); | |
105 | } | |
106 | ||
107 | /* | |
108 | * Called with sessions lock held. | |
109 | */ | |
110 | int lttng_session_active(void) | |
111 | { | |
112 | struct lttng_session *iter; | |
113 | ||
114 | list_for_each_entry(iter, &sessions, list) { | |
115 | if (iter->active) | |
116 | return 1; | |
117 | } | |
118 | return 0; | |
119 | } | |
120 | ||
a90917c3 | 121 | struct lttng_session *lttng_session_create(void) |
4e3c1b9b | 122 | { |
a90917c3 | 123 | struct lttng_session *session; |
d83004aa | 124 | struct lttng_metadata_cache *metadata_cache; |
3c997079 | 125 | int i; |
4e3c1b9b MD |
126 | |
127 | mutex_lock(&sessions_mutex); | |
48f5e0b5 | 128 | session = lttng_kvzalloc(sizeof(struct lttng_session), GFP_KERNEL); |
4e3c1b9b | 129 | if (!session) |
d83004aa | 130 | goto err; |
4e3c1b9b | 131 | INIT_LIST_HEAD(&session->chan); |
f3d01b96 | 132 | INIT_LIST_HEAD(&session->events); |
d793d5e1 | 133 | uuid_le_gen(&session->uuid); |
d83004aa JD |
134 | |
135 | metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache), | |
136 | GFP_KERNEL); | |
137 | if (!metadata_cache) | |
138 | goto err_free_session; | |
f6da044f | 139 | metadata_cache->data = lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE); |
d83004aa JD |
140 | if (!metadata_cache->data) |
141 | goto err_free_cache; | |
142 | metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE; | |
143 | kref_init(&metadata_cache->refcount); | |
92d9f5e6 | 144 | mutex_init(&metadata_cache->lock); |
d83004aa JD |
145 | session->metadata_cache = metadata_cache; |
146 | INIT_LIST_HEAD(&metadata_cache->metadata_stream); | |
a36580d5 MD |
147 | memcpy(&metadata_cache->uuid, &session->uuid, |
148 | sizeof(metadata_cache->uuid)); | |
3c997079 MD |
149 | INIT_LIST_HEAD(&session->enablers_head); |
150 | for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++) | |
151 | INIT_HLIST_HEAD(&session->events_ht.table[i]); | |
4e3c1b9b MD |
152 | list_add(&session->list, &sessions); |
153 | mutex_unlock(&sessions_mutex); | |
154 | return session; | |
d83004aa JD |
155 | |
156 | err_free_cache: | |
157 | kfree(metadata_cache); | |
158 | err_free_session: | |
48f5e0b5 | 159 | lttng_kvfree(session); |
d83004aa JD |
160 | err: |
161 | mutex_unlock(&sessions_mutex); | |
162 | return NULL; | |
163 | } | |
164 | ||
165 | void metadata_cache_destroy(struct kref *kref) | |
166 | { | |
167 | struct lttng_metadata_cache *cache = | |
168 | container_of(kref, struct lttng_metadata_cache, refcount); | |
a606b6e8 | 169 | vfree(cache->data); |
d83004aa | 170 | kfree(cache); |
4e3c1b9b MD |
171 | } |
172 | ||
a90917c3 | 173 | void lttng_session_destroy(struct lttng_session *session) |
4e3c1b9b | 174 | { |
a90917c3 MD |
175 | struct lttng_channel *chan, *tmpchan; |
176 | struct lttng_event *event, *tmpevent; | |
d83004aa | 177 | struct lttng_metadata_stream *metadata_stream; |
3c997079 | 178 | struct lttng_enabler *enabler, *tmpenabler; |
dda6a249 | 179 | int ret; |
4e3c1b9b MD |
180 | |
181 | mutex_lock(&sessions_mutex); | |
a8f2d0c7 | 182 | WRITE_ONCE(session->active, 0); |
1ec65de1 MD |
183 | list_for_each_entry(chan, &session->chan, list) { |
184 | ret = lttng_syscalls_unregister(chan); | |
185 | WARN_ON(ret); | |
186 | } | |
dda6a249 | 187 | list_for_each_entry(event, &session->events, list) { |
a90917c3 | 188 | ret = _lttng_event_unregister(event); |
dda6a249 MD |
189 | WARN_ON(ret); |
190 | } | |
abcca994 | 191 | synchronize_trace(); /* Wait for in-flight events to complete */ |
3c997079 MD |
192 | list_for_each_entry_safe(enabler, tmpenabler, |
193 | &session->enablers_head, node) | |
194 | lttng_enabler_destroy(enabler); | |
4e3c1b9b | 195 | list_for_each_entry_safe(event, tmpevent, &session->events, list) |
a90917c3 | 196 | _lttng_event_destroy(event); |
d83004aa JD |
197 | list_for_each_entry_safe(chan, tmpchan, &session->chan, list) { |
198 | BUG_ON(chan->channel_type == METADATA_CHANNEL); | |
a90917c3 | 199 | _lttng_channel_destroy(chan); |
d83004aa JD |
200 | } |
201 | list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list) | |
202 | _lttng_metadata_channel_hangup(metadata_stream); | |
e0130fab MD |
203 | if (session->pid_tracker) |
204 | lttng_pid_tracker_destroy(session->pid_tracker); | |
d83004aa | 205 | kref_put(&session->metadata_cache->refcount, metadata_cache_destroy); |
4e3c1b9b MD |
206 | list_del(&session->list); |
207 | mutex_unlock(&sessions_mutex); | |
48f5e0b5 | 208 | lttng_kvfree(session); |
4e3c1b9b MD |
209 | } |
210 | ||
601252cf MD |
211 | int lttng_session_statedump(struct lttng_session *session) |
212 | { | |
213 | int ret; | |
214 | ||
215 | mutex_lock(&sessions_mutex); | |
216 | ret = lttng_statedump_start(session); | |
217 | mutex_unlock(&sessions_mutex); | |
218 | return ret; | |
219 | } | |
220 | ||
a90917c3 | 221 | int lttng_session_enable(struct lttng_session *session) |
c0e31d2e MD |
222 | { |
223 | int ret = 0; | |
a90917c3 | 224 | struct lttng_channel *chan; |
c0e31d2e MD |
225 | |
226 | mutex_lock(&sessions_mutex); | |
227 | if (session->active) { | |
228 | ret = -EBUSY; | |
229 | goto end; | |
230 | } | |
c099397a | 231 | |
3c997079 MD |
232 | /* Set transient enabler state to "enabled" */ |
233 | session->tstate = 1; | |
234 | ||
c099397a MD |
235 | /* |
236 | * Snapshot the number of events per channel to know the type of header | |
237 | * we need to use. | |
238 | */ | |
239 | list_for_each_entry(chan, &session->chan, list) { | |
240 | if (chan->header_type) | |
241 | continue; /* don't change it if session stop/restart */ | |
242 | if (chan->free_event_id < 31) | |
243 | chan->header_type = 1; /* compact */ | |
244 | else | |
245 | chan->header_type = 2; /* large */ | |
246 | } | |
247 | ||
3c997079 MD |
248 | /* We need to sync enablers with session before activation. */ |
249 | lttng_session_sync_enablers(session); | |
250 | ||
64af2437 | 251 | /* Clear each stream's quiescent state. */ |
09b93db9 MD |
252 | list_for_each_entry(chan, &session->chan, list) { |
253 | if (chan->channel_type != METADATA_CHANNEL) | |
254 | lib_ring_buffer_clear_quiescent_channel(chan->chan); | |
255 | } | |
64af2437 | 256 | |
a8f2d0c7 MJ |
257 | WRITE_ONCE(session->active, 1); |
258 | WRITE_ONCE(session->been_active, 1); | |
a90917c3 | 259 | ret = _lttng_session_metadata_statedump(session); |
c337ddc2 | 260 | if (ret) { |
a8f2d0c7 | 261 | WRITE_ONCE(session->active, 0); |
c337ddc2 MD |
262 | goto end; |
263 | } | |
264 | ret = lttng_statedump_start(session); | |
360f38ea | 265 | if (ret) |
a8f2d0c7 | 266 | WRITE_ONCE(session->active, 0); |
c0e31d2e MD |
267 | end: |
268 | mutex_unlock(&sessions_mutex); | |
11b5a3c2 | 269 | return ret; |
c0e31d2e MD |
270 | } |
271 | ||
a90917c3 | 272 | int lttng_session_disable(struct lttng_session *session) |
c0e31d2e MD |
273 | { |
274 | int ret = 0; | |
64af2437 | 275 | struct lttng_channel *chan; |
c0e31d2e MD |
276 | |
277 | mutex_lock(&sessions_mutex); | |
278 | if (!session->active) { | |
279 | ret = -EBUSY; | |
280 | goto end; | |
281 | } | |
a8f2d0c7 | 282 | WRITE_ONCE(session->active, 0); |
3c997079 MD |
283 | |
284 | /* Set transient enabler state to "disabled" */ | |
285 | session->tstate = 0; | |
286 | lttng_session_sync_enablers(session); | |
64af2437 MD |
287 | |
288 | /* Set each stream's quiescent state. */ | |
09b93db9 MD |
289 | list_for_each_entry(chan, &session->chan, list) { |
290 | if (chan->channel_type != METADATA_CHANNEL) | |
291 | lib_ring_buffer_set_quiescent_channel(chan->chan); | |
292 | } | |
c0e31d2e MD |
293 | end: |
294 | mutex_unlock(&sessions_mutex); | |
11b5a3c2 | 295 | return ret; |
c0e31d2e MD |
296 | } |
297 | ||
9616f0bf JD |
298 | int lttng_session_metadata_regenerate(struct lttng_session *session) |
299 | { | |
300 | int ret = 0; | |
301 | struct lttng_channel *chan; | |
302 | struct lttng_event *event; | |
303 | struct lttng_metadata_cache *cache = session->metadata_cache; | |
304 | struct lttng_metadata_stream *stream; | |
305 | ||
306 | mutex_lock(&sessions_mutex); | |
307 | if (!session->active) { | |
308 | ret = -EBUSY; | |
309 | goto end; | |
310 | } | |
311 | ||
312 | mutex_lock(&cache->lock); | |
313 | memset(cache->data, 0, cache->cache_alloc); | |
314 | cache->metadata_written = 0; | |
315 | cache->version++; | |
316 | list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) { | |
317 | stream->metadata_out = 0; | |
318 | stream->metadata_in = 0; | |
319 | } | |
320 | mutex_unlock(&cache->lock); | |
321 | ||
322 | session->metadata_dumped = 0; | |
323 | list_for_each_entry(chan, &session->chan, list) { | |
324 | chan->metadata_dumped = 0; | |
325 | } | |
326 | ||
327 | list_for_each_entry(event, &session->events, list) { | |
328 | event->metadata_dumped = 0; | |
329 | } | |
330 | ||
331 | ret = _lttng_session_metadata_statedump(session); | |
332 | ||
333 | end: | |
334 | mutex_unlock(&sessions_mutex); | |
335 | return ret; | |
336 | } | |
337 | ||
a90917c3 | 338 | int lttng_channel_enable(struct lttng_channel *channel) |
e64957da | 339 | { |
3c997079 | 340 | int ret = 0; |
e64957da | 341 | |
3c997079 MD |
342 | mutex_lock(&sessions_mutex); |
343 | if (channel->channel_type == METADATA_CHANNEL) { | |
344 | ret = -EPERM; | |
345 | goto end; | |
346 | } | |
347 | if (channel->enabled) { | |
348 | ret = -EEXIST; | |
349 | goto end; | |
350 | } | |
351 | /* Set transient enabler state to "enabled" */ | |
352 | channel->tstate = 1; | |
353 | lttng_session_sync_enablers(channel->session); | |
354 | /* Set atomically the state to "enabled" */ | |
a8f2d0c7 | 355 | WRITE_ONCE(channel->enabled, 1); |
3c997079 MD |
356 | end: |
357 | mutex_unlock(&sessions_mutex); | |
358 | return ret; | |
e64957da MD |
359 | } |
360 | ||
a90917c3 | 361 | int lttng_channel_disable(struct lttng_channel *channel) |
e64957da | 362 | { |
3c997079 | 363 | int ret = 0; |
e64957da | 364 | |
3c997079 MD |
365 | mutex_lock(&sessions_mutex); |
366 | if (channel->channel_type == METADATA_CHANNEL) { | |
367 | ret = -EPERM; | |
368 | goto end; | |
369 | } | |
370 | if (!channel->enabled) { | |
371 | ret = -EEXIST; | |
372 | goto end; | |
373 | } | |
374 | /* Set atomically the state to "disabled" */ | |
a8f2d0c7 | 375 | WRITE_ONCE(channel->enabled, 0); |
3c997079 MD |
376 | /* Set transient enabler state to "enabled" */ |
377 | channel->tstate = 0; | |
378 | lttng_session_sync_enablers(channel->session); | |
379 | end: | |
380 | mutex_unlock(&sessions_mutex); | |
381 | return ret; | |
e64957da MD |
382 | } |
383 | ||
a90917c3 | 384 | int lttng_event_enable(struct lttng_event *event) |
e64957da | 385 | { |
3c997079 | 386 | int ret = 0; |
e64957da | 387 | |
3c997079 MD |
388 | mutex_lock(&sessions_mutex); |
389 | if (event->chan->channel_type == METADATA_CHANNEL) { | |
390 | ret = -EPERM; | |
391 | goto end; | |
392 | } | |
393 | if (event->enabled) { | |
394 | ret = -EEXIST; | |
395 | goto end; | |
396 | } | |
a0493bef MD |
397 | switch (event->instrumentation) { |
398 | case LTTNG_KERNEL_TRACEPOINT: | |
399 | case LTTNG_KERNEL_SYSCALL: | |
400 | ret = -EINVAL; | |
401 | break; | |
402 | case LTTNG_KERNEL_KPROBE: | |
149b9a9d | 403 | case LTTNG_KERNEL_UPROBE: |
a0493bef | 404 | case LTTNG_KERNEL_NOOP: |
a8f2d0c7 | 405 | WRITE_ONCE(event->enabled, 1); |
a0493bef MD |
406 | break; |
407 | case LTTNG_KERNEL_KRETPROBE: | |
408 | ret = lttng_kretprobes_event_enable_state(event, 1); | |
409 | break; | |
853aee81 | 410 | case LTTNG_KERNEL_FUNCTION: /* Fall-through. */ |
a0493bef MD |
411 | default: |
412 | WARN_ON_ONCE(1); | |
413 | ret = -EINVAL; | |
414 | } | |
3c997079 MD |
415 | end: |
416 | mutex_unlock(&sessions_mutex); | |
417 | return ret; | |
e64957da MD |
418 | } |
419 | ||
a90917c3 | 420 | int lttng_event_disable(struct lttng_event *event) |
e64957da | 421 | { |
3c997079 | 422 | int ret = 0; |
e64957da | 423 | |
3c997079 MD |
424 | mutex_lock(&sessions_mutex); |
425 | if (event->chan->channel_type == METADATA_CHANNEL) { | |
426 | ret = -EPERM; | |
427 | goto end; | |
428 | } | |
429 | if (!event->enabled) { | |
430 | ret = -EEXIST; | |
431 | goto end; | |
432 | } | |
a0493bef MD |
433 | switch (event->instrumentation) { |
434 | case LTTNG_KERNEL_TRACEPOINT: | |
435 | case LTTNG_KERNEL_SYSCALL: | |
436 | ret = -EINVAL; | |
437 | break; | |
438 | case LTTNG_KERNEL_KPROBE: | |
149b9a9d | 439 | case LTTNG_KERNEL_UPROBE: |
a0493bef | 440 | case LTTNG_KERNEL_NOOP: |
a8f2d0c7 | 441 | WRITE_ONCE(event->enabled, 0); |
a0493bef MD |
442 | break; |
443 | case LTTNG_KERNEL_KRETPROBE: | |
444 | ret = lttng_kretprobes_event_enable_state(event, 0); | |
445 | break; | |
853aee81 | 446 | case LTTNG_KERNEL_FUNCTION: /* Fall-through. */ |
a0493bef MD |
447 | default: |
448 | WARN_ON_ONCE(1); | |
449 | ret = -EINVAL; | |
450 | } | |
3c997079 MD |
451 | end: |
452 | mutex_unlock(&sessions_mutex); | |
453 | return ret; | |
e64957da MD |
454 | } |
455 | ||
a90917c3 | 456 | static struct lttng_transport *lttng_transport_find(const char *name) |
f3d01b96 | 457 | { |
a90917c3 | 458 | struct lttng_transport *transport; |
f3d01b96 | 459 | |
a90917c3 | 460 | list_for_each_entry(transport, <tng_transport_list, node) { |
f3d01b96 MD |
461 | if (!strcmp(transport->name, name)) |
462 | return transport; | |
463 | } | |
464 | return NULL; | |
465 | } | |
466 | ||
a90917c3 | 467 | struct lttng_channel *lttng_channel_create(struct lttng_session *session, |
5dbbdb43 MD |
468 | const char *transport_name, |
469 | void *buf_addr, | |
4e3c1b9b MD |
470 | size_t subbuf_size, size_t num_subbuf, |
471 | unsigned int switch_timer_interval, | |
d83004aa JD |
472 | unsigned int read_timer_interval, |
473 | enum channel_type channel_type) | |
4e3c1b9b | 474 | { |
a90917c3 MD |
475 | struct lttng_channel *chan; |
476 | struct lttng_transport *transport = NULL; | |
4e3c1b9b MD |
477 | |
478 | mutex_lock(&sessions_mutex); | |
d83004aa | 479 | if (session->been_active && channel_type != METADATA_CHANNEL) |
e5382b6d | 480 | goto active; /* Refuse to add channel to active session */ |
a90917c3 | 481 | transport = lttng_transport_find(transport_name); |
f3d01b96 MD |
482 | if (!transport) { |
483 | printk(KERN_WARNING "LTTng transport %s not found\n", | |
484 | transport_name); | |
c0e31d2e | 485 | goto notransport; |
f3d01b96 | 486 | } |
a33c9927 MD |
487 | if (!try_module_get(transport->owner)) { |
488 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); | |
489 | goto notransport; | |
490 | } | |
a90917c3 | 491 | chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL); |
4e3c1b9b | 492 | if (!chan) |
c0e31d2e | 493 | goto nomem; |
4e3c1b9b | 494 | chan->session = session; |
05d32c64 | 495 | chan->id = session->free_chan_id++; |
3b731ab1 | 496 | chan->ops = &transport->ops; |
125b4df4 MD |
497 | /* |
498 | * Note: the channel creation op already writes into the packet | |
499 | * headers. Therefore the "chan" information used as input | |
500 | * should be already accessible. | |
501 | */ | |
5f5ddf01 MD |
502 | chan->chan = transport->ops.channel_create(transport_name, |
503 | chan, buf_addr, subbuf_size, num_subbuf, | |
504 | switch_timer_interval, read_timer_interval); | |
f3d01b96 MD |
505 | if (!chan->chan) |
506 | goto create_error; | |
3c997079 | 507 | chan->tstate = 1; |
e64957da | 508 | chan->enabled = 1; |
a33c9927 | 509 | chan->transport = transport; |
d83004aa | 510 | chan->channel_type = channel_type; |
4e3c1b9b MD |
511 | list_add(&chan->list, &session->chan); |
512 | mutex_unlock(&sessions_mutex); | |
513 | return chan; | |
514 | ||
f3d01b96 MD |
515 | create_error: |
516 | kfree(chan); | |
c0e31d2e | 517 | nomem: |
a33c9927 MD |
518 | if (transport) |
519 | module_put(transport->owner); | |
c0e31d2e | 520 | notransport: |
e5382b6d | 521 | active: |
4e3c1b9b MD |
522 | mutex_unlock(&sessions_mutex); |
523 | return NULL; | |
524 | } | |
525 | ||
526 | /* | |
d83004aa JD |
527 | * Only used internally at session destruction for per-cpu channels, and |
528 | * when metadata channel is released. | |
529 | * Needs to be called with sessions mutex held. | |
4e3c1b9b | 530 | */ |
aa7c23a9 | 531 | static |
a90917c3 | 532 | void _lttng_channel_destroy(struct lttng_channel *chan) |
4e3c1b9b | 533 | { |
11b5a3c2 | 534 | chan->ops->channel_destroy(chan->chan); |
a33c9927 | 535 | module_put(chan->transport->owner); |
4e3c1b9b | 536 | list_del(&chan->list); |
8070f5c0 | 537 | lttng_destroy_context(chan->ctx); |
4e3c1b9b MD |
538 | kfree(chan); |
539 | } | |
540 | ||
d83004aa JD |
541 | void lttng_metadata_channel_destroy(struct lttng_channel *chan) |
542 | { | |
543 | BUG_ON(chan->channel_type != METADATA_CHANNEL); | |
544 | ||
545 | /* Protect the metadata cache with the sessions_mutex. */ | |
546 | mutex_lock(&sessions_mutex); | |
547 | _lttng_channel_destroy(chan); | |
548 | mutex_unlock(&sessions_mutex); | |
549 | } | |
550 | EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy); | |
551 | ||
552 | static | |
553 | void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream) | |
554 | { | |
555 | stream->finalized = 1; | |
556 | wake_up_interruptible(&stream->read_wait); | |
557 | } | |
558 | ||
e5382b6d MD |
559 | /* |
560 | * Supports event creation while tracing session is active. | |
3c997079 | 561 | * Needs to be called with sessions mutex held. |
e5382b6d | 562 | */ |
3c997079 MD |
563 | struct lttng_event *_lttng_event_create(struct lttng_channel *chan, |
564 | struct lttng_kernel_event *event_param, | |
565 | void *filter, | |
566 | const struct lttng_event_desc *event_desc, | |
567 | enum lttng_kernel_instrumentation itype) | |
4e3c1b9b | 568 | { |
3c997079 | 569 | struct lttng_session *session = chan->session; |
a90917c3 | 570 | struct lttng_event *event; |
3c997079 MD |
571 | const char *event_name; |
572 | struct hlist_head *head; | |
573 | size_t name_len; | |
574 | uint32_t hash; | |
3d084699 | 575 | int ret; |
4e3c1b9b | 576 | |
abc0446a | 577 | if (chan->free_event_id == -1U) { |
4cf0bf51 | 578 | ret = -EMFILE; |
e5382b6d | 579 | goto full; |
abc0446a | 580 | } |
3c997079 MD |
581 | |
582 | switch (itype) { | |
583 | case LTTNG_KERNEL_TRACEPOINT: | |
584 | event_name = event_desc->name; | |
585 | break; | |
586 | case LTTNG_KERNEL_KPROBE: | |
149b9a9d | 587 | case LTTNG_KERNEL_UPROBE: |
3c997079 | 588 | case LTTNG_KERNEL_KRETPROBE: |
3c997079 | 589 | case LTTNG_KERNEL_NOOP: |
33a39a3c | 590 | case LTTNG_KERNEL_SYSCALL: |
3c997079 MD |
591 | event_name = event_param->name; |
592 | break; | |
853aee81 | 593 | case LTTNG_KERNEL_FUNCTION: /* Fall-through. */ |
3c997079 MD |
594 | default: |
595 | WARN_ON_ONCE(1); | |
596 | ret = -EINVAL; | |
597 | goto type_error; | |
598 | } | |
599 | name_len = strlen(event_name); | |
600 | hash = jhash(event_name, name_len, 0); | |
601 | head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)]; | |
d216ecae | 602 | lttng_hlist_for_each_entry(event, head, hlist) { |
3c997079 MD |
603 | WARN_ON_ONCE(!event->desc); |
604 | if (!strncmp(event->desc->name, event_name, | |
605 | LTTNG_KERNEL_SYM_NAME_LEN - 1) | |
606 | && chan == event->chan) { | |
607 | ret = -EEXIST; | |
608 | goto exist; | |
abc0446a MD |
609 | } |
610 | } | |
3c997079 | 611 | |
11b5a3c2 | 612 | event = kmem_cache_zalloc(event_cache, GFP_KERNEL); |
abc0446a | 613 | if (!event) { |
4cf0bf51 | 614 | ret = -ENOMEM; |
4e3c1b9b | 615 | goto cache_error; |
abc0446a | 616 | } |
4e3c1b9b MD |
617 | event->chan = chan; |
618 | event->filter = filter; | |
e5382b6d | 619 | event->id = chan->free_event_id++; |
3c997079 MD |
620 | event->instrumentation = itype; |
621 | event->evtype = LTTNG_TYPE_EVENT; | |
07dfc1d0 | 622 | INIT_LIST_HEAD(&event->bytecode_runtime_head); |
3c997079 MD |
623 | INIT_LIST_HEAD(&event->enablers_ref_head); |
624 | ||
625 | switch (itype) { | |
ab2277d6 | 626 | case LTTNG_KERNEL_TRACEPOINT: |
3c997079 MD |
627 | /* Event will be enabled by enabler sync. */ |
628 | event->enabled = 0; | |
629 | event->registered = 0; | |
630 | event->desc = lttng_event_get(event_name); | |
abc0446a | 631 | if (!event->desc) { |
4cf0bf51 | 632 | ret = -ENOENT; |
d3dbe23c | 633 | goto register_error; |
abc0446a | 634 | } |
3c997079 MD |
635 | /* Populate lttng_event structure before event registration. */ |
636 | smp_wmb(); | |
baf20995 | 637 | break; |
ab2277d6 | 638 | case LTTNG_KERNEL_KPROBE: |
a0493bef MD |
639 | /* |
640 | * Needs to be explicitly enabled after creation, since | |
641 | * we may want to apply filters. | |
642 | */ | |
643 | event->enabled = 0; | |
3c997079 MD |
644 | event->registered = 1; |
645 | /* | |
646 | * Populate lttng_event structure before event | |
647 | * registration. | |
648 | */ | |
649 | smp_wmb(); | |
650 | ret = lttng_kprobes_register(event_name, | |
f17701fb MD |
651 | event_param->u.kprobe.symbol_name, |
652 | event_param->u.kprobe.offset, | |
653 | event_param->u.kprobe.addr, | |
654 | event); | |
abc0446a | 655 | if (ret) { |
4cf0bf51 | 656 | ret = -EINVAL; |
d6d808f3 | 657 | goto register_error; |
abc0446a | 658 | } |
edeb3137 MD |
659 | ret = try_module_get(event->desc->owner); |
660 | WARN_ON_ONCE(!ret); | |
d6d808f3 | 661 | break; |
7371f44c MD |
662 | case LTTNG_KERNEL_KRETPROBE: |
663 | { | |
a90917c3 | 664 | struct lttng_event *event_return; |
7371f44c MD |
665 | |
666 | /* kretprobe defines 2 events */ | |
a0493bef MD |
667 | /* |
668 | * Needs to be explicitly enabled after creation, since | |
669 | * we may want to apply filters. | |
670 | */ | |
671 | event->enabled = 0; | |
3c997079 | 672 | event->registered = 1; |
7371f44c MD |
673 | event_return = |
674 | kmem_cache_zalloc(event_cache, GFP_KERNEL); | |
abc0446a | 675 | if (!event_return) { |
4cf0bf51 | 676 | ret = -ENOMEM; |
7371f44c | 677 | goto register_error; |
abc0446a | 678 | } |
7371f44c MD |
679 | event_return->chan = chan; |
680 | event_return->filter = filter; | |
681 | event_return->id = chan->free_event_id++; | |
a0493bef | 682 | event_return->enabled = 0; |
3c997079 MD |
683 | event_return->registered = 1; |
684 | event_return->instrumentation = itype; | |
7371f44c | 685 | /* |
a90917c3 | 686 | * Populate lttng_event structure before kretprobe registration. |
7371f44c MD |
687 | */ |
688 | smp_wmb(); | |
3c997079 | 689 | ret = lttng_kretprobes_register(event_name, |
7371f44c MD |
690 | event_param->u.kretprobe.symbol_name, |
691 | event_param->u.kretprobe.offset, | |
692 | event_param->u.kretprobe.addr, | |
693 | event, event_return); | |
694 | if (ret) { | |
695 | kmem_cache_free(event_cache, event_return); | |
4cf0bf51 | 696 | ret = -EINVAL; |
7371f44c MD |
697 | goto register_error; |
698 | } | |
699 | /* Take 2 refs on the module: one per event. */ | |
700 | ret = try_module_get(event->desc->owner); | |
701 | WARN_ON_ONCE(!ret); | |
702 | ret = try_module_get(event->desc->owner); | |
703 | WARN_ON_ONCE(!ret); | |
a90917c3 | 704 | ret = _lttng_event_metadata_statedump(chan->session, chan, |
7371f44c | 705 | event_return); |
abc0446a | 706 | WARN_ON_ONCE(ret > 0); |
7371f44c MD |
707 | if (ret) { |
708 | kmem_cache_free(event_cache, event_return); | |
709 | module_put(event->desc->owner); | |
710 | module_put(event->desc->owner); | |
711 | goto statedump_error; | |
712 | } | |
713 | list_add(&event_return->list, &chan->session->events); | |
714 | break; | |
715 | } | |
1ec65de1 | 716 | case LTTNG_KERNEL_NOOP: |
33a39a3c | 717 | case LTTNG_KERNEL_SYSCALL: |
a0493bef MD |
718 | /* |
719 | * Needs to be explicitly enabled after creation, since | |
720 | * we may want to apply filters. | |
721 | */ | |
722 | event->enabled = 0; | |
3c997079 MD |
723 | event->registered = 0; |
724 | event->desc = event_desc; | |
abc0446a | 725 | if (!event->desc) { |
4cf0bf51 | 726 | ret = -EINVAL; |
259b6cb3 | 727 | goto register_error; |
abc0446a | 728 | } |
1ec65de1 | 729 | break; |
149b9a9d | 730 | case LTTNG_KERNEL_UPROBE: |
a344f64b FD |
731 | /* |
732 | * Needs to be explicitly enabled after creation, since | |
733 | * we may want to apply filters. | |
734 | */ | |
735 | event->enabled = 0; | |
736 | event->registered = 1; | |
3aed4dca | 737 | |
a344f64b FD |
738 | /* |
739 | * Populate lttng_event structure before event | |
740 | * registration. | |
741 | */ | |
742 | smp_wmb(); | |
149b9a9d YB |
743 | |
744 | ret = lttng_uprobes_register(event_param->name, | |
56377c91 | 745 | event_param->u.uprobe.fd, |
149b9a9d YB |
746 | event); |
747 | if (ret) | |
748 | goto register_error; | |
749 | ret = try_module_get(event->desc->owner); | |
750 | WARN_ON_ONCE(!ret); | |
751 | break; | |
853aee81 | 752 | case LTTNG_KERNEL_FUNCTION: /* Fall-through */ |
baf20995 MD |
753 | default: |
754 | WARN_ON_ONCE(1); | |
4cf0bf51 | 755 | ret = -EINVAL; |
df07930b | 756 | goto register_error; |
baf20995 | 757 | } |
a90917c3 | 758 | ret = _lttng_event_metadata_statedump(chan->session, chan, event); |
abc0446a MD |
759 | WARN_ON_ONCE(ret > 0); |
760 | if (ret) { | |
c099397a | 761 | goto statedump_error; |
abc0446a | 762 | } |
3c997079 | 763 | hlist_add_head(&event->hlist, head); |
dda6a249 | 764 | list_add(&event->list, &chan->session->events); |
4e3c1b9b MD |
765 | return event; |
766 | ||
c099397a | 767 | statedump_error: |
259b6cb3 | 768 | /* If a statedump error occurs, events will not be readable. */ |
11b5a3c2 | 769 | register_error: |
11b5a3c2 | 770 | kmem_cache_free(event_cache, event); |
4e3c1b9b MD |
771 | cache_error: |
772 | exist: | |
3c997079 | 773 | type_error: |
e5382b6d | 774 | full: |
4cf0bf51 | 775 | return ERR_PTR(ret); |
4e3c1b9b MD |
776 | } |
777 | ||
3c997079 MD |
778 | struct lttng_event *lttng_event_create(struct lttng_channel *chan, |
779 | struct lttng_kernel_event *event_param, | |
780 | void *filter, | |
781 | const struct lttng_event_desc *event_desc, | |
782 | enum lttng_kernel_instrumentation itype) | |
783 | { | |
784 | struct lttng_event *event; | |
785 | ||
786 | mutex_lock(&sessions_mutex); | |
787 | event = _lttng_event_create(chan, event_param, filter, event_desc, | |
788 | itype); | |
789 | mutex_unlock(&sessions_mutex); | |
790 | return event; | |
791 | } | |
792 | ||
793 | /* Only used for tracepoints for now. */ | |
794 | static | |
795 | void register_event(struct lttng_event *event) | |
796 | { | |
797 | const struct lttng_event_desc *desc; | |
f127e61e | 798 | int ret = -EINVAL; |
3c997079 | 799 | |
3c997079 MD |
800 | if (event->registered) |
801 | return; | |
4ecb5ad5 | 802 | |
3c997079 | 803 | desc = event->desc; |
4ecb5ad5 MD |
804 | switch (event->instrumentation) { |
805 | case LTTNG_KERNEL_TRACEPOINT: | |
806 | ret = lttng_wrapper_tracepoint_probe_register(desc->kname, | |
807 | desc->probe_callback, | |
808 | event); | |
809 | break; | |
810 | case LTTNG_KERNEL_SYSCALL: | |
811 | ret = lttng_syscall_filter_enable(event->chan, | |
812 | desc->name); | |
813 | break; | |
814 | case LTTNG_KERNEL_KPROBE: | |
a344f64b | 815 | case LTTNG_KERNEL_UPROBE: |
4ecb5ad5 | 816 | case LTTNG_KERNEL_KRETPROBE: |
4ecb5ad5 MD |
817 | case LTTNG_KERNEL_NOOP: |
818 | ret = 0; | |
819 | break; | |
853aee81 | 820 | case LTTNG_KERNEL_FUNCTION: /* Fall-through */ |
4ecb5ad5 MD |
821 | default: |
822 | WARN_ON_ONCE(1); | |
823 | } | |
3c997079 MD |
824 | if (!ret) |
825 | event->registered = 1; | |
826 | } | |
827 | ||
4e3c1b9b MD |
828 | /* |
829 | * Only used internally at session destruction. | |
830 | */ | |
a90917c3 | 831 | int _lttng_event_unregister(struct lttng_event *event) |
4e3c1b9b | 832 | { |
4ecb5ad5 | 833 | const struct lttng_event_desc *desc; |
11b5a3c2 MD |
834 | int ret = -EINVAL; |
835 | ||
3c997079 MD |
836 | if (!event->registered) |
837 | return 0; | |
838 | ||
4ecb5ad5 | 839 | desc = event->desc; |
38d024ae | 840 | switch (event->instrumentation) { |
ab2277d6 | 841 | case LTTNG_KERNEL_TRACEPOINT: |
20591cf7 | 842 | ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname, |
85a9ca7f | 843 | event->desc->probe_callback, |
11b5a3c2 | 844 | event); |
baf20995 | 845 | break; |
ab2277d6 | 846 | case LTTNG_KERNEL_KPROBE: |
f17701fb | 847 | lttng_kprobes_unregister(event); |
d6d808f3 MD |
848 | ret = 0; |
849 | break; | |
7371f44c MD |
850 | case LTTNG_KERNEL_KRETPROBE: |
851 | lttng_kretprobes_unregister(event); | |
852 | ret = 0; | |
853 | break; | |
33a39a3c | 854 | case LTTNG_KERNEL_SYSCALL: |
4ecb5ad5 MD |
855 | ret = lttng_syscall_filter_disable(event->chan, |
856 | desc->name); | |
857 | break; | |
858 | case LTTNG_KERNEL_NOOP: | |
259b6cb3 MD |
859 | ret = 0; |
860 | break; | |
149b9a9d YB |
861 | case LTTNG_KERNEL_UPROBE: |
862 | lttng_uprobes_unregister(event); | |
863 | ret = 0; | |
864 | break; | |
853aee81 | 865 | case LTTNG_KERNEL_FUNCTION: /* Fall-through */ |
baf20995 MD |
866 | default: |
867 | WARN_ON_ONCE(1); | |
868 | } | |
3c997079 MD |
869 | if (!ret) |
870 | event->registered = 0; | |
dda6a249 MD |
871 | return ret; |
872 | } | |
873 | ||
874 | /* | |
875 | * Only used internally at session destruction. | |
876 | */ | |
be066e6c | 877 | static |
a90917c3 | 878 | void _lttng_event_destroy(struct lttng_event *event) |
dda6a249 | 879 | { |
edeb3137 MD |
880 | switch (event->instrumentation) { |
881 | case LTTNG_KERNEL_TRACEPOINT: | |
a90917c3 | 882 | lttng_event_put(event->desc); |
edeb3137 MD |
883 | break; |
884 | case LTTNG_KERNEL_KPROBE: | |
885 | module_put(event->desc->owner); | |
886 | lttng_kprobes_destroy_private(event); | |
887 | break; | |
7371f44c MD |
888 | case LTTNG_KERNEL_KRETPROBE: |
889 | module_put(event->desc->owner); | |
890 | lttng_kretprobes_destroy_private(event); | |
891 | break; | |
259b6cb3 | 892 | case LTTNG_KERNEL_NOOP: |
33a39a3c | 893 | case LTTNG_KERNEL_SYSCALL: |
259b6cb3 | 894 | break; |
149b9a9d YB |
895 | case LTTNG_KERNEL_UPROBE: |
896 | module_put(event->desc->owner); | |
897 | lttng_uprobes_destroy_private(event); | |
898 | break; | |
853aee81 | 899 | case LTTNG_KERNEL_FUNCTION: /* Fall-through */ |
edeb3137 MD |
900 | default: |
901 | WARN_ON_ONCE(1); | |
902 | } | |
dda6a249 | 903 | list_del(&event->list); |
8070f5c0 | 904 | lttng_destroy_context(event->ctx); |
11b5a3c2 | 905 | kmem_cache_free(event_cache, event); |
4e3c1b9b MD |
906 | } |
907 | ||
e0130fab MD |
908 | int lttng_session_track_pid(struct lttng_session *session, int pid) |
909 | { | |
910 | int ret; | |
911 | ||
912 | if (pid < -1) | |
913 | return -EINVAL; | |
914 | mutex_lock(&sessions_mutex); | |
915 | if (pid == -1) { | |
916 | /* track all pids: destroy tracker. */ | |
917 | if (session->pid_tracker) { | |
918 | struct lttng_pid_tracker *lpf; | |
919 | ||
920 | lpf = session->pid_tracker; | |
921 | rcu_assign_pointer(session->pid_tracker, NULL); | |
922 | synchronize_trace(); | |
923 | lttng_pid_tracker_destroy(lpf); | |
924 | } | |
925 | ret = 0; | |
926 | } else { | |
927 | if (!session->pid_tracker) { | |
928 | struct lttng_pid_tracker *lpf; | |
929 | ||
930 | lpf = lttng_pid_tracker_create(); | |
931 | if (!lpf) { | |
932 | ret = -ENOMEM; | |
933 | goto unlock; | |
934 | } | |
935 | ret = lttng_pid_tracker_add(lpf, pid); | |
936 | rcu_assign_pointer(session->pid_tracker, lpf); | |
937 | } else { | |
938 | ret = lttng_pid_tracker_add(session->pid_tracker, pid); | |
939 | } | |
940 | } | |
941 | unlock: | |
942 | mutex_unlock(&sessions_mutex); | |
943 | return ret; | |
944 | } | |
945 | ||
946 | int lttng_session_untrack_pid(struct lttng_session *session, int pid) | |
947 | { | |
948 | int ret; | |
949 | ||
950 | if (pid < -1) | |
951 | return -EINVAL; | |
952 | mutex_lock(&sessions_mutex); | |
953 | if (pid == -1) { | |
954 | /* untrack all pids: replace by empty tracker. */ | |
955 | struct lttng_pid_tracker *old_lpf = session->pid_tracker; | |
956 | struct lttng_pid_tracker *lpf; | |
957 | ||
958 | lpf = lttng_pid_tracker_create(); | |
959 | if (!lpf) { | |
960 | ret = -ENOMEM; | |
961 | goto unlock; | |
962 | } | |
963 | rcu_assign_pointer(session->pid_tracker, lpf); | |
964 | synchronize_trace(); | |
965 | if (old_lpf) | |
966 | lttng_pid_tracker_destroy(old_lpf); | |
967 | ret = 0; | |
968 | } else { | |
969 | if (!session->pid_tracker) { | |
970 | ret = -ENOENT; | |
971 | goto unlock; | |
972 | } | |
973 | ret = lttng_pid_tracker_del(session->pid_tracker, pid); | |
974 | } | |
975 | unlock: | |
976 | mutex_unlock(&sessions_mutex); | |
977 | return ret; | |
978 | } | |
979 | ||
7e6f9ef6 MD |
980 | static |
981 | void *pid_list_start(struct seq_file *m, loff_t *pos) | |
982 | { | |
983 | struct lttng_session *session = m->private; | |
984 | struct lttng_pid_tracker *lpf; | |
985 | struct lttng_pid_hash_node *e; | |
986 | int iter = 0, i; | |
987 | ||
988 | mutex_lock(&sessions_mutex); | |
989 | lpf = session->pid_tracker; | |
990 | if (lpf) { | |
991 | for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) { | |
992 | struct hlist_head *head = &lpf->pid_hash[i]; | |
993 | ||
f934e302 | 994 | lttng_hlist_for_each_entry(e, head, hlist) { |
7e6f9ef6 MD |
995 | if (iter++ >= *pos) |
996 | return e; | |
997 | } | |
998 | } | |
999 | } else { | |
1000 | /* PID tracker disabled. */ | |
1001 | if (iter >= *pos && iter == 0) { | |
1002 | return session; /* empty tracker */ | |
1003 | } | |
1004 | iter++; | |
1005 | } | |
1006 | /* End of list */ | |
1007 | return NULL; | |
1008 | } | |
1009 | ||
1010 | /* Called with sessions_mutex held. */ | |
1011 | static | |
1012 | void *pid_list_next(struct seq_file *m, void *p, loff_t *ppos) | |
1013 | { | |
1014 | struct lttng_session *session = m->private; | |
1015 | struct lttng_pid_tracker *lpf; | |
1016 | struct lttng_pid_hash_node *e; | |
1017 | int iter = 0, i; | |
1018 | ||
1019 | (*ppos)++; | |
1020 | lpf = session->pid_tracker; | |
1021 | if (lpf) { | |
1022 | for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) { | |
1023 | struct hlist_head *head = &lpf->pid_hash[i]; | |
1024 | ||
f934e302 | 1025 | lttng_hlist_for_each_entry(e, head, hlist) { |
7e6f9ef6 MD |
1026 | if (iter++ >= *ppos) |
1027 | return e; | |
1028 | } | |
1029 | } | |
1030 | } else { | |
1031 | /* PID tracker disabled. */ | |
1032 | if (iter >= *ppos && iter == 0) | |
1033 | return session; /* empty tracker */ | |
1034 | iter++; | |
1035 | } | |
1036 | ||
1037 | /* End of list */ | |
1038 | return NULL; | |
1039 | } | |
1040 | ||
1041 | static | |
1042 | void pid_list_stop(struct seq_file *m, void *p) | |
1043 | { | |
1044 | mutex_unlock(&sessions_mutex); | |
1045 | } | |
1046 | ||
1047 | static | |
1048 | int pid_list_show(struct seq_file *m, void *p) | |
1049 | { | |
1050 | int pid; | |
1051 | ||
1052 | if (p == m->private) { | |
1053 | /* Tracker disabled. */ | |
1054 | pid = -1; | |
1055 | } else { | |
1056 | const struct lttng_pid_hash_node *e = p; | |
1057 | ||
1058 | pid = lttng_pid_tracker_get_node_pid(e); | |
1059 | } | |
1060 | seq_printf(m, "process { pid = %d; };\n", pid); | |
1061 | return 0; | |
1062 | } | |
1063 | ||
1064 | static | |
1065 | const struct seq_operations lttng_tracker_pids_list_seq_ops = { | |
1066 | .start = pid_list_start, | |
1067 | .next = pid_list_next, | |
1068 | .stop = pid_list_stop, | |
1069 | .show = pid_list_show, | |
1070 | }; | |
1071 | ||
1072 | static | |
1073 | int lttng_tracker_pids_list_open(struct inode *inode, struct file *file) | |
1074 | { | |
1075 | return seq_open(file, <tng_tracker_pids_list_seq_ops); | |
1076 | } | |
1077 | ||
1078 | static | |
1079 | int lttng_tracker_pids_list_release(struct inode *inode, struct file *file) | |
1080 | { | |
1081 | struct seq_file *m = file->private_data; | |
1082 | struct lttng_session *session = m->private; | |
1083 | int ret; | |
1084 | ||
1085 | WARN_ON_ONCE(!session); | |
1086 | ret = seq_release(inode, file); | |
1087 | if (!ret && session) | |
1088 | fput(session->file); | |
1089 | return ret; | |
1090 | } | |
1091 | ||
1092 | const struct file_operations lttng_tracker_pids_list_fops = { | |
1093 | .owner = THIS_MODULE, | |
1094 | .open = lttng_tracker_pids_list_open, | |
1095 | .read = seq_read, | |
1096 | .llseek = seq_lseek, | |
1097 | .release = lttng_tracker_pids_list_release, | |
1098 | }; | |
1099 | ||
1100 | int lttng_session_list_tracker_pids(struct lttng_session *session) | |
1101 | { | |
1102 | struct file *tracker_pids_list_file; | |
1103 | struct seq_file *m; | |
1104 | int file_fd, ret; | |
1105 | ||
1106 | file_fd = lttng_get_unused_fd(); | |
1107 | if (file_fd < 0) { | |
1108 | ret = file_fd; | |
1109 | goto fd_error; | |
1110 | } | |
1111 | ||
1112 | tracker_pids_list_file = anon_inode_getfile("[lttng_tracker_pids_list]", | |
1113 | <tng_tracker_pids_list_fops, | |
1114 | NULL, O_RDWR); | |
1115 | if (IS_ERR(tracker_pids_list_file)) { | |
1116 | ret = PTR_ERR(tracker_pids_list_file); | |
1117 | goto file_error; | |
1118 | } | |
5ad19305 MJ |
1119 | if (!atomic_long_add_unless(&session->file->f_count, 1, LONG_MAX)) { |
1120 | ret = -EOVERFLOW; | |
9c1f4643 MD |
1121 | goto refcount_error; |
1122 | } | |
7e6f9ef6 MD |
1123 | ret = lttng_tracker_pids_list_fops.open(NULL, tracker_pids_list_file); |
1124 | if (ret < 0) | |
1125 | goto open_error; | |
1126 | m = tracker_pids_list_file->private_data; | |
1127 | m->private = session; | |
1128 | fd_install(file_fd, tracker_pids_list_file); | |
7e6f9ef6 MD |
1129 | |
1130 | return file_fd; | |
1131 | ||
1132 | open_error: | |
9c1f4643 MD |
1133 | atomic_long_dec(&session->file->f_count); |
1134 | refcount_error: | |
7e6f9ef6 MD |
1135 | fput(tracker_pids_list_file); |
1136 | file_error: | |
1137 | put_unused_fd(file_fd); | |
1138 | fd_error: | |
1139 | return ret; | |
1140 | } | |
1141 | ||
3c997079 MD |
1142 | /* |
1143 | * Enabler management. | |
1144 | */ | |
1145 | static | |
4993071a PP |
1146 | int lttng_match_enabler_star_glob(const char *desc_name, |
1147 | const char *pattern) | |
3c997079 | 1148 | { |
cbc19040 MD |
1149 | if (!strutils_star_glob_match(pattern, LTTNG_SIZE_MAX, |
1150 | desc_name, LTTNG_SIZE_MAX)) | |
3c997079 MD |
1151 | return 0; |
1152 | return 1; | |
1153 | } | |
1154 | ||
1155 | static | |
4ecb5ad5 MD |
1156 | int lttng_match_enabler_name(const char *desc_name, |
1157 | const char *name) | |
3c997079 | 1158 | { |
4ecb5ad5 | 1159 | if (strcmp(desc_name, name)) |
3c997079 MD |
1160 | return 0; |
1161 | return 1; | |
1162 | } | |
1163 | ||
1164 | static | |
1165 | int lttng_desc_match_enabler(const struct lttng_event_desc *desc, | |
1166 | struct lttng_enabler *enabler) | |
1167 | { | |
4ecb5ad5 MD |
1168 | const char *desc_name, *enabler_name; |
1169 | ||
1170 | enabler_name = enabler->event_param.name; | |
1171 | switch (enabler->event_param.instrumentation) { | |
1172 | case LTTNG_KERNEL_TRACEPOINT: | |
1173 | desc_name = desc->name; | |
1174 | break; | |
1175 | case LTTNG_KERNEL_SYSCALL: | |
1176 | desc_name = desc->name; | |
1177 | if (!strncmp(desc_name, "compat_", strlen("compat_"))) | |
1178 | desc_name += strlen("compat_"); | |
1179 | if (!strncmp(desc_name, "syscall_exit_", | |
1180 | strlen("syscall_exit_"))) { | |
1181 | desc_name += strlen("syscall_exit_"); | |
1182 | } else if (!strncmp(desc_name, "syscall_entry_", | |
1183 | strlen("syscall_entry_"))) { | |
1184 | desc_name += strlen("syscall_entry_"); | |
1185 | } else { | |
1186 | WARN_ON_ONCE(1); | |
1187 | return -EINVAL; | |
1188 | } | |
1189 | break; | |
1190 | default: | |
1191 | WARN_ON_ONCE(1); | |
1192 | return -EINVAL; | |
1193 | } | |
3c997079 | 1194 | switch (enabler->type) { |
4993071a PP |
1195 | case LTTNG_ENABLER_STAR_GLOB: |
1196 | return lttng_match_enabler_star_glob(desc_name, enabler_name); | |
3c997079 | 1197 | case LTTNG_ENABLER_NAME: |
4ecb5ad5 | 1198 | return lttng_match_enabler_name(desc_name, enabler_name); |
3c997079 MD |
1199 | default: |
1200 | return -EINVAL; | |
1201 | } | |
1202 | } | |
1203 | ||
1204 | static | |
1205 | int lttng_event_match_enabler(struct lttng_event *event, | |
1206 | struct lttng_enabler *enabler) | |
1207 | { | |
33a39a3c MD |
1208 | if (enabler->event_param.instrumentation != event->instrumentation) |
1209 | return 0; | |
3c997079 MD |
1210 | if (lttng_desc_match_enabler(event->desc, enabler) |
1211 | && event->chan == enabler->chan) | |
1212 | return 1; | |
1213 | else | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | static | |
1218 | struct lttng_enabler_ref *lttng_event_enabler_ref(struct lttng_event *event, | |
1219 | struct lttng_enabler *enabler) | |
1220 | { | |
1221 | struct lttng_enabler_ref *enabler_ref; | |
1222 | ||
1223 | list_for_each_entry(enabler_ref, | |
1224 | &event->enablers_ref_head, node) { | |
1225 | if (enabler_ref->ref == enabler) | |
1226 | return enabler_ref; | |
1227 | } | |
1228 | return NULL; | |
1229 | } | |
1230 | ||
3c997079 | 1231 | static |
33a39a3c | 1232 | void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler) |
3c997079 MD |
1233 | { |
1234 | struct lttng_session *session = enabler->chan->session; | |
1235 | struct lttng_probe_desc *probe_desc; | |
1236 | const struct lttng_event_desc *desc; | |
1237 | int i; | |
1238 | struct list_head *probe_list; | |
1239 | ||
1240 | probe_list = lttng_get_probe_list_head(); | |
1241 | /* | |
1242 | * For each probe event, if we find that a probe event matches | |
1243 | * our enabler, create an associated lttng_event if not | |
1244 | * already present. | |
1245 | */ | |
1246 | list_for_each_entry(probe_desc, probe_list, head) { | |
1247 | for (i = 0; i < probe_desc->nr_events; i++) { | |
1248 | int found = 0; | |
1249 | struct hlist_head *head; | |
1250 | const char *event_name; | |
1251 | size_t name_len; | |
1252 | uint32_t hash; | |
1253 | struct lttng_event *event; | |
1254 | ||
1255 | desc = probe_desc->event_desc[i]; | |
1256 | if (!lttng_desc_match_enabler(desc, enabler)) | |
1257 | continue; | |
1258 | event_name = desc->name; | |
1259 | name_len = strlen(event_name); | |
1260 | ||
1261 | /* | |
1262 | * Check if already created. | |
1263 | */ | |
1264 | hash = jhash(event_name, name_len, 0); | |
1265 | head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)]; | |
d216ecae | 1266 | lttng_hlist_for_each_entry(event, head, hlist) { |
3c997079 MD |
1267 | if (event->desc == desc |
1268 | && event->chan == enabler->chan) | |
1269 | found = 1; | |
1270 | } | |
1271 | if (found) | |
1272 | continue; | |
1273 | ||
1274 | /* | |
1275 | * We need to create an event for this | |
1276 | * event probe. | |
1277 | */ | |
1278 | event = _lttng_event_create(enabler->chan, | |
1279 | NULL, NULL, desc, | |
1280 | LTTNG_KERNEL_TRACEPOINT); | |
1281 | if (!event) { | |
1282 | printk(KERN_INFO "Unable to create event %s\n", | |
1283 | probe_desc->event_desc[i]->name); | |
1284 | } | |
1285 | } | |
1286 | } | |
1287 | } | |
1288 | ||
33a39a3c MD |
1289 | static |
1290 | void lttng_create_syscall_if_missing(struct lttng_enabler *enabler) | |
1291 | { | |
1292 | int ret; | |
1293 | ||
1294 | ret = lttng_syscalls_register(enabler->chan, NULL); | |
1295 | WARN_ON_ONCE(ret); | |
1296 | } | |
1297 | ||
1298 | /* | |
1299 | * Create struct lttng_event if it is missing and present in the list of | |
1300 | * tracepoint probes. | |
1301 | * Should be called with sessions mutex held. | |
1302 | */ | |
1303 | static | |
1304 | void lttng_create_event_if_missing(struct lttng_enabler *enabler) | |
1305 | { | |
1306 | switch (enabler->event_param.instrumentation) { | |
1307 | case LTTNG_KERNEL_TRACEPOINT: | |
1308 | lttng_create_tracepoint_if_missing(enabler); | |
1309 | break; | |
1310 | case LTTNG_KERNEL_SYSCALL: | |
1311 | lttng_create_syscall_if_missing(enabler); | |
1312 | break; | |
1313 | default: | |
1314 | WARN_ON_ONCE(1); | |
1315 | break; | |
1316 | } | |
1317 | } | |
1318 | ||
3c997079 MD |
1319 | /* |
1320 | * Create events associated with an enabler (if not already present), | |
1321 | * and add backward reference from the event to the enabler. | |
1322 | * Should be called with sessions mutex held. | |
1323 | */ | |
1324 | static | |
1325 | int lttng_enabler_ref_events(struct lttng_enabler *enabler) | |
1326 | { | |
1327 | struct lttng_session *session = enabler->chan->session; | |
1328 | struct lttng_event *event; | |
1329 | ||
1330 | /* First ensure that probe events are created for this enabler. */ | |
1331 | lttng_create_event_if_missing(enabler); | |
1332 | ||
1333 | /* For each event matching enabler in session event list. */ | |
1334 | list_for_each_entry(event, &session->events, list) { | |
1335 | struct lttng_enabler_ref *enabler_ref; | |
1336 | ||
1337 | if (!lttng_event_match_enabler(event, enabler)) | |
1338 | continue; | |
1339 | enabler_ref = lttng_event_enabler_ref(event, enabler); | |
1340 | if (!enabler_ref) { | |
1341 | /* | |
1342 | * If no backward ref, create it. | |
1343 | * Add backward ref from event to enabler. | |
1344 | */ | |
1345 | enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL); | |
1346 | if (!enabler_ref) | |
1347 | return -ENOMEM; | |
1348 | enabler_ref->ref = enabler; | |
1349 | list_add(&enabler_ref->node, | |
1350 | &event->enablers_ref_head); | |
1351 | } | |
1352 | ||
f127e61e MD |
1353 | /* |
1354 | * Link filter bytecodes if not linked yet. | |
1355 | */ | |
1356 | lttng_enabler_event_link_bytecode(event, enabler); | |
1357 | ||
3c997079 MD |
1358 | /* TODO: merge event context. */ |
1359 | } | |
1360 | return 0; | |
1361 | } | |
1362 | ||
1363 | /* | |
1364 | * Called at module load: connect the probe on all enablers matching | |
1365 | * this event. | |
1366 | * Called with sessions lock held. | |
1367 | */ | |
1368 | int lttng_fix_pending_events(void) | |
1369 | { | |
1370 | struct lttng_session *session; | |
1371 | ||
1372 | list_for_each_entry(session, &sessions, list) | |
1373 | lttng_session_lazy_sync_enablers(session); | |
1374 | return 0; | |
1375 | } | |
1376 | ||
1377 | struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type, | |
1378 | struct lttng_kernel_event *event_param, | |
1379 | struct lttng_channel *chan) | |
1380 | { | |
1381 | struct lttng_enabler *enabler; | |
1382 | ||
1383 | enabler = kzalloc(sizeof(*enabler), GFP_KERNEL); | |
1384 | if (!enabler) | |
1385 | return NULL; | |
1386 | enabler->type = type; | |
07dfc1d0 | 1387 | INIT_LIST_HEAD(&enabler->filter_bytecode_head); |
3c997079 MD |
1388 | memcpy(&enabler->event_param, event_param, |
1389 | sizeof(enabler->event_param)); | |
1390 | enabler->chan = chan; | |
1391 | /* ctx left NULL */ | |
4fd19d8a | 1392 | enabler->enabled = 0; |
3c997079 MD |
1393 | enabler->evtype = LTTNG_TYPE_ENABLER; |
1394 | mutex_lock(&sessions_mutex); | |
1395 | list_add(&enabler->node, &enabler->chan->session->enablers_head); | |
1396 | lttng_session_lazy_sync_enablers(enabler->chan->session); | |
1397 | mutex_unlock(&sessions_mutex); | |
1398 | return enabler; | |
1399 | } | |
1400 | ||
1401 | int lttng_enabler_enable(struct lttng_enabler *enabler) | |
1402 | { | |
1403 | mutex_lock(&sessions_mutex); | |
1404 | enabler->enabled = 1; | |
1405 | lttng_session_lazy_sync_enablers(enabler->chan->session); | |
1406 | mutex_unlock(&sessions_mutex); | |
1407 | return 0; | |
1408 | } | |
1409 | ||
1410 | int lttng_enabler_disable(struct lttng_enabler *enabler) | |
1411 | { | |
1412 | mutex_lock(&sessions_mutex); | |
1413 | enabler->enabled = 0; | |
1414 | lttng_session_lazy_sync_enablers(enabler->chan->session); | |
1415 | mutex_unlock(&sessions_mutex); | |
1416 | return 0; | |
1417 | } | |
1418 | ||
07dfc1d0 MD |
1419 | int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler, |
1420 | struct lttng_kernel_filter_bytecode __user *bytecode) | |
1421 | { | |
1422 | struct lttng_filter_bytecode_node *bytecode_node; | |
1423 | uint32_t bytecode_len; | |
1424 | int ret; | |
1425 | ||
1426 | ret = get_user(bytecode_len, &bytecode->len); | |
1427 | if (ret) | |
1428 | return ret; | |
1429 | bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len, | |
1430 | GFP_KERNEL); | |
1431 | if (!bytecode_node) | |
1432 | return -ENOMEM; | |
1433 | ret = copy_from_user(&bytecode_node->bc, bytecode, | |
1434 | sizeof(*bytecode) + bytecode_len); | |
1435 | if (ret) | |
1436 | goto error_free; | |
1437 | bytecode_node->enabler = enabler; | |
1438 | /* Enforce length based on allocated size */ | |
1439 | bytecode_node->bc.len = bytecode_len; | |
1440 | list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head); | |
1441 | lttng_session_lazy_sync_enablers(enabler->chan->session); | |
1442 | return 0; | |
1443 | ||
1444 | error_free: | |
1445 | kfree(bytecode_node); | |
1446 | return ret; | |
1447 | } | |
1448 | ||
3aed4dca FD |
1449 | int lttng_event_add_callsite(struct lttng_event *event, |
1450 | struct lttng_kernel_event_callsite __user *callsite) | |
1451 | { | |
1452 | ||
1453 | switch (event->instrumentation) { | |
1454 | case LTTNG_KERNEL_UPROBE: | |
1455 | return lttng_uprobes_add_callsite(event, callsite); | |
1456 | default: | |
1457 | return -EINVAL; | |
1458 | } | |
1459 | } | |
1460 | ||
3c997079 MD |
1461 | int lttng_enabler_attach_context(struct lttng_enabler *enabler, |
1462 | struct lttng_kernel_context *context_param) | |
1463 | { | |
1464 | return -ENOSYS; | |
1465 | } | |
1466 | ||
1467 | static | |
1468 | void lttng_enabler_destroy(struct lttng_enabler *enabler) | |
1469 | { | |
07dfc1d0 MD |
1470 | struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node; |
1471 | ||
1472 | /* Destroy filter bytecode */ | |
1473 | list_for_each_entry_safe(filter_node, tmp_filter_node, | |
1474 | &enabler->filter_bytecode_head, node) { | |
1475 | kfree(filter_node); | |
1476 | } | |
1477 | ||
3c997079 MD |
1478 | /* Destroy contexts */ |
1479 | lttng_destroy_context(enabler->ctx); | |
1480 | ||
1481 | list_del(&enabler->node); | |
1482 | kfree(enabler); | |
1483 | } | |
1484 | ||
1485 | /* | |
1486 | * lttng_session_sync_enablers should be called just before starting a | |
1487 | * session. | |
1488 | * Should be called with sessions mutex held. | |
1489 | */ | |
1490 | static | |
1491 | void lttng_session_sync_enablers(struct lttng_session *session) | |
1492 | { | |
1493 | struct lttng_enabler *enabler; | |
1494 | struct lttng_event *event; | |
1495 | ||
1496 | list_for_each_entry(enabler, &session->enablers_head, node) | |
1497 | lttng_enabler_ref_events(enabler); | |
1498 | /* | |
1499 | * For each event, if at least one of its enablers is enabled, | |
1500 | * and its channel and session transient states are enabled, we | |
1501 | * enable the event, else we disable it. | |
1502 | */ | |
1503 | list_for_each_entry(event, &session->events, list) { | |
1504 | struct lttng_enabler_ref *enabler_ref; | |
07dfc1d0 MD |
1505 | struct lttng_bytecode_runtime *runtime; |
1506 | int enabled = 0, has_enablers_without_bytecode = 0; | |
3c997079 | 1507 | |
4ecb5ad5 MD |
1508 | switch (event->instrumentation) { |
1509 | case LTTNG_KERNEL_TRACEPOINT: | |
1510 | case LTTNG_KERNEL_SYSCALL: | |
3c997079 MD |
1511 | /* Enable events */ |
1512 | list_for_each_entry(enabler_ref, | |
1513 | &event->enablers_ref_head, node) { | |
1514 | if (enabler_ref->ref->enabled) { | |
1515 | enabled = 1; | |
1516 | break; | |
1517 | } | |
1518 | } | |
4ecb5ad5 MD |
1519 | break; |
1520 | default: | |
3c997079 MD |
1521 | /* Not handled with lazy sync. */ |
1522 | continue; | |
1523 | } | |
1524 | /* | |
1525 | * Enabled state is based on union of enablers, with | |
1526 | * intesection of session and channel transient enable | |
1527 | * states. | |
1528 | */ | |
1529 | enabled = enabled && session->tstate && event->chan->tstate; | |
1530 | ||
a8f2d0c7 | 1531 | WRITE_ONCE(event->enabled, enabled); |
3c997079 MD |
1532 | /* |
1533 | * Sync tracepoint registration with event enabled | |
1534 | * state. | |
1535 | */ | |
1536 | if (enabled) { | |
1537 | register_event(event); | |
1538 | } else { | |
1539 | _lttng_event_unregister(event); | |
1540 | } | |
07dfc1d0 MD |
1541 | |
1542 | /* Check if has enablers without bytecode enabled */ | |
1543 | list_for_each_entry(enabler_ref, | |
1544 | &event->enablers_ref_head, node) { | |
1545 | if (enabler_ref->ref->enabled | |
1546 | && list_empty(&enabler_ref->ref->filter_bytecode_head)) { | |
1547 | has_enablers_without_bytecode = 1; | |
1548 | break; | |
1549 | } | |
1550 | } | |
1551 | event->has_enablers_without_bytecode = | |
1552 | has_enablers_without_bytecode; | |
1553 | ||
1554 | /* Enable filters */ | |
1555 | list_for_each_entry(runtime, | |
f127e61e | 1556 | &event->bytecode_runtime_head, node) |
07dfc1d0 | 1557 | lttng_filter_sync_state(runtime); |
3c997079 MD |
1558 | } |
1559 | } | |
1560 | ||
1561 | /* | |
1562 | * Apply enablers to session events, adding events to session if need | |
1563 | * be. It is required after each modification applied to an active | |
1564 | * session, and right before session "start". | |
1565 | * "lazy" sync means we only sync if required. | |
1566 | * Should be called with sessions mutex held. | |
1567 | */ | |
1568 | static | |
1569 | void lttng_session_lazy_sync_enablers(struct lttng_session *session) | |
1570 | { | |
1571 | /* We can skip if session is not active */ | |
1572 | if (!session->active) | |
1573 | return; | |
1574 | lttng_session_sync_enablers(session); | |
1575 | } | |
1576 | ||
1ec3f75a | 1577 | /* |
d83004aa JD |
1578 | * Serialize at most one packet worth of metadata into a metadata |
1579 | * channel. | |
92d9f5e6 MD |
1580 | * We grab the metadata cache mutex to get exclusive access to our metadata |
1581 | * buffer and to the metadata cache. Exclusive access to the metadata buffer | |
1582 | * allows us to do racy operations such as looking for remaining space left in | |
1583 | * packet and write, since mutual exclusion protects us from concurrent writes. | |
1584 | * Mutual exclusion on the metadata cache allow us to read the cache content | |
1585 | * without racing against reallocation of the cache by updates. | |
35097f36 JD |
1586 | * Returns the number of bytes written in the channel, 0 if no data |
1587 | * was written and a negative value on error. | |
1ec3f75a | 1588 | */ |
b3b8072b MD |
1589 | int lttng_metadata_output_channel(struct lttng_metadata_stream *stream, |
1590 | struct channel *chan) | |
d83004aa JD |
1591 | { |
1592 | struct lib_ring_buffer_ctx ctx; | |
1593 | int ret = 0; | |
1594 | size_t len, reserve_len; | |
1595 | ||
f613e3e6 | 1596 | /* |
92d9f5e6 MD |
1597 | * Ensure we support mutiple get_next / put sequences followed by |
1598 | * put_next. The metadata cache lock protects reading the metadata | |
1599 | * cache. It can indeed be read concurrently by "get_next_subbuf" and | |
1600 | * "flush" operations on the buffer invoked by different processes. | |
1601 | * Moreover, since the metadata cache memory can be reallocated, we | |
1602 | * need to have exclusive access against updates even though we only | |
1603 | * read it. | |
f613e3e6 | 1604 | */ |
92d9f5e6 | 1605 | mutex_lock(&stream->metadata_cache->lock); |
f613e3e6 MD |
1606 | WARN_ON(stream->metadata_in < stream->metadata_out); |
1607 | if (stream->metadata_in != stream->metadata_out) | |
de23d59d | 1608 | goto end; |
f613e3e6 | 1609 | |
9616f0bf JD |
1610 | /* Metadata regenerated, change the version. */ |
1611 | if (stream->metadata_cache->version != stream->version) | |
1612 | stream->version = stream->metadata_cache->version; | |
1613 | ||
d83004aa | 1614 | len = stream->metadata_cache->metadata_written - |
f613e3e6 | 1615 | stream->metadata_in; |
9de2c215 | 1616 | if (!len) |
de23d59d | 1617 | goto end; |
d83004aa | 1618 | reserve_len = min_t(size_t, |
b3b8072b | 1619 | stream->transport->ops.packet_avail_size(chan), |
d83004aa | 1620 | len); |
b3b8072b | 1621 | lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len, |
d83004aa JD |
1622 | sizeof(char), -1); |
1623 | /* | |
1624 | * If reservation failed, return an error to the caller. | |
1625 | */ | |
b3b8072b | 1626 | ret = stream->transport->ops.event_reserve(&ctx, 0); |
d83004aa JD |
1627 | if (ret != 0) { |
1628 | printk(KERN_WARNING "LTTng: Metadata event reservation failed\n"); | |
1629 | goto end; | |
1630 | } | |
b3b8072b | 1631 | stream->transport->ops.event_write(&ctx, |
f613e3e6 | 1632 | stream->metadata_cache->data + stream->metadata_in, |
d83004aa | 1633 | reserve_len); |
b3b8072b | 1634 | stream->transport->ops.event_commit(&ctx); |
f613e3e6 | 1635 | stream->metadata_in += reserve_len; |
d83004aa JD |
1636 | ret = reserve_len; |
1637 | ||
1638 | end: | |
92d9f5e6 | 1639 | mutex_unlock(&stream->metadata_cache->lock); |
d83004aa JD |
1640 | return ret; |
1641 | } | |
1642 | ||
1643 | /* | |
1644 | * Write the metadata to the metadata cache. | |
1645 | * Must be called with sessions_mutex held. | |
92d9f5e6 MD |
1646 | * The metadata cache lock protects us from concurrent read access from |
1647 | * thread outputting metadata content to ring buffer. | |
d83004aa | 1648 | */ |
a90917c3 | 1649 | int lttng_metadata_printf(struct lttng_session *session, |
c099397a MD |
1650 | const char *fmt, ...) |
1651 | { | |
c099397a | 1652 | char *str; |
d83004aa | 1653 | size_t len; |
c099397a | 1654 | va_list ap; |
d83004aa | 1655 | struct lttng_metadata_stream *stream; |
c099397a | 1656 | |
a8f2d0c7 | 1657 | WARN_ON_ONCE(!READ_ONCE(session->active)); |
c099397a MD |
1658 | |
1659 | va_start(ap, fmt); | |
1660 | str = kvasprintf(GFP_KERNEL, fmt, ap); | |
1661 | va_end(ap); | |
1662 | if (!str) | |
1663 | return -ENOMEM; | |
1664 | ||
1ec3f75a | 1665 | len = strlen(str); |
92d9f5e6 | 1666 | mutex_lock(&session->metadata_cache->lock); |
d83004aa JD |
1667 | if (session->metadata_cache->metadata_written + len > |
1668 | session->metadata_cache->cache_alloc) { | |
1669 | char *tmp_cache_realloc; | |
1670 | unsigned int tmp_cache_alloc_size; | |
1671 | ||
1672 | tmp_cache_alloc_size = max_t(unsigned int, | |
1673 | session->metadata_cache->cache_alloc + len, | |
1674 | session->metadata_cache->cache_alloc << 1); | |
f6da044f | 1675 | tmp_cache_realloc = lttng_vzalloc(tmp_cache_alloc_size); |
d83004aa JD |
1676 | if (!tmp_cache_realloc) |
1677 | goto err; | |
a606b6e8 ML |
1678 | if (session->metadata_cache->data) { |
1679 | memcpy(tmp_cache_realloc, | |
1680 | session->metadata_cache->data, | |
1681 | session->metadata_cache->cache_alloc); | |
1682 | vfree(session->metadata_cache->data); | |
1683 | } | |
1684 | ||
d83004aa JD |
1685 | session->metadata_cache->cache_alloc = tmp_cache_alloc_size; |
1686 | session->metadata_cache->data = tmp_cache_realloc; | |
c099397a | 1687 | } |
d83004aa JD |
1688 | memcpy(session->metadata_cache->data + |
1689 | session->metadata_cache->metadata_written, | |
1690 | str, len); | |
1691 | session->metadata_cache->metadata_written += len; | |
92d9f5e6 | 1692 | mutex_unlock(&session->metadata_cache->lock); |
c099397a | 1693 | kfree(str); |
d83004aa JD |
1694 | |
1695 | list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) | |
1696 | wake_up_interruptible(&stream->read_wait); | |
1697 | ||
1698 | return 0; | |
1699 | ||
1700 | err: | |
92d9f5e6 | 1701 | mutex_unlock(&session->metadata_cache->lock); |
d83004aa JD |
1702 | kfree(str); |
1703 | return -ENOMEM; | |
c099397a MD |
1704 | } |
1705 | ||
f513b2bf MD |
1706 | static |
1707 | int print_tabs(struct lttng_session *session, size_t nesting) | |
1708 | { | |
1709 | size_t i; | |
1710 | ||
1711 | for (i = 0; i < nesting; i++) { | |
1712 | int ret; | |
1713 | ||
1714 | ret = lttng_metadata_printf(session, " "); | |
1715 | if (ret) { | |
1716 | return ret; | |
1717 | } | |
1718 | } | |
1719 | return 0; | |
1720 | } | |
1721 | ||
1722 | /* | |
1723 | * Must be called with sessions_mutex held. | |
1724 | */ | |
1725 | static | |
1726 | int _lttng_struct_type_statedump(struct lttng_session *session, | |
1727 | const struct lttng_type *type, | |
1728 | size_t nesting) | |
1729 | { | |
1730 | int ret; | |
1731 | uint32_t i, nr_fields; | |
1732 | ||
1733 | ret = print_tabs(session, nesting); | |
1734 | if (ret) | |
1735 | return ret; | |
1736 | ret = lttng_metadata_printf(session, | |
1737 | "struct {\n"); | |
1738 | if (ret) | |
1739 | return ret; | |
1740 | nr_fields = type->u._struct.nr_fields; | |
1741 | for (i = 0; i < nr_fields; i++) { | |
1742 | const struct lttng_event_field *iter_field; | |
1743 | ||
1744 | iter_field = &type->u._struct.fields[i]; | |
1745 | ret = _lttng_field_statedump(session, iter_field, nesting + 1); | |
1746 | if (ret) | |
1747 | return ret; | |
1748 | } | |
1749 | ret = print_tabs(session, nesting); | |
1750 | if (ret) | |
1751 | return ret; | |
1752 | ret = lttng_metadata_printf(session, | |
1753 | "}"); | |
1754 | return ret; | |
1755 | } | |
1756 | ||
1757 | /* | |
1758 | * Must be called with sessions_mutex held. | |
1759 | */ | |
1760 | static | |
1761 | int _lttng_struct_statedump(struct lttng_session *session, | |
1762 | const struct lttng_event_field *field, | |
1763 | size_t nesting) | |
1764 | { | |
1765 | int ret; | |
1766 | ||
1767 | ret = _lttng_struct_type_statedump(session, | |
1768 | &field->type, nesting); | |
1769 | if (ret) | |
1770 | return ret; | |
1771 | ret = lttng_metadata_printf(session, | |
1772 | "_%s;\n", | |
1773 | field->name); | |
1774 | return ret; | |
1775 | } | |
1776 | ||
65c85aa6 MD |
1777 | /* |
1778 | * Must be called with sessions_mutex held. | |
1779 | */ | |
1780 | static | |
1781 | int _lttng_variant_type_statedump(struct lttng_session *session, | |
1782 | const struct lttng_type *type, | |
1783 | size_t nesting) | |
1784 | { | |
1785 | int ret; | |
1786 | uint32_t i, nr_choices; | |
1787 | ||
1788 | ret = print_tabs(session, nesting); | |
1789 | if (ret) | |
1790 | return ret; | |
1791 | ret = lttng_metadata_printf(session, | |
1792 | "variant <_%s> {\n", | |
1793 | type->u.variant.tag_name); | |
1794 | if (ret) | |
1795 | return ret; | |
1796 | nr_choices = type->u.variant.nr_choices; | |
1797 | for (i = 0; i < nr_choices; i++) { | |
1798 | const struct lttng_event_field *iter_field; | |
1799 | ||
1800 | iter_field = &type->u.variant.choices[i]; | |
1801 | ret = _lttng_field_statedump(session, iter_field, nesting + 1); | |
1802 | if (ret) | |
1803 | return ret; | |
1804 | } | |
1805 | ret = print_tabs(session, nesting); | |
1806 | if (ret) | |
1807 | return ret; | |
1808 | ret = lttng_metadata_printf(session, | |
1809 | "}"); | |
1810 | return ret; | |
1811 | } | |
1812 | ||
1813 | /* | |
1814 | * Must be called with sessions_mutex held. | |
1815 | */ | |
1816 | static | |
1817 | int _lttng_variant_statedump(struct lttng_session *session, | |
1818 | const struct lttng_event_field *field, | |
1819 | size_t nesting) | |
1820 | { | |
1821 | int ret; | |
1822 | ||
1823 | ret = _lttng_variant_type_statedump(session, | |
1824 | &field->type, nesting); | |
1825 | if (ret) | |
1826 | return ret; | |
1827 | ret = lttng_metadata_printf(session, | |
1828 | "_%s;\n", | |
1829 | field->name); | |
1830 | return ret; | |
1831 | } | |
1832 | ||
f513b2bf MD |
1833 | /* |
1834 | * Must be called with sessions_mutex held. | |
1835 | */ | |
1836 | static | |
1837 | int _lttng_array_compound_statedump(struct lttng_session *session, | |
1838 | const struct lttng_event_field *field, | |
1839 | size_t nesting) | |
1840 | { | |
1841 | int ret; | |
1842 | const struct lttng_type *elem_type; | |
1843 | ||
65c85aa6 | 1844 | /* Only array of structures and variants are currently supported. */ |
f513b2bf MD |
1845 | elem_type = field->type.u.array_compound.elem_type; |
1846 | switch (elem_type->atype) { | |
1847 | case atype_struct: | |
1848 | ret = _lttng_struct_type_statedump(session, elem_type, nesting); | |
1849 | if (ret) | |
1850 | return ret; | |
1851 | break; | |
65c85aa6 MD |
1852 | case atype_variant: |
1853 | ret = _lttng_variant_type_statedump(session, elem_type, nesting); | |
1854 | if (ret) | |
1855 | return ret; | |
1856 | break; | |
f513b2bf MD |
1857 | default: |
1858 | return -EINVAL; | |
1859 | } | |
1860 | ret = lttng_metadata_printf(session, | |
1861 | " _%s[%u];\n", | |
1862 | field->name, | |
1863 | field->type.u.array_compound.length); | |
1864 | return ret; | |
1865 | } | |
1866 | ||
1867 | /* | |
1868 | * Must be called with sessions_mutex held. | |
1869 | */ | |
1870 | static | |
1871 | int _lttng_sequence_compound_statedump(struct lttng_session *session, | |
1872 | const struct lttng_event_field *field, | |
1873 | size_t nesting) | |
1874 | { | |
1875 | int ret; | |
1876 | const char *length_name; | |
1877 | const struct lttng_type *elem_type; | |
1878 | ||
1879 | length_name = field->type.u.sequence_compound.length_name; | |
1880 | ||
65c85aa6 | 1881 | /* Only array of structures and variants are currently supported. */ |
f513b2bf MD |
1882 | elem_type = field->type.u.sequence_compound.elem_type; |
1883 | switch (elem_type->atype) { | |
1884 | case atype_struct: | |
1885 | ret = _lttng_struct_type_statedump(session, elem_type, nesting); | |
1886 | if (ret) | |
1887 | return ret; | |
1888 | break; | |
65c85aa6 MD |
1889 | case atype_variant: |
1890 | ret = _lttng_variant_type_statedump(session, elem_type, nesting); | |
1891 | if (ret) | |
1892 | return ret; | |
1893 | break; | |
f513b2bf MD |
1894 | default: |
1895 | return -EINVAL; | |
1896 | } | |
1897 | ret = lttng_metadata_printf(session, | |
1898 | " _%s[ _%s ];\n", | |
1899 | field->name, | |
1900 | length_name); | |
1901 | return ret; | |
1902 | } | |
1903 | ||
141ddf28 MD |
1904 | /* |
1905 | * Must be called with sessions_mutex held. | |
1906 | */ | |
1907 | static | |
1908 | int _lttng_enum_statedump(struct lttng_session *session, | |
1909 | const struct lttng_event_field *field, | |
1910 | size_t nesting) | |
1911 | { | |
1912 | const struct lttng_enum_desc *enum_desc; | |
1913 | const struct lttng_integer_type *container_type; | |
1914 | int ret; | |
1915 | unsigned int i, nr_entries; | |
1916 | ||
1917 | enum_desc = field->type.u.basic.enumeration.desc; | |
1918 | container_type = &field->type.u.basic.enumeration.container_type; | |
1919 | nr_entries = enum_desc->nr_entries; | |
1920 | ||
1921 | ret = print_tabs(session, nesting); | |
1922 | if (ret) | |
1923 | goto end; | |
1924 | ret = lttng_metadata_printf(session, | |
578e68ec | 1925 | "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n", |
141ddf28 MD |
1926 | container_type->size, |
1927 | container_type->alignment, | |
1928 | container_type->signedness, | |
1929 | (container_type->encoding == lttng_encode_none) | |
1930 | ? "none" | |
1931 | : (container_type->encoding == lttng_encode_UTF8) | |
1932 | ? "UTF8" | |
1933 | : "ASCII", | |
578e68ec GB |
1934 | container_type->base, |
1935 | #if __BYTE_ORDER == __BIG_ENDIAN | |
1936 | container_type->reverse_byte_order ? " byte_order = le;" : "" | |
1937 | #else | |
1938 | container_type->reverse_byte_order ? " byte_order = be;" : "" | |
1939 | #endif | |
1940 | ); | |
141ddf28 MD |
1941 | if (ret) |
1942 | goto end; | |
1943 | /* Dump all entries */ | |
1944 | for (i = 0; i < nr_entries; i++) { | |
1945 | const struct lttng_enum_entry *entry = &enum_desc->entries[i]; | |
1946 | int j, len; | |
1947 | ||
1948 | ret = print_tabs(session, nesting + 1); | |
1949 | if (ret) | |
1950 | goto end; | |
1951 | ret = lttng_metadata_printf(session, | |
1952 | "\""); | |
1953 | if (ret) | |
1954 | goto end; | |
1955 | len = strlen(entry->string); | |
1956 | /* Escape the character '"' */ | |
1957 | for (j = 0; j < len; j++) { | |
1958 | char c = entry->string[j]; | |
1959 | ||
1960 | switch (c) { | |
1961 | case '"': | |
1962 | ret = lttng_metadata_printf(session, | |
1963 | "\\\""); | |
1964 | break; | |
1965 | case '\\': | |
1966 | ret = lttng_metadata_printf(session, | |
1967 | "\\\\"); | |
1968 | break; | |
1969 | default: | |
1970 | ret = lttng_metadata_printf(session, | |
1971 | "%c", c); | |
1972 | break; | |
1973 | } | |
1974 | if (ret) | |
1975 | goto end; | |
1976 | } | |
08ad1061 PP |
1977 | ret = lttng_metadata_printf(session, "\""); |
1978 | if (ret) | |
1979 | goto end; | |
1980 | ||
1981 | if (entry->options.is_auto) { | |
1982 | ret = lttng_metadata_printf(session, ",\n"); | |
1983 | if (ret) | |
1984 | goto end; | |
141ddf28 | 1985 | } else { |
7c559490 MD |
1986 | ret = lttng_metadata_printf(session, |
1987 | " = "); | |
1988 | if (ret) | |
1989 | goto end; | |
1990 | if (entry->start.signedness) | |
141ddf28 | 1991 | ret = lttng_metadata_printf(session, |
7c559490 MD |
1992 | "%lld", (long long) entry->start.value); |
1993 | else | |
1994 | ret = lttng_metadata_printf(session, | |
1995 | "%llu", entry->start.value); | |
1996 | if (ret) | |
1997 | goto end; | |
1998 | if (entry->start.signedness == entry->end.signedness && | |
1999 | entry->start.value | |
2000 | == entry->end.value) { | |
141ddf28 | 2001 | ret = lttng_metadata_printf(session, |
7c559490 MD |
2002 | ",\n"); |
2003 | } else { | |
2004 | if (entry->end.signedness) { | |
2005 | ret = lttng_metadata_printf(session, | |
2006 | " ... %lld,\n", | |
2007 | (long long) entry->end.value); | |
2008 | } else { | |
2009 | ret = lttng_metadata_printf(session, | |
2010 | " ... %llu,\n", | |
2011 | entry->end.value); | |
2012 | } | |
141ddf28 | 2013 | } |
7c559490 MD |
2014 | if (ret) |
2015 | goto end; | |
141ddf28 | 2016 | } |
141ddf28 MD |
2017 | } |
2018 | ret = print_tabs(session, nesting); | |
2019 | if (ret) | |
2020 | goto end; | |
2021 | ret = lttng_metadata_printf(session, "} _%s;\n", | |
2022 | field->name); | |
2023 | end: | |
2024 | return ret; | |
2025 | } | |
2026 | ||
d83004aa JD |
2027 | /* |
2028 | * Must be called with sessions_mutex held. | |
2029 | */ | |
c099397a | 2030 | static |
a90917c3 | 2031 | int _lttng_field_statedump(struct lttng_session *session, |
f513b2bf MD |
2032 | const struct lttng_event_field *field, |
2033 | size_t nesting) | |
c099397a | 2034 | { |
c099397a | 2035 | int ret = 0; |
c099397a | 2036 | |
8070f5c0 MD |
2037 | switch (field->type.atype) { |
2038 | case atype_integer: | |
f513b2bf MD |
2039 | ret = print_tabs(session, nesting); |
2040 | if (ret) | |
2041 | return ret; | |
8070f5c0 | 2042 | ret = lttng_metadata_printf(session, |
f513b2bf | 2043 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
8070f5c0 MD |
2044 | field->type.u.basic.integer.size, |
2045 | field->type.u.basic.integer.alignment, | |
2046 | field->type.u.basic.integer.signedness, | |
2047 | (field->type.u.basic.integer.encoding == lttng_encode_none) | |
2048 | ? "none" | |
2049 | : (field->type.u.basic.integer.encoding == lttng_encode_UTF8) | |
2050 | ? "UTF8" | |
2051 | : "ASCII", | |
2052 | field->type.u.basic.integer.base, | |
5214fa50 | 2053 | #if __BYTE_ORDER == __BIG_ENDIAN |
8070f5c0 | 2054 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
c099397a | 2055 | #else |
8070f5c0 | 2056 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
c099397a | 2057 | #endif |
8070f5c0 MD |
2058 | field->name); |
2059 | break; | |
2060 | case atype_enum: | |
141ddf28 | 2061 | ret = _lttng_enum_statedump(session, field, nesting); |
8070f5c0 MD |
2062 | break; |
2063 | case atype_array: | |
3834b99f | 2064 | case atype_array_bitfield: |
8070f5c0 MD |
2065 | { |
2066 | const struct lttng_basic_type *elem_type; | |
2067 | ||
2068 | elem_type = &field->type.u.array.elem_type; | |
43803cf2 | 2069 | if (field->type.u.array.elem_alignment) { |
f513b2bf MD |
2070 | ret = print_tabs(session, nesting); |
2071 | if (ret) | |
2072 | return ret; | |
43803cf2 | 2073 | ret = lttng_metadata_printf(session, |
f513b2bf | 2074 | "struct { } align(%u) _%s_padding;\n", |
43803cf2 MD |
2075 | field->type.u.array.elem_alignment * CHAR_BIT, |
2076 | field->name); | |
2077 | if (ret) | |
2078 | return ret; | |
2079 | } | |
f513b2bf MD |
2080 | ret = print_tabs(session, nesting); |
2081 | if (ret) | |
2082 | return ret; | |
8070f5c0 | 2083 | ret = lttng_metadata_printf(session, |
f513b2bf | 2084 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
8070f5c0 MD |
2085 | elem_type->u.basic.integer.size, |
2086 | elem_type->u.basic.integer.alignment, | |
2087 | elem_type->u.basic.integer.signedness, | |
2088 | (elem_type->u.basic.integer.encoding == lttng_encode_none) | |
2089 | ? "none" | |
2090 | : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8) | |
2091 | ? "UTF8" | |
2092 | : "ASCII", | |
2093 | elem_type->u.basic.integer.base, | |
5214fa50 | 2094 | #if __BYTE_ORDER == __BIG_ENDIAN |
8070f5c0 | 2095 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
8fa75f34 | 2096 | #else |
8070f5c0 | 2097 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
8fa75f34 | 2098 | #endif |
8070f5c0 MD |
2099 | field->name, field->type.u.array.length); |
2100 | break; | |
2101 | } | |
2102 | case atype_sequence: | |
3834b99f | 2103 | case atype_sequence_bitfield: |
8070f5c0 MD |
2104 | { |
2105 | const struct lttng_basic_type *elem_type; | |
2106 | const struct lttng_basic_type *length_type; | |
2107 | ||
2108 | elem_type = &field->type.u.sequence.elem_type; | |
2109 | length_type = &field->type.u.sequence.length_type; | |
f513b2bf MD |
2110 | ret = print_tabs(session, nesting); |
2111 | if (ret) | |
2112 | return ret; | |
8070f5c0 | 2113 | ret = lttng_metadata_printf(session, |
f513b2bf | 2114 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", |
8070f5c0 | 2115 | length_type->u.basic.integer.size, |
32257272 | 2116 | (unsigned int) length_type->u.basic.integer.alignment, |
8070f5c0 MD |
2117 | length_type->u.basic.integer.signedness, |
2118 | (length_type->u.basic.integer.encoding == lttng_encode_none) | |
2119 | ? "none" | |
0635cdd4 | 2120 | : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8) |
8070f5c0 | 2121 | ? "UTF8" |
0635cdd4 | 2122 | : "ASCII"), |
8070f5c0 | 2123 | length_type->u.basic.integer.base, |
5214fa50 | 2124 | #if __BYTE_ORDER == __BIG_ENDIAN |
8070f5c0 | 2125 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
e0a7a7c4 | 2126 | #else |
0635cdd4 | 2127 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
e0a7a7c4 | 2128 | #endif |
27d2368e MD |
2129 | field->name); |
2130 | if (ret) | |
2131 | return ret; | |
2132 | ||
43803cf2 | 2133 | if (field->type.u.sequence.elem_alignment) { |
f513b2bf MD |
2134 | ret = print_tabs(session, nesting); |
2135 | if (ret) | |
2136 | return ret; | |
43803cf2 | 2137 | ret = lttng_metadata_printf(session, |
f513b2bf | 2138 | "struct { } align(%u) _%s_padding;\n", |
43803cf2 MD |
2139 | field->type.u.sequence.elem_alignment * CHAR_BIT, |
2140 | field->name); | |
2141 | if (ret) | |
2142 | return ret; | |
2143 | } | |
f513b2bf MD |
2144 | ret = print_tabs(session, nesting); |
2145 | if (ret) | |
2146 | return ret; | |
27d2368e | 2147 | ret = lttng_metadata_printf(session, |
f513b2bf | 2148 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
8070f5c0 | 2149 | elem_type->u.basic.integer.size, |
32257272 | 2150 | (unsigned int) elem_type->u.basic.integer.alignment, |
8070f5c0 MD |
2151 | elem_type->u.basic.integer.signedness, |
2152 | (elem_type->u.basic.integer.encoding == lttng_encode_none) | |
2153 | ? "none" | |
0635cdd4 | 2154 | : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8) |
8070f5c0 | 2155 | ? "UTF8" |
0635cdd4 | 2156 | : "ASCII"), |
8070f5c0 | 2157 | elem_type->u.basic.integer.base, |
5214fa50 | 2158 | #if __BYTE_ORDER == __BIG_ENDIAN |
8070f5c0 | 2159 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
8fa75f34 | 2160 | #else |
8070f5c0 | 2161 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", |
8fa75f34 | 2162 | #endif |
8070f5c0 | 2163 | field->name, |
27d2368e | 2164 | field->name); |
8070f5c0 MD |
2165 | break; |
2166 | } | |
c099397a | 2167 | |
8070f5c0 MD |
2168 | case atype_string: |
2169 | /* Default encoding is UTF8 */ | |
f513b2bf MD |
2170 | ret = print_tabs(session, nesting); |
2171 | if (ret) | |
2172 | return ret; | |
8070f5c0 | 2173 | ret = lttng_metadata_printf(session, |
f513b2bf | 2174 | "string%s _%s;\n", |
8070f5c0 MD |
2175 | field->type.u.basic.string.encoding == lttng_encode_ASCII ? |
2176 | " { encoding = ASCII; }" : "", | |
2177 | field->name); | |
2178 | break; | |
f513b2bf MD |
2179 | case atype_struct: |
2180 | ret = _lttng_struct_statedump(session, field, nesting); | |
2181 | break; | |
2182 | case atype_array_compound: | |
2183 | ret = _lttng_array_compound_statedump(session, field, nesting); | |
2184 | break; | |
2185 | case atype_sequence_compound: | |
2186 | ret = _lttng_sequence_compound_statedump(session, field, nesting); | |
2187 | break; | |
65c85aa6 MD |
2188 | case atype_variant: |
2189 | ret = _lttng_variant_statedump(session, field, nesting); | |
2190 | break; | |
f513b2bf | 2191 | |
8070f5c0 MD |
2192 | default: |
2193 | WARN_ON_ONCE(1); | |
2194 | return -EINVAL; | |
2195 | } | |
2196 | return ret; | |
2197 | } | |
2198 | ||
2199 | static | |
a90917c3 | 2200 | int _lttng_context_metadata_statedump(struct lttng_session *session, |
8070f5c0 MD |
2201 | struct lttng_ctx *ctx) |
2202 | { | |
2203 | int ret = 0; | |
2204 | int i; | |
2205 | ||
2206 | if (!ctx) | |
2207 | return 0; | |
2208 | for (i = 0; i < ctx->nr_fields; i++) { | |
2209 | const struct lttng_ctx_field *field = &ctx->fields[i]; | |
2210 | ||
f513b2bf | 2211 | ret = _lttng_field_statedump(session, &field->event_field, 2); |
8070f5c0 MD |
2212 | if (ret) |
2213 | return ret; | |
2214 | } | |
2215 | return ret; | |
2216 | } | |
2217 | ||
2218 | static | |
a90917c3 MD |
2219 | int _lttng_fields_metadata_statedump(struct lttng_session *session, |
2220 | struct lttng_event *event) | |
8070f5c0 MD |
2221 | { |
2222 | const struct lttng_event_desc *desc = event->desc; | |
2223 | int ret = 0; | |
2224 | int i; | |
2225 | ||
2226 | for (i = 0; i < desc->nr_fields; i++) { | |
2227 | const struct lttng_event_field *field = &desc->fields[i]; | |
2228 | ||
f513b2bf | 2229 | ret = _lttng_field_statedump(session, field, 2); |
8070f5c0 MD |
2230 | if (ret) |
2231 | return ret; | |
c099397a MD |
2232 | } |
2233 | return ret; | |
2234 | } | |
2235 | ||
d83004aa JD |
2236 | /* |
2237 | * Must be called with sessions_mutex held. | |
2238 | */ | |
c099397a | 2239 | static |
a90917c3 MD |
2240 | int _lttng_event_metadata_statedump(struct lttng_session *session, |
2241 | struct lttng_channel *chan, | |
2242 | struct lttng_event *event) | |
c099397a MD |
2243 | { |
2244 | int ret = 0; | |
2245 | ||
a8f2d0c7 | 2246 | if (event->metadata_dumped || !READ_ONCE(session->active)) |
c099397a | 2247 | return 0; |
d83004aa | 2248 | if (chan->channel_type == METADATA_CHANNEL) |
c099397a MD |
2249 | return 0; |
2250 | ||
2251 | ret = lttng_metadata_printf(session, | |
2252 | "event {\n" | |
ae734547 | 2253 | " name = \"%s\";\n" |
c099397a | 2254 | " id = %u;\n" |
b9074a1b | 2255 | " stream_id = %u;\n", |
c099397a MD |
2256 | event->desc->name, |
2257 | event->id, | |
2258 | event->chan->id); | |
2259 | if (ret) | |
2260 | goto end; | |
2261 | ||
b9074a1b MD |
2262 | if (event->ctx) { |
2263 | ret = lttng_metadata_printf(session, | |
2264 | " context := struct {\n"); | |
2265 | if (ret) | |
2266 | goto end; | |
2267 | } | |
a90917c3 | 2268 | ret = _lttng_context_metadata_statedump(session, event->ctx); |
8070f5c0 MD |
2269 | if (ret) |
2270 | goto end; | |
b9074a1b MD |
2271 | if (event->ctx) { |
2272 | ret = lttng_metadata_printf(session, | |
2273 | " };\n"); | |
2274 | if (ret) | |
2275 | goto end; | |
2276 | } | |
8070f5c0 MD |
2277 | |
2278 | ret = lttng_metadata_printf(session, | |
8070f5c0 MD |
2279 | " fields := struct {\n" |
2280 | ); | |
2281 | if (ret) | |
2282 | goto end; | |
2283 | ||
a90917c3 | 2284 | ret = _lttng_fields_metadata_statedump(session, event); |
c099397a MD |
2285 | if (ret) |
2286 | goto end; | |
2287 | ||
2288 | /* | |
2289 | * LTTng space reservation can only reserve multiples of the | |
2290 | * byte size. | |
2291 | */ | |
2292 | ret = lttng_metadata_printf(session, | |
9115fbdc MD |
2293 | " };\n" |
2294 | "};\n\n"); | |
c099397a MD |
2295 | if (ret) |
2296 | goto end; | |
2297 | ||
c099397a MD |
2298 | event->metadata_dumped = 1; |
2299 | end: | |
2300 | return ret; | |
2301 | ||
2302 | } | |
2303 | ||
d83004aa JD |
2304 | /* |
2305 | * Must be called with sessions_mutex held. | |
2306 | */ | |
c099397a | 2307 | static |
a90917c3 MD |
2308 | int _lttng_channel_metadata_statedump(struct lttng_session *session, |
2309 | struct lttng_channel *chan) | |
c099397a MD |
2310 | { |
2311 | int ret = 0; | |
2312 | ||
a8f2d0c7 | 2313 | if (chan->metadata_dumped || !READ_ONCE(session->active)) |
c099397a | 2314 | return 0; |
d83004aa JD |
2315 | |
2316 | if (chan->channel_type == METADATA_CHANNEL) | |
c099397a MD |
2317 | return 0; |
2318 | ||
2319 | WARN_ON_ONCE(!chan->header_type); | |
2320 | ret = lttng_metadata_printf(session, | |
2321 | "stream {\n" | |
2322 | " id = %u;\n" | |
9115fbdc | 2323 | " event.header := %s;\n" |
b9074a1b | 2324 | " packet.context := struct packet_context;\n", |
c099397a MD |
2325 | chan->id, |
2326 | chan->header_type == 1 ? "struct event_header_compact" : | |
2327 | "struct event_header_large"); | |
2328 | if (ret) | |
2329 | goto end; | |
2330 | ||
b9074a1b MD |
2331 | if (chan->ctx) { |
2332 | ret = lttng_metadata_printf(session, | |
2333 | " event.context := struct {\n"); | |
2334 | if (ret) | |
2335 | goto end; | |
2336 | } | |
a90917c3 | 2337 | ret = _lttng_context_metadata_statedump(session, chan->ctx); |
8070f5c0 MD |
2338 | if (ret) |
2339 | goto end; | |
b9074a1b MD |
2340 | if (chan->ctx) { |
2341 | ret = lttng_metadata_printf(session, | |
2342 | " };\n"); | |
2343 | if (ret) | |
2344 | goto end; | |
2345 | } | |
8070f5c0 MD |
2346 | |
2347 | ret = lttng_metadata_printf(session, | |
b9074a1b | 2348 | "};\n\n"); |
8070f5c0 | 2349 | |
c099397a MD |
2350 | chan->metadata_dumped = 1; |
2351 | end: | |
2352 | return ret; | |
2353 | } | |
2354 | ||
d83004aa JD |
2355 | /* |
2356 | * Must be called with sessions_mutex held. | |
2357 | */ | |
9115fbdc | 2358 | static |
a90917c3 | 2359 | int _lttng_stream_packet_context_declare(struct lttng_session *session) |
9115fbdc MD |
2360 | { |
2361 | return lttng_metadata_printf(session, | |
2362 | "struct packet_context {\n" | |
a3ccff4f MD |
2363 | " uint64_clock_monotonic_t timestamp_begin;\n" |
2364 | " uint64_clock_monotonic_t timestamp_end;\n" | |
576ca06a MD |
2365 | " uint64_t content_size;\n" |
2366 | " uint64_t packet_size;\n" | |
5b3cf4f9 | 2367 | " uint64_t packet_seq_num;\n" |
a9afe705 | 2368 | " unsigned long events_discarded;\n" |
9115fbdc | 2369 | " uint32_t cpu_id;\n" |
c6dfdf6f | 2370 | "};\n\n" |
9115fbdc MD |
2371 | ); |
2372 | } | |
2373 | ||
2374 | /* | |
2375 | * Compact header: | |
2376 | * id: range: 0 - 30. | |
2377 | * id 31 is reserved to indicate an extended header. | |
2378 | * | |
2379 | * Large header: | |
2380 | * id: range: 0 - 65534. | |
2381 | * id 65535 is reserved to indicate an extended header. | |
d83004aa JD |
2382 | * |
2383 | * Must be called with sessions_mutex held. | |
9115fbdc MD |
2384 | */ |
2385 | static | |
a90917c3 | 2386 | int _lttng_event_header_declare(struct lttng_session *session) |
9115fbdc MD |
2387 | { |
2388 | return lttng_metadata_printf(session, | |
2389 | "struct event_header_compact {\n" | |
2390 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" | |
2391 | " variant <id> {\n" | |
2392 | " struct {\n" | |
a3ccff4f | 2393 | " uint27_clock_monotonic_t timestamp;\n" |
9115fbdc MD |
2394 | " } compact;\n" |
2395 | " struct {\n" | |
2396 | " uint32_t id;\n" | |
a3ccff4f | 2397 | " uint64_clock_monotonic_t timestamp;\n" |
9115fbdc MD |
2398 | " } extended;\n" |
2399 | " } v;\n" | |
2400 | "} align(%u);\n" | |
2401 | "\n" | |
2402 | "struct event_header_large {\n" | |
2403 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" | |
2404 | " variant <id> {\n" | |
2405 | " struct {\n" | |
a3ccff4f | 2406 | " uint32_clock_monotonic_t timestamp;\n" |
9115fbdc MD |
2407 | " } compact;\n" |
2408 | " struct {\n" | |
2409 | " uint32_t id;\n" | |
a3ccff4f | 2410 | " uint64_clock_monotonic_t timestamp;\n" |
9115fbdc MD |
2411 | " } extended;\n" |
2412 | " } v;\n" | |
2413 | "} align(%u);\n\n", | |
a90917c3 MD |
2414 | lttng_alignof(uint32_t) * CHAR_BIT, |
2415 | lttng_alignof(uint16_t) * CHAR_BIT | |
9115fbdc MD |
2416 | ); |
2417 | } | |
2418 | ||
a3ccff4f MD |
2419 | /* |
2420 | * Approximation of NTP time of day to clock monotonic correlation, | |
2421 | * taken at start of trace. | |
2422 | * Yes, this is only an approximation. Yes, we can (and will) do better | |
2423 | * in future versions. | |
5283350b MD |
2424 | * This function may return a negative offset. It may happen if the |
2425 | * system sets the REALTIME clock to 0 after boot. | |
25cc7051 MJ |
2426 | * |
2427 | * Use 64bit timespec on kernels that have it, this makes 32bit arch | |
2428 | * y2038 compliant. | |
a3ccff4f MD |
2429 | */ |
2430 | static | |
8967b2a3 | 2431 | int64_t measure_clock_offset(void) |
a3ccff4f | 2432 | { |
dcb93448 | 2433 | uint64_t monotonic_avg, monotonic[2], realtime; |
2754583e | 2434 | uint64_t tcf = trace_clock_freq(); |
dcb93448 | 2435 | int64_t offset; |
a3ccff4f | 2436 | unsigned long flags; |
25cc7051 MJ |
2437 | #ifdef LTTNG_KERNEL_HAS_TIMESPEC64 |
2438 | struct timespec64 rts = { 0, 0 }; | |
2439 | #else | |
2440 | struct timespec rts = { 0, 0 }; | |
2441 | #endif | |
a3ccff4f MD |
2442 | |
2443 | /* Disable interrupts to increase correlation precision. */ | |
2444 | local_irq_save(flags); | |
2445 | monotonic[0] = trace_clock_read64(); | |
25cc7051 MJ |
2446 | #ifdef LTTNG_KERNEL_HAS_TIMESPEC64 |
2447 | ktime_get_real_ts64(&rts); | |
2448 | #else | |
216b6baa | 2449 | getnstimeofday(&rts); |
25cc7051 | 2450 | #endif |
a3ccff4f MD |
2451 | monotonic[1] = trace_clock_read64(); |
2452 | local_irq_restore(flags); | |
2453 | ||
dcb93448 | 2454 | monotonic_avg = (monotonic[0] + monotonic[1]) >> 1; |
2754583e MD |
2455 | realtime = (uint64_t) rts.tv_sec * tcf; |
2456 | if (tcf == NSEC_PER_SEC) { | |
2457 | realtime += rts.tv_nsec; | |
2458 | } else { | |
2459 | uint64_t n = rts.tv_nsec * tcf; | |
2460 | ||
2461 | do_div(n, NSEC_PER_SEC); | |
2462 | realtime += n; | |
2463 | } | |
dcb93448 | 2464 | offset = (int64_t) realtime - monotonic_avg; |
a3ccff4f MD |
2465 | return offset; |
2466 | } | |
2467 | ||
deba832d JR |
2468 | static |
2469 | int print_escaped_ctf_string(struct lttng_session *session, const char *string) | |
2470 | { | |
2471 | int ret; | |
2472 | size_t i; | |
2473 | char cur; | |
2474 | ||
2475 | i = 0; | |
2476 | cur = string[i]; | |
2477 | while (cur != '\0') { | |
2478 | switch (cur) { | |
2479 | case '\n': | |
2480 | ret = lttng_metadata_printf(session, "%s", "\\n"); | |
2481 | break; | |
2482 | case '\\': | |
2483 | case '"': | |
2484 | ret = lttng_metadata_printf(session, "%c", '\\'); | |
2485 | if (ret) | |
2486 | goto error; | |
2487 | /* We still print the current char */ | |
2488 | /* Fallthrough */ | |
2489 | default: | |
2490 | ret = lttng_metadata_printf(session, "%c", cur); | |
2491 | break; | |
2492 | } | |
2493 | ||
2494 | if (ret) | |
2495 | goto error; | |
2496 | ||
2497 | cur = string[++i]; | |
2498 | } | |
2499 | error: | |
2500 | return ret; | |
2501 | } | |
2502 | ||
2503 | static | |
a9550ab5 JR |
2504 | int print_metadata_escaped_field(struct lttng_session *session, const char *field, |
2505 | const char *field_value) | |
deba832d JR |
2506 | { |
2507 | int ret; | |
2508 | ||
a9550ab5 | 2509 | ret = lttng_metadata_printf(session, " %s = \"", field); |
deba832d JR |
2510 | if (ret) |
2511 | goto error; | |
2512 | ||
a9550ab5 | 2513 | ret = print_escaped_ctf_string(session, field_value); |
deba832d JR |
2514 | if (ret) |
2515 | goto error; | |
2516 | ||
2517 | ret = lttng_metadata_printf(session, "\";\n"); | |
2518 | ||
2519 | error: | |
2520 | return ret; | |
2521 | } | |
2522 | ||
c099397a MD |
2523 | /* |
2524 | * Output metadata into this session's metadata buffers. | |
d83004aa | 2525 | * Must be called with sessions_mutex held. |
c099397a MD |
2526 | */ |
2527 | static | |
a90917c3 | 2528 | int _lttng_session_metadata_statedump(struct lttng_session *session) |
c099397a | 2529 | { |
30bdb6e4 | 2530 | unsigned char *uuid_c = session->uuid.b; |
a82c63f1 | 2531 | unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN]; |
a90917c3 MD |
2532 | struct lttng_channel *chan; |
2533 | struct lttng_event *event; | |
c099397a MD |
2534 | int ret = 0; |
2535 | ||
a8f2d0c7 | 2536 | if (!READ_ONCE(session->active)) |
c099397a MD |
2537 | return 0; |
2538 | if (session->metadata_dumped) | |
2539 | goto skip_session; | |
c099397a | 2540 | |
d793d5e1 | 2541 | snprintf(uuid_s, sizeof(uuid_s), |
30bdb6e4 MD |
2542 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
2543 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], | |
2544 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], | |
2545 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], | |
2546 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); | |
d793d5e1 MD |
2547 | |
2548 | ret = lttng_metadata_printf(session, | |
9115fbdc MD |
2549 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
2550 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" | |
2551 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" | |
2552 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" | |
a9afe705 | 2553 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" |
9115fbdc MD |
2554 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" |
2555 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" | |
d793d5e1 MD |
2556 | "\n" |
2557 | "trace {\n" | |
2558 | " major = %u;\n" | |
2559 | " minor = %u;\n" | |
30bdb6e4 | 2560 | " uuid = \"%s\";\n" |
d793d5e1 MD |
2561 | " byte_order = %s;\n" |
2562 | " packet.header := struct {\n" | |
2563 | " uint32_t magic;\n" | |
1ec3f75a | 2564 | " uint8_t uuid[16];\n" |
d793d5e1 | 2565 | " uint32_t stream_id;\n" |
5594698f | 2566 | " uint64_t stream_instance_id;\n" |
0eb25f58 | 2567 | " };\n" |
d793d5e1 | 2568 | "};\n\n", |
a90917c3 MD |
2569 | lttng_alignof(uint8_t) * CHAR_BIT, |
2570 | lttng_alignof(uint16_t) * CHAR_BIT, | |
2571 | lttng_alignof(uint32_t) * CHAR_BIT, | |
2572 | lttng_alignof(uint64_t) * CHAR_BIT, | |
a9afe705 MD |
2573 | sizeof(unsigned long) * CHAR_BIT, |
2574 | lttng_alignof(unsigned long) * CHAR_BIT, | |
c6c9e10f MD |
2575 | CTF_SPEC_MAJOR, |
2576 | CTF_SPEC_MINOR, | |
d793d5e1 | 2577 | uuid_s, |
5214fa50 | 2578 | #if __BYTE_ORDER == __BIG_ENDIAN |
d793d5e1 MD |
2579 | "be" |
2580 | #else | |
2581 | "le" | |
2582 | #endif | |
2583 | ); | |
2584 | if (ret) | |
2585 | goto end; | |
2586 | ||
99dc9597 MD |
2587 | ret = lttng_metadata_printf(session, |
2588 | "env {\n" | |
a6058143 | 2589 | " hostname = \"%s\";\n" |
c6c9e10f | 2590 | " domain = \"kernel\";\n" |
99dc9597 | 2591 | " sysname = \"%s\";\n" |
c6c9e10f MD |
2592 | " kernel_release = \"%s\";\n" |
2593 | " kernel_version = \"%s\";\n" | |
2594 | " tracer_name = \"lttng-modules\";\n" | |
2595 | " tracer_major = %d;\n" | |
2596 | " tracer_minor = %d;\n" | |
2597 | " tracer_patchlevel = %d;\n" | |
deba832d | 2598 | " trace_buffering_scheme = \"global\";\n", |
3d0d43db | 2599 | current->nsproxy->uts_ns->name.nodename, |
99dc9597 MD |
2600 | utsname()->sysname, |
2601 | utsname()->release, | |
c6c9e10f MD |
2602 | utsname()->version, |
2603 | LTTNG_MODULES_MAJOR_VERSION, | |
2604 | LTTNG_MODULES_MINOR_VERSION, | |
2605 | LTTNG_MODULES_PATCHLEVEL_VERSION | |
99dc9597 MD |
2606 | ); |
2607 | if (ret) | |
2608 | goto end; | |
2609 | ||
a9550ab5 JR |
2610 | ret = print_metadata_escaped_field(session, "trace_name", session->name); |
2611 | if (ret) | |
2612 | goto end; | |
2613 | ret = print_metadata_escaped_field(session, "trace_creation_datetime", | |
2614 | session->creation_time); | |
deba832d JR |
2615 | if (ret) |
2616 | goto end; | |
2617 | ||
2618 | /* Close env */ | |
2619 | ret = lttng_metadata_printf(session, "};\n\n"); | |
2620 | if (ret) | |
2621 | goto end; | |
2622 | ||
a3ccff4f MD |
2623 | ret = lttng_metadata_printf(session, |
2624 | "clock {\n" | |
2754583e MD |
2625 | " name = \"%s\";\n", |
2626 | trace_clock_name() | |
a82c63f1 MD |
2627 | ); |
2628 | if (ret) | |
2629 | goto end; | |
2630 | ||
2631 | if (!trace_clock_uuid(clock_uuid_s)) { | |
2632 | ret = lttng_metadata_printf(session, | |
7c27cb17 | 2633 | " uuid = \"%s\";\n", |
a82c63f1 MD |
2634 | clock_uuid_s |
2635 | ); | |
2636 | if (ret) | |
2637 | goto end; | |
2638 | } | |
2639 | ||
2640 | ret = lttng_metadata_printf(session, | |
2754583e | 2641 | " description = \"%s\";\n" |
a3ccff4f MD |
2642 | " freq = %llu; /* Frequency, in Hz */\n" |
2643 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" | |
8967b2a3 | 2644 | " offset = %lld;\n" |
a3ccff4f | 2645 | "};\n\n", |
2754583e | 2646 | trace_clock_description(), |
a3ccff4f | 2647 | (unsigned long long) trace_clock_freq(), |
8967b2a3 | 2648 | (long long) measure_clock_offset() |
a3ccff4f MD |
2649 | ); |
2650 | if (ret) | |
2651 | goto end; | |
2652 | ||
2653 | ret = lttng_metadata_printf(session, | |
2654 | "typealias integer {\n" | |
2655 | " size = 27; align = 1; signed = false;\n" | |
2754583e | 2656 | " map = clock.%s.value;\n" |
a3ccff4f MD |
2657 | "} := uint27_clock_monotonic_t;\n" |
2658 | "\n" | |
2659 | "typealias integer {\n" | |
2660 | " size = 32; align = %u; signed = false;\n" | |
2754583e | 2661 | " map = clock.%s.value;\n" |
a3ccff4f MD |
2662 | "} := uint32_clock_monotonic_t;\n" |
2663 | "\n" | |
2664 | "typealias integer {\n" | |
2665 | " size = 64; align = %u; signed = false;\n" | |
2754583e | 2666 | " map = clock.%s.value;\n" |
a3ccff4f | 2667 | "} := uint64_clock_monotonic_t;\n\n", |
2754583e | 2668 | trace_clock_name(), |
a3ccff4f | 2669 | lttng_alignof(uint32_t) * CHAR_BIT, |
2754583e MD |
2670 | trace_clock_name(), |
2671 | lttng_alignof(uint64_t) * CHAR_BIT, | |
2672 | trace_clock_name() | |
a3ccff4f MD |
2673 | ); |
2674 | if (ret) | |
2675 | goto end; | |
2676 | ||
a90917c3 | 2677 | ret = _lttng_stream_packet_context_declare(session); |
9115fbdc MD |
2678 | if (ret) |
2679 | goto end; | |
2680 | ||
a90917c3 | 2681 | ret = _lttng_event_header_declare(session); |
9115fbdc MD |
2682 | if (ret) |
2683 | goto end; | |
2684 | ||
c099397a MD |
2685 | skip_session: |
2686 | list_for_each_entry(chan, &session->chan, list) { | |
a90917c3 | 2687 | ret = _lttng_channel_metadata_statedump(session, chan); |
c099397a MD |
2688 | if (ret) |
2689 | goto end; | |
2690 | } | |
2691 | ||
2692 | list_for_each_entry(event, &session->events, list) { | |
a90917c3 | 2693 | ret = _lttng_event_metadata_statedump(session, event->chan, event); |
c099397a MD |
2694 | if (ret) |
2695 | goto end; | |
2696 | } | |
2697 | session->metadata_dumped = 1; | |
2698 | end: | |
2699 | return ret; | |
2700 | } | |
2701 | ||
c0e31d2e | 2702 | /** |
a90917c3 | 2703 | * lttng_transport_register - LTT transport registration |
c0e31d2e MD |
2704 | * @transport: transport structure |
2705 | * | |
2706 | * Registers a transport which can be used as output to extract the data out of | |
2707 | * LTTng. The module calling this registration function must ensure that no | |
2708 | * trap-inducing code will be executed by the transport functions. E.g. | |
2709 | * vmalloc_sync_all() must be called between a vmalloc and the moment the memory | |
2710 | * is made visible to the transport function. This registration acts as a | |
2711 | * vmalloc_sync_all. Therefore, only if the module allocates virtual memory | |
2712 | * after its registration must it synchronize the TLBs. | |
2713 | */ | |
a90917c3 | 2714 | void lttng_transport_register(struct lttng_transport *transport) |
c0e31d2e MD |
2715 | { |
2716 | /* | |
2717 | * Make sure no page fault can be triggered by the module about to be | |
2718 | * registered. We deal with this here so we don't have to call | |
2719 | * vmalloc_sync_all() in each module's init. | |
2720 | */ | |
6d2a620c | 2721 | wrapper_vmalloc_sync_all(); |
c0e31d2e MD |
2722 | |
2723 | mutex_lock(&sessions_mutex); | |
a90917c3 | 2724 | list_add_tail(&transport->node, <tng_transport_list); |
c0e31d2e MD |
2725 | mutex_unlock(&sessions_mutex); |
2726 | } | |
a90917c3 | 2727 | EXPORT_SYMBOL_GPL(lttng_transport_register); |
c0e31d2e MD |
2728 | |
2729 | /** | |
a90917c3 | 2730 | * lttng_transport_unregister - LTT transport unregistration |
c0e31d2e MD |
2731 | * @transport: transport structure |
2732 | */ | |
a90917c3 | 2733 | void lttng_transport_unregister(struct lttng_transport *transport) |
c0e31d2e MD |
2734 | { |
2735 | mutex_lock(&sessions_mutex); | |
2736 | list_del(&transport->node); | |
2737 | mutex_unlock(&sessions_mutex); | |
2738 | } | |
a90917c3 | 2739 | EXPORT_SYMBOL_GPL(lttng_transport_unregister); |
c0e31d2e | 2740 | |
894ce5dc | 2741 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
1e367326 MD |
2742 | |
2743 | enum cpuhp_state lttng_hp_prepare; | |
2744 | enum cpuhp_state lttng_hp_online; | |
2745 | ||
2746 | static int lttng_hotplug_prepare(unsigned int cpu, struct hlist_node *node) | |
2747 | { | |
2748 | struct lttng_cpuhp_node *lttng_node; | |
2749 | ||
2750 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); | |
2751 | switch (lttng_node->component) { | |
2752 | case LTTNG_RING_BUFFER_FRONTEND: | |
2753 | return 0; | |
2754 | case LTTNG_RING_BUFFER_BACKEND: | |
2755 | return lttng_cpuhp_rb_backend_prepare(cpu, lttng_node); | |
2756 | case LTTNG_RING_BUFFER_ITER: | |
2757 | return 0; | |
2758 | case LTTNG_CONTEXT_PERF_COUNTERS: | |
2759 | return 0; | |
2760 | default: | |
2761 | return -EINVAL; | |
2762 | } | |
2763 | } | |
2764 | ||
2765 | static int lttng_hotplug_dead(unsigned int cpu, struct hlist_node *node) | |
2766 | { | |
2767 | struct lttng_cpuhp_node *lttng_node; | |
2768 | ||
2769 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); | |
2770 | switch (lttng_node->component) { | |
2771 | case LTTNG_RING_BUFFER_FRONTEND: | |
2772 | return lttng_cpuhp_rb_frontend_dead(cpu, lttng_node); | |
2773 | case LTTNG_RING_BUFFER_BACKEND: | |
2774 | return 0; | |
2775 | case LTTNG_RING_BUFFER_ITER: | |
2776 | return 0; | |
2777 | case LTTNG_CONTEXT_PERF_COUNTERS: | |
2778 | return lttng_cpuhp_perf_counter_dead(cpu, lttng_node); | |
2779 | default: | |
2780 | return -EINVAL; | |
2781 | } | |
2782 | } | |
2783 | ||
2784 | static int lttng_hotplug_online(unsigned int cpu, struct hlist_node *node) | |
2785 | { | |
2786 | struct lttng_cpuhp_node *lttng_node; | |
2787 | ||
2788 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); | |
2789 | switch (lttng_node->component) { | |
2790 | case LTTNG_RING_BUFFER_FRONTEND: | |
2791 | return lttng_cpuhp_rb_frontend_online(cpu, lttng_node); | |
2792 | case LTTNG_RING_BUFFER_BACKEND: | |
2793 | return 0; | |
2794 | case LTTNG_RING_BUFFER_ITER: | |
2795 | return lttng_cpuhp_rb_iter_online(cpu, lttng_node); | |
2796 | case LTTNG_CONTEXT_PERF_COUNTERS: | |
2797 | return lttng_cpuhp_perf_counter_online(cpu, lttng_node); | |
2798 | default: | |
2799 | return -EINVAL; | |
2800 | } | |
2801 | } | |
2802 | ||
2803 | static int lttng_hotplug_offline(unsigned int cpu, struct hlist_node *node) | |
2804 | { | |
2805 | struct lttng_cpuhp_node *lttng_node; | |
2806 | ||
2807 | lttng_node = container_of(node, struct lttng_cpuhp_node, node); | |
2808 | switch (lttng_node->component) { | |
2809 | case LTTNG_RING_BUFFER_FRONTEND: | |
2810 | return lttng_cpuhp_rb_frontend_offline(cpu, lttng_node); | |
2811 | case LTTNG_RING_BUFFER_BACKEND: | |
2812 | return 0; | |
2813 | case LTTNG_RING_BUFFER_ITER: | |
2814 | return 0; | |
2815 | case LTTNG_CONTEXT_PERF_COUNTERS: | |
2816 | return 0; | |
2817 | default: | |
2818 | return -EINVAL; | |
2819 | } | |
2820 | } | |
2821 | ||
2822 | static int __init lttng_init_cpu_hotplug(void) | |
2823 | { | |
2824 | int ret; | |
2825 | ||
2826 | ret = cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN, "lttng:prepare", | |
2827 | lttng_hotplug_prepare, | |
2828 | lttng_hotplug_dead); | |
2829 | if (ret < 0) { | |
2830 | return ret; | |
2831 | } | |
2832 | lttng_hp_prepare = ret; | |
2833 | lttng_rb_set_hp_prepare(ret); | |
2834 | ||
2835 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "lttng:online", | |
2836 | lttng_hotplug_online, | |
2837 | lttng_hotplug_offline); | |
2838 | if (ret < 0) { | |
2839 | cpuhp_remove_multi_state(lttng_hp_prepare); | |
2840 | lttng_hp_prepare = 0; | |
2841 | return ret; | |
2842 | } | |
2843 | lttng_hp_online = ret; | |
2844 | lttng_rb_set_hp_online(ret); | |
2845 | ||
2846 | return 0; | |
2847 | } | |
2848 | ||
2849 | static void __exit lttng_exit_cpu_hotplug(void) | |
2850 | { | |
2851 | lttng_rb_set_hp_online(0); | |
2852 | cpuhp_remove_multi_state(lttng_hp_online); | |
2853 | lttng_rb_set_hp_prepare(0); | |
2854 | cpuhp_remove_multi_state(lttng_hp_prepare); | |
2855 | } | |
2856 | ||
1ee63d8d | 2857 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
1e367326 MD |
2858 | static int lttng_init_cpu_hotplug(void) |
2859 | { | |
2860 | return 0; | |
2861 | } | |
2862 | static void lttng_exit_cpu_hotplug(void) | |
2863 | { | |
2864 | } | |
1ee63d8d | 2865 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
1e367326 MD |
2866 | |
2867 | ||
a90917c3 | 2868 | static int __init lttng_events_init(void) |
4e3c1b9b | 2869 | { |
1c25284c MD |
2870 | int ret; |
2871 | ||
453b2495 JD |
2872 | ret = wrapper_lttng_fixup_sig(THIS_MODULE); |
2873 | if (ret) | |
2874 | return ret; | |
389d7070 | 2875 | ret = wrapper_get_pfnblock_flags_mask_init(); |
36561775 MD |
2876 | if (ret) |
2877 | return ret; | |
2878 | ret = wrapper_get_pageblock_flags_mask_init(); | |
114667d5 MD |
2879 | if (ret) |
2880 | return ret; | |
2881 | ret = lttng_probes_init(); | |
389d7070 MD |
2882 | if (ret) |
2883 | return ret; | |
07dfc1d0 | 2884 | ret = lttng_context_init(); |
20591cf7 MD |
2885 | if (ret) |
2886 | return ret; | |
07dfc1d0 MD |
2887 | ret = lttng_tracepoint_init(); |
2888 | if (ret) | |
2889 | goto error_tp; | |
a90917c3 | 2890 | event_cache = KMEM_CACHE(lttng_event, 0); |
20591cf7 MD |
2891 | if (!event_cache) { |
2892 | ret = -ENOMEM; | |
2893 | goto error_kmem; | |
2894 | } | |
80996790 | 2895 | ret = lttng_abi_init(); |
02119ee5 MD |
2896 | if (ret) |
2897 | goto error_abi; | |
0c956676 MD |
2898 | ret = lttng_logger_init(); |
2899 | if (ret) | |
2900 | goto error_logger; | |
1e367326 MD |
2901 | ret = lttng_init_cpu_hotplug(); |
2902 | if (ret) | |
2903 | goto error_hotplug; | |
101215b7 | 2904 | printk(KERN_NOTICE "LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n", |
489de24b MJ |
2905 | __stringify(LTTNG_MODULES_MAJOR_VERSION), |
2906 | __stringify(LTTNG_MODULES_MINOR_VERSION), | |
2907 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION), | |
2908 | LTTNG_MODULES_EXTRAVERSION, | |
101215b7 MJ |
2909 | LTTNG_VERSION_NAME, |
2910 | #ifdef LTTNG_EXTRA_VERSION_GIT | |
2911 | LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT, | |
2912 | #else | |
2913 | "", | |
2914 | #endif | |
2915 | #ifdef LTTNG_EXTRA_VERSION_NAME | |
2916 | LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME); | |
2917 | #else | |
2918 | ""); | |
2919 | #endif | |
4e3c1b9b | 2920 | return 0; |
0c956676 | 2921 | |
1e367326 MD |
2922 | error_hotplug: |
2923 | lttng_logger_exit(); | |
0c956676 MD |
2924 | error_logger: |
2925 | lttng_abi_exit(); | |
02119ee5 | 2926 | error_abi: |
1c25284c | 2927 | kmem_cache_destroy(event_cache); |
20591cf7 MD |
2928 | error_kmem: |
2929 | lttng_tracepoint_exit(); | |
07dfc1d0 MD |
2930 | error_tp: |
2931 | lttng_context_exit(); | |
101215b7 | 2932 | printk(KERN_NOTICE "LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n", |
489de24b MJ |
2933 | __stringify(LTTNG_MODULES_MAJOR_VERSION), |
2934 | __stringify(LTTNG_MODULES_MINOR_VERSION), | |
2935 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION), | |
2936 | LTTNG_MODULES_EXTRAVERSION, | |
101215b7 MJ |
2937 | LTTNG_VERSION_NAME, |
2938 | #ifdef LTTNG_EXTRA_VERSION_GIT | |
2939 | LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT, | |
2940 | #else | |
2941 | "", | |
2942 | #endif | |
2943 | #ifdef LTTNG_EXTRA_VERSION_NAME | |
2944 | LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME); | |
2945 | #else | |
2946 | ""); | |
2947 | #endif | |
1c25284c | 2948 | return ret; |
4e3c1b9b MD |
2949 | } |
2950 | ||
a90917c3 | 2951 | module_init(lttng_events_init); |
11b5a3c2 | 2952 | |
a90917c3 | 2953 | static void __exit lttng_events_exit(void) |
4e3c1b9b | 2954 | { |
a90917c3 | 2955 | struct lttng_session *session, *tmpsession; |
92e94819 | 2956 | |
1e367326 | 2957 | lttng_exit_cpu_hotplug(); |
0c956676 | 2958 | lttng_logger_exit(); |
80996790 | 2959 | lttng_abi_exit(); |
92e94819 | 2960 | list_for_each_entry_safe(session, tmpsession, &sessions, list) |
a90917c3 | 2961 | lttng_session_destroy(session); |
11b5a3c2 | 2962 | kmem_cache_destroy(event_cache); |
20591cf7 | 2963 | lttng_tracepoint_exit(); |
07dfc1d0 | 2964 | lttng_context_exit(); |
101215b7 | 2965 | printk(KERN_NOTICE "LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n", |
489de24b MJ |
2966 | __stringify(LTTNG_MODULES_MAJOR_VERSION), |
2967 | __stringify(LTTNG_MODULES_MINOR_VERSION), | |
2968 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION), | |
2969 | LTTNG_MODULES_EXTRAVERSION, | |
101215b7 MJ |
2970 | LTTNG_VERSION_NAME, |
2971 | #ifdef LTTNG_EXTRA_VERSION_GIT | |
2972 | LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT, | |
2973 | #else | |
2974 | "", | |
2975 | #endif | |
2976 | #ifdef LTTNG_EXTRA_VERSION_NAME | |
2977 | LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME); | |
2978 | #else | |
2979 | ""); | |
2980 | #endif | |
4e3c1b9b | 2981 | } |
92e94819 | 2982 | |
a90917c3 | 2983 | module_exit(lttng_events_exit); |
11b5a3c2 | 2984 | |
101215b7 MJ |
2985 | #include "extra_version/patches.i" |
2986 | #ifdef LTTNG_EXTRA_VERSION_GIT | |
2987 | MODULE_INFO(extra_version_git, LTTNG_EXTRA_VERSION_GIT); | |
2988 | #endif | |
2989 | #ifdef LTTNG_EXTRA_VERSION_NAME | |
2990 | MODULE_INFO(extra_version_name, LTTNG_EXTRA_VERSION_NAME); | |
2991 | #endif | |
92e94819 MD |
2992 | MODULE_LICENSE("GPL and additional rights"); |
2993 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); | |
1c124020 | 2994 | MODULE_DESCRIPTION("LTTng tracer"); |
9a9973ef MD |
2995 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
2996 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." | |
309370e2 MD |
2997 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) |
2998 | LTTNG_MODULES_EXTRAVERSION); |