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"
31 #include "lttng-ust-ctl.h"
32 #include "lttng-ust-error.h"
38 * Return allocated channel attributes.
40 struct lttng_channel
*channel_new_default_attr(int dom
,
41 enum lttng_buffer_type type
)
43 struct lttng_channel
*chan
;
44 const char *channel_name
= DEFAULT_CHANNEL_NAME
;
45 struct lttng_channel_extended
*extended_attr
= NULL
;
47 chan
= zmalloc(sizeof(struct lttng_channel
));
49 PERROR("zmalloc channel init");
53 extended_attr
= zmalloc(sizeof(struct lttng_channel_extended
));
55 PERROR("zmalloc channel extended init");
59 chan
->attr
.extended
.ptr
= extended_attr
;
61 /* Same for all domains. */
62 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
63 chan
->attr
.tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
64 chan
->attr
.tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
67 case LTTNG_DOMAIN_KERNEL
:
68 assert(type
== LTTNG_BUFFER_GLOBAL
);
69 chan
->attr
.subbuf_size
=
70 default_get_kernel_channel_subbuf_size();
71 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
72 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
73 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
74 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
75 chan
->attr
.live_timer_interval
= DEFAULT_KERNEL_CHANNEL_LIVE_TIMER
;
76 extended_attr
->blocking_timeout
= DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
77 extended_attr
->monitor_timer_interval
=
78 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
80 case LTTNG_DOMAIN_JUL
:
81 channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
83 case LTTNG_DOMAIN_LOG4J
:
84 channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
86 case LTTNG_DOMAIN_PYTHON
:
87 channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
89 case LTTNG_DOMAIN_UST
:
92 case LTTNG_BUFFER_PER_UID
:
93 chan
->attr
.subbuf_size
= default_get_ust_uid_channel_subbuf_size();
94 chan
->attr
.num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
95 chan
->attr
.output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
96 chan
->attr
.switch_timer_interval
=
97 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
98 chan
->attr
.read_timer_interval
=
99 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
100 chan
->attr
.live_timer_interval
=
101 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
102 extended_attr
->blocking_timeout
= DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
103 extended_attr
->monitor_timer_interval
=
104 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
106 case LTTNG_BUFFER_PER_PID
:
108 chan
->attr
.subbuf_size
= default_get_ust_pid_channel_subbuf_size();
109 chan
->attr
.num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
110 chan
->attr
.output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
111 chan
->attr
.switch_timer_interval
=
112 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
113 chan
->attr
.read_timer_interval
=
114 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
115 chan
->attr
.live_timer_interval
=
116 DEFAULT_UST_PID_CHANNEL_LIVE_TIMER
;
117 extended_attr
->blocking_timeout
= DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
118 extended_attr
->monitor_timer_interval
=
119 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
124 goto error
; /* Not implemented */
127 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s",
129 PERROR("snprintf default channel name");
141 void channel_attr_destroy(struct lttng_channel
*channel
)
146 free(channel
->attr
.extended
.ptr
);
151 * Disable kernel channel of the kernel session.
153 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
157 struct ltt_kernel_channel
*kchan
;
160 assert(channel_name
);
162 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
164 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
168 /* Only if channel is enabled disable it. */
169 if (kchan
->enabled
== 1) {
170 ret
= kernel_disable_channel(kchan
);
171 if (ret
< 0 && ret
!= -EEXIST
) {
172 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
184 * Enable kernel channel of the kernel session.
186 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
187 struct ltt_kernel_channel
*kchan
)
194 if (kchan
->enabled
== 0) {
195 ret
= kernel_enable_channel(kchan
);
197 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
201 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
211 static int channel_validate(struct lttng_channel
*attr
)
214 * The ringbuffer (both in user space and kernel) behaves badly
215 * in overwrite mode and with less than 2 subbuffers so block it
216 * right away and send back an invalid attribute error.
218 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
224 static int channel_validate_kernel(struct lttng_channel
*attr
)
226 /* Kernel channels do not support blocking timeout. */
227 if (((struct lttng_channel_extended
*)attr
->attr
.extended
.ptr
)->blocking_timeout
) {
234 * Create kernel channel of the kernel session and notify kernel thread.
236 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
237 struct lttng_channel
*attr
, int kernel_pipe
)
240 struct lttng_channel
*defattr
= NULL
;
244 /* Creating channel attributes if needed */
246 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
247 LTTNG_BUFFER_GLOBAL
);
248 if (defattr
== NULL
) {
249 ret
= LTTNG_ERR_FATAL
;
256 * Set the overwrite mode for this channel based on the session
257 * type unless the client explicitly overrides the channel mode.
259 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
260 attr
->attr
.overwrite
= !!ksession
->snapshot_mode
;
263 /* Enforce mmap output for snapshot sessions. */
264 if (ksession
->snapshot_mode
) {
265 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
268 /* Validate common channel properties. */
269 if (channel_validate(attr
) < 0) {
270 ret
= LTTNG_ERR_INVALID
;
274 if (channel_validate_kernel(attr
) < 0) {
275 ret
= LTTNG_ERR_INVALID
;
279 /* Channel not found, creating it */
280 ret
= kernel_create_channel(ksession
, attr
);
282 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
286 /* Notify kernel thread that there is a new channel */
287 ret
= notify_thread_pipe(kernel_pipe
);
289 ret
= LTTNG_ERR_FATAL
;
295 channel_attr_destroy(defattr
);
300 * Enable UST channel for session and domain.
302 int channel_ust_enable(struct ltt_ust_session
*usess
,
303 struct ltt_ust_channel
*uchan
)
310 /* If already enabled, everything is OK */
311 if (uchan
->enabled
) {
312 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
313 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
317 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
320 * Enable channel for UST global domain on all applications. Ignore return
321 * value here since whatever error we got, it means that the channel was
322 * not created on one or many registered applications and we can not report
323 * this to the user yet. However, at this stage, the channel was
324 * successfully created on the session daemon side so the enable-channel
325 * command is a success.
327 (void) ust_app_enable_channel_glb(usess
, uchan
);
330 DBG2("Channel %s enabled successfully", uchan
->name
);
337 * Create UST channel for session and domain.
339 int channel_ust_create(struct ltt_ust_session
*usess
,
340 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
343 struct ltt_ust_channel
*uchan
= NULL
;
344 struct lttng_channel
*defattr
= NULL
;
345 enum lttng_domain_type domain
= LTTNG_DOMAIN_UST
;
346 bool chan_published
= false;
350 /* Creating channel attributes if needed */
352 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
353 if (defattr
== NULL
) {
354 ret
= LTTNG_ERR_FATAL
;
360 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
361 * based on the default name.
363 if (!strcmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
)) {
364 domain
= LTTNG_DOMAIN_JUL
;
365 } else if (!strcmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
366 domain
= LTTNG_DOMAIN_LOG4J
;
367 } else if (!strcmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
)) {
368 domain
= LTTNG_DOMAIN_PYTHON
;
373 * Set the overwrite mode for this channel based on the session
374 * type unless the client explicitly overrides the channel mode.
376 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
377 attr
->attr
.overwrite
= !!usess
->snapshot_mode
;
380 /* Enforce mmap output for snapshot sessions. */
381 if (usess
->snapshot_mode
) {
382 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
385 /* Validate common channel properties. */
386 if (channel_validate(attr
) < 0) {
387 ret
= LTTNG_ERR_INVALID
;
392 * Validate UST buffer size and number of buffers: must both be power of 2
393 * and nonzero. We validate right here for UST, because applications will
394 * not report the error to the user (unlike kernel tracing).
396 if (!attr
->attr
.subbuf_size
||
397 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
398 ret
= LTTNG_ERR_INVALID
;
403 * Invalid subbuffer size if it's lower then the page size.
405 if (attr
->attr
.subbuf_size
< page_size
) {
406 ret
= LTTNG_ERR_INVALID
;
410 if (!attr
->attr
.num_subbuf
||
411 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
412 ret
= LTTNG_ERR_INVALID
;
416 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
417 ret
= LTTNG_ERR_NOT_SUPPORTED
;
422 * The tracefile_size should not be < to the subbuf_size, otherwise
423 * we won't be able to write the packets on disk
425 if ((attr
->attr
.tracefile_size
> 0) &&
426 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
427 ret
= LTTNG_ERR_INVALID
;
431 /* Validate buffer type. */
433 case LTTNG_BUFFER_PER_PID
:
435 case LTTNG_BUFFER_PER_UID
:
438 ret
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
442 /* Create UST channel */
443 uchan
= trace_ust_create_channel(attr
, domain
);
445 ret
= LTTNG_ERR_FATAL
;
450 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
451 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
454 uchan
->id
= trace_ust_get_next_chan_id(usess
);
456 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
457 uchan
->name
, type
, uchan
->id
);
459 /* Flag session buffer type. */
460 if (!usess
->buffer_type_changed
) {
461 usess
->buffer_type
= type
;
462 usess
->buffer_type_changed
= 1;
463 } else if (usess
->buffer_type
!= type
) {
464 /* Buffer type was already set. Refuse to create channel. */
465 ret
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
466 goto error_free_chan
;
469 /* Enable channel for global domain */
470 ret
= ust_app_create_channel_glb(usess
, uchan
);
471 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
472 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
473 goto error_free_chan
;
476 /* Adding the channel to the channel hash table. */
478 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
479 sizeof(uchan
->name
))) {
480 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
481 chan_published
= true;
484 * Copy channel attribute to session if this is metadata so if NO
485 * application exists we can access that data in the shadow copy during
486 * the global update of newly registered application.
488 memcpy(&usess
->metadata_attr
, &uchan
->attr
,
489 sizeof(usess
->metadata_attr
));
493 DBG2("Channel %s created successfully", uchan
->name
);
494 if (domain
!= LTTNG_DOMAIN_UST
) {
495 struct agent
*agt
= trace_ust_find_agent(usess
, domain
);
498 agt
= agent_create(domain
);
500 ret
= LTTNG_ERR_NOMEM
;
501 goto error_remove_chan
;
503 agent_add(agt
, usess
->agents
);
507 channel_attr_destroy(defattr
);
511 if (chan_published
) {
512 trace_ust_delete_channel(usess
->domain_global
.channels
, uchan
);
515 trace_ust_destroy_channel(uchan
);
517 channel_attr_destroy(defattr
);
522 * Disable UST channel for session and domain.
524 int channel_ust_disable(struct ltt_ust_session
*usess
,
525 struct ltt_ust_channel
*uchan
)
532 /* Already disabled */
533 if (uchan
->enabled
== 0) {
534 DBG2("Channel UST %s already disabled", uchan
->name
);
538 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
539 /* Disable channel for global domain */
540 ret
= ust_app_disable_channel_glb(usess
, uchan
);
541 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
542 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
548 DBG2("Channel %s disabled successfully", uchan
->name
);