2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
11 #include "channel.hpp"
13 #include "lttng-sessiond.hpp"
14 #include "lttng-ust-ctl.hpp"
15 #include "lttng-ust-error.hpp"
16 #include "ust-app.hpp"
19 #include <common/common.hpp>
20 #include <common/defaults.hpp>
21 #include <common/sessiond-comm/sessiond-comm.hpp>
28 * Return allocated channel attributes.
30 struct lttng_channel
*channel_new_default_attr(int dom
, enum lttng_buffer_type type
)
32 struct lttng_channel
*chan
;
33 const char *channel_name
= DEFAULT_CHANNEL_NAME
;
34 struct lttng_channel_extended
*extended_attr
= nullptr;
36 chan
= zmalloc
<lttng_channel
>();
37 if (chan
== nullptr) {
38 PERROR("zmalloc channel init");
42 extended_attr
= zmalloc
<lttng_channel_extended
>();
44 PERROR("zmalloc channel extended init");
48 chan
->attr
.extended
.ptr
= extended_attr
;
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 LTTNG_ASSERT(type
== LTTNG_BUFFER_GLOBAL
);
58 chan
->attr
.subbuf_size
= default_get_kernel_channel_subbuf_size();
59 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
60 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
61 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
62 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
63 chan
->attr
.live_timer_interval
= DEFAULT_KERNEL_CHANNEL_LIVE_TIMER
;
64 extended_attr
->blocking_timeout
= DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
65 extended_attr
->monitor_timer_interval
= DEFAULT_KERNEL_CHANNEL_MONITOR_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_LOG4J2
:
74 channel_name
= DEFAULT_LOG4J2_CHANNEL_NAME
;
76 case LTTNG_DOMAIN_PYTHON
:
77 channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
79 case LTTNG_DOMAIN_UST
:
82 case LTTNG_BUFFER_PER_UID
:
83 chan
->attr
.subbuf_size
= default_get_ust_uid_channel_subbuf_size();
84 chan
->attr
.num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
85 chan
->attr
.output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
86 chan
->attr
.switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
87 chan
->attr
.read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
88 chan
->attr
.live_timer_interval
= DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
89 extended_attr
->blocking_timeout
= DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
90 extended_attr
->monitor_timer_interval
=
91 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
93 case LTTNG_BUFFER_PER_PID
:
95 chan
->attr
.subbuf_size
= default_get_ust_pid_channel_subbuf_size();
96 chan
->attr
.num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
97 chan
->attr
.output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
98 chan
->attr
.switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
99 chan
->attr
.read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
100 chan
->attr
.live_timer_interval
= DEFAULT_UST_PID_CHANNEL_LIVE_TIMER
;
101 extended_attr
->blocking_timeout
= DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
102 extended_attr
->monitor_timer_interval
=
103 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
108 goto error
; /* Not implemented */
111 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s", channel_name
) < 0) {
112 PERROR("snprintf default channel name");
124 void channel_attr_destroy(struct lttng_channel
*channel
)
129 free(channel
->attr
.extended
.ptr
);
134 * Disable kernel channel of the kernel session.
136 int channel_kernel_disable(struct ltt_kernel_session
*ksession
, char *channel_name
)
139 struct ltt_kernel_channel
*kchan
;
141 LTTNG_ASSERT(ksession
);
142 LTTNG_ASSERT(channel_name
);
144 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
145 if (kchan
== nullptr) {
146 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
150 /* Only if channel is enabled disable it. */
151 if (kchan
->enabled
) {
152 ret
= kernel_disable_channel(kchan
);
153 if (ret
< 0 && ret
!= -EEXIST
) {
154 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
166 * Enable kernel channel of the kernel session.
168 enum lttng_error_code
channel_kernel_enable(struct ltt_kernel_session
*ksession
,
169 struct ltt_kernel_channel
*kchan
)
171 enum lttng_error_code ret_code
;
173 LTTNG_ASSERT(ksession
);
176 if (!kchan
->enabled
) {
177 if (kernel_enable_channel(kchan
) < 0) {
178 ret_code
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
182 ret_code
= LTTNG_ERR_KERN_CHAN_EXIST
;
192 static int channel_validate(struct lttng_channel
*attr
)
195 * The ringbuffer (both in user space and kernel) behaves badly
196 * in overwrite mode and with less than 2 subbuffers so block it
197 * right away and send back an invalid attribute error.
199 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
205 static int channel_validate_kernel(struct lttng_channel
*attr
)
207 /* Kernel channels do not support blocking timeout. */
208 if (((struct lttng_channel_extended
*) attr
->attr
.extended
.ptr
)->blocking_timeout
) {
215 * Create kernel channel of the kernel session and notify kernel thread.
217 enum lttng_error_code
channel_kernel_create(struct ltt_kernel_session
*ksession
,
218 struct lttng_channel
*attr
,
221 enum lttng_error_code ret_code
;
222 struct lttng_channel
*defattr
= nullptr;
224 LTTNG_ASSERT(ksession
);
226 /* Creating channel attributes if needed */
227 if (attr
== nullptr) {
228 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
, LTTNG_BUFFER_GLOBAL
);
229 if (defattr
== nullptr) {
230 ret_code
= LTTNG_ERR_FATAL
;
237 * Set the overwrite mode for this channel based on the session
238 * type unless the client explicitly overrides the channel mode.
240 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
241 attr
->attr
.overwrite
= !!ksession
->snapshot_mode
;
244 /* Validate common channel properties. */
245 if (channel_validate(attr
) < 0) {
246 ret_code
= LTTNG_ERR_INVALID
;
250 if (channel_validate_kernel(attr
) < 0) {
251 ret_code
= LTTNG_ERR_INVALID
;
255 /* Channel not found, creating it. */
256 if (kernel_create_channel(ksession
, attr
) < 0) {
257 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
261 /* Notify kernel thread that there is a new channel */
262 if (notify_thread_pipe(kernel_pipe
) < 0) {
263 ret_code
= LTTNG_ERR_FATAL
;
269 channel_attr_destroy(defattr
);
274 * Enable UST channel for session and domain.
276 enum lttng_error_code
channel_ust_enable(struct ltt_ust_session
*usess
,
277 struct ltt_ust_channel
*uchan
)
279 enum lttng_error_code ret_code
= LTTNG_OK
;
284 /* If already enabled, everything is OK */
285 if (uchan
->enabled
) {
286 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
287 ret_code
= LTTNG_ERR_UST_CHAN_EXIST
;
290 uchan
->enabled
= true;
291 DBG2("Channel %s enabled successfully", uchan
->name
);
294 if (!usess
->active
) {
296 * The channel will be activated against the apps
297 * when the session is started as part of the
298 * application channel "synchronize" operation.
303 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
306 * Enable channel for UST global domain on all applications. Ignore return
307 * value here since whatever error we got, it means that the channel was
308 * not created on one or many registered applications and we can not report
309 * this to the user yet. However, at this stage, the channel was
310 * successfully created on the session daemon side so the enable-channel
311 * command is a success.
313 (void) ust_app_enable_channel_glb(usess
, uchan
);
320 * Create UST channel for session and domain.
322 enum lttng_error_code
channel_ust_create(struct ltt_ust_session
*usess
,
323 struct lttng_channel
*attr
,
324 enum lttng_buffer_type type
)
326 enum lttng_error_code ret_code
= LTTNG_OK
;
327 struct ltt_ust_channel
*uchan
= nullptr;
328 struct lttng_channel
*defattr
= nullptr;
329 enum lttng_domain_type domain
= LTTNG_DOMAIN_UST
;
330 bool chan_published
= false;
331 const lttng::urcu::read_lock_guard read_lock
;
335 /* Creating channel attributes if needed */
336 if (attr
== nullptr) {
337 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
338 if (defattr
== nullptr) {
339 ret_code
= LTTNG_ERR_FATAL
;
345 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
346 * based on the default name.
348 if (!strcmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
)) {
349 domain
= LTTNG_DOMAIN_JUL
;
350 } else if (!strcmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
351 domain
= LTTNG_DOMAIN_LOG4J
;
352 } else if (!strcmp(attr
->name
, DEFAULT_LOG4J2_CHANNEL_NAME
)) {
353 domain
= LTTNG_DOMAIN_LOG4J2
;
354 } else if (!strcmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
)) {
355 domain
= LTTNG_DOMAIN_PYTHON
;
360 * Set the overwrite mode for this channel based on the session
361 * type unless the client explicitly overrides the channel mode.
363 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
364 attr
->attr
.overwrite
= !!usess
->snapshot_mode
;
367 /* Enforce mmap output for snapshot sessions. */
368 if (usess
->snapshot_mode
) {
369 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
372 /* Validate common channel properties. */
373 if (channel_validate(attr
) < 0) {
374 ret_code
= LTTNG_ERR_INVALID
;
379 * Validate UST buffer size and number of buffers: must both be power of 2
380 * and nonzero. We validate right here for UST, because applications will
381 * not report the error to the user (unlike kernel tracing).
383 if (!attr
->attr
.subbuf_size
|| (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
384 ret_code
= LTTNG_ERR_INVALID
;
389 * Invalid subbuffer size if it's lower then the page size.
391 if (attr
->attr
.subbuf_size
< the_page_size
) {
392 ret_code
= LTTNG_ERR_INVALID
;
396 if (!attr
->attr
.num_subbuf
|| (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
397 ret_code
= LTTNG_ERR_INVALID
;
401 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
402 ret_code
= LTTNG_ERR_NOT_SUPPORTED
;
407 * The tracefile_size should not be < to the subbuf_size, otherwise
408 * we won't be able to write the packets on disk
410 if ((attr
->attr
.tracefile_size
> 0) &&
411 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
412 ret_code
= LTTNG_ERR_INVALID
;
416 /* Validate buffer type. */
418 case LTTNG_BUFFER_PER_PID
:
420 case LTTNG_BUFFER_PER_UID
:
423 ret_code
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
427 /* Create UST channel */
428 uchan
= trace_ust_create_channel(attr
, domain
);
429 if (uchan
== nullptr) {
430 ret_code
= LTTNG_ERR_FATAL
;
434 uchan
->enabled
= true;
435 if (trace_ust_is_max_id(usess
->used_event_container_id
)) {
436 ret_code
= LTTNG_ERR_UST_CHAN_FAIL
;
440 uchan
->id
= trace_ust_get_next_event_container_id(usess
);
442 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
447 /* Flag session buffer type. */
448 if (!usess
->buffer_type_changed
) {
449 usess
->buffer_type
= type
;
450 usess
->buffer_type_changed
= 1;
451 } else if (usess
->buffer_type
!= type
) {
452 /* Buffer type was already set. Refuse to create channel. */
453 ret_code
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
454 goto error_free_chan
;
457 /* Adding the channel to the channel hash table. */
458 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
, sizeof(uchan
->name
)) != 0) {
459 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
460 chan_published
= true;
463 * Copy channel attribute to session if this is metadata so if NO
464 * application exists we can access that data in the shadow copy during
465 * the global update of newly registered application.
467 memcpy(&usess
->metadata_attr
, &uchan
->attr
, sizeof(usess
->metadata_attr
));
470 DBG2("Channel %s created successfully", uchan
->name
);
471 if (domain
!= LTTNG_DOMAIN_UST
) {
472 struct agent
*agt
= trace_ust_find_agent(usess
, domain
);
475 agt
= agent_create(domain
);
477 ret_code
= LTTNG_ERR_NOMEM
;
478 goto error_remove_chan
;
480 agent_add(agt
, usess
->agents
);
484 channel_attr_destroy(defattr
);
488 if (chan_published
) {
489 trace_ust_delete_channel(usess
->domain_global
.channels
, uchan
);
492 trace_ust_destroy_channel(uchan
);
494 channel_attr_destroy(defattr
);
499 * Disable UST channel for session and domain.
501 int channel_ust_disable(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
508 /* Already disabled */
509 if (!uchan
->enabled
) {
510 DBG2("Channel UST %s already disabled", uchan
->name
);
514 uchan
->enabled
= false;
517 * If session is inactive we don't notify the tracer right away. We
518 * wait for the next synchronization.
520 if (!usess
->active
) {
524 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
525 /* Disable channel for global domain */
526 ret
= ust_app_disable_channel_glb(usess
, uchan
);
527 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
528 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
532 DBG2("Channel %s disabled successfully", uchan
->name
);
541 struct lttng_channel
*trace_ust_channel_to_lttng_channel(const struct ltt_ust_channel
*uchan
)
543 struct lttng_channel
*channel
= nullptr, *ret
= nullptr;
545 channel
= lttng_channel_create_internal();
547 ERR("Failed to create lttng_channel during conversion from ltt_ust_channel to lttng_channel");
551 if (lttng_strncpy(channel
->name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
552 ERR("Failed to set channel name during conversion from ltt_ust_channel to lttng_channel");
556 channel
->attr
.overwrite
= uchan
->attr
.overwrite
;
557 channel
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
558 channel
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
559 channel
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
560 channel
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
561 channel
->enabled
= uchan
->enabled
;
562 channel
->attr
.tracefile_size
= uchan
->tracefile_size
;
563 channel
->attr
.tracefile_count
= uchan
->tracefile_count
;
566 * Map enum lttng_ust_output to enum lttng_event_output.
568 switch (uchan
->attr
.output
) {
569 case LTTNG_UST_ABI_MMAP
:
570 channel
->attr
.output
= LTTNG_EVENT_MMAP
;
574 * LTTNG_UST_MMAP is the only supported UST
581 lttng_channel_set_blocking_timeout(channel
, uchan
->attr
.u
.s
.blocking_timeout
);
582 lttng_channel_set_monitor_timer_interval(channel
, uchan
->monitor_timer_interval
);
588 lttng_channel_destroy(channel
);