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/lttng-kernel.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;
145 enum lttng_kernel_tracer_status kernel_tracer_status
;
147 struct lttng_domain dom
;
149 memset(&dom
, 0, sizeof(dom
));
151 /* Validate options. */
153 if (opt_blocking_timeout
.set
) {
154 ERR("Retry timeout option not supported for kernel domain (-k)");
160 /* Create lttng domain */
162 dom
.type
= LTTNG_DOMAIN_KERNEL
;
163 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
164 if (opt_buffer_uid
|| opt_buffer_pid
) {
165 ERR("Buffer type not supported for domain -k");
169 } else if (opt_userspace
) {
170 dom
.type
= LTTNG_DOMAIN_UST
;
171 if (opt_buffer_pid
) {
172 dom
.buf_type
= LTTNG_BUFFER_PER_PID
;
174 if (opt_buffer_global
) {
175 ERR("Buffer type not supported for domain -u");
179 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
182 /* Checked by the caller. */
186 set_default_attr(&dom
);
188 if (chan_opts
.attr
.tracefile_size
== 0 && chan_opts
.attr
.tracefile_count
) {
189 ERR("Missing option --tracefile-size. "
190 "A file count without a size won't do anything.");
195 if ((chan_opts
.attr
.tracefile_size
> 0) &&
196 (chan_opts
.attr
.tracefile_size
< chan_opts
.attr
.subbuf_size
)) {
197 WARN("Tracefile size rounded up from (%" PRIu64
") to subbuffer size (%" PRIu64
")",
198 chan_opts
.attr
.tracefile_size
,
199 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",
219 handle
= lttng_create_handle(session_name
, &dom
);
220 if (handle
== nullptr) {
225 /* Mi open channels element */
227 LTTNG_ASSERT(writer
);
228 ret
= mi_lttng_channels_open(writer
);
235 /* Strip channel list (format: chan1,chan2,...) */
236 channel_name
= strtok(channel_list
, ",");
237 while (channel_name
!= nullptr) {
240 /* Validate channel name's length */
241 if (strlen(channel_name
) >= sizeof(chan_opts
.name
)) {
242 ERR("Channel name is too long (max. %zu characters)",
243 sizeof(chan_opts
.name
) - 1);
249 * A dynamically-allocated channel is used in order to allow
250 * the configuration of extended attributes (post-2.9).
252 channel
= lttng_channel_create(&dom
);
254 ERR("Unable to create channel object");
259 /* Copy channel name */
260 strcpy(channel
->name
, channel_name
);
261 channel
->enabled
= 1;
262 extended_ptr
= channel
->attr
.extended
.ptr
;
263 memcpy(&channel
->attr
, &chan_opts
.attr
, sizeof(chan_opts
.attr
));
264 channel
->attr
.extended
.ptr
= extended_ptr
;
265 if (opt_monitor_timer
.set
) {
266 ret
= lttng_channel_set_monitor_timer_interval(channel
,
267 opt_monitor_timer
.interval
);
269 ERR("Failed to set the channel's monitor timer interval");
274 if (opt_blocking_timeout
.set
) {
275 ret
= lttng_channel_set_blocking_timeout(channel
,
276 opt_blocking_timeout
.value
);
278 ERR("Failed to set the channel's blocking timeout");
284 DBG("Enabling channel %s", channel_name
);
286 ret
= lttng_enable_channel(handle
, channel
);
290 case LTTNG_ERR_KERN_CHAN_EXIST
:
291 case LTTNG_ERR_UST_CHAN_EXIST
:
292 case LTTNG_ERR_CHAN_EXIST
:
293 WARN("Channel %s: %s (session %s)",
299 case LTTNG_ERR_INVALID_CHANNEL_NAME
:
300 ERR("Invalid channel name: \"%s\". "
301 "Channel names may not start with '.', and "
302 "may not contain '/'.",
307 ERR("Channel %s: %s (session %s)",
315 * Ask the sessiond for the more details on the status of the kernel tracer.
317 ret
= lttng_get_kernel_tracer_status(&kernel_tracer_status
);
319 ERR("Failed to get kernel tracer status: %s", lttng_strerror(ret
));
321 switch (kernel_tracer_status
) {
322 case LTTNG_KERNEL_TRACER_STATUS_INITIALIZED
:
324 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN
:
325 MSG("\tKernel module loading failed");
327 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING
:
328 MSG("\tMissing one or more required kernel modules");
330 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE
:
331 MSG("\tKernel module signature error prevented loading of one or more required kernel modules");
333 case LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT
:
334 MSG("\tlttng-sessiond isn't running as root");
336 case LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER
:
337 MSG("\tFailed to setup notifiers");
339 case LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG
:
340 MSG("\tlttng-sessiond failed to open proc lttng");
342 case LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH
:
343 MSG("\tVersion mismatch between kernel tracer and kernel tracer ABI");
346 MSG("\tUnknown kernel tracer status (%d)", kernel_tracer_status
);
349 MSG("\tConsult lttng-sessiond logs for more information");
352 MSG("%s channel %s enabled for session %s",
353 lttng_domain_type_str(dom
.type
),
361 /* Mi print the channel element and leave it open */
362 ret
= mi_lttng_channel(writer
, channel
, 1);
368 /* Individual Success ? */
369 ret
= mi_lttng_writer_write_element_bool(
370 writer
, mi_lttng_element_command_success
, success
);
376 /* Close channel element */
377 ret
= mi_lttng_writer_close_element(writer
);
385 channel_name
= strtok(nullptr, ",");
386 lttng_channel_destroy(channel
);
391 /* Close channels element */
392 ret
= mi_lttng_writer_close_element(writer
);
403 lttng_channel_destroy(channel
);
405 /* If more important error happen bypass the warning */
409 /* If more important error happen bypass the warning */
414 lttng_destroy_handle(handle
);
420 * Default value for channel configuration.
422 static void init_channel_config()
425 * Put -1 everywhere so we can identify those set by the command line and
426 * those needed to be set by the default values.
428 memset(&chan_opts
.attr
, -1, sizeof(chan_opts
.attr
));
429 chan_opts
.attr
.extended
.ptr
= nullptr;
433 * Add channel to trace session
435 int cmd_enable_channels(int argc
, const char **argv
)
437 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
438 static poptContext pc
;
439 char *session_name
= nullptr;
440 char *channel_list
= nullptr;
441 char *opt_arg
= nullptr;
442 const char *arg_channel_list
= nullptr;
443 const char *leftover
= nullptr;
445 init_channel_config();
447 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
448 poptReadDefaultConfig(pc
, 0);
450 while ((opt
= poptGetNextOpt(pc
)) != -1) {
456 chan_opts
.attr
.overwrite
= 0;
457 DBG("Channel set to discard");
460 chan_opts
.attr
.overwrite
= 1;
461 DBG("Channel set to overwrite");
463 case OPT_SUBBUF_SIZE
:
465 uint64_t rounded_size
;
469 opt_arg
= poptGetOptArg(pc
);
470 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.subbuf_size
) < 0 ||
471 !chan_opts
.attr
.subbuf_size
) {
472 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg
);
477 order
= get_count_order_u64(chan_opts
.attr
.subbuf_size
);
478 LTTNG_ASSERT(order
>= 0);
479 rounded_size
= 1ULL << order
;
480 if (rounded_size
< chan_opts
.attr
.subbuf_size
) {
481 ERR("The subbuf size (%" PRIu64
") is rounded and overflows!",
482 chan_opts
.attr
.subbuf_size
);
487 if (rounded_size
!= chan_opts
.attr
.subbuf_size
) {
488 WARN("The subbuf size (%" PRIu64
489 ") is rounded to the next power of 2 (%" PRIu64
")",
490 chan_opts
.attr
.subbuf_size
,
492 chan_opts
.attr
.subbuf_size
= rounded_size
;
495 /* Should now be power of 2 */
497 !((chan_opts
.attr
.subbuf_size
- 1) & chan_opts
.attr
.subbuf_size
));
499 DBG("Channel subbuf size set to %" PRIu64
, chan_opts
.attr
.subbuf_size
);
504 uint64_t rounded_size
;
508 opt_arg
= poptGetOptArg(pc
);
509 chan_opts
.attr
.num_subbuf
= strtoull(opt_arg
, nullptr, 0);
510 if (errno
!= 0 || !chan_opts
.attr
.num_subbuf
|| !isdigit(opt_arg
[0])) {
511 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg
);
516 order
= get_count_order_u64(chan_opts
.attr
.num_subbuf
);
517 LTTNG_ASSERT(order
>= 0);
518 rounded_size
= 1ULL << order
;
519 if (rounded_size
< chan_opts
.attr
.num_subbuf
) {
520 ERR("The number of subbuffers (%" PRIu64
521 ") is rounded and overflows!",
522 chan_opts
.attr
.num_subbuf
);
527 if (rounded_size
!= chan_opts
.attr
.num_subbuf
) {
528 WARN("The number of subbuffers (%" PRIu64
529 ") is rounded to the next power of 2 (%" PRIu64
")",
530 chan_opts
.attr
.num_subbuf
,
532 chan_opts
.attr
.num_subbuf
= rounded_size
;
535 /* Should now be power of 2 */
537 !((chan_opts
.attr
.num_subbuf
- 1) & chan_opts
.attr
.num_subbuf
));
539 DBG("Channel subbuf num set to %" PRIu64
, chan_opts
.attr
.num_subbuf
);
542 case OPT_SWITCH_TIMER
:
547 opt_arg
= poptGetOptArg(pc
);
549 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
550 ERR("Wrong value for --switch-timer parameter: %s", opt_arg
);
555 if (v
!= (uint32_t) v
) {
556 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
560 chan_opts
.attr
.switch_timer_interval
= (uint32_t) v
;
561 DBG("Channel switch timer interval set to %d %s",
562 chan_opts
.attr
.switch_timer_interval
,
571 opt_arg
= poptGetOptArg(pc
);
573 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
574 ERR("Wrong value for --read-timer parameter: %s", opt_arg
);
579 if (v
!= (uint32_t) v
) {
580 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
584 chan_opts
.attr
.read_timer_interval
= (uint32_t) v
;
585 DBG("Channel read timer interval set to %d %s",
586 chan_opts
.attr
.read_timer_interval
,
590 case OPT_MONITOR_TIMER
:
595 opt_arg
= poptGetOptArg(pc
);
597 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
598 ERR("Wrong value for --monitor-timer parameter: %s", opt_arg
);
602 opt_monitor_timer
.interval
= (uint64_t) v
;
603 opt_monitor_timer
.set
= true;
604 DBG("Channel monitor timer interval set to %" PRIu64
" %s",
605 opt_monitor_timer
.interval
,
609 case OPT_BLOCKING_TIMEOUT
:
615 opt_arg
= poptGetOptArg(pc
);
617 if (strcmp(opt_arg
, "inf") == 0) {
618 opt_blocking_timeout
.value
= (int64_t) -1;
619 opt_blocking_timeout
.set
= true;
620 DBG("Channel blocking timeout set to infinity");
624 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
625 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg
);
631 * While LTTng-UST and LTTng-tools will accept a
632 * blocking timeout expressed in µs, the current
633 * tracer implementation relies on poll() which
634 * takes an "int timeout" parameter expressed in
637 * Since the error reporting from the tracer is
638 * not precise, we perform this check here to
639 * provide a helpful error message in case of
642 * The setter (liblttng-ctl) also performs an
646 if (v_msec
!= (int32_t) v_msec
) {
647 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s",
653 opt_blocking_timeout
.value
= (int64_t) v
;
654 opt_blocking_timeout
.set
= true;
655 DBG("Channel blocking timeout set to %" PRId64
" %s%s",
656 opt_blocking_timeout
.value
,
658 opt_blocking_timeout
.value
== 0 ? " (non-blocking)" : "");
664 case OPT_TRACEFILE_SIZE
:
665 opt_arg
= poptGetOptArg(pc
);
666 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.tracefile_size
) < 0) {
667 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg
);
671 DBG("Maximum tracefile size set to %" PRIu64
,
672 chan_opts
.attr
.tracefile_size
);
674 case OPT_TRACEFILE_COUNT
:
679 opt_arg
= poptGetOptArg(pc
);
680 v
= strtoul(opt_arg
, nullptr, 0);
681 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
682 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg
);
686 if (v
!= (uint32_t) v
) {
687 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
691 chan_opts
.attr
.tracefile_count
= (uint32_t) v
;
692 DBG("Maximum tracefile count set to %" PRIu64
,
693 chan_opts
.attr
.tracefile_count
);
696 case OPT_LIST_OPTIONS
:
697 list_cmd_options(stdout
, long_options
);
710 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
, false);
716 if (chan_opts
.attr
.overwrite
== 1 && opt_blocking_timeout
.set
&&
717 opt_blocking_timeout
.value
!= 0) {
718 ERR("You cannot specify --overwrite and --blocking-timeout=N, "
719 "where N is different than 0");
726 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
728 ret
= -LTTNG_ERR_NOMEM
;
732 /* Open command element */
733 ret
= mi_lttng_writer_command_open(writer
,
734 mi_lttng_element_command_enable_channels
);
740 /* Open output element */
741 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
748 arg_channel_list
= poptGetArg(pc
);
749 if (arg_channel_list
== nullptr) {
750 ERR("Missing channel name.");
756 channel_list
= strdup(arg_channel_list
);
757 if (channel_list
== nullptr) {
758 PERROR("Failed to copy channel name");
764 leftover
= poptGetArg(pc
);
766 ERR("Unknown argument: %s", leftover
);
772 if (!opt_session_name
) {
773 session_name
= get_session_name();
774 if (session_name
== nullptr) {
775 command_ret
= CMD_ERROR
;
780 session_name
= opt_session_name
;
783 command_ret
= enable_channel(session_name
, channel_list
);
791 /* Close output element */
792 ret
= mi_lttng_writer_close_element(writer
);
798 ret
= mi_lttng_writer_write_element_bool(
799 writer
, mi_lttng_element_command_success
, success
);
804 /* Command element close */
805 ret
= mi_lttng_writer_command_close(writer
);
813 if (writer
&& mi_lttng_writer_destroy(writer
)) {
814 /* Preserve original error code */
815 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
818 if (!opt_session_name
&& session_name
) {
824 /* Overwrite ret if an error occurred when enable_channel */
825 ret
= command_ret
? command_ret
: ret
;