2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
10 #include <urcu/list.h>
13 #include <common/compat/errno.h>
14 #include <lttng/lttng.h>
15 #include <lttng/condition/condition.h>
16 #include <lttng/condition/event-rule-matches.h>
17 #include <lttng/event-rule/event-rule.h>
18 #include <lttng/event-rule/event-rule-internal.h>
19 #include <common/bytecode/bytecode.h>
20 #include <common/error.h>
21 #include <common/sessiond-comm/sessiond-comm.h>
22 #include <common/filter.h>
23 #include <common/context.h>
28 #include "lttng-sessiond.h"
29 #include "lttng-ust-ctl.h"
30 #include "lttng-ust-error.h"
32 #include "trace-kernel.h"
33 #include "trace-ust.h"
38 * Add unique UST event based on the event name, filter bytecode and loglevel.
40 static void add_unique_ust_event(struct lttng_ht
*ht
,
41 struct ltt_ust_event
*event
)
43 struct cds_lfht_node
*node_ptr
;
44 struct ltt_ust_ht_key key
;
50 key
.name
= event
->attr
.name
;
51 key
.filter
= (struct lttng_bytecode
*) event
->filter
;
52 key
.loglevel_type
= event
->attr
.loglevel_type
;
53 key
.loglevel_value
= event
->attr
.loglevel
;
54 key
.exclusion
= event
->exclusion
;
56 node_ptr
= cds_lfht_add_unique(ht
->ht
,
57 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
58 trace_ust_ht_match_event
, &key
, &event
->node
.node
);
59 assert(node_ptr
== &event
->node
.node
);
63 * Disable kernel tracepoint events for a channel from the kernel session of
64 * a specified event_name and event type.
65 * On type LTTNG_EVENT_ALL all events with event_name are disabled.
66 * If event_name is NULL all events of the specified type are disabled.
68 int event_kernel_disable_event(struct ltt_kernel_channel
*kchan
,
69 const char *event_name
, enum lttng_event_type type
)
71 int ret
, error
= 0, found
= 0;
72 struct ltt_kernel_event
*kevent
;
76 /* For each event in the kernel session */
77 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
78 if (type
!= LTTNG_EVENT_ALL
&& kevent
->type
!= type
)
80 if (event_name
!= NULL
&& strcmp(event_name
, kevent
->event
->name
)) {
84 ret
= kernel_disable_event(kevent
);
90 DBG("Disable kernel event: found %d events with name: %s and type: %d",
91 found
, event_name
? event_name
: "NULL", type
);
93 if (event_name
!= NULL
&& !found
) {
94 ret
= LTTNG_ERR_NO_EVENT
;
96 ret
= error
? LTTNG_ERR_KERN_DISABLE_FAIL
: LTTNG_OK
;
103 * Enable kernel tracepoint event for a channel from the kernel session.
104 * We own filter_expression and filter.
106 int event_kernel_enable_event(struct ltt_kernel_channel
*kchan
,
107 struct lttng_event
*event
, char *filter_expression
,
108 struct lttng_bytecode
*filter
)
111 struct ltt_kernel_event
*kevent
;
116 kevent
= trace_kernel_find_event(event
->name
, kchan
,
117 event
->type
, filter
);
118 if (kevent
== NULL
) {
119 ret
= kernel_create_event(event
, kchan
, filter_expression
, filter
);
120 /* We have passed ownership */
121 filter_expression
= NULL
;
126 } else if (kevent
->enabled
== 0) {
127 ret
= kernel_enable_event(kevent
);
129 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
133 /* At this point, the event is considered enabled */
134 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
140 free(filter_expression
);
146 * ============================
147 * UST : The Ultimate Frontier!
148 * ============================
152 * Enable UST tracepoint event for a channel from a UST session.
153 * We own filter_expression, filter, and exclusion.
155 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
,
156 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
157 char *filter_expression
,
158 struct lttng_bytecode
*filter
,
159 struct lttng_event_exclusion
*exclusion
,
162 int ret
= LTTNG_OK
, to_create
= 0;
163 struct ltt_ust_event
*uevent
;
171 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
172 (enum lttng_ust_abi_loglevel_type
) event
->loglevel_type
,
173 event
->loglevel
, exclusion
);
175 ret
= trace_ust_create_event(event
, filter_expression
,
176 filter
, exclusion
, internal_event
, &uevent
);
177 /* We have passed ownership */
178 filter_expression
= NULL
;
181 if (ret
!= LTTNG_OK
) {
185 /* Valid to set it after the goto error since uevent is still NULL */
189 if (uevent
->enabled
) {
190 /* It's already enabled so everything is OK */
192 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
198 /* Add ltt ust event to channel */
199 add_unique_ust_event(uchan
->events
, uevent
);
202 if (!usess
->active
) {
207 /* Create event on all UST registered apps for session */
208 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
210 /* Enable event on all UST registered apps for session */
211 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
215 if (ret
== -LTTNG_UST_ERR_EXIST
) {
216 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
219 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
224 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
225 to_create
? "created" : "enabled", uchan
->name
);
231 free(filter_expression
);
238 * Only destroy event on creation time (not enabling time) because if the
239 * event is found in the channel (to_create == 0), it means that at some
240 * point the enable_event worked and it's thus valid to keep it alive.
241 * Destroying it also implies that we also destroy it's shadow copy to sync
245 /* In this code path, the uevent was not added to the hash table */
246 trace_ust_destroy_event(uevent
);
249 free(filter_expression
);
256 * Disable UST tracepoint of a channel from a UST session.
258 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
259 struct ltt_ust_channel
*uchan
, const char *event_name
)
262 struct ltt_ust_event
*uevent
;
263 struct lttng_ht_node_str
*node
;
264 struct lttng_ht_iter iter
;
276 * We use a custom lookup since we need the iterator for the next_duplicate
277 * call in the do while loop below.
279 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
280 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
281 node
= lttng_ht_iter_get_node_str(&iter
);
283 DBG2("Trace UST event NOT found by name %s", event_name
);
284 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
289 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
292 if (uevent
->enabled
== 0) {
293 /* It's already disabled so everything is OK */
297 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
300 if (!usess
->active
) {
303 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
304 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
305 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
309 /* Get next duplicate event by name. */
310 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
311 event_name
, &iter
.iter
);
312 node
= lttng_ht_iter_get_node_str(&iter
);
323 * Disable all UST tracepoints for a channel from a UST session.
325 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
,
326 struct ltt_ust_channel
*uchan
)
328 int ret
, i
, size
, error
= 0;
329 struct lttng_ht_iter iter
;
330 struct ltt_ust_event
*uevent
= NULL
;
331 struct lttng_event
*events
= NULL
;
338 /* Disabling existing events */
339 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
341 if (uevent
->enabled
== 1) {
342 ret
= event_ust_disable_tracepoint(usess
, uchan
,
345 error
= LTTNG_ERR_UST_DISABLE_FAIL
;
351 /* Get all UST available events */
352 size
= ust_app_list_events(&events
);
354 ret
= LTTNG_ERR_UST_LIST_FAIL
;
358 for (i
= 0; i
< size
; i
++) {
359 ret
= event_ust_disable_tracepoint(usess
, uchan
,
362 /* Continue to disable the rest... */
363 error
= LTTNG_ERR_UST_DISABLE_FAIL
;
368 ret
= error
? error
: LTTNG_OK
;
375 static void agent_enable_all(struct agent
*agt
)
377 struct agent_event
*aevent
;
378 struct lttng_ht_iter iter
;
380 /* Flag every event as enabled. */
382 cds_lfht_for_each_entry (
383 agt
->events
->ht
, &iter
.iter
, aevent
, node
.node
) {
384 aevent
->enabled_count
++;
390 * Enable all agent event for a given UST session.
392 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
394 int event_agent_enable_all(struct ltt_ust_session
*usess
,
395 struct agent
*agt
, struct lttng_event
*event
,
396 struct lttng_bytecode
*filter
,char *filter_expression
)
402 DBG("Event agent enabling ALL events for session %" PRIu64
, usess
->id
);
404 /* Enable event on agent application through TCP socket. */
405 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
406 if (ret
!= LTTNG_OK
) {
410 agent_enable_all(agt
);
419 * Check if this event's filter requires the activation of application contexts
420 * and enable them in the agent.
421 * TODO: bytecode iterator does not support non-legacy application
422 * contexts yet. Not an issue for now, since they are not generated by
423 * the lttng-ctl library.
425 static int add_filter_app_ctx(struct lttng_bytecode
*bytecode
,
426 const char *filter_expression
, struct agent
*agt
)
429 char *provider_name
= NULL
, *ctx_name
= NULL
;
430 struct bytecode_symbol_iterator
*it
=
431 bytecode_symbol_iterator_create(bytecode
);
434 ret
= LTTNG_ERR_NOMEM
;
439 struct lttng_event_context ctx
;
440 const char *symbol_name
=
441 bytecode_symbol_iterator_get_name(it
);
443 if (parse_application_context(symbol_name
, &provider_name
,
445 /* Not an application context. */
449 ctx
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
450 ctx
.u
.app_ctx
.provider_name
= provider_name
;
451 ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
453 /* Recognized an application context. */
454 DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.",
455 filter_expression
, provider_name
, ctx_name
);
457 ret
= agent_add_context(&ctx
, agt
);
458 if (ret
!= LTTNG_OK
) {
459 ERR("Failed to add application context %s:%s.",
460 provider_name
, ctx_name
);
464 ret
= agent_enable_context(&ctx
, agt
->domain
);
465 if (ret
!= LTTNG_OK
) {
466 ERR("Failed to enable application context %s:%s.",
467 provider_name
, ctx_name
);
473 provider_name
= ctx_name
= NULL
;
474 } while (bytecode_symbol_iterator_next(it
) == 0);
478 bytecode_symbol_iterator_destroy(it
);
482 static int agent_enable(struct agent
*agt
,
483 struct lttng_event
*event
,
484 struct lttng_bytecode
*filter
,
485 char *filter_expression
)
487 int ret
, created
= 0;
488 struct agent_event
*aevent
;
493 aevent
= agent_find_event(event
->name
, event
->loglevel_type
,
494 event
->loglevel
, filter_expression
, agt
);
496 aevent
= agent_create_event(event
->name
, event
->loglevel_type
,
497 event
->loglevel
, filter
,
500 ret
= LTTNG_ERR_NOMEM
;
504 filter_expression
= NULL
;
506 assert(!AGENT_EVENT_IS_ENABLED(aevent
));
509 if (created
&& aevent
->filter
) {
510 ret
= add_filter_app_ctx(
511 aevent
->filter
, aevent
->filter_expression
, agt
);
512 if (ret
!= LTTNG_OK
) {
517 /* Already enabled? */
518 if (AGENT_EVENT_IS_ENABLED(aevent
)) {
523 ret
= agent_enable_event(aevent
, agt
->domain
);
524 if (ret
!= LTTNG_OK
) {
528 /* If the event was created prior to the enable, add it to the domain. */
530 agent_add_event(aevent
, agt
);
538 agent_destroy_event(aevent
);
542 free(filter_expression
);
547 * Enable a single agent event for a given UST session.
549 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
551 int event_agent_enable(struct ltt_ust_session
*usess
,
553 struct lttng_event
*event
,
554 struct lttng_bytecode
*filter
,
555 char *filter_expression
)
561 DBG("Enabling agent event: event pattern = '%s', session id = %" PRIu64
", loglevel type = %d, loglevel = %d, filter expression = '%s'",
562 event
->name
, usess
->id
, event
->loglevel_type
,
564 filter_expression
? filter_expression
: "(none)");
566 return agent_enable(agt
, event
, filter
, filter_expression
);
570 * Enable a single agent event for a trigger.
572 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
574 int trigger_agent_enable(const struct lttng_trigger
*trigger
, struct agent
*agt
)
577 enum lttng_condition_status c_status
;
578 enum lttng_trigger_status t_status
;
579 enum lttng_domain_type d_type
;
580 const struct lttng_condition
*condition
;
581 const struct lttng_event_rule
*rule
;
582 const char *filter_expression
;
583 char *filter_expression_copy
= NULL
;
584 const struct lttng_bytecode
*filter_bytecode
;
585 struct lttng_bytecode
*filter_bytecode_copy
= NULL
;
586 struct lttng_event
*event
= NULL
;
587 uid_t trigger_owner_uid
= 0;
588 const char *trigger_name
;
593 t_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
594 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
595 trigger_name
= "(anonymous)";
598 t_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner_uid
);
599 assert(t_status
== LTTNG_TRIGGER_STATUS_OK
);
601 condition
= lttng_trigger_get_const_condition(trigger
);
603 assert(lttng_condition_get_type(condition
) ==
604 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
606 c_status
= lttng_condition_event_rule_matches_get_rule(
608 assert(c_status
== LTTNG_CONDITION_STATUS_OK
);
610 switch (lttng_event_rule_get_type(rule
)) {
611 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
612 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
613 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
620 d_type
= lttng_event_rule_get_domain_type(rule
);
621 assert(d_type
== agt
->domain
);
623 event
= lttng_event_rule_generate_lttng_event(rule
);
625 ret
= LTTNG_ERR_NOMEM
;
629 /* Get the internal filter expression and bytecode. */
630 filter_expression
= lttng_event_rule_get_filter(rule
);
631 if (filter_expression
) {
632 filter_expression_copy
= strdup(filter_expression
);
633 if (!filter_expression_copy
) {
634 ret
= LTTNG_ERR_NOMEM
;
638 /* Get the filter bytecode */
639 filter_bytecode
= lttng_event_rule_get_filter_bytecode(rule
);
640 if (filter_bytecode
) {
641 filter_bytecode_copy
=
642 lttng_bytecode_copy(filter_bytecode
);
643 if (!filter_bytecode_copy
) {
644 ret
= LTTNG_ERR_NOMEM
;
650 DBG("Enabling agent event from trigger: trigger name = '%s', trigger owner uid = %d, token = %" PRIu64
,
651 trigger_name
, trigger_owner_uid
,
652 lttng_trigger_get_tracer_token(trigger
));
654 ret
= agent_enable(agt
, event
, filter_bytecode_copy
,
655 filter_expression_copy
);
656 /* Ownership was passed even in case of error. */
657 filter_expression_copy
= NULL
;
658 filter_bytecode_copy
= NULL
;
661 free(filter_expression_copy
);
662 free(filter_bytecode_copy
);
668 * Return the default event name associated with the provided UST domain. Return
671 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain
)
673 const char *default_event_name
= NULL
;
676 case LTTNG_DOMAIN_LOG4J
:
677 default_event_name
= DEFAULT_LOG4J_EVENT_NAME
;
679 case LTTNG_DOMAIN_JUL
:
680 default_event_name
= DEFAULT_JUL_EVENT_NAME
;
682 case LTTNG_DOMAIN_PYTHON
:
683 default_event_name
= DEFAULT_PYTHON_EVENT_NAME
;
689 return default_event_name
;
692 static int trigger_agent_disable_one(const struct lttng_trigger
*trigger
,
694 struct agent_event
*aevent
)
704 * Actual ust event un-registration happens on the trigger
705 * un-registration at that point.
708 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for trigger %" PRIu64
,
709 aevent
->name
, aevent
->loglevel_type
,
710 aevent
->loglevel_value
, lttng_trigger_get_tracer_token(trigger
));
712 /* Already disabled? */
713 if (!AGENT_EVENT_IS_ENABLED(aevent
)) {
717 ret
= agent_disable_event(aevent
, agt
->domain
);
718 if (ret
!= LTTNG_OK
) {
730 * Disable a given agent event for a given UST session.
732 * Must be called with the RCU read lock held.
733 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
735 static int event_agent_disable_one(struct ltt_ust_session
*usess
,
736 struct agent
*agt
, struct agent_event
*aevent
)
739 struct ltt_ust_event
*uevent
= NULL
;
740 struct ltt_ust_channel
*uchan
= NULL
;
741 const char *ust_event_name
, *ust_channel_name
;
747 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64
,
748 aevent
->name
, aevent
->loglevel_type
, aevent
->loglevel_value
,
751 /* Already disabled? */
752 if (!AGENT_EVENT_IS_ENABLED(aevent
)) {
756 if (agt
->domain
== LTTNG_DOMAIN_JUL
) {
757 ust_channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
758 } else if (agt
->domain
== LTTNG_DOMAIN_LOG4J
) {
759 ust_channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
760 } else if (agt
->domain
== LTTNG_DOMAIN_PYTHON
) {
761 ust_channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
763 ret
= LTTNG_ERR_INVALID
;
768 * Disable it on the UST side. First get the channel reference then find
769 * the event and finally disable it.
771 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
772 (char *) ust_channel_name
);
774 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
778 ust_event_name
= event_get_default_agent_ust_name(agt
->domain
);
779 if (!ust_event_name
) {
780 ret
= LTTNG_ERR_FATAL
;
785 * Agent UST event has its loglevel type forced to
786 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
787 * happens thanks to an UST filter. The following -1 is actually
788 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
790 uevent
= trace_ust_find_event(uchan
->events
, (char *) ust_event_name
,
791 aevent
->filter
, LTTNG_UST_ABI_LOGLEVEL_ALL
, -1, NULL
);
792 /* If the agent event exists, it must be available on the UST side. */
796 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
797 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
798 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
804 * Flag event that it's disabled so the shadow copy on the ust app side
805 * will disable it if an application shows up.
809 ret
= agent_disable_event(aevent
, agt
->domain
);
810 if (ret
!= LTTNG_OK
) {
822 * Disable agent event matching a given trigger.
824 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
826 int trigger_agent_disable(
827 const struct lttng_trigger
*trigger
, struct agent
*agt
)
830 struct agent_event
*aevent
;
835 DBG("Event agent disabling for trigger %" PRIu64
,
836 lttng_trigger_get_tracer_token(trigger
));
839 aevent
= agent_find_event_by_trigger(trigger
, agt
);
841 if (aevent
== NULL
) {
842 DBG2("Event agent NOT found by trigger %" PRIu64
,
843 lttng_trigger_get_tracer_token(trigger
));
844 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
848 ret
= trigger_agent_disable_one(trigger
, agt
, aevent
);
850 if (ret
!= LTTNG_OK
) {
860 * Disable all agent events matching a given name for a given UST session.
862 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
864 int event_agent_disable(struct ltt_ust_session
*usess
, struct agent
*agt
,
865 const char *event_name
)
868 struct agent_event
*aevent
;
869 struct lttng_ht_iter iter
;
870 struct lttng_ht_node_str
*node
;
876 DBG("Event agent disabling %s (all loglevels) for session %" PRIu64
, event_name
, usess
->id
);
879 agent_find_events_by_name(event_name
, agt
, &iter
);
880 node
= lttng_ht_iter_get_node_str(&iter
);
883 DBG2("Event agent NOT found by name %s", event_name
);
884 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
889 aevent
= caa_container_of(node
, struct agent_event
, node
);
890 ret
= event_agent_disable_one(usess
, agt
, aevent
);
892 if (ret
!= LTTNG_OK
) {
896 /* Get next duplicate agent event by name. */
897 agent_event_next_duplicate(event_name
, agt
, &iter
);
898 node
= lttng_ht_iter_get_node_str(&iter
);
905 * Disable all agent event for a given UST session.
907 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
909 int event_agent_disable_all(struct ltt_ust_session
*usess
,
913 struct agent_event
*aevent
;
914 struct lttng_ht_iter iter
;
920 * Disable event on agent application. Continue to disable all other events
921 * if the * event is not found.
923 ret
= event_agent_disable(usess
, agt
, "*");
924 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
928 /* Disable every event. */
930 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
932 if (!AGENT_EVENT_IS_ENABLED(aevent
)) {
936 ret
= event_agent_disable(usess
, agt
, aevent
->name
);
937 if (ret
!= LTTNG_OK
) {