Fix: define _LGPL_SOURCE in C files
[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 print_missing_domain();
147 ret = CMD_ERROR;
148 goto error;
149 }
150
151 handle = lttng_create_handle(session_name, &dom);
152 if (handle == NULL) {
153 ret = -1;
154 goto error;
155 }
156
157 /* Prepare MI */
158 if (lttng_opt_mi) {
159 /* open a channels element */
160 ret = mi_lttng_writer_open_element(writer, config_element_channels);
161 if (ret) {
162 ret = CMD_ERROR;
163 goto error;
164 }
165
166 }
167
168 /* Strip channel list */
169 channel_name = strtok(opt_channels, ",");
170 while (channel_name != NULL) {
171 DBG("Disabling channel %s", channel_name);
172
173 ret = lttng_disable_channel(handle, channel_name);
174 if (ret < 0) {
175 ERR("Channel %s: %s (session %s)", channel_name,
176 lttng_strerror(ret), session_name);
177 warn = 1;
178
179 /*
180 * Mi:
181 * We assume that if an error occurred the channel is still active.
182 * This might not be the case but is a good assumption.
183 * The client should look at the stderr stream
184 * for more informations.
185 */
186 enabled = 1;
187 success = 0;
188
189 } else {
190 MSG("%s channel %s disabled for session %s",
191 get_domain_str(dom.type), channel_name, session_name);
192 enabled = 0;
193 success = 1;
194 }
195
196 /* Print the channel */
197 if (lttng_opt_mi) {
198 ret = mi_partial_channel_print(channel_name, enabled, success);
199 if (ret) {
200 ret = CMD_ERROR;
201 goto error;
202 }
203 }
204
205 /* Next channel */
206 channel_name = strtok(NULL, ",");
207 }
208
209 ret = CMD_SUCCESS;
210
211 /* Close Mi */
212 if (lttng_opt_mi) {
213 /* Close channels element */
214 ret = mi_lttng_writer_close_element(writer);
215 if (ret) {
216 ret = CMD_ERROR;
217 goto error;
218 }
219 }
220
221 error:
222 /* Bypass the warning if a more important error happened */
223 if (!ret && warn) {
224 ret = CMD_WARNING;
225 }
226
227 lttng_destroy_handle(handle);
228
229 return ret;
230 }
231
232 /*
233 * cmd_disable_channels
234 *
235 * Disable channel to trace session
236 */
237 int cmd_disable_channels(int argc, const char **argv)
238 {
239 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
240 static poptContext pc;
241 char *session_name = NULL;
242
243 pc = poptGetContext(NULL, argc, argv, long_options, 0);
244 poptReadDefaultConfig(pc, 0);
245
246 while ((opt = poptGetNextOpt(pc)) != -1) {
247 switch (opt) {
248 case OPT_HELP:
249 usage(stdout);
250 goto end;
251 case OPT_USERSPACE:
252 opt_userspace = 1;
253 break;
254 case OPT_LIST_OPTIONS:
255 list_cmd_options(stdout, long_options);
256 goto end;
257 default:
258 usage(stderr);
259 ret = CMD_UNDEFINED;
260 goto end;
261 }
262 }
263
264 opt_channels = (char*) poptGetArg(pc);
265 if (opt_channels == NULL) {
266 ERR("Missing channel name(s).\n");
267 usage(stderr);
268 ret = CMD_ERROR;
269 goto end;
270 }
271
272 if (!opt_session_name) {
273 session_name = get_session_name();
274 if (session_name == NULL) {
275 ret = CMD_ERROR;
276 goto end;
277 }
278 } else {
279 session_name = opt_session_name;
280 }
281
282 /* Mi check */
283 if (lttng_opt_mi) {
284 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
285 if (!writer) {
286 ret = -LTTNG_ERR_NOMEM;
287 goto end;
288 }
289
290 /* Open command element */
291 ret = mi_lttng_writer_command_open(writer,
292 mi_lttng_element_command_disable_channel);
293 if (ret) {
294 ret = CMD_ERROR;
295 goto end;
296 }
297
298 /* Open output element */
299 ret = mi_lttng_writer_open_element(writer,
300 mi_lttng_element_command_output);
301 if (ret) {
302 ret = CMD_ERROR;
303 goto end;
304 }
305 }
306
307 command_ret = disable_channels(session_name);
308 if (command_ret) {
309 success = 0;
310 }
311
312 /* Mi closing */
313 if (lttng_opt_mi) {
314 /* Close output element */
315 ret = mi_lttng_writer_close_element(writer);
316 if (ret) {
317 ret = CMD_ERROR;
318 goto end;
319 }
320
321 /* Success ? */
322 ret = mi_lttng_writer_write_element_bool(writer,
323 mi_lttng_element_success, success);
324 if (ret) {
325 ret = CMD_ERROR;
326 goto end;
327 }
328
329 /* Command element close */
330 ret = mi_lttng_writer_command_close(writer);
331 if (ret) {
332 ret = CMD_ERROR;
333 goto end;
334 }
335 }
336
337 end:
338 /* Mi clean-up */
339 if (writer && mi_lttng_writer_destroy(writer)) {
340 /* Preserve original error code */
341 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
342 }
343
344 if (!opt_session_name && session_name) {
345 free(session_name);
346 }
347
348 /* Overwrite ret if an error occurred in disable_channels */
349 ret = command_ret ? command_ret : ret;
350
351 poptFreeContext(pc);
352 return ret;
353 }
This page took 0.036979 seconds and 5 git commands to generate.