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