Commit | Line | Data |
---|---|---|
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 |
d36b8583 DG |
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> | |
5a0de755 | 16 | #include <inttypes.h> |
1cb514ce | 17 | #include <ctype.h> |
d36b8583 | 18 | |
c9e313bc SM |
19 | #include <common/sessiond-comm/sessiond-comm.hpp> |
20 | #include <common/utils.hpp> | |
21 | #include <common/mi-lttng.hpp> | |
acc09215 | 22 | |
c9e313bc | 23 | #include <lttng/domain-internal.hpp> |
1004b719 | 24 | |
c9e313bc SM |
25 | #include "../command.hpp" |
26 | #include "../utils.hpp" | |
d36b8583 | 27 | |
42224349 | 28 | |
cf0bcb51 | 29 | static struct lttng_channel chan_opts; |
5edd7e09 | 30 | static int opt_kernel; |
5440dc42 | 31 | static char *opt_session_name; |
d36b8583 | 32 | static int opt_userspace; |
a79d84dd | 33 | static char *opt_output; |
7972aab2 DG |
34 | static int opt_buffer_uid; |
35 | static int opt_buffer_pid; | |
36 | static int opt_buffer_global; | |
cf0bcb51 JG |
37 | static struct { |
38 | bool set; | |
8193ef17 | 39 | uint64_t interval; |
cf0bcb51 | 40 | } opt_monitor_timer; |
491d1539 MD |
41 | static struct { |
42 | bool set; | |
43 | int64_t value; | |
44 | } opt_blocking_timeout; | |
d36b8583 | 45 | |
acc09215 JRJ |
46 | static struct mi_writer *writer; |
47 | ||
4fc83d94 PP |
48 | #ifdef LTTNG_EMBED_HELP |
49 | static const char help_msg[] = | |
50 | #include <lttng-enable-channel.1.h> | |
51 | ; | |
52 | #endif | |
53 | ||
d36b8583 DG |
54 | enum { |
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 |
70 | static struct lttng_handle *handle; |
71 | ||
a79d84dd DG |
72 | const char *output_mmap = "mmap"; |
73 | const char *output_splice = "splice"; | |
74 | ||
d36b8583 DG |
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}, | |
5440dc42 | 78 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
d36b8583 | 79 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
d78d6610 | 80 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
7d29a247 DG |
81 | {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0}, |
82 | {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0}, | |
70d0b120 | 83 | {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0}, |
1b579521 DG |
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}, | |
cf0bcb51 | 86 | {"monitor-timer", 0, POPT_ARG_INT, 0, OPT_MONITOR_TIMER, 0, 0}, |
1b579521 | 87 | {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0}, |
679b4943 | 88 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
a79d84dd | 89 | {"output", 0, POPT_ARG_STRING, &opt_output, 0, 0, 0}, |
7972aab2 DG |
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}, | |
1624d5b7 JD |
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}, | |
491d1539 | 95 | {"blocking-timeout", 0, POPT_ARG_INT, 0, OPT_BLOCKING_TIMEOUT, 0, 0}, |
d36b8583 DG |
96 | {0, 0, 0, 0, 0, 0, 0} |
97 | }; | |
98 | ||
5edd7e09 DG |
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 | ||
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 | 141 | static int enable_channel(char *session_name, char *channel_list) |
d36b8583 | 142 | { |
cf0bcb51 | 143 | struct lttng_channel *channel = NULL; |
acc09215 | 144 | int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0; |
d36b8583 | 145 | char *channel_name; |
7d29a247 | 146 | struct lttng_domain dom; |
d36b8583 | 147 | |
441c16a7 MD |
148 | memset(&dom, 0, sizeof(dom)); |
149 | ||
491d1539 MD |
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 | ||
d78d6610 | 159 | /* Create lttng domain */ |
7d29a247 DG |
160 | if (opt_kernel) { |
161 | dom.type = LTTNG_DOMAIN_KERNEL; | |
7972aab2 | 162 | dom.buf_type = LTTNG_BUFFER_GLOBAL; |
88c5f0d8 DG |
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 | } | |
d78d6610 | 168 | } else if (opt_userspace) { |
f6a9efaa | 169 | dom.type = LTTNG_DOMAIN_UST; |
8692d4e5 DG |
170 | if (opt_buffer_pid) { |
171 | dom.buf_type = LTTNG_BUFFER_PER_PID; | |
7972aab2 | 172 | } else { |
88c5f0d8 DG |
173 | if (opt_buffer_global) { |
174 | ERR("Buffer type not supported for domain -u"); | |
175 | ret = CMD_ERROR; | |
176 | goto error; | |
177 | } | |
8692d4e5 | 178 | dom.buf_type = LTTNG_BUFFER_PER_UID; |
7972aab2 | 179 | } |
0fdd1e2c | 180 | } else { |
3ecec76a | 181 | /* Checked by the caller. */ |
a0377dfe | 182 | abort(); |
7d29a247 DG |
183 | } |
184 | ||
5edd7e09 DG |
185 | set_default_attr(&dom); |
186 | ||
cf0bcb51 | 187 | if (chan_opts.attr.tracefile_size == 0 && chan_opts.attr.tracefile_count) { |
d16dee89 DG |
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 | ||
cf0bcb51 JG |
194 | if ((chan_opts.attr.tracefile_size > 0) && |
195 | (chan_opts.attr.tracefile_size < chan_opts.attr.subbuf_size)) { | |
de65565a | 196 | WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")", |
cf0bcb51 JG |
197 | chan_opts.attr.tracefile_size, chan_opts.attr.subbuf_size); |
198 | chan_opts.attr.tracefile_size = chan_opts.attr.subbuf_size; | |
1624d5b7 JD |
199 | } |
200 | ||
a79d84dd DG |
201 | /* Setting channel output */ |
202 | if (opt_output) { | |
203 | if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) { | |
cf0bcb51 | 204 | chan_opts.attr.output = LTTNG_EVENT_MMAP; |
a79d84dd | 205 | } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) { |
cf0bcb51 | 206 | chan_opts.attr.output = LTTNG_EVENT_SPLICE; |
a79d84dd DG |
207 | } else { |
208 | ERR("Unknown output type %s. Possible values are: %s, %s\n", | |
209 | opt_output, output_mmap, output_splice); | |
a79d84dd DG |
210 | ret = CMD_ERROR; |
211 | goto error; | |
212 | } | |
213 | } | |
214 | ||
cd80958d DG |
215 | handle = lttng_create_handle(session_name, &dom); |
216 | if (handle == NULL) { | |
217 | ret = -1; | |
218 | goto error; | |
219 | } | |
220 | ||
acc09215 JRJ |
221 | /* Mi open channels element */ |
222 | if (lttng_opt_mi) { | |
a0377dfe | 223 | LTTNG_ASSERT(writer); |
acc09215 JRJ |
224 | ret = mi_lttng_channels_open(writer); |
225 | if (ret) { | |
226 | ret = CMD_ERROR; | |
227 | goto error; | |
228 | } | |
229 | } | |
230 | ||
0fdd1e2c | 231 | /* Strip channel list (format: chan1,chan2,...) */ |
5b915816 | 232 | channel_name = strtok(channel_list, ","); |
d36b8583 | 233 | while (channel_name != NULL) { |
cf0bcb51 JG |
234 | void *extended_ptr; |
235 | ||
1f345e94 | 236 | /* Validate channel name's length */ |
d6a6a609 | 237 | if (strlen(channel_name) >= sizeof(chan_opts.name)) { |
1f345e94 | 238 | ERR("Channel name is too long (max. %zu characters)", |
cf0bcb51 | 239 | sizeof(chan_opts.name) - 1); |
1f345e94 PP |
240 | error = 1; |
241 | goto skip_enable; | |
242 | } | |
243 | ||
cf0bcb51 JG |
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 | ||
1f345e94 | 255 | /* Copy channel name */ |
cf0bcb51 JG |
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 | } | |
491d1539 MD |
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 | } | |
0fdd1e2c DG |
279 | |
280 | DBG("Enabling channel %s", channel_name); | |
281 | ||
cf0bcb51 | 282 | ret = lttng_enable_channel(handle, channel); |
0fdd1e2c | 283 | if (ret < 0) { |
acc09215 | 284 | success = 0; |
42224349 | 285 | switch (-ret) { |
f73fabfd DG |
286 | case LTTNG_ERR_KERN_CHAN_EXIST: |
287 | case LTTNG_ERR_UST_CHAN_EXIST: | |
b9dfb167 | 288 | case LTTNG_ERR_CHAN_EXIST: |
88c5f0d8 | 289 | WARN("Channel %s: %s (session %s)", channel_name, |
42224349 | 290 | lttng_strerror(ret), session_name); |
acc09215 JRJ |
291 | warn = 1; |
292 | break; | |
1f345e94 PP |
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; | |
42224349 DG |
299 | default: |
300 | ERR("Channel %s: %s (session %s)", channel_name, | |
301 | lttng_strerror(ret), session_name); | |
acc09215 | 302 | error = 1; |
42224349 DG |
303 | break; |
304 | } | |
d36b8583 | 305 | } else { |
7885e399 | 306 | MSG("%s channel %s enabled for session %s", |
1004b719 FD |
307 | lttng_domain_type_str(dom.type), |
308 | channel_name, session_name); | |
acc09215 JRJ |
309 | success = 1; |
310 | } | |
311 | ||
1f345e94 | 312 | skip_enable: |
acc09215 JRJ |
313 | if (lttng_opt_mi) { |
314 | /* Mi print the channel element and leave it open */ | |
cf0bcb51 | 315 | ret = mi_lttng_channel(writer, channel, 1); |
acc09215 JRJ |
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 | } | |
d36b8583 DG |
335 | } |
336 | ||
acc09215 | 337 | /* Next channel */ |
d36b8583 | 338 | channel_name = strtok(NULL, ","); |
cf0bcb51 JG |
339 | lttng_channel_destroy(channel); |
340 | channel = NULL; | |
d36b8583 DG |
341 | } |
342 | ||
acc09215 JRJ |
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 | ||
ae856491 DG |
352 | ret = CMD_SUCCESS; |
353 | ||
d36b8583 | 354 | error: |
cf0bcb51 JG |
355 | if (channel) { |
356 | lttng_channel_destroy(channel); | |
357 | } | |
acc09215 JRJ |
358 | /* If more important error happen bypass the warning */ |
359 | if (!ret && warn) { | |
ae856491 DG |
360 | ret = CMD_WARNING; |
361 | } | |
acc09215 JRJ |
362 | /* If more important error happen bypass the warning */ |
363 | if (!ret && error) { | |
364 | ret = CMD_ERROR; | |
365 | } | |
ae856491 | 366 | |
cd80958d DG |
367 | lttng_destroy_handle(handle); |
368 | ||
d36b8583 DG |
369 | return ret; |
370 | } | |
371 | ||
372 | /* | |
0fdd1e2c | 373 | * Default value for channel configuration. |
7d29a247 DG |
374 | */ |
375 | static void init_channel_config(void) | |
376 | { | |
5edd7e09 DG |
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 | */ | |
cf0bcb51 JG |
381 | memset(&chan_opts.attr, -1, sizeof(chan_opts.attr)); |
382 | chan_opts.attr.extended.ptr = NULL; | |
7d29a247 DG |
383 | } |
384 | ||
385 | /* | |
0fdd1e2c | 386 | * Add channel to trace session |
d36b8583 DG |
387 | */ |
388 | int cmd_enable_channels(int argc, const char **argv) | |
389 | { | |
acc09215 | 390 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
d36b8583 | 391 | static poptContext pc; |
cd80958d | 392 | char *session_name = NULL; |
5b915816 | 393 | char *channel_list = NULL; |
70d0b120 | 394 | char *opt_arg = NULL; |
5b915816 | 395 | const char *arg_channel_list = NULL; |
68c7f6e5 | 396 | const char *leftover = NULL; |
d36b8583 | 397 | |
7d29a247 DG |
398 | init_channel_config(); |
399 | ||
d36b8583 DG |
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: | |
4ba92f18 | 406 | SHOW_HELP(); |
d36b8583 | 407 | goto end; |
7d29a247 | 408 | case OPT_DISCARD: |
cf0bcb51 | 409 | chan_opts.attr.overwrite = 0; |
7d29a247 DG |
410 | DBG("Channel set to discard"); |
411 | break; | |
412 | case OPT_OVERWRITE: | |
cf0bcb51 | 413 | chan_opts.attr.overwrite = 1; |
7d29a247 DG |
414 | DBG("Channel set to overwrite"); |
415 | break; | |
416 | case OPT_SUBBUF_SIZE: | |
1cb514ce MD |
417 | { |
418 | uint64_t rounded_size; | |
419 | int order; | |
420 | ||
70d0b120 SM |
421 | /* Parse the size */ |
422 | opt_arg = poptGetOptArg(pc); | |
cf0bcb51 | 423 | if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.subbuf_size) < 0 || !chan_opts.attr.subbuf_size) { |
1cb514ce | 424 | ERR("Wrong value in --subbuf-size parameter: %s", opt_arg); |
70d0b120 SM |
425 | ret = CMD_ERROR; |
426 | goto end; | |
427 | } | |
428 | ||
cf0bcb51 | 429 | order = get_count_order_u64(chan_opts.attr.subbuf_size); |
a0377dfe | 430 | LTTNG_ASSERT(order >= 0); |
1cb514ce | 431 | rounded_size = 1ULL << order; |
cf0bcb51 | 432 | if (rounded_size < chan_opts.attr.subbuf_size) { |
06c0da96 | 433 | ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!", |
cf0bcb51 | 434 | chan_opts.attr.subbuf_size); |
06c0da96 DG |
435 | ret = CMD_ERROR; |
436 | goto end; | |
437 | } | |
438 | ||
cf0bcb51 | 439 | if (rounded_size != chan_opts.attr.subbuf_size) { |
1cb514ce | 440 | WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")", |
cf0bcb51 JG |
441 | chan_opts.attr.subbuf_size, rounded_size); |
442 | chan_opts.attr.subbuf_size = rounded_size; | |
70d0b120 SM |
443 | } |
444 | ||
1cb514ce | 445 | /* Should now be power of 2 */ |
a0377dfe | 446 | LTTNG_ASSERT(!((chan_opts.attr.subbuf_size - 1) & chan_opts.attr.subbuf_size)); |
1cb514ce | 447 | |
cf0bcb51 | 448 | DBG("Channel subbuf size set to %" PRIu64, chan_opts.attr.subbuf_size); |
7d29a247 | 449 | break; |
1cb514ce | 450 | } |
7d29a247 | 451 | case OPT_NUM_SUBBUF: |
1cb514ce MD |
452 | { |
453 | uint64_t rounded_size; | |
454 | int order; | |
455 | ||
16068db5 | 456 | errno = 0; |
1cb514ce | 457 | opt_arg = poptGetOptArg(pc); |
cf0bcb51 JG |
458 | chan_opts.attr.num_subbuf = strtoull(opt_arg, NULL, 0); |
459 | if (errno != 0 || !chan_opts.attr.num_subbuf || !isdigit(opt_arg[0])) { | |
1cb514ce | 460 | ERR("Wrong value in --num-subbuf parameter: %s", opt_arg); |
16068db5 MD |
461 | ret = CMD_ERROR; |
462 | goto end; | |
463 | } | |
464 | ||
cf0bcb51 | 465 | order = get_count_order_u64(chan_opts.attr.num_subbuf); |
a0377dfe | 466 | LTTNG_ASSERT(order >= 0); |
1cb514ce | 467 | rounded_size = 1ULL << order; |
cf0bcb51 | 468 | if (rounded_size < chan_opts.attr.num_subbuf) { |
06c0da96 | 469 | ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!", |
cf0bcb51 | 470 | chan_opts.attr.num_subbuf); |
06c0da96 DG |
471 | ret = CMD_ERROR; |
472 | goto end; | |
473 | } | |
474 | ||
cf0bcb51 | 475 | if (rounded_size != chan_opts.attr.num_subbuf) { |
1cb514ce | 476 | WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")", |
cf0bcb51 JG |
477 | chan_opts.attr.num_subbuf, rounded_size); |
478 | chan_opts.attr.num_subbuf = rounded_size; | |
1cb514ce MD |
479 | } |
480 | ||
481 | /* Should now be power of 2 */ | |
a0377dfe | 482 | LTTNG_ASSERT(!((chan_opts.attr.num_subbuf - 1) & chan_opts.attr.num_subbuf)); |
1cb514ce | 483 | |
cf0bcb51 | 484 | DBG("Channel subbuf num set to %" PRIu64, chan_opts.attr.num_subbuf); |
7d29a247 | 485 | break; |
1cb514ce | 486 | } |
7d29a247 | 487 | case OPT_SWITCH_TIMER: |
16068db5 | 488 | { |
c7219617 | 489 | uint64_t v; |
16068db5 MD |
490 | |
491 | errno = 0; | |
1cb514ce | 492 | opt_arg = poptGetOptArg(pc); |
c7219617 SM |
493 | |
494 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { | |
495 | ERR("Wrong value for --switch-timer parameter: %s", opt_arg); | |
16068db5 MD |
496 | ret = CMD_ERROR; |
497 | goto end; | |
498 | } | |
c7219617 | 499 | |
16068db5 MD |
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 | } | |
cf0bcb51 | 505 | chan_opts.attr.switch_timer_interval = (uint32_t) v; |
2a1135fa JG |
506 | DBG("Channel switch timer interval set to %d %s", |
507 | chan_opts.attr.switch_timer_interval, | |
508 | USEC_UNIT); | |
7d29a247 | 509 | break; |
16068db5 | 510 | } |
7d29a247 | 511 | case OPT_READ_TIMER: |
16068db5 | 512 | { |
c7219617 | 513 | uint64_t v; |
16068db5 MD |
514 | |
515 | errno = 0; | |
1cb514ce | 516 | opt_arg = poptGetOptArg(pc); |
c7219617 SM |
517 | |
518 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { | |
519 | ERR("Wrong value for --read-timer parameter: %s", opt_arg); | |
16068db5 MD |
520 | ret = CMD_ERROR; |
521 | goto end; | |
522 | } | |
c7219617 | 523 | |
16068db5 MD |
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 | } | |
cf0bcb51 | 529 | chan_opts.attr.read_timer_interval = (uint32_t) v; |
2a1135fa JG |
530 | DBG("Channel read timer interval set to %d %s", |
531 | chan_opts.attr.read_timer_interval, | |
532 | USEC_UNIT); | |
cf0bcb51 JG |
533 | break; |
534 | } | |
535 | case OPT_MONITOR_TIMER: | |
536 | { | |
7010c033 | 537 | uint64_t v; |
cf0bcb51 JG |
538 | |
539 | errno = 0; | |
540 | opt_arg = poptGetOptArg(pc); | |
7010c033 SM |
541 | |
542 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { | |
543 | ERR("Wrong value for --monitor-timer parameter: %s", opt_arg); | |
cf0bcb51 JG |
544 | ret = CMD_ERROR; |
545 | goto end; | |
546 | } | |
8193ef17 | 547 | opt_monitor_timer.interval = (uint64_t) v; |
cf0bcb51 | 548 | opt_monitor_timer.set = true; |
2a1135fa JG |
549 | DBG("Channel monitor timer interval set to %" PRIu64 " %s", |
550 | opt_monitor_timer.interval, | |
551 | USEC_UNIT); | |
d36b8583 | 552 | break; |
16068db5 | 553 | } |
491d1539 MD |
554 | case OPT_BLOCKING_TIMEOUT: |
555 | { | |
7010c033 | 556 | uint64_t v; |
491d1539 MD |
557 | long long v_msec; |
558 | ||
559 | errno = 0; | |
560 | opt_arg = poptGetOptArg(pc); | |
63dd9b28 PP |
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 | ||
7010c033 SM |
569 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
570 | ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg); | |
491d1539 MD |
571 | ret = CMD_ERROR; |
572 | goto end; | |
573 | } | |
63dd9b28 PP |
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; | |
491d1539 | 595 | } |
63dd9b28 | 596 | |
491d1539 MD |
597 | opt_blocking_timeout.value = (int64_t) v; |
598 | opt_blocking_timeout.set = true; | |
2a1135fa | 599 | DBG("Channel blocking timeout set to %" PRId64 " %s%s", |
63dd9b28 | 600 | opt_blocking_timeout.value, |
2a1135fa | 601 | USEC_UNIT, |
63dd9b28 PP |
602 | opt_blocking_timeout.value == 0 ? |
603 | " (non-blocking)" : ""); | |
491d1539 MD |
604 | break; |
605 | } | |
eeac7d46 MD |
606 | case OPT_USERSPACE: |
607 | opt_userspace = 1; | |
eeac7d46 | 608 | break; |
1624d5b7 | 609 | case OPT_TRACEFILE_SIZE: |
1cb514ce | 610 | opt_arg = poptGetOptArg(pc); |
cf0bcb51 | 611 | if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.tracefile_size) < 0) { |
1cb514ce | 612 | ERR("Wrong value in --tracefile-size parameter: %s", opt_arg); |
16068db5 MD |
613 | ret = CMD_ERROR; |
614 | goto end; | |
615 | } | |
1624d5b7 | 616 | DBG("Maximum tracefile size set to %" PRIu64, |
cf0bcb51 | 617 | chan_opts.attr.tracefile_size); |
1624d5b7 JD |
618 | break; |
619 | case OPT_TRACEFILE_COUNT: | |
16068db5 MD |
620 | { |
621 | unsigned long v; | |
622 | ||
623 | errno = 0; | |
1cb514ce MD |
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); | |
16068db5 MD |
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 | } | |
cf0bcb51 | 636 | chan_opts.attr.tracefile_count = (uint32_t) v; |
1624d5b7 | 637 | DBG("Maximum tracefile count set to %" PRIu64, |
cf0bcb51 | 638 | chan_opts.attr.tracefile_count); |
1624d5b7 | 639 | break; |
16068db5 | 640 | } |
679b4943 SM |
641 | case OPT_LIST_OPTIONS: |
642 | list_cmd_options(stdout, long_options); | |
679b4943 | 643 | goto end; |
d36b8583 | 644 | default: |
d36b8583 DG |
645 | ret = CMD_UNDEFINED; |
646 | goto end; | |
647 | } | |
aafac6e1 JG |
648 | |
649 | if (opt_arg) { | |
650 | free(opt_arg); | |
651 | opt_arg = nullptr; | |
652 | } | |
d36b8583 DG |
653 | } |
654 | ||
3533d06b JG |
655 | ret = print_missing_or_multiple_domains( |
656 | opt_kernel + opt_userspace, false); | |
3ecec76a PP |
657 | if (ret) { |
658 | ret = CMD_ERROR; | |
659 | goto end; | |
660 | } | |
661 | ||
ea10baf2 PP |
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 | ||
acc09215 JRJ |
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 | ||
5b915816 MJ |
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"); | |
ca1c3607 | 706 | ret = CMD_ERROR; |
acc09215 JRJ |
707 | success = 0; |
708 | goto mi_closing; | |
d36b8583 DG |
709 | } |
710 | ||
68c7f6e5 JD |
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 | ||
cd80958d DG |
719 | if (!opt_session_name) { |
720 | session_name = get_session_name(); | |
721 | if (session_name == NULL) { | |
acc09215 JRJ |
722 | command_ret = CMD_ERROR; |
723 | success = 0; | |
724 | goto mi_closing; | |
cd80958d DG |
725 | } |
726 | } else { | |
727 | session_name = opt_session_name; | |
728 | } | |
729 | ||
5b915816 | 730 | command_ret = enable_channel(session_name, channel_list); |
acc09215 JRJ |
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 | } | |
d36b8583 DG |
757 | |
758 | end: | |
acc09215 JRJ |
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 | ||
5853fd43 DG |
765 | if (!opt_session_name && session_name) { |
766 | free(session_name); | |
767 | } | |
acc09215 | 768 | |
5b915816 MJ |
769 | free(channel_list); |
770 | ||
acc09215 JRJ |
771 | /* Overwrite ret if an error occurred when enable_channel */ |
772 | ret = command_ret ? command_ret : ret; | |
ca1c3607 | 773 | poptFreeContext(pc); |
aafac6e1 | 774 | free(opt_arg); |
d36b8583 DG |
775 | return ret; |
776 | } |