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