sessiond: lttng: Add command to check kernel tracer status
[lttng-tools.git] / src / bin / lttng / commands / enable_channels.cpp
CommitLineData
d36b8583 1/*
21cf9b6b 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
28ab034a
JG
9#include "../command.hpp"
10#include "../utils.hpp"
11
12#include <common/mi-lttng.hpp>
13#include <common/sessiond-comm/sessiond-comm.hpp>
49cddecd 14#include <common/lttng-kernel.hpp>
28ab034a
JG
15#include <common/utils.hpp>
16
17#include <lttng/domain-internal.hpp>
18
19#include <ctype.h>
20#include <inttypes.h>
d36b8583
DG
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>
42224349 28
cf0bcb51 29static struct lttng_channel chan_opts;
5edd7e09 30static int opt_kernel;
5440dc42 31static char *opt_session_name;
d36b8583 32static int opt_userspace;
a79d84dd 33static char *opt_output;
7972aab2
DG
34static int opt_buffer_uid;
35static int opt_buffer_pid;
36static int opt_buffer_global;
cf0bcb51
JG
37static struct {
38 bool set;
8193ef17 39 uint64_t interval;
cf0bcb51 40} opt_monitor_timer;
491d1539
MD
41static struct {
42 bool set;
43 int64_t value;
44} opt_blocking_timeout;
d36b8583 45
acc09215
JRJ
46static struct mi_writer *writer;
47
4fc83d94
PP
48#ifdef LTTNG_EMBED_HELP
49static const char help_msg[] =
50#include <lttng-enable-channel.1.h>
28ab034a 51 ;
4fc83d94
PP
52#endif
53
d36b8583
DG
54enum {
55 OPT_HELP = 1,
7d29a247
DG
56 OPT_DISCARD,
57 OPT_OVERWRITE,
58 OPT_SUBBUF_SIZE,
59 OPT_NUM_SUBBUF,
60 OPT_SWITCH_TIMER,
cf0bcb51 61 OPT_MONITOR_TIMER,
7d29a247 62 OPT_READ_TIMER,
d36b8583 63 OPT_USERSPACE,
679b4943 64 OPT_LIST_OPTIONS,
1624d5b7
JD
65 OPT_TRACEFILE_SIZE,
66 OPT_TRACEFILE_COUNT,
491d1539 67 OPT_BLOCKING_TIMEOUT,
d36b8583
DG
68};
69
cd80958d
DG
70static struct lttng_handle *handle;
71
a79d84dd
DG
72const char *output_mmap = "mmap";
73const char *output_splice = "splice";
74
d36b8583
DG
75static struct poptOption long_options[] = {
76 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
cd9adb8b
JG
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 }
d36b8583
DG
97};
98
5edd7e09
DG
99/*
100 * Set default attributes depending on those already defined from the command
101 * line.
102 */
103static void set_default_attr(struct lttng_domain *dom)
104{
105 struct lttng_channel_attr default_attr;
106
c1e4642c
JG
107 memset(&default_attr, 0, sizeof(default_attr));
108
5edd7e09
DG
109 /* Set attributes */
110 lttng_channel_set_default_attr(dom, &default_attr);
111
cf0bcb51
JG
112 if (chan_opts.attr.overwrite == -1) {
113 chan_opts.attr.overwrite = default_attr.overwrite;
5edd7e09 114 }
cf0bcb51
JG
115 if (chan_opts.attr.subbuf_size == -1) {
116 chan_opts.attr.subbuf_size = default_attr.subbuf_size;
5edd7e09 117 }
cf0bcb51
JG
118 if (chan_opts.attr.num_subbuf == -1) {
119 chan_opts.attr.num_subbuf = default_attr.num_subbuf;
5edd7e09 120 }
cf0bcb51
JG
121 if (chan_opts.attr.switch_timer_interval == -1) {
122 chan_opts.attr.switch_timer_interval = default_attr.switch_timer_interval;
5edd7e09 123 }
cf0bcb51
JG
124 if (chan_opts.attr.read_timer_interval == -1) {
125 chan_opts.attr.read_timer_interval = default_attr.read_timer_interval;
5edd7e09 126 }
cf0bcb51
JG
127 if ((int) chan_opts.attr.output == -1) {
128 chan_opts.attr.output = default_attr.output;
5edd7e09 129 }
cf0bcb51
JG
130 if (chan_opts.attr.tracefile_count == -1) {
131 chan_opts.attr.tracefile_count = default_attr.tracefile_count;
1624d5b7 132 }
cf0bcb51
JG
133 if (chan_opts.attr.tracefile_size == -1) {
134 chan_opts.attr.tracefile_size = default_attr.tracefile_size;
1624d5b7 135 }
5edd7e09
DG
136}
137
d36b8583 138/*
0fdd1e2c 139 * Adding channel using the lttng API.
d36b8583 140 */
5b915816 141static int enable_channel(char *session_name, char *channel_list)
d36b8583 142{
cd9adb8b 143 struct lttng_channel *channel = nullptr;
acc09215 144 int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0;
49cddecd 145 enum lttng_kernel_tracer_status kernel_tracer_status;
d36b8583 146 char *channel_name;
7d29a247 147 struct lttng_domain dom;
d36b8583 148
441c16a7
MD
149 memset(&dom, 0, sizeof(dom));
150
491d1539
MD
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
d78d6610 160 /* Create lttng domain */
7d29a247
DG
161 if (opt_kernel) {
162 dom.type = LTTNG_DOMAIN_KERNEL;
7972aab2 163 dom.buf_type = LTTNG_BUFFER_GLOBAL;
88c5f0d8
DG
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 }
d78d6610 169 } else if (opt_userspace) {
f6a9efaa 170 dom.type = LTTNG_DOMAIN_UST;
8692d4e5
DG
171 if (opt_buffer_pid) {
172 dom.buf_type = LTTNG_BUFFER_PER_PID;
7972aab2 173 } else {
88c5f0d8
DG
174 if (opt_buffer_global) {
175 ERR("Buffer type not supported for domain -u");
176 ret = CMD_ERROR;
177 goto error;
178 }
8692d4e5 179 dom.buf_type = LTTNG_BUFFER_PER_UID;
7972aab2 180 }
0fdd1e2c 181 } else {
3ecec76a 182 /* Checked by the caller. */
a0377dfe 183 abort();
7d29a247
DG
184 }
185
5edd7e09
DG
186 set_default_attr(&dom);
187
cf0bcb51 188 if (chan_opts.attr.tracefile_size == 0 && chan_opts.attr.tracefile_count) {
d16dee89 189 ERR("Missing option --tracefile-size. "
28ab034a 190 "A file count without a size won't do anything.");
d16dee89
DG
191 ret = CMD_ERROR;
192 goto error;
193 }
194
cf0bcb51 195 if ((chan_opts.attr.tracefile_size > 0) &&
28ab034a 196 (chan_opts.attr.tracefile_size < chan_opts.attr.subbuf_size)) {
de65565a 197 WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")",
28ab034a
JG
198 chan_opts.attr.tracefile_size,
199 chan_opts.attr.subbuf_size);
cf0bcb51 200 chan_opts.attr.tracefile_size = chan_opts.attr.subbuf_size;
1624d5b7
JD
201 }
202
a79d84dd
DG
203 /* Setting channel output */
204 if (opt_output) {
205 if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) {
cf0bcb51 206 chan_opts.attr.output = LTTNG_EVENT_MMAP;
a79d84dd 207 } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) {
cf0bcb51 208 chan_opts.attr.output = LTTNG_EVENT_SPLICE;
a79d84dd
DG
209 } else {
210 ERR("Unknown output type %s. Possible values are: %s, %s\n",
28ab034a
JG
211 opt_output,
212 output_mmap,
213 output_splice);
a79d84dd
DG
214 ret = CMD_ERROR;
215 goto error;
216 }
217 }
218
cd80958d 219 handle = lttng_create_handle(session_name, &dom);
cd9adb8b 220 if (handle == nullptr) {
cd80958d
DG
221 ret = -1;
222 goto error;
223 }
224
acc09215
JRJ
225 /* Mi open channels element */
226 if (lttng_opt_mi) {
a0377dfe 227 LTTNG_ASSERT(writer);
acc09215
JRJ
228 ret = mi_lttng_channels_open(writer);
229 if (ret) {
230 ret = CMD_ERROR;
231 goto error;
232 }
233 }
234
0fdd1e2c 235 /* Strip channel list (format: chan1,chan2,...) */
5b915816 236 channel_name = strtok(channel_list, ",");
cd9adb8b 237 while (channel_name != nullptr) {
cf0bcb51
JG
238 void *extended_ptr;
239
1f345e94 240 /* Validate channel name's length */
d6a6a609 241 if (strlen(channel_name) >= sizeof(chan_opts.name)) {
1f345e94 242 ERR("Channel name is too long (max. %zu characters)",
28ab034a 243 sizeof(chan_opts.name) - 1);
1f345e94
PP
244 error = 1;
245 goto skip_enable;
246 }
247
cf0bcb51
JG
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
1f345e94 259 /* Copy channel name */
cf0bcb51
JG
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,
28ab034a 267 opt_monitor_timer.interval);
cf0bcb51
JG
268 if (ret) {
269 ERR("Failed to set the channel's monitor timer interval");
270 error = 1;
271 goto error;
272 }
273 }
491d1539
MD
274 if (opt_blocking_timeout.set) {
275 ret = lttng_channel_set_blocking_timeout(channel,
28ab034a 276 opt_blocking_timeout.value);
491d1539
MD
277 if (ret) {
278 ERR("Failed to set the channel's blocking timeout");
279 error = 1;
280 goto error;
281 }
282 }
0fdd1e2c
DG
283
284 DBG("Enabling channel %s", channel_name);
285
cf0bcb51 286 ret = lttng_enable_channel(handle, channel);
0fdd1e2c 287 if (ret < 0) {
acc09215 288 success = 0;
42224349 289 switch (-ret) {
f73fabfd
DG
290 case LTTNG_ERR_KERN_CHAN_EXIST:
291 case LTTNG_ERR_UST_CHAN_EXIST:
b9dfb167 292 case LTTNG_ERR_CHAN_EXIST:
28ab034a
JG
293 WARN("Channel %s: %s (session %s)",
294 channel_name,
295 lttng_strerror(ret),
296 session_name);
acc09215
JRJ
297 warn = 1;
298 break;
1f345e94
PP
299 case LTTNG_ERR_INVALID_CHANNEL_NAME:
300 ERR("Invalid channel name: \"%s\". "
301 "Channel names may not start with '.', and "
28ab034a
JG
302 "may not contain '/'.",
303 channel_name);
1f345e94
PP
304 error = 1;
305 break;
42224349 306 default:
28ab034a
JG
307 ERR("Channel %s: %s (session %s)",
308 channel_name,
309 lttng_strerror(ret),
310 session_name);
acc09215 311 error = 1;
42224349
DG
312 break;
313 }
49cddecd
KS
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 }
d36b8583 351 } else {
7885e399 352 MSG("%s channel %s enabled for session %s",
28ab034a
JG
353 lttng_domain_type_str(dom.type),
354 channel_name,
355 session_name);
acc09215
JRJ
356 success = 1;
357 }
358
28ab034a 359 skip_enable:
acc09215
JRJ
360 if (lttng_opt_mi) {
361 /* Mi print the channel element and leave it open */
cf0bcb51 362 ret = mi_lttng_channel(writer, channel, 1);
acc09215
JRJ
363 if (ret) {
364 ret = CMD_ERROR;
365 goto error;
366 }
367
368 /* Individual Success ? */
28ab034a
JG
369 ret = mi_lttng_writer_write_element_bool(
370 writer, mi_lttng_element_command_success, success);
acc09215
JRJ
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 }
d36b8583
DG
382 }
383
acc09215 384 /* Next channel */
cd9adb8b 385 channel_name = strtok(nullptr, ",");
cf0bcb51 386 lttng_channel_destroy(channel);
cd9adb8b 387 channel = nullptr;
d36b8583
DG
388 }
389
acc09215
JRJ
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
ae856491
DG
399 ret = CMD_SUCCESS;
400
d36b8583 401error:
cf0bcb51
JG
402 if (channel) {
403 lttng_channel_destroy(channel);
404 }
acc09215
JRJ
405 /* If more important error happen bypass the warning */
406 if (!ret && warn) {
ae856491
DG
407 ret = CMD_WARNING;
408 }
acc09215
JRJ
409 /* If more important error happen bypass the warning */
410 if (!ret && error) {
411 ret = CMD_ERROR;
412 }
ae856491 413
cd80958d
DG
414 lttng_destroy_handle(handle);
415
d36b8583
DG
416 return ret;
417}
418
419/*
0fdd1e2c 420 * Default value for channel configuration.
7d29a247 421 */
cd9adb8b 422static void init_channel_config()
7d29a247 423{
5edd7e09
DG
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 */
cf0bcb51 428 memset(&chan_opts.attr, -1, sizeof(chan_opts.attr));
cd9adb8b 429 chan_opts.attr.extended.ptr = nullptr;
7d29a247
DG
430}
431
432/*
0fdd1e2c 433 * Add channel to trace session
d36b8583
DG
434 */
435int cmd_enable_channels(int argc, const char **argv)
436{
acc09215 437 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
d36b8583 438 static poptContext pc;
cd9adb8b
JG
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;
d36b8583 444
7d29a247
DG
445 init_channel_config();
446
cd9adb8b 447 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
d36b8583
DG
448 poptReadDefaultConfig(pc, 0);
449
450 while ((opt = poptGetNextOpt(pc)) != -1) {
451 switch (opt) {
452 case OPT_HELP:
4ba92f18 453 SHOW_HELP();
d36b8583 454 goto end;
7d29a247 455 case OPT_DISCARD:
cf0bcb51 456 chan_opts.attr.overwrite = 0;
7d29a247
DG
457 DBG("Channel set to discard");
458 break;
459 case OPT_OVERWRITE:
cf0bcb51 460 chan_opts.attr.overwrite = 1;
7d29a247
DG
461 DBG("Channel set to overwrite");
462 break;
463 case OPT_SUBBUF_SIZE:
1cb514ce
MD
464 {
465 uint64_t rounded_size;
466 int order;
467
70d0b120
SM
468 /* Parse the size */
469 opt_arg = poptGetOptArg(pc);
28ab034a
JG
470 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.subbuf_size) < 0 ||
471 !chan_opts.attr.subbuf_size) {
1cb514ce 472 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg);
70d0b120
SM
473 ret = CMD_ERROR;
474 goto end;
475 }
476
cf0bcb51 477 order = get_count_order_u64(chan_opts.attr.subbuf_size);
a0377dfe 478 LTTNG_ASSERT(order >= 0);
1cb514ce 479 rounded_size = 1ULL << order;
cf0bcb51 480 if (rounded_size < chan_opts.attr.subbuf_size) {
06c0da96 481 ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!",
28ab034a 482 chan_opts.attr.subbuf_size);
06c0da96
DG
483 ret = CMD_ERROR;
484 goto end;
485 }
486
cf0bcb51 487 if (rounded_size != chan_opts.attr.subbuf_size) {
28ab034a
JG
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);
cf0bcb51 492 chan_opts.attr.subbuf_size = rounded_size;
70d0b120
SM
493 }
494
1cb514ce 495 /* Should now be power of 2 */
28ab034a
JG
496 LTTNG_ASSERT(
497 !((chan_opts.attr.subbuf_size - 1) & chan_opts.attr.subbuf_size));
1cb514ce 498
cf0bcb51 499 DBG("Channel subbuf size set to %" PRIu64, chan_opts.attr.subbuf_size);
7d29a247 500 break;
1cb514ce 501 }
7d29a247 502 case OPT_NUM_SUBBUF:
1cb514ce
MD
503 {
504 uint64_t rounded_size;
505 int order;
506
16068db5 507 errno = 0;
1cb514ce 508 opt_arg = poptGetOptArg(pc);
cd9adb8b 509 chan_opts.attr.num_subbuf = strtoull(opt_arg, nullptr, 0);
cf0bcb51 510 if (errno != 0 || !chan_opts.attr.num_subbuf || !isdigit(opt_arg[0])) {
1cb514ce 511 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg);
16068db5
MD
512 ret = CMD_ERROR;
513 goto end;
514 }
515
cf0bcb51 516 order = get_count_order_u64(chan_opts.attr.num_subbuf);
a0377dfe 517 LTTNG_ASSERT(order >= 0);
1cb514ce 518 rounded_size = 1ULL << order;
cf0bcb51 519 if (rounded_size < chan_opts.attr.num_subbuf) {
28ab034a
JG
520 ERR("The number of subbuffers (%" PRIu64
521 ") is rounded and overflows!",
522 chan_opts.attr.num_subbuf);
06c0da96
DG
523 ret = CMD_ERROR;
524 goto end;
525 }
526
cf0bcb51 527 if (rounded_size != chan_opts.attr.num_subbuf) {
28ab034a
JG
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);
cf0bcb51 532 chan_opts.attr.num_subbuf = rounded_size;
1cb514ce
MD
533 }
534
535 /* Should now be power of 2 */
28ab034a
JG
536 LTTNG_ASSERT(
537 !((chan_opts.attr.num_subbuf - 1) & chan_opts.attr.num_subbuf));
1cb514ce 538
cf0bcb51 539 DBG("Channel subbuf num set to %" PRIu64, chan_opts.attr.num_subbuf);
7d29a247 540 break;
1cb514ce 541 }
7d29a247 542 case OPT_SWITCH_TIMER:
16068db5 543 {
c7219617 544 uint64_t v;
16068db5
MD
545
546 errno = 0;
1cb514ce 547 opt_arg = poptGetOptArg(pc);
c7219617
SM
548
549 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
550 ERR("Wrong value for --switch-timer parameter: %s", opt_arg);
16068db5
MD
551 ret = CMD_ERROR;
552 goto end;
553 }
c7219617 554
16068db5
MD
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 }
cf0bcb51 560 chan_opts.attr.switch_timer_interval = (uint32_t) v;
2a1135fa 561 DBG("Channel switch timer interval set to %d %s",
28ab034a
JG
562 chan_opts.attr.switch_timer_interval,
563 USEC_UNIT);
7d29a247 564 break;
16068db5 565 }
7d29a247 566 case OPT_READ_TIMER:
16068db5 567 {
c7219617 568 uint64_t v;
16068db5
MD
569
570 errno = 0;
1cb514ce 571 opt_arg = poptGetOptArg(pc);
c7219617
SM
572
573 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
574 ERR("Wrong value for --read-timer parameter: %s", opt_arg);
16068db5
MD
575 ret = CMD_ERROR;
576 goto end;
577 }
c7219617 578
16068db5
MD
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 }
cf0bcb51 584 chan_opts.attr.read_timer_interval = (uint32_t) v;
2a1135fa 585 DBG("Channel read timer interval set to %d %s",
28ab034a
JG
586 chan_opts.attr.read_timer_interval,
587 USEC_UNIT);
cf0bcb51
JG
588 break;
589 }
590 case OPT_MONITOR_TIMER:
591 {
7010c033 592 uint64_t v;
cf0bcb51
JG
593
594 errno = 0;
595 opt_arg = poptGetOptArg(pc);
7010c033
SM
596
597 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
598 ERR("Wrong value for --monitor-timer parameter: %s", opt_arg);
cf0bcb51
JG
599 ret = CMD_ERROR;
600 goto end;
601 }
8193ef17 602 opt_monitor_timer.interval = (uint64_t) v;
cf0bcb51 603 opt_monitor_timer.set = true;
2a1135fa 604 DBG("Channel monitor timer interval set to %" PRIu64 " %s",
28ab034a
JG
605 opt_monitor_timer.interval,
606 USEC_UNIT);
d36b8583 607 break;
16068db5 608 }
491d1539
MD
609 case OPT_BLOCKING_TIMEOUT:
610 {
7010c033 611 uint64_t v;
491d1539
MD
612 long long v_msec;
613
614 errno = 0;
615 opt_arg = poptGetOptArg(pc);
63dd9b28
PP
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
7010c033
SM
624 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
625 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg);
491d1539
MD
626 ret = CMD_ERROR;
627 goto end;
628 }
63dd9b28
PP
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) {
28ab034a
JG
647 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s",
648 opt_arg);
63dd9b28
PP
649 ret = CMD_ERROR;
650 goto end;
491d1539 651 }
63dd9b28 652
491d1539
MD
653 opt_blocking_timeout.value = (int64_t) v;
654 opt_blocking_timeout.set = true;
2a1135fa 655 DBG("Channel blocking timeout set to %" PRId64 " %s%s",
28ab034a
JG
656 opt_blocking_timeout.value,
657 USEC_UNIT,
658 opt_blocking_timeout.value == 0 ? " (non-blocking)" : "");
491d1539
MD
659 break;
660 }
eeac7d46
MD
661 case OPT_USERSPACE:
662 opt_userspace = 1;
eeac7d46 663 break;
1624d5b7 664 case OPT_TRACEFILE_SIZE:
1cb514ce 665 opt_arg = poptGetOptArg(pc);
cf0bcb51 666 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.tracefile_size) < 0) {
1cb514ce 667 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg);
16068db5
MD
668 ret = CMD_ERROR;
669 goto end;
670 }
1624d5b7 671 DBG("Maximum tracefile size set to %" PRIu64,
28ab034a 672 chan_opts.attr.tracefile_size);
1624d5b7
JD
673 break;
674 case OPT_TRACEFILE_COUNT:
16068db5
MD
675 {
676 unsigned long v;
677
678 errno = 0;
1cb514ce 679 opt_arg = poptGetOptArg(pc);
cd9adb8b 680 v = strtoul(opt_arg, nullptr, 0);
1cb514ce
MD
681 if (errno != 0 || !isdigit(opt_arg[0])) {
682 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg);
16068db5
MD
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 }
cf0bcb51 691 chan_opts.attr.tracefile_count = (uint32_t) v;
1624d5b7 692 DBG("Maximum tracefile count set to %" PRIu64,
28ab034a 693 chan_opts.attr.tracefile_count);
1624d5b7 694 break;
16068db5 695 }
679b4943
SM
696 case OPT_LIST_OPTIONS:
697 list_cmd_options(stdout, long_options);
679b4943 698 goto end;
d36b8583 699 default:
d36b8583
DG
700 ret = CMD_UNDEFINED;
701 goto end;
702 }
aafac6e1
JG
703
704 if (opt_arg) {
705 free(opt_arg);
706 opt_arg = nullptr;
707 }
d36b8583
DG
708 }
709
28ab034a 710 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace, false);
3ecec76a
PP
711 if (ret) {
712 ret = CMD_ERROR;
713 goto end;
714 }
715
ea10baf2 716 if (chan_opts.attr.overwrite == 1 && opt_blocking_timeout.set &&
28ab034a 717 opt_blocking_timeout.value != 0) {
ea10baf2 718 ERR("You cannot specify --overwrite and --blocking-timeout=N, "
28ab034a 719 "where N is different than 0");
ea10baf2
PP
720 ret = CMD_ERROR;
721 goto end;
722 }
723
acc09215
JRJ
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,
28ab034a 734 mi_lttng_element_command_enable_channels);
acc09215
JRJ
735 if (ret) {
736 ret = CMD_ERROR;
737 goto end;
738 }
739
740 /* Open output element */
28ab034a 741 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output);
acc09215
JRJ
742 if (ret) {
743 ret = CMD_ERROR;
744 goto end;
745 }
746 }
747
5b915816 748 arg_channel_list = poptGetArg(pc);
cd9adb8b 749 if (arg_channel_list == nullptr) {
5b915816
MJ
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);
cd9adb8b 757 if (channel_list == nullptr) {
5b915816 758 PERROR("Failed to copy channel name");
ca1c3607 759 ret = CMD_ERROR;
acc09215
JRJ
760 success = 0;
761 goto mi_closing;
d36b8583
DG
762 }
763
68c7f6e5
JD
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
cd80958d
DG
772 if (!opt_session_name) {
773 session_name = get_session_name();
cd9adb8b 774 if (session_name == nullptr) {
acc09215
JRJ
775 command_ret = CMD_ERROR;
776 success = 0;
777 goto mi_closing;
cd80958d
DG
778 }
779 } else {
780 session_name = opt_session_name;
781 }
782
5b915816 783 command_ret = enable_channel(session_name, channel_list);
acc09215
JRJ
784 if (command_ret) {
785 success = 0;
786 }
787
788mi_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 ? */
28ab034a
JG
798 ret = mi_lttng_writer_write_element_bool(
799 writer, mi_lttng_element_command_success, success);
acc09215
JRJ
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 }
d36b8583
DG
810
811end:
acc09215
JRJ
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
5853fd43
DG
818 if (!opt_session_name && session_name) {
819 free(session_name);
820 }
acc09215 821
5b915816
MJ
822 free(channel_list);
823
acc09215
JRJ
824 /* Overwrite ret if an error occurred when enable_channel */
825 ret = command_ret ? command_ret : ret;
ca1c3607 826 poptFreeContext(pc);
aafac6e1 827 free(opt_arg);
d36b8583
DG
828 return ret;
829}
This page took 0.120213 seconds and 4 git commands to generate.