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