6bb123fc72a71ea3a29e0a74fca174f437a0676c
[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 _GNU_SOURCE
19 #define _LGPL_SOURCE
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <assert.h>
28
29 #include <common/mi-lttng.h>
30
31 #include "../command.h"
32
33 static char *opt_channels;
34 static int opt_kernel;
35 static char *opt_session_name;
36 static int opt_userspace;
37 #if 0
38 /* Not implemented yet */
39 static char *opt_cmd_name;
40 static pid_t opt_pid;
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 #if 0
58 /* Not implemented yet */
59 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
60 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
61 #else
62 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
63 #endif
64 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
65 {0, 0, 0, 0, 0, 0, 0}
66 };
67
68 /*
69 * usage
70 */
71 static void usage(FILE *ofp)
72 {
73 fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
74 fprintf(ofp, "\n");
75 fprintf(ofp, "Options:\n");
76 fprintf(ofp, " -h, --help Show this help\n");
77 fprintf(ofp, " --list-options Simple listing of options\n");
78 fprintf(ofp, " -s, --session NAME Apply to session name\n");
79 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
80 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
81 fprintf(ofp, "\n");
82 }
83
84 static int mi_partial_channel_print(char *channel_name, unsigned int enabled,
85 int success)
86 {
87 int ret;
88
89 assert(writer);
90 assert(channel_name);
91
92 /* Open channel element */
93 ret = mi_lttng_writer_open_element(writer, config_element_channel);
94 if (ret) {
95 goto end;
96 }
97
98 /* Name */
99 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
100 channel_name);
101 if (ret) {
102 goto end;
103 }
104
105 /* Enabled ? */
106 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
107 enabled);
108 if (ret) {
109 goto end;
110 }
111
112 /* Success ? */
113 ret = mi_lttng_writer_write_element_bool(writer,
114 mi_lttng_element_success, success);
115 if (ret) {
116 goto end;
117 }
118
119 /* Closing channel element */
120 ret = mi_lttng_writer_close_element(writer);
121
122 end:
123 return ret;
124 }
125
126 /*
127 * Disabling channel using the lttng API.
128 */
129 static int disable_channels(char *session_name)
130 {
131 int ret = CMD_SUCCESS, warn = 0, success;
132
133 /* Normal case for disable channed is enabled = 0 */
134 unsigned int enabled = 0;
135 char *channel_name;
136 struct lttng_domain dom;
137
138 memset(&dom, 0, sizeof(dom));
139
140 /* Create lttng domain */
141 if (opt_kernel) {
142 dom.type = LTTNG_DOMAIN_KERNEL;
143 } else if (opt_userspace) {
144 dom.type = LTTNG_DOMAIN_UST;
145 } else {
146 /* Checked by the caller. */
147 assert(0);
148 }
149
150 handle = lttng_create_handle(session_name, &dom);
151 if (handle == NULL) {
152 ret = -1;
153 goto error;
154 }
155
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
167 /* Strip channel list */
168 channel_name = strtok(opt_channels, ",");
169 while (channel_name != NULL) {
170 DBG("Disabling channel %s", channel_name);
171
172 ret = lttng_disable_channel(handle, channel_name);
173 if (ret < 0) {
174 ERR("Channel %s: %s (session %s)", channel_name,
175 lttng_strerror(ret), session_name);
176 warn = 1;
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.
182 * The client should look at the stderr stream
183 * for more informations.
184 */
185 enabled = 1;
186 success = 0;
187
188 } else {
189 MSG("%s channel %s disabled for session %s",
190 get_domain_str(dom.type), channel_name, session_name);
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 }
202 }
203
204 /* Next channel */
205 channel_name = strtok(NULL, ",");
206 }
207
208 ret = CMD_SUCCESS;
209
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
220 error:
221 /* Bypass the warning if a more important error happened */
222 if (!ret && warn) {
223 ret = CMD_WARNING;
224 }
225
226 lttng_destroy_handle(handle);
227
228 return ret;
229 }
230
231 /*
232 * cmd_disable_channels
233 *
234 * Disable channel to trace session
235 */
236 int cmd_disable_channels(int argc, const char **argv)
237 {
238 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
239 static poptContext pc;
240 char *session_name = NULL;
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:
248 usage(stdout);
249 goto end;
250 case OPT_USERSPACE:
251 opt_userspace = 1;
252 break;
253 case OPT_LIST_OPTIONS:
254 list_cmd_options(stdout, long_options);
255 goto end;
256 default:
257 usage(stderr);
258 ret = CMD_UNDEFINED;
259 goto end;
260 }
261 }
262
263 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
264 if (ret) {
265 ret = CMD_ERROR;
266 goto end;
267 }
268
269 opt_channels = (char*) poptGetArg(pc);
270 if (opt_channels == NULL) {
271 ERR("Missing channel name(s).\n");
272 usage(stderr);
273 ret = CMD_ERROR;
274 goto end;
275 }
276
277 if (!opt_session_name) {
278 session_name = get_session_name();
279 if (session_name == NULL) {
280 ret = CMD_ERROR;
281 goto end;
282 }
283 } else {
284 session_name = opt_session_name;
285 }
286
287 /* Mi check */
288 if (lttng_opt_mi) {
289 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
290 if (!writer) {
291 ret = -LTTNG_ERR_NOMEM;
292 goto end;
293 }
294
295 /* Open command element */
296 ret = mi_lttng_writer_command_open(writer,
297 mi_lttng_element_command_disable_channel);
298 if (ret) {
299 ret = CMD_ERROR;
300 goto end;
301 }
302
303 /* Open output element */
304 ret = mi_lttng_writer_open_element(writer,
305 mi_lttng_element_command_output);
306 if (ret) {
307 ret = CMD_ERROR;
308 goto end;
309 }
310 }
311
312 command_ret = disable_channels(session_name);
313 if (command_ret) {
314 success = 0;
315 }
316
317 /* Mi closing */
318 if (lttng_opt_mi) {
319 /* Close output element */
320 ret = mi_lttng_writer_close_element(writer);
321 if (ret) {
322 ret = CMD_ERROR;
323 goto end;
324 }
325
326 /* Success ? */
327 ret = mi_lttng_writer_write_element_bool(writer,
328 mi_lttng_element_success, success);
329 if (ret) {
330 ret = CMD_ERROR;
331 goto end;
332 }
333
334 /* Command element close */
335 ret = mi_lttng_writer_command_close(writer);
336 if (ret) {
337 ret = CMD_ERROR;
338 goto end;
339 }
340 }
341
342 end:
343 /* Mi clean-up */
344 if (writer && mi_lttng_writer_destroy(writer)) {
345 /* Preserve original error code */
346 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
347 }
348
349 if (!opt_session_name && session_name) {
350 free(session_name);
351 }
352
353 /* Overwrite ret if an error occurred in disable_channels */
354 ret = command_ret ? command_ret : ret;
355
356 poptFreeContext(pc);
357 return ret;
358 }
This page took 0.036162 seconds and 3 git commands to generate.