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
;
52 static struct mi_writer
*writer
;
54 #ifdef LTTNG_EMBED_HELP
55 static const char help_msg
[] =
56 #include <lttng-enable-channel.1.h>
75 static struct lttng_handle
*handle
;
77 const char *output_mmap
= "mmap";
78 const char *output_splice
= "splice";
80 static struct poptOption long_options
[] = {
81 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
82 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
83 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
84 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
85 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
86 {"discard", 0, POPT_ARG_NONE
, 0, OPT_DISCARD
, 0, 0},
87 {"overwrite", 0, POPT_ARG_NONE
, 0, OPT_OVERWRITE
, 0, 0},
88 {"subbuf-size", 0, POPT_ARG_STRING
, 0, OPT_SUBBUF_SIZE
, 0, 0},
89 {"num-subbuf", 0, POPT_ARG_INT
, 0, OPT_NUM_SUBBUF
, 0, 0},
90 {"switch-timer", 0, POPT_ARG_INT
, 0, OPT_SWITCH_TIMER
, 0, 0},
91 {"monitor-timer", 0, POPT_ARG_INT
, 0, OPT_MONITOR_TIMER
, 0, 0},
92 {"read-timer", 0, POPT_ARG_INT
, 0, OPT_READ_TIMER
, 0, 0},
93 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
94 {"output", 0, POPT_ARG_STRING
, &opt_output
, 0, 0, 0},
95 {"buffers-uid", 0, POPT_ARG_VAL
, &opt_buffer_uid
, 1, 0, 0},
96 {"buffers-pid", 0, POPT_ARG_VAL
, &opt_buffer_pid
, 1, 0, 0},
97 {"buffers-global", 0, POPT_ARG_VAL
, &opt_buffer_global
, 1, 0, 0},
98 {"tracefile-size", 'C', POPT_ARG_INT
, 0, OPT_TRACEFILE_SIZE
, 0, 0},
99 {"tracefile-count", 'W', POPT_ARG_INT
, 0, OPT_TRACEFILE_COUNT
, 0, 0},
100 {0, 0, 0, 0, 0, 0, 0}
104 * Set default attributes depending on those already defined from the command
107 static void set_default_attr(struct lttng_domain
*dom
)
109 struct lttng_channel_attr default_attr
;
112 lttng_channel_set_default_attr(dom
, &default_attr
);
114 if (chan_opts
.attr
.overwrite
== -1) {
115 chan_opts
.attr
.overwrite
= default_attr
.overwrite
;
117 if (chan_opts
.attr
.subbuf_size
== -1) {
118 chan_opts
.attr
.subbuf_size
= default_attr
.subbuf_size
;
120 if (chan_opts
.attr
.num_subbuf
== -1) {
121 chan_opts
.attr
.num_subbuf
= default_attr
.num_subbuf
;
123 if (chan_opts
.attr
.switch_timer_interval
== -1) {
124 chan_opts
.attr
.switch_timer_interval
= default_attr
.switch_timer_interval
;
126 if (chan_opts
.attr
.read_timer_interval
== -1) {
127 chan_opts
.attr
.read_timer_interval
= default_attr
.read_timer_interval
;
129 if ((int) chan_opts
.attr
.output
== -1) {
130 chan_opts
.attr
.output
= default_attr
.output
;
132 if (chan_opts
.attr
.tracefile_count
== -1) {
133 chan_opts
.attr
.tracefile_count
= default_attr
.tracefile_count
;
135 if (chan_opts
.attr
.tracefile_size
== -1) {
136 chan_opts
.attr
.tracefile_size
= default_attr
.tracefile_size
;
141 * Adding channel using the lttng API.
143 static int enable_channel(char *session_name
)
145 struct lttng_channel
*channel
= NULL
;
146 int ret
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 0;
148 struct lttng_domain dom
;
150 memset(&dom
, 0, sizeof(dom
));
152 /* Create lttng domain */
154 dom
.type
= LTTNG_DOMAIN_KERNEL
;
155 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
156 if (opt_buffer_uid
|| opt_buffer_pid
) {
157 ERR("Buffer type not supported for domain -k");
161 } else if (opt_userspace
) {
162 dom
.type
= LTTNG_DOMAIN_UST
;
163 if (opt_buffer_pid
) {
164 dom
.buf_type
= LTTNG_BUFFER_PER_PID
;
166 if (opt_buffer_global
) {
167 ERR("Buffer type not supported for domain -u");
171 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
174 /* Checked by the caller. */
178 set_default_attr(&dom
);
180 if (chan_opts
.attr
.tracefile_size
== 0 && chan_opts
.attr
.tracefile_count
) {
181 ERR("Missing option --tracefile-size. "
182 "A file count without a size won't do anything.");
187 if ((chan_opts
.attr
.tracefile_size
> 0) &&
188 (chan_opts
.attr
.tracefile_size
< chan_opts
.attr
.subbuf_size
)) {
189 WARN("Tracefile size rounded up from (%" PRIu64
") to subbuffer size (%" PRIu64
")",
190 chan_opts
.attr
.tracefile_size
, chan_opts
.attr
.subbuf_size
);
191 chan_opts
.attr
.tracefile_size
= chan_opts
.attr
.subbuf_size
;
194 /* Setting channel output */
196 if (!strncmp(output_mmap
, opt_output
, strlen(output_mmap
))) {
197 chan_opts
.attr
.output
= LTTNG_EVENT_MMAP
;
198 } else if (!strncmp(output_splice
, opt_output
, strlen(output_splice
))) {
199 chan_opts
.attr
.output
= LTTNG_EVENT_SPLICE
;
201 ERR("Unknown output type %s. Possible values are: %s, %s\n",
202 opt_output
, output_mmap
, output_splice
);
208 handle
= lttng_create_handle(session_name
, &dom
);
209 if (handle
== NULL
) {
214 /* Mi open channels element */
217 ret
= mi_lttng_channels_open(writer
);
224 /* Strip channel list (format: chan1,chan2,...) */
225 channel_name
= strtok(opt_channels
, ",");
226 while (channel_name
!= NULL
) {
229 /* Validate channel name's length */
230 if (strlen(channel_name
) >= NAME_MAX
) {
231 ERR("Channel name is too long (max. %zu characters)",
232 sizeof(chan_opts
.name
) - 1);
238 * A dynamically-allocated channel is used in order to allow
239 * the configuration of extended attributes (post-2.9).
241 channel
= lttng_channel_create(&dom
);
243 ERR("Unable to create channel object");
248 /* Copy channel name */
249 strcpy(channel
->name
, channel_name
);
250 channel
->enabled
= 1;
251 extended_ptr
= channel
->attr
.extended
.ptr
;
252 memcpy(&channel
->attr
, &chan_opts
.attr
, sizeof(chan_opts
.attr
));
253 channel
->attr
.extended
.ptr
= extended_ptr
;
254 if (opt_monitor_timer
.set
) {
255 ret
= lttng_channel_set_monitor_timer_interval(channel
,
256 opt_monitor_timer
.interval
);
258 ERR("Failed to set the channel's monitor timer interval");
264 DBG("Enabling channel %s", channel_name
);
266 ret
= lttng_enable_channel(handle
, channel
);
270 case LTTNG_ERR_KERN_CHAN_EXIST
:
271 case LTTNG_ERR_UST_CHAN_EXIST
:
272 case LTTNG_ERR_CHAN_EXIST
:
273 WARN("Channel %s: %s (session %s)", channel_name
,
274 lttng_strerror(ret
), session_name
);
277 case LTTNG_ERR_INVALID_CHANNEL_NAME
:
278 ERR("Invalid channel name: \"%s\". "
279 "Channel names may not start with '.', and "
280 "may not contain '/'.", channel_name
);
284 ERR("Channel %s: %s (session %s)", channel_name
,
285 lttng_strerror(ret
), session_name
);
290 MSG("%s channel %s enabled for session %s",
291 get_domain_str(dom
.type
), channel_name
, session_name
);
297 /* Mi print the channel element and leave it open */
298 ret
= mi_lttng_channel(writer
, channel
, 1);
304 /* Individual Success ? */
305 ret
= mi_lttng_writer_write_element_bool(writer
,
306 mi_lttng_element_command_success
, success
);
312 /* Close channel element */
313 ret
= mi_lttng_writer_close_element(writer
);
321 channel_name
= strtok(NULL
, ",");
322 lttng_channel_destroy(channel
);
327 /* Close channels element */
328 ret
= mi_lttng_writer_close_element(writer
);
339 lttng_channel_destroy(channel
);
341 /* If more important error happen bypass the warning */
345 /* If more important error happen bypass the warning */
350 lttng_destroy_handle(handle
);
356 * Default value for channel configuration.
358 static void init_channel_config(void)
361 * Put -1 everywhere so we can identify those set by the command line and
362 * those needed to be set by the default values.
364 memset(&chan_opts
.attr
, -1, sizeof(chan_opts
.attr
));
365 chan_opts
.attr
.extended
.ptr
= NULL
;
369 * Add channel to trace session
371 int cmd_enable_channels(int argc
, const char **argv
)
373 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
374 static poptContext pc
;
375 char *session_name
= NULL
;
376 char *opt_arg
= NULL
;
378 init_channel_config();
380 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
381 poptReadDefaultConfig(pc
, 0);
383 while ((opt
= poptGetNextOpt(pc
)) != -1) {
389 chan_opts
.attr
.overwrite
= 0;
390 DBG("Channel set to discard");
393 chan_opts
.attr
.overwrite
= 1;
394 DBG("Channel set to overwrite");
396 case OPT_SUBBUF_SIZE
:
398 uint64_t rounded_size
;
402 opt_arg
= poptGetOptArg(pc
);
403 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.subbuf_size
) < 0 || !chan_opts
.attr
.subbuf_size
) {
404 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg
);
409 order
= get_count_order_u64(chan_opts
.attr
.subbuf_size
);
411 rounded_size
= 1ULL << order
;
412 if (rounded_size
< chan_opts
.attr
.subbuf_size
) {
413 ERR("The subbuf size (%" PRIu64
") is rounded and overflows!",
414 chan_opts
.attr
.subbuf_size
);
419 if (rounded_size
!= chan_opts
.attr
.subbuf_size
) {
420 WARN("The subbuf size (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
421 chan_opts
.attr
.subbuf_size
, rounded_size
);
422 chan_opts
.attr
.subbuf_size
= rounded_size
;
425 /* Should now be power of 2 */
426 assert(!((chan_opts
.attr
.subbuf_size
- 1) & chan_opts
.attr
.subbuf_size
));
428 DBG("Channel subbuf size set to %" PRIu64
, chan_opts
.attr
.subbuf_size
);
433 uint64_t rounded_size
;
437 opt_arg
= poptGetOptArg(pc
);
438 chan_opts
.attr
.num_subbuf
= strtoull(opt_arg
, NULL
, 0);
439 if (errno
!= 0 || !chan_opts
.attr
.num_subbuf
|| !isdigit(opt_arg
[0])) {
440 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg
);
445 order
= get_count_order_u64(chan_opts
.attr
.num_subbuf
);
447 rounded_size
= 1ULL << order
;
448 if (rounded_size
< chan_opts
.attr
.num_subbuf
) {
449 ERR("The number of subbuffers (%" PRIu64
") is rounded and overflows!",
450 chan_opts
.attr
.num_subbuf
);
455 if (rounded_size
!= chan_opts
.attr
.num_subbuf
) {
456 WARN("The number of subbuffers (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
457 chan_opts
.attr
.num_subbuf
, rounded_size
);
458 chan_opts
.attr
.num_subbuf
= rounded_size
;
461 /* Should now be power of 2 */
462 assert(!((chan_opts
.attr
.num_subbuf
- 1) & chan_opts
.attr
.num_subbuf
));
464 DBG("Channel subbuf num set to %" PRIu64
, chan_opts
.attr
.num_subbuf
);
467 case OPT_SWITCH_TIMER
:
472 opt_arg
= poptGetOptArg(pc
);
473 v
= strtoul(opt_arg
, NULL
, 0);
474 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
475 ERR("Wrong value in --switch-timer parameter: %s", opt_arg
);
479 if (v
!= (uint32_t) v
) {
480 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
484 chan_opts
.attr
.switch_timer_interval
= (uint32_t) v
;
485 DBG("Channel switch timer interval set to %d", chan_opts
.attr
.switch_timer_interval
);
493 opt_arg
= poptGetOptArg(pc
);
494 v
= strtoul(opt_arg
, NULL
, 0);
495 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
496 ERR("Wrong value in --read-timer parameter: %s", opt_arg
);
500 if (v
!= (uint32_t) v
) {
501 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
505 chan_opts
.attr
.read_timer_interval
= (uint32_t) v
;
506 DBG("Channel read timer interval set to %d", chan_opts
.attr
.read_timer_interval
);
509 case OPT_MONITOR_TIMER
:
514 opt_arg
= poptGetOptArg(pc
);
515 v
= strtoul(opt_arg
, NULL
, 0);
516 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
517 ERR("Wrong value in --monitor-timer parameter: %s", opt_arg
);
521 if (v
!= (uint32_t) v
) {
522 ERR("32-bit overflow in --monitor-timer parameter: %s", opt_arg
);
526 opt_monitor_timer
.interval
= (uint32_t) v
;
527 opt_monitor_timer
.set
= true;
528 DBG("Channel monitor timer interval set to %d", opt_monitor_timer
.interval
);
534 case OPT_TRACEFILE_SIZE
:
535 opt_arg
= poptGetOptArg(pc
);
536 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.tracefile_size
) < 0) {
537 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg
);
541 DBG("Maximum tracefile size set to %" PRIu64
,
542 chan_opts
.attr
.tracefile_size
);
544 case OPT_TRACEFILE_COUNT
:
549 opt_arg
= poptGetOptArg(pc
);
550 v
= strtoul(opt_arg
, NULL
, 0);
551 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
552 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg
);
556 if (v
!= (uint32_t) v
) {
557 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
561 chan_opts
.attr
.tracefile_count
= (uint32_t) v
;
562 DBG("Maximum tracefile count set to %" PRIu64
,
563 chan_opts
.attr
.tracefile_count
);
566 case OPT_LIST_OPTIONS
:
567 list_cmd_options(stdout
, long_options
);
575 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
);
583 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
585 ret
= -LTTNG_ERR_NOMEM
;
589 /* Open command element */
590 ret
= mi_lttng_writer_command_open(writer
,
591 mi_lttng_element_command_enable_channels
);
597 /* Open output element */
598 ret
= mi_lttng_writer_open_element(writer
,
599 mi_lttng_element_command_output
);
606 opt_channels
= (char*) poptGetArg(pc
);
607 if (opt_channels
== NULL
) {
608 ERR("Missing channel name.\n");
614 if (!opt_session_name
) {
615 session_name
= get_session_name();
616 if (session_name
== NULL
) {
617 command_ret
= CMD_ERROR
;
622 session_name
= opt_session_name
;
625 command_ret
= enable_channel(session_name
);
633 /* Close output element */
634 ret
= mi_lttng_writer_close_element(writer
);
640 ret
= mi_lttng_writer_write_element_bool(writer
,
641 mi_lttng_element_command_success
, success
);
646 /* Command element close */
647 ret
= mi_lttng_writer_command_close(writer
);
655 if (writer
&& mi_lttng_writer_destroy(writer
)) {
656 /* Preserve original error code */
657 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
660 if (!opt_session_name
&& session_name
) {
664 /* Overwrite ret if an error occurred when enable_channel */
665 ret
= command_ret
? command_ret
: ret
;