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
16 #include <common/common.hpp>
17 #include <common/defaults.hpp>
18 #include <common/trace-chunk.hpp>
19 #include <common/utils.hpp>
21 #include "buffer-registry.hpp"
22 #include "trace-ust.hpp"
24 #include "ust-app.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
,
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
,
87 ev_loglevel_value
, key
->loglevel_type
,
88 key
->loglevel_value
, LTTNG_UST_ABI_LOGLEVEL_ALL
);
94 /* Only one of the filters is NULL, fail. */
95 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
99 if (key
->filter
&& event
->filter
) {
100 /* Both filters exists, check length followed by the bytecode. */
101 if (event
->filter
->len
!= key
->filter
->len
||
102 memcmp(event
->filter
->data
, key
->filter
->data
,
103 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
=
126 LTTNG_EVENT_EXCLUSION_NAME_AT(
127 event
->exclusion
, i
);
130 * Compare this exclusion name to all the key's
133 for (j
= 0; j
< key
->exclusion
->count
; ++j
) {
134 const char *name_key
=
135 LTTNG_EVENT_EXCLUSION_NAME_AT(
138 if (!strncmp(name_ev
, name_key
,
139 LTTNG_SYMBOL_NAME_LEN
)) {
147 * If the current exclusion name was not found amongst
148 * the key's exclusion names, then there's no match.
163 * Find the channel in the hashtable and return channel pointer. RCU read side
164 * lock MUST be acquired before calling this.
166 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct lttng_ht
*ht
,
169 struct lttng_ht_node_str
*node
;
170 struct lttng_ht_iter iter
;
172 ASSERT_RCU_READ_LOCKED();
174 * If we receive an empty string for channel name, it means the
175 * default channel name is requested.
178 name
= DEFAULT_CHANNEL_NAME
;
180 lttng_ht_lookup(ht
, (void *)name
, &iter
);
181 node
= lttng_ht_iter_get_node_str(&iter
);
186 DBG2("Trace UST channel %s found by name", name
);
188 return lttng::utils::container_of(node
, <t_ust_channel::node
);
191 DBG2("Trace UST channel %s not found by name", name
);
196 * Find the event in the hashtable and return event pointer. RCU read side lock
197 * MUST be acquired before calling this.
199 struct ltt_ust_event
*trace_ust_find_event(struct lttng_ht
*ht
,
200 char *name
, struct lttng_bytecode
*filter
,
201 enum lttng_ust_abi_loglevel_type loglevel_type
, int loglevel_value
,
202 struct lttng_event_exclusion
*exclusion
)
204 struct lttng_ht_node_str
*node
;
205 struct lttng_ht_iter iter
;
206 struct ltt_ust_ht_key key
;
210 ASSERT_RCU_READ_LOCKED();
214 key
.loglevel_type
= loglevel_type
;
215 key
.loglevel_value
= loglevel_value
;
216 key
.exclusion
= exclusion
;
218 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
219 trace_ust_ht_match_event
, &key
, &iter
.iter
);
220 node
= lttng_ht_iter_get_node_str(&iter
);
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
= NULL
;
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
>();
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
== NULL
) {
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
>();
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
= ((struct lttng_channel_extended
*)
371 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
372 luc
->attr
.u
.s
.blocking_timeout
= ((struct lttng_channel_extended
*)
373 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
) {
427 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
429 for (j
= 0; j
< i
; ++j
) {
431 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, j
);
433 if (!strncmp(name_a
, name_b
, LTTNG_SYMBOL_NAME_LEN
)) {
446 * Allocate and initialize a ust event. Set name and event type.
447 * We own filter_expression, filter, and exclusion.
449 * Return an lttng_error_code
451 enum lttng_error_code
trace_ust_create_event(struct lttng_event
*ev
,
452 char *filter_expression
,
453 struct lttng_bytecode
*filter
,
454 struct lttng_event_exclusion
*exclusion
,
456 struct ltt_ust_event
**ust_event
)
458 struct ltt_ust_event
*local_ust_event
;
459 enum lttng_error_code ret
= LTTNG_OK
;
463 if (exclusion
&& validate_exclusion(exclusion
)) {
464 ret
= LTTNG_ERR_INVALID
;
468 local_ust_event
= zmalloc
<ltt_ust_event
>();
469 if (local_ust_event
== NULL
) {
470 PERROR("ust event zmalloc");
471 ret
= LTTNG_ERR_NOMEM
;
475 local_ust_event
->internal
= internal_event
;
478 case LTTNG_EVENT_PROBE
:
479 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_PROBE
;
481 case LTTNG_EVENT_FUNCTION
:
482 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_FUNCTION
;
484 case LTTNG_EVENT_FUNCTION_ENTRY
:
485 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_FUNCTION
;
487 case LTTNG_EVENT_TRACEPOINT
:
488 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
491 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
492 ret
= LTTNG_ERR_INVALID
;
493 goto error_free_event
;
496 /* Copy event name */
497 strncpy(local_ust_event
->attr
.name
, ev
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
498 local_ust_event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
500 switch (ev
->loglevel_type
) {
501 case LTTNG_EVENT_LOGLEVEL_ALL
:
502 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
503 local_ust_event
->attr
.loglevel
= -1; /* Force to -1 */
505 case LTTNG_EVENT_LOGLEVEL_RANGE
:
506 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
507 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
509 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
510 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
511 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
514 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
515 ret
= LTTNG_ERR_INVALID
;
516 goto error_free_event
;
520 local_ust_event
->filter_expression
= filter_expression
;
521 local_ust_event
->filter
= filter
;
522 local_ust_event
->exclusion
= exclusion
;
525 lttng_ht_node_init_str(&local_ust_event
->node
, local_ust_event
->attr
.name
);
527 DBG2("Trace UST event %s, loglevel (%d,%d) created",
528 local_ust_event
->attr
.name
, 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
);
545 int trace_ust_context_type_event_to_ust(
546 enum lttng_event_context_type type
)
551 case LTTNG_EVENT_CONTEXT_VTID
:
552 utype
= LTTNG_UST_ABI_CONTEXT_VTID
;
554 case LTTNG_EVENT_CONTEXT_VPID
:
555 utype
= LTTNG_UST_ABI_CONTEXT_VPID
;
557 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
558 utype
= LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
;
560 case LTTNG_EVENT_CONTEXT_PROCNAME
:
561 utype
= LTTNG_UST_ABI_CONTEXT_PROCNAME
;
563 case LTTNG_EVENT_CONTEXT_IP
:
564 utype
= LTTNG_UST_ABI_CONTEXT_IP
;
566 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
567 if (!lttng_ust_ctl_has_perf_counters()) {
569 WARN("Perf counters not implemented in UST");
571 utype
= LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
;
574 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
575 utype
= LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
;
577 case LTTNG_EVENT_CONTEXT_CGROUP_NS
:
578 utype
= LTTNG_UST_ABI_CONTEXT_CGROUP_NS
;
580 case LTTNG_EVENT_CONTEXT_IPC_NS
:
581 utype
= LTTNG_UST_ABI_CONTEXT_IPC_NS
;
583 case LTTNG_EVENT_CONTEXT_MNT_NS
:
584 utype
= LTTNG_UST_ABI_CONTEXT_MNT_NS
;
586 case LTTNG_EVENT_CONTEXT_NET_NS
:
587 utype
= LTTNG_UST_ABI_CONTEXT_NET_NS
;
589 case LTTNG_EVENT_CONTEXT_PID_NS
:
590 utype
= LTTNG_UST_ABI_CONTEXT_PID_NS
;
592 case LTTNG_EVENT_CONTEXT_TIME_NS
:
593 utype
= LTTNG_UST_ABI_CONTEXT_TIME_NS
;
595 case LTTNG_EVENT_CONTEXT_USER_NS
:
596 utype
= LTTNG_UST_ABI_CONTEXT_USER_NS
;
598 case LTTNG_EVENT_CONTEXT_UTS_NS
:
599 utype
= LTTNG_UST_ABI_CONTEXT_UTS_NS
;
601 case LTTNG_EVENT_CONTEXT_VUID
:
602 utype
= LTTNG_UST_ABI_CONTEXT_VUID
;
604 case LTTNG_EVENT_CONTEXT_VEUID
:
605 utype
= LTTNG_UST_ABI_CONTEXT_VEUID
;
607 case LTTNG_EVENT_CONTEXT_VSUID
:
608 utype
= LTTNG_UST_ABI_CONTEXT_VSUID
;
610 case LTTNG_EVENT_CONTEXT_VGID
:
611 utype
= LTTNG_UST_ABI_CONTEXT_VGID
;
613 case LTTNG_EVENT_CONTEXT_VEGID
:
614 utype
= LTTNG_UST_ABI_CONTEXT_VEGID
;
616 case LTTNG_EVENT_CONTEXT_VSGID
:
617 utype
= LTTNG_UST_ABI_CONTEXT_VSGID
;
627 * Return 1 if contexts match, 0 otherwise.
629 int trace_ust_match_context(const struct ltt_ust_context
*uctx
,
630 const struct lttng_event_context
*ctx
)
634 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
638 if (uctx
->ctx
.ctx
!= utype
) {
642 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
643 if (uctx
->ctx
.u
.perf_counter
.type
644 != ctx
->u
.perf_counter
.type
) {
647 if (uctx
->ctx
.u
.perf_counter
.config
648 != ctx
->u
.perf_counter
.config
) {
651 if (strncmp(uctx
->ctx
.u
.perf_counter
.name
,
652 ctx
->u
.perf_counter
.name
,
653 LTTNG_UST_ABI_SYM_NAME_LEN
)) {
657 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
658 LTTNG_ASSERT(uctx
->ctx
.u
.app_ctx
.provider_name
);
659 LTTNG_ASSERT(uctx
->ctx
.u
.app_ctx
.ctx_name
);
660 if (strcmp(uctx
->ctx
.u
.app_ctx
.provider_name
,
661 ctx
->u
.app_ctx
.provider_name
) ||
662 strcmp(uctx
->ctx
.u
.app_ctx
.ctx_name
,
663 ctx
->u
.app_ctx
.ctx_name
)) {
674 * Allocate and initialize an UST context.
676 * Return pointer to structure or NULL.
678 struct ltt_ust_context
*trace_ust_create_context(
679 const struct lttng_event_context
*ctx
)
681 struct ltt_ust_context
*uctx
= NULL
;
686 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
688 ERR("Invalid UST context");
692 uctx
= zmalloc
<ltt_ust_context
>();
694 PERROR("zmalloc ltt_ust_context");
698 uctx
->ctx
.ctx
= (enum lttng_ust_abi_context_type
) utype
;
700 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
701 uctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
702 uctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
703 strncpy(uctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
704 LTTNG_UST_ABI_SYM_NAME_LEN
);
705 uctx
->ctx
.u
.perf_counter
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
707 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
709 char *provider_name
= NULL
, *ctx_name
= NULL
;
711 provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
712 if (!provider_name
) {
715 uctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
717 ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
721 uctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
727 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
731 trace_ust_destroy_context(uctx
);
735 static void destroy_id_tracker_node_rcu(struct rcu_head
*head
)
737 struct ust_id_tracker_node
*tracker_node
= caa_container_of(
738 head
, struct ust_id_tracker_node
, node
.head
);
742 static void destroy_id_tracker_node(struct ust_id_tracker_node
*tracker_node
)
744 call_rcu(&tracker_node
->node
.head
, destroy_id_tracker_node_rcu
);
747 static int init_id_tracker(struct ust_id_tracker
*id_tracker
)
751 id_tracker
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
752 if (!id_tracker
->ht
) {
753 ret
= LTTNG_ERR_NOMEM
;
762 * Teardown id tracker content, but don't free id_tracker object.
764 static void fini_id_tracker(struct ust_id_tracker
*id_tracker
)
766 struct ust_id_tracker_node
*tracker_node
;
767 struct lttng_ht_iter iter
;
769 if (!id_tracker
->ht
) {
773 cds_lfht_for_each_entry (id_tracker
->ht
->ht
, &iter
.iter
, tracker_node
,
775 int ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
778 destroy_id_tracker_node(tracker_node
);
781 lttng_ht_destroy(id_tracker
->ht
);
782 id_tracker
->ht
= NULL
;
785 static struct ust_id_tracker_node
*id_tracker_lookup(
786 struct ust_id_tracker
*id_tracker
,
788 struct lttng_ht_iter
*iter
)
790 unsigned long _id
= (unsigned long) id
;
791 struct lttng_ht_node_ulong
*node
;
793 lttng_ht_lookup(id_tracker
->ht
, (void *) _id
, iter
);
794 node
= lttng_ht_iter_get_node_ulong(iter
);
796 return lttng::utils::container_of(node
, &ust_id_tracker_node::node
);
802 static int id_tracker_add_id(struct ust_id_tracker
*id_tracker
, int id
)
804 int retval
= LTTNG_OK
;
805 struct ust_id_tracker_node
*tracker_node
;
806 struct lttng_ht_iter iter
;
809 retval
= LTTNG_ERR_INVALID
;
812 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
814 /* Already exists. */
815 retval
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
818 tracker_node
= zmalloc
<ust_id_tracker_node
>();
820 retval
= LTTNG_ERR_NOMEM
;
823 lttng_ht_node_init_ulong(&tracker_node
->node
, (unsigned long) id
);
824 lttng_ht_add_unique_ulong(id_tracker
->ht
, &tracker_node
->node
);
829 static int id_tracker_del_id(struct ust_id_tracker
*id_tracker
, int id
)
831 int retval
= LTTNG_OK
, ret
;
832 struct ust_id_tracker_node
*tracker_node
;
833 struct lttng_ht_iter iter
;
836 retval
= LTTNG_ERR_INVALID
;
839 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
842 retval
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
845 ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
848 destroy_id_tracker_node(tracker_node
);
853 static struct ust_id_tracker
*get_id_tracker(struct ltt_ust_session
*session
,
854 enum lttng_process_attr process_attr
)
856 switch (process_attr
) {
857 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
858 return &session
->vpid_tracker
;
859 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
860 return &session
->vuid_tracker
;
861 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
862 return &session
->vgid_tracker
;
868 static struct process_attr_tracker
*_trace_ust_get_process_attr_tracker(
869 struct ltt_ust_session
*session
,
870 enum lttng_process_attr process_attr
)
872 switch (process_attr
) {
873 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
874 return session
->tracker_vpid
;
875 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
876 return session
->tracker_vuid
;
877 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
878 return session
->tracker_vgid
;
884 const struct process_attr_tracker
*trace_ust_get_process_attr_tracker(
885 struct ltt_ust_session
*session
,
886 enum lttng_process_attr process_attr
)
888 return (const struct process_attr_tracker
*)
889 _trace_ust_get_process_attr_tracker(
890 session
, process_attr
);
894 * The session lock is held when calling this function.
896 int trace_ust_id_tracker_lookup(enum lttng_process_attr process_attr
,
897 struct ltt_ust_session
*session
,
900 struct lttng_ht_iter iter
;
901 struct ust_id_tracker
*id_tracker
;
903 id_tracker
= get_id_tracker(session
, process_attr
);
907 if (!id_tracker
->ht
) {
910 if (id_tracker_lookup(id_tracker
, id
, &iter
)) {
917 * Called with the session lock held.
919 enum lttng_error_code
trace_ust_process_attr_tracker_set_tracking_policy(
920 struct ltt_ust_session
*session
,
921 enum lttng_process_attr process_attr
,
922 enum lttng_tracking_policy policy
)
925 enum lttng_error_code ret_code
= LTTNG_OK
;
926 struct ust_id_tracker
*id_tracker
=
927 get_id_tracker(session
, process_attr
);
928 struct process_attr_tracker
*tracker
=
929 _trace_ust_get_process_attr_tracker(
930 session
, process_attr
);
931 bool should_update_apps
= false;
932 enum lttng_tracking_policy previous_policy
;
935 ret_code
= LTTNG_ERR_INVALID
;
939 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
940 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
942 ret_code
= LTTNG_ERR_UNK
;
946 if (previous_policy
== policy
) {
951 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
952 /* Track all values: destroy tracker if exists. */
953 if (id_tracker
->ht
) {
954 fini_id_tracker(id_tracker
);
955 /* Ensure all apps have session. */
956 should_update_apps
= true;
959 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
960 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
962 fini_id_tracker(id_tracker
);
963 ret_code
= (lttng_error_code
) init_id_tracker(id_tracker
);
964 if (ret_code
!= LTTNG_OK
) {
965 ERR("Error initializing ID tracker");
968 /* Remove all apps from session. */
969 should_update_apps
= true;
974 if (should_update_apps
&& session
->active
) {
975 ust_app_global_update_all(session
);
981 /* Called with the session lock held. */
982 enum lttng_error_code
trace_ust_process_attr_tracker_inclusion_set_add_value(
983 struct ltt_ust_session
*session
,
984 enum lttng_process_attr process_attr
,
985 const struct process_attr_value
*value
)
987 enum lttng_error_code ret_code
= LTTNG_OK
;
988 bool should_update_apps
= false;
989 struct ust_id_tracker
*id_tracker
=
990 get_id_tracker(session
, process_attr
);
991 struct process_attr_tracker
*tracker
;
993 enum process_attr_tracker_status status
;
997 * Convert process attribute tracker value to the integral
998 * representation required by the kern-ctl API.
1000 switch (process_attr
) {
1001 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1002 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1003 integral_value
= (int) value
->value
.pid
;
1005 case LTTNG_PROCESS_ATTR_USER_ID
:
1006 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1007 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1010 ret_code
= utils_user_id_from_name(
1011 value
->value
.user_name
, &uid
);
1012 if (ret_code
!= LTTNG_OK
) {
1015 integral_value
= (int) uid
;
1017 integral_value
= (int) value
->value
.uid
;
1020 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1021 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1022 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1025 ret_code
= utils_group_id_from_name(
1026 value
->value
.group_name
, &gid
);
1027 if (ret_code
!= LTTNG_OK
) {
1030 integral_value
= (int) gid
;
1032 integral_value
= (int) value
->value
.gid
;
1036 ret_code
= LTTNG_ERR_INVALID
;
1040 tracker
= _trace_ust_get_process_attr_tracker(session
, process_attr
);
1042 ret_code
= LTTNG_ERR_INVALID
;
1046 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
1047 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1049 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
1050 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1052 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1053 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1055 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1057 ret_code
= LTTNG_ERR_UNK
;
1063 DBG("User space track %s %d for session id %" PRIu64
,
1064 lttng_process_attr_to_string(process_attr
),
1065 integral_value
, session
->id
);
1067 ret_code
= (lttng_error_code
) id_tracker_add_id(id_tracker
, integral_value
);
1068 if (ret_code
!= LTTNG_OK
) {
1071 /* Add session to application */
1072 switch (process_attr
) {
1073 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1074 app
= ust_app_find_by_pid(integral_value
);
1076 should_update_apps
= true;
1080 should_update_apps
= true;
1083 if (should_update_apps
&& session
->active
) {
1084 ust_app_global_update_all(session
);
1090 /* Called with the session lock held. */
1091 enum lttng_error_code
trace_ust_process_attr_tracker_inclusion_set_remove_value(
1092 struct ltt_ust_session
*session
,
1093 enum lttng_process_attr process_attr
,
1094 const struct process_attr_value
*value
)
1096 enum lttng_error_code ret_code
= LTTNG_OK
;
1097 bool should_update_apps
= false;
1098 struct ust_id_tracker
*id_tracker
=
1099 get_id_tracker(session
, process_attr
);
1100 struct process_attr_tracker
*tracker
;
1102 enum process_attr_tracker_status status
;
1103 struct ust_app
*app
;
1106 * Convert process attribute tracker value to the integral
1107 * representation required by the kern-ctl API.
1109 switch (process_attr
) {
1110 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1111 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1112 integral_value
= (int) value
->value
.pid
;
1114 case LTTNG_PROCESS_ATTR_USER_ID
:
1115 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1116 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1119 ret_code
= utils_user_id_from_name(
1120 value
->value
.user_name
, &uid
);
1121 if (ret_code
!= LTTNG_OK
) {
1124 integral_value
= (int) uid
;
1126 integral_value
= (int) value
->value
.uid
;
1129 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1130 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1131 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1134 ret_code
= utils_group_id_from_name(
1135 value
->value
.group_name
, &gid
);
1136 if (ret_code
!= LTTNG_OK
) {
1139 integral_value
= (int) gid
;
1141 integral_value
= (int) value
->value
.gid
;
1145 ret_code
= LTTNG_ERR_INVALID
;
1149 tracker
= _trace_ust_get_process_attr_tracker(session
, process_attr
);
1151 ret_code
= LTTNG_ERR_INVALID
;
1155 status
= process_attr_tracker_inclusion_set_remove_value(
1157 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1159 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1160 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1162 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1163 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1165 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1167 ret_code
= LTTNG_ERR_UNK
;
1173 DBG("User space untrack %s %d for session id %" PRIu64
,
1174 lttng_process_attr_to_string(process_attr
),
1175 integral_value
, session
->id
);
1177 ret_code
= (lttng_error_code
) id_tracker_del_id(id_tracker
, integral_value
);
1178 if (ret_code
!= LTTNG_OK
) {
1181 /* Add session to application */
1182 switch (process_attr
) {
1183 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1184 app
= ust_app_find_by_pid(integral_value
);
1186 should_update_apps
= true;
1190 should_update_apps
= true;
1193 if (should_update_apps
&& session
->active
) {
1194 ust_app_global_update_all(session
);
1201 * RCU safe free context structure.
1203 static void destroy_context_rcu(struct rcu_head
*head
)
1205 struct lttng_ht_node_ulong
*node
=
1206 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
1207 struct ltt_ust_context
*ctx
=
1208 lttng::utils::container_of(node
, <t_ust_context::node
);
1210 trace_ust_destroy_context(ctx
);
1214 * Cleanup UST context hash table.
1216 static void destroy_contexts(struct lttng_ht
*ht
)
1219 struct lttng_ht_node_ulong
*node
;
1220 struct lttng_ht_iter iter
;
1221 struct ltt_ust_context
*ctx
;
1226 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
1227 /* Remove from ordered list. */
1228 ctx
= lttng::utils::container_of(node
, <t_ust_context::node
);
1229 cds_list_del(&ctx
->list
);
1230 /* Remove from channel's hash table. */
1231 ret
= lttng_ht_del(ht
, &iter
);
1233 call_rcu(&node
->head
, destroy_context_rcu
);
1238 lttng_ht_destroy(ht
);
1242 * Cleanup ust event structure.
1244 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
1246 LTTNG_ASSERT(event
);
1248 DBG2("Trace destroy UST event %s", event
->attr
.name
);
1249 free(event
->filter_expression
);
1250 free(event
->filter
);
1251 free(event
->exclusion
);
1256 * Cleanup ust context structure.
1258 void trace_ust_destroy_context(struct ltt_ust_context
*ctx
)
1262 if (ctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1263 free(ctx
->ctx
.u
.app_ctx
.provider_name
);
1264 free(ctx
->ctx
.u
.app_ctx
.ctx_name
);
1270 * URCU intermediate call to complete destroy event.
1272 static void destroy_event_rcu(struct rcu_head
*head
)
1274 struct lttng_ht_node_str
*node
=
1275 lttng::utils::container_of(head
, <tng_ht_node_str::head
);
1276 struct ltt_ust_event
*event
=
1277 lttng::utils::container_of(node
, <t_ust_event::node
);
1279 trace_ust_destroy_event(event
);
1283 * Cleanup UST events hashtable.
1285 static void destroy_events(struct lttng_ht
*events
)
1288 struct lttng_ht_node_str
*node
;
1289 struct lttng_ht_iter iter
;
1291 LTTNG_ASSERT(events
);
1294 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
1295 ret
= lttng_ht_del(events
, &iter
);
1297 call_rcu(&node
->head
, destroy_event_rcu
);
1301 lttng_ht_destroy(events
);
1305 * Cleanup ust channel structure.
1307 * Should _NOT_ be called with RCU read lock held.
1309 static void _trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1311 LTTNG_ASSERT(channel
);
1313 DBG2("Trace destroy UST channel %s", channel
->name
);
1319 * URCU intermediate call to complete destroy channel.
1321 static void destroy_channel_rcu(struct rcu_head
*head
)
1323 struct lttng_ht_node_str
*node
=
1324 lttng::utils::container_of(head
, <tng_ht_node_str::head
);
1325 struct ltt_ust_channel
*channel
=
1326 lttng::utils::container_of(node
, <t_ust_channel::node
);
1328 _trace_ust_destroy_channel(channel
);
1331 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1333 /* Destroying all events of the channel */
1334 destroy_events(channel
->events
);
1335 /* Destroying all context of the channel */
1336 destroy_contexts(channel
->ctx
);
1338 call_rcu(&channel
->node
.head
, destroy_channel_rcu
);
1342 * Remove an UST channel from a channel HT.
1344 void trace_ust_delete_channel(struct lttng_ht
*ht
,
1345 struct ltt_ust_channel
*channel
)
1348 struct lttng_ht_iter iter
;
1351 LTTNG_ASSERT(channel
);
1353 iter
.iter
.node
= &channel
->node
.node
;
1354 ret
= lttng_ht_del(ht
, &iter
);
1358 int trace_ust_regenerate_metadata(struct ltt_ust_session
*usess
)
1361 struct buffer_reg_uid
*uid_reg
= NULL
;
1362 struct buffer_reg_session
*session_reg
= NULL
;
1365 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
1366 lsu::registry_session
*registry
;
1368 session_reg
= uid_reg
->registry
;
1369 registry
= session_reg
->reg
.ust
;
1372 registry
->regenerate_metadata();
1373 } catch (const std::exception
& ex
) {
1374 ERR("Failed to regenerate user space session metadata: %s", ex
.what());
1386 * Iterate over a hash table containing channels and cleanup safely.
1388 static void destroy_channels(struct lttng_ht
*channels
)
1390 struct lttng_ht_node_str
*node
;
1391 struct lttng_ht_iter iter
;
1393 LTTNG_ASSERT(channels
);
1396 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
1397 struct ltt_ust_channel
*chan
=
1398 lttng::utils::container_of(node
, <t_ust_channel::node
);
1400 trace_ust_delete_channel(channels
, chan
);
1401 trace_ust_destroy_channel(chan
);
1405 lttng_ht_destroy(channels
);
1409 * Cleanup UST global domain.
1411 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
1415 destroy_channels(dom
->channels
);
1419 * Cleanup ust session structure, keeping data required by
1422 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
1425 struct buffer_reg_uid
*reg
, *sreg
;
1426 struct lttng_ht_iter iter
;
1428 LTTNG_ASSERT(session
);
1430 DBG2("Trace UST destroy session %" PRIu64
, session
->id
);
1432 /* Cleaning up UST domain */
1433 destroy_domain_global(&session
->domain_global
);
1436 cds_lfht_for_each_entry(session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
1437 int ret
= lttng_ht_del(session
->agents
, &iter
);
1444 lttng_ht_destroy(session
->agents
);
1446 /* Cleanup UID buffer registry object(s). */
1447 cds_list_for_each_entry_safe(reg
, sreg
, &session
->buffer_reg_uid_list
,
1449 cds_list_del(®
->lnode
);
1450 buffer_reg_uid_remove(reg
);
1451 buffer_reg_uid_destroy(reg
, session
->consumer
);
1454 process_attr_tracker_destroy(session
->tracker_vpid
);
1455 process_attr_tracker_destroy(session
->tracker_vuid
);
1456 process_attr_tracker_destroy(session
->tracker_vgid
);
1458 fini_id_tracker(&session
->vpid_tracker
);
1459 fini_id_tracker(&session
->vuid_tracker
);
1460 fini_id_tracker(&session
->vgid_tracker
);
1461 lttng_trace_chunk_put(session
->current_trace_chunk
);
1464 /* Free elements needed by destroy notifiers. */
1465 void trace_ust_free_session(struct ltt_ust_session
*session
)
1467 consumer_output_put(session
->consumer
);