2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
11 #include "channel.hpp"
14 #include "lttng-sessiond.hpp"
15 #include "lttng-ust-ctl.hpp"
16 #include "lttng-ust-error.hpp"
17 #include "trace-kernel.hpp"
18 #include "trace-ust.hpp"
19 #include "ust-app.hpp"
22 #include <common/bytecode/bytecode.hpp>
23 #include <common/compat/errno.hpp>
24 #include <common/context.hpp>
25 #include <common/error.hpp>
26 #include <common/filter.hpp>
27 #include <common/sessiond-comm/sessiond-comm.hpp>
29 #include <lttng/condition/condition.h>
30 #include <lttng/condition/event-rule-matches.h>
31 #include <lttng/event-rule/event-rule-internal.hpp>
32 #include <lttng/event-rule/event-rule.h>
33 #include <lttng/lttng.h>
36 #include <urcu/list.h>
39 * Add unique UST event based on the event name, filter bytecode and loglevel.
41 static void add_unique_ust_event(struct lttng_ht
*ht
, 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
= (lttng_ust_abi_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
,
61 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
65 * Disable kernel tracepoint events for a channel from the kernel session of
66 * a specified event_name and event type.
67 * On type LTTNG_EVENT_ALL all events with event_name are disabled.
68 * If event_name is NULL all events of the specified type are disabled.
70 int event_kernel_disable_event(struct ltt_kernel_channel
*kchan
,
71 const char *event_name
,
72 enum lttng_event_type type
)
74 int ret
, error
= 0, found
= 0;
75 struct ltt_kernel_event
*kevent
;
79 /* For each event in the kernel session */
80 cds_list_for_each_entry (kevent
, &kchan
->events_list
.head
, list
) {
81 if (type
!= LTTNG_EVENT_ALL
&& kevent
->type
!= type
)
83 if (event_name
!= nullptr && strcmp(event_name
, kevent
->event
->name
) != 0) {
87 ret
= kernel_disable_event(kevent
);
93 DBG("Disable kernel event: found %d events with name: %s and type: %d",
95 event_name
? event_name
: "NULL",
98 if (event_name
!= nullptr && !found
) {
99 ret
= LTTNG_ERR_NO_EVENT
;
101 ret
= error
? LTTNG_ERR_KERN_DISABLE_FAIL
: LTTNG_OK
;
108 * Enable kernel tracepoint event for a channel from the kernel session.
109 * We own filter_expression and filter.
111 int event_kernel_enable_event(struct ltt_kernel_channel
*kchan
,
112 struct lttng_event
*event
,
113 char *filter_expression
,
114 struct lttng_bytecode
*filter
)
117 struct ltt_kernel_event
*kevent
;
122 kevent
= trace_kernel_find_event(event
->name
, kchan
, event
->type
, filter
);
123 if (kevent
== nullptr) {
124 ret
= kernel_create_event(event
, kchan
, filter_expression
, filter
);
125 /* We have passed ownership */
126 filter_expression
= nullptr;
131 } else if (kevent
->enabled
== 0) {
132 ret
= kernel_enable_event(kevent
);
134 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
138 /* At this point, the event is considered enabled */
139 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
145 free(filter_expression
);
151 * ============================
152 * UST : The Ultimate Frontier!
153 * ============================
157 * Enable UST tracepoint event for a channel from a UST session.
158 * We own filter_expression, filter, and exclusion.
160 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
,
161 struct ltt_ust_channel
*uchan
,
162 struct lttng_event
*event
,
163 char *filter_expression
,
164 struct lttng_bytecode
*filter
,
165 struct lttng_event_exclusion
*exclusion
,
168 int ret
= LTTNG_OK
, to_create
= 0;
169 struct ltt_ust_event
*uevent
;
177 uevent
= trace_ust_find_event(uchan
->events
,
180 (enum lttng_ust_abi_loglevel_type
) event
->loglevel_type
,
184 ret
= trace_ust_create_event(
185 event
, filter_expression
, filter
, exclusion
, internal_event
, &uevent
);
186 /* We have passed ownership */
187 filter_expression
= nullptr;
190 if (ret
!= LTTNG_OK
) {
194 /* Valid to set it after the goto error since uevent is still NULL */
198 if (uevent
->enabled
) {
199 /* It's already enabled so everything is OK */
200 LTTNG_ASSERT(!to_create
);
201 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
207 /* Add ltt ust event to channel */
208 add_unique_ust_event(uchan
->events
, uevent
);
211 if (!usess
->active
) {
216 /* Create event on all UST registered apps for session */
217 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
219 /* Enable event on all UST registered apps for session */
220 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
224 if (ret
== -LTTNG_UST_ERR_EXIST
) {
225 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
227 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
232 DBG("Event UST %s %s in channel %s",
234 to_create
? "created" : "enabled",
241 free(filter_expression
);
248 * Disable UST tracepoint of a channel from a UST session.
250 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
251 struct ltt_ust_channel
*uchan
,
252 const char *event_name
)
255 struct ltt_ust_event
*uevent
;
256 struct lttng_ht_node_str
*node
;
257 struct lttng_ht_iter iter
;
262 LTTNG_ASSERT(event_name
);
269 * We use a custom lookup since we need the iterator for the next_duplicate
270 * call in the do while loop below.
272 cds_lfht_lookup(ht
->ht
,
273 ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
274 trace_ust_ht_match_event_by_name
,
277 node
= lttng_ht_iter_get_node_str(&iter
);
278 if (node
== nullptr) {
279 DBG2("Trace UST event NOT found by name %s", event_name
);
280 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
285 uevent
= lttng::utils::container_of(node
, <t_ust_event::node
);
286 LTTNG_ASSERT(uevent
);
288 if (uevent
->enabled
== 0) {
289 /* It's already disabled so everything is OK */
293 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
, uchan
->name
);
295 if (!usess
->active
) {
298 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
299 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
300 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
304 /* Get next duplicate event by name. */
305 cds_lfht_next_duplicate(
306 ht
->ht
, trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
307 node
= lttng_ht_iter_get_node_str(&iter
);
318 * Disable all UST tracepoints for a channel from a UST session.
320 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
322 int ret
, i
, size
, error
= 0;
323 struct lttng_ht_iter iter
;
324 struct ltt_ust_event
*uevent
= nullptr;
325 struct lttng_event
*events
= nullptr;
332 /* Disabling existing events */
333 cds_lfht_for_each_entry (uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
334 if (uevent
->enabled
== 1) {
335 ret
= event_ust_disable_tracepoint(usess
, uchan
, uevent
->attr
.name
);
337 error
= LTTNG_ERR_UST_DISABLE_FAIL
;
343 /* Get all UST available events */
344 size
= ust_app_list_events(&events
);
346 ret
= LTTNG_ERR_UST_LIST_FAIL
;
350 for (i
= 0; i
< size
; i
++) {
351 ret
= event_ust_disable_tracepoint(usess
, uchan
, events
[i
].name
);
353 /* Continue to disable the rest... */
354 error
= LTTNG_ERR_UST_DISABLE_FAIL
;
359 ret
= error
? error
: LTTNG_OK
;
366 static void agent_enable_all(struct agent
*agt
)
368 struct agent_event
*aevent
;
369 struct lttng_ht_iter iter
;
371 /* Flag every event as enabled. */
373 cds_lfht_for_each_entry (agt
->events
->ht
, &iter
.iter
, aevent
, node
.node
) {
374 aevent
->enabled_count
++;
380 * Enable all agent event for a given UST session.
382 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
384 int event_agent_enable_all(struct ltt_ust_session
*usess
,
386 struct lttng_event
*event
,
387 struct lttng_bytecode
*filter
,
388 char *filter_expression
)
394 DBG("Event agent enabling ALL events for session %" PRIu64
, usess
->id
);
396 /* Enable event on agent application through TCP socket. */
397 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
398 if (ret
!= LTTNG_OK
) {
402 agent_enable_all(agt
);
411 * Check if this event's filter requires the activation of application contexts
412 * and enable them in the agent.
413 * TODO: bytecode iterator does not support non-legacy application
414 * contexts yet. Not an issue for now, since they are not generated by
415 * the lttng-ctl library.
417 static int add_filter_app_ctx(struct lttng_bytecode
*bytecode
,
418 const char *filter_expression
,
422 char *provider_name
= nullptr, *ctx_name
= nullptr;
423 struct bytecode_symbol_iterator
*it
= bytecode_symbol_iterator_create(bytecode
);
426 ret
= LTTNG_ERR_NOMEM
;
431 struct lttng_event_context ctx
;
432 const char *symbol_name
= bytecode_symbol_iterator_get_name(it
);
434 if (parse_application_context(symbol_name
, &provider_name
, &ctx_name
)) {
435 /* Not an application context. */
439 ctx
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
440 ctx
.u
.app_ctx
.provider_name
= provider_name
;
441 ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
443 /* Recognized an application context. */
444 DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.",
449 ret
= agent_add_context(&ctx
, agt
);
450 if (ret
!= LTTNG_OK
) {
451 ERR("Failed to add application context %s:%s.", provider_name
, ctx_name
);
455 ret
= agent_enable_context(&ctx
, agt
->domain
);
456 if (ret
!= LTTNG_OK
) {
457 ERR("Failed to enable application context %s:%s.", provider_name
, ctx_name
);
463 provider_name
= ctx_name
= nullptr;
464 } while (bytecode_symbol_iterator_next(it
) == 0);
468 bytecode_symbol_iterator_destroy(it
);
472 static int agent_enable(struct agent
*agt
,
473 struct lttng_event
*event
,
474 struct lttng_bytecode
*filter
,
475 char *filter_expression
)
477 int ret
, created
= 0;
478 struct agent_event
*aevent
;
484 aevent
= agent_find_event(
485 event
->name
, event
->loglevel_type
, event
->loglevel
, filter_expression
, agt
);
487 aevent
= agent_create_event(event
->name
,
488 event
->loglevel_type
,
493 ret
= LTTNG_ERR_NOMEM
;
497 filter_expression
= nullptr;
499 LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(aevent
));
502 if (created
&& aevent
->filter
) {
503 ret
= add_filter_app_ctx(aevent
->filter
, aevent
->filter_expression
, agt
);
504 if (ret
!= LTTNG_OK
) {
509 /* Already enabled? */
510 if (AGENT_EVENT_IS_ENABLED(aevent
)) {
515 ret
= agent_enable_event(aevent
, agt
->domain
);
516 if (ret
!= LTTNG_OK
) {
520 /* If the event was created prior to the enable, add it to the domain. */
522 agent_add_event(aevent
, agt
);
530 agent_destroy_event(aevent
);
535 free(filter_expression
);
540 * Enable a single agent event for a given UST session.
542 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
544 int event_agent_enable(struct ltt_ust_session
*usess
,
546 struct lttng_event
*event
,
547 struct lttng_bytecode
*filter
,
548 char *filter_expression
)
554 DBG("Enabling agent event: event pattern = '%s', session id = %" PRIu64
555 ", loglevel type = %d, loglevel = %d, filter expression = '%s'",
558 event
->loglevel_type
,
560 filter_expression
? filter_expression
: "(none)");
562 return agent_enable(agt
, event
, filter
, filter_expression
);
566 * Enable a single agent event for a trigger.
568 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
570 int trigger_agent_enable(const struct lttng_trigger
*trigger
, struct agent
*agt
)
573 enum lttng_condition_status c_status
;
574 enum lttng_trigger_status t_status
;
575 enum lttng_domain_type d_type
;
576 const struct lttng_condition
*condition
;
577 const struct lttng_event_rule
*rule
;
578 const char *filter_expression
;
579 char *filter_expression_copy
= nullptr;
580 const struct lttng_bytecode
*filter_bytecode
;
581 struct lttng_bytecode
*filter_bytecode_copy
= nullptr;
582 struct lttng_event
*event
= nullptr;
583 uid_t trigger_owner_uid
= 0;
584 const char *trigger_name
;
586 LTTNG_ASSERT(trigger
);
589 t_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
590 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
591 trigger_name
= "(anonymous)";
594 t_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner_uid
);
595 LTTNG_ASSERT(t_status
== LTTNG_TRIGGER_STATUS_OK
);
597 condition
= lttng_trigger_get_const_condition(trigger
);
599 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
600 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
602 c_status
= lttng_condition_event_rule_matches_get_rule(condition
, &rule
);
603 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
605 switch (lttng_event_rule_get_type(rule
)) {
606 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
607 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
608 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
615 d_type
= lttng_event_rule_get_domain_type(rule
);
616 LTTNG_ASSERT(d_type
== agt
->domain
);
618 event
= lttng_event_rule_generate_lttng_event(rule
);
620 ret
= LTTNG_ERR_NOMEM
;
624 /* Get the internal filter expression and bytecode. */
625 filter_expression
= lttng_event_rule_get_filter(rule
);
626 if (filter_expression
) {
627 filter_expression_copy
= strdup(filter_expression
);
628 if (!filter_expression_copy
) {
629 ret
= LTTNG_ERR_NOMEM
;
633 /* Get the filter bytecode */
634 filter_bytecode
= lttng_event_rule_get_filter_bytecode(rule
);
635 if (filter_bytecode
) {
636 filter_bytecode_copy
= lttng_bytecode_copy(filter_bytecode
);
637 if (!filter_bytecode_copy
) {
638 ret
= LTTNG_ERR_NOMEM
;
644 DBG("Enabling agent event from trigger: trigger name = '%s', trigger owner uid = %d, token = %" PRIu64
,
647 lttng_trigger_get_tracer_token(trigger
));
649 ret
= agent_enable(agt
, event
, filter_bytecode_copy
, filter_expression_copy
);
650 /* Ownership was passed even in case of error. */
651 filter_expression_copy
= nullptr;
652 filter_bytecode_copy
= nullptr;
655 free(filter_expression_copy
);
656 free(filter_bytecode_copy
);
662 * Return the default event name associated with the provided UST domain. Return
665 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain
)
667 const char *default_event_name
= nullptr;
670 case LTTNG_DOMAIN_LOG4J
:
671 default_event_name
= DEFAULT_LOG4J_EVENT_NAME
;
673 case LTTNG_DOMAIN_JUL
:
674 default_event_name
= DEFAULT_JUL_EVENT_NAME
;
676 case LTTNG_DOMAIN_PYTHON
:
677 default_event_name
= DEFAULT_PYTHON_EVENT_NAME
;
683 return default_event_name
;
686 static int trigger_agent_disable_one(const struct lttng_trigger
*trigger
,
688 struct agent_event
*aevent
)
694 LTTNG_ASSERT(trigger
);
695 LTTNG_ASSERT(aevent
);
698 * Actual ust event un-registration happens on the trigger
699 * un-registration at that point.
702 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for trigger %" PRIu64
,
704 aevent
->loglevel_type
,
705 aevent
->loglevel_value
,
706 lttng_trigger_get_tracer_token(trigger
));
708 /* Already disabled? */
709 if (!AGENT_EVENT_IS_ENABLED(aevent
)) {
713 ret
= agent_disable_event(aevent
, agt
->domain
);
714 if (ret
!= LTTNG_OK
) {
726 * Disable a given agent event for a given UST session.
728 * Must be called with the RCU read lock held.
729 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
731 static int event_agent_disable_one(struct ltt_ust_session
*usess
,
733 struct agent_event
*aevent
)
736 struct ltt_ust_event
*uevent
= nullptr;
737 struct ltt_ust_channel
*uchan
= nullptr;
738 const char *ust_event_name
, *ust_channel_name
;
742 LTTNG_ASSERT(aevent
);
744 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64
,
746 aevent
->loglevel_type
,
747 aevent
->loglevel_value
,
750 /* Already disabled? */
751 if (!AGENT_EVENT_IS_ENABLED(aevent
)) {
755 if (agt
->domain
== LTTNG_DOMAIN_JUL
) {
756 ust_channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
757 } else if (agt
->domain
== LTTNG_DOMAIN_LOG4J
) {
758 ust_channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
759 } else if (agt
->domain
== LTTNG_DOMAIN_PYTHON
) {
760 ust_channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
762 ret
= LTTNG_ERR_INVALID
;
767 * Disable it on the UST side. First get the channel reference then find
768 * the event and finally disable it.
770 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
771 (char *) ust_channel_name
);
773 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
777 ust_event_name
= event_get_default_agent_ust_name(agt
->domain
);
778 if (!ust_event_name
) {
779 ret
= LTTNG_ERR_FATAL
;
784 * Agent UST event has its loglevel type forced to
785 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
786 * happens thanks to an UST filter. The following -1 is actually
787 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
789 uevent
= trace_ust_find_event(uchan
->events
,
790 (char *) ust_event_name
,
792 LTTNG_UST_ABI_LOGLEVEL_ALL
,
795 /* If the agent event exists, it must be available on the UST side. */
796 LTTNG_ASSERT(uevent
);
799 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
800 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
801 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
807 * Flag event that it's disabled so the shadow copy on the ust app side
808 * will disable it if an application shows up.
812 ret
= agent_disable_event(aevent
, agt
->domain
);
813 if (ret
!= LTTNG_OK
) {
825 * Disable agent event matching a given trigger.
827 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
829 int trigger_agent_disable(const struct lttng_trigger
*trigger
, struct agent
*agt
)
832 struct agent_event
*aevent
;
834 LTTNG_ASSERT(trigger
);
837 DBG("Event agent disabling for trigger %" PRIu64
, lttng_trigger_get_tracer_token(trigger
));
840 aevent
= agent_find_event_by_trigger(trigger
, agt
);
842 if (aevent
== nullptr) {
843 DBG2("Event agent NOT found by trigger %" PRIu64
,
844 lttng_trigger_get_tracer_token(trigger
));
845 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
849 ret
= trigger_agent_disable_one(trigger
, agt
, aevent
);
851 if (ret
!= LTTNG_OK
) {
861 * Disable all agent events matching a given name for a given UST session.
863 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
865 int event_agent_disable(struct ltt_ust_session
*usess
, struct agent
*agt
, const char *event_name
)
868 struct agent_event
*aevent
;
869 struct lttng_ht_iter iter
;
870 struct lttng_ht_node_str
*node
;
874 LTTNG_ASSERT(event_name
);
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
);
882 if (node
== nullptr) {
883 DBG2("Event agent NOT found by name %s", event_name
);
884 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
889 aevent
= lttng::utils::container_of(node
, &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
, struct agent
*agt
)
912 struct agent_event
*aevent
;
913 struct lttng_ht_iter iter
;
919 * Disable event on agent application. Continue to disable all other events
920 * if the * event is not found.
922 ret
= event_agent_disable(usess
, agt
, "*");
923 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
927 /* Disable every event. */
929 cds_lfht_for_each_entry (agt
->events
->ht
, &iter
.iter
, aevent
, node
.node
) {
930 if (!AGENT_EVENT_IS_ENABLED(aevent
)) {
934 ret
= event_agent_disable(usess
, agt
, aevent
->name
);
935 if (ret
!= LTTNG_OK
) {