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.
24 #include <common/common.h>
25 #include <common/defaults.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
29 #include "lttng-sessiond.h"
36 * Return allocated channel attributes.
38 struct lttng_channel
*channel_new_default_attr(int dom
,
39 enum lttng_buffer_type type
)
41 struct lttng_channel
*chan
;
43 chan
= zmalloc(sizeof(struct lttng_channel
));
45 PERROR("zmalloc channel init");
49 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s",
50 DEFAULT_CHANNEL_NAME
) < 0) {
51 PERROR("snprintf default channel name");
55 /* Same for all domains. */
56 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
57 chan
->attr
.tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
58 chan
->attr
.tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
61 case LTTNG_DOMAIN_KERNEL
:
62 assert(type
== LTTNG_BUFFER_GLOBAL
);
63 chan
->attr
.subbuf_size
=
64 default_get_kernel_channel_subbuf_size();
65 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
66 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
67 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
68 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
69 chan
->attr
.live_timer_interval
= DEFAULT_KERNEL_CHANNEL_LIVE_TIMER
;
71 case LTTNG_DOMAIN_UST
:
73 case LTTNG_BUFFER_PER_UID
:
74 chan
->attr
.subbuf_size
= default_get_ust_uid_channel_subbuf_size();
75 chan
->attr
.num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
76 chan
->attr
.output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
77 chan
->attr
.switch_timer_interval
=
78 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
79 chan
->attr
.read_timer_interval
=
80 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
81 chan
->attr
.live_timer_interval
=
82 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
84 case LTTNG_BUFFER_PER_PID
:
86 chan
->attr
.subbuf_size
= default_get_ust_pid_channel_subbuf_size();
87 chan
->attr
.num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
88 chan
->attr
.output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
89 chan
->attr
.switch_timer_interval
=
90 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
91 chan
->attr
.read_timer_interval
=
92 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
93 chan
->attr
.live_timer_interval
=
94 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
99 goto error
; /* Not implemented */
111 * Disable kernel channel of the kernel session.
113 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
117 struct ltt_kernel_channel
*kchan
;
120 assert(channel_name
);
122 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
124 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
128 /* Only if channel is enabled disable it. */
129 if (kchan
->enabled
== 1) {
130 ret
= kernel_disable_channel(kchan
);
131 if (ret
< 0 && ret
!= -EEXIST
) {
132 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
144 * Enable kernel channel of the kernel session.
146 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
147 struct ltt_kernel_channel
*kchan
)
154 if (kchan
->enabled
== 0) {
155 ret
= kernel_enable_channel(kchan
);
157 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
161 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
172 * Create kernel channel of the kernel session and notify kernel thread.
174 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
175 struct lttng_channel
*attr
, int kernel_pipe
)
178 struct lttng_channel
*defattr
= NULL
;
182 /* Creating channel attributes if needed */
184 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
185 LTTNG_BUFFER_GLOBAL
);
186 if (defattr
== NULL
) {
187 ret
= LTTNG_ERR_FATAL
;
193 if (ksession
->snapshot_mode
) {
194 /* Force channel attribute for snapshot mode. */
195 attr
->attr
.overwrite
= 1;
196 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
199 /* Channel not found, creating it */
200 ret
= kernel_create_channel(ksession
, attr
);
202 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
206 /* Notify kernel thread that there is a new channel */
207 ret
= notify_thread_pipe(kernel_pipe
);
209 ret
= LTTNG_ERR_FATAL
;
220 * Enable UST channel for session and domain.
222 int channel_ust_enable(struct ltt_ust_session
*usess
,
223 struct ltt_ust_channel
*uchan
)
230 /* If already enabled, everything is OK */
231 if (uchan
->enabled
) {
232 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
233 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
237 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
240 * Enable channel for UST global domain on all applications. Ignore return
241 * value here since whatever error we got, it means that the channel was
242 * not created on one or many registered applications and we can not report
243 * this to the user yet. However, at this stage, the channel was
244 * successfully created on the session daemon side so the enable-channel
245 * command is a success.
247 (void) ust_app_enable_channel_glb(usess
, uchan
);
250 DBG2("Channel %s enabled successfully", uchan
->name
);
257 * Create UST channel for session and domain.
259 int channel_ust_create(struct ltt_ust_session
*usess
,
260 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
263 struct ltt_ust_channel
*uchan
= NULL
;
264 struct lttng_channel
*defattr
= NULL
;
268 /* Creating channel attributes if needed */
270 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
271 if (defattr
== NULL
) {
272 ret
= LTTNG_ERR_FATAL
;
278 if (usess
->snapshot_mode
) {
279 /* Force channel attribute for snapshot mode. */
280 attr
->attr
.overwrite
= 1;
281 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
285 * Validate UST buffer size and number of buffers: must both be power of 2
286 * and nonzero. We validate right here for UST, because applications will
287 * not report the error to the user (unlike kernel tracing).
289 if (!attr
->attr
.subbuf_size
||
290 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
291 ret
= LTTNG_ERR_INVALID
;
296 * Invalid subbuffer size if it's lower then the page size.
298 if (attr
->attr
.subbuf_size
< page_size
) {
299 ret
= LTTNG_ERR_INVALID
;
303 if (!attr
->attr
.num_subbuf
||
304 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
305 ret
= LTTNG_ERR_INVALID
;
309 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
310 ret
= LTTNG_ERR_NOT_SUPPORTED
;
315 * The tracefile_size should not be < to the subbuf_size, otherwise
316 * we won't be able to write the packets on disk
318 if ((attr
->attr
.tracefile_size
> 0) &&
319 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
320 ret
= LTTNG_ERR_INVALID
;
324 /* Validate buffer type. */
326 case LTTNG_BUFFER_PER_PID
:
328 case LTTNG_BUFFER_PER_UID
:
331 ret
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
335 /* Create UST channel */
336 uchan
= trace_ust_create_channel(attr
);
338 ret
= LTTNG_ERR_FATAL
;
342 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
343 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
346 uchan
->id
= trace_ust_get_next_chan_id(usess
);
348 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
349 uchan
->name
, type
, uchan
->id
);
351 /* Flag session buffer type. */
352 if (!usess
->buffer_type_changed
) {
353 usess
->buffer_type
= type
;
354 usess
->buffer_type_changed
= 1;
355 } else if (usess
->buffer_type
!= type
) {
356 /* Buffer type was already set. Refuse to create channel. */
357 ret
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
358 goto error_free_chan
;
361 /* Enable channel for global domain */
362 ret
= ust_app_create_channel_glb(usess
, uchan
);
363 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
364 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
365 goto error_free_chan
;
368 /* Adding the channel to the channel hash table. */
370 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
371 sizeof(uchan
->name
))) {
372 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
375 * Copy channel attribute to session if this is metadata so if NO
376 * application exists we can access that data in the shadow copy during
377 * the global update of newly registered application.
379 memcpy(&usess
->metadata_attr
, &uchan
->attr
,
380 sizeof(usess
->metadata_attr
));
384 DBG2("Channel %s created successfully", uchan
->name
);
391 * No need to remove the channel from the hash table because at this point
392 * it was not added hence the direct call and no call_rcu().
394 trace_ust_destroy_channel(uchan
);
401 * Disable UST channel for session and domain.
403 int channel_ust_disable(struct ltt_ust_session
*usess
,
404 struct ltt_ust_channel
*uchan
)
411 /* Already disabled */
412 if (uchan
->enabled
== 0) {
413 DBG2("Channel UST %s already disabled", uchan
->name
);
417 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
418 /* Disable channel for global domain */
419 ret
= ust_app_disable_channel_glb(usess
, uchan
);
420 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
421 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
427 DBG2("Channel %s disabled successfully", uchan
->name
);