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 <common/mi-lttng.h>
30 #include "../command.h"
32 static char *opt_channels
;
33 static int opt_kernel
;
34 static char *opt_session_name
;
35 static int opt_userspace
;
37 /* Not implemented yet */
38 static char *opt_cmd_name
;
48 static struct lttng_handle
*handle
;
49 static struct mi_writer
*writer
;
51 static struct poptOption long_options
[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
53 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
54 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
55 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
57 /* Not implemented yet */
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},
61 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
63 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
70 static void usage(FILE *ofp
)
72 fprintf(ofp
, "usage: lttng disable-channel NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
74 fprintf(ofp
, "Options:\n");
75 fprintf(ofp
, " -h, --help Show this help\n");
76 fprintf(ofp
, " --list-options Simple listing of options\n");
77 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
78 fprintf(ofp
, " -k, --kernel Apply to the kernel tracer\n");
79 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
83 static int mi_partial_channel_print(char *channel_name
, unsigned int enabled
,
91 /* Open channel element */
92 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
98 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
105 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
112 ret
= mi_lttng_writer_write_element_bool(writer
,
113 mi_lttng_element_success
, success
);
118 /* Closing channel element */
119 ret
= mi_lttng_writer_close_element(writer
);
126 * Disabling channel using the lttng API.
128 static int disable_channels(char *session_name
)
130 int ret
= CMD_SUCCESS
, warn
= 0, success
;
132 /* Normal case for disable channed is enabled = 0 */
133 unsigned int enabled
= 0;
135 struct lttng_domain dom
;
137 memset(&dom
, 0, sizeof(dom
));
139 /* Create lttng domain */
141 dom
.type
= LTTNG_DOMAIN_KERNEL
;
142 } else if (opt_userspace
) {
143 dom
.type
= LTTNG_DOMAIN_UST
;
145 print_missing_domain();
150 handle
= lttng_create_handle(session_name
, &dom
);
151 if (handle
== NULL
) {
158 /* open a channels element */
159 ret
= mi_lttng_writer_open_element(writer
, config_element_channels
);
167 /* Strip channel list */
168 channel_name
= strtok(opt_channels
, ",");
169 while (channel_name
!= NULL
) {
170 DBG("Disabling channel %s", channel_name
);
172 ret
= lttng_disable_channel(handle
, channel_name
);
174 ERR("Channel %s: %s (session %s)", channel_name
,
175 lttng_strerror(ret
), session_name
);
180 * We assume that if an error occurred the channel is still active.
181 * This might not be the case but is a good assumption.
182 * The client should look at the stderr stream
183 * for more informations.
189 MSG("%s channel %s disabled for session %s",
190 get_domain_str(dom
.type
), channel_name
, session_name
);
195 /* Print the channel */
197 ret
= mi_partial_channel_print(channel_name
, enabled
, success
);
205 channel_name
= strtok(NULL
, ",");
212 /* Close channels element */
213 ret
= mi_lttng_writer_close_element(writer
);
221 /* Bypass the warning if a more important error happened */
226 lttng_destroy_handle(handle
);
232 * cmd_disable_channels
234 * Disable channel to trace session
236 int cmd_disable_channels(int argc
, const char **argv
)
238 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
239 static poptContext pc
;
240 char *session_name
= NULL
;
242 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
243 poptReadDefaultConfig(pc
, 0);
245 while ((opt
= poptGetNextOpt(pc
)) != -1) {
253 case OPT_LIST_OPTIONS
:
254 list_cmd_options(stdout
, long_options
);
263 opt_channels
= (char*) poptGetArg(pc
);
264 if (opt_channels
== NULL
) {
265 ERR("Missing channel name(s).\n");
271 if (!opt_session_name
) {
272 session_name
= get_session_name();
273 if (session_name
== NULL
) {
278 session_name
= opt_session_name
;
283 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
285 ret
= -LTTNG_ERR_NOMEM
;
289 /* Open command element */
290 ret
= mi_lttng_writer_command_open(writer
,
291 mi_lttng_element_command_disable_channel
);
297 /* Open output element */
298 ret
= mi_lttng_writer_open_element(writer
,
299 mi_lttng_element_command_output
);
306 command_ret
= disable_channels(session_name
);
313 /* Close output element */
314 ret
= mi_lttng_writer_close_element(writer
);
321 ret
= mi_lttng_writer_write_element_bool(writer
,
322 mi_lttng_element_success
, success
);
328 /* Command element close */
329 ret
= mi_lttng_writer_command_close(writer
);
338 if (writer
&& mi_lttng_writer_destroy(writer
)) {
339 /* Preserve original error code */
340 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
343 if (!opt_session_name
&& session_name
) {
347 /* Overwrite ret if an error occurred in disable_channels */
348 ret
= command_ret
? command_ret
: ret
;