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