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
;
54 case LTTNG_DOMAIN_KERNEL
:
55 chan
->attr
.subbuf_size
=
56 default_get_kernel_channel_subbuf_size();
57 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
58 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
59 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
60 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
62 case LTTNG_DOMAIN_UST
:
64 case LTTNG_DOMAIN_UST_PID
:
65 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
66 case LTTNG_DOMAIN_UST_EXEC_NAME
:
68 chan
->attr
.subbuf_size
= default_get_ust_channel_subbuf_size();
69 chan
->attr
.num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
70 chan
->attr
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
71 chan
->attr
.switch_timer_interval
= DEFAULT_UST_CHANNEL_SWITCH_TIMER
;
72 chan
->attr
.read_timer_interval
= DEFAULT_UST_CHANNEL_READ_TIMER
;
75 goto error
; /* Not implemented */
87 * Disable kernel channel of the kernel session.
89 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
93 struct ltt_kernel_channel
*kchan
;
98 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
100 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
104 /* Only if channel is enabled disable it. */
105 if (kchan
->enabled
== 1) {
106 ret
= kernel_disable_channel(kchan
);
107 if (ret
< 0 && ret
!= -EEXIST
) {
108 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
120 * Enable kernel channel of the kernel session.
122 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
123 struct ltt_kernel_channel
*kchan
)
130 if (kchan
->enabled
== 0) {
131 ret
= kernel_enable_channel(kchan
);
133 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
137 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
148 * Create kernel channel of the kernel session and notify kernel thread.
150 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
151 struct lttng_channel
*attr
, int kernel_pipe
)
154 struct lttng_channel
*defattr
= NULL
;
158 /* Creating channel attributes if needed */
160 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
);
161 if (defattr
== NULL
) {
162 ret
= LTTNG_ERR_FATAL
;
168 /* Channel not found, creating it */
169 ret
= kernel_create_channel(ksession
, attr
);
171 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
175 /* Notify kernel thread that there is a new channel */
176 ret
= notify_thread_pipe(kernel_pipe
);
178 ret
= LTTNG_ERR_FATAL
;
189 * Enable UST channel for session and domain.
191 int channel_ust_enable(struct ltt_ust_session
*usess
, int domain
,
192 struct ltt_ust_channel
*uchan
)
199 /* If already enabled, everything is OK */
200 if (uchan
->enabled
) {
201 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
202 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
207 case LTTNG_DOMAIN_UST
:
208 DBG2("Channel %s being enabled in UST global domain", uchan
->name
);
211 * Enable channel for UST global domain on all applications. Ignore
212 * return value here since whatever error we got, it means that the
213 * channel was not created on one or many registered applications and
214 * we can not report this to the user yet. However, at this stage, the
215 * channel was successfully created on the session daemon side so the
216 * enable-channel command is a success.
218 (void) ust_app_create_channel_glb(usess
, uchan
);
221 case LTTNG_DOMAIN_UST_PID
:
222 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
223 case LTTNG_DOMAIN_UST_EXEC_NAME
:
231 DBG2("Channel %s enabled successfully", uchan
->name
);
239 * Create UST channel for session and domain.
241 int channel_ust_create(struct ltt_ust_session
*usess
, int domain
,
242 struct lttng_channel
*attr
)
245 struct ltt_ust_channel
*uchan
= NULL
;
246 struct lttng_channel
*defattr
= NULL
;
250 /* Creating channel attributes if needed */
252 defattr
= channel_new_default_attr(domain
);
253 if (defattr
== NULL
) {
254 ret
= LTTNG_ERR_FATAL
;
260 if (attr
->attr
.subbuf_size
< DEFAULT_UST_CHANNEL_SUBBUF_SIZE
) {
261 ret
= LTTNG_ERR_INVALID
;
266 * Validate UST buffer size and number of buffers: must both be power of 2
267 * and nonzero. We validate right here for UST, because applications will
268 * not report the error to the user (unlike kernel tracing).
270 if (!attr
->attr
.subbuf_size
||
271 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
272 ret
= LTTNG_ERR_INVALID
;
276 if (!attr
->attr
.num_subbuf
||
277 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
278 ret
= LTTNG_ERR_INVALID
;
282 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
283 ret
= LTTNG_ERR_NOT_SUPPORTED
;
287 /* Create UST channel */
288 uchan
= trace_ust_create_channel(attr
, usess
->pathname
);
290 ret
= LTTNG_ERR_FATAL
;
296 case LTTNG_DOMAIN_UST
:
297 DBG2("Channel %s being created in UST global domain", uchan
->name
);
299 /* Enable channel for global domain */
300 ret
= ust_app_create_channel_glb(usess
, uchan
);
303 case LTTNG_DOMAIN_UST_PID
:
304 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
305 case LTTNG_DOMAIN_UST_EXEC_NAME
:
309 goto error_free_chan
;
312 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
313 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
314 goto error_free_chan
;
317 /* Adding the channel to the channel hash table. */
319 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
322 DBG2("Channel %s created successfully", uchan
->name
);
329 * No need to remove the channel from the hash table because at this point
330 * it was not added hence the direct call and no call_rcu().
332 trace_ust_destroy_channel(uchan
);
339 * Disable UST channel for session and domain.
341 int channel_ust_disable(struct ltt_ust_session
*usess
, int domain
,
342 struct ltt_ust_channel
*uchan
)
349 /* Already disabled */
350 if (uchan
->enabled
== 0) {
351 DBG2("Channel UST %s already disabled", uchan
->name
);
355 /* Get the right channel's hashtable */
357 case LTTNG_DOMAIN_UST
:
358 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
359 /* Disable channel for global domain */
360 ret
= ust_app_disable_channel_glb(usess
, uchan
);
363 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
364 case LTTNG_DOMAIN_UST_EXEC_NAME
:
365 case LTTNG_DOMAIN_UST_PID
:
372 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
373 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
379 DBG2("Channel %s disabled successfully", uchan
->name
);