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.
21 #include <urcu/list.h>
24 #include <lttng/lttng.h>
25 #include <common/error.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/filter.h>
28 #include <common/context.h>
33 #include "lttng-sessiond.h"
36 #include "trace-kernel.h"
37 #include "trace-ust.h"
41 * Add unique UST event based on the event name, filter bytecode and loglevel.
43 static void add_unique_ust_event(struct lttng_ht
*ht
,
44 struct ltt_ust_event
*event
)
46 struct cds_lfht_node
*node_ptr
;
47 struct ltt_ust_ht_key key
;
53 key
.name
= event
->attr
.name
;
54 key
.filter
= (struct lttng_filter_bytecode
*) event
->filter
;
55 key
.loglevel_type
= event
->attr
.loglevel_type
;
56 key
.loglevel_value
= event
->attr
.loglevel
;
57 key
.exclusion
= event
->exclusion
;
59 node_ptr
= cds_lfht_add_unique(ht
->ht
,
60 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
61 trace_ust_ht_match_event
, &key
, &event
->node
.node
);
62 assert(node_ptr
== &event
->node
.node
);
66 * Disable kernel tracepoint events for a channel from the kernel session of
67 * a specified event_name and event type.
68 * On type LTTNG_EVENT_ALL all events with event_name are disabled.
69 * If event_name is NULL all events of the specified type are disabled.
71 int event_kernel_disable_event(struct ltt_kernel_channel
*kchan
,
72 char *event_name
, 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
!= NULL
&& strcmp(event_name
, kevent
->event
->name
)) {
87 ret
= kernel_disable_event(kevent
);
93 DBG("Disable kernel event: found %d events with name: %s and type: %d",
94 found
, event_name
? event_name
: "NULL", type
);
96 if (event_name
!= NULL
&& !found
) {
97 ret
= LTTNG_ERR_NO_EVENT
;
99 ret
= error
? LTTNG_ERR_KERN_DISABLE_FAIL
: LTTNG_OK
;
106 * Enable kernel tracepoint event for a channel from the kernel session.
107 * We own filter_expression and filter.
109 int event_kernel_enable_event(struct ltt_kernel_channel
*kchan
,
110 struct lttng_event
*event
, char *filter_expression
,
111 struct lttng_filter_bytecode
*filter
)
114 struct ltt_kernel_event
*kevent
;
119 kevent
= trace_kernel_find_event(event
->name
, kchan
,
120 event
->type
, filter
);
121 if (kevent
== NULL
) {
122 ret
= kernel_create_event(event
, kchan
, filter_expression
, filter
);
123 /* We have passed ownership */
124 filter_expression
= NULL
;
129 } else if (kevent
->enabled
== 0) {
130 ret
= kernel_enable_event(kevent
);
132 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
136 /* At this point, the event is considered enabled */
137 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
143 free(filter_expression
);
149 * ============================
150 * UST : The Ultimate Frontier!
151 * ============================
155 * Enable UST tracepoint event for a channel from a UST session.
156 * We own filter_expression, filter, and exclusion.
158 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
,
159 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
160 char *filter_expression
,
161 struct lttng_filter_bytecode
*filter
,
162 struct lttng_event_exclusion
*exclusion
,
165 int ret
= LTTNG_OK
, to_create
= 0;
166 struct ltt_ust_event
*uevent
;
174 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
175 event
->loglevel_type
, event
->loglevel
, exclusion
);
177 ret
= trace_ust_create_event(event
, filter_expression
,
178 filter
, exclusion
, internal_event
, &uevent
);
179 /* We have passed ownership */
180 filter_expression
= NULL
;
183 if (ret
!= LTTNG_OK
) {
187 /* Valid to set it after the goto error since uevent is still NULL */
191 if (uevent
->enabled
) {
192 /* It's already enabled so everything is OK */
194 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
200 /* Add ltt ust event to channel */
201 add_unique_ust_event(uchan
->events
, uevent
);
204 if (!usess
->active
) {
209 /* Create event on all UST registered apps for session */
210 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
212 /* Enable event on all UST registered apps for session */
213 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
217 if (ret
== -LTTNG_UST_ERR_EXIST
) {
218 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
221 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
226 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
227 to_create
? "created" : "enabled", uchan
->name
);
233 free(filter_expression
);
240 * Only destroy event on creation time (not enabling time) because if the
241 * event is found in the channel (to_create == 0), it means that at some
242 * point the enable_event worked and it's thus valid to keep it alive.
243 * Destroying it also implies that we also destroy it's shadow copy to sync
247 /* In this code path, the uevent was not added to the hash table */
248 trace_ust_destroy_event(uevent
);
251 free(filter_expression
);
258 * Disable UST tracepoint of a channel from a UST session.
260 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
261 struct ltt_ust_channel
*uchan
, char *event_name
)
264 struct ltt_ust_event
*uevent
;
265 struct lttng_ht_node_str
*node
;
266 struct lttng_ht_iter iter
;
278 * We use a custom lookup since we need the iterator for the next_duplicate
279 * call in the do while loop below.
281 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
282 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
283 node
= lttng_ht_iter_get_node_str(&iter
);
285 DBG2("Trace UST event NOT found by name %s", event_name
);
286 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
291 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
294 if (uevent
->enabled
== 0) {
295 /* It's already disabled so everything is OK */
299 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
302 if (!usess
->active
) {
305 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
306 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
307 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
311 /* Get next duplicate event by name. */
312 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
313 event_name
, &iter
.iter
);
314 node
= lttng_ht_iter_get_node_str(&iter
);
325 * Disable all UST tracepoints for a channel from a UST session.
327 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
,
328 struct ltt_ust_channel
*uchan
)
330 int ret
, i
, size
, error
= 0;
331 struct lttng_ht_iter iter
;
332 struct ltt_ust_event
*uevent
= NULL
;
333 struct lttng_event
*events
= NULL
;
340 /* Disabling existing events */
341 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
343 if (uevent
->enabled
== 1) {
344 ret
= event_ust_disable_tracepoint(usess
, uchan
,
347 error
= LTTNG_ERR_UST_DISABLE_FAIL
;
353 /* Get all UST available events */
354 size
= ust_app_list_events(&events
);
356 ret
= LTTNG_ERR_UST_LIST_FAIL
;
360 for (i
= 0; i
< size
; i
++) {
361 ret
= event_ust_disable_tracepoint(usess
, uchan
,
364 /* Continue to disable the rest... */
365 error
= LTTNG_ERR_UST_DISABLE_FAIL
;
370 ret
= error
? error
: LTTNG_OK
;
378 * Enable all agent event for a given UST session.
380 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
382 int event_agent_enable_all(struct ltt_ust_session
*usess
,
383 struct agent
*agt
, struct lttng_event
*event
,
384 struct lttng_filter_bytecode
*filter
,char *filter_expression
)
387 struct agent_event
*aevent
;
388 struct lttng_ht_iter iter
;
392 DBG("Event agent enabling ALL events for session %" PRIu64
, usess
->id
);
394 /* Enable event on agent application through TCP socket. */
395 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
396 if (ret
!= LTTNG_OK
) {
400 /* Flag every event that they are now enabled. */
402 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
415 * Check if this event's filter requires the activation of application contexts
416 * and enable them in the agent.
417 * TODO: bytecode iterator does not support non-legacy application
418 * contexts yet. Not an issue for now, since they are not generated by
419 * the lttng-ctl library.
421 static int add_filter_app_ctx(struct lttng_filter_bytecode
*bytecode
,
422 const char *filter_expression
, struct agent
*agt
)
425 char *provider_name
= NULL
, *ctx_name
= NULL
;
426 struct bytecode_symbol_iterator
*it
=
427 bytecode_symbol_iterator_create(bytecode
);
430 ret
= LTTNG_ERR_NOMEM
;
435 struct lttng_event_context ctx
;
436 const char *symbol_name
=
437 bytecode_symbol_iterator_get_name(it
);
439 if (parse_application_context(symbol_name
, &provider_name
,
441 /* Not an application context. */
445 ctx
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
446 ctx
.u
.app_ctx
.provider_name
= provider_name
;
447 ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
449 /* Recognized an application context. */
450 DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.",
451 filter_expression
, provider_name
, ctx_name
);
453 ret
= agent_add_context(&ctx
, agt
);
454 if (ret
!= LTTNG_OK
) {
455 ERR("Failed to add application context %s:%s.",
456 provider_name
, ctx_name
);
460 ret
= agent_enable_context(&ctx
, agt
->domain
);
461 if (ret
!= LTTNG_OK
) {
462 ERR("Failed to enable application context %s:%s.",
463 provider_name
, ctx_name
);
469 provider_name
= ctx_name
= NULL
;
470 } while (bytecode_symbol_iterator_next(it
) == 0);
474 bytecode_symbol_iterator_destroy(it
);
479 * Enable a single agent event for a given UST session.
481 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
483 int event_agent_enable(struct ltt_ust_session
*usess
,
484 struct agent
*agt
, struct lttng_event
*event
,
485 struct lttng_filter_bytecode
*filter
,
486 char *filter_expression
)
488 int ret
, created
= 0;
489 struct agent_event
*aevent
;
495 DBG("Event agent enabling %s for session %" PRIu64
" with loglevel type %d "
496 ", loglevel %d and filter \"%s\"", event
->name
,
497 usess
->id
, event
->loglevel_type
, event
->loglevel
,
498 filter_expression
? filter_expression
: "NULL");
500 aevent
= agent_find_event(event
->name
, event
->loglevel_type
,
501 event
->loglevel
, filter_expression
, agt
);
503 aevent
= agent_create_event(event
->name
, event
->loglevel_type
,
504 event
->loglevel
, filter
,
507 ret
= LTTNG_ERR_NOMEM
;
511 filter_expression
= NULL
;
515 if (created
&& filter
) {
516 ret
= add_filter_app_ctx(filter
, filter_expression
, agt
);
517 if (ret
!= LTTNG_OK
) {
522 /* Already enabled? */
523 if (aevent
->enabled
) {
528 ret
= agent_enable_event(aevent
, agt
->domain
);
529 if (ret
!= LTTNG_OK
) {
533 /* If the event was created prior to the enable, add it to the domain. */
535 agent_add_event(aevent
, agt
);
543 agent_destroy_event(aevent
);
547 free(filter_expression
);
552 * Return the default event name associated with the provided UST domain. Return
555 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain
)
557 const char *default_event_name
= NULL
;
560 case LTTNG_DOMAIN_LOG4J
:
561 default_event_name
= DEFAULT_LOG4J_EVENT_NAME
;
563 case LTTNG_DOMAIN_JUL
:
564 default_event_name
= DEFAULT_JUL_EVENT_NAME
;
566 case LTTNG_DOMAIN_PYTHON
:
567 default_event_name
= DEFAULT_PYTHON_EVENT_NAME
;
573 return default_event_name
;
577 * Disable a given agent event for a given UST session.
579 * Must be called with the RCU read lock held.
580 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
582 static int event_agent_disable_one(struct ltt_ust_session
*usess
,
583 struct agent
*agt
, struct agent_event
*aevent
)
586 struct ltt_ust_event
*uevent
= NULL
;
587 struct ltt_ust_channel
*uchan
= NULL
;
588 const char *ust_event_name
, *ust_channel_name
;
594 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64
,
595 aevent
->name
, aevent
->loglevel_type
, aevent
->loglevel_value
,
598 /* Already disabled? */
599 if (!aevent
->enabled
) {
603 if (agt
->domain
== LTTNG_DOMAIN_JUL
) {
604 ust_channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
605 } else if (agt
->domain
== LTTNG_DOMAIN_LOG4J
) {
606 ust_channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
607 } else if (agt
->domain
== LTTNG_DOMAIN_PYTHON
) {
608 ust_channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
610 ret
= LTTNG_ERR_INVALID
;
615 * Disable it on the UST side. First get the channel reference then find
616 * the event and finally disable it.
618 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
619 (char *) ust_channel_name
);
621 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
625 ust_event_name
= event_get_default_agent_ust_name(agt
->domain
);
626 if (!ust_event_name
) {
627 ret
= LTTNG_ERR_FATAL
;
632 * Agent UST event has its loglevel type forced to
633 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
634 * happens thanks to an UST filter. The following -1 is actually
635 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
637 uevent
= trace_ust_find_event(uchan
->events
, (char *) ust_event_name
,
638 aevent
->filter
, LTTNG_UST_LOGLEVEL_ALL
, -1, NULL
);
639 /* If the agent event exists, it must be available on the UST side. */
643 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
644 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
645 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
651 * Flag event that it's disabled so the shadow copy on the ust app side
652 * will disable it if an application shows up.
656 ret
= agent_disable_event(aevent
, agt
->domain
);
657 if (ret
!= LTTNG_OK
) {
669 * Disable all agent events matching a given name for a given UST session.
671 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
673 int event_agent_disable(struct ltt_ust_session
*usess
, struct agent
*agt
,
677 struct agent_event
*aevent
;
678 struct lttng_ht_iter iter
;
679 struct lttng_ht_node_str
*node
;
685 DBG("Event agent disabling %s (all loglevels) for session %" PRIu64
, event_name
, usess
->id
);
688 agent_find_events_by_name(event_name
, agt
, &iter
);
689 node
= lttng_ht_iter_get_node_str(&iter
);
692 DBG2("Event agent NOT found by name %s", event_name
);
693 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
698 aevent
= caa_container_of(node
, struct agent_event
, node
);
699 ret
= event_agent_disable_one(usess
, agt
, aevent
);
701 if (ret
!= LTTNG_OK
) {
705 /* Get next duplicate agent event by name. */
706 agent_event_next_duplicate(event_name
, agt
, &iter
);
707 node
= lttng_ht_iter_get_node_str(&iter
);
714 * Disable all agent event for a given UST session.
716 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
718 int event_agent_disable_all(struct ltt_ust_session
*usess
,
722 struct agent_event
*aevent
;
723 struct lttng_ht_iter iter
;
729 * Disable event on agent application. Continue to disable all other events
730 * if the * event is not found.
732 ret
= event_agent_disable(usess
, agt
, "*");
733 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
737 /* Disable every event. */
739 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
741 if (!aevent
->enabled
) {
745 ret
= event_agent_disable(usess
, agt
, aevent
->name
);
746 if (ret
!= LTTNG_OK
) {