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
;
111 memset(&default_attr
, 0, sizeof(default_attr
));
114 lttng_channel_set_default_attr(dom
, &default_attr
);
116 if (chan_opts
.attr
.overwrite
== -1) {
117 chan_opts
.attr
.overwrite
= default_attr
.overwrite
;
119 if (chan_opts
.attr
.subbuf_size
== -1) {
120 chan_opts
.attr
.subbuf_size
= default_attr
.subbuf_size
;
122 if (chan_opts
.attr
.num_subbuf
== -1) {
123 chan_opts
.attr
.num_subbuf
= default_attr
.num_subbuf
;
125 if (chan_opts
.attr
.switch_timer_interval
== -1) {
126 chan_opts
.attr
.switch_timer_interval
= default_attr
.switch_timer_interval
;
128 if (chan_opts
.attr
.read_timer_interval
== -1) {
129 chan_opts
.attr
.read_timer_interval
= default_attr
.read_timer_interval
;
131 if ((int) chan_opts
.attr
.output
== -1) {
132 chan_opts
.attr
.output
= default_attr
.output
;
134 if (chan_opts
.attr
.tracefile_count
== -1) {
135 chan_opts
.attr
.tracefile_count
= default_attr
.tracefile_count
;
137 if (chan_opts
.attr
.tracefile_size
== -1) {
138 chan_opts
.attr
.tracefile_size
= default_attr
.tracefile_size
;
143 * Adding channel using the lttng API.
145 static int enable_channel(char *session_name
)
147 struct lttng_channel
*channel
= NULL
;
148 int ret
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 0;
150 struct lttng_domain dom
;
152 memset(&dom
, 0, sizeof(dom
));
154 /* Create lttng domain */
156 dom
.type
= LTTNG_DOMAIN_KERNEL
;
157 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
158 if (opt_buffer_uid
|| opt_buffer_pid
) {
159 ERR("Buffer type not supported for domain -k");
163 } else if (opt_userspace
) {
164 dom
.type
= LTTNG_DOMAIN_UST
;
165 if (opt_buffer_pid
) {
166 dom
.buf_type
= LTTNG_BUFFER_PER_PID
;
168 if (opt_buffer_global
) {
169 ERR("Buffer type not supported for domain -u");
173 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
176 /* Checked by the caller. */
180 set_default_attr(&dom
);
182 if (chan_opts
.attr
.tracefile_size
== 0 && chan_opts
.attr
.tracefile_count
) {
183 ERR("Missing option --tracefile-size. "
184 "A file count without a size won't do anything.");
189 if ((chan_opts
.attr
.tracefile_size
> 0) &&
190 (chan_opts
.attr
.tracefile_size
< chan_opts
.attr
.subbuf_size
)) {
191 WARN("Tracefile size rounded up from (%" PRIu64
") to subbuffer size (%" PRIu64
")",
192 chan_opts
.attr
.tracefile_size
, chan_opts
.attr
.subbuf_size
);
193 chan_opts
.attr
.tracefile_size
= chan_opts
.attr
.subbuf_size
;
196 /* Setting channel output */
198 if (!strncmp(output_mmap
, opt_output
, strlen(output_mmap
))) {
199 chan_opts
.attr
.output
= LTTNG_EVENT_MMAP
;
200 } else if (!strncmp(output_splice
, opt_output
, strlen(output_splice
))) {
201 chan_opts
.attr
.output
= LTTNG_EVENT_SPLICE
;
203 ERR("Unknown output type %s. Possible values are: %s, %s\n",
204 opt_output
, output_mmap
, output_splice
);
210 handle
= lttng_create_handle(session_name
, &dom
);
211 if (handle
== NULL
) {
216 /* Mi open channels element */
219 ret
= mi_lttng_channels_open(writer
);
226 /* Strip channel list (format: chan1,chan2,...) */
227 channel_name
= strtok(opt_channels
, ",");
228 while (channel_name
!= NULL
) {
231 /* Validate channel name's length */
232 if (strlen(channel_name
) >= NAME_MAX
) {
233 ERR("Channel name is too long (max. %zu characters)",
234 sizeof(chan_opts
.name
) - 1);
240 * A dynamically-allocated channel is used in order to allow
241 * the configuration of extended attributes (post-2.9).
243 channel
= lttng_channel_create(&dom
);
245 ERR("Unable to create channel object");
250 /* Copy channel name */
251 strcpy(channel
->name
, channel_name
);
252 channel
->enabled
= 1;
253 extended_ptr
= channel
->attr
.extended
.ptr
;
254 memcpy(&channel
->attr
, &chan_opts
.attr
, sizeof(chan_opts
.attr
));
255 channel
->attr
.extended
.ptr
= extended_ptr
;
256 if (opt_monitor_timer
.set
) {
257 ret
= lttng_channel_set_monitor_timer_interval(channel
,
258 opt_monitor_timer
.interval
);
260 ERR("Failed to set the channel's monitor timer interval");
266 DBG("Enabling channel %s", channel_name
);
268 ret
= lttng_enable_channel(handle
, channel
);
272 case LTTNG_ERR_KERN_CHAN_EXIST
:
273 case LTTNG_ERR_UST_CHAN_EXIST
:
274 case LTTNG_ERR_CHAN_EXIST
:
275 WARN("Channel %s: %s (session %s)", channel_name
,
276 lttng_strerror(ret
), session_name
);
279 case LTTNG_ERR_INVALID_CHANNEL_NAME
:
280 ERR("Invalid channel name: \"%s\". "
281 "Channel names may not start with '.', and "
282 "may not contain '/'.", channel_name
);
286 ERR("Channel %s: %s (session %s)", channel_name
,
287 lttng_strerror(ret
), session_name
);
292 MSG("%s channel %s enabled for session %s",
293 get_domain_str(dom
.type
), channel_name
, session_name
);
299 /* Mi print the channel element and leave it open */
300 ret
= mi_lttng_channel(writer
, channel
, 1);
306 /* Individual Success ? */
307 ret
= mi_lttng_writer_write_element_bool(writer
,
308 mi_lttng_element_command_success
, success
);
314 /* Close channel element */
315 ret
= mi_lttng_writer_close_element(writer
);
323 channel_name
= strtok(NULL
, ",");
324 lttng_channel_destroy(channel
);
329 /* Close channels element */
330 ret
= mi_lttng_writer_close_element(writer
);
341 lttng_channel_destroy(channel
);
343 /* If more important error happen bypass the warning */
347 /* If more important error happen bypass the warning */
352 lttng_destroy_handle(handle
);
358 * Default value for channel configuration.
360 static void init_channel_config(void)
363 * Put -1 everywhere so we can identify those set by the command line and
364 * those needed to be set by the default values.
366 memset(&chan_opts
.attr
, -1, sizeof(chan_opts
.attr
));
367 chan_opts
.attr
.extended
.ptr
= NULL
;
371 * Add channel to trace session
373 int cmd_enable_channels(int argc
, const char **argv
)
375 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
376 static poptContext pc
;
377 char *session_name
= NULL
;
378 char *opt_arg
= NULL
;
380 init_channel_config();
382 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
383 poptReadDefaultConfig(pc
, 0);
385 while ((opt
= poptGetNextOpt(pc
)) != -1) {
391 chan_opts
.attr
.overwrite
= 0;
392 DBG("Channel set to discard");
395 chan_opts
.attr
.overwrite
= 1;
396 DBG("Channel set to overwrite");
398 case OPT_SUBBUF_SIZE
:
400 uint64_t rounded_size
;
404 opt_arg
= poptGetOptArg(pc
);
405 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.subbuf_size
) < 0 || !chan_opts
.attr
.subbuf_size
) {
406 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg
);
411 order
= get_count_order_u64(chan_opts
.attr
.subbuf_size
);
413 rounded_size
= 1ULL << order
;
414 if (rounded_size
< chan_opts
.attr
.subbuf_size
) {
415 ERR("The subbuf size (%" PRIu64
") is rounded and overflows!",
416 chan_opts
.attr
.subbuf_size
);
421 if (rounded_size
!= chan_opts
.attr
.subbuf_size
) {
422 WARN("The subbuf size (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
423 chan_opts
.attr
.subbuf_size
, rounded_size
);
424 chan_opts
.attr
.subbuf_size
= rounded_size
;
427 /* Should now be power of 2 */
428 assert(!((chan_opts
.attr
.subbuf_size
- 1) & chan_opts
.attr
.subbuf_size
));
430 DBG("Channel subbuf size set to %" PRIu64
, chan_opts
.attr
.subbuf_size
);
435 uint64_t rounded_size
;
439 opt_arg
= poptGetOptArg(pc
);
440 chan_opts
.attr
.num_subbuf
= strtoull(opt_arg
, NULL
, 0);
441 if (errno
!= 0 || !chan_opts
.attr
.num_subbuf
|| !isdigit(opt_arg
[0])) {
442 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg
);
447 order
= get_count_order_u64(chan_opts
.attr
.num_subbuf
);
449 rounded_size
= 1ULL << order
;
450 if (rounded_size
< chan_opts
.attr
.num_subbuf
) {
451 ERR("The number of subbuffers (%" PRIu64
") is rounded and overflows!",
452 chan_opts
.attr
.num_subbuf
);
457 if (rounded_size
!= chan_opts
.attr
.num_subbuf
) {
458 WARN("The number of subbuffers (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
459 chan_opts
.attr
.num_subbuf
, rounded_size
);
460 chan_opts
.attr
.num_subbuf
= rounded_size
;
463 /* Should now be power of 2 */
464 assert(!((chan_opts
.attr
.num_subbuf
- 1) & chan_opts
.attr
.num_subbuf
));
466 DBG("Channel subbuf num set to %" PRIu64
, chan_opts
.attr
.num_subbuf
);
469 case OPT_SWITCH_TIMER
:
474 opt_arg
= poptGetOptArg(pc
);
475 v
= strtoul(opt_arg
, NULL
, 0);
476 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
477 ERR("Wrong value in --switch-timer parameter: %s", opt_arg
);
481 if (v
!= (uint32_t) v
) {
482 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
486 chan_opts
.attr
.switch_timer_interval
= (uint32_t) v
;
487 DBG("Channel switch timer interval set to %d", chan_opts
.attr
.switch_timer_interval
);
495 opt_arg
= poptGetOptArg(pc
);
496 v
= strtoul(opt_arg
, NULL
, 0);
497 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
498 ERR("Wrong value in --read-timer parameter: %s", opt_arg
);
502 if (v
!= (uint32_t) v
) {
503 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
507 chan_opts
.attr
.read_timer_interval
= (uint32_t) v
;
508 DBG("Channel read timer interval set to %d", chan_opts
.attr
.read_timer_interval
);
511 case OPT_MONITOR_TIMER
:
516 opt_arg
= poptGetOptArg(pc
);
517 v
= strtoul(opt_arg
, NULL
, 0);
518 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
519 ERR("Wrong value in --monitor-timer parameter: %s", opt_arg
);
523 if (v
!= (uint32_t) v
) {
524 ERR("32-bit overflow in --monitor-timer parameter: %s", opt_arg
);
528 opt_monitor_timer
.interval
= (uint32_t) v
;
529 opt_monitor_timer
.set
= true;
530 DBG("Channel monitor timer interval set to %d", opt_monitor_timer
.interval
);
536 case OPT_TRACEFILE_SIZE
:
537 opt_arg
= poptGetOptArg(pc
);
538 if (utils_parse_size_suffix(opt_arg
, &chan_opts
.attr
.tracefile_size
) < 0) {
539 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg
);
543 DBG("Maximum tracefile size set to %" PRIu64
,
544 chan_opts
.attr
.tracefile_size
);
546 case OPT_TRACEFILE_COUNT
:
551 opt_arg
= poptGetOptArg(pc
);
552 v
= strtoul(opt_arg
, NULL
, 0);
553 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
554 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg
);
558 if (v
!= (uint32_t) v
) {
559 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
563 chan_opts
.attr
.tracefile_count
= (uint32_t) v
;
564 DBG("Maximum tracefile count set to %" PRIu64
,
565 chan_opts
.attr
.tracefile_count
);
568 case OPT_LIST_OPTIONS
:
569 list_cmd_options(stdout
, long_options
);
577 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
);
585 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
587 ret
= -LTTNG_ERR_NOMEM
;
591 /* Open command element */
592 ret
= mi_lttng_writer_command_open(writer
,
593 mi_lttng_element_command_enable_channels
);
599 /* Open output element */
600 ret
= mi_lttng_writer_open_element(writer
,
601 mi_lttng_element_command_output
);
608 opt_channels
= (char*) poptGetArg(pc
);
609 if (opt_channels
== NULL
) {
610 ERR("Missing channel name.\n");
616 if (!opt_session_name
) {
617 session_name
= get_session_name();
618 if (session_name
== NULL
) {
619 command_ret
= CMD_ERROR
;
624 session_name
= opt_session_name
;
627 command_ret
= enable_channel(session_name
);
635 /* Close output element */
636 ret
= mi_lttng_writer_close_element(writer
);
642 ret
= mi_lttng_writer_write_element_bool(writer
,
643 mi_lttng_element_command_success
, success
);
648 /* Command element close */
649 ret
= mi_lttng_writer_command_close(writer
);
657 if (writer
&& mi_lttng_writer_destroy(writer
)) {
658 /* Preserve original error code */
659 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
662 if (!opt_session_name
&& session_name
) {
666 /* Overwrite ret if an error occurred when enable_channel */
667 ret
= command_ret
? command_ret
: ret
;