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
= NULL
;
36 chan
= zmalloc
<lttng_channel
>();
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_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
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
84 chan
->attr
.read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
85 chan
->attr
.live_timer_interval
= DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
86 extended_attr
->blocking_timeout
= DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
87 extended_attr
->monitor_timer_interval
=
88 DEFAULT_UST_UID_CHANNEL_MONITOR_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
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
96 chan
->attr
.read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
97 chan
->attr
.live_timer_interval
= DEFAULT_UST_PID_CHANNEL_LIVE_TIMER
;
98 extended_attr
->blocking_timeout
= DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
99 extended_attr
->monitor_timer_interval
=
100 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
105 goto error
; /* Not implemented */
108 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s", channel_name
) < 0) {
109 PERROR("snprintf default channel name");
121 void channel_attr_destroy(struct lttng_channel
*channel
)
126 free(channel
->attr
.extended
.ptr
);
131 * Disable kernel channel of the kernel session.
133 int channel_kernel_disable(struct ltt_kernel_session
*ksession
, char *channel_name
)
136 struct ltt_kernel_channel
*kchan
;
138 LTTNG_ASSERT(ksession
);
139 LTTNG_ASSERT(channel_name
);
141 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
143 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
147 /* Only if channel is enabled disable it. */
148 if (kchan
->enabled
== 1) {
149 ret
= kernel_disable_channel(kchan
);
150 if (ret
< 0 && ret
!= -EEXIST
) {
151 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
163 * Enable kernel channel of the kernel session.
165 enum lttng_error_code
channel_kernel_enable(struct ltt_kernel_session
*ksession
,
166 struct ltt_kernel_channel
*kchan
)
168 enum lttng_error_code ret_code
;
170 LTTNG_ASSERT(ksession
);
173 if (kchan
->enabled
== 0) {
174 if (kernel_enable_channel(kchan
) < 0) {
175 ret_code
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
179 ret_code
= LTTNG_ERR_KERN_CHAN_EXIST
;
189 static int channel_validate(struct lttng_channel
*attr
)
192 * The ringbuffer (both in user space and kernel) behaves badly
193 * in overwrite mode and with less than 2 subbuffers so block it
194 * right away and send back an invalid attribute error.
196 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
202 static int channel_validate_kernel(struct lttng_channel
*attr
)
204 /* Kernel channels do not support blocking timeout. */
205 if (((struct lttng_channel_extended
*) attr
->attr
.extended
.ptr
)->blocking_timeout
) {
212 * Create kernel channel of the kernel session and notify kernel thread.
214 enum lttng_error_code
channel_kernel_create(struct ltt_kernel_session
*ksession
,
215 struct lttng_channel
*attr
,
218 enum lttng_error_code ret_code
;
219 struct lttng_channel
*defattr
= NULL
;
221 LTTNG_ASSERT(ksession
);
223 /* Creating channel attributes if needed */
225 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
, LTTNG_BUFFER_GLOBAL
);
226 if (defattr
== NULL
) {
227 ret_code
= LTTNG_ERR_FATAL
;
234 * Set the overwrite mode for this channel based on the session
235 * type unless the client explicitly overrides the channel mode.
237 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
238 attr
->attr
.overwrite
= !!ksession
->snapshot_mode
;
241 /* Validate common channel properties. */
242 if (channel_validate(attr
) < 0) {
243 ret_code
= LTTNG_ERR_INVALID
;
247 if (channel_validate_kernel(attr
) < 0) {
248 ret_code
= LTTNG_ERR_INVALID
;
252 /* Channel not found, creating it. */
253 if (kernel_create_channel(ksession
, attr
) < 0) {
254 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
258 /* Notify kernel thread that there is a new channel */
259 if (notify_thread_pipe(kernel_pipe
) < 0) {
260 ret_code
= LTTNG_ERR_FATAL
;
266 channel_attr_destroy(defattr
);
271 * Enable UST channel for session and domain.
273 enum lttng_error_code
channel_ust_enable(struct ltt_ust_session
*usess
,
274 struct ltt_ust_channel
*uchan
)
276 enum lttng_error_code ret_code
= LTTNG_OK
;
281 /* If already enabled, everything is OK */
282 if (uchan
->enabled
) {
283 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
284 ret_code
= LTTNG_ERR_UST_CHAN_EXIST
;
288 DBG2("Channel %s enabled successfully", uchan
->name
);
291 if (!usess
->active
) {
293 * The channel will be activated against the apps
294 * when the session is started as part of the
295 * application channel "synchronize" operation.
300 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
303 * Enable channel for UST global domain on all applications. Ignore return
304 * value here since whatever error we got, it means that the channel was
305 * not created on one or many registered applications and we can not report
306 * this to the user yet. However, at this stage, the channel was
307 * successfully created on the session daemon side so the enable-channel
308 * command is a success.
310 (void) ust_app_enable_channel_glb(usess
, uchan
);
317 * Create UST channel for session and domain.
319 enum lttng_error_code
channel_ust_create(struct ltt_ust_session
*usess
,
320 struct lttng_channel
*attr
,
321 enum lttng_buffer_type type
)
323 enum lttng_error_code ret_code
= LTTNG_OK
;
324 struct ltt_ust_channel
*uchan
= NULL
;
325 struct lttng_channel
*defattr
= NULL
;
326 enum lttng_domain_type domain
= LTTNG_DOMAIN_UST
;
327 bool chan_published
= false;
331 /* Creating channel attributes if needed */
333 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
334 if (defattr
== NULL
) {
335 ret_code
= 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_code
= 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
|| (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
378 ret_code
= LTTNG_ERR_INVALID
;
383 * Invalid subbuffer size if it's lower then the page size.
385 if (attr
->attr
.subbuf_size
< the_page_size
) {
386 ret_code
= LTTNG_ERR_INVALID
;
390 if (!attr
->attr
.num_subbuf
|| (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
391 ret_code
= LTTNG_ERR_INVALID
;
395 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
396 ret_code
= LTTNG_ERR_NOT_SUPPORTED
;
401 * The tracefile_size should not be < to the subbuf_size, otherwise
402 * we won't be able to write the packets on disk
404 if ((attr
->attr
.tracefile_size
> 0) &&
405 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
406 ret_code
= LTTNG_ERR_INVALID
;
410 /* Validate buffer type. */
412 case LTTNG_BUFFER_PER_PID
:
414 case LTTNG_BUFFER_PER_UID
:
417 ret_code
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
421 /* Create UST channel */
422 uchan
= trace_ust_create_channel(attr
, domain
);
424 ret_code
= LTTNG_ERR_FATAL
;
429 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
430 ret_code
= LTTNG_ERR_UST_CHAN_FAIL
;
433 uchan
->id
= trace_ust_get_next_chan_id(usess
);
435 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
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_code
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
447 goto error_free_chan
;
450 /* Adding the channel to the channel hash table. */
452 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
, sizeof(uchan
->name
))) {
453 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
454 chan_published
= true;
457 * Copy channel attribute to session if this is metadata so if NO
458 * application exists we can access that data in the shadow copy during
459 * the global update of newly registered application.
461 memcpy(&usess
->metadata_attr
, &uchan
->attr
, sizeof(usess
->metadata_attr
));
465 DBG2("Channel %s created successfully", uchan
->name
);
466 if (domain
!= LTTNG_DOMAIN_UST
) {
467 struct agent
*agt
= trace_ust_find_agent(usess
, domain
);
470 agt
= agent_create(domain
);
472 ret_code
= LTTNG_ERR_NOMEM
;
473 goto error_remove_chan
;
475 agent_add(agt
, usess
->agents
);
479 channel_attr_destroy(defattr
);
483 if (chan_published
) {
484 trace_ust_delete_channel(usess
->domain_global
.channels
, uchan
);
487 trace_ust_destroy_channel(uchan
);
489 channel_attr_destroy(defattr
);
494 * Disable UST channel for session and domain.
496 int channel_ust_disable(struct ltt_ust_session
*usess
, struct ltt_ust_channel
*uchan
)
503 /* Already disabled */
504 if (uchan
->enabled
== 0) {
505 DBG2("Channel UST %s already disabled", uchan
->name
);
512 * If session is inactive we don't notify the tracer right away. We
513 * wait for the next synchronization.
515 if (!usess
->active
) {
519 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
520 /* Disable channel for global domain */
521 ret
= ust_app_disable_channel_glb(usess
, uchan
);
522 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
523 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
527 DBG2("Channel %s disabled successfully", uchan
->name
);
536 struct lttng_channel
*trace_ust_channel_to_lttng_channel(const struct ltt_ust_channel
*uchan
)
538 struct lttng_channel
*channel
= NULL
, *ret
= NULL
;
540 channel
= lttng_channel_create_internal();
542 ERR("Failed to create lttng_channel during conversion from ltt_ust_channel to lttng_channel");
546 if (lttng_strncpy(channel
->name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
547 ERR("Failed to set channel name during conversion from ltt_ust_channel to lttng_channel");
551 channel
->attr
.overwrite
= uchan
->attr
.overwrite
;
552 channel
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
553 channel
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
554 channel
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
555 channel
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
556 channel
->enabled
= uchan
->enabled
;
557 channel
->attr
.tracefile_size
= uchan
->tracefile_size
;
558 channel
->attr
.tracefile_count
= uchan
->tracefile_count
;
561 * Map enum lttng_ust_output to enum lttng_event_output.
563 switch (uchan
->attr
.output
) {
564 case LTTNG_UST_ABI_MMAP
:
565 channel
->attr
.output
= LTTNG_EVENT_MMAP
;
569 * LTTNG_UST_MMAP is the only supported UST
576 lttng_channel_set_blocking_timeout(channel
, uchan
->attr
.u
.s
.blocking_timeout
);
577 lttng_channel_set_monitor_timer_interval(channel
, uchan
->monitor_timer_interval
);
583 lttng_channel_destroy(channel
);