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