2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 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
;
42 const char *channel_name
= DEFAULT_CHANNEL_NAME
;
44 chan
= zmalloc(sizeof(struct lttng_channel
));
46 PERROR("zmalloc channel init");
50 /* Same for all domains. */
51 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
52 chan
->attr
.tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
53 chan
->attr
.tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
56 case LTTNG_DOMAIN_KERNEL
:
57 assert(type
== LTTNG_BUFFER_GLOBAL
);
58 chan
->attr
.subbuf_size
=
59 default_get_kernel_channel_subbuf_size();
60 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
61 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
62 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
63 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
64 chan
->attr
.live_timer_interval
= DEFAULT_KERNEL_CHANNEL_LIVE_TIMER
;
66 case LTTNG_DOMAIN_JUL
:
67 channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
69 case LTTNG_DOMAIN_LOG4J
:
70 channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
72 case LTTNG_DOMAIN_PYTHON
:
73 channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
75 case LTTNG_DOMAIN_UST
:
78 case LTTNG_BUFFER_PER_UID
:
79 chan
->attr
.subbuf_size
= default_get_ust_uid_channel_subbuf_size();
80 chan
->attr
.num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
81 chan
->attr
.output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
82 chan
->attr
.switch_timer_interval
=
83 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
84 chan
->attr
.read_timer_interval
=
85 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
86 chan
->attr
.live_timer_interval
=
87 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
89 case LTTNG_BUFFER_PER_PID
:
91 chan
->attr
.subbuf_size
= default_get_ust_pid_channel_subbuf_size();
92 chan
->attr
.num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
93 chan
->attr
.output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
94 chan
->attr
.switch_timer_interval
=
95 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
96 chan
->attr
.read_timer_interval
=
97 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
98 chan
->attr
.live_timer_interval
=
99 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
104 goto error
; /* Not implemented */
107 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s",
109 PERROR("snprintf default channel name");
121 * Disable kernel channel of the kernel session.
123 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
127 struct ltt_kernel_channel
*kchan
;
130 assert(channel_name
);
132 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
134 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
138 /* Only if channel is enabled disable it. */
139 if (kchan
->enabled
== 1) {
140 ret
= kernel_disable_channel(kchan
);
141 if (ret
< 0 && ret
!= -EEXIST
) {
142 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
154 * Enable kernel channel of the kernel session.
156 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
157 struct ltt_kernel_channel
*kchan
)
164 if (kchan
->enabled
== 0) {
165 ret
= kernel_enable_channel(kchan
);
167 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
171 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
182 * Create kernel channel of the kernel session and notify kernel thread.
184 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
185 struct lttng_channel
*attr
, int kernel_pipe
)
188 struct lttng_channel
*defattr
= NULL
;
192 /* Creating channel attributes if needed */
194 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
195 LTTNG_BUFFER_GLOBAL
);
196 if (defattr
== NULL
) {
197 ret
= LTTNG_ERR_FATAL
;
203 if (ksession
->snapshot_mode
) {
204 /* Force channel attribute for snapshot mode. */
205 attr
->attr
.overwrite
= 1;
206 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
209 /* Channel not found, creating it */
210 ret
= kernel_create_channel(ksession
, attr
);
212 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
216 /* Notify kernel thread that there is a new channel */
217 ret
= notify_thread_pipe(kernel_pipe
);
219 ret
= LTTNG_ERR_FATAL
;
230 * Enable UST channel for session and domain.
232 int channel_ust_enable(struct ltt_ust_session
*usess
,
233 struct ltt_ust_channel
*uchan
)
240 /* If already enabled, everything is OK */
241 if (uchan
->enabled
) {
242 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
243 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
247 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
250 * Enable channel for UST global domain on all applications. Ignore return
251 * value here since whatever error we got, it means that the channel was
252 * not created on one or many registered applications and we can not report
253 * this to the user yet. However, at this stage, the channel was
254 * successfully created on the session daemon side so the enable-channel
255 * command is a success.
257 (void) ust_app_enable_channel_glb(usess
, uchan
);
260 DBG2("Channel %s enabled successfully", uchan
->name
);
267 * Create UST channel for session and domain.
269 int channel_ust_create(struct ltt_ust_session
*usess
,
270 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
273 struct ltt_ust_channel
*uchan
= NULL
;
274 struct lttng_channel
*defattr
= NULL
;
278 /* Creating channel attributes if needed */
280 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
281 if (defattr
== NULL
) {
282 ret
= LTTNG_ERR_FATAL
;
288 if (usess
->snapshot_mode
) {
289 /* Force channel attribute for snapshot mode. */
290 attr
->attr
.overwrite
= 1;
291 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
295 * Validate UST buffer size and number of buffers: must both be power of 2
296 * and nonzero. We validate right here for UST, because applications will
297 * not report the error to the user (unlike kernel tracing).
299 if (!attr
->attr
.subbuf_size
||
300 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
301 ret
= LTTNG_ERR_INVALID
;
306 * Invalid subbuffer size if it's lower then the page size.
308 if (attr
->attr
.subbuf_size
< page_size
) {
309 ret
= LTTNG_ERR_INVALID
;
313 if (!attr
->attr
.num_subbuf
||
314 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
315 ret
= LTTNG_ERR_INVALID
;
319 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
320 ret
= LTTNG_ERR_NOT_SUPPORTED
;
325 * The tracefile_size should not be < to the subbuf_size, otherwise
326 * we won't be able to write the packets on disk
328 if ((attr
->attr
.tracefile_size
> 0) &&
329 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
330 ret
= LTTNG_ERR_INVALID
;
334 /* Validate buffer type. */
336 case LTTNG_BUFFER_PER_PID
:
338 case LTTNG_BUFFER_PER_UID
:
341 ret
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
345 /* Create UST channel */
346 uchan
= trace_ust_create_channel(attr
, LTTNG_DOMAIN_UST
);
348 ret
= LTTNG_ERR_FATAL
;
353 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
354 * based on the default name.
356 if (!strcmp(uchan
->name
, DEFAULT_JUL_CHANNEL_NAME
)) {
357 uchan
->domain
= LTTNG_DOMAIN_JUL
;
358 } else if (!strcmp(uchan
->name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
359 uchan
->domain
= LTTNG_DOMAIN_LOG4J
;
360 } else if (!strcmp(uchan
->name
, DEFAULT_PYTHON_CHANNEL_NAME
)) {
361 uchan
->domain
= LTTNG_DOMAIN_PYTHON
;
365 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
366 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
369 uchan
->id
= trace_ust_get_next_chan_id(usess
);
371 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
372 uchan
->name
, type
, uchan
->id
);
374 /* Flag session buffer type. */
375 if (!usess
->buffer_type_changed
) {
376 usess
->buffer_type
= type
;
377 usess
->buffer_type_changed
= 1;
378 } else if (usess
->buffer_type
!= type
) {
379 /* Buffer type was already set. Refuse to create channel. */
380 ret
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
381 goto error_free_chan
;
384 /* Enable channel for global domain */
385 ret
= ust_app_create_channel_glb(usess
, uchan
);
386 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
387 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
388 goto error_free_chan
;
391 /* Adding the channel to the channel hash table. */
393 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
394 sizeof(uchan
->name
))) {
395 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
398 * Copy channel attribute to session if this is metadata so if NO
399 * application exists we can access that data in the shadow copy during
400 * the global update of newly registered application.
402 memcpy(&usess
->metadata_attr
, &uchan
->attr
,
403 sizeof(usess
->metadata_attr
));
407 DBG2("Channel %s created successfully", uchan
->name
);
414 * No need to remove the channel from the hash table because at this point
415 * it was not added hence the direct call and no call_rcu().
417 trace_ust_destroy_channel(uchan
);
424 * Disable UST channel for session and domain.
426 int channel_ust_disable(struct ltt_ust_session
*usess
,
427 struct ltt_ust_channel
*uchan
)
434 /* Already disabled */
435 if (uchan
->enabled
== 0) {
436 DBG2("Channel UST %s already disabled", uchan
->name
);
440 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
441 /* Disable channel for global domain */
442 ret
= ust_app_disable_channel_glb(usess
, uchan
);
443 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
444 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
450 DBG2("Channel %s disabled successfully", uchan
->name
);