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 "../command.h"
28 static struct lttng_channel chan_opts
;
29 static char *opt_channels
;
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
, 0, OPT_HELP
, 0, 0},
78 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
79 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
80 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
81 {"discard", 0, POPT_ARG_NONE
, 0, OPT_DISCARD
, 0, 0},
82 {"overwrite", 0, POPT_ARG_NONE
, 0, OPT_OVERWRITE
, 0, 0},
83 {"subbuf-size", 0, POPT_ARG_STRING
, 0, OPT_SUBBUF_SIZE
, 0, 0},
84 {"num-subbuf", 0, POPT_ARG_INT
, 0, OPT_NUM_SUBBUF
, 0, 0},
85 {"switch-timer", 0, POPT_ARG_INT
, 0, OPT_SWITCH_TIMER
, 0, 0},
86 {"monitor-timer", 0, POPT_ARG_INT
, 0, OPT_MONITOR_TIMER
, 0, 0},
87 {"read-timer", 0, POPT_ARG_INT
, 0, OPT_READ_TIMER
, 0, 0},
88 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
89 {"output", 0, POPT_ARG_STRING
, &opt_output
, 0, 0, 0},
90 {"buffers-uid", 0, POPT_ARG_VAL
, &opt_buffer_uid
, 1, 0, 0},
91 {"buffers-pid", 0, POPT_ARG_VAL
, &opt_buffer_pid
, 1, 0, 0},
92 {"buffers-global", 0, POPT_ARG_VAL
, &opt_buffer_global
, 1, 0, 0},
93 {"tracefile-size", 'C', POPT_ARG_INT
, 0, OPT_TRACEFILE_SIZE
, 0, 0},
94 {"tracefile-count", 'W', POPT_ARG_INT
, 0, OPT_TRACEFILE_COUNT
, 0, 0},
95 {"blocking-timeout", 0, POPT_ARG_INT
, 0, OPT_BLOCKING_TIMEOUT
, 0, 0},
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
)
143 struct lttng_channel
*channel
= NULL
;
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
, 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",
209 opt_output
, output_mmap
, output_splice
);
215 handle
= lttng_create_handle(session_name
, &dom
);
216 if (handle
== NULL
) {
221 /* Mi open channels element */
224 ret
= mi_lttng_channels_open(writer
);
231 /* Strip channel list (format: chan1,chan2,...) */
232 channel_name
= strtok(opt_channels
, ",");
233 while (channel_name
!= NULL
) {
236 /* Validate channel name's length */
237 if (strlen(channel_name
) >= sizeof(chan_opts
.name
)) {
238 ERR("Channel name is too long (max. %zu characters)",
239 sizeof(chan_opts
.name
) - 1);
245 * A dynamically-allocated channel is used in order to allow
246 * the configuration of extended attributes (post-2.9).
248 channel
= lttng_channel_create(&dom
);
250 ERR("Unable to create channel object");
255 /* Copy channel name */
256 strcpy(channel
->name
, channel_name
);
257 channel
->enabled
= 1;
258 extended_ptr
= channel
->attr
.extended
.ptr
;
259 memcpy(&channel
->attr
, &chan_opts
.attr
, sizeof(chan_opts
.attr
));
260 channel
->attr
.extended
.ptr
= extended_ptr
;
261 if (opt_monitor_timer
.set
) {
262 ret
= lttng_channel_set_monitor_timer_interval(channel
,
263 opt_monitor_timer
.interval
);
265 ERR("Failed to set the channel's monitor timer interval");
270 if (opt_blocking_timeout
.set
) {
271 ret
= lttng_channel_set_blocking_timeout(channel
,
272 opt_blocking_timeout
.value
);
274 ERR("Failed to set the channel's blocking timeout");
280 DBG("Enabling channel %s", channel_name
);
282 ret
= lttng_enable_channel(handle
, channel
);
286 case LTTNG_ERR_KERN_CHAN_EXIST
:
287 case LTTNG_ERR_UST_CHAN_EXIST
:
288 case LTTNG_ERR_CHAN_EXIST
:
289 WARN("Channel %s: %s (session %s)", channel_name
,
290 lttng_strerror(ret
), session_name
);
293 case LTTNG_ERR_INVALID_CHANNEL_NAME
:
294 ERR("Invalid channel name: \"%s\". "
295 "Channel names may not start with '.', and "
296 "may not contain '/'.", channel_name
);
300 ERR("Channel %s: %s (session %s)", channel_name
,
301 lttng_strerror(ret
), session_name
);
306 MSG("%s channel %s enabled for session %s",
307 get_domain_str(dom
.type
), channel_name
, session_name
);
313 /* Mi print the channel element and leave it open */
314 ret
= mi_lttng_channel(writer
, channel
, 1);
320 /* Individual Success ? */
321 ret
= mi_lttng_writer_write_element_bool(writer
,
322 mi_lttng_element_command_success
, success
);
328 /* Close channel element */
329 ret
= mi_lttng_writer_close_element(writer
);
337 channel_name
= strtok(NULL
, ",");
338 lttng_channel_destroy(channel
);
343 /* Close channels element */
344 ret
= mi_lttng_writer_close_element(writer
);
355 lttng_channel_destroy(channel
);
357 /* If more important error happen bypass the warning */
361 /* If more important error happen bypass the warning */
366 lttng_destroy_handle(handle
);
372 * Default value for channel configuration.
374 static void init_channel_config(void)
377 * Put -1 everywhere so we can identify those set by the command line and
378 * those needed to be set by the default values.
380 memset(&chan_opts
.attr
, -1, sizeof(chan_opts
.attr
));
381 chan_opts
.attr
.extended
.ptr
= NULL
;
385 * Add channel to trace session
387 int cmd_enable_channels(int argc
, const char **argv
)
389 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
390 static poptContext pc
;
391 char *session_name
= NULL
;
392 char *opt_arg
= NULL
;
393 const char *leftover
= NULL
;
395 init_channel_config();
397 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
398 poptReadDefaultConfig(pc
, 0);
400 while ((opt
= poptGetNextOpt(pc
)) != -1) {
406 chan_opts
.attr
.overwrite
= 0;
407 DBG("Channel set to discard");
410 chan_opts
.attr
.overwrite
= 1;
411 DBG("Channel set to overwrite");
413 case OPT_SUBBUF_SIZE
:
415 uint64_t rounded_size
;
419 opt_arg
= poptGetOptArg(pc
);
420 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.subbuf_size
) < 0 || !chan_opts
.attr
.subbuf_size
) {
421 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg
);
426 order
= get_count_order_u64(chan_opts
.attr
.subbuf_size
);
428 rounded_size
= 1ULL << order
;
429 if (rounded_size
< chan_opts
.attr
.subbuf_size
) {
430 ERR("The subbuf size (%" PRIu64
") is rounded and overflows!",
431 chan_opts
.attr
.subbuf_size
);
436 if (rounded_size
!= chan_opts
.attr
.subbuf_size
) {
437 WARN("The subbuf size (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
438 chan_opts
.attr
.subbuf_size
, rounded_size
);
439 chan_opts
.attr
.subbuf_size
= rounded_size
;
442 /* Should now be power of 2 */
443 assert(!((chan_opts
.attr
.subbuf_size
- 1) & chan_opts
.attr
.subbuf_size
));
445 DBG("Channel subbuf size set to %" PRIu64
, chan_opts
.attr
.subbuf_size
);
450 uint64_t rounded_size
;
454 opt_arg
= poptGetOptArg(pc
);
455 chan_opts
.attr
.num_subbuf
= strtoull(opt_arg
, NULL
, 0);
456 if (errno
!= 0 || !chan_opts
.attr
.num_subbuf
|| !isdigit(opt_arg
[0])) {
457 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg
);
462 order
= get_count_order_u64(chan_opts
.attr
.num_subbuf
);
464 rounded_size
= 1ULL << order
;
465 if (rounded_size
< chan_opts
.attr
.num_subbuf
) {
466 ERR("The number of subbuffers (%" PRIu64
") is rounded and overflows!",
467 chan_opts
.attr
.num_subbuf
);
472 if (rounded_size
!= chan_opts
.attr
.num_subbuf
) {
473 WARN("The number of subbuffers (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
474 chan_opts
.attr
.num_subbuf
, rounded_size
);
475 chan_opts
.attr
.num_subbuf
= rounded_size
;
478 /* Should now be power of 2 */
479 assert(!((chan_opts
.attr
.num_subbuf
- 1) & chan_opts
.attr
.num_subbuf
));
481 DBG("Channel subbuf num set to %" PRIu64
, chan_opts
.attr
.num_subbuf
);
484 case OPT_SWITCH_TIMER
:
489 opt_arg
= poptGetOptArg(pc
);
491 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
492 ERR("Wrong value for --switch-timer parameter: %s", opt_arg
);
497 if (v
!= (uint32_t) v
) {
498 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
502 chan_opts
.attr
.switch_timer_interval
= (uint32_t) v
;
503 DBG("Channel switch timer interval set to %d %s",
504 chan_opts
.attr
.switch_timer_interval
,
513 opt_arg
= poptGetOptArg(pc
);
515 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
516 ERR("Wrong value for --read-timer parameter: %s", opt_arg
);
521 if (v
!= (uint32_t) v
) {
522 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
526 chan_opts
.attr
.read_timer_interval
= (uint32_t) v
;
527 DBG("Channel read timer interval set to %d %s",
528 chan_opts
.attr
.read_timer_interval
,
532 case OPT_MONITOR_TIMER
:
537 opt_arg
= poptGetOptArg(pc
);
539 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
540 ERR("Wrong value for --monitor-timer parameter: %s", opt_arg
);
544 opt_monitor_timer
.interval
= (uint64_t) v
;
545 opt_monitor_timer
.set
= true;
546 DBG("Channel monitor timer interval set to %" PRIu64
" %s",
547 opt_monitor_timer
.interval
,
551 case OPT_BLOCKING_TIMEOUT
:
557 opt_arg
= poptGetOptArg(pc
);
559 if (strcmp(opt_arg
, "inf") == 0) {
560 opt_blocking_timeout
.value
= (int64_t) -1;
561 opt_blocking_timeout
.set
= true;
562 DBG("Channel blocking timeout set to infinity");
566 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
567 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg
);
573 * While LTTng-UST and LTTng-tools will accept a
574 * blocking timeout expressed in µs, the current
575 * tracer implementation relies on poll() which
576 * takes an "int timeout" parameter expressed in
579 * Since the error reporting from the tracer is
580 * not precise, we perform this check here to
581 * provide a helpful error message in case of
584 * The setter (liblttng-ctl) also performs an
588 if (v_msec
!= (int32_t) v_msec
) {
589 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s", opt_arg
);
594 opt_blocking_timeout
.value
= (int64_t) v
;
595 opt_blocking_timeout
.set
= true;
596 DBG("Channel blocking timeout set to %" PRId64
" %s%s",
597 opt_blocking_timeout
.value
,
599 opt_blocking_timeout
.value
== 0 ?
600 " (non-blocking)" : "");
606 case OPT_TRACEFILE_SIZE
:
607 opt_arg
= poptGetOptArg(pc
);
608 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.tracefile_size
) < 0) {
609 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg
);
613 DBG("Maximum tracefile size set to %" PRIu64
,
614 chan_opts
.attr
.tracefile_size
);
616 case OPT_TRACEFILE_COUNT
:
621 opt_arg
= poptGetOptArg(pc
);
622 v
= strtoul(opt_arg
, NULL
, 0);
623 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
624 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg
);
628 if (v
!= (uint32_t) v
) {
629 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
633 chan_opts
.attr
.tracefile_count
= (uint32_t) v
;
634 DBG("Maximum tracefile count set to %" PRIu64
,
635 chan_opts
.attr
.tracefile_count
);
638 case OPT_LIST_OPTIONS
:
639 list_cmd_options(stdout
, long_options
);
647 ret
= print_missing_or_multiple_domains(
648 opt_kernel
+ opt_userspace
, false);
654 if (chan_opts
.attr
.overwrite
== 1 && opt_blocking_timeout
.set
&&
655 opt_blocking_timeout
.value
!= 0) {
656 ERR("You cannot specify --overwrite and --blocking-timeout=N, "
657 "where N is different than 0");
664 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
666 ret
= -LTTNG_ERR_NOMEM
;
670 /* Open command element */
671 ret
= mi_lttng_writer_command_open(writer
,
672 mi_lttng_element_command_enable_channels
);
678 /* Open output element */
679 ret
= mi_lttng_writer_open_element(writer
,
680 mi_lttng_element_command_output
);
687 opt_channels
= (char*) poptGetArg(pc
);
688 if (opt_channels
== NULL
) {
689 ERR("Missing channel name.\n");
695 leftover
= poptGetArg(pc
);
697 ERR("Unknown argument: %s", leftover
);
703 if (!opt_session_name
) {
704 session_name
= get_session_name();
705 if (session_name
== NULL
) {
706 command_ret
= CMD_ERROR
;
711 session_name
= opt_session_name
;
714 command_ret
= enable_channel(session_name
);
722 /* Close output element */
723 ret
= mi_lttng_writer_close_element(writer
);
729 ret
= mi_lttng_writer_write_element_bool(writer
,
730 mi_lttng_element_command_success
, success
);
735 /* Command element close */
736 ret
= mi_lttng_writer_command_close(writer
);
744 if (writer
&& mi_lttng_writer_destroy(writer
)) {
745 /* Preserve original error code */
746 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
749 if (!opt_session_name
&& session_name
) {
753 /* Overwrite ret if an error occurred when enable_channel */
754 ret
= command_ret
? command_ret
: ret
;