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 char *opt_channels
;
39 static int opt_kernel
;
40 static char *opt_session_name
;
41 static int opt_userspace
;
42 static struct lttng_channel chan
;
43 static char *opt_output
;
44 static int opt_buffer_uid
;
45 static int opt_buffer_pid
;
46 static int opt_buffer_global
;
48 static struct mi_writer
*writer
;
64 static struct lttng_handle
*handle
;
66 const char *output_mmap
= "mmap";
67 const char *output_splice
= "splice";
69 static struct poptOption long_options
[] = {
70 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
71 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
72 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
73 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
74 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
75 {"discard", 0, POPT_ARG_NONE
, 0, OPT_DISCARD
, 0, 0},
76 {"overwrite", 0, POPT_ARG_NONE
, 0, OPT_OVERWRITE
, 0, 0},
77 {"subbuf-size", 0, POPT_ARG_STRING
, 0, OPT_SUBBUF_SIZE
, 0, 0},
78 {"num-subbuf", 0, POPT_ARG_INT
, 0, OPT_NUM_SUBBUF
, 0, 0},
79 {"switch-timer", 0, POPT_ARG_INT
, 0, OPT_SWITCH_TIMER
, 0, 0},
80 {"read-timer", 0, POPT_ARG_INT
, 0, OPT_READ_TIMER
, 0, 0},
81 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
82 {"output", 0, POPT_ARG_STRING
, &opt_output
, 0, 0, 0},
83 {"buffers-uid", 0, POPT_ARG_VAL
, &opt_buffer_uid
, 1, 0, 0},
84 {"buffers-pid", 0, POPT_ARG_VAL
, &opt_buffer_pid
, 1, 0, 0},
85 {"buffers-global", 0, POPT_ARG_VAL
, &opt_buffer_global
, 1, 0, 0},
86 {"tracefile-size", 'C', POPT_ARG_INT
, 0, OPT_TRACEFILE_SIZE
, 0, 0},
87 {"tracefile-count", 'W', POPT_ARG_INT
, 0, OPT_TRACEFILE_COUNT
, 0, 0},
92 * Set default attributes depending on those already defined from the command
95 static void set_default_attr(struct lttng_domain
*dom
)
97 struct lttng_channel_attr default_attr
;
100 lttng_channel_set_default_attr(dom
, &default_attr
);
102 if (chan
.attr
.overwrite
== -1) {
103 chan
.attr
.overwrite
= default_attr
.overwrite
;
105 if (chan
.attr
.subbuf_size
== -1) {
106 chan
.attr
.subbuf_size
= default_attr
.subbuf_size
;
108 if (chan
.attr
.num_subbuf
== -1) {
109 chan
.attr
.num_subbuf
= default_attr
.num_subbuf
;
111 if (chan
.attr
.switch_timer_interval
== -1) {
112 chan
.attr
.switch_timer_interval
= default_attr
.switch_timer_interval
;
114 if (chan
.attr
.read_timer_interval
== -1) {
115 chan
.attr
.read_timer_interval
= default_attr
.read_timer_interval
;
117 if ((int) chan
.attr
.output
== -1) {
118 chan
.attr
.output
= default_attr
.output
;
120 if (chan
.attr
.tracefile_count
== -1) {
121 chan
.attr
.tracefile_count
= default_attr
.tracefile_count
;
123 if (chan
.attr
.tracefile_size
== -1) {
124 chan
.attr
.tracefile_size
= default_attr
.tracefile_size
;
129 * Adding channel using the lttng API.
131 static int enable_channel(char *session_name
)
133 int ret
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 0;
135 struct lttng_domain dom
;
137 memset(&dom
, 0, sizeof(dom
));
139 /* Create lttng domain */
141 dom
.type
= LTTNG_DOMAIN_KERNEL
;
142 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
143 if (opt_buffer_uid
|| opt_buffer_pid
) {
144 ERR("Buffer type not supported for domain -k");
148 } else if (opt_userspace
) {
149 dom
.type
= LTTNG_DOMAIN_UST
;
150 if (opt_buffer_pid
) {
151 dom
.buf_type
= LTTNG_BUFFER_PER_PID
;
153 if (opt_buffer_global
) {
154 ERR("Buffer type not supported for domain -u");
158 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
161 /* Checked by the caller. */
165 set_default_attr(&dom
);
167 if (chan
.attr
.tracefile_size
== 0 && chan
.attr
.tracefile_count
) {
168 ERR("Missing option --tracefile-size. "
169 "A file count without a size won't do anything.");
174 if ((chan
.attr
.tracefile_size
> 0) &&
175 (chan
.attr
.tracefile_size
< chan
.attr
.subbuf_size
)) {
176 WARN("Tracefile size rounded up from (%" PRIu64
") to subbuffer size (%" PRIu64
")",
177 chan
.attr
.tracefile_size
, chan
.attr
.subbuf_size
);
178 chan
.attr
.tracefile_size
= chan
.attr
.subbuf_size
;
181 /* Setting channel output */
183 if (!strncmp(output_mmap
, opt_output
, strlen(output_mmap
))) {
184 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
185 } else if (!strncmp(output_splice
, opt_output
, strlen(output_splice
))) {
186 chan
.attr
.output
= LTTNG_EVENT_SPLICE
;
188 ERR("Unknown output type %s. Possible values are: %s, %s\n",
189 opt_output
, output_mmap
, output_splice
);
195 handle
= lttng_create_handle(session_name
, &dom
);
196 if (handle
== NULL
) {
201 /* Mi open channels element */
204 ret
= mi_lttng_channels_open(writer
);
211 /* Strip channel list (format: chan1,chan2,...) */
212 channel_name
= strtok(opt_channels
, ",");
213 while (channel_name
!= NULL
) {
214 /* Validate channel name's length */
215 if (strlen(channel_name
) >= NAME_MAX
) {
216 ERR("Channel name is too long (max. %zu characters)",
217 sizeof(chan
.name
) - 1);
222 /* Copy channel name */
223 strcpy(chan
.name
, channel_name
);
225 DBG("Enabling channel %s", channel_name
);
227 ret
= lttng_enable_channel(handle
, &chan
);
231 case LTTNG_ERR_KERN_CHAN_EXIST
:
232 case LTTNG_ERR_UST_CHAN_EXIST
:
233 case LTTNG_ERR_CHAN_EXIST
:
234 WARN("Channel %s: %s (session %s)", channel_name
,
235 lttng_strerror(ret
), session_name
);
238 case LTTNG_ERR_INVALID_CHANNEL_NAME
:
239 ERR("Invalid channel name: \"%s\". "
240 "Channel names may not start with '.', and "
241 "may not contain '/'.", channel_name
);
245 ERR("Channel %s: %s (session %s)", channel_name
,
246 lttng_strerror(ret
), session_name
);
251 MSG("%s channel %s enabled for session %s",
252 get_domain_str(dom
.type
), channel_name
, session_name
);
258 /* Mi print the channel element and leave it open */
259 ret
= mi_lttng_channel(writer
, &chan
, 1);
265 /* Individual Success ? */
266 ret
= mi_lttng_writer_write_element_bool(writer
,
267 mi_lttng_element_command_success
, success
);
273 /* Close channel element */
274 ret
= mi_lttng_writer_close_element(writer
);
282 channel_name
= strtok(NULL
, ",");
286 /* Close channels element */
287 ret
= mi_lttng_writer_close_element(writer
);
297 /* If more important error happen bypass the warning */
301 /* If more important error happen bypass the warning */
306 lttng_destroy_handle(handle
);
312 * Default value for channel configuration.
314 static void init_channel_config(void)
317 * Put -1 everywhere so we can identify those set by the command line and
318 * those needed to be set by the default values.
320 memset(&chan
.attr
, -1, sizeof(chan
.attr
));
321 chan
.attr
.extended
.ptr
= NULL
;
325 * Add channel to trace session
327 int cmd_enable_channels(int argc
, const char **argv
)
329 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
330 static poptContext pc
;
331 char *session_name
= NULL
;
332 char *opt_arg
= NULL
;
334 init_channel_config();
336 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
337 poptReadDefaultConfig(pc
, 0);
339 while ((opt
= poptGetNextOpt(pc
)) != -1) {
345 chan
.attr
.overwrite
= 0;
346 DBG("Channel set to discard");
349 chan
.attr
.overwrite
= 1;
350 DBG("Channel set to overwrite");
352 case OPT_SUBBUF_SIZE
:
354 uint64_t rounded_size
;
358 opt_arg
= poptGetOptArg(pc
);
359 if (utils_parse_size_suffix(opt_arg
, &chan
.attr
.subbuf_size
) < 0 || !chan
.attr
.subbuf_size
) {
360 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg
);
365 order
= get_count_order_u64(chan
.attr
.subbuf_size
);
367 rounded_size
= 1ULL << order
;
368 if (rounded_size
< chan
.attr
.subbuf_size
) {
369 ERR("The subbuf size (%" PRIu64
") is rounded and overflows!",
370 chan
.attr
.subbuf_size
);
375 if (rounded_size
!= chan
.attr
.subbuf_size
) {
376 WARN("The subbuf size (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
377 chan
.attr
.subbuf_size
, rounded_size
);
378 chan
.attr
.subbuf_size
= rounded_size
;
381 /* Should now be power of 2 */
382 assert(!((chan
.attr
.subbuf_size
- 1) & chan
.attr
.subbuf_size
));
384 DBG("Channel subbuf size set to %" PRIu64
, chan
.attr
.subbuf_size
);
389 uint64_t rounded_size
;
393 opt_arg
= poptGetOptArg(pc
);
394 chan
.attr
.num_subbuf
= strtoull(opt_arg
, NULL
, 0);
395 if (errno
!= 0 || !chan
.attr
.num_subbuf
|| !isdigit(opt_arg
[0])) {
396 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg
);
401 order
= get_count_order_u64(chan
.attr
.num_subbuf
);
403 rounded_size
= 1ULL << order
;
404 if (rounded_size
< chan
.attr
.num_subbuf
) {
405 ERR("The number of subbuffers (%" PRIu64
") is rounded and overflows!",
406 chan
.attr
.num_subbuf
);
411 if (rounded_size
!= chan
.attr
.num_subbuf
) {
412 WARN("The number of subbuffers (%" PRIu64
") is rounded to the next power of 2 (%" PRIu64
")",
413 chan
.attr
.num_subbuf
, rounded_size
);
414 chan
.attr
.num_subbuf
= rounded_size
;
417 /* Should now be power of 2 */
418 assert(!((chan
.attr
.num_subbuf
- 1) & chan
.attr
.num_subbuf
));
420 DBG("Channel subbuf num set to %" PRIu64
, chan
.attr
.num_subbuf
);
423 case OPT_SWITCH_TIMER
:
428 opt_arg
= poptGetOptArg(pc
);
429 v
= strtoul(opt_arg
, NULL
, 0);
430 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
431 ERR("Wrong value in --switch-timer parameter: %s", opt_arg
);
435 if (v
!= (uint32_t) v
) {
436 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
440 chan
.attr
.switch_timer_interval
= (uint32_t) v
;
441 DBG("Channel switch timer interval set to %d", chan
.attr
.switch_timer_interval
);
449 opt_arg
= poptGetOptArg(pc
);
450 v
= strtoul(opt_arg
, NULL
, 0);
451 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
452 ERR("Wrong value in --read-timer parameter: %s", opt_arg
);
456 if (v
!= (uint32_t) v
) {
457 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
461 chan
.attr
.read_timer_interval
= (uint32_t) v
;
462 DBG("Channel read timer interval set to %d", chan
.attr
.read_timer_interval
);
468 case OPT_TRACEFILE_SIZE
:
469 opt_arg
= poptGetOptArg(pc
);
470 if (utils_parse_size_suffix(opt_arg
, &chan
.attr
.tracefile_size
) < 0) {
471 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg
);
475 DBG("Maximum tracefile size set to %" PRIu64
,
476 chan
.attr
.tracefile_size
);
478 case OPT_TRACEFILE_COUNT
:
483 opt_arg
= poptGetOptArg(pc
);
484 v
= strtoul(opt_arg
, NULL
, 0);
485 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
486 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg
);
490 if (v
!= (uint32_t) v
) {
491 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
495 chan
.attr
.tracefile_count
= (uint32_t) v
;
496 DBG("Maximum tracefile count set to %" PRIu64
,
497 chan
.attr
.tracefile_count
);
500 case OPT_LIST_OPTIONS
:
501 list_cmd_options(stdout
, long_options
);
509 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
);
517 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
519 ret
= -LTTNG_ERR_NOMEM
;
523 /* Open command element */
524 ret
= mi_lttng_writer_command_open(writer
,
525 mi_lttng_element_command_enable_channels
);
531 /* Open output element */
532 ret
= mi_lttng_writer_open_element(writer
,
533 mi_lttng_element_command_output
);
540 opt_channels
= (char*) poptGetArg(pc
);
541 if (opt_channels
== NULL
) {
542 ERR("Missing channel name.\n");
548 if (!opt_session_name
) {
549 session_name
= get_session_name();
550 if (session_name
== NULL
) {
551 command_ret
= CMD_ERROR
;
556 session_name
= opt_session_name
;
559 command_ret
= enable_channel(session_name
);
567 /* Close output element */
568 ret
= mi_lttng_writer_close_element(writer
);
574 ret
= mi_lttng_writer_write_element_bool(writer
,
575 mi_lttng_element_command_success
, success
);
580 /* Command element close */
581 ret
= mi_lttng_writer_command_close(writer
);
589 if (writer
&& mi_lttng_writer_destroy(writer
)) {
590 /* Preserve original error code */
591 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
594 if (!opt_session_name
&& session_name
) {
598 /* Overwrite ret if an error occurred when enable_channel */
599 ret
= command_ret
? command_ret
: ret
;