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>
28 #include "lttng-sessiond.h"
35 * Return allocated channel attributes.
37 struct lttng_channel
*channel_new_default_attr(int dom
,
38 enum lttng_buffer_type type
)
40 struct lttng_channel
*chan
;
42 chan
= zmalloc(sizeof(struct lttng_channel
));
44 PERROR("zmalloc channel init");
48 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s",
49 DEFAULT_CHANNEL_NAME
) < 0) {
50 PERROR("snprintf default channel name");
54 /* Same for all domains. */
55 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
56 chan
->attr
.tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
57 chan
->attr
.tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
60 case LTTNG_DOMAIN_KERNEL
:
61 assert(type
== LTTNG_BUFFER_GLOBAL
);
62 chan
->attr
.subbuf_size
=
63 default_get_kernel_channel_subbuf_size();
64 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
65 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
66 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
67 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
68 chan
->attr
.live_timer_interval
= DEFAULT_KERNEL_CHANNEL_LIVE_TIMER
;
70 case LTTNG_DOMAIN_UST
:
72 case LTTNG_BUFFER_PER_UID
:
73 chan
->attr
.subbuf_size
= default_get_ust_uid_channel_subbuf_size();
74 chan
->attr
.num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
75 chan
->attr
.output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
76 chan
->attr
.switch_timer_interval
=
77 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
78 chan
->attr
.read_timer_interval
=
79 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
80 chan
->attr
.live_timer_interval
=
81 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
83 case LTTNG_BUFFER_PER_PID
:
85 chan
->attr
.subbuf_size
= default_get_ust_pid_channel_subbuf_size();
86 chan
->attr
.num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
87 chan
->attr
.output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
88 chan
->attr
.switch_timer_interval
=
89 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
90 chan
->attr
.read_timer_interval
=
91 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
92 chan
->attr
.live_timer_interval
=
93 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
98 goto error
; /* Not implemented */
110 * Disable kernel channel of the kernel session.
112 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
116 struct ltt_kernel_channel
*kchan
;
119 assert(channel_name
);
121 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
123 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
127 /* Only if channel is enabled disable it. */
128 if (kchan
->enabled
== 1) {
129 ret
= kernel_disable_channel(kchan
);
130 if (ret
< 0 && ret
!= -EEXIST
) {
131 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
143 * Enable kernel channel of the kernel session.
145 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
146 struct ltt_kernel_channel
*kchan
)
153 if (kchan
->enabled
== 0) {
154 ret
= kernel_enable_channel(kchan
);
156 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
160 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
171 * Create kernel channel of the kernel session and notify kernel thread.
173 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
174 struct lttng_channel
*attr
, int kernel_pipe
)
177 struct lttng_channel
*defattr
= NULL
;
181 /* Creating channel attributes if needed */
183 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
184 LTTNG_BUFFER_GLOBAL
);
185 if (defattr
== NULL
) {
186 ret
= LTTNG_ERR_FATAL
;
192 if (ksession
->snapshot_mode
) {
193 /* Force channel attribute for snapshot mode. */
194 attr
->attr
.overwrite
= 1;
195 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
198 /* Channel not found, creating it */
199 ret
= kernel_create_channel(ksession
, attr
);
201 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
205 /* Notify kernel thread that there is a new channel */
206 ret
= notify_thread_pipe(kernel_pipe
);
208 ret
= LTTNG_ERR_FATAL
;
219 * Enable UST channel for session and domain.
221 int channel_ust_enable(struct ltt_ust_session
*usess
,
222 struct ltt_ust_channel
*uchan
)
229 /* If already enabled, everything is OK */
230 if (uchan
->enabled
) {
231 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
232 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
236 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
239 * Enable channel for UST global domain on all applications. Ignore return
240 * value here since whatever error we got, it means that the channel was
241 * not created on one or many registered applications and we can not report
242 * this to the user yet. However, at this stage, the channel was
243 * successfully created on the session daemon side so the enable-channel
244 * command is a success.
246 (void) ust_app_enable_channel_glb(usess
, uchan
);
249 DBG2("Channel %s enabled successfully", uchan
->name
);
256 * Create UST channel for session and domain.
258 int channel_ust_create(struct ltt_ust_session
*usess
,
259 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
262 struct ltt_ust_channel
*uchan
= NULL
;
263 struct lttng_channel
*defattr
= NULL
;
267 /* Creating channel attributes if needed */
269 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
270 if (defattr
== NULL
) {
271 ret
= LTTNG_ERR_FATAL
;
277 if (usess
->snapshot_mode
) {
278 /* Force channel attribute for snapshot mode. */
279 attr
->attr
.overwrite
= 1;
280 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
284 * Validate UST buffer size and number of buffers: must both be power of 2
285 * and nonzero. We validate right here for UST, because applications will
286 * not report the error to the user (unlike kernel tracing).
288 if (!attr
->attr
.subbuf_size
||
289 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
290 ret
= LTTNG_ERR_INVALID
;
295 * Invalid subbuffer size if it's lower then the page size.
297 if (attr
->attr
.subbuf_size
< page_size
) {
298 ret
= LTTNG_ERR_INVALID
;
302 if (!attr
->attr
.num_subbuf
||
303 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
304 ret
= LTTNG_ERR_INVALID
;
308 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
309 ret
= LTTNG_ERR_NOT_SUPPORTED
;
314 * The tracefile_size should not be < to the subbuf_size, otherwise
315 * we won't be able to write the packets on disk
317 if ((attr
->attr
.tracefile_size
> 0) &&
318 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
319 ret
= LTTNG_ERR_INVALID
;
323 /* Validate buffer type. */
325 case LTTNG_BUFFER_PER_PID
:
327 case LTTNG_BUFFER_PER_UID
:
330 ret
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
334 /* Create UST channel */
335 uchan
= trace_ust_create_channel(attr
, LTTNG_DOMAIN_UST
);
337 ret
= LTTNG_ERR_FATAL
;
342 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
343 * based on the default name.
345 if (!strcmp(uchan
->name
, DEFAULT_JUL_CHANNEL_NAME
)) {
346 uchan
->domain
= LTTNG_DOMAIN_JUL
;
347 } else if (!strcmp(uchan
->name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
348 uchan
->domain
= LTTNG_DOMAIN_LOG4J
;
349 } else if (!strcmp(uchan
->name
, DEFAULT_PYTHON_CHANNEL_NAME
)) {
350 uchan
->domain
= LTTNG_DOMAIN_PYTHON
;
354 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
355 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
358 uchan
->id
= trace_ust_get_next_chan_id(usess
);
360 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
361 uchan
->name
, type
, uchan
->id
);
363 /* Flag session buffer type. */
364 if (!usess
->buffer_type_changed
) {
365 usess
->buffer_type
= type
;
366 usess
->buffer_type_changed
= 1;
367 } else if (usess
->buffer_type
!= type
) {
368 /* Buffer type was already set. Refuse to create channel. */
369 ret
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
370 goto error_free_chan
;
373 /* Enable channel for global domain */
374 ret
= ust_app_create_channel_glb(usess
, uchan
);
375 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
376 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
377 goto error_free_chan
;
380 /* Adding the channel to the channel hash table. */
382 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
383 sizeof(uchan
->name
))) {
384 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
387 * Copy channel attribute to session if this is metadata so if NO
388 * application exists we can access that data in the shadow copy during
389 * the global update of newly registered application.
391 memcpy(&usess
->metadata_attr
, &uchan
->attr
,
392 sizeof(usess
->metadata_attr
));
396 DBG2("Channel %s created successfully", uchan
->name
);
403 * No need to remove the channel from the hash table because at this point
404 * it was not added hence the direct call and no call_rcu().
406 trace_ust_destroy_channel(uchan
);
413 * Disable UST channel for session and domain.
415 int channel_ust_disable(struct ltt_ust_session
*usess
,
416 struct ltt_ust_channel
*uchan
)
423 /* Already disabled */
424 if (uchan
->enabled
== 0) {
425 DBG2("Channel UST %s already disabled", uchan
->name
);
429 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
430 /* Disable channel for global domain */
431 ret
= ust_app_disable_channel_glb(usess
, uchan
);
432 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
433 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
439 DBG2("Channel %s disabled successfully", uchan
->name
);