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.
25 #include <sys/types.h>
29 #include <common/mi-lttng.h>
31 #include "../command.h"
33 static char *opt_channels
;
34 static int opt_kernel
;
35 static char *opt_session_name
;
36 static int opt_userspace
;
38 /* Not implemented yet */
39 static char *opt_cmd_name
;
49 static struct lttng_handle
*handle
;
50 static struct mi_writer
*writer
;
52 static struct poptOption long_options
[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
55 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
56 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
58 /* Not implemented yet */
59 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
60 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
62 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
64 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
71 static void usage(FILE *ofp
)
73 fprintf(ofp
, "usage: lttng disable-channel NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
75 fprintf(ofp
, "Options:\n");
76 fprintf(ofp
, " -h, --help Show this help\n");
77 fprintf(ofp
, " --list-options Simple listing of options\n");
78 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
79 fprintf(ofp
, " -k, --kernel Apply to the kernel tracer\n");
80 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
84 static int mi_partial_channel_print(char *channel_name
, unsigned int enabled
,
92 /* Open channel element */
93 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
99 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
106 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
113 ret
= mi_lttng_writer_write_element_bool(writer
,
114 mi_lttng_element_success
, success
);
119 /* Closing channel element */
120 ret
= mi_lttng_writer_close_element(writer
);
127 * Disabling channel using the lttng API.
129 static int disable_channels(char *session_name
)
131 int ret
= CMD_SUCCESS
, warn
= 0, success
;
133 /* Normal case for disable channed is enabled = 0 */
134 unsigned int enabled
= 0;
136 struct lttng_domain dom
;
138 memset(&dom
, 0, sizeof(dom
));
140 /* Create lttng domain */
142 dom
.type
= LTTNG_DOMAIN_KERNEL
;
143 } else if (opt_userspace
) {
144 dom
.type
= LTTNG_DOMAIN_UST
;
146 print_missing_domain();
151 handle
= lttng_create_handle(session_name
, &dom
);
152 if (handle
== NULL
) {
159 /* open a channels element */
160 ret
= mi_lttng_writer_open_element(writer
, config_element_channels
);
168 /* Strip channel list */
169 channel_name
= strtok(opt_channels
, ",");
170 while (channel_name
!= NULL
) {
171 DBG("Disabling channel %s", channel_name
);
173 ret
= lttng_disable_channel(handle
, channel_name
);
175 ERR("Channel %s: %s (session %s)", channel_name
,
176 lttng_strerror(ret
), session_name
);
181 * We assume that if an error occurred the channel is still active.
182 * This might not be the case but is a good assumption.
183 * The client should look at the stderr stream
184 * for more informations.
190 MSG("%s channel %s disabled for session %s",
191 get_domain_str(dom
.type
), channel_name
, session_name
);
196 /* Print the channel */
198 ret
= mi_partial_channel_print(channel_name
, enabled
, success
);
206 channel_name
= strtok(NULL
, ",");
213 /* Close channels element */
214 ret
= mi_lttng_writer_close_element(writer
);
222 /* Bypass the warning if a more important error happened */
227 lttng_destroy_handle(handle
);
233 * cmd_disable_channels
235 * Disable channel to trace session
237 int cmd_disable_channels(int argc
, const char **argv
)
239 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
240 static poptContext pc
;
241 char *session_name
= NULL
;
243 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
244 poptReadDefaultConfig(pc
, 0);
246 while ((opt
= poptGetNextOpt(pc
)) != -1) {
254 case OPT_LIST_OPTIONS
:
255 list_cmd_options(stdout
, long_options
);
264 opt_channels
= (char*) poptGetArg(pc
);
265 if (opt_channels
== NULL
) {
266 ERR("Missing channel name(s).\n");
272 if (!opt_session_name
) {
273 session_name
= get_session_name();
274 if (session_name
== NULL
) {
279 session_name
= opt_session_name
;
284 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
286 ret
= -LTTNG_ERR_NOMEM
;
290 /* Open command element */
291 ret
= mi_lttng_writer_command_open(writer
,
292 mi_lttng_element_command_disable_channel
);
298 /* Open output element */
299 ret
= mi_lttng_writer_open_element(writer
,
300 mi_lttng_element_command_output
);
307 command_ret
= disable_channels(session_name
);
314 /* Close output element */
315 ret
= mi_lttng_writer_close_element(writer
);
322 ret
= mi_lttng_writer_write_element_bool(writer
,
323 mi_lttng_element_success
, success
);
329 /* Command element close */
330 ret
= mi_lttng_writer_command_close(writer
);
339 if (writer
&& mi_lttng_writer_destroy(writer
)) {
340 /* Preserve original error code */
341 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
344 if (!opt_session_name
&& session_name
) {
348 /* Overwrite ret if an error occurred in disable_channels */
349 ret
= command_ret
? command_ret
: ret
;