2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <urcu/list.h>
23 #include <lttng/lttng.h>
24 #include <common/error.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
30 #include "lttng-sessiond.h"
33 #include "trace-kernel.h"
34 #include "trace-ust.h"
37 * Add unique UST event based on the event name, filter bytecode and loglevel.
39 static void add_unique_ust_event(struct lttng_ht
*ht
,
40 struct ltt_ust_event
*event
)
42 struct cds_lfht_node
*node_ptr
;
43 struct ltt_ust_ht_key key
;
49 key
.name
= event
->attr
.name
;
50 key
.filter
= (struct lttng_filter_bytecode
*) event
->filter
;
51 key
.loglevel
= event
->attr
.loglevel
;
52 key
.exclusion
= event
->exclusion
;
54 node_ptr
= cds_lfht_add_unique(ht
->ht
,
55 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
56 trace_ust_ht_match_event
, &key
, &event
->node
.node
);
57 assert(node_ptr
== &event
->node
.node
);
61 * Disable kernel tracepoint event for a channel from the kernel session.
63 int event_kernel_disable_tracepoint(struct ltt_kernel_channel
*kchan
,
67 struct ltt_kernel_event
*kevent
;
71 kevent
= trace_kernel_get_event_by_name(event_name
, kchan
);
73 ret
= LTTNG_ERR_NO_EVENT
;
77 ret
= kernel_disable_event(kevent
);
79 ret
= LTTNG_ERR_KERN_DISABLE_FAIL
;
83 DBG("Kernel event %s disable for channel %s.",
84 kevent
->event
->name
, kchan
->channel
->name
);
93 * Enable kernel system call for a channel from the kernel session.
95 int event_kernel_enable_syscall(struct ltt_kernel_channel
*kchan
,
102 ret
= kernel_enable_syscall(syscall_name
, kchan
);
104 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
108 DBG("Kernel event %s enable for channel %s.",
109 syscall_name
, kchan
->channel
->name
);
118 * Disable kernel system call for a channel from the kernel session.
120 int event_kernel_disable_syscall(struct ltt_kernel_channel
*kchan
,
127 ret
= kernel_disable_syscall(syscall_name
, kchan
);
129 ret
= LTTNG_ERR_KERN_DISABLE_FAIL
;
133 DBG("Kernel syscall %s disable for channel %s.",
134 syscall_name
[0] == '\0' ? "<all>" : syscall_name
,
135 kchan
->channel
->name
);
144 * Disable kernel tracepoint events for a channel from the kernel session.
146 int event_kernel_disable_all_tracepoints(struct ltt_kernel_channel
*kchan
)
149 struct ltt_kernel_event
*kevent
;
153 /* For each event in the kernel session */
154 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
155 ret
= kernel_disable_event(kevent
);
157 /* We continue disabling the rest */
166 * Disable all kernel event for a channel from the kernel session.
168 int event_kernel_disable_all(struct ltt_kernel_channel
*kchan
)
174 ret
= event_kernel_disable_all_tracepoints(kchan
);
177 ret
= event_kernel_disable_syscall(kchan
, "");
182 * Enable kernel tracepoint event for a channel from the kernel session.
184 int event_kernel_enable_tracepoint(struct ltt_kernel_channel
*kchan
,
185 struct lttng_event
*event
)
188 struct ltt_kernel_event
*kevent
;
193 kevent
= trace_kernel_get_event_by_name(event
->name
, kchan
);
194 if (kevent
== NULL
) {
195 ret
= kernel_create_event(event
, kchan
);
199 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
202 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
205 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
210 } else if (kevent
->enabled
== 0) {
211 ret
= kernel_enable_event(kevent
);
213 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
217 /* At this point, the event is considered enabled */
218 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
228 * Enable all kernel tracepoint events of a channel of the kernel session.
230 int event_kernel_enable_all_tracepoints(struct ltt_kernel_channel
*kchan
,
231 int kernel_tracer_fd
)
234 struct ltt_kernel_event
*kevent
;
235 struct lttng_event
*event_list
= NULL
;
239 /* For each event in the kernel session */
240 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
241 if (kevent
->enabled
== 0) {
242 ret
= kernel_enable_event(kevent
);
244 /* Enable failed but still continue */
250 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
252 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
256 for (i
= 0; i
< size
; i
++) {
257 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
258 if (kevent
== NULL
) {
259 /* Default event type for enable all */
260 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
261 /* Enable each single tracepoint event */
262 ret
= kernel_create_event(&event_list
[i
], kchan
);
264 /* Ignore error here and continue */
276 * Enable all kernel events of a channel of the kernel session.
278 int event_kernel_enable_all(struct ltt_kernel_channel
*kchan
,
279 int kernel_tracer_fd
)
285 tp_ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
286 if (tp_ret
!= LTTNG_OK
) {
291 * Reaching this code path means that all tracepoints were enabled without
292 * errors so we ignore the error value of syscalls.
294 * At the moment, failing to enable syscalls on "lttng enable-event -a -k"
295 * is not considered an error that need to be returned to the client since
296 * tracepoints did not fail. Future work will allow us to send back
297 * multiple errors to the client in one API call.
299 (void) event_kernel_enable_syscall(kchan
, "");
306 * ============================
307 * UST : The Ultimate Frontier!
308 * ============================
312 * Enable all UST tracepoints for a channel from a UST session.
314 int event_ust_enable_all_tracepoints(struct ltt_ust_session
*usess
,
315 struct ltt_ust_channel
*uchan
,
316 char *filter_expression
,
317 struct lttng_filter_bytecode
*filter
)
320 struct lttng_ht_iter iter
;
321 struct ltt_ust_event
*uevent
= NULL
;
322 struct lttng_event
*events
= NULL
;
329 /* Enable existing events */
330 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
332 if (uevent
->enabled
== 0) {
333 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
341 /* Get all UST available events */
342 size
= ust_app_list_events(&events
);
344 ret
= LTTNG_ERR_UST_LIST_FAIL
;
348 for (i
= 0; i
< size
; i
++) {
350 * Check if event exist and if so, continue since it was enable
353 uevent
= trace_ust_find_event(uchan
->events
, events
[i
].name
, filter
,
354 events
[i
].loglevel
, NULL
);
355 if (uevent
!= NULL
) {
356 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
359 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
360 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
367 /* Create ust event */
368 uevent
= trace_ust_create_event(&events
[i
], filter_expression
,
370 if (uevent
== NULL
) {
371 ret
= LTTNG_ERR_FATAL
;
375 /* Create event for the specific PID */
376 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
379 if (ret
== -LTTNG_UST_ERR_EXIST
) {
380 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
383 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
389 /* Add ltt ust event to channel */
391 add_unique_ust_event(uchan
->events
, uevent
);
400 trace_ust_destroy_event(uevent
);
409 * Enable UST tracepoint event for a channel from a UST session.
411 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
,
412 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
413 char *filter_expression
,
414 struct lttng_filter_bytecode
*filter
,
415 struct lttng_event_exclusion
*exclusion
)
417 int ret
= LTTNG_OK
, to_create
= 0;
418 struct ltt_ust_event
*uevent
;
426 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
427 event
->loglevel
, exclusion
);
428 if (uevent
== NULL
) {
429 uevent
= trace_ust_create_event(event
, filter_expression
,
431 if (uevent
== NULL
) {
432 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
436 /* Valid to set it after the goto error since uevent is still NULL */
440 if (uevent
->enabled
) {
441 /* It's already enabled so everything is OK */
442 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
449 /* Create event on all UST registered apps for session */
450 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
452 /* Enable event on all UST registered apps for session */
453 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
457 if (ret
== -LTTNG_UST_ERR_EXIST
) {
458 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
461 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
467 /* Add ltt ust event to channel */
468 add_unique_ust_event(uchan
->events
, uevent
);
471 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
472 to_create
? "created" : "enabled", uchan
->name
);
482 * Only destroy event on creation time (not enabling time) because if the
483 * event is found in the channel (to_create == 0), it means that at some
484 * point the enable_event worked and it's thus valid to keep it alive.
485 * Destroying it also implies that we also destroy it's shadow copy to sync
489 /* In this code path, the uevent was not added to the hash table */
490 trace_ust_destroy_event(uevent
);
497 * Disable UST tracepoint of a channel from a UST session.
499 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
500 struct ltt_ust_channel
*uchan
, char *event_name
)
503 struct ltt_ust_event
*uevent
;
504 struct lttng_ht_node_str
*node
;
505 struct lttng_ht_iter iter
;
517 * We use a custom lookup since we need the iterator for the next_duplicate
518 * call in the do while loop below.
520 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
521 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
522 node
= lttng_ht_iter_get_node_str(&iter
);
524 DBG2("Trace UST event NOT found by name %s", event_name
);
525 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
530 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
533 if (uevent
->enabled
== 0) {
534 /* It's already disabled so everything is OK */
538 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
539 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
540 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
545 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
549 /* Get next duplicate event by name. */
550 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
551 event_name
, &iter
.iter
);
552 node
= lttng_ht_iter_get_node_str(&iter
);
563 * Disable all UST tracepoints for a channel from a UST session.
565 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
,
566 struct ltt_ust_channel
*uchan
)
569 struct lttng_ht_iter iter
;
570 struct ltt_ust_event
*uevent
= NULL
;
571 struct lttng_event
*events
= NULL
;
578 /* Disabling existing events */
579 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
581 if (uevent
->enabled
== 1) {
582 ret
= event_ust_disable_tracepoint(usess
, uchan
,
590 /* Get all UST available events */
591 size
= ust_app_list_events(&events
);
593 ret
= LTTNG_ERR_UST_LIST_FAIL
;
597 for (i
= 0; i
< size
; i
++) {
598 ret
= event_ust_disable_tracepoint(usess
, uchan
,
600 if (ret
!= LTTNG_OK
) {
601 /* Continue to disable the rest... */
617 * Enable all agent event for a given UST session.
619 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
621 int event_agent_enable_all(struct ltt_ust_session
*usess
,
622 struct agent
*agt
, struct lttng_event
*event
,
623 struct lttng_filter_bytecode
*filter
)
626 struct agent_event
*aevent
;
627 struct lttng_ht_iter iter
;
631 DBG("Event agent enabling ALL events for session %" PRIu64
, usess
->id
);
633 /* Enable event on agent application through TCP socket. */
634 ret
= event_agent_enable(usess
, agt
, event
, filter
);
635 if (ret
!= LTTNG_OK
) {
639 /* Flag every event that they are now enabled. */
641 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
654 * Enable a single agent event for a given UST session.
656 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
658 int event_agent_enable(struct ltt_ust_session
*usess
,
659 struct agent
*agt
, struct lttng_event
*event
,
660 struct lttng_filter_bytecode
*filter
)
662 int ret
, created
= 0;
663 struct agent_event
*aevent
;
669 DBG("Event agent enabling %s for session %" PRIu64
" with loglevel type %d "
670 "and loglevel %d", event
->name
, usess
->id
, event
->loglevel_type
,
673 aevent
= agent_find_event(event
->name
, event
->loglevel
, agt
);
675 aevent
= agent_create_event(event
->name
, filter
);
677 ret
= LTTNG_ERR_NOMEM
;
680 aevent
->loglevel
= event
->loglevel
;
681 aevent
->loglevel_type
= event
->loglevel_type
;
685 /* Already enabled? */
686 if (aevent
->enabled
) {
690 ret
= agent_enable_event(aevent
, agt
->domain
);
691 if (ret
!= LTTNG_OK
) {
695 /* If the event was created prior to the enable, add it to the domain. */
697 agent_add_event(aevent
, agt
);
705 agent_destroy_event(aevent
);
711 * Return the agent default event name to use by testing if the process is root
712 * or not. Return NULL on error.
714 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain
)
716 const char *default_event_name
= NULL
;
719 case LTTNG_DOMAIN_LOG4J
:
721 default_event_name
= DEFAULT_SYS_LOG4J_EVENT_NAME
;
723 default_event_name
= DEFAULT_USER_LOG4J_EVENT_NAME
;
726 case LTTNG_DOMAIN_JUL
:
728 default_event_name
= DEFAULT_SYS_JUL_EVENT_NAME
;
730 default_event_name
= DEFAULT_USER_JUL_EVENT_NAME
;
733 case LTTNG_DOMAIN_PYTHON
:
734 default_event_name
= DEFAULT_USER_PYTHON_EVENT_NAME
;
740 return default_event_name
;
744 * Disable a single agent event for a given UST session.
746 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
748 int event_agent_disable(struct ltt_ust_session
*usess
, struct agent
*agt
,
752 struct agent_event
*aevent
;
753 struct ltt_ust_event
*uevent
= NULL
;
754 struct ltt_ust_channel
*uchan
= NULL
;
755 const char *ust_event_name
, *ust_channel_name
;
761 DBG("Event agent disabling %s for session %" PRIu64
, event_name
, usess
->id
);
763 aevent
= agent_find_event_by_name(event_name
, agt
);
765 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
769 /* Already disabled? */
770 if (!aevent
->enabled
) {
774 if (agt
->domain
== LTTNG_DOMAIN_JUL
) {
775 ust_channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
776 } else if (agt
->domain
== LTTNG_DOMAIN_LOG4J
) {
777 ust_channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
778 } else if (agt
->domain
== LTTNG_DOMAIN_PYTHON
) {
779 ust_channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
781 ret
= LTTNG_ERR_INVALID
;
786 * Disable it on the UST side. First get the channel reference then find
787 * the event and finally disable it.
789 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
790 (char *) ust_channel_name
);
792 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
796 ust_event_name
= event_get_default_agent_ust_name(agt
->domain
);
797 if (!ust_event_name
) {
798 ret
= LTTNG_ERR_FATAL
;
803 * The loglevel is hardcoded with 0 here since the agent ust event is set
804 * with the loglevel type to ALL thus the loglevel stays 0. The event's
805 * filter is the one handling the loglevel for agent.
807 uevent
= trace_ust_find_event(uchan
->events
, (char *) ust_event_name
,
808 aevent
->filter
, 0, NULL
);
809 /* If the agent event exists, it must be available on the UST side. */
812 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
813 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
814 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
819 * Flag event that it's disabled so the shadow copy on the ust app side
820 * will disable it if an application shows up.
824 ret
= agent_disable_event(aevent
, agt
->domain
);
825 if (ret
!= LTTNG_OK
) {
836 * Disable all agent event for a given UST session.
838 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
840 int event_agent_disable_all(struct ltt_ust_session
*usess
,
844 struct agent_event
*aevent
;
845 struct lttng_ht_iter iter
;
851 * Disable event on agent application. Continue to disable all other events
852 * if the * event is not found.
854 ret
= event_agent_disable(usess
, agt
, "*");
855 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
859 /* Flag every event that they are now enabled. */
861 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
863 if (!aevent
->enabled
) {
867 ret
= event_agent_disable(usess
, agt
, aevent
->name
);
868 if (ret
!= LTTNG_OK
) {