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.
22 #include <common/common.h>
23 #include <common/defaults.h>
24 #include <common/sessiond-comm/sessiond-comm.h>
33 * Return allocated channel attributes.
35 struct lttng_channel
*channel_new_default_attr(int dom
)
37 struct lttng_channel
*chan
;
39 chan
= zmalloc(sizeof(struct lttng_channel
));
41 PERROR("zmalloc channel init");
45 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s",
46 DEFAULT_CHANNEL_NAME
) < 0) {
47 PERROR("snprintf default channel name");
51 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
52 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
53 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
56 case LTTNG_DOMAIN_KERNEL
:
57 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
58 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
59 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
61 case LTTNG_DOMAIN_UST
:
63 case LTTNG_DOMAIN_UST_PID
:
64 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
65 case LTTNG_DOMAIN_UST_EXEC_NAME
:
67 chan
->attr
.subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
68 chan
->attr
.num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
69 chan
->attr
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
72 goto error
; /* Not implemented */
84 * Disable kernel channel of the kernel session.
86 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
90 struct ltt_kernel_channel
*kchan
;
92 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
94 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
96 } else if (kchan
->enabled
== 1) {
97 ret
= kernel_disable_channel(kchan
);
98 if (ret
< 0 && ret
!= -EEXIST
) {
99 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
111 * Enable kernel channel of the kernel session.
113 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
114 struct ltt_kernel_channel
*kchan
)
118 if (kchan
->enabled
== 0) {
119 ret
= kernel_enable_channel(kchan
);
121 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
125 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
136 * Create kernel channel of the kernel session and notify kernel thread.
138 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
139 struct lttng_channel
*attr
, int kernel_pipe
)
142 struct lttng_channel
*defattr
= NULL
;
144 /* Creating channel attributes if needed */
146 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
);
147 if (defattr
== NULL
) {
148 ret
= LTTNG_ERR_FATAL
;
154 /* Channel not found, creating it */
155 ret
= kernel_create_channel(ksession
, attr
, ksession
->trace_path
);
157 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
161 /* Notify kernel thread that there is a new channel */
162 ret
= notify_thread_pipe(kernel_pipe
);
164 ret
= LTTNG_ERR_FATAL
;
175 * Enable UST channel for session and domain.
177 int channel_ust_enable(struct ltt_ust_session
*usess
, int domain
,
178 struct ltt_ust_channel
*uchan
)
182 /* If already enabled, everything is OK */
183 if (uchan
->enabled
) {
184 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
185 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
190 case LTTNG_DOMAIN_UST
:
191 DBG2("Channel %s being enabled in UST global domain", uchan
->name
);
192 /* Enable channel for global domain */
193 ret
= ust_app_enable_channel_glb(usess
, uchan
);
196 case LTTNG_DOMAIN_UST_PID
:
197 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
198 case LTTNG_DOMAIN_UST_EXEC_NAME
:
206 if (ret
!= -EEXIST
) {
207 ret
= LTTNG_ERR_UST_CHAN_ENABLE_FAIL
;
215 DBG2("Channel %s enabled successfully", uchan
->name
);
223 * Create UST channel for session and domain.
225 int channel_ust_create(struct ltt_ust_session
*usess
, int domain
,
226 struct lttng_channel
*attr
)
229 struct ltt_ust_channel
*uchan
= NULL
;
230 struct lttng_channel
*defattr
= NULL
;
232 /* Creating channel attributes if needed */
234 defattr
= channel_new_default_attr(domain
);
235 if (defattr
== NULL
) {
236 ret
= LTTNG_ERR_FATAL
;
243 * Validate UST buffer size and number of buffers: must both be
244 * power of 2 and nonzero. We validate right here for UST,
245 * because applications will not report the error to the user
246 * (unlike kernel tracing).
248 if (!attr
->attr
.subbuf_size
|| (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
249 ret
= LTTNG_ERR_INVALID
;
252 if (!attr
->attr
.num_subbuf
|| (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
253 ret
= LTTNG_ERR_INVALID
;
257 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
258 ret
= LTTNG_ERR_NOT_SUPPORTED
;
262 /* Create UST channel */
263 uchan
= trace_ust_create_channel(attr
, usess
->pathname
);
265 ret
= LTTNG_ERR_FATAL
;
271 case LTTNG_DOMAIN_UST
:
272 DBG2("Channel %s being created in UST global domain", uchan
->name
);
274 /* Enable channel for global domain */
275 ret
= ust_app_create_channel_glb(usess
, uchan
);
278 case LTTNG_DOMAIN_UST_PID
:
279 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
280 case LTTNG_DOMAIN_UST_EXEC_NAME
:
284 goto error_free_chan
;
287 if (ret
< 0 && ret
!= -EEXIST
) {
288 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
289 goto error_free_chan
;
292 /* Adding the channel to the channel hash table. */
294 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
297 DBG2("Channel %s created successfully", uchan
->name
);
304 * No need to remove the channel from the hash table because at this point
305 * it was not added hence the direct call and no call_rcu().
307 trace_ust_destroy_channel(uchan
);
314 * Disable UST channel for session and domain.
316 int channel_ust_disable(struct ltt_ust_session
*usess
, int domain
,
317 struct ltt_ust_channel
*uchan
)
321 /* Already disabled */
322 if (uchan
->enabled
== 0) {
323 DBG2("Channel UST %s already disabled", uchan
->name
);
327 /* Get the right channel's hashtable */
329 case LTTNG_DOMAIN_UST
:
330 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
331 /* Disable channel for global domain */
332 ret
= ust_app_disable_channel_glb(usess
, uchan
);
335 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
336 case LTTNG_DOMAIN_UST_EXEC_NAME
:
337 case LTTNG_DOMAIN_UST_PID
:
344 if (ret
< 0 && ret
!= -EEXIST
) {
345 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
351 DBG2("Channel %s disabled successfully", uchan
->name
);