Fix: define _LGPL_SOURCE in C files
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.c
CommitLineData
26cc6b4e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
26cc6b4e
DG
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 *
d14d33bf
AM
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.
26cc6b4e
DG
16 */
17
18#define _GNU_SOURCE
6c1c0768 19#define _LGPL_SOURCE
26cc6b4e
DG
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>
50534d6f
JRJ
27#include <assert.h>
28
29#include <common/mi-lttng.h>
26cc6b4e 30
c399183f 31#include "../command.h"
26cc6b4e
DG
32
33static char *opt_channels;
e14f64a8 34static int opt_kernel;
5440dc42 35static char *opt_session_name;
26cc6b4e 36static int opt_userspace;
d78d6610
DG
37#if 0
38/* Not implemented yet */
eeac7d46 39static char *opt_cmd_name;
26cc6b4e 40static pid_t opt_pid;
d78d6610 41#endif
26cc6b4e
DG
42
43enum {
44 OPT_HELP = 1,
45 OPT_USERSPACE,
679b4943 46 OPT_LIST_OPTIONS,
26cc6b4e
DG
47};
48
cd80958d 49static struct lttng_handle *handle;
50534d6f 50static struct mi_writer *writer;
cd80958d 51
26cc6b4e
DG
52static struct poptOption long_options[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 55 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
26cc6b4e 56 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610
DG
57#if 0
58 /* Not implemented yet */
6caa2bcc 59 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
26cc6b4e 60 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
d78d6610
DG
61#else
62 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
63#endif
679b4943 64 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
26cc6b4e
DG
65 {0, 0, 0, 0, 0, 0, 0}
66};
67
68/*
69 * usage
70 */
71static void usage(FILE *ofp)
72{
272c6a17 73 fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
26cc6b4e 74 fprintf(ofp, "\n");
32a6298d 75 fprintf(ofp, "Options:\n");
78f0bacd 76 fprintf(ofp, " -h, --help Show this help\n");
679b4943 77 fprintf(ofp, " --list-options Simple listing of options\n");
32a6298d 78 fprintf(ofp, " -s, --session NAME Apply to session name\n");
27221701 79 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
27221701 80 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
26cc6b4e
DG
81 fprintf(ofp, "\n");
82}
83
50534d6f
JRJ
84static 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,
9618049b 114 mi_lttng_element_success, success);
50534d6f
JRJ
115 if (ret) {
116 goto end;
117 }
118
119 /* Closing channel element */
120 ret = mi_lttng_writer_close_element(writer);
121
122end:
123 return ret;
124}
125
26cc6b4e 126/*
cd80958d 127 * Disabling channel using the lttng API.
26cc6b4e 128 */
cd80958d 129static int disable_channels(char *session_name)
26cc6b4e 130{
50534d6f
JRJ
131 int ret = CMD_SUCCESS, warn = 0, success;
132
133 /* Normal case for disable channed is enabled = 0 */
134 unsigned int enabled = 0;
26cc6b4e 135 char *channel_name;
7d29a247 136 struct lttng_domain dom;
26cc6b4e 137
441c16a7
MD
138 memset(&dom, 0, sizeof(dom));
139
d78d6610 140 /* Create lttng domain */
7d29a247
DG
141 if (opt_kernel) {
142 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 143 } else if (opt_userspace) {
78f0bacd 144 dom.type = LTTNG_DOMAIN_UST;
78f0bacd 145 } else {
b9dfb167 146 print_missing_domain();
d16c1a4c 147 ret = CMD_ERROR;
78f0bacd 148 goto error;
7d29a247
DG
149 }
150
cd80958d
DG
151 handle = lttng_create_handle(session_name, &dom);
152 if (handle == NULL) {
153 ret = -1;
154 goto error;
155 }
156
50534d6f
JRJ
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
26cc6b4e
DG
168 /* Strip channel list */
169 channel_name = strtok(opt_channels, ",");
170 while (channel_name != NULL) {
78f0bacd
DG
171 DBG("Disabling channel %s", channel_name);
172
173 ret = lttng_disable_channel(handle, channel_name);
174 if (ret < 0) {
ae856491
DG
175 ERR("Channel %s: %s (session %s)", channel_name,
176 lttng_strerror(ret), session_name);
177 warn = 1;
50534d6f
JRJ
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.
9618049b
JRJ
183 * The client should look at the stderr stream
184 * for more informations.
50534d6f
JRJ
185 */
186 enabled = 1;
187 success = 0;
188
26cc6b4e 189 } else {
7885e399 190 MSG("%s channel %s disabled for session %s",
b9dfb167 191 get_domain_str(dom.type), channel_name, session_name);
50534d6f
JRJ
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 }
26cc6b4e
DG
203 }
204
205 /* Next channel */
206 channel_name = strtok(NULL, ",");
207 }
208
ae856491
DG
209 ret = CMD_SUCCESS;
210
50534d6f
JRJ
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
26cc6b4e 221error:
50534d6f
JRJ
222 /* Bypass the warning if a more important error happened */
223 if (!ret && warn) {
ae856491
DG
224 ret = CMD_WARNING;
225 }
226
cd80958d
DG
227 lttng_destroy_handle(handle);
228
26cc6b4e
DG
229 return ret;
230}
231
232/*
233 * cmd_disable_channels
234 *
235 * Disable channel to trace session
236 */
237int cmd_disable_channels(int argc, const char **argv)
238{
50534d6f 239 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
26cc6b4e 240 static poptContext pc;
cd80958d 241 char *session_name = NULL;
26cc6b4e
DG
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:
ca1c3607 249 usage(stdout);
26cc6b4e
DG
250 goto end;
251 case OPT_USERSPACE:
252 opt_userspace = 1;
253 break;
679b4943
SM
254 case OPT_LIST_OPTIONS:
255 list_cmd_options(stdout, long_options);
679b4943 256 goto end;
26cc6b4e
DG
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);
ca1c3607 268 ret = CMD_ERROR;
26cc6b4e
DG
269 goto end;
270 }
271
cd80958d
DG
272 if (!opt_session_name) {
273 session_name = get_session_name();
274 if (session_name == NULL) {
ca1c3607 275 ret = CMD_ERROR;
cd80958d
DG
276 goto end;
277 }
278 } else {
279 session_name = opt_session_name;
280 }
281
50534d6f
JRJ
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 }
26cc6b4e
DG
336
337end:
50534d6f
JRJ
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
5853fd43
DG
344 if (!opt_session_name && session_name) {
345 free(session_name);
346 }
50534d6f
JRJ
347
348 /* Overwrite ret if an error occurred in disable_channels */
349 ret = command_ret ? command_ret : ret;
350
ca1c3607 351 poptFreeContext(pc);
26cc6b4e
DG
352 return ret;
353}
This page took 0.063083 seconds and 5 git commands to generate.