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"
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
;
170 ASSERT_RCU_READ_LOCKED();
172 * If we receive an empty string for channel name, it means the
173 * default channel name is requested.
176 name
= DEFAULT_CHANNEL_NAME
;
178 lttng_ht_lookup(ht
, (void *)name
, &iter
);
179 node
= lttng_ht_iter_get_node_str(&iter
);
184 DBG2("Trace UST channel %s found by name", name
);
186 return caa_container_of(node
, struct ltt_ust_channel
, node
);
189 DBG2("Trace UST channel %s not found by name", name
);
194 * Find the event in the hashtable and return event pointer. RCU read side lock
195 * MUST be acquired before calling this.
197 struct ltt_ust_event
*trace_ust_find_event(struct lttng_ht
*ht
,
198 char *name
, struct lttng_bytecode
*filter
,
199 enum lttng_ust_abi_loglevel_type loglevel_type
, int loglevel_value
,
200 struct lttng_event_exclusion
*exclusion
)
202 struct lttng_ht_node_str
*node
;
203 struct lttng_ht_iter iter
;
204 struct ltt_ust_ht_key key
;
208 ASSERT_RCU_READ_LOCKED();
212 key
.loglevel_type
= loglevel_type
;
213 key
.loglevel_value
= loglevel_value
;
214 key
.exclusion
= exclusion
;
216 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
217 trace_ust_ht_match_event
, &key
, &iter
.iter
);
218 node
= lttng_ht_iter_get_node_str(&iter
);
223 DBG2("Trace UST event %s found", key
.name
);
225 return caa_container_of(node
, struct ltt_ust_event
, node
);
228 DBG2("Trace UST event %s NOT found", key
.name
);
233 * Lookup an agent in the session agents hash table by domain type and return
234 * the object if found else NULL.
236 * RCU read side lock must be acquired before calling and only released
237 * once the agent is no longer in scope or being used.
239 struct agent
*trace_ust_find_agent(struct ltt_ust_session
*session
,
240 enum lttng_domain_type domain_type
)
242 struct agent
*agt
= NULL
;
243 struct lttng_ht_node_u64
*node
;
244 struct lttng_ht_iter iter
;
247 LTTNG_ASSERT(session
);
249 DBG3("Trace ust agent lookup for domain %d", domain_type
);
253 lttng_ht_lookup(session
->agents
, &key
, &iter
);
254 node
= lttng_ht_iter_get_node_u64(&iter
);
258 agt
= caa_container_of(node
, struct agent
, node
);
265 * Allocate and initialize a ust session data structure.
267 * Return pointer to structure or NULL.
269 struct ltt_ust_session
*trace_ust_create_session(uint64_t session_id
)
271 struct ltt_ust_session
*lus
;
273 /* Allocate a new ltt ust session */
274 lus
= (ltt_ust_session
*) zmalloc(sizeof(struct ltt_ust_session
));
276 PERROR("create ust session zmalloc");
280 /* Init data structure */
281 lus
->id
= session_id
;
284 /* Set default metadata channel attribute. */
285 lus
->metadata_attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
286 lus
->metadata_attr
.subbuf_size
= default_get_metadata_subbuf_size();
287 lus
->metadata_attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
288 lus
->metadata_attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
289 lus
->metadata_attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;
290 lus
->metadata_attr
.output
= LTTNG_UST_ABI_MMAP
;
293 * Default buffer type. This can be changed through an enable channel
294 * requesting a different type. Note that this can only be changed once
295 * during the session lifetime which is at the first enable channel and
296 * only before start. The flag buffer_type_changed indicates the status.
298 lus
->buffer_type
= LTTNG_BUFFER_PER_UID
;
299 /* Once set to 1, the buffer_type is immutable for the session. */
300 lus
->buffer_type_changed
= 0;
301 /* Init it in case it get used after allocation. */
302 CDS_INIT_LIST_HEAD(&lus
->buffer_reg_uid_list
);
304 /* Alloc UST global domain channels' HT */
305 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
306 /* Alloc agent hash table. */
307 lus
->agents
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
309 lus
->tracker_vpid
= process_attr_tracker_create();
310 if (!lus
->tracker_vpid
) {
313 lus
->tracker_vuid
= process_attr_tracker_create();
314 if (!lus
->tracker_vuid
) {
317 lus
->tracker_vgid
= process_attr_tracker_create();
318 if (!lus
->tracker_vgid
) {
321 lus
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
322 if (lus
->consumer
== NULL
) {
326 DBG2("UST trace session create successful");
331 process_attr_tracker_destroy(lus
->tracker_vpid
);
332 process_attr_tracker_destroy(lus
->tracker_vuid
);
333 process_attr_tracker_destroy(lus
->tracker_vgid
);
334 lttng_ht_destroy(lus
->domain_global
.channels
);
335 lttng_ht_destroy(lus
->agents
);
342 * Allocate and initialize a ust channel data structure.
344 * Return pointer to structure or NULL.
346 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
347 enum lttng_domain_type domain
)
349 struct ltt_ust_channel
*luc
;
353 luc
= (ltt_ust_channel
*) zmalloc(sizeof(struct ltt_ust_channel
));
355 PERROR("ltt_ust_channel zmalloc");
359 luc
->domain
= domain
;
361 /* Copy UST channel attributes */
362 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
363 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
364 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
365 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
366 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
367 luc
->attr
.output
= (enum lttng_ust_abi_output
) chan
->attr
.output
;
368 luc
->monitor_timer_interval
= ((struct lttng_channel_extended
*)
369 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
370 luc
->attr
.u
.s
.blocking_timeout
= ((struct lttng_channel_extended
*)
371 chan
->attr
.extended
.ptr
)->blocking_timeout
;
373 /* Translate to UST output enum */
374 switch (luc
->attr
.output
) {
376 luc
->attr
.output
= LTTNG_UST_ABI_MMAP
;
381 * If we receive an empty string for channel name, it means the
382 * default channel name is requested.
384 if (chan
->name
[0] == '\0') {
385 strncpy(luc
->name
, DEFAULT_CHANNEL_NAME
, sizeof(luc
->name
));
387 /* Copy channel name */
388 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
390 luc
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
393 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
394 CDS_INIT_LIST_HEAD(&luc
->ctx_list
);
396 /* Alloc hash tables */
397 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
398 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
400 /* On-disk circular buffer parameters */
401 luc
->tracefile_size
= chan
->attr
.tracefile_size
;
402 luc
->tracefile_count
= chan
->attr
.tracefile_count
;
404 DBG2("Trace UST channel %s created", luc
->name
);
411 * Validates an exclusion list.
413 * Returns 0 if valid, negative value if invalid.
415 static int validate_exclusion(struct lttng_event_exclusion
*exclusion
)
420 LTTNG_ASSERT(exclusion
);
422 for (i
= 0; i
< exclusion
->count
; ++i
) {
425 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
427 for (j
= 0; j
< i
; ++j
) {
429 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, j
);
431 if (!strncmp(name_a
, name_b
, LTTNG_SYMBOL_NAME_LEN
)) {
444 * Allocate and initialize a ust event. Set name and event type.
445 * We own filter_expression, filter, and exclusion.
447 * Return an lttng_error_code
449 enum lttng_error_code
trace_ust_create_event(struct lttng_event
*ev
,
450 char *filter_expression
,
451 struct lttng_bytecode
*filter
,
452 struct lttng_event_exclusion
*exclusion
,
454 struct ltt_ust_event
**ust_event
)
456 struct ltt_ust_event
*local_ust_event
;
457 enum lttng_error_code ret
= LTTNG_OK
;
461 if (exclusion
&& validate_exclusion(exclusion
)) {
462 ret
= LTTNG_ERR_INVALID
;
466 local_ust_event
= (ltt_ust_event
*) zmalloc(sizeof(struct ltt_ust_event
));
467 if (local_ust_event
== NULL
) {
468 PERROR("ust event zmalloc");
469 ret
= LTTNG_ERR_NOMEM
;
473 local_ust_event
->internal
= internal_event
;
476 case LTTNG_EVENT_PROBE
:
477 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_PROBE
;
479 case LTTNG_EVENT_FUNCTION
:
480 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_FUNCTION
;
482 case LTTNG_EVENT_FUNCTION_ENTRY
:
483 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_FUNCTION
;
485 case LTTNG_EVENT_TRACEPOINT
:
486 local_ust_event
->attr
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
489 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
490 ret
= LTTNG_ERR_INVALID
;
491 goto error_free_event
;
494 /* Copy event name */
495 strncpy(local_ust_event
->attr
.name
, ev
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
496 local_ust_event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
498 switch (ev
->loglevel_type
) {
499 case LTTNG_EVENT_LOGLEVEL_ALL
:
500 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
501 local_ust_event
->attr
.loglevel
= -1; /* Force to -1 */
503 case LTTNG_EVENT_LOGLEVEL_RANGE
:
504 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
505 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
507 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
508 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
509 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
512 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
513 ret
= LTTNG_ERR_INVALID
;
514 goto error_free_event
;
518 local_ust_event
->filter_expression
= filter_expression
;
519 local_ust_event
->filter
= filter
;
520 local_ust_event
->exclusion
= exclusion
;
523 lttng_ht_node_init_str(&local_ust_event
->node
, local_ust_event
->attr
.name
);
525 DBG2("Trace UST event %s, loglevel (%d,%d) created",
526 local_ust_event
->attr
.name
, local_ust_event
->attr
.loglevel_type
,
527 local_ust_event
->attr
.loglevel
);
529 *ust_event
= local_ust_event
;
534 free(local_ust_event
);
536 free(filter_expression
);
543 int trace_ust_context_type_event_to_ust(
544 enum lttng_event_context_type type
)
549 case LTTNG_EVENT_CONTEXT_VTID
:
550 utype
= LTTNG_UST_ABI_CONTEXT_VTID
;
552 case LTTNG_EVENT_CONTEXT_VPID
:
553 utype
= LTTNG_UST_ABI_CONTEXT_VPID
;
555 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
556 utype
= LTTNG_UST_ABI_CONTEXT_PTHREAD_ID
;
558 case LTTNG_EVENT_CONTEXT_PROCNAME
:
559 utype
= LTTNG_UST_ABI_CONTEXT_PROCNAME
;
561 case LTTNG_EVENT_CONTEXT_IP
:
562 utype
= LTTNG_UST_ABI_CONTEXT_IP
;
564 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
565 if (!lttng_ust_ctl_has_perf_counters()) {
567 WARN("Perf counters not implemented in UST");
569 utype
= LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
;
572 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
573 utype
= LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
;
575 case LTTNG_EVENT_CONTEXT_CGROUP_NS
:
576 utype
= LTTNG_UST_ABI_CONTEXT_CGROUP_NS
;
578 case LTTNG_EVENT_CONTEXT_IPC_NS
:
579 utype
= LTTNG_UST_ABI_CONTEXT_IPC_NS
;
581 case LTTNG_EVENT_CONTEXT_MNT_NS
:
582 utype
= LTTNG_UST_ABI_CONTEXT_MNT_NS
;
584 case LTTNG_EVENT_CONTEXT_NET_NS
:
585 utype
= LTTNG_UST_ABI_CONTEXT_NET_NS
;
587 case LTTNG_EVENT_CONTEXT_PID_NS
:
588 utype
= LTTNG_UST_ABI_CONTEXT_PID_NS
;
590 case LTTNG_EVENT_CONTEXT_TIME_NS
:
591 utype
= LTTNG_UST_ABI_CONTEXT_TIME_NS
;
593 case LTTNG_EVENT_CONTEXT_USER_NS
:
594 utype
= LTTNG_UST_ABI_CONTEXT_USER_NS
;
596 case LTTNG_EVENT_CONTEXT_UTS_NS
:
597 utype
= LTTNG_UST_ABI_CONTEXT_UTS_NS
;
599 case LTTNG_EVENT_CONTEXT_VUID
:
600 utype
= LTTNG_UST_ABI_CONTEXT_VUID
;
602 case LTTNG_EVENT_CONTEXT_VEUID
:
603 utype
= LTTNG_UST_ABI_CONTEXT_VEUID
;
605 case LTTNG_EVENT_CONTEXT_VSUID
:
606 utype
= LTTNG_UST_ABI_CONTEXT_VSUID
;
608 case LTTNG_EVENT_CONTEXT_VGID
:
609 utype
= LTTNG_UST_ABI_CONTEXT_VGID
;
611 case LTTNG_EVENT_CONTEXT_VEGID
:
612 utype
= LTTNG_UST_ABI_CONTEXT_VEGID
;
614 case LTTNG_EVENT_CONTEXT_VSGID
:
615 utype
= LTTNG_UST_ABI_CONTEXT_VSGID
;
625 * Return 1 if contexts match, 0 otherwise.
627 int trace_ust_match_context(const struct ltt_ust_context
*uctx
,
628 const struct lttng_event_context
*ctx
)
632 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
636 if (uctx
->ctx
.ctx
!= utype
) {
640 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
641 if (uctx
->ctx
.u
.perf_counter
.type
642 != ctx
->u
.perf_counter
.type
) {
645 if (uctx
->ctx
.u
.perf_counter
.config
646 != ctx
->u
.perf_counter
.config
) {
649 if (strncmp(uctx
->ctx
.u
.perf_counter
.name
,
650 ctx
->u
.perf_counter
.name
,
651 LTTNG_UST_ABI_SYM_NAME_LEN
)) {
655 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
656 LTTNG_ASSERT(uctx
->ctx
.u
.app_ctx
.provider_name
);
657 LTTNG_ASSERT(uctx
->ctx
.u
.app_ctx
.ctx_name
);
658 if (strcmp(uctx
->ctx
.u
.app_ctx
.provider_name
,
659 ctx
->u
.app_ctx
.provider_name
) ||
660 strcmp(uctx
->ctx
.u
.app_ctx
.ctx_name
,
661 ctx
->u
.app_ctx
.ctx_name
)) {
672 * Allocate and initialize an UST context.
674 * Return pointer to structure or NULL.
676 struct ltt_ust_context
*trace_ust_create_context(
677 const struct lttng_event_context
*ctx
)
679 struct ltt_ust_context
*uctx
= NULL
;
684 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
686 ERR("Invalid UST context");
690 uctx
= (ltt_ust_context
*) zmalloc(sizeof(struct ltt_ust_context
));
692 PERROR("zmalloc ltt_ust_context");
696 uctx
->ctx
.ctx
= (enum lttng_ust_abi_context_type
) utype
;
698 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
699 uctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
700 uctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
701 strncpy(uctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
702 LTTNG_UST_ABI_SYM_NAME_LEN
);
703 uctx
->ctx
.u
.perf_counter
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
705 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
707 char *provider_name
= NULL
, *ctx_name
= NULL
;
709 provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
710 if (!provider_name
) {
713 uctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
715 ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
719 uctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
725 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
729 trace_ust_destroy_context(uctx
);
733 static void destroy_id_tracker_node_rcu(struct rcu_head
*head
)
735 struct ust_id_tracker_node
*tracker_node
= caa_container_of(
736 head
, struct ust_id_tracker_node
, node
.head
);
740 static void destroy_id_tracker_node(struct ust_id_tracker_node
*tracker_node
)
742 call_rcu(&tracker_node
->node
.head
, destroy_id_tracker_node_rcu
);
745 static int init_id_tracker(struct ust_id_tracker
*id_tracker
)
749 id_tracker
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
750 if (!id_tracker
->ht
) {
751 ret
= LTTNG_ERR_NOMEM
;
760 * Teardown id tracker content, but don't free id_tracker object.
762 static void fini_id_tracker(struct ust_id_tracker
*id_tracker
)
764 struct ust_id_tracker_node
*tracker_node
;
765 struct lttng_ht_iter iter
;
767 if (!id_tracker
->ht
) {
771 cds_lfht_for_each_entry (id_tracker
->ht
->ht
, &iter
.iter
, tracker_node
,
773 int ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
776 destroy_id_tracker_node(tracker_node
);
779 lttng_ht_destroy(id_tracker
->ht
);
780 id_tracker
->ht
= NULL
;
783 static struct ust_id_tracker_node
*id_tracker_lookup(
784 struct ust_id_tracker
*id_tracker
,
786 struct lttng_ht_iter
*iter
)
788 unsigned long _id
= (unsigned long) id
;
789 struct lttng_ht_node_ulong
*node
;
791 lttng_ht_lookup(id_tracker
->ht
, (void *) _id
, iter
);
792 node
= lttng_ht_iter_get_node_ulong(iter
);
794 return caa_container_of(node
, struct ust_id_tracker_node
, node
);
800 static int id_tracker_add_id(struct ust_id_tracker
*id_tracker
, int id
)
802 int retval
= LTTNG_OK
;
803 struct ust_id_tracker_node
*tracker_node
;
804 struct lttng_ht_iter iter
;
807 retval
= LTTNG_ERR_INVALID
;
810 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
812 /* Already exists. */
813 retval
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
816 tracker_node
= (ust_id_tracker_node
*) zmalloc(sizeof(*tracker_node
));
818 retval
= LTTNG_ERR_NOMEM
;
821 lttng_ht_node_init_ulong(&tracker_node
->node
, (unsigned long) id
);
822 lttng_ht_add_unique_ulong(id_tracker
->ht
, &tracker_node
->node
);
827 static int id_tracker_del_id(struct ust_id_tracker
*id_tracker
, int id
)
829 int retval
= LTTNG_OK
, ret
;
830 struct ust_id_tracker_node
*tracker_node
;
831 struct lttng_ht_iter iter
;
834 retval
= LTTNG_ERR_INVALID
;
837 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
840 retval
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
843 ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
846 destroy_id_tracker_node(tracker_node
);
851 static struct ust_id_tracker
*get_id_tracker(struct ltt_ust_session
*session
,
852 enum lttng_process_attr process_attr
)
854 switch (process_attr
) {
855 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
856 return &session
->vpid_tracker
;
857 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
858 return &session
->vuid_tracker
;
859 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
860 return &session
->vgid_tracker
;
866 static struct process_attr_tracker
*_trace_ust_get_process_attr_tracker(
867 struct ltt_ust_session
*session
,
868 enum lttng_process_attr process_attr
)
870 switch (process_attr
) {
871 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
872 return session
->tracker_vpid
;
873 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
874 return session
->tracker_vuid
;
875 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
876 return session
->tracker_vgid
;
882 const struct process_attr_tracker
*trace_ust_get_process_attr_tracker(
883 struct ltt_ust_session
*session
,
884 enum lttng_process_attr process_attr
)
886 return (const struct process_attr_tracker
*)
887 _trace_ust_get_process_attr_tracker(
888 session
, process_attr
);
892 * The session lock is held when calling this function.
894 int trace_ust_id_tracker_lookup(enum lttng_process_attr process_attr
,
895 struct ltt_ust_session
*session
,
898 struct lttng_ht_iter iter
;
899 struct ust_id_tracker
*id_tracker
;
901 id_tracker
= get_id_tracker(session
, process_attr
);
905 if (!id_tracker
->ht
) {
908 if (id_tracker_lookup(id_tracker
, id
, &iter
)) {
915 * Called with the session lock held.
917 enum lttng_error_code
trace_ust_process_attr_tracker_set_tracking_policy(
918 struct ltt_ust_session
*session
,
919 enum lttng_process_attr process_attr
,
920 enum lttng_tracking_policy policy
)
923 enum lttng_error_code ret_code
= LTTNG_OK
;
924 struct ust_id_tracker
*id_tracker
=
925 get_id_tracker(session
, process_attr
);
926 struct process_attr_tracker
*tracker
=
927 _trace_ust_get_process_attr_tracker(
928 session
, process_attr
);
929 bool should_update_apps
= false;
930 enum lttng_tracking_policy previous_policy
;
933 ret_code
= LTTNG_ERR_INVALID
;
937 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
938 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
940 ret_code
= LTTNG_ERR_UNK
;
944 if (previous_policy
== policy
) {
949 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
950 /* Track all values: destroy tracker if exists. */
951 if (id_tracker
->ht
) {
952 fini_id_tracker(id_tracker
);
953 /* Ensure all apps have session. */
954 should_update_apps
= true;
957 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
958 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
960 fini_id_tracker(id_tracker
);
961 ret_code
= (lttng_error_code
) init_id_tracker(id_tracker
);
962 if (ret_code
!= LTTNG_OK
) {
963 ERR("Error initializing ID tracker");
966 /* Remove all apps from session. */
967 should_update_apps
= true;
972 if (should_update_apps
&& session
->active
) {
973 ust_app_global_update_all(session
);
979 /* Called with the session lock held. */
980 enum lttng_error_code
trace_ust_process_attr_tracker_inclusion_set_add_value(
981 struct ltt_ust_session
*session
,
982 enum lttng_process_attr process_attr
,
983 const struct process_attr_value
*value
)
985 enum lttng_error_code ret_code
= LTTNG_OK
;
986 bool should_update_apps
= false;
987 struct ust_id_tracker
*id_tracker
=
988 get_id_tracker(session
, process_attr
);
989 struct process_attr_tracker
*tracker
;
991 enum process_attr_tracker_status status
;
995 * Convert process attribute tracker value to the integral
996 * representation required by the kern-ctl API.
998 switch (process_attr
) {
999 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1000 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1001 integral_value
= (int) value
->value
.pid
;
1003 case LTTNG_PROCESS_ATTR_USER_ID
:
1004 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1005 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1008 ret_code
= utils_user_id_from_name(
1009 value
->value
.user_name
, &uid
);
1010 if (ret_code
!= LTTNG_OK
) {
1013 integral_value
= (int) uid
;
1015 integral_value
= (int) value
->value
.uid
;
1018 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1019 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1020 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1023 ret_code
= utils_group_id_from_name(
1024 value
->value
.group_name
, &gid
);
1025 if (ret_code
!= LTTNG_OK
) {
1028 integral_value
= (int) gid
;
1030 integral_value
= (int) value
->value
.gid
;
1034 ret_code
= LTTNG_ERR_INVALID
;
1038 tracker
= _trace_ust_get_process_attr_tracker(session
, process_attr
);
1040 ret_code
= LTTNG_ERR_INVALID
;
1044 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
1045 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1047 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
1048 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1050 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1051 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1053 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1055 ret_code
= LTTNG_ERR_UNK
;
1061 DBG("User space track %s %d for session id %" PRIu64
,
1062 lttng_process_attr_to_string(process_attr
),
1063 integral_value
, session
->id
);
1065 ret_code
= (lttng_error_code
) id_tracker_add_id(id_tracker
, integral_value
);
1066 if (ret_code
!= LTTNG_OK
) {
1069 /* Add session to application */
1070 switch (process_attr
) {
1071 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1072 app
= ust_app_find_by_pid(integral_value
);
1074 should_update_apps
= true;
1078 should_update_apps
= true;
1081 if (should_update_apps
&& session
->active
) {
1082 ust_app_global_update_all(session
);
1088 /* Called with the session lock held. */
1089 enum lttng_error_code
trace_ust_process_attr_tracker_inclusion_set_remove_value(
1090 struct ltt_ust_session
*session
,
1091 enum lttng_process_attr process_attr
,
1092 const struct process_attr_value
*value
)
1094 enum lttng_error_code ret_code
= LTTNG_OK
;
1095 bool should_update_apps
= false;
1096 struct ust_id_tracker
*id_tracker
=
1097 get_id_tracker(session
, process_attr
);
1098 struct process_attr_tracker
*tracker
;
1100 enum process_attr_tracker_status status
;
1101 struct ust_app
*app
;
1104 * Convert process attribute tracker value to the integral
1105 * representation required by the kern-ctl API.
1107 switch (process_attr
) {
1108 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1109 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1110 integral_value
= (int) value
->value
.pid
;
1112 case LTTNG_PROCESS_ATTR_USER_ID
:
1113 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1114 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1117 ret_code
= utils_user_id_from_name(
1118 value
->value
.user_name
, &uid
);
1119 if (ret_code
!= LTTNG_OK
) {
1122 integral_value
= (int) uid
;
1124 integral_value
= (int) value
->value
.uid
;
1127 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1128 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1129 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1132 ret_code
= utils_group_id_from_name(
1133 value
->value
.group_name
, &gid
);
1134 if (ret_code
!= LTTNG_OK
) {
1137 integral_value
= (int) gid
;
1139 integral_value
= (int) value
->value
.gid
;
1143 ret_code
= LTTNG_ERR_INVALID
;
1147 tracker
= _trace_ust_get_process_attr_tracker(session
, process_attr
);
1149 ret_code
= LTTNG_ERR_INVALID
;
1153 status
= process_attr_tracker_inclusion_set_remove_value(
1155 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1157 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1158 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1160 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1161 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1163 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1165 ret_code
= LTTNG_ERR_UNK
;
1171 DBG("User space untrack %s %d for session id %" PRIu64
,
1172 lttng_process_attr_to_string(process_attr
),
1173 integral_value
, session
->id
);
1175 ret_code
= (lttng_error_code
) id_tracker_del_id(id_tracker
, integral_value
);
1176 if (ret_code
!= LTTNG_OK
) {
1179 /* Add session to application */
1180 switch (process_attr
) {
1181 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1182 app
= ust_app_find_by_pid(integral_value
);
1184 should_update_apps
= true;
1188 should_update_apps
= true;
1191 if (should_update_apps
&& session
->active
) {
1192 ust_app_global_update_all(session
);
1199 * RCU safe free context structure.
1201 static void destroy_context_rcu(struct rcu_head
*head
)
1203 struct lttng_ht_node_ulong
*node
=
1204 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1205 struct ltt_ust_context
*ctx
=
1206 caa_container_of(node
, struct ltt_ust_context
, node
);
1208 trace_ust_destroy_context(ctx
);
1212 * Cleanup UST context hash table.
1214 static void destroy_contexts(struct lttng_ht
*ht
)
1217 struct lttng_ht_node_ulong
*node
;
1218 struct lttng_ht_iter iter
;
1219 struct ltt_ust_context
*ctx
;
1224 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
1225 /* Remove from ordered list. */
1226 ctx
= caa_container_of(node
, struct ltt_ust_context
, node
);
1227 cds_list_del(&ctx
->list
);
1228 /* Remove from channel's hash table. */
1229 ret
= lttng_ht_del(ht
, &iter
);
1231 call_rcu(&node
->head
, destroy_context_rcu
);
1236 lttng_ht_destroy(ht
);
1240 * Cleanup ust event structure.
1242 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
1244 LTTNG_ASSERT(event
);
1246 DBG2("Trace destroy UST event %s", event
->attr
.name
);
1247 free(event
->filter_expression
);
1248 free(event
->filter
);
1249 free(event
->exclusion
);
1254 * Cleanup ust context structure.
1256 void trace_ust_destroy_context(struct ltt_ust_context
*ctx
)
1260 if (ctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1261 free(ctx
->ctx
.u
.app_ctx
.provider_name
);
1262 free(ctx
->ctx
.u
.app_ctx
.ctx_name
);
1268 * URCU intermediate call to complete destroy event.
1270 static void destroy_event_rcu(struct rcu_head
*head
)
1272 struct lttng_ht_node_str
*node
=
1273 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1274 struct ltt_ust_event
*event
=
1275 caa_container_of(node
, struct ltt_ust_event
, node
);
1277 trace_ust_destroy_event(event
);
1281 * Cleanup UST events hashtable.
1283 static void destroy_events(struct lttng_ht
*events
)
1286 struct lttng_ht_node_str
*node
;
1287 struct lttng_ht_iter iter
;
1289 LTTNG_ASSERT(events
);
1292 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
1293 ret
= lttng_ht_del(events
, &iter
);
1295 call_rcu(&node
->head
, destroy_event_rcu
);
1299 lttng_ht_destroy(events
);
1303 * Cleanup ust channel structure.
1305 * Should _NOT_ be called with RCU read lock held.
1307 static void _trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1309 LTTNG_ASSERT(channel
);
1311 DBG2("Trace destroy UST channel %s", channel
->name
);
1317 * URCU intermediate call to complete destroy channel.
1319 static void destroy_channel_rcu(struct rcu_head
*head
)
1321 struct lttng_ht_node_str
*node
=
1322 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1323 struct ltt_ust_channel
*channel
=
1324 caa_container_of(node
, struct ltt_ust_channel
, node
);
1326 _trace_ust_destroy_channel(channel
);
1329 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1331 /* Destroying all events of the channel */
1332 destroy_events(channel
->events
);
1333 /* Destroying all context of the channel */
1334 destroy_contexts(channel
->ctx
);
1336 call_rcu(&channel
->node
.head
, destroy_channel_rcu
);
1340 * Remove an UST channel from a channel HT.
1342 void trace_ust_delete_channel(struct lttng_ht
*ht
,
1343 struct ltt_ust_channel
*channel
)
1346 struct lttng_ht_iter iter
;
1349 LTTNG_ASSERT(channel
);
1351 iter
.iter
.node
= &channel
->node
.node
;
1352 ret
= lttng_ht_del(ht
, &iter
);
1357 * Iterate over a hash table containing channels and cleanup safely.
1359 static void destroy_channels(struct lttng_ht
*channels
)
1361 struct lttng_ht_node_str
*node
;
1362 struct lttng_ht_iter iter
;
1364 LTTNG_ASSERT(channels
);
1367 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
1368 struct ltt_ust_channel
*chan
=
1369 caa_container_of(node
, struct ltt_ust_channel
, node
);
1371 trace_ust_delete_channel(channels
, chan
);
1372 trace_ust_destroy_channel(chan
);
1376 lttng_ht_destroy(channels
);
1380 * Cleanup UST global domain.
1382 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
1386 destroy_channels(dom
->channels
);
1390 * Cleanup ust session structure, keeping data required by
1393 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
1396 struct buffer_reg_uid
*reg
, *sreg
;
1397 struct lttng_ht_iter iter
;
1399 LTTNG_ASSERT(session
);
1401 DBG2("Trace UST destroy session %" PRIu64
, session
->id
);
1403 /* Cleaning up UST domain */
1404 destroy_domain_global(&session
->domain_global
);
1407 cds_lfht_for_each_entry(session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
1408 int ret
= lttng_ht_del(session
->agents
, &iter
);
1415 lttng_ht_destroy(session
->agents
);
1417 /* Cleanup UID buffer registry object(s). */
1418 cds_list_for_each_entry_safe(reg
, sreg
, &session
->buffer_reg_uid_list
,
1420 cds_list_del(®
->lnode
);
1421 buffer_reg_uid_remove(reg
);
1422 buffer_reg_uid_destroy(reg
, session
->consumer
);
1425 process_attr_tracker_destroy(session
->tracker_vpid
);
1426 process_attr_tracker_destroy(session
->tracker_vuid
);
1427 process_attr_tracker_destroy(session
->tracker_vgid
);
1429 fini_id_tracker(&session
->vpid_tracker
);
1430 fini_id_tracker(&session
->vuid_tracker
);
1431 fini_id_tracker(&session
->vgid_tracker
);
1432 lttng_trace_chunk_put(session
->current_trace_chunk
);
1435 /* Free elements needed by destroy notifiers. */
1436 void trace_ust_free_session(struct ltt_ust_session
*session
)
1438 consumer_output_put(session
->consumer
);