7ae2ed79ad9439f036d2b1606581174321ba290c
[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 #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 #if 0
37 /* Not implemented yet */
38 static char *opt_cmd_name;
39 static pid_t opt_pid;
40 #endif
41
42 enum {
43 OPT_HELP = 1,
44 OPT_USERSPACE,
45 OPT_LIST_OPTIONS,
46 };
47
48 static struct lttng_handle *handle;
49 static struct mi_writer *writer;
50
51 static struct poptOption long_options[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
53 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
54 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
55 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
56 #if 0
57 /* Not implemented yet */
58 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
59 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
60 #else
61 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
62 #endif
63 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
64 {0, 0, 0, 0, 0, 0, 0}
65 };
66
67 /*
68 * usage
69 */
70 static void usage(FILE *ofp)
71 {
72 fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
73 fprintf(ofp, "\n");
74 fprintf(ofp, "Options:\n");
75 fprintf(ofp, " -h, --help Show this help\n");
76 fprintf(ofp, " --list-options Simple listing of options\n");
77 fprintf(ofp, " -s, --session NAME Apply to session name\n");
78 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
79 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
80 fprintf(ofp, "\n");
81 }
82
83 static 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,
113 mi_lttng_element_command, success);
114 if (ret) {
115 goto end;
116 }
117
118 /* Closing channel element */
119 ret = mi_lttng_writer_close_element(writer);
120
121 end:
122 return ret;
123 }
124
125 /*
126 * Disabling channel using the lttng API.
127 */
128 static int disable_channels(char *session_name)
129 {
130 int ret = CMD_SUCCESS, warn = 0, success;
131
132 /* Normal case for disable channed is enabled = 0 */
133 unsigned int enabled = 0;
134 char *channel_name;
135 struct lttng_domain dom;
136
137 memset(&dom, 0, sizeof(dom));
138
139 /* Create lttng domain */
140 if (opt_kernel) {
141 dom.type = LTTNG_DOMAIN_KERNEL;
142 } else if (opt_userspace) {
143 dom.type = LTTNG_DOMAIN_UST;
144 } else {
145 print_missing_domain();
146 ret = CMD_ERROR;
147 goto error;
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 for more informations.
183 */
184 enabled = 1;
185 success = 0;
186
187 } else {
188 MSG("%s channel %s disabled for session %s",
189 get_domain_str(dom.type), channel_name, session_name);
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 }
201 }
202
203 /* Next channel */
204 channel_name = strtok(NULL, ",");
205 }
206
207 ret = CMD_SUCCESS;
208
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
219 error:
220 /* Bypass the warning if a more important error happened */
221 if (!ret && warn) {
222 ret = CMD_WARNING;
223 }
224
225 lttng_destroy_handle(handle);
226
227 return ret;
228 }
229
230 /*
231 * cmd_disable_channels
232 *
233 * Disable channel to trace session
234 */
235 int cmd_disable_channels(int argc, const char **argv)
236 {
237 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
238 static poptContext pc;
239 char *session_name = NULL;
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:
247 usage(stdout);
248 goto end;
249 case OPT_USERSPACE:
250 opt_userspace = 1;
251 break;
252 case OPT_LIST_OPTIONS:
253 list_cmd_options(stdout, long_options);
254 goto end;
255 default:
256 usage(stderr);
257 ret = CMD_UNDEFINED;
258 goto end;
259 }
260 }
261
262 opt_channels = (char*) poptGetArg(pc);
263 if (opt_channels == NULL) {
264 ERR("Missing channel name(s).\n");
265 usage(stderr);
266 ret = CMD_ERROR;
267 goto end;
268 }
269
270 if (!opt_session_name) {
271 session_name = get_session_name();
272 if (session_name == NULL) {
273 ret = CMD_ERROR;
274 goto end;
275 }
276 } else {
277 session_name = opt_session_name;
278 }
279
280 /* Mi check */
281 if (lttng_opt_mi) {
282 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
283 if (!writer) {
284 ret = -LTTNG_ERR_NOMEM;
285 goto end;
286 }
287
288 /* Open command element */
289 ret = mi_lttng_writer_command_open(writer,
290 mi_lttng_element_command_disable_channel);
291 if (ret) {
292 ret = CMD_ERROR;
293 goto end;
294 }
295
296 /* Open output element */
297 ret = mi_lttng_writer_open_element(writer,
298 mi_lttng_element_command_output);
299 if (ret) {
300 ret = CMD_ERROR;
301 goto end;
302 }
303 }
304
305 command_ret = disable_channels(session_name);
306 if (command_ret) {
307 success = 0;
308 }
309
310 /* Mi closing */
311 if (lttng_opt_mi) {
312 /* Close output element */
313 ret = mi_lttng_writer_close_element(writer);
314 if (ret) {
315 ret = CMD_ERROR;
316 goto end;
317 }
318
319 /* Success ? */
320 ret = mi_lttng_writer_write_element_bool(writer,
321 mi_lttng_element_success, success);
322 if (ret) {
323 ret = CMD_ERROR;
324 goto end;
325 }
326
327 /* Command element close */
328 ret = mi_lttng_writer_command_close(writer);
329 if (ret) {
330 ret = CMD_ERROR;
331 goto end;
332 }
333 }
334
335 end:
336 /* Mi clean-up */
337 if (writer && mi_lttng_writer_destroy(writer)) {
338 /* Preserve original error code */
339 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
340 }
341
342 if (!opt_session_name && session_name) {
343 free(session_name);
344 }
345
346 /* Overwrite ret if an error occurred in disable_channels */
347 ret = command_ret ? command_ret : ret;
348
349 poptFreeContext(pc);
350 return ret;
351 }
This page took 0.051325 seconds and 3 git commands to generate.