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