Mi fix: refactor disable_channel mi output to success element
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.c
CommitLineData
26cc6b4e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
26cc6b4e
DG
7 *
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.
12 *
d14d33bf
AM
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.
26cc6b4e
DG
16 */
17
18#define _GNU_SOURCE
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
50534d6f
JRJ
26#include <assert.h>
27
28#include <common/mi-lttng.h>
26cc6b4e 29
c399183f 30#include "../command.h"
26cc6b4e
DG
31
32static char *opt_channels;
e14f64a8 33static int opt_kernel;
5440dc42 34static char *opt_session_name;
26cc6b4e 35static int opt_userspace;
d78d6610
DG
36#if 0
37/* Not implemented yet */
eeac7d46 38static char *opt_cmd_name;
26cc6b4e 39static pid_t opt_pid;
d78d6610 40#endif
26cc6b4e
DG
41
42enum {
43 OPT_HELP = 1,
44 OPT_USERSPACE,
679b4943 45 OPT_LIST_OPTIONS,
26cc6b4e
DG
46};
47
cd80958d 48static struct lttng_handle *handle;
50534d6f 49static struct mi_writer *writer;
cd80958d 50
26cc6b4e
DG
51static struct poptOption long_options[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
53 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 54 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
26cc6b4e 55 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610
DG
56#if 0
57 /* Not implemented yet */
6caa2bcc 58 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
26cc6b4e 59 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
d78d6610
DG
60#else
61 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
62#endif
679b4943 63 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
26cc6b4e
DG
64 {0, 0, 0, 0, 0, 0, 0}
65};
66
67/*
68 * usage
69 */
70static void usage(FILE *ofp)
71{
272c6a17 72 fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
26cc6b4e 73 fprintf(ofp, "\n");
32a6298d 74 fprintf(ofp, "Options:\n");
78f0bacd 75 fprintf(ofp, " -h, --help Show this help\n");
679b4943 76 fprintf(ofp, " --list-options Simple listing of options\n");
32a6298d 77 fprintf(ofp, " -s, --session NAME Apply to session name\n");
27221701 78 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
27221701 79 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
26cc6b4e
DG
80 fprintf(ofp, "\n");
81}
82
50534d6f
JRJ
83static int mi_partial_channel_print(char *channel_name, unsigned int enabled,
84 int success)
85{
86 int ret;
87
88 assert(writer);
89 assert(channel_name);
90
91 /* Open channel element */
92 ret = mi_lttng_writer_open_element(writer, config_element_channel);
93 if (ret) {
94 goto end;
95 }
96
97 /* Name */
98 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
99 channel_name);
100 if (ret) {
101 goto end;
102 }
103
104 /* Enabled ? */
105 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
106 enabled);
107 if (ret) {
108 goto end;
109 }
110
111 /* Success ? */
112 ret = mi_lttng_writer_write_element_bool(writer,
9618049b 113 mi_lttng_element_success, success);
50534d6f
JRJ
114 if (ret) {
115 goto end;
116 }
117
118 /* Closing channel element */
119 ret = mi_lttng_writer_close_element(writer);
120
121end:
122 return ret;
123}
124
26cc6b4e 125/*
cd80958d 126 * Disabling channel using the lttng API.
26cc6b4e 127 */
cd80958d 128static int disable_channels(char *session_name)
26cc6b4e 129{
50534d6f
JRJ
130 int ret = CMD_SUCCESS, warn = 0, success;
131
132 /* Normal case for disable channed is enabled = 0 */
133 unsigned int enabled = 0;
26cc6b4e 134 char *channel_name;
7d29a247 135 struct lttng_domain dom;
26cc6b4e 136
441c16a7
MD
137 memset(&dom, 0, sizeof(dom));
138
d78d6610 139 /* Create lttng domain */
7d29a247
DG
140 if (opt_kernel) {
141 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 142 } else if (opt_userspace) {
78f0bacd 143 dom.type = LTTNG_DOMAIN_UST;
78f0bacd 144 } else {
b9dfb167 145 print_missing_domain();
d16c1a4c 146 ret = CMD_ERROR;
78f0bacd 147 goto error;
7d29a247
DG
148 }
149
cd80958d
DG
150 handle = lttng_create_handle(session_name, &dom);
151 if (handle == NULL) {
152 ret = -1;
153 goto error;
154 }
155
50534d6f
JRJ
156 /* Prepare MI */
157 if (lttng_opt_mi) {
158 /* open a channels element */
159 ret = mi_lttng_writer_open_element(writer, config_element_channels);
160 if (ret) {
161 ret = CMD_ERROR;
162 goto error;
163 }
164
165 }
166
26cc6b4e
DG
167 /* Strip channel list */
168 channel_name = strtok(opt_channels, ",");
169 while (channel_name != NULL) {
78f0bacd
DG
170 DBG("Disabling channel %s", channel_name);
171
172 ret = lttng_disable_channel(handle, channel_name);
173 if (ret < 0) {
ae856491
DG
174 ERR("Channel %s: %s (session %s)", channel_name,
175 lttng_strerror(ret), session_name);
176 warn = 1;
50534d6f
JRJ
177
178 /*
179 * Mi:
180 * We assume that if an error occurred the channel is still active.
181 * This might not be the case but is a good assumption.
9618049b
JRJ
182 * The client should look at the stderr stream
183 * for more informations.
50534d6f
JRJ
184 */
185 enabled = 1;
186 success = 0;
187
26cc6b4e 188 } else {
7885e399 189 MSG("%s channel %s disabled for session %s",
b9dfb167 190 get_domain_str(dom.type), channel_name, session_name);
50534d6f
JRJ
191 enabled = 0;
192 success = 1;
193 }
194
195 /* Print the channel */
196 if (lttng_opt_mi) {
197 ret = mi_partial_channel_print(channel_name, enabled, success);
198 if (ret) {
199 ret = CMD_ERROR;
200 goto error;
201 }
26cc6b4e
DG
202 }
203
204 /* Next channel */
205 channel_name = strtok(NULL, ",");
206 }
207
ae856491
DG
208 ret = CMD_SUCCESS;
209
50534d6f
JRJ
210 /* Close Mi */
211 if (lttng_opt_mi) {
212 /* Close channels element */
213 ret = mi_lttng_writer_close_element(writer);
214 if (ret) {
215 ret = CMD_ERROR;
216 goto error;
217 }
218 }
219
26cc6b4e 220error:
50534d6f
JRJ
221 /* Bypass the warning if a more important error happened */
222 if (!ret && warn) {
ae856491
DG
223 ret = CMD_WARNING;
224 }
225
cd80958d
DG
226 lttng_destroy_handle(handle);
227
26cc6b4e
DG
228 return ret;
229}
230
231/*
232 * cmd_disable_channels
233 *
234 * Disable channel to trace session
235 */
236int cmd_disable_channels(int argc, const char **argv)
237{
50534d6f 238 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
26cc6b4e 239 static poptContext pc;
cd80958d 240 char *session_name = NULL;
26cc6b4e
DG
241
242 pc = poptGetContext(NULL, argc, argv, long_options, 0);
243 poptReadDefaultConfig(pc, 0);
244
245 while ((opt = poptGetNextOpt(pc)) != -1) {
246 switch (opt) {
247 case OPT_HELP:
ca1c3607 248 usage(stdout);
26cc6b4e
DG
249 goto end;
250 case OPT_USERSPACE:
251 opt_userspace = 1;
252 break;
679b4943
SM
253 case OPT_LIST_OPTIONS:
254 list_cmd_options(stdout, long_options);
679b4943 255 goto end;
26cc6b4e
DG
256 default:
257 usage(stderr);
258 ret = CMD_UNDEFINED;
259 goto end;
260 }
261 }
262
263 opt_channels = (char*) poptGetArg(pc);
264 if (opt_channels == NULL) {
265 ERR("Missing channel name(s).\n");
266 usage(stderr);
ca1c3607 267 ret = CMD_ERROR;
26cc6b4e
DG
268 goto end;
269 }
270
cd80958d
DG
271 if (!opt_session_name) {
272 session_name = get_session_name();
273 if (session_name == NULL) {
ca1c3607 274 ret = CMD_ERROR;
cd80958d
DG
275 goto end;
276 }
277 } else {
278 session_name = opt_session_name;
279 }
280
50534d6f
JRJ
281 /* Mi check */
282 if (lttng_opt_mi) {
283 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
284 if (!writer) {
285 ret = -LTTNG_ERR_NOMEM;
286 goto end;
287 }
288
289 /* Open command element */
290 ret = mi_lttng_writer_command_open(writer,
291 mi_lttng_element_command_disable_channel);
292 if (ret) {
293 ret = CMD_ERROR;
294 goto end;
295 }
296
297 /* Open output element */
298 ret = mi_lttng_writer_open_element(writer,
299 mi_lttng_element_command_output);
300 if (ret) {
301 ret = CMD_ERROR;
302 goto end;
303 }
304 }
305
306 command_ret = disable_channels(session_name);
307 if (command_ret) {
308 success = 0;
309 }
310
311 /* Mi closing */
312 if (lttng_opt_mi) {
313 /* Close output element */
314 ret = mi_lttng_writer_close_element(writer);
315 if (ret) {
316 ret = CMD_ERROR;
317 goto end;
318 }
319
320 /* Success ? */
321 ret = mi_lttng_writer_write_element_bool(writer,
322 mi_lttng_element_success, success);
323 if (ret) {
324 ret = CMD_ERROR;
325 goto end;
326 }
327
328 /* Command element close */
329 ret = mi_lttng_writer_command_close(writer);
330 if (ret) {
331 ret = CMD_ERROR;
332 goto end;
333 }
334 }
26cc6b4e
DG
335
336end:
50534d6f
JRJ
337 /* Mi clean-up */
338 if (writer && mi_lttng_writer_destroy(writer)) {
339 /* Preserve original error code */
340 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
341 }
342
5853fd43
DG
343 if (!opt_session_name && session_name) {
344 free(session_name);
345 }
50534d6f
JRJ
346
347 /* Overwrite ret if an error occurred in disable_channels */
348 ret = command_ret ? command_ret : ret;
349
ca1c3607 350 poptFreeContext(pc);
26cc6b4e
DG
351 return ret;
352}
This page took 0.080015 seconds and 4 git commands to generate.