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
;
43 static struct lttng_handle
*handle
;
44 static struct mi_writer
*writer
;
46 static struct poptOption long_options
[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
49 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
50 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
51 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
52 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
56 static int mi_partial_channel_print(char *channel_name
, unsigned int enabled
,
64 /* Open channel element */
65 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
71 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
78 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
85 ret
= mi_lttng_writer_write_element_bool(writer
,
86 mi_lttng_element_success
, success
);
91 /* Closing channel element */
92 ret
= mi_lttng_writer_close_element(writer
);
99 * Disabling channel using the lttng API.
101 static int disable_channels(char *session_name
)
103 int ret
= CMD_SUCCESS
, warn
= 0, success
;
105 /* Normal case for disable channed is enabled = 0 */
106 unsigned int enabled
= 0;
108 struct lttng_domain dom
;
110 memset(&dom
, 0, sizeof(dom
));
112 /* Create lttng domain */
114 dom
.type
= LTTNG_DOMAIN_KERNEL
;
115 } else if (opt_userspace
) {
116 dom
.type
= LTTNG_DOMAIN_UST
;
118 /* Checked by the caller. */
122 handle
= lttng_create_handle(session_name
, &dom
);
123 if (handle
== NULL
) {
130 /* open a channels element */
131 ret
= mi_lttng_writer_open_element(writer
, config_element_channels
);
139 /* Strip channel list */
140 channel_name
= strtok(opt_channels
, ",");
141 while (channel_name
!= NULL
) {
142 DBG("Disabling channel %s", channel_name
);
144 ret
= lttng_disable_channel(handle
, channel_name
);
146 ERR("Channel %s: %s (session %s)", channel_name
,
147 lttng_strerror(ret
), session_name
);
152 * We assume that if an error occurred the channel is still active.
153 * This might not be the case but is a good assumption.
154 * The client should look at the stderr stream
155 * for more informations.
161 MSG("%s channel %s disabled for session %s",
162 get_domain_str(dom
.type
), channel_name
, session_name
);
167 /* Print the channel */
169 ret
= mi_partial_channel_print(channel_name
, enabled
, success
);
177 channel_name
= strtok(NULL
, ",");
184 /* Close channels element */
185 ret
= mi_lttng_writer_close_element(writer
);
193 /* Bypass the warning if a more important error happened */
198 lttng_destroy_handle(handle
);
204 * cmd_disable_channels
206 * Disable channel to trace session
208 int cmd_disable_channels(int argc
, const char **argv
)
210 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
211 static poptContext pc
;
212 char *session_name
= NULL
;
214 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
215 poptReadDefaultConfig(pc
, 0);
217 while ((opt
= poptGetNextOpt(pc
)) != -1) {
225 case OPT_LIST_OPTIONS
:
226 list_cmd_options(stdout
, long_options
);
234 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
);
240 opt_channels
= (char*) poptGetArg(pc
);
241 if (opt_channels
== NULL
) {
242 ERR("Missing channel name(s).\n");
247 if (!opt_session_name
) {
248 session_name
= get_session_name();
249 if (session_name
== NULL
) {
254 session_name
= opt_session_name
;
259 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
261 ret
= -LTTNG_ERR_NOMEM
;
265 /* Open command element */
266 ret
= mi_lttng_writer_command_open(writer
,
267 mi_lttng_element_command_disable_channel
);
273 /* Open output element */
274 ret
= mi_lttng_writer_open_element(writer
,
275 mi_lttng_element_command_output
);
282 command_ret
= disable_channels(session_name
);
289 /* Close output element */
290 ret
= mi_lttng_writer_close_element(writer
);
297 ret
= mi_lttng_writer_write_element_bool(writer
,
298 mi_lttng_element_success
, success
);
304 /* Command element close */
305 ret
= mi_lttng_writer_command_close(writer
);
314 if (writer
&& mi_lttng_writer_destroy(writer
)) {
315 /* Preserve original error code */
316 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
319 if (!opt_session_name
&& session_name
) {
323 /* Overwrite ret if an error occurred in disable_channels */
324 ret
= command_ret
? command_ret
: ret
;