Mi enable-channel command: support and validation
[lttng-tools.git] / src / bin / lttng / commands / enable_channels.c
CommitLineData
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
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>
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 38static char *opt_channels;
5edd7e09 39static int opt_kernel;
5440dc42 40static char *opt_session_name;
d36b8583 41static int opt_userspace;
d78d6610 42static struct lttng_channel chan;
a79d84dd 43static char *opt_output;
7972aab2
DG
44static int opt_buffer_uid;
45static int opt_buffer_pid;
46static int opt_buffer_global;
d36b8583 47
acc09215
JRJ
48static struct mi_writer *writer;
49
d36b8583
DG
50enum {
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
64static struct lttng_handle *handle;
65
a79d84dd
DG
66const char *output_mmap = "mmap";
67const char *output_splice = "splice";
68
d36b8583
DG
69static 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
91/*
92 * usage
93 */
94static void usage(FILE *ofp)
95{
05be3802 96 fprintf(ofp, "usage: lttng enable-channel NAME[,NAME2,...] (-u | -k) [OPTIONS]\n");
d36b8583 97 fprintf(ofp, "\n");
32a6298d 98 fprintf(ofp, "Options:\n");
7d29a247 99 fprintf(ofp, " -h, --help Show this help\n");
679b4943 100 fprintf(ofp, " --list-options Simple listing of options\n");
32a6298d 101 fprintf(ofp, " -s, --session NAME Apply to session name\n");
27221701 102 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
27221701 103 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
7d29a247
DG
104 fprintf(ofp, "\n");
105 fprintf(ofp, "Channel options:\n");
cabd9892
MD
106 fprintf(ofp, " --discard Discard event when buffers are full%s\n",
107 DEFAULT_CHANNEL_OVERWRITE ? "" : " (default)");
108 fprintf(ofp, " --overwrite Flight recorder mode%s\n",
109 DEFAULT_CHANNEL_OVERWRITE ? " (default)" : "");
70d0b120 110 fprintf(ofp, " --subbuf-size SIZE Subbuffer size in bytes {+k,+M,+G}\n");
0a9c6494
DG
111 fprintf(ofp, " (default UST uid: %zu, UST pid: %zu, kernel: %zu, metadata: %zu)\n",
112 default_get_ust_uid_channel_subbuf_size(),
113 default_get_ust_pid_channel_subbuf_size(),
114 default_get_kernel_channel_subbuf_size(),
115 default_get_metadata_subbuf_size());
1cb514ce 116 fprintf(ofp, " Rounded up to the next power of 2.\n");
cc62c0c0 117 fprintf(ofp, " --num-subbuf NUM Number of subbufers\n");
0a9c6494
DG
118 fprintf(ofp, " (default UST uid: %u, UST pid: %u, kernel: %u, metadata: %u)\n",
119 DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM, DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM,
120 DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM, DEFAULT_METADATA_SUBBUF_NUM);
1cb514ce 121 fprintf(ofp, " Rounded up to the next power of 2.\n");
0a9c6494
DG
122 fprintf(ofp, " --switch-timer USEC Switch timer interval in usec\n");
123 fprintf(ofp, " (default UST uid: %u, UST pid: %u, kernel: %u, metadata: %u)\n",
124 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER, DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER,
125 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER, DEFAULT_METADATA_SWITCH_TIMER);
126 fprintf(ofp, " --read-timer USEC Read timer interval in usec.\n");
127 fprintf(ofp, " (default UST uid: %u, UST pid: %u, kernel: %u, metadata: %u)\n",
128 DEFAULT_UST_UID_CHANNEL_READ_TIMER, DEFAULT_UST_UID_CHANNEL_READ_TIMER,
129 DEFAULT_KERNEL_CHANNEL_READ_TIMER, DEFAULT_METADATA_READ_TIMER);
a79d84dd
DG
130 fprintf(ofp, " --output TYPE Channel output type (Values: %s, %s)\n",
131 output_mmap, output_splice);
0a9c6494
DG
132 fprintf(ofp, " (default UST uid: %s, UST pid: %s, kernel: %s, metadata: %s)\n",
133 DEFAULT_UST_UID_CHANNEL_OUTPUT == LTTNG_EVENT_MMAP ? output_mmap : output_splice,
134 DEFAULT_UST_PID_CHANNEL_OUTPUT == LTTNG_EVENT_MMAP ? output_mmap : output_splice,
135 DEFAULT_KERNEL_CHANNEL_OUTPUT == LTTNG_EVENT_MMAP ? output_mmap : output_splice,
136 DEFAULT_METADATA_OUTPUT == LTTNG_EVENT_MMAP ? output_mmap : output_splice);
05c35276
DG
137 fprintf(ofp, " --buffers-uid Use per UID buffer (-u only)\n");
138 fprintf(ofp, " --buffers-pid Use per PID buffer (-u only)\n");
7972aab2 139 fprintf(ofp, " --buffers-global Use shared buffer for the whole system (-k only)\n");
1624d5b7 140 fprintf(ofp, " -C, --tracefile-size SIZE\n");
0a9c6494
DG
141 fprintf(ofp, " Maximum size of each tracefile within a stream (in bytes). 0 means unlimited.\n");
142 fprintf(ofp, " (default: %u)\n", DEFAULT_CHANNEL_TRACEFILE_SIZE);
c0684a0d
MD
143 fprintf(ofp, " Note: traces generated with this option may inaccurately report\n");
144 fprintf(ofp, " discarded events as per CTF 1.8.\n");
1624d5b7
JD
145 fprintf(ofp, " -W, --tracefile-count COUNT\n");
146 fprintf(ofp, " Used in conjunction with -C option, this will limit the number\n");
0a9c6494
DG
147 fprintf(ofp, " of files created to the specified count. 0 means unlimited.\n");
148 fprintf(ofp, " (default: %u)\n", DEFAULT_CHANNEL_TRACEFILE_COUNT);
d36b8583
DG
149 fprintf(ofp, "\n");
150}
151
5edd7e09
DG
152/*
153 * Set default attributes depending on those already defined from the command
154 * line.
155 */
156static void set_default_attr(struct lttng_domain *dom)
157{
158 struct lttng_channel_attr default_attr;
159
160 /* Set attributes */
161 lttng_channel_set_default_attr(dom, &default_attr);
162
163 if (chan.attr.overwrite == -1) {
164 chan.attr.overwrite = default_attr.overwrite;
165 }
166 if (chan.attr.subbuf_size == -1) {
167 chan.attr.subbuf_size = default_attr.subbuf_size;
168 }
169 if (chan.attr.num_subbuf == -1) {
170 chan.attr.num_subbuf = default_attr.num_subbuf;
171 }
172 if (chan.attr.switch_timer_interval == -1) {
173 chan.attr.switch_timer_interval = default_attr.switch_timer_interval;
174 }
175 if (chan.attr.read_timer_interval == -1) {
176 chan.attr.read_timer_interval = default_attr.read_timer_interval;
177 }
109be0da 178 if ((int) chan.attr.output == -1) {
5edd7e09
DG
179 chan.attr.output = default_attr.output;
180 }
1624d5b7
JD
181 if (chan.attr.tracefile_count == -1) {
182 chan.attr.tracefile_count = default_attr.tracefile_count;
183 }
184 if (chan.attr.tracefile_size == -1) {
185 chan.attr.tracefile_size = default_attr.tracefile_size;
186 }
5edd7e09
DG
187}
188
d36b8583 189/*
0fdd1e2c 190 * Adding channel using the lttng API.
d36b8583 191 */
cd80958d 192static int enable_channel(char *session_name)
d36b8583 193{
acc09215 194 int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0;
d36b8583 195 char *channel_name;
7d29a247 196 struct lttng_domain dom;
d36b8583 197
441c16a7
MD
198 memset(&dom, 0, sizeof(dom));
199
d78d6610 200 /* Create lttng domain */
7d29a247
DG
201 if (opt_kernel) {
202 dom.type = LTTNG_DOMAIN_KERNEL;
7972aab2 203 dom.buf_type = LTTNG_BUFFER_GLOBAL;
88c5f0d8
DG
204 if (opt_buffer_uid || opt_buffer_pid) {
205 ERR("Buffer type not supported for domain -k");
206 ret = CMD_ERROR;
207 goto error;
208 }
d78d6610 209 } else if (opt_userspace) {
f6a9efaa 210 dom.type = LTTNG_DOMAIN_UST;
8692d4e5
DG
211 if (opt_buffer_pid) {
212 dom.buf_type = LTTNG_BUFFER_PER_PID;
7972aab2 213 } else {
88c5f0d8
DG
214 if (opt_buffer_global) {
215 ERR("Buffer type not supported for domain -u");
216 ret = CMD_ERROR;
217 goto error;
218 }
8692d4e5 219 dom.buf_type = LTTNG_BUFFER_PER_UID;
7972aab2 220 }
0fdd1e2c 221 } else {
b9dfb167 222 print_missing_domain();
d16c1a4c 223 ret = CMD_ERROR;
0fdd1e2c 224 goto error;
7d29a247
DG
225 }
226
5edd7e09
DG
227 set_default_attr(&dom);
228
d16dee89
DG
229 if (chan.attr.tracefile_size == 0 && chan.attr.tracefile_count) {
230 ERR("Missing option --tracefile-size. "
231 "A file count without a size won't do anything.");
232 ret = CMD_ERROR;
233 goto error;
234 }
235
1624d5b7
JD
236 if ((chan.attr.tracefile_size > 0) &&
237 (chan.attr.tracefile_size < chan.attr.subbuf_size)) {
de65565a 238 WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")",
1624d5b7 239 chan.attr.tracefile_size, chan.attr.subbuf_size);
de65565a 240 chan.attr.tracefile_size = chan.attr.subbuf_size;
1624d5b7
JD
241 }
242
a79d84dd
DG
243 /* Setting channel output */
244 if (opt_output) {
245 if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) {
246 chan.attr.output = LTTNG_EVENT_MMAP;
247 } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) {
248 chan.attr.output = LTTNG_EVENT_SPLICE;
249 } else {
250 ERR("Unknown output type %s. Possible values are: %s, %s\n",
251 opt_output, output_mmap, output_splice);
252 usage(stderr);
253 ret = CMD_ERROR;
254 goto error;
255 }
256 }
257
cd80958d
DG
258 handle = lttng_create_handle(session_name, &dom);
259 if (handle == NULL) {
260 ret = -1;
261 goto error;
262 }
263
acc09215
JRJ
264 /* Mi open channels element */
265 if (lttng_opt_mi) {
266 assert(writer);
267 ret = mi_lttng_channels_open(writer);
268 if (ret) {
269 ret = CMD_ERROR;
270 goto error;
271 }
272 }
273
0fdd1e2c 274 /* Strip channel list (format: chan1,chan2,...) */
d36b8583
DG
275 channel_name = strtok(opt_channels, ",");
276 while (channel_name != NULL) {
0fdd1e2c
DG
277 /* Copy channel name and normalize it */
278 strncpy(chan.name, channel_name, NAME_MAX);
279 chan.name[NAME_MAX - 1] = '\0';
280
281 DBG("Enabling channel %s", channel_name);
282
283 ret = lttng_enable_channel(handle, &chan);
284 if (ret < 0) {
acc09215 285 success = 0;
42224349 286 switch (-ret) {
f73fabfd
DG
287 case LTTNG_ERR_KERN_CHAN_EXIST:
288 case LTTNG_ERR_UST_CHAN_EXIST:
b9dfb167 289 case LTTNG_ERR_CHAN_EXIST:
88c5f0d8 290 WARN("Channel %s: %s (session %s)", channel_name,
42224349 291 lttng_strerror(ret), session_name);
acc09215
JRJ
292 warn = 1;
293 break;
42224349
DG
294 default:
295 ERR("Channel %s: %s (session %s)", channel_name,
296 lttng_strerror(ret), session_name);
acc09215 297 error = 1;
42224349
DG
298 break;
299 }
d36b8583 300 } else {
7885e399 301 MSG("%s channel %s enabled for session %s",
b9dfb167 302 get_domain_str(dom.type), channel_name, session_name);
acc09215
JRJ
303 success = 1;
304 }
305
306 if (lttng_opt_mi) {
307 /* Mi print the channel element and leave it open */
308 ret = mi_lttng_channel(writer, &chan, 1);
309 if (ret) {
310 ret = CMD_ERROR;
311 goto error;
312 }
313
314 /* Individual Success ? */
315 ret = mi_lttng_writer_write_element_bool(writer,
316 mi_lttng_element_command_success, success);
317 if (ret) {
318 ret = CMD_ERROR;
319 goto error;
320 }
321
322 /* Close channel element */
323 ret = mi_lttng_writer_close_element(writer);
324 if (ret) {
325 ret = CMD_ERROR;
326 goto error;
327 }
d36b8583
DG
328 }
329
acc09215 330 /* Next channel */
d36b8583
DG
331 channel_name = strtok(NULL, ",");
332 }
333
acc09215
JRJ
334 if (lttng_opt_mi) {
335 /* Close channels element */
336 ret = mi_lttng_writer_close_element(writer);
337 if (ret) {
338 ret = CMD_ERROR;
339 goto error;
340 }
341 }
342
ae856491
DG
343 ret = CMD_SUCCESS;
344
d36b8583 345error:
acc09215
JRJ
346 /* If more important error happen bypass the warning */
347 if (!ret && warn) {
ae856491
DG
348 ret = CMD_WARNING;
349 }
acc09215
JRJ
350 /* If more important error happen bypass the warning */
351 if (!ret && error) {
352 ret = CMD_ERROR;
353 }
ae856491 354
cd80958d
DG
355 lttng_destroy_handle(handle);
356
d36b8583
DG
357 return ret;
358}
359
360/*
0fdd1e2c 361 * Default value for channel configuration.
7d29a247
DG
362 */
363static void init_channel_config(void)
364{
5edd7e09
DG
365 /*
366 * Put -1 everywhere so we can identify those set by the command line and
367 * those needed to be set by the default values.
368 */
369 memset(&chan.attr, -1, sizeof(chan.attr));
7d29a247
DG
370}
371
372/*
0fdd1e2c 373 * Add channel to trace session
d36b8583
DG
374 */
375int cmd_enable_channels(int argc, const char **argv)
376{
acc09215 377 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
d36b8583 378 static poptContext pc;
cd80958d 379 char *session_name = NULL;
70d0b120 380 char *opt_arg = NULL;
d36b8583 381
7d29a247
DG
382 init_channel_config();
383
d36b8583
DG
384 pc = poptGetContext(NULL, argc, argv, long_options, 0);
385 poptReadDefaultConfig(pc, 0);
386
387 while ((opt = poptGetNextOpt(pc)) != -1) {
388 switch (opt) {
389 case OPT_HELP:
ca1c3607 390 usage(stdout);
d36b8583 391 goto end;
7d29a247
DG
392 case OPT_DISCARD:
393 chan.attr.overwrite = 0;
394 DBG("Channel set to discard");
395 break;
396 case OPT_OVERWRITE:
397 chan.attr.overwrite = 1;
398 DBG("Channel set to overwrite");
399 break;
400 case OPT_SUBBUF_SIZE:
1cb514ce
MD
401 {
402 uint64_t rounded_size;
403 int order;
404
70d0b120
SM
405 /* Parse the size */
406 opt_arg = poptGetOptArg(pc);
1cb514ce
MD
407 if (utils_parse_size_suffix(opt_arg, &chan.attr.subbuf_size) < 0 || !chan.attr.subbuf_size) {
408 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg);
70d0b120
SM
409 ret = CMD_ERROR;
410 goto end;
411 }
412
1cb514ce
MD
413 order = get_count_order_u64(chan.attr.subbuf_size);
414 assert(order >= 0);
415 rounded_size = 1ULL << order;
06c0da96
DG
416 if (rounded_size < chan.attr.subbuf_size) {
417 ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!",
418 chan.attr.subbuf_size);
419 ret = CMD_ERROR;
420 goto end;
421 }
422
1cb514ce
MD
423 if (rounded_size != chan.attr.subbuf_size) {
424 WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
425 chan.attr.subbuf_size, rounded_size);
426 chan.attr.subbuf_size = rounded_size;
70d0b120
SM
427 }
428
1cb514ce
MD
429 /* Should now be power of 2 */
430 assert(!((chan.attr.subbuf_size - 1) & chan.attr.subbuf_size));
431
5a0de755 432 DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);
7d29a247 433 break;
1cb514ce 434 }
7d29a247 435 case OPT_NUM_SUBBUF:
1cb514ce
MD
436 {
437 uint64_t rounded_size;
438 int order;
439
16068db5 440 errno = 0;
1cb514ce
MD
441 opt_arg = poptGetOptArg(pc);
442 chan.attr.num_subbuf = strtoull(opt_arg, NULL, 0);
443 if (errno != 0 || !chan.attr.num_subbuf || !isdigit(opt_arg[0])) {
444 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg);
16068db5
MD
445 ret = CMD_ERROR;
446 goto end;
447 }
448
1cb514ce
MD
449 order = get_count_order_u64(chan.attr.num_subbuf);
450 assert(order >= 0);
451 rounded_size = 1ULL << order;
e123b80c 452 if (rounded_size < chan.attr.num_subbuf) {
06c0da96
DG
453 ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!",
454 chan.attr.num_subbuf);
455 ret = CMD_ERROR;
456 goto end;
457 }
458
1cb514ce
MD
459 if (rounded_size != chan.attr.num_subbuf) {
460 WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
461 chan.attr.num_subbuf, rounded_size);
462 chan.attr.num_subbuf = rounded_size;
463 }
464
465 /* Should now be power of 2 */
466 assert(!((chan.attr.num_subbuf - 1) & chan.attr.num_subbuf));
467
5a0de755 468 DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf);
7d29a247 469 break;
1cb514ce 470 }
7d29a247 471 case OPT_SWITCH_TIMER:
16068db5
MD
472 {
473 unsigned long v;
474
475 errno = 0;
1cb514ce
MD
476 opt_arg = poptGetOptArg(pc);
477 v = strtoul(opt_arg, NULL, 0);
478 if (errno != 0 || !isdigit(opt_arg[0])) {
479 ERR("Wrong value in --switch-timer parameter: %s", opt_arg);
16068db5
MD
480 ret = CMD_ERROR;
481 goto end;
482 }
483 if (v != (uint32_t) v) {
484 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg);
485 ret = CMD_ERROR;
486 goto end;
487 }
488 chan.attr.switch_timer_interval = (uint32_t) v;
7d29a247
DG
489 DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval);
490 break;
16068db5 491 }
7d29a247 492 case OPT_READ_TIMER:
16068db5
MD
493 {
494 unsigned long v;
495
496 errno = 0;
1cb514ce
MD
497 opt_arg = poptGetOptArg(pc);
498 v = strtoul(opt_arg, NULL, 0);
499 if (errno != 0 || !isdigit(opt_arg[0])) {
500 ERR("Wrong value in --read-timer parameter: %s", opt_arg);
16068db5
MD
501 ret = CMD_ERROR;
502 goto end;
503 }
504 if (v != (uint32_t) v) {
505 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg);
506 ret = CMD_ERROR;
507 goto end;
508 }
509 chan.attr.read_timer_interval = (uint32_t) v;
7d29a247 510 DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval);
d36b8583 511 break;
16068db5 512 }
eeac7d46
MD
513 case OPT_USERSPACE:
514 opt_userspace = 1;
eeac7d46 515 break;
1624d5b7 516 case OPT_TRACEFILE_SIZE:
1cb514ce 517 opt_arg = poptGetOptArg(pc);
16068db5 518 if (utils_parse_size_suffix(opt_arg, &chan.attr.tracefile_size) < 0) {
1cb514ce 519 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg);
16068db5
MD
520 ret = CMD_ERROR;
521 goto end;
522 }
1624d5b7
JD
523 DBG("Maximum tracefile size set to %" PRIu64,
524 chan.attr.tracefile_size);
525 break;
526 case OPT_TRACEFILE_COUNT:
16068db5
MD
527 {
528 unsigned long v;
529
530 errno = 0;
1cb514ce
MD
531 opt_arg = poptGetOptArg(pc);
532 v = strtoul(opt_arg, NULL, 0);
533 if (errno != 0 || !isdigit(opt_arg[0])) {
534 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg);
16068db5
MD
535 ret = CMD_ERROR;
536 goto end;
537 }
538 if (v != (uint32_t) v) {
539 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg);
540 ret = CMD_ERROR;
541 goto end;
542 }
543 chan.attr.tracefile_count = (uint32_t) v;
1624d5b7
JD
544 DBG("Maximum tracefile count set to %" PRIu64,
545 chan.attr.tracefile_count);
546 break;
16068db5 547 }
679b4943
SM
548 case OPT_LIST_OPTIONS:
549 list_cmd_options(stdout, long_options);
679b4943 550 goto end;
d36b8583
DG
551 default:
552 usage(stderr);
553 ret = CMD_UNDEFINED;
554 goto end;
555 }
556 }
557
acc09215
JRJ
558 /* Mi check */
559 if (lttng_opt_mi) {
560 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
561 if (!writer) {
562 ret = -LTTNG_ERR_NOMEM;
563 goto end;
564 }
565
566 /* Open command element */
567 ret = mi_lttng_writer_command_open(writer,
568 mi_lttng_element_command_enable_channels);
569 if (ret) {
570 ret = CMD_ERROR;
571 goto end;
572 }
573
574 /* Open output element */
575 ret = mi_lttng_writer_open_element(writer,
576 mi_lttng_element_command_output);
577 if (ret) {
578 ret = CMD_ERROR;
579 goto end;
580 }
581 }
582
d36b8583
DG
583 opt_channels = (char*) poptGetArg(pc);
584 if (opt_channels == NULL) {
7d29a247 585 ERR("Missing channel name.\n");
d36b8583 586 usage(stderr);
ca1c3607 587 ret = CMD_ERROR;
acc09215
JRJ
588 success = 0;
589 goto mi_closing;
d36b8583
DG
590 }
591
cd80958d
DG
592 if (!opt_session_name) {
593 session_name = get_session_name();
594 if (session_name == NULL) {
acc09215
JRJ
595 command_ret = CMD_ERROR;
596 success = 0;
597 goto mi_closing;
cd80958d
DG
598 }
599 } else {
600 session_name = opt_session_name;
601 }
602
acc09215
JRJ
603 command_ret = enable_channel(session_name);
604 if (command_ret) {
605 success = 0;
606 }
607
608mi_closing:
609 /* Mi closing */
610 if (lttng_opt_mi) {
611 /* Close output element */
612 ret = mi_lttng_writer_close_element(writer);
613 if (ret) {
614 goto end;
615 }
616
617 /* Success ? */
618 ret = mi_lttng_writer_write_element_bool(writer,
619 mi_lttng_element_command_success, success);
620 if (ret) {
621 goto end;
622 }
623
624 /* Command element close */
625 ret = mi_lttng_writer_command_close(writer);
626 if (ret) {
627 goto end;
628 }
629 }
d36b8583
DG
630
631end:
acc09215
JRJ
632 /* Mi clean-up */
633 if (writer && mi_lttng_writer_destroy(writer)) {
634 /* Preserve original error code */
635 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
636 }
637
5853fd43
DG
638 if (!opt_session_name && session_name) {
639 free(session_name);
640 }
acc09215
JRJ
641
642 /* Overwrite ret if an error occurred when enable_channel */
643 ret = command_ret ? command_ret : ret;
ca1c3607 644 poptFreeContext(pc);
d36b8583
DG
645 return ret;
646}
This page took 0.077399 seconds and 5 git commands to generate.