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"
34 #include "lttng-ust-ctl.h"
35 #include "lttng-ust-error.h"
37 #include "trace-kernel.h"
38 #include "trace-ust.h"
42 * Add unique UST event based on the event name, filter bytecode and loglevel.
44 static void add_unique_ust_event(struct lttng_ht
*ht
,
45 struct ltt_ust_event
*event
)
47 struct cds_lfht_node
*node_ptr
;
48 struct ltt_ust_ht_key key
;
54 key
.name
= event
->attr
.name
;
55 key
.filter
= (struct lttng_filter_bytecode
*) event
->filter
;
56 key
.loglevel_type
= event
->attr
.loglevel_type
;
57 key
.loglevel_value
= event
->attr
.loglevel
;
58 key
.exclusion
= event
->exclusion
;
60 node_ptr
= cds_lfht_add_unique(ht
->ht
,
61 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
62 trace_ust_ht_match_event
, &key
, &event
->node
.node
);
63 assert(node_ptr
== &event
->node
.node
);
67 * Disable kernel tracepoint events for a channel from the kernel session of
68 * a specified event_name and event type.
69 * On type LTTNG_EVENT_ALL all events with event_name are disabled.
70 * If event_name is NULL all events of the specified type are disabled.
72 int event_kernel_disable_event(struct ltt_kernel_channel
*kchan
,
73 const char *event_name
, enum lttng_event_type type
)
75 int ret
, error
= 0, found
= 0;
76 struct ltt_kernel_event
*kevent
;
80 /* For each event in the kernel session */
81 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
82 if (type
!= LTTNG_EVENT_ALL
&& kevent
->type
!= type
)
84 if (event_name
!= NULL
&& strcmp(event_name
, kevent
->event
->name
)) {
88 ret
= kernel_disable_event(kevent
);
94 DBG("Disable kernel event: found %d events with name: %s and type: %d",
95 found
, event_name
? event_name
: "NULL", type
);
97 if (event_name
!= NULL
&& !found
) {
98 ret
= LTTNG_ERR_NO_EVENT
;
100 ret
= error
? LTTNG_ERR_KERN_DISABLE_FAIL
: LTTNG_OK
;
107 * Enable kernel tracepoint event for a channel from the kernel session.
108 * We own filter_expression and filter.
110 int event_kernel_enable_event(struct ltt_kernel_channel
*kchan
,
111 struct lttng_event
*event
, char *filter_expression
,
112 struct lttng_filter_bytecode
*filter
)
115 struct ltt_kernel_event
*kevent
;
120 kevent
= trace_kernel_find_event(event
->name
, kchan
,
121 event
->type
, filter
);
122 if (kevent
== NULL
) {
123 ret
= kernel_create_event(event
, kchan
, filter_expression
, filter
);
124 /* We have passed ownership */
125 filter_expression
= NULL
;
130 } else if (kevent
->enabled
== 0) {
131 ret
= kernel_enable_event(kevent
);
133 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
137 /* At this point, the event is considered enabled */
138 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
144 free(filter_expression
);
150 * ============================
151 * UST : The Ultimate Frontier!
152 * ============================
156 * Enable UST tracepoint event for a channel from a UST session.
157 * We own filter_expression, filter, and exclusion.
159 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
,
160 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
161 char *filter_expression
,
162 struct lttng_filter_bytecode
*filter
,
163 struct lttng_event_exclusion
*exclusion
,
166 int ret
= LTTNG_OK
, to_create
= 0;
167 struct ltt_ust_event
*uevent
;
175 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
176 (enum lttng_ust_loglevel_type
) event
->loglevel_type
,
177 event
->loglevel
, exclusion
);
179 ret
= trace_ust_create_event(event
, filter_expression
,
180 filter
, exclusion
, internal_event
, &uevent
);
181 /* We have passed ownership */
182 filter_expression
= NULL
;
185 if (ret
!= LTTNG_OK
) {
189 /* Valid to set it after the goto error since uevent is still NULL */
193 if (uevent
->enabled
) {
194 /* It's already enabled so everything is OK */
196 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
202 /* Add ltt ust event to channel */
203 add_unique_ust_event(uchan
->events
, uevent
);
206 if (!usess
->active
) {
211 /* Create event on all UST registered apps for session */
212 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
214 /* Enable event on all UST registered apps for session */
215 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
219 if (ret
== -LTTNG_UST_ERR_EXIST
) {
220 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
223 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
228 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
229 to_create
? "created" : "enabled", uchan
->name
);
235 free(filter_expression
);
242 * Only destroy event on creation time (not enabling time) because if the
243 * event is found in the channel (to_create == 0), it means that at some
244 * point the enable_event worked and it's thus valid to keep it alive.
245 * Destroying it also implies that we also destroy it's shadow copy to sync
249 /* In this code path, the uevent was not added to the hash table */
250 trace_ust_destroy_event(uevent
);
253 free(filter_expression
);
260 * Disable UST tracepoint of a channel from a UST session.
262 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
263 struct ltt_ust_channel
*uchan
, const char *event_name
)
266 struct ltt_ust_event
*uevent
;
267 struct lttng_ht_node_str
*node
;
268 struct lttng_ht_iter iter
;
280 * We use a custom lookup since we need the iterator for the next_duplicate
281 * call in the do while loop below.
283 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
284 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
285 node
= lttng_ht_iter_get_node_str(&iter
);
287 DBG2("Trace UST event NOT found by name %s", event_name
);
288 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
293 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
296 if (uevent
->enabled
== 0) {
297 /* It's already disabled so everything is OK */
301 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
304 if (!usess
->active
) {
307 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
308 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
309 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
313 /* Get next duplicate event by name. */
314 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
315 event_name
, &iter
.iter
);
316 node
= lttng_ht_iter_get_node_str(&iter
);
327 * Disable all UST tracepoints for a channel from a UST session.
329 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
,
330 struct ltt_ust_channel
*uchan
)
332 int ret
, i
, size
, error
= 0;
333 struct lttng_ht_iter iter
;
334 struct ltt_ust_event
*uevent
= NULL
;
335 struct lttng_event
*events
= NULL
;
342 /* Disabling existing events */
343 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
345 if (uevent
->enabled
== 1) {
346 ret
= event_ust_disable_tracepoint(usess
, uchan
,
349 error
= LTTNG_ERR_UST_DISABLE_FAIL
;
355 /* Get all UST available events */
356 size
= ust_app_list_events(&events
);
358 ret
= LTTNG_ERR_UST_LIST_FAIL
;
362 for (i
= 0; i
< size
; i
++) {
363 ret
= event_ust_disable_tracepoint(usess
, uchan
,
366 /* Continue to disable the rest... */
367 error
= LTTNG_ERR_UST_DISABLE_FAIL
;
372 ret
= error
? error
: LTTNG_OK
;
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
,
385 struct agent
*agt
, struct lttng_event
*event
,
386 struct lttng_filter_bytecode
*filter
,char *filter_expression
)
389 struct agent_event
*aevent
;
390 struct lttng_ht_iter iter
;
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 /* Flag every event that they are now enabled. */
404 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
417 * Check if this event's filter requires the activation of application contexts
418 * and enable them in the agent.
419 * TODO: bytecode iterator does not support non-legacy application
420 * contexts yet. Not an issue for now, since they are not generated by
421 * the lttng-ctl library.
423 static int add_filter_app_ctx(struct lttng_filter_bytecode
*bytecode
,
424 const char *filter_expression
, struct agent
*agt
)
427 char *provider_name
= NULL
, *ctx_name
= NULL
;
428 struct bytecode_symbol_iterator
*it
=
429 bytecode_symbol_iterator_create(bytecode
);
432 ret
= LTTNG_ERR_NOMEM
;
437 struct lttng_event_context ctx
;
438 const char *symbol_name
=
439 bytecode_symbol_iterator_get_name(it
);
441 if (parse_application_context(symbol_name
, &provider_name
,
443 /* Not an application context. */
447 ctx
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
448 ctx
.u
.app_ctx
.provider_name
= provider_name
;
449 ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
451 /* Recognized an application context. */
452 DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.",
453 filter_expression
, provider_name
, ctx_name
);
455 ret
= agent_add_context(&ctx
, agt
);
456 if (ret
!= LTTNG_OK
) {
457 ERR("Failed to add application context %s:%s.",
458 provider_name
, ctx_name
);
462 ret
= agent_enable_context(&ctx
, agt
->domain
);
463 if (ret
!= LTTNG_OK
) {
464 ERR("Failed to enable application context %s:%s.",
465 provider_name
, ctx_name
);
471 provider_name
= ctx_name
= NULL
;
472 } while (bytecode_symbol_iterator_next(it
) == 0);
476 bytecode_symbol_iterator_destroy(it
);
481 * Enable a single agent event for a given UST session.
483 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
485 int event_agent_enable(struct ltt_ust_session
*usess
,
486 struct agent
*agt
, struct lttng_event
*event
,
487 struct lttng_filter_bytecode
*filter
,
488 char *filter_expression
)
490 int ret
, created
= 0;
491 struct agent_event
*aevent
;
497 DBG("Event agent enabling %s for session %" PRIu64
" with loglevel type %d "
498 ", loglevel %d and filter \"%s\"", event
->name
,
499 usess
->id
, event
->loglevel_type
, event
->loglevel
,
500 filter_expression
? filter_expression
: "NULL");
502 aevent
= agent_find_event(event
->name
, event
->loglevel_type
,
503 event
->loglevel
, filter_expression
, agt
);
505 aevent
= agent_create_event(event
->name
, event
->loglevel_type
,
506 event
->loglevel
, filter
,
509 ret
= LTTNG_ERR_NOMEM
;
513 filter_expression
= NULL
;
515 assert(!aevent
->enabled
);
518 if (created
&& aevent
->filter
) {
519 ret
= add_filter_app_ctx(
520 aevent
->filter
, aevent
->filter_expression
, agt
);
521 if (ret
!= LTTNG_OK
) {
526 /* Already enabled? */
527 if (aevent
->enabled
) {
532 ret
= agent_enable_event(aevent
, agt
->domain
);
533 if (ret
!= LTTNG_OK
) {
537 /* If the event was created prior to the enable, add it to the domain. */
539 agent_add_event(aevent
, agt
);
547 agent_destroy_event(aevent
);
551 free(filter_expression
);
556 * Return the default event name associated with the provided UST domain. Return
559 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain
)
561 const char *default_event_name
= NULL
;
564 case LTTNG_DOMAIN_LOG4J
:
565 default_event_name
= DEFAULT_LOG4J_EVENT_NAME
;
567 case LTTNG_DOMAIN_JUL
:
568 default_event_name
= DEFAULT_JUL_EVENT_NAME
;
570 case LTTNG_DOMAIN_PYTHON
:
571 default_event_name
= DEFAULT_PYTHON_EVENT_NAME
;
577 return default_event_name
;
581 * Disable a given agent event for a given UST session.
583 * Must be called with the RCU read lock held.
584 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
586 static int event_agent_disable_one(struct ltt_ust_session
*usess
,
587 struct agent
*agt
, struct agent_event
*aevent
)
590 struct ltt_ust_event
*uevent
= NULL
;
591 struct ltt_ust_channel
*uchan
= NULL
;
592 const char *ust_event_name
, *ust_channel_name
;
598 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64
,
599 aevent
->name
, aevent
->loglevel_type
, aevent
->loglevel_value
,
602 /* Already disabled? */
603 if (!aevent
->enabled
) {
607 if (agt
->domain
== LTTNG_DOMAIN_JUL
) {
608 ust_channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
609 } else if (agt
->domain
== LTTNG_DOMAIN_LOG4J
) {
610 ust_channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
611 } else if (agt
->domain
== LTTNG_DOMAIN_PYTHON
) {
612 ust_channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
614 ret
= LTTNG_ERR_INVALID
;
619 * Disable it on the UST side. First get the channel reference then find
620 * the event and finally disable it.
622 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
623 (char *) ust_channel_name
);
625 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
629 ust_event_name
= event_get_default_agent_ust_name(agt
->domain
);
630 if (!ust_event_name
) {
631 ret
= LTTNG_ERR_FATAL
;
636 * Agent UST event has its loglevel type forced to
637 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
638 * happens thanks to an UST filter. The following -1 is actually
639 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
641 uevent
= trace_ust_find_event(uchan
->events
, (char *) ust_event_name
,
642 aevent
->filter
, LTTNG_UST_LOGLEVEL_ALL
, -1, NULL
);
643 /* If the agent event exists, it must be available on the UST side. */
647 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
648 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
649 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
655 * Flag event that it's disabled so the shadow copy on the ust app side
656 * will disable it if an application shows up.
660 ret
= agent_disable_event(aevent
, agt
->domain
);
661 if (ret
!= LTTNG_OK
) {
673 * Disable all agent events matching a given name for a given UST session.
675 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
677 int event_agent_disable(struct ltt_ust_session
*usess
, struct agent
*agt
,
678 const char *event_name
)
681 struct agent_event
*aevent
;
682 struct lttng_ht_iter iter
;
683 struct lttng_ht_node_str
*node
;
689 DBG("Event agent disabling %s (all loglevels) for session %" PRIu64
, event_name
, usess
->id
);
692 agent_find_events_by_name(event_name
, agt
, &iter
);
693 node
= lttng_ht_iter_get_node_str(&iter
);
696 DBG2("Event agent NOT found by name %s", event_name
);
697 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
702 aevent
= caa_container_of(node
, struct agent_event
, node
);
703 ret
= event_agent_disable_one(usess
, agt
, aevent
);
705 if (ret
!= LTTNG_OK
) {
709 /* Get next duplicate agent event by name. */
710 agent_event_next_duplicate(event_name
, agt
, &iter
);
711 node
= lttng_ht_iter_get_node_str(&iter
);
718 * Disable all agent event for a given UST session.
720 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
722 int event_agent_disable_all(struct ltt_ust_session
*usess
,
726 struct agent_event
*aevent
;
727 struct lttng_ht_iter iter
;
733 * Disable event on agent application. Continue to disable all other events
734 * if the * event is not found.
736 ret
= event_agent_disable(usess
, agt
, "*");
737 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
741 /* Disable every event. */
743 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
745 if (!aevent
->enabled
) {
749 ret
= event_agent_disable(usess
, agt
, aevent
->name
);
750 if (ret
!= LTTNG_OK
) {