Commit | Line | Data |
---|---|---|
d36b8583 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. | |
d36b8583 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. | |
d36b8583 DG |
16 | */ |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
d36b8583 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> | |
5a0de755 | 26 | #include <inttypes.h> |
1cb514ce MD |
27 | #include <assert.h> |
28 | #include <ctype.h> | |
d36b8583 | 29 | |
acc09215 JRJ |
30 | #include <common/sessiond-comm/sessiond-comm.h> |
31 | #include <common/utils.h> | |
32 | #include <common/mi-lttng.h> | |
33 | ||
c399183f | 34 | #include "../command.h" |
1cb514ce | 35 | #include "../utils.h" |
d36b8583 | 36 | |
42224349 | 37 | |
d36b8583 | 38 | static char *opt_channels; |
5edd7e09 | 39 | static int opt_kernel; |
5440dc42 | 40 | static char *opt_session_name; |
d36b8583 | 41 | static int opt_userspace; |
d78d6610 | 42 | static struct lttng_channel chan; |
a79d84dd | 43 | static char *opt_output; |
7972aab2 DG |
44 | static int opt_buffer_uid; |
45 | static int opt_buffer_pid; | |
46 | static int opt_buffer_global; | |
d36b8583 | 47 | |
acc09215 JRJ |
48 | static struct mi_writer *writer; |
49 | ||
d36b8583 DG |
50 | enum { |
51 | OPT_HELP = 1, | |
7d29a247 DG |
52 | OPT_DISCARD, |
53 | OPT_OVERWRITE, | |
54 | OPT_SUBBUF_SIZE, | |
55 | OPT_NUM_SUBBUF, | |
56 | OPT_SWITCH_TIMER, | |
57 | OPT_READ_TIMER, | |
d36b8583 | 58 | OPT_USERSPACE, |
679b4943 | 59 | OPT_LIST_OPTIONS, |
1624d5b7 JD |
60 | OPT_TRACEFILE_SIZE, |
61 | OPT_TRACEFILE_COUNT, | |
d36b8583 DG |
62 | }; |
63 | ||
cd80958d DG |
64 | static struct lttng_handle *handle; |
65 | ||
a79d84dd DG |
66 | const char *output_mmap = "mmap"; |
67 | const char *output_splice = "splice"; | |
68 | ||
d36b8583 DG |
69 | static struct poptOption long_options[] = { |
70 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
71 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
5440dc42 | 72 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
d36b8583 | 73 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
d78d6610 | 74 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
7d29a247 DG |
75 | {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0}, |
76 | {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0}, | |
70d0b120 | 77 | {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0}, |
1b579521 DG |
78 | {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0}, |
79 | {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0}, | |
80 | {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0}, | |
679b4943 | 81 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
a79d84dd | 82 | {"output", 0, POPT_ARG_STRING, &opt_output, 0, 0, 0}, |
7972aab2 DG |
83 | {"buffers-uid", 0, POPT_ARG_VAL, &opt_buffer_uid, 1, 0, 0}, |
84 | {"buffers-pid", 0, POPT_ARG_VAL, &opt_buffer_pid, 1, 0, 0}, | |
85 | {"buffers-global", 0, POPT_ARG_VAL, &opt_buffer_global, 1, 0, 0}, | |
1624d5b7 JD |
86 | {"tracefile-size", 'C', POPT_ARG_INT, 0, OPT_TRACEFILE_SIZE, 0, 0}, |
87 | {"tracefile-count", 'W', POPT_ARG_INT, 0, OPT_TRACEFILE_COUNT, 0, 0}, | |
d36b8583 DG |
88 | {0, 0, 0, 0, 0, 0, 0} |
89 | }; | |
90 | ||
5edd7e09 DG |
91 | /* |
92 | * Set default attributes depending on those already defined from the command | |
93 | * line. | |
94 | */ | |
95 | static void set_default_attr(struct lttng_domain *dom) | |
96 | { | |
97 | struct lttng_channel_attr default_attr; | |
98 | ||
99 | /* Set attributes */ | |
100 | lttng_channel_set_default_attr(dom, &default_attr); | |
101 | ||
102 | if (chan.attr.overwrite == -1) { | |
103 | chan.attr.overwrite = default_attr.overwrite; | |
104 | } | |
105 | if (chan.attr.subbuf_size == -1) { | |
106 | chan.attr.subbuf_size = default_attr.subbuf_size; | |
107 | } | |
108 | if (chan.attr.num_subbuf == -1) { | |
109 | chan.attr.num_subbuf = default_attr.num_subbuf; | |
110 | } | |
111 | if (chan.attr.switch_timer_interval == -1) { | |
112 | chan.attr.switch_timer_interval = default_attr.switch_timer_interval; | |
113 | } | |
114 | if (chan.attr.read_timer_interval == -1) { | |
115 | chan.attr.read_timer_interval = default_attr.read_timer_interval; | |
116 | } | |
109be0da | 117 | if ((int) chan.attr.output == -1) { |
5edd7e09 DG |
118 | chan.attr.output = default_attr.output; |
119 | } | |
1624d5b7 JD |
120 | if (chan.attr.tracefile_count == -1) { |
121 | chan.attr.tracefile_count = default_attr.tracefile_count; | |
122 | } | |
123 | if (chan.attr.tracefile_size == -1) { | |
124 | chan.attr.tracefile_size = default_attr.tracefile_size; | |
125 | } | |
5edd7e09 DG |
126 | } |
127 | ||
d36b8583 | 128 | /* |
0fdd1e2c | 129 | * Adding channel using the lttng API. |
d36b8583 | 130 | */ |
cd80958d | 131 | static int enable_channel(char *session_name) |
d36b8583 | 132 | { |
acc09215 | 133 | int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0; |
d36b8583 | 134 | char *channel_name; |
7d29a247 | 135 | struct lttng_domain dom; |
d36b8583 | 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; | |
7972aab2 | 142 | dom.buf_type = LTTNG_BUFFER_GLOBAL; |
88c5f0d8 DG |
143 | if (opt_buffer_uid || opt_buffer_pid) { |
144 | ERR("Buffer type not supported for domain -k"); | |
145 | ret = CMD_ERROR; | |
146 | goto error; | |
147 | } | |
d78d6610 | 148 | } else if (opt_userspace) { |
f6a9efaa | 149 | dom.type = LTTNG_DOMAIN_UST; |
8692d4e5 DG |
150 | if (opt_buffer_pid) { |
151 | dom.buf_type = LTTNG_BUFFER_PER_PID; | |
7972aab2 | 152 | } else { |
88c5f0d8 DG |
153 | if (opt_buffer_global) { |
154 | ERR("Buffer type not supported for domain -u"); | |
155 | ret = CMD_ERROR; | |
156 | goto error; | |
157 | } | |
8692d4e5 | 158 | dom.buf_type = LTTNG_BUFFER_PER_UID; |
7972aab2 | 159 | } |
0fdd1e2c | 160 | } else { |
3ecec76a PP |
161 | /* Checked by the caller. */ |
162 | assert(0); | |
7d29a247 DG |
163 | } |
164 | ||
5edd7e09 DG |
165 | set_default_attr(&dom); |
166 | ||
d16dee89 DG |
167 | if (chan.attr.tracefile_size == 0 && chan.attr.tracefile_count) { |
168 | ERR("Missing option --tracefile-size. " | |
169 | "A file count without a size won't do anything."); | |
170 | ret = CMD_ERROR; | |
171 | goto error; | |
172 | } | |
173 | ||
1624d5b7 JD |
174 | if ((chan.attr.tracefile_size > 0) && |
175 | (chan.attr.tracefile_size < chan.attr.subbuf_size)) { | |
de65565a | 176 | WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")", |
1624d5b7 | 177 | chan.attr.tracefile_size, chan.attr.subbuf_size); |
de65565a | 178 | chan.attr.tracefile_size = chan.attr.subbuf_size; |
1624d5b7 JD |
179 | } |
180 | ||
a79d84dd DG |
181 | /* Setting channel output */ |
182 | if (opt_output) { | |
183 | if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) { | |
184 | chan.attr.output = LTTNG_EVENT_MMAP; | |
185 | } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) { | |
186 | chan.attr.output = LTTNG_EVENT_SPLICE; | |
187 | } else { | |
188 | ERR("Unknown output type %s. Possible values are: %s, %s\n", | |
189 | opt_output, output_mmap, output_splice); | |
a79d84dd DG |
190 | ret = CMD_ERROR; |
191 | goto error; | |
192 | } | |
193 | } | |
194 | ||
cd80958d DG |
195 | handle = lttng_create_handle(session_name, &dom); |
196 | if (handle == NULL) { | |
197 | ret = -1; | |
198 | goto error; | |
199 | } | |
200 | ||
acc09215 JRJ |
201 | /* Mi open channels element */ |
202 | if (lttng_opt_mi) { | |
203 | assert(writer); | |
204 | ret = mi_lttng_channels_open(writer); | |
205 | if (ret) { | |
206 | ret = CMD_ERROR; | |
207 | goto error; | |
208 | } | |
209 | } | |
210 | ||
0fdd1e2c | 211 | /* Strip channel list (format: chan1,chan2,...) */ |
d36b8583 DG |
212 | channel_name = strtok(opt_channels, ","); |
213 | while (channel_name != NULL) { | |
1f345e94 PP |
214 | /* Validate channel name's length */ |
215 | if (strlen(channel_name) >= NAME_MAX) { | |
216 | ERR("Channel name is too long (max. %zu characters)", | |
217 | sizeof(chan.name) - 1); | |
218 | error = 1; | |
219 | goto skip_enable; | |
220 | } | |
221 | ||
222 | /* Copy channel name */ | |
223 | strcpy(chan.name, channel_name); | |
0fdd1e2c DG |
224 | |
225 | DBG("Enabling channel %s", channel_name); | |
226 | ||
227 | ret = lttng_enable_channel(handle, &chan); | |
228 | if (ret < 0) { | |
acc09215 | 229 | success = 0; |
42224349 | 230 | switch (-ret) { |
f73fabfd DG |
231 | case LTTNG_ERR_KERN_CHAN_EXIST: |
232 | case LTTNG_ERR_UST_CHAN_EXIST: | |
b9dfb167 | 233 | case LTTNG_ERR_CHAN_EXIST: |
88c5f0d8 | 234 | WARN("Channel %s: %s (session %s)", channel_name, |
42224349 | 235 | lttng_strerror(ret), session_name); |
acc09215 JRJ |
236 | warn = 1; |
237 | break; | |
1f345e94 PP |
238 | case LTTNG_ERR_INVALID_CHANNEL_NAME: |
239 | ERR("Invalid channel name: \"%s\". " | |
240 | "Channel names may not start with '.', and " | |
241 | "may not contain '/'.", channel_name); | |
242 | error = 1; | |
243 | break; | |
42224349 DG |
244 | default: |
245 | ERR("Channel %s: %s (session %s)", channel_name, | |
246 | lttng_strerror(ret), session_name); | |
acc09215 | 247 | error = 1; |
42224349 DG |
248 | break; |
249 | } | |
d36b8583 | 250 | } else { |
7885e399 | 251 | MSG("%s channel %s enabled for session %s", |
b9dfb167 | 252 | get_domain_str(dom.type), channel_name, session_name); |
acc09215 JRJ |
253 | success = 1; |
254 | } | |
255 | ||
1f345e94 | 256 | skip_enable: |
acc09215 JRJ |
257 | if (lttng_opt_mi) { |
258 | /* Mi print the channel element and leave it open */ | |
259 | ret = mi_lttng_channel(writer, &chan, 1); | |
260 | if (ret) { | |
261 | ret = CMD_ERROR; | |
262 | goto error; | |
263 | } | |
264 | ||
265 | /* Individual Success ? */ | |
266 | ret = mi_lttng_writer_write_element_bool(writer, | |
267 | mi_lttng_element_command_success, success); | |
268 | if (ret) { | |
269 | ret = CMD_ERROR; | |
270 | goto error; | |
271 | } | |
272 | ||
273 | /* Close channel element */ | |
274 | ret = mi_lttng_writer_close_element(writer); | |
275 | if (ret) { | |
276 | ret = CMD_ERROR; | |
277 | goto error; | |
278 | } | |
d36b8583 DG |
279 | } |
280 | ||
acc09215 | 281 | /* Next channel */ |
d36b8583 DG |
282 | channel_name = strtok(NULL, ","); |
283 | } | |
284 | ||
acc09215 JRJ |
285 | if (lttng_opt_mi) { |
286 | /* Close channels element */ | |
287 | ret = mi_lttng_writer_close_element(writer); | |
288 | if (ret) { | |
289 | ret = CMD_ERROR; | |
290 | goto error; | |
291 | } | |
292 | } | |
293 | ||
ae856491 DG |
294 | ret = CMD_SUCCESS; |
295 | ||
d36b8583 | 296 | error: |
acc09215 JRJ |
297 | /* If more important error happen bypass the warning */ |
298 | if (!ret && warn) { | |
ae856491 DG |
299 | ret = CMD_WARNING; |
300 | } | |
acc09215 JRJ |
301 | /* If more important error happen bypass the warning */ |
302 | if (!ret && error) { | |
303 | ret = CMD_ERROR; | |
304 | } | |
ae856491 | 305 | |
cd80958d DG |
306 | lttng_destroy_handle(handle); |
307 | ||
d36b8583 DG |
308 | return ret; |
309 | } | |
310 | ||
311 | /* | |
0fdd1e2c | 312 | * Default value for channel configuration. |
7d29a247 DG |
313 | */ |
314 | static void init_channel_config(void) | |
315 | { | |
5edd7e09 DG |
316 | /* |
317 | * Put -1 everywhere so we can identify those set by the command line and | |
318 | * those needed to be set by the default values. | |
319 | */ | |
320 | memset(&chan.attr, -1, sizeof(chan.attr)); | |
394ff38e | 321 | chan.attr.extended.ptr = NULL; |
7d29a247 DG |
322 | } |
323 | ||
324 | /* | |
0fdd1e2c | 325 | * Add channel to trace session |
d36b8583 DG |
326 | */ |
327 | int cmd_enable_channels(int argc, const char **argv) | |
328 | { | |
acc09215 | 329 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
d36b8583 | 330 | static poptContext pc; |
cd80958d | 331 | char *session_name = NULL; |
70d0b120 | 332 | char *opt_arg = NULL; |
d36b8583 | 333 | |
7d29a247 DG |
334 | init_channel_config(); |
335 | ||
d36b8583 DG |
336 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
337 | poptReadDefaultConfig(pc, 0); | |
338 | ||
339 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
340 | switch (opt) { | |
341 | case OPT_HELP: | |
4ba92f18 | 342 | SHOW_HELP(); |
d36b8583 | 343 | goto end; |
7d29a247 DG |
344 | case OPT_DISCARD: |
345 | chan.attr.overwrite = 0; | |
346 | DBG("Channel set to discard"); | |
347 | break; | |
348 | case OPT_OVERWRITE: | |
349 | chan.attr.overwrite = 1; | |
350 | DBG("Channel set to overwrite"); | |
351 | break; | |
352 | case OPT_SUBBUF_SIZE: | |
1cb514ce MD |
353 | { |
354 | uint64_t rounded_size; | |
355 | int order; | |
356 | ||
70d0b120 SM |
357 | /* Parse the size */ |
358 | opt_arg = poptGetOptArg(pc); | |
1cb514ce MD |
359 | if (utils_parse_size_suffix(opt_arg, &chan.attr.subbuf_size) < 0 || !chan.attr.subbuf_size) { |
360 | ERR("Wrong value in --subbuf-size parameter: %s", opt_arg); | |
70d0b120 SM |
361 | ret = CMD_ERROR; |
362 | goto end; | |
363 | } | |
364 | ||
1cb514ce MD |
365 | order = get_count_order_u64(chan.attr.subbuf_size); |
366 | assert(order >= 0); | |
367 | rounded_size = 1ULL << order; | |
06c0da96 DG |
368 | if (rounded_size < chan.attr.subbuf_size) { |
369 | ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!", | |
370 | chan.attr.subbuf_size); | |
371 | ret = CMD_ERROR; | |
372 | goto end; | |
373 | } | |
374 | ||
1cb514ce MD |
375 | if (rounded_size != chan.attr.subbuf_size) { |
376 | WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")", | |
377 | chan.attr.subbuf_size, rounded_size); | |
378 | chan.attr.subbuf_size = rounded_size; | |
70d0b120 SM |
379 | } |
380 | ||
1cb514ce MD |
381 | /* Should now be power of 2 */ |
382 | assert(!((chan.attr.subbuf_size - 1) & chan.attr.subbuf_size)); | |
383 | ||
5a0de755 | 384 | DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size); |
7d29a247 | 385 | break; |
1cb514ce | 386 | } |
7d29a247 | 387 | case OPT_NUM_SUBBUF: |
1cb514ce MD |
388 | { |
389 | uint64_t rounded_size; | |
390 | int order; | |
391 | ||
16068db5 | 392 | errno = 0; |
1cb514ce MD |
393 | opt_arg = poptGetOptArg(pc); |
394 | chan.attr.num_subbuf = strtoull(opt_arg, NULL, 0); | |
395 | if (errno != 0 || !chan.attr.num_subbuf || !isdigit(opt_arg[0])) { | |
396 | ERR("Wrong value in --num-subbuf parameter: %s", opt_arg); | |
16068db5 MD |
397 | ret = CMD_ERROR; |
398 | goto end; | |
399 | } | |
400 | ||
1cb514ce MD |
401 | order = get_count_order_u64(chan.attr.num_subbuf); |
402 | assert(order >= 0); | |
403 | rounded_size = 1ULL << order; | |
e123b80c | 404 | if (rounded_size < chan.attr.num_subbuf) { |
06c0da96 DG |
405 | ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!", |
406 | chan.attr.num_subbuf); | |
407 | ret = CMD_ERROR; | |
408 | goto end; | |
409 | } | |
410 | ||
1cb514ce MD |
411 | if (rounded_size != chan.attr.num_subbuf) { |
412 | WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")", | |
413 | chan.attr.num_subbuf, rounded_size); | |
414 | chan.attr.num_subbuf = rounded_size; | |
415 | } | |
416 | ||
417 | /* Should now be power of 2 */ | |
418 | assert(!((chan.attr.num_subbuf - 1) & chan.attr.num_subbuf)); | |
419 | ||
5a0de755 | 420 | DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf); |
7d29a247 | 421 | break; |
1cb514ce | 422 | } |
7d29a247 | 423 | case OPT_SWITCH_TIMER: |
16068db5 MD |
424 | { |
425 | unsigned long v; | |
426 | ||
427 | errno = 0; | |
1cb514ce MD |
428 | opt_arg = poptGetOptArg(pc); |
429 | v = strtoul(opt_arg, NULL, 0); | |
430 | if (errno != 0 || !isdigit(opt_arg[0])) { | |
431 | ERR("Wrong value in --switch-timer parameter: %s", opt_arg); | |
16068db5 MD |
432 | ret = CMD_ERROR; |
433 | goto end; | |
434 | } | |
435 | if (v != (uint32_t) v) { | |
436 | ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg); | |
437 | ret = CMD_ERROR; | |
438 | goto end; | |
439 | } | |
440 | chan.attr.switch_timer_interval = (uint32_t) v; | |
7d29a247 DG |
441 | DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval); |
442 | break; | |
16068db5 | 443 | } |
7d29a247 | 444 | case OPT_READ_TIMER: |
16068db5 MD |
445 | { |
446 | unsigned long v; | |
447 | ||
448 | errno = 0; | |
1cb514ce MD |
449 | opt_arg = poptGetOptArg(pc); |
450 | v = strtoul(opt_arg, NULL, 0); | |
451 | if (errno != 0 || !isdigit(opt_arg[0])) { | |
452 | ERR("Wrong value in --read-timer parameter: %s", opt_arg); | |
16068db5 MD |
453 | ret = CMD_ERROR; |
454 | goto end; | |
455 | } | |
456 | if (v != (uint32_t) v) { | |
457 | ERR("32-bit overflow in --read-timer parameter: %s", opt_arg); | |
458 | ret = CMD_ERROR; | |
459 | goto end; | |
460 | } | |
461 | chan.attr.read_timer_interval = (uint32_t) v; | |
7d29a247 | 462 | DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval); |
d36b8583 | 463 | break; |
16068db5 | 464 | } |
eeac7d46 MD |
465 | case OPT_USERSPACE: |
466 | opt_userspace = 1; | |
eeac7d46 | 467 | break; |
1624d5b7 | 468 | case OPT_TRACEFILE_SIZE: |
1cb514ce | 469 | opt_arg = poptGetOptArg(pc); |
16068db5 | 470 | if (utils_parse_size_suffix(opt_arg, &chan.attr.tracefile_size) < 0) { |
1cb514ce | 471 | ERR("Wrong value in --tracefile-size parameter: %s", opt_arg); |
16068db5 MD |
472 | ret = CMD_ERROR; |
473 | goto end; | |
474 | } | |
1624d5b7 JD |
475 | DBG("Maximum tracefile size set to %" PRIu64, |
476 | chan.attr.tracefile_size); | |
477 | break; | |
478 | case OPT_TRACEFILE_COUNT: | |
16068db5 MD |
479 | { |
480 | unsigned long v; | |
481 | ||
482 | errno = 0; | |
1cb514ce MD |
483 | opt_arg = poptGetOptArg(pc); |
484 | v = strtoul(opt_arg, NULL, 0); | |
485 | if (errno != 0 || !isdigit(opt_arg[0])) { | |
486 | ERR("Wrong value in --tracefile-count parameter: %s", opt_arg); | |
16068db5 MD |
487 | ret = CMD_ERROR; |
488 | goto end; | |
489 | } | |
490 | if (v != (uint32_t) v) { | |
491 | ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg); | |
492 | ret = CMD_ERROR; | |
493 | goto end; | |
494 | } | |
495 | chan.attr.tracefile_count = (uint32_t) v; | |
1624d5b7 JD |
496 | DBG("Maximum tracefile count set to %" PRIu64, |
497 | chan.attr.tracefile_count); | |
498 | break; | |
16068db5 | 499 | } |
679b4943 SM |
500 | case OPT_LIST_OPTIONS: |
501 | list_cmd_options(stdout, long_options); | |
679b4943 | 502 | goto end; |
d36b8583 | 503 | default: |
d36b8583 DG |
504 | ret = CMD_UNDEFINED; |
505 | goto end; | |
506 | } | |
507 | } | |
508 | ||
3ecec76a PP |
509 | ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace); |
510 | if (ret) { | |
511 | ret = CMD_ERROR; | |
512 | goto end; | |
513 | } | |
514 | ||
acc09215 JRJ |
515 | /* Mi check */ |
516 | if (lttng_opt_mi) { | |
517 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
518 | if (!writer) { | |
519 | ret = -LTTNG_ERR_NOMEM; | |
520 | goto end; | |
521 | } | |
522 | ||
523 | /* Open command element */ | |
524 | ret = mi_lttng_writer_command_open(writer, | |
525 | mi_lttng_element_command_enable_channels); | |
526 | if (ret) { | |
527 | ret = CMD_ERROR; | |
528 | goto end; | |
529 | } | |
530 | ||
531 | /* Open output element */ | |
532 | ret = mi_lttng_writer_open_element(writer, | |
533 | mi_lttng_element_command_output); | |
534 | if (ret) { | |
535 | ret = CMD_ERROR; | |
536 | goto end; | |
537 | } | |
538 | } | |
539 | ||
d36b8583 DG |
540 | opt_channels = (char*) poptGetArg(pc); |
541 | if (opt_channels == NULL) { | |
7d29a247 | 542 | ERR("Missing channel name.\n"); |
ca1c3607 | 543 | ret = CMD_ERROR; |
acc09215 JRJ |
544 | success = 0; |
545 | goto mi_closing; | |
d36b8583 DG |
546 | } |
547 | ||
cd80958d DG |
548 | if (!opt_session_name) { |
549 | session_name = get_session_name(); | |
550 | if (session_name == NULL) { | |
acc09215 JRJ |
551 | command_ret = CMD_ERROR; |
552 | success = 0; | |
553 | goto mi_closing; | |
cd80958d DG |
554 | } |
555 | } else { | |
556 | session_name = opt_session_name; | |
557 | } | |
558 | ||
acc09215 JRJ |
559 | command_ret = enable_channel(session_name); |
560 | if (command_ret) { | |
561 | success = 0; | |
562 | } | |
563 | ||
564 | mi_closing: | |
565 | /* Mi closing */ | |
566 | if (lttng_opt_mi) { | |
567 | /* Close output element */ | |
568 | ret = mi_lttng_writer_close_element(writer); | |
569 | if (ret) { | |
570 | goto end; | |
571 | } | |
572 | ||
573 | /* Success ? */ | |
574 | ret = mi_lttng_writer_write_element_bool(writer, | |
575 | mi_lttng_element_command_success, success); | |
576 | if (ret) { | |
577 | goto end; | |
578 | } | |
579 | ||
580 | /* Command element close */ | |
581 | ret = mi_lttng_writer_command_close(writer); | |
582 | if (ret) { | |
583 | goto end; | |
584 | } | |
585 | } | |
d36b8583 DG |
586 | |
587 | end: | |
acc09215 JRJ |
588 | /* Mi clean-up */ |
589 | if (writer && mi_lttng_writer_destroy(writer)) { | |
590 | /* Preserve original error code */ | |
591 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; | |
592 | } | |
593 | ||
5853fd43 DG |
594 | if (!opt_session_name && session_name) { |
595 | free(session_name); | |
596 | } | |
acc09215 JRJ |
597 | |
598 | /* Overwrite ret if an error occurred when enable_channel */ | |
599 | ret = command_ret ? command_ret : ret; | |
ca1c3607 | 600 | poptFreeContext(pc); |
d36b8583 DG |
601 | return ret; |
602 | } |