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