2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
11 #include "buffer-registry.hpp"
12 #include "trace-ust.hpp"
13 #include "ust-app.hpp"
16 #include <common/common.hpp>
17 #include <common/defaults.hpp>
18 #include <common/trace-chunk.hpp>
19 #include <common/utils.hpp>
27 namespace lsu
= lttng::sessiond::ust
;
30 * Match function for the events hash table lookup.
32 * Matches by name only. Used by the disable command.
34 int trace_ust_ht_match_event_by_name(struct cds_lfht_node
*node
, const void *_key
)
36 struct ltt_ust_event
*event
;
42 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
43 name
= (const char *) _key
;
46 if (strncmp(event
->attr
.name
, name
, sizeof(event
->attr
.name
)) != 0) {
58 * Match function for the hash table lookup.
60 * It matches an ust event based on three attributes which are the event name,
61 * the filter bytecode and the loglevel.
63 int trace_ust_ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
65 struct ltt_ust_event
*event
;
66 const struct ltt_ust_ht_key
*key
;
67 int ev_loglevel_value
;
73 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
74 key
= (ltt_ust_ht_key
*) _key
;
75 ev_loglevel_value
= event
->attr
.loglevel
;
77 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
80 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
84 /* Event loglevel value and type. */
85 ll_match
= loglevels_match(event
->attr
.loglevel_type
,
89 LTTNG_UST_ABI_LOGLEVEL_ALL
);
95 /* Only one of the filters is NULL, fail. */
96 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
100 if (key
->filter
&& event
->filter
) {
101 /* Both filters exists, check length followed by the bytecode. */
102 if (event
->filter
->len
!= key
->filter
->len
||
103 memcmp(event
->filter
->data
, key
->filter
->data
, event
->filter
->len
) != 0) {
108 /* If only one of the exclusions is NULL, fail. */
109 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
113 if (key
->exclusion
&& event
->exclusion
) {
116 /* Check exclusion counts first. */
117 if (event
->exclusion
->count
!= key
->exclusion
->count
) {
121 /* Compare names individually. */
122 for (i
= 0; i
< event
->exclusion
->count
; ++i
) {
125 const char *name_ev
= LTTNG_EVENT_EXCLUSION_NAME_AT(event
->exclusion
, i
);
128 * Compare this exclusion name to all the key's
131 for (j
= 0; j
< key
->exclusion
->count
; ++j
) {
132 const char *name_key
=
133 LTTNG_EVENT_EXCLUSION_NAME_AT(key
->exclusion
, j
);
135 if (!strncmp(name_ev
, name_key
, LTTNG_SYMBOL_NAME_LEN
)) {
143 * If the current exclusion name was not found amongst
144 * the key's exclusion names, then there's no match.
159 * Find the channel in the hashtable and return channel pointer. RCU read side
160 * lock MUST be acquired before calling this.
162 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct lttng_ht
*ht
, const char *name
)
164 struct lttng_ht_node_str
*node
;
165 struct lttng_ht_iter iter
;
167 ASSERT_RCU_READ_LOCKED();
169 * If we receive an empty string for channel name, it means the
170 * default channel name is requested.
173 name
= DEFAULT_CHANNEL_NAME
;
175 lttng_ht_lookup(ht
, (void *) name
, &iter
);
176 node
= lttng_ht_iter_get_node_str(&iter
);
177 if (node
== nullptr) {
181 DBG2("Trace UST channel %s found by name", name
);
183 return lttng::utils::container_of(node
, <t_ust_channel::node
);
186 DBG2("Trace UST channel %s not found by name", name
);
191 * Find the event in the hashtable and return event pointer. RCU read side lock
192 * MUST be acquired before calling this.
194 struct ltt_ust_event
*trace_ust_find_event(struct lttng_ht
*ht
,
196 struct lttng_bytecode
*filter
,
197 enum lttng_ust_abi_loglevel_type loglevel_type
,
199 struct lttng_event_exclusion
*exclusion
)
201 struct lttng_ht_node_str
*node
;
202 struct lttng_ht_iter iter
;
203 struct ltt_ust_ht_key key
;
207 ASSERT_RCU_READ_LOCKED();
211 key
.loglevel_type
= loglevel_type
;
212 key
.loglevel_value
= loglevel_value
;
213 key
.exclusion
= exclusion
;
215 cds_lfht_lookup(ht
->ht
,
216 ht
->hash_fct((void *) name
, lttng_ht_seed
),
217 trace_ust_ht_match_event
,
220 node
= lttng_ht_iter_get_node_str(&iter
);
221 if (node
== nullptr) {
225 DBG2("Trace UST event %s found", key
.name
);
227 return lttng::utils::container_of(node
, <t_ust_event::node
);
230 DBG2("Trace UST event %s NOT found", key
.name
);
235 * Lookup an agent in the session agents hash table by domain type and return
236 * the object if found else NULL.
238 * RCU read side lock must be acquired before calling and only released
239 * once the agent is no longer in scope or being used.
241 struct agent
*trace_ust_find_agent(struct ltt_ust_session
*session
,
242 enum lttng_domain_type domain_type
)
244 struct agent
*agt
= nullptr;
245 struct lttng_ht_node_u64
*node
;
246 struct lttng_ht_iter iter
;
249 LTTNG_ASSERT(session
);
251 DBG3("Trace ust agent lookup for domain %d", domain_type
);
255 lttng_ht_lookup(session
->agents
, &key
, &iter
);
256 node
= lttng_ht_iter_get_node_u64(&iter
);
260 agt
= lttng::utils::container_of(node
, &agent::node
);
267 * Allocate and initialize a ust session data structure.
269 * Return pointer to structure or NULL.
271 struct ltt_ust_session
*trace_ust_create_session(uint64_t session_id
)
273 struct ltt_ust_session
*lus
;
275 /* Allocate a new ltt ust session */
276 lus
= zmalloc
<ltt_ust_session
>();
277 if (lus
== nullptr) {
278 PERROR("create ust session zmalloc");
282 /* Init data structure */
283 lus
->id
= session_id
;
286 /* Set default metadata channel attribute. */
287 lus
->metadata_attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
288 lus
->metadata_attr
.subbuf_size
= default_get_metadata_subbuf_size();
289 lus
->metadata_attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
290 lus
->metadata_attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
291 lus
->metadata_attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;
292 lus
->metadata_attr
.output
= LTTNG_UST_ABI_MMAP
;
295 * Default buffer type. This can be changed through an enable channel
296 * requesting a different type. Note that this can only be changed once
297 * during the session lifetime which is at the first enable channel and
298 * only before start. The flag buffer_type_changed indicates the status.
300 lus
->buffer_type
= LTTNG_BUFFER_PER_UID
;
301 /* Once set to 1, the buffer_type is immutable for the session. */
302 lus
->buffer_type_changed
= 0;
303 /* Init it in case it get used after allocation. */
304 CDS_INIT_LIST_HEAD(&lus
->buffer_reg_uid_list
);
306 /* Alloc UST global domain channels' HT */
307 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
308 /* Alloc agent hash table. */
309 lus
->agents
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
311 lus
->tracker_vpid
= process_attr_tracker_create();
312 if (!lus
->tracker_vpid
) {
315 lus
->tracker_vuid
= process_attr_tracker_create();
316 if (!lus
->tracker_vuid
) {
319 lus
->tracker_vgid
= process_attr_tracker_create();
320 if (!lus
->tracker_vgid
) {
323 lus
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
324 if (lus
->consumer
== nullptr) {
328 DBG2("UST trace session create successful");
333 process_attr_tracker_destroy(lus
->tracker_vpid
);
334 process_attr_tracker_destroy(lus
->tracker_vuid
);
335 process_attr_tracker_destroy(lus
->tracker_vgid
);
336 lttng_ht_destroy(lus
->domain_global
.channels
);
337 lttng_ht_destroy(lus
->agents
);
344 * Allocate and initialize a ust channel data structure.
346 * Return pointer to structure or NULL.
348 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
349 enum lttng_domain_type domain
)
351 struct ltt_ust_channel
*luc
;
355 luc
= zmalloc
<ltt_ust_channel
>();
356 if (luc
== nullptr) {
357 PERROR("ltt_ust_channel zmalloc");
361 luc
->domain
= domain
;
363 /* Copy UST channel attributes */
364 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
365 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
366 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
367 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
368 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
369 luc
->attr
.output
= (enum lttng_ust_abi_output
) chan
->attr
.output
;
370 luc
->monitor_timer_interval
=
371 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
372 luc
->attr
.u
.s
.blocking_timeout
=
373 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->blocking_timeout
;
375 /* Translate to UST output enum */
376 switch (luc
->attr
.output
) {
378 luc
->attr
.output
= LTTNG_UST_ABI_MMAP
;
383 * If we receive an empty string for channel name, it means the
384 * default channel name is requested.
386 if (chan
->name
[0] == '\0') {
387 strncpy(luc
->name
, DEFAULT_CHANNEL_NAME
, sizeof(luc
->name
));
389 /* Copy channel name */
390 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
392 luc
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
395 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
396 CDS_INIT_LIST_HEAD(&luc
->ctx_list
);
398 /* Alloc hash tables */
399 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
400 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
402 /* On-disk circular buffer parameters */
403 luc
->tracefile_size
= chan
->attr
.tracefile_size
;
404 luc
->tracefile_count
= chan
->attr
.tracefile_count
;
406 DBG2("Trace UST channel %s created", luc
->name
);
413 * Validates an exclusion list.
415 * Returns 0 if valid, negative value if invalid.
417 static int validate_exclusion(struct lttng_event_exclusion
*exclusion
)
422 LTTNG_ASSERT(exclusion
);
424 for (i
= 0; i
< exclusion
->count
; ++i
) {
426 const char *name_a
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
428 for (j
= 0; j
< i
; ++j
) {
429 const char *name_b
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, j
);
431 if (!strncmp(name_a
, name_b
, LTTNG_SYMBOL_NAME_LEN
)) {
444 * Allocate and initialize a ust event. Set name and event type.
445 * We own filter_expression, filter, and exclusion.
447 * Return an lttng_error_code
449 enum lttng_error_code
trace_ust_create_event(struct lttng_event
*ev
,
450 char *filter_expression
,
451 struct lttng_bytecode
*filter
,
452 struct lttng_event_exclusion
*exclusion
,
454 struct ltt_ust_event
**ust_event
)
456 struct ltt_ust_event
*local_ust_event
;
457 enum lttng_error_code ret
= LTTNG_OK
;
461 if (exclusion
&& validate_exclusion(exclusion
)) {
462 ret
= LTTNG_ERR_INVALID
;
466 local_ust_event
= zmalloc
<ltt_ust_event
>();
467 if (local_ust_event
== nullptr) {
468 PERROR("ust event zmalloc");
469 ret
= LTTNG_ERR_NOMEM
;
473 local_ust_event
->internal
= internal_event
;
476 case LTTNG_EVENT_PROBE
:
477 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_PROBE
;
479 case LTTNG_EVENT_FUNCTION
:
480 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_FUNCTION
;
482 case LTTNG_EVENT_FUNCTION_ENTRY
:
483 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_FUNCTION
;
485 case LTTNG_EVENT_TRACEPOINT
:
486 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
489 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
490 ret
= LTTNG_ERR_INVALID
;
491 goto error_free_event
;
494 /* Copy event name */
495 strncpy(local_ust_event
->attr
.name
, ev
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
496 local_ust_event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
498 switch (ev
->loglevel_type
) {
499 case LTTNG_EVENT_LOGLEVEL_ALL
:
500 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
501 local_ust_event
->attr
.loglevel
= -1; /* Force to -1 */
503 case LTTNG_EVENT_LOGLEVEL_RANGE
:
504 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
505 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
507 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
508 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
509 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
512 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
513 ret
= LTTNG_ERR_INVALID
;
514 goto error_free_event
;
518 local_ust_event
->filter_expression
= filter_expression
;
519 local_ust_event
->filter
= filter
;
520 local_ust_event
->exclusion
= exclusion
;
523 lttng_ht_node_init_str(&local_ust_event
->node
, local_ust_event
->attr
.name
);
525 DBG2("Trace UST event %s, loglevel (%d,%d) created",
526 local_ust_event
->attr
.name
,
527 local_ust_event
->attr
.loglevel_type
,
528 local_ust_event
->attr
.loglevel
);
530 *ust_event
= local_ust_event
;
535 free(local_ust_event
);
537 free(filter_expression
);
543 static int trace_ust_context_type_event_to_ust(enum lttng_event_context_type type
)
548 case LTTNG_EVENT_CONTEXT_VTID
:
549 utype
= LTTNG_UST_ABI_CONTEXT_VTID
;
551 case LTTNG_EVENT_CONTEXT_VPID
:
552 utype
= LTTNG_UST_ABI_CONTEXT_VPID
;
554 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
555 utype
= LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
;
557 case LTTNG_EVENT_CONTEXT_PROCNAME
:
558 utype
= LTTNG_UST_ABI_CONTEXT_PROCNAME
;
560 case LTTNG_EVENT_CONTEXT_IP
:
561 utype
= LTTNG_UST_ABI_CONTEXT_IP
;
563 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
564 if (!lttng_ust_ctl_has_perf_counters()) {
566 WARN("Perf counters not implemented in UST");
568 utype
= LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
;
571 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
572 utype
= LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
;
574 case LTTNG_EVENT_CONTEXT_CGROUP_NS
:
575 utype
= LTTNG_UST_ABI_CONTEXT_CGROUP_NS
;
577 case LTTNG_EVENT_CONTEXT_IPC_NS
:
578 utype
= LTTNG_UST_ABI_CONTEXT_IPC_NS
;
580 case LTTNG_EVENT_CONTEXT_MNT_NS
:
581 utype
= LTTNG_UST_ABI_CONTEXT_MNT_NS
;
583 case LTTNG_EVENT_CONTEXT_NET_NS
:
584 utype
= LTTNG_UST_ABI_CONTEXT_NET_NS
;
586 case LTTNG_EVENT_CONTEXT_PID_NS
:
587 utype
= LTTNG_UST_ABI_CONTEXT_PID_NS
;
589 case LTTNG_EVENT_CONTEXT_TIME_NS
:
590 utype
= LTTNG_UST_ABI_CONTEXT_TIME_NS
;
592 case LTTNG_EVENT_CONTEXT_USER_NS
:
593 utype
= LTTNG_UST_ABI_CONTEXT_USER_NS
;
595 case LTTNG_EVENT_CONTEXT_UTS_NS
:
596 utype
= LTTNG_UST_ABI_CONTEXT_UTS_NS
;
598 case LTTNG_EVENT_CONTEXT_VUID
:
599 utype
= LTTNG_UST_ABI_CONTEXT_VUID
;
601 case LTTNG_EVENT_CONTEXT_VEUID
:
602 utype
= LTTNG_UST_ABI_CONTEXT_VEUID
;
604 case LTTNG_EVENT_CONTEXT_VSUID
:
605 utype
= LTTNG_UST_ABI_CONTEXT_VSUID
;
607 case LTTNG_EVENT_CONTEXT_VGID
:
608 utype
= LTTNG_UST_ABI_CONTEXT_VGID
;
610 case LTTNG_EVENT_CONTEXT_VEGID
:
611 utype
= LTTNG_UST_ABI_CONTEXT_VEGID
;
613 case LTTNG_EVENT_CONTEXT_VSGID
:
614 utype
= LTTNG_UST_ABI_CONTEXT_VSGID
;
624 * Return 1 if contexts match, 0 otherwise.
626 int trace_ust_match_context(const struct ltt_ust_context
*uctx
,
627 const struct lttng_event_context
*ctx
)
631 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
635 if (uctx
->ctx
.ctx
!= utype
) {
639 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
640 if (uctx
->ctx
.u
.perf_counter
.type
!= ctx
->u
.perf_counter
.type
) {
643 if (uctx
->ctx
.u
.perf_counter
.config
!= ctx
->u
.perf_counter
.config
) {
646 if (strncmp(uctx
->ctx
.u
.perf_counter
.name
,
647 ctx
->u
.perf_counter
.name
,
648 LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
652 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
653 LTTNG_ASSERT(uctx
->ctx
.u
.app_ctx
.provider_name
);
654 LTTNG_ASSERT(uctx
->ctx
.u
.app_ctx
.ctx_name
);
655 if (strcmp(uctx
->ctx
.u
.app_ctx
.provider_name
, ctx
->u
.app_ctx
.provider_name
) != 0 ||
656 strcmp(uctx
->ctx
.u
.app_ctx
.ctx_name
, ctx
->u
.app_ctx
.ctx_name
) != 0) {
666 * Allocate and initialize an UST context.
668 * Return pointer to structure or NULL.
670 struct ltt_ust_context
*trace_ust_create_context(const struct lttng_event_context
*ctx
)
672 struct ltt_ust_context
*uctx
= nullptr;
677 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
679 ERR("Invalid UST context");
683 uctx
= zmalloc
<ltt_ust_context
>();
685 PERROR("zmalloc ltt_ust_context");
689 uctx
->ctx
.ctx
= (enum lttng_ust_abi_context_type
) utype
;
691 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
692 uctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
693 uctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
694 strncpy(uctx
->ctx
.u
.perf_counter
.name
,
695 ctx
->u
.perf_counter
.name
,
696 LTTNG_UST_ABI_SYM_NAME_LEN
);
697 uctx
->ctx
.u
.perf_counter
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
699 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
701 char *provider_name
= nullptr, *ctx_name
= nullptr;
703 provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
704 if (!provider_name
) {
707 uctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
709 ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
713 uctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
719 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
723 trace_ust_destroy_context(uctx
);
727 static void destroy_id_tracker_node_rcu(struct rcu_head
*head
)
729 struct ust_id_tracker_node
*tracker_node
=
730 caa_container_of(head
, struct ust_id_tracker_node
, node
.head
);
734 static void destroy_id_tracker_node(struct ust_id_tracker_node
*tracker_node
)
736 call_rcu(&tracker_node
->node
.head
, destroy_id_tracker_node_rcu
);
739 static int init_id_tracker(struct ust_id_tracker
*id_tracker
)
743 id_tracker
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
744 if (!id_tracker
->ht
) {
745 ret
= LTTNG_ERR_NOMEM
;
754 * Teardown id tracker content, but don't free id_tracker object.
756 static void fini_id_tracker(struct ust_id_tracker
*id_tracker
)
758 struct ust_id_tracker_node
*tracker_node
;
759 struct lttng_ht_iter iter
;
761 if (!id_tracker
->ht
) {
765 cds_lfht_for_each_entry (id_tracker
->ht
->ht
, &iter
.iter
, tracker_node
, node
.node
) {
766 int ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
769 destroy_id_tracker_node(tracker_node
);
772 lttng_ht_destroy(id_tracker
->ht
);
773 id_tracker
->ht
= nullptr;
776 static struct ust_id_tracker_node
*
777 id_tracker_lookup(struct ust_id_tracker
*id_tracker
, int id
, struct lttng_ht_iter
*iter
)
779 unsigned long _id
= (unsigned long) id
;
780 struct lttng_ht_node_ulong
*node
;
782 lttng_ht_lookup(id_tracker
->ht
, (void *) _id
, iter
);
783 node
= lttng_ht_iter_get_node_ulong(iter
);
785 return lttng::utils::container_of(node
, &ust_id_tracker_node::node
);
791 static int id_tracker_add_id(struct ust_id_tracker
*id_tracker
, int id
)
793 int retval
= LTTNG_OK
;
794 struct ust_id_tracker_node
*tracker_node
;
795 struct lttng_ht_iter iter
;
798 retval
= LTTNG_ERR_INVALID
;
801 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
803 /* Already exists. */
804 retval
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
807 tracker_node
= zmalloc
<ust_id_tracker_node
>();
809 retval
= LTTNG_ERR_NOMEM
;
812 lttng_ht_node_init_ulong(&tracker_node
->node
, (unsigned long) id
);
813 lttng_ht_add_unique_ulong(id_tracker
->ht
, &tracker_node
->node
);
818 static int id_tracker_del_id(struct ust_id_tracker
*id_tracker
, int id
)
820 int retval
= LTTNG_OK
, ret
;
821 struct ust_id_tracker_node
*tracker_node
;
822 struct lttng_ht_iter iter
;
825 retval
= LTTNG_ERR_INVALID
;
828 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
831 retval
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
834 ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
837 destroy_id_tracker_node(tracker_node
);
842 static struct ust_id_tracker
*get_id_tracker(struct ltt_ust_session
*session
,
843 enum lttng_process_attr process_attr
)
845 switch (process_attr
) {
846 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
847 return &session
->vpid_tracker
;
848 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
849 return &session
->vuid_tracker
;
850 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
851 return &session
->vgid_tracker
;
857 static struct process_attr_tracker
*
858 _trace_ust_get_process_attr_tracker(struct ltt_ust_session
*session
,
859 enum lttng_process_attr process_attr
)
861 switch (process_attr
) {
862 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
863 return session
->tracker_vpid
;
864 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
865 return session
->tracker_vuid
;
866 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
867 return session
->tracker_vgid
;
873 const struct process_attr_tracker
*
874 trace_ust_get_process_attr_tracker(struct ltt_ust_session
*session
,
875 enum lttng_process_attr process_attr
)
877 return (const struct process_attr_tracker
*) _trace_ust_get_process_attr_tracker(
878 session
, process_attr
);
882 * The session lock is held when calling this function.
884 int trace_ust_id_tracker_lookup(enum lttng_process_attr process_attr
,
885 struct ltt_ust_session
*session
,
888 struct lttng_ht_iter iter
;
889 struct ust_id_tracker
*id_tracker
;
891 id_tracker
= get_id_tracker(session
, process_attr
);
895 if (!id_tracker
->ht
) {
898 if (id_tracker_lookup(id_tracker
, id
, &iter
)) {
905 * Called with the session lock held.
907 enum lttng_error_code
908 trace_ust_process_attr_tracker_set_tracking_policy(struct ltt_ust_session
*session
,
909 enum lttng_process_attr process_attr
,
910 enum lttng_tracking_policy policy
)
913 enum lttng_error_code ret_code
= LTTNG_OK
;
914 struct ust_id_tracker
*id_tracker
= get_id_tracker(session
, process_attr
);
915 struct process_attr_tracker
*tracker
=
916 _trace_ust_get_process_attr_tracker(session
, process_attr
);
917 bool should_update_apps
= false;
918 enum lttng_tracking_policy previous_policy
;
921 ret_code
= LTTNG_ERR_INVALID
;
925 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
926 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
928 ret_code
= LTTNG_ERR_UNK
;
932 if (previous_policy
== policy
) {
937 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
938 /* Track all values: destroy tracker if exists. */
939 if (id_tracker
->ht
) {
940 fini_id_tracker(id_tracker
);
941 /* Ensure all apps have session. */
942 should_update_apps
= true;
945 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
946 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
948 fini_id_tracker(id_tracker
);
949 ret_code
= (lttng_error_code
) init_id_tracker(id_tracker
);
950 if (ret_code
!= LTTNG_OK
) {
951 ERR("Error initializing ID tracker");
954 /* Remove all apps from session. */
955 should_update_apps
= true;
960 if (should_update_apps
&& session
->active
) {
961 ust_app_global_update_all(session
);
967 /* Called with the session lock held. */
968 enum lttng_error_code
969 trace_ust_process_attr_tracker_inclusion_set_add_value(struct ltt_ust_session
*session
,
970 enum lttng_process_attr process_attr
,
971 const struct process_attr_value
*value
)
973 enum lttng_error_code ret_code
= LTTNG_OK
;
974 bool should_update_apps
= false;
975 struct ust_id_tracker
*id_tracker
= get_id_tracker(session
, process_attr
);
976 struct process_attr_tracker
*tracker
;
978 enum process_attr_tracker_status status
;
982 * Convert process attribute tracker value to the integral
983 * representation required by the kern-ctl API.
985 switch (process_attr
) {
986 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
987 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
988 integral_value
= (int) value
->value
.pid
;
990 case LTTNG_PROCESS_ATTR_USER_ID
:
991 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
992 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
995 ret_code
= utils_user_id_from_name(value
->value
.user_name
, &uid
);
996 if (ret_code
!= LTTNG_OK
) {
999 integral_value
= (int) uid
;
1001 integral_value
= (int) value
->value
.uid
;
1004 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1005 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1006 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1009 ret_code
= utils_group_id_from_name(value
->value
.group_name
, &gid
);
1010 if (ret_code
!= LTTNG_OK
) {
1013 integral_value
= (int) gid
;
1015 integral_value
= (int) value
->value
.gid
;
1019 ret_code
= LTTNG_ERR_INVALID
;
1023 tracker
= _trace_ust_get_process_attr_tracker(session
, process_attr
);
1025 ret_code
= LTTNG_ERR_INVALID
;
1029 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
1030 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1032 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
1033 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1035 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1036 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1038 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1040 ret_code
= LTTNG_ERR_UNK
;
1046 DBG("User space track %s %d for session id %" PRIu64
,
1047 lttng_process_attr_to_string(process_attr
),
1051 ret_code
= (lttng_error_code
) id_tracker_add_id(id_tracker
, integral_value
);
1052 if (ret_code
!= LTTNG_OK
) {
1055 /* Add session to application */
1056 switch (process_attr
) {
1057 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1058 app
= ust_app_find_by_pid(integral_value
);
1060 should_update_apps
= true;
1064 should_update_apps
= true;
1067 if (should_update_apps
&& session
->active
) {
1068 ust_app_global_update_all(session
);
1074 /* Called with the session lock held. */
1075 enum lttng_error_code
1076 trace_ust_process_attr_tracker_inclusion_set_remove_value(struct ltt_ust_session
*session
,
1077 enum lttng_process_attr process_attr
,
1078 const struct process_attr_value
*value
)
1080 enum lttng_error_code ret_code
= LTTNG_OK
;
1081 bool should_update_apps
= false;
1082 struct ust_id_tracker
*id_tracker
= get_id_tracker(session
, process_attr
);
1083 struct process_attr_tracker
*tracker
;
1085 enum process_attr_tracker_status status
;
1086 struct ust_app
*app
;
1089 * Convert process attribute tracker value to the integral
1090 * representation required by the kern-ctl API.
1092 switch (process_attr
) {
1093 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1094 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1095 integral_value
= (int) value
->value
.pid
;
1097 case LTTNG_PROCESS_ATTR_USER_ID
:
1098 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1099 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1102 ret_code
= utils_user_id_from_name(value
->value
.user_name
, &uid
);
1103 if (ret_code
!= LTTNG_OK
) {
1106 integral_value
= (int) uid
;
1108 integral_value
= (int) value
->value
.uid
;
1111 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1112 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1113 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1116 ret_code
= utils_group_id_from_name(value
->value
.group_name
, &gid
);
1117 if (ret_code
!= LTTNG_OK
) {
1120 integral_value
= (int) gid
;
1122 integral_value
= (int) value
->value
.gid
;
1126 ret_code
= LTTNG_ERR_INVALID
;
1130 tracker
= _trace_ust_get_process_attr_tracker(session
, process_attr
);
1132 ret_code
= LTTNG_ERR_INVALID
;
1136 status
= process_attr_tracker_inclusion_set_remove_value(tracker
, value
);
1137 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1139 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1140 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1142 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1143 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1145 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1147 ret_code
= LTTNG_ERR_UNK
;
1153 DBG("User space untrack %s %d for session id %" PRIu64
,
1154 lttng_process_attr_to_string(process_attr
),
1158 ret_code
= (lttng_error_code
) id_tracker_del_id(id_tracker
, integral_value
);
1159 if (ret_code
!= LTTNG_OK
) {
1162 /* Add session to application */
1163 switch (process_attr
) {
1164 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1165 app
= ust_app_find_by_pid(integral_value
);
1167 should_update_apps
= true;
1171 should_update_apps
= true;
1174 if (should_update_apps
&& session
->active
) {
1175 ust_app_global_update_all(session
);
1182 * RCU safe free context structure.
1184 static void destroy_context_rcu(struct rcu_head
*head
)
1186 struct lttng_ht_node_ulong
*node
=
1187 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
1188 struct ltt_ust_context
*ctx
= lttng::utils::container_of(node
, <t_ust_context::node
);
1190 trace_ust_destroy_context(ctx
);
1194 * Cleanup UST context hash table.
1196 static void destroy_contexts(struct lttng_ht
*ht
)
1199 struct lttng_ht_node_ulong
*node
;
1200 struct lttng_ht_iter iter
;
1201 struct ltt_ust_context
*ctx
;
1206 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, node
, node
) {
1207 /* Remove from ordered list. */
1208 ctx
= lttng::utils::container_of(node
, <t_ust_context::node
);
1209 cds_list_del(&ctx
->list
);
1210 /* Remove from channel's hash table. */
1211 ret
= lttng_ht_del(ht
, &iter
);
1213 call_rcu(&node
->head
, destroy_context_rcu
);
1218 lttng_ht_destroy(ht
);
1222 * Cleanup ust event structure.
1224 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
1226 LTTNG_ASSERT(event
);
1228 DBG2("Trace destroy UST event %s", event
->attr
.name
);
1229 free(event
->filter_expression
);
1230 free(event
->filter
);
1231 free(event
->exclusion
);
1236 * Cleanup ust context structure.
1238 void trace_ust_destroy_context(struct ltt_ust_context
*ctx
)
1242 if (ctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1243 free(ctx
->ctx
.u
.app_ctx
.provider_name
);
1244 free(ctx
->ctx
.u
.app_ctx
.ctx_name
);
1250 * URCU intermediate call to complete destroy event.
1252 static void destroy_event_rcu(struct rcu_head
*head
)
1254 struct lttng_ht_node_str
*node
= lttng::utils::container_of(head
, <tng_ht_node_str::head
);
1255 struct ltt_ust_event
*event
= lttng::utils::container_of(node
, <t_ust_event::node
);
1257 trace_ust_destroy_event(event
);
1261 * Cleanup UST events hashtable.
1263 static void destroy_events(struct lttng_ht
*events
)
1266 struct lttng_ht_node_str
*node
;
1267 struct lttng_ht_iter iter
;
1269 LTTNG_ASSERT(events
);
1272 cds_lfht_for_each_entry (events
->ht
, &iter
.iter
, node
, node
) {
1273 ret
= lttng_ht_del(events
, &iter
);
1275 call_rcu(&node
->head
, destroy_event_rcu
);
1279 lttng_ht_destroy(events
);
1283 * Cleanup ust channel structure.
1285 * Should _NOT_ be called with RCU read lock held.
1287 static void _trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1289 LTTNG_ASSERT(channel
);
1291 DBG2("Trace destroy UST channel %s", channel
->name
);
1297 * URCU intermediate call to complete destroy channel.
1299 static void destroy_channel_rcu(struct rcu_head
*head
)
1301 struct lttng_ht_node_str
*node
= lttng::utils::container_of(head
, <tng_ht_node_str::head
);
1302 struct ltt_ust_channel
*channel
= lttng::utils::container_of(node
, <t_ust_channel::node
);
1304 _trace_ust_destroy_channel(channel
);
1307 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1309 /* Destroying all events of the channel */
1310 destroy_events(channel
->events
);
1311 /* Destroying all context of the channel */
1312 destroy_contexts(channel
->ctx
);
1314 call_rcu(&channel
->node
.head
, destroy_channel_rcu
);
1318 * Remove an UST channel from a channel HT.
1320 void trace_ust_delete_channel(struct lttng_ht
*ht
, struct ltt_ust_channel
*channel
)
1323 struct lttng_ht_iter iter
;
1326 LTTNG_ASSERT(channel
);
1328 iter
.iter
.node
= &channel
->node
.node
;
1329 ret
= lttng_ht_del(ht
, &iter
);
1333 int trace_ust_regenerate_metadata(struct ltt_ust_session
*usess
)
1336 struct buffer_reg_uid
*uid_reg
= nullptr;
1337 struct buffer_reg_session
*session_reg
= nullptr;
1340 cds_list_for_each_entry (uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
1341 lsu::registry_session
*registry
;
1343 session_reg
= uid_reg
->registry
;
1344 registry
= session_reg
->reg
.ust
;
1347 registry
->regenerate_metadata();
1348 } catch (const std::exception
& ex
) {
1349 ERR("Failed to regenerate user space session metadata: %s", ex
.what());
1361 * Iterate over a hash table containing channels and cleanup safely.
1363 static void destroy_channels(struct lttng_ht
*channels
)
1365 struct lttng_ht_node_str
*node
;
1366 struct lttng_ht_iter iter
;
1368 LTTNG_ASSERT(channels
);
1371 cds_lfht_for_each_entry (channels
->ht
, &iter
.iter
, node
, node
) {
1372 struct ltt_ust_channel
*chan
=
1373 lttng::utils::container_of(node
, <t_ust_channel::node
);
1375 trace_ust_delete_channel(channels
, chan
);
1376 trace_ust_destroy_channel(chan
);
1380 lttng_ht_destroy(channels
);
1384 * Cleanup UST global domain.
1386 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
1390 destroy_channels(dom
->channels
);
1394 * Cleanup ust session structure, keeping data required by
1397 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
1400 struct buffer_reg_uid
*reg
, *sreg
;
1401 struct lttng_ht_iter iter
;
1403 LTTNG_ASSERT(session
);
1405 DBG2("Trace UST destroy session %" PRIu64
, session
->id
);
1407 /* Cleaning up UST domain */
1408 destroy_domain_global(&session
->domain_global
);
1411 cds_lfht_for_each_entry (session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
1412 int ret
= lttng_ht_del(session
->agents
, &iter
);
1419 lttng_ht_destroy(session
->agents
);
1421 /* Cleanup UID buffer registry object(s). */
1422 cds_list_for_each_entry_safe (reg
, sreg
, &session
->buffer_reg_uid_list
, lnode
) {
1423 cds_list_del(®
->lnode
);
1424 buffer_reg_uid_remove(reg
);
1425 buffer_reg_uid_destroy(reg
, session
->consumer
);
1428 process_attr_tracker_destroy(session
->tracker_vpid
);
1429 process_attr_tracker_destroy(session
->tracker_vuid
);
1430 process_attr_tracker_destroy(session
->tracker_vgid
);
1432 fini_id_tracker(&session
->vpid_tracker
);
1433 fini_id_tracker(&session
->vuid_tracker
);
1434 fini_id_tracker(&session
->vgid_tracker
);
1435 lttng_trace_chunk_put(session
->current_trace_chunk
);
1438 /* Free elements needed by destroy notifiers. */
1439 void trace_ust_free_session(struct ltt_ust_session
*session
)
1441 consumer_output_put(session
->consumer
);