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
;
44 struct lttng_channel_extended
*extended_attr
= NULL
;
46 chan
= zmalloc(sizeof(struct lttng_channel
));
48 PERROR("zmalloc channel init");
52 extended_attr
= zmalloc(sizeof(struct lttng_channel_extended
));
54 PERROR("zmalloc channel extended init");
58 chan
->attr
.extended
.ptr
= extended_attr
;
60 /* Same for all domains. */
61 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
62 chan
->attr
.tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
63 chan
->attr
.tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
66 case LTTNG_DOMAIN_KERNEL
:
67 assert(type
== LTTNG_BUFFER_GLOBAL
);
68 chan
->attr
.subbuf_size
=
69 default_get_kernel_channel_subbuf_size();
70 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
71 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
72 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
73 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
74 chan
->attr
.live_timer_interval
= DEFAULT_KERNEL_CHANNEL_LIVE_TIMER
;
75 extended_attr
->monitor_timer_interval
=
76 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
78 case LTTNG_DOMAIN_JUL
:
79 channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
81 case LTTNG_DOMAIN_LOG4J
:
82 channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
84 case LTTNG_DOMAIN_PYTHON
:
85 channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
87 case LTTNG_DOMAIN_UST
:
90 case LTTNG_BUFFER_PER_UID
:
91 chan
->attr
.subbuf_size
= default_get_ust_uid_channel_subbuf_size();
92 chan
->attr
.num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
93 chan
->attr
.output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
94 chan
->attr
.switch_timer_interval
=
95 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
96 chan
->attr
.read_timer_interval
=
97 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
98 chan
->attr
.live_timer_interval
=
99 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
100 extended_attr
->monitor_timer_interval
=
101 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
103 case LTTNG_BUFFER_PER_PID
:
105 chan
->attr
.subbuf_size
= default_get_ust_pid_channel_subbuf_size();
106 chan
->attr
.num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
107 chan
->attr
.output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
108 chan
->attr
.switch_timer_interval
=
109 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
110 chan
->attr
.read_timer_interval
=
111 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
112 chan
->attr
.live_timer_interval
=
113 DEFAULT_UST_PID_CHANNEL_LIVE_TIMER
;
114 extended_attr
->monitor_timer_interval
=
115 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
120 goto error
; /* Not implemented */
123 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s",
125 PERROR("snprintf default channel name");
137 void channel_attr_destroy(struct lttng_channel
*channel
)
142 free(channel
->attr
.extended
.ptr
);
147 * Disable kernel channel of the kernel session.
149 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
153 struct ltt_kernel_channel
*kchan
;
156 assert(channel_name
);
158 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
160 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
164 /* Only if channel is enabled disable it. */
165 if (kchan
->enabled
== 1) {
166 ret
= kernel_disable_channel(kchan
);
167 if (ret
< 0 && ret
!= -EEXIST
) {
168 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
180 * Enable kernel channel of the kernel session.
182 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
183 struct ltt_kernel_channel
*kchan
)
190 if (kchan
->enabled
== 0) {
191 ret
= kernel_enable_channel(kchan
);
193 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
197 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
207 static int channel_validate(struct lttng_channel
*attr
)
210 * The ringbuffer (both in user space and kernel) behaves badly
211 * in overwrite mode and with less than 2 subbuffers so block it
212 * right away and send back an invalid attribute error.
214 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
221 * Create kernel channel of the kernel session and notify kernel thread.
223 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
224 struct lttng_channel
*attr
, int kernel_pipe
)
227 struct lttng_channel
*defattr
= NULL
;
231 /* Creating channel attributes if needed */
233 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
234 LTTNG_BUFFER_GLOBAL
);
235 if (defattr
== NULL
) {
236 ret
= LTTNG_ERR_FATAL
;
243 * Set the overwrite mode for this channel based on the session
244 * type unless the client explicitly overrides the channel mode.
246 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
247 attr
->attr
.overwrite
= !!ksession
->snapshot_mode
;
250 /* Enforce mmap output for snapshot sessions. */
251 if (ksession
->snapshot_mode
) {
252 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
255 /* Validate common channel properties. */
256 if (channel_validate(attr
) < 0) {
257 ret
= LTTNG_ERR_INVALID
;
261 /* Channel not found, creating it */
262 ret
= kernel_create_channel(ksession
, attr
);
264 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
268 /* Notify kernel thread that there is a new channel */
269 ret
= notify_thread_pipe(kernel_pipe
);
271 ret
= LTTNG_ERR_FATAL
;
277 channel_attr_destroy(defattr
);
282 * Enable UST channel for session and domain.
284 int channel_ust_enable(struct ltt_ust_session
*usess
,
285 struct ltt_ust_channel
*uchan
)
292 /* If already enabled, everything is OK */
293 if (uchan
->enabled
) {
294 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
295 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
299 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
302 * Enable channel for UST global domain on all applications. Ignore return
303 * value here since whatever error we got, it means that the channel was
304 * not created on one or many registered applications and we can not report
305 * this to the user yet. However, at this stage, the channel was
306 * successfully created on the session daemon side so the enable-channel
307 * command is a success.
309 (void) ust_app_enable_channel_glb(usess
, uchan
);
312 DBG2("Channel %s enabled successfully", uchan
->name
);
319 * Create UST channel for session and domain.
321 int channel_ust_create(struct ltt_ust_session
*usess
,
322 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
325 struct ltt_ust_channel
*uchan
= NULL
;
326 struct lttng_channel
*defattr
= NULL
;
327 enum lttng_domain_type domain
= LTTNG_DOMAIN_UST
;
331 /* Creating channel attributes if needed */
333 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
334 if (defattr
== NULL
) {
335 ret
= LTTNG_ERR_FATAL
;
341 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
342 * based on the default name.
344 if (!strcmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
)) {
345 domain
= LTTNG_DOMAIN_JUL
;
346 } else if (!strcmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
347 domain
= LTTNG_DOMAIN_LOG4J
;
348 } else if (!strcmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
)) {
349 domain
= LTTNG_DOMAIN_PYTHON
;
354 * Set the overwrite mode for this channel based on the session
355 * type unless the client explicitly overrides the channel mode.
357 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
358 attr
->attr
.overwrite
= !!usess
->snapshot_mode
;
361 /* Enforce mmap output for snapshot sessions. */
362 if (usess
->snapshot_mode
) {
363 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
366 /* Validate common channel properties. */
367 if (channel_validate(attr
) < 0) {
368 ret
= LTTNG_ERR_INVALID
;
373 * Validate UST buffer size and number of buffers: must both be power of 2
374 * and nonzero. We validate right here for UST, because applications will
375 * not report the error to the user (unlike kernel tracing).
377 if (!attr
->attr
.subbuf_size
||
378 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
379 ret
= LTTNG_ERR_INVALID
;
384 * Invalid subbuffer size if it's lower then the page size.
386 if (attr
->attr
.subbuf_size
< page_size
) {
387 ret
= LTTNG_ERR_INVALID
;
391 if (!attr
->attr
.num_subbuf
||
392 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
393 ret
= LTTNG_ERR_INVALID
;
397 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
398 ret
= LTTNG_ERR_NOT_SUPPORTED
;
403 * The tracefile_size should not be < to the subbuf_size, otherwise
404 * we won't be able to write the packets on disk
406 if ((attr
->attr
.tracefile_size
> 0) &&
407 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
408 ret
= LTTNG_ERR_INVALID
;
412 /* Validate buffer type. */
414 case LTTNG_BUFFER_PER_PID
:
416 case LTTNG_BUFFER_PER_UID
:
419 ret
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
423 /* Create UST channel */
424 uchan
= trace_ust_create_channel(attr
, domain
);
426 ret
= LTTNG_ERR_FATAL
;
431 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
432 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
435 uchan
->id
= trace_ust_get_next_chan_id(usess
);
437 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
438 uchan
->name
, type
, uchan
->id
);
440 /* Flag session buffer type. */
441 if (!usess
->buffer_type_changed
) {
442 usess
->buffer_type
= type
;
443 usess
->buffer_type_changed
= 1;
444 } else if (usess
->buffer_type
!= type
) {
445 /* Buffer type was already set. Refuse to create channel. */
446 ret
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
447 goto error_free_chan
;
450 /* Enable channel for global domain */
451 ret
= ust_app_create_channel_glb(usess
, uchan
);
452 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
453 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
454 goto error_free_chan
;
457 /* Adding the channel to the channel hash table. */
459 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
460 sizeof(uchan
->name
))) {
461 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
464 * Copy channel attribute to session if this is metadata so if NO
465 * application exists we can access that data in the shadow copy during
466 * the global update of newly registered application.
468 memcpy(&usess
->metadata_attr
, &uchan
->attr
,
469 sizeof(usess
->metadata_attr
));
473 DBG2("Channel %s created successfully", uchan
->name
);
474 if (domain
!= LTTNG_DOMAIN_UST
) {
475 struct agent
*agt
= trace_ust_find_agent(usess
, domain
);
478 agt
= agent_create(domain
);
480 ret
= LTTNG_ERR_NOMEM
;
481 goto error_free_chan
;
483 agent_add(agt
, usess
->agents
);
487 channel_attr_destroy(defattr
);
492 * No need to remove the channel from the hash table because at this point
493 * it was not added hence the direct call and no call_rcu().
495 trace_ust_destroy_channel(uchan
);
497 channel_attr_destroy(defattr
);
502 * Disable UST channel for session and domain.
504 int channel_ust_disable(struct ltt_ust_session
*usess
,
505 struct ltt_ust_channel
*uchan
)
512 /* Already disabled */
513 if (uchan
->enabled
== 0) {
514 DBG2("Channel UST %s already disabled", uchan
->name
);
518 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
519 /* Disable channel for global domain */
520 ret
= ust_app_disable_channel_glb(usess
, uchan
);
521 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
522 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
528 DBG2("Channel %s disabled successfully", uchan
->name
);