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/urcu.hpp>
20 #include <common/utils.hpp>
28 namespace lsu
= lttng::sessiond::ust
;
31 * Match function for the events hash table lookup.
33 * Matches by name only. Used by the disable command.
35 int trace_ust_ht_match_event_by_name(struct cds_lfht_node
*node
, const void *_key
)
37 struct ltt_ust_event
*event
;
43 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
44 name
= (const char *) _key
;
47 if (strncmp(event
->attr
.name
, name
, sizeof(event
->attr
.name
)) != 0) {
59 * Match function for the hash table lookup.
61 * It matches an ust event based on three attributes which are the event name,
62 * the filter bytecode and the loglevel.
64 int trace_ust_ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
66 struct ltt_ust_event
*event
;
67 const struct ltt_ust_ht_key
*key
;
68 int ev_loglevel_value
;
74 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
75 key
= (ltt_ust_ht_key
*) _key
;
76 ev_loglevel_value
= event
->attr
.loglevel
;
78 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
81 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
85 /* Event loglevel value and type. */
86 ll_match
= loglevels_match(event
->attr
.loglevel_type
,
90 LTTNG_UST_ABI_LOGLEVEL_ALL
);
96 /* Only one of the filters is NULL, fail. */
97 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
101 if (key
->filter
&& event
->filter
) {
102 /* Both filters exists, check length followed by the bytecode. */
103 if (event
->filter
->len
!= key
->filter
->len
||
104 memcmp(event
->filter
->data
, key
->filter
->data
, event
->filter
->len
) != 0) {
109 /* If only one of the exclusions is NULL, fail. */
110 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
114 if (key
->exclusion
&& event
->exclusion
) {
117 /* Check exclusion counts first. */
118 if (event
->exclusion
->count
!= key
->exclusion
->count
) {
122 /* Compare names individually. */
123 for (i
= 0; i
< event
->exclusion
->count
; ++i
) {
126 const char *name_ev
= LTTNG_EVENT_EXCLUSION_NAME_AT(event
->exclusion
, i
);
129 * Compare this exclusion name to all the key's
132 for (j
= 0; j
< key
->exclusion
->count
; ++j
) {
133 const char *name_key
=
134 LTTNG_EVENT_EXCLUSION_NAME_AT(key
->exclusion
, j
);
136 if (!strncmp(name_ev
, name_key
, LTTNG_SYMBOL_NAME_LEN
)) {
144 * If the current exclusion name was not found amongst
145 * the key's exclusion names, then there's no match.
160 * Find the channel in the hashtable and return channel pointer. RCU read side
161 * lock MUST be acquired before calling this.
163 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct lttng_ht
*ht
, const char *name
)
165 struct lttng_ht_node_str
*node
;
166 struct lttng_ht_iter iter
;
168 ASSERT_RCU_READ_LOCKED();
170 * If we receive an empty string for channel name, it means the
171 * default channel name is requested.
174 name
= DEFAULT_CHANNEL_NAME
;
176 lttng_ht_lookup(ht
, (void *) name
, &iter
);
177 node
= lttng_ht_iter_get_node_str(&iter
);
178 if (node
== nullptr) {
182 DBG2("Trace UST channel %s found by name", name
);
184 return lttng::utils::container_of(node
, <t_ust_channel::node
);
187 DBG2("Trace UST channel %s not found by name", name
);
192 * Find the event in the hashtable and return event pointer. RCU read side lock
193 * MUST be acquired before calling this.
195 struct ltt_ust_event
*trace_ust_find_event(struct lttng_ht
*ht
,
197 struct lttng_bytecode
*filter
,
198 enum lttng_ust_abi_loglevel_type loglevel_type
,
200 struct lttng_event_exclusion
*exclusion
)
202 struct lttng_ht_node_str
*node
;
203 struct lttng_ht_iter iter
;
204 struct ltt_ust_ht_key key
;
208 ASSERT_RCU_READ_LOCKED();
212 key
.loglevel_type
= loglevel_type
;
213 key
.loglevel_value
= loglevel_value
;
214 key
.exclusion
= exclusion
;
216 cds_lfht_lookup(ht
->ht
,
217 ht
->hash_fct((void *) name
, lttng_ht_seed
),
218 trace_ust_ht_match_event
,
221 node
= lttng_ht_iter_get_node_str(&iter
);
222 if (node
== nullptr) {
226 DBG2("Trace UST event %s found", key
.name
);
228 return lttng::utils::container_of(node
, <t_ust_event::node
);
231 DBG2("Trace UST event %s NOT found", key
.name
);
236 * Lookup an agent in the session agents hash table by domain type and return
237 * the object if found else NULL.
239 * RCU read side lock must be acquired before calling and only released
240 * once the agent is no longer in scope or being used.
242 struct agent
*trace_ust_find_agent(struct ltt_ust_session
*session
,
243 enum lttng_domain_type domain_type
)
245 struct agent
*agt
= nullptr;
246 struct lttng_ht_node_u64
*node
;
247 struct lttng_ht_iter iter
;
250 LTTNG_ASSERT(session
);
252 DBG3("Trace ust agent lookup for domain %d", domain_type
);
256 lttng_ht_lookup(session
->agents
, &key
, &iter
);
257 node
= lttng_ht_iter_get_node_u64(&iter
);
261 agt
= lttng::utils::container_of(node
, &agent::node
);
268 * Allocate and initialize a ust session data structure.
270 * Return pointer to structure or NULL.
272 struct ltt_ust_session
*trace_ust_create_session(uint64_t session_id
)
274 struct ltt_ust_session
*lus
;
276 /* Allocate a new ltt ust session */
277 lus
= zmalloc
<ltt_ust_session
>();
278 if (lus
== nullptr) {
279 PERROR("create ust session zmalloc");
283 /* Init data structure */
284 lus
->id
= session_id
;
287 /* Set default metadata channel attribute. */
288 lus
->metadata_attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
289 lus
->metadata_attr
.subbuf_size
= default_get_metadata_subbuf_size();
290 lus
->metadata_attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
291 lus
->metadata_attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
292 lus
->metadata_attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;
293 lus
->metadata_attr
.output
= LTTNG_UST_ABI_MMAP
;
296 * Default buffer type. This can be changed through an enable channel
297 * requesting a different type. Note that this can only be changed once
298 * during the session lifetime which is at the first enable channel and
299 * only before start. The flag buffer_type_changed indicates the status.
301 lus
->buffer_type
= LTTNG_BUFFER_PER_UID
;
302 /* Once set to 1, the buffer_type is immutable for the session. */
303 lus
->buffer_type_changed
= 0;
304 /* Init it in case it get used after allocation. */
305 CDS_INIT_LIST_HEAD(&lus
->buffer_reg_uid_list
);
307 /* Alloc UST global domain channels' HT */
308 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
309 /* Alloc agent hash table. */
310 lus
->agents
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
312 lus
->tracker_vpid
= process_attr_tracker_create();
313 if (!lus
->tracker_vpid
) {
316 lus
->tracker_vuid
= process_attr_tracker_create();
317 if (!lus
->tracker_vuid
) {
320 lus
->tracker_vgid
= process_attr_tracker_create();
321 if (!lus
->tracker_vgid
) {
324 lus
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
325 if (lus
->consumer
== nullptr) {
329 DBG2("UST trace session create successful");
334 process_attr_tracker_destroy(lus
->tracker_vpid
);
335 process_attr_tracker_destroy(lus
->tracker_vuid
);
336 process_attr_tracker_destroy(lus
->tracker_vgid
);
337 lttng_ht_destroy(lus
->domain_global
.channels
);
338 lttng_ht_destroy(lus
->agents
);
345 * Allocate and initialize a ust channel data structure.
347 * Return pointer to structure or NULL.
349 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
350 enum lttng_domain_type domain
)
352 struct ltt_ust_channel
*luc
;
356 luc
= zmalloc
<ltt_ust_channel
>();
357 if (luc
== nullptr) {
358 PERROR("ltt_ust_channel zmalloc");
362 luc
->domain
= domain
;
364 /* Copy UST channel attributes */
365 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
366 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
367 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
368 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
369 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
370 luc
->attr
.output
= (enum lttng_ust_abi_output
) chan
->attr
.output
;
371 luc
->monitor_timer_interval
=
372 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
373 luc
->attr
.u
.s
.blocking_timeout
=
374 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->blocking_timeout
;
376 /* Translate to UST output enum */
377 switch (luc
->attr
.output
) {
379 luc
->attr
.output
= LTTNG_UST_ABI_MMAP
;
384 * If we receive an empty string for channel name, it means the
385 * default channel name is requested.
387 if (chan
->name
[0] == '\0') {
388 strncpy(luc
->name
, DEFAULT_CHANNEL_NAME
, sizeof(luc
->name
));
390 /* Copy channel name */
391 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
393 luc
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
396 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
397 CDS_INIT_LIST_HEAD(&luc
->ctx_list
);
399 /* Alloc hash tables */
400 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
401 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
403 /* On-disk circular buffer parameters */
404 luc
->tracefile_size
= chan
->attr
.tracefile_size
;
405 luc
->tracefile_count
= chan
->attr
.tracefile_count
;
407 DBG2("Trace UST channel %s created", luc
->name
);
414 * Validates an exclusion list.
416 * Returns 0 if valid, negative value if invalid.
418 static int validate_exclusion(struct lttng_event_exclusion
*exclusion
)
423 LTTNG_ASSERT(exclusion
);
425 for (i
= 0; i
< exclusion
->count
; ++i
) {
427 const char *name_a
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
429 for (j
= 0; j
< i
; ++j
) {
430 const char *name_b
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, j
);
432 if (!strncmp(name_a
, name_b
, LTTNG_SYMBOL_NAME_LEN
)) {
445 * Allocate and initialize a ust event. Set name and event type.
446 * We own filter_expression, filter, and exclusion.
448 * Return an lttng_error_code
450 enum lttng_error_code
trace_ust_create_event(struct lttng_event
*ev
,
451 char *filter_expression
,
452 struct lttng_bytecode
*filter
,
453 struct lttng_event_exclusion
*exclusion
,
455 struct ltt_ust_event
**ust_event
)
457 struct ltt_ust_event
*local_ust_event
;
458 enum lttng_error_code ret
= LTTNG_OK
;
462 if (exclusion
&& validate_exclusion(exclusion
)) {
463 ret
= LTTNG_ERR_INVALID
;
467 local_ust_event
= zmalloc
<ltt_ust_event
>();
468 if (local_ust_event
== nullptr) {
469 PERROR("ust event zmalloc");
470 ret
= LTTNG_ERR_NOMEM
;
474 local_ust_event
->internal
= internal_event
;
477 case LTTNG_EVENT_PROBE
:
478 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_PROBE
;
480 case LTTNG_EVENT_FUNCTION
:
481 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_FUNCTION
;
483 case LTTNG_EVENT_FUNCTION_ENTRY
:
484 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_FUNCTION
;
486 case LTTNG_EVENT_TRACEPOINT
:
487 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
490 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
491 ret
= LTTNG_ERR_INVALID
;
492 goto error_free_event
;
495 /* Copy event name */
496 strncpy(local_ust_event
->attr
.name
, ev
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
497 local_ust_event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
499 switch (ev
->loglevel_type
) {
500 case LTTNG_EVENT_LOGLEVEL_ALL
:
501 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
502 local_ust_event
->attr
.loglevel
= -1; /* Force to -1 */
504 case LTTNG_EVENT_LOGLEVEL_RANGE
:
505 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
506 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
508 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
509 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
510 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
513 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
514 ret
= LTTNG_ERR_INVALID
;
515 goto error_free_event
;
519 local_ust_event
->filter_expression
= filter_expression
;
520 local_ust_event
->filter
= filter
;
521 local_ust_event
->exclusion
= exclusion
;
524 lttng_ht_node_init_str(&local_ust_event
->node
, local_ust_event
->attr
.name
);
526 DBG2("Trace UST event %s, loglevel (%d,%d) created",
527 local_ust_event
->attr
.name
,
528 local_ust_event
->attr
.loglevel_type
,
529 local_ust_event
->attr
.loglevel
);
531 *ust_event
= local_ust_event
;
536 free(local_ust_event
);
538 free(filter_expression
);
544 static int trace_ust_context_type_event_to_ust(enum lttng_event_context_type type
)
549 case LTTNG_EVENT_CONTEXT_VTID
:
550 utype
= LTTNG_UST_ABI_CONTEXT_VTID
;
552 case LTTNG_EVENT_CONTEXT_VPID
:
553 utype
= LTTNG_UST_ABI_CONTEXT_VPID
;
555 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
556 utype
= LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
;
558 case LTTNG_EVENT_CONTEXT_PROCNAME
:
559 utype
= LTTNG_UST_ABI_CONTEXT_PROCNAME
;
561 case LTTNG_EVENT_CONTEXT_IP
:
562 utype
= LTTNG_UST_ABI_CONTEXT_IP
;
564 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
565 if (!lttng_ust_ctl_has_perf_counters()) {
567 WARN("Perf counters not implemented in UST");
569 utype
= LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
;
572 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
573 utype
= LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
;
575 case LTTNG_EVENT_CONTEXT_CGROUP_NS
:
576 utype
= LTTNG_UST_ABI_CONTEXT_CGROUP_NS
;
578 case LTTNG_EVENT_CONTEXT_IPC_NS
:
579 utype
= LTTNG_UST_ABI_CONTEXT_IPC_NS
;
581 case LTTNG_EVENT_CONTEXT_MNT_NS
:
582 utype
= LTTNG_UST_ABI_CONTEXT_MNT_NS
;
584 case LTTNG_EVENT_CONTEXT_NET_NS
:
585 utype
= LTTNG_UST_ABI_CONTEXT_NET_NS
;
587 case LTTNG_EVENT_CONTEXT_PID_NS
:
588 utype
= LTTNG_UST_ABI_CONTEXT_PID_NS
;
590 case LTTNG_EVENT_CONTEXT_TIME_NS
:
591 utype
= LTTNG_UST_ABI_CONTEXT_TIME_NS
;
593 case LTTNG_EVENT_CONTEXT_USER_NS
:
594 utype
= LTTNG_UST_ABI_CONTEXT_USER_NS
;
596 case LTTNG_EVENT_CONTEXT_UTS_NS
:
597 utype
= LTTNG_UST_ABI_CONTEXT_UTS_NS
;
599 case LTTNG_EVENT_CONTEXT_VUID
:
600 utype
= LTTNG_UST_ABI_CONTEXT_VUID
;
602 case LTTNG_EVENT_CONTEXT_VEUID
:
603 utype
= LTTNG_UST_ABI_CONTEXT_VEUID
;
605 case LTTNG_EVENT_CONTEXT_VSUID
:
606 utype
= LTTNG_UST_ABI_CONTEXT_VSUID
;
608 case LTTNG_EVENT_CONTEXT_VGID
:
609 utype
= LTTNG_UST_ABI_CONTEXT_VGID
;
611 case LTTNG_EVENT_CONTEXT_VEGID
:
612 utype
= LTTNG_UST_ABI_CONTEXT_VEGID
;
614 case LTTNG_EVENT_CONTEXT_VSGID
:
615 utype
= LTTNG_UST_ABI_CONTEXT_VSGID
;
625 * Return 1 if contexts match, 0 otherwise.
627 int trace_ust_match_context(const struct ltt_ust_context
*uctx
,
628 const struct lttng_event_context
*ctx
)
632 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
636 if (uctx
->ctx
.ctx
!= utype
) {
640 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
641 if (uctx
->ctx
.u
.perf_counter
.type
!= ctx
->u
.perf_counter
.type
) {
644 if (uctx
->ctx
.u
.perf_counter
.config
!= ctx
->u
.perf_counter
.config
) {
647 if (strncmp(uctx
->ctx
.u
.perf_counter
.name
,
648 ctx
->u
.perf_counter
.name
,
649 LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
653 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
654 LTTNG_ASSERT(uctx
->ctx
.u
.app_ctx
.provider_name
);
655 LTTNG_ASSERT(uctx
->ctx
.u
.app_ctx
.ctx_name
);
656 if (strcmp(uctx
->ctx
.u
.app_ctx
.provider_name
, ctx
->u
.app_ctx
.provider_name
) != 0 ||
657 strcmp(uctx
->ctx
.u
.app_ctx
.ctx_name
, ctx
->u
.app_ctx
.ctx_name
) != 0) {
667 * Allocate and initialize an UST context.
669 * Return pointer to structure or NULL.
671 struct ltt_ust_context
*trace_ust_create_context(const struct lttng_event_context
*ctx
)
673 struct ltt_ust_context
*uctx
= nullptr;
678 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
680 ERR("Invalid UST context");
684 uctx
= zmalloc
<ltt_ust_context
>();
686 PERROR("zmalloc ltt_ust_context");
690 uctx
->ctx
.ctx
= (enum lttng_ust_abi_context_type
) utype
;
692 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
693 uctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
694 uctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
695 strncpy(uctx
->ctx
.u
.perf_counter
.name
,
696 ctx
->u
.perf_counter
.name
,
697 LTTNG_UST_ABI_SYM_NAME_LEN
);
698 uctx
->ctx
.u
.perf_counter
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
700 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
702 char *provider_name
= nullptr, *ctx_name
= nullptr;
704 provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
705 if (!provider_name
) {
708 uctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
710 ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
714 uctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
720 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
724 trace_ust_destroy_context(uctx
);
728 static void destroy_id_tracker_node_rcu(struct rcu_head
*head
)
730 struct ust_id_tracker_node
*tracker_node
=
731 caa_container_of(head
, struct ust_id_tracker_node
, node
.head
);
735 static void destroy_id_tracker_node(struct ust_id_tracker_node
*tracker_node
)
737 call_rcu(&tracker_node
->node
.head
, destroy_id_tracker_node_rcu
);
740 static int init_id_tracker(struct ust_id_tracker
*id_tracker
)
744 id_tracker
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
745 if (!id_tracker
->ht
) {
746 ret
= LTTNG_ERR_NOMEM
;
755 * Teardown id tracker content, but don't free id_tracker object.
757 static void fini_id_tracker(struct ust_id_tracker
*id_tracker
)
759 struct ust_id_tracker_node
*tracker_node
;
760 struct lttng_ht_iter iter
;
762 if (!id_tracker
->ht
) {
767 lttng::urcu::read_lock_guard read_lock
;
769 cds_lfht_for_each_entry (id_tracker
->ht
->ht
, &iter
.iter
, tracker_node
, node
.node
) {
770 int ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
773 destroy_id_tracker_node(tracker_node
);
777 lttng_ht_destroy(id_tracker
->ht
);
778 id_tracker
->ht
= nullptr;
781 static struct ust_id_tracker_node
*
782 id_tracker_lookup(struct ust_id_tracker
*id_tracker
, int id
, struct lttng_ht_iter
*iter
)
784 unsigned long _id
= (unsigned long) id
;
785 struct lttng_ht_node_ulong
*node
;
787 lttng_ht_lookup(id_tracker
->ht
, (void *) _id
, iter
);
788 node
= lttng_ht_iter_get_node_ulong(iter
);
790 return lttng::utils::container_of(node
, &ust_id_tracker_node::node
);
796 static int id_tracker_add_id(struct ust_id_tracker
*id_tracker
, int id
)
798 int retval
= LTTNG_OK
;
799 struct ust_id_tracker_node
*tracker_node
;
800 struct lttng_ht_iter iter
;
803 retval
= LTTNG_ERR_INVALID
;
806 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
808 /* Already exists. */
809 retval
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
812 tracker_node
= zmalloc
<ust_id_tracker_node
>();
814 retval
= LTTNG_ERR_NOMEM
;
817 lttng_ht_node_init_ulong(&tracker_node
->node
, (unsigned long) id
);
818 lttng_ht_add_unique_ulong(id_tracker
->ht
, &tracker_node
->node
);
823 static int id_tracker_del_id(struct ust_id_tracker
*id_tracker
, int id
)
825 int retval
= LTTNG_OK
, ret
;
826 struct ust_id_tracker_node
*tracker_node
;
827 struct lttng_ht_iter iter
;
830 retval
= LTTNG_ERR_INVALID
;
833 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
836 retval
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
839 ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
842 destroy_id_tracker_node(tracker_node
);
847 static struct ust_id_tracker
*get_id_tracker(struct ltt_ust_session
*session
,
848 enum lttng_process_attr process_attr
)
850 switch (process_attr
) {
851 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
852 return &session
->vpid_tracker
;
853 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
854 return &session
->vuid_tracker
;
855 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
856 return &session
->vgid_tracker
;
862 static struct process_attr_tracker
*
863 _trace_ust_get_process_attr_tracker(struct ltt_ust_session
*session
,
864 enum lttng_process_attr process_attr
)
866 switch (process_attr
) {
867 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
868 return session
->tracker_vpid
;
869 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
870 return session
->tracker_vuid
;
871 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
872 return session
->tracker_vgid
;
878 const struct process_attr_tracker
*
879 trace_ust_get_process_attr_tracker(struct ltt_ust_session
*session
,
880 enum lttng_process_attr process_attr
)
882 return (const struct process_attr_tracker
*) _trace_ust_get_process_attr_tracker(
883 session
, process_attr
);
887 * The session lock is held when calling this function.
889 int trace_ust_id_tracker_lookup(enum lttng_process_attr process_attr
,
890 struct ltt_ust_session
*session
,
893 struct lttng_ht_iter iter
;
894 struct ust_id_tracker
*id_tracker
;
896 id_tracker
= get_id_tracker(session
, process_attr
);
900 if (!id_tracker
->ht
) {
903 if (id_tracker_lookup(id_tracker
, id
, &iter
)) {
910 * Called with the session lock held.
912 enum lttng_error_code
913 trace_ust_process_attr_tracker_set_tracking_policy(struct ltt_ust_session
*session
,
914 enum lttng_process_attr process_attr
,
915 enum lttng_tracking_policy policy
)
918 enum lttng_error_code ret_code
= LTTNG_OK
;
919 struct ust_id_tracker
*id_tracker
= get_id_tracker(session
, process_attr
);
920 struct process_attr_tracker
*tracker
=
921 _trace_ust_get_process_attr_tracker(session
, process_attr
);
922 bool should_update_apps
= false;
923 enum lttng_tracking_policy previous_policy
;
926 ret_code
= LTTNG_ERR_INVALID
;
930 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
931 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
933 ret_code
= LTTNG_ERR_UNK
;
937 if (previous_policy
== policy
) {
942 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
943 /* Track all values: destroy tracker if exists. */
944 if (id_tracker
->ht
) {
945 fini_id_tracker(id_tracker
);
946 /* Ensure all apps have session. */
947 should_update_apps
= true;
950 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
951 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
953 fini_id_tracker(id_tracker
);
954 ret_code
= (lttng_error_code
) init_id_tracker(id_tracker
);
955 if (ret_code
!= LTTNG_OK
) {
956 ERR("Error initializing ID tracker");
959 /* Remove all apps from session. */
960 should_update_apps
= true;
965 if (should_update_apps
&& session
->active
) {
966 ust_app_global_update_all(session
);
972 /* Called with the session lock held. */
973 enum lttng_error_code
974 trace_ust_process_attr_tracker_inclusion_set_add_value(struct ltt_ust_session
*session
,
975 enum lttng_process_attr process_attr
,
976 const struct process_attr_value
*value
)
978 enum lttng_error_code ret_code
= LTTNG_OK
;
979 bool should_update_apps
= false;
980 struct ust_id_tracker
*id_tracker
= get_id_tracker(session
, process_attr
);
981 struct process_attr_tracker
*tracker
;
983 enum process_attr_tracker_status status
;
987 * Convert process attribute tracker value to the integral
988 * representation required by the kern-ctl API.
990 switch (process_attr
) {
991 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
992 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
993 integral_value
= (int) value
->value
.pid
;
995 case LTTNG_PROCESS_ATTR_USER_ID
:
996 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
997 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1000 ret_code
= utils_user_id_from_name(value
->value
.user_name
, &uid
);
1001 if (ret_code
!= LTTNG_OK
) {
1004 integral_value
= (int) uid
;
1006 integral_value
= (int) value
->value
.uid
;
1009 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1010 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1011 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1014 ret_code
= utils_group_id_from_name(value
->value
.group_name
, &gid
);
1015 if (ret_code
!= LTTNG_OK
) {
1018 integral_value
= (int) gid
;
1020 integral_value
= (int) value
->value
.gid
;
1024 ret_code
= LTTNG_ERR_INVALID
;
1028 tracker
= _trace_ust_get_process_attr_tracker(session
, process_attr
);
1030 ret_code
= LTTNG_ERR_INVALID
;
1034 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
1035 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1037 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
1038 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1040 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1041 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1043 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1045 ret_code
= LTTNG_ERR_UNK
;
1051 DBG("User space track %s %d for session id %" PRIu64
,
1052 lttng_process_attr_to_string(process_attr
),
1056 ret_code
= (lttng_error_code
) id_tracker_add_id(id_tracker
, integral_value
);
1057 if (ret_code
!= LTTNG_OK
) {
1060 /* Add session to application */
1061 switch (process_attr
) {
1062 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1063 app
= ust_app_find_by_pid(integral_value
);
1065 should_update_apps
= true;
1069 should_update_apps
= true;
1072 if (should_update_apps
&& session
->active
) {
1073 ust_app_global_update_all(session
);
1079 /* Called with the session lock held. */
1080 enum lttng_error_code
1081 trace_ust_process_attr_tracker_inclusion_set_remove_value(struct ltt_ust_session
*session
,
1082 enum lttng_process_attr process_attr
,
1083 const struct process_attr_value
*value
)
1085 enum lttng_error_code ret_code
= LTTNG_OK
;
1086 bool should_update_apps
= false;
1087 struct ust_id_tracker
*id_tracker
= get_id_tracker(session
, process_attr
);
1088 struct process_attr_tracker
*tracker
;
1090 enum process_attr_tracker_status status
;
1091 struct ust_app
*app
;
1094 * Convert process attribute tracker value to the integral
1095 * representation required by the kern-ctl API.
1097 switch (process_attr
) {
1098 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1099 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1100 integral_value
= (int) value
->value
.pid
;
1102 case LTTNG_PROCESS_ATTR_USER_ID
:
1103 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1104 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1107 ret_code
= utils_user_id_from_name(value
->value
.user_name
, &uid
);
1108 if (ret_code
!= LTTNG_OK
) {
1111 integral_value
= (int) uid
;
1113 integral_value
= (int) value
->value
.uid
;
1116 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1117 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1118 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1121 ret_code
= utils_group_id_from_name(value
->value
.group_name
, &gid
);
1122 if (ret_code
!= LTTNG_OK
) {
1125 integral_value
= (int) gid
;
1127 integral_value
= (int) value
->value
.gid
;
1131 ret_code
= LTTNG_ERR_INVALID
;
1135 tracker
= _trace_ust_get_process_attr_tracker(session
, process_attr
);
1137 ret_code
= LTTNG_ERR_INVALID
;
1141 status
= process_attr_tracker_inclusion_set_remove_value(tracker
, value
);
1142 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1144 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1145 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1147 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1148 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1150 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1152 ret_code
= LTTNG_ERR_UNK
;
1158 DBG("User space untrack %s %d for session id %" PRIu64
,
1159 lttng_process_attr_to_string(process_attr
),
1163 ret_code
= (lttng_error_code
) id_tracker_del_id(id_tracker
, integral_value
);
1164 if (ret_code
!= LTTNG_OK
) {
1167 /* Add session to application */
1168 switch (process_attr
) {
1169 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1170 app
= ust_app_find_by_pid(integral_value
);
1172 should_update_apps
= true;
1176 should_update_apps
= true;
1179 if (should_update_apps
&& session
->active
) {
1180 ust_app_global_update_all(session
);
1187 * RCU safe free context structure.
1189 static void destroy_context_rcu(struct rcu_head
*head
)
1191 struct lttng_ht_node_ulong
*node
=
1192 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
1193 struct ltt_ust_context
*ctx
= lttng::utils::container_of(node
, <t_ust_context::node
);
1195 trace_ust_destroy_context(ctx
);
1199 * Cleanup UST context hash table.
1201 static void destroy_contexts(struct lttng_ht
*ht
)
1204 struct lttng_ht_node_ulong
*node
;
1205 struct lttng_ht_iter iter
;
1206 struct ltt_ust_context
*ctx
;
1211 lttng::urcu::read_lock_guard read_lock
;
1213 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, node
, node
) {
1214 /* Remove from ordered list. */
1215 ctx
= lttng::utils::container_of(node
, <t_ust_context::node
);
1216 cds_list_del(&ctx
->list
);
1217 /* Remove from channel's hash table. */
1218 ret
= lttng_ht_del(ht
, &iter
);
1220 call_rcu(&node
->head
, destroy_context_rcu
);
1225 lttng_ht_destroy(ht
);
1229 * Cleanup ust event structure.
1231 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
1233 LTTNG_ASSERT(event
);
1235 DBG2("Trace destroy UST event %s", event
->attr
.name
);
1236 free(event
->filter_expression
);
1237 free(event
->filter
);
1238 free(event
->exclusion
);
1243 * Cleanup ust context structure.
1245 void trace_ust_destroy_context(struct ltt_ust_context
*ctx
)
1249 if (ctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1250 free(ctx
->ctx
.u
.app_ctx
.provider_name
);
1251 free(ctx
->ctx
.u
.app_ctx
.ctx_name
);
1257 * URCU intermediate call to complete destroy event.
1259 static void destroy_event_rcu(struct rcu_head
*head
)
1261 struct lttng_ht_node_str
*node
= lttng::utils::container_of(head
, <tng_ht_node_str::head
);
1262 struct ltt_ust_event
*event
= lttng::utils::container_of(node
, <t_ust_event::node
);
1264 trace_ust_destroy_event(event
);
1268 * Cleanup UST events hashtable.
1270 static void destroy_events(struct lttng_ht
*events
)
1273 struct lttng_ht_node_str
*node
;
1274 struct lttng_ht_iter iter
;
1276 LTTNG_ASSERT(events
);
1279 lttng::urcu::read_lock_guard read_lock
;
1281 cds_lfht_for_each_entry (events
->ht
, &iter
.iter
, node
, node
) {
1282 ret
= lttng_ht_del(events
, &iter
);
1284 call_rcu(&node
->head
, destroy_event_rcu
);
1288 lttng_ht_destroy(events
);
1292 * Cleanup ust channel structure.
1294 * Should _NOT_ be called with RCU read lock held.
1296 static void _trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1298 LTTNG_ASSERT(channel
);
1300 DBG2("Trace destroy UST channel %s", channel
->name
);
1306 * URCU intermediate call to complete destroy channel.
1308 static void destroy_channel_rcu(struct rcu_head
*head
)
1310 struct lttng_ht_node_str
*node
= lttng::utils::container_of(head
, <tng_ht_node_str::head
);
1311 struct ltt_ust_channel
*channel
= lttng::utils::container_of(node
, <t_ust_channel::node
);
1313 _trace_ust_destroy_channel(channel
);
1316 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1318 /* Destroying all events of the channel */
1319 destroy_events(channel
->events
);
1320 /* Destroying all context of the channel */
1321 destroy_contexts(channel
->ctx
);
1323 call_rcu(&channel
->node
.head
, destroy_channel_rcu
);
1327 * Remove an UST channel from a channel HT.
1329 void trace_ust_delete_channel(struct lttng_ht
*ht
, struct ltt_ust_channel
*channel
)
1332 struct lttng_ht_iter iter
;
1335 LTTNG_ASSERT(channel
);
1337 iter
.iter
.node
= &channel
->node
.node
;
1338 ret
= lttng_ht_del(ht
, &iter
);
1342 int trace_ust_regenerate_metadata(struct ltt_ust_session
*usess
)
1345 struct buffer_reg_uid
*uid_reg
= nullptr;
1346 struct buffer_reg_session
*session_reg
= nullptr;
1348 lttng::urcu::read_lock_guard read_lock
;
1349 cds_list_for_each_entry (uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
1350 lsu::registry_session
*registry
;
1352 session_reg
= uid_reg
->registry
;
1353 registry
= session_reg
->reg
.ust
;
1356 registry
->regenerate_metadata();
1357 } catch (const std::exception
& ex
) {
1358 ERR("Failed to regenerate user space session metadata: %s", ex
.what());
1369 * Iterate over a hash table containing channels and cleanup safely.
1371 static void destroy_channels(struct lttng_ht
*channels
)
1373 struct lttng_ht_node_str
*node
;
1374 struct lttng_ht_iter iter
;
1376 LTTNG_ASSERT(channels
);
1379 lttng::urcu::read_lock_guard read_lock
;
1381 cds_lfht_for_each_entry (channels
->ht
, &iter
.iter
, node
, node
) {
1382 struct ltt_ust_channel
*chan
=
1383 lttng::utils::container_of(node
, <t_ust_channel::node
);
1385 trace_ust_delete_channel(channels
, chan
);
1386 trace_ust_destroy_channel(chan
);
1390 lttng_ht_destroy(channels
);
1394 * Cleanup UST global domain.
1396 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
1400 destroy_channels(dom
->channels
);
1404 * Cleanup ust session structure, keeping data required by
1407 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
1410 struct buffer_reg_uid
*reg
, *sreg
;
1411 struct lttng_ht_iter iter
;
1413 LTTNG_ASSERT(session
);
1415 DBG2("Trace UST destroy session %" PRIu64
, session
->id
);
1417 /* Cleaning up UST domain */
1418 destroy_domain_global(&session
->domain_global
);
1421 lttng::urcu::read_lock_guard read_lock
;
1423 cds_lfht_for_each_entry (session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
1424 int ret
= lttng_ht_del(session
->agents
, &iter
);
1431 lttng_ht_destroy(session
->agents
);
1433 /* Cleanup UID buffer registry object(s). */
1434 cds_list_for_each_entry_safe (reg
, sreg
, &session
->buffer_reg_uid_list
, lnode
) {
1435 cds_list_del(®
->lnode
);
1436 buffer_reg_uid_remove(reg
);
1437 buffer_reg_uid_destroy(reg
, session
->consumer
);
1440 process_attr_tracker_destroy(session
->tracker_vpid
);
1441 process_attr_tracker_destroy(session
->tracker_vuid
);
1442 process_attr_tracker_destroy(session
->tracker_vgid
);
1444 fini_id_tracker(&session
->vpid_tracker
);
1445 fini_id_tracker(&session
->vuid_tracker
);
1446 fini_id_tracker(&session
->vgid_tracker
);
1447 lttng_trace_chunk_put(session
->current_trace_chunk
);
1450 /* Free elements needed by destroy notifiers. */
1451 void trace_ust_free_session(struct ltt_ust_session
*session
)
1453 consumer_output_put(session
->consumer
);