2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <sys/types.h>
29 #include "../command.h"
31 static char *opt_channels
;
32 static int opt_kernel
;
33 static char *opt_cmd_name
;
34 static char *opt_session_name
;
35 static int opt_userspace
;
36 static char *opt_cmd_name
;
38 static struct lttng_channel chan
;
51 static struct lttng_handle
*handle
;
53 static struct poptOption long_options
[] = {
54 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
55 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
56 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
57 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
58 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
59 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
60 {"discard", 0, POPT_ARG_NONE
, 0, OPT_DISCARD
, 0, 0},
61 {"overwrite", 0, POPT_ARG_NONE
, 0, OPT_OVERWRITE
, 0, 0},
62 {"subbuf-size", 0, POPT_ARG_DOUBLE
, 0, OPT_SUBBUF_SIZE
, 0, 0},
63 {"num-subbuf", 0, POPT_ARG_INT
, 0, OPT_NUM_SUBBUF
, 0, 0},
64 {"switch-timer", 0, POPT_ARG_INT
, 0, OPT_SWITCH_TIMER
, 0, 0},
65 {"read-timer", 0, POPT_ARG_INT
, 0, OPT_READ_TIMER
, 0, 0},
72 static void usage(FILE *ofp
)
74 fprintf(ofp
, "usage: lttng enable-channel NAME[,NAME2,...] [options] [channel_options]\n");
76 fprintf(ofp
, " -h, --help Show this help\n");
77 fprintf(ofp
, " -s, --session Apply on session name\n");
78 fprintf(ofp
, " -k, --kernel Apply on the kernel tracer\n");
79 fprintf(ofp
, " -u, --userspace [CMD] Apply for the user-space tracer\n");
80 fprintf(ofp
, " If no CMD, the domain used is UST global\n");
81 fprintf(ofp
, " or else the domain is UST EXEC_NAME\n");
82 fprintf(ofp
, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
84 fprintf(ofp
, "Channel options:\n");
85 fprintf(ofp
, " --discard Discard event when buffers are full%s\n",
86 DEFAULT_CHANNEL_OVERWRITE
? "" : " (default)");
87 fprintf(ofp
, " --overwrite Flight recorder mode%s\n",
88 DEFAULT_CHANNEL_OVERWRITE
? " (default)" : "");
89 fprintf(ofp
, " --subbuf-size Subbuffer size in bytes\n");
90 fprintf(ofp
, " (default: %u, kernel default: %u)\n",
91 DEFAULT_CHANNEL_SUBBUF_SIZE
,
92 DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
);
93 fprintf(ofp
, " --num-subbuf Number of subbufers\n");
94 fprintf(ofp
, " (default: %u, kernel default: %u)\n",
95 DEFAULT_CHANNEL_SUBBUF_NUM
,
96 DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
);
97 fprintf(ofp
, " --switch-timer Switch timer interval in usec (default: %u)\n",
98 DEFAULT_CHANNEL_SWITCH_TIMER
);
99 fprintf(ofp
, " --read-timer Read timer interval in usec (default: %u)\n",
100 DEFAULT_CHANNEL_READ_TIMER
);
105 * Set default attributes depending on those already defined from the command
108 static void set_default_attr(struct lttng_domain
*dom
)
110 struct lttng_channel_attr default_attr
;
113 lttng_channel_set_default_attr(dom
, &default_attr
);
115 if (chan
.attr
.overwrite
== -1) {
116 chan
.attr
.overwrite
= default_attr
.overwrite
;
118 if (chan
.attr
.subbuf_size
== -1) {
119 chan
.attr
.subbuf_size
= default_attr
.subbuf_size
;
121 if (chan
.attr
.num_subbuf
== -1) {
122 chan
.attr
.num_subbuf
= default_attr
.num_subbuf
;
124 if (chan
.attr
.switch_timer_interval
== -1) {
125 chan
.attr
.switch_timer_interval
= default_attr
.switch_timer_interval
;
127 if (chan
.attr
.read_timer_interval
== -1) {
128 chan
.attr
.read_timer_interval
= default_attr
.read_timer_interval
;
130 if (chan
.attr
.output
== -1) {
131 chan
.attr
.output
= default_attr
.output
;
136 * Adding channel using the lttng API.
138 static int enable_channel(char *session_name
)
140 int ret
= CMD_SUCCESS
;
142 struct lttng_domain dom
;
145 dom
.type
= LTTNG_DOMAIN_KERNEL
;
146 } else if (opt_pid
!= 0) {
147 dom
.type
= LTTNG_DOMAIN_UST_PID
;
148 dom
.attr
.pid
= opt_pid
;
149 DBG("PID %d set to lttng handle", opt_pid
);
150 } else if (opt_userspace
&& opt_cmd_name
== NULL
) {
151 dom
.type
= LTTNG_DOMAIN_UST
;
152 } else if (opt_userspace
&& opt_cmd_name
!= NULL
) {
153 dom
.type
= LTTNG_DOMAIN_UST_EXEC_NAME
;
154 strncpy(dom
.attr
.exec_name
, opt_cmd_name
, NAME_MAX
);
156 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
157 ret
= CMD_NOT_IMPLEMENTED
;
161 set_default_attr(&dom
);
163 handle
= lttng_create_handle(session_name
, &dom
);
164 if (handle
== NULL
) {
169 /* Strip channel list (format: chan1,chan2,...) */
170 channel_name
= strtok(opt_channels
, ",");
171 while (channel_name
!= NULL
) {
172 /* Copy channel name and normalize it */
173 strncpy(chan
.name
, channel_name
, NAME_MAX
);
174 chan
.name
[NAME_MAX
- 1] = '\0';
176 DBG("Enabling channel %s", channel_name
);
178 ret
= lttng_enable_channel(handle
, &chan
);
182 MSG("%s channel %s enabled for session %s",
183 opt_kernel
? "Kernel" : "UST", channel_name
,
188 channel_name
= strtok(NULL
, ",");
192 lttng_destroy_handle(handle
);
198 * Default value for channel configuration.
200 static void init_channel_config(void)
203 * Put -1 everywhere so we can identify those set by the command line and
204 * those needed to be set by the default values.
206 memset(&chan
.attr
, -1, sizeof(chan
.attr
));
210 * Add channel to trace session
212 int cmd_enable_channels(int argc
, const char **argv
)
215 static poptContext pc
;
216 char *session_name
= NULL
;
218 init_channel_config();
220 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
221 poptReadDefaultConfig(pc
, 0);
223 while ((opt
= poptGetNextOpt(pc
)) != -1) {
230 chan
.attr
.overwrite
= 0;
231 DBG("Channel set to discard");
234 chan
.attr
.overwrite
= 1;
235 DBG("Channel set to overwrite");
237 case OPT_SUBBUF_SIZE
:
238 chan
.attr
.subbuf_size
= atol(poptGetOptArg(pc
));
239 DBG("Channel subbuf size set to %" PRIu64
, chan
.attr
.subbuf_size
);
242 chan
.attr
.num_subbuf
= atoi(poptGetOptArg(pc
));
243 DBG("Channel subbuf num set to %" PRIu64
, chan
.attr
.num_subbuf
);
245 case OPT_SWITCH_TIMER
:
246 chan
.attr
.switch_timer_interval
= atoi(poptGetOptArg(pc
));
247 DBG("Channel switch timer interval set to %d", chan
.attr
.switch_timer_interval
);
250 chan
.attr
.read_timer_interval
= atoi(poptGetOptArg(pc
));
251 DBG("Channel read timer interval set to %d", chan
.attr
.read_timer_interval
);
263 opt_channels
= (char*) poptGetArg(pc
);
264 if (opt_channels
== NULL
) {
265 ERR("Missing channel name.\n");
271 if (!opt_session_name
) {
272 session_name
= get_session_name();
273 if (session_name
== NULL
) {
278 session_name
= opt_session_name
;
281 ret
= enable_channel(session_name
);