sessiond: lttng: Add command to check kernel tracer status
[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/lttng-kernel.hpp>
15 #include <common/utils.hpp>
16
17 #include <lttng/domain-internal.hpp>
18
19 #include <ctype.h>
20 #include <inttypes.h>
21 #include <popt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
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, nullptr, OPT_HELP, nullptr, nullptr },
78 { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, nullptr, nullptr },
79 { "kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, nullptr, nullptr },
80 { "userspace", 'u', POPT_ARG_NONE, nullptr, OPT_USERSPACE, nullptr, nullptr },
81 { "discard", 0, POPT_ARG_NONE, nullptr, OPT_DISCARD, nullptr, nullptr },
82 { "overwrite", 0, POPT_ARG_NONE, nullptr, OPT_OVERWRITE, nullptr, nullptr },
83 { "subbuf-size", 0, POPT_ARG_STRING, nullptr, OPT_SUBBUF_SIZE, nullptr, nullptr },
84 { "num-subbuf", 0, POPT_ARG_INT, nullptr, OPT_NUM_SUBBUF, nullptr, nullptr },
85 { "switch-timer", 0, POPT_ARG_INT, nullptr, OPT_SWITCH_TIMER, nullptr, nullptr },
86 { "monitor-timer", 0, POPT_ARG_INT, nullptr, OPT_MONITOR_TIMER, nullptr, nullptr },
87 { "read-timer", 0, POPT_ARG_INT, nullptr, OPT_READ_TIMER, nullptr, nullptr },
88 { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
89 { "output", 0, POPT_ARG_STRING, &opt_output, 0, nullptr, nullptr },
90 { "buffers-uid", 0, POPT_ARG_VAL, &opt_buffer_uid, 1, nullptr, nullptr },
91 { "buffers-pid", 0, POPT_ARG_VAL, &opt_buffer_pid, 1, nullptr, nullptr },
92 { "buffers-global", 0, POPT_ARG_VAL, &opt_buffer_global, 1, nullptr, nullptr },
93 { "tracefile-size", 'C', POPT_ARG_INT, nullptr, OPT_TRACEFILE_SIZE, nullptr, nullptr },
94 { "tracefile-count", 'W', POPT_ARG_INT, nullptr, OPT_TRACEFILE_COUNT, nullptr, nullptr },
95 { "blocking-timeout", 0, POPT_ARG_INT, nullptr, OPT_BLOCKING_TIMEOUT, nullptr, nullptr },
96 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
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 = nullptr;
144 int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0;
145 enum lttng_kernel_tracer_status kernel_tracer_status;
146 char *channel_name;
147 struct lttng_domain dom;
148
149 memset(&dom, 0, sizeof(dom));
150
151 /* Validate options. */
152 if (opt_kernel) {
153 if (opt_blocking_timeout.set) {
154 ERR("Retry timeout option not supported for kernel domain (-k)");
155 ret = CMD_ERROR;
156 goto error;
157 }
158 }
159
160 /* Create lttng domain */
161 if (opt_kernel) {
162 dom.type = LTTNG_DOMAIN_KERNEL;
163 dom.buf_type = LTTNG_BUFFER_GLOBAL;
164 if (opt_buffer_uid || opt_buffer_pid) {
165 ERR("Buffer type not supported for domain -k");
166 ret = CMD_ERROR;
167 goto error;
168 }
169 } else if (opt_userspace) {
170 dom.type = LTTNG_DOMAIN_UST;
171 if (opt_buffer_pid) {
172 dom.buf_type = LTTNG_BUFFER_PER_PID;
173 } else {
174 if (opt_buffer_global) {
175 ERR("Buffer type not supported for domain -u");
176 ret = CMD_ERROR;
177 goto error;
178 }
179 dom.buf_type = LTTNG_BUFFER_PER_UID;
180 }
181 } else {
182 /* Checked by the caller. */
183 abort();
184 }
185
186 set_default_attr(&dom);
187
188 if (chan_opts.attr.tracefile_size == 0 && chan_opts.attr.tracefile_count) {
189 ERR("Missing option --tracefile-size. "
190 "A file count without a size won't do anything.");
191 ret = CMD_ERROR;
192 goto error;
193 }
194
195 if ((chan_opts.attr.tracefile_size > 0) &&
196 (chan_opts.attr.tracefile_size < chan_opts.attr.subbuf_size)) {
197 WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")",
198 chan_opts.attr.tracefile_size,
199 chan_opts.attr.subbuf_size);
200 chan_opts.attr.tracefile_size = chan_opts.attr.subbuf_size;
201 }
202
203 /* Setting channel output */
204 if (opt_output) {
205 if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) {
206 chan_opts.attr.output = LTTNG_EVENT_MMAP;
207 } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) {
208 chan_opts.attr.output = LTTNG_EVENT_SPLICE;
209 } else {
210 ERR("Unknown output type %s. Possible values are: %s, %s\n",
211 opt_output,
212 output_mmap,
213 output_splice);
214 ret = CMD_ERROR;
215 goto error;
216 }
217 }
218
219 handle = lttng_create_handle(session_name, &dom);
220 if (handle == nullptr) {
221 ret = -1;
222 goto error;
223 }
224
225 /* Mi open channels element */
226 if (lttng_opt_mi) {
227 LTTNG_ASSERT(writer);
228 ret = mi_lttng_channels_open(writer);
229 if (ret) {
230 ret = CMD_ERROR;
231 goto error;
232 }
233 }
234
235 /* Strip channel list (format: chan1,chan2,...) */
236 channel_name = strtok(channel_list, ",");
237 while (channel_name != nullptr) {
238 void *extended_ptr;
239
240 /* Validate channel name's length */
241 if (strlen(channel_name) >= sizeof(chan_opts.name)) {
242 ERR("Channel name is too long (max. %zu characters)",
243 sizeof(chan_opts.name) - 1);
244 error = 1;
245 goto skip_enable;
246 }
247
248 /*
249 * A dynamically-allocated channel is used in order to allow
250 * the configuration of extended attributes (post-2.9).
251 */
252 channel = lttng_channel_create(&dom);
253 if (!channel) {
254 ERR("Unable to create channel object");
255 error = 1;
256 goto error;
257 }
258
259 /* Copy channel name */
260 strcpy(channel->name, channel_name);
261 channel->enabled = 1;
262 extended_ptr = channel->attr.extended.ptr;
263 memcpy(&channel->attr, &chan_opts.attr, sizeof(chan_opts.attr));
264 channel->attr.extended.ptr = extended_ptr;
265 if (opt_monitor_timer.set) {
266 ret = lttng_channel_set_monitor_timer_interval(channel,
267 opt_monitor_timer.interval);
268 if (ret) {
269 ERR("Failed to set the channel's monitor timer interval");
270 error = 1;
271 goto error;
272 }
273 }
274 if (opt_blocking_timeout.set) {
275 ret = lttng_channel_set_blocking_timeout(channel,
276 opt_blocking_timeout.value);
277 if (ret) {
278 ERR("Failed to set the channel's blocking timeout");
279 error = 1;
280 goto error;
281 }
282 }
283
284 DBG("Enabling channel %s", channel_name);
285
286 ret = lttng_enable_channel(handle, channel);
287 if (ret < 0) {
288 success = 0;
289 switch (-ret) {
290 case LTTNG_ERR_KERN_CHAN_EXIST:
291 case LTTNG_ERR_UST_CHAN_EXIST:
292 case LTTNG_ERR_CHAN_EXIST:
293 WARN("Channel %s: %s (session %s)",
294 channel_name,
295 lttng_strerror(ret),
296 session_name);
297 warn = 1;
298 break;
299 case LTTNG_ERR_INVALID_CHANNEL_NAME:
300 ERR("Invalid channel name: \"%s\". "
301 "Channel names may not start with '.', and "
302 "may not contain '/'.",
303 channel_name);
304 error = 1;
305 break;
306 default:
307 ERR("Channel %s: %s (session %s)",
308 channel_name,
309 lttng_strerror(ret),
310 session_name);
311 error = 1;
312 break;
313 }
314 /*
315 * Ask the sessiond for the more details on the status of the kernel tracer.
316 */
317 ret = lttng_get_kernel_tracer_status(&kernel_tracer_status);
318 if (ret < 0) {
319 ERR("Failed to get kernel tracer status: %s", lttng_strerror(ret));
320 } else {
321 switch (kernel_tracer_status) {
322 case LTTNG_KERNEL_TRACER_STATUS_INITIALIZED:
323 break;
324 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN:
325 MSG("\tKernel module loading failed");
326 break;
327 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING:
328 MSG("\tMissing one or more required kernel modules");
329 break;
330 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE:
331 MSG("\tKernel module signature error prevented loading of one or more required kernel modules");
332 break;
333 case LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT:
334 MSG("\tlttng-sessiond isn't running as root");
335 break;
336 case LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER:
337 MSG("\tFailed to setup notifiers");
338 break;
339 case LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG:
340 MSG("\tlttng-sessiond failed to open proc lttng");
341 break;
342 case LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH:
343 MSG("\tVersion mismatch between kernel tracer and kernel tracer ABI");
344 break;
345 default:
346 MSG("\tUnknown kernel tracer status (%d)", kernel_tracer_status);
347 break;
348 }
349 MSG("\tConsult lttng-sessiond logs for more information");
350 }
351 } else {
352 MSG("%s channel %s enabled for session %s",
353 lttng_domain_type_str(dom.type),
354 channel_name,
355 session_name);
356 success = 1;
357 }
358
359 skip_enable:
360 if (lttng_opt_mi) {
361 /* Mi print the channel element and leave it open */
362 ret = mi_lttng_channel(writer, channel, 1);
363 if (ret) {
364 ret = CMD_ERROR;
365 goto error;
366 }
367
368 /* Individual Success ? */
369 ret = mi_lttng_writer_write_element_bool(
370 writer, mi_lttng_element_command_success, success);
371 if (ret) {
372 ret = CMD_ERROR;
373 goto error;
374 }
375
376 /* Close channel element */
377 ret = mi_lttng_writer_close_element(writer);
378 if (ret) {
379 ret = CMD_ERROR;
380 goto error;
381 }
382 }
383
384 /* Next channel */
385 channel_name = strtok(nullptr, ",");
386 lttng_channel_destroy(channel);
387 channel = nullptr;
388 }
389
390 if (lttng_opt_mi) {
391 /* Close channels element */
392 ret = mi_lttng_writer_close_element(writer);
393 if (ret) {
394 ret = CMD_ERROR;
395 goto error;
396 }
397 }
398
399 ret = CMD_SUCCESS;
400
401 error:
402 if (channel) {
403 lttng_channel_destroy(channel);
404 }
405 /* If more important error happen bypass the warning */
406 if (!ret && warn) {
407 ret = CMD_WARNING;
408 }
409 /* If more important error happen bypass the warning */
410 if (!ret && error) {
411 ret = CMD_ERROR;
412 }
413
414 lttng_destroy_handle(handle);
415
416 return ret;
417 }
418
419 /*
420 * Default value for channel configuration.
421 */
422 static void init_channel_config()
423 {
424 /*
425 * Put -1 everywhere so we can identify those set by the command line and
426 * those needed to be set by the default values.
427 */
428 memset(&chan_opts.attr, -1, sizeof(chan_opts.attr));
429 chan_opts.attr.extended.ptr = nullptr;
430 }
431
432 /*
433 * Add channel to trace session
434 */
435 int cmd_enable_channels(int argc, const char **argv)
436 {
437 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
438 static poptContext pc;
439 char *session_name = nullptr;
440 char *channel_list = nullptr;
441 char *opt_arg = nullptr;
442 const char *arg_channel_list = nullptr;
443 const char *leftover = nullptr;
444
445 init_channel_config();
446
447 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
448 poptReadDefaultConfig(pc, 0);
449
450 while ((opt = poptGetNextOpt(pc)) != -1) {
451 switch (opt) {
452 case OPT_HELP:
453 SHOW_HELP();
454 goto end;
455 case OPT_DISCARD:
456 chan_opts.attr.overwrite = 0;
457 DBG("Channel set to discard");
458 break;
459 case OPT_OVERWRITE:
460 chan_opts.attr.overwrite = 1;
461 DBG("Channel set to overwrite");
462 break;
463 case OPT_SUBBUF_SIZE:
464 {
465 uint64_t rounded_size;
466 int order;
467
468 /* Parse the size */
469 opt_arg = poptGetOptArg(pc);
470 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.subbuf_size) < 0 ||
471 !chan_opts.attr.subbuf_size) {
472 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg);
473 ret = CMD_ERROR;
474 goto end;
475 }
476
477 order = get_count_order_u64(chan_opts.attr.subbuf_size);
478 LTTNG_ASSERT(order >= 0);
479 rounded_size = 1ULL << order;
480 if (rounded_size < chan_opts.attr.subbuf_size) {
481 ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!",
482 chan_opts.attr.subbuf_size);
483 ret = CMD_ERROR;
484 goto end;
485 }
486
487 if (rounded_size != chan_opts.attr.subbuf_size) {
488 WARN("The subbuf size (%" PRIu64
489 ") is rounded to the next power of 2 (%" PRIu64 ")",
490 chan_opts.attr.subbuf_size,
491 rounded_size);
492 chan_opts.attr.subbuf_size = rounded_size;
493 }
494
495 /* Should now be power of 2 */
496 LTTNG_ASSERT(
497 !((chan_opts.attr.subbuf_size - 1) & chan_opts.attr.subbuf_size));
498
499 DBG("Channel subbuf size set to %" PRIu64, chan_opts.attr.subbuf_size);
500 break;
501 }
502 case OPT_NUM_SUBBUF:
503 {
504 uint64_t rounded_size;
505 int order;
506
507 errno = 0;
508 opt_arg = poptGetOptArg(pc);
509 chan_opts.attr.num_subbuf = strtoull(opt_arg, nullptr, 0);
510 if (errno != 0 || !chan_opts.attr.num_subbuf || !isdigit(opt_arg[0])) {
511 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg);
512 ret = CMD_ERROR;
513 goto end;
514 }
515
516 order = get_count_order_u64(chan_opts.attr.num_subbuf);
517 LTTNG_ASSERT(order >= 0);
518 rounded_size = 1ULL << order;
519 if (rounded_size < chan_opts.attr.num_subbuf) {
520 ERR("The number of subbuffers (%" PRIu64
521 ") is rounded and overflows!",
522 chan_opts.attr.num_subbuf);
523 ret = CMD_ERROR;
524 goto end;
525 }
526
527 if (rounded_size != chan_opts.attr.num_subbuf) {
528 WARN("The number of subbuffers (%" PRIu64
529 ") is rounded to the next power of 2 (%" PRIu64 ")",
530 chan_opts.attr.num_subbuf,
531 rounded_size);
532 chan_opts.attr.num_subbuf = rounded_size;
533 }
534
535 /* Should now be power of 2 */
536 LTTNG_ASSERT(
537 !((chan_opts.attr.num_subbuf - 1) & chan_opts.attr.num_subbuf));
538
539 DBG("Channel subbuf num set to %" PRIu64, chan_opts.attr.num_subbuf);
540 break;
541 }
542 case OPT_SWITCH_TIMER:
543 {
544 uint64_t v;
545
546 errno = 0;
547 opt_arg = poptGetOptArg(pc);
548
549 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
550 ERR("Wrong value for --switch-timer parameter: %s", opt_arg);
551 ret = CMD_ERROR;
552 goto end;
553 }
554
555 if (v != (uint32_t) v) {
556 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg);
557 ret = CMD_ERROR;
558 goto end;
559 }
560 chan_opts.attr.switch_timer_interval = (uint32_t) v;
561 DBG("Channel switch timer interval set to %d %s",
562 chan_opts.attr.switch_timer_interval,
563 USEC_UNIT);
564 break;
565 }
566 case OPT_READ_TIMER:
567 {
568 uint64_t v;
569
570 errno = 0;
571 opt_arg = poptGetOptArg(pc);
572
573 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
574 ERR("Wrong value for --read-timer parameter: %s", opt_arg);
575 ret = CMD_ERROR;
576 goto end;
577 }
578
579 if (v != (uint32_t) v) {
580 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg);
581 ret = CMD_ERROR;
582 goto end;
583 }
584 chan_opts.attr.read_timer_interval = (uint32_t) v;
585 DBG("Channel read timer interval set to %d %s",
586 chan_opts.attr.read_timer_interval,
587 USEC_UNIT);
588 break;
589 }
590 case OPT_MONITOR_TIMER:
591 {
592 uint64_t v;
593
594 errno = 0;
595 opt_arg = poptGetOptArg(pc);
596
597 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
598 ERR("Wrong value for --monitor-timer parameter: %s", opt_arg);
599 ret = CMD_ERROR;
600 goto end;
601 }
602 opt_monitor_timer.interval = (uint64_t) v;
603 opt_monitor_timer.set = true;
604 DBG("Channel monitor timer interval set to %" PRIu64 " %s",
605 opt_monitor_timer.interval,
606 USEC_UNIT);
607 break;
608 }
609 case OPT_BLOCKING_TIMEOUT:
610 {
611 uint64_t v;
612 long long v_msec;
613
614 errno = 0;
615 opt_arg = poptGetOptArg(pc);
616
617 if (strcmp(opt_arg, "inf") == 0) {
618 opt_blocking_timeout.value = (int64_t) -1;
619 opt_blocking_timeout.set = true;
620 DBG("Channel blocking timeout set to infinity");
621 break;
622 }
623
624 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
625 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg);
626 ret = CMD_ERROR;
627 goto end;
628 }
629
630 /*
631 * While LTTng-UST and LTTng-tools will accept a
632 * blocking timeout expressed in µs, the current
633 * tracer implementation relies on poll() which
634 * takes an "int timeout" parameter expressed in
635 * msec.
636 *
637 * Since the error reporting from the tracer is
638 * not precise, we perform this check here to
639 * provide a helpful error message in case of
640 * overflow.
641 *
642 * The setter (liblttng-ctl) also performs an
643 * equivalent check.
644 */
645 v_msec = v / 1000;
646 if (v_msec != (int32_t) v_msec) {
647 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s",
648 opt_arg);
649 ret = CMD_ERROR;
650 goto end;
651 }
652
653 opt_blocking_timeout.value = (int64_t) v;
654 opt_blocking_timeout.set = true;
655 DBG("Channel blocking timeout set to %" PRId64 " %s%s",
656 opt_blocking_timeout.value,
657 USEC_UNIT,
658 opt_blocking_timeout.value == 0 ? " (non-blocking)" : "");
659 break;
660 }
661 case OPT_USERSPACE:
662 opt_userspace = 1;
663 break;
664 case OPT_TRACEFILE_SIZE:
665 opt_arg = poptGetOptArg(pc);
666 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.tracefile_size) < 0) {
667 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg);
668 ret = CMD_ERROR;
669 goto end;
670 }
671 DBG("Maximum tracefile size set to %" PRIu64,
672 chan_opts.attr.tracefile_size);
673 break;
674 case OPT_TRACEFILE_COUNT:
675 {
676 unsigned long v;
677
678 errno = 0;
679 opt_arg = poptGetOptArg(pc);
680 v = strtoul(opt_arg, nullptr, 0);
681 if (errno != 0 || !isdigit(opt_arg[0])) {
682 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg);
683 ret = CMD_ERROR;
684 goto end;
685 }
686 if (v != (uint32_t) v) {
687 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg);
688 ret = CMD_ERROR;
689 goto end;
690 }
691 chan_opts.attr.tracefile_count = (uint32_t) v;
692 DBG("Maximum tracefile count set to %" PRIu64,
693 chan_opts.attr.tracefile_count);
694 break;
695 }
696 case OPT_LIST_OPTIONS:
697 list_cmd_options(stdout, long_options);
698 goto end;
699 default:
700 ret = CMD_UNDEFINED;
701 goto end;
702 }
703
704 if (opt_arg) {
705 free(opt_arg);
706 opt_arg = nullptr;
707 }
708 }
709
710 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace, false);
711 if (ret) {
712 ret = CMD_ERROR;
713 goto end;
714 }
715
716 if (chan_opts.attr.overwrite == 1 && opt_blocking_timeout.set &&
717 opt_blocking_timeout.value != 0) {
718 ERR("You cannot specify --overwrite and --blocking-timeout=N, "
719 "where N is different than 0");
720 ret = CMD_ERROR;
721 goto end;
722 }
723
724 /* Mi check */
725 if (lttng_opt_mi) {
726 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
727 if (!writer) {
728 ret = -LTTNG_ERR_NOMEM;
729 goto end;
730 }
731
732 /* Open command element */
733 ret = mi_lttng_writer_command_open(writer,
734 mi_lttng_element_command_enable_channels);
735 if (ret) {
736 ret = CMD_ERROR;
737 goto end;
738 }
739
740 /* Open output element */
741 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output);
742 if (ret) {
743 ret = CMD_ERROR;
744 goto end;
745 }
746 }
747
748 arg_channel_list = poptGetArg(pc);
749 if (arg_channel_list == nullptr) {
750 ERR("Missing channel name.");
751 ret = CMD_ERROR;
752 success = 0;
753 goto mi_closing;
754 }
755
756 channel_list = strdup(arg_channel_list);
757 if (channel_list == nullptr) {
758 PERROR("Failed to copy channel name");
759 ret = CMD_ERROR;
760 success = 0;
761 goto mi_closing;
762 }
763
764 leftover = poptGetArg(pc);
765 if (leftover) {
766 ERR("Unknown argument: %s", leftover);
767 ret = CMD_ERROR;
768 success = 0;
769 goto mi_closing;
770 }
771
772 if (!opt_session_name) {
773 session_name = get_session_name();
774 if (session_name == nullptr) {
775 command_ret = CMD_ERROR;
776 success = 0;
777 goto mi_closing;
778 }
779 } else {
780 session_name = opt_session_name;
781 }
782
783 command_ret = enable_channel(session_name, channel_list);
784 if (command_ret) {
785 success = 0;
786 }
787
788 mi_closing:
789 /* Mi closing */
790 if (lttng_opt_mi) {
791 /* Close output element */
792 ret = mi_lttng_writer_close_element(writer);
793 if (ret) {
794 goto end;
795 }
796
797 /* Success ? */
798 ret = mi_lttng_writer_write_element_bool(
799 writer, mi_lttng_element_command_success, success);
800 if (ret) {
801 goto end;
802 }
803
804 /* Command element close */
805 ret = mi_lttng_writer_command_close(writer);
806 if (ret) {
807 goto end;
808 }
809 }
810
811 end:
812 /* Mi clean-up */
813 if (writer && mi_lttng_writer_destroy(writer)) {
814 /* Preserve original error code */
815 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
816 }
817
818 if (!opt_session_name && session_name) {
819 free(session_name);
820 }
821
822 free(channel_list);
823
824 /* Overwrite ret if an error occurred when enable_channel */
825 ret = command_ret ? command_ret : ret;
826 poptFreeContext(pc);
827 free(opt_arg);
828 return ret;
829 }
This page took 0.04626 seconds and 4 git commands to generate.