Fix: error on no/multiple domain options
[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 {
deaef3cf
PP
145 /* Checked by the caller. */
146 assert(0);
7d29a247
DG
147 }
148
cd80958d
DG
149 handle = lttng_create_handle(session_name, &dom);
150 if (handle == NULL) {
151 ret = -1;
152 goto error;
153 }
154
50534d6f
JRJ
155 /* Prepare MI */
156 if (lttng_opt_mi) {
157 /* open a channels element */
158 ret = mi_lttng_writer_open_element(writer, config_element_channels);
159 if (ret) {
160 ret = CMD_ERROR;
161 goto error;
162 }
163
164 }
165
26cc6b4e
DG
166 /* Strip channel list */
167 channel_name = strtok(opt_channels, ",");
168 while (channel_name != NULL) {
78f0bacd
DG
169 DBG("Disabling channel %s", channel_name);
170
171 ret = lttng_disable_channel(handle, channel_name);
172 if (ret < 0) {
ae856491
DG
173 ERR("Channel %s: %s (session %s)", channel_name,
174 lttng_strerror(ret), session_name);
175 warn = 1;
50534d6f
JRJ
176
177 /*
178 * Mi:
179 * We assume that if an error occurred the channel is still active.
180 * This might not be the case but is a good assumption.
9618049b
JRJ
181 * The client should look at the stderr stream
182 * for more informations.
50534d6f
JRJ
183 */
184 enabled = 1;
185 success = 0;
186
26cc6b4e 187 } else {
7885e399 188 MSG("%s channel %s disabled for session %s",
b9dfb167 189 get_domain_str(dom.type), channel_name, session_name);
50534d6f
JRJ
190 enabled = 0;
191 success = 1;
192 }
193
194 /* Print the channel */
195 if (lttng_opt_mi) {
196 ret = mi_partial_channel_print(channel_name, enabled, success);
197 if (ret) {
198 ret = CMD_ERROR;
199 goto error;
200 }
26cc6b4e
DG
201 }
202
203 /* Next channel */
204 channel_name = strtok(NULL, ",");
205 }
206
ae856491
DG
207 ret = CMD_SUCCESS;
208
50534d6f
JRJ
209 /* Close Mi */
210 if (lttng_opt_mi) {
211 /* Close channels element */
212 ret = mi_lttng_writer_close_element(writer);
213 if (ret) {
214 ret = CMD_ERROR;
215 goto error;
216 }
217 }
218
26cc6b4e 219error:
50534d6f
JRJ
220 /* Bypass the warning if a more important error happened */
221 if (!ret && warn) {
ae856491
DG
222 ret = CMD_WARNING;
223 }
224
cd80958d
DG
225 lttng_destroy_handle(handle);
226
26cc6b4e
DG
227 return ret;
228}
229
230/*
231 * cmd_disable_channels
232 *
233 * Disable channel to trace session
234 */
235int cmd_disable_channels(int argc, const char **argv)
236{
50534d6f 237 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
26cc6b4e 238 static poptContext pc;
cd80958d 239 char *session_name = NULL;
26cc6b4e
DG
240
241 pc = poptGetContext(NULL, argc, argv, long_options, 0);
242 poptReadDefaultConfig(pc, 0);
243
244 while ((opt = poptGetNextOpt(pc)) != -1) {
245 switch (opt) {
246 case OPT_HELP:
ca1c3607 247 usage(stdout);
26cc6b4e
DG
248 goto end;
249 case OPT_USERSPACE:
250 opt_userspace = 1;
251 break;
679b4943
SM
252 case OPT_LIST_OPTIONS:
253 list_cmd_options(stdout, long_options);
679b4943 254 goto end;
26cc6b4e
DG
255 default:
256 usage(stderr);
257 ret = CMD_UNDEFINED;
258 goto end;
259 }
260 }
261
deaef3cf
PP
262 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
263 if (ret) {
264 ret = CMD_ERROR;
265 goto end;
266 }
267
26cc6b4e
DG
268 opt_channels = (char*) poptGetArg(pc);
269 if (opt_channels == NULL) {
270 ERR("Missing channel name(s).\n");
271 usage(stderr);
ca1c3607 272 ret = CMD_ERROR;
26cc6b4e
DG
273 goto end;
274 }
275
cd80958d
DG
276 if (!opt_session_name) {
277 session_name = get_session_name();
278 if (session_name == NULL) {
ca1c3607 279 ret = CMD_ERROR;
cd80958d
DG
280 goto end;
281 }
282 } else {
283 session_name = opt_session_name;
284 }
285
50534d6f
JRJ
286 /* Mi check */
287 if (lttng_opt_mi) {
288 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
289 if (!writer) {
290 ret = -LTTNG_ERR_NOMEM;
291 goto end;
292 }
293
294 /* Open command element */
295 ret = mi_lttng_writer_command_open(writer,
296 mi_lttng_element_command_disable_channel);
297 if (ret) {
298 ret = CMD_ERROR;
299 goto end;
300 }
301
302 /* Open output element */
303 ret = mi_lttng_writer_open_element(writer,
304 mi_lttng_element_command_output);
305 if (ret) {
306 ret = CMD_ERROR;
307 goto end;
308 }
309 }
310
311 command_ret = disable_channels(session_name);
312 if (command_ret) {
313 success = 0;
314 }
315
316 /* Mi closing */
317 if (lttng_opt_mi) {
318 /* Close output element */
319 ret = mi_lttng_writer_close_element(writer);
320 if (ret) {
321 ret = CMD_ERROR;
322 goto end;
323 }
324
325 /* Success ? */
326 ret = mi_lttng_writer_write_element_bool(writer,
327 mi_lttng_element_success, success);
328 if (ret) {
329 ret = CMD_ERROR;
330 goto end;
331 }
332
333 /* Command element close */
334 ret = mi_lttng_writer_command_close(writer);
335 if (ret) {
336 ret = CMD_ERROR;
337 goto end;
338 }
339 }
26cc6b4e
DG
340
341end:
50534d6f
JRJ
342 /* Mi clean-up */
343 if (writer && mi_lttng_writer_destroy(writer)) {
344 /* Preserve original error code */
345 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
346 }
347
5853fd43
DG
348 if (!opt_session_name && session_name) {
349 free(session_name);
350 }
50534d6f
JRJ
351
352 /* Overwrite ret if an error occurred in disable_channels */
353 ret = command_ret ? command_ret : ret;
354
ca1c3607 355 poptFreeContext(pc);
26cc6b4e
DG
356 return ret;
357}
This page took 0.05242 seconds and 4 git commands to generate.