Cleanup: rename `get_domain_str()` -> `lttng_domain_type_str()`
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <popt.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <assert.h>
17
18 #include <common/mi-lttng.h>
19 #include <lttng/domain-internal.h>
20
21 #include "../command.h"
22
23 static char *opt_channels;
24 static int opt_kernel;
25 static char *opt_session_name;
26 static int opt_userspace;
27
28 #ifdef LTTNG_EMBED_HELP
29 static const char help_msg[] =
30 #include <lttng-disable-channel.1.h>
31 ;
32 #endif
33
34 enum {
35 OPT_HELP = 1,
36 OPT_USERSPACE,
37 OPT_LIST_OPTIONS,
38 };
39
40 static struct lttng_handle *handle;
41 static struct mi_writer *writer;
42
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},
50 {0, 0, 0, 0, 0, 0, 0}
51 };
52
53 static int mi_partial_channel_print(char *channel_name, unsigned int enabled,
54 int success)
55 {
56 int ret;
57
58 assert(writer);
59 assert(channel_name);
60
61 /* Open channel element */
62 ret = mi_lttng_writer_open_element(writer, config_element_channel);
63 if (ret) {
64 goto end;
65 }
66
67 /* Name */
68 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
69 channel_name);
70 if (ret) {
71 goto end;
72 }
73
74 /* Enabled ? */
75 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
76 enabled);
77 if (ret) {
78 goto end;
79 }
80
81 /* Success ? */
82 ret = mi_lttng_writer_write_element_bool(writer,
83 mi_lttng_element_success, success);
84 if (ret) {
85 goto end;
86 }
87
88 /* Closing channel element */
89 ret = mi_lttng_writer_close_element(writer);
90
91 end:
92 return ret;
93 }
94
95 /*
96 * Disabling channel using the lttng API.
97 */
98 static int disable_channels(char *session_name)
99 {
100 int ret = CMD_SUCCESS, warn = 0, success;
101
102 /* Normal case for disable channed is enabled = 0 */
103 unsigned int enabled = 0;
104 char *channel_name;
105 struct lttng_domain dom;
106
107 memset(&dom, 0, sizeof(dom));
108
109 /* Create lttng domain */
110 if (opt_kernel) {
111 dom.type = LTTNG_DOMAIN_KERNEL;
112 } else if (opt_userspace) {
113 dom.type = LTTNG_DOMAIN_UST;
114 } else {
115 /* Checked by the caller. */
116 assert(0);
117 }
118
119 handle = lttng_create_handle(session_name, &dom);
120 if (handle == NULL) {
121 ret = -1;
122 goto error;
123 }
124
125 /* Prepare MI */
126 if (lttng_opt_mi) {
127 /* open a channels element */
128 ret = mi_lttng_writer_open_element(writer, config_element_channels);
129 if (ret) {
130 ret = CMD_ERROR;
131 goto error;
132 }
133
134 }
135
136 /* Strip channel list */
137 channel_name = strtok(opt_channels, ",");
138 while (channel_name != NULL) {
139 DBG("Disabling channel %s", channel_name);
140
141 ret = lttng_disable_channel(handle, channel_name);
142 if (ret < 0) {
143 ERR("Channel %s: %s (session %s)", channel_name,
144 lttng_strerror(ret), session_name);
145 warn = 1;
146
147 /*
148 * Mi:
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.
153 */
154 enabled = 1;
155 success = 0;
156
157 } else {
158 MSG("%s channel %s disabled for session %s",
159 lttng_domain_type_str(dom.type),
160 channel_name, session_name);
161 enabled = 0;
162 success = 1;
163 }
164
165 /* Print the channel */
166 if (lttng_opt_mi) {
167 ret = mi_partial_channel_print(channel_name, enabled, success);
168 if (ret) {
169 ret = CMD_ERROR;
170 goto error;
171 }
172 }
173
174 /* Next channel */
175 channel_name = strtok(NULL, ",");
176 }
177
178 ret = CMD_SUCCESS;
179
180 /* Close Mi */
181 if (lttng_opt_mi) {
182 /* Close channels element */
183 ret = mi_lttng_writer_close_element(writer);
184 if (ret) {
185 ret = CMD_ERROR;
186 goto error;
187 }
188 }
189
190 error:
191 /* Bypass the warning if a more important error happened */
192 if (!ret && warn) {
193 ret = CMD_WARNING;
194 }
195
196 lttng_destroy_handle(handle);
197
198 return ret;
199 }
200
201 /*
202 * cmd_disable_channels
203 *
204 * Disable channel to trace session
205 */
206 int cmd_disable_channels(int argc, const char **argv)
207 {
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;
212
213 pc = poptGetContext(NULL, argc, argv, long_options, 0);
214 poptReadDefaultConfig(pc, 0);
215
216 while ((opt = poptGetNextOpt(pc)) != -1) {
217 switch (opt) {
218 case OPT_HELP:
219 SHOW_HELP();
220 goto end;
221 case OPT_USERSPACE:
222 opt_userspace = 1;
223 break;
224 case OPT_LIST_OPTIONS:
225 list_cmd_options(stdout, long_options);
226 goto end;
227 default:
228 ret = CMD_UNDEFINED;
229 goto end;
230 }
231 }
232
233 ret = print_missing_or_multiple_domains(
234 opt_kernel + opt_userspace, false);
235 if (ret) {
236 ret = CMD_ERROR;
237 goto end;
238 }
239
240 opt_channels = (char*) poptGetArg(pc);
241 if (opt_channels == NULL) {
242 ERR("Missing channel name(s).\n");
243 ret = CMD_ERROR;
244 goto end;
245 }
246
247 leftover = poptGetArg(pc);
248 if (leftover) {
249 ERR("Unknown argument: %s", leftover);
250 ret = CMD_ERROR;
251 goto end;
252 }
253
254 if (!opt_session_name) {
255 session_name = get_session_name();
256 if (session_name == NULL) {
257 ret = CMD_ERROR;
258 goto end;
259 }
260 } else {
261 session_name = opt_session_name;
262 }
263
264 /* Mi check */
265 if (lttng_opt_mi) {
266 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
267 if (!writer) {
268 ret = -LTTNG_ERR_NOMEM;
269 goto end;
270 }
271
272 /* Open command element */
273 ret = mi_lttng_writer_command_open(writer,
274 mi_lttng_element_command_disable_channel);
275 if (ret) {
276 ret = CMD_ERROR;
277 goto end;
278 }
279
280 /* Open output element */
281 ret = mi_lttng_writer_open_element(writer,
282 mi_lttng_element_command_output);
283 if (ret) {
284 ret = CMD_ERROR;
285 goto end;
286 }
287 }
288
289 command_ret = disable_channels(session_name);
290 if (command_ret) {
291 success = 0;
292 }
293
294 /* Mi closing */
295 if (lttng_opt_mi) {
296 /* Close output element */
297 ret = mi_lttng_writer_close_element(writer);
298 if (ret) {
299 ret = CMD_ERROR;
300 goto end;
301 }
302
303 /* Success ? */
304 ret = mi_lttng_writer_write_element_bool(writer,
305 mi_lttng_element_success, success);
306 if (ret) {
307 ret = CMD_ERROR;
308 goto end;
309 }
310
311 /* Command element close */
312 ret = mi_lttng_writer_command_close(writer);
313 if (ret) {
314 ret = CMD_ERROR;
315 goto end;
316 }
317 }
318
319 end:
320 /* Mi clean-up */
321 if (writer && mi_lttng_writer_destroy(writer)) {
322 /* Preserve original error code */
323 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
324 }
325
326 if (!opt_session_name && session_name) {
327 free(session_name);
328 }
329
330 /* Overwrite ret if an error occurred in disable_channels */
331 ret = command_ret ? command_ret : ret;
332
333 poptFreeContext(pc);
334 return ret;
335 }
This page took 0.052168 seconds and 4 git commands to generate.