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>
19 #include <lttng/domain-internal.h>
21 #include "../command.h"
23 static char *opt_channels
;
24 static int opt_kernel
;
25 static char *opt_session_name
;
26 static int opt_userspace
;
28 #ifdef LTTNG_EMBED_HELP
29 static const char help_msg
[] =
30 #include <lttng-disable-channel.1.h>
40 static struct lttng_handle
*handle
;
41 static struct mi_writer
*writer
;
43 static struct poptOption long_options
[] = {
44 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
45 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
46 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
47 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
48 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
49 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
53 static int mi_partial_channel_print(char *channel_name
, unsigned int enabled
,
61 /* Open channel element */
62 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
68 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
75 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
82 ret
= mi_lttng_writer_write_element_bool(writer
,
83 mi_lttng_element_success
, success
);
88 /* Closing channel element */
89 ret
= mi_lttng_writer_close_element(writer
);
96 * Disabling channel using the lttng API.
98 static int disable_channels(char *session_name
)
100 int ret
= CMD_SUCCESS
, warn
= 0, success
;
102 /* Normal case for disable channed is enabled = 0 */
103 unsigned int enabled
= 0;
105 struct lttng_domain dom
;
107 memset(&dom
, 0, sizeof(dom
));
109 /* Create lttng domain */
111 dom
.type
= LTTNG_DOMAIN_KERNEL
;
112 } else if (opt_userspace
) {
113 dom
.type
= LTTNG_DOMAIN_UST
;
115 /* Checked by the caller. */
119 handle
= lttng_create_handle(session_name
, &dom
);
120 if (handle
== NULL
) {
127 /* open a channels element */
128 ret
= mi_lttng_writer_open_element(writer
, config_element_channels
);
136 /* Strip channel list */
137 channel_name
= strtok(opt_channels
, ",");
138 while (channel_name
!= NULL
) {
139 DBG("Disabling channel %s", channel_name
);
141 ret
= lttng_disable_channel(handle
, channel_name
);
143 ERR("Channel %s: %s (session %s)", channel_name
,
144 lttng_strerror(ret
), session_name
);
149 * We assume that if an error occurred the channel is still active.
150 * This might not be the case but is a good assumption.
151 * The client should look at the stderr stream
152 * for more informations.
158 MSG("%s channel %s disabled for session %s",
159 lttng_domain_type_str(dom
.type
),
160 channel_name
, session_name
);
165 /* Print the channel */
167 ret
= mi_partial_channel_print(channel_name
, enabled
, success
);
175 channel_name
= strtok(NULL
, ",");
182 /* Close channels element */
183 ret
= mi_lttng_writer_close_element(writer
);
191 /* Bypass the warning if a more important error happened */
196 lttng_destroy_handle(handle
);
202 * cmd_disable_channels
204 * Disable channel to trace session
206 int cmd_disable_channels(int argc
, const char **argv
)
208 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
209 static poptContext pc
;
210 char *session_name
= NULL
;
211 const char *leftover
= NULL
;
213 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
214 poptReadDefaultConfig(pc
, 0);
216 while ((opt
= poptGetNextOpt(pc
)) != -1) {
224 case OPT_LIST_OPTIONS
:
225 list_cmd_options(stdout
, long_options
);
233 ret
= print_missing_or_multiple_domains(
234 opt_kernel
+ opt_userspace
, false);
240 opt_channels
= (char*) poptGetArg(pc
);
241 if (opt_channels
== NULL
) {
242 ERR("Missing channel name(s).\n");
247 leftover
= poptGetArg(pc
);
249 ERR("Unknown argument: %s", leftover
);
254 if (!opt_session_name
) {
255 session_name
= get_session_name();
256 if (session_name
== NULL
) {
261 session_name
= opt_session_name
;
266 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
268 ret
= -LTTNG_ERR_NOMEM
;
272 /* Open command element */
273 ret
= mi_lttng_writer_command_open(writer
,
274 mi_lttng_element_command_disable_channel
);
280 /* Open output element */
281 ret
= mi_lttng_writer_open_element(writer
,
282 mi_lttng_element_command_output
);
289 command_ret
= disable_channels(session_name
);
296 /* Close output element */
297 ret
= mi_lttng_writer_close_element(writer
);
304 ret
= mi_lttng_writer_write_element_bool(writer
,
305 mi_lttng_element_success
, success
);
311 /* Command element close */
312 ret
= mi_lttng_writer_command_close(writer
);
321 if (writer
&& mi_lttng_writer_destroy(writer
)) {
322 /* Preserve original error code */
323 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
326 if (!opt_session_name
&& session_name
) {
330 /* Overwrite ret if an error occurred in disable_channels */
331 ret
= command_ret
? command_ret
: ret
;