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_channel
*kchan
,
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_channel
*kchan
)
111 struct ltt_kernel_event
*kevent
;
115 /* For each event in the kernel session */
116 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
117 ret
= kernel_disable_event(kevent
);
119 /* We continue disabling the rest */
128 * Disable kernel syscall events for a channel from the kernel session.
130 int event_kernel_disable_all_syscalls(struct ltt_kernel_channel
*kchan
)
132 ERR("Cannot disable syscall tracing for existing session. Please destroy session instead.");
133 return LTTNG_OK
; /* Return OK so disable all succeeds */
137 * Disable all kernel event for a channel from the kernel session.
139 int event_kernel_disable_all(struct ltt_kernel_channel
*kchan
)
145 ret
= event_kernel_disable_all_tracepoints(kchan
);
148 ret
= event_kernel_disable_all_syscalls(kchan
);
153 * Enable kernel tracepoint event for a channel from the kernel session.
155 int event_kernel_enable_tracepoint(struct ltt_kernel_channel
*kchan
,
156 struct lttng_event
*event
)
159 struct ltt_kernel_event
*kevent
;
164 kevent
= trace_kernel_get_event_by_name(event
->name
, kchan
);
165 if (kevent
== NULL
) {
166 ret
= kernel_create_event(event
, kchan
);
170 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
173 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
176 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
181 } else if (kevent
->enabled
== 0) {
182 ret
= kernel_enable_event(kevent
);
184 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
188 /* At this point, the event is considered enabled */
189 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
199 * Enable all kernel tracepoint events of a channel of the kernel session.
201 int event_kernel_enable_all_tracepoints(struct ltt_kernel_channel
*kchan
,
202 int kernel_tracer_fd
)
205 struct ltt_kernel_event
*kevent
;
206 struct lttng_event
*event_list
= NULL
;
210 /* For each event in the kernel session */
211 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
212 if (kevent
->enabled
== 0) {
213 ret
= kernel_enable_event(kevent
);
215 /* Enable failed but still continue */
221 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
223 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
227 for (i
= 0; i
< size
; i
++) {
228 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
229 if (kevent
== NULL
) {
230 /* Default event type for enable all */
231 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
232 /* Enable each single tracepoint event */
233 ret
= kernel_create_event(&event_list
[i
], kchan
);
235 /* Ignore error here and continue */
247 * Enable all kernel tracepoint events of a channel of the kernel session.
249 int event_kernel_enable_all_syscalls(struct ltt_kernel_channel
*kchan
,
250 int kernel_tracer_fd
)
253 struct lttng_event event
;
257 init_syscalls_kernel_event(&event
);
259 DBG("Enabling all syscall tracing");
261 ret
= kernel_create_event(&event
, kchan
);
263 if (ret
== -EEXIST
) {
264 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
266 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
277 * Enable all kernel events of a channel of the kernel session.
279 int event_kernel_enable_all(struct ltt_kernel_channel
*kchan
,
280 int kernel_tracer_fd
)
286 tp_ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
287 if (tp_ret
!= LTTNG_OK
) {
292 * Reaching this code path means that all tracepoints were enabled without
293 * errors so we ignore the error value of syscalls.
295 * At the moment, failing to enable syscalls on "lttng enable-event -a -k"
296 * is not considered an error that need to be returned to the client since
297 * tracepoints did not fail. Future work will allow us to send back
298 * multiple errors to the client in one API call.
300 (void) event_kernel_enable_all_syscalls(kchan
, kernel_tracer_fd
);
307 * ============================
308 * UST : The Ultimate Frontier!
309 * ============================
313 * Enable all UST tracepoints for a channel from a UST session.
315 int event_ust_enable_all_tracepoints(struct ltt_ust_session
*usess
, int domain
,
316 struct ltt_ust_channel
*uchan
, struct lttng_filter_bytecode
*filter
)
319 struct lttng_ht_iter iter
;
320 struct ltt_ust_event
*uevent
= NULL
;
321 struct lttng_event
*events
= NULL
;
329 case LTTNG_DOMAIN_UST
:
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
,
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
);
371 if (uevent
== NULL
) {
372 ret
= LTTNG_ERR_FATAL
;
376 /* Create event for the specific PID */
377 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
380 if (ret
== -LTTNG_UST_ERR_EXIST
) {
381 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
384 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
390 /* Add ltt ust event to channel */
392 add_unique_ust_event(uchan
->events
, uevent
);
400 case LTTNG_DOMAIN_UST_EXEC_NAME
:
401 case LTTNG_DOMAIN_UST_PID
:
402 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
413 trace_ust_destroy_event(uevent
);
422 * Enable UST tracepoint event for a channel from a UST session.
424 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
, int domain
,
425 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
426 struct lttng_filter_bytecode
*filter
)
428 int ret
= LTTNG_OK
, to_create
= 0;
429 struct ltt_ust_event
*uevent
;
437 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
439 if (uevent
== NULL
) {
440 uevent
= trace_ust_create_event(event
, filter
);
441 if (uevent
== NULL
) {
442 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
446 /* Valid to set it after the goto error since uevent is still NULL */
450 if (uevent
->enabled
) {
451 /* It's already enabled so everything is OK */
452 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
459 case LTTNG_DOMAIN_UST
:
462 /* Create event on all UST registered apps for session */
463 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
465 /* Enable event on all UST registered apps for session */
466 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
470 if (ret
== -LTTNG_UST_ERR_EXIST
) {
471 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
474 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
481 case LTTNG_DOMAIN_UST_EXEC_NAME
:
482 case LTTNG_DOMAIN_UST_PID
:
483 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
491 /* Add ltt ust event to channel */
492 add_unique_ust_event(uchan
->events
, uevent
);
495 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
496 to_create
? "created" : "enabled", uchan
->name
);
506 * Only destroy event on creation time (not enabling time) because if the
507 * event is found in the channel (to_create == 0), it means that at some
508 * point the enable_event worked and it's thus valid to keep it alive.
509 * Destroying it also implies that we also destroy it's shadow copy to sync
513 /* In this code path, the uevent was not added to the hash table */
514 trace_ust_destroy_event(uevent
);
521 * Disable UST tracepoint of a channel from a UST session.
523 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
, int domain
,
524 struct ltt_ust_channel
*uchan
, char *event_name
)
527 struct ltt_ust_event
*uevent
;
528 struct lttng_ht_node_str
*node
;
529 struct lttng_ht_iter iter
;
541 * We use a custom lookup since we need the iterator for the next_duplicate
542 * call in the do while loop below.
544 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
545 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
546 node
= lttng_ht_iter_get_node_str(&iter
);
548 DBG2("Trace UST event NOT found by name %s", event_name
);
549 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
554 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
557 if (uevent
->enabled
== 0) {
558 /* It's already disabled so everything is OK */
564 case LTTNG_DOMAIN_UST
:
565 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
566 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
567 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
572 case LTTNG_DOMAIN_UST_EXEC_NAME
:
573 case LTTNG_DOMAIN_UST_PID
:
574 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
583 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
586 /* Get next duplicate event by name. */
587 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
588 event_name
, &iter
.iter
);
589 node
= lttng_ht_iter_get_node_str(&iter
);
600 * Disable all UST tracepoints for a channel from a UST session.
602 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
, int domain
,
603 struct ltt_ust_channel
*uchan
)
606 struct lttng_ht_iter iter
;
607 struct ltt_ust_event
*uevent
= NULL
;
608 struct lttng_event
*events
= NULL
;
616 case LTTNG_DOMAIN_UST
:
618 /* Disabling existing events */
619 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
621 if (uevent
->enabled
== 1) {
622 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
,
630 /* Get all UST available events */
631 size
= ust_app_list_events(&events
);
633 ret
= LTTNG_ERR_UST_LIST_FAIL
;
637 for (i
= 0; i
< size
; i
++) {
638 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
,
640 if (ret
!= LTTNG_OK
) {
641 /* Continue to disable the rest... */
650 case LTTNG_DOMAIN_UST_EXEC_NAME
:
651 case LTTNG_DOMAIN_UST_PID
:
652 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
: