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 * Setup a lttng_event used to enable *all* syscall tracing.
63 static void init_syscalls_kernel_event(struct lttng_event
*event
)
67 event
->name
[0] = '\0';
69 * We use LTTNG_EVENT* here since the trace kernel creation will make the
70 * right changes for the kernel.
72 event
->type
= LTTNG_EVENT_SYSCALL
;
76 * Disable kernel tracepoint event for a channel from the kernel session.
78 int event_kernel_disable_tracepoint(struct ltt_kernel_channel
*kchan
,
82 struct ltt_kernel_event
*kevent
;
86 kevent
= trace_kernel_get_event_by_name(event_name
, kchan
);
88 ret
= LTTNG_ERR_NO_EVENT
;
92 ret
= kernel_disable_event(kevent
);
94 ret
= LTTNG_ERR_KERN_DISABLE_FAIL
;
98 DBG("Kernel event %s disable for channel %s.",
99 kevent
->event
->name
, kchan
->channel
->name
);
108 * Disable kernel tracepoint events for a channel from the kernel session.
110 int event_kernel_disable_all_tracepoints(struct ltt_kernel_channel
*kchan
)
113 struct ltt_kernel_event
*kevent
;
117 /* For each event in the kernel session */
118 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
119 ret
= kernel_disable_event(kevent
);
121 /* We continue disabling the rest */
130 * Disable kernel syscall events for a channel from the kernel session.
132 int event_kernel_disable_all_syscalls(struct ltt_kernel_channel
*kchan
)
134 ERR("Cannot disable syscall tracing for existing session. Please destroy session instead.");
135 return LTTNG_OK
; /* Return OK so disable all succeeds */
139 * Disable all kernel event for a channel from the kernel session.
141 int event_kernel_disable_all(struct ltt_kernel_channel
*kchan
)
147 ret
= event_kernel_disable_all_tracepoints(kchan
);
150 ret
= event_kernel_disable_all_syscalls(kchan
);
155 * Enable kernel tracepoint event for a channel from the kernel session.
157 int event_kernel_enable_tracepoint(struct ltt_kernel_channel
*kchan
,
158 struct lttng_event
*event
)
161 struct ltt_kernel_event
*kevent
;
166 kevent
= trace_kernel_get_event_by_name(event
->name
, kchan
);
167 if (kevent
== NULL
) {
168 ret
= kernel_create_event(event
, kchan
);
172 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
175 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
178 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
183 } else if (kevent
->enabled
== 0) {
184 ret
= kernel_enable_event(kevent
);
186 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
190 /* At this point, the event is considered enabled */
191 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
201 * Enable all kernel tracepoint events of a channel of the kernel session.
203 int event_kernel_enable_all_tracepoints(struct ltt_kernel_channel
*kchan
,
204 int kernel_tracer_fd
)
207 struct ltt_kernel_event
*kevent
;
208 struct lttng_event
*event_list
= NULL
;
212 /* For each event in the kernel session */
213 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
214 if (kevent
->enabled
== 0) {
215 ret
= kernel_enable_event(kevent
);
217 /* Enable failed but still continue */
223 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
225 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
229 for (i
= 0; i
< size
; i
++) {
230 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
231 if (kevent
== NULL
) {
232 /* Default event type for enable all */
233 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
234 /* Enable each single tracepoint event */
235 ret
= kernel_create_event(&event_list
[i
], kchan
);
237 /* Ignore error here and continue */
249 * Enable all kernel sycalls events of a channel of the kernel session.
251 int event_kernel_enable_all_syscalls(struct ltt_kernel_channel
*kchan
,
252 int kernel_tracer_fd
)
255 struct lttng_event event
;
259 init_syscalls_kernel_event(&event
);
261 DBG("Enabling all syscall tracing");
263 ret
= kernel_create_event(&event
, kchan
);
265 if (ret
== -EEXIST
) {
266 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
268 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
279 * Enable all kernel events of a channel of the kernel session.
281 int event_kernel_enable_all(struct ltt_kernel_channel
*kchan
,
282 int kernel_tracer_fd
)
288 tp_ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
289 if (tp_ret
!= LTTNG_OK
) {
294 * Reaching this code path means that all tracepoints were enabled without
295 * errors so we ignore the error value of syscalls.
297 * At the moment, failing to enable syscalls on "lttng enable-event -a -k"
298 * is not considered an error that need to be returned to the client since
299 * tracepoints did not fail. Future work will allow us to send back
300 * multiple errors to the client in one API call.
302 (void) event_kernel_enable_all_syscalls(kchan
, kernel_tracer_fd
);
309 * ============================
310 * UST : The Ultimate Frontier!
311 * ============================
315 * Enable all UST tracepoints for a channel from a UST session.
317 int event_ust_enable_all_tracepoints(struct ltt_ust_session
*usess
,
318 struct ltt_ust_channel
*uchan
,
319 char *filter_expression
,
320 struct lttng_filter_bytecode
*filter
)
323 struct lttng_ht_iter iter
;
324 struct ltt_ust_event
*uevent
= NULL
;
325 struct lttng_event
*events
= NULL
;
332 /* Enable existing events */
333 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
335 if (uevent
->enabled
== 0) {
336 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
344 /* Get all UST available events */
345 size
= ust_app_list_events(&events
);
347 ret
= LTTNG_ERR_UST_LIST_FAIL
;
351 for (i
= 0; i
< size
; i
++) {
353 * Check if event exist and if so, continue since it was enable
356 uevent
= trace_ust_find_event(uchan
->events
, events
[i
].name
, filter
,
357 events
[i
].loglevel
, NULL
);
358 if (uevent
!= NULL
) {
359 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
362 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
363 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
370 /* Create ust event */
371 uevent
= trace_ust_create_event(&events
[i
], filter_expression
,
373 if (uevent
== NULL
) {
374 ret
= LTTNG_ERR_FATAL
;
378 /* Create event for the specific PID */
379 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
382 if (ret
== -LTTNG_UST_ERR_EXIST
) {
383 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
386 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
392 /* Add ltt ust event to channel */
394 add_unique_ust_event(uchan
->events
, uevent
);
403 trace_ust_destroy_event(uevent
);
412 * Enable UST tracepoint event for a channel from a UST session.
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 if (uevent
== NULL
) {
435 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
439 /* Valid to set it after the goto error since uevent is still NULL */
443 if (uevent
->enabled
) {
444 /* It's already enabled so everything is OK */
445 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
452 /* Create event on all UST registered apps for session */
453 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
455 /* Enable event on all UST registered apps for session */
456 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
460 if (ret
== -LTTNG_UST_ERR_EXIST
) {
461 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
464 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
470 /* Add ltt ust event to channel */
471 add_unique_ust_event(uchan
->events
, uevent
);
474 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
475 to_create
? "created" : "enabled", uchan
->name
);
485 * Only destroy event on creation time (not enabling time) because if the
486 * event is found in the channel (to_create == 0), it means that at some
487 * point the enable_event worked and it's thus valid to keep it alive.
488 * Destroying it also implies that we also destroy it's shadow copy to sync
492 /* In this code path, the uevent was not added to the hash table */
493 trace_ust_destroy_event(uevent
);
500 * Disable UST tracepoint of a channel from a UST session.
502 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
503 struct ltt_ust_channel
*uchan
, char *event_name
)
506 struct ltt_ust_event
*uevent
;
507 struct lttng_ht_node_str
*node
;
508 struct lttng_ht_iter iter
;
520 * We use a custom lookup since we need the iterator for the next_duplicate
521 * call in the do while loop below.
523 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
524 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
525 node
= lttng_ht_iter_get_node_str(&iter
);
527 DBG2("Trace UST event NOT found by name %s", event_name
);
528 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
533 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
536 if (uevent
->enabled
== 0) {
537 /* It's already disabled so everything is OK */
541 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
542 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
543 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
548 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
552 /* Get next duplicate event by name. */
553 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
554 event_name
, &iter
.iter
);
555 node
= lttng_ht_iter_get_node_str(&iter
);
566 * Disable all UST tracepoints for a channel from a UST session.
568 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
,
569 struct ltt_ust_channel
*uchan
)
572 struct lttng_ht_iter iter
;
573 struct ltt_ust_event
*uevent
= NULL
;
574 struct lttng_event
*events
= NULL
;
581 /* Disabling existing events */
582 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
584 if (uevent
->enabled
== 1) {
585 ret
= event_ust_disable_tracepoint(usess
, uchan
,
593 /* Get all UST available events */
594 size
= ust_app_list_events(&events
);
596 ret
= LTTNG_ERR_UST_LIST_FAIL
;
600 for (i
= 0; i
< size
; i
++) {
601 ret
= event_ust_disable_tracepoint(usess
, uchan
,
603 if (ret
!= LTTNG_OK
) {
604 /* Continue to disable the rest... */
620 * Enable all JUL event for a given UST session.
622 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
624 int event_jul_enable_all(struct ltt_ust_session
*usess
,
625 struct lttng_event
*event
, struct lttng_filter_bytecode
*filter
)
628 struct jul_event
*jevent
;
629 struct lttng_ht_iter iter
;
633 DBG("Event JUL enabling ALL events for session %" PRIu64
, usess
->id
);
635 /* Enable event on JUL application through TCP socket. */
636 ret
= event_jul_enable(usess
, event
, filter
);
637 if (ret
!= LTTNG_OK
) {
641 /* Flag every event that they are now enabled. */
643 cds_lfht_for_each_entry(usess
->domain_jul
.events
->ht
, &iter
.iter
, jevent
,
656 * Enable a single JUL event for a given UST session.
658 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
660 int event_jul_enable(struct ltt_ust_session
*usess
, struct lttng_event
*event
,
661 struct lttng_filter_bytecode
*filter
)
663 int ret
, created
= 0;
664 struct jul_event
*jevent
;
669 DBG("Event JUL enabling %s for session %" PRIu64
" with loglevel type %d "
670 "and loglevel %d", event
->name
, usess
->id
, event
->loglevel_type
,
673 jevent
= jul_find_event(event
->name
, event
->loglevel
, &usess
->domain_jul
);
675 jevent
= jul_create_event(event
->name
, filter
);
677 ret
= LTTNG_ERR_NOMEM
;
680 jevent
->loglevel
= event
->loglevel
;
681 jevent
->loglevel_type
= event
->loglevel_type
;
685 /* Already enabled? */
686 if (jevent
->enabled
) {
690 ret
= jul_enable_event(jevent
);
691 if (ret
!= LTTNG_OK
) {
695 /* If the event was created prior to the enable, add it to the domain. */
697 jul_add_event(jevent
, &usess
->domain_jul
);
705 jul_destroy_event(jevent
);
711 * Disable a single JUL event for a given UST session.
713 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
715 int event_jul_disable(struct ltt_ust_session
*usess
, char *event_name
)
718 struct jul_event
*jevent
;
719 struct ltt_ust_event
*uevent
= NULL
;
720 struct ltt_ust_channel
*uchan
= NULL
;
721 char *ust_event_name
;
726 DBG("Event JUL disabling %s for session %" PRIu64
, event_name
, usess
->id
);
728 jevent
= jul_find_event_by_name(event_name
, &usess
->domain_jul
);
730 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
734 /* Already disabled? */
735 if (!jevent
->enabled
) {
740 * Disable it on the UST side. First get the channel reference then find
741 * the event and finally disable it.
743 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
744 DEFAULT_JUL_CHANNEL_NAME
);
746 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
751 ust_event_name
= DEFAULT_SYS_JUL_EVENT_NAME
;
753 ust_event_name
= DEFAULT_USER_JUL_EVENT_NAME
;
757 * The loglevel is hardcoded with 0 here since the JUL ust event is set
758 * with the loglevel type to ALL thus the loglevel stays 0. The event's
759 * filter is the one handling the loglevel for JUL.
761 uevent
= trace_ust_find_event(uchan
->events
, ust_event_name
,
762 jevent
->filter
, 0, NULL
);
763 /* If the JUL event exists, it must be available on the UST side. */
766 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
767 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
768 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
772 ret
= jul_disable_event(jevent
);
773 if (ret
!= LTTNG_OK
) {
784 * Disable all JUL event for a given UST session.
786 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
788 int event_jul_disable_all(struct ltt_ust_session
*usess
)
790 int ret
, do_disable
= 0;
791 struct jul_event
*jevent
;
792 struct lttng_ht_iter iter
;
796 /* Enable event on JUL application through TCP socket. */
797 ret
= event_jul_disable(usess
, "*");
798 if (ret
!= LTTNG_OK
) {
799 if (ret
== LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
801 * This means that no enable all was done before but still a user
802 * could want to disable everything even though the * wild card
803 * event does not exists.
811 /* Flag every event that they are now enabled. */
813 cds_lfht_for_each_entry(usess
->domain_jul
.events
->ht
, &iter
.iter
, jevent
,
815 if (jevent
->enabled
&& do_disable
) {
816 ret
= event_jul_disable(usess
, jevent
->name
);
817 if (ret
!= LTTNG_OK
) {