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"
35 static void add_unique_ust_event(struct lttng_ht
*ht
,
36 struct ltt_ust_event
*event
)
38 struct cds_lfht_node
*node_ptr
;
39 struct ltt_ust_ht_key key
;
45 key
.name
= event
->attr
.name
;
46 key
.filter
= (struct lttng_filter_bytecode
*) event
->filter
;
47 key
.loglevel
= event
->attr
.loglevel
;
49 node_ptr
= cds_lfht_add_unique(ht
->ht
,
50 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
51 trace_ust_ht_match_event
, &key
, &event
->node
.node
);
52 assert(node_ptr
== &event
->node
.node
);
56 * Setup a lttng_event used to enable *all* syscall tracing.
58 static void init_syscalls_kernel_event(struct lttng_event
*event
)
60 event
->name
[0] = '\0';
62 * We use LTTNG_EVENT* here since the trace kernel creation will make the
63 * right changes for the kernel.
65 event
->type
= LTTNG_EVENT_SYSCALL
;
69 * Return 1 if loglevels match or 0 on failure.
71 static int loglevel_match(struct ltt_ust_event
*uevent
,
72 enum lttng_ust_loglevel_type log_type
, int loglevel
)
75 * For the loglevel type ALL, the loglevel is set to -1 but the event
76 * received by the session daemon is 0 which does not match the negative
77 * value in the existing event.
79 if (log_type
== LTTNG_UST_LOGLEVEL_ALL
) {
83 if (uevent
== NULL
|| uevent
->attr
.loglevel_type
!= log_type
||
84 uevent
->attr
.loglevel
!= loglevel
) {
95 * Disable kernel tracepoint event for a channel from the kernel session.
97 int event_kernel_disable_tracepoint(struct ltt_kernel_session
*ksession
,
98 struct ltt_kernel_channel
*kchan
, char *event_name
)
101 struct ltt_kernel_event
*kevent
;
103 kevent
= trace_kernel_get_event_by_name(event_name
, kchan
);
104 if (kevent
== NULL
) {
105 ret
= LTTNG_ERR_NO_EVENT
;
109 ret
= kernel_disable_event(kevent
);
111 ret
= LTTNG_ERR_KERN_DISABLE_FAIL
;
115 DBG("Kernel event %s disable for channel %s.",
116 kevent
->event
->name
, kchan
->channel
->name
);
125 * Disable kernel tracepoint events for a channel from the kernel session.
127 int event_kernel_disable_all_tracepoints(struct ltt_kernel_session
*ksession
,
128 struct ltt_kernel_channel
*kchan
)
131 struct ltt_kernel_event
*kevent
;
133 /* For each event in the kernel session */
134 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
135 ret
= kernel_disable_event(kevent
);
137 /* We continue disabling the rest */
146 * Disable kernel syscall events for a channel from the kernel session.
148 int event_kernel_disable_all_syscalls(struct ltt_kernel_session
*ksession
,
149 struct ltt_kernel_channel
*kchan
)
151 ERR("Cannot disable syscall tracing for existing session. Please destroy session instead.");
152 return LTTNG_OK
; /* Return OK so disable all succeeds */
156 * Disable all kernel event for a channel from the kernel session.
158 int event_kernel_disable_all(struct ltt_kernel_session
*ksession
,
159 struct ltt_kernel_channel
*kchan
)
163 ret
= event_kernel_disable_all_tracepoints(ksession
, kchan
);
166 ret
= event_kernel_disable_all_syscalls(ksession
, kchan
);
171 * Enable kernel tracepoint event for a channel from the kernel session.
173 int event_kernel_enable_tracepoint(struct ltt_kernel_session
*ksession
,
174 struct ltt_kernel_channel
*kchan
, struct lttng_event
*event
)
177 struct ltt_kernel_event
*kevent
;
179 kevent
= trace_kernel_get_event_by_name(event
->name
, kchan
);
180 if (kevent
== NULL
) {
181 ret
= kernel_create_event(event
, kchan
);
185 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
188 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
191 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
196 } else if (kevent
->enabled
== 0) {
197 ret
= kernel_enable_event(kevent
);
199 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
203 /* At this point, the event is considered enabled */
204 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
214 * Enable all kernel tracepoint events of a channel of the kernel session.
216 int event_kernel_enable_all_tracepoints(struct ltt_kernel_session
*ksession
,
217 struct ltt_kernel_channel
*kchan
, int kernel_tracer_fd
)
220 struct ltt_kernel_event
*kevent
;
221 struct lttng_event
*event_list
= NULL
;
223 /* For each event in the kernel session */
224 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
225 if (kevent
->enabled
== 0) {
226 ret
= kernel_enable_event(kevent
);
228 /* Enable failed but still continue */
234 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
236 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
240 for (i
= 0; i
< size
; i
++) {
241 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
242 if (kevent
== NULL
) {
243 /* Default event type for enable all */
244 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
245 /* Enable each single tracepoint event */
246 ret
= kernel_create_event(&event_list
[i
], kchan
);
248 /* Ignore error here and continue */
261 * Enable all kernel tracepoint events of a channel of the kernel session.
263 int event_kernel_enable_all_syscalls(struct ltt_kernel_session
*ksession
,
264 struct ltt_kernel_channel
*kchan
, int kernel_tracer_fd
)
267 struct lttng_event event
;
269 init_syscalls_kernel_event(&event
);
271 DBG("Enabling all syscall tracing");
273 ret
= kernel_create_event(&event
, kchan
);
275 if (ret
== -EEXIST
) {
276 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
278 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
289 * Enable all kernel events of a channel of the kernel session.
291 int event_kernel_enable_all(struct ltt_kernel_session
*ksession
,
292 struct ltt_kernel_channel
*kchan
, int kernel_tracer_fd
)
296 tp_ret
= event_kernel_enable_all_tracepoints(ksession
, kchan
, kernel_tracer_fd
);
297 if (tp_ret
!= LTTNG_OK
) {
302 * Reaching this code path means that all tracepoints were enabled without
303 * errors so we ignore the error value of syscalls.
305 * At the moment, failing to enable syscalls on "lttng enable-event -a -k"
306 * is not considered an error that need to be returned to the client since
307 * tracepoints did not fail. Future work will allow us to send back
308 * multiple errors to the client in one API call.
310 (void) event_kernel_enable_all_syscalls(ksession
, kchan
, kernel_tracer_fd
);
317 * ============================
318 * UST : The Ultimate Frontier!
319 * ============================
323 * Enable all UST tracepoints for a channel from a UST session.
325 int event_ust_enable_all_tracepoints(struct ltt_ust_session
*usess
, int domain
,
326 struct ltt_ust_channel
*uchan
)
329 struct lttng_ht_iter iter
;
330 struct ltt_ust_event
*uevent
= NULL
;
331 struct lttng_event
*events
= NULL
;
334 case LTTNG_DOMAIN_UST
:
336 /* Enable existing events */
337 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
339 if (uevent
->enabled
== 0) {
340 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
348 /* Get all UST available events */
349 size
= ust_app_list_events(&events
);
351 ret
= LTTNG_ERR_UST_LIST_FAIL
;
355 for (i
= 0; i
< size
; i
++) {
357 * Check if event exist and if so, continue since it was enable
360 uevent
= trace_ust_find_event(uchan
->events
, events
[i
].name
, NULL
,
362 if (uevent
!= NULL
) {
363 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
366 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
367 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
374 /* Create ust event */
375 uevent
= trace_ust_create_event(&events
[i
]);
376 if (uevent
== NULL
) {
377 ret
= LTTNG_ERR_FATAL
;
381 /* Create event for the specific PID */
382 ret
= ust_app_enable_event_pid(usess
, uchan
, uevent
,
385 if (ret
== -LTTNG_UST_ERR_EXIST
) {
386 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
389 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
395 /* Add ltt ust event to channel */
397 add_unique_ust_event(uchan
->events
, uevent
);
405 case LTTNG_DOMAIN_UST_EXEC_NAME
:
406 case LTTNG_DOMAIN_UST_PID
:
407 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
417 trace_ust_destroy_event(uevent
);
425 * Enable UST tracepoint event for a channel from a UST session.
427 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
, int domain
,
428 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
)
430 int ret
= LTTNG_OK
, to_create
= 0;
431 struct ltt_ust_event
*uevent
;
433 DBG3("Enable ust: %s l:%d f:%p", event
->name
, event
->loglevel
, NULL
);
437 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, NULL
,
439 if (uevent
== NULL
) {
440 uevent
= trace_ust_create_event(event
);
441 if (uevent
== NULL
) {
442 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
445 /* Valid to set it after the goto error since uevent is still NULL */
449 /* Check loglevels */
450 ret
= loglevel_match(uevent
, event
->loglevel_type
, event
->loglevel
);
453 * No match meaning that the user tried to enable a known event but
454 * with a different loglevel.
456 DBG("Enable event %s does not match existing event %s with loglevel "
457 "respectively of %d and %d", event
->name
, uevent
->attr
.name
,
458 uevent
->attr
.loglevel
, event
->loglevel
);
459 ret
= LTTNG_ERR_EVENT_EXIST_LOGLEVEL
;
463 if (uevent
->enabled
) {
464 /* It's already enabled so everything is OK */
472 case LTTNG_DOMAIN_UST
:
475 /* Create event on all UST registered apps for session */
476 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
478 /* Enable event on all UST registered apps for session */
479 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
483 if (ret
== -LTTNG_UST_ERR_EXIST
) {
484 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
487 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
494 case LTTNG_DOMAIN_UST_EXEC_NAME
:
495 case LTTNG_DOMAIN_UST_PID
:
496 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
504 /* Add ltt ust event to channel */
505 add_unique_ust_event(uchan
->events
, uevent
);
508 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
509 to_create
? "created" : "enabled", uchan
->name
);
519 * Only destroy event on creation time (not enabling time) because if the
520 * event is found in the channel (to_create == 0), it means that at some
521 * point the enable_event worked and it's thus valid to keep it alive.
522 * Destroying it also implies that we also destroy it's shadow copy to sync
526 /* In this code path, the uevent was not added to the hash table */
527 trace_ust_destroy_event(uevent
);
534 * Disable UST tracepoint of a channel from a UST session.
536 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
, int domain
,
537 struct ltt_ust_channel
*uchan
, char *event_name
)
540 struct ltt_ust_event
*uevent
;
541 struct lttng_ht_node_str
*node
;
542 struct lttng_ht_iter iter
;
543 void *orig_match_fct
;
548 /* Save match function so we can use the event by name match. */
549 orig_match_fct
= (void *) ht
->match_fct
;
550 ht
->match_fct
= trace_ust_ht_match_event_by_name
;
553 lttng_ht_lookup(ht
, (void *) event_name
, &iter
);
554 node
= lttng_ht_iter_get_node_str(&iter
);
556 DBG2("Trace UST event NOT found by name %s", event_name
);
557 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
562 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 /* Get next duplicate event by name. */
590 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
591 event_name
, &iter
.iter
);
592 node
= lttng_ht_iter_get_node_str(&iter
);
597 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
600 ht
->match_fct
= orig_match_fct
;
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
;
617 case LTTNG_DOMAIN_UST
:
619 /* Disabling existing events */
620 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
,
622 if (uevent
->enabled
== 1) {
623 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
631 /* Get all UST available events */
632 size
= ust_app_list_events(&events
);
634 ret
= LTTNG_ERR_UST_LIST_FAIL
;
638 for (i
= 0; i
< size
; i
++) {
639 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
,
641 if (ret
!= LTTNG_OK
) {
642 /* Continue to disable the rest... */
651 case LTTNG_DOMAIN_UST_EXEC_NAME
:
652 case LTTNG_DOMAIN_UST_PID
:
653 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
: