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