| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 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> |
| 16 | #include <inttypes.h> |
| 17 | #include <assert.h> |
| 18 | #include <ctype.h> |
| 19 | |
| 20 | #include <common/sessiond-comm/sessiond-comm.h> |
| 21 | #include <common/utils.h> |
| 22 | #include <common/mi-lttng.h> |
| 23 | |
| 24 | #include "../command.h" |
| 25 | #include "../utils.h" |
| 26 | |
| 27 | |
| 28 | static struct lttng_channel chan_opts; |
| 29 | static char *opt_channels; |
| 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, 0, OPT_HELP, 0, 0}, |
| 78 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 79 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 80 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 81 | {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0}, |
| 82 | {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0}, |
| 83 | {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0}, |
| 84 | {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0}, |
| 85 | {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0}, |
| 86 | {"monitor-timer", 0, POPT_ARG_INT, 0, OPT_MONITOR_TIMER, 0, 0}, |
| 87 | {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0}, |
| 88 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 89 | {"output", 0, POPT_ARG_STRING, &opt_output, 0, 0, 0}, |
| 90 | {"buffers-uid", 0, POPT_ARG_VAL, &opt_buffer_uid, 1, 0, 0}, |
| 91 | {"buffers-pid", 0, POPT_ARG_VAL, &opt_buffer_pid, 1, 0, 0}, |
| 92 | {"buffers-global", 0, POPT_ARG_VAL, &opt_buffer_global, 1, 0, 0}, |
| 93 | {"tracefile-size", 'C', POPT_ARG_INT, 0, OPT_TRACEFILE_SIZE, 0, 0}, |
| 94 | {"tracefile-count", 'W', POPT_ARG_INT, 0, OPT_TRACEFILE_COUNT, 0, 0}, |
| 95 | {"blocking-timeout", 0, POPT_ARG_INT, 0, OPT_BLOCKING_TIMEOUT, 0, 0}, |
| 96 | {0, 0, 0, 0, 0, 0, 0} |
| 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) |
| 142 | { |
| 143 | struct lttng_channel *channel = NULL; |
| 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 | assert(0); |
| 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, chan_opts.attr.subbuf_size); |
| 198 | chan_opts.attr.tracefile_size = chan_opts.attr.subbuf_size; |
| 199 | } |
| 200 | |
| 201 | /* Setting channel output */ |
| 202 | if (opt_output) { |
| 203 | if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) { |
| 204 | chan_opts.attr.output = LTTNG_EVENT_MMAP; |
| 205 | } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) { |
| 206 | chan_opts.attr.output = LTTNG_EVENT_SPLICE; |
| 207 | } else { |
| 208 | ERR("Unknown output type %s. Possible values are: %s, %s\n", |
| 209 | opt_output, output_mmap, output_splice); |
| 210 | ret = CMD_ERROR; |
| 211 | goto error; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | handle = lttng_create_handle(session_name, &dom); |
| 216 | if (handle == NULL) { |
| 217 | ret = -1; |
| 218 | goto error; |
| 219 | } |
| 220 | |
| 221 | /* Mi open channels element */ |
| 222 | if (lttng_opt_mi) { |
| 223 | assert(writer); |
| 224 | ret = mi_lttng_channels_open(writer); |
| 225 | if (ret) { |
| 226 | ret = CMD_ERROR; |
| 227 | goto error; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /* Strip channel list (format: chan1,chan2,...) */ |
| 232 | channel_name = strtok(opt_channels, ","); |
| 233 | while (channel_name != NULL) { |
| 234 | void *extended_ptr; |
| 235 | |
| 236 | /* Validate channel name's length */ |
| 237 | if (strlen(channel_name) >= sizeof(chan_opts.name)) { |
| 238 | ERR("Channel name is too long (max. %zu characters)", |
| 239 | sizeof(chan_opts.name) - 1); |
| 240 | error = 1; |
| 241 | goto skip_enable; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * A dynamically-allocated channel is used in order to allow |
| 246 | * the configuration of extended attributes (post-2.9). |
| 247 | */ |
| 248 | channel = lttng_channel_create(&dom); |
| 249 | if (!channel) { |
| 250 | ERR("Unable to create channel object"); |
| 251 | error = 1; |
| 252 | goto error; |
| 253 | } |
| 254 | |
| 255 | /* Copy channel name */ |
| 256 | strcpy(channel->name, channel_name); |
| 257 | channel->enabled = 1; |
| 258 | extended_ptr = channel->attr.extended.ptr; |
| 259 | memcpy(&channel->attr, &chan_opts.attr, sizeof(chan_opts.attr)); |
| 260 | channel->attr.extended.ptr = extended_ptr; |
| 261 | if (opt_monitor_timer.set) { |
| 262 | ret = lttng_channel_set_monitor_timer_interval(channel, |
| 263 | opt_monitor_timer.interval); |
| 264 | if (ret) { |
| 265 | ERR("Failed to set the channel's monitor timer interval"); |
| 266 | error = 1; |
| 267 | goto error; |
| 268 | } |
| 269 | } |
| 270 | if (opt_blocking_timeout.set) { |
| 271 | ret = lttng_channel_set_blocking_timeout(channel, |
| 272 | opt_blocking_timeout.value); |
| 273 | if (ret) { |
| 274 | ERR("Failed to set the channel's blocking timeout"); |
| 275 | error = 1; |
| 276 | goto error; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | DBG("Enabling channel %s", channel_name); |
| 281 | |
| 282 | ret = lttng_enable_channel(handle, channel); |
| 283 | if (ret < 0) { |
| 284 | success = 0; |
| 285 | switch (-ret) { |
| 286 | case LTTNG_ERR_KERN_CHAN_EXIST: |
| 287 | case LTTNG_ERR_UST_CHAN_EXIST: |
| 288 | case LTTNG_ERR_CHAN_EXIST: |
| 289 | WARN("Channel %s: %s (session %s)", channel_name, |
| 290 | lttng_strerror(ret), session_name); |
| 291 | warn = 1; |
| 292 | break; |
| 293 | case LTTNG_ERR_INVALID_CHANNEL_NAME: |
| 294 | ERR("Invalid channel name: \"%s\". " |
| 295 | "Channel names may not start with '.', and " |
| 296 | "may not contain '/'.", channel_name); |
| 297 | error = 1; |
| 298 | break; |
| 299 | default: |
| 300 | ERR("Channel %s: %s (session %s)", channel_name, |
| 301 | lttng_strerror(ret), session_name); |
| 302 | error = 1; |
| 303 | break; |
| 304 | } |
| 305 | } else { |
| 306 | MSG("%s channel %s enabled for session %s", |
| 307 | get_domain_str(dom.type), channel_name, session_name); |
| 308 | success = 1; |
| 309 | } |
| 310 | |
| 311 | skip_enable: |
| 312 | if (lttng_opt_mi) { |
| 313 | /* Mi print the channel element and leave it open */ |
| 314 | ret = mi_lttng_channel(writer, channel, 1); |
| 315 | if (ret) { |
| 316 | ret = CMD_ERROR; |
| 317 | goto error; |
| 318 | } |
| 319 | |
| 320 | /* Individual Success ? */ |
| 321 | ret = mi_lttng_writer_write_element_bool(writer, |
| 322 | mi_lttng_element_command_success, success); |
| 323 | if (ret) { |
| 324 | ret = CMD_ERROR; |
| 325 | goto error; |
| 326 | } |
| 327 | |
| 328 | /* Close channel element */ |
| 329 | ret = mi_lttng_writer_close_element(writer); |
| 330 | if (ret) { |
| 331 | ret = CMD_ERROR; |
| 332 | goto error; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* Next channel */ |
| 337 | channel_name = strtok(NULL, ","); |
| 338 | lttng_channel_destroy(channel); |
| 339 | channel = NULL; |
| 340 | } |
| 341 | |
| 342 | if (lttng_opt_mi) { |
| 343 | /* Close channels element */ |
| 344 | ret = mi_lttng_writer_close_element(writer); |
| 345 | if (ret) { |
| 346 | ret = CMD_ERROR; |
| 347 | goto error; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | ret = CMD_SUCCESS; |
| 352 | |
| 353 | error: |
| 354 | if (channel) { |
| 355 | lttng_channel_destroy(channel); |
| 356 | } |
| 357 | /* If more important error happen bypass the warning */ |
| 358 | if (!ret && warn) { |
| 359 | ret = CMD_WARNING; |
| 360 | } |
| 361 | /* If more important error happen bypass the warning */ |
| 362 | if (!ret && error) { |
| 363 | ret = CMD_ERROR; |
| 364 | } |
| 365 | |
| 366 | lttng_destroy_handle(handle); |
| 367 | |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Default value for channel configuration. |
| 373 | */ |
| 374 | static void init_channel_config(void) |
| 375 | { |
| 376 | /* |
| 377 | * Put -1 everywhere so we can identify those set by the command line and |
| 378 | * those needed to be set by the default values. |
| 379 | */ |
| 380 | memset(&chan_opts.attr, -1, sizeof(chan_opts.attr)); |
| 381 | chan_opts.attr.extended.ptr = NULL; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Add channel to trace session |
| 386 | */ |
| 387 | int cmd_enable_channels(int argc, const char **argv) |
| 388 | { |
| 389 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 390 | static poptContext pc; |
| 391 | char *session_name = NULL; |
| 392 | char *opt_arg = NULL; |
| 393 | const char *leftover = NULL; |
| 394 | |
| 395 | init_channel_config(); |
| 396 | |
| 397 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 398 | poptReadDefaultConfig(pc, 0); |
| 399 | |
| 400 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 401 | switch (opt) { |
| 402 | case OPT_HELP: |
| 403 | SHOW_HELP(); |
| 404 | goto end; |
| 405 | case OPT_DISCARD: |
| 406 | chan_opts.attr.overwrite = 0; |
| 407 | DBG("Channel set to discard"); |
| 408 | break; |
| 409 | case OPT_OVERWRITE: |
| 410 | chan_opts.attr.overwrite = 1; |
| 411 | DBG("Channel set to overwrite"); |
| 412 | break; |
| 413 | case OPT_SUBBUF_SIZE: |
| 414 | { |
| 415 | uint64_t rounded_size; |
| 416 | int order; |
| 417 | |
| 418 | /* Parse the size */ |
| 419 | opt_arg = poptGetOptArg(pc); |
| 420 | if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.subbuf_size) < 0 || !chan_opts.attr.subbuf_size) { |
| 421 | ERR("Wrong value in --subbuf-size parameter: %s", opt_arg); |
| 422 | ret = CMD_ERROR; |
| 423 | goto end; |
| 424 | } |
| 425 | |
| 426 | order = get_count_order_u64(chan_opts.attr.subbuf_size); |
| 427 | assert(order >= 0); |
| 428 | rounded_size = 1ULL << order; |
| 429 | if (rounded_size < chan_opts.attr.subbuf_size) { |
| 430 | ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!", |
| 431 | chan_opts.attr.subbuf_size); |
| 432 | ret = CMD_ERROR; |
| 433 | goto end; |
| 434 | } |
| 435 | |
| 436 | if (rounded_size != chan_opts.attr.subbuf_size) { |
| 437 | WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")", |
| 438 | chan_opts.attr.subbuf_size, rounded_size); |
| 439 | chan_opts.attr.subbuf_size = rounded_size; |
| 440 | } |
| 441 | |
| 442 | /* Should now be power of 2 */ |
| 443 | assert(!((chan_opts.attr.subbuf_size - 1) & chan_opts.attr.subbuf_size)); |
| 444 | |
| 445 | DBG("Channel subbuf size set to %" PRIu64, chan_opts.attr.subbuf_size); |
| 446 | break; |
| 447 | } |
| 448 | case OPT_NUM_SUBBUF: |
| 449 | { |
| 450 | uint64_t rounded_size; |
| 451 | int order; |
| 452 | |
| 453 | errno = 0; |
| 454 | opt_arg = poptGetOptArg(pc); |
| 455 | chan_opts.attr.num_subbuf = strtoull(opt_arg, NULL, 0); |
| 456 | if (errno != 0 || !chan_opts.attr.num_subbuf || !isdigit(opt_arg[0])) { |
| 457 | ERR("Wrong value in --num-subbuf parameter: %s", opt_arg); |
| 458 | ret = CMD_ERROR; |
| 459 | goto end; |
| 460 | } |
| 461 | |
| 462 | order = get_count_order_u64(chan_opts.attr.num_subbuf); |
| 463 | assert(order >= 0); |
| 464 | rounded_size = 1ULL << order; |
| 465 | if (rounded_size < chan_opts.attr.num_subbuf) { |
| 466 | ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!", |
| 467 | chan_opts.attr.num_subbuf); |
| 468 | ret = CMD_ERROR; |
| 469 | goto end; |
| 470 | } |
| 471 | |
| 472 | if (rounded_size != chan_opts.attr.num_subbuf) { |
| 473 | WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")", |
| 474 | chan_opts.attr.num_subbuf, rounded_size); |
| 475 | chan_opts.attr.num_subbuf = rounded_size; |
| 476 | } |
| 477 | |
| 478 | /* Should now be power of 2 */ |
| 479 | assert(!((chan_opts.attr.num_subbuf - 1) & chan_opts.attr.num_subbuf)); |
| 480 | |
| 481 | DBG("Channel subbuf num set to %" PRIu64, chan_opts.attr.num_subbuf); |
| 482 | break; |
| 483 | } |
| 484 | case OPT_SWITCH_TIMER: |
| 485 | { |
| 486 | uint64_t v; |
| 487 | |
| 488 | errno = 0; |
| 489 | opt_arg = poptGetOptArg(pc); |
| 490 | |
| 491 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
| 492 | ERR("Wrong value for --switch-timer parameter: %s", opt_arg); |
| 493 | ret = CMD_ERROR; |
| 494 | goto end; |
| 495 | } |
| 496 | |
| 497 | if (v != (uint32_t) v) { |
| 498 | ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg); |
| 499 | ret = CMD_ERROR; |
| 500 | goto end; |
| 501 | } |
| 502 | chan_opts.attr.switch_timer_interval = (uint32_t) v; |
| 503 | DBG("Channel switch timer interval set to %d %s", |
| 504 | chan_opts.attr.switch_timer_interval, |
| 505 | USEC_UNIT); |
| 506 | break; |
| 507 | } |
| 508 | case OPT_READ_TIMER: |
| 509 | { |
| 510 | uint64_t v; |
| 511 | |
| 512 | errno = 0; |
| 513 | opt_arg = poptGetOptArg(pc); |
| 514 | |
| 515 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
| 516 | ERR("Wrong value for --read-timer parameter: %s", opt_arg); |
| 517 | ret = CMD_ERROR; |
| 518 | goto end; |
| 519 | } |
| 520 | |
| 521 | if (v != (uint32_t) v) { |
| 522 | ERR("32-bit overflow in --read-timer parameter: %s", opt_arg); |
| 523 | ret = CMD_ERROR; |
| 524 | goto end; |
| 525 | } |
| 526 | chan_opts.attr.read_timer_interval = (uint32_t) v; |
| 527 | DBG("Channel read timer interval set to %d %s", |
| 528 | chan_opts.attr.read_timer_interval, |
| 529 | USEC_UNIT); |
| 530 | break; |
| 531 | } |
| 532 | case OPT_MONITOR_TIMER: |
| 533 | { |
| 534 | uint64_t v; |
| 535 | |
| 536 | errno = 0; |
| 537 | opt_arg = poptGetOptArg(pc); |
| 538 | |
| 539 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
| 540 | ERR("Wrong value for --monitor-timer parameter: %s", opt_arg); |
| 541 | ret = CMD_ERROR; |
| 542 | goto end; |
| 543 | } |
| 544 | opt_monitor_timer.interval = (uint64_t) v; |
| 545 | opt_monitor_timer.set = true; |
| 546 | DBG("Channel monitor timer interval set to %" PRIu64 " %s", |
| 547 | opt_monitor_timer.interval, |
| 548 | USEC_UNIT); |
| 549 | break; |
| 550 | } |
| 551 | case OPT_BLOCKING_TIMEOUT: |
| 552 | { |
| 553 | uint64_t v; |
| 554 | long long v_msec; |
| 555 | |
| 556 | errno = 0; |
| 557 | opt_arg = poptGetOptArg(pc); |
| 558 | |
| 559 | if (strcmp(opt_arg, "inf") == 0) { |
| 560 | opt_blocking_timeout.value = (int64_t) -1; |
| 561 | opt_blocking_timeout.set = true; |
| 562 | DBG("Channel blocking timeout set to infinity"); |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
| 567 | ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg); |
| 568 | ret = CMD_ERROR; |
| 569 | goto end; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * While LTTng-UST and LTTng-tools will accept a |
| 574 | * blocking timeout expressed in µs, the current |
| 575 | * tracer implementation relies on poll() which |
| 576 | * takes an "int timeout" parameter expressed in |
| 577 | * msec. |
| 578 | * |
| 579 | * Since the error reporting from the tracer is |
| 580 | * not precise, we perform this check here to |
| 581 | * provide a helpful error message in case of |
| 582 | * overflow. |
| 583 | * |
| 584 | * The setter (liblttng-ctl) also performs an |
| 585 | * equivalent check. |
| 586 | */ |
| 587 | v_msec = v / 1000; |
| 588 | if (v_msec != (int32_t) v_msec) { |
| 589 | ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s", opt_arg); |
| 590 | ret = CMD_ERROR; |
| 591 | goto end; |
| 592 | } |
| 593 | |
| 594 | opt_blocking_timeout.value = (int64_t) v; |
| 595 | opt_blocking_timeout.set = true; |
| 596 | DBG("Channel blocking timeout set to %" PRId64 " %s%s", |
| 597 | opt_blocking_timeout.value, |
| 598 | USEC_UNIT, |
| 599 | opt_blocking_timeout.value == 0 ? |
| 600 | " (non-blocking)" : ""); |
| 601 | break; |
| 602 | } |
| 603 | case OPT_USERSPACE: |
| 604 | opt_userspace = 1; |
| 605 | break; |
| 606 | case OPT_TRACEFILE_SIZE: |
| 607 | opt_arg = poptGetOptArg(pc); |
| 608 | if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.tracefile_size) < 0) { |
| 609 | ERR("Wrong value in --tracefile-size parameter: %s", opt_arg); |
| 610 | ret = CMD_ERROR; |
| 611 | goto end; |
| 612 | } |
| 613 | DBG("Maximum tracefile size set to %" PRIu64, |
| 614 | chan_opts.attr.tracefile_size); |
| 615 | break; |
| 616 | case OPT_TRACEFILE_COUNT: |
| 617 | { |
| 618 | unsigned long v; |
| 619 | |
| 620 | errno = 0; |
| 621 | opt_arg = poptGetOptArg(pc); |
| 622 | v = strtoul(opt_arg, NULL, 0); |
| 623 | if (errno != 0 || !isdigit(opt_arg[0])) { |
| 624 | ERR("Wrong value in --tracefile-count parameter: %s", opt_arg); |
| 625 | ret = CMD_ERROR; |
| 626 | goto end; |
| 627 | } |
| 628 | if (v != (uint32_t) v) { |
| 629 | ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg); |
| 630 | ret = CMD_ERROR; |
| 631 | goto end; |
| 632 | } |
| 633 | chan_opts.attr.tracefile_count = (uint32_t) v; |
| 634 | DBG("Maximum tracefile count set to %" PRIu64, |
| 635 | chan_opts.attr.tracefile_count); |
| 636 | break; |
| 637 | } |
| 638 | case OPT_LIST_OPTIONS: |
| 639 | list_cmd_options(stdout, long_options); |
| 640 | goto end; |
| 641 | default: |
| 642 | ret = CMD_UNDEFINED; |
| 643 | goto end; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | ret = print_missing_or_multiple_domains( |
| 648 | opt_kernel + opt_userspace, false); |
| 649 | if (ret) { |
| 650 | ret = CMD_ERROR; |
| 651 | goto end; |
| 652 | } |
| 653 | |
| 654 | if (chan_opts.attr.overwrite == 1 && opt_blocking_timeout.set && |
| 655 | opt_blocking_timeout.value != 0) { |
| 656 | ERR("You cannot specify --overwrite and --blocking-timeout=N, " |
| 657 | "where N is different than 0"); |
| 658 | ret = CMD_ERROR; |
| 659 | goto end; |
| 660 | } |
| 661 | |
| 662 | /* Mi check */ |
| 663 | if (lttng_opt_mi) { |
| 664 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 665 | if (!writer) { |
| 666 | ret = -LTTNG_ERR_NOMEM; |
| 667 | goto end; |
| 668 | } |
| 669 | |
| 670 | /* Open command element */ |
| 671 | ret = mi_lttng_writer_command_open(writer, |
| 672 | mi_lttng_element_command_enable_channels); |
| 673 | if (ret) { |
| 674 | ret = CMD_ERROR; |
| 675 | goto end; |
| 676 | } |
| 677 | |
| 678 | /* Open output element */ |
| 679 | ret = mi_lttng_writer_open_element(writer, |
| 680 | mi_lttng_element_command_output); |
| 681 | if (ret) { |
| 682 | ret = CMD_ERROR; |
| 683 | goto end; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | opt_channels = (char*) poptGetArg(pc); |
| 688 | if (opt_channels == NULL) { |
| 689 | ERR("Missing channel name.\n"); |
| 690 | ret = CMD_ERROR; |
| 691 | success = 0; |
| 692 | goto mi_closing; |
| 693 | } |
| 694 | |
| 695 | leftover = poptGetArg(pc); |
| 696 | if (leftover) { |
| 697 | ERR("Unknown argument: %s", leftover); |
| 698 | ret = CMD_ERROR; |
| 699 | success = 0; |
| 700 | goto mi_closing; |
| 701 | } |
| 702 | |
| 703 | if (!opt_session_name) { |
| 704 | session_name = get_session_name(); |
| 705 | if (session_name == NULL) { |
| 706 | command_ret = CMD_ERROR; |
| 707 | success = 0; |
| 708 | goto mi_closing; |
| 709 | } |
| 710 | } else { |
| 711 | session_name = opt_session_name; |
| 712 | } |
| 713 | |
| 714 | command_ret = enable_channel(session_name); |
| 715 | if (command_ret) { |
| 716 | success = 0; |
| 717 | } |
| 718 | |
| 719 | mi_closing: |
| 720 | /* Mi closing */ |
| 721 | if (lttng_opt_mi) { |
| 722 | /* Close output element */ |
| 723 | ret = mi_lttng_writer_close_element(writer); |
| 724 | if (ret) { |
| 725 | goto end; |
| 726 | } |
| 727 | |
| 728 | /* Success ? */ |
| 729 | ret = mi_lttng_writer_write_element_bool(writer, |
| 730 | mi_lttng_element_command_success, success); |
| 731 | if (ret) { |
| 732 | goto end; |
| 733 | } |
| 734 | |
| 735 | /* Command element close */ |
| 736 | ret = mi_lttng_writer_command_close(writer); |
| 737 | if (ret) { |
| 738 | goto end; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | end: |
| 743 | /* Mi clean-up */ |
| 744 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 745 | /* Preserve original error code */ |
| 746 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; |
| 747 | } |
| 748 | |
| 749 | if (!opt_session_name && session_name) { |
| 750 | free(session_name); |
| 751 | } |
| 752 | |
| 753 | /* Overwrite ret if an error occurred when enable_channel */ |
| 754 | ret = command_ret ? command_ret : ret; |
| 755 | poptFreeContext(pc); |
| 756 | return ret; |
| 757 | } |