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.
21 #include <urcu/list.h>
24 #include <lttng/lttng.h>
25 #include <common/error.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
31 #include "lttng-sessiond.h"
34 #include "trace-kernel.h"
35 #include "trace-ust.h"
38 * Add unique UST event based on the event name, filter bytecode and loglevel.
40 static void add_unique_ust_event(struct lttng_ht
*ht
,
41 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_filter_bytecode
*) event
->filter
;
52 key
.loglevel
= event
->attr
.loglevel
;
53 key
.exclusion
= event
->exclusion
;
55 node_ptr
= cds_lfht_add_unique(ht
->ht
,
56 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
57 trace_ust_ht_match_event
, &key
, &event
->node
.node
);
58 assert(node_ptr
== &event
->node
.node
);
62 * Disable kernel tracepoint event for a channel from the kernel session.
64 int event_kernel_disable_tracepoint(struct ltt_kernel_channel
*kchan
,
68 struct ltt_kernel_event
*kevent
;
72 kevent
= trace_kernel_get_event_by_name(event_name
, kchan
);
74 ret
= LTTNG_ERR_NO_EVENT
;
78 ret
= kernel_disable_event(kevent
);
80 ret
= LTTNG_ERR_KERN_DISABLE_FAIL
;
84 DBG("Kernel event %s disable for channel %s.",
85 kevent
->event
->name
, kchan
->channel
->name
);
94 * Enable kernel system call for a channel from the kernel session.
96 int event_kernel_enable_syscall(struct ltt_kernel_channel
*kchan
,
103 ret
= kernel_enable_syscall(syscall_name
, kchan
);
105 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
109 DBG("Kernel event %s enable for channel %s.",
110 syscall_name
, kchan
->channel
->name
);
119 * Disable kernel system call for a channel from the kernel session.
121 int event_kernel_disable_syscall(struct ltt_kernel_channel
*kchan
,
128 ret
= kernel_disable_syscall(syscall_name
, kchan
);
130 ret
= LTTNG_ERR_KERN_DISABLE_FAIL
;
134 DBG("Kernel syscall %s disable for channel %s.",
135 syscall_name
[0] == '\0' ? "<all>" : syscall_name
,
136 kchan
->channel
->name
);
145 * Disable kernel tracepoint events for a channel from the kernel session.
147 int event_kernel_disable_all_tracepoints(struct ltt_kernel_channel
*kchan
)
150 struct ltt_kernel_event
*kevent
;
154 /* For each event in the kernel session */
155 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
156 ret
= kernel_disable_event(kevent
);
158 /* We continue disabling the rest */
167 * Disable all kernel event for a channel from the kernel session.
169 int event_kernel_disable_all(struct ltt_kernel_channel
*kchan
)
175 ret
= event_kernel_disable_all_tracepoints(kchan
);
178 ret
= event_kernel_disable_syscall(kchan
, "");
183 * Enable kernel tracepoint event for a channel from the kernel session.
184 * We own filter_expression and filter.
186 int event_kernel_enable_tracepoint(struct ltt_kernel_channel
*kchan
,
187 struct lttng_event
*event
)
190 struct ltt_kernel_event
*kevent
;
195 kevent
= trace_kernel_get_event_by_name(event
->name
, kchan
);
196 if (kevent
== NULL
) {
197 ret
= kernel_create_event(event
, kchan
);
201 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
204 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
207 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
212 } else if (kevent
->enabled
== 0) {
213 ret
= kernel_enable_event(kevent
);
215 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
219 /* At this point, the event is considered enabled */
220 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
230 * Enable all kernel tracepoint events of a channel of the kernel session.
232 int event_kernel_enable_all_tracepoints(struct ltt_kernel_channel
*kchan
,
233 int kernel_tracer_fd
)
236 struct ltt_kernel_event
*kevent
;
237 struct lttng_event
*event_list
= NULL
;
241 /* For each event in the kernel session */
242 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
243 if (kevent
->enabled
== 0) {
244 ret
= kernel_enable_event(kevent
);
246 /* Enable failed but still continue */
252 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
254 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
258 for (i
= 0; i
< size
; i
++) {
259 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
260 if (kevent
== NULL
) {
261 /* Default event type for enable all */
262 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
263 /* Enable each single tracepoint event */
264 ret
= kernel_create_event(&event_list
[i
], kchan
);
266 /* Ignore error here and continue */
278 * Enable all kernel events of a channel of the kernel session.
280 int event_kernel_enable_all(struct ltt_kernel_channel
*kchan
,
281 int kernel_tracer_fd
)
287 tp_ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
288 if (tp_ret
!= LTTNG_OK
) {
293 * Reaching this code path means that all tracepoints were enabled without
294 * errors so we ignore the error value of syscalls.
296 * At the moment, failing to enable syscalls on "lttng enable-event -a -k"
297 * is not considered an error that need to be returned to the client since
298 * tracepoints did not fail. Future work will allow us to send back
299 * multiple errors to the client in one API call.
301 (void) event_kernel_enable_syscall(kchan
, "");
308 * ============================
309 * UST : The Ultimate Frontier!
310 * ============================
314 * Enable all UST tracepoints for a channel from a UST session.
316 int event_ust_enable_all_tracepoints(struct ltt_ust_session
*usess
,
317 struct ltt_ust_channel
*uchan
,
318 char *filter_expression
,
319 struct lttng_filter_bytecode
*filter
)
322 struct lttng_ht_iter iter
;
323 struct ltt_ust_event
*uevent
= NULL
;
324 struct lttng_event
*events
= NULL
;
331 /* Enable existing events */
332 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
334 if (uevent
->enabled
== 0) {
335 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
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
++) {
352 * Check if event exist and if so, continue since it was enable
355 uevent
= trace_ust_find_event(uchan
->events
, events
[i
].name
, filter
,
356 events
[i
].loglevel
, NULL
);
357 if (uevent
!= NULL
) {
358 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
361 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
362 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
369 /* Create ust event */
370 uevent
= trace_ust_create_event(&events
[i
], filter_expression
,
372 if (uevent
== NULL
) {
373 ret
= LTTNG_ERR_FATAL
;
377 /* Create event for the specific PID */
378 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
381 if (ret
== -LTTNG_UST_ERR_EXIST
) {
382 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
385 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
391 /* Add ltt ust event to channel */
393 add_unique_ust_event(uchan
->events
, uevent
);
402 trace_ust_destroy_event(uevent
);
411 * Enable UST tracepoint event for a channel from a UST session.
412 * We own filter_expression, filter, and exclusion.
414 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
,
415 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
416 char *filter_expression
,
417 struct lttng_filter_bytecode
*filter
,
418 struct lttng_event_exclusion
*exclusion
)
420 int ret
= LTTNG_OK
, to_create
= 0;
421 struct ltt_ust_event
*uevent
;
429 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
430 event
->loglevel
, exclusion
);
431 if (uevent
== NULL
) {
432 uevent
= trace_ust_create_event(event
, filter_expression
,
434 /* We have passed ownership */
435 filter_expression
= NULL
;
438 if (uevent
== NULL
) {
439 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
443 /* Valid to set it after the goto error since uevent is still NULL */
447 if (uevent
->enabled
) {
448 /* It's already enabled so everything is OK */
449 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
456 /* Create event on all UST registered apps for session */
457 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
459 /* Enable event on all UST registered apps for session */
460 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
464 if (ret
== -LTTNG_UST_ERR_EXIST
) {
465 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
468 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
474 /* Add ltt ust event to channel */
475 add_unique_ust_event(uchan
->events
, uevent
);
478 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
479 to_create
? "created" : "enabled", uchan
->name
);
485 free(filter_expression
);
492 * Only destroy event on creation time (not enabling time) because if the
493 * event is found in the channel (to_create == 0), it means that at some
494 * point the enable_event worked and it's thus valid to keep it alive.
495 * Destroying it also implies that we also destroy it's shadow copy to sync
499 /* In this code path, the uevent was not added to the hash table */
500 trace_ust_destroy_event(uevent
);
503 free(filter_expression
);
510 * Disable UST tracepoint of a channel from a UST session.
512 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
513 struct ltt_ust_channel
*uchan
, char *event_name
)
516 struct ltt_ust_event
*uevent
;
517 struct lttng_ht_node_str
*node
;
518 struct lttng_ht_iter iter
;
530 * We use a custom lookup since we need the iterator for the next_duplicate
531 * call in the do while loop below.
533 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
534 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
535 node
= lttng_ht_iter_get_node_str(&iter
);
537 DBG2("Trace UST event NOT found by name %s", event_name
);
538 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
543 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
546 if (uevent
->enabled
== 0) {
547 /* It's already disabled so everything is OK */
551 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
552 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
553 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
558 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
562 /* Get next duplicate event by name. */
563 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
564 event_name
, &iter
.iter
);
565 node
= lttng_ht_iter_get_node_str(&iter
);
576 * Disable all UST tracepoints for a channel from a UST session.
578 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
,
579 struct ltt_ust_channel
*uchan
)
582 struct lttng_ht_iter iter
;
583 struct ltt_ust_event
*uevent
= NULL
;
584 struct lttng_event
*events
= NULL
;
591 /* Disabling existing events */
592 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
594 if (uevent
->enabled
== 1) {
595 ret
= event_ust_disable_tracepoint(usess
, uchan
,
603 /* Get all UST available events */
604 size
= ust_app_list_events(&events
);
606 ret
= LTTNG_ERR_UST_LIST_FAIL
;
610 for (i
= 0; i
< size
; i
++) {
611 ret
= event_ust_disable_tracepoint(usess
, uchan
,
613 if (ret
!= LTTNG_OK
) {
614 /* Continue to disable the rest... */
630 * Enable all agent event for a given UST session.
632 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
634 int event_agent_enable_all(struct ltt_ust_session
*usess
,
635 struct agent
*agt
, struct lttng_event
*event
,
636 struct lttng_filter_bytecode
*filter
)
639 struct agent_event
*aevent
;
640 struct lttng_ht_iter iter
;
644 DBG("Event agent enabling ALL events for session %" PRIu64
, usess
->id
);
646 /* Enable event on agent application through TCP socket. */
647 ret
= event_agent_enable(usess
, agt
, event
, filter
);
648 if (ret
!= LTTNG_OK
) {
652 /* Flag every event that they are now enabled. */
654 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
667 * Enable a single agent event for a given UST session.
669 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
671 int event_agent_enable(struct ltt_ust_session
*usess
,
672 struct agent
*agt
, struct lttng_event
*event
,
673 struct lttng_filter_bytecode
*filter
)
675 int ret
, created
= 0;
676 struct agent_event
*aevent
;
682 DBG("Event agent enabling %s for session %" PRIu64
" with loglevel type %d "
683 "and loglevel %d", event
->name
, usess
->id
, event
->loglevel_type
,
686 aevent
= agent_find_event(event
->name
, event
->loglevel
, agt
);
688 aevent
= agent_create_event(event
->name
, filter
);
690 ret
= LTTNG_ERR_NOMEM
;
693 aevent
->loglevel
= event
->loglevel
;
694 aevent
->loglevel_type
= event
->loglevel_type
;
698 /* Already enabled? */
699 if (aevent
->enabled
) {
703 ret
= agent_enable_event(aevent
, agt
->domain
);
704 if (ret
!= LTTNG_OK
) {
708 /* If the event was created prior to the enable, add it to the domain. */
710 agent_add_event(aevent
, agt
);
718 agent_destroy_event(aevent
);
724 * Return the agent default event name to use by testing if the process is root
725 * or not. Return NULL on error.
727 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain
)
729 const char *default_event_name
= NULL
;
732 case LTTNG_DOMAIN_LOG4J
:
734 default_event_name
= DEFAULT_SYS_LOG4J_EVENT_NAME
;
736 default_event_name
= DEFAULT_USER_LOG4J_EVENT_NAME
;
739 case LTTNG_DOMAIN_JUL
:
741 default_event_name
= DEFAULT_SYS_JUL_EVENT_NAME
;
743 default_event_name
= DEFAULT_USER_JUL_EVENT_NAME
;
746 case LTTNG_DOMAIN_PYTHON
:
747 default_event_name
= DEFAULT_USER_PYTHON_EVENT_NAME
;
753 return default_event_name
;
757 * Disable a single agent event for a given UST session.
759 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
761 int event_agent_disable(struct ltt_ust_session
*usess
, struct agent
*agt
,
765 struct agent_event
*aevent
;
766 struct ltt_ust_event
*uevent
= NULL
;
767 struct ltt_ust_channel
*uchan
= NULL
;
768 const char *ust_event_name
, *ust_channel_name
;
774 DBG("Event agent disabling %s for session %" PRIu64
, event_name
, usess
->id
);
776 aevent
= agent_find_event_by_name(event_name
, agt
);
778 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
782 /* Already disabled? */
783 if (!aevent
->enabled
) {
787 if (agt
->domain
== LTTNG_DOMAIN_JUL
) {
788 ust_channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
789 } else if (agt
->domain
== LTTNG_DOMAIN_LOG4J
) {
790 ust_channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
791 } else if (agt
->domain
== LTTNG_DOMAIN_PYTHON
) {
792 ust_channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
794 ret
= LTTNG_ERR_INVALID
;
799 * Disable it on the UST side. First get the channel reference then find
800 * the event and finally disable it.
802 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
803 (char *) ust_channel_name
);
805 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
809 ust_event_name
= event_get_default_agent_ust_name(agt
->domain
);
810 if (!ust_event_name
) {
811 ret
= LTTNG_ERR_FATAL
;
816 * The loglevel is hardcoded with 0 here since the agent ust event is set
817 * with the loglevel type to ALL thus the loglevel stays 0. The event's
818 * filter is the one handling the loglevel for agent.
820 uevent
= trace_ust_find_event(uchan
->events
, (char *) ust_event_name
,
821 aevent
->filter
, 0, NULL
);
822 /* If the agent event exists, it must be available on the UST side. */
825 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
826 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
827 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
832 * Flag event that it's disabled so the shadow copy on the ust app side
833 * will disable it if an application shows up.
837 ret
= agent_disable_event(aevent
, agt
->domain
);
838 if (ret
!= LTTNG_OK
) {
849 * Disable all agent event for a given UST session.
851 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
853 int event_agent_disable_all(struct ltt_ust_session
*usess
,
857 struct agent_event
*aevent
;
858 struct lttng_ht_iter iter
;
864 * Disable event on agent application. Continue to disable all other events
865 * if the * event is not found.
867 ret
= event_agent_disable(usess
, agt
, "*");
868 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
872 /* Flag every event that they are now enabled. */
874 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
876 if (!aevent
->enabled
) {
880 ret
= event_agent_disable(usess
, agt
, aevent
->name
);
881 if (ret
!= LTTNG_OK
) {