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"
37 * Return allocated channel attributes.
39 struct lttng_channel
*channel_new_default_attr(int dom
,
40 enum lttng_buffer_type type
)
42 struct lttng_channel
*chan
;
43 const char *channel_name
= DEFAULT_CHANNEL_NAME
;
45 chan
= zmalloc(sizeof(struct lttng_channel
));
47 PERROR("zmalloc channel init");
51 /* Same for all domains. */
52 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
53 chan
->attr
.tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
54 chan
->attr
.tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
57 case LTTNG_DOMAIN_KERNEL
:
58 assert(type
== LTTNG_BUFFER_GLOBAL
);
59 chan
->attr
.subbuf_size
=
60 default_get_kernel_channel_subbuf_size();
61 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
62 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
63 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
64 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
65 chan
->attr
.live_timer_interval
= DEFAULT_KERNEL_CHANNEL_LIVE_TIMER
;
67 case LTTNG_DOMAIN_JUL
:
68 channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
70 case LTTNG_DOMAIN_LOG4J
:
71 channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
73 case LTTNG_DOMAIN_PYTHON
:
74 channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
76 case LTTNG_DOMAIN_UST
:
79 case LTTNG_BUFFER_PER_UID
:
80 chan
->attr
.subbuf_size
= default_get_ust_uid_channel_subbuf_size();
81 chan
->attr
.num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
82 chan
->attr
.output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
83 chan
->attr
.switch_timer_interval
=
84 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
85 chan
->attr
.read_timer_interval
=
86 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
87 chan
->attr
.live_timer_interval
=
88 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
90 case LTTNG_BUFFER_PER_PID
:
92 chan
->attr
.subbuf_size
= default_get_ust_pid_channel_subbuf_size();
93 chan
->attr
.num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
94 chan
->attr
.output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
95 chan
->attr
.switch_timer_interval
=
96 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
97 chan
->attr
.read_timer_interval
=
98 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
99 chan
->attr
.live_timer_interval
=
100 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
105 goto error
; /* Not implemented */
108 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s",
110 PERROR("snprintf default channel name");
122 * Disable kernel channel of the kernel session.
124 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
128 struct ltt_kernel_channel
*kchan
;
131 assert(channel_name
);
133 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
135 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
139 /* Only if channel is enabled disable it. */
140 if (kchan
->enabled
== 1) {
141 ret
= kernel_disable_channel(kchan
);
142 if (ret
< 0 && ret
!= -EEXIST
) {
143 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
155 * Enable kernel channel of the kernel session.
157 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
158 struct ltt_kernel_channel
*kchan
)
165 if (kchan
->enabled
== 0) {
166 ret
= kernel_enable_channel(kchan
);
168 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
172 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
182 static int channel_validate(struct lttng_channel
*attr
)
185 * The ringbuffer (both in user space and kernel) behaves badly
186 * in overwrite mode and with less than 2 subbuffers so block it
187 * right away and send back an invalid attribute error.
189 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
196 * Create kernel channel of the kernel session and notify kernel thread.
198 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
199 struct lttng_channel
*attr
, int kernel_pipe
)
202 struct lttng_channel
*defattr
= NULL
;
206 /* Creating channel attributes if needed */
208 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
209 LTTNG_BUFFER_GLOBAL
);
210 if (defattr
== NULL
) {
211 ret
= LTTNG_ERR_FATAL
;
218 * Set the overwrite mode for this channel based on the session
219 * type unless the client explicitly overrides the channel mode.
221 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
222 attr
->attr
.overwrite
= !!ksession
->snapshot_mode
;
225 /* Enforce mmap output for snapshot sessions. */
226 if (ksession
->snapshot_mode
) {
227 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
230 /* Validate common channel properties. */
231 if (channel_validate(attr
) < 0) {
232 ret
= LTTNG_ERR_INVALID
;
236 /* Channel not found, creating it */
237 ret
= kernel_create_channel(ksession
, attr
);
239 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
243 /* Notify kernel thread that there is a new channel */
244 ret
= notify_thread_pipe(kernel_pipe
);
246 ret
= LTTNG_ERR_FATAL
;
257 * Enable UST channel for session and domain.
259 int channel_ust_enable(struct ltt_ust_session
*usess
,
260 struct ltt_ust_channel
*uchan
)
267 /* If already enabled, everything is OK */
268 if (uchan
->enabled
) {
269 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
270 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
274 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
277 * Enable channel for UST global domain on all applications. Ignore return
278 * value here since whatever error we got, it means that the channel was
279 * not created on one or many registered applications and we can not report
280 * this to the user yet. However, at this stage, the channel was
281 * successfully created on the session daemon side so the enable-channel
282 * command is a success.
284 (void) ust_app_enable_channel_glb(usess
, uchan
);
287 DBG2("Channel %s enabled successfully", uchan
->name
);
294 * Create UST channel for session and domain.
296 int channel_ust_create(struct ltt_ust_session
*usess
,
297 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
300 struct ltt_ust_channel
*uchan
= NULL
;
301 struct lttng_channel
*defattr
= NULL
;
302 enum lttng_domain_type domain
= LTTNG_DOMAIN_UST
;
306 /* Creating channel attributes if needed */
308 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
309 if (defattr
== NULL
) {
310 ret
= LTTNG_ERR_FATAL
;
316 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
317 * based on the default name.
319 if (!strcmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
)) {
320 domain
= LTTNG_DOMAIN_JUL
;
321 } else if (!strcmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
322 domain
= LTTNG_DOMAIN_LOG4J
;
323 } else if (!strcmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
)) {
324 domain
= LTTNG_DOMAIN_PYTHON
;
329 * Set the overwrite mode for this channel based on the session
330 * type unless the client explicitly overrides the channel mode.
332 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
333 attr
->attr
.overwrite
= !!usess
->snapshot_mode
;
336 /* Enforce mmap output for snapshot sessions. */
337 if (usess
->snapshot_mode
) {
338 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
341 /* Validate common channel properties. */
342 if (channel_validate(attr
) < 0) {
343 ret
= LTTNG_ERR_INVALID
;
348 * Validate UST buffer size and number of buffers: must both be power of 2
349 * and nonzero. We validate right here for UST, because applications will
350 * not report the error to the user (unlike kernel tracing).
352 if (!attr
->attr
.subbuf_size
||
353 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
354 ret
= LTTNG_ERR_INVALID
;
359 * Invalid subbuffer size if it's lower then the page size.
361 if (attr
->attr
.subbuf_size
< page_size
) {
362 ret
= LTTNG_ERR_INVALID
;
366 if (!attr
->attr
.num_subbuf
||
367 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
368 ret
= LTTNG_ERR_INVALID
;
372 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
373 ret
= LTTNG_ERR_NOT_SUPPORTED
;
378 * The tracefile_size should not be < to the subbuf_size, otherwise
379 * we won't be able to write the packets on disk
381 if ((attr
->attr
.tracefile_size
> 0) &&
382 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
383 ret
= LTTNG_ERR_INVALID
;
387 /* Validate buffer type. */
389 case LTTNG_BUFFER_PER_PID
:
391 case LTTNG_BUFFER_PER_UID
:
394 ret
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
398 /* Create UST channel */
399 uchan
= trace_ust_create_channel(attr
, domain
);
401 ret
= LTTNG_ERR_FATAL
;
406 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
407 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
410 uchan
->id
= trace_ust_get_next_chan_id(usess
);
412 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
413 uchan
->name
, type
, uchan
->id
);
415 /* Flag session buffer type. */
416 if (!usess
->buffer_type_changed
) {
417 usess
->buffer_type
= type
;
418 usess
->buffer_type_changed
= 1;
419 } else if (usess
->buffer_type
!= type
) {
420 /* Buffer type was already set. Refuse to create channel. */
421 ret
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
422 goto error_free_chan
;
425 /* Enable channel for global domain */
426 ret
= ust_app_create_channel_glb(usess
, uchan
);
427 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
428 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
429 goto error_free_chan
;
432 /* Adding the channel to the channel hash table. */
434 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
435 sizeof(uchan
->name
))) {
436 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
439 * Copy channel attribute to session if this is metadata so if NO
440 * application exists we can access that data in the shadow copy during
441 * the global update of newly registered application.
443 memcpy(&usess
->metadata_attr
, &uchan
->attr
,
444 sizeof(usess
->metadata_attr
));
448 DBG2("Channel %s created successfully", uchan
->name
);
449 if (domain
!= LTTNG_DOMAIN_UST
) {
450 struct agent
*agt
= trace_ust_find_agent(usess
, domain
);
453 agt
= agent_create(domain
);
455 ret
= LTTNG_ERR_NOMEM
;
456 goto error_free_chan
;
458 agent_add(agt
, usess
->agents
);
467 * No need to remove the channel from the hash table because at this point
468 * it was not added hence the direct call and no call_rcu().
470 trace_ust_destroy_channel(uchan
);
477 * Disable UST channel for session and domain.
479 int channel_ust_disable(struct ltt_ust_session
*usess
,
480 struct ltt_ust_channel
*uchan
)
487 /* Already disabled */
488 if (uchan
->enabled
== 0) {
489 DBG2("Channel UST %s already disabled", uchan
->name
);
493 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
494 /* Disable channel for global domain */
495 ret
= ust_app_disable_channel_glb(usess
, uchan
);
496 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
497 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
503 DBG2("Channel %s disabled successfully", uchan
->name
);