2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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>
20 #include "buffer-registry.h"
21 #include "trace-ust.h"
27 * Match function for the events hash table lookup.
29 * Matches by name only. Used by the disable command.
31 int trace_ust_ht_match_event_by_name(struct cds_lfht_node
*node
,
34 struct ltt_ust_event
*event
;
40 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
44 if (strncmp(event
->attr
.name
, name
, sizeof(event
->attr
.name
)) != 0) {
56 * Match function for the hash table lookup.
58 * It matches an ust event based on three attributes which are the event name,
59 * the filter bytecode and the loglevel.
61 int trace_ust_ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
63 struct ltt_ust_event
*event
;
64 const struct ltt_ust_ht_key
*key
;
65 int ev_loglevel_value
;
71 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
73 ev_loglevel_value
= event
->attr
.loglevel
;
75 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
78 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
82 /* Event loglevel value and type. */
83 ll_match
= loglevels_match(event
->attr
.loglevel_type
,
84 ev_loglevel_value
, key
->loglevel_type
,
85 key
->loglevel_value
, LTTNG_UST_LOGLEVEL_ALL
);
91 /* Only one of the filters is NULL, fail. */
92 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
96 if (key
->filter
&& event
->filter
) {
97 /* Both filters exists, check length followed by the bytecode. */
98 if (event
->filter
->len
!= key
->filter
->len
||
99 memcmp(event
->filter
->data
, key
->filter
->data
,
100 event
->filter
->len
) != 0) {
105 /* If only one of the exclusions is NULL, fail. */
106 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
110 if (key
->exclusion
&& event
->exclusion
) {
113 /* Check exclusion counts first. */
114 if (event
->exclusion
->count
!= key
->exclusion
->count
) {
118 /* Compare names individually. */
119 for (i
= 0; i
< event
->exclusion
->count
; ++i
) {
122 const char *name_ev
=
123 LTTNG_EVENT_EXCLUSION_NAME_AT(
124 event
->exclusion
, i
);
127 * Compare this exclusion name to all the key's
130 for (j
= 0; j
< key
->exclusion
->count
; ++j
) {
131 const char *name_key
=
132 LTTNG_EVENT_EXCLUSION_NAME_AT(
135 if (!strncmp(name_ev
, name_key
,
136 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
,
166 struct lttng_ht_node_str
*node
;
167 struct lttng_ht_iter iter
;
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
);
182 DBG2("Trace UST channel %s found by name", name
);
184 return caa_container_of(node
, struct ltt_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
,
196 char *name
, struct lttng_filter_bytecode
*filter
,
197 enum lttng_ust_loglevel_type loglevel_type
, int loglevel_value
,
198 struct lttng_event_exclusion
*exclusion
)
200 struct lttng_ht_node_str
*node
;
201 struct lttng_ht_iter iter
;
202 struct ltt_ust_ht_key key
;
209 key
.loglevel_type
= loglevel_type
;
210 key
.loglevel_value
= loglevel_value
;
211 key
.exclusion
= exclusion
;
213 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
214 trace_ust_ht_match_event
, &key
, &iter
.iter
);
215 node
= lttng_ht_iter_get_node_str(&iter
);
220 DBG2("Trace UST event %s found", key
.name
);
222 return caa_container_of(node
, struct ltt_ust_event
, node
);
225 DBG2("Trace UST event %s NOT found", key
.name
);
230 * Lookup an agent in the session agents hash table by domain type and return
231 * the object if found else NULL.
233 * RCU read side lock must be acquired before calling and only released
234 * once the agent is no longer in scope or being used.
236 struct agent
*trace_ust_find_agent(struct ltt_ust_session
*session
,
237 enum lttng_domain_type domain_type
)
239 struct agent
*agt
= NULL
;
240 struct lttng_ht_node_u64
*node
;
241 struct lttng_ht_iter iter
;
246 DBG3("Trace ust agent lookup for domain %d", domain_type
);
250 lttng_ht_lookup(session
->agents
, &key
, &iter
);
251 node
= lttng_ht_iter_get_node_u64(&iter
);
255 agt
= caa_container_of(node
, struct agent
, node
);
262 * Allocate and initialize a ust session data structure.
264 * Return pointer to structure or NULL.
266 struct ltt_ust_session
*trace_ust_create_session(uint64_t session_id
)
268 struct ltt_ust_session
*lus
;
270 /* Allocate a new ltt ust session */
271 lus
= zmalloc(sizeof(struct ltt_ust_session
));
273 PERROR("create ust session zmalloc");
277 /* Init data structure */
278 lus
->id
= session_id
;
281 /* Set default metadata channel attribute. */
282 lus
->metadata_attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
283 lus
->metadata_attr
.subbuf_size
= default_get_metadata_subbuf_size();
284 lus
->metadata_attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
285 lus
->metadata_attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
286 lus
->metadata_attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;
287 lus
->metadata_attr
.output
= LTTNG_UST_MMAP
;
290 * Default buffer type. This can be changed through an enable channel
291 * requesting a different type. Note that this can only be changed once
292 * during the session lifetime which is at the first enable channel and
293 * only before start. The flag buffer_type_changed indicates the status.
295 lus
->buffer_type
= LTTNG_BUFFER_PER_UID
;
296 /* Once set to 1, the buffer_type is immutable for the session. */
297 lus
->buffer_type_changed
= 0;
298 /* Init it in case it get used after allocation. */
299 CDS_INIT_LIST_HEAD(&lus
->buffer_reg_uid_list
);
301 /* Alloc UST global domain channels' HT */
302 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
303 /* Alloc agent hash table. */
304 lus
->agents
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
306 lus
->tracker_list_vpid
= lttng_tracker_list_create();
307 if (!lus
->tracker_list_vpid
) {
310 lus
->tracker_list_vuid
= lttng_tracker_list_create();
311 if (!lus
->tracker_list_vuid
) {
314 lus
->tracker_list_vgid
= lttng_tracker_list_create();
315 if (!lus
->tracker_list_vgid
) {
318 lus
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
319 if (lus
->consumer
== NULL
) {
323 DBG2("UST trace session create successful");
328 lttng_tracker_list_destroy(lus
->tracker_list_vpid
);
329 lttng_tracker_list_destroy(lus
->tracker_list_vuid
);
330 lttng_tracker_list_destroy(lus
->tracker_list_vgid
);
331 ht_cleanup_push(lus
->domain_global
.channels
);
332 ht_cleanup_push(lus
->agents
);
339 * Allocate and initialize a ust channel data structure.
341 * Return pointer to structure or NULL.
343 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
344 enum lttng_domain_type domain
)
346 struct ltt_ust_channel
*luc
;
350 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
352 PERROR("ltt_ust_channel zmalloc");
356 luc
->domain
= domain
;
358 /* Copy UST channel attributes */
359 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
360 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
361 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
362 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
363 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
364 luc
->attr
.output
= (enum lttng_ust_output
) chan
->attr
.output
;
365 luc
->monitor_timer_interval
= ((struct lttng_channel_extended
*)
366 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
367 luc
->attr
.u
.s
.blocking_timeout
= ((struct lttng_channel_extended
*)
368 chan
->attr
.extended
.ptr
)->blocking_timeout
;
370 /* Translate to UST output enum */
371 switch (luc
->attr
.output
) {
373 luc
->attr
.output
= LTTNG_UST_MMAP
;
378 * If we receive an empty string for channel name, it means the
379 * default channel name is requested.
381 if (chan
->name
[0] == '\0') {
382 strncpy(luc
->name
, DEFAULT_CHANNEL_NAME
, sizeof(luc
->name
));
384 /* Copy channel name */
385 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
387 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
390 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
391 CDS_INIT_LIST_HEAD(&luc
->ctx_list
);
393 /* Alloc hash tables */
394 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
395 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
397 /* On-disk circular buffer parameters */
398 luc
->tracefile_size
= chan
->attr
.tracefile_size
;
399 luc
->tracefile_count
= chan
->attr
.tracefile_count
;
401 DBG2("Trace UST channel %s created", luc
->name
);
408 * Validates an exclusion list.
410 * Returns 0 if valid, negative value if invalid.
412 static int validate_exclusion(struct lttng_event_exclusion
*exclusion
)
419 for (i
= 0; i
< exclusion
->count
; ++i
) {
422 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
424 for (j
= 0; j
< i
; ++j
) {
426 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, j
);
428 if (!strncmp(name_a
, name_b
, LTTNG_SYMBOL_NAME_LEN
)) {
441 * Allocate and initialize a ust event. Set name and event type.
442 * We own filter_expression, filter, and exclusion.
444 * Return an lttng_error_code
446 enum lttng_error_code
trace_ust_create_event(struct lttng_event
*ev
,
447 char *filter_expression
,
448 struct lttng_filter_bytecode
*filter
,
449 struct lttng_event_exclusion
*exclusion
,
451 struct ltt_ust_event
**ust_event
)
453 struct ltt_ust_event
*local_ust_event
;
454 enum lttng_error_code ret
= LTTNG_OK
;
458 if (exclusion
&& validate_exclusion(exclusion
)) {
459 ret
= LTTNG_ERR_INVALID
;
463 local_ust_event
= zmalloc(sizeof(struct ltt_ust_event
));
464 if (local_ust_event
== NULL
) {
465 PERROR("ust event zmalloc");
466 ret
= LTTNG_ERR_NOMEM
;
470 local_ust_event
->internal
= internal_event
;
473 case LTTNG_EVENT_PROBE
:
474 local_ust_event
->attr
.instrumentation
= LTTNG_UST_PROBE
;
476 case LTTNG_EVENT_FUNCTION
:
477 local_ust_event
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
479 case LTTNG_EVENT_FUNCTION_ENTRY
:
480 local_ust_event
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
482 case LTTNG_EVENT_TRACEPOINT
:
483 local_ust_event
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
486 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
487 ret
= LTTNG_ERR_INVALID
;
488 goto error_free_event
;
491 /* Copy event name */
492 strncpy(local_ust_event
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
493 local_ust_event
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
495 switch (ev
->loglevel_type
) {
496 case LTTNG_EVENT_LOGLEVEL_ALL
:
497 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
498 local_ust_event
->attr
.loglevel
= -1; /* Force to -1 */
500 case LTTNG_EVENT_LOGLEVEL_RANGE
:
501 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
502 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
504 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
505 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
506 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
509 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
510 ret
= LTTNG_ERR_INVALID
;
511 goto error_free_event
;
515 local_ust_event
->filter_expression
= filter_expression
;
516 local_ust_event
->filter
= filter
;
517 local_ust_event
->exclusion
= exclusion
;
520 lttng_ht_node_init_str(&local_ust_event
->node
, local_ust_event
->attr
.name
);
522 DBG2("Trace UST event %s, loglevel (%d,%d) created",
523 local_ust_event
->attr
.name
, local_ust_event
->attr
.loglevel_type
,
524 local_ust_event
->attr
.loglevel
);
526 *ust_event
= local_ust_event
;
531 free(local_ust_event
);
533 free(filter_expression
);
540 int trace_ust_context_type_event_to_ust(
541 enum lttng_event_context_type type
)
546 case LTTNG_EVENT_CONTEXT_VTID
:
547 utype
= LTTNG_UST_CONTEXT_VTID
;
549 case LTTNG_EVENT_CONTEXT_VPID
:
550 utype
= LTTNG_UST_CONTEXT_VPID
;
552 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
553 utype
= LTTNG_UST_CONTEXT_PTHREAD_ID
;
555 case LTTNG_EVENT_CONTEXT_PROCNAME
:
556 utype
= LTTNG_UST_CONTEXT_PROCNAME
;
558 case LTTNG_EVENT_CONTEXT_IP
:
559 utype
= LTTNG_UST_CONTEXT_IP
;
561 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
562 if (!ustctl_has_perf_counters()) {
564 WARN("Perf counters not implemented in UST");
566 utype
= LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
;
569 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
570 utype
= LTTNG_UST_CONTEXT_APP_CONTEXT
;
572 case LTTNG_EVENT_CONTEXT_CGROUP_NS
:
573 utype
= LTTNG_UST_CONTEXT_CGROUP_NS
;
575 case LTTNG_EVENT_CONTEXT_IPC_NS
:
576 utype
= LTTNG_UST_CONTEXT_IPC_NS
;
578 case LTTNG_EVENT_CONTEXT_MNT_NS
:
579 utype
= LTTNG_UST_CONTEXT_MNT_NS
;
581 case LTTNG_EVENT_CONTEXT_NET_NS
:
582 utype
= LTTNG_UST_CONTEXT_NET_NS
;
584 case LTTNG_EVENT_CONTEXT_PID_NS
:
585 utype
= LTTNG_UST_CONTEXT_PID_NS
;
587 case LTTNG_EVENT_CONTEXT_USER_NS
:
588 utype
= LTTNG_UST_CONTEXT_USER_NS
;
590 case LTTNG_EVENT_CONTEXT_UTS_NS
:
591 utype
= LTTNG_UST_CONTEXT_UTS_NS
;
593 case LTTNG_EVENT_CONTEXT_VUID
:
594 utype
= LTTNG_UST_CONTEXT_VUID
;
596 case LTTNG_EVENT_CONTEXT_VEUID
:
597 utype
= LTTNG_UST_CONTEXT_VEUID
;
599 case LTTNG_EVENT_CONTEXT_VSUID
:
600 utype
= LTTNG_UST_CONTEXT_VSUID
;
602 case LTTNG_EVENT_CONTEXT_VGID
:
603 utype
= LTTNG_UST_CONTEXT_VGID
;
605 case LTTNG_EVENT_CONTEXT_VEGID
:
606 utype
= LTTNG_UST_CONTEXT_VEGID
;
608 case LTTNG_EVENT_CONTEXT_VSGID
:
609 utype
= LTTNG_UST_CONTEXT_VSGID
;
619 * Return 1 if contexts match, 0 otherwise.
621 int trace_ust_match_context(const struct ltt_ust_context
*uctx
,
622 const struct lttng_event_context
*ctx
)
626 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
630 if (uctx
->ctx
.ctx
!= utype
) {
634 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
635 if (uctx
->ctx
.u
.perf_counter
.type
636 != ctx
->u
.perf_counter
.type
) {
639 if (uctx
->ctx
.u
.perf_counter
.config
640 != ctx
->u
.perf_counter
.config
) {
643 if (strncmp(uctx
->ctx
.u
.perf_counter
.name
,
644 ctx
->u
.perf_counter
.name
,
645 LTTNG_UST_SYM_NAME_LEN
)) {
649 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
650 assert(uctx
->ctx
.u
.app_ctx
.provider_name
);
651 assert(uctx
->ctx
.u
.app_ctx
.ctx_name
);
652 if (strcmp(uctx
->ctx
.u
.app_ctx
.provider_name
,
653 ctx
->u
.app_ctx
.provider_name
) ||
654 strcmp(uctx
->ctx
.u
.app_ctx
.ctx_name
,
655 ctx
->u
.app_ctx
.ctx_name
)) {
666 * Allocate and initialize an UST context.
668 * Return pointer to structure or NULL.
670 struct ltt_ust_context
*trace_ust_create_context(
671 const struct lttng_event_context
*ctx
)
673 struct ltt_ust_context
*uctx
= NULL
;
678 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
680 ERR("Invalid UST context");
684 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
686 PERROR("zmalloc ltt_ust_context");
690 uctx
->ctx
.ctx
= (enum lttng_ust_context_type
) utype
;
692 case LTTNG_UST_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
, ctx
->u
.perf_counter
.name
,
696 LTTNG_UST_SYM_NAME_LEN
);
697 uctx
->ctx
.u
.perf_counter
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
699 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
701 char *provider_name
= NULL
, *ctx_name
= NULL
;
703 provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
704 if (!provider_name
) {
707 uctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
709 ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
713 uctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
719 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
723 trace_ust_destroy_context(uctx
);
727 static void destroy_id_tracker_node_rcu(struct rcu_head
*head
)
729 struct ust_id_tracker_node
*tracker_node
= caa_container_of(
730 head
, struct ust_id_tracker_node
, node
.head
);
734 static void destroy_id_tracker_node(struct ust_id_tracker_node
*tracker_node
)
736 call_rcu(&tracker_node
->node
.head
, destroy_id_tracker_node_rcu
);
739 static int init_id_tracker(struct ust_id_tracker
*id_tracker
)
743 id_tracker
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
744 if (!id_tracker
->ht
) {
745 ret
= LTTNG_ERR_NOMEM
;
754 * Teardown id tracker content, but don't free id_tracker object.
756 static void fini_id_tracker(struct ust_id_tracker
*id_tracker
)
758 struct ust_id_tracker_node
*tracker_node
;
759 struct lttng_ht_iter iter
;
761 if (!id_tracker
->ht
) {
765 cds_lfht_for_each_entry (id_tracker
->ht
->ht
, &iter
.iter
, tracker_node
,
767 int ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
770 destroy_id_tracker_node(tracker_node
);
773 ht_cleanup_push(id_tracker
->ht
);
774 id_tracker
->ht
= NULL
;
777 static struct ust_id_tracker_node
*id_tracker_lookup(
778 struct ust_id_tracker
*id_tracker
,
780 struct lttng_ht_iter
*iter
)
782 unsigned long _id
= (unsigned long) id
;
783 struct lttng_ht_node_ulong
*node
;
785 lttng_ht_lookup(id_tracker
->ht
, (void *) _id
, iter
);
786 node
= lttng_ht_iter_get_node_ulong(iter
);
788 return caa_container_of(node
, struct ust_id_tracker_node
, node
);
794 static int id_tracker_add_id(struct ust_id_tracker
*id_tracker
, int id
)
796 int retval
= LTTNG_OK
;
797 struct ust_id_tracker_node
*tracker_node
;
798 struct lttng_ht_iter iter
;
801 retval
= LTTNG_ERR_INVALID
;
804 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
806 /* Already exists. */
807 retval
= LTTNG_ERR_ID_TRACKED
;
810 tracker_node
= zmalloc(sizeof(*tracker_node
));
812 retval
= LTTNG_ERR_NOMEM
;
815 lttng_ht_node_init_ulong(&tracker_node
->node
, (unsigned long) id
);
816 lttng_ht_add_unique_ulong(id_tracker
->ht
, &tracker_node
->node
);
821 static int id_tracker_del_id(struct ust_id_tracker
*id_tracker
, int id
)
823 int retval
= LTTNG_OK
, ret
;
824 struct ust_id_tracker_node
*tracker_node
;
825 struct lttng_ht_iter iter
;
828 retval
= LTTNG_ERR_INVALID
;
831 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
834 retval
= LTTNG_ERR_ID_NOT_TRACKED
;
837 ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
840 destroy_id_tracker_node(tracker_node
);
845 static struct ust_id_tracker
*get_id_tracker(struct ltt_ust_session
*session
,
846 enum lttng_tracker_type tracker_type
)
848 switch (tracker_type
) {
849 case LTTNG_TRACKER_VPID
:
850 return &session
->vpid_tracker
;
851 case LTTNG_TRACKER_VUID
:
852 return &session
->vuid_tracker
;
853 case LTTNG_TRACKER_VGID
:
854 return &session
->vgid_tracker
;
860 static struct lttng_tracker_list
*get_id_tracker_list(
861 struct ltt_ust_session
*session
,
862 enum lttng_tracker_type tracker_type
)
864 switch (tracker_type
) {
865 case LTTNG_TRACKER_VPID
:
866 return session
->tracker_list_vpid
;
867 case LTTNG_TRACKER_VUID
:
868 return session
->tracker_list_vuid
;
869 case LTTNG_TRACKER_VGID
:
870 return session
->tracker_list_vgid
;
877 * The session lock is held when calling this function.
879 int trace_ust_id_tracker_lookup(enum lttng_tracker_type tracker_type
,
880 struct ltt_ust_session
*session
,
883 struct lttng_ht_iter iter
;
884 struct ust_id_tracker
*id_tracker
;
886 id_tracker
= get_id_tracker(session
, tracker_type
);
890 if (!id_tracker
->ht
) {
893 if (id_tracker_lookup(id_tracker
, id
, &iter
)) {
900 * Called with the session lock held.
902 int trace_ust_track_id(enum lttng_tracker_type tracker_type
,
903 struct ltt_ust_session
*session
,
904 const struct lttng_tracker_id
*id
)
906 int retval
= LTTNG_OK
;
907 bool should_update_apps
= false;
908 struct ust_id_tracker
*id_tracker
;
909 struct lttng_tracker_list
*tracker_list
;
911 struct lttng_tracker_ids
*saved_ids
;
913 if (tracker_type
== LTTNG_TRACKER_PID
) {
914 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
915 tracker_type
= LTTNG_TRACKER_VPID
;
918 retval
= lttng_tracker_id_lookup_string(tracker_type
, id
, &value
);
919 if (retval
!= LTTNG_OK
) {
922 tracker_list
= get_id_tracker_list(session
, tracker_type
);
924 return LTTNG_ERR_INVALID
;
926 /* Save list for restore on error. */
927 retval
= lttng_tracker_id_get_list(tracker_list
, &saved_ids
);
928 if (retval
!= LTTNG_OK
) {
929 return LTTNG_ERR_INVALID
;
932 retval
= lttng_tracker_list_add(tracker_list
, id
);
933 if (retval
!= LTTNG_OK
) {
937 id_tracker
= get_id_tracker(session
, tracker_type
);
942 /* Track all ids: destroy tracker if exists. */
943 if (id_tracker
->ht
) {
944 fini_id_tracker(id_tracker
);
945 /* Ensure all apps have session. */
946 should_update_apps
= true;
949 if (!id_tracker
->ht
) {
950 /* Create tracker. */
951 retval
= init_id_tracker(id_tracker
);
952 if (retval
!= LTTNG_OK
) {
953 ERR("Error initializing ID tracker");
956 retval
= id_tracker_add_id(id_tracker
, value
);
957 if (retval
!= LTTNG_OK
) {
958 fini_id_tracker(id_tracker
);
961 /* Remove all apps from session except pid. */
962 should_update_apps
= true;
966 retval
= id_tracker_add_id(id_tracker
, value
);
967 if (retval
!= LTTNG_OK
) {
970 /* Add session to application */
971 switch (tracker_type
) {
972 case LTTNG_TRACKER_VPID
:
973 app
= ust_app_find_by_pid(value
);
975 should_update_apps
= true;
979 should_update_apps
= true;
983 if (should_update_apps
&& session
->active
) {
984 ust_app_global_update_all(session
);
989 if (lttng_tracker_id_set_list(tracker_list
, saved_ids
) != LTTNG_OK
) {
990 ERR("Error on tracker add error handling.\n");
993 lttng_tracker_ids_destroy(saved_ids
);
998 * Called with the session lock held.
1000 int trace_ust_untrack_id(enum lttng_tracker_type tracker_type
,
1001 struct ltt_ust_session
*session
,
1002 const struct lttng_tracker_id
*id
)
1004 int retval
= LTTNG_OK
;
1005 bool should_update_apps
= false;
1006 struct ust_id_tracker
*id_tracker
;
1007 struct lttng_tracker_list
*tracker_list
;
1009 struct lttng_tracker_ids
*saved_ids
;
1011 if (tracker_type
== LTTNG_TRACKER_PID
) {
1012 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
1013 tracker_type
= LTTNG_TRACKER_VPID
;
1016 retval
= lttng_tracker_id_lookup_string(tracker_type
, id
, &value
);
1017 if (retval
!= LTTNG_OK
) {
1021 tracker_list
= get_id_tracker_list(session
, tracker_type
);
1022 if (!tracker_list
) {
1023 return LTTNG_ERR_INVALID
;
1025 /* Save list for restore on error. */
1026 retval
= lttng_tracker_id_get_list(tracker_list
, &saved_ids
);
1027 if (retval
!= LTTNG_OK
) {
1028 return LTTNG_ERR_INVALID
;
1030 /* Remove from list. */
1031 retval
= lttng_tracker_list_remove(tracker_list
, id
);
1032 if (retval
!= LTTNG_OK
) {
1036 id_tracker
= get_id_tracker(session
, tracker_type
);
1042 /* Create empty tracker, replace old tracker. */
1043 struct ust_id_tracker tmp_tracker
;
1045 tmp_tracker
= *id_tracker
;
1046 retval
= init_id_tracker(id_tracker
);
1047 if (retval
!= LTTNG_OK
) {
1048 ERR("Error initializing ID tracker");
1049 /* Rollback operation. */
1050 *id_tracker
= tmp_tracker
;
1053 fini_id_tracker(&tmp_tracker
);
1055 /* Remove session from all applications */
1056 should_update_apps
= true;
1058 struct ust_app
*app
;
1060 if (!id_tracker
->ht
) {
1061 /* No ID being tracked. */
1062 retval
= LTTNG_ERR_ID_NOT_TRACKED
;
1065 /* Remove ID from tracker */
1066 retval
= id_tracker_del_id(id_tracker
, value
);
1067 if (retval
!= LTTNG_OK
) {
1070 switch (tracker_type
) {
1071 case LTTNG_TRACKER_VPID
:
1072 /* Remove session from application. */
1073 app
= ust_app_find_by_pid(value
);
1075 should_update_apps
= true;
1079 /* Keep only apps matching ID. */
1080 should_update_apps
= true;
1083 if (should_update_apps
&& session
->active
) {
1084 ust_app_global_update_all(session
);
1089 if (lttng_tracker_id_set_list(tracker_list
, saved_ids
) != LTTNG_OK
) {
1090 ERR("Error on tracker remove error handling.\n");
1093 lttng_tracker_ids_destroy(saved_ids
);
1098 * Called with session lock held.
1100 int trace_ust_list_tracker_ids(enum lttng_tracker_type tracker_type
,
1101 struct ltt_ust_session
*session
,
1102 struct lttng_tracker_ids
**_ids
)
1105 struct lttng_tracker_list
*tracker_list
;
1107 if (tracker_type
== LTTNG_TRACKER_PID
) {
1108 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
1109 tracker_type
= LTTNG_TRACKER_VPID
;
1112 tracker_list
= get_id_tracker_list(session
, tracker_type
);
1113 if (!tracker_list
) {
1114 ret
= -LTTNG_ERR_INVALID
;
1118 ret
= lttng_tracker_id_get_list(tracker_list
, _ids
);
1119 if (ret
!= LTTNG_OK
) {
1120 ret
= -LTTNG_ERR_INVALID
;
1128 * RCU safe free context structure.
1130 static void destroy_context_rcu(struct rcu_head
*head
)
1132 struct lttng_ht_node_ulong
*node
=
1133 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1134 struct ltt_ust_context
*ctx
=
1135 caa_container_of(node
, struct ltt_ust_context
, node
);
1137 trace_ust_destroy_context(ctx
);
1141 * Cleanup UST context hash table.
1143 static void destroy_contexts(struct lttng_ht
*ht
)
1146 struct lttng_ht_node_ulong
*node
;
1147 struct lttng_ht_iter iter
;
1148 struct ltt_ust_context
*ctx
;
1153 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
1154 /* Remove from ordered list. */
1155 ctx
= caa_container_of(node
, struct ltt_ust_context
, node
);
1156 cds_list_del(&ctx
->list
);
1157 /* Remove from channel's hash table. */
1158 ret
= lttng_ht_del(ht
, &iter
);
1160 call_rcu(&node
->head
, destroy_context_rcu
);
1165 ht_cleanup_push(ht
);
1169 * Cleanup ust event structure.
1171 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
1175 DBG2("Trace destroy UST event %s", event
->attr
.name
);
1176 free(event
->filter_expression
);
1177 free(event
->filter
);
1178 free(event
->exclusion
);
1183 * Cleanup ust context structure.
1185 void trace_ust_destroy_context(struct ltt_ust_context
*ctx
)
1189 if (ctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1190 free(ctx
->ctx
.u
.app_ctx
.provider_name
);
1191 free(ctx
->ctx
.u
.app_ctx
.ctx_name
);
1197 * URCU intermediate call to complete destroy event.
1199 static void destroy_event_rcu(struct rcu_head
*head
)
1201 struct lttng_ht_node_str
*node
=
1202 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1203 struct ltt_ust_event
*event
=
1204 caa_container_of(node
, struct ltt_ust_event
, node
);
1206 trace_ust_destroy_event(event
);
1210 * Cleanup UST events hashtable.
1212 static void destroy_events(struct lttng_ht
*events
)
1215 struct lttng_ht_node_str
*node
;
1216 struct lttng_ht_iter iter
;
1221 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
1222 ret
= lttng_ht_del(events
, &iter
);
1224 call_rcu(&node
->head
, destroy_event_rcu
);
1228 ht_cleanup_push(events
);
1232 * Cleanup ust channel structure.
1234 * Should _NOT_ be called with RCU read lock held.
1236 static void _trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1240 DBG2("Trace destroy UST channel %s", channel
->name
);
1246 * URCU intermediate call to complete destroy channel.
1248 static void destroy_channel_rcu(struct rcu_head
*head
)
1250 struct lttng_ht_node_str
*node
=
1251 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1252 struct ltt_ust_channel
*channel
=
1253 caa_container_of(node
, struct ltt_ust_channel
, node
);
1255 _trace_ust_destroy_channel(channel
);
1258 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1260 /* Destroying all events of the channel */
1261 destroy_events(channel
->events
);
1262 /* Destroying all context of the channel */
1263 destroy_contexts(channel
->ctx
);
1265 call_rcu(&channel
->node
.head
, destroy_channel_rcu
);
1269 * Remove an UST channel from a channel HT.
1271 void trace_ust_delete_channel(struct lttng_ht
*ht
,
1272 struct ltt_ust_channel
*channel
)
1275 struct lttng_ht_iter iter
;
1280 iter
.iter
.node
= &channel
->node
.node
;
1281 ret
= lttng_ht_del(ht
, &iter
);
1286 * Iterate over a hash table containing channels and cleanup safely.
1288 static void destroy_channels(struct lttng_ht
*channels
)
1290 struct lttng_ht_node_str
*node
;
1291 struct lttng_ht_iter iter
;
1296 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
1297 struct ltt_ust_channel
*chan
=
1298 caa_container_of(node
, struct ltt_ust_channel
, node
);
1300 trace_ust_delete_channel(channels
, chan
);
1301 trace_ust_destroy_channel(chan
);
1305 ht_cleanup_push(channels
);
1309 * Cleanup UST global domain.
1311 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
1315 destroy_channels(dom
->channels
);
1319 * Cleanup ust session structure, keeping data required by
1322 * Should *NOT* be called with RCU read-side lock held.
1324 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
1327 struct buffer_reg_uid
*reg
, *sreg
;
1328 struct lttng_ht_iter iter
;
1332 DBG2("Trace UST destroy session %" PRIu64
, session
->id
);
1334 /* Cleaning up UST domain */
1335 destroy_domain_global(&session
->domain_global
);
1338 cds_lfht_for_each_entry(session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
1339 int ret
= lttng_ht_del(session
->agents
, &iter
);
1346 ht_cleanup_push(session
->agents
);
1348 /* Cleanup UID buffer registry object(s). */
1349 cds_list_for_each_entry_safe(reg
, sreg
, &session
->buffer_reg_uid_list
,
1351 cds_list_del(®
->lnode
);
1352 buffer_reg_uid_remove(reg
);
1353 buffer_reg_uid_destroy(reg
, session
->consumer
);
1356 lttng_tracker_list_destroy(session
->tracker_list_vpid
);
1357 lttng_tracker_list_destroy(session
->tracker_list_vuid
);
1358 lttng_tracker_list_destroy(session
->tracker_list_vgid
);
1360 fini_id_tracker(&session
->vpid_tracker
);
1361 fini_id_tracker(&session
->vuid_tracker
);
1362 fini_id_tracker(&session
->vgid_tracker
);
1363 lttng_trace_chunk_put(session
->current_trace_chunk
);
1366 /* Free elements needed by destroy notifiers. */
1367 void trace_ust_free_session(struct ltt_ust_session
*session
)
1369 consumer_output_put(session
->consumer
);