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
;
52 node_ptr
= cds_lfht_add_unique(ht
->ht
,
53 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
54 trace_ust_ht_match_event
, &key
, &event
->node
.node
);
55 assert(node_ptr
== &event
->node
.node
);
59 * Setup a lttng_event used to enable *all* syscall tracing.
61 static void init_syscalls_kernel_event(struct lttng_event
*event
)
65 event
->name
[0] = '\0';
67 * We use LTTNG_EVENT* here since the trace kernel creation will make the
68 * right changes for the kernel.
70 event
->type
= LTTNG_EVENT_SYSCALL
;
74 * Disable kernel tracepoint event for a channel from the kernel session.
76 int event_kernel_disable_tracepoint(struct ltt_kernel_session
*ksession
,
77 struct ltt_kernel_channel
*kchan
, char *event_name
)
80 struct ltt_kernel_event
*kevent
;
84 kevent
= trace_kernel_get_event_by_name(event_name
, kchan
);
86 ret
= LTTNG_ERR_NO_EVENT
;
90 ret
= kernel_disable_event(kevent
);
92 ret
= LTTNG_ERR_KERN_DISABLE_FAIL
;
96 DBG("Kernel event %s disable for channel %s.",
97 kevent
->event
->name
, kchan
->channel
->name
);
106 * Disable kernel tracepoint events for a channel from the kernel session.
108 int event_kernel_disable_all_tracepoints(struct ltt_kernel_session
*ksession
,
109 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_session
*ksession
,
132 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_session
*ksession
,
142 struct ltt_kernel_channel
*kchan
)
149 ret
= event_kernel_disable_all_tracepoints(ksession
, kchan
);
152 ret
= event_kernel_disable_all_syscalls(ksession
, kchan
);
157 * Enable kernel tracepoint event for a channel from the kernel session.
159 int event_kernel_enable_tracepoint(struct ltt_kernel_session
*ksession
,
160 struct ltt_kernel_channel
*kchan
, struct lttng_event
*event
)
163 struct ltt_kernel_event
*kevent
;
168 kevent
= trace_kernel_get_event_by_name(event
->name
, kchan
);
169 if (kevent
== NULL
) {
170 ret
= kernel_create_event(event
, kchan
);
174 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
177 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
180 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
185 } else if (kevent
->enabled
== 0) {
186 ret
= kernel_enable_event(kevent
);
188 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
192 /* At this point, the event is considered enabled */
193 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
203 * Enable all kernel tracepoint events of a channel of the kernel session.
205 int event_kernel_enable_all_tracepoints(struct ltt_kernel_session
*ksession
,
206 struct ltt_kernel_channel
*kchan
, int kernel_tracer_fd
)
209 struct ltt_kernel_event
*kevent
;
210 struct lttng_event
*event_list
= NULL
;
214 /* For each event in the kernel session */
215 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
216 if (kevent
->enabled
== 0) {
217 ret
= kernel_enable_event(kevent
);
219 /* Enable failed but still continue */
225 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
227 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
231 for (i
= 0; i
< size
; i
++) {
232 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
233 if (kevent
== NULL
) {
234 /* Default event type for enable all */
235 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
236 /* Enable each single tracepoint event */
237 ret
= kernel_create_event(&event_list
[i
], kchan
);
239 /* Ignore error here and continue */
251 * Enable all kernel tracepoint events of a channel of the kernel session.
253 int event_kernel_enable_all_syscalls(struct ltt_kernel_session
*ksession
,
254 struct ltt_kernel_channel
*kchan
, int kernel_tracer_fd
)
257 struct lttng_event event
;
261 init_syscalls_kernel_event(&event
);
263 DBG("Enabling all syscall tracing");
265 ret
= kernel_create_event(&event
, kchan
);
267 if (ret
== -EEXIST
) {
268 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
270 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
281 * Enable all kernel events of a channel of the kernel session.
283 int event_kernel_enable_all(struct ltt_kernel_session
*ksession
,
284 struct ltt_kernel_channel
*kchan
, int kernel_tracer_fd
)
291 tp_ret
= event_kernel_enable_all_tracepoints(ksession
, kchan
,
293 if (tp_ret
!= LTTNG_OK
) {
298 * Reaching this code path means that all tracepoints were enabled without
299 * errors so we ignore the error value of syscalls.
301 * At the moment, failing to enable syscalls on "lttng enable-event -a -k"
302 * is not considered an error that need to be returned to the client since
303 * tracepoints did not fail. Future work will allow us to send back
304 * multiple errors to the client in one API call.
306 (void) event_kernel_enable_all_syscalls(ksession
, kchan
, kernel_tracer_fd
);
313 * ============================
314 * UST : The Ultimate Frontier!
315 * ============================
319 * Enable all UST tracepoints for a channel from a UST session.
321 int event_ust_enable_all_tracepoints(struct ltt_ust_session
*usess
, int domain
,
322 struct ltt_ust_channel
*uchan
, struct lttng_filter_bytecode
*filter
)
325 struct lttng_ht_iter iter
;
326 struct ltt_ust_event
*uevent
= NULL
;
327 struct lttng_event
*events
= NULL
;
335 case LTTNG_DOMAIN_UST
:
337 /* Enable existing events */
338 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
340 if (uevent
->enabled
== 0) {
341 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
349 /* Get all UST available events */
350 size
= ust_app_list_events(&events
);
352 ret
= LTTNG_ERR_UST_LIST_FAIL
;
356 for (i
= 0; i
< size
; i
++) {
358 * Check if event exist and if so, continue since it was enable
361 uevent
= trace_ust_find_event(uchan
->events
, events
[i
].name
, filter
,
363 if (uevent
!= NULL
) {
364 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
367 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
368 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
375 /* Create ust event */
376 uevent
= trace_ust_create_event(&events
[i
], filter
);
377 if (uevent
== NULL
) {
378 ret
= LTTNG_ERR_FATAL
;
382 /* Create event for the specific PID */
383 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
386 if (ret
== -LTTNG_UST_ERR_EXIST
) {
387 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
390 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
396 /* Add ltt ust event to channel */
398 add_unique_ust_event(uchan
->events
, uevent
);
406 case LTTNG_DOMAIN_UST_EXEC_NAME
:
407 case LTTNG_DOMAIN_UST_PID
:
408 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
419 trace_ust_destroy_event(uevent
);
428 * Enable UST tracepoint event for a channel from a UST session.
430 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
, int domain
,
431 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
432 struct lttng_filter_bytecode
*filter
)
434 int ret
= LTTNG_OK
, to_create
= 0;
435 struct ltt_ust_event
*uevent
;
443 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
445 if (uevent
== NULL
) {
446 uevent
= trace_ust_create_event(event
, filter
);
447 if (uevent
== NULL
) {
448 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
452 /* Valid to set it after the goto error since uevent is still NULL */
456 if (uevent
->enabled
) {
457 /* It's already enabled so everything is OK */
458 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
465 case LTTNG_DOMAIN_UST
:
468 /* Create event on all UST registered apps for session */
469 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
471 /* Enable event on all UST registered apps for session */
472 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
476 if (ret
== -LTTNG_UST_ERR_EXIST
) {
477 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
480 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
487 case LTTNG_DOMAIN_UST_EXEC_NAME
:
488 case LTTNG_DOMAIN_UST_PID
:
489 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
497 /* Add ltt ust event to channel */
498 add_unique_ust_event(uchan
->events
, uevent
);
501 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
502 to_create
? "created" : "enabled", uchan
->name
);
512 * Only destroy event on creation time (not enabling time) because if the
513 * event is found in the channel (to_create == 0), it means that at some
514 * point the enable_event worked and it's thus valid to keep it alive.
515 * Destroying it also implies that we also destroy it's shadow copy to sync
519 /* In this code path, the uevent was not added to the hash table */
520 trace_ust_destroy_event(uevent
);
527 * Disable UST tracepoint of a channel from a UST session.
529 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
, int domain
,
530 struct ltt_ust_channel
*uchan
, char *event_name
)
533 struct ltt_ust_event
*uevent
;
534 struct lttng_ht_node_str
*node
;
535 struct lttng_ht_iter iter
;
547 * We use a custom lookup since we need the iterator for the next_duplicate
548 * call in the do while loop below.
550 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
551 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
552 node
= lttng_ht_iter_get_node_str(&iter
);
554 DBG2("Trace UST event NOT found by name %s", event_name
);
555 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
560 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
563 if (uevent
->enabled
== 0) {
564 /* It's already disabled so everything is OK */
570 case LTTNG_DOMAIN_UST
:
571 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
572 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
573 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
578 case LTTNG_DOMAIN_UST_EXEC_NAME
:
579 case LTTNG_DOMAIN_UST_PID
:
580 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
589 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
592 /* Get next duplicate event by name. */
593 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
594 event_name
, &iter
.iter
);
595 node
= lttng_ht_iter_get_node_str(&iter
);
606 * Disable all UST tracepoints for a channel from a UST session.
608 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
, int domain
,
609 struct ltt_ust_channel
*uchan
)
612 struct lttng_ht_iter iter
;
613 struct ltt_ust_event
*uevent
= NULL
;
614 struct lttng_event
*events
= NULL
;
622 case LTTNG_DOMAIN_UST
:
624 /* Disabling existing events */
625 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
627 if (uevent
->enabled
== 1) {
628 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
,
636 /* Get all UST available events */
637 size
= ust_app_list_events(&events
);
639 ret
= LTTNG_ERR_UST_LIST_FAIL
;
643 for (i
= 0; i
< size
; i
++) {
644 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
,
646 if (ret
!= LTTNG_OK
) {
647 /* Continue to disable the rest... */
656 case LTTNG_DOMAIN_UST_EXEC_NAME
:
657 case LTTNG_DOMAIN_UST_PID
:
658 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
: