2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
18 #include <common/mi-lttng.h>
20 #include "../command.h"
22 static char *opt_channels
;
23 static int opt_kernel
;
24 static char *opt_session_name
;
25 static int opt_userspace
;
27 #ifdef LTTNG_EMBED_HELP
28 static const char help_msg
[] =
29 #include <lttng-disable-channel.1.h>
39 static struct lttng_handle
*handle
;
40 static struct mi_writer
*writer
;
42 static struct poptOption long_options
[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
45 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
46 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
47 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
48 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
52 static int mi_partial_channel_print(char *channel_name
, unsigned int enabled
,
60 /* Open channel element */
61 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
67 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
74 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
81 ret
= mi_lttng_writer_write_element_bool(writer
,
82 mi_lttng_element_success
, success
);
87 /* Closing channel element */
88 ret
= mi_lttng_writer_close_element(writer
);
95 * Disabling channel using the lttng API.
97 static int disable_channels(char *session_name
)
99 int ret
= CMD_SUCCESS
, warn
= 0, success
;
101 /* Normal case for disable channed is enabled = 0 */
102 unsigned int enabled
= 0;
104 struct lttng_domain dom
;
106 memset(&dom
, 0, sizeof(dom
));
108 /* Create lttng domain */
110 dom
.type
= LTTNG_DOMAIN_KERNEL
;
111 } else if (opt_userspace
) {
112 dom
.type
= LTTNG_DOMAIN_UST
;
114 /* Checked by the caller. */
118 handle
= lttng_create_handle(session_name
, &dom
);
119 if (handle
== NULL
) {
126 /* open a channels element */
127 ret
= mi_lttng_writer_open_element(writer
, config_element_channels
);
135 /* Strip channel list */
136 channel_name
= strtok(opt_channels
, ",");
137 while (channel_name
!= NULL
) {
138 DBG("Disabling channel %s", channel_name
);
140 ret
= lttng_disable_channel(handle
, channel_name
);
142 ERR("Channel %s: %s (session %s)", channel_name
,
143 lttng_strerror(ret
), session_name
);
148 * We assume that if an error occurred the channel is still active.
149 * This might not be the case but is a good assumption.
150 * The client should look at the stderr stream
151 * for more informations.
157 MSG("%s channel %s disabled for session %s",
158 get_domain_str(dom
.type
), channel_name
, session_name
);
163 /* Print the channel */
165 ret
= mi_partial_channel_print(channel_name
, enabled
, success
);
173 channel_name
= strtok(NULL
, ",");
180 /* Close channels element */
181 ret
= mi_lttng_writer_close_element(writer
);
189 /* Bypass the warning if a more important error happened */
194 lttng_destroy_handle(handle
);
200 * cmd_disable_channels
202 * Disable channel to trace session
204 int cmd_disable_channels(int argc
, const char **argv
)
206 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
207 static poptContext pc
;
208 char *session_name
= NULL
;
209 const char *leftover
= NULL
;
211 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
212 poptReadDefaultConfig(pc
, 0);
214 while ((opt
= poptGetNextOpt(pc
)) != -1) {
222 case OPT_LIST_OPTIONS
:
223 list_cmd_options(stdout
, long_options
);
231 ret
= print_missing_or_multiple_domains(
232 opt_kernel
+ opt_userspace
, false);
238 opt_channels
= (char*) poptGetArg(pc
);
239 if (opt_channels
== NULL
) {
240 ERR("Missing channel name(s).\n");
245 leftover
= poptGetArg(pc
);
247 ERR("Unknown argument: %s", leftover
);
252 if (!opt_session_name
) {
253 session_name
= get_session_name();
254 if (session_name
== NULL
) {
259 session_name
= opt_session_name
;
264 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
266 ret
= -LTTNG_ERR_NOMEM
;
270 /* Open command element */
271 ret
= mi_lttng_writer_command_open(writer
,
272 mi_lttng_element_command_disable_channel
);
278 /* Open output element */
279 ret
= mi_lttng_writer_open_element(writer
,
280 mi_lttng_element_command_output
);
287 command_ret
= disable_channels(session_name
);
294 /* Close output element */
295 ret
= mi_lttng_writer_close_element(writer
);
302 ret
= mi_lttng_writer_write_element_bool(writer
,
303 mi_lttng_element_success
, success
);
309 /* Command element close */
310 ret
= mi_lttng_writer_command_close(writer
);
319 if (writer
&& mi_lttng_writer_destroy(writer
)) {
320 /* Preserve original error code */
321 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
324 if (!opt_session_name
&& session_name
) {
328 /* Overwrite ret if an error occurred in disable_channels */
329 ret
= command_ret
? command_ret
: ret
;