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
14 #include <common/common.h>
15 #include <common/defaults.h>
16 #include <common/sessiond-comm/sessiond-comm.h>
19 #include "lttng-sessiond.h"
21 #include "lttng-ust-ctl.h"
22 #include "lttng-ust-error.h"
28 * Return allocated channel attributes.
30 struct lttng_channel
*channel_new_default_attr(int dom
,
31 enum lttng_buffer_type type
)
33 struct lttng_channel
*chan
;
34 const char *channel_name
= DEFAULT_CHANNEL_NAME
;
35 struct lttng_channel_extended
*extended_attr
= NULL
;
37 chan
= (lttng_channel
*) zmalloc(sizeof(struct lttng_channel
));
39 PERROR("zmalloc channel init");
43 extended_attr
= (lttng_channel_extended
*) zmalloc(sizeof(struct lttng_channel_extended
));
45 PERROR("zmalloc channel extended init");
49 chan
->attr
.extended
.ptr
= extended_attr
;
51 /* Same for all domains. */
52 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
53 chan
->attr
.tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
54 chan
->attr
.tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
57 case LTTNG_DOMAIN_KERNEL
:
58 LTTNG_ASSERT(type
== LTTNG_BUFFER_GLOBAL
);
59 chan
->attr
.subbuf_size
=
60 default_get_kernel_channel_subbuf_size();
61 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
62 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
63 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
64 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
65 chan
->attr
.live_timer_interval
= DEFAULT_KERNEL_CHANNEL_LIVE_TIMER
;
66 extended_attr
->blocking_timeout
= DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
67 extended_attr
->monitor_timer_interval
=
68 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
70 case LTTNG_DOMAIN_JUL
:
71 channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
73 case LTTNG_DOMAIN_LOG4J
:
74 channel_name
= DEFAULT_LOG4J_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
=
87 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
88 chan
->attr
.read_timer_interval
=
89 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
90 chan
->attr
.live_timer_interval
=
91 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
92 extended_attr
->blocking_timeout
= DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
93 extended_attr
->monitor_timer_interval
=
94 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
96 case LTTNG_BUFFER_PER_PID
:
98 chan
->attr
.subbuf_size
= default_get_ust_pid_channel_subbuf_size();
99 chan
->attr
.num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
100 chan
->attr
.output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
101 chan
->attr
.switch_timer_interval
=
102 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
103 chan
->attr
.read_timer_interval
=
104 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
105 chan
->attr
.live_timer_interval
=
106 DEFAULT_UST_PID_CHANNEL_LIVE_TIMER
;
107 extended_attr
->blocking_timeout
= DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
108 extended_attr
->monitor_timer_interval
=
109 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
114 goto error
; /* Not implemented */
117 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s",
119 PERROR("snprintf default channel name");
131 void channel_attr_destroy(struct lttng_channel
*channel
)
136 free(channel
->attr
.extended
.ptr
);
141 * Disable kernel channel of the kernel session.
143 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
147 struct ltt_kernel_channel
*kchan
;
149 LTTNG_ASSERT(ksession
);
150 LTTNG_ASSERT(channel_name
);
152 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
154 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
158 /* Only if channel is enabled disable it. */
159 if (kchan
->enabled
== 1) {
160 ret
= kernel_disable_channel(kchan
);
161 if (ret
< 0 && ret
!= -EEXIST
) {
162 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
174 * Enable kernel channel of the kernel session.
176 enum lttng_error_code
channel_kernel_enable(struct ltt_kernel_session
*ksession
,
177 struct ltt_kernel_channel
*kchan
)
179 enum lttng_error_code ret_code
;
181 LTTNG_ASSERT(ksession
);
184 if (kchan
->enabled
== 0) {
185 if (kernel_enable_channel(kchan
) < 0) {
186 ret_code
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
190 ret_code
= LTTNG_ERR_KERN_CHAN_EXIST
;
200 static int channel_validate(struct lttng_channel
*attr
)
203 * The ringbuffer (both in user space and kernel) behaves badly
204 * in overwrite mode and with less than 2 subbuffers so block it
205 * right away and send back an invalid attribute error.
207 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
213 static int channel_validate_kernel(struct lttng_channel
*attr
)
215 /* Kernel channels do not support blocking timeout. */
216 if (((struct lttng_channel_extended
*)attr
->attr
.extended
.ptr
)->blocking_timeout
) {
223 * Create kernel channel of the kernel session and notify kernel thread.
225 enum lttng_error_code
channel_kernel_create(struct ltt_kernel_session
*ksession
,
226 struct lttng_channel
*attr
, int kernel_pipe
)
228 enum lttng_error_code ret_code
;
229 struct lttng_channel
*defattr
= NULL
;
231 LTTNG_ASSERT(ksession
);
233 /* Creating channel attributes if needed */
235 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
236 LTTNG_BUFFER_GLOBAL
);
237 if (defattr
== NULL
) {
238 ret_code
= LTTNG_ERR_FATAL
;
245 * Set the overwrite mode for this channel based on the session
246 * type unless the client explicitly overrides the channel mode.
248 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
249 attr
->attr
.overwrite
= !!ksession
->snapshot_mode
;
252 /* Validate common channel properties. */
253 if (channel_validate(attr
) < 0) {
254 ret_code
= LTTNG_ERR_INVALID
;
258 if (channel_validate_kernel(attr
) < 0) {
259 ret_code
= LTTNG_ERR_INVALID
;
263 /* Channel not found, creating it. */
264 if (kernel_create_channel(ksession
, attr
) < 0) {
265 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
269 /* Notify kernel thread that there is a new channel */
270 if (notify_thread_pipe(kernel_pipe
) < 0) {
271 ret_code
= LTTNG_ERR_FATAL
;
277 channel_attr_destroy(defattr
);
282 * Enable UST channel for session and domain.
284 enum lttng_error_code
channel_ust_enable(struct ltt_ust_session
*usess
,
285 struct ltt_ust_channel
*uchan
)
287 enum lttng_error_code ret_code
= LTTNG_OK
;
292 /* If already enabled, everything is OK */
293 if (uchan
->enabled
) {
294 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
295 ret_code
= LTTNG_ERR_UST_CHAN_EXIST
;
299 DBG2("Channel %s enabled successfully", uchan
->name
);
302 if (!usess
->active
) {
304 * The channel will be activated against the apps
305 * when the session is started as part of the
306 * application channel "synchronize" operation.
311 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
314 * Enable channel for UST global domain on all applications. Ignore return
315 * value here since whatever error we got, it means that the channel was
316 * not created on one or many registered applications and we can not report
317 * this to the user yet. However, at this stage, the channel was
318 * successfully created on the session daemon side so the enable-channel
319 * command is a success.
321 (void) ust_app_enable_channel_glb(usess
, uchan
);
329 * Create UST channel for session and domain.
331 enum lttng_error_code
channel_ust_create(struct ltt_ust_session
*usess
,
332 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
334 enum lttng_error_code ret_code
= LTTNG_OK
;
335 struct ltt_ust_channel
*uchan
= NULL
;
336 struct lttng_channel
*defattr
= NULL
;
337 enum lttng_domain_type domain
= LTTNG_DOMAIN_UST
;
338 bool chan_published
= false;
342 /* Creating channel attributes if needed */
344 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
345 if (defattr
== NULL
) {
346 ret_code
= LTTNG_ERR_FATAL
;
352 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
353 * based on the default name.
355 if (!strcmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
)) {
356 domain
= LTTNG_DOMAIN_JUL
;
357 } else if (!strcmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
358 domain
= LTTNG_DOMAIN_LOG4J
;
359 } else if (!strcmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
)) {
360 domain
= LTTNG_DOMAIN_PYTHON
;
365 * Set the overwrite mode for this channel based on the session
366 * type unless the client explicitly overrides the channel mode.
368 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
369 attr
->attr
.overwrite
= !!usess
->snapshot_mode
;
372 /* Enforce mmap output for snapshot sessions. */
373 if (usess
->snapshot_mode
) {
374 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
377 /* Validate common channel properties. */
378 if (channel_validate(attr
) < 0) {
379 ret_code
= LTTNG_ERR_INVALID
;
384 * Validate UST buffer size and number of buffers: must both be power of 2
385 * and nonzero. We validate right here for UST, because applications will
386 * not report the error to the user (unlike kernel tracing).
388 if (!attr
->attr
.subbuf_size
||
389 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
390 ret_code
= LTTNG_ERR_INVALID
;
395 * Invalid subbuffer size if it's lower then the page size.
397 if (attr
->attr
.subbuf_size
< the_page_size
) {
398 ret_code
= LTTNG_ERR_INVALID
;
402 if (!attr
->attr
.num_subbuf
||
403 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
404 ret_code
= LTTNG_ERR_INVALID
;
408 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
409 ret_code
= LTTNG_ERR_NOT_SUPPORTED
;
414 * The tracefile_size should not be < to the subbuf_size, otherwise
415 * we won't be able to write the packets on disk
417 if ((attr
->attr
.tracefile_size
> 0) &&
418 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
419 ret_code
= LTTNG_ERR_INVALID
;
423 /* Validate buffer type. */
425 case LTTNG_BUFFER_PER_PID
:
427 case LTTNG_BUFFER_PER_UID
:
430 ret_code
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
434 /* Create UST channel */
435 uchan
= trace_ust_create_channel(attr
, domain
);
437 ret_code
= LTTNG_ERR_FATAL
;
442 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
443 ret_code
= LTTNG_ERR_UST_CHAN_FAIL
;
446 uchan
->id
= trace_ust_get_next_chan_id(usess
);
448 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
449 uchan
->name
, type
, uchan
->id
);
451 /* Flag session buffer type. */
452 if (!usess
->buffer_type_changed
) {
453 usess
->buffer_type
= type
;
454 usess
->buffer_type_changed
= 1;
455 } else if (usess
->buffer_type
!= type
) {
456 /* Buffer type was already set. Refuse to create channel. */
457 ret_code
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
458 goto error_free_chan
;
461 /* Adding the channel to the channel hash table. */
463 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
464 sizeof(uchan
->name
))) {
465 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
466 chan_published
= true;
469 * Copy channel attribute to session if this is metadata so if NO
470 * application exists we can access that data in the shadow copy during
471 * the global update of newly registered application.
473 memcpy(&usess
->metadata_attr
, &uchan
->attr
,
474 sizeof(usess
->metadata_attr
));
478 DBG2("Channel %s created successfully", uchan
->name
);
479 if (domain
!= LTTNG_DOMAIN_UST
) {
480 struct agent
*agt
= trace_ust_find_agent(usess
, domain
);
483 agt
= agent_create(domain
);
485 ret_code
= LTTNG_ERR_NOMEM
;
486 goto error_remove_chan
;
488 agent_add(agt
, usess
->agents
);
492 channel_attr_destroy(defattr
);
496 if (chan_published
) {
497 trace_ust_delete_channel(usess
->domain_global
.channels
, uchan
);
500 trace_ust_destroy_channel(uchan
);
502 channel_attr_destroy(defattr
);
507 * Disable UST channel for session and domain.
509 int channel_ust_disable(struct ltt_ust_session
*usess
,
510 struct ltt_ust_channel
*uchan
)
517 /* Already disabled */
518 if (uchan
->enabled
== 0) {
519 DBG2("Channel UST %s already disabled", uchan
->name
);
526 * If session is inactive we don't notify the tracer right away. We
527 * wait for the next synchronization.
529 if (!usess
->active
) {
533 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
534 /* Disable channel for global domain */
535 ret
= ust_app_disable_channel_glb(usess
, uchan
);
536 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
537 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
541 DBG2("Channel %s disabled successfully", uchan
->name
);
550 struct lttng_channel
*trace_ust_channel_to_lttng_channel(
551 const struct ltt_ust_channel
*uchan
)
553 struct lttng_channel
*channel
= NULL
, *ret
= NULL
;
555 channel
= lttng_channel_create_internal();
557 ERR("Failed to create lttng_channel during conversion from ltt_ust_channel to lttng_channel");
561 if (lttng_strncpy(channel
->name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
562 ERR("Failed to set channel name during conversion from ltt_ust_channel to lttng_channel");
566 channel
->attr
.overwrite
= uchan
->attr
.overwrite
;
567 channel
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
568 channel
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
569 channel
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
570 channel
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
571 channel
->enabled
= uchan
->enabled
;
572 channel
->attr
.tracefile_size
= uchan
->tracefile_size
;
573 channel
->attr
.tracefile_count
= uchan
->tracefile_count
;
576 * Map enum lttng_ust_output to enum lttng_event_output.
578 switch (uchan
->attr
.output
) {
579 case LTTNG_UST_ABI_MMAP
:
580 channel
->attr
.output
= LTTNG_EVENT_MMAP
;
584 * LTTNG_UST_MMAP is the only supported UST
591 lttng_channel_set_blocking_timeout(
592 channel
, uchan
->attr
.u
.s
.blocking_timeout
);
593 lttng_channel_set_monitor_timer_interval(
594 channel
, uchan
->monitor_timer_interval
);
600 lttng_channel_destroy(channel
);