Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / enable_channels.c
CommitLineData
d36b8583 1/*
90c106c6 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
MD
17#include <assert.h>
18#include <ctype.h>
d36b8583 19
acc09215
JRJ
20#include <common/sessiond-comm/sessiond-comm.h>
21#include <common/utils.h>
22#include <common/mi-lttng.h>
23
1ceda6f1
FD
24#include <lttng/domain-internal.h>
25
c399183f 26#include "../command.h"
1cb514ce 27#include "../utils.h"
d36b8583 28
42224349 29
cf0bcb51 30static struct lttng_channel chan_opts;
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 */
b7f4ea0f 142static int enable_channel(char *session_name, char *channel_list)
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
PP
182 /* Checked by the caller. */
183 assert(0);
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) {
224 assert(writer);
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,...) */
b7f4ea0f 233 channel_name = strtok(channel_list, ",");
d36b8583 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",
1ceda6f1
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;
b7f4ea0f 394 char *channel_list = NULL;
70d0b120 395 char *opt_arg = NULL;
b7f4ea0f 396 const char *arg_channel_list = NULL;
68c7f6e5 397 const char *leftover = NULL;
d36b8583 398
7d29a247
DG
399 init_channel_config();
400
d36b8583
DG
401 pc = poptGetContext(NULL, argc, argv, long_options, 0);
402 poptReadDefaultConfig(pc, 0);
403
404 while ((opt = poptGetNextOpt(pc)) != -1) {
405 switch (opt) {
406 case OPT_HELP:
4ba92f18 407 SHOW_HELP();
d36b8583 408 goto end;
7d29a247 409 case OPT_DISCARD:
cf0bcb51 410 chan_opts.attr.overwrite = 0;
7d29a247
DG
411 DBG("Channel set to discard");
412 break;
413 case OPT_OVERWRITE:
cf0bcb51 414 chan_opts.attr.overwrite = 1;
7d29a247
DG
415 DBG("Channel set to overwrite");
416 break;
417 case OPT_SUBBUF_SIZE:
1cb514ce
MD
418 {
419 uint64_t rounded_size;
420 int order;
421
70d0b120
SM
422 /* Parse the size */
423 opt_arg = poptGetOptArg(pc);
cf0bcb51 424 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.subbuf_size) < 0 || !chan_opts.attr.subbuf_size) {
1cb514ce 425 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg);
70d0b120
SM
426 ret = CMD_ERROR;
427 goto end;
428 }
429
cf0bcb51 430 order = get_count_order_u64(chan_opts.attr.subbuf_size);
1cb514ce
MD
431 assert(order >= 0);
432 rounded_size = 1ULL << order;
cf0bcb51 433 if (rounded_size < chan_opts.attr.subbuf_size) {
06c0da96 434 ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!",
cf0bcb51 435 chan_opts.attr.subbuf_size);
06c0da96
DG
436 ret = CMD_ERROR;
437 goto end;
438 }
439
cf0bcb51 440 if (rounded_size != chan_opts.attr.subbuf_size) {
1cb514ce 441 WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
cf0bcb51
JG
442 chan_opts.attr.subbuf_size, rounded_size);
443 chan_opts.attr.subbuf_size = rounded_size;
70d0b120
SM
444 }
445
1cb514ce 446 /* Should now be power of 2 */
cf0bcb51 447 assert(!((chan_opts.attr.subbuf_size - 1) & chan_opts.attr.subbuf_size));
1cb514ce 448
cf0bcb51 449 DBG("Channel subbuf size set to %" PRIu64, chan_opts.attr.subbuf_size);
7d29a247 450 break;
1cb514ce 451 }
7d29a247 452 case OPT_NUM_SUBBUF:
1cb514ce
MD
453 {
454 uint64_t rounded_size;
455 int order;
456
16068db5 457 errno = 0;
1cb514ce 458 opt_arg = poptGetOptArg(pc);
cf0bcb51
JG
459 chan_opts.attr.num_subbuf = strtoull(opt_arg, NULL, 0);
460 if (errno != 0 || !chan_opts.attr.num_subbuf || !isdigit(opt_arg[0])) {
1cb514ce 461 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg);
16068db5
MD
462 ret = CMD_ERROR;
463 goto end;
464 }
465
cf0bcb51 466 order = get_count_order_u64(chan_opts.attr.num_subbuf);
1cb514ce
MD
467 assert(order >= 0);
468 rounded_size = 1ULL << order;
cf0bcb51 469 if (rounded_size < chan_opts.attr.num_subbuf) {
06c0da96 470 ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!",
cf0bcb51 471 chan_opts.attr.num_subbuf);
06c0da96
DG
472 ret = CMD_ERROR;
473 goto end;
474 }
475
cf0bcb51 476 if (rounded_size != chan_opts.attr.num_subbuf) {
1cb514ce 477 WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
cf0bcb51
JG
478 chan_opts.attr.num_subbuf, rounded_size);
479 chan_opts.attr.num_subbuf = rounded_size;
1cb514ce
MD
480 }
481
482 /* Should now be power of 2 */
cf0bcb51 483 assert(!((chan_opts.attr.num_subbuf - 1) & chan_opts.attr.num_subbuf));
1cb514ce 484
cf0bcb51 485 DBG("Channel subbuf num set to %" PRIu64, chan_opts.attr.num_subbuf);
7d29a247 486 break;
1cb514ce 487 }
7d29a247 488 case OPT_SWITCH_TIMER:
16068db5 489 {
c7219617 490 uint64_t v;
16068db5
MD
491
492 errno = 0;
1cb514ce 493 opt_arg = poptGetOptArg(pc);
c7219617
SM
494
495 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
496 ERR("Wrong value for --switch-timer parameter: %s", opt_arg);
16068db5
MD
497 ret = CMD_ERROR;
498 goto end;
499 }
c7219617 500
16068db5
MD
501 if (v != (uint32_t) v) {
502 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg);
503 ret = CMD_ERROR;
504 goto end;
505 }
cf0bcb51 506 chan_opts.attr.switch_timer_interval = (uint32_t) v;
2a1135fa
JG
507 DBG("Channel switch timer interval set to %d %s",
508 chan_opts.attr.switch_timer_interval,
509 USEC_UNIT);
7d29a247 510 break;
16068db5 511 }
7d29a247 512 case OPT_READ_TIMER:
16068db5 513 {
c7219617 514 uint64_t v;
16068db5
MD
515
516 errno = 0;
1cb514ce 517 opt_arg = poptGetOptArg(pc);
c7219617
SM
518
519 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
520 ERR("Wrong value for --read-timer parameter: %s", opt_arg);
16068db5
MD
521 ret = CMD_ERROR;
522 goto end;
523 }
c7219617 524
16068db5
MD
525 if (v != (uint32_t) v) {
526 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg);
527 ret = CMD_ERROR;
528 goto end;
529 }
cf0bcb51 530 chan_opts.attr.read_timer_interval = (uint32_t) v;
2a1135fa
JG
531 DBG("Channel read timer interval set to %d %s",
532 chan_opts.attr.read_timer_interval,
533 USEC_UNIT);
cf0bcb51
JG
534 break;
535 }
536 case OPT_MONITOR_TIMER:
537 {
7010c033 538 uint64_t v;
cf0bcb51
JG
539
540 errno = 0;
541 opt_arg = poptGetOptArg(pc);
7010c033
SM
542
543 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
544 ERR("Wrong value for --monitor-timer parameter: %s", opt_arg);
cf0bcb51
JG
545 ret = CMD_ERROR;
546 goto end;
547 }
8193ef17 548 opt_monitor_timer.interval = (uint64_t) v;
cf0bcb51 549 opt_monitor_timer.set = true;
2a1135fa
JG
550 DBG("Channel monitor timer interval set to %" PRIu64 " %s",
551 opt_monitor_timer.interval,
552 USEC_UNIT);
d36b8583 553 break;
16068db5 554 }
491d1539
MD
555 case OPT_BLOCKING_TIMEOUT:
556 {
7010c033 557 uint64_t v;
491d1539
MD
558 long long v_msec;
559
560 errno = 0;
561 opt_arg = poptGetOptArg(pc);
63dd9b28
PP
562
563 if (strcmp(opt_arg, "inf") == 0) {
564 opt_blocking_timeout.value = (int64_t) -1;
565 opt_blocking_timeout.set = true;
566 DBG("Channel blocking timeout set to infinity");
567 break;
568 }
569
7010c033
SM
570 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
571 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg);
491d1539
MD
572 ret = CMD_ERROR;
573 goto end;
574 }
63dd9b28
PP
575
576 /*
577 * While LTTng-UST and LTTng-tools will accept a
578 * blocking timeout expressed in µs, the current
579 * tracer implementation relies on poll() which
580 * takes an "int timeout" parameter expressed in
581 * msec.
582 *
583 * Since the error reporting from the tracer is
584 * not precise, we perform this check here to
585 * provide a helpful error message in case of
586 * overflow.
587 *
588 * The setter (liblttng-ctl) also performs an
589 * equivalent check.
590 */
591 v_msec = v / 1000;
592 if (v_msec != (int32_t) v_msec) {
593 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s", opt_arg);
594 ret = CMD_ERROR;
595 goto end;
491d1539 596 }
63dd9b28 597
491d1539
MD
598 opt_blocking_timeout.value = (int64_t) v;
599 opt_blocking_timeout.set = true;
2a1135fa 600 DBG("Channel blocking timeout set to %" PRId64 " %s%s",
63dd9b28 601 opt_blocking_timeout.value,
2a1135fa 602 USEC_UNIT,
63dd9b28
PP
603 opt_blocking_timeout.value == 0 ?
604 " (non-blocking)" : "");
491d1539
MD
605 break;
606 }
eeac7d46
MD
607 case OPT_USERSPACE:
608 opt_userspace = 1;
eeac7d46 609 break;
1624d5b7 610 case OPT_TRACEFILE_SIZE:
1cb514ce 611 opt_arg = poptGetOptArg(pc);
cf0bcb51 612 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.tracefile_size) < 0) {
1cb514ce 613 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg);
16068db5
MD
614 ret = CMD_ERROR;
615 goto end;
616 }
1624d5b7 617 DBG("Maximum tracefile size set to %" PRIu64,
cf0bcb51 618 chan_opts.attr.tracefile_size);
1624d5b7
JD
619 break;
620 case OPT_TRACEFILE_COUNT:
16068db5
MD
621 {
622 unsigned long v;
623
624 errno = 0;
1cb514ce
MD
625 opt_arg = poptGetOptArg(pc);
626 v = strtoul(opt_arg, NULL, 0);
627 if (errno != 0 || !isdigit(opt_arg[0])) {
628 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg);
16068db5
MD
629 ret = CMD_ERROR;
630 goto end;
631 }
632 if (v != (uint32_t) v) {
633 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg);
634 ret = CMD_ERROR;
635 goto end;
636 }
cf0bcb51 637 chan_opts.attr.tracefile_count = (uint32_t) v;
1624d5b7 638 DBG("Maximum tracefile count set to %" PRIu64,
cf0bcb51 639 chan_opts.attr.tracefile_count);
1624d5b7 640 break;
16068db5 641 }
679b4943
SM
642 case OPT_LIST_OPTIONS:
643 list_cmd_options(stdout, long_options);
679b4943 644 goto end;
d36b8583 645 default:
d36b8583
DG
646 ret = CMD_UNDEFINED;
647 goto end;
648 }
ccdec6d9
JG
649
650 if (opt_arg) {
651 free(opt_arg);
652 opt_arg = NULL;
653 }
d36b8583
DG
654 }
655
3533d06b
JG
656 ret = print_missing_or_multiple_domains(
657 opt_kernel + opt_userspace, false);
3ecec76a
PP
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
b7f4ea0f
MJ
696 arg_channel_list = poptGetArg(pc);
697 if (arg_channel_list == NULL) {
698 ERR("Missing channel name.");
699 ret = CMD_ERROR;
700 success = 0;
701 goto mi_closing;
702 }
703
704 channel_list = strdup(arg_channel_list);
705 if (channel_list == NULL) {
706 PERROR("Failed to copy channel name");
ca1c3607 707 ret = CMD_ERROR;
acc09215
JRJ
708 success = 0;
709 goto mi_closing;
d36b8583
DG
710 }
711
68c7f6e5
JD
712 leftover = poptGetArg(pc);
713 if (leftover) {
714 ERR("Unknown argument: %s", leftover);
715 ret = CMD_ERROR;
716 success = 0;
717 goto mi_closing;
718 }
719
cd80958d
DG
720 if (!opt_session_name) {
721 session_name = get_session_name();
722 if (session_name == NULL) {
acc09215
JRJ
723 command_ret = CMD_ERROR;
724 success = 0;
725 goto mi_closing;
cd80958d
DG
726 }
727 } else {
728 session_name = opt_session_name;
729 }
730
b7f4ea0f 731 command_ret = enable_channel(session_name, channel_list);
acc09215
JRJ
732 if (command_ret) {
733 success = 0;
734 }
735
736mi_closing:
737 /* Mi closing */
738 if (lttng_opt_mi) {
739 /* Close output element */
740 ret = mi_lttng_writer_close_element(writer);
741 if (ret) {
742 goto end;
743 }
744
745 /* Success ? */
746 ret = mi_lttng_writer_write_element_bool(writer,
747 mi_lttng_element_command_success, success);
748 if (ret) {
749 goto end;
750 }
751
752 /* Command element close */
753 ret = mi_lttng_writer_command_close(writer);
754 if (ret) {
755 goto end;
756 }
757 }
d36b8583
DG
758
759end:
acc09215
JRJ
760 /* Mi clean-up */
761 if (writer && mi_lttng_writer_destroy(writer)) {
762 /* Preserve original error code */
763 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
764 }
765
5853fd43
DG
766 if (!opt_session_name && session_name) {
767 free(session_name);
768 }
acc09215 769
b7f4ea0f
MJ
770 free(channel_list);
771
acc09215
JRJ
772 /* Overwrite ret if an error occurred when enable_channel */
773 ret = command_ret ? command_ret : ret;
ca1c3607 774 poptFreeContext(pc);
ccdec6d9 775 free(opt_arg);
d36b8583
DG
776 return ret;
777}
This page took 0.110332 seconds and 4 git commands to generate.