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
;
93 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
95 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
97 } else if (kchan
->enabled
== 1) {
98 ret
= kernel_disable_channel(kchan
);
99 if (ret
< 0 && ret
!= -EEXIST
) {
100 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
112 * Enable kernel channel of the kernel session.
114 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
115 struct ltt_kernel_channel
*kchan
)
119 if (kchan
->enabled
== 0) {
120 ret
= kernel_enable_channel(kchan
);
122 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
126 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
137 * Create kernel channel of the kernel session and notify kernel thread.
139 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
140 struct lttng_channel
*attr
, int kernel_pipe
)
143 struct lttng_channel
*defattr
= NULL
;
145 /* Creating channel attributes if needed */
147 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
);
148 if (defattr
== NULL
) {
149 ret
= LTTNG_ERR_FATAL
;
155 /* Channel not found, creating it */
156 ret
= kernel_create_channel(ksession
, attr
, ksession
->trace_path
);
158 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
162 /* Notify kernel thread that there is a new channel */
163 ret
= notify_thread_pipe(kernel_pipe
);
165 ret
= LTTNG_ERR_FATAL
;
176 * Enable UST channel for session and domain.
178 int channel_ust_enable(struct ltt_ust_session
*usess
, int domain
,
179 struct ltt_ust_channel
*uchan
)
183 /* If already enabled, everything is OK */
184 if (uchan
->enabled
) {
185 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
186 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
191 case LTTNG_DOMAIN_UST
:
192 DBG2("Channel %s being enabled in UST global domain", uchan
->name
);
193 /* Enable channel for global domain */
194 ret
= ust_app_enable_channel_glb(usess
, uchan
);
197 case LTTNG_DOMAIN_UST_PID
:
198 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
199 case LTTNG_DOMAIN_UST_EXEC_NAME
:
207 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
208 ret
= LTTNG_ERR_UST_CHAN_ENABLE_FAIL
;
216 DBG2("Channel %s enabled successfully", uchan
->name
);
224 * Create UST channel for session and domain.
226 int channel_ust_create(struct ltt_ust_session
*usess
, int domain
,
227 struct lttng_channel
*attr
)
230 struct ltt_ust_channel
*uchan
= NULL
;
231 struct lttng_channel
*defattr
= NULL
;
233 /* Creating channel attributes if needed */
235 defattr
= channel_new_default_attr(domain
);
236 if (defattr
== NULL
) {
237 ret
= LTTNG_ERR_FATAL
;
243 if (attr
->attr
.subbuf_size
< DEFAULT_UST_CHANNEL_SUBBUF_SIZE
) {
244 ret
= LTTNG_ERR_INVALID
;
249 * Validate UST buffer size and number of buffers: must both be
250 * power of 2 and nonzero. We validate right here for UST,
251 * because applications will not report the error to the user
252 * (unlike kernel tracing).
254 if (!attr
->attr
.subbuf_size
|| (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
255 ret
= LTTNG_ERR_INVALID
;
258 if (!attr
->attr
.num_subbuf
|| (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
259 ret
= LTTNG_ERR_INVALID
;
263 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
264 ret
= LTTNG_ERR_NOT_SUPPORTED
;
268 /* Create UST channel */
269 uchan
= trace_ust_create_channel(attr
, usess
->pathname
);
271 ret
= LTTNG_ERR_FATAL
;
277 case LTTNG_DOMAIN_UST
:
278 DBG2("Channel %s being created in UST global domain", uchan
->name
);
280 /* Enable channel for global domain */
281 ret
= ust_app_create_channel_glb(usess
, uchan
);
284 case LTTNG_DOMAIN_UST_PID
:
285 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
286 case LTTNG_DOMAIN_UST_EXEC_NAME
:
290 goto error_free_chan
;
293 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
294 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
295 goto error_free_chan
;
298 /* Adding the channel to the channel hash table. */
300 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
303 DBG2("Channel %s created successfully", uchan
->name
);
310 * No need to remove the channel from the hash table because at this point
311 * it was not added hence the direct call and no call_rcu().
313 trace_ust_destroy_channel(uchan
);
320 * Disable UST channel for session and domain.
322 int channel_ust_disable(struct ltt_ust_session
*usess
, int domain
,
323 struct ltt_ust_channel
*uchan
)
327 /* Already disabled */
328 if (uchan
->enabled
== 0) {
329 DBG2("Channel UST %s already disabled", uchan
->name
);
333 /* Get the right channel's hashtable */
335 case LTTNG_DOMAIN_UST
:
336 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
337 /* Disable channel for global domain */
338 ret
= ust_app_disable_channel_glb(usess
, uchan
);
341 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
342 case LTTNG_DOMAIN_UST_EXEC_NAME
:
343 case LTTNG_DOMAIN_UST_PID
:
350 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
351 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
357 DBG2("Channel %s disabled successfully", uchan
->name
);