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
,
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.
413 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
,
414 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
415 char *filter_expression
,
416 struct lttng_filter_bytecode
*filter
,
417 struct lttng_event_exclusion
*exclusion
)
419 int ret
= LTTNG_OK
, to_create
= 0;
420 struct ltt_ust_event
*uevent
;
428 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
429 event
->loglevel
, exclusion
);
430 if (uevent
== NULL
) {
431 uevent
= trace_ust_create_event(event
, filter_expression
,
433 if (uevent
== NULL
) {
434 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
438 /* Valid to set it after the goto error since uevent is still NULL */
442 if (uevent
->enabled
) {
443 /* It's already enabled so everything is OK */
444 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
451 /* Create event on all UST registered apps for session */
452 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
454 /* Enable event on all UST registered apps for session */
455 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
459 if (ret
== -LTTNG_UST_ERR_EXIST
) {
460 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
463 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
469 /* Add ltt ust event to channel */
470 add_unique_ust_event(uchan
->events
, uevent
);
473 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
474 to_create
? "created" : "enabled", uchan
->name
);
484 * Only destroy event on creation time (not enabling time) because if the
485 * event is found in the channel (to_create == 0), it means that at some
486 * point the enable_event worked and it's thus valid to keep it alive.
487 * Destroying it also implies that we also destroy it's shadow copy to sync
491 /* In this code path, the uevent was not added to the hash table */
492 trace_ust_destroy_event(uevent
);
499 * Disable UST tracepoint of a channel from a UST session.
501 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
502 struct ltt_ust_channel
*uchan
, char *event_name
)
505 struct ltt_ust_event
*uevent
;
506 struct lttng_ht_node_str
*node
;
507 struct lttng_ht_iter iter
;
519 * We use a custom lookup since we need the iterator for the next_duplicate
520 * call in the do while loop below.
522 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
523 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
524 node
= lttng_ht_iter_get_node_str(&iter
);
526 DBG2("Trace UST event NOT found by name %s", event_name
);
527 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
532 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
535 if (uevent
->enabled
== 0) {
536 /* It's already disabled so everything is OK */
540 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
541 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
542 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
547 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
551 /* Get next duplicate event by name. */
552 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
553 event_name
, &iter
.iter
);
554 node
= lttng_ht_iter_get_node_str(&iter
);
565 * Disable all UST tracepoints for a channel from a UST session.
567 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
,
568 struct ltt_ust_channel
*uchan
)
571 struct lttng_ht_iter iter
;
572 struct ltt_ust_event
*uevent
= NULL
;
573 struct lttng_event
*events
= NULL
;
580 /* Disabling existing events */
581 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
583 if (uevent
->enabled
== 1) {
584 ret
= event_ust_disable_tracepoint(usess
, uchan
,
592 /* Get all UST available events */
593 size
= ust_app_list_events(&events
);
595 ret
= LTTNG_ERR_UST_LIST_FAIL
;
599 for (i
= 0; i
< size
; i
++) {
600 ret
= event_ust_disable_tracepoint(usess
, uchan
,
602 if (ret
!= LTTNG_OK
) {
603 /* Continue to disable the rest... */
619 * Enable all JUL event for a given UST session.
621 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
623 int event_jul_enable_all(struct ltt_ust_session
*usess
,
624 struct lttng_event
*event
)
627 struct jul_event
*jevent
;
628 struct lttng_ht_iter iter
;
632 DBG("Event JUL enabling ALL events for session %" PRIu64
, usess
->id
);
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
" with loglevel type %d "
668 "and loglevel %d", event
->name
, usess
->id
, event
->loglevel_type
,
671 jevent
= jul_find_event(event
->name
, event
->loglevel
, &usess
->domain_jul
);
673 jevent
= jul_create_event(event
->name
);
675 ret
= LTTNG_ERR_NOMEM
;
678 jevent
->loglevel
= event
->loglevel
;
679 jevent
->loglevel_type
= event
->loglevel_type
;
683 /* Already enabled? */
684 if (jevent
->enabled
) {
688 ret
= jul_enable_event(jevent
);
689 if (ret
!= LTTNG_OK
) {
693 /* If the event was created prior to the enable, add it to the domain. */
695 jul_add_event(jevent
, &usess
->domain_jul
);
703 jul_destroy_event(jevent
);
709 * Disable a single JUL event for a given UST session.
711 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
713 int event_jul_disable(struct ltt_ust_session
*usess
, char *event_name
)
716 struct jul_event
*jevent
;
721 DBG("Event JUL disabling %s for session %" PRIu64
, event_name
, usess
->id
);
723 jevent
= jul_find_event_by_name(event_name
, &usess
->domain_jul
);
725 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
729 /* Already disabled? */
730 if (!jevent
->enabled
) {
734 ret
= jul_disable_event(jevent
);
735 if (ret
!= LTTNG_OK
) {
746 * Disable all JUL event for a given UST session.
748 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
750 int event_jul_disable_all(struct ltt_ust_session
*usess
)
752 int ret
, do_disable
= 0;
753 struct jul_event
*jevent
;
754 struct lttng_ht_iter iter
;
758 /* Enable event on JUL application through TCP socket. */
759 ret
= event_jul_disable(usess
, "*");
760 if (ret
!= LTTNG_OK
) {
761 if (ret
== LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
763 * This means that no enable all was done before but still a user
764 * could want to disable everything even though the * wild card
765 * event does not exists.
773 /* Flag every event that they are now enabled. */
775 cds_lfht_for_each_entry(usess
->domain_jul
.events
->ht
, &iter
.iter
, jevent
,
777 if (jevent
->enabled
&& do_disable
) {
778 ret
= event_jul_disable(usess
, jevent
->name
);
779 if (ret
!= LTTNG_OK
) {