Add kernel namespace contexts
[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 const char *leftover = NULL;
220
221 pc = poptGetContext(NULL, argc, argv, long_options, 0);
222 poptReadDefaultConfig(pc, 0);
223
224 while ((opt = poptGetNextOpt(pc)) != -1) {
225 switch (opt) {
226 case OPT_HELP:
227 SHOW_HELP();
228 goto end;
229 case OPT_USERSPACE:
230 opt_userspace = 1;
231 break;
232 case OPT_LIST_OPTIONS:
233 list_cmd_options(stdout, long_options);
234 goto end;
235 default:
236 ret = CMD_UNDEFINED;
237 goto end;
238 }
239 }
240
241 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
242 if (ret) {
243 ret = CMD_ERROR;
244 goto end;
245 }
246
247 opt_channels = (char*) poptGetArg(pc);
248 if (opt_channels == NULL) {
249 ERR("Missing channel name(s).\n");
250 ret = CMD_ERROR;
251 goto end;
252 }
253
254 leftover = poptGetArg(pc);
255 if (leftover) {
256 ERR("Unknown argument: %s", leftover);
257 ret = CMD_ERROR;
258 goto end;
259 }
260
261 if (!opt_session_name) {
262 session_name = get_session_name();
263 if (session_name == NULL) {
264 ret = CMD_ERROR;
265 goto end;
266 }
267 } else {
268 session_name = opt_session_name;
269 }
270
271 /* Mi check */
272 if (lttng_opt_mi) {
273 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
274 if (!writer) {
275 ret = -LTTNG_ERR_NOMEM;
276 goto end;
277 }
278
279 /* Open command element */
280 ret = mi_lttng_writer_command_open(writer,
281 mi_lttng_element_command_disable_channel);
282 if (ret) {
283 ret = CMD_ERROR;
284 goto end;
285 }
286
287 /* Open output element */
288 ret = mi_lttng_writer_open_element(writer,
289 mi_lttng_element_command_output);
290 if (ret) {
291 ret = CMD_ERROR;
292 goto end;
293 }
294 }
295
296 command_ret = disable_channels(session_name);
297 if (command_ret) {
298 success = 0;
299 }
300
301 /* Mi closing */
302 if (lttng_opt_mi) {
303 /* Close output element */
304 ret = mi_lttng_writer_close_element(writer);
305 if (ret) {
306 ret = CMD_ERROR;
307 goto end;
308 }
309
310 /* Success ? */
311 ret = mi_lttng_writer_write_element_bool(writer,
312 mi_lttng_element_success, success);
313 if (ret) {
314 ret = CMD_ERROR;
315 goto end;
316 }
317
318 /* Command element close */
319 ret = mi_lttng_writer_command_close(writer);
320 if (ret) {
321 ret = CMD_ERROR;
322 goto end;
323 }
324 }
325
326 end:
327 /* Mi clean-up */
328 if (writer && mi_lttng_writer_destroy(writer)) {
329 /* Preserve original error code */
330 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
331 }
332
333 if (!opt_session_name && session_name) {
334 free(session_name);
335 }
336
337 /* Overwrite ret if an error occurred in disable_channels */
338 ret = command_ret ? command_ret : ret;
339
340 poptFreeContext(pc);
341 return ret;
342 }
This page took 0.038234 seconds and 4 git commands to generate.