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>
32 #include "trace-kernel.h"
33 #include "trace-ust.h"
36 * Add unique UST event based on the event name, filter bytecode and loglevel.
38 static void add_unique_ust_event(struct lttng_ht
*ht
,
39 struct ltt_ust_event
*event
)
41 struct cds_lfht_node
*node_ptr
;
42 struct ltt_ust_ht_key key
;
48 key
.name
= event
->attr
.name
;
49 key
.filter
= (struct lttng_filter_bytecode
*) event
->filter
;
50 key
.loglevel
= event
->attr
.loglevel
;
51 key
.exclusion
= event
->exclusion
;
53 node_ptr
= cds_lfht_add_unique(ht
->ht
,
54 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
55 trace_ust_ht_match_event
, &key
, &event
->node
.node
);
56 assert(node_ptr
== &event
->node
.node
);
60 * Setup a lttng_event used to enable *all* syscall tracing.
62 static void init_syscalls_kernel_event(struct lttng_event
*event
)
66 event
->name
[0] = '\0';
68 * We use LTTNG_EVENT* here since the trace kernel creation will make the
69 * right changes for the kernel.
71 event
->type
= LTTNG_EVENT_SYSCALL
;
75 * Disable kernel tracepoint event for a channel from the kernel session.
77 int event_kernel_disable_tracepoint(struct ltt_kernel_channel
*kchan
,
81 struct ltt_kernel_event
*kevent
;
85 kevent
= trace_kernel_get_event_by_name(event_name
, kchan
);
87 ret
= LTTNG_ERR_NO_EVENT
;
91 ret
= kernel_disable_event(kevent
);
93 ret
= LTTNG_ERR_KERN_DISABLE_FAIL
;
97 DBG("Kernel event %s disable for channel %s.",
98 kevent
->event
->name
, kchan
->channel
->name
);
107 * Disable kernel tracepoint events for a channel from the kernel session.
109 int event_kernel_disable_all_tracepoints(struct ltt_kernel_channel
*kchan
)
112 struct ltt_kernel_event
*kevent
;
116 /* For each event in the kernel session */
117 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
118 ret
= kernel_disable_event(kevent
);
120 /* We continue disabling the rest */
129 * Disable kernel syscall events for a channel from the kernel session.
131 int event_kernel_disable_all_syscalls(struct ltt_kernel_channel
*kchan
)
133 ERR("Cannot disable syscall tracing for existing session. Please destroy session instead.");
134 return LTTNG_OK
; /* Return OK so disable all succeeds */
138 * Disable all kernel event for a channel from the kernel session.
140 int event_kernel_disable_all(struct ltt_kernel_channel
*kchan
)
146 ret
= event_kernel_disable_all_tracepoints(kchan
);
149 ret
= event_kernel_disable_all_syscalls(kchan
);
154 * Enable kernel tracepoint event for a channel from the kernel session.
156 int event_kernel_enable_tracepoint(struct ltt_kernel_channel
*kchan
,
157 struct lttng_event
*event
)
160 struct ltt_kernel_event
*kevent
;
165 kevent
= trace_kernel_get_event_by_name(event
->name
, kchan
);
166 if (kevent
== NULL
) {
167 ret
= kernel_create_event(event
, kchan
);
171 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
174 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
177 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
182 } else if (kevent
->enabled
== 0) {
183 ret
= kernel_enable_event(kevent
);
185 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
189 /* At this point, the event is considered enabled */
190 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
200 * Enable all kernel tracepoint events of a channel of the kernel session.
202 int event_kernel_enable_all_tracepoints(struct ltt_kernel_channel
*kchan
,
203 int kernel_tracer_fd
)
206 struct ltt_kernel_event
*kevent
;
207 struct lttng_event
*event_list
= NULL
;
211 /* For each event in the kernel session */
212 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
213 if (kevent
->enabled
== 0) {
214 ret
= kernel_enable_event(kevent
);
216 /* Enable failed but still continue */
222 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
224 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
228 for (i
= 0; i
< size
; i
++) {
229 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
230 if (kevent
== NULL
) {
231 /* Default event type for enable all */
232 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
233 /* Enable each single tracepoint event */
234 ret
= kernel_create_event(&event_list
[i
], kchan
);
236 /* Ignore error here and continue */
248 * Enable all kernel sycalls events of a channel of the kernel session.
250 int event_kernel_enable_all_syscalls(struct ltt_kernel_channel
*kchan
,
251 int kernel_tracer_fd
)
254 struct lttng_event event
;
258 init_syscalls_kernel_event(&event
);
260 DBG("Enabling all syscall tracing");
262 ret
= kernel_create_event(&event
, kchan
);
264 if (ret
== -EEXIST
) {
265 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
267 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
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_all_syscalls(kchan
, kernel_tracer_fd
);
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
, 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
, NULL
);
369 if (uevent
== NULL
) {
370 ret
= LTTNG_ERR_FATAL
;
374 /* Create event for the specific PID */
375 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
378 if (ret
== -LTTNG_UST_ERR_EXIST
) {
379 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
382 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
388 /* Add ltt ust event to channel */
390 add_unique_ust_event(uchan
->events
, uevent
);
399 trace_ust_destroy_event(uevent
);
408 * Enable UST tracepoint event for a channel from a UST session.
410 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
,
411 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
412 struct lttng_filter_bytecode
*filter
,
413 struct lttng_event_exclusion
*exclusion
)
415 int ret
= LTTNG_OK
, to_create
= 0;
416 struct ltt_ust_event
*uevent
;
424 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
425 event
->loglevel
, exclusion
);
426 if (uevent
== NULL
) {
427 uevent
= trace_ust_create_event(event
, filter
, exclusion
);
428 if (uevent
== NULL
) {
429 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
433 /* Valid to set it after the goto error since uevent is still NULL */
437 if (uevent
->enabled
) {
438 /* It's already enabled so everything is OK */
439 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
446 /* Create event on all UST registered apps for session */
447 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
449 /* Enable event on all UST registered apps for session */
450 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
454 if (ret
== -LTTNG_UST_ERR_EXIST
) {
455 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
458 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
464 /* Add ltt ust event to channel */
465 add_unique_ust_event(uchan
->events
, uevent
);
468 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
469 to_create
? "created" : "enabled", uchan
->name
);
479 * Only destroy event on creation time (not enabling time) because if the
480 * event is found in the channel (to_create == 0), it means that at some
481 * point the enable_event worked and it's thus valid to keep it alive.
482 * Destroying it also implies that we also destroy it's shadow copy to sync
486 /* In this code path, the uevent was not added to the hash table */
487 trace_ust_destroy_event(uevent
);
494 * Disable UST tracepoint of a channel from a UST session.
496 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
497 struct ltt_ust_channel
*uchan
, char *event_name
)
500 struct ltt_ust_event
*uevent
;
501 struct lttng_ht_node_str
*node
;
502 struct lttng_ht_iter iter
;
514 * We use a custom lookup since we need the iterator for the next_duplicate
515 * call in the do while loop below.
517 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
518 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
519 node
= lttng_ht_iter_get_node_str(&iter
);
521 DBG2("Trace UST event NOT found by name %s", event_name
);
522 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
527 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
530 if (uevent
->enabled
== 0) {
531 /* It's already disabled so everything is OK */
535 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
536 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
537 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
542 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
546 /* Get next duplicate event by name. */
547 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
548 event_name
, &iter
.iter
);
549 node
= lttng_ht_iter_get_node_str(&iter
);
560 * Disable all UST tracepoints for a channel from a UST session.
562 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
,
563 struct ltt_ust_channel
*uchan
)
566 struct lttng_ht_iter iter
;
567 struct ltt_ust_event
*uevent
= NULL
;
568 struct lttng_event
*events
= NULL
;
575 /* Disabling existing events */
576 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
578 if (uevent
->enabled
== 1) {
579 ret
= event_ust_disable_tracepoint(usess
, uchan
,
587 /* Get all UST available events */
588 size
= ust_app_list_events(&events
);
590 ret
= LTTNG_ERR_UST_LIST_FAIL
;
594 for (i
= 0; i
< size
; i
++) {
595 ret
= event_ust_disable_tracepoint(usess
, uchan
,
597 if (ret
!= LTTNG_OK
) {
598 /* Continue to disable the rest... */
614 * Enable all JUL event for a given UST session.
616 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
618 int event_jul_enable_all(struct ltt_ust_session
*usess
)
621 struct jul_event
*jevent
;
622 struct lttng_event event
;
623 struct lttng_ht_iter iter
;
627 DBG("Event JUL enabling ALL events for session %" PRIu64
, usess
->id
);
629 /* Create the * wildcard event name for the Java agent. */
630 memset(event
.name
, 0, sizeof(event
.name
));
631 strncpy(event
.name
, "*", sizeof(event
.name
));
632 event
.name
[sizeof(event
.name
) - 1] = '\0';
634 /* Enable event on JUL application through TCP socket. */
635 ret
= event_jul_enable(usess
, &event
);
636 if (ret
!= LTTNG_OK
) {
640 /* Flag every event that they are now enabled. */
642 cds_lfht_for_each_entry(usess
->domain_jul
.events
->ht
, &iter
.iter
, jevent
,
655 * Enable a single JUL event for a given UST session.
657 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
659 int event_jul_enable(struct ltt_ust_session
*usess
, struct lttng_event
*event
)
661 int ret
, created
= 0;
662 struct jul_event
*jevent
;
667 DBG("Event JUL enabling %s for session %" PRIu64
, event
->name
, usess
->id
);
669 jevent
= jul_find_by_name(event
->name
, &usess
->domain_jul
);
671 jevent
= jul_create_event(event
->name
);
673 ret
= LTTNG_ERR_NOMEM
;
679 /* Already enabled? */
680 if (jevent
->enabled
) {
684 ret
= jul_enable_event(jevent
);
685 if (ret
!= LTTNG_OK
) {
689 /* If the event was created prior to the enable, add it to the domain. */
691 jul_add_event(jevent
, &usess
->domain_jul
);
699 jul_destroy_event(jevent
);
705 * Disable a single JUL event for a given UST session.
707 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
709 int event_jul_disable(struct ltt_ust_session
*usess
, char *event_name
)
712 struct jul_event
*jevent
;
717 DBG("Event JUL disabling %s for session %" PRIu64
, event_name
, usess
->id
);
719 jevent
= jul_find_by_name(event_name
, &usess
->domain_jul
);
721 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
725 /* Already disabled? */
726 if (!jevent
->enabled
) {
730 ret
= jul_disable_event(jevent
);
731 if (ret
!= LTTNG_OK
) {
742 * Disable all JUL event for a given UST session.
744 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
746 int event_jul_disable_all(struct ltt_ust_session
*usess
)
748 int ret
, do_disable
= 0;
749 struct jul_event
*jevent
;
750 struct lttng_ht_iter iter
;
754 /* Enable event on JUL application through TCP socket. */
755 ret
= event_jul_disable(usess
, "*");
756 if (ret
!= LTTNG_OK
) {
757 if (ret
== LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
759 * This means that no enable all was done before but still a user
760 * could want to disable everything even though the * wild card
761 * event does not exists.
769 /* Flag every event that they are now enabled. */
771 cds_lfht_for_each_entry(usess
->domain_jul
.events
->ht
, &iter
.iter
, jevent
,
773 if (jevent
->enabled
&& do_disable
) {
774 ret
= event_jul_disable(usess
, jevent
->name
);
775 if (ret
!= LTTNG_OK
) {