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 #ifdef LTTNG_EMBED_HELP
38 static const char help_msg
[] =
39 #include <lttng-disable-channel.1.h>
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},
57 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
58 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
62 static int mi_partial_channel_print(char *channel_name
, unsigned int enabled
,
70 /* Open channel element */
71 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
77 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
84 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
91 ret
= mi_lttng_writer_write_element_bool(writer
,
92 mi_lttng_element_success
, success
);
97 /* Closing channel element */
98 ret
= mi_lttng_writer_close_element(writer
);
105 * Disabling channel using the lttng API.
107 static int disable_channels(char *session_name
)
109 int ret
= CMD_SUCCESS
, warn
= 0, success
;
111 /* Normal case for disable channed is enabled = 0 */
112 unsigned int enabled
= 0;
114 struct lttng_domain dom
;
116 memset(&dom
, 0, sizeof(dom
));
118 /* Create lttng domain */
120 dom
.type
= LTTNG_DOMAIN_KERNEL
;
121 } else if (opt_userspace
) {
122 dom
.type
= LTTNG_DOMAIN_UST
;
124 /* Checked by the caller. */
128 handle
= lttng_create_handle(session_name
, &dom
);
129 if (handle
== NULL
) {
136 /* open a channels element */
137 ret
= mi_lttng_writer_open_element(writer
, config_element_channels
);
145 /* Strip channel list */
146 channel_name
= strtok(opt_channels
, ",");
147 while (channel_name
!= NULL
) {
148 DBG("Disabling channel %s", channel_name
);
150 ret
= lttng_disable_channel(handle
, channel_name
);
152 ERR("Channel %s: %s (session %s)", channel_name
,
153 lttng_strerror(ret
), session_name
);
158 * We assume that if an error occurred the channel is still active.
159 * This might not be the case but is a good assumption.
160 * The client should look at the stderr stream
161 * for more informations.
167 MSG("%s channel %s disabled for session %s",
168 get_domain_str(dom
.type
), channel_name
, session_name
);
173 /* Print the channel */
175 ret
= mi_partial_channel_print(channel_name
, enabled
, success
);
183 channel_name
= strtok(NULL
, ",");
190 /* Close channels element */
191 ret
= mi_lttng_writer_close_element(writer
);
199 /* Bypass the warning if a more important error happened */
204 lttng_destroy_handle(handle
);
210 * cmd_disable_channels
212 * Disable channel to trace session
214 int cmd_disable_channels(int argc
, const char **argv
)
216 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
217 static poptContext pc
;
218 char *session_name
= NULL
;
219 const char *leftover
= NULL
;
221 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
222 poptReadDefaultConfig(pc
, 0);
224 while ((opt
= poptGetNextOpt(pc
)) != -1) {
232 case OPT_LIST_OPTIONS
:
233 list_cmd_options(stdout
, long_options
);
241 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
);
247 opt_channels
= (char*) poptGetArg(pc
);
248 if (opt_channels
== NULL
) {
249 ERR("Missing channel name(s).\n");
254 leftover
= poptGetArg(pc
);
256 ERR("Unknown argument: %s", leftover
);
261 if (!opt_session_name
) {
262 session_name
= get_session_name();
263 if (session_name
== NULL
) {
268 session_name
= opt_session_name
;
273 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
275 ret
= -LTTNG_ERR_NOMEM
;
279 /* Open command element */
280 ret
= mi_lttng_writer_command_open(writer
,
281 mi_lttng_element_command_disable_channel
);
287 /* Open output element */
288 ret
= mi_lttng_writer_open_element(writer
,
289 mi_lttng_element_command_output
);
296 command_ret
= disable_channels(session_name
);
303 /* Close output element */
304 ret
= mi_lttng_writer_close_element(writer
);
311 ret
= mi_lttng_writer_write_element_bool(writer
,
312 mi_lttng_element_success
, success
);
318 /* Command element close */
319 ret
= mi_lttng_writer_command_close(writer
);
328 if (writer
&& mi_lttng_writer_destroy(writer
)) {
329 /* Preserve original error code */
330 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
333 if (!opt_session_name
&& session_name
) {
337 /* Overwrite ret if an error occurred in disable_channels */
338 ret
= command_ret
? command_ret
: ret
;