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
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
317 if (lus
->consumer
== NULL
) {
321 DBG2("UST trace session create successful");
326 ht_cleanup_push(lus
->domain_global
.channels
);
327 ht_cleanup_push(lus
->agents
);
334 * Allocate and initialize a ust channel data structure.
336 * Return pointer to structure or NULL.
338 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
339 enum lttng_domain_type domain
)
341 struct ltt_ust_channel
*luc
;
345 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
347 PERROR("ltt_ust_channel zmalloc");
351 luc
->domain
= domain
;
353 /* Copy UST channel attributes */
354 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
355 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
356 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
357 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
358 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
359 luc
->attr
.output
= (enum lttng_ust_output
) chan
->attr
.output
;
360 luc
->monitor_timer_interval
= ((struct lttng_channel_extended
*)
361 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
362 luc
->attr
.u
.s
.blocking_timeout
= ((struct lttng_channel_extended
*)
363 chan
->attr
.extended
.ptr
)->blocking_timeout
;
365 /* Translate to UST output enum */
366 switch (luc
->attr
.output
) {
368 luc
->attr
.output
= LTTNG_UST_MMAP
;
373 * If we receive an empty string for channel name, it means the
374 * default channel name is requested.
376 if (chan
->name
[0] == '\0') {
377 strncpy(luc
->name
, DEFAULT_CHANNEL_NAME
, sizeof(luc
->name
));
379 /* Copy channel name */
380 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
382 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
385 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
386 CDS_INIT_LIST_HEAD(&luc
->ctx_list
);
388 /* Alloc hash tables */
389 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
390 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
392 /* On-disk circular buffer parameters */
393 luc
->tracefile_size
= chan
->attr
.tracefile_size
;
394 luc
->tracefile_count
= chan
->attr
.tracefile_count
;
396 DBG2("Trace UST channel %s created", luc
->name
);
403 * Validates an exclusion list.
405 * Returns 0 if valid, negative value if invalid.
407 static int validate_exclusion(struct lttng_event_exclusion
*exclusion
)
414 for (i
= 0; i
< exclusion
->count
; ++i
) {
417 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
419 for (j
= 0; j
< i
; ++j
) {
421 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, j
);
423 if (!strncmp(name_a
, name_b
, LTTNG_SYMBOL_NAME_LEN
)) {
436 * Allocate and initialize a ust event. Set name and event type.
437 * We own filter_expression, filter, and exclusion.
439 * Return an lttng_error_code
441 enum lttng_error_code
trace_ust_create_event(struct lttng_event
*ev
,
442 char *filter_expression
,
443 struct lttng_filter_bytecode
*filter
,
444 struct lttng_event_exclusion
*exclusion
,
446 struct ltt_ust_event
**ust_event
)
448 struct ltt_ust_event
*local_ust_event
;
449 enum lttng_error_code ret
= LTTNG_OK
;
453 if (exclusion
&& validate_exclusion(exclusion
)) {
454 ret
= LTTNG_ERR_INVALID
;
458 local_ust_event
= zmalloc(sizeof(struct ltt_ust_event
));
459 if (local_ust_event
== NULL
) {
460 PERROR("ust event zmalloc");
461 ret
= LTTNG_ERR_NOMEM
;
465 local_ust_event
->internal
= internal_event
;
468 case LTTNG_EVENT_PROBE
:
469 local_ust_event
->attr
.instrumentation
= LTTNG_UST_PROBE
;
471 case LTTNG_EVENT_FUNCTION
:
472 local_ust_event
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
474 case LTTNG_EVENT_FUNCTION_ENTRY
:
475 local_ust_event
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
477 case LTTNG_EVENT_TRACEPOINT
:
478 local_ust_event
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
481 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
482 ret
= LTTNG_ERR_INVALID
;
483 goto error_free_event
;
486 /* Copy event name */
487 strncpy(local_ust_event
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
488 local_ust_event
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
490 switch (ev
->loglevel_type
) {
491 case LTTNG_EVENT_LOGLEVEL_ALL
:
492 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
493 local_ust_event
->attr
.loglevel
= -1; /* Force to -1 */
495 case LTTNG_EVENT_LOGLEVEL_RANGE
:
496 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
497 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
499 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
500 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
501 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
504 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
505 ret
= LTTNG_ERR_INVALID
;
506 goto error_free_event
;
510 local_ust_event
->filter_expression
= filter_expression
;
511 local_ust_event
->filter
= filter
;
512 local_ust_event
->exclusion
= exclusion
;
515 lttng_ht_node_init_str(&local_ust_event
->node
, local_ust_event
->attr
.name
);
517 DBG2("Trace UST event %s, loglevel (%d,%d) created",
518 local_ust_event
->attr
.name
, local_ust_event
->attr
.loglevel_type
,
519 local_ust_event
->attr
.loglevel
);
521 *ust_event
= local_ust_event
;
526 free(local_ust_event
);
528 free(filter_expression
);
535 int trace_ust_context_type_event_to_ust(
536 enum lttng_event_context_type type
)
541 case LTTNG_EVENT_CONTEXT_VTID
:
542 utype
= LTTNG_UST_CONTEXT_VTID
;
544 case LTTNG_EVENT_CONTEXT_VPID
:
545 utype
= LTTNG_UST_CONTEXT_VPID
;
547 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
548 utype
= LTTNG_UST_CONTEXT_PTHREAD_ID
;
550 case LTTNG_EVENT_CONTEXT_PROCNAME
:
551 utype
= LTTNG_UST_CONTEXT_PROCNAME
;
553 case LTTNG_EVENT_CONTEXT_IP
:
554 utype
= LTTNG_UST_CONTEXT_IP
;
556 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
557 if (!ustctl_has_perf_counters()) {
559 WARN("Perf counters not implemented in UST");
561 utype
= LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
;
564 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
565 utype
= LTTNG_UST_CONTEXT_APP_CONTEXT
;
575 * Return 1 if contexts match, 0 otherwise.
577 int trace_ust_match_context(const struct ltt_ust_context
*uctx
,
578 const struct lttng_event_context
*ctx
)
582 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
586 if (uctx
->ctx
.ctx
!= utype
) {
590 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
591 if (uctx
->ctx
.u
.perf_counter
.type
592 != ctx
->u
.perf_counter
.type
) {
595 if (uctx
->ctx
.u
.perf_counter
.config
596 != ctx
->u
.perf_counter
.config
) {
599 if (strncmp(uctx
->ctx
.u
.perf_counter
.name
,
600 ctx
->u
.perf_counter
.name
,
601 LTTNG_UST_SYM_NAME_LEN
)) {
605 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
606 assert(uctx
->ctx
.u
.app_ctx
.provider_name
);
607 assert(uctx
->ctx
.u
.app_ctx
.ctx_name
);
608 if (strcmp(uctx
->ctx
.u
.app_ctx
.provider_name
,
609 ctx
->u
.app_ctx
.provider_name
) ||
610 strcmp(uctx
->ctx
.u
.app_ctx
.ctx_name
,
611 ctx
->u
.app_ctx
.ctx_name
)) {
622 * Allocate and initialize an UST context.
624 * Return pointer to structure or NULL.
626 struct ltt_ust_context
*trace_ust_create_context(
627 const struct lttng_event_context
*ctx
)
629 struct ltt_ust_context
*uctx
= NULL
;
634 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
636 ERR("Invalid UST context");
640 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
642 PERROR("zmalloc ltt_ust_context");
646 uctx
->ctx
.ctx
= (enum lttng_ust_context_type
) utype
;
648 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
649 uctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
650 uctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
651 strncpy(uctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
652 LTTNG_UST_SYM_NAME_LEN
);
653 uctx
->ctx
.u
.perf_counter
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
655 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
657 char *provider_name
= NULL
, *ctx_name
= NULL
;
659 provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
660 if (!provider_name
) {
663 uctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
665 ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
669 uctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
675 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
679 trace_ust_destroy_context(uctx
);
684 void destroy_pid_tracker_node_rcu(struct rcu_head
*head
)
686 struct ust_pid_tracker_node
*tracker_node
=
687 caa_container_of(head
, struct ust_pid_tracker_node
, node
.head
);
692 void destroy_pid_tracker_node(struct ust_pid_tracker_node
*tracker_node
)
695 call_rcu(&tracker_node
->node
.head
, destroy_pid_tracker_node_rcu
);
699 int init_pid_tracker(struct ust_pid_tracker
*pid_tracker
)
703 pid_tracker
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
704 if (!pid_tracker
->ht
) {
714 * Teardown pid tracker content, but don't free pid_tracker object.
717 void fini_pid_tracker(struct ust_pid_tracker
*pid_tracker
)
719 struct ust_pid_tracker_node
*tracker_node
;
720 struct lttng_ht_iter iter
;
722 if (!pid_tracker
->ht
) {
726 cds_lfht_for_each_entry(pid_tracker
->ht
->ht
,
727 &iter
.iter
, tracker_node
, node
.node
) {
728 int ret
= lttng_ht_del(pid_tracker
->ht
, &iter
);
731 destroy_pid_tracker_node(tracker_node
);
734 ht_cleanup_push(pid_tracker
->ht
);
735 pid_tracker
->ht
= NULL
;
739 struct ust_pid_tracker_node
*pid_tracker_lookup(
740 struct ust_pid_tracker
*pid_tracker
, int pid
,
741 struct lttng_ht_iter
*iter
)
743 unsigned long _pid
= (unsigned long) pid
;
744 struct lttng_ht_node_ulong
*node
;
746 lttng_ht_lookup(pid_tracker
->ht
, (void *) _pid
, iter
);
747 node
= lttng_ht_iter_get_node_ulong(iter
);
749 return caa_container_of(node
, struct ust_pid_tracker_node
,
757 int pid_tracker_add_pid(struct ust_pid_tracker
*pid_tracker
, int pid
)
759 int retval
= LTTNG_OK
;
760 struct ust_pid_tracker_node
*tracker_node
;
761 struct lttng_ht_iter iter
;
764 retval
= LTTNG_ERR_INVALID
;
767 tracker_node
= pid_tracker_lookup(pid_tracker
, pid
, &iter
);
769 /* Already exists. */
770 retval
= LTTNG_ERR_PID_TRACKED
;
773 tracker_node
= zmalloc(sizeof(*tracker_node
));
775 retval
= LTTNG_ERR_NOMEM
;
778 lttng_ht_node_init_ulong(&tracker_node
->node
, (unsigned long) pid
);
779 lttng_ht_add_unique_ulong(pid_tracker
->ht
, &tracker_node
->node
);
785 int pid_tracker_del_pid(struct ust_pid_tracker
*pid_tracker
, int pid
)
787 int retval
= LTTNG_OK
, ret
;
788 struct ust_pid_tracker_node
*tracker_node
;
789 struct lttng_ht_iter iter
;
792 retval
= LTTNG_ERR_INVALID
;
795 tracker_node
= pid_tracker_lookup(pid_tracker
, pid
, &iter
);
798 retval
= LTTNG_ERR_PID_NOT_TRACKED
;
801 ret
= lttng_ht_del(pid_tracker
->ht
, &iter
);
804 destroy_pid_tracker_node(tracker_node
);
810 * The session lock is held when calling this function.
812 int trace_ust_pid_tracker_lookup(struct ltt_ust_session
*session
, int pid
)
814 struct lttng_ht_iter iter
;
816 if (!session
->pid_tracker
.ht
) {
819 if (pid_tracker_lookup(&session
->pid_tracker
, pid
, &iter
)) {
826 * Called with the session lock held.
828 int trace_ust_track_pid(struct ltt_ust_session
*session
, int pid
)
830 int retval
= LTTNG_OK
;
831 bool should_update_apps
= false;
834 /* Track all pids: destroy tracker if exists. */
835 if (session
->pid_tracker
.ht
) {
836 fini_pid_tracker(&session
->pid_tracker
);
837 /* Ensure all apps have session. */
838 should_update_apps
= true;
843 if (!session
->pid_tracker
.ht
) {
844 /* Create tracker. */
845 if (init_pid_tracker(&session
->pid_tracker
)) {
846 ERR("Error initializing PID tracker");
847 retval
= LTTNG_ERR_NOMEM
;
850 ret
= pid_tracker_add_pid(&session
->pid_tracker
, pid
);
851 if (ret
!= LTTNG_OK
) {
853 fini_pid_tracker(&session
->pid_tracker
);
856 /* Remove all apps from session except pid. */
857 should_update_apps
= true;
861 ret
= pid_tracker_add_pid(&session
->pid_tracker
, pid
);
862 if (ret
!= LTTNG_OK
) {
866 /* Add session to application */
867 app
= ust_app_find_by_pid(pid
);
869 should_update_apps
= true;
873 if (should_update_apps
&& session
->active
) {
874 ust_app_global_update_all(session
);
881 * Called with the session lock held.
883 int trace_ust_untrack_pid(struct ltt_ust_session
*session
, int pid
)
885 int retval
= LTTNG_OK
;
886 bool should_update_apps
= false;
889 /* Create empty tracker, replace old tracker. */
890 struct ust_pid_tracker tmp_tracker
;
892 tmp_tracker
= session
->pid_tracker
;
893 if (init_pid_tracker(&session
->pid_tracker
)) {
894 ERR("Error initializing PID tracker");
895 retval
= LTTNG_ERR_NOMEM
;
896 /* Rollback operation. */
897 session
->pid_tracker
= tmp_tracker
;
900 fini_pid_tracker(&tmp_tracker
);
902 /* Remove session from all applications */
903 should_update_apps
= true;
908 if (!session
->pid_tracker
.ht
) {
909 /* No PID being tracked. */
910 retval
= LTTNG_ERR_PID_NOT_TRACKED
;
913 /* Remove PID from tracker */
914 ret
= pid_tracker_del_pid(&session
->pid_tracker
, pid
);
915 if (ret
!= LTTNG_OK
) {
919 /* Remove session from application. */
920 app
= ust_app_find_by_pid(pid
);
922 should_update_apps
= true;
925 if (should_update_apps
&& session
->active
) {
926 ust_app_global_update_all(session
);
933 * Called with session lock held.
935 ssize_t
trace_ust_list_tracker_pids(struct ltt_ust_session
*session
,
938 struct ust_pid_tracker_node
*tracker_node
;
939 struct lttng_ht_iter iter
;
940 unsigned long count
, i
= 0;
945 if (!session
->pid_tracker
.ht
) {
946 /* Tracker disabled. Set first entry to -1. */
947 pids
= zmalloc(sizeof(*pids
));
958 cds_lfht_count_nodes(session
->pid_tracker
.ht
->ht
,
959 &approx
[0], &count
, &approx
[1]);
960 pids
= zmalloc(sizeof(*pids
) * count
);
965 cds_lfht_for_each_entry(session
->pid_tracker
.ht
->ht
,
966 &iter
.iter
, tracker_node
, node
.node
) {
967 pids
[i
++] = tracker_node
->node
.key
;
977 * RCU safe free context structure.
979 static void destroy_context_rcu(struct rcu_head
*head
)
981 struct lttng_ht_node_ulong
*node
=
982 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
983 struct ltt_ust_context
*ctx
=
984 caa_container_of(node
, struct ltt_ust_context
, node
);
986 trace_ust_destroy_context(ctx
);
990 * Cleanup UST context hash table.
992 static void destroy_contexts(struct lttng_ht
*ht
)
995 struct lttng_ht_node_ulong
*node
;
996 struct lttng_ht_iter iter
;
997 struct ltt_ust_context
*ctx
;
1002 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
1003 /* Remove from ordered list. */
1004 ctx
= caa_container_of(node
, struct ltt_ust_context
, node
);
1005 cds_list_del(&ctx
->list
);
1006 /* Remove from channel's hash table. */
1007 ret
= lttng_ht_del(ht
, &iter
);
1009 call_rcu(&node
->head
, destroy_context_rcu
);
1014 ht_cleanup_push(ht
);
1018 * Cleanup ust event structure.
1020 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
1024 DBG2("Trace destroy UST event %s", event
->attr
.name
);
1025 free(event
->filter_expression
);
1026 free(event
->filter
);
1027 free(event
->exclusion
);
1032 * Cleanup ust context structure.
1034 void trace_ust_destroy_context(struct ltt_ust_context
*ctx
)
1038 if (ctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1039 free(ctx
->ctx
.u
.app_ctx
.provider_name
);
1040 free(ctx
->ctx
.u
.app_ctx
.ctx_name
);
1046 * URCU intermediate call to complete destroy event.
1048 static void destroy_event_rcu(struct rcu_head
*head
)
1050 struct lttng_ht_node_str
*node
=
1051 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1052 struct ltt_ust_event
*event
=
1053 caa_container_of(node
, struct ltt_ust_event
, node
);
1055 trace_ust_destroy_event(event
);
1059 * Cleanup UST events hashtable.
1061 static void destroy_events(struct lttng_ht
*events
)
1064 struct lttng_ht_node_str
*node
;
1065 struct lttng_ht_iter iter
;
1070 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
1071 ret
= lttng_ht_del(events
, &iter
);
1073 call_rcu(&node
->head
, destroy_event_rcu
);
1077 ht_cleanup_push(events
);
1081 * Cleanup ust channel structure.
1083 * Should _NOT_ be called with RCU read lock held.
1085 static void _trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1089 DBG2("Trace destroy UST channel %s", channel
->name
);
1095 * URCU intermediate call to complete destroy channel.
1097 static void destroy_channel_rcu(struct rcu_head
*head
)
1099 struct lttng_ht_node_str
*node
=
1100 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1101 struct ltt_ust_channel
*channel
=
1102 caa_container_of(node
, struct ltt_ust_channel
, node
);
1104 _trace_ust_destroy_channel(channel
);
1107 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1109 /* Destroying all events of the channel */
1110 destroy_events(channel
->events
);
1111 /* Destroying all context of the channel */
1112 destroy_contexts(channel
->ctx
);
1114 call_rcu(&channel
->node
.head
, destroy_channel_rcu
);
1118 * Remove an UST channel from a channel HT.
1120 void trace_ust_delete_channel(struct lttng_ht
*ht
,
1121 struct ltt_ust_channel
*channel
)
1124 struct lttng_ht_iter iter
;
1129 iter
.iter
.node
= &channel
->node
.node
;
1130 ret
= lttng_ht_del(ht
, &iter
);
1135 * Iterate over a hash table containing channels and cleanup safely.
1137 static void destroy_channels(struct lttng_ht
*channels
)
1139 struct lttng_ht_node_str
*node
;
1140 struct lttng_ht_iter iter
;
1145 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
1146 struct ltt_ust_channel
*chan
=
1147 caa_container_of(node
, struct ltt_ust_channel
, node
);
1149 trace_ust_delete_channel(channels
, chan
);
1150 trace_ust_destroy_channel(chan
);
1154 ht_cleanup_push(channels
);
1158 * Cleanup UST global domain.
1160 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
1164 destroy_channels(dom
->channels
);
1168 * Cleanup ust session structure, keeping data required by
1171 * Should *NOT* be called with RCU read-side lock held.
1173 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
1176 struct buffer_reg_uid
*reg
, *sreg
;
1177 struct lttng_ht_iter iter
;
1181 DBG2("Trace UST destroy session %" PRIu64
, session
->id
);
1183 /* Cleaning up UST domain */
1184 destroy_domain_global(&session
->domain_global
);
1187 cds_lfht_for_each_entry(session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
1188 int ret
= lttng_ht_del(session
->agents
, &iter
);
1195 ht_cleanup_push(session
->agents
);
1197 /* Cleanup UID buffer registry object(s). */
1198 cds_list_for_each_entry_safe(reg
, sreg
, &session
->buffer_reg_uid_list
,
1200 cds_list_del(®
->lnode
);
1201 buffer_reg_uid_remove(reg
);
1202 buffer_reg_uid_destroy(reg
, session
->consumer
);
1205 fini_pid_tracker(&session
->pid_tracker
);
1206 lttng_trace_chunk_put(session
->current_trace_chunk
);
1209 /* Free elements needed by destroy notifiers. */
1210 void trace_ust_free_session(struct ltt_ust_session
*session
)
1212 consumer_output_put(session
->consumer
);