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