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