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
=
58 default_get_kernel_channel_subbuf_size();
59 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
60 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
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
;
73 goto error
; /* Not implemented */
85 * Disable kernel channel of the kernel session.
87 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
91 struct ltt_kernel_channel
*kchan
;
96 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
98 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
102 /* Only if channel is enabled disable it. */
103 if (kchan
->enabled
== 1) {
104 ret
= kernel_disable_channel(kchan
);
105 if (ret
< 0 && ret
!= -EEXIST
) {
106 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
118 * Enable kernel channel of the kernel session.
120 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
121 struct ltt_kernel_channel
*kchan
)
128 if (kchan
->enabled
== 0) {
129 ret
= kernel_enable_channel(kchan
);
131 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
135 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
146 * Create kernel channel of the kernel session and notify kernel thread.
148 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
149 struct lttng_channel
*attr
, int kernel_pipe
)
152 struct lttng_channel
*defattr
= NULL
;
156 /* Creating channel attributes if needed */
158 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
);
159 if (defattr
== NULL
) {
160 ret
= LTTNG_ERR_FATAL
;
166 /* Channel not found, creating it */
167 ret
= kernel_create_channel(ksession
, attr
);
169 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
173 /* Notify kernel thread that there is a new channel */
174 ret
= notify_thread_pipe(kernel_pipe
);
176 ret
= LTTNG_ERR_FATAL
;
187 * Enable UST channel for session and domain.
189 int channel_ust_enable(struct ltt_ust_session
*usess
, int domain
,
190 struct ltt_ust_channel
*uchan
)
197 /* If already enabled, everything is OK */
198 if (uchan
->enabled
) {
199 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
200 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
205 case LTTNG_DOMAIN_UST
:
206 DBG2("Channel %s being enabled in UST global domain", uchan
->name
);
209 * Enable channel for UST global domain on all applications. Ignore
210 * return value here since whatever error we got, it means that the
211 * channel was not created on one or many registered applications and
212 * we can not report this to the user yet. However, at this stage, the
213 * channel was successfully created on the session daemon side so the
214 * enable-channel command is a success.
216 (void) ust_app_create_channel_glb(usess
, uchan
);
219 case LTTNG_DOMAIN_UST_PID
:
220 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
221 case LTTNG_DOMAIN_UST_EXEC_NAME
:
229 DBG2("Channel %s enabled successfully", uchan
->name
);
237 * Create UST channel for session and domain.
239 int channel_ust_create(struct ltt_ust_session
*usess
, int domain
,
240 struct lttng_channel
*attr
)
243 struct ltt_ust_channel
*uchan
= NULL
;
244 struct lttng_channel
*defattr
= NULL
;
248 /* Creating channel attributes if needed */
250 defattr
= channel_new_default_attr(domain
);
251 if (defattr
== NULL
) {
252 ret
= LTTNG_ERR_FATAL
;
258 if (attr
->attr
.subbuf_size
< DEFAULT_UST_CHANNEL_SUBBUF_SIZE
) {
259 ret
= LTTNG_ERR_INVALID
;
264 * Validate UST buffer size and number of buffers: must both be power of 2
265 * and nonzero. We validate right here for UST, because applications will
266 * not report the error to the user (unlike kernel tracing).
268 if (!attr
->attr
.subbuf_size
||
269 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
270 ret
= LTTNG_ERR_INVALID
;
274 if (!attr
->attr
.num_subbuf
||
275 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
276 ret
= LTTNG_ERR_INVALID
;
280 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
281 ret
= LTTNG_ERR_NOT_SUPPORTED
;
285 /* Create UST channel */
286 uchan
= trace_ust_create_channel(attr
, usess
->pathname
);
288 ret
= LTTNG_ERR_FATAL
;
294 case LTTNG_DOMAIN_UST
:
295 DBG2("Channel %s being created in UST global domain", uchan
->name
);
297 /* Enable channel for global domain */
298 ret
= ust_app_create_channel_glb(usess
, uchan
);
301 case LTTNG_DOMAIN_UST_PID
:
302 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
303 case LTTNG_DOMAIN_UST_EXEC_NAME
:
307 goto error_free_chan
;
310 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
311 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
312 goto error_free_chan
;
315 /* Adding the channel to the channel hash table. */
317 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
320 DBG2("Channel %s created successfully", uchan
->name
);
327 * No need to remove the channel from the hash table because at this point
328 * it was not added hence the direct call and no call_rcu().
330 trace_ust_destroy_channel(uchan
);
337 * Disable UST channel for session and domain.
339 int channel_ust_disable(struct ltt_ust_session
*usess
, int domain
,
340 struct ltt_ust_channel
*uchan
)
347 /* Already disabled */
348 if (uchan
->enabled
== 0) {
349 DBG2("Channel UST %s already disabled", uchan
->name
);
353 /* Get the right channel's hashtable */
355 case LTTNG_DOMAIN_UST
:
356 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
357 /* Disable channel for global domain */
358 ret
= ust_app_disable_channel_glb(usess
, uchan
);
361 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
362 case LTTNG_DOMAIN_UST_EXEC_NAME
:
363 case LTTNG_DOMAIN_UST_PID
:
370 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
371 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
377 DBG2("Channel %s disabled successfully", uchan
->name
);