2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
10 #include "../utils.hpp"
12 #include <common/lttng-kernel.hpp>
13 #include <common/mi-lttng.hpp>
14 #include <common/sessiond-comm/sessiond-comm.hpp>
15 #include <common/utils.hpp>
17 #include <lttng/domain-internal.hpp>
26 #include <sys/types.h>
29 static struct lttng_channel chan_opts
;
30 static int opt_kernel
;
31 static char *opt_session_name
;
32 static int opt_userspace
;
33 static char *opt_output
;
34 static int opt_buffer_uid
;
35 static int opt_buffer_pid
;
36 static int opt_buffer_global
;
44 } opt_blocking_timeout
;
46 static struct mi_writer
*writer
;
48 #ifdef LTTNG_EMBED_HELP
49 static const char help_msg
[] =
50 #include <lttng-enable-channel.1.h>
70 static struct lttng_handle
*handle
;
72 const char *output_mmap
= "mmap";
73 const char *output_splice
= "splice";
75 static struct poptOption long_options
[] = {
76 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
77 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
78 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, nullptr, nullptr },
79 { "kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, nullptr, nullptr },
80 { "userspace", 'u', POPT_ARG_NONE
, nullptr, OPT_USERSPACE
, nullptr, nullptr },
81 { "discard", 0, POPT_ARG_NONE
, nullptr, OPT_DISCARD
, nullptr, nullptr },
82 { "overwrite", 0, POPT_ARG_NONE
, nullptr, OPT_OVERWRITE
, nullptr, nullptr },
83 { "subbuf-size", 0, POPT_ARG_STRING
, nullptr, OPT_SUBBUF_SIZE
, nullptr, nullptr },
84 { "num-subbuf", 0, POPT_ARG_INT
, nullptr, OPT_NUM_SUBBUF
, nullptr, nullptr },
85 { "switch-timer", 0, POPT_ARG_INT
, nullptr, OPT_SWITCH_TIMER
, nullptr, nullptr },
86 { "monitor-timer", 0, POPT_ARG_INT
, nullptr, OPT_MONITOR_TIMER
, nullptr, nullptr },
87 { "read-timer", 0, POPT_ARG_INT
, nullptr, OPT_READ_TIMER
, nullptr, nullptr },
88 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
89 { "output", 0, POPT_ARG_STRING
, &opt_output
, 0, nullptr, nullptr },
90 { "buffers-uid", 0, POPT_ARG_VAL
, &opt_buffer_uid
, 1, nullptr, nullptr },
91 { "buffers-pid", 0, POPT_ARG_VAL
, &opt_buffer_pid
, 1, nullptr, nullptr },
92 { "buffers-global", 0, POPT_ARG_VAL
, &opt_buffer_global
, 1, nullptr, nullptr },
93 { "tracefile-size", 'C', POPT_ARG_INT
, nullptr, OPT_TRACEFILE_SIZE
, nullptr, nullptr },
94 { "tracefile-count", 'W', POPT_ARG_INT
, nullptr, OPT_TRACEFILE_COUNT
, nullptr, nullptr },
95 { "blocking-timeout", 0, POPT_ARG_INT
, nullptr, OPT_BLOCKING_TIMEOUT
, nullptr, nullptr },
96 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
100 * Set default attributes depending on those already defined from the command
103 static void set_default_attr(struct lttng_domain
*dom
)
105 struct lttng_channel_attr default_attr
;
107 memset(&default_attr
, 0, sizeof(default_attr
));
110 lttng_channel_set_default_attr(dom
, &default_attr
);
112 if (chan_opts
.attr
.overwrite
== -1) {
113 chan_opts
.attr
.overwrite
= default_attr
.overwrite
;
115 if (chan_opts
.attr
.subbuf_size
== -1) {
116 chan_opts
.attr
.subbuf_size
= default_attr
.subbuf_size
;
118 if (chan_opts
.attr
.num_subbuf
== -1) {
119 chan_opts
.attr
.num_subbuf
= default_attr
.num_subbuf
;
121 if (chan_opts
.attr
.switch_timer_interval
== -1) {
122 chan_opts
.attr
.switch_timer_interval
= default_attr
.switch_timer_interval
;
124 if (chan_opts
.attr
.read_timer_interval
== -1) {
125 chan_opts
.attr
.read_timer_interval
= default_attr
.read_timer_interval
;
127 if ((int) chan_opts
.attr
.output
== -1) {
128 chan_opts
.attr
.output
= default_attr
.output
;
130 if (chan_opts
.attr
.tracefile_count
== -1) {
131 chan_opts
.attr
.tracefile_count
= default_attr
.tracefile_count
;
133 if (chan_opts
.attr
.tracefile_size
== -1) {
134 chan_opts
.attr
.tracefile_size
= default_attr
.tracefile_size
;
139 * Adding channel using the lttng API.
141 static int enable_channel(char *session_name
, char *channel_list
)
143 struct lttng_channel
*channel
= nullptr;
144 int ret
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 0;
146 struct lttng_domain dom
;
148 memset(&dom
, 0, sizeof(dom
));
150 /* Validate options. */
152 if (opt_blocking_timeout
.set
) {
153 ERR("Retry timeout option not supported for kernel domain (-k)");
159 /* Create lttng domain */
161 dom
.type
= LTTNG_DOMAIN_KERNEL
;
162 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
163 if (opt_buffer_uid
|| opt_buffer_pid
) {
164 ERR("Buffer type not supported for domain -k");
168 } else if (opt_userspace
) {
169 dom
.type
= LTTNG_DOMAIN_UST
;
170 if (opt_buffer_pid
) {
171 dom
.buf_type
= LTTNG_BUFFER_PER_PID
;
173 if (opt_buffer_global
) {
174 ERR("Buffer type not supported for domain -u");
178 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
181 /* Checked by the caller. */
185 set_default_attr(&dom
);
187 if (chan_opts
.attr
.tracefile_size
== 0 && chan_opts
.attr
.tracefile_count
) {
188 ERR("Missing option --tracefile-size. "
189 "A file count without a size won't do anything.");
194 if ((chan_opts
.attr
.tracefile_size
> 0) &&
195 (chan_opts
.attr
.tracefile_size
< chan_opts
.attr
.subbuf_size
)) {
196 WARN("Tracefile size rounded up from (%" PRIu64
") to subbuffer size (%" PRIu64
")",
197 chan_opts
.attr
.tracefile_size
,
198 chan_opts
.attr
.subbuf_size
);
199 chan_opts
.attr
.tracefile_size
= chan_opts
.attr
.subbuf_size
;
202 /* Setting channel output */
204 if (!strncmp(output_mmap
, opt_output
, strlen(output_mmap
))) {
205 chan_opts
.attr
.output
= LTTNG_EVENT_MMAP
;
206 } else if (!strncmp(output_splice
, opt_output
, strlen(output_splice
))) {
207 chan_opts
.attr
.output
= LTTNG_EVENT_SPLICE
;
209 ERR("Unknown output type %s. Possible values are: %s, %s\n",
218 handle
= lttng_create_handle(session_name
, &dom
);
219 if (handle
== nullptr) {
224 /* Mi open channels element */
226 LTTNG_ASSERT(writer
);
227 ret
= mi_lttng_channels_open(writer
);
234 /* Strip channel list (format: chan1,chan2,...) */
235 channel_name
= strtok(channel_list
, ",");
236 while (channel_name
!= nullptr) {
239 /* Validate channel name's length */
240 if (strlen(channel_name
) >= sizeof(chan_opts
.name
)) {
241 ERR("Channel name is too long (max. %zu characters)",
242 sizeof(chan_opts
.name
) - 1);
248 * A dynamically-allocated channel is used in order to allow
249 * the configuration of extended attributes (post-2.9).
251 channel
= lttng_channel_create(&dom
);
253 ERR("Unable to create channel object");
258 /* Copy channel name */
259 strcpy(channel
->name
, channel_name
);
260 channel
->enabled
= 1;
261 extended_ptr
= channel
->attr
.extended
.ptr
;
262 memcpy(&channel
->attr
, &chan_opts
.attr
, sizeof(chan_opts
.attr
));
263 channel
->attr
.extended
.ptr
= extended_ptr
;
264 if (opt_monitor_timer
.set
) {
265 ret
= lttng_channel_set_monitor_timer_interval(channel
,
266 opt_monitor_timer
.interval
);
268 ERR("Failed to set the channel's monitor timer interval");
273 if (opt_blocking_timeout
.set
) {
274 ret
= lttng_channel_set_blocking_timeout(channel
,
275 opt_blocking_timeout
.value
);
277 ERR("Failed to set the channel's blocking timeout");
283 DBG("Enabling channel %s", channel_name
);
285 ret
= lttng_enable_channel(handle
, channel
);
287 bool msg_already_printed
= false;
291 case LTTNG_ERR_KERN_CHAN_EXIST
:
292 case LTTNG_ERR_UST_CHAN_EXIST
:
293 case LTTNG_ERR_CHAN_EXIST
:
296 case LTTNG_ERR_INVALID_CHANNEL_NAME
:
297 ERR("Invalid channel name: \"%s\". "
298 "Channel names may not start with '.', and "
299 "may not contain '/'.",
301 msg_already_printed
= true;
309 if (!msg_already_printed
) {
310 LOG(error
? PRINT_ERR
: PRINT_WARN
,
311 "Failed to enable channel `%s` under session `%s`: %s",
314 lttng_strerror(ret
));
318 print_kernel_tracer_status_error();
321 MSG("%s channel `%s` enabled for session `%s`",
322 lttng_domain_type_str(dom
.type
),
330 /* Mi print the channel element and leave it open */
331 ret
= mi_lttng_channel(writer
, channel
, 1);
337 /* Individual Success ? */
338 ret
= mi_lttng_writer_write_element_bool(
339 writer
, mi_lttng_element_command_success
, success
);
345 /* Close channel element */
346 ret
= mi_lttng_writer_close_element(writer
);
354 channel_name
= strtok(nullptr, ",");
355 lttng_channel_destroy(channel
);
360 /* Close channels element */
361 ret
= mi_lttng_writer_close_element(writer
);
372 lttng_channel_destroy(channel
);
374 /* If more important error happen bypass the warning */
378 /* If more important error happen bypass the warning */
383 lttng_destroy_handle(handle
);
389 * Default value for channel configuration.
391 static void init_channel_config()
394 * Put -1 everywhere so we can identify those set by the command line and
395 * those needed to be set by the default values.
397 memset(&chan_opts
.attr
, -1, sizeof(chan_opts
.attr
));
398 chan_opts
.attr
.extended
.ptr
= nullptr;
402 * Add channel to trace session
404 int cmd_enable_channels(int argc
, const char **argv
)
406 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
407 static poptContext pc
;
408 char *session_name
= nullptr;
409 char *channel_list
= nullptr;
410 char *opt_arg
= nullptr;
411 const char *arg_channel_list
= nullptr;
412 const char *leftover
= nullptr;
414 init_channel_config();
416 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
417 poptReadDefaultConfig(pc
, 0);
419 while ((opt
= poptGetNextOpt(pc
)) != -1) {
425 chan_opts
.attr
.overwrite
= 0;
426 DBG("Channel set to discard");
429 chan_opts
.attr
.overwrite
= 1;
430 DBG("Channel set to overwrite");
432 case OPT_SUBBUF_SIZE
:
434 uint64_t rounded_size
;
438 opt_arg
= poptGetOptArg(pc
);
439 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.subbuf_size
) < 0 ||
440 !chan_opts
.attr
.subbuf_size
) {
441 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg
);
446 order
= get_count_order_u64(chan_opts
.attr
.subbuf_size
);
447 LTTNG_ASSERT(order
>= 0);
448 rounded_size
= 1ULL << order
;
449 if (rounded_size
< chan_opts
.attr
.subbuf_size
) {
450 ERR("The subbuf size (%" PRIu64
") is rounded and overflows!",
451 chan_opts
.attr
.subbuf_size
);
456 if (rounded_size
!= chan_opts
.attr
.subbuf_size
) {
457 WARN("The subbuf size (%" PRIu64
458 ") is rounded to the next power of 2 (%" PRIu64
")",
459 chan_opts
.attr
.subbuf_size
,
461 chan_opts
.attr
.subbuf_size
= rounded_size
;
464 /* Should now be power of 2 */
466 !((chan_opts
.attr
.subbuf_size
- 1) & chan_opts
.attr
.subbuf_size
));
468 DBG("Channel subbuf size set to %" PRIu64
, chan_opts
.attr
.subbuf_size
);
473 uint64_t rounded_size
;
477 opt_arg
= poptGetOptArg(pc
);
478 chan_opts
.attr
.num_subbuf
= strtoull(opt_arg
, nullptr, 0);
479 if (errno
!= 0 || !chan_opts
.attr
.num_subbuf
|| !isdigit(opt_arg
[0])) {
480 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg
);
485 order
= get_count_order_u64(chan_opts
.attr
.num_subbuf
);
486 LTTNG_ASSERT(order
>= 0);
487 rounded_size
= 1ULL << order
;
488 if (rounded_size
< chan_opts
.attr
.num_subbuf
) {
489 ERR("The number of subbuffers (%" PRIu64
490 ") is rounded and overflows!",
491 chan_opts
.attr
.num_subbuf
);
496 if (rounded_size
!= chan_opts
.attr
.num_subbuf
) {
497 WARN("The number of subbuffers (%" PRIu64
498 ") is rounded to the next power of 2 (%" PRIu64
")",
499 chan_opts
.attr
.num_subbuf
,
501 chan_opts
.attr
.num_subbuf
= rounded_size
;
504 /* Should now be power of 2 */
506 !((chan_opts
.attr
.num_subbuf
- 1) & chan_opts
.attr
.num_subbuf
));
508 DBG("Channel subbuf num set to %" PRIu64
, chan_opts
.attr
.num_subbuf
);
511 case OPT_SWITCH_TIMER
:
516 opt_arg
= poptGetOptArg(pc
);
518 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
519 ERR("Wrong value for --switch-timer parameter: %s", opt_arg
);
524 if (v
!= (uint32_t) v
) {
525 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
529 chan_opts
.attr
.switch_timer_interval
= (uint32_t) v
;
530 DBG("Channel switch timer interval set to %d %s",
531 chan_opts
.attr
.switch_timer_interval
,
540 opt_arg
= poptGetOptArg(pc
);
542 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
543 ERR("Wrong value for --read-timer parameter: %s", opt_arg
);
548 if (v
!= (uint32_t) v
) {
549 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
553 chan_opts
.attr
.read_timer_interval
= (uint32_t) v
;
554 DBG("Channel read timer interval set to %d %s",
555 chan_opts
.attr
.read_timer_interval
,
559 case OPT_MONITOR_TIMER
:
564 opt_arg
= poptGetOptArg(pc
);
566 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
567 ERR("Wrong value for --monitor-timer parameter: %s", opt_arg
);
571 opt_monitor_timer
.interval
= (uint64_t) v
;
572 opt_monitor_timer
.set
= true;
573 DBG("Channel monitor timer interval set to %" PRIu64
" %s",
574 opt_monitor_timer
.interval
,
578 case OPT_BLOCKING_TIMEOUT
:
584 opt_arg
= poptGetOptArg(pc
);
586 if (strcmp(opt_arg
, "inf") == 0) {
587 opt_blocking_timeout
.value
= (int64_t) -1;
588 opt_blocking_timeout
.set
= true;
589 DBG("Channel blocking timeout set to infinity");
593 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
594 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg
);
600 * While LTTng-UST and LTTng-tools will accept a
601 * blocking timeout expressed in µs, the current
602 * tracer implementation relies on poll() which
603 * takes an "int timeout" parameter expressed in
606 * Since the error reporting from the tracer is
607 * not precise, we perform this check here to
608 * provide a helpful error message in case of
611 * The setter (liblttng-ctl) also performs an
615 if (v_msec
!= (int32_t) v_msec
) {
616 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s",
622 opt_blocking_timeout
.value
= (int64_t) v
;
623 opt_blocking_timeout
.set
= true;
624 DBG("Channel blocking timeout set to %" PRId64
" %s%s",
625 opt_blocking_timeout
.value
,
627 opt_blocking_timeout
.value
== 0 ? " (non-blocking)" : "");
633 case OPT_TRACEFILE_SIZE
:
634 opt_arg
= poptGetOptArg(pc
);
635 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.tracefile_size
) < 0) {
636 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg
);
640 DBG("Maximum tracefile size set to %" PRIu64
,
641 chan_opts
.attr
.tracefile_size
);
643 case OPT_TRACEFILE_COUNT
:
648 opt_arg
= poptGetOptArg(pc
);
649 v
= strtoul(opt_arg
, nullptr, 0);
650 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
651 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg
);
655 if (v
!= (uint32_t) v
) {
656 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
660 chan_opts
.attr
.tracefile_count
= (uint32_t) v
;
661 DBG("Maximum tracefile count set to %" PRIu64
,
662 chan_opts
.attr
.tracefile_count
);
665 case OPT_LIST_OPTIONS
:
666 list_cmd_options(stdout
, long_options
);
679 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
, false);
685 if (chan_opts
.attr
.overwrite
== 1 && opt_blocking_timeout
.set
&&
686 opt_blocking_timeout
.value
!= 0) {
687 ERR("You cannot specify --overwrite and --blocking-timeout=N, "
688 "where N is different than 0");
695 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
697 ret
= -LTTNG_ERR_NOMEM
;
701 /* Open command element */
702 ret
= mi_lttng_writer_command_open(writer
,
703 mi_lttng_element_command_enable_channels
);
709 /* Open output element */
710 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
717 arg_channel_list
= poptGetArg(pc
);
718 if (arg_channel_list
== nullptr) {
719 ERR("Missing channel name.");
725 channel_list
= strdup(arg_channel_list
);
726 if (channel_list
== nullptr) {
727 PERROR("Failed to copy channel name");
733 leftover
= poptGetArg(pc
);
735 ERR("Unknown argument: %s", leftover
);
741 if (!opt_session_name
) {
742 session_name
= get_session_name();
743 if (session_name
== nullptr) {
744 command_ret
= CMD_ERROR
;
749 session_name
= opt_session_name
;
752 command_ret
= enable_channel(session_name
, channel_list
);
760 /* Close output element */
761 ret
= mi_lttng_writer_close_element(writer
);
767 ret
= mi_lttng_writer_write_element_bool(
768 writer
, mi_lttng_element_command_success
, success
);
773 /* Command element close */
774 ret
= mi_lttng_writer_command_close(writer
);
782 if (writer
&& mi_lttng_writer_destroy(writer
)) {
783 /* Preserve original error code */
784 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
787 if (!opt_session_name
&& session_name
) {
793 /* Overwrite ret if an error occurred when enable_channel */
794 ret
= command_ret
? command_ret
: ret
;