2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/trace-chunk.h>
30 #include "buffer-registry.h"
31 #include "trace-ust.h"
37 * Match function for the events hash table lookup.
39 * Matches by name only. Used by the disable command.
41 int trace_ust_ht_match_event_by_name(struct cds_lfht_node
*node
,
44 struct ltt_ust_event
*event
;
50 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
54 if (strncmp(event
->attr
.name
, name
, sizeof(event
->attr
.name
)) != 0) {
66 * Match function for the hash table lookup.
68 * It matches an ust event based on three attributes which are the event name,
69 * the filter bytecode and the loglevel.
71 int trace_ust_ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
73 struct ltt_ust_event
*event
;
74 const struct ltt_ust_ht_key
*key
;
75 int ev_loglevel_value
;
81 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
83 ev_loglevel_value
= event
->attr
.loglevel
;
85 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
88 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
92 /* Event loglevel value and type. */
93 ll_match
= loglevels_match(event
->attr
.loglevel_type
,
94 ev_loglevel_value
, key
->loglevel_type
,
95 key
->loglevel_value
, LTTNG_UST_LOGLEVEL_ALL
);
101 /* Only one of the filters is NULL, fail. */
102 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
106 if (key
->filter
&& event
->filter
) {
107 /* Both filters exists, check length followed by the bytecode. */
108 if (event
->filter
->len
!= key
->filter
->len
||
109 memcmp(event
->filter
->data
, key
->filter
->data
,
110 event
->filter
->len
) != 0) {
115 /* If only one of the exclusions is NULL, fail. */
116 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
120 if (key
->exclusion
&& event
->exclusion
) {
123 /* Check exclusion counts first. */
124 if (event
->exclusion
->count
!= key
->exclusion
->count
) {
128 /* Compare names individually. */
129 for (i
= 0; i
< event
->exclusion
->count
; ++i
) {
132 const char *name_ev
=
133 LTTNG_EVENT_EXCLUSION_NAME_AT(
134 event
->exclusion
, i
);
137 * Compare this exclusion name to all the key's
140 for (j
= 0; j
< key
->exclusion
->count
; ++j
) {
141 const char *name_key
=
142 LTTNG_EVENT_EXCLUSION_NAME_AT(
145 if (!strncmp(name_ev
, name_key
,
146 LTTNG_SYMBOL_NAME_LEN
)) {
154 * If the current exclusion name was not found amongst
155 * the key's exclusion names, then there's no match.
170 * Find the channel in the hashtable and return channel pointer. RCU read side
171 * lock MUST be acquired before calling this.
173 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct lttng_ht
*ht
,
176 struct lttng_ht_node_str
*node
;
177 struct lttng_ht_iter iter
;
180 * If we receive an empty string for channel name, it means the
181 * default channel name is requested.
184 name
= DEFAULT_CHANNEL_NAME
;
186 lttng_ht_lookup(ht
, (void *)name
, &iter
);
187 node
= lttng_ht_iter_get_node_str(&iter
);
192 DBG2("Trace UST channel %s found by name", name
);
194 return caa_container_of(node
, struct ltt_ust_channel
, node
);
197 DBG2("Trace UST channel %s not found by name", name
);
202 * Find the event in the hashtable and return event pointer. RCU read side lock
203 * MUST be acquired before calling this.
205 struct ltt_ust_event
*trace_ust_find_event(struct lttng_ht
*ht
,
206 char *name
, struct lttng_filter_bytecode
*filter
,
207 enum lttng_ust_loglevel_type loglevel_type
, int loglevel_value
,
208 struct lttng_event_exclusion
*exclusion
)
210 struct lttng_ht_node_str
*node
;
211 struct lttng_ht_iter iter
;
212 struct ltt_ust_ht_key key
;
219 key
.loglevel_type
= loglevel_type
;
220 key
.loglevel_value
= loglevel_value
;
221 key
.exclusion
= exclusion
;
223 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
224 trace_ust_ht_match_event
, &key
, &iter
.iter
);
225 node
= lttng_ht_iter_get_node_str(&iter
);
230 DBG2("Trace UST event %s found", key
.name
);
232 return caa_container_of(node
, struct ltt_ust_event
, node
);
235 DBG2("Trace UST event %s NOT found", key
.name
);
240 * Lookup an agent in the session agents hash table by domain type and return
241 * the object if found else NULL.
243 * RCU read side lock must be acquired before calling and only released
244 * once the agent is no longer in scope or being used.
246 struct agent
*trace_ust_find_agent(struct ltt_ust_session
*session
,
247 enum lttng_domain_type domain_type
)
249 struct agent
*agt
= NULL
;
250 struct lttng_ht_node_u64
*node
;
251 struct lttng_ht_iter iter
;
256 DBG3("Trace ust agent lookup for domain %d", domain_type
);
260 lttng_ht_lookup(session
->agents
, &key
, &iter
);
261 node
= lttng_ht_iter_get_node_u64(&iter
);
265 agt
= caa_container_of(node
, struct agent
, node
);
272 * Allocate and initialize a ust session data structure.
274 * Return pointer to structure or NULL.
276 struct ltt_ust_session
*trace_ust_create_session(uint64_t session_id
)
278 struct ltt_ust_session
*lus
;
280 /* Allocate a new ltt ust session */
281 lus
= zmalloc(sizeof(struct ltt_ust_session
));
283 PERROR("create ust session zmalloc");
287 /* Init data structure */
288 lus
->id
= session_id
;
291 /* Set default metadata channel attribute. */
292 lus
->metadata_attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
293 lus
->metadata_attr
.subbuf_size
= default_get_metadata_subbuf_size();
294 lus
->metadata_attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
295 lus
->metadata_attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
296 lus
->metadata_attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;
297 lus
->metadata_attr
.output
= LTTNG_UST_MMAP
;
300 * Default buffer type. This can be changed through an enable channel
301 * requesting a different type. Note that this can only be changed once
302 * during the session lifetime which is at the first enable channel and
303 * only before start. The flag buffer_type_changed indicates the status.
305 lus
->buffer_type
= LTTNG_BUFFER_PER_UID
;
306 /* Once set to 1, the buffer_type is immutable for the session. */
307 lus
->buffer_type_changed
= 0;
308 /* Init it in case it get used after allocation. */
309 CDS_INIT_LIST_HEAD(&lus
->buffer_reg_uid_list
);
311 /* Alloc UST global domain channels' HT */
312 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
313 /* Alloc agent hash table. */
314 lus
->agents
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
316 lus
->tracker_list_vpid
= lttng_tracker_list_create();
317 if (!lus
->tracker_list_vpid
) {
320 lus
->tracker_list_vuid
= lttng_tracker_list_create();
321 if (!lus
->tracker_list_vuid
) {
324 lus
->tracker_list_vgid
= lttng_tracker_list_create();
325 if (!lus
->tracker_list_vgid
) {
328 lus
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
329 if (lus
->consumer
== NULL
) {
333 DBG2("UST trace session create successful");
338 lttng_tracker_list_destroy(lus
->tracker_list_vpid
);
339 lttng_tracker_list_destroy(lus
->tracker_list_vuid
);
340 lttng_tracker_list_destroy(lus
->tracker_list_vgid
);
341 ht_cleanup_push(lus
->domain_global
.channels
);
342 ht_cleanup_push(lus
->agents
);
349 * Allocate and initialize a ust channel data structure.
351 * Return pointer to structure or NULL.
353 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
354 enum lttng_domain_type domain
)
356 struct ltt_ust_channel
*luc
;
360 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
362 PERROR("ltt_ust_channel zmalloc");
366 luc
->domain
= domain
;
368 /* Copy UST channel attributes */
369 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
370 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
371 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
372 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
373 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
374 luc
->attr
.output
= (enum lttng_ust_output
) chan
->attr
.output
;
375 luc
->monitor_timer_interval
= ((struct lttng_channel_extended
*)
376 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
377 luc
->attr
.u
.s
.blocking_timeout
= ((struct lttng_channel_extended
*)
378 chan
->attr
.extended
.ptr
)->blocking_timeout
;
380 /* Translate to UST output enum */
381 switch (luc
->attr
.output
) {
383 luc
->attr
.output
= LTTNG_UST_MMAP
;
388 * If we receive an empty string for channel name, it means the
389 * default channel name is requested.
391 if (chan
->name
[0] == '\0') {
392 strncpy(luc
->name
, DEFAULT_CHANNEL_NAME
, sizeof(luc
->name
));
394 /* Copy channel name */
395 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
397 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
400 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
401 CDS_INIT_LIST_HEAD(&luc
->ctx_list
);
403 /* Alloc hash tables */
404 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
405 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
407 /* On-disk circular buffer parameters */
408 luc
->tracefile_size
= chan
->attr
.tracefile_size
;
409 luc
->tracefile_count
= chan
->attr
.tracefile_count
;
411 DBG2("Trace UST channel %s created", luc
->name
);
418 * Validates an exclusion list.
420 * Returns 0 if valid, negative value if invalid.
422 static int validate_exclusion(struct lttng_event_exclusion
*exclusion
)
429 for (i
= 0; i
< exclusion
->count
; ++i
) {
432 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
434 for (j
= 0; j
< i
; ++j
) {
436 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, j
);
438 if (!strncmp(name_a
, name_b
, LTTNG_SYMBOL_NAME_LEN
)) {
451 * Allocate and initialize a ust event. Set name and event type.
452 * We own filter_expression, filter, and exclusion.
454 * Return an lttng_error_code
456 enum lttng_error_code
trace_ust_create_event(struct lttng_event
*ev
,
457 char *filter_expression
,
458 struct lttng_filter_bytecode
*filter
,
459 struct lttng_event_exclusion
*exclusion
,
461 struct ltt_ust_event
**ust_event
)
463 struct ltt_ust_event
*local_ust_event
;
464 enum lttng_error_code ret
= LTTNG_OK
;
468 if (exclusion
&& validate_exclusion(exclusion
)) {
469 ret
= LTTNG_ERR_INVALID
;
473 local_ust_event
= zmalloc(sizeof(struct ltt_ust_event
));
474 if (local_ust_event
== NULL
) {
475 PERROR("ust event zmalloc");
476 ret
= LTTNG_ERR_NOMEM
;
480 local_ust_event
->internal
= internal_event
;
483 case LTTNG_EVENT_PROBE
:
484 local_ust_event
->attr
.instrumentation
= LTTNG_UST_PROBE
;
486 case LTTNG_EVENT_FUNCTION
:
487 local_ust_event
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
489 case LTTNG_EVENT_FUNCTION_ENTRY
:
490 local_ust_event
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
492 case LTTNG_EVENT_TRACEPOINT
:
493 local_ust_event
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
496 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
497 ret
= LTTNG_ERR_INVALID
;
498 goto error_free_event
;
501 /* Copy event name */
502 strncpy(local_ust_event
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
503 local_ust_event
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
505 switch (ev
->loglevel_type
) {
506 case LTTNG_EVENT_LOGLEVEL_ALL
:
507 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
508 local_ust_event
->attr
.loglevel
= -1; /* Force to -1 */
510 case LTTNG_EVENT_LOGLEVEL_RANGE
:
511 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
512 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
514 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
515 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
516 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
519 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
520 ret
= LTTNG_ERR_INVALID
;
521 goto error_free_event
;
525 local_ust_event
->filter_expression
= filter_expression
;
526 local_ust_event
->filter
= filter
;
527 local_ust_event
->exclusion
= exclusion
;
530 lttng_ht_node_init_str(&local_ust_event
->node
, local_ust_event
->attr
.name
);
532 DBG2("Trace UST event %s, loglevel (%d,%d) created",
533 local_ust_event
->attr
.name
, local_ust_event
->attr
.loglevel_type
,
534 local_ust_event
->attr
.loglevel
);
536 *ust_event
= local_ust_event
;
541 free(local_ust_event
);
543 free(filter_expression
);
550 int trace_ust_context_type_event_to_ust(
551 enum lttng_event_context_type type
)
556 case LTTNG_EVENT_CONTEXT_VTID
:
557 utype
= LTTNG_UST_CONTEXT_VTID
;
559 case LTTNG_EVENT_CONTEXT_VPID
:
560 utype
= LTTNG_UST_CONTEXT_VPID
;
562 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
563 utype
= LTTNG_UST_CONTEXT_PTHREAD_ID
;
565 case LTTNG_EVENT_CONTEXT_PROCNAME
:
566 utype
= LTTNG_UST_CONTEXT_PROCNAME
;
568 case LTTNG_EVENT_CONTEXT_IP
:
569 utype
= LTTNG_UST_CONTEXT_IP
;
571 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
572 if (!ustctl_has_perf_counters()) {
574 WARN("Perf counters not implemented in UST");
576 utype
= LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
;
579 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
580 utype
= LTTNG_UST_CONTEXT_APP_CONTEXT
;
582 case LTTNG_EVENT_CONTEXT_CGROUP_NS
:
583 utype
= LTTNG_UST_CONTEXT_CGROUP_NS
;
585 case LTTNG_EVENT_CONTEXT_IPC_NS
:
586 utype
= LTTNG_UST_CONTEXT_IPC_NS
;
588 case LTTNG_EVENT_CONTEXT_MNT_NS
:
589 utype
= LTTNG_UST_CONTEXT_MNT_NS
;
591 case LTTNG_EVENT_CONTEXT_NET_NS
:
592 utype
= LTTNG_UST_CONTEXT_NET_NS
;
594 case LTTNG_EVENT_CONTEXT_PID_NS
:
595 utype
= LTTNG_UST_CONTEXT_PID_NS
;
597 case LTTNG_EVENT_CONTEXT_USER_NS
:
598 utype
= LTTNG_UST_CONTEXT_USER_NS
;
600 case LTTNG_EVENT_CONTEXT_UTS_NS
:
601 utype
= LTTNG_UST_CONTEXT_UTS_NS
;
603 case LTTNG_EVENT_CONTEXT_VUID
:
604 utype
= LTTNG_UST_CONTEXT_VUID
;
606 case LTTNG_EVENT_CONTEXT_VEUID
:
607 utype
= LTTNG_UST_CONTEXT_VEUID
;
609 case LTTNG_EVENT_CONTEXT_VSUID
:
610 utype
= LTTNG_UST_CONTEXT_VSUID
;
612 case LTTNG_EVENT_CONTEXT_VGID
:
613 utype
= LTTNG_UST_CONTEXT_VGID
;
615 case LTTNG_EVENT_CONTEXT_VEGID
:
616 utype
= LTTNG_UST_CONTEXT_VEGID
;
618 case LTTNG_EVENT_CONTEXT_VSGID
:
619 utype
= LTTNG_UST_CONTEXT_VSGID
;
629 * Return 1 if contexts match, 0 otherwise.
631 int trace_ust_match_context(const struct ltt_ust_context
*uctx
,
632 const struct lttng_event_context
*ctx
)
636 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
640 if (uctx
->ctx
.ctx
!= utype
) {
644 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
645 if (uctx
->ctx
.u
.perf_counter
.type
646 != ctx
->u
.perf_counter
.type
) {
649 if (uctx
->ctx
.u
.perf_counter
.config
650 != ctx
->u
.perf_counter
.config
) {
653 if (strncmp(uctx
->ctx
.u
.perf_counter
.name
,
654 ctx
->u
.perf_counter
.name
,
655 LTTNG_UST_SYM_NAME_LEN
)) {
659 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
660 assert(uctx
->ctx
.u
.app_ctx
.provider_name
);
661 assert(uctx
->ctx
.u
.app_ctx
.ctx_name
);
662 if (strcmp(uctx
->ctx
.u
.app_ctx
.provider_name
,
663 ctx
->u
.app_ctx
.provider_name
) ||
664 strcmp(uctx
->ctx
.u
.app_ctx
.ctx_name
,
665 ctx
->u
.app_ctx
.ctx_name
)) {
676 * Allocate and initialize an UST context.
678 * Return pointer to structure or NULL.
680 struct ltt_ust_context
*trace_ust_create_context(
681 const struct lttng_event_context
*ctx
)
683 struct ltt_ust_context
*uctx
= NULL
;
688 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
690 ERR("Invalid UST context");
694 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
696 PERROR("zmalloc ltt_ust_context");
700 uctx
->ctx
.ctx
= (enum lttng_ust_context_type
) utype
;
702 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
703 uctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
704 uctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
705 strncpy(uctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
706 LTTNG_UST_SYM_NAME_LEN
);
707 uctx
->ctx
.u
.perf_counter
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
709 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
711 char *provider_name
= NULL
, *ctx_name
= NULL
;
713 provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
714 if (!provider_name
) {
717 uctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
719 ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
723 uctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
729 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
733 trace_ust_destroy_context(uctx
);
737 static void destroy_id_tracker_node_rcu(struct rcu_head
*head
)
739 struct ust_id_tracker_node
*tracker_node
= caa_container_of(
740 head
, struct ust_id_tracker_node
, node
.head
);
744 static void destroy_id_tracker_node(struct ust_id_tracker_node
*tracker_node
)
746 call_rcu(&tracker_node
->node
.head
, destroy_id_tracker_node_rcu
);
749 static int init_id_tracker(struct ust_id_tracker
*id_tracker
)
753 id_tracker
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
754 if (!id_tracker
->ht
) {
755 ret
= LTTNG_ERR_NOMEM
;
764 * Teardown id tracker content, but don't free id_tracker object.
766 static void fini_id_tracker(struct ust_id_tracker
*id_tracker
)
768 struct ust_id_tracker_node
*tracker_node
;
769 struct lttng_ht_iter iter
;
771 if (!id_tracker
->ht
) {
775 cds_lfht_for_each_entry (id_tracker
->ht
->ht
, &iter
.iter
, tracker_node
,
777 int ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
780 destroy_id_tracker_node(tracker_node
);
783 ht_cleanup_push(id_tracker
->ht
);
784 id_tracker
->ht
= NULL
;
787 static struct ust_id_tracker_node
*id_tracker_lookup(
788 struct ust_id_tracker
*id_tracker
,
790 struct lttng_ht_iter
*iter
)
792 unsigned long _id
= (unsigned long) id
;
793 struct lttng_ht_node_ulong
*node
;
795 lttng_ht_lookup(id_tracker
->ht
, (void *) _id
, iter
);
796 node
= lttng_ht_iter_get_node_ulong(iter
);
798 return caa_container_of(node
, struct ust_id_tracker_node
, node
);
804 static int id_tracker_add_id(struct ust_id_tracker
*id_tracker
, int id
)
806 int retval
= LTTNG_OK
;
807 struct ust_id_tracker_node
*tracker_node
;
808 struct lttng_ht_iter iter
;
811 retval
= LTTNG_ERR_INVALID
;
814 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
816 /* Already exists. */
817 retval
= LTTNG_ERR_ID_TRACKED
;
820 tracker_node
= zmalloc(sizeof(*tracker_node
));
822 retval
= LTTNG_ERR_NOMEM
;
825 lttng_ht_node_init_ulong(&tracker_node
->node
, (unsigned long) id
);
826 lttng_ht_add_unique_ulong(id_tracker
->ht
, &tracker_node
->node
);
831 static int id_tracker_del_id(struct ust_id_tracker
*id_tracker
, int id
)
833 int retval
= LTTNG_OK
, ret
;
834 struct ust_id_tracker_node
*tracker_node
;
835 struct lttng_ht_iter iter
;
838 retval
= LTTNG_ERR_INVALID
;
841 tracker_node
= id_tracker_lookup(id_tracker
, id
, &iter
);
844 retval
= LTTNG_ERR_ID_NOT_TRACKED
;
847 ret
= lttng_ht_del(id_tracker
->ht
, &iter
);
850 destroy_id_tracker_node(tracker_node
);
855 static struct ust_id_tracker
*get_id_tracker(struct ltt_ust_session
*session
,
856 enum lttng_tracker_type tracker_type
)
858 switch (tracker_type
) {
859 case LTTNG_TRACKER_VPID
:
860 return &session
->vpid_tracker
;
861 case LTTNG_TRACKER_VUID
:
862 return &session
->vuid_tracker
;
863 case LTTNG_TRACKER_VGID
:
864 return &session
->vgid_tracker
;
870 static struct lttng_tracker_list
*get_id_tracker_list(
871 struct ltt_ust_session
*session
,
872 enum lttng_tracker_type tracker_type
)
874 switch (tracker_type
) {
875 case LTTNG_TRACKER_VPID
:
876 return session
->tracker_list_vpid
;
877 case LTTNG_TRACKER_VUID
:
878 return session
->tracker_list_vuid
;
879 case LTTNG_TRACKER_VGID
:
880 return session
->tracker_list_vgid
;
887 * The session lock is held when calling this function.
889 int trace_ust_id_tracker_lookup(enum lttng_tracker_type tracker_type
,
890 struct ltt_ust_session
*session
,
893 struct lttng_ht_iter iter
;
894 struct ust_id_tracker
*id_tracker
;
896 id_tracker
= get_id_tracker(session
, tracker_type
);
900 if (!id_tracker
->ht
) {
903 if (id_tracker_lookup(id_tracker
, id
, &iter
)) {
910 * Called with the session lock held.
912 int trace_ust_track_id(enum lttng_tracker_type tracker_type
,
913 struct ltt_ust_session
*session
,
914 const struct lttng_tracker_id
*id
)
916 int retval
= LTTNG_OK
;
917 bool should_update_apps
= false;
918 struct ust_id_tracker
*id_tracker
;
919 struct lttng_tracker_list
*tracker_list
;
921 struct lttng_tracker_ids
*saved_ids
;
923 if (tracker_type
== LTTNG_TRACKER_PID
) {
924 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
925 tracker_type
= LTTNG_TRACKER_VPID
;
928 retval
= lttng_tracker_id_lookup_string(tracker_type
, id
, &value
);
929 if (retval
!= LTTNG_OK
) {
932 tracker_list
= get_id_tracker_list(session
, tracker_type
);
934 return LTTNG_ERR_INVALID
;
936 /* Save list for restore on error. */
937 retval
= lttng_tracker_id_get_list(tracker_list
, &saved_ids
);
938 if (retval
!= LTTNG_OK
) {
939 return LTTNG_ERR_INVALID
;
942 retval
= lttng_tracker_list_add(tracker_list
, id
);
943 if (retval
!= LTTNG_OK
) {
947 id_tracker
= get_id_tracker(session
, tracker_type
);
952 /* Track all ids: destroy tracker if exists. */
953 if (id_tracker
->ht
) {
954 fini_id_tracker(id_tracker
);
955 /* Ensure all apps have session. */
956 should_update_apps
= true;
959 if (!id_tracker
->ht
) {
960 /* Create tracker. */
961 retval
= init_id_tracker(id_tracker
);
962 if (retval
!= LTTNG_OK
) {
963 ERR("Error initializing ID tracker");
966 retval
= id_tracker_add_id(id_tracker
, value
);
967 if (retval
!= LTTNG_OK
) {
968 fini_id_tracker(id_tracker
);
971 /* Remove all apps from session except pid. */
972 should_update_apps
= true;
976 retval
= id_tracker_add_id(id_tracker
, value
);
977 if (retval
!= LTTNG_OK
) {
980 /* Add session to application */
981 switch (tracker_type
) {
982 case LTTNG_TRACKER_VPID
:
983 app
= ust_app_find_by_pid(value
);
985 should_update_apps
= true;
989 should_update_apps
= true;
993 if (should_update_apps
&& session
->active
) {
994 ust_app_global_update_all(session
);
999 if (lttng_tracker_id_set_list(tracker_list
, saved_ids
) != LTTNG_OK
) {
1000 ERR("Error on tracker add error handling.\n");
1003 lttng_tracker_ids_destroy(saved_ids
);
1008 * Called with the session lock held.
1010 int trace_ust_untrack_id(enum lttng_tracker_type tracker_type
,
1011 struct ltt_ust_session
*session
,
1012 const struct lttng_tracker_id
*id
)
1014 int retval
= LTTNG_OK
;
1015 bool should_update_apps
= false;
1016 struct ust_id_tracker
*id_tracker
;
1017 struct lttng_tracker_list
*tracker_list
;
1019 struct lttng_tracker_ids
*saved_ids
;
1021 if (tracker_type
== LTTNG_TRACKER_PID
) {
1022 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
1023 tracker_type
= LTTNG_TRACKER_VPID
;
1026 retval
= lttng_tracker_id_lookup_string(tracker_type
, id
, &value
);
1027 if (retval
!= LTTNG_OK
) {
1031 tracker_list
= get_id_tracker_list(session
, tracker_type
);
1032 if (!tracker_list
) {
1033 return LTTNG_ERR_INVALID
;
1035 /* Save list for restore on error. */
1036 retval
= lttng_tracker_id_get_list(tracker_list
, &saved_ids
);
1037 if (retval
!= LTTNG_OK
) {
1038 return LTTNG_ERR_INVALID
;
1040 /* Remove from list. */
1041 retval
= lttng_tracker_list_remove(tracker_list
, id
);
1042 if (retval
!= LTTNG_OK
) {
1046 id_tracker
= get_id_tracker(session
, tracker_type
);
1052 /* Create empty tracker, replace old tracker. */
1053 struct ust_id_tracker tmp_tracker
;
1055 tmp_tracker
= *id_tracker
;
1056 retval
= init_id_tracker(id_tracker
);
1057 if (retval
!= LTTNG_OK
) {
1058 ERR("Error initializing ID tracker");
1059 /* Rollback operation. */
1060 *id_tracker
= tmp_tracker
;
1063 fini_id_tracker(&tmp_tracker
);
1065 /* Remove session from all applications */
1066 should_update_apps
= true;
1068 struct ust_app
*app
;
1070 if (!id_tracker
->ht
) {
1071 /* No ID being tracked. */
1072 retval
= LTTNG_ERR_ID_NOT_TRACKED
;
1075 /* Remove ID from tracker */
1076 retval
= id_tracker_del_id(id_tracker
, value
);
1077 if (retval
!= LTTNG_OK
) {
1080 switch (tracker_type
) {
1081 case LTTNG_TRACKER_VPID
:
1082 /* Remove session from application. */
1083 app
= ust_app_find_by_pid(value
);
1085 should_update_apps
= true;
1089 /* Keep only apps matching ID. */
1090 should_update_apps
= true;
1093 if (should_update_apps
&& session
->active
) {
1094 ust_app_global_update_all(session
);
1099 if (lttng_tracker_id_set_list(tracker_list
, saved_ids
) != LTTNG_OK
) {
1100 ERR("Error on tracker remove error handling.\n");
1103 lttng_tracker_ids_destroy(saved_ids
);
1108 * Called with session lock held.
1110 int trace_ust_list_tracker_ids(enum lttng_tracker_type tracker_type
,
1111 struct ltt_ust_session
*session
,
1112 struct lttng_tracker_ids
**_ids
)
1115 struct lttng_tracker_list
*tracker_list
;
1117 if (tracker_type
== LTTNG_TRACKER_PID
) {
1118 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
1119 tracker_type
= LTTNG_TRACKER_VPID
;
1122 tracker_list
= get_id_tracker_list(session
, tracker_type
);
1123 if (!tracker_list
) {
1124 ret
= -LTTNG_ERR_INVALID
;
1128 ret
= lttng_tracker_id_get_list(tracker_list
, _ids
);
1129 if (ret
!= LTTNG_OK
) {
1130 ret
= -LTTNG_ERR_INVALID
;
1138 * RCU safe free context structure.
1140 static void destroy_context_rcu(struct rcu_head
*head
)
1142 struct lttng_ht_node_ulong
*node
=
1143 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1144 struct ltt_ust_context
*ctx
=
1145 caa_container_of(node
, struct ltt_ust_context
, node
);
1147 trace_ust_destroy_context(ctx
);
1151 * Cleanup UST context hash table.
1153 static void destroy_contexts(struct lttng_ht
*ht
)
1156 struct lttng_ht_node_ulong
*node
;
1157 struct lttng_ht_iter iter
;
1158 struct ltt_ust_context
*ctx
;
1163 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
1164 /* Remove from ordered list. */
1165 ctx
= caa_container_of(node
, struct ltt_ust_context
, node
);
1166 cds_list_del(&ctx
->list
);
1167 /* Remove from channel's hash table. */
1168 ret
= lttng_ht_del(ht
, &iter
);
1170 call_rcu(&node
->head
, destroy_context_rcu
);
1175 ht_cleanup_push(ht
);
1179 * Cleanup ust event structure.
1181 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
1185 DBG2("Trace destroy UST event %s", event
->attr
.name
);
1186 free(event
->filter_expression
);
1187 free(event
->filter
);
1188 free(event
->exclusion
);
1193 * Cleanup ust context structure.
1195 void trace_ust_destroy_context(struct ltt_ust_context
*ctx
)
1199 if (ctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1200 free(ctx
->ctx
.u
.app_ctx
.provider_name
);
1201 free(ctx
->ctx
.u
.app_ctx
.ctx_name
);
1207 * URCU intermediate call to complete destroy event.
1209 static void destroy_event_rcu(struct rcu_head
*head
)
1211 struct lttng_ht_node_str
*node
=
1212 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1213 struct ltt_ust_event
*event
=
1214 caa_container_of(node
, struct ltt_ust_event
, node
);
1216 trace_ust_destroy_event(event
);
1220 * Cleanup UST events hashtable.
1222 static void destroy_events(struct lttng_ht
*events
)
1225 struct lttng_ht_node_str
*node
;
1226 struct lttng_ht_iter iter
;
1231 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
1232 ret
= lttng_ht_del(events
, &iter
);
1234 call_rcu(&node
->head
, destroy_event_rcu
);
1238 ht_cleanup_push(events
);
1242 * Cleanup ust channel structure.
1244 * Should _NOT_ be called with RCU read lock held.
1246 static void _trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1250 DBG2("Trace destroy UST channel %s", channel
->name
);
1256 * URCU intermediate call to complete destroy channel.
1258 static void destroy_channel_rcu(struct rcu_head
*head
)
1260 struct lttng_ht_node_str
*node
=
1261 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1262 struct ltt_ust_channel
*channel
=
1263 caa_container_of(node
, struct ltt_ust_channel
, node
);
1265 _trace_ust_destroy_channel(channel
);
1268 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1270 /* Destroying all events of the channel */
1271 destroy_events(channel
->events
);
1272 /* Destroying all context of the channel */
1273 destroy_contexts(channel
->ctx
);
1275 call_rcu(&channel
->node
.head
, destroy_channel_rcu
);
1279 * Remove an UST channel from a channel HT.
1281 void trace_ust_delete_channel(struct lttng_ht
*ht
,
1282 struct ltt_ust_channel
*channel
)
1285 struct lttng_ht_iter iter
;
1290 iter
.iter
.node
= &channel
->node
.node
;
1291 ret
= lttng_ht_del(ht
, &iter
);
1296 * Iterate over a hash table containing channels and cleanup safely.
1298 static void destroy_channels(struct lttng_ht
*channels
)
1300 struct lttng_ht_node_str
*node
;
1301 struct lttng_ht_iter iter
;
1306 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
1307 struct ltt_ust_channel
*chan
=
1308 caa_container_of(node
, struct ltt_ust_channel
, node
);
1310 trace_ust_delete_channel(channels
, chan
);
1311 trace_ust_destroy_channel(chan
);
1315 ht_cleanup_push(channels
);
1319 * Cleanup UST global domain.
1321 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
1325 destroy_channels(dom
->channels
);
1329 * Cleanup ust session structure, keeping data required by
1332 * Should *NOT* be called with RCU read-side lock held.
1334 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
1337 struct buffer_reg_uid
*reg
, *sreg
;
1338 struct lttng_ht_iter iter
;
1342 DBG2("Trace UST destroy session %" PRIu64
, session
->id
);
1344 /* Cleaning up UST domain */
1345 destroy_domain_global(&session
->domain_global
);
1348 cds_lfht_for_each_entry(session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
1349 int ret
= lttng_ht_del(session
->agents
, &iter
);
1356 ht_cleanup_push(session
->agents
);
1358 /* Cleanup UID buffer registry object(s). */
1359 cds_list_for_each_entry_safe(reg
, sreg
, &session
->buffer_reg_uid_list
,
1361 cds_list_del(®
->lnode
);
1362 buffer_reg_uid_remove(reg
);
1363 buffer_reg_uid_destroy(reg
, session
->consumer
);
1366 lttng_tracker_list_destroy(session
->tracker_list_vpid
);
1367 lttng_tracker_list_destroy(session
->tracker_list_vuid
);
1368 lttng_tracker_list_destroy(session
->tracker_list_vgid
);
1370 fini_id_tracker(&session
->vpid_tracker
);
1371 fini_id_tracker(&session
->vuid_tracker
);
1372 fini_id_tracker(&session
->vgid_tracker
);
1373 lttng_trace_chunk_put(session
->current_trace_chunk
);
1376 /* Free elements needed by destroy notifiers. */
1377 void trace_ust_free_session(struct ltt_ust_session
*session
)
1379 consumer_output_put(session
->consumer
);