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>
29 #include "buffer-registry.h"
30 #include "trace-ust.h"
36 * Match function for the events hash table lookup.
38 * Matches by name only. Used by the disable command.
40 int trace_ust_ht_match_event_by_name(struct cds_lfht_node
*node
,
43 struct ltt_ust_event
*event
;
49 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
53 if (strncmp(event
->attr
.name
, name
, sizeof(event
->attr
.name
)) != 0) {
65 * Match function for the hash table lookup.
67 * It matches an ust event based on three attributes which are the event name,
68 * the filter bytecode and the loglevel.
70 int trace_ust_ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
72 struct ltt_ust_event
*event
;
73 const struct ltt_ust_ht_key
*key
;
74 int ev_loglevel_value
;
80 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
82 ev_loglevel_value
= event
->attr
.loglevel
;
84 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
87 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
91 /* Event loglevel value and type. */
92 ll_match
= loglevels_match(event
->attr
.loglevel_type
,
93 ev_loglevel_value
, key
->loglevel_type
,
94 key
->loglevel_value
, LTTNG_UST_LOGLEVEL_ALL
);
100 /* Only one of the filters is NULL, fail. */
101 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
105 if (key
->filter
&& event
->filter
) {
106 /* Both filters exists, check length followed by the bytecode. */
107 if (event
->filter
->len
!= key
->filter
->len
||
108 memcmp(event
->filter
->data
, key
->filter
->data
,
109 event
->filter
->len
) != 0) {
114 /* If only one of the exclusions is NULL, fail. */
115 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
119 if (key
->exclusion
&& event
->exclusion
) {
122 /* Check exclusion counts first. */
123 if (event
->exclusion
->count
!= key
->exclusion
->count
) {
127 /* Compare names individually. */
128 for (i
= 0; i
< event
->exclusion
->count
; ++i
) {
131 const char *name_ev
=
132 LTTNG_EVENT_EXCLUSION_NAME_AT(
133 event
->exclusion
, i
);
136 * Compare this exclusion name to all the key's
139 for (j
= 0; j
< key
->exclusion
->count
; ++j
) {
140 const char *name_key
=
141 LTTNG_EVENT_EXCLUSION_NAME_AT(
144 if (!strncmp(name_ev
, name_key
,
145 LTTNG_SYMBOL_NAME_LEN
)) {
153 * If the current exclusion name was not found amongst
154 * the key's exclusion names, then there's no match.
169 * Find the channel in the hashtable and return channel pointer. RCU read side
170 * lock MUST be acquired before calling this.
172 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct lttng_ht
*ht
,
175 struct lttng_ht_node_str
*node
;
176 struct lttng_ht_iter iter
;
179 * If we receive an empty string for channel name, it means the
180 * default channel name is requested.
183 name
= DEFAULT_CHANNEL_NAME
;
185 lttng_ht_lookup(ht
, (void *)name
, &iter
);
186 node
= lttng_ht_iter_get_node_str(&iter
);
191 DBG2("Trace UST channel %s found by name", name
);
193 return caa_container_of(node
, struct ltt_ust_channel
, node
);
196 DBG2("Trace UST channel %s not found by name", name
);
201 * Find the event in the hashtable and return event pointer. RCU read side lock
202 * MUST be acquired before calling this.
204 struct ltt_ust_event
*trace_ust_find_event(struct lttng_ht
*ht
,
205 char *name
, struct lttng_filter_bytecode
*filter
,
206 enum lttng_ust_loglevel_type loglevel_type
, int loglevel_value
,
207 struct lttng_event_exclusion
*exclusion
)
209 struct lttng_ht_node_str
*node
;
210 struct lttng_ht_iter iter
;
211 struct ltt_ust_ht_key key
;
218 key
.loglevel_type
= loglevel_type
;
219 key
.loglevel_value
= loglevel_value
;
220 key
.exclusion
= exclusion
;
222 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
223 trace_ust_ht_match_event
, &key
, &iter
.iter
);
224 node
= lttng_ht_iter_get_node_str(&iter
);
229 DBG2("Trace UST event %s found", key
.name
);
231 return caa_container_of(node
, struct ltt_ust_event
, node
);
234 DBG2("Trace UST event %s NOT found", key
.name
);
239 * Lookup an agent in the session agents hash table by domain type and return
240 * the object if found else NULL.
242 * RCU read side lock must be acquired before calling and only released
243 * once the agent is no longer in scope or being used.
245 struct agent
*trace_ust_find_agent(struct ltt_ust_session
*session
,
246 enum lttng_domain_type domain_type
)
248 struct agent
*agt
= NULL
;
249 struct lttng_ht_node_u64
*node
;
250 struct lttng_ht_iter iter
;
255 DBG3("Trace ust agent lookup for domain %d", domain_type
);
259 lttng_ht_lookup(session
->agents
, &key
, &iter
);
260 node
= lttng_ht_iter_get_node_u64(&iter
);
264 agt
= caa_container_of(node
, struct agent
, node
);
271 * Allocate and initialize a ust session data structure.
273 * Return pointer to structure or NULL.
275 struct ltt_ust_session
*trace_ust_create_session(uint64_t session_id
)
277 struct ltt_ust_session
*lus
;
279 /* Allocate a new ltt ust session */
280 lus
= zmalloc(sizeof(struct ltt_ust_session
));
282 PERROR("create ust session zmalloc");
286 /* Init data structure */
287 lus
->id
= session_id
;
290 /* Set default metadata channel attribute. */
291 lus
->metadata_attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
292 lus
->metadata_attr
.subbuf_size
= default_get_metadata_subbuf_size();
293 lus
->metadata_attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
294 lus
->metadata_attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
295 lus
->metadata_attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;
296 lus
->metadata_attr
.output
= LTTNG_UST_MMAP
;
299 * Default buffer type. This can be changed through an enable channel
300 * requesting a different type. Note that this can only be changed once
301 * during the session lifetime which is at the first enable channel and
302 * only before start. The flag buffer_type_changed indicates the status.
304 lus
->buffer_type
= LTTNG_BUFFER_PER_UID
;
305 /* Once set to 1, the buffer_type is immutable for the session. */
306 lus
->buffer_type_changed
= 0;
307 /* Init it in case it get used after allocation. */
308 CDS_INIT_LIST_HEAD(&lus
->buffer_reg_uid_list
);
310 /* Alloc UST global domain channels' HT */
311 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
312 /* Alloc agent hash table. */
313 lus
->agents
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
315 lus
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
316 if (lus
->consumer
== NULL
) {
320 DBG2("UST trace session create successful");
325 ht_cleanup_push(lus
->domain_global
.channels
);
326 ht_cleanup_push(lus
->agents
);
333 * Allocate and initialize a ust channel data structure.
335 * Return pointer to structure or NULL.
337 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
338 enum lttng_domain_type domain
)
340 struct ltt_ust_channel
*luc
;
344 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
346 PERROR("ltt_ust_channel zmalloc");
350 luc
->domain
= domain
;
352 /* Copy UST channel attributes */
353 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
354 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
355 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
356 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
357 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
358 luc
->attr
.output
= (enum lttng_ust_output
) chan
->attr
.output
;
359 luc
->monitor_timer_interval
= ((struct lttng_channel_extended
*)
360 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
361 luc
->attr
.u
.s
.blocking_timeout
= ((struct lttng_channel_extended
*)
362 chan
->attr
.extended
.ptr
)->blocking_timeout
;
364 /* Translate to UST output enum */
365 switch (luc
->attr
.output
) {
367 luc
->attr
.output
= LTTNG_UST_MMAP
;
372 * If we receive an empty string for channel name, it means the
373 * default channel name is requested.
375 if (chan
->name
[0] == '\0') {
376 strncpy(luc
->name
, DEFAULT_CHANNEL_NAME
, sizeof(luc
->name
));
378 /* Copy channel name */
379 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
381 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
384 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
385 CDS_INIT_LIST_HEAD(&luc
->ctx_list
);
387 /* Alloc hash tables */
388 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
389 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
391 /* On-disk circular buffer parameters */
392 luc
->tracefile_size
= chan
->attr
.tracefile_size
;
393 luc
->tracefile_count
= chan
->attr
.tracefile_count
;
395 DBG2("Trace UST channel %s created", luc
->name
);
402 * Validates an exclusion list.
404 * Returns 0 if valid, negative value if invalid.
406 static int validate_exclusion(struct lttng_event_exclusion
*exclusion
)
413 for (i
= 0; i
< exclusion
->count
; ++i
) {
416 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
418 for (j
= 0; j
< i
; ++j
) {
420 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, j
);
422 if (!strncmp(name_a
, name_b
, LTTNG_SYMBOL_NAME_LEN
)) {
435 * Allocate and initialize a ust event. Set name and event type.
436 * We own filter_expression, filter, and exclusion.
438 * Return an lttng_error_code
440 enum lttng_error_code
trace_ust_create_event(struct lttng_event
*ev
,
441 char *filter_expression
,
442 struct lttng_filter_bytecode
*filter
,
443 struct lttng_event_exclusion
*exclusion
,
445 struct ltt_ust_event
**ust_event
)
447 struct ltt_ust_event
*local_ust_event
;
448 enum lttng_error_code ret
= LTTNG_OK
;
452 if (exclusion
&& validate_exclusion(exclusion
)) {
453 ret
= LTTNG_ERR_INVALID
;
457 local_ust_event
= zmalloc(sizeof(struct ltt_ust_event
));
458 if (local_ust_event
== NULL
) {
459 PERROR("ust event zmalloc");
460 ret
= LTTNG_ERR_NOMEM
;
464 local_ust_event
->internal
= internal_event
;
467 case LTTNG_EVENT_PROBE
:
468 local_ust_event
->attr
.instrumentation
= LTTNG_UST_PROBE
;
470 case LTTNG_EVENT_FUNCTION
:
471 local_ust_event
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
473 case LTTNG_EVENT_FUNCTION_ENTRY
:
474 local_ust_event
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
476 case LTTNG_EVENT_TRACEPOINT
:
477 local_ust_event
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
480 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
481 ret
= LTTNG_ERR_INVALID
;
482 goto error_free_event
;
485 /* Copy event name */
486 strncpy(local_ust_event
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
487 local_ust_event
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
489 switch (ev
->loglevel_type
) {
490 case LTTNG_EVENT_LOGLEVEL_ALL
:
491 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
492 local_ust_event
->attr
.loglevel
= -1; /* Force to -1 */
494 case LTTNG_EVENT_LOGLEVEL_RANGE
:
495 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
496 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
498 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
499 local_ust_event
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
500 local_ust_event
->attr
.loglevel
= ev
->loglevel
;
503 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
504 ret
= LTTNG_ERR_INVALID
;
505 goto error_free_event
;
509 local_ust_event
->filter_expression
= filter_expression
;
510 local_ust_event
->filter
= filter
;
511 local_ust_event
->exclusion
= exclusion
;
514 lttng_ht_node_init_str(&local_ust_event
->node
, local_ust_event
->attr
.name
);
516 DBG2("Trace UST event %s, loglevel (%d,%d) created",
517 local_ust_event
->attr
.name
, local_ust_event
->attr
.loglevel_type
,
518 local_ust_event
->attr
.loglevel
);
520 *ust_event
= local_ust_event
;
525 free(local_ust_event
);
527 free(filter_expression
);
534 int trace_ust_context_type_event_to_ust(
535 enum lttng_event_context_type type
)
540 case LTTNG_EVENT_CONTEXT_VTID
:
541 utype
= LTTNG_UST_CONTEXT_VTID
;
543 case LTTNG_EVENT_CONTEXT_VPID
:
544 utype
= LTTNG_UST_CONTEXT_VPID
;
546 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
547 utype
= LTTNG_UST_CONTEXT_PTHREAD_ID
;
549 case LTTNG_EVENT_CONTEXT_PROCNAME
:
550 utype
= LTTNG_UST_CONTEXT_PROCNAME
;
552 case LTTNG_EVENT_CONTEXT_IP
:
553 utype
= LTTNG_UST_CONTEXT_IP
;
555 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
556 if (!ustctl_has_perf_counters()) {
558 WARN("Perf counters not implemented in UST");
560 utype
= LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
;
563 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
564 utype
= LTTNG_UST_CONTEXT_APP_CONTEXT
;
566 case LTTNG_EVENT_CONTEXT_CGROUP_NS
:
567 utype
= LTTNG_UST_CONTEXT_CGROUP_NS
;
569 case LTTNG_EVENT_CONTEXT_IPC_NS
:
570 utype
= LTTNG_UST_CONTEXT_IPC_NS
;
572 case LTTNG_EVENT_CONTEXT_MNT_NS
:
573 utype
= LTTNG_UST_CONTEXT_MNT_NS
;
575 case LTTNG_EVENT_CONTEXT_NET_NS
:
576 utype
= LTTNG_UST_CONTEXT_NET_NS
;
578 case LTTNG_EVENT_CONTEXT_PID_NS
:
579 utype
= LTTNG_UST_CONTEXT_PID_NS
;
581 case LTTNG_EVENT_CONTEXT_USER_NS
:
582 utype
= LTTNG_UST_CONTEXT_USER_NS
;
584 case LTTNG_EVENT_CONTEXT_UTS_NS
:
585 utype
= LTTNG_UST_CONTEXT_UTS_NS
;
587 case LTTNG_EVENT_CONTEXT_VUID
:
588 utype
= LTTNG_UST_CONTEXT_VUID
;
590 case LTTNG_EVENT_CONTEXT_VEUID
:
591 utype
= LTTNG_UST_CONTEXT_VEUID
;
593 case LTTNG_EVENT_CONTEXT_VSUID
:
594 utype
= LTTNG_UST_CONTEXT_VSUID
;
596 case LTTNG_EVENT_CONTEXT_VGID
:
597 utype
= LTTNG_UST_CONTEXT_VGID
;
599 case LTTNG_EVENT_CONTEXT_VEGID
:
600 utype
= LTTNG_UST_CONTEXT_VEGID
;
602 case LTTNG_EVENT_CONTEXT_VSGID
:
603 utype
= LTTNG_UST_CONTEXT_VSGID
;
613 * Return 1 if contexts match, 0 otherwise.
615 int trace_ust_match_context(struct ltt_ust_context
*uctx
,
616 struct lttng_event_context
*ctx
)
620 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
624 if (uctx
->ctx
.ctx
!= utype
) {
628 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
629 if (uctx
->ctx
.u
.perf_counter
.type
630 != ctx
->u
.perf_counter
.type
) {
633 if (uctx
->ctx
.u
.perf_counter
.config
634 != ctx
->u
.perf_counter
.config
) {
637 if (strncmp(uctx
->ctx
.u
.perf_counter
.name
,
638 ctx
->u
.perf_counter
.name
,
639 LTTNG_UST_SYM_NAME_LEN
)) {
643 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
644 assert(uctx
->ctx
.u
.app_ctx
.provider_name
);
645 assert(uctx
->ctx
.u
.app_ctx
.ctx_name
);
646 if (strcmp(uctx
->ctx
.u
.app_ctx
.provider_name
,
647 ctx
->u
.app_ctx
.provider_name
) ||
648 strcmp(uctx
->ctx
.u
.app_ctx
.ctx_name
,
649 ctx
->u
.app_ctx
.ctx_name
)) {
660 * Allocate and initialize an UST context.
662 * Return pointer to structure or NULL.
664 struct ltt_ust_context
*trace_ust_create_context(
665 struct lttng_event_context
*ctx
)
667 struct ltt_ust_context
*uctx
= NULL
;
672 utype
= trace_ust_context_type_event_to_ust(ctx
->ctx
);
674 ERR("Invalid UST context");
678 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
680 PERROR("zmalloc ltt_ust_context");
684 uctx
->ctx
.ctx
= (enum lttng_ust_context_type
) utype
;
686 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
687 uctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
688 uctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
689 strncpy(uctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
690 LTTNG_UST_SYM_NAME_LEN
);
691 uctx
->ctx
.u
.perf_counter
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
693 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
695 char *provider_name
= NULL
, *ctx_name
= NULL
;
697 provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
698 if (!provider_name
) {
701 uctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
703 ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
707 uctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
713 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
717 trace_ust_destroy_context(uctx
);
722 void destroy_pid_tracker_node_rcu(struct rcu_head
*head
)
724 struct ust_pid_tracker_node
*tracker_node
=
725 caa_container_of(head
, struct ust_pid_tracker_node
, node
.head
);
730 void destroy_pid_tracker_node(struct ust_pid_tracker_node
*tracker_node
)
733 call_rcu(&tracker_node
->node
.head
, destroy_pid_tracker_node_rcu
);
737 int init_pid_tracker(struct ust_pid_tracker
*pid_tracker
)
741 pid_tracker
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
742 if (!pid_tracker
->ht
) {
752 * Teardown pid tracker content, but don't free pid_tracker object.
755 void fini_pid_tracker(struct ust_pid_tracker
*pid_tracker
)
757 struct ust_pid_tracker_node
*tracker_node
;
758 struct lttng_ht_iter iter
;
760 if (!pid_tracker
->ht
) {
764 cds_lfht_for_each_entry(pid_tracker
->ht
->ht
,
765 &iter
.iter
, tracker_node
, node
.node
) {
766 int ret
= lttng_ht_del(pid_tracker
->ht
, &iter
);
769 destroy_pid_tracker_node(tracker_node
);
772 ht_cleanup_push(pid_tracker
->ht
);
773 pid_tracker
->ht
= NULL
;
777 struct ust_pid_tracker_node
*pid_tracker_lookup(
778 struct ust_pid_tracker
*pid_tracker
, int pid
,
779 struct lttng_ht_iter
*iter
)
781 unsigned long _pid
= (unsigned long) pid
;
782 struct lttng_ht_node_ulong
*node
;
784 lttng_ht_lookup(pid_tracker
->ht
, (void *) _pid
, iter
);
785 node
= lttng_ht_iter_get_node_ulong(iter
);
787 return caa_container_of(node
, struct ust_pid_tracker_node
,
795 int pid_tracker_add_pid(struct ust_pid_tracker
*pid_tracker
, int pid
)
797 int retval
= LTTNG_OK
;
798 struct ust_pid_tracker_node
*tracker_node
;
799 struct lttng_ht_iter iter
;
802 retval
= LTTNG_ERR_INVALID
;
805 tracker_node
= pid_tracker_lookup(pid_tracker
, pid
, &iter
);
807 /* Already exists. */
808 retval
= LTTNG_ERR_PID_TRACKED
;
811 tracker_node
= zmalloc(sizeof(*tracker_node
));
813 retval
= LTTNG_ERR_NOMEM
;
816 lttng_ht_node_init_ulong(&tracker_node
->node
, (unsigned long) pid
);
817 lttng_ht_add_unique_ulong(pid_tracker
->ht
, &tracker_node
->node
);
823 int pid_tracker_del_pid(struct ust_pid_tracker
*pid_tracker
, int pid
)
825 int retval
= LTTNG_OK
, ret
;
826 struct ust_pid_tracker_node
*tracker_node
;
827 struct lttng_ht_iter iter
;
830 retval
= LTTNG_ERR_INVALID
;
833 tracker_node
= pid_tracker_lookup(pid_tracker
, pid
, &iter
);
836 retval
= LTTNG_ERR_PID_NOT_TRACKED
;
839 ret
= lttng_ht_del(pid_tracker
->ht
, &iter
);
842 destroy_pid_tracker_node(tracker_node
);
848 * The session lock is held when calling this function.
850 int trace_ust_pid_tracker_lookup(struct ltt_ust_session
*session
, int pid
)
852 struct lttng_ht_iter iter
;
854 if (!session
->pid_tracker
.ht
) {
857 if (pid_tracker_lookup(&session
->pid_tracker
, pid
, &iter
)) {
864 * Called with the session lock held.
866 int trace_ust_track_pid(struct ltt_ust_session
*session
, int pid
)
868 int retval
= LTTNG_OK
;
869 bool should_update_apps
= false;
872 /* Track all pids: destroy tracker if exists. */
873 if (session
->pid_tracker
.ht
) {
874 fini_pid_tracker(&session
->pid_tracker
);
875 /* Ensure all apps have session. */
876 should_update_apps
= true;
881 if (!session
->pid_tracker
.ht
) {
882 /* Create tracker. */
883 if (init_pid_tracker(&session
->pid_tracker
)) {
884 ERR("Error initializing PID tracker");
885 retval
= LTTNG_ERR_NOMEM
;
888 ret
= pid_tracker_add_pid(&session
->pid_tracker
, pid
);
889 if (ret
!= LTTNG_OK
) {
891 fini_pid_tracker(&session
->pid_tracker
);
894 /* Remove all apps from session except pid. */
895 should_update_apps
= true;
899 ret
= pid_tracker_add_pid(&session
->pid_tracker
, pid
);
900 if (ret
!= LTTNG_OK
) {
904 /* Add session to application */
905 app
= ust_app_find_by_pid(pid
);
907 should_update_apps
= true;
911 if (should_update_apps
&& session
->active
) {
912 ust_app_global_update_all(session
);
919 * Called with the session lock held.
921 int trace_ust_untrack_pid(struct ltt_ust_session
*session
, int pid
)
923 int retval
= LTTNG_OK
;
926 /* Create empty tracker, replace old tracker. */
927 struct ust_pid_tracker tmp_tracker
;
929 tmp_tracker
= session
->pid_tracker
;
930 if (init_pid_tracker(&session
->pid_tracker
)) {
931 ERR("Error initializing PID tracker");
932 retval
= LTTNG_ERR_NOMEM
;
933 /* Rollback operation. */
934 session
->pid_tracker
= tmp_tracker
;
937 fini_pid_tracker(&tmp_tracker
);
939 /* Remove session from all applications */
940 ust_app_global_update_all(session
);
945 if (!session
->pid_tracker
.ht
) {
946 /* No PID being tracked. */
947 retval
= LTTNG_ERR_PID_NOT_TRACKED
;
950 /* Remove PID from tracker */
951 ret
= pid_tracker_del_pid(&session
->pid_tracker
, pid
);
952 if (ret
!= LTTNG_OK
) {
956 /* Remove session from application. */
957 app
= ust_app_find_by_pid(pid
);
959 ust_app_global_update(session
, app
);
967 * Called with session lock held.
969 ssize_t
trace_ust_list_tracker_pids(struct ltt_ust_session
*session
,
972 struct ust_pid_tracker_node
*tracker_node
;
973 struct lttng_ht_iter iter
;
974 unsigned long count
, i
= 0;
979 if (!session
->pid_tracker
.ht
) {
980 /* Tracker disabled. Set first entry to -1. */
981 pids
= zmalloc(sizeof(*pids
));
992 cds_lfht_count_nodes(session
->pid_tracker
.ht
->ht
,
993 &approx
[0], &count
, &approx
[1]);
994 pids
= zmalloc(sizeof(*pids
) * count
);
999 cds_lfht_for_each_entry(session
->pid_tracker
.ht
->ht
,
1000 &iter
.iter
, tracker_node
, node
.node
) {
1001 pids
[i
++] = tracker_node
->node
.key
;
1011 * RCU safe free context structure.
1013 static void destroy_context_rcu(struct rcu_head
*head
)
1015 struct lttng_ht_node_ulong
*node
=
1016 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1017 struct ltt_ust_context
*ctx
=
1018 caa_container_of(node
, struct ltt_ust_context
, node
);
1020 trace_ust_destroy_context(ctx
);
1024 * Cleanup UST context hash table.
1026 static void destroy_contexts(struct lttng_ht
*ht
)
1029 struct lttng_ht_node_ulong
*node
;
1030 struct lttng_ht_iter iter
;
1031 struct ltt_ust_context
*ctx
;
1036 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
1037 /* Remove from ordered list. */
1038 ctx
= caa_container_of(node
, struct ltt_ust_context
, node
);
1039 cds_list_del(&ctx
->list
);
1040 /* Remove from channel's hash table. */
1041 ret
= lttng_ht_del(ht
, &iter
);
1043 call_rcu(&node
->head
, destroy_context_rcu
);
1048 ht_cleanup_push(ht
);
1052 * Cleanup ust event structure.
1054 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
1058 DBG2("Trace destroy UST event %s", event
->attr
.name
);
1059 free(event
->filter_expression
);
1060 free(event
->filter
);
1061 free(event
->exclusion
);
1066 * Cleanup ust context structure.
1068 void trace_ust_destroy_context(struct ltt_ust_context
*ctx
)
1072 if (ctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1073 free(ctx
->ctx
.u
.app_ctx
.provider_name
);
1074 free(ctx
->ctx
.u
.app_ctx
.ctx_name
);
1080 * URCU intermediate call to complete destroy event.
1082 static void destroy_event_rcu(struct rcu_head
*head
)
1084 struct lttng_ht_node_str
*node
=
1085 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1086 struct ltt_ust_event
*event
=
1087 caa_container_of(node
, struct ltt_ust_event
, node
);
1089 trace_ust_destroy_event(event
);
1093 * Cleanup UST events hashtable.
1095 static void destroy_events(struct lttng_ht
*events
)
1098 struct lttng_ht_node_str
*node
;
1099 struct lttng_ht_iter iter
;
1104 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
1105 ret
= lttng_ht_del(events
, &iter
);
1107 call_rcu(&node
->head
, destroy_event_rcu
);
1111 ht_cleanup_push(events
);
1115 * Cleanup ust channel structure.
1117 * Should _NOT_ be called with RCU read lock held.
1119 static void _trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1123 DBG2("Trace destroy UST channel %s", channel
->name
);
1129 * URCU intermediate call to complete destroy channel.
1131 static void destroy_channel_rcu(struct rcu_head
*head
)
1133 struct lttng_ht_node_str
*node
=
1134 caa_container_of(head
, struct lttng_ht_node_str
, head
);
1135 struct ltt_ust_channel
*channel
=
1136 caa_container_of(node
, struct ltt_ust_channel
, node
);
1138 _trace_ust_destroy_channel(channel
);
1141 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
1143 /* Destroying all events of the channel */
1144 destroy_events(channel
->events
);
1145 /* Destroying all context of the channel */
1146 destroy_contexts(channel
->ctx
);
1148 call_rcu(&channel
->node
.head
, destroy_channel_rcu
);
1152 * Remove an UST channel from a channel HT.
1154 void trace_ust_delete_channel(struct lttng_ht
*ht
,
1155 struct ltt_ust_channel
*channel
)
1158 struct lttng_ht_iter iter
;
1163 iter
.iter
.node
= &channel
->node
.node
;
1164 ret
= lttng_ht_del(ht
, &iter
);
1169 * Iterate over a hash table containing channels and cleanup safely.
1171 static void destroy_channels(struct lttng_ht
*channels
)
1173 struct lttng_ht_node_str
*node
;
1174 struct lttng_ht_iter iter
;
1179 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
1180 struct ltt_ust_channel
*chan
=
1181 caa_container_of(node
, struct ltt_ust_channel
, node
);
1183 trace_ust_delete_channel(channels
, chan
);
1184 trace_ust_destroy_channel(chan
);
1188 ht_cleanup_push(channels
);
1192 * Cleanup UST global domain.
1194 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
1198 destroy_channels(dom
->channels
);
1202 * Cleanup ust session structure
1204 * Should *NOT* be called with RCU read-side lock held.
1206 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
1209 struct buffer_reg_uid
*reg
, *sreg
;
1210 struct lttng_ht_iter iter
;
1214 DBG2("Trace UST destroy session %" PRIu64
, session
->id
);
1216 /* Cleaning up UST domain */
1217 destroy_domain_global(&session
->domain_global
);
1220 cds_lfht_for_each_entry(session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
1221 int ret
= lttng_ht_del(session
->agents
, &iter
);
1228 ht_cleanup_push(session
->agents
);
1230 /* Cleanup UID buffer registry object(s). */
1231 cds_list_for_each_entry_safe(reg
, sreg
, &session
->buffer_reg_uid_list
,
1233 cds_list_del(®
->lnode
);
1234 buffer_reg_uid_remove(reg
);
1235 buffer_reg_uid_destroy(reg
, session
->consumer
);
1238 consumer_output_put(session
->consumer
);
1240 fini_pid_tracker(&session
->pid_tracker
);