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 * Setup a lttng_event used to enable *all* syscall tracing.
38 static void init_syscalls_kernel_event(struct lttng_event
*event
)
40 event
->name
[0] = '\0';
42 * We use LTTNG_EVENT* here since the trace kernel creation will make the
43 * right changes for the kernel.
45 event
->type
= LTTNG_EVENT_SYSCALL
;
49 * Return 1 if loglevels match or 0 on failure.
51 static int loglevel_match(struct ltt_ust_event
*uevent
,
52 enum lttng_ust_loglevel_type log_type
, int loglevel
)
55 * For the loglevel type ALL, the loglevel is set to -1 but the event
56 * received by the session daemon is 0 which does not match the negative
57 * value in the existing event.
59 if (log_type
== LTTNG_UST_LOGLEVEL_ALL
) {
63 if (uevent
== NULL
|| uevent
->attr
.loglevel_type
!= log_type
||
64 uevent
->attr
.loglevel
!= loglevel
) {
75 * Disable kernel tracepoint event for a channel from the kernel session.
77 int event_kernel_disable_tracepoint(struct ltt_kernel_session
*ksession
,
78 struct ltt_kernel_channel
*kchan
, char *event_name
)
81 struct ltt_kernel_event
*kevent
;
83 kevent
= trace_kernel_get_event_by_name(event_name
, kchan
);
85 ret
= LTTNG_ERR_NO_EVENT
;
89 ret
= kernel_disable_event(kevent
);
91 ret
= LTTNG_ERR_KERN_DISABLE_FAIL
;
95 DBG("Kernel event %s disable for channel %s.",
96 kevent
->event
->name
, kchan
->channel
->name
);
105 * Disable kernel tracepoint events for a channel from the kernel session.
107 int event_kernel_disable_all_tracepoints(struct ltt_kernel_session
*ksession
,
108 struct ltt_kernel_channel
*kchan
)
111 struct ltt_kernel_event
*kevent
;
113 /* For each event in the kernel session */
114 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
115 ret
= kernel_disable_event(kevent
);
117 /* We continue disabling the rest */
126 * Disable kernel syscall events for a channel from the kernel session.
128 int event_kernel_disable_all_syscalls(struct ltt_kernel_session
*ksession
,
129 struct ltt_kernel_channel
*kchan
)
131 ERR("Cannot disable syscall tracing for existing session. Please destroy session instead.");
132 return LTTNG_OK
; /* Return OK so disable all succeeds */
136 * Disable all kernel event for a channel from the kernel session.
138 int event_kernel_disable_all(struct ltt_kernel_session
*ksession
,
139 struct ltt_kernel_channel
*kchan
)
143 ret
= event_kernel_disable_all_tracepoints(ksession
, kchan
);
146 ret
= event_kernel_disable_all_syscalls(ksession
, kchan
);
151 * Enable kernel tracepoint event for a channel from the kernel session.
153 int event_kernel_enable_tracepoint(struct ltt_kernel_session
*ksession
,
154 struct ltt_kernel_channel
*kchan
, struct lttng_event
*event
)
157 struct ltt_kernel_event
*kevent
;
159 kevent
= trace_kernel_get_event_by_name(event
->name
, kchan
);
160 if (kevent
== NULL
) {
161 ret
= kernel_create_event(event
, kchan
);
165 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
168 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
171 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
176 } else if (kevent
->enabled
== 0) {
177 ret
= kernel_enable_event(kevent
);
179 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
183 /* At this point, the event is considered enabled */
184 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
194 * Enable all kernel tracepoint events of a channel of the kernel session.
196 int event_kernel_enable_all_tracepoints(struct ltt_kernel_session
*ksession
,
197 struct ltt_kernel_channel
*kchan
, int kernel_tracer_fd
)
200 struct ltt_kernel_event
*kevent
;
201 struct lttng_event
*event_list
= NULL
;
203 /* For each event in the kernel session */
204 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
205 if (kevent
->enabled
== 0) {
206 ret
= kernel_enable_event(kevent
);
208 /* Enable failed but still continue */
214 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
216 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
220 for (i
= 0; i
< size
; i
++) {
221 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
222 if (kevent
== NULL
) {
223 /* Default event type for enable all */
224 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
225 /* Enable each single tracepoint event */
226 ret
= kernel_create_event(&event_list
[i
], kchan
);
228 /* Ignore error here and continue */
241 * Enable all kernel tracepoint events of a channel of the kernel session.
243 int event_kernel_enable_all_syscalls(struct ltt_kernel_session
*ksession
,
244 struct ltt_kernel_channel
*kchan
, int kernel_tracer_fd
)
247 struct lttng_event event
;
249 init_syscalls_kernel_event(&event
);
251 DBG("Enabling all syscall tracing");
253 ret
= kernel_create_event(&event
, kchan
);
255 if (ret
== -EEXIST
) {
256 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
258 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
269 * Enable all kernel events of a channel of the kernel session.
271 int event_kernel_enable_all(struct ltt_kernel_session
*ksession
,
272 struct ltt_kernel_channel
*kchan
, int kernel_tracer_fd
)
276 tp_ret
= event_kernel_enable_all_tracepoints(ksession
, kchan
, kernel_tracer_fd
);
277 if (tp_ret
!= LTTNG_OK
) {
282 * Reaching this code path means that all tracepoints were enabled without
283 * errors so we ignore the error value of syscalls.
285 * At the moment, failing to enable syscalls on "lttng enable-event -a -k"
286 * is not considered an error that need to be returned to the client since
287 * tracepoints did not fail. Future work will allow us to send back
288 * multiple errors to the client in one API call.
290 (void) event_kernel_enable_all_syscalls(ksession
, kchan
, kernel_tracer_fd
);
297 * ============================
298 * UST : The Ultimate Frontier!
299 * ============================
303 * Enable all UST tracepoints for a channel from a UST session.
305 int event_ust_enable_all_tracepoints(struct ltt_ust_session
*usess
, int domain
,
306 struct ltt_ust_channel
*uchan
)
309 struct lttng_ht_iter iter
;
310 struct ltt_ust_event
*uevent
= NULL
;
311 struct lttng_event
*events
= NULL
;
314 case LTTNG_DOMAIN_UST
:
316 /* Enable existing events */
317 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
319 if (uevent
->enabled
== 0) {
320 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
328 /* Get all UST available events */
329 size
= ust_app_list_events(&events
);
331 ret
= LTTNG_ERR_UST_LIST_FAIL
;
335 for (i
= 0; i
< size
; i
++) {
337 * Check if event exist and if so, continue since it was enable
340 uevent
= trace_ust_find_event_by_name(uchan
->events
,
342 if (uevent
!= NULL
) {
343 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
346 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
347 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
354 /* Create ust event */
355 uevent
= trace_ust_create_event(&events
[i
]);
356 if (uevent
== NULL
) {
357 ret
= LTTNG_ERR_FATAL
;
361 /* Create event for the specific PID */
362 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
365 if (ret
== -LTTNG_UST_ERR_EXIST
) {
366 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
369 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
375 /* Add ltt ust event to channel */
377 lttng_ht_add_unique_str(uchan
->events
, &uevent
->node
);
385 case LTTNG_DOMAIN_UST_EXEC_NAME
:
386 case LTTNG_DOMAIN_UST_PID
:
387 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
397 trace_ust_destroy_event(uevent
);
405 * Enable UST tracepoint event for a channel from a UST session.
407 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
, int domain
,
408 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
)
410 int ret
= LTTNG_OK
, to_create
= 0;
411 struct ltt_ust_event
*uevent
;
413 uevent
= trace_ust_find_event_by_name(uchan
->events
, event
->name
);
414 if (uevent
== NULL
) {
415 uevent
= trace_ust_create_event(event
);
416 if (uevent
== NULL
) {
417 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
420 /* Valid to set it after the goto error since uevent is still NULL */
424 /* Check loglevels */
425 ret
= loglevel_match(uevent
, event
->loglevel_type
, event
->loglevel
);
428 * No match meaning that the user tried to enable a known event but
429 * with a different loglevel.
431 DBG("Enable event %s does not match existing event %s with loglevel "
432 "respectively of %d and %d", event
->name
, uevent
->attr
.name
,
433 uevent
->attr
.loglevel
, event
->loglevel
);
434 ret
= LTTNG_ERR_EVENT_EXIST_LOGLEVEL
;
438 if (uevent
->enabled
) {
439 /* It's already enabled so everything is OK */
447 case LTTNG_DOMAIN_UST
:
450 /* Create event on all UST registered apps for session */
451 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
453 /* Enable event on all UST registered apps for session */
454 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
458 if (ret
== -LTTNG_UST_ERR_EXIST
) {
459 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
462 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
469 case LTTNG_DOMAIN_UST_EXEC_NAME
:
470 case LTTNG_DOMAIN_UST_PID
:
471 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
480 /* Add ltt ust event to channel */
481 lttng_ht_add_unique_str(uchan
->events
, &uevent
->node
);
485 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
486 to_create
? "created" : "enabled", uchan
->name
);
495 * Only destroy event on creation time (not enabling time) because if the
496 * event is found in the channel (to_create == 0), it means that at some
497 * point the enable_event worked and it's thus valid to keep it alive.
498 * Destroying it also implies that we also destroy it's shadow copy to sync
502 /* In this code path, the uevent was not added to the hash table */
503 trace_ust_destroy_event(uevent
);
509 * Disable UST tracepoint of a channel from a UST session.
511 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
, int domain
,
512 struct ltt_ust_channel
*uchan
, char *event_name
)
515 struct ltt_ust_event
*uevent
;
517 uevent
= trace_ust_find_event_by_name(uchan
->events
, event_name
);
518 if (uevent
== NULL
) {
519 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
523 if (uevent
->enabled
== 0) {
524 /* It's already enabled so everything is OK */
530 case LTTNG_DOMAIN_UST
:
531 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
532 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
533 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
538 case LTTNG_DOMAIN_UST_EXEC_NAME
:
539 case LTTNG_DOMAIN_UST_PID
:
540 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
551 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
559 * Disable all UST tracepoints for a channel from a UST session.
561 int event_ust_disable_all_tracepoints(struct ltt_ust_session
*usess
, int domain
,
562 struct ltt_ust_channel
*uchan
)
565 struct lttng_ht_iter iter
;
566 struct ltt_ust_event
*uevent
= NULL
;
567 struct lttng_event
*events
= NULL
;
570 case LTTNG_DOMAIN_UST
:
572 /* Disabling existing events */
573 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
575 if (uevent
->enabled
== 1) {
576 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
584 /* Get all UST available events */
585 size
= ust_app_list_events(&events
);
587 ret
= LTTNG_ERR_UST_LIST_FAIL
;
591 for (i
= 0; i
< size
; i
++) {
592 uevent
= trace_ust_find_event_by_name(uchan
->events
,
594 if (uevent
!= NULL
&& uevent
->enabled
== 1) {
595 ret
= ust_app_disable_event_pid(usess
, uchan
, uevent
,
597 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
598 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
610 case LTTNG_DOMAIN_UST_EXEC_NAME
:
611 case LTTNG_DOMAIN_UST_PID
:
612 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
: