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