2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/utils.h>
22 #include <common/mi-lttng.h>
24 #include <lttng/domain-internal.h>
26 #include "../command.h"
30 static struct lttng_channel chan_opts
;
31 static char *opt_channels
;
32 static int opt_kernel
;
33 static char *opt_session_name
;
34 static int opt_userspace
;
35 static char *opt_output
;
36 static int opt_buffer_uid
;
37 static int opt_buffer_pid
;
38 static int opt_buffer_global
;
46 } opt_blocking_timeout
;
48 static struct mi_writer
*writer
;
50 #ifdef LTTNG_EMBED_HELP
51 static const char help_msg
[] =
52 #include <lttng-enable-channel.1.h>
72 static struct lttng_handle
*handle
;
74 const char *output_mmap
= "mmap";
75 const char *output_splice
= "splice";
77 static struct poptOption long_options
[] = {
78 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
79 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
80 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
81 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
82 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
83 {"discard", 0, POPT_ARG_NONE
, 0, OPT_DISCARD
, 0, 0},
84 {"overwrite", 0, POPT_ARG_NONE
, 0, OPT_OVERWRITE
, 0, 0},
85 {"subbuf-size", 0, POPT_ARG_STRING
, 0, OPT_SUBBUF_SIZE
, 0, 0},
86 {"num-subbuf", 0, POPT_ARG_INT
, 0, OPT_NUM_SUBBUF
, 0, 0},
87 {"switch-timer", 0, POPT_ARG_INT
, 0, OPT_SWITCH_TIMER
, 0, 0},
88 {"monitor-timer", 0, POPT_ARG_INT
, 0, OPT_MONITOR_TIMER
, 0, 0},
89 {"read-timer", 0, POPT_ARG_INT
, 0, OPT_READ_TIMER
, 0, 0},
90 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
91 {"output", 0, POPT_ARG_STRING
, &opt_output
, 0, 0, 0},
92 {"buffers-uid", 0, POPT_ARG_VAL
, &opt_buffer_uid
, 1, 0, 0},
93 {"buffers-pid", 0, POPT_ARG_VAL
, &opt_buffer_pid
, 1, 0, 0},
94 {"buffers-global", 0, POPT_ARG_VAL
, &opt_buffer_global
, 1, 0, 0},
95 {"tracefile-size", 'C', POPT_ARG_INT
, 0, OPT_TRACEFILE_SIZE
, 0, 0},
96 {"tracefile-count", 'W', POPT_ARG_INT
, 0, OPT_TRACEFILE_COUNT
, 0, 0},
97 {"blocking-timeout", 0, POPT_ARG_INT
, 0, OPT_BLOCKING_TIMEOUT
, 0, 0},
102 * Set default attributes depending on those already defined from the command
105 static void set_default_attr(struct lttng_domain
*dom
)
107 struct lttng_channel_attr default_attr
;
109 memset(&default_attr
, 0, sizeof(default_attr
));
112 lttng_channel_set_default_attr(dom
, &default_attr
);
114 if (chan_opts
.attr
.overwrite
== -1) {
115 chan_opts
.attr
.overwrite
= default_attr
.overwrite
;
117 if (chan_opts
.attr
.subbuf_size
== -1) {
118 chan_opts
.attr
.subbuf_size
= default_attr
.subbuf_size
;
120 if (chan_opts
.attr
.num_subbuf
== -1) {
121 chan_opts
.attr
.num_subbuf
= default_attr
.num_subbuf
;
123 if (chan_opts
.attr
.switch_timer_interval
== -1) {
124 chan_opts
.attr
.switch_timer_interval
= default_attr
.switch_timer_interval
;
126 if (chan_opts
.attr
.read_timer_interval
== -1) {
127 chan_opts
.attr
.read_timer_interval
= default_attr
.read_timer_interval
;
129 if ((int) chan_opts
.attr
.output
== -1) {
130 chan_opts
.attr
.output
= default_attr
.output
;
132 if (chan_opts
.attr
.tracefile_count
== -1) {
133 chan_opts
.attr
.tracefile_count
= default_attr
.tracefile_count
;
135 if (chan_opts
.attr
.tracefile_size
== -1) {
136 chan_opts
.attr
.tracefile_size
= default_attr
.tracefile_size
;
141 * Adding channel using the lttng API.
143 static int enable_channel(char *session_name
)
145 struct lttng_channel
*channel
= NULL
;
146 int ret
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 0;
148 struct lttng_domain dom
;
150 memset(&dom
, 0, sizeof(dom
));
152 /* Validate options. */
154 if (opt_blocking_timeout
.set
) {
155 ERR("Retry timeout option not supported for kernel domain (-k)");
161 /* Create lttng domain */
163 dom
.type
= LTTNG_DOMAIN_KERNEL
;
164 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
165 if (opt_buffer_uid
|| opt_buffer_pid
) {
166 ERR("Buffer type not supported for domain -k");
170 } else if (opt_userspace
) {
171 dom
.type
= LTTNG_DOMAIN_UST
;
172 if (opt_buffer_pid
) {
173 dom
.buf_type
= LTTNG_BUFFER_PER_PID
;
175 if (opt_buffer_global
) {
176 ERR("Buffer type not supported for domain -u");
180 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
183 /* Checked by the caller. */
187 set_default_attr(&dom
);
189 if (chan_opts
.attr
.tracefile_size
== 0 && chan_opts
.attr
.tracefile_count
) {
190 ERR("Missing option --tracefile-size. "
191 "A file count without a size won't do anything.");
196 if ((chan_opts
.attr
.tracefile_size
> 0) &&
197 (chan_opts
.attr
.tracefile_size
< chan_opts
.attr
.subbuf_size
)) {
198 WARN("Tracefile size rounded up from (%" PRIu64
") to subbuffer size (%" PRIu64
")",
199 chan_opts
.attr
.tracefile_size
, chan_opts
.attr
.subbuf_size
);
200 chan_opts
.attr
.tracefile_size
= chan_opts
.attr
.subbuf_size
;
203 /* Setting channel output */
205 if (!strncmp(output_mmap
, opt_output
, strlen(output_mmap
))) {
206 chan_opts
.attr
.output
= LTTNG_EVENT_MMAP
;
207 } else if (!strncmp(output_splice
, opt_output
, strlen(output_splice
))) {
208 chan_opts
.attr
.output
= LTTNG_EVENT_SPLICE
;
210 ERR("Unknown output type %s. Possible values are: %s, %s\n",
211 opt_output
, output_mmap
, output_splice
);
217 handle
= lttng_create_handle(session_name
, &dom
);
218 if (handle
== NULL
) {
223 /* Mi open channels element */
226 ret
= mi_lttng_channels_open(writer
);
233 /* Strip channel list (format: chan1,chan2,...) */
234 channel_name
= strtok(opt_channels
, ",");
235 while (channel_name
!= NULL
) {
238 /* Validate channel name's length */
239 if (strlen(channel_name
) >= sizeof(chan_opts
.name
)) {
240 ERR("Channel name is too long (max. %zu characters)",
241 sizeof(chan_opts
.name
) - 1);
247 * A dynamically-allocated channel is used in order to allow
248 * the configuration of extended attributes (post-2.9).
250 channel
= lttng_channel_create(&dom
);
252 ERR("Unable to create channel object");
257 /* Copy channel name */
258 strcpy(channel
->name
, channel_name
);
259 channel
->enabled
= 1;
260 extended_ptr
= channel
->attr
.extended
.ptr
;
261 memcpy(&channel
->attr
, &chan_opts
.attr
, sizeof(chan_opts
.attr
));
262 channel
->attr
.extended
.ptr
= extended_ptr
;
263 if (opt_monitor_timer
.set
) {
264 ret
= lttng_channel_set_monitor_timer_interval(channel
,
265 opt_monitor_timer
.interval
);
267 ERR("Failed to set the channel's monitor timer interval");
272 if (opt_blocking_timeout
.set
) {
273 ret
= lttng_channel_set_blocking_timeout(channel
,
274 opt_blocking_timeout
.value
);
276 ERR("Failed to set the channel's blocking timeout");
282 DBG("Enabling channel %s", channel_name
);
284 ret
= lttng_enable_channel(handle
, channel
);
288 case LTTNG_ERR_KERN_CHAN_EXIST
:
289 case LTTNG_ERR_UST_CHAN_EXIST
:
290 case LTTNG_ERR_CHAN_EXIST
:
291 WARN("Channel %s: %s (session %s)", channel_name
,
292 lttng_strerror(ret
), session_name
);
295 case LTTNG_ERR_INVALID_CHANNEL_NAME
:
296 ERR("Invalid channel name: \"%s\". "
297 "Channel names may not start with '.', and "
298 "may not contain '/'.", channel_name
);
302 ERR("Channel %s: %s (session %s)", channel_name
,
303 lttng_strerror(ret
), session_name
);
308 MSG("%s channel %s enabled for session %s",
309 lttng_domain_type_str(dom
.type
),
310 channel_name
, session_name
);
316 /* Mi print the channel element and leave it open */
317 ret
= mi_lttng_channel(writer
, channel
, 1);
323 /* Individual Success ? */
324 ret
= mi_lttng_writer_write_element_bool(writer
,
325 mi_lttng_element_command_success
, success
);
331 /* Close channel element */
332 ret
= mi_lttng_writer_close_element(writer
);
340 channel_name
= strtok(NULL
, ",");
341 lttng_channel_destroy(channel
);
346 /* Close channels element */
347 ret
= mi_lttng_writer_close_element(writer
);
358 lttng_channel_destroy(channel
);
360 /* If more important error happen bypass the warning */
364 /* If more important error happen bypass the warning */
369 lttng_destroy_handle(handle
);
375 * Default value for channel configuration.
377 static void init_channel_config(void)
380 * Put -1 everywhere so we can identify those set by the command line and
381 * those needed to be set by the default values.
383 memset(&chan_opts
.attr
, -1, sizeof(chan_opts
.attr
));
384 chan_opts
.attr
.extended
.ptr
= NULL
;
388 * Add channel to trace session
390 int cmd_enable_channels(int argc
, const char **argv
)
392 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
393 static poptContext pc
;
394 char *session_name
= NULL
;
395 char *opt_arg
= NULL
;
396 const char *leftover
= NULL
;
398 init_channel_config();
400 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
401 poptReadDefaultConfig(pc
, 0);
403 while ((opt
= poptGetNextOpt(pc
)) != -1) {
409 chan_opts
.attr
.overwrite
= 0;
410 DBG("Channel set to discard");
413 chan_opts
.attr
.overwrite
= 1;
414 DBG("Channel set to overwrite");
416 case OPT_SUBBUF_SIZE
:
418 uint64_t rounded_size
;
422 opt_arg
= poptGetOptArg(pc
);
423 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.subbuf_size
) < 0 || !chan_opts
.attr
.subbuf_size
) {
424 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg
);
429 order
= get_count_order_u64(chan_opts
.attr
.subbuf_size
);
431 rounded_size
= 1ULL << order
;
432 if (rounded_size
< chan_opts
.attr
.subbuf_size
) {
433 ERR("The subbuf size (%" PRIu64
") is rounded and overflows!",
434 chan_opts
.attr
.subbuf_size
);
439 if (rounded_size
!= chan_opts
.attr
.subbuf_size
) {
440 WARN("The subbuf size (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
441 chan_opts
.attr
.subbuf_size
, rounded_size
);
442 chan_opts
.attr
.subbuf_size
= rounded_size
;
445 /* Should now be power of 2 */
446 assert(!((chan_opts
.attr
.subbuf_size
- 1) & chan_opts
.attr
.subbuf_size
));
448 DBG("Channel subbuf size set to %" PRIu64
, chan_opts
.attr
.subbuf_size
);
453 uint64_t rounded_size
;
457 opt_arg
= poptGetOptArg(pc
);
458 chan_opts
.attr
.num_subbuf
= strtoull(opt_arg
, NULL
, 0);
459 if (errno
!= 0 || !chan_opts
.attr
.num_subbuf
|| !isdigit(opt_arg
[0])) {
460 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg
);
465 order
= get_count_order_u64(chan_opts
.attr
.num_subbuf
);
467 rounded_size
= 1ULL << order
;
468 if (rounded_size
< chan_opts
.attr
.num_subbuf
) {
469 ERR("The number of subbuffers (%" PRIu64
") is rounded and overflows!",
470 chan_opts
.attr
.num_subbuf
);
475 if (rounded_size
!= chan_opts
.attr
.num_subbuf
) {
476 WARN("The number of subbuffers (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
477 chan_opts
.attr
.num_subbuf
, rounded_size
);
478 chan_opts
.attr
.num_subbuf
= rounded_size
;
481 /* Should now be power of 2 */
482 assert(!((chan_opts
.attr
.num_subbuf
- 1) & chan_opts
.attr
.num_subbuf
));
484 DBG("Channel subbuf num set to %" PRIu64
, chan_opts
.attr
.num_subbuf
);
487 case OPT_SWITCH_TIMER
:
492 opt_arg
= poptGetOptArg(pc
);
494 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
495 ERR("Wrong value for --switch-timer parameter: %s", opt_arg
);
500 if (v
!= (uint32_t) v
) {
501 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
505 chan_opts
.attr
.switch_timer_interval
= (uint32_t) v
;
506 DBG("Channel switch timer interval set to %d %s",
507 chan_opts
.attr
.switch_timer_interval
,
516 opt_arg
= poptGetOptArg(pc
);
518 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
519 ERR("Wrong value for --read-timer parameter: %s", opt_arg
);
524 if (v
!= (uint32_t) v
) {
525 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
529 chan_opts
.attr
.read_timer_interval
= (uint32_t) v
;
530 DBG("Channel read timer interval set to %d %s",
531 chan_opts
.attr
.read_timer_interval
,
535 case OPT_MONITOR_TIMER
:
540 opt_arg
= poptGetOptArg(pc
);
542 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
543 ERR("Wrong value for --monitor-timer parameter: %s", opt_arg
);
547 opt_monitor_timer
.interval
= (uint64_t) v
;
548 opt_monitor_timer
.set
= true;
549 DBG("Channel monitor timer interval set to %" PRIu64
" %s",
550 opt_monitor_timer
.interval
,
554 case OPT_BLOCKING_TIMEOUT
:
560 opt_arg
= poptGetOptArg(pc
);
562 if (strcmp(opt_arg
, "inf") == 0) {
563 opt_blocking_timeout
.value
= (int64_t) -1;
564 opt_blocking_timeout
.set
= true;
565 DBG("Channel blocking timeout set to infinity");
569 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
570 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg
);
576 * While LTTng-UST and LTTng-tools will accept a
577 * blocking timeout expressed in µs, the current
578 * tracer implementation relies on poll() which
579 * takes an "int timeout" parameter expressed in
582 * Since the error reporting from the tracer is
583 * not precise, we perform this check here to
584 * provide a helpful error message in case of
587 * The setter (liblttng-ctl) also performs an
591 if (v_msec
!= (int32_t) v_msec
) {
592 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s", opt_arg
);
597 opt_blocking_timeout
.value
= (int64_t) v
;
598 opt_blocking_timeout
.set
= true;
599 DBG("Channel blocking timeout set to %" PRId64
" %s%s",
600 opt_blocking_timeout
.value
,
602 opt_blocking_timeout
.value
== 0 ?
603 " (non-blocking)" : "");
609 case OPT_TRACEFILE_SIZE
:
610 opt_arg
= poptGetOptArg(pc
);
611 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.tracefile_size
) < 0) {
612 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg
);
616 DBG("Maximum tracefile size set to %" PRIu64
,
617 chan_opts
.attr
.tracefile_size
);
619 case OPT_TRACEFILE_COUNT
:
624 opt_arg
= poptGetOptArg(pc
);
625 v
= strtoul(opt_arg
, NULL
, 0);
626 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
627 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg
);
631 if (v
!= (uint32_t) v
) {
632 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
636 chan_opts
.attr
.tracefile_count
= (uint32_t) v
;
637 DBG("Maximum tracefile count set to %" PRIu64
,
638 chan_opts
.attr
.tracefile_count
);
641 case OPT_LIST_OPTIONS
:
642 list_cmd_options(stdout
, long_options
);
650 ret
= print_missing_or_multiple_domains(
651 opt_kernel
+ opt_userspace
, false);
657 if (chan_opts
.attr
.overwrite
== 1 && opt_blocking_timeout
.set
&&
658 opt_blocking_timeout
.value
!= 0) {
659 ERR("You cannot specify --overwrite and --blocking-timeout=N, "
660 "where N is different than 0");
667 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
669 ret
= -LTTNG_ERR_NOMEM
;
673 /* Open command element */
674 ret
= mi_lttng_writer_command_open(writer
,
675 mi_lttng_element_command_enable_channels
);
681 /* Open output element */
682 ret
= mi_lttng_writer_open_element(writer
,
683 mi_lttng_element_command_output
);
690 opt_channels
= (char*) poptGetArg(pc
);
691 if (opt_channels
== NULL
) {
692 ERR("Missing channel name.\n");
698 leftover
= poptGetArg(pc
);
700 ERR("Unknown argument: %s", leftover
);
706 if (!opt_session_name
) {
707 session_name
= get_session_name();
708 if (session_name
== NULL
) {
709 command_ret
= CMD_ERROR
;
714 session_name
= opt_session_name
;
717 command_ret
= enable_channel(session_name
);
725 /* Close output element */
726 ret
= mi_lttng_writer_close_element(writer
);
732 ret
= mi_lttng_writer_write_element_bool(writer
,
733 mi_lttng_element_command_success
, success
);
738 /* Command element close */
739 ret
= mi_lttng_writer_command_close(writer
);
747 if (writer
&& mi_lttng_writer_destroy(writer
)) {
748 /* Preserve original error code */
749 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
752 if (!opt_session_name
&& session_name
) {
756 /* Overwrite ret if an error occurred when enable_channel */
757 ret
= command_ret
? command_ret
: ret
;