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
11 #include <urcu/list.h>
14 #include <lttng/lttng.h>
15 #include <common/error.h>
16 #include <common/sessiond-comm/sessiond-comm.h>
17 #include <common/filter.h>
18 #include <common/context.h>
23 #include "lttng-sessiond.h"
24 #include "lttng-ust-ctl.h"
25 #include "lttng-ust-error.h"
27 #include "trace-kernel.h"
28 #include "trace-ust.h"
32 * Add unique UST event based on the event name, filter bytecode and loglevel.
34 static void add_unique_ust_event(struct lttng_ht
*ht
,
35 struct ltt_ust_event
*event
)
37 struct cds_lfht_node
*node_ptr
;
38 struct ltt_ust_ht_key key
;
44 key
.name
= event
->attr
.name
;
45 key
.filter
= (struct lttng_filter_bytecode
*) event
->filter
;
46 key
.loglevel_type
= event
->attr
.loglevel_type
;
47 key
.loglevel_value
= event
->attr
.loglevel
;
48 key
.exclusion
= event
->exclusion
;
50 node_ptr
= cds_lfht_add_unique(ht
->ht
,
51 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
52 trace_ust_ht_match_event
, &key
, &event
->node
.node
);
53 assert(node_ptr
== &event
->node
.node
);
57 * Disable kernel tracepoint events for a channel from the kernel session of
58 * a specified event_name and event type.
59 * On type LTTNG_EVENT_ALL all events with event_name are disabled.
60 * If event_name is NULL all events of the specified type are disabled.
62 int event_kernel_disable_event(struct ltt_kernel_channel
*kchan
,
63 const char *event_name
, enum lttng_event_type type
)
65 int ret
, error
= 0, found
= 0;
66 struct ltt_kernel_event
*kevent
;
70 /* For each event in the kernel session */
71 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
72 if (type
!= LTTNG_EVENT_ALL
&& kevent
->type
!= type
)
74 if (event_name
!= NULL
&& strcmp(event_name
, kevent
->event
->name
)) {
78 ret
= kernel_disable_event(kevent
);
84 DBG("Disable kernel event: found %d events with name: %s and type: %d",
85 found
, event_name
? event_name
: "NULL", type
);
87 if (event_name
!= NULL
&& !found
) {
88 ret
= LTTNG_ERR_NO_EVENT
;
90 ret
= error
? LTTNG_ERR_KERN_DISABLE_FAIL
: LTTNG_OK
;
97 * Enable kernel tracepoint event for a channel from the kernel session.
98 * We own filter_expression and filter.
100 int event_kernel_enable_event(struct ltt_kernel_channel
*kchan
,
101 struct lttng_event
*event
, char *filter_expression
,
102 struct lttng_filter_bytecode
*filter
)
105 struct ltt_kernel_event
*kevent
;
110 kevent
= trace_kernel_find_event(event
->name
, kchan
,
111 event
->type
, filter
);
112 if (kevent
== NULL
) {
113 ret
= kernel_create_event(event
, kchan
, filter_expression
, filter
);
114 /* We have passed ownership */
115 filter_expression
= NULL
;
120 } else if (kevent
->enabled
== 0) {
121 ret
= kernel_enable_event(kevent
);
123 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
127 /* At this point, the event is considered enabled */
128 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
134 free(filter_expression
);
140 * ============================
141 * UST : The Ultimate Frontier!
142 * ============================
146 * Enable UST tracepoint event for a channel from a UST session.
147 * We own filter_expression, filter, and exclusion.
149 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
,
150 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
151 char *filter_expression
,
152 struct lttng_filter_bytecode
*filter
,
153 struct lttng_event_exclusion
*exclusion
,
156 int ret
= LTTNG_OK
, to_create
= 0;
157 struct ltt_ust_event
*uevent
;
165 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
166 (enum lttng_ust_loglevel_type
) event
->loglevel_type
,
167 event
->loglevel
, exclusion
);
169 ret
= trace_ust_create_event(event
, filter_expression
,
170 filter
, exclusion
, internal_event
, &uevent
);
171 /* We have passed ownership */
172 filter_expression
= NULL
;
175 if (ret
!= LTTNG_OK
) {
179 /* Valid to set it after the goto error since uevent is still NULL */
183 if (uevent
->enabled
) {
184 /* It's already enabled so everything is OK */
186 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
192 /* Add ltt ust event to channel */
193 add_unique_ust_event(uchan
->events
, uevent
);
196 if (!usess
->active
) {
201 /* Create event on all UST registered apps for session */
202 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
204 /* Enable event on all UST registered apps for session */
205 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
209 if (ret
== -LTTNG_UST_ERR_EXIST
) {
210 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
213 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
218 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
219 to_create
? "created" : "enabled", uchan
->name
);
225 free(filter_expression
);
232 * Only destroy event on creation time (not enabling time) because if the
233 * event is found in the channel (to_create == 0), it means that at some
234 * point the enable_event worked and it's thus valid to keep it alive.
235 * Destroying it also implies that we also destroy it's shadow copy to sync
239 /* In this code path, the uevent was not added to the hash table */
240 trace_ust_destroy_event(uevent
);
243 free(filter_expression
);
250 * Disable UST tracepoint of a channel from a UST session.
252 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
253 struct ltt_ust_channel
*uchan
, const char *event_name
)
256 struct ltt_ust_event
*uevent
;
257 struct lttng_ht_node_str
*node
;
258 struct lttng_ht_iter iter
;
270 * We use a custom lookup since we need the iterator for the next_duplicate
271 * call in the do while loop below.
273 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
274 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
275 node
= lttng_ht_iter_get_node_str(&iter
);
277 DBG2("Trace UST event NOT found by name %s", event_name
);
278 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
283 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
286 if (uevent
->enabled
== 0) {
287 /* It's already disabled so everything is OK */
291 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
294 if (!usess
->active
) {
297 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
298 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
299 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
303 /* Get next duplicate event by name. */
304 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
305 event_name
, &iter
.iter
);
306 node
= lttng_ht_iter_get_node_str(&iter
);
317 * Disable all UST tracepoints for a channel from a UST session.
319 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
,
320 struct ltt_ust_channel
*uchan
)
322 int ret
, i
, size
, error
= 0;
323 struct lttng_ht_iter iter
;
324 struct ltt_ust_event
*uevent
= NULL
;
325 struct lttng_event
*events
= NULL
;
332 /* Disabling existing events */
333 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
335 if (uevent
->enabled
== 1) {
336 ret
= event_ust_disable_tracepoint(usess
, uchan
,
339 error
= LTTNG_ERR_UST_DISABLE_FAIL
;
345 /* Get all UST available events */
346 size
= ust_app_list_events(&events
);
348 ret
= LTTNG_ERR_UST_LIST_FAIL
;
352 for (i
= 0; i
< size
; i
++) {
353 ret
= event_ust_disable_tracepoint(usess
, uchan
,
356 /* Continue to disable the rest... */
357 error
= LTTNG_ERR_UST_DISABLE_FAIL
;
362 ret
= error
? error
: LTTNG_OK
;
370 * Enable all agent event for a given UST session.
372 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
374 int event_agent_enable_all(struct ltt_ust_session
*usess
,
375 struct agent
*agt
, struct lttng_event
*event
,
376 struct lttng_filter_bytecode
*filter
,char *filter_expression
)
379 struct agent_event
*aevent
;
380 struct lttng_ht_iter iter
;
384 DBG("Event agent enabling ALL events for session %" PRIu64
, usess
->id
);
386 /* Enable event on agent application through TCP socket. */
387 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
388 if (ret
!= LTTNG_OK
) {
392 /* Flag every event that they are now enabled. */
394 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
407 * Check if this event's filter requires the activation of application contexts
408 * and enable them in the agent.
409 * TODO: bytecode iterator does not support non-legacy application
410 * contexts yet. Not an issue for now, since they are not generated by
411 * the lttng-ctl library.
413 static int add_filter_app_ctx(struct lttng_filter_bytecode
*bytecode
,
414 const char *filter_expression
, struct agent
*agt
)
417 char *provider_name
= NULL
, *ctx_name
= NULL
;
418 struct bytecode_symbol_iterator
*it
=
419 bytecode_symbol_iterator_create(bytecode
);
422 ret
= LTTNG_ERR_NOMEM
;
427 struct lttng_event_context ctx
;
428 const char *symbol_name
=
429 bytecode_symbol_iterator_get_name(it
);
431 if (parse_application_context(symbol_name
, &provider_name
,
433 /* Not an application context. */
437 ctx
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
438 ctx
.u
.app_ctx
.provider_name
= provider_name
;
439 ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
441 /* Recognized an application context. */
442 DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.",
443 filter_expression
, provider_name
, ctx_name
);
445 ret
= agent_add_context(&ctx
, agt
);
446 if (ret
!= LTTNG_OK
) {
447 ERR("Failed to add application context %s:%s.",
448 provider_name
, ctx_name
);
452 ret
= agent_enable_context(&ctx
, agt
->domain
);
453 if (ret
!= LTTNG_OK
) {
454 ERR("Failed to enable application context %s:%s.",
455 provider_name
, ctx_name
);
461 provider_name
= ctx_name
= NULL
;
462 } while (bytecode_symbol_iterator_next(it
) == 0);
466 bytecode_symbol_iterator_destroy(it
);
471 * Enable a single agent event for a given UST session.
473 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
475 int event_agent_enable(struct ltt_ust_session
*usess
,
476 struct agent
*agt
, struct lttng_event
*event
,
477 struct lttng_filter_bytecode
*filter
,
478 char *filter_expression
)
480 int ret
, created
= 0;
481 struct agent_event
*aevent
;
487 DBG("Event agent enabling %s for session %" PRIu64
" with loglevel type %d "
488 ", loglevel %d and filter \"%s\"", event
->name
,
489 usess
->id
, event
->loglevel_type
, event
->loglevel
,
490 filter_expression
? filter_expression
: "NULL");
492 aevent
= agent_find_event(event
->name
, event
->loglevel_type
,
493 event
->loglevel
, filter_expression
, agt
);
495 aevent
= agent_create_event(event
->name
, event
->loglevel_type
,
496 event
->loglevel
, filter
,
499 ret
= LTTNG_ERR_NOMEM
;
503 filter_expression
= NULL
;
505 assert(!aevent
->enabled
);
508 if (created
&& aevent
->filter
) {
509 ret
= add_filter_app_ctx(
510 aevent
->filter
, aevent
->filter_expression
, agt
);
511 if (ret
!= LTTNG_OK
) {
516 /* Already enabled? */
517 if (aevent
->enabled
) {
522 ret
= agent_enable_event(aevent
, agt
->domain
);
523 if (ret
!= LTTNG_OK
) {
527 /* If the event was created prior to the enable, add it to the domain. */
529 agent_add_event(aevent
, agt
);
537 agent_destroy_event(aevent
);
541 free(filter_expression
);
546 * Return the default event name associated with the provided UST domain. Return
549 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain
)
551 const char *default_event_name
= NULL
;
554 case LTTNG_DOMAIN_LOG4J
:
555 default_event_name
= DEFAULT_LOG4J_EVENT_NAME
;
557 case LTTNG_DOMAIN_JUL
:
558 default_event_name
= DEFAULT_JUL_EVENT_NAME
;
560 case LTTNG_DOMAIN_PYTHON
:
561 default_event_name
= DEFAULT_PYTHON_EVENT_NAME
;
567 return default_event_name
;
571 * Disable a given agent event for a given UST session.
573 * Must be called with the RCU read lock held.
574 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
576 static int event_agent_disable_one(struct ltt_ust_session
*usess
,
577 struct agent
*agt
, struct agent_event
*aevent
)
580 struct ltt_ust_event
*uevent
= NULL
;
581 struct ltt_ust_channel
*uchan
= NULL
;
582 const char *ust_event_name
, *ust_channel_name
;
588 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64
,
589 aevent
->name
, aevent
->loglevel_type
, aevent
->loglevel_value
,
592 /* Already disabled? */
593 if (!aevent
->enabled
) {
597 if (agt
->domain
== LTTNG_DOMAIN_JUL
) {
598 ust_channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
599 } else if (agt
->domain
== LTTNG_DOMAIN_LOG4J
) {
600 ust_channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
601 } else if (agt
->domain
== LTTNG_DOMAIN_PYTHON
) {
602 ust_channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
604 ret
= LTTNG_ERR_INVALID
;
609 * Disable it on the UST side. First get the channel reference then find
610 * the event and finally disable it.
612 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
613 (char *) ust_channel_name
);
615 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
619 ust_event_name
= event_get_default_agent_ust_name(agt
->domain
);
620 if (!ust_event_name
) {
621 ret
= LTTNG_ERR_FATAL
;
626 * Agent UST event has its loglevel type forced to
627 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
628 * happens thanks to an UST filter. The following -1 is actually
629 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
631 uevent
= trace_ust_find_event(uchan
->events
, (char *) ust_event_name
,
632 aevent
->filter
, LTTNG_UST_LOGLEVEL_ALL
, -1, NULL
);
633 /* If the agent event exists, it must be available on the UST side. */
637 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
638 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
639 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
645 * Flag event that it's disabled so the shadow copy on the ust app side
646 * will disable it if an application shows up.
650 ret
= agent_disable_event(aevent
, agt
->domain
);
651 if (ret
!= LTTNG_OK
) {
663 * Disable all agent events matching a given name for a given UST session.
665 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
667 int event_agent_disable(struct ltt_ust_session
*usess
, struct agent
*agt
,
668 const char *event_name
)
671 struct agent_event
*aevent
;
672 struct lttng_ht_iter iter
;
673 struct lttng_ht_node_str
*node
;
679 DBG("Event agent disabling %s (all loglevels) for session %" PRIu64
, event_name
, usess
->id
);
682 agent_find_events_by_name(event_name
, agt
, &iter
);
683 node
= lttng_ht_iter_get_node_str(&iter
);
686 DBG2("Event agent NOT found by name %s", event_name
);
687 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
692 aevent
= caa_container_of(node
, struct agent_event
, node
);
693 ret
= event_agent_disable_one(usess
, agt
, aevent
);
695 if (ret
!= LTTNG_OK
) {
699 /* Get next duplicate agent event by name. */
700 agent_event_next_duplicate(event_name
, agt
, &iter
);
701 node
= lttng_ht_iter_get_node_str(&iter
);
708 * Disable all agent event for a given UST session.
710 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
712 int event_agent_disable_all(struct ltt_ust_session
*usess
,
716 struct agent_event
*aevent
;
717 struct lttng_ht_iter iter
;
723 * Disable event on agent application. Continue to disable all other events
724 * if the * event is not found.
726 ret
= event_agent_disable(usess
, agt
, "*");
727 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
731 /* Disable every event. */
733 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
735 if (!aevent
->enabled
) {
739 ret
= event_agent_disable(usess
, agt
, aevent
->name
);
740 if (ret
!= LTTNG_OK
) {