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