64317190978882cbc07d0135a695e725dfb1aaf0
[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 <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 <ctype.h>
18
19 #include <common/sessiond-comm/sessiond-comm.hpp>
20 #include <common/utils.hpp>
21 #include <common/mi-lttng.hpp>
22
23 #include <lttng/domain-internal.hpp>
24
25 #include "../command.hpp"
26 #include "../utils.hpp"
27
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, 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, char *channel_list)
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 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, 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 LTTNG_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(channel_list, ",");
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 lttng_domain_type_str(dom.type),
308 channel_name, session_name);
309 success = 1;
310 }
311
312 skip_enable:
313 if (lttng_opt_mi) {
314 /* Mi print the channel element and leave it open */
315 ret = mi_lttng_channel(writer, channel, 1);
316 if (ret) {
317 ret = CMD_ERROR;
318 goto error;
319 }
320
321 /* Individual Success ? */
322 ret = mi_lttng_writer_write_element_bool(writer,
323 mi_lttng_element_command_success, success);
324 if (ret) {
325 ret = CMD_ERROR;
326 goto error;
327 }
328
329 /* Close channel element */
330 ret = mi_lttng_writer_close_element(writer);
331 if (ret) {
332 ret = CMD_ERROR;
333 goto error;
334 }
335 }
336
337 /* Next channel */
338 channel_name = strtok(NULL, ",");
339 lttng_channel_destroy(channel);
340 channel = NULL;
341 }
342
343 if (lttng_opt_mi) {
344 /* Close channels element */
345 ret = mi_lttng_writer_close_element(writer);
346 if (ret) {
347 ret = CMD_ERROR;
348 goto error;
349 }
350 }
351
352 ret = CMD_SUCCESS;
353
354 error:
355 if (channel) {
356 lttng_channel_destroy(channel);
357 }
358 /* If more important error happen bypass the warning */
359 if (!ret && warn) {
360 ret = CMD_WARNING;
361 }
362 /* If more important error happen bypass the warning */
363 if (!ret && error) {
364 ret = CMD_ERROR;
365 }
366
367 lttng_destroy_handle(handle);
368
369 return ret;
370 }
371
372 /*
373 * Default value for channel configuration.
374 */
375 static void init_channel_config(void)
376 {
377 /*
378 * Put -1 everywhere so we can identify those set by the command line and
379 * those needed to be set by the default values.
380 */
381 memset(&chan_opts.attr, -1, sizeof(chan_opts.attr));
382 chan_opts.attr.extended.ptr = NULL;
383 }
384
385 /*
386 * Add channel to trace session
387 */
388 int cmd_enable_channels(int argc, const char **argv)
389 {
390 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
391 static poptContext pc;
392 char *session_name = NULL;
393 char *channel_list = NULL;
394 char *opt_arg = NULL;
395 const char *arg_channel_list = NULL;
396 const char *leftover = NULL;
397
398 init_channel_config();
399
400 pc = poptGetContext(NULL, argc, argv, long_options, 0);
401 poptReadDefaultConfig(pc, 0);
402
403 while ((opt = poptGetNextOpt(pc)) != -1) {
404 switch (opt) {
405 case OPT_HELP:
406 SHOW_HELP();
407 goto end;
408 case OPT_DISCARD:
409 chan_opts.attr.overwrite = 0;
410 DBG("Channel set to discard");
411 break;
412 case OPT_OVERWRITE:
413 chan_opts.attr.overwrite = 1;
414 DBG("Channel set to overwrite");
415 break;
416 case OPT_SUBBUF_SIZE:
417 {
418 uint64_t rounded_size;
419 int order;
420
421 /* Parse the size */
422 opt_arg = poptGetOptArg(pc);
423 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.subbuf_size) < 0 || !chan_opts.attr.subbuf_size) {
424 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg);
425 ret = CMD_ERROR;
426 goto end;
427 }
428
429 order = get_count_order_u64(chan_opts.attr.subbuf_size);
430 LTTNG_ASSERT(order >= 0);
431 rounded_size = 1ULL << order;
432 if (rounded_size < chan_opts.attr.subbuf_size) {
433 ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!",
434 chan_opts.attr.subbuf_size);
435 ret = CMD_ERROR;
436 goto end;
437 }
438
439 if (rounded_size != chan_opts.attr.subbuf_size) {
440 WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
441 chan_opts.attr.subbuf_size, rounded_size);
442 chan_opts.attr.subbuf_size = rounded_size;
443 }
444
445 /* Should now be power of 2 */
446 LTTNG_ASSERT(!((chan_opts.attr.subbuf_size - 1) & chan_opts.attr.subbuf_size));
447
448 DBG("Channel subbuf size set to %" PRIu64, chan_opts.attr.subbuf_size);
449 break;
450 }
451 case OPT_NUM_SUBBUF:
452 {
453 uint64_t rounded_size;
454 int order;
455
456 errno = 0;
457 opt_arg = poptGetOptArg(pc);
458 chan_opts.attr.num_subbuf = strtoull(opt_arg, NULL, 0);
459 if (errno != 0 || !chan_opts.attr.num_subbuf || !isdigit(opt_arg[0])) {
460 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg);
461 ret = CMD_ERROR;
462 goto end;
463 }
464
465 order = get_count_order_u64(chan_opts.attr.num_subbuf);
466 LTTNG_ASSERT(order >= 0);
467 rounded_size = 1ULL << order;
468 if (rounded_size < chan_opts.attr.num_subbuf) {
469 ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!",
470 chan_opts.attr.num_subbuf);
471 ret = CMD_ERROR;
472 goto end;
473 }
474
475 if (rounded_size != chan_opts.attr.num_subbuf) {
476 WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
477 chan_opts.attr.num_subbuf, rounded_size);
478 chan_opts.attr.num_subbuf = rounded_size;
479 }
480
481 /* Should now be power of 2 */
482 LTTNG_ASSERT(!((chan_opts.attr.num_subbuf - 1) & chan_opts.attr.num_subbuf));
483
484 DBG("Channel subbuf num set to %" PRIu64, chan_opts.attr.num_subbuf);
485 break;
486 }
487 case OPT_SWITCH_TIMER:
488 {
489 uint64_t v;
490
491 errno = 0;
492 opt_arg = poptGetOptArg(pc);
493
494 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
495 ERR("Wrong value for --switch-timer parameter: %s", opt_arg);
496 ret = CMD_ERROR;
497 goto end;
498 }
499
500 if (v != (uint32_t) v) {
501 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg);
502 ret = CMD_ERROR;
503 goto end;
504 }
505 chan_opts.attr.switch_timer_interval = (uint32_t) v;
506 DBG("Channel switch timer interval set to %d %s",
507 chan_opts.attr.switch_timer_interval,
508 USEC_UNIT);
509 break;
510 }
511 case OPT_READ_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 --read-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 --read-timer parameter: %s", opt_arg);
526 ret = CMD_ERROR;
527 goto end;
528 }
529 chan_opts.attr.read_timer_interval = (uint32_t) v;
530 DBG("Channel read timer interval set to %d %s",
531 chan_opts.attr.read_timer_interval,
532 USEC_UNIT);
533 break;
534 }
535 case OPT_MONITOR_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 --monitor-timer parameter: %s", opt_arg);
544 ret = CMD_ERROR;
545 goto end;
546 }
547 opt_monitor_timer.interval = (uint64_t) v;
548 opt_monitor_timer.set = true;
549 DBG("Channel monitor timer interval set to %" PRIu64 " %s",
550 opt_monitor_timer.interval,
551 USEC_UNIT);
552 break;
553 }
554 case OPT_BLOCKING_TIMEOUT:
555 {
556 uint64_t v;
557 long long v_msec;
558
559 errno = 0;
560 opt_arg = poptGetOptArg(pc);
561
562 if (strcmp(opt_arg, "inf") == 0) {
563 opt_blocking_timeout.value = (int64_t) -1;
564 opt_blocking_timeout.set = true;
565 DBG("Channel blocking timeout set to infinity");
566 break;
567 }
568
569 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
570 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg);
571 ret = CMD_ERROR;
572 goto end;
573 }
574
575 /*
576 * While LTTng-UST and LTTng-tools will accept a
577 * blocking timeout expressed in µs, the current
578 * tracer implementation relies on poll() which
579 * takes an "int timeout" parameter expressed in
580 * msec.
581 *
582 * Since the error reporting from the tracer is
583 * not precise, we perform this check here to
584 * provide a helpful error message in case of
585 * overflow.
586 *
587 * The setter (liblttng-ctl) also performs an
588 * equivalent check.
589 */
590 v_msec = v / 1000;
591 if (v_msec != (int32_t) v_msec) {
592 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s", opt_arg);
593 ret = CMD_ERROR;
594 goto end;
595 }
596
597 opt_blocking_timeout.value = (int64_t) v;
598 opt_blocking_timeout.set = true;
599 DBG("Channel blocking timeout set to %" PRId64 " %s%s",
600 opt_blocking_timeout.value,
601 USEC_UNIT,
602 opt_blocking_timeout.value == 0 ?
603 " (non-blocking)" : "");
604 break;
605 }
606 case OPT_USERSPACE:
607 opt_userspace = 1;
608 break;
609 case OPT_TRACEFILE_SIZE:
610 opt_arg = poptGetOptArg(pc);
611 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.tracefile_size) < 0) {
612 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg);
613 ret = CMD_ERROR;
614 goto end;
615 }
616 DBG("Maximum tracefile size set to %" PRIu64,
617 chan_opts.attr.tracefile_size);
618 break;
619 case OPT_TRACEFILE_COUNT:
620 {
621 unsigned long v;
622
623 errno = 0;
624 opt_arg = poptGetOptArg(pc);
625 v = strtoul(opt_arg, NULL, 0);
626 if (errno != 0 || !isdigit(opt_arg[0])) {
627 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg);
628 ret = CMD_ERROR;
629 goto end;
630 }
631 if (v != (uint32_t) v) {
632 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg);
633 ret = CMD_ERROR;
634 goto end;
635 }
636 chan_opts.attr.tracefile_count = (uint32_t) v;
637 DBG("Maximum tracefile count set to %" PRIu64,
638 chan_opts.attr.tracefile_count);
639 break;
640 }
641 case OPT_LIST_OPTIONS:
642 list_cmd_options(stdout, long_options);
643 goto end;
644 default:
645 ret = CMD_UNDEFINED;
646 goto end;
647 }
648
649 if (opt_arg) {
650 free(opt_arg);
651 opt_arg = nullptr;
652 }
653 }
654
655 ret = print_missing_or_multiple_domains(
656 opt_kernel + opt_userspace, false);
657 if (ret) {
658 ret = CMD_ERROR;
659 goto end;
660 }
661
662 if (chan_opts.attr.overwrite == 1 && opt_blocking_timeout.set &&
663 opt_blocking_timeout.value != 0) {
664 ERR("You cannot specify --overwrite and --blocking-timeout=N, "
665 "where N is different than 0");
666 ret = CMD_ERROR;
667 goto end;
668 }
669
670 /* Mi check */
671 if (lttng_opt_mi) {
672 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
673 if (!writer) {
674 ret = -LTTNG_ERR_NOMEM;
675 goto end;
676 }
677
678 /* Open command element */
679 ret = mi_lttng_writer_command_open(writer,
680 mi_lttng_element_command_enable_channels);
681 if (ret) {
682 ret = CMD_ERROR;
683 goto end;
684 }
685
686 /* Open output element */
687 ret = mi_lttng_writer_open_element(writer,
688 mi_lttng_element_command_output);
689 if (ret) {
690 ret = CMD_ERROR;
691 goto end;
692 }
693 }
694
695 arg_channel_list = poptGetArg(pc);
696 if (arg_channel_list == NULL) {
697 ERR("Missing channel name.");
698 ret = CMD_ERROR;
699 success = 0;
700 goto mi_closing;
701 }
702
703 channel_list = strdup(arg_channel_list);
704 if (channel_list == NULL) {
705 PERROR("Failed to copy channel name");
706 ret = CMD_ERROR;
707 success = 0;
708 goto mi_closing;
709 }
710
711 leftover = poptGetArg(pc);
712 if (leftover) {
713 ERR("Unknown argument: %s", leftover);
714 ret = CMD_ERROR;
715 success = 0;
716 goto mi_closing;
717 }
718
719 if (!opt_session_name) {
720 session_name = get_session_name();
721 if (session_name == NULL) {
722 command_ret = CMD_ERROR;
723 success = 0;
724 goto mi_closing;
725 }
726 } else {
727 session_name = opt_session_name;
728 }
729
730 command_ret = enable_channel(session_name, channel_list);
731 if (command_ret) {
732 success = 0;
733 }
734
735 mi_closing:
736 /* Mi closing */
737 if (lttng_opt_mi) {
738 /* Close output element */
739 ret = mi_lttng_writer_close_element(writer);
740 if (ret) {
741 goto end;
742 }
743
744 /* Success ? */
745 ret = mi_lttng_writer_write_element_bool(writer,
746 mi_lttng_element_command_success, success);
747 if (ret) {
748 goto end;
749 }
750
751 /* Command element close */
752 ret = mi_lttng_writer_command_close(writer);
753 if (ret) {
754 goto end;
755 }
756 }
757
758 end:
759 /* Mi clean-up */
760 if (writer && mi_lttng_writer_destroy(writer)) {
761 /* Preserve original error code */
762 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
763 }
764
765 if (!opt_session_name && session_name) {
766 free(session_name);
767 }
768
769 free(channel_list);
770
771 /* Overwrite ret if an error occurred when enable_channel */
772 ret = command_ret ? command_ret : ret;
773 poptFreeContext(pc);
774 free(opt_arg);
775 return ret;
776 }
This page took 0.044587 seconds and 4 git commands to generate.