2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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
= zmalloc(sizeof(struct lttng_channel
));
39 PERROR("zmalloc channel init");
43 extended_attr
= 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 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
;
150 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 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
177 struct ltt_kernel_channel
*kchan
)
184 if (kchan
->enabled
== 0) {
185 ret
= kernel_enable_channel(kchan
);
187 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
191 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
201 static int channel_validate(struct lttng_channel
*attr
)
204 * The ringbuffer (both in user space and kernel) behaves badly
205 * in overwrite mode and with less than 2 subbuffers so block it
206 * right away and send back an invalid attribute error.
208 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
214 static int channel_validate_kernel(struct lttng_channel
*attr
)
216 /* Kernel channels do not support blocking timeout. */
217 if (((struct lttng_channel_extended
*)attr
->attr
.extended
.ptr
)->blocking_timeout
) {
224 * Create kernel channel of the kernel session and notify kernel thread.
226 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
227 struct lttng_channel
*attr
, int kernel_pipe
)
230 struct lttng_channel
*defattr
= NULL
;
234 /* Creating channel attributes if needed */
236 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
237 LTTNG_BUFFER_GLOBAL
);
238 if (defattr
== NULL
) {
239 ret
= LTTNG_ERR_FATAL
;
246 * Set the overwrite mode for this channel based on the session
247 * type unless the client explicitly overrides the channel mode.
249 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
250 attr
->attr
.overwrite
= !!ksession
->snapshot_mode
;
253 /* Validate common channel properties. */
254 if (channel_validate(attr
) < 0) {
255 ret
= LTTNG_ERR_INVALID
;
259 if (channel_validate_kernel(attr
) < 0) {
260 ret
= LTTNG_ERR_INVALID
;
264 /* Channel not found, creating it */
265 ret
= kernel_create_channel(ksession
, attr
);
267 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
271 /* Notify kernel thread that there is a new channel */
272 ret
= notify_thread_pipe(kernel_pipe
);
274 ret
= LTTNG_ERR_FATAL
;
280 channel_attr_destroy(defattr
);
285 * Enable UST channel for session and domain.
287 int channel_ust_enable(struct ltt_ust_session
*usess
,
288 struct ltt_ust_channel
*uchan
)
295 /* If already enabled, everything is OK */
296 if (uchan
->enabled
) {
297 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
298 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
302 DBG2("Channel %s enabled successfully", uchan
->name
);
305 if (!usess
->active
) {
307 * The channel will be activated against the apps
308 * when the session is started as part of the
309 * application channel "synchronize" operation.
314 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
317 * Enable channel for UST global domain on all applications. Ignore return
318 * value here since whatever error we got, it means that the channel was
319 * not created on one or many registered applications and we can not report
320 * this to the user yet. However, at this stage, the channel was
321 * successfully created on the session daemon side so the enable-channel
322 * command is a success.
324 (void) ust_app_enable_channel_glb(usess
, uchan
);
332 * Create UST channel for session and domain.
334 int channel_ust_create(struct ltt_ust_session
*usess
,
335 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
338 struct ltt_ust_channel
*uchan
= NULL
;
339 struct lttng_channel
*defattr
= NULL
;
340 enum lttng_domain_type domain
= LTTNG_DOMAIN_UST
;
341 bool chan_published
= false;
345 /* Creating channel attributes if needed */
347 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
348 if (defattr
== NULL
) {
349 ret
= LTTNG_ERR_FATAL
;
355 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
356 * based on the default name.
358 if (!strcmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
)) {
359 domain
= LTTNG_DOMAIN_JUL
;
360 } else if (!strcmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
361 domain
= LTTNG_DOMAIN_LOG4J
;
362 } else if (!strcmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
)) {
363 domain
= LTTNG_DOMAIN_PYTHON
;
368 * Set the overwrite mode for this channel based on the session
369 * type unless the client explicitly overrides the channel mode.
371 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
372 attr
->attr
.overwrite
= !!usess
->snapshot_mode
;
375 /* Enforce mmap output for snapshot sessions. */
376 if (usess
->snapshot_mode
) {
377 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
380 /* Validate common channel properties. */
381 if (channel_validate(attr
) < 0) {
382 ret
= LTTNG_ERR_INVALID
;
387 * Validate UST buffer size and number of buffers: must both be power of 2
388 * and nonzero. We validate right here for UST, because applications will
389 * not report the error to the user (unlike kernel tracing).
391 if (!attr
->attr
.subbuf_size
||
392 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
393 ret
= LTTNG_ERR_INVALID
;
398 * Invalid subbuffer size if it's lower then the page size.
400 if (attr
->attr
.subbuf_size
< the_page_size
) {
401 ret
= LTTNG_ERR_INVALID
;
405 if (!attr
->attr
.num_subbuf
||
406 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
407 ret
= LTTNG_ERR_INVALID
;
411 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
412 ret
= LTTNG_ERR_NOT_SUPPORTED
;
417 * The tracefile_size should not be < to the subbuf_size, otherwise
418 * we won't be able to write the packets on disk
420 if ((attr
->attr
.tracefile_size
> 0) &&
421 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
422 ret
= LTTNG_ERR_INVALID
;
426 /* Validate buffer type. */
428 case LTTNG_BUFFER_PER_PID
:
430 case LTTNG_BUFFER_PER_UID
:
433 ret
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
437 /* Create UST channel */
438 uchan
= trace_ust_create_channel(attr
, domain
);
440 ret
= LTTNG_ERR_FATAL
;
445 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
446 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
449 uchan
->id
= trace_ust_get_next_chan_id(usess
);
451 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
452 uchan
->name
, type
, uchan
->id
);
454 /* Flag session buffer type. */
455 if (!usess
->buffer_type_changed
) {
456 usess
->buffer_type
= type
;
457 usess
->buffer_type_changed
= 1;
458 } else if (usess
->buffer_type
!= type
) {
459 /* Buffer type was already set. Refuse to create channel. */
460 ret
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
461 goto error_free_chan
;
464 /* Adding the channel to the channel hash table. */
466 if (strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
467 sizeof(uchan
->name
))) {
468 lttng_ht_add_unique_str(usess
->domain_global
.channels
, &uchan
->node
);
469 chan_published
= true;
472 * Copy channel attribute to session if this is metadata so if NO
473 * application exists we can access that data in the shadow copy during
474 * the global update of newly registered application.
476 memcpy(&usess
->metadata_attr
, &uchan
->attr
,
477 sizeof(usess
->metadata_attr
));
481 DBG2("Channel %s created successfully", uchan
->name
);
482 if (domain
!= LTTNG_DOMAIN_UST
) {
483 struct agent
*agt
= trace_ust_find_agent(usess
, domain
);
486 agt
= agent_create(domain
);
488 ret
= LTTNG_ERR_NOMEM
;
489 goto error_remove_chan
;
491 agent_add(agt
, usess
->agents
);
495 channel_attr_destroy(defattr
);
499 if (chan_published
) {
500 trace_ust_delete_channel(usess
->domain_global
.channels
, uchan
);
503 trace_ust_destroy_channel(uchan
);
505 channel_attr_destroy(defattr
);
510 * Disable UST channel for session and domain.
512 int channel_ust_disable(struct ltt_ust_session
*usess
,
513 struct ltt_ust_channel
*uchan
)
520 /* Already disabled */
521 if (uchan
->enabled
== 0) {
522 DBG2("Channel UST %s already disabled", uchan
->name
);
525 if (!usess
->active
) {
529 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
530 /* Disable channel for global domain */
531 ret
= ust_app_disable_channel_glb(usess
, uchan
);
532 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
533 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
539 DBG2("Channel %s disabled successfully", uchan
->name
);