2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
19 #include <common/sessiond-comm/sessiond-comm.h>
20 #include <common/utils.h>
21 #include <common/mi-lttng.h>
23 #include <lttng/domain-internal.h>
25 #include "../command.h"
29 static struct lttng_channel chan_opts
;
30 static char *opt_channels
;
31 static int opt_kernel
;
32 static char *opt_session_name
;
33 static int opt_userspace
;
34 static char *opt_output
;
35 static int opt_buffer_uid
;
36 static int opt_buffer_pid
;
37 static int opt_buffer_global
;
45 } opt_blocking_timeout
;
47 static struct mi_writer
*writer
;
49 #ifdef LTTNG_EMBED_HELP
50 static const char help_msg
[] =
51 #include <lttng-enable-channel.1.h>
71 static struct lttng_handle
*handle
;
73 const char *output_mmap
= "mmap";
74 const char *output_splice
= "splice";
76 static struct poptOption long_options
[] = {
77 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
78 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
79 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
80 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
81 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
82 {"discard", 0, POPT_ARG_NONE
, 0, OPT_DISCARD
, 0, 0},
83 {"overwrite", 0, POPT_ARG_NONE
, 0, OPT_OVERWRITE
, 0, 0},
84 {"subbuf-size", 0, POPT_ARG_STRING
, 0, OPT_SUBBUF_SIZE
, 0, 0},
85 {"num-subbuf", 0, POPT_ARG_INT
, 0, OPT_NUM_SUBBUF
, 0, 0},
86 {"switch-timer", 0, POPT_ARG_INT
, 0, OPT_SWITCH_TIMER
, 0, 0},
87 {"monitor-timer", 0, POPT_ARG_INT
, 0, OPT_MONITOR_TIMER
, 0, 0},
88 {"read-timer", 0, POPT_ARG_INT
, 0, OPT_READ_TIMER
, 0, 0},
89 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
90 {"output", 0, POPT_ARG_STRING
, &opt_output
, 0, 0, 0},
91 {"buffers-uid", 0, POPT_ARG_VAL
, &opt_buffer_uid
, 1, 0, 0},
92 {"buffers-pid", 0, POPT_ARG_VAL
, &opt_buffer_pid
, 1, 0, 0},
93 {"buffers-global", 0, POPT_ARG_VAL
, &opt_buffer_global
, 1, 0, 0},
94 {"tracefile-size", 'C', POPT_ARG_INT
, 0, OPT_TRACEFILE_SIZE
, 0, 0},
95 {"tracefile-count", 'W', POPT_ARG_INT
, 0, OPT_TRACEFILE_COUNT
, 0, 0},
96 {"blocking-timeout", 0, POPT_ARG_INT
, 0, OPT_BLOCKING_TIMEOUT
, 0, 0},
101 * Set default attributes depending on those already defined from the command
104 static void set_default_attr(struct lttng_domain
*dom
)
106 struct lttng_channel_attr default_attr
;
108 memset(&default_attr
, 0, sizeof(default_attr
));
111 lttng_channel_set_default_attr(dom
, &default_attr
);
113 if (chan_opts
.attr
.overwrite
== -1) {
114 chan_opts
.attr
.overwrite
= default_attr
.overwrite
;
116 if (chan_opts
.attr
.subbuf_size
== -1) {
117 chan_opts
.attr
.subbuf_size
= default_attr
.subbuf_size
;
119 if (chan_opts
.attr
.num_subbuf
== -1) {
120 chan_opts
.attr
.num_subbuf
= default_attr
.num_subbuf
;
122 if (chan_opts
.attr
.switch_timer_interval
== -1) {
123 chan_opts
.attr
.switch_timer_interval
= default_attr
.switch_timer_interval
;
125 if (chan_opts
.attr
.read_timer_interval
== -1) {
126 chan_opts
.attr
.read_timer_interval
= default_attr
.read_timer_interval
;
128 if ((int) chan_opts
.attr
.output
== -1) {
129 chan_opts
.attr
.output
= default_attr
.output
;
131 if (chan_opts
.attr
.tracefile_count
== -1) {
132 chan_opts
.attr
.tracefile_count
= default_attr
.tracefile_count
;
134 if (chan_opts
.attr
.tracefile_size
== -1) {
135 chan_opts
.attr
.tracefile_size
= default_attr
.tracefile_size
;
140 * Adding channel using the lttng API.
142 static int enable_channel(char *session_name
)
144 struct lttng_channel
*channel
= NULL
;
145 int ret
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 0;
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
, 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",
210 opt_output
, output_mmap
, output_splice
);
216 handle
= lttng_create_handle(session_name
, &dom
);
217 if (handle
== NULL
) {
222 /* Mi open channels element */
224 LTTNG_ASSERT(writer
);
225 ret
= mi_lttng_channels_open(writer
);
232 /* Strip channel list (format: chan1,chan2,...) */
233 channel_name
= strtok(opt_channels
, ",");
234 while (channel_name
!= NULL
) {
237 /* Validate channel name's length */
238 if (strlen(channel_name
) >= sizeof(chan_opts
.name
)) {
239 ERR("Channel name is too long (max. %zu characters)",
240 sizeof(chan_opts
.name
) - 1);
246 * A dynamically-allocated channel is used in order to allow
247 * the configuration of extended attributes (post-2.9).
249 channel
= lttng_channel_create(&dom
);
251 ERR("Unable to create channel object");
256 /* Copy channel name */
257 strcpy(channel
->name
, channel_name
);
258 channel
->enabled
= 1;
259 extended_ptr
= channel
->attr
.extended
.ptr
;
260 memcpy(&channel
->attr
, &chan_opts
.attr
, sizeof(chan_opts
.attr
));
261 channel
->attr
.extended
.ptr
= extended_ptr
;
262 if (opt_monitor_timer
.set
) {
263 ret
= lttng_channel_set_monitor_timer_interval(channel
,
264 opt_monitor_timer
.interval
);
266 ERR("Failed to set the channel's monitor timer interval");
271 if (opt_blocking_timeout
.set
) {
272 ret
= lttng_channel_set_blocking_timeout(channel
,
273 opt_blocking_timeout
.value
);
275 ERR("Failed to set the channel's blocking timeout");
281 DBG("Enabling channel %s", channel_name
);
283 ret
= lttng_enable_channel(handle
, channel
);
287 case LTTNG_ERR_KERN_CHAN_EXIST
:
288 case LTTNG_ERR_UST_CHAN_EXIST
:
289 case LTTNG_ERR_CHAN_EXIST
:
290 WARN("Channel %s: %s (session %s)", channel_name
,
291 lttng_strerror(ret
), session_name
);
294 case LTTNG_ERR_INVALID_CHANNEL_NAME
:
295 ERR("Invalid channel name: \"%s\". "
296 "Channel names may not start with '.', and "
297 "may not contain '/'.", channel_name
);
301 ERR("Channel %s: %s (session %s)", channel_name
,
302 lttng_strerror(ret
), session_name
);
307 MSG("%s channel %s enabled for session %s",
308 lttng_domain_type_str(dom
.type
),
309 channel_name
, session_name
);
315 /* Mi print the channel element and leave it open */
316 ret
= mi_lttng_channel(writer
, channel
, 1);
322 /* Individual Success ? */
323 ret
= mi_lttng_writer_write_element_bool(writer
,
324 mi_lttng_element_command_success
, success
);
330 /* Close channel element */
331 ret
= mi_lttng_writer_close_element(writer
);
339 channel_name
= strtok(NULL
, ",");
340 lttng_channel_destroy(channel
);
345 /* Close channels element */
346 ret
= mi_lttng_writer_close_element(writer
);
357 lttng_channel_destroy(channel
);
359 /* If more important error happen bypass the warning */
363 /* If more important error happen bypass the warning */
368 lttng_destroy_handle(handle
);
374 * Default value for channel configuration.
376 static void init_channel_config(void)
379 * Put -1 everywhere so we can identify those set by the command line and
380 * those needed to be set by the default values.
382 memset(&chan_opts
.attr
, -1, sizeof(chan_opts
.attr
));
383 chan_opts
.attr
.extended
.ptr
= NULL
;
387 * Add channel to trace session
389 int cmd_enable_channels(int argc
, const char **argv
)
391 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
392 static poptContext pc
;
393 char *session_name
= NULL
;
394 char *opt_arg
= NULL
;
395 const char *leftover
= NULL
;
397 init_channel_config();
399 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
400 poptReadDefaultConfig(pc
, 0);
402 while ((opt
= poptGetNextOpt(pc
)) != -1) {
408 chan_opts
.attr
.overwrite
= 0;
409 DBG("Channel set to discard");
412 chan_opts
.attr
.overwrite
= 1;
413 DBG("Channel set to overwrite");
415 case OPT_SUBBUF_SIZE
:
417 uint64_t rounded_size
;
421 opt_arg
= poptGetOptArg(pc
);
422 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.subbuf_size
) < 0 || !chan_opts
.attr
.subbuf_size
) {
423 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg
);
428 order
= get_count_order_u64(chan_opts
.attr
.subbuf_size
);
429 LTTNG_ASSERT(order
>= 0);
430 rounded_size
= 1ULL << order
;
431 if (rounded_size
< chan_opts
.attr
.subbuf_size
) {
432 ERR("The subbuf size (%" PRIu64
") is rounded and overflows!",
433 chan_opts
.attr
.subbuf_size
);
438 if (rounded_size
!= chan_opts
.attr
.subbuf_size
) {
439 WARN("The subbuf size (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
440 chan_opts
.attr
.subbuf_size
, rounded_size
);
441 chan_opts
.attr
.subbuf_size
= rounded_size
;
444 /* Should now be power of 2 */
445 LTTNG_ASSERT(!((chan_opts
.attr
.subbuf_size
- 1) & chan_opts
.attr
.subbuf_size
));
447 DBG("Channel subbuf size set to %" PRIu64
, chan_opts
.attr
.subbuf_size
);
452 uint64_t rounded_size
;
456 opt_arg
= poptGetOptArg(pc
);
457 chan_opts
.attr
.num_subbuf
= strtoull(opt_arg
, NULL
, 0);
458 if (errno
!= 0 || !chan_opts
.attr
.num_subbuf
|| !isdigit(opt_arg
[0])) {
459 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg
);
464 order
= get_count_order_u64(chan_opts
.attr
.num_subbuf
);
465 LTTNG_ASSERT(order
>= 0);
466 rounded_size
= 1ULL << order
;
467 if (rounded_size
< chan_opts
.attr
.num_subbuf
) {
468 ERR("The number of subbuffers (%" PRIu64
") is rounded and overflows!",
469 chan_opts
.attr
.num_subbuf
);
474 if (rounded_size
!= chan_opts
.attr
.num_subbuf
) {
475 WARN("The number of subbuffers (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
476 chan_opts
.attr
.num_subbuf
, rounded_size
);
477 chan_opts
.attr
.num_subbuf
= rounded_size
;
480 /* Should now be power of 2 */
481 LTTNG_ASSERT(!((chan_opts
.attr
.num_subbuf
- 1) & chan_opts
.attr
.num_subbuf
));
483 DBG("Channel subbuf num set to %" PRIu64
, chan_opts
.attr
.num_subbuf
);
486 case OPT_SWITCH_TIMER
:
491 opt_arg
= poptGetOptArg(pc
);
493 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
494 ERR("Wrong value for --switch-timer parameter: %s", opt_arg
);
499 if (v
!= (uint32_t) v
) {
500 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
504 chan_opts
.attr
.switch_timer_interval
= (uint32_t) v
;
505 DBG("Channel switch timer interval set to %d %s",
506 chan_opts
.attr
.switch_timer_interval
,
515 opt_arg
= poptGetOptArg(pc
);
517 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
518 ERR("Wrong value for --read-timer parameter: %s", opt_arg
);
523 if (v
!= (uint32_t) v
) {
524 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
528 chan_opts
.attr
.read_timer_interval
= (uint32_t) v
;
529 DBG("Channel read timer interval set to %d %s",
530 chan_opts
.attr
.read_timer_interval
,
534 case OPT_MONITOR_TIMER
:
539 opt_arg
= poptGetOptArg(pc
);
541 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
542 ERR("Wrong value for --monitor-timer parameter: %s", opt_arg
);
546 opt_monitor_timer
.interval
= (uint64_t) v
;
547 opt_monitor_timer
.set
= true;
548 DBG("Channel monitor timer interval set to %" PRIu64
" %s",
549 opt_monitor_timer
.interval
,
553 case OPT_BLOCKING_TIMEOUT
:
559 opt_arg
= poptGetOptArg(pc
);
561 if (strcmp(opt_arg
, "inf") == 0) {
562 opt_blocking_timeout
.value
= (int64_t) -1;
563 opt_blocking_timeout
.set
= true;
564 DBG("Channel blocking timeout set to infinity");
568 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
569 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg
);
575 * While LTTng-UST and LTTng-tools will accept a
576 * blocking timeout expressed in µs, the current
577 * tracer implementation relies on poll() which
578 * takes an "int timeout" parameter expressed in
581 * Since the error reporting from the tracer is
582 * not precise, we perform this check here to
583 * provide a helpful error message in case of
586 * The setter (liblttng-ctl) also performs an
590 if (v_msec
!= (int32_t) v_msec
) {
591 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s", opt_arg
);
596 opt_blocking_timeout
.value
= (int64_t) v
;
597 opt_blocking_timeout
.set
= true;
598 DBG("Channel blocking timeout set to %" PRId64
" %s%s",
599 opt_blocking_timeout
.value
,
601 opt_blocking_timeout
.value
== 0 ?
602 " (non-blocking)" : "");
608 case OPT_TRACEFILE_SIZE
:
609 opt_arg
= poptGetOptArg(pc
);
610 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.tracefile_size
) < 0) {
611 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg
);
615 DBG("Maximum tracefile size set to %" PRIu64
,
616 chan_opts
.attr
.tracefile_size
);
618 case OPT_TRACEFILE_COUNT
:
623 opt_arg
= poptGetOptArg(pc
);
624 v
= strtoul(opt_arg
, NULL
, 0);
625 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
626 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg
);
630 if (v
!= (uint32_t) v
) {
631 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
635 chan_opts
.attr
.tracefile_count
= (uint32_t) v
;
636 DBG("Maximum tracefile count set to %" PRIu64
,
637 chan_opts
.attr
.tracefile_count
);
640 case OPT_LIST_OPTIONS
:
641 list_cmd_options(stdout
, long_options
);
649 ret
= print_missing_or_multiple_domains(
650 opt_kernel
+ opt_userspace
, false);
656 if (chan_opts
.attr
.overwrite
== 1 && opt_blocking_timeout
.set
&&
657 opt_blocking_timeout
.value
!= 0) {
658 ERR("You cannot specify --overwrite and --blocking-timeout=N, "
659 "where N is different than 0");
666 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
668 ret
= -LTTNG_ERR_NOMEM
;
672 /* Open command element */
673 ret
= mi_lttng_writer_command_open(writer
,
674 mi_lttng_element_command_enable_channels
);
680 /* Open output element */
681 ret
= mi_lttng_writer_open_element(writer
,
682 mi_lttng_element_command_output
);
689 opt_channels
= (char*) poptGetArg(pc
);
690 if (opt_channels
== NULL
) {
691 ERR("Missing channel name.\n");
697 leftover
= poptGetArg(pc
);
699 ERR("Unknown argument: %s", leftover
);
705 if (!opt_session_name
) {
706 session_name
= get_session_name();
707 if (session_name
== NULL
) {
708 command_ret
= CMD_ERROR
;
713 session_name
= opt_session_name
;
716 command_ret
= enable_channel(session_name
);
724 /* Close output element */
725 ret
= mi_lttng_writer_close_element(writer
);
731 ret
= mi_lttng_writer_write_element_bool(writer
,
732 mi_lttng_element_command_success
, success
);
737 /* Command element close */
738 ret
= mi_lttng_writer_command_close(writer
);
746 if (writer
&& mi_lttng_writer_destroy(writer
)) {
747 /* Preserve original error code */
748 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
751 if (!opt_session_name
&& session_name
) {
755 /* Overwrite ret if an error occurred when enable_channel */
756 ret
= command_ret
? command_ret
: ret
;