2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/utils.h>
32 #include <common/mi-lttng.h>
34 #include "../command.h"
38 static struct lttng_channel chan_opts
;
39 static char *opt_channels
;
40 static int opt_kernel
;
41 static char *opt_session_name
;
42 static int opt_userspace
;
43 static char *opt_output
;
44 static int opt_buffer_uid
;
45 static int opt_buffer_pid
;
46 static int opt_buffer_global
;
54 } opt_blocking_timeout
;
56 static struct mi_writer
*writer
;
58 #ifdef LTTNG_EMBED_HELP
59 static const char help_msg
[] =
60 #include <lttng-enable-channel.1.h>
80 static struct lttng_handle
*handle
;
82 const char *output_mmap
= "mmap";
83 const char *output_splice
= "splice";
85 static struct poptOption long_options
[] = {
86 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
87 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
88 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
89 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
90 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
91 {"discard", 0, POPT_ARG_NONE
, 0, OPT_DISCARD
, 0, 0},
92 {"overwrite", 0, POPT_ARG_NONE
, 0, OPT_OVERWRITE
, 0, 0},
93 {"subbuf-size", 0, POPT_ARG_STRING
, 0, OPT_SUBBUF_SIZE
, 0, 0},
94 {"num-subbuf", 0, POPT_ARG_INT
, 0, OPT_NUM_SUBBUF
, 0, 0},
95 {"switch-timer", 0, POPT_ARG_INT
, 0, OPT_SWITCH_TIMER
, 0, 0},
96 {"monitor-timer", 0, POPT_ARG_INT
, 0, OPT_MONITOR_TIMER
, 0, 0},
97 {"read-timer", 0, POPT_ARG_INT
, 0, OPT_READ_TIMER
, 0, 0},
98 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
99 {"output", 0, POPT_ARG_STRING
, &opt_output
, 0, 0, 0},
100 {"buffers-uid", 0, POPT_ARG_VAL
, &opt_buffer_uid
, 1, 0, 0},
101 {"buffers-pid", 0, POPT_ARG_VAL
, &opt_buffer_pid
, 1, 0, 0},
102 {"buffers-global", 0, POPT_ARG_VAL
, &opt_buffer_global
, 1, 0, 0},
103 {"tracefile-size", 'C', POPT_ARG_INT
, 0, OPT_TRACEFILE_SIZE
, 0, 0},
104 {"tracefile-count", 'W', POPT_ARG_INT
, 0, OPT_TRACEFILE_COUNT
, 0, 0},
105 {"blocking-timeout", 0, POPT_ARG_INT
, 0, OPT_BLOCKING_TIMEOUT
, 0, 0},
106 {0, 0, 0, 0, 0, 0, 0}
110 * Set default attributes depending on those already defined from the command
113 static void set_default_attr(struct lttng_domain
*dom
)
115 struct lttng_channel_attr default_attr
;
117 memset(&default_attr
, 0, sizeof(default_attr
));
120 lttng_channel_set_default_attr(dom
, &default_attr
);
122 if (chan_opts
.attr
.overwrite
== -1) {
123 chan_opts
.attr
.overwrite
= default_attr
.overwrite
;
125 if (chan_opts
.attr
.subbuf_size
== -1) {
126 chan_opts
.attr
.subbuf_size
= default_attr
.subbuf_size
;
128 if (chan_opts
.attr
.num_subbuf
== -1) {
129 chan_opts
.attr
.num_subbuf
= default_attr
.num_subbuf
;
131 if (chan_opts
.attr
.switch_timer_interval
== -1) {
132 chan_opts
.attr
.switch_timer_interval
= default_attr
.switch_timer_interval
;
134 if (chan_opts
.attr
.read_timer_interval
== -1) {
135 chan_opts
.attr
.read_timer_interval
= default_attr
.read_timer_interval
;
137 if ((int) chan_opts
.attr
.output
== -1) {
138 chan_opts
.attr
.output
= default_attr
.output
;
140 if (chan_opts
.attr
.tracefile_count
== -1) {
141 chan_opts
.attr
.tracefile_count
= default_attr
.tracefile_count
;
143 if (chan_opts
.attr
.tracefile_size
== -1) {
144 chan_opts
.attr
.tracefile_size
= default_attr
.tracefile_size
;
149 * Adding channel using the lttng API.
151 static int enable_channel(char *session_name
)
153 struct lttng_channel
*channel
= NULL
;
154 int ret
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 0;
156 struct lttng_domain dom
;
158 memset(&dom
, 0, sizeof(dom
));
160 /* Validate options. */
162 if (opt_blocking_timeout
.set
) {
163 ERR("Retry timeout option not supported for kernel domain (-k)");
169 /* Create lttng domain */
171 dom
.type
= LTTNG_DOMAIN_KERNEL
;
172 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
173 if (opt_buffer_uid
|| opt_buffer_pid
) {
174 ERR("Buffer type not supported for domain -k");
178 } else if (opt_userspace
) {
179 dom
.type
= LTTNG_DOMAIN_UST
;
180 if (opt_buffer_pid
) {
181 dom
.buf_type
= LTTNG_BUFFER_PER_PID
;
183 if (opt_buffer_global
) {
184 ERR("Buffer type not supported for domain -u");
188 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
191 /* Checked by the caller. */
195 set_default_attr(&dom
);
197 if (chan_opts
.attr
.tracefile_size
== 0 && chan_opts
.attr
.tracefile_count
) {
198 ERR("Missing option --tracefile-size. "
199 "A file count without a size won't do anything.");
204 if ((chan_opts
.attr
.tracefile_size
> 0) &&
205 (chan_opts
.attr
.tracefile_size
< chan_opts
.attr
.subbuf_size
)) {
206 WARN("Tracefile size rounded up from (%" PRIu64
") to subbuffer size (%" PRIu64
")",
207 chan_opts
.attr
.tracefile_size
, chan_opts
.attr
.subbuf_size
);
208 chan_opts
.attr
.tracefile_size
= chan_opts
.attr
.subbuf_size
;
211 /* Setting channel output */
213 if (!strncmp(output_mmap
, opt_output
, strlen(output_mmap
))) {
214 chan_opts
.attr
.output
= LTTNG_EVENT_MMAP
;
215 } else if (!strncmp(output_splice
, opt_output
, strlen(output_splice
))) {
216 chan_opts
.attr
.output
= LTTNG_EVENT_SPLICE
;
218 ERR("Unknown output type %s. Possible values are: %s, %s\n",
219 opt_output
, output_mmap
, output_splice
);
225 handle
= lttng_create_handle(session_name
, &dom
);
226 if (handle
== NULL
) {
231 /* Mi open channels element */
234 ret
= mi_lttng_channels_open(writer
);
241 /* Strip channel list (format: chan1,chan2,...) */
242 channel_name
= strtok(opt_channels
, ",");
243 while (channel_name
!= NULL
) {
246 /* Validate channel name's length */
247 if (strlen(channel_name
) >= NAME_MAX
) {
248 ERR("Channel name is too long (max. %zu characters)",
249 sizeof(chan_opts
.name
) - 1);
255 * A dynamically-allocated channel is used in order to allow
256 * the configuration of extended attributes (post-2.9).
258 channel
= lttng_channel_create(&dom
);
260 ERR("Unable to create channel object");
265 /* Copy channel name */
266 strcpy(channel
->name
, channel_name
);
267 channel
->enabled
= 1;
268 extended_ptr
= channel
->attr
.extended
.ptr
;
269 memcpy(&channel
->attr
, &chan_opts
.attr
, sizeof(chan_opts
.attr
));
270 channel
->attr
.extended
.ptr
= extended_ptr
;
271 if (opt_monitor_timer
.set
) {
272 ret
= lttng_channel_set_monitor_timer_interval(channel
,
273 opt_monitor_timer
.interval
);
275 ERR("Failed to set the channel's monitor timer interval");
280 if (opt_blocking_timeout
.set
) {
281 ret
= lttng_channel_set_blocking_timeout(channel
,
282 opt_blocking_timeout
.value
);
284 ERR("Failed to set the channel's blocking timeout");
290 DBG("Enabling channel %s", channel_name
);
292 ret
= lttng_enable_channel(handle
, channel
);
296 case LTTNG_ERR_KERN_CHAN_EXIST
:
297 case LTTNG_ERR_UST_CHAN_EXIST
:
298 case LTTNG_ERR_CHAN_EXIST
:
299 WARN("Channel %s: %s (session %s)", channel_name
,
300 lttng_strerror(ret
), session_name
);
303 case LTTNG_ERR_INVALID_CHANNEL_NAME
:
304 ERR("Invalid channel name: \"%s\". "
305 "Channel names may not start with '.', and "
306 "may not contain '/'.", channel_name
);
310 ERR("Channel %s: %s (session %s)", channel_name
,
311 lttng_strerror(ret
), session_name
);
316 MSG("%s channel %s enabled for session %s",
317 get_domain_str(dom
.type
), channel_name
, session_name
);
323 /* Mi print the channel element and leave it open */
324 ret
= mi_lttng_channel(writer
, channel
, 1);
330 /* Individual Success ? */
331 ret
= mi_lttng_writer_write_element_bool(writer
,
332 mi_lttng_element_command_success
, success
);
338 /* Close channel element */
339 ret
= mi_lttng_writer_close_element(writer
);
347 channel_name
= strtok(NULL
, ",");
348 lttng_channel_destroy(channel
);
353 /* Close channels element */
354 ret
= mi_lttng_writer_close_element(writer
);
365 lttng_channel_destroy(channel
);
367 /* If more important error happen bypass the warning */
371 /* If more important error happen bypass the warning */
376 lttng_destroy_handle(handle
);
382 * Default value for channel configuration.
384 static void init_channel_config(void)
387 * Put -1 everywhere so we can identify those set by the command line and
388 * those needed to be set by the default values.
390 memset(&chan_opts
.attr
, -1, sizeof(chan_opts
.attr
));
391 chan_opts
.attr
.extended
.ptr
= NULL
;
395 * Add channel to trace session
397 int cmd_enable_channels(int argc
, const char **argv
)
399 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
400 static poptContext pc
;
401 char *session_name
= NULL
;
402 char *opt_arg
= NULL
;
404 init_channel_config();
406 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
407 poptReadDefaultConfig(pc
, 0);
409 while ((opt
= poptGetNextOpt(pc
)) != -1) {
415 chan_opts
.attr
.overwrite
= 0;
416 DBG("Channel set to discard");
419 chan_opts
.attr
.overwrite
= 1;
420 DBG("Channel set to overwrite");
422 case OPT_SUBBUF_SIZE
:
424 uint64_t rounded_size
;
428 opt_arg
= poptGetOptArg(pc
);
429 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.subbuf_size
) < 0 || !chan_opts
.attr
.subbuf_size
) {
430 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg
);
435 order
= get_count_order_u64(chan_opts
.attr
.subbuf_size
);
437 rounded_size
= 1ULL << order
;
438 if (rounded_size
< chan_opts
.attr
.subbuf_size
) {
439 ERR("The subbuf size (%" PRIu64
") is rounded and overflows!",
440 chan_opts
.attr
.subbuf_size
);
445 if (rounded_size
!= chan_opts
.attr
.subbuf_size
) {
446 WARN("The subbuf size (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
447 chan_opts
.attr
.subbuf_size
, rounded_size
);
448 chan_opts
.attr
.subbuf_size
= rounded_size
;
451 /* Should now be power of 2 */
452 assert(!((chan_opts
.attr
.subbuf_size
- 1) & chan_opts
.attr
.subbuf_size
));
454 DBG("Channel subbuf size set to %" PRIu64
, chan_opts
.attr
.subbuf_size
);
459 uint64_t rounded_size
;
463 opt_arg
= poptGetOptArg(pc
);
464 chan_opts
.attr
.num_subbuf
= strtoull(opt_arg
, NULL
, 0);
465 if (errno
!= 0 || !chan_opts
.attr
.num_subbuf
|| !isdigit(opt_arg
[0])) {
466 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg
);
471 order
= get_count_order_u64(chan_opts
.attr
.num_subbuf
);
473 rounded_size
= 1ULL << order
;
474 if (rounded_size
< chan_opts
.attr
.num_subbuf
) {
475 ERR("The number of subbuffers (%" PRIu64
") is rounded and overflows!",
476 chan_opts
.attr
.num_subbuf
);
481 if (rounded_size
!= chan_opts
.attr
.num_subbuf
) {
482 WARN("The number of subbuffers (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
483 chan_opts
.attr
.num_subbuf
, rounded_size
);
484 chan_opts
.attr
.num_subbuf
= rounded_size
;
487 /* Should now be power of 2 */
488 assert(!((chan_opts
.attr
.num_subbuf
- 1) & chan_opts
.attr
.num_subbuf
));
490 DBG("Channel subbuf num set to %" PRIu64
, chan_opts
.attr
.num_subbuf
);
493 case OPT_SWITCH_TIMER
:
498 opt_arg
= poptGetOptArg(pc
);
499 v
= strtoul(opt_arg
, NULL
, 0);
500 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
501 ERR("Wrong value in --switch-timer parameter: %s", opt_arg
);
505 if (v
!= (uint32_t) v
) {
506 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
510 chan_opts
.attr
.switch_timer_interval
= (uint32_t) v
;
511 DBG("Channel switch timer interval set to %d", chan_opts
.attr
.switch_timer_interval
);
519 opt_arg
= poptGetOptArg(pc
);
520 v
= strtoul(opt_arg
, NULL
, 0);
521 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
522 ERR("Wrong value in --read-timer parameter: %s", opt_arg
);
526 if (v
!= (uint32_t) v
) {
527 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
531 chan_opts
.attr
.read_timer_interval
= (uint32_t) v
;
532 DBG("Channel read timer interval set to %d", chan_opts
.attr
.read_timer_interval
);
535 case OPT_MONITOR_TIMER
:
537 unsigned long long v
;
540 opt_arg
= poptGetOptArg(pc
);
541 v
= strtoull(opt_arg
, NULL
, 0);
542 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
543 ERR("Wrong value in --monitor-timer parameter: %s", opt_arg
);
547 opt_monitor_timer
.interval
= (uint64_t) v
;
548 opt_monitor_timer
.set
= true;
549 DBG("Channel monitor timer interval set to %" PRIu64
" (µs)", opt_monitor_timer
.interval
);
552 case OPT_BLOCKING_TIMEOUT
:
554 long long v
; /* in usec */
558 opt_arg
= poptGetOptArg(pc
);
559 v
= strtoll(opt_arg
, NULL
, 0);
560 if (errno
!= 0 || (!isdigit(opt_arg
[0]) && opt_arg
[0] != '-')
562 ERR("Wrong value in --blocking-timeout parameter: %s", opt_arg
);
568 * While LTTng-UST and LTTng-tools will accept
569 * a blocking timeout expressed in µs, the
570 * current tracer implementation relies on
571 * poll() which takes an "int timeout" parameter
574 * Since the error reporting from the tracer
575 * is not precise, we perform this check here
576 * to provide a helpful error message in case of
579 * The setter (liblttng-ctl) also performs an
583 if (v_msec
!= (int32_t) v_msec
) {
584 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s", opt_arg
);
588 } else if (v
!= -1) {
589 ERR("Invalid negative value passed as --blocking-timeout parameter; -1 (block forever) is the only valid negative value");
591 opt_blocking_timeout
.value
= (int64_t) v
;
592 opt_blocking_timeout
.set
= true;
593 DBG("Channel blocking timeout set to %" PRId64
" (µs)",
594 opt_blocking_timeout
.value
);
600 case OPT_TRACEFILE_SIZE
:
601 opt_arg
= poptGetOptArg(pc
);
602 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.tracefile_size
) < 0) {
603 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg
);
607 DBG("Maximum tracefile size set to %" PRIu64
,
608 chan_opts
.attr
.tracefile_size
);
610 case OPT_TRACEFILE_COUNT
:
615 opt_arg
= poptGetOptArg(pc
);
616 v
= strtoul(opt_arg
, NULL
, 0);
617 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
618 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg
);
622 if (v
!= (uint32_t) v
) {
623 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
627 chan_opts
.attr
.tracefile_count
= (uint32_t) v
;
628 DBG("Maximum tracefile count set to %" PRIu64
,
629 chan_opts
.attr
.tracefile_count
);
632 case OPT_LIST_OPTIONS
:
633 list_cmd_options(stdout
, long_options
);
641 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
);
649 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
651 ret
= -LTTNG_ERR_NOMEM
;
655 /* Open command element */
656 ret
= mi_lttng_writer_command_open(writer
,
657 mi_lttng_element_command_enable_channels
);
663 /* Open output element */
664 ret
= mi_lttng_writer_open_element(writer
,
665 mi_lttng_element_command_output
);
672 opt_channels
= (char*) poptGetArg(pc
);
673 if (opt_channels
== NULL
) {
674 ERR("Missing channel name.\n");
680 if (!opt_session_name
) {
681 session_name
= get_session_name();
682 if (session_name
== NULL
) {
683 command_ret
= CMD_ERROR
;
688 session_name
= opt_session_name
;
691 command_ret
= enable_channel(session_name
);
699 /* Close output element */
700 ret
= mi_lttng_writer_close_element(writer
);
706 ret
= mi_lttng_writer_write_element_bool(writer
,
707 mi_lttng_element_command_success
, success
);
712 /* Command element close */
713 ret
= mi_lttng_writer_command_close(writer
);
721 if (writer
&& mi_lttng_writer_destroy(writer
)) {
722 /* Preserve original error code */
723 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
726 if (!opt_session_name
&& session_name
) {
730 /* Overwrite ret if an error occurred when enable_channel */
731 ret
= command_ret
? command_ret
: ret
;