Commit | Line | Data |
---|---|---|
8020ceb5 MD |
1 | /* |
2 | * ltt-events.c | |
3 | * | |
4 | * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
6 | * Holds LTTng per-session event registry. | |
7 | * | |
8 | * Dual LGPL v2.1/GPL v2 license. | |
9 | */ | |
10 | ||
b5234c06 MD |
11 | #define _GNU_SOURCE |
12 | #include <stdio.h> | |
13 | #include <urcu/list.h> | |
8165c8da | 14 | #include <urcu/hlist.h> |
b5234c06 | 15 | #include <pthread.h> |
b5234c06 | 16 | #include <errno.h> |
f4681817 MD |
17 | #include <sys/shm.h> |
18 | #include <sys/ipc.h> | |
44c72f10 MD |
19 | #include <stdint.h> |
20 | #include <stddef.h> | |
28b12049 MD |
21 | #include <inttypes.h> |
22 | #include <time.h> | |
2ae57758 | 23 | #include <lttng/ust-endian.h> |
28b12049 | 24 | #include "clock.h" |
44c72f10 MD |
25 | |
26 | #include <urcu-bp.h> | |
27 | #include <urcu/compiler.h> | |
28 | #include <urcu/uatomic.h> | |
29 | #include <urcu/arch.h> | |
30 | ||
31 | #include <lttng/tracepoint.h> | |
4318ae1b | 32 | #include <lttng/ust-events.h> |
44c72f10 MD |
33 | |
34 | #include <usterr-signal-safe.h> | |
35 | #include <helper.h> | |
1f18504e | 36 | #include "error.h" |
08114193 | 37 | #include "compat.h" |
eda498b8 | 38 | #include "lttng-ust-uuid.h" |
44c72f10 | 39 | |
457a6b58 | 40 | #include "tracepoint-internal.h" |
8020ceb5 | 41 | #include "ltt-tracer.h" |
8165c8da | 42 | #include "ltt-tracer-core.h" |
b728d87e | 43 | #include "wait.h" |
8d8a24c8 | 44 | #include "../libringbuffer/shm.h" |
596c4223 | 45 | #include "jhash.h" |
8165c8da MD |
46 | |
47 | /* | |
48 | * The sessions mutex is the centralized mutex across UST tracing | |
17dfb34b MD |
49 | * control and probe registration. All operations within this file are |
50 | * called by the communication thread, under ust_lock protection. | |
8165c8da | 51 | */ |
2b2d6ff7 | 52 | static pthread_mutex_t sessions_mutex = PTHREAD_MUTEX_INITIALIZER; |
8165c8da | 53 | |
17dfb34b | 54 | void ust_lock(void) |
8165c8da MD |
55 | { |
56 | pthread_mutex_lock(&sessions_mutex); | |
57 | } | |
58 | ||
17dfb34b | 59 | void ust_unlock(void) |
8165c8da MD |
60 | { |
61 | pthread_mutex_unlock(&sessions_mutex); | |
62 | } | |
63 | ||
b5234c06 | 64 | static CDS_LIST_HEAD(sessions); |
8165c8da | 65 | |
457a6b58 MD |
66 | /* |
67 | * Wildcard list, containing the active wildcards. | |
68 | * Protected by ust lock. | |
69 | */ | |
70 | static CDS_LIST_HEAD(wildcard_list); | |
71 | ||
8165c8da MD |
72 | /* |
73 | * Pending probes hash table, containing the registered ltt events for | |
74 | * which tracepoint probes are still missing. Protected by the sessions | |
75 | * mutex. | |
76 | */ | |
77 | #define PENDING_PROBE_HASH_BITS 6 | |
78 | #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS) | |
79 | static struct cds_hlist_head pending_probe_table[PENDING_PROBE_HASH_SIZE]; | |
80 | ||
81 | struct ust_pending_probe { | |
82 | struct ltt_event *event; | |
83 | struct cds_hlist_node node; | |
457a6b58 MD |
84 | enum lttng_ust_loglevel_type loglevel_type; |
85 | int loglevel; | |
8165c8da MD |
86 | char name[]; |
87 | }; | |
8020ceb5 MD |
88 | |
89 | static void _ltt_event_destroy(struct ltt_event *event); | |
e6c12e3d | 90 | static void _ltt_wildcard_destroy(struct session_wildcard *sw); |
8020ceb5 MD |
91 | static void _ltt_channel_destroy(struct ltt_channel *chan); |
92 | static int _ltt_event_unregister(struct ltt_event *event); | |
93 | static | |
94 | int _ltt_event_metadata_statedump(struct ltt_session *session, | |
95 | struct ltt_channel *chan, | |
96 | struct ltt_event *event); | |
97 | static | |
98 | int _ltt_session_metadata_statedump(struct ltt_session *session); | |
99 | ||
457a6b58 MD |
100 | int ltt_loglevel_match(const struct lttng_event_desc *desc, |
101 | enum lttng_ust_loglevel_type req_type, | |
102 | int req_loglevel) | |
103 | { | |
104 | int ev_loglevel; | |
105 | ||
106 | if (req_type == LTTNG_UST_LOGLEVEL_ALL) | |
107 | return 1; | |
108 | if (!desc->loglevel) | |
109 | ev_loglevel = TRACE_DEFAULT; | |
110 | else | |
111 | ev_loglevel = *(*desc->loglevel); | |
112 | switch (req_type) { | |
113 | case LTTNG_UST_LOGLEVEL_RANGE: | |
114 | if (ev_loglevel <= req_loglevel || req_loglevel == -1) | |
115 | return 1; | |
116 | else | |
117 | return 0; | |
118 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
119 | if (ev_loglevel == req_loglevel || req_loglevel == -1) | |
120 | return 1; | |
121 | else | |
122 | return 0; | |
123 | case LTTNG_UST_LOGLEVEL_ALL: | |
124 | default: | |
125 | return 1; | |
126 | } | |
127 | } | |
128 | ||
129 | /* | |
130 | * Return wildcard for a given event name if the event name match the | |
131 | * one of the wildcards. | |
132 | * Must be called with ust lock held. | |
133 | * Returns NULL if not present. | |
134 | */ | |
135 | static | |
136 | struct wildcard_entry *match_wildcard(const struct lttng_event_desc *desc) | |
137 | { | |
138 | struct wildcard_entry *e; | |
139 | ||
140 | cds_list_for_each_entry(e, &wildcard_list, list) { | |
141 | /* If only contain '*' */ | |
142 | if (strlen(e->name) == 1) | |
143 | goto possible_match; | |
144 | /* Compare excluding final '*' */ | |
145 | if (!strncmp(desc->name, e->name, strlen(e->name) - 1)) | |
146 | goto possible_match; | |
147 | continue; /* goto next, no match */ | |
148 | possible_match: | |
149 | if (ltt_loglevel_match(desc, | |
150 | e->loglevel_type, | |
151 | e->loglevel)) { | |
152 | return e; | |
153 | } | |
154 | /* no match, loop to next */ | |
155 | } | |
156 | return NULL; | |
157 | } | |
158 | ||
8165c8da MD |
159 | /* |
160 | * called at event creation if probe is missing. | |
161 | * called with session mutex held. | |
162 | */ | |
163 | static | |
457a6b58 MD |
164 | int add_pending_probe(struct ltt_event *event, const char *name, |
165 | enum lttng_ust_loglevel_type loglevel_type, | |
166 | int loglevel) | |
8165c8da MD |
167 | { |
168 | struct cds_hlist_head *head; | |
8165c8da | 169 | struct ust_pending_probe *e; |
ff412fb5 MD |
170 | size_t name_len = strlen(name); |
171 | uint32_t hash; | |
8165c8da | 172 | |
ff412fb5 MD |
173 | if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) { |
174 | WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1); | |
175 | name_len = LTTNG_UST_SYM_NAME_LEN - 1; | |
176 | } | |
177 | hash = jhash(name, name_len, 0); | |
8165c8da MD |
178 | head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)]; |
179 | e = zmalloc(sizeof(struct ust_pending_probe) + name_len); | |
180 | if (!e) | |
181 | return -ENOMEM; | |
ff412fb5 MD |
182 | memcpy(&e->name[0], name, name_len + 1); |
183 | e->name[name_len] = '\0'; | |
457a6b58 MD |
184 | e->loglevel_type = loglevel_type; |
185 | e->loglevel = loglevel; | |
8165c8da MD |
186 | cds_hlist_add_head(&e->node, head); |
187 | e->event = event; | |
188 | event->pending_probe = e; | |
189 | return 0; | |
190 | } | |
191 | ||
192 | /* | |
193 | * remove a pending probe. called when at event teardown and when an | |
194 | * event is fixed (probe is loaded). | |
195 | * called with session mutex held. | |
196 | */ | |
197 | static | |
198 | void remove_pending_probe(struct ust_pending_probe *e) | |
199 | { | |
200 | if (!e) | |
201 | return; | |
202 | cds_hlist_del(&e->node); | |
203 | free(e); | |
204 | } | |
205 | ||
206 | /* | |
207 | * Called at library load: connect the probe on the events pending on | |
208 | * probe load. | |
209 | * called with session mutex held. | |
210 | */ | |
211 | int pending_probe_fix_events(const struct lttng_event_desc *desc) | |
212 | { | |
213 | struct cds_hlist_head *head; | |
214 | struct cds_hlist_node *node, *p; | |
215 | struct ust_pending_probe *e; | |
216 | const char *name = desc->name; | |
8165c8da | 217 | int ret = 0; |
7ee3cc56 | 218 | struct lttng_ust_event event_param; |
ff412fb5 MD |
219 | size_t name_len = strlen(name); |
220 | uint32_t hash; | |
8165c8da | 221 | |
e6c12e3d MD |
222 | /* Wildcard */ |
223 | { | |
224 | struct wildcard_entry *wildcard; | |
225 | ||
457a6b58 | 226 | wildcard = match_wildcard(desc); |
e6c12e3d MD |
227 | if (strcmp(desc->name, "lttng_ust:metadata") && wildcard) { |
228 | struct session_wildcard *sw; | |
229 | ||
230 | cds_list_for_each_entry(sw, &wildcard->session_list, | |
231 | session_list) { | |
232 | struct ltt_event *ev; | |
233 | int ret; | |
234 | ||
235 | memcpy(&event_param, &sw->event_param, | |
236 | sizeof(event_param)); | |
237 | memcpy(event_param.name, | |
238 | desc->name, | |
239 | sizeof(event_param.name)); | |
240 | /* create event */ | |
241 | ret = ltt_event_create(sw->chan, | |
242 | &event_param, NULL, | |
243 | &ev); | |
244 | if (ret) { | |
245 | DBG("Error creating event"); | |
246 | continue; | |
247 | } | |
248 | cds_list_add(&ev->wildcard_list, | |
249 | &sw->events); | |
250 | } | |
251 | } | |
252 | } | |
253 | ||
ff412fb5 MD |
254 | if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) { |
255 | WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1); | |
256 | name_len = LTTNG_UST_SYM_NAME_LEN - 1; | |
257 | } | |
258 | hash = jhash(name, name_len, 0); | |
8165c8da MD |
259 | head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)]; |
260 | cds_hlist_for_each_entry_safe(e, node, p, head, node) { | |
261 | struct ltt_event *event; | |
262 | struct ltt_channel *chan; | |
263 | ||
fe2d02f4 | 264 | if (!ltt_loglevel_match(desc, |
457a6b58 MD |
265 | e->loglevel_type, |
266 | e->loglevel)) { | |
267 | continue; | |
268 | } | |
269 | if (strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) { | |
270 | continue; | |
271 | } | |
fe2d02f4 | 272 | event = e->event; |
8165c8da MD |
273 | chan = event->chan; |
274 | assert(!event->desc); | |
275 | event->desc = desc; | |
276 | event->pending_probe = NULL; | |
277 | remove_pending_probe(e); | |
278 | ret |= __tracepoint_probe_register(name, | |
279 | event->desc->probe_callback, | |
67e5f391 | 280 | event, event->desc->signature); |
636c5aa4 MD |
281 | if (ret) |
282 | continue; | |
283 | event->id = chan->free_event_id++; | |
8165c8da MD |
284 | ret |= _ltt_event_metadata_statedump(chan->session, chan, |
285 | event); | |
286 | } | |
287 | return ret; | |
288 | } | |
289 | ||
8020ceb5 MD |
290 | void synchronize_trace(void) |
291 | { | |
8020ceb5 | 292 | synchronize_rcu(); |
8020ceb5 MD |
293 | } |
294 | ||
295 | struct ltt_session *ltt_session_create(void) | |
296 | { | |
297 | struct ltt_session *session; | |
19d8b1b3 | 298 | int ret; |
8020ceb5 | 299 | |
b5234c06 | 300 | session = zmalloc(sizeof(struct ltt_session)); |
8020ceb5 MD |
301 | if (!session) |
302 | return NULL; | |
b5234c06 MD |
303 | CDS_INIT_LIST_HEAD(&session->chan); |
304 | CDS_INIT_LIST_HEAD(&session->events); | |
e6c12e3d | 305 | CDS_INIT_LIST_HEAD(&session->wildcards); |
19d8b1b3 MD |
306 | ret = lttng_ust_uuid_generate(session->uuid); |
307 | if (ret != 0) { | |
308 | session->uuid[0] = '\0'; | |
309 | } | |
b5234c06 | 310 | cds_list_add(&session->list, &sessions); |
8020ceb5 MD |
311 | return session; |
312 | } | |
313 | ||
314 | void ltt_session_destroy(struct ltt_session *session) | |
315 | { | |
316 | struct ltt_channel *chan, *tmpchan; | |
317 | struct ltt_event *event, *tmpevent; | |
e6c12e3d | 318 | struct session_wildcard *wildcard, *tmpwildcard; |
8020ceb5 MD |
319 | int ret; |
320 | ||
b5234c06 MD |
321 | CMM_ACCESS_ONCE(session->active) = 0; |
322 | cds_list_for_each_entry(event, &session->events, list) { | |
8020ceb5 MD |
323 | ret = _ltt_event_unregister(event); |
324 | WARN_ON(ret); | |
325 | } | |
326 | synchronize_trace(); /* Wait for in-flight events to complete */ | |
e6c12e3d MD |
327 | cds_list_for_each_entry_safe(wildcard, tmpwildcard, &session->wildcards, list) |
328 | _ltt_wildcard_destroy(wildcard); | |
b5234c06 | 329 | cds_list_for_each_entry_safe(event, tmpevent, &session->events, list) |
8020ceb5 | 330 | _ltt_event_destroy(event); |
b5234c06 | 331 | cds_list_for_each_entry_safe(chan, tmpchan, &session->chan, list) |
8020ceb5 | 332 | _ltt_channel_destroy(chan); |
b5234c06 | 333 | cds_list_del(&session->list); |
b5234c06 | 334 | free(session); |
8020ceb5 MD |
335 | } |
336 | ||
976fe9ea | 337 | int ltt_session_enable(struct ltt_session *session) |
8020ceb5 MD |
338 | { |
339 | int ret = 0; | |
340 | struct ltt_channel *chan; | |
341 | ||
8020ceb5 MD |
342 | if (session->active) { |
343 | ret = -EBUSY; | |
344 | goto end; | |
345 | } | |
346 | ||
347 | /* | |
348 | * Snapshot the number of events per channel to know the type of header | |
349 | * we need to use. | |
350 | */ | |
b5234c06 | 351 | cds_list_for_each_entry(chan, &session->chan, list) { |
8020ceb5 MD |
352 | if (chan->header_type) |
353 | continue; /* don't change it if session stop/restart */ | |
354 | if (chan->free_event_id < 31) | |
355 | chan->header_type = 1; /* compact */ | |
356 | else | |
357 | chan->header_type = 2; /* large */ | |
358 | } | |
359 | ||
b5234c06 MD |
360 | CMM_ACCESS_ONCE(session->active) = 1; |
361 | CMM_ACCESS_ONCE(session->been_active) = 1; | |
8020ceb5 | 362 | ret = _ltt_session_metadata_statedump(session); |
4b4de73e | 363 | if (ret) |
b5234c06 | 364 | CMM_ACCESS_ONCE(session->active) = 0; |
8020ceb5 | 365 | end: |
8020ceb5 MD |
366 | return ret; |
367 | } | |
368 | ||
976fe9ea | 369 | int ltt_session_disable(struct ltt_session *session) |
8020ceb5 MD |
370 | { |
371 | int ret = 0; | |
372 | ||
8020ceb5 MD |
373 | if (!session->active) { |
374 | ret = -EBUSY; | |
375 | goto end; | |
376 | } | |
b5234c06 | 377 | CMM_ACCESS_ONCE(session->active) = 0; |
8020ceb5 | 378 | end: |
8020ceb5 MD |
379 | return ret; |
380 | } | |
381 | ||
976fe9ea MD |
382 | int ltt_channel_enable(struct ltt_channel *channel) |
383 | { | |
384 | int old; | |
385 | ||
9beb36ba MD |
386 | if (channel == channel->session->metadata) |
387 | return -EPERM; | |
b5234c06 | 388 | old = uatomic_xchg(&channel->enabled, 1); |
976fe9ea MD |
389 | if (old) |
390 | return -EEXIST; | |
391 | return 0; | |
392 | } | |
393 | ||
394 | int ltt_channel_disable(struct ltt_channel *channel) | |
395 | { | |
396 | int old; | |
397 | ||
9beb36ba MD |
398 | if (channel == channel->session->metadata) |
399 | return -EPERM; | |
b5234c06 | 400 | old = uatomic_xchg(&channel->enabled, 0); |
976fe9ea MD |
401 | if (!old) |
402 | return -EEXIST; | |
403 | return 0; | |
404 | } | |
405 | ||
406 | int ltt_event_enable(struct ltt_event *event) | |
407 | { | |
408 | int old; | |
409 | ||
9beb36ba MD |
410 | if (event->chan == event->chan->session->metadata) |
411 | return -EPERM; | |
b5234c06 | 412 | old = uatomic_xchg(&event->enabled, 1); |
976fe9ea MD |
413 | if (old) |
414 | return -EEXIST; | |
415 | return 0; | |
416 | } | |
417 | ||
418 | int ltt_event_disable(struct ltt_event *event) | |
419 | { | |
420 | int old; | |
421 | ||
9beb36ba MD |
422 | if (event->chan == event->chan->session->metadata) |
423 | return -EPERM; | |
b5234c06 | 424 | old = uatomic_xchg(&event->enabled, 0); |
976fe9ea MD |
425 | if (!old) |
426 | return -EEXIST; | |
427 | return 0; | |
428 | } | |
429 | ||
8020ceb5 MD |
430 | struct ltt_channel *ltt_channel_create(struct ltt_session *session, |
431 | const char *transport_name, | |
432 | void *buf_addr, | |
433 | size_t subbuf_size, size_t num_subbuf, | |
434 | unsigned int switch_timer_interval, | |
193183fb | 435 | unsigned int read_timer_interval, |
ef9ff354 MD |
436 | int **shm_fd, int **wait_fd, |
437 | uint64_t **memory_map_size, | |
d028eddb | 438 | struct ltt_channel *chan_priv_init) |
8020ceb5 | 439 | { |
a3f61e7f | 440 | struct ltt_channel *chan = NULL; |
8020ceb5 MD |
441 | struct ltt_transport *transport; |
442 | ||
8020ceb5 MD |
443 | if (session->been_active) |
444 | goto active; /* Refuse to add channel to active session */ | |
445 | transport = ltt_transport_find(transport_name); | |
446 | if (!transport) { | |
b5234c06 | 447 | DBG("LTTng transport %s not found\n", |
8020ceb5 MD |
448 | transport_name); |
449 | goto notransport; | |
450 | } | |
d028eddb MD |
451 | chan_priv_init->id = session->free_chan_id++; |
452 | chan_priv_init->session = session; | |
8020ceb5 MD |
453 | /* |
454 | * Note: the channel creation op already writes into the packet | |
455 | * headers. Therefore the "chan" information used as input | |
456 | * should be already accessible. | |
457 | */ | |
e7372d13 | 458 | chan = transport->ops.channel_create(transport_name, buf_addr, |
8020ceb5 | 459 | subbuf_size, num_subbuf, switch_timer_interval, |
193183fb | 460 | read_timer_interval, shm_fd, wait_fd, |
d028eddb | 461 | memory_map_size, chan_priv_init); |
a3f61e7f | 462 | if (!chan) |
8020ceb5 | 463 | goto create_error; |
976fe9ea | 464 | chan->enabled = 1; |
8020ceb5 | 465 | chan->ops = &transport->ops; |
b5234c06 | 466 | cds_list_add(&chan->list, &session->chan); |
8020ceb5 MD |
467 | return chan; |
468 | ||
469 | create_error: | |
8020ceb5 MD |
470 | notransport: |
471 | active: | |
8020ceb5 MD |
472 | return NULL; |
473 | } | |
474 | ||
475 | /* | |
476 | * Only used internally at session destruction. | |
477 | */ | |
478 | static | |
479 | void _ltt_channel_destroy(struct ltt_channel *chan) | |
480 | { | |
b5234c06 | 481 | cds_list_del(&chan->list); |
8020ceb5 | 482 | lttng_destroy_context(chan->ctx); |
a3f61e7f | 483 | chan->ops->channel_destroy(chan); |
8020ceb5 MD |
484 | } |
485 | ||
486 | /* | |
487 | * Supports event creation while tracing session is active. | |
488 | */ | |
576599a0 MD |
489 | int ltt_event_create(struct ltt_channel *chan, |
490 | struct lttng_ust_event *event_param, | |
491 | void *filter, | |
492 | struct ltt_event **_event) | |
8020ceb5 | 493 | { |
457a6b58 | 494 | const struct lttng_event_desc *desc = NULL; /* silence gcc */ |
8020ceb5 | 495 | struct ltt_event *event; |
576599a0 | 496 | int ret = 0; |
8020ceb5 | 497 | |
38eb0baa | 498 | if (chan->used_event_id == -1U) { |
576599a0 | 499 | ret = -ENOMEM; |
8020ceb5 | 500 | goto full; |
576599a0 | 501 | } |
8020ceb5 MD |
502 | /* |
503 | * This is O(n^2) (for each event, the loop is called at event | |
504 | * creation). Might require a hash if we have lots of events. | |
505 | */ | |
576599a0 | 506 | cds_list_for_each_entry(event, &chan->session->events, list) { |
ff412fb5 MD |
507 | if (event->desc && !strncmp(event->desc->name, |
508 | event_param->name, | |
509 | LTTNG_UST_SYM_NAME_LEN - 1)) { | |
576599a0 | 510 | ret = -EEXIST; |
8020ceb5 | 511 | goto exist; |
576599a0 MD |
512 | } |
513 | } | |
457a6b58 MD |
514 | |
515 | /* | |
516 | * Check if loglevel match. Refuse to connect event if not. | |
517 | */ | |
518 | if (event_param->instrumentation == LTTNG_UST_TRACEPOINT) { | |
519 | desc = ltt_event_get(event_param->name); | |
fe2d02f4 MD |
520 | if (desc) { |
521 | if (!ltt_loglevel_match(desc, | |
522 | event_param->loglevel_type, | |
523 | event_param->loglevel)) { | |
524 | ret = -EPERM; | |
525 | goto no_loglevel_match; | |
526 | } | |
457a6b58 | 527 | } |
fe2d02f4 MD |
528 | /* |
529 | * If descriptor is not there, it will be added to | |
530 | * pending probes. | |
531 | */ | |
457a6b58 | 532 | } |
b5234c06 | 533 | event = zmalloc(sizeof(struct ltt_event)); |
576599a0 MD |
534 | if (!event) { |
535 | ret = -ENOMEM; | |
8020ceb5 | 536 | goto cache_error; |
576599a0 | 537 | } |
8020ceb5 MD |
538 | event->chan = chan; |
539 | event->filter = filter; | |
8165c8da MD |
540 | /* |
541 | * used_event_id counts the maximum number of event IDs that can | |
542 | * register if all probes register. | |
543 | */ | |
544 | chan->used_event_id++; | |
976fe9ea | 545 | event->enabled = 1; |
8020ceb5 MD |
546 | event->instrumentation = event_param->instrumentation; |
547 | /* Populate ltt_event structure before tracepoint registration. */ | |
b5234c06 | 548 | cmm_smp_wmb(); |
8020ceb5 | 549 | switch (event_param->instrumentation) { |
b5234c06 | 550 | case LTTNG_UST_TRACEPOINT: |
457a6b58 | 551 | event->desc = desc; |
8165c8da MD |
552 | if (event->desc) { |
553 | ret = __tracepoint_probe_register(event_param->name, | |
554 | event->desc->probe_callback, | |
67e5f391 | 555 | event, event->desc->signature); |
8165c8da MD |
556 | if (ret) |
557 | goto register_error; | |
558 | event->id = chan->free_event_id++; | |
559 | } else { | |
560 | /* | |
561 | * If the probe is not present, event->desc stays NULL, | |
562 | * waiting for the probe to register, and the event->id | |
563 | * stays unallocated. | |
564 | */ | |
457a6b58 MD |
565 | ret = add_pending_probe(event, event_param->name, |
566 | event_param->loglevel_type, | |
567 | event_param->loglevel); | |
8165c8da MD |
568 | if (ret) |
569 | goto add_pending_error; | |
570 | } | |
8020ceb5 | 571 | break; |
8020ceb5 MD |
572 | default: |
573 | WARN_ON_ONCE(1); | |
574 | } | |
8165c8da MD |
575 | if (event->desc) { |
576 | ret = _ltt_event_metadata_statedump(chan->session, chan, event); | |
577 | if (ret) | |
578 | goto statedump_error; | |
579 | } | |
b5234c06 | 580 | cds_list_add(&event->list, &chan->session->events); |
576599a0 MD |
581 | *_event = event; |
582 | return 0; | |
8020ceb5 MD |
583 | |
584 | statedump_error: | |
8165c8da MD |
585 | if (event->desc) { |
586 | WARN_ON_ONCE(__tracepoint_probe_unregister(event_param->name, | |
587 | event->desc->probe_callback, | |
588 | event)); | |
589 | ltt_event_put(event->desc); | |
590 | } | |
591 | add_pending_error: | |
8020ceb5 | 592 | register_error: |
b5234c06 | 593 | free(event); |
8020ceb5 | 594 | cache_error: |
457a6b58 | 595 | no_loglevel_match: |
8020ceb5 MD |
596 | exist: |
597 | full: | |
576599a0 | 598 | return ret; |
8020ceb5 MD |
599 | } |
600 | ||
601 | /* | |
602 | * Only used internally at session destruction. | |
603 | */ | |
604 | int _ltt_event_unregister(struct ltt_event *event) | |
605 | { | |
606 | int ret = -EINVAL; | |
607 | ||
608 | switch (event->instrumentation) { | |
b5234c06 | 609 | case LTTNG_UST_TRACEPOINT: |
8165c8da MD |
610 | if (event->desc) { |
611 | ret = __tracepoint_probe_unregister(event->desc->name, | |
612 | event->desc->probe_callback, | |
613 | event); | |
614 | if (ret) | |
615 | return ret; | |
616 | } else { | |
617 | remove_pending_probe(event->pending_probe); | |
618 | ret = 0; | |
619 | } | |
8020ceb5 | 620 | break; |
8020ceb5 MD |
621 | default: |
622 | WARN_ON_ONCE(1); | |
623 | } | |
624 | return ret; | |
625 | } | |
626 | ||
627 | /* | |
628 | * Only used internally at session destruction. | |
629 | */ | |
630 | static | |
631 | void _ltt_event_destroy(struct ltt_event *event) | |
632 | { | |
633 | switch (event->instrumentation) { | |
b5234c06 | 634 | case LTTNG_UST_TRACEPOINT: |
8165c8da MD |
635 | if (event->desc) { |
636 | ltt_event_put(event->desc); | |
637 | } | |
8020ceb5 | 638 | break; |
8020ceb5 MD |
639 | default: |
640 | WARN_ON_ONCE(1); | |
641 | } | |
b5234c06 | 642 | cds_list_del(&event->list); |
8020ceb5 | 643 | lttng_destroy_context(event->ctx); |
b5234c06 | 644 | free(event); |
8020ceb5 MD |
645 | } |
646 | ||
647 | /* | |
648 | * We have exclusive access to our metadata buffer (protected by the | |
17dfb34b | 649 | * ust_lock), so we can do racy operations such as looking for |
8020ceb5 MD |
650 | * remaining space left in packet and write, since mutual exclusion |
651 | * protects us from concurrent writes. | |
652 | */ | |
653 | int lttng_metadata_printf(struct ltt_session *session, | |
654 | const char *fmt, ...) | |
655 | { | |
4cfec15c | 656 | struct lttng_ust_lib_ring_buffer_ctx ctx; |
8020ceb5 | 657 | struct ltt_channel *chan = session->metadata; |
b5234c06 | 658 | char *str = NULL; |
8020ceb5 MD |
659 | int ret = 0, waitret; |
660 | size_t len, reserve_len, pos; | |
661 | va_list ap; | |
662 | ||
b5234c06 | 663 | WARN_ON_ONCE(!CMM_ACCESS_ONCE(session->active)); |
8020ceb5 MD |
664 | |
665 | va_start(ap, fmt); | |
b5234c06 | 666 | ret = vasprintf(&str, fmt, ap); |
8020ceb5 | 667 | va_end(ap); |
b5234c06 | 668 | if (ret < 0) |
8020ceb5 MD |
669 | return -ENOMEM; |
670 | ||
671 | len = strlen(str); | |
672 | pos = 0; | |
673 | ||
674 | for (pos = 0; pos < len; pos += reserve_len) { | |
675 | reserve_len = min_t(size_t, | |
1d498196 | 676 | chan->ops->packet_avail_size(chan->chan, chan->handle), |
8020ceb5 MD |
677 | len - pos); |
678 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, | |
1d498196 | 679 | sizeof(char), -1, chan->handle); |
8020ceb5 MD |
680 | /* |
681 | * We don't care about metadata buffer's records lost | |
682 | * count, because we always retry here. Report error if | |
683 | * we need to bail out after timeout or being | |
684 | * interrupted. | |
685 | */ | |
b5234c06 | 686 | waitret = wait_cond_interruptible_timeout( |
8020ceb5 MD |
687 | ({ |
688 | ret = chan->ops->event_reserve(&ctx, 0); | |
689 | ret != -ENOBUFS || !ret; | |
690 | }), | |
b5234c06 | 691 | LTTNG_METADATA_TIMEOUT_MSEC); |
b472cfc0 | 692 | if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) { |
b5234c06 MD |
693 | DBG("LTTng: Failure to write metadata to buffers (%s)\n", |
694 | waitret == -EINTR ? "interrupted" : | |
8020ceb5 | 695 | (ret == -ENOBUFS ? "timeout" : "I/O error")); |
b5234c06 | 696 | if (waitret == -EINTR) |
8020ceb5 MD |
697 | ret = waitret; |
698 | goto end; | |
699 | } | |
700 | chan->ops->event_write(&ctx, &str[pos], reserve_len); | |
701 | chan->ops->event_commit(&ctx); | |
702 | } | |
703 | end: | |
b5234c06 | 704 | free(str); |
8020ceb5 MD |
705 | return ret; |
706 | } | |
707 | ||
708 | static | |
709 | int _ltt_field_statedump(struct ltt_session *session, | |
710 | const struct lttng_event_field *field) | |
711 | { | |
712 | int ret = 0; | |
713 | ||
714 | switch (field->type.atype) { | |
715 | case atype_integer: | |
716 | ret = lttng_metadata_printf(session, | |
4e2ee1f5 | 717 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
8020ceb5 MD |
718 | field->type.u.basic.integer.size, |
719 | field->type.u.basic.integer.alignment, | |
720 | field->type.u.basic.integer.signedness, | |
721 | (field->type.u.basic.integer.encoding == lttng_encode_none) | |
722 | ? "none" | |
723 | : (field->type.u.basic.integer.encoding == lttng_encode_UTF8) | |
724 | ? "UTF8" | |
725 | : "ASCII", | |
726 | field->type.u.basic.integer.base, | |
8a98a75d | 727 | #if (BYTE_ORDER == BIG_ENDIAN) |
403c40b4 MD |
728 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
729 | #else | |
730 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", | |
731 | #endif | |
732 | field->name); | |
733 | break; | |
734 | case atype_float: | |
735 | ret = lttng_metadata_printf(session, | |
8a98a75d | 736 | " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n", |
403c40b4 MD |
737 | field->type.u.basic._float.exp_dig, |
738 | field->type.u.basic._float.mant_dig, | |
739 | field->type.u.basic._float.alignment, | |
8a98a75d | 740 | #if (BYTE_ORDER == BIG_ENDIAN) |
8020ceb5 MD |
741 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
742 | #else | |
743 | field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", | |
744 | #endif | |
745 | field->name); | |
746 | break; | |
747 | case atype_enum: | |
748 | ret = lttng_metadata_printf(session, | |
749 | " %s %s;\n", | |
750 | field->type.u.basic.enumeration.name, | |
751 | field->name); | |
752 | break; | |
753 | case atype_array: | |
754 | { | |
755 | const struct lttng_basic_type *elem_type; | |
756 | ||
757 | elem_type = &field->type.u.array.elem_type; | |
758 | ret = lttng_metadata_printf(session, | |
4e2ee1f5 | 759 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
8020ceb5 MD |
760 | elem_type->u.basic.integer.size, |
761 | elem_type->u.basic.integer.alignment, | |
762 | elem_type->u.basic.integer.signedness, | |
763 | (elem_type->u.basic.integer.encoding == lttng_encode_none) | |
764 | ? "none" | |
765 | : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8) | |
766 | ? "UTF8" | |
767 | : "ASCII", | |
768 | elem_type->u.basic.integer.base, | |
8a98a75d | 769 | #if (BYTE_ORDER == BIG_ENDIAN) |
8020ceb5 MD |
770 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
771 | #else | |
772 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", | |
773 | #endif | |
774 | field->name, field->type.u.array.length); | |
775 | break; | |
776 | } | |
777 | case atype_sequence: | |
778 | { | |
779 | const struct lttng_basic_type *elem_type; | |
780 | const struct lttng_basic_type *length_type; | |
781 | ||
782 | elem_type = &field->type.u.sequence.elem_type; | |
783 | length_type = &field->type.u.sequence.length_type; | |
784 | ret = lttng_metadata_printf(session, | |
785 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", | |
786 | length_type->u.basic.integer.size, | |
787 | (unsigned int) length_type->u.basic.integer.alignment, | |
788 | length_type->u.basic.integer.signedness, | |
789 | (length_type->u.basic.integer.encoding == lttng_encode_none) | |
790 | ? "none" | |
791 | : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8) | |
792 | ? "UTF8" | |
793 | : "ASCII"), | |
794 | length_type->u.basic.integer.base, | |
8a98a75d | 795 | #if (BYTE_ORDER == BIG_ENDIAN) |
8020ceb5 MD |
796 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
797 | #else | |
798 | length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", | |
799 | #endif | |
800 | field->name); | |
801 | if (ret) | |
802 | return ret; | |
803 | ||
804 | ret = lttng_metadata_printf(session, | |
4e2ee1f5 | 805 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
8020ceb5 MD |
806 | elem_type->u.basic.integer.size, |
807 | (unsigned int) elem_type->u.basic.integer.alignment, | |
808 | elem_type->u.basic.integer.signedness, | |
809 | (elem_type->u.basic.integer.encoding == lttng_encode_none) | |
810 | ? "none" | |
811 | : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8) | |
812 | ? "UTF8" | |
813 | : "ASCII"), | |
814 | elem_type->u.basic.integer.base, | |
8a98a75d | 815 | #if (BYTE_ORDER == BIG_ENDIAN) |
8020ceb5 MD |
816 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "", |
817 | #else | |
818 | elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "", | |
819 | #endif | |
820 | field->name, | |
821 | field->name); | |
822 | break; | |
823 | } | |
824 | ||
825 | case atype_string: | |
826 | /* Default encoding is UTF8 */ | |
827 | ret = lttng_metadata_printf(session, | |
4e2ee1f5 | 828 | " string%s _%s;\n", |
8020ceb5 MD |
829 | field->type.u.basic.string.encoding == lttng_encode_ASCII ? |
830 | " { encoding = ASCII; }" : "", | |
831 | field->name); | |
832 | break; | |
833 | default: | |
834 | WARN_ON_ONCE(1); | |
835 | return -EINVAL; | |
836 | } | |
837 | return ret; | |
838 | } | |
839 | ||
840 | static | |
841 | int _ltt_context_metadata_statedump(struct ltt_session *session, | |
842 | struct lttng_ctx *ctx) | |
843 | { | |
844 | int ret = 0; | |
845 | int i; | |
846 | ||
847 | if (!ctx) | |
848 | return 0; | |
849 | for (i = 0; i < ctx->nr_fields; i++) { | |
850 | const struct lttng_ctx_field *field = &ctx->fields[i]; | |
851 | ||
852 | ret = _ltt_field_statedump(session, &field->event_field); | |
853 | if (ret) | |
854 | return ret; | |
855 | } | |
856 | return ret; | |
857 | } | |
858 | ||
859 | static | |
860 | int _ltt_fields_metadata_statedump(struct ltt_session *session, | |
861 | struct ltt_event *event) | |
862 | { | |
863 | const struct lttng_event_desc *desc = event->desc; | |
864 | int ret = 0; | |
865 | int i; | |
866 | ||
867 | for (i = 0; i < desc->nr_fields; i++) { | |
868 | const struct lttng_event_field *field = &desc->fields[i]; | |
869 | ||
870 | ret = _ltt_field_statedump(session, field); | |
871 | if (ret) | |
872 | return ret; | |
873 | } | |
874 | return ret; | |
875 | } | |
876 | ||
877 | static | |
878 | int _ltt_event_metadata_statedump(struct ltt_session *session, | |
879 | struct ltt_channel *chan, | |
880 | struct ltt_event *event) | |
881 | { | |
882 | int ret = 0; | |
8155c5e8 | 883 | int loglevel = TRACE_DEFAULT; |
8020ceb5 | 884 | |
b5234c06 | 885 | if (event->metadata_dumped || !CMM_ACCESS_ONCE(session->active)) |
8020ceb5 MD |
886 | return 0; |
887 | if (chan == session->metadata) | |
888 | return 0; | |
8165c8da MD |
889 | /* |
890 | * Don't print events for which probe load is pending. | |
891 | */ | |
892 | if (!event->desc) | |
893 | return 0; | |
8020ceb5 MD |
894 | |
895 | ret = lttng_metadata_printf(session, | |
896 | "event {\n" | |
7083f0fe | 897 | " name = \"%s\";\n" |
8020ceb5 MD |
898 | " id = %u;\n" |
899 | " stream_id = %u;\n", | |
900 | event->desc->name, | |
901 | event->id, | |
902 | event->chan->id); | |
903 | if (ret) | |
904 | goto end; | |
905 | ||
8155c5e8 MD |
906 | if (event->desc->loglevel) |
907 | loglevel = *(*event->desc->loglevel); | |
908 | ||
909 | ret = lttng_metadata_printf(session, | |
910 | " loglevel = %d;\n", | |
911 | loglevel); | |
912 | if (ret) | |
913 | goto end; | |
e6c12e3d | 914 | |
8020ceb5 MD |
915 | if (event->ctx) { |
916 | ret = lttng_metadata_printf(session, | |
917 | " context := struct {\n"); | |
918 | if (ret) | |
919 | goto end; | |
920 | } | |
921 | ret = _ltt_context_metadata_statedump(session, event->ctx); | |
922 | if (ret) | |
923 | goto end; | |
924 | if (event->ctx) { | |
925 | ret = lttng_metadata_printf(session, | |
926 | " };\n"); | |
927 | if (ret) | |
928 | goto end; | |
929 | } | |
930 | ||
931 | ret = lttng_metadata_printf(session, | |
932 | " fields := struct {\n" | |
933 | ); | |
934 | if (ret) | |
935 | goto end; | |
936 | ||
937 | ret = _ltt_fields_metadata_statedump(session, event); | |
938 | if (ret) | |
939 | goto end; | |
940 | ||
941 | /* | |
942 | * LTTng space reservation can only reserve multiples of the | |
943 | * byte size. | |
944 | */ | |
945 | ret = lttng_metadata_printf(session, | |
946 | " };\n" | |
947 | "};\n\n"); | |
948 | if (ret) | |
949 | goto end; | |
950 | ||
951 | event->metadata_dumped = 1; | |
952 | end: | |
953 | return ret; | |
954 | ||
955 | } | |
956 | ||
957 | static | |
958 | int _ltt_channel_metadata_statedump(struct ltt_session *session, | |
959 | struct ltt_channel *chan) | |
960 | { | |
961 | int ret = 0; | |
962 | ||
b5234c06 | 963 | if (chan->metadata_dumped || !CMM_ACCESS_ONCE(session->active)) |
8020ceb5 MD |
964 | return 0; |
965 | if (chan == session->metadata) | |
966 | return 0; | |
967 | ||
968 | WARN_ON_ONCE(!chan->header_type); | |
969 | ret = lttng_metadata_printf(session, | |
970 | "stream {\n" | |
971 | " id = %u;\n" | |
972 | " event.header := %s;\n" | |
973 | " packet.context := struct packet_context;\n", | |
974 | chan->id, | |
975 | chan->header_type == 1 ? "struct event_header_compact" : | |
976 | "struct event_header_large"); | |
977 | if (ret) | |
978 | goto end; | |
979 | ||
980 | if (chan->ctx) { | |
981 | ret = lttng_metadata_printf(session, | |
982 | " event.context := struct {\n"); | |
983 | if (ret) | |
984 | goto end; | |
985 | } | |
986 | ret = _ltt_context_metadata_statedump(session, chan->ctx); | |
987 | if (ret) | |
988 | goto end; | |
989 | if (chan->ctx) { | |
990 | ret = lttng_metadata_printf(session, | |
991 | " };\n"); | |
992 | if (ret) | |
993 | goto end; | |
994 | } | |
995 | ||
996 | ret = lttng_metadata_printf(session, | |
997 | "};\n\n"); | |
998 | ||
999 | chan->metadata_dumped = 1; | |
1000 | end: | |
1001 | return ret; | |
1002 | } | |
1003 | ||
1004 | static | |
1005 | int _ltt_stream_packet_context_declare(struct ltt_session *session) | |
1006 | { | |
1007 | return lttng_metadata_printf(session, | |
1008 | "struct packet_context {\n" | |
28b12049 MD |
1009 | " uint64_clock_monotonic_t timestamp_begin;\n" |
1010 | " uint64_clock_monotonic_t timestamp_end;\n" | |
8020ceb5 MD |
1011 | " uint32_t events_discarded;\n" |
1012 | " uint32_t content_size;\n" | |
1013 | " uint32_t packet_size;\n" | |
1014 | " uint32_t cpu_id;\n" | |
1015 | "};\n\n" | |
1016 | ); | |
1017 | } | |
1018 | ||
1019 | /* | |
1020 | * Compact header: | |
1021 | * id: range: 0 - 30. | |
1022 | * id 31 is reserved to indicate an extended header. | |
1023 | * | |
1024 | * Large header: | |
1025 | * id: range: 0 - 65534. | |
1026 | * id 65535 is reserved to indicate an extended header. | |
1027 | */ | |
1028 | static | |
1029 | int _ltt_event_header_declare(struct ltt_session *session) | |
1030 | { | |
1031 | return lttng_metadata_printf(session, | |
1032 | "struct event_header_compact {\n" | |
1033 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" | |
1034 | " variant <id> {\n" | |
1035 | " struct {\n" | |
28b12049 | 1036 | " uint27_clock_monotonic_t timestamp;\n" |
8020ceb5 MD |
1037 | " } compact;\n" |
1038 | " struct {\n" | |
1039 | " uint32_t id;\n" | |
28b12049 | 1040 | " uint64_clock_monotonic_t timestamp;\n" |
8020ceb5 MD |
1041 | " } extended;\n" |
1042 | " } v;\n" | |
1043 | "} align(%u);\n" | |
1044 | "\n" | |
1045 | "struct event_header_large {\n" | |
1046 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" | |
1047 | " variant <id> {\n" | |
1048 | " struct {\n" | |
28b12049 | 1049 | " uint32_clock_monotonic_t timestamp;\n" |
8020ceb5 MD |
1050 | " } compact;\n" |
1051 | " struct {\n" | |
1052 | " uint32_t id;\n" | |
28b12049 | 1053 | " uint64_clock_monotonic_t timestamp;\n" |
8020ceb5 MD |
1054 | " } extended;\n" |
1055 | " } v;\n" | |
1056 | "} align(%u);\n\n", | |
1dbfff0c MD |
1057 | lttng_alignof(uint32_t) * CHAR_BIT, |
1058 | lttng_alignof(uint16_t) * CHAR_BIT | |
8020ceb5 MD |
1059 | ); |
1060 | } | |
1061 | ||
28b12049 MD |
1062 | /* |
1063 | * Approximation of NTP time of day to clock monotonic correlation, | |
1064 | * taken at start of trace. | |
1065 | * Yes, this is only an approximation. Yes, we can (and will) do better | |
1066 | * in future versions. | |
1067 | */ | |
1068 | static | |
1069 | uint64_t measure_clock_offset(void) | |
1070 | { | |
1071 | uint64_t offset, monotonic[2], realtime; | |
1072 | struct timespec rts = { 0, 0 }; | |
1073 | int ret; | |
1074 | ||
1075 | monotonic[0] = trace_clock_read64(); | |
1076 | ret = clock_gettime(CLOCK_REALTIME, &rts); | |
1077 | if (ret < 0) | |
1078 | return 0; | |
1079 | monotonic[1] = trace_clock_read64(); | |
1080 | offset = (monotonic[0] + monotonic[1]) >> 1; | |
1081 | realtime = rts.tv_sec * 1000000000ULL; | |
1082 | realtime += rts.tv_nsec; | |
1083 | offset = realtime - offset; | |
1084 | return offset; | |
1085 | } | |
1086 | ||
8020ceb5 MD |
1087 | /* |
1088 | * Output metadata into this session's metadata buffers. | |
1089 | */ | |
1090 | static | |
1091 | int _ltt_session_metadata_statedump(struct ltt_session *session) | |
1092 | { | |
b5234c06 | 1093 | unsigned char *uuid_c = session->uuid; |
19d8b1b3 MD |
1094 | char uuid_s[LTTNG_UST_UUID_STR_LEN], |
1095 | clock_uuid_s[LTTNG_UST_UUID_STR_LEN]; | |
8020ceb5 MD |
1096 | struct ltt_channel *chan; |
1097 | struct ltt_event *event; | |
1098 | int ret = 0; | |
08114193 | 1099 | char procname[LTTNG_UST_PROCNAME_LEN] = ""; |
8020ceb5 | 1100 | |
b5234c06 | 1101 | if (!CMM_ACCESS_ONCE(session->active)) |
8020ceb5 MD |
1102 | return 0; |
1103 | if (session->metadata_dumped) | |
1104 | goto skip_session; | |
1105 | if (!session->metadata) { | |
b5234c06 | 1106 | DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n"); |
8020ceb5 MD |
1107 | return -EPERM; |
1108 | } | |
1109 | ||
1110 | snprintf(uuid_s, sizeof(uuid_s), | |
1111 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", | |
1112 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], | |
1113 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], | |
1114 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], | |
1115 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); | |
1116 | ||
1117 | ret = lttng_metadata_printf(session, | |
1118 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" | |
1119 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" | |
1120 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" | |
1121 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" | |
1122 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" | |
1123 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" | |
1124 | "\n" | |
1125 | "trace {\n" | |
1126 | " major = %u;\n" | |
1127 | " minor = %u;\n" | |
1128 | " uuid = \"%s\";\n" | |
1129 | " byte_order = %s;\n" | |
1130 | " packet.header := struct {\n" | |
1131 | " uint32_t magic;\n" | |
1132 | " uint8_t uuid[16];\n" | |
1133 | " uint32_t stream_id;\n" | |
1134 | " };\n" | |
1135 | "};\n\n", | |
1dbfff0c MD |
1136 | lttng_alignof(uint8_t) * CHAR_BIT, |
1137 | lttng_alignof(uint16_t) * CHAR_BIT, | |
1138 | lttng_alignof(uint32_t) * CHAR_BIT, | |
1139 | lttng_alignof(uint64_t) * CHAR_BIT, | |
2694975d MD |
1140 | CTF_SPEC_MAJOR, |
1141 | CTF_SPEC_MINOR, | |
8020ceb5 | 1142 | uuid_s, |
8a98a75d | 1143 | #if (BYTE_ORDER == BIG_ENDIAN) |
8020ceb5 MD |
1144 | "be" |
1145 | #else | |
1146 | "le" | |
1147 | #endif | |
1148 | ); | |
1149 | if (ret) | |
1150 | goto end; | |
1151 | ||
b020e504 | 1152 | /* ignore error, just use empty string if error. */ |
08114193 MD |
1153 | lttng_ust_getprocname(procname); |
1154 | procname[LTTNG_UST_PROCNAME_LEN - 1] = '\0'; | |
b020e504 MD |
1155 | ret = lttng_metadata_printf(session, |
1156 | "env {\n" | |
1157 | " vpid = %d;\n" | |
1158 | " procname = \"%s\";\n" | |
2694975d MD |
1159 | " domain = \"ust\";\n" |
1160 | " tracer_name = \"lttng-ust\";\n" | |
1161 | " tracer_major = %u;\n" | |
1162 | " tracer_minor = %u;\n" | |
1163 | " tracer_patchlevel = %u;\n" | |
b020e504 MD |
1164 | "};\n\n", |
1165 | (int) getpid(), | |
1166 | procname, | |
2694975d MD |
1167 | LTTNG_UST_MAJOR_VERSION, |
1168 | LTTNG_UST_MINOR_VERSION, | |
1169 | LTTNG_UST_PATCHLEVEL_VERSION | |
b020e504 MD |
1170 | ); |
1171 | if (ret) | |
1172 | goto end; | |
1173 | ||
28b12049 MD |
1174 | ret = lttng_metadata_printf(session, |
1175 | "clock {\n" | |
939950af MD |
1176 | " name = %s;\n", |
1177 | "monotonic" | |
1178 | ); | |
1179 | if (ret) | |
1180 | goto end; | |
1181 | ||
1182 | if (!trace_clock_uuid(clock_uuid_s)) { | |
1183 | ret = lttng_metadata_printf(session, | |
1184 | " uuid = \"%s\";\n", | |
1185 | clock_uuid_s | |
1186 | ); | |
1187 | if (ret) | |
1188 | goto end; | |
1189 | } | |
1190 | ||
1191 | ret = lttng_metadata_printf(session, | |
28b12049 MD |
1192 | " description = \"Monotonic Clock\";\n" |
1193 | " freq = %" PRIu64 "; /* Frequency, in Hz */\n" | |
1194 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" | |
1195 | " offset = %" PRIu64 ";\n" | |
1196 | "};\n\n", | |
28b12049 MD |
1197 | trace_clock_freq(), |
1198 | measure_clock_offset() | |
1199 | ); | |
1200 | if (ret) | |
1201 | goto end; | |
1202 | ||
1203 | ret = lttng_metadata_printf(session, | |
1204 | "typealias integer {\n" | |
1205 | " size = 27; align = 1; signed = false;\n" | |
1206 | " map = clock.monotonic.value;\n" | |
1207 | "} := uint27_clock_monotonic_t;\n" | |
1208 | "\n" | |
1209 | "typealias integer {\n" | |
1210 | " size = 32; align = %u; signed = false;\n" | |
1211 | " map = clock.monotonic.value;\n" | |
1212 | "} := uint32_clock_monotonic_t;\n" | |
1213 | "\n" | |
1214 | "typealias integer {\n" | |
1215 | " size = 64; align = %u; signed = false;\n" | |
1216 | " map = clock.monotonic.value;\n" | |
1217 | "} := uint64_clock_monotonic_t;\n\n", | |
1218 | lttng_alignof(uint32_t) * CHAR_BIT, | |
1219 | lttng_alignof(uint64_t) * CHAR_BIT | |
1220 | ); | |
1221 | if (ret) | |
1222 | goto end; | |
1223 | ||
8020ceb5 MD |
1224 | ret = _ltt_stream_packet_context_declare(session); |
1225 | if (ret) | |
1226 | goto end; | |
1227 | ||
1228 | ret = _ltt_event_header_declare(session); | |
1229 | if (ret) | |
1230 | goto end; | |
1231 | ||
1232 | skip_session: | |
b5234c06 | 1233 | cds_list_for_each_entry(chan, &session->chan, list) { |
8020ceb5 MD |
1234 | ret = _ltt_channel_metadata_statedump(session, chan); |
1235 | if (ret) | |
1236 | goto end; | |
1237 | } | |
1238 | ||
b5234c06 | 1239 | cds_list_for_each_entry(event, &session->events, list) { |
8020ceb5 MD |
1240 | ret = _ltt_event_metadata_statedump(session, event->chan, event); |
1241 | if (ret) | |
1242 | goto end; | |
1243 | } | |
1244 | session->metadata_dumped = 1; | |
1245 | end: | |
1246 | return ret; | |
1247 | } | |
1248 | ||
003fedf4 | 1249 | void lttng_ust_events_exit(void) |
8020ceb5 MD |
1250 | { |
1251 | struct ltt_session *session, *tmpsession; | |
1252 | ||
b5234c06 | 1253 | cds_list_for_each_entry_safe(session, tmpsession, &sessions, list) |
8020ceb5 | 1254 | ltt_session_destroy(session); |
8020ceb5 | 1255 | } |
457a6b58 MD |
1256 | |
1257 | /* WILDCARDS */ | |
1258 | ||
1259 | static | |
1260 | int wildcard_same_loglevel(struct wildcard_entry *e, | |
1261 | enum lttng_ust_loglevel_type loglevel_type, | |
1262 | int loglevel) | |
1263 | { | |
1264 | if (e->loglevel_type == loglevel_type && e->loglevel == loglevel) | |
1265 | return 1; | |
1266 | else | |
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | #if 0 | |
1271 | static | |
1272 | int wildcard_is_within(struct wildcard_entry *e, | |
1273 | enum lttng_ust_loglevel_type loglevel_type, | |
1274 | int loglevel) | |
1275 | { | |
1276 | if (e->loglevel_type == LTTNG_UST_LOGLEVEL_ALL | |
1277 | || e->loglevel == -1) | |
1278 | return 1; | |
1279 | switch (e->loglevel_type) { | |
1280 | case LTTNG_UST_LOGLEVEL_RANGE: | |
1281 | switch (loglevel_type) { | |
1282 | case LTTNG_UST_LOGLEVEL_RANGE: | |
1283 | if (e->loglevel >= loglevel) | |
1284 | return 1; | |
1285 | else | |
1286 | return 0; | |
1287 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
1288 | if (e->loglevel <= 0 && loglevel == 0) | |
1289 | return 1; | |
1290 | else | |
1291 | return 0; | |
1292 | } | |
1293 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
1294 | switch (loglevel_type) { | |
1295 | case LTTNG_UST_LOGLEVEL_RANGE: | |
1296 | if (loglevel <= 0) | |
1297 | return 1; | |
1298 | else | |
1299 | return 0; | |
1300 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
1301 | if (e->loglevel == loglevel) | |
1302 | return 1; | |
1303 | else | |
1304 | return 0; | |
1305 | } | |
1306 | } | |
1307 | } | |
1308 | #endif | |
1309 | ||
1310 | /* | |
1311 | * Add the wildcard to the wildcard list. Must be called with | |
1312 | * ust lock held. | |
1313 | */ | |
1314 | static | |
1315 | struct session_wildcard *add_wildcard(struct ltt_channel *chan, | |
1316 | struct lttng_ust_event *event_param) | |
1317 | { | |
1318 | struct wildcard_entry *e; | |
1319 | struct session_wildcard *sw; | |
1320 | size_t name_len = strlen(event_param->name) + 1; | |
1321 | int found = 0; | |
1322 | ||
1323 | /* | |
1324 | * Try to find global wildcard entry. Given that this is shared | |
1325 | * across all sessions, we need to check for exact loglevel | |
1326 | * match, not just whether contained within the existing ones. | |
1327 | */ | |
1328 | cds_list_for_each_entry(e, &wildcard_list, list) { | |
1329 | if (!strncmp(event_param->name, e->name, | |
1330 | LTTNG_UST_SYM_NAME_LEN - 1)) { | |
1331 | if (wildcard_same_loglevel(e, | |
1332 | event_param->loglevel_type, | |
1333 | event_param->loglevel)) { | |
1334 | found = 1; | |
1335 | break; | |
1336 | } | |
1337 | } | |
1338 | } | |
1339 | ||
1340 | if (!found) { | |
1341 | /* | |
1342 | * Create global wildcard entry if not found. Using | |
1343 | * zmalloc here to allocate a variable length element. | |
1344 | * Could cause some memory fragmentation if overused. | |
1345 | */ | |
1346 | e = zmalloc(sizeof(struct wildcard_entry) + name_len); | |
1347 | if (!e) | |
1348 | return ERR_PTR(-ENOMEM); | |
1349 | memcpy(&e->name[0], event_param->name, name_len); | |
1350 | cds_list_add(&e->list, &wildcard_list); | |
1351 | CDS_INIT_LIST_HEAD(&e->session_list); | |
1352 | } | |
1353 | ||
1354 | /* session wildcard */ | |
1355 | cds_list_for_each_entry(sw, &e->session_list, session_list) { | |
1356 | if (chan == sw->chan) { | |
1357 | DBG("wildcard %s busy for this channel", | |
1358 | event_param->name); | |
1359 | return ERR_PTR(-EEXIST); /* Already there */ | |
1360 | } | |
1361 | } | |
1362 | sw = zmalloc(sizeof(struct session_wildcard)); | |
1363 | if (!sw) | |
1364 | return ERR_PTR(-ENOMEM); | |
1365 | sw->chan = chan; | |
1366 | sw->enabled = 1; | |
1367 | memcpy(&sw->event_param, event_param, sizeof(sw->event_param)); | |
1368 | sw->event_param.instrumentation = LTTNG_UST_TRACEPOINT; | |
1369 | sw->event_param.loglevel_type = event_param->loglevel_type; | |
1370 | sw->event_param.loglevel = event_param->loglevel; | |
1371 | CDS_INIT_LIST_HEAD(&sw->events); | |
1372 | cds_list_add(&sw->list, &chan->session->wildcards); | |
1373 | cds_list_add(&sw->session_list, &e->session_list); | |
1374 | sw->entry = e; | |
1375 | ltt_probes_create_wildcard_events(e, sw); | |
1376 | return sw; | |
1377 | } | |
1378 | ||
1379 | /* | |
1380 | * Remove the wildcard from the wildcard list. Must be called with | |
1381 | * ust_lock held. Only called at session teardown. | |
1382 | */ | |
1383 | static | |
1384 | void _remove_wildcard(struct session_wildcard *wildcard) | |
1385 | { | |
1386 | struct ltt_event *ev, *tmp; | |
1387 | ||
1388 | /* | |
1389 | * Just remove the events owned (for enable/disable) by this | |
1390 | * wildcard from the list. The session teardown will take care | |
1391 | * of freeing the event memory. | |
1392 | */ | |
1393 | cds_list_for_each_entry_safe(ev, tmp, &wildcard->events, | |
1394 | wildcard_list) { | |
1395 | cds_list_del(&ev->wildcard_list); | |
1396 | } | |
1397 | cds_list_del(&wildcard->session_list); | |
1398 | cds_list_del(&wildcard->list); | |
1399 | if (cds_list_empty(&wildcard->entry->session_list)) { | |
1400 | cds_list_del(&wildcard->entry->list); | |
1401 | free(wildcard->entry); | |
1402 | } | |
1403 | free(wildcard); | |
1404 | } | |
1405 | ||
1406 | int ltt_wildcard_create(struct ltt_channel *chan, | |
1407 | struct lttng_ust_event *event_param, | |
1408 | struct session_wildcard **_sw) | |
1409 | { | |
1410 | struct session_wildcard *sw; | |
1411 | ||
1412 | sw = add_wildcard(chan, event_param); | |
1413 | if (!sw || IS_ERR(sw)) { | |
1414 | return PTR_ERR(sw); | |
1415 | } | |
1416 | *_sw = sw; | |
1417 | return 0; | |
1418 | } | |
1419 | ||
1420 | static | |
1421 | void _ltt_wildcard_destroy(struct session_wildcard *sw) | |
1422 | { | |
1423 | _remove_wildcard(sw); | |
1424 | } | |
1425 | ||
1426 | int ltt_wildcard_enable(struct session_wildcard *wildcard) | |
1427 | { | |
1428 | struct ltt_event *ev; | |
1429 | int ret; | |
1430 | ||
1431 | if (wildcard->enabled) | |
1432 | return -EEXIST; | |
1433 | cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) { | |
1434 | ret = ltt_event_enable(ev); | |
1435 | if (ret) { | |
1436 | DBG("Error: enable error.\n"); | |
1437 | return ret; | |
1438 | } | |
1439 | } | |
1440 | wildcard->enabled = 1; | |
1441 | return 0; | |
1442 | } | |
1443 | ||
1444 | int ltt_wildcard_disable(struct session_wildcard *wildcard) | |
1445 | { | |
1446 | struct ltt_event *ev; | |
1447 | int ret; | |
1448 | ||
1449 | if (!wildcard->enabled) | |
1450 | return -EEXIST; | |
1451 | cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) { | |
1452 | ret = ltt_event_disable(ev); | |
1453 | if (ret) { | |
1454 | DBG("Error: disable error.\n"); | |
1455 | return ret; | |
1456 | } | |
1457 | } | |
1458 | wildcard->enabled = 0; | |
1459 | return 0; | |
1460 | } | |
60c089cf MD |
1461 | |
1462 | /* | |
1463 | * Take the TLS "fault" in libuuid if dlopen'd, which can take the | |
1464 | * dynamic linker mutex, outside of the UST lock, since the UST lock is | |
1465 | * taken in constructors, which are called with dynamic linker mutex | |
1466 | * held. | |
1467 | */ | |
1468 | void lttng_fixup_event_tls(void) | |
1469 | { | |
1470 | unsigned char uuid[LTTNG_UST_UUID_STR_LEN]; | |
1471 | ||
1472 | (void) lttng_ust_uuid_generate(uuid); | |
1473 | } |