| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | #include "../command.hpp" |
| 10 | #include "../utils.hpp" |
| 11 | |
| 12 | #include <common/lttng-kernel.hpp> |
| 13 | #include <common/mi-lttng.hpp> |
| 14 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 15 | #include <common/utils.hpp> |
| 16 | |
| 17 | #include <lttng/domain-internal.hpp> |
| 18 | |
| 19 | #include <ctype.h> |
| 20 | #include <inttypes.h> |
| 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> |
| 28 | |
| 29 | static struct lttng_channel chan_opts; |
| 30 | static int opt_kernel; |
| 31 | static char *opt_session_name; |
| 32 | static int opt_userspace; |
| 33 | static char *opt_output; |
| 34 | static int opt_buffer_uid; |
| 35 | static int opt_buffer_pid; |
| 36 | static int opt_buffer_global; |
| 37 | static struct { |
| 38 | bool set; |
| 39 | uint64_t interval; |
| 40 | } opt_monitor_timer; |
| 41 | static struct { |
| 42 | bool set; |
| 43 | int64_t value; |
| 44 | } opt_blocking_timeout; |
| 45 | |
| 46 | static struct mi_writer *writer; |
| 47 | |
| 48 | #ifdef LTTNG_EMBED_HELP |
| 49 | static const char help_msg[] = |
| 50 | #include <lttng-enable-channel.1.h> |
| 51 | ; |
| 52 | #endif |
| 53 | |
| 54 | enum { |
| 55 | OPT_HELP = 1, |
| 56 | OPT_DISCARD, |
| 57 | OPT_OVERWRITE, |
| 58 | OPT_SUBBUF_SIZE, |
| 59 | OPT_NUM_SUBBUF, |
| 60 | OPT_SWITCH_TIMER, |
| 61 | OPT_MONITOR_TIMER, |
| 62 | OPT_READ_TIMER, |
| 63 | OPT_USERSPACE, |
| 64 | OPT_LIST_OPTIONS, |
| 65 | OPT_TRACEFILE_SIZE, |
| 66 | OPT_TRACEFILE_COUNT, |
| 67 | OPT_BLOCKING_TIMEOUT, |
| 68 | }; |
| 69 | |
| 70 | static struct lttng_handle *handle; |
| 71 | |
| 72 | const char *output_mmap = "mmap"; |
| 73 | const char *output_splice = "splice"; |
| 74 | |
| 75 | static struct poptOption long_options[] = { |
| 76 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 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 } |
| 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * Set default attributes depending on those already defined from the command |
| 101 | * line. |
| 102 | */ |
| 103 | static void set_default_attr(struct lttng_domain *dom) |
| 104 | { |
| 105 | struct lttng_channel_attr default_attr; |
| 106 | |
| 107 | memset(&default_attr, 0, sizeof(default_attr)); |
| 108 | |
| 109 | /* Set attributes */ |
| 110 | lttng_channel_set_default_attr(dom, &default_attr); |
| 111 | |
| 112 | if (chan_opts.attr.overwrite == -1) { |
| 113 | chan_opts.attr.overwrite = default_attr.overwrite; |
| 114 | } |
| 115 | if (chan_opts.attr.subbuf_size == -1) { |
| 116 | chan_opts.attr.subbuf_size = default_attr.subbuf_size; |
| 117 | } |
| 118 | if (chan_opts.attr.num_subbuf == -1) { |
| 119 | chan_opts.attr.num_subbuf = default_attr.num_subbuf; |
| 120 | } |
| 121 | if (chan_opts.attr.switch_timer_interval == -1) { |
| 122 | chan_opts.attr.switch_timer_interval = default_attr.switch_timer_interval; |
| 123 | } |
| 124 | if (chan_opts.attr.read_timer_interval == -1) { |
| 125 | chan_opts.attr.read_timer_interval = default_attr.read_timer_interval; |
| 126 | } |
| 127 | if ((int) chan_opts.attr.output == -1) { |
| 128 | chan_opts.attr.output = default_attr.output; |
| 129 | } |
| 130 | if (chan_opts.attr.tracefile_count == -1) { |
| 131 | chan_opts.attr.tracefile_count = default_attr.tracefile_count; |
| 132 | } |
| 133 | if (chan_opts.attr.tracefile_size == -1) { |
| 134 | chan_opts.attr.tracefile_size = default_attr.tracefile_size; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Adding channel using the lttng API. |
| 140 | */ |
| 141 | static int enable_channel(char *session_name, char *channel_list) |
| 142 | { |
| 143 | struct lttng_channel *channel = nullptr; |
| 144 | int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0; |
| 145 | char *channel_name; |
| 146 | struct lttng_domain dom; |
| 147 | |
| 148 | memset(&dom, 0, sizeof(dom)); |
| 149 | |
| 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 | |
| 159 | /* Create lttng domain */ |
| 160 | if (opt_kernel) { |
| 161 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 162 | dom.buf_type = LTTNG_BUFFER_GLOBAL; |
| 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 | } |
| 168 | } else if (opt_userspace) { |
| 169 | dom.type = LTTNG_DOMAIN_UST; |
| 170 | if (opt_buffer_pid) { |
| 171 | dom.buf_type = LTTNG_BUFFER_PER_PID; |
| 172 | } else { |
| 173 | if (opt_buffer_global) { |
| 174 | ERR("Buffer type not supported for domain -u"); |
| 175 | ret = CMD_ERROR; |
| 176 | goto error; |
| 177 | } |
| 178 | dom.buf_type = LTTNG_BUFFER_PER_UID; |
| 179 | } |
| 180 | } else { |
| 181 | /* Checked by the caller. */ |
| 182 | abort(); |
| 183 | } |
| 184 | |
| 185 | set_default_attr(&dom); |
| 186 | |
| 187 | if (chan_opts.attr.tracefile_size == 0 && chan_opts.attr.tracefile_count) { |
| 188 | ERR("Missing option --tracefile-size. " |
| 189 | "A file count without a size won't do anything."); |
| 190 | ret = CMD_ERROR; |
| 191 | goto error; |
| 192 | } |
| 193 | |
| 194 | if ((chan_opts.attr.tracefile_size > 0) && |
| 195 | (chan_opts.attr.tracefile_size < chan_opts.attr.subbuf_size)) { |
| 196 | WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")", |
| 197 | chan_opts.attr.tracefile_size, |
| 198 | chan_opts.attr.subbuf_size); |
| 199 | chan_opts.attr.tracefile_size = chan_opts.attr.subbuf_size; |
| 200 | } |
| 201 | |
| 202 | /* Setting channel output */ |
| 203 | if (opt_output) { |
| 204 | if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) { |
| 205 | chan_opts.attr.output = LTTNG_EVENT_MMAP; |
| 206 | } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) { |
| 207 | chan_opts.attr.output = LTTNG_EVENT_SPLICE; |
| 208 | } else { |
| 209 | ERR("Unknown output type %s. Possible values are: %s, %s\n", |
| 210 | opt_output, |
| 211 | output_mmap, |
| 212 | output_splice); |
| 213 | ret = CMD_ERROR; |
| 214 | goto error; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | handle = lttng_create_handle(session_name, &dom); |
| 219 | if (handle == nullptr) { |
| 220 | ret = -1; |
| 221 | goto error; |
| 222 | } |
| 223 | |
| 224 | /* Mi open channels element */ |
| 225 | if (lttng_opt_mi) { |
| 226 | LTTNG_ASSERT(writer); |
| 227 | ret = mi_lttng_channels_open(writer); |
| 228 | if (ret) { |
| 229 | ret = CMD_ERROR; |
| 230 | goto error; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /* Strip channel list (format: chan1,chan2,...) */ |
| 235 | channel_name = strtok(channel_list, ","); |
| 236 | while (channel_name != nullptr) { |
| 237 | void *extended_ptr; |
| 238 | |
| 239 | /* Validate channel name's length */ |
| 240 | if (strlen(channel_name) >= sizeof(chan_opts.name)) { |
| 241 | ERR("Channel name is too long (max. %zu characters)", |
| 242 | sizeof(chan_opts.name) - 1); |
| 243 | error = 1; |
| 244 | goto skip_enable; |
| 245 | } |
| 246 | |
| 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 | |
| 258 | /* Copy channel name */ |
| 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, |
| 266 | opt_monitor_timer.interval); |
| 267 | if (ret) { |
| 268 | ERR("Failed to set the channel's monitor timer interval"); |
| 269 | error = 1; |
| 270 | goto error; |
| 271 | } |
| 272 | } |
| 273 | if (opt_blocking_timeout.set) { |
| 274 | ret = lttng_channel_set_blocking_timeout(channel, |
| 275 | opt_blocking_timeout.value); |
| 276 | if (ret) { |
| 277 | ERR("Failed to set the channel's blocking timeout"); |
| 278 | error = 1; |
| 279 | goto error; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | DBG("Enabling channel %s", channel_name); |
| 284 | |
| 285 | ret = lttng_enable_channel(handle, channel); |
| 286 | if (ret < 0) { |
| 287 | bool msg_already_printed = false; |
| 288 | |
| 289 | success = 0; |
| 290 | switch (-ret) { |
| 291 | case LTTNG_ERR_KERN_CHAN_EXIST: |
| 292 | case LTTNG_ERR_UST_CHAN_EXIST: |
| 293 | case LTTNG_ERR_CHAN_EXIST: |
| 294 | warn = 1; |
| 295 | break; |
| 296 | case LTTNG_ERR_INVALID_CHANNEL_NAME: |
| 297 | ERR("Invalid channel name: \"%s\". " |
| 298 | "Channel names may not start with '.', and " |
| 299 | "may not contain '/'.", |
| 300 | channel_name); |
| 301 | msg_already_printed = true; |
| 302 | error = 1; |
| 303 | break; |
| 304 | default: |
| 305 | error = 1; |
| 306 | break; |
| 307 | } |
| 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(); |
| 319 | } |
| 320 | } else { |
| 321 | MSG("%s channel `%s` enabled for session `%s`", |
| 322 | lttng_domain_type_str(dom.type), |
| 323 | channel_name, |
| 324 | session_name); |
| 325 | success = 1; |
| 326 | } |
| 327 | |
| 328 | skip_enable: |
| 329 | if (lttng_opt_mi) { |
| 330 | /* Mi print the channel element and leave it open */ |
| 331 | ret = mi_lttng_channel(writer, channel, 1); |
| 332 | if (ret) { |
| 333 | ret = CMD_ERROR; |
| 334 | goto error; |
| 335 | } |
| 336 | |
| 337 | /* Individual Success ? */ |
| 338 | ret = mi_lttng_writer_write_element_bool( |
| 339 | writer, mi_lttng_element_command_success, success); |
| 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 | } |
| 351 | } |
| 352 | |
| 353 | /* Next channel */ |
| 354 | channel_name = strtok(nullptr, ","); |
| 355 | lttng_channel_destroy(channel); |
| 356 | channel = nullptr; |
| 357 | } |
| 358 | |
| 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 | |
| 368 | ret = CMD_SUCCESS; |
| 369 | |
| 370 | error: |
| 371 | if (channel) { |
| 372 | lttng_channel_destroy(channel); |
| 373 | } |
| 374 | /* If more important error happen bypass the warning */ |
| 375 | if (!ret && warn) { |
| 376 | ret = CMD_WARNING; |
| 377 | } |
| 378 | /* If more important error happen bypass the warning */ |
| 379 | if (!ret && error) { |
| 380 | ret = CMD_ERROR; |
| 381 | } |
| 382 | |
| 383 | lttng_destroy_handle(handle); |
| 384 | |
| 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Default value for channel configuration. |
| 390 | */ |
| 391 | static void init_channel_config() |
| 392 | { |
| 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 | */ |
| 397 | memset(&chan_opts.attr, -1, sizeof(chan_opts.attr)); |
| 398 | chan_opts.attr.extended.ptr = nullptr; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Add channel to trace session |
| 403 | */ |
| 404 | int cmd_enable_channels(int argc, const char **argv) |
| 405 | { |
| 406 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 407 | static poptContext pc; |
| 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; |
| 413 | |
| 414 | init_channel_config(); |
| 415 | |
| 416 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
| 417 | poptReadDefaultConfig(pc, 0); |
| 418 | |
| 419 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 420 | switch (opt) { |
| 421 | case OPT_HELP: |
| 422 | SHOW_HELP(); |
| 423 | goto end; |
| 424 | case OPT_DISCARD: |
| 425 | chan_opts.attr.overwrite = 0; |
| 426 | DBG("Channel set to discard"); |
| 427 | break; |
| 428 | case OPT_OVERWRITE: |
| 429 | chan_opts.attr.overwrite = 1; |
| 430 | DBG("Channel set to overwrite"); |
| 431 | break; |
| 432 | case OPT_SUBBUF_SIZE: |
| 433 | { |
| 434 | uint64_t rounded_size; |
| 435 | int order; |
| 436 | |
| 437 | /* Parse the size */ |
| 438 | opt_arg = poptGetOptArg(pc); |
| 439 | if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.subbuf_size) < 0 || |
| 440 | !chan_opts.attr.subbuf_size) { |
| 441 | ERR("Wrong value in --subbuf-size parameter: %s", opt_arg); |
| 442 | ret = CMD_ERROR; |
| 443 | goto end; |
| 444 | } |
| 445 | |
| 446 | order = get_count_order_u64(chan_opts.attr.subbuf_size); |
| 447 | LTTNG_ASSERT(order >= 0); |
| 448 | rounded_size = 1ULL << order; |
| 449 | if (rounded_size < chan_opts.attr.subbuf_size) { |
| 450 | ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!", |
| 451 | chan_opts.attr.subbuf_size); |
| 452 | ret = CMD_ERROR; |
| 453 | goto end; |
| 454 | } |
| 455 | |
| 456 | if (rounded_size != chan_opts.attr.subbuf_size) { |
| 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); |
| 461 | chan_opts.attr.subbuf_size = rounded_size; |
| 462 | } |
| 463 | |
| 464 | /* Should now be power of 2 */ |
| 465 | LTTNG_ASSERT( |
| 466 | !((chan_opts.attr.subbuf_size - 1) & chan_opts.attr.subbuf_size)); |
| 467 | |
| 468 | DBG("Channel subbuf size set to %" PRIu64, chan_opts.attr.subbuf_size); |
| 469 | break; |
| 470 | } |
| 471 | case OPT_NUM_SUBBUF: |
| 472 | { |
| 473 | uint64_t rounded_size; |
| 474 | int order; |
| 475 | |
| 476 | errno = 0; |
| 477 | opt_arg = poptGetOptArg(pc); |
| 478 | chan_opts.attr.num_subbuf = strtoull(opt_arg, nullptr, 0); |
| 479 | if (errno != 0 || !chan_opts.attr.num_subbuf || !isdigit(opt_arg[0])) { |
| 480 | ERR("Wrong value in --num-subbuf parameter: %s", opt_arg); |
| 481 | ret = CMD_ERROR; |
| 482 | goto end; |
| 483 | } |
| 484 | |
| 485 | order = get_count_order_u64(chan_opts.attr.num_subbuf); |
| 486 | LTTNG_ASSERT(order >= 0); |
| 487 | rounded_size = 1ULL << order; |
| 488 | if (rounded_size < chan_opts.attr.num_subbuf) { |
| 489 | ERR("The number of subbuffers (%" PRIu64 |
| 490 | ") is rounded and overflows!", |
| 491 | chan_opts.attr.num_subbuf); |
| 492 | ret = CMD_ERROR; |
| 493 | goto end; |
| 494 | } |
| 495 | |
| 496 | if (rounded_size != chan_opts.attr.num_subbuf) { |
| 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); |
| 501 | chan_opts.attr.num_subbuf = rounded_size; |
| 502 | } |
| 503 | |
| 504 | /* Should now be power of 2 */ |
| 505 | LTTNG_ASSERT( |
| 506 | !((chan_opts.attr.num_subbuf - 1) & chan_opts.attr.num_subbuf)); |
| 507 | |
| 508 | DBG("Channel subbuf num set to %" PRIu64, chan_opts.attr.num_subbuf); |
| 509 | break; |
| 510 | } |
| 511 | case OPT_SWITCH_TIMER: |
| 512 | { |
| 513 | uint64_t v; |
| 514 | |
| 515 | errno = 0; |
| 516 | opt_arg = poptGetOptArg(pc); |
| 517 | |
| 518 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
| 519 | ERR("Wrong value for --switch-timer parameter: %s", opt_arg); |
| 520 | ret = CMD_ERROR; |
| 521 | goto end; |
| 522 | } |
| 523 | |
| 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 | } |
| 529 | chan_opts.attr.switch_timer_interval = (uint32_t) v; |
| 530 | DBG("Channel switch timer interval set to %d %s", |
| 531 | chan_opts.attr.switch_timer_interval, |
| 532 | USEC_UNIT); |
| 533 | break; |
| 534 | } |
| 535 | case OPT_READ_TIMER: |
| 536 | { |
| 537 | uint64_t v; |
| 538 | |
| 539 | errno = 0; |
| 540 | opt_arg = poptGetOptArg(pc); |
| 541 | |
| 542 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
| 543 | ERR("Wrong value for --read-timer parameter: %s", opt_arg); |
| 544 | ret = CMD_ERROR; |
| 545 | goto end; |
| 546 | } |
| 547 | |
| 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 | } |
| 553 | chan_opts.attr.read_timer_interval = (uint32_t) v; |
| 554 | DBG("Channel read timer interval set to %d %s", |
| 555 | chan_opts.attr.read_timer_interval, |
| 556 | USEC_UNIT); |
| 557 | break; |
| 558 | } |
| 559 | case OPT_MONITOR_TIMER: |
| 560 | { |
| 561 | uint64_t v; |
| 562 | |
| 563 | errno = 0; |
| 564 | opt_arg = poptGetOptArg(pc); |
| 565 | |
| 566 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
| 567 | ERR("Wrong value for --monitor-timer parameter: %s", opt_arg); |
| 568 | ret = CMD_ERROR; |
| 569 | goto end; |
| 570 | } |
| 571 | opt_monitor_timer.interval = (uint64_t) v; |
| 572 | opt_monitor_timer.set = true; |
| 573 | DBG("Channel monitor timer interval set to %" PRIu64 " %s", |
| 574 | opt_monitor_timer.interval, |
| 575 | USEC_UNIT); |
| 576 | break; |
| 577 | } |
| 578 | case OPT_BLOCKING_TIMEOUT: |
| 579 | { |
| 580 | uint64_t v; |
| 581 | long long v_msec; |
| 582 | |
| 583 | errno = 0; |
| 584 | opt_arg = poptGetOptArg(pc); |
| 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 | |
| 593 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
| 594 | ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg); |
| 595 | ret = CMD_ERROR; |
| 596 | goto end; |
| 597 | } |
| 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) { |
| 616 | ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s", |
| 617 | opt_arg); |
| 618 | ret = CMD_ERROR; |
| 619 | goto end; |
| 620 | } |
| 621 | |
| 622 | opt_blocking_timeout.value = (int64_t) v; |
| 623 | opt_blocking_timeout.set = true; |
| 624 | DBG("Channel blocking timeout set to %" PRId64 " %s%s", |
| 625 | opt_blocking_timeout.value, |
| 626 | USEC_UNIT, |
| 627 | opt_blocking_timeout.value == 0 ? " (non-blocking)" : ""); |
| 628 | break; |
| 629 | } |
| 630 | case OPT_USERSPACE: |
| 631 | opt_userspace = 1; |
| 632 | break; |
| 633 | case OPT_TRACEFILE_SIZE: |
| 634 | opt_arg = poptGetOptArg(pc); |
| 635 | if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.tracefile_size) < 0) { |
| 636 | ERR("Wrong value in --tracefile-size parameter: %s", opt_arg); |
| 637 | ret = CMD_ERROR; |
| 638 | goto end; |
| 639 | } |
| 640 | DBG("Maximum tracefile size set to %" PRIu64, |
| 641 | chan_opts.attr.tracefile_size); |
| 642 | break; |
| 643 | case OPT_TRACEFILE_COUNT: |
| 644 | { |
| 645 | unsigned long v; |
| 646 | |
| 647 | errno = 0; |
| 648 | opt_arg = poptGetOptArg(pc); |
| 649 | v = strtoul(opt_arg, nullptr, 0); |
| 650 | if (errno != 0 || !isdigit(opt_arg[0])) { |
| 651 | ERR("Wrong value in --tracefile-count parameter: %s", opt_arg); |
| 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 | } |
| 660 | chan_opts.attr.tracefile_count = (uint32_t) v; |
| 661 | DBG("Maximum tracefile count set to %" PRIu64, |
| 662 | chan_opts.attr.tracefile_count); |
| 663 | break; |
| 664 | } |
| 665 | case OPT_LIST_OPTIONS: |
| 666 | list_cmd_options(stdout, long_options); |
| 667 | goto end; |
| 668 | default: |
| 669 | ret = CMD_UNDEFINED; |
| 670 | goto end; |
| 671 | } |
| 672 | |
| 673 | if (opt_arg) { |
| 674 | free(opt_arg); |
| 675 | opt_arg = nullptr; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace, false); |
| 680 | if (ret) { |
| 681 | ret = CMD_ERROR; |
| 682 | goto end; |
| 683 | } |
| 684 | |
| 685 | if (chan_opts.attr.overwrite == 1 && opt_blocking_timeout.set && |
| 686 | opt_blocking_timeout.value != 0) { |
| 687 | ERR("You cannot specify --overwrite and --blocking-timeout=N, " |
| 688 | "where N is different than 0"); |
| 689 | ret = CMD_ERROR; |
| 690 | goto end; |
| 691 | } |
| 692 | |
| 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, |
| 703 | mi_lttng_element_command_enable_channels); |
| 704 | if (ret) { |
| 705 | ret = CMD_ERROR; |
| 706 | goto end; |
| 707 | } |
| 708 | |
| 709 | /* Open output element */ |
| 710 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
| 711 | if (ret) { |
| 712 | ret = CMD_ERROR; |
| 713 | goto end; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | arg_channel_list = poptGetArg(pc); |
| 718 | if (arg_channel_list == nullptr) { |
| 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); |
| 726 | if (channel_list == nullptr) { |
| 727 | PERROR("Failed to copy channel name"); |
| 728 | ret = CMD_ERROR; |
| 729 | success = 0; |
| 730 | goto mi_closing; |
| 731 | } |
| 732 | |
| 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 | |
| 741 | if (!opt_session_name) { |
| 742 | session_name = get_session_name(); |
| 743 | if (session_name == nullptr) { |
| 744 | command_ret = CMD_ERROR; |
| 745 | success = 0; |
| 746 | goto mi_closing; |
| 747 | } |
| 748 | } else { |
| 749 | session_name = opt_session_name; |
| 750 | } |
| 751 | |
| 752 | command_ret = enable_channel(session_name, channel_list); |
| 753 | if (command_ret) { |
| 754 | success = 0; |
| 755 | } |
| 756 | |
| 757 | mi_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 ? */ |
| 767 | ret = mi_lttng_writer_write_element_bool( |
| 768 | writer, mi_lttng_element_command_success, success); |
| 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 | } |
| 779 | |
| 780 | end: |
| 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 | |
| 787 | if (!opt_session_name && session_name) { |
| 788 | free(session_name); |
| 789 | } |
| 790 | |
| 791 | free(channel_list); |
| 792 | |
| 793 | /* Overwrite ret if an error occurred when enable_channel */ |
| 794 | ret = command_ret ? command_ret : ret; |
| 795 | poptFreeContext(pc); |
| 796 | free(opt_arg); |
| 797 | return ret; |
| 798 | } |