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