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 /* Validate common channel properties. */
264 if (channel_validate(attr
) < 0) {
265 ret
= LTTNG_ERR_INVALID
;
269 if (channel_validate_kernel(attr
) < 0) {
270 ret
= LTTNG_ERR_INVALID
;
274 /* Channel not found, creating it */
275 ret
= kernel_create_channel(ksession
, attr
);
277 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
281 /* Notify kernel thread that there is a new channel */
282 ret
= notify_thread_pipe(kernel_pipe
);
284 ret
= LTTNG_ERR_FATAL
;
290 channel_attr_destroy(defattr
);
295 * Enable UST channel for session and domain.
297 int channel_ust_enable(struct ltt_ust_session
*usess
,
298 struct ltt_ust_channel
*uchan
)
305 /* If already enabled, everything is OK */
306 if (uchan
->enabled
) {
307 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
308 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
312 DBG2("Channel %s enabled successfully", uchan
->name
);
315 if (!usess
->active
) {
317 * The channel will be activated against the apps
318 * when the session is started as part of the
319 * application channel "synchronize" operation.
324 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
327 * Enable channel for UST global domain on all applications. Ignore return
328 * value here since whatever error we got, it means that the channel was
329 * not created on one or many registered applications and we can not report
330 * this to the user yet. However, at this stage, the channel was
331 * successfully created on the session daemon side so the enable-channel
332 * command is a success.
334 (void) ust_app_enable_channel_glb(usess
, uchan
);
342 * Create UST channel for session and domain.
344 int channel_ust_create(struct ltt_ust_session
*usess
,
345 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
348 struct ltt_ust_channel
*uchan
= NULL
;
349 struct lttng_channel
*defattr
= NULL
;
350 enum lttng_domain_type domain
= LTTNG_DOMAIN_UST
;
351 bool chan_published
= false;
355 /* Creating channel attributes if needed */
357 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
358 if (defattr
== NULL
) {
359 ret
= LTTNG_ERR_FATAL
;
365 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
366 * based on the default name.
368 if (!strcmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
)) {
369 domain
= LTTNG_DOMAIN_JUL
;
370 } else if (!strcmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
371 domain
= LTTNG_DOMAIN_LOG4J
;
372 } else if (!strcmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
)) {
373 domain
= LTTNG_DOMAIN_PYTHON
;
378 * Set the overwrite mode for this channel based on the session
379 * type unless the client explicitly overrides the channel mode.
381 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
382 attr
->attr
.overwrite
= !!usess
->snapshot_mode
;
385 /* Enforce mmap output for snapshot sessions. */
386 if (usess
->snapshot_mode
) {
387 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
390 /* Validate common channel properties. */
391 if (channel_validate(attr
) < 0) {
392 ret
= LTTNG_ERR_INVALID
;
397 * Validate UST buffer size and number of buffers: must both be power of 2
398 * and nonzero. We validate right here for UST, because applications will
399 * not report the error to the user (unlike kernel tracing).
401 if (!attr
->attr
.subbuf_size
||
402 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
403 ret
= LTTNG_ERR_INVALID
;
408 * Invalid subbuffer size if it's lower then the page size.
410 if (attr
->attr
.subbuf_size
< page_size
) {
411 ret
= LTTNG_ERR_INVALID
;
415 if (!attr
->attr
.num_subbuf
||
416 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
417 ret
= LTTNG_ERR_INVALID
;
421 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
422 ret
= LTTNG_ERR_NOT_SUPPORTED
;
427 * The tracefile_size should not be < to the subbuf_size, otherwise
428 * we won't be able to write the packets on disk
430 if ((attr
->attr
.tracefile_size
> 0) &&
431 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
432 ret
= LTTNG_ERR_INVALID
;
436 /* Validate buffer type. */
438 case LTTNG_BUFFER_PER_PID
:
440 case LTTNG_BUFFER_PER_UID
:
443 ret
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
447 /* Create UST channel */
448 uchan
= trace_ust_create_channel(attr
, domain
);
450 ret
= LTTNG_ERR_FATAL
;
455 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
456 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
459 uchan
->id
= trace_ust_get_next_chan_id(usess
);
461 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
462 uchan
->name
, type
, uchan
->id
);
464 /* Flag session buffer type. */
465 if (!usess
->buffer_type_changed
) {
466 usess
->buffer_type
= type
;
467 usess
->buffer_type_changed
= 1;
468 } else if (usess
->buffer_type
!= type
) {
469 /* Buffer type was already set. Refuse to create channel. */
470 ret
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
471 goto error_free_chan
;
474 /* Adding the channel to the channel hash table. */
476 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
477 sizeof(uchan
->name
))) {
478 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
479 chan_published
= true;
482 * Copy channel attribute to session if this is metadata so if NO
483 * application exists we can access that data in the shadow copy during
484 * the global update of newly registered application.
486 memcpy(&usess
->metadata_attr
, &uchan
->attr
,
487 sizeof(usess
->metadata_attr
));
491 DBG2("Channel %s created successfully", uchan
->name
);
492 if (domain
!= LTTNG_DOMAIN_UST
) {
493 struct agent
*agt
= trace_ust_find_agent(usess
, domain
);
496 agt
= agent_create(domain
);
498 ret
= LTTNG_ERR_NOMEM
;
499 goto error_remove_chan
;
501 agent_add(agt
, usess
->agents
);
505 channel_attr_destroy(defattr
);
509 if (chan_published
) {
510 trace_ust_delete_channel(usess
->domain_global
.channels
, uchan
);
513 trace_ust_destroy_channel(uchan
);
515 channel_attr_destroy(defattr
);
520 * Disable UST channel for session and domain.
522 int channel_ust_disable(struct ltt_ust_session
*usess
,
523 struct ltt_ust_channel
*uchan
)
530 /* Already disabled */
531 if (uchan
->enabled
== 0) {
532 DBG2("Channel UST %s already disabled", uchan
->name
);
535 if (!usess
->active
) {
539 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
540 /* Disable channel for global domain */
541 ret
= ust_app_disable_channel_glb(usess
, uchan
);
542 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
543 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
549 DBG2("Channel %s disabled successfully", uchan
->name
);