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