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