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.
23 #include <common/common.h>
24 #include <common/defaults.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
34 * Return allocated channel attributes.
36 struct lttng_channel
*channel_new_default_attr(int dom
)
38 struct lttng_channel
*chan
;
40 chan
= zmalloc(sizeof(struct lttng_channel
));
42 PERROR("zmalloc channel init");
46 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s",
47 DEFAULT_CHANNEL_NAME
) < 0) {
48 PERROR("snprintf default channel name");
52 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
55 case LTTNG_DOMAIN_KERNEL
:
56 chan
->attr
.subbuf_size
=
57 default_get_kernel_channel_subbuf_size();
58 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
59 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
60 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
61 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
63 case LTTNG_DOMAIN_UST
:
65 case LTTNG_DOMAIN_UST_PID
:
66 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
67 case LTTNG_DOMAIN_UST_EXEC_NAME
:
69 chan
->attr
.subbuf_size
= default_get_ust_channel_subbuf_size();
70 chan
->attr
.num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
71 chan
->attr
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
72 chan
->attr
.switch_timer_interval
= DEFAULT_UST_CHANNEL_SWITCH_TIMER
;
73 chan
->attr
.read_timer_interval
= DEFAULT_UST_CHANNEL_READ_TIMER
;
76 goto error
; /* Not implemented */
88 * Disable kernel channel of the kernel session.
90 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
94 struct ltt_kernel_channel
*kchan
;
99 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
101 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
105 /* Only if channel is enabled disable it. */
106 if (kchan
->enabled
== 1) {
107 ret
= kernel_disable_channel(kchan
);
108 if (ret
< 0 && ret
!= -EEXIST
) {
109 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
121 * Enable kernel channel of the kernel session.
123 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
124 struct ltt_kernel_channel
*kchan
)
131 if (kchan
->enabled
== 0) {
132 ret
= kernel_enable_channel(kchan
);
134 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
138 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
149 * Create kernel channel of the kernel session and notify kernel thread.
151 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
152 struct lttng_channel
*attr
, int kernel_pipe
)
155 struct lttng_channel
*defattr
= NULL
;
159 /* Creating channel attributes if needed */
161 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
);
162 if (defattr
== NULL
) {
163 ret
= LTTNG_ERR_FATAL
;
169 /* Channel not found, creating it */
170 ret
= kernel_create_channel(ksession
, attr
);
172 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
176 /* Notify kernel thread that there is a new channel */
177 ret
= notify_thread_pipe(kernel_pipe
);
179 ret
= LTTNG_ERR_FATAL
;
190 * Enable UST channel for session and domain.
192 int channel_ust_enable(struct ltt_ust_session
*usess
,
193 struct ltt_ust_channel
*uchan
)
200 /* If already enabled, everything is OK */
201 if (uchan
->enabled
) {
202 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
203 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
207 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
210 * Enable channel for UST global domain on all applications. Ignore return
211 * value here since whatever error we got, it means that the channel was
212 * not created on one or many registered applications and we can not report
213 * this to the user yet. However, at this stage, the channel was
214 * successfully created on the session daemon side so the enable-channel
215 * command is a success.
217 (void) ust_app_create_channel_glb(usess
, uchan
);
220 DBG2("Channel %s enabled successfully", uchan
->name
);
227 * Create UST channel for session and domain.
229 int channel_ust_create(struct ltt_ust_session
*usess
,
230 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
233 struct ltt_ust_channel
*uchan
= NULL
;
234 struct lttng_channel
*defattr
= NULL
;
238 /* Creating channel attributes if needed */
240 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
);
241 if (defattr
== NULL
) {
242 ret
= LTTNG_ERR_FATAL
;
248 if (attr
->attr
.subbuf_size
< DEFAULT_UST_CHANNEL_SUBBUF_SIZE
) {
249 ret
= LTTNG_ERR_INVALID
;
254 * Validate UST buffer size and number of buffers: must both be power of 2
255 * and nonzero. We validate right here for UST, because applications will
256 * not report the error to the user (unlike kernel tracing).
258 if (!attr
->attr
.subbuf_size
||
259 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
260 ret
= LTTNG_ERR_INVALID
;
264 if (!attr
->attr
.num_subbuf
||
265 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
266 ret
= LTTNG_ERR_INVALID
;
270 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
271 ret
= LTTNG_ERR_NOT_SUPPORTED
;
276 * The tracefile_size should not be < to the subbuf_size, otherwise
277 * we won't be able to write the packets on disk
279 if ((attr
->attr
.tracefile_size
> 0) &&
280 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
281 ret
= LTTNG_ERR_INVALID
;
285 /* Validate buffer type. */
287 case LTTNG_BUFFER_PER_PID
:
288 case LTTNG_BUFFER_PER_UID
:
291 ret
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
295 /* Create UST channel */
296 uchan
= trace_ust_create_channel(attr
, usess
->pathname
);
298 ret
= LTTNG_ERR_FATAL
;
302 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
303 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
306 uchan
->id
= trace_ust_get_next_chan_id(usess
);
308 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
309 uchan
->name
, type
, uchan
->id
);
311 /* Flag session buffer type. */
312 if (!usess
->buffer_type_changed
) {
313 usess
->buffer_type
= type
;
314 usess
->buffer_type_changed
= 1;
315 } else if (usess
->buffer_type
!= type
) {
316 /* Buffer type was already set. Refuse to create channel. */
317 ret
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
318 goto error_free_chan
;
321 /* Enable channel for global domain */
322 ret
= ust_app_create_channel_glb(usess
, uchan
);
323 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
324 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
325 goto error_free_chan
;
328 /* Adding the channel to the channel hash table. */
330 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
333 DBG2("Channel %s created successfully", uchan
->name
);
340 * No need to remove the channel from the hash table because at this point
341 * it was not added hence the direct call and no call_rcu().
343 trace_ust_destroy_channel(uchan
);
350 * Disable UST channel for session and domain.
352 int channel_ust_disable(struct ltt_ust_session
*usess
,
353 struct ltt_ust_channel
*uchan
)
360 /* Already disabled */
361 if (uchan
->enabled
== 0) {
362 DBG2("Channel UST %s already disabled", uchan
->name
);
366 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
367 /* Disable channel for global domain */
368 ret
= ust_app_disable_channel_glb(usess
, uchan
);
369 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
370 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
376 DBG2("Channel %s disabled successfully", uchan
->name
);