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