2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <common/mi-lttng.h>
18 #include <lttng/domain-internal.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
,
58 LTTNG_ASSERT(channel_name
);
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 lttng_domain_type_str(dom
.type
),
159 channel_name
, session_name
);
164 /* Print the channel */
166 ret
= mi_partial_channel_print(channel_name
, enabled
, success
);
174 channel_name
= strtok(NULL
, ",");
181 /* Close channels element */
182 ret
= mi_lttng_writer_close_element(writer
);
190 /* Bypass the warning if a more important error happened */
195 lttng_destroy_handle(handle
);
201 * cmd_disable_channels
203 * Disable channel to trace session
205 int cmd_disable_channels(int argc
, const char **argv
)
207 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
208 static poptContext pc
;
209 char *session_name
= NULL
;
210 const char *leftover
= NULL
;
212 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
213 poptReadDefaultConfig(pc
, 0);
215 while ((opt
= poptGetNextOpt(pc
)) != -1) {
223 case OPT_LIST_OPTIONS
:
224 list_cmd_options(stdout
, long_options
);
232 ret
= print_missing_or_multiple_domains(
233 opt_kernel
+ opt_userspace
, false);
239 opt_channels
= (char*) poptGetArg(pc
);
240 if (opt_channels
== NULL
) {
241 ERR("Missing channel name(s).\n");
246 leftover
= poptGetArg(pc
);
248 ERR("Unknown argument: %s", leftover
);
253 if (!opt_session_name
) {
254 session_name
= get_session_name();
255 if (session_name
== NULL
) {
260 session_name
= opt_session_name
;
265 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
267 ret
= -LTTNG_ERR_NOMEM
;
271 /* Open command element */
272 ret
= mi_lttng_writer_command_open(writer
,
273 mi_lttng_element_command_disable_channel
);
279 /* Open output element */
280 ret
= mi_lttng_writer_open_element(writer
,
281 mi_lttng_element_command_output
);
288 command_ret
= disable_channels(session_name
);
295 /* Close output element */
296 ret
= mi_lttng_writer_close_element(writer
);
303 ret
= mi_lttng_writer_write_element_bool(writer
,
304 mi_lttng_element_success
, success
);
310 /* Command element close */
311 ret
= mi_lttng_writer_command_close(writer
);
320 if (writer
&& mi_lttng_writer_destroy(writer
)) {
321 /* Preserve original error code */
322 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
325 if (!opt_session_name
&& session_name
) {
329 /* Overwrite ret if an error occurred in disable_channels */
330 ret
= command_ret
? command_ret
: ret
;