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.
21 #include <urcu/list.h>
24 #include <lttng/lttng.h>
25 #include <common/error.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
31 #include "lttng-sessiond.h"
34 #include "trace-kernel.h"
35 #include "trace-ust.h"
39 * Add unique UST event based on the event name, filter bytecode and loglevel.
41 static void add_unique_ust_event(struct lttng_ht
*ht
,
42 struct ltt_ust_event
*event
)
44 struct cds_lfht_node
*node_ptr
;
45 struct ltt_ust_ht_key key
;
51 key
.name
= event
->attr
.name
;
52 key
.filter
= (struct lttng_filter_bytecode
*) event
->filter
;
53 key
.loglevel_type
= event
->attr
.loglevel_type
;
54 key
.loglevel_value
= event
->attr
.loglevel
;
55 key
.exclusion
= event
->exclusion
;
57 node_ptr
= cds_lfht_add_unique(ht
->ht
,
58 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
59 trace_ust_ht_match_event
, &key
, &event
->node
.node
);
60 assert(node_ptr
== &event
->node
.node
);
64 * Disable kernel tracepoint event for a channel from the kernel session.
66 int event_kernel_disable_event(struct ltt_kernel_channel
*kchan
,
70 struct ltt_kernel_event
*kevent
;
74 kevent
= trace_kernel_get_event_by_name(event_name
, kchan
,
77 ret
= LTTNG_ERR_NO_EVENT
;
81 ret
= kernel_disable_event(kevent
);
83 ret
= LTTNG_ERR_KERN_DISABLE_FAIL
;
87 DBG("Kernel event %s disable for channel %s.",
88 kevent
->event
->name
, kchan
->channel
->name
);
97 * Disable kernel tracepoint events for a channel from the kernel session.
99 int event_kernel_disable_event_type(struct ltt_kernel_channel
*kchan
,
100 enum lttng_event_type type
)
103 struct ltt_kernel_event
*kevent
;
107 /* For each event in the kernel session */
108 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
109 if (type
!= LTTNG_EVENT_ALL
&& kevent
->type
!= type
)
111 ret
= kernel_disable_event(kevent
);
113 /* We continue disabling the rest */
122 * Disable all kernel event for a channel from the kernel session.
124 int event_kernel_disable_event_all(struct ltt_kernel_channel
*kchan
)
126 return event_kernel_disable_event_type(kchan
, LTTNG_EVENT_ALL
);
130 * Enable kernel tracepoint event for a channel from the kernel session.
131 * We own filter_expression and filter.
133 int event_kernel_enable_event(struct ltt_kernel_channel
*kchan
,
134 struct lttng_event
*event
, char *filter_expression
,
135 struct lttng_filter_bytecode
*filter
)
138 struct ltt_kernel_event
*kevent
;
143 kevent
= trace_kernel_find_event(event
->name
, kchan
,
144 event
->type
, filter
);
145 if (kevent
== NULL
) {
146 ret
= kernel_create_event(event
, kchan
,
147 filter_expression
, filter
);
148 /* We have passed ownership */
149 filter_expression
= NULL
;
154 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
157 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
160 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
165 } else if (kevent
->enabled
== 0) {
166 ret
= kernel_enable_event(kevent
);
168 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
172 /* At this point, the event is considered enabled */
173 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
179 free(filter_expression
);
185 * ============================
186 * UST : The Ultimate Frontier!
187 * ============================
191 * Enable UST tracepoint event for a channel from a UST session.
192 * We own filter_expression, filter, and exclusion.
194 int event_ust_enable_tracepoint(struct ltt_ust_session
*usess
,
195 struct ltt_ust_channel
*uchan
, struct lttng_event
*event
,
196 char *filter_expression
,
197 struct lttng_filter_bytecode
*filter
,
198 struct lttng_event_exclusion
*exclusion
,
201 int ret
= LTTNG_OK
, to_create
= 0;
202 struct ltt_ust_event
*uevent
;
210 uevent
= trace_ust_find_event(uchan
->events
, event
->name
, filter
,
211 event
->loglevel_type
, event
->loglevel
, exclusion
);
213 uevent
= trace_ust_create_event(event
, filter_expression
,
214 filter
, exclusion
, internal_event
);
215 /* We have passed ownership */
216 filter_expression
= NULL
;
219 if (uevent
== NULL
) {
220 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
224 /* Valid to set it after the goto error since uevent is still NULL */
228 if (uevent
->enabled
) {
229 /* It's already enabled so everything is OK */
230 ret
= LTTNG_ERR_UST_EVENT_ENABLED
;
237 /* Create event on all UST registered apps for session */
238 ret
= ust_app_create_event_glb(usess
, uchan
, uevent
);
240 /* Enable event on all UST registered apps for session */
241 ret
= ust_app_enable_event_glb(usess
, uchan
, uevent
);
245 if (ret
== -LTTNG_UST_ERR_EXIST
) {
246 ret
= LTTNG_ERR_UST_EVENT_EXIST
;
249 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
255 /* Add ltt ust event to channel */
256 add_unique_ust_event(uchan
->events
, uevent
);
259 DBG("Event UST %s %s in channel %s", uevent
->attr
.name
,
260 to_create
? "created" : "enabled", uchan
->name
);
266 free(filter_expression
);
273 * Only destroy event on creation time (not enabling time) because if the
274 * event is found in the channel (to_create == 0), it means that at some
275 * point the enable_event worked and it's thus valid to keep it alive.
276 * Destroying it also implies that we also destroy it's shadow copy to sync
280 /* In this code path, the uevent was not added to the hash table */
281 trace_ust_destroy_event(uevent
);
284 free(filter_expression
);
291 * Disable UST tracepoint of a channel from a UST session.
293 int event_ust_disable_tracepoint(struct ltt_ust_session
*usess
,
294 struct ltt_ust_channel
*uchan
, char *event_name
)
297 struct ltt_ust_event
*uevent
;
298 struct lttng_ht_node_str
*node
;
299 struct lttng_ht_iter iter
;
311 * We use a custom lookup since we need the iterator for the next_duplicate
312 * call in the do while loop below.
314 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) event_name
, lttng_ht_seed
),
315 trace_ust_ht_match_event_by_name
, event_name
, &iter
.iter
);
316 node
= lttng_ht_iter_get_node_str(&iter
);
318 DBG2("Trace UST event NOT found by name %s", event_name
);
319 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
324 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
327 if (uevent
->enabled
== 0) {
328 /* It's already disabled so everything is OK */
332 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
333 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
334 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
339 DBG2("Event UST %s disabled in channel %s", uevent
->attr
.name
,
343 /* Get next duplicate event by name. */
344 cds_lfht_next_duplicate(ht
->ht
, trace_ust_ht_match_event_by_name
,
345 event_name
, &iter
.iter
);
346 node
= lttng_ht_iter_get_node_str(&iter
);
357 * Enable all agent event for a given UST session.
359 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
361 int event_agent_enable_all(struct ltt_ust_session
*usess
,
362 struct agent
*agt
, struct lttng_event
*event
,
363 struct lttng_filter_bytecode
*filter
,char *filter_expression
)
366 struct agent_event
*aevent
;
367 struct lttng_ht_iter iter
;
371 DBG("Event agent enabling ALL events for session %" PRIu64
, usess
->id
);
373 /* Enable event on agent application through TCP socket. */
374 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
375 if (ret
!= LTTNG_OK
) {
379 /* Flag every event that they are now enabled. */
381 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
394 * Enable a single agent event for a given UST session.
396 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
398 int event_agent_enable(struct ltt_ust_session
*usess
,
399 struct agent
*agt
, struct lttng_event
*event
,
400 struct lttng_filter_bytecode
*filter
,
401 char *filter_expression
)
403 int ret
, created
= 0;
404 struct agent_event
*aevent
;
410 DBG("Event agent enabling %s for session %" PRIu64
" with loglevel type %d "
411 ", loglevel %d and filter \"%s\"", event
->name
,
412 usess
->id
, event
->loglevel_type
, event
->loglevel
,
413 filter_expression
? filter_expression
: "NULL");
415 aevent
= agent_find_event(event
->name
, event
->loglevel_type
,
416 event
->loglevel
, agt
);
418 aevent
= agent_create_event(event
->name
, event
->loglevel_type
,
419 event
->loglevel
, filter
,
422 ret
= LTTNG_ERR_NOMEM
;
428 /* Already enabled? */
429 if (aevent
->enabled
) {
433 ret
= agent_enable_event(aevent
, agt
->domain
);
434 if (ret
!= LTTNG_OK
) {
438 /* If the event was created prior to the enable, add it to the domain. */
440 agent_add_event(aevent
, agt
);
448 agent_destroy_event(aevent
);
454 * Return the default event name associated with the provided UST domain. Return
457 const char *event_get_default_agent_ust_name(enum lttng_domain_type domain
)
459 const char *default_event_name
= NULL
;
462 case LTTNG_DOMAIN_LOG4J
:
463 default_event_name
= DEFAULT_LOG4J_EVENT_NAME
;
465 case LTTNG_DOMAIN_JUL
:
466 default_event_name
= DEFAULT_JUL_EVENT_NAME
;
468 case LTTNG_DOMAIN_PYTHON
:
469 default_event_name
= DEFAULT_PYTHON_EVENT_NAME
;
475 return default_event_name
;
479 * Disable a given agent event for a given UST session.
481 * Must be called with the RCU read lock held.
482 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
484 static int event_agent_disable_one(struct ltt_ust_session
*usess
,
485 struct agent
*agt
, struct agent_event
*aevent
)
488 struct ltt_ust_event
*uevent
= NULL
;
489 struct ltt_ust_channel
*uchan
= NULL
;
490 const char *ust_event_name
, *ust_channel_name
;
496 DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64
,
497 aevent
->name
, aevent
->loglevel_type
, aevent
->loglevel_value
,
500 /* Already disabled? */
501 if (!aevent
->enabled
) {
505 if (agt
->domain
== LTTNG_DOMAIN_JUL
) {
506 ust_channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
507 } else if (agt
->domain
== LTTNG_DOMAIN_LOG4J
) {
508 ust_channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
509 } else if (agt
->domain
== LTTNG_DOMAIN_PYTHON
) {
510 ust_channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
512 ret
= LTTNG_ERR_INVALID
;
517 * Disable it on the UST side. First get the channel reference then find
518 * the event and finally disable it.
520 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
521 (char *) ust_channel_name
);
523 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
527 ust_event_name
= event_get_default_agent_ust_name(agt
->domain
);
528 if (!ust_event_name
) {
529 ret
= LTTNG_ERR_FATAL
;
534 * Agent UST event has its loglevel type forced to
535 * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
536 * happens thanks to an UST filter. The following -1 is actually
537 * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
539 uevent
= trace_ust_find_event(uchan
->events
, (char *) ust_event_name
,
540 aevent
->filter
, LTTNG_UST_LOGLEVEL_ALL
, -1, NULL
);
541 /* If the agent event exists, it must be available on the UST side. */
544 ret
= ust_app_disable_event_glb(usess
, uchan
, uevent
);
545 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
546 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
551 * Flag event that it's disabled so the shadow copy on the ust app side
552 * will disable it if an application shows up.
556 ret
= agent_disable_event(aevent
, agt
->domain
);
557 if (ret
!= LTTNG_OK
) {
569 * Disable all agent events matching a given name for a given UST session.
571 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
573 int event_agent_disable(struct ltt_ust_session
*usess
, struct agent
*agt
,
577 struct agent_event
*aevent
;
578 struct lttng_ht_iter iter
;
579 struct lttng_ht_node_str
*node
;
585 DBG("Event agent disabling %s (all loglevels) for session %" PRIu64
, event_name
, usess
->id
);
588 agent_find_events_by_name(event_name
, agt
, &iter
);
589 node
= lttng_ht_iter_get_node_str(&iter
);
592 DBG2("Event agent NOT found by name %s", event_name
);
593 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
598 aevent
= caa_container_of(node
, struct agent_event
, node
);
599 ret
= event_agent_disable_one(usess
, agt
, aevent
);
601 if (ret
!= LTTNG_OK
) {
605 /* Get next duplicate agent event by name. */
606 agent_event_next_duplicate(event_name
, agt
, &iter
);
607 node
= lttng_ht_iter_get_node_str(&iter
);
614 * Disable all agent event for a given UST session.
616 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
618 int event_agent_disable_all(struct ltt_ust_session
*usess
,
622 struct agent_event
*aevent
;
623 struct lttng_ht_iter iter
;
629 * Disable event on agent application. Continue to disable all other events
630 * if the * event is not found.
632 ret
= event_agent_disable(usess
, agt
, "*");
633 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_NOT_FOUND
) {
637 /* Disable every event. */
639 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, aevent
,
641 if (!aevent
->enabled
) {
645 ret
= event_agent_disable(usess
, agt
, aevent
->name
);
646 if (ret
!= LTTNG_OK
) {