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