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"
37 * Return allocated channel attributes.
39 struct lttng_channel
*channel_new_default_attr(int dom
,
40 enum lttng_buffer_type type
)
42 struct lttng_channel
*chan
;
43 const char *channel_name
= DEFAULT_CHANNEL_NAME
;
44 struct lttng_channel_extended
*extended_attr
= NULL
;
46 chan
= zmalloc(sizeof(struct lttng_channel
));
48 PERROR("zmalloc channel init");
52 extended_attr
= zmalloc(sizeof(struct lttng_channel_extended
));
54 PERROR("zmalloc channel extended init");
58 chan
->attr
.extended
.ptr
= extended_attr
;
60 /* Same for all domains. */
61 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
62 chan
->attr
.tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
63 chan
->attr
.tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
66 case LTTNG_DOMAIN_KERNEL
:
67 assert(type
== LTTNG_BUFFER_GLOBAL
);
68 chan
->attr
.subbuf_size
=
69 default_get_kernel_channel_subbuf_size();
70 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
71 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
72 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
73 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
74 chan
->attr
.live_timer_interval
= DEFAULT_KERNEL_CHANNEL_LIVE_TIMER
;
75 extended_attr
->blocking_timeout
= DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
76 extended_attr
->monitor_timer_interval
=
77 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
79 case LTTNG_DOMAIN_JUL
:
80 channel_name
= DEFAULT_JUL_CHANNEL_NAME
;
82 case LTTNG_DOMAIN_LOG4J
:
83 channel_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
85 case LTTNG_DOMAIN_PYTHON
:
86 channel_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
88 case LTTNG_DOMAIN_UST
:
91 case LTTNG_BUFFER_PER_UID
:
92 chan
->attr
.subbuf_size
= default_get_ust_uid_channel_subbuf_size();
93 chan
->attr
.num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
94 chan
->attr
.output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
95 chan
->attr
.switch_timer_interval
=
96 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
97 chan
->attr
.read_timer_interval
=
98 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
99 chan
->attr
.live_timer_interval
=
100 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER
;
101 extended_attr
->blocking_timeout
= DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
102 extended_attr
->monitor_timer_interval
=
103 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
105 case LTTNG_BUFFER_PER_PID
:
107 chan
->attr
.subbuf_size
= default_get_ust_pid_channel_subbuf_size();
108 chan
->attr
.num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
109 chan
->attr
.output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
110 chan
->attr
.switch_timer_interval
=
111 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
112 chan
->attr
.read_timer_interval
=
113 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
114 chan
->attr
.live_timer_interval
=
115 DEFAULT_UST_PID_CHANNEL_LIVE_TIMER
;
116 extended_attr
->blocking_timeout
= DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
117 extended_attr
->monitor_timer_interval
=
118 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
123 goto error
; /* Not implemented */
126 if (snprintf(chan
->name
, sizeof(chan
->name
), "%s",
128 PERROR("snprintf default channel name");
140 void channel_attr_destroy(struct lttng_channel
*channel
)
145 free(channel
->attr
.extended
.ptr
);
150 * Disable kernel channel of the kernel session.
152 int channel_kernel_disable(struct ltt_kernel_session
*ksession
,
156 struct ltt_kernel_channel
*kchan
;
159 assert(channel_name
);
161 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
163 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
167 /* Only if channel is enabled disable it. */
168 if (kchan
->enabled
== 1) {
169 ret
= kernel_disable_channel(kchan
);
170 if (ret
< 0 && ret
!= -EEXIST
) {
171 ret
= LTTNG_ERR_KERN_CHAN_DISABLE_FAIL
;
183 * Enable kernel channel of the kernel session.
185 int channel_kernel_enable(struct ltt_kernel_session
*ksession
,
186 struct ltt_kernel_channel
*kchan
)
193 if (kchan
->enabled
== 0) {
194 ret
= kernel_enable_channel(kchan
);
196 ret
= LTTNG_ERR_KERN_CHAN_ENABLE_FAIL
;
200 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
210 static int channel_validate(struct lttng_channel
*attr
)
213 * The ringbuffer (both in user space and kernel) behaves badly
214 * in overwrite mode and with less than 2 subbuffers so block it
215 * right away and send back an invalid attribute error.
217 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
223 static int channel_validate_kernel(struct lttng_channel
*attr
)
225 /* Kernel channels do not support blocking timeout. */
226 if (((struct lttng_channel_extended
*)attr
->attr
.extended
.ptr
)->blocking_timeout
) {
233 * Create kernel channel of the kernel session and notify kernel thread.
235 int channel_kernel_create(struct ltt_kernel_session
*ksession
,
236 struct lttng_channel
*attr
, int kernel_pipe
)
239 struct lttng_channel
*defattr
= NULL
;
243 /* Creating channel attributes if needed */
245 defattr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
246 LTTNG_BUFFER_GLOBAL
);
247 if (defattr
== NULL
) {
248 ret
= LTTNG_ERR_FATAL
;
255 * Set the overwrite mode for this channel based on the session
256 * type unless the client explicitly overrides the channel mode.
258 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
259 attr
->attr
.overwrite
= !!ksession
->snapshot_mode
;
262 /* Enforce mmap output for snapshot sessions. */
263 if (ksession
->snapshot_mode
) {
264 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
267 /* Validate common channel properties. */
268 if (channel_validate(attr
) < 0) {
269 ret
= LTTNG_ERR_INVALID
;
273 if (channel_validate_kernel(attr
) < 0) {
274 ret
= LTTNG_ERR_INVALID
;
278 /* Channel not found, creating it */
279 ret
= kernel_create_channel(ksession
, attr
);
281 ret
= LTTNG_ERR_KERN_CHAN_FAIL
;
285 /* Notify kernel thread that there is a new channel */
286 ret
= notify_thread_pipe(kernel_pipe
);
288 ret
= LTTNG_ERR_FATAL
;
294 channel_attr_destroy(defattr
);
299 * Enable UST channel for session and domain.
301 int channel_ust_enable(struct ltt_ust_session
*usess
,
302 struct ltt_ust_channel
*uchan
)
309 /* If already enabled, everything is OK */
310 if (uchan
->enabled
) {
311 DBG3("Channel %s already enabled. Skipping", uchan
->name
);
312 ret
= LTTNG_ERR_UST_CHAN_EXIST
;
316 DBG2("Channel %s being enabled in UST domain", uchan
->name
);
319 * Enable channel for UST global domain on all applications. Ignore return
320 * value here since whatever error we got, it means that the channel was
321 * not created on one or many registered applications and we can not report
322 * this to the user yet. However, at this stage, the channel was
323 * successfully created on the session daemon side so the enable-channel
324 * command is a success.
326 (void) ust_app_enable_channel_glb(usess
, uchan
);
329 DBG2("Channel %s enabled successfully", uchan
->name
);
336 * Create UST channel for session and domain.
338 int channel_ust_create(struct ltt_ust_session
*usess
,
339 struct lttng_channel
*attr
, enum lttng_buffer_type type
)
342 struct ltt_ust_channel
*uchan
= NULL
;
343 struct lttng_channel
*defattr
= NULL
;
344 enum lttng_domain_type domain
= LTTNG_DOMAIN_UST
;
348 /* Creating channel attributes if needed */
350 defattr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, type
);
351 if (defattr
== NULL
) {
352 ret
= LTTNG_ERR_FATAL
;
358 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
359 * based on the default name.
361 if (!strcmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
)) {
362 domain
= LTTNG_DOMAIN_JUL
;
363 } else if (!strcmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
364 domain
= LTTNG_DOMAIN_LOG4J
;
365 } else if (!strcmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
)) {
366 domain
= LTTNG_DOMAIN_PYTHON
;
371 * Set the overwrite mode for this channel based on the session
372 * type unless the client explicitly overrides the channel mode.
374 if (attr
->attr
.overwrite
== DEFAULT_CHANNEL_OVERWRITE
) {
375 attr
->attr
.overwrite
= !!usess
->snapshot_mode
;
378 /* Enforce mmap output for snapshot sessions. */
379 if (usess
->snapshot_mode
) {
380 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
383 /* Validate common channel properties. */
384 if (channel_validate(attr
) < 0) {
385 ret
= LTTNG_ERR_INVALID
;
390 * Validate UST buffer size and number of buffers: must both be power of 2
391 * and nonzero. We validate right here for UST, because applications will
392 * not report the error to the user (unlike kernel tracing).
394 if (!attr
->attr
.subbuf_size
||
395 (attr
->attr
.subbuf_size
& (attr
->attr
.subbuf_size
- 1))) {
396 ret
= LTTNG_ERR_INVALID
;
401 * Invalid subbuffer size if it's lower then the page size.
403 if (attr
->attr
.subbuf_size
< page_size
) {
404 ret
= LTTNG_ERR_INVALID
;
408 if (!attr
->attr
.num_subbuf
||
409 (attr
->attr
.num_subbuf
& (attr
->attr
.num_subbuf
- 1))) {
410 ret
= LTTNG_ERR_INVALID
;
414 if (attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
415 ret
= LTTNG_ERR_NOT_SUPPORTED
;
420 * The tracefile_size should not be < to the subbuf_size, otherwise
421 * we won't be able to write the packets on disk
423 if ((attr
->attr
.tracefile_size
> 0) &&
424 (attr
->attr
.tracefile_size
< attr
->attr
.subbuf_size
)) {
425 ret
= LTTNG_ERR_INVALID
;
429 /* Validate buffer type. */
431 case LTTNG_BUFFER_PER_PID
:
433 case LTTNG_BUFFER_PER_UID
:
436 ret
= LTTNG_ERR_BUFFER_NOT_SUPPORTED
;
440 /* Create UST channel */
441 uchan
= trace_ust_create_channel(attr
, domain
);
443 ret
= LTTNG_ERR_FATAL
;
448 if (trace_ust_is_max_id(usess
->used_channel_id
)) {
449 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
452 uchan
->id
= trace_ust_get_next_chan_id(usess
);
454 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64
,
455 uchan
->name
, type
, uchan
->id
);
457 /* Flag session buffer type. */
458 if (!usess
->buffer_type_changed
) {
459 usess
->buffer_type
= type
;
460 usess
->buffer_type_changed
= 1;
461 } else if (usess
->buffer_type
!= type
) {
462 /* Buffer type was already set. Refuse to create channel. */
463 ret
= LTTNG_ERR_BUFFER_TYPE_MISMATCH
;
464 goto error_free_chan
;
467 /* Enable channel for global domain */
468 ret
= ust_app_create_channel_glb(usess
, uchan
);
469 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
470 ret
= LTTNG_ERR_UST_CHAN_FAIL
;
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
);
481 * Copy channel attribute to session if this is metadata so if NO
482 * application exists we can access that data in the shadow copy during
483 * the global update of newly registered application.
485 memcpy(&usess
->metadata_attr
, &uchan
->attr
,
486 sizeof(usess
->metadata_attr
));
490 DBG2("Channel %s created successfully", uchan
->name
);
491 if (domain
!= LTTNG_DOMAIN_UST
) {
492 struct agent
*agt
= trace_ust_find_agent(usess
, domain
);
495 agt
= agent_create(domain
);
497 ret
= LTTNG_ERR_NOMEM
;
498 goto error_free_chan
;
500 agent_add(agt
, usess
->agents
);
504 channel_attr_destroy(defattr
);
509 * No need to remove the channel from the hash table because at this point
510 * it was not added hence the direct call and no call_rcu().
512 trace_ust_destroy_channel(uchan
);
514 channel_attr_destroy(defattr
);
519 * Disable UST channel for session and domain.
521 int channel_ust_disable(struct ltt_ust_session
*usess
,
522 struct ltt_ust_channel
*uchan
)
529 /* Already disabled */
530 if (uchan
->enabled
== 0) {
531 DBG2("Channel UST %s already disabled", uchan
->name
);
535 DBG2("Channel %s being disabled in UST global domain", uchan
->name
);
536 /* Disable channel for global domain */
537 ret
= ust_app_disable_channel_glb(usess
, uchan
);
538 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXIST
) {
539 ret
= LTTNG_ERR_UST_CHAN_DISABLE_FAIL
;
545 DBG2("Channel %s disabled successfully", uchan
->name
);