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.h>
17 #include <common/defaults.h>
18 #include <common/trace-chunk.h>
19 #include <common/utils.h>
21 #include "buffer-registry.h"
22 #include "trace-ust.h"
28 * Match function for the events hash table lookup.
30 * Matches by name only. Used by the disable command.
32 int trace_ust_ht_match_event_by_name(struct cds_lfht_node
*node
,
35 struct ltt_ust_event
*event
;
41 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
42 name
= (const char *) _key
;
45 if (strncmp(event
->attr
.name
, name
, sizeof(event
->attr
.name
)) != 0) {
57 * Match function for the hash table lookup.
59 * It matches an ust event based on three attributes which are the event name,
60 * the filter bytecode and the loglevel.
62 int trace_ust_ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
64 struct ltt_ust_event
*event
;
65 const struct ltt_ust_ht_key
*key
;
66 int ev_loglevel_value
;
72 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
73 key
= (ltt_ust_ht_key
*) _key
;
74 ev_loglevel_value
= event
->attr
.loglevel
;
76 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
79 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
83 /* Event loglevel value and type. */
84 ll_match
= loglevels_match(event
->attr
.loglevel_type
,
85 ev_loglevel_value
, key
->loglevel_type
,
86 key
->loglevel_value
, LTTNG_UST_ABI_LOGLEVEL_ALL
);
92 /* Only one of the filters is NULL, fail. */
93 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
97 if (key
->filter
&& event
->filter
) {
98 /* Both filters exists, check length followed by the bytecode. */
99 if (event
->filter
->len
!= key
->filter
->len
||
100 memcmp(event
->filter
->data
, key
->filter
->data
,
101 event
->filter
->len
) != 0) {
106 /* If only one of the exclusions is NULL, fail. */
107 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
111 if (key
->exclusion
&& event
->exclusion
) {
114 /* Check exclusion counts first. */
115 if (event
->exclusion
->count
!= key
->exclusion
->count
) {
119 /* Compare names individually. */
120 for (i
= 0; i
< event
->exclusion
->count
; ++i
) {
123 const char *name_ev
=
124 LTTNG_EVENT_EXCLUSION_NAME_AT(
125 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(
136 if (!strncmp(name_ev
, name_key
,
137 LTTNG_SYMBOL_NAME_LEN
)) {
145 * If the current exclusion name was not found amongst
146 * the key's exclusion names, then there's no match.
161 * Find the channel in the hashtable and return channel pointer. RCU read side
162 * lock MUST be acquired before calling this.
164 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct lttng_ht
*ht
,
167 struct lttng_ht_node_str
*node
;
168 struct lttng_ht_iter iter
;
171 * If we receive an empty string for channel name, it means the
172 * default channel name is requested.
175 name
= DEFAULT_CHANNEL_NAME
;
177 lttng_ht_lookup(ht
, (void *)name
, &iter
);
178 node
= lttng_ht_iter_get_node_str(&iter
);
183 DBG2("Trace UST channel %s found by name", name
);
185 return caa_container_of(node
, struct ltt_ust_channel
, node
);
188 DBG2("Trace UST channel %s not found by name", name
);
193 * Find the event in the hashtable and return event pointer. RCU read side lock
194 * MUST be acquired before calling this.
196 struct ltt_ust_event
*trace_ust_find_event(struct lttng_ht
*ht
,
197 char *name
, struct lttng_bytecode
*filter
,
198 enum lttng_ust_abi_loglevel_type loglevel_type
, int loglevel_value
,
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
;
210 key
.loglevel_type
= loglevel_type
;
211 key
.loglevel_value
= loglevel_value
;
212 key
.exclusion
= exclusion
;
214 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
215 trace_ust_ht_match_event
, &key
, &iter
.iter
);
216 node
= lttng_ht_iter_get_node_str(&iter
);
221 DBG2("Trace UST event %s found", key
.name
);
223 return caa_container_of(node
, struct ltt_ust_event
, node
);
226 DBG2("Trace UST event %s NOT found", key
.name
);
231 * Lookup an agent in the session agents hash table by domain type and return
232 * the object if found else NULL.
234 * RCU read side lock must be acquired before calling and only released
235 * once the agent is no longer in scope or being used.
237 struct agent
*trace_ust_find_agent(struct ltt_ust_session
*session
,
238 enum lttng_domain_type domain_type
)
240 struct agent
*agt
= NULL
;
241 struct lttng_ht_node_u64
*node
;
242 struct lttng_ht_iter iter
;
245 LTTNG_ASSERT(session
);
247 DBG3("Trace ust agent lookup for domain %d", domain_type
);
251 lttng_ht_lookup(session
->agents
, &key
, &iter
);
252 node
= lttng_ht_iter_get_node_u64(&iter
);
256 agt
= caa_container_of(node
, struct agent
, node
);
263 * Allocate and initialize a ust session data structure.
265 * Return pointer to structure or NULL.
267 struct ltt_ust_session
*trace_ust_create_session(uint64_t session_id
)
269 struct ltt_ust_session
*lus
;
271 /* Allocate a new ltt ust session */
272 lus
= (ltt_ust_session
*) zmalloc(sizeof(struct ltt_ust_session
));
274 PERROR("create ust session zmalloc");
278 /* Init data structure */
279 lus
->id
= session_id
;
282 /* Set default metadata channel attribute. */
283 lus
->metadata_attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
284 lus
->metadata_attr
.subbuf_size
= default_get_metadata_subbuf_size();
285 lus
->metadata_attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
286 lus
->metadata_attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
287 lus
->metadata_attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;
288 lus
->metadata_attr
.output
= LTTNG_UST_ABI_MMAP
;
291 * Default buffer type. This can be changed through an enable channel
292 * requesting a different type. Note that this can only be changed once
293 * during the session lifetime which is at the first enable channel and
294 * only before start. The flag buffer_type_changed indicates the status.
296 lus
->buffer_type
= LTTNG_BUFFER_PER_UID
;
297 /* Once set to 1, the buffer_type is immutable for the session. */
298 lus
->buffer_type_changed
= 0;
299 /* Init it in case it get used after allocation. */
300 CDS_INIT_LIST_HEAD(&lus
->buffer_reg_uid_list
);
302 /* Alloc UST global domain channels' HT */
303 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
304 /* Alloc agent hash table. */
305 lus
->agents
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
307 lus
->tracker_vpid
= process_attr_tracker_create();
308 if (!lus
->tracker_vpid
) {
311 lus
->tracker_vuid
= process_attr_tracker_create();
312 if (!lus
->tracker_vuid
) {
315 lus
->tracker_vgid
= process_attr_tracker_create();
316 if (!lus
->tracker_vgid
) {
319 lus
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
320 if (lus
->consumer
== NULL
) {
324 DBG2("UST trace session create successful");
329 process_attr_tracker_destroy(lus
->tracker_vpid
);
330 process_attr_tracker_destroy(lus
->tracker_vuid
);
331 process_attr_tracker_destroy(lus
->tracker_vgid
);
332 lttng_ht_destroy(lus
->domain_global
.channels
);
333 lttng_ht_destroy(lus
->agents
);
340 * Allocate and initialize a ust channel data structure.
342 * Return pointer to structure or NULL.
344 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
345 enum lttng_domain_type domain
)
347 struct ltt_ust_channel
*luc
;
351 luc
= (ltt_ust_channel
*) zmalloc(sizeof(struct ltt_ust_channel
));
353 PERROR("ltt_ust_channel zmalloc");
357 luc
->domain
= domain
;
359 /* Copy UST channel attributes */
360 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
361 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
362 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
363 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
364 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
365 luc
->attr
.output
= (enum lttng_ust_abi_output
) chan
->attr
.output
;
366 luc
->monitor_timer_interval
= ((struct lttng_channel_extended
*)
367 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
368 luc
->attr
.u
.s
.blocking_timeout
= ((struct lttng_channel_extended
*)
369 chan
->attr
.extended
.ptr
)->blocking_timeout
;
371 /* Translate to UST output enum */
372 switch (luc
->attr
.output
) {
374 luc
->attr
.output
= LTTNG_UST_ABI_MMAP
;
379 * If we receive an empty string for channel name, it means the
380 * default channel name is requested.
382 if (chan
->name
[0] == '\0') {
383 strncpy(luc
->name
, DEFAULT_CHANNEL_NAME
, sizeof(luc
->name
));
385 /* Copy channel name */
386 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
388 luc
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
391 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
392 CDS_INIT_LIST_HEAD(&luc
->ctx_list
);
394 /* Alloc hash tables */
395 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
396 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
398 /* On-disk circular buffer parameters */
399 luc
->tracefile_size
= chan
->attr
.tracefile_size
;
400 luc
->tracefile_count
= chan
->attr
.tracefile_count
;
402 DBG2("Trace UST channel %s created", luc
->name
);
409 * Validates an exclusion list.
411 * Returns 0 if valid, negative value if invalid.
413 static int validate_exclusion(struct lttng_event_exclusion
*exclusion
)
418 LTTNG_ASSERT(exclusion
);
420 for (i
= 0; i
< exclusion
->count
; ++i
) {
423 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
425 for (j
= 0; j
< i
; ++j
) {
427 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, j
);
429 if (!strncmp(name_a
, name_b
, LTTNG_SYMBOL_NAME_LEN
)) {
442 * Allocate and initialize a ust event. Set name and event type.
443 * We own filter_expression, filter, and exclusion.
445 * Return an lttng_error_code
447 enum lttng_error_code
trace_ust_create_event(struct lttng_event
*ev
,
448 char *filter_expression
,
449 struct lttng_bytecode
*filter
,
450 struct lttng_event_exclusion
*exclusion
,
452 struct ltt_ust_event
**ust_event
)
454 struct ltt_ust_event
*local_ust_event
;
455 enum lttng_error_code ret
= LTTNG_OK
;
459 if (exclusion
&& validate_exclusion(exclusion
)) {
460 ret
= LTTNG_ERR_INVALID
;
464 local_ust_event
= (ltt_ust_event
*) zmalloc(sizeof(struct ltt_ust_event
));
465 if (local_ust_event
== NULL
) {
466 PERROR("ust event zmalloc");
467 ret
= LTTNG_ERR_NOMEM
;
471 local_ust_event
->internal
= internal_event
;
474 case LTTNG_EVENT_PROBE
:
475 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_PROBE
;
477 case LTTNG_EVENT_FUNCTION
:
478 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_FUNCTION
;
480 case LTTNG_EVENT_FUNCTION_ENTRY
:
481 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_FUNCTION
;
483 case LTTNG_EVENT_TRACEPOINT
:
484 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
487 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
488 ret
= LTTNG_ERR_INVALID
;
489 goto error_free_event
;
492 /* Copy event name */
493 strncpy(local_ust_event
->attr
.name
, ev
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
494 local_ust_event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
496 switch (ev
->loglevel_type
) {
497 case LTTNG_EVENT_LOGLEVEL_ALL
:
498 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
499 local_ust_event
->attr
.loglevel
= -1; /* Force to -1 */
501 case LTTNG_EVENT_LOGLEVEL_RANGE
:
502 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
503 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
505 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
506 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
507 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
510 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
511 ret
= LTTNG_ERR_INVALID
;
512 goto error_free_event
;
516 local_ust_event
->filter_expression
= filter_expression
;
517 local_ust_event
->filter
= filter
;
518 local_ust_event
->exclusion
= exclusion
;
521 lttng_ht_node_init_str(&local_ust_event
->node
, local_ust_event
->attr
.name
);
523 DBG2("Trace UST event %s, loglevel (%d,%d) created",
524 local_ust_event
->attr
.name
, local_ust_event
->attr
.loglevel_type
,
525 local_ust_event
->attr
.loglevel
);
527 *ust_event
= local_ust_event
;
532 free(local_ust_event
);
534 free(filter_expression
);
541 int trace_ust_context_type_event_to_ust(
542 enum lttng_event_context_type type
)
547 case LTTNG_EVENT_CONTEXT_VTID
:
548 utype
= LTTNG_UST_ABI_CONTEXT_VTID
;
550 case LTTNG_EVENT_CONTEXT_VPID
:
551 utype
= LTTNG_UST_ABI_CONTEXT_VPID
;
553 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
554 utype
= LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
;
556 case LTTNG_EVENT_CONTEXT_PROCNAME
:
557 utype
= LTTNG_UST_ABI_CONTEXT_PROCNAME
;
559 case LTTNG_EVENT_CONTEXT_IP
:
560 utype
= LTTNG_UST_ABI_CONTEXT_IP
;
562 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
563 if (!lttng_ust_ctl_has_perf_counters()) {
565 WARN("Perf counters not implemented in UST");
567 utype
= LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
;
570 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
571 utype
= LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
;
573 case LTTNG_EVENT_CONTEXT_CGROUP_NS
:
574 utype
= LTTNG_UST_ABI_CONTEXT_CGROUP_NS
;
576 case LTTNG_EVENT_CONTEXT_IPC_NS
:
577 utype
= LTTNG_UST_ABI_CONTEXT_IPC_NS
;
579 case LTTNG_EVENT_CONTEXT_MNT_NS
:
580 utype
= LTTNG_UST_ABI_CONTEXT_MNT_NS
;
582 case LTTNG_EVENT_CONTEXT_NET_NS
:
583 utype
= LTTNG_UST_ABI_CONTEXT_NET_NS
;
585 case LTTNG_EVENT_CONTEXT_PID_NS
:
586 utype
= LTTNG_UST_ABI_CONTEXT_PID_NS
;
588 case LTTNG_EVENT_CONTEXT_TIME_NS
:
589 utype
= LTTNG_UST_ABI_CONTEXT_TIME_NS
;
591 case LTTNG_EVENT_CONTEXT_USER_NS
:
592 utype
= LTTNG_UST_ABI_CONTEXT_USER_NS
;
594 case LTTNG_EVENT_CONTEXT_UTS_NS
:
595 utype
= LTTNG_UST_ABI_CONTEXT_UTS_NS
;
597 case LTTNG_EVENT_CONTEXT_VUID
:
598 utype
= LTTNG_UST_ABI_CONTEXT_VUID
;
600 case LTTNG_EVENT_CONTEXT_VEUID
:
601 utype
= LTTNG_UST_ABI_CONTEXT_VEUID
;
603 case LTTNG_EVENT_CONTEXT_VSUID
:
604 utype
= LTTNG_UST_ABI_CONTEXT_VSUID
;
606 case LTTNG_EVENT_CONTEXT_VGID
:
607 utype
= LTTNG_UST_ABI_CONTEXT_VGID
;
609 case LTTNG_EVENT_CONTEXT_VEGID
:
610 utype
= LTTNG_UST_ABI_CONTEXT_VEGID
;
612 case LTTNG_EVENT_CONTEXT_VSGID
:
613 utype
= LTTNG_UST_ABI_CONTEXT_VSGID
;
623 * Return 1 if contexts match, 0 otherwise.
625 int trace_ust_match_context(const struct ltt_ust_context
*uctx
,
626 const struct lttng_event_context
*ctx
)
630 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
634 if (uctx
->ctx
.ctx
!= utype
) {
638 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
639 if (uctx
->ctx
.u
.perf_counter
.type
640 != ctx
->u
.perf_counter
.type
) {
643 if (uctx
->ctx
.u
.perf_counter
.config
644 != 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
)) {
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
,
657 ctx
->u
.app_ctx
.provider_name
) ||
658 strcmp(uctx
->ctx
.u
.app_ctx
.ctx_name
,
659 ctx
->u
.app_ctx
.ctx_name
)) {
670 * Allocate and initialize an UST context.
672 * Return pointer to structure or NULL.
674 struct ltt_ust_context
*trace_ust_create_context(
675 const struct lttng_event_context
*ctx
)
677 struct ltt_ust_context
*uctx
= NULL
;
682 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
684 ERR("Invalid UST context");
688 uctx
= (ltt_ust_context
*) zmalloc(sizeof(struct ltt_ust_context
));
690 PERROR("zmalloc ltt_ust_context");
694 uctx
->ctx
.ctx
= (enum lttng_ust_abi_context_type
) utype
;
696 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
697 uctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
698 uctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
699 strncpy(uctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
700 LTTNG_UST_ABI_SYM_NAME_LEN
);
701 uctx
->ctx
.u
.perf_counter
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
703 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
705 char *provider_name
= NULL
, *ctx_name
= NULL
;
707 provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
708 if (!provider_name
) {
711 uctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
713 ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
717 uctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
723 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
727 trace_ust_destroy_context(uctx
);
731 static void destroy_id_tracker_node_rcu(struct rcu_head
*head
)
733 struct ust_id_tracker_node
*tracker_node
= caa_container_of(
734 head
, struct ust_id_tracker_node
, node
.head
);
738 static void destroy_id_tracker_node(struct ust_id_tracker_node
*tracker_node
)
740 call_rcu(&tracker_node
->node
.head
, destroy_id_tracker_node_rcu
);
743 static int init_id_tracker(struct ust_id_tracker
*id_tracker
)
747 id_tracker
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
748 if (!id_tracker
->ht
) {
749 ret
= LTTNG_ERR_NOMEM
;
758 * Teardown id tracker content, but don't free id_tracker object.
760 static void fini_id_tracker(struct ust_id_tracker
*id_tracker
)
762 struct ust_id_tracker_node
*tracker_node
;
763 struct lttng_ht_iter iter
;
765 if (!id_tracker
->ht
) {
769 cds_lfht_for_each_entry (id_tracker
->ht
->ht
, &iter
.iter
, tracker_node
,
771 int ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
774 destroy_id_tracker_node(tracker_node
);
777 lttng_ht_destroy(id_tracker
->ht
);
778 id_tracker
->ht
= NULL
;
781 static struct ust_id_tracker_node
*id_tracker_lookup(
782 struct ust_id_tracker
*id_tracker
,
784 struct lttng_ht_iter
*iter
)
786 unsigned long _id
= (unsigned long) id
;
787 struct lttng_ht_node_ulong
*node
;
789 lttng_ht_lookup(id_tracker
->ht
, (void *) _id
, iter
);
790 node
= lttng_ht_iter_get_node_ulong(iter
);
792 return caa_container_of(node
, struct ust_id_tracker_node
, node
);
798 static int id_tracker_add_id(struct ust_id_tracker
*id_tracker
, int id
)
800 int retval
= LTTNG_OK
;
801 struct ust_id_tracker_node
*tracker_node
;
802 struct lttng_ht_iter iter
;
805 retval
= LTTNG_ERR_INVALID
;
808 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
810 /* Already exists. */
811 retval
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
814 tracker_node
= (ust_id_tracker_node
*) zmalloc(sizeof(*tracker_node
));
816 retval
= LTTNG_ERR_NOMEM
;
819 lttng_ht_node_init_ulong(&tracker_node
->node
, (unsigned long) id
);
820 lttng_ht_add_unique_ulong(id_tracker
->ht
, &tracker_node
->node
);
825 static int id_tracker_del_id(struct ust_id_tracker
*id_tracker
, int id
)
827 int retval
= LTTNG_OK
, ret
;
828 struct ust_id_tracker_node
*tracker_node
;
829 struct lttng_ht_iter iter
;
832 retval
= LTTNG_ERR_INVALID
;
835 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
838 retval
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
841 ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
844 destroy_id_tracker_node(tracker_node
);
849 static struct ust_id_tracker
*get_id_tracker(struct ltt_ust_session
*session
,
850 enum lttng_process_attr process_attr
)
852 switch (process_attr
) {
853 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
854 return &session
->vpid_tracker
;
855 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
856 return &session
->vuid_tracker
;
857 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
858 return &session
->vgid_tracker
;
864 static struct process_attr_tracker
*_trace_ust_get_process_attr_tracker(
865 struct ltt_ust_session
*session
,
866 enum lttng_process_attr process_attr
)
868 switch (process_attr
) {
869 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
870 return session
->tracker_vpid
;
871 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
872 return session
->tracker_vuid
;
873 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
874 return session
->tracker_vgid
;
880 const struct process_attr_tracker
*trace_ust_get_process_attr_tracker(
881 struct ltt_ust_session
*session
,
882 enum lttng_process_attr process_attr
)
884 return (const struct process_attr_tracker
*)
885 _trace_ust_get_process_attr_tracker(
886 session
, process_attr
);
890 * The session lock is held when calling this function.
892 int trace_ust_id_tracker_lookup(enum lttng_process_attr process_attr
,
893 struct ltt_ust_session
*session
,
896 struct lttng_ht_iter iter
;
897 struct ust_id_tracker
*id_tracker
;
899 id_tracker
= get_id_tracker(session
, process_attr
);
903 if (!id_tracker
->ht
) {
906 if (id_tracker_lookup(id_tracker
, id
, &iter
)) {
913 * Called with the session lock held.
915 enum lttng_error_code
trace_ust_process_attr_tracker_set_tracking_policy(
916 struct ltt_ust_session
*session
,
917 enum lttng_process_attr process_attr
,
918 enum lttng_tracking_policy policy
)
921 enum lttng_error_code ret_code
= LTTNG_OK
;
922 struct ust_id_tracker
*id_tracker
=
923 get_id_tracker(session
, process_attr
);
924 struct process_attr_tracker
*tracker
=
925 _trace_ust_get_process_attr_tracker(
926 session
, process_attr
);
927 bool should_update_apps
= false;
928 enum lttng_tracking_policy previous_policy
;
931 ret_code
= LTTNG_ERR_INVALID
;
935 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
936 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
938 ret_code
= LTTNG_ERR_UNK
;
942 if (previous_policy
== policy
) {
947 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
948 /* Track all values: destroy tracker if exists. */
949 if (id_tracker
->ht
) {
950 fini_id_tracker(id_tracker
);
951 /* Ensure all apps have session. */
952 should_update_apps
= true;
955 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
956 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
958 fini_id_tracker(id_tracker
);
959 ret_code
= (lttng_error_code
) init_id_tracker(id_tracker
);
960 if (ret_code
!= LTTNG_OK
) {
961 ERR("Error initializing ID tracker");
964 /* Remove all apps from session. */
965 should_update_apps
= true;
970 if (should_update_apps
&& session
->active
) {
971 ust_app_global_update_all(session
);
977 /* Called with the session lock held. */
978 enum lttng_error_code
trace_ust_process_attr_tracker_inclusion_set_add_value(
979 struct ltt_ust_session
*session
,
980 enum lttng_process_attr process_attr
,
981 const struct process_attr_value
*value
)
983 enum lttng_error_code ret_code
= LTTNG_OK
;
984 bool should_update_apps
= false;
985 struct ust_id_tracker
*id_tracker
=
986 get_id_tracker(session
, process_attr
);
987 struct process_attr_tracker
*tracker
;
989 enum process_attr_tracker_status status
;
993 * Convert process attribute tracker value to the integral
994 * representation required by the kern-ctl API.
996 switch (process_attr
) {
997 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
998 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
999 integral_value
= (int) value
->value
.pid
;
1001 case LTTNG_PROCESS_ATTR_USER_ID
:
1002 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1003 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1006 ret_code
= utils_user_id_from_name(
1007 value
->value
.user_name
, &uid
);
1008 if (ret_code
!= LTTNG_OK
) {
1011 integral_value
= (int) uid
;
1013 integral_value
= (int) value
->value
.uid
;
1016 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1017 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1018 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1021 ret_code
= utils_group_id_from_name(
1022 value
->value
.group_name
, &gid
);
1023 if (ret_code
!= LTTNG_OK
) {
1026 integral_value
= (int) gid
;
1028 integral_value
= (int) value
->value
.gid
;
1032 ret_code
= LTTNG_ERR_INVALID
;
1036 tracker
= _trace_ust_get_process_attr_tracker(session
, process_attr
);
1038 ret_code
= LTTNG_ERR_INVALID
;
1042 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
1043 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1045 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
1046 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1048 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1049 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1051 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1053 ret_code
= LTTNG_ERR_UNK
;
1059 DBG("User space track %s %d for session id %" PRIu64
,
1060 lttng_process_attr_to_string(process_attr
),
1061 integral_value
, session
->id
);
1063 ret_code
= (lttng_error_code
) id_tracker_add_id(id_tracker
, integral_value
);
1064 if (ret_code
!= LTTNG_OK
) {
1067 /* Add session to application */
1068 switch (process_attr
) {
1069 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1070 app
= ust_app_find_by_pid(integral_value
);
1072 should_update_apps
= true;
1076 should_update_apps
= true;
1079 if (should_update_apps
&& session
->active
) {
1080 ust_app_global_update_all(session
);
1086 /* Called with the session lock held. */
1087 enum lttng_error_code
trace_ust_process_attr_tracker_inclusion_set_remove_value(
1088 struct ltt_ust_session
*session
,
1089 enum lttng_process_attr process_attr
,
1090 const struct process_attr_value
*value
)
1092 enum lttng_error_code ret_code
= LTTNG_OK
;
1093 bool should_update_apps
= false;
1094 struct ust_id_tracker
*id_tracker
=
1095 get_id_tracker(session
, process_attr
);
1096 struct process_attr_tracker
*tracker
;
1098 enum process_attr_tracker_status status
;
1099 struct ust_app
*app
;
1102 * Convert process attribute tracker value to the integral
1103 * representation required by the kern-ctl API.
1105 switch (process_attr
) {
1106 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1107 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1108 integral_value
= (int) value
->value
.pid
;
1110 case LTTNG_PROCESS_ATTR_USER_ID
:
1111 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1112 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1115 ret_code
= utils_user_id_from_name(
1116 value
->value
.user_name
, &uid
);
1117 if (ret_code
!= LTTNG_OK
) {
1120 integral_value
= (int) uid
;
1122 integral_value
= (int) value
->value
.uid
;
1125 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1126 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1127 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1130 ret_code
= utils_group_id_from_name(
1131 value
->value
.group_name
, &gid
);
1132 if (ret_code
!= LTTNG_OK
) {
1135 integral_value
= (int) gid
;
1137 integral_value
= (int) value
->value
.gid
;
1141 ret_code
= LTTNG_ERR_INVALID
;
1145 tracker
= _trace_ust_get_process_attr_tracker(session
, process_attr
);
1147 ret_code
= LTTNG_ERR_INVALID
;
1151 status
= process_attr_tracker_inclusion_set_remove_value(
1153 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1155 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1156 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1158 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1159 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1161 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1163 ret_code
= LTTNG_ERR_UNK
;
1169 DBG("User space untrack %s %d for session id %" PRIu64
,
1170 lttng_process_attr_to_string(process_attr
),
1171 integral_value
, session
->id
);
1173 ret_code
= (lttng_error_code
) id_tracker_del_id(id_tracker
, integral_value
);
1174 if (ret_code
!= LTTNG_OK
) {
1177 /* Add session to application */
1178 switch (process_attr
) {
1179 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1180 app
= ust_app_find_by_pid(integral_value
);
1182 should_update_apps
= true;
1186 should_update_apps
= true;
1189 if (should_update_apps
&& session
->active
) {
1190 ust_app_global_update_all(session
);
1197 * RCU safe free context structure.
1199 static void destroy_context_rcu(struct rcu_head
*head
)
1201 struct lttng_ht_node_ulong
*node
=
1202 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1203 struct ltt_ust_context
*ctx
=
1204 caa_container_of(node
, struct ltt_ust_context
, node
);
1206 trace_ust_destroy_context(ctx
);
1210 * Cleanup UST context hash table.
1212 static void destroy_contexts(struct lttng_ht
*ht
)
1215 struct lttng_ht_node_ulong
*node
;
1216 struct lttng_ht_iter iter
;
1217 struct ltt_ust_context
*ctx
;
1222 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
1223 /* Remove from ordered list. */
1224 ctx
= caa_container_of(node
, struct ltt_ust_context
, node
);
1225 cds_list_del(&ctx
->list
);
1226 /* Remove from channel's hash table. */
1227 ret
= lttng_ht_del(ht
, &iter
);
1229 call_rcu(&node
->head
, destroy_context_rcu
);
1234 lttng_ht_destroy(ht
);
1238 * Cleanup ust event structure.
1240 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
1242 LTTNG_ASSERT(event
);
1244 DBG2("Trace destroy UST event %s", event
->attr
.name
);
1245 free(event
->filter_expression
);
1246 free(event
->filter
);
1247 free(event
->exclusion
);
1252 * Cleanup ust context structure.
1254 void trace_ust_destroy_context(struct ltt_ust_context
*ctx
)
1258 if (ctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1259 free(ctx
->ctx
.u
.app_ctx
.provider_name
);
1260 free(ctx
->ctx
.u
.app_ctx
.ctx_name
);
1266 * URCU intermediate call to complete destroy event.
1268 static void destroy_event_rcu(struct rcu_head
*head
)
1270 struct lttng_ht_node_str
*node
=
1271 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1272 struct ltt_ust_event
*event
=
1273 caa_container_of(node
, struct ltt_ust_event
, node
);
1275 trace_ust_destroy_event(event
);
1279 * Cleanup UST events hashtable.
1281 static void destroy_events(struct lttng_ht
*events
)
1284 struct lttng_ht_node_str
*node
;
1285 struct lttng_ht_iter iter
;
1287 LTTNG_ASSERT(events
);
1290 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
1291 ret
= lttng_ht_del(events
, &iter
);
1293 call_rcu(&node
->head
, destroy_event_rcu
);
1297 lttng_ht_destroy(events
);
1301 * Cleanup ust channel structure.
1303 * Should _NOT_ be called with RCU read lock held.
1305 static void _trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1307 LTTNG_ASSERT(channel
);
1309 DBG2("Trace destroy UST channel %s", channel
->name
);
1315 * URCU intermediate call to complete destroy channel.
1317 static void destroy_channel_rcu(struct rcu_head
*head
)
1319 struct lttng_ht_node_str
*node
=
1320 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1321 struct ltt_ust_channel
*channel
=
1322 caa_container_of(node
, struct ltt_ust_channel
, node
);
1324 _trace_ust_destroy_channel(channel
);
1327 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1329 /* Destroying all events of the channel */
1330 destroy_events(channel
->events
);
1331 /* Destroying all context of the channel */
1332 destroy_contexts(channel
->ctx
);
1334 call_rcu(&channel
->node
.head
, destroy_channel_rcu
);
1338 * Remove an UST channel from a channel HT.
1340 void trace_ust_delete_channel(struct lttng_ht
*ht
,
1341 struct ltt_ust_channel
*channel
)
1344 struct lttng_ht_iter iter
;
1347 LTTNG_ASSERT(channel
);
1349 iter
.iter
.node
= &channel
->node
.node
;
1350 ret
= lttng_ht_del(ht
, &iter
);
1355 * Iterate over a hash table containing channels and cleanup safely.
1357 static void destroy_channels(struct lttng_ht
*channels
)
1359 struct lttng_ht_node_str
*node
;
1360 struct lttng_ht_iter iter
;
1362 LTTNG_ASSERT(channels
);
1365 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
1366 struct ltt_ust_channel
*chan
=
1367 caa_container_of(node
, struct ltt_ust_channel
, node
);
1369 trace_ust_delete_channel(channels
, chan
);
1370 trace_ust_destroy_channel(chan
);
1374 lttng_ht_destroy(channels
);
1378 * Cleanup UST global domain.
1380 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
1384 destroy_channels(dom
->channels
);
1388 * Cleanup ust session structure, keeping data required by
1391 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
1394 struct buffer_reg_uid
*reg
, *sreg
;
1395 struct lttng_ht_iter iter
;
1397 LTTNG_ASSERT(session
);
1399 DBG2("Trace UST destroy session %" PRIu64
, session
->id
);
1401 /* Cleaning up UST domain */
1402 destroy_domain_global(&session
->domain_global
);
1405 cds_lfht_for_each_entry(session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
1406 int ret
= lttng_ht_del(session
->agents
, &iter
);
1413 lttng_ht_destroy(session
->agents
);
1415 /* Cleanup UID buffer registry object(s). */
1416 cds_list_for_each_entry_safe(reg
, sreg
, &session
->buffer_reg_uid_list
,
1418 cds_list_del(®
->lnode
);
1419 buffer_reg_uid_remove(reg
);
1420 buffer_reg_uid_destroy(reg
, session
->consumer
);
1423 process_attr_tracker_destroy(session
->tracker_vpid
);
1424 process_attr_tracker_destroy(session
->tracker_vuid
);
1425 process_attr_tracker_destroy(session
->tracker_vgid
);
1427 fini_id_tracker(&session
->vpid_tracker
);
1428 fini_id_tracker(&session
->vuid_tracker
);
1429 fini_id_tracker(&session
->vgid_tracker
);
1430 lttng_trace_chunk_put(session
->current_trace_chunk
);
1433 /* Free elements needed by destroy notifiers. */
1434 void trace_ust_free_session(struct ltt_ust_session
*session
)
1436 consumer_output_put(session
->consumer
);