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/mi-lttng.hpp>
13 #include <common/sessiond-comm/sessiond-comm.hpp>
14 #include <common/utils.hpp>
16 #include <lttng/domain-internal.hpp>
25 #include <sys/types.h>
28 static struct lttng_channel chan_opts
;
29 static int opt_kernel
;
30 static char *opt_session_name
;
31 static int opt_userspace
;
32 static char *opt_output
;
33 static int opt_buffer_uid
;
34 static int opt_buffer_pid
;
35 static int opt_buffer_global
;
43 } opt_blocking_timeout
;
45 static struct mi_writer
*writer
;
47 #ifdef LTTNG_EMBED_HELP
48 static const char help_msg
[] =
49 #include <lttng-enable-channel.1.h>
69 static struct lttng_handle
*handle
;
71 const char *output_mmap
= "mmap";
72 const char *output_splice
= "splice";
74 static struct poptOption long_options
[] = {
75 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
76 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
77 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, nullptr, nullptr },
78 { "kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, nullptr, nullptr },
79 { "userspace", 'u', POPT_ARG_NONE
, nullptr, OPT_USERSPACE
, nullptr, nullptr },
80 { "discard", 0, POPT_ARG_NONE
, nullptr, OPT_DISCARD
, nullptr, nullptr },
81 { "overwrite", 0, POPT_ARG_NONE
, nullptr, OPT_OVERWRITE
, nullptr, nullptr },
82 { "subbuf-size", 0, POPT_ARG_STRING
, nullptr, OPT_SUBBUF_SIZE
, nullptr, nullptr },
83 { "num-subbuf", 0, POPT_ARG_INT
, nullptr, OPT_NUM_SUBBUF
, nullptr, nullptr },
84 { "switch-timer", 0, POPT_ARG_INT
, nullptr, OPT_SWITCH_TIMER
, nullptr, nullptr },
85 { "monitor-timer", 0, POPT_ARG_INT
, nullptr, OPT_MONITOR_TIMER
, nullptr, nullptr },
86 { "read-timer", 0, POPT_ARG_INT
, nullptr, OPT_READ_TIMER
, nullptr, nullptr },
87 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
88 { "output", 0, POPT_ARG_STRING
, &opt_output
, 0, nullptr, nullptr },
89 { "buffers-uid", 0, POPT_ARG_VAL
, &opt_buffer_uid
, 1, nullptr, nullptr },
90 { "buffers-pid", 0, POPT_ARG_VAL
, &opt_buffer_pid
, 1, nullptr, nullptr },
91 { "buffers-global", 0, POPT_ARG_VAL
, &opt_buffer_global
, 1, nullptr, nullptr },
92 { "tracefile-size", 'C', POPT_ARG_INT
, nullptr, OPT_TRACEFILE_SIZE
, nullptr, nullptr },
93 { "tracefile-count", 'W', POPT_ARG_INT
, nullptr, OPT_TRACEFILE_COUNT
, nullptr, nullptr },
94 { "blocking-timeout", 0, POPT_ARG_INT
, nullptr, OPT_BLOCKING_TIMEOUT
, nullptr, nullptr },
95 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
99 * Set default attributes depending on those already defined from the command
102 static void set_default_attr(struct lttng_domain
*dom
)
104 struct lttng_channel_attr default_attr
;
106 memset(&default_attr
, 0, sizeof(default_attr
));
109 lttng_channel_set_default_attr(dom
, &default_attr
);
111 if (chan_opts
.attr
.overwrite
== -1) {
112 chan_opts
.attr
.overwrite
= default_attr
.overwrite
;
114 if (chan_opts
.attr
.subbuf_size
== -1) {
115 chan_opts
.attr
.subbuf_size
= default_attr
.subbuf_size
;
117 if (chan_opts
.attr
.num_subbuf
== -1) {
118 chan_opts
.attr
.num_subbuf
= default_attr
.num_subbuf
;
120 if (chan_opts
.attr
.switch_timer_interval
== -1) {
121 chan_opts
.attr
.switch_timer_interval
= default_attr
.switch_timer_interval
;
123 if (chan_opts
.attr
.read_timer_interval
== -1) {
124 chan_opts
.attr
.read_timer_interval
= default_attr
.read_timer_interval
;
126 if ((int) chan_opts
.attr
.output
== -1) {
127 chan_opts
.attr
.output
= default_attr
.output
;
129 if (chan_opts
.attr
.tracefile_count
== -1) {
130 chan_opts
.attr
.tracefile_count
= default_attr
.tracefile_count
;
132 if (chan_opts
.attr
.tracefile_size
== -1) {
133 chan_opts
.attr
.tracefile_size
= default_attr
.tracefile_size
;
138 * Adding channel using the lttng API.
140 static int enable_channel(char *session_name
, char *channel_list
)
142 struct lttng_channel
*channel
= nullptr;
143 int ret
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 0;
145 struct lttng_domain dom
;
147 memset(&dom
, 0, sizeof(dom
));
149 /* Validate options. */
151 if (opt_blocking_timeout
.set
) {
152 ERR("Retry timeout option not supported for kernel domain (-k)");
158 /* Create lttng domain */
160 dom
.type
= LTTNG_DOMAIN_KERNEL
;
161 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
162 if (opt_buffer_uid
|| opt_buffer_pid
) {
163 ERR("Buffer type not supported for domain -k");
167 } else if (opt_userspace
) {
168 dom
.type
= LTTNG_DOMAIN_UST
;
169 if (opt_buffer_pid
) {
170 dom
.buf_type
= LTTNG_BUFFER_PER_PID
;
172 if (opt_buffer_global
) {
173 ERR("Buffer type not supported for domain -u");
177 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
180 /* Checked by the caller. */
184 set_default_attr(&dom
);
186 if (chan_opts
.attr
.tracefile_size
== 0 && chan_opts
.attr
.tracefile_count
) {
187 ERR("Missing option --tracefile-size. "
188 "A file count without a size won't do anything.");
193 if ((chan_opts
.attr
.tracefile_size
> 0) &&
194 (chan_opts
.attr
.tracefile_size
< chan_opts
.attr
.subbuf_size
)) {
195 WARN("Tracefile size rounded up from (%" PRIu64
") to subbuffer size (%" PRIu64
")",
196 chan_opts
.attr
.tracefile_size
,
197 chan_opts
.attr
.subbuf_size
);
198 chan_opts
.attr
.tracefile_size
= chan_opts
.attr
.subbuf_size
;
201 /* Setting channel output */
203 if (!strncmp(output_mmap
, opt_output
, strlen(output_mmap
))) {
204 chan_opts
.attr
.output
= LTTNG_EVENT_MMAP
;
205 } else if (!strncmp(output_splice
, opt_output
, strlen(output_splice
))) {
206 chan_opts
.attr
.output
= LTTNG_EVENT_SPLICE
;
208 ERR("Unknown output type %s. Possible values are: %s, %s\n",
217 handle
= lttng_create_handle(session_name
, &dom
);
218 if (handle
== nullptr) {
223 /* Mi open channels element */
225 LTTNG_ASSERT(writer
);
226 ret
= mi_lttng_channels_open(writer
);
233 /* Strip channel list (format: chan1,chan2,...) */
234 channel_name
= strtok(channel_list
, ",");
235 while (channel_name
!= nullptr) {
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)",
297 case LTTNG_ERR_INVALID_CHANNEL_NAME
:
298 ERR("Invalid channel name: \"%s\". "
299 "Channel names may not start with '.', and "
300 "may not contain '/'.",
305 ERR("Channel %s: %s (session %s)",
313 MSG("%s channel %s enabled for session %s",
314 lttng_domain_type_str(dom
.type
),
322 /* Mi print the channel element and leave it open */
323 ret
= mi_lttng_channel(writer
, channel
, 1);
329 /* Individual Success ? */
330 ret
= mi_lttng_writer_write_element_bool(
331 writer
, mi_lttng_element_command_success
, success
);
337 /* Close channel element */
338 ret
= mi_lttng_writer_close_element(writer
);
346 channel_name
= strtok(nullptr, ",");
347 lttng_channel_destroy(channel
);
352 /* Close channels element */
353 ret
= mi_lttng_writer_close_element(writer
);
364 lttng_channel_destroy(channel
);
366 /* If more important error happen bypass the warning */
370 /* If more important error happen bypass the warning */
375 lttng_destroy_handle(handle
);
381 * Default value for channel configuration.
383 static void init_channel_config()
386 * Put -1 everywhere so we can identify those set by the command line and
387 * those needed to be set by the default values.
389 memset(&chan_opts
.attr
, -1, sizeof(chan_opts
.attr
));
390 chan_opts
.attr
.extended
.ptr
= nullptr;
394 * Add channel to trace session
396 int cmd_enable_channels(int argc
, const char **argv
)
398 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
399 static poptContext pc
;
400 char *session_name
= nullptr;
401 char *channel_list
= nullptr;
402 char *opt_arg
= nullptr;
403 const char *arg_channel_list
= nullptr;
404 const char *leftover
= nullptr;
406 init_channel_config();
408 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
409 poptReadDefaultConfig(pc
, 0);
411 while ((opt
= poptGetNextOpt(pc
)) != -1) {
417 chan_opts
.attr
.overwrite
= 0;
418 DBG("Channel set to discard");
421 chan_opts
.attr
.overwrite
= 1;
422 DBG("Channel set to overwrite");
424 case OPT_SUBBUF_SIZE
:
426 uint64_t rounded_size
;
430 opt_arg
= poptGetOptArg(pc
);
431 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.subbuf_size
) < 0 ||
432 !chan_opts
.attr
.subbuf_size
) {
433 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg
);
438 order
= get_count_order_u64(chan_opts
.attr
.subbuf_size
);
439 LTTNG_ASSERT(order
>= 0);
440 rounded_size
= 1ULL << order
;
441 if (rounded_size
< chan_opts
.attr
.subbuf_size
) {
442 ERR("The subbuf size (%" PRIu64
") is rounded and overflows!",
443 chan_opts
.attr
.subbuf_size
);
448 if (rounded_size
!= chan_opts
.attr
.subbuf_size
) {
449 WARN("The subbuf size (%" PRIu64
450 ") is rounded to the next power of 2 (%" PRIu64
")",
451 chan_opts
.attr
.subbuf_size
,
453 chan_opts
.attr
.subbuf_size
= rounded_size
;
456 /* Should now be power of 2 */
458 !((chan_opts
.attr
.subbuf_size
- 1) & chan_opts
.attr
.subbuf_size
));
460 DBG("Channel subbuf size set to %" PRIu64
, chan_opts
.attr
.subbuf_size
);
465 uint64_t rounded_size
;
469 opt_arg
= poptGetOptArg(pc
);
470 chan_opts
.attr
.num_subbuf
= strtoull(opt_arg
, nullptr, 0);
471 if (errno
!= 0 || !chan_opts
.attr
.num_subbuf
|| !isdigit(opt_arg
[0])) {
472 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg
);
477 order
= get_count_order_u64(chan_opts
.attr
.num_subbuf
);
478 LTTNG_ASSERT(order
>= 0);
479 rounded_size
= 1ULL << order
;
480 if (rounded_size
< chan_opts
.attr
.num_subbuf
) {
481 ERR("The number of subbuffers (%" PRIu64
482 ") is rounded and overflows!",
483 chan_opts
.attr
.num_subbuf
);
488 if (rounded_size
!= chan_opts
.attr
.num_subbuf
) {
489 WARN("The number of subbuffers (%" PRIu64
490 ") is rounded to the next power of 2 (%" PRIu64
")",
491 chan_opts
.attr
.num_subbuf
,
493 chan_opts
.attr
.num_subbuf
= rounded_size
;
496 /* Should now be power of 2 */
498 !((chan_opts
.attr
.num_subbuf
- 1) & chan_opts
.attr
.num_subbuf
));
500 DBG("Channel subbuf num set to %" PRIu64
, chan_opts
.attr
.num_subbuf
);
503 case OPT_SWITCH_TIMER
:
508 opt_arg
= poptGetOptArg(pc
);
510 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
511 ERR("Wrong value for --switch-timer parameter: %s", opt_arg
);
516 if (v
!= (uint32_t) v
) {
517 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
521 chan_opts
.attr
.switch_timer_interval
= (uint32_t) v
;
522 DBG("Channel switch timer interval set to %d %s",
523 chan_opts
.attr
.switch_timer_interval
,
532 opt_arg
= poptGetOptArg(pc
);
534 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
535 ERR("Wrong value for --read-timer parameter: %s", opt_arg
);
540 if (v
!= (uint32_t) v
) {
541 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
545 chan_opts
.attr
.read_timer_interval
= (uint32_t) v
;
546 DBG("Channel read timer interval set to %d %s",
547 chan_opts
.attr
.read_timer_interval
,
551 case OPT_MONITOR_TIMER
:
556 opt_arg
= poptGetOptArg(pc
);
558 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
559 ERR("Wrong value for --monitor-timer parameter: %s", opt_arg
);
563 opt_monitor_timer
.interval
= (uint64_t) v
;
564 opt_monitor_timer
.set
= true;
565 DBG("Channel monitor timer interval set to %" PRIu64
" %s",
566 opt_monitor_timer
.interval
,
570 case OPT_BLOCKING_TIMEOUT
:
576 opt_arg
= poptGetOptArg(pc
);
578 if (strcmp(opt_arg
, "inf") == 0) {
579 opt_blocking_timeout
.value
= (int64_t) -1;
580 opt_blocking_timeout
.set
= true;
581 DBG("Channel blocking timeout set to infinity");
585 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
586 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg
);
592 * While LTTng-UST and LTTng-tools will accept a
593 * blocking timeout expressed in µs, the current
594 * tracer implementation relies on poll() which
595 * takes an "int timeout" parameter expressed in
598 * Since the error reporting from the tracer is
599 * not precise, we perform this check here to
600 * provide a helpful error message in case of
603 * The setter (liblttng-ctl) also performs an
607 if (v_msec
!= (int32_t) v_msec
) {
608 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s",
614 opt_blocking_timeout
.value
= (int64_t) v
;
615 opt_blocking_timeout
.set
= true;
616 DBG("Channel blocking timeout set to %" PRId64
" %s%s",
617 opt_blocking_timeout
.value
,
619 opt_blocking_timeout
.value
== 0 ? " (non-blocking)" : "");
625 case OPT_TRACEFILE_SIZE
:
626 opt_arg
= poptGetOptArg(pc
);
627 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.tracefile_size
) < 0) {
628 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg
);
632 DBG("Maximum tracefile size set to %" PRIu64
,
633 chan_opts
.attr
.tracefile_size
);
635 case OPT_TRACEFILE_COUNT
:
640 opt_arg
= poptGetOptArg(pc
);
641 v
= strtoul(opt_arg
, nullptr, 0);
642 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
643 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg
);
647 if (v
!= (uint32_t) v
) {
648 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
652 chan_opts
.attr
.tracefile_count
= (uint32_t) v
;
653 DBG("Maximum tracefile count set to %" PRIu64
,
654 chan_opts
.attr
.tracefile_count
);
657 case OPT_LIST_OPTIONS
:
658 list_cmd_options(stdout
, long_options
);
671 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
, false);
677 if (chan_opts
.attr
.overwrite
== 1 && opt_blocking_timeout
.set
&&
678 opt_blocking_timeout
.value
!= 0) {
679 ERR("You cannot specify --overwrite and --blocking-timeout=N, "
680 "where N is different than 0");
687 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
689 ret
= -LTTNG_ERR_NOMEM
;
693 /* Open command element */
694 ret
= mi_lttng_writer_command_open(writer
,
695 mi_lttng_element_command_enable_channels
);
701 /* Open output element */
702 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
709 arg_channel_list
= poptGetArg(pc
);
710 if (arg_channel_list
== nullptr) {
711 ERR("Missing channel name.");
717 channel_list
= strdup(arg_channel_list
);
718 if (channel_list
== nullptr) {
719 PERROR("Failed to copy channel name");
725 leftover
= poptGetArg(pc
);
727 ERR("Unknown argument: %s", leftover
);
733 if (!opt_session_name
) {
734 session_name
= get_session_name();
735 if (session_name
== nullptr) {
736 command_ret
= CMD_ERROR
;
741 session_name
= opt_session_name
;
744 command_ret
= enable_channel(session_name
, channel_list
);
752 /* Close output element */
753 ret
= mi_lttng_writer_close_element(writer
);
759 ret
= mi_lttng_writer_write_element_bool(
760 writer
, mi_lttng_element_command_success
, success
);
765 /* Command element close */
766 ret
= mi_lttng_writer_command_close(writer
);
774 if (writer
&& mi_lttng_writer_destroy(writer
)) {
775 /* Preserve original error code */
776 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
779 if (!opt_session_name
&& session_name
) {
785 /* Overwrite ret if an error occurred when enable_channel */
786 ret
= command_ret
? command_ret
: ret
;