bin: compile lttng as C++
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.cpp
CommitLineData
26cc6b4e 1/*
ab5be9fa 2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
26cc6b4e 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
26cc6b4e 5 *
26cc6b4e
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
26cc6b4e
DG
9#include <popt.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/stat.h>
14#include <sys/types.h>
15#include <unistd.h>
50534d6f
JRJ
16
17#include <common/mi-lttng.h>
1004b719 18#include <lttng/domain-internal.h>
26cc6b4e 19
c399183f 20#include "../command.h"
26cc6b4e
DG
21
22static char *opt_channels;
e14f64a8 23static int opt_kernel;
5440dc42 24static char *opt_session_name;
26cc6b4e 25static int opt_userspace;
26cc6b4e 26
4fc83d94
PP
27#ifdef LTTNG_EMBED_HELP
28static const char help_msg[] =
29#include <lttng-disable-channel.1.h>
30;
31#endif
32
26cc6b4e
DG
33enum {
34 OPT_HELP = 1,
35 OPT_USERSPACE,
679b4943 36 OPT_LIST_OPTIONS,
26cc6b4e
DG
37};
38
cd80958d 39static struct lttng_handle *handle;
50534d6f 40static struct mi_writer *writer;
cd80958d 41
26cc6b4e
DG
42static struct poptOption long_options[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 45 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
26cc6b4e 46 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 47 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
679b4943 48 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
26cc6b4e
DG
49 {0, 0, 0, 0, 0, 0, 0}
50};
51
50534d6f
JRJ
52static int mi_partial_channel_print(char *channel_name, unsigned int enabled,
53 int success)
54{
55 int ret;
56
a0377dfe
FD
57 LTTNG_ASSERT(writer);
58 LTTNG_ASSERT(channel_name);
50534d6f
JRJ
59
60 /* Open channel element */
61 ret = mi_lttng_writer_open_element(writer, config_element_channel);
62 if (ret) {
63 goto end;
64 }
65
66 /* Name */
67 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
68 channel_name);
69 if (ret) {
70 goto end;
71 }
72
73 /* Enabled ? */
74 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
75 enabled);
76 if (ret) {
77 goto end;
78 }
79
80 /* Success ? */
81 ret = mi_lttng_writer_write_element_bool(writer,
9618049b 82 mi_lttng_element_success, success);
50534d6f
JRJ
83 if (ret) {
84 goto end;
85 }
86
87 /* Closing channel element */
88 ret = mi_lttng_writer_close_element(writer);
89
90end:
91 return ret;
92}
93
26cc6b4e 94/*
cd80958d 95 * Disabling channel using the lttng API.
26cc6b4e 96 */
cd80958d 97static int disable_channels(char *session_name)
26cc6b4e 98{
50534d6f
JRJ
99 int ret = CMD_SUCCESS, warn = 0, success;
100
101 /* Normal case for disable channed is enabled = 0 */
102 unsigned int enabled = 0;
26cc6b4e 103 char *channel_name;
7d29a247 104 struct lttng_domain dom;
26cc6b4e 105
441c16a7
MD
106 memset(&dom, 0, sizeof(dom));
107
d78d6610 108 /* Create lttng domain */
7d29a247
DG
109 if (opt_kernel) {
110 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 111 } else if (opt_userspace) {
78f0bacd 112 dom.type = LTTNG_DOMAIN_UST;
78f0bacd 113 } else {
3ecec76a 114 /* Checked by the caller. */
a0377dfe 115 abort();
7d29a247
DG
116 }
117
cd80958d
DG
118 handle = lttng_create_handle(session_name, &dom);
119 if (handle == NULL) {
120 ret = -1;
121 goto error;
122 }
123
50534d6f
JRJ
124 /* Prepare MI */
125 if (lttng_opt_mi) {
126 /* open a channels element */
127 ret = mi_lttng_writer_open_element(writer, config_element_channels);
128 if (ret) {
129 ret = CMD_ERROR;
130 goto error;
131 }
132
133 }
134
26cc6b4e
DG
135 /* Strip channel list */
136 channel_name = strtok(opt_channels, ",");
137 while (channel_name != NULL) {
78f0bacd
DG
138 DBG("Disabling channel %s", channel_name);
139
140 ret = lttng_disable_channel(handle, channel_name);
141 if (ret < 0) {
ae856491
DG
142 ERR("Channel %s: %s (session %s)", channel_name,
143 lttng_strerror(ret), session_name);
144 warn = 1;
50534d6f
JRJ
145
146 /*
147 * Mi:
148 * We assume that if an error occurred the channel is still active.
149 * This might not be the case but is a good assumption.
9618049b
JRJ
150 * The client should look at the stderr stream
151 * for more informations.
50534d6f
JRJ
152 */
153 enabled = 1;
154 success = 0;
155
26cc6b4e 156 } else {
7885e399 157 MSG("%s channel %s disabled for session %s",
1004b719
FD
158 lttng_domain_type_str(dom.type),
159 channel_name, session_name);
50534d6f
JRJ
160 enabled = 0;
161 success = 1;
162 }
163
164 /* Print the channel */
165 if (lttng_opt_mi) {
166 ret = mi_partial_channel_print(channel_name, enabled, success);
167 if (ret) {
168 ret = CMD_ERROR;
169 goto error;
170 }
26cc6b4e
DG
171 }
172
173 /* Next channel */
174 channel_name = strtok(NULL, ",");
175 }
176
ae856491
DG
177 ret = CMD_SUCCESS;
178
50534d6f
JRJ
179 /* Close Mi */
180 if (lttng_opt_mi) {
181 /* Close channels element */
182 ret = mi_lttng_writer_close_element(writer);
183 if (ret) {
184 ret = CMD_ERROR;
185 goto error;
186 }
187 }
188
26cc6b4e 189error:
50534d6f
JRJ
190 /* Bypass the warning if a more important error happened */
191 if (!ret && warn) {
ae856491
DG
192 ret = CMD_WARNING;
193 }
194
cd80958d
DG
195 lttng_destroy_handle(handle);
196
26cc6b4e
DG
197 return ret;
198}
199
200/*
201 * cmd_disable_channels
202 *
203 * Disable channel to trace session
204 */
205int cmd_disable_channels(int argc, const char **argv)
206{
50534d6f 207 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
26cc6b4e 208 static poptContext pc;
cd80958d 209 char *session_name = NULL;
68c7f6e5 210 const char *leftover = NULL;
26cc6b4e
DG
211
212 pc = poptGetContext(NULL, argc, argv, long_options, 0);
213 poptReadDefaultConfig(pc, 0);
214
215 while ((opt = poptGetNextOpt(pc)) != -1) {
216 switch (opt) {
217 case OPT_HELP:
4ba92f18 218 SHOW_HELP();
26cc6b4e
DG
219 goto end;
220 case OPT_USERSPACE:
221 opt_userspace = 1;
222 break;
679b4943
SM
223 case OPT_LIST_OPTIONS:
224 list_cmd_options(stdout, long_options);
679b4943 225 goto end;
26cc6b4e 226 default:
26cc6b4e
DG
227 ret = CMD_UNDEFINED;
228 goto end;
229 }
230 }
231
3533d06b
JG
232 ret = print_missing_or_multiple_domains(
233 opt_kernel + opt_userspace, false);
3ecec76a
PP
234 if (ret) {
235 ret = CMD_ERROR;
236 goto end;
237 }
238
26cc6b4e
DG
239 opt_channels = (char*) poptGetArg(pc);
240 if (opt_channels == NULL) {
241 ERR("Missing channel name(s).\n");
ca1c3607 242 ret = CMD_ERROR;
26cc6b4e
DG
243 goto end;
244 }
245
68c7f6e5
JD
246 leftover = poptGetArg(pc);
247 if (leftover) {
248 ERR("Unknown argument: %s", leftover);
249 ret = CMD_ERROR;
250 goto end;
251 }
252
cd80958d
DG
253 if (!opt_session_name) {
254 session_name = get_session_name();
255 if (session_name == NULL) {
ca1c3607 256 ret = CMD_ERROR;
cd80958d
DG
257 goto end;
258 }
259 } else {
260 session_name = opt_session_name;
261 }
262
50534d6f
JRJ
263 /* Mi check */
264 if (lttng_opt_mi) {
265 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
266 if (!writer) {
267 ret = -LTTNG_ERR_NOMEM;
268 goto end;
269 }
270
271 /* Open command element */
272 ret = mi_lttng_writer_command_open(writer,
273 mi_lttng_element_command_disable_channel);
274 if (ret) {
275 ret = CMD_ERROR;
276 goto end;
277 }
278
279 /* Open output element */
280 ret = mi_lttng_writer_open_element(writer,
281 mi_lttng_element_command_output);
282 if (ret) {
283 ret = CMD_ERROR;
284 goto end;
285 }
286 }
287
288 command_ret = disable_channels(session_name);
289 if (command_ret) {
290 success = 0;
291 }
292
293 /* Mi closing */
294 if (lttng_opt_mi) {
295 /* Close output element */
296 ret = mi_lttng_writer_close_element(writer);
297 if (ret) {
298 ret = CMD_ERROR;
299 goto end;
300 }
301
302 /* Success ? */
303 ret = mi_lttng_writer_write_element_bool(writer,
304 mi_lttng_element_success, success);
305 if (ret) {
306 ret = CMD_ERROR;
307 goto end;
308 }
309
310 /* Command element close */
311 ret = mi_lttng_writer_command_close(writer);
312 if (ret) {
313 ret = CMD_ERROR;
314 goto end;
315 }
316 }
26cc6b4e
DG
317
318end:
50534d6f
JRJ
319 /* Mi clean-up */
320 if (writer && mi_lttng_writer_destroy(writer)) {
321 /* Preserve original error code */
322 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
323 }
324
5853fd43
DG
325 if (!opt_session_name && session_name) {
326 free(session_name);
327 }
50534d6f
JRJ
328
329 /* Overwrite ret if an error occurred in disable_channels */
330 ret = command_ret ? command_ret : ret;
331
ca1c3607 332 poptFreeContext(pc);
26cc6b4e
DG
333 return ret;
334}
This page took 0.072721 seconds and 4 git commands to generate.