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