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>
28 #include "../command.h"
30 #include <src/common/sessiond-comm/sessiond-comm.h>
31 #include <src/common/utils.h>
33 static char *opt_channels
;
34 static int opt_kernel
;
35 static char *opt_session_name
;
36 static int opt_userspace
;
37 static struct lttng_channel chan
;
38 static char *opt_output
;
39 static int opt_buffer_uid
;
40 static int opt_buffer_pid
;
41 static int opt_buffer_global
;
57 static struct lttng_handle
*handle
;
59 const char *output_mmap
= "mmap";
60 const char *output_splice
= "splice";
62 static struct poptOption long_options
[] = {
63 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
64 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
65 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
66 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
67 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
68 {"discard", 0, POPT_ARG_NONE
, 0, OPT_DISCARD
, 0, 0},
69 {"overwrite", 0, POPT_ARG_NONE
, 0, OPT_OVERWRITE
, 0, 0},
70 {"subbuf-size", 0, POPT_ARG_STRING
, 0, OPT_SUBBUF_SIZE
, 0, 0},
71 {"num-subbuf", 0, POPT_ARG_INT
, 0, OPT_NUM_SUBBUF
, 0, 0},
72 {"switch-timer", 0, POPT_ARG_INT
, 0, OPT_SWITCH_TIMER
, 0, 0},
73 {"read-timer", 0, POPT_ARG_INT
, 0, OPT_READ_TIMER
, 0, 0},
74 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
75 {"output", 0, POPT_ARG_STRING
, &opt_output
, 0, 0, 0},
76 {"buffers-uid", 0, POPT_ARG_VAL
, &opt_buffer_uid
, 1, 0, 0},
77 {"buffers-pid", 0, POPT_ARG_VAL
, &opt_buffer_pid
, 1, 0, 0},
78 {"buffers-global", 0, POPT_ARG_VAL
, &opt_buffer_global
, 1, 0, 0},
79 {"tracefile-size", 'C', POPT_ARG_INT
, 0, OPT_TRACEFILE_SIZE
, 0, 0},
80 {"tracefile-count", 'W', POPT_ARG_INT
, 0, OPT_TRACEFILE_COUNT
, 0, 0},
87 static void usage(FILE *ofp
)
89 fprintf(ofp
, "usage: lttng enable-channel NAME[,NAME2,...] [-u|-k] [OPTIONS]\n");
91 fprintf(ofp
, "Options:\n");
92 fprintf(ofp
, " -h, --help Show this help\n");
93 fprintf(ofp
, " --list-options Simple listing of options\n");
94 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
95 fprintf(ofp
, " -k, --kernel Apply to the kernel tracer\n");
96 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
98 fprintf(ofp
, "Channel options:\n");
99 fprintf(ofp
, " --discard Discard event when buffers are full%s\n",
100 DEFAULT_CHANNEL_OVERWRITE
? "" : " (default)");
101 fprintf(ofp
, " --overwrite Flight recorder mode%s\n",
102 DEFAULT_CHANNEL_OVERWRITE
? " (default)" : "");
103 fprintf(ofp
, " --subbuf-size SIZE Subbuffer size in bytes {+k,+M,+G}\n");
104 fprintf(ofp
, " (default: %zu, kernel default: %zu)\n",
105 default_get_channel_subbuf_size(),
106 default_get_kernel_channel_subbuf_size());
107 fprintf(ofp
, " Needs to be a power of 2 for\n");
108 fprintf(ofp
, " kernel and ust tracers\n");
109 fprintf(ofp
, " --num-subbuf NUM Number of subbufers\n");
110 fprintf(ofp
, " (default: %u)\n",
111 DEFAULT_CHANNEL_SUBBUF_NUM
);
112 fprintf(ofp
, " Needs to be a power of 2 for\n");
113 fprintf(ofp
, " kernel and ust tracers\n");
114 fprintf(ofp
, " --switch-timer USEC Switch timer interval in usec (default: %u)\n",
115 DEFAULT_CHANNEL_SWITCH_TIMER
);
116 fprintf(ofp
, " --read-timer USEC Read timer interval in usec (UST default: %u, kernel default: %u)\n",
117 DEFAULT_UST_CHANNEL_READ_TIMER
, DEFAULT_KERNEL_CHANNEL_READ_TIMER
);
118 fprintf(ofp
, " --output TYPE Channel output type (Values: %s, %s)\n",
119 output_mmap
, output_splice
);
120 fprintf(ofp
, " --buffers-uid Use per UID buffer (-u only)\n");
121 fprintf(ofp
, " --buffers-pid Use per PID buffer (-u only)\n");
122 fprintf(ofp
, " --buffers-global Use shared buffer for the whole system (-k only)\n");
123 fprintf(ofp
, " -C, --tracefile-size SIZE\n");
124 fprintf(ofp
, " Maximum size of each tracefile within a stream (in bytes).\n");
125 fprintf(ofp
, " -W, --tracefile-count COUNT\n");
126 fprintf(ofp
, " Used in conjunction with -C option, this will limit the number\n");
127 fprintf(ofp
, " of files created to the specified count.\n");
132 * Set default attributes depending on those already defined from the command
135 static void set_default_attr(struct lttng_domain
*dom
)
137 struct lttng_channel_attr default_attr
;
140 lttng_channel_set_default_attr(dom
, &default_attr
);
142 if (chan
.attr
.overwrite
== -1) {
143 chan
.attr
.overwrite
= default_attr
.overwrite
;
145 if (chan
.attr
.subbuf_size
== -1) {
146 chan
.attr
.subbuf_size
= default_attr
.subbuf_size
;
148 if (chan
.attr
.num_subbuf
== -1) {
149 chan
.attr
.num_subbuf
= default_attr
.num_subbuf
;
151 if (chan
.attr
.switch_timer_interval
== -1) {
152 chan
.attr
.switch_timer_interval
= default_attr
.switch_timer_interval
;
154 if (chan
.attr
.read_timer_interval
== -1) {
155 chan
.attr
.read_timer_interval
= default_attr
.read_timer_interval
;
157 if (chan
.attr
.output
== -1) {
158 chan
.attr
.output
= default_attr
.output
;
160 if (chan
.attr
.tracefile_count
== -1) {
161 chan
.attr
.tracefile_count
= default_attr
.tracefile_count
;
163 if (chan
.attr
.tracefile_size
== -1) {
164 chan
.attr
.tracefile_size
= default_attr
.tracefile_size
;
169 * Adding channel using the lttng API.
171 static int enable_channel(char *session_name
)
173 int ret
= CMD_SUCCESS
, warn
= 0;
175 struct lttng_domain dom
;
177 memset(&dom
, 0, sizeof(dom
));
179 /* Create lttng domain */
181 dom
.type
= LTTNG_DOMAIN_KERNEL
;
182 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
183 if (opt_buffer_uid
|| opt_buffer_pid
) {
184 ERR("Buffer type not supported for domain -k");
188 } else if (opt_userspace
) {
189 dom
.type
= LTTNG_DOMAIN_UST
;
190 if (opt_buffer_uid
) {
191 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
193 if (opt_buffer_global
) {
194 ERR("Buffer type not supported for domain -u");
198 dom
.buf_type
= LTTNG_BUFFER_PER_PID
;
201 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
206 set_default_attr(&dom
);
208 if ((chan
.attr
.tracefile_size
> 0) &&
209 (chan
.attr
.tracefile_size
< chan
.attr
.subbuf_size
)) {
210 ERR("Tracefile_size must be greater than or equal to subbuf_size "
211 "(%" PRIu64
" < %" PRIu64
")",
212 chan
.attr
.tracefile_size
, chan
.attr
.subbuf_size
);
217 /* Setting channel output */
219 if (!strncmp(output_mmap
, opt_output
, strlen(output_mmap
))) {
220 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
221 } else if (!strncmp(output_splice
, opt_output
, strlen(output_splice
))) {
222 chan
.attr
.output
= LTTNG_EVENT_SPLICE
;
224 ERR("Unknown output type %s. Possible values are: %s, %s\n",
225 opt_output
, output_mmap
, output_splice
);
232 handle
= lttng_create_handle(session_name
, &dom
);
233 if (handle
== NULL
) {
238 /* Strip channel list (format: chan1,chan2,...) */
239 channel_name
= strtok(opt_channels
, ",");
240 while (channel_name
!= NULL
) {
241 /* Copy channel name and normalize it */
242 strncpy(chan
.name
, channel_name
, NAME_MAX
);
243 chan
.name
[NAME_MAX
- 1] = '\0';
245 DBG("Enabling channel %s", channel_name
);
247 ret
= lttng_enable_channel(handle
, &chan
);
250 case LTTNG_ERR_KERN_CHAN_EXIST
:
251 case LTTNG_ERR_UST_CHAN_EXIST
:
252 WARN("Channel %s: %s (session %s)", channel_name
,
253 lttng_strerror(ret
), session_name
);
256 ERR("Channel %s: %s (session %s)", channel_name
,
257 lttng_strerror(ret
), session_name
);
262 MSG("%s channel %s enabled for session %s",
263 opt_kernel
? "Kernel" : "UST", channel_name
,
268 channel_name
= strtok(NULL
, ",");
278 lttng_destroy_handle(handle
);
284 * Default value for channel configuration.
286 static void init_channel_config(void)
289 * Put -1 everywhere so we can identify those set by the command line and
290 * those needed to be set by the default values.
292 memset(&chan
.attr
, -1, sizeof(chan
.attr
));
296 * Add channel to trace session
298 int cmd_enable_channels(int argc
, const char **argv
)
300 int opt
, ret
= CMD_SUCCESS
;
301 static poptContext pc
;
302 char *session_name
= NULL
;
303 char *opt_arg
= NULL
;
305 init_channel_config();
307 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
308 poptReadDefaultConfig(pc
, 0);
310 while ((opt
= poptGetNextOpt(pc
)) != -1) {
316 chan
.attr
.overwrite
= 0;
317 DBG("Channel set to discard");
320 chan
.attr
.overwrite
= 1;
321 DBG("Channel set to overwrite");
323 case OPT_SUBBUF_SIZE
:
325 opt_arg
= poptGetOptArg(pc
);
326 if (utils_parse_size_suffix(opt_arg
, &chan
.attr
.subbuf_size
) < 0) {
327 ERR("Wrong value the --subbuf-size parameter: %s", opt_arg
);
332 /* Check if power of 2 */
333 if ((chan
.attr
.subbuf_size
- 1) & chan
.attr
.subbuf_size
) {
334 ERR("The subbuf size is not a power of 2: %" PRIu64
" (%s)",
335 chan
.attr
.subbuf_size
, opt_arg
);
340 DBG("Channel subbuf size set to %" PRIu64
, chan
.attr
.subbuf_size
);
344 chan
.attr
.num_subbuf
= strtoull(poptGetOptArg(pc
), NULL
, 0);
346 ERR("Wrong value the --num-subbuf parameter: %s", opt_arg
);
351 DBG("Channel subbuf num set to %" PRIu64
, chan
.attr
.num_subbuf
);
353 case OPT_SWITCH_TIMER
:
358 v
= strtoul(poptGetOptArg(pc
), NULL
, 0);
360 ERR("Wrong value the --switch-timer parameter: %s", opt_arg
);
364 if (v
!= (uint32_t) v
) {
365 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg
);
369 chan
.attr
.switch_timer_interval
= (uint32_t) v
;
370 DBG("Channel switch timer interval set to %d", chan
.attr
.switch_timer_interval
);
378 v
= strtoul(poptGetOptArg(pc
), NULL
, 0);
380 ERR("Wrong value the --read-timer parameter: %s", opt_arg
);
384 if (v
!= (uint32_t) v
) {
385 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg
);
389 chan
.attr
.read_timer_interval
= (uint32_t) v
;
390 DBG("Channel read timer interval set to %d", chan
.attr
.read_timer_interval
);
396 case OPT_TRACEFILE_SIZE
:
397 if (utils_parse_size_suffix(opt_arg
, &chan
.attr
.tracefile_size
) < 0) {
398 ERR("Wrong value the --tracefile-size parameter: %s", opt_arg
);
402 DBG("Maximum tracefile size set to %" PRIu64
,
403 chan
.attr
.tracefile_size
);
405 case OPT_TRACEFILE_COUNT
:
410 v
= strtoul(poptGetOptArg(pc
), NULL
, 0);
412 ERR("Wrong value the --tracefile-count parameter: %s", opt_arg
);
416 if (v
!= (uint32_t) v
) {
417 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg
);
421 chan
.attr
.tracefile_count
= (uint32_t) v
;
422 DBG("Maximum tracefile count set to %" PRIu64
,
423 chan
.attr
.tracefile_count
);
426 case OPT_LIST_OPTIONS
:
427 list_cmd_options(stdout
, long_options
);
436 opt_channels
= (char*) poptGetArg(pc
);
437 if (opt_channels
== NULL
) {
438 ERR("Missing channel name.\n");
444 if (!opt_session_name
) {
445 session_name
= get_session_name();
446 if (session_name
== NULL
) {
451 session_name
= opt_session_name
;
454 ret
= enable_channel(session_name
);
457 if (!opt_session_name
&& session_name
) {