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 | ||
1ceda6f1 FD |
24 | #include <lttng/domain-internal.h> |
25 | ||
c399183f | 26 | #include "../command.h" |
1cb514ce | 27 | #include "../utils.h" |
d36b8583 | 28 | |
42224349 | 29 | |
cf0bcb51 | 30 | static struct lttng_channel chan_opts; |
d36b8583 | 31 | static char *opt_channels; |
5edd7e09 | 32 | static int opt_kernel; |
5440dc42 | 33 | static char *opt_session_name; |
d36b8583 | 34 | static int opt_userspace; |
a79d84dd | 35 | static char *opt_output; |
7972aab2 DG |
36 | static int opt_buffer_uid; |
37 | static int opt_buffer_pid; | |
38 | static int opt_buffer_global; | |
cf0bcb51 JG |
39 | static struct { |
40 | bool set; | |
8193ef17 | 41 | uint64_t interval; |
cf0bcb51 | 42 | } opt_monitor_timer; |
491d1539 MD |
43 | static struct { |
44 | bool set; | |
45 | int64_t value; | |
46 | } opt_blocking_timeout; | |
d36b8583 | 47 | |
acc09215 JRJ |
48 | static struct mi_writer *writer; |
49 | ||
4fc83d94 PP |
50 | #ifdef LTTNG_EMBED_HELP |
51 | static const char help_msg[] = | |
52 | #include <lttng-enable-channel.1.h> | |
53 | ; | |
54 | #endif | |
55 | ||
d36b8583 DG |
56 | enum { |
57 | OPT_HELP = 1, | |
7d29a247 DG |
58 | OPT_DISCARD, |
59 | OPT_OVERWRITE, | |
60 | OPT_SUBBUF_SIZE, | |
61 | OPT_NUM_SUBBUF, | |
62 | OPT_SWITCH_TIMER, | |
cf0bcb51 | 63 | OPT_MONITOR_TIMER, |
7d29a247 | 64 | OPT_READ_TIMER, |
d36b8583 | 65 | OPT_USERSPACE, |
679b4943 | 66 | OPT_LIST_OPTIONS, |
1624d5b7 JD |
67 | OPT_TRACEFILE_SIZE, |
68 | OPT_TRACEFILE_COUNT, | |
491d1539 | 69 | OPT_BLOCKING_TIMEOUT, |
d36b8583 DG |
70 | }; |
71 | ||
cd80958d DG |
72 | static struct lttng_handle *handle; |
73 | ||
a79d84dd DG |
74 | const char *output_mmap = "mmap"; |
75 | const char *output_splice = "splice"; | |
76 | ||
d36b8583 DG |
77 | static struct poptOption long_options[] = { |
78 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
79 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
5440dc42 | 80 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
d36b8583 | 81 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
d78d6610 | 82 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
7d29a247 DG |
83 | {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0}, |
84 | {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0}, | |
70d0b120 | 85 | {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0}, |
1b579521 DG |
86 | {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0}, |
87 | {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0}, | |
cf0bcb51 | 88 | {"monitor-timer", 0, POPT_ARG_INT, 0, OPT_MONITOR_TIMER, 0, 0}, |
1b579521 | 89 | {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0}, |
679b4943 | 90 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
a79d84dd | 91 | {"output", 0, POPT_ARG_STRING, &opt_output, 0, 0, 0}, |
7972aab2 DG |
92 | {"buffers-uid", 0, POPT_ARG_VAL, &opt_buffer_uid, 1, 0, 0}, |
93 | {"buffers-pid", 0, POPT_ARG_VAL, &opt_buffer_pid, 1, 0, 0}, | |
94 | {"buffers-global", 0, POPT_ARG_VAL, &opt_buffer_global, 1, 0, 0}, | |
1624d5b7 JD |
95 | {"tracefile-size", 'C', POPT_ARG_INT, 0, OPT_TRACEFILE_SIZE, 0, 0}, |
96 | {"tracefile-count", 'W', POPT_ARG_INT, 0, OPT_TRACEFILE_COUNT, 0, 0}, | |
491d1539 | 97 | {"blocking-timeout", 0, POPT_ARG_INT, 0, OPT_BLOCKING_TIMEOUT, 0, 0}, |
d36b8583 DG |
98 | {0, 0, 0, 0, 0, 0, 0} |
99 | }; | |
100 | ||
5edd7e09 DG |
101 | /* |
102 | * Set default attributes depending on those already defined from the command | |
103 | * line. | |
104 | */ | |
105 | static void set_default_attr(struct lttng_domain *dom) | |
106 | { | |
107 | struct lttng_channel_attr default_attr; | |
108 | ||
c1e4642c JG |
109 | memset(&default_attr, 0, sizeof(default_attr)); |
110 | ||
5edd7e09 DG |
111 | /* Set attributes */ |
112 | lttng_channel_set_default_attr(dom, &default_attr); | |
113 | ||
cf0bcb51 JG |
114 | if (chan_opts.attr.overwrite == -1) { |
115 | chan_opts.attr.overwrite = default_attr.overwrite; | |
5edd7e09 | 116 | } |
cf0bcb51 JG |
117 | if (chan_opts.attr.subbuf_size == -1) { |
118 | chan_opts.attr.subbuf_size = default_attr.subbuf_size; | |
5edd7e09 | 119 | } |
cf0bcb51 JG |
120 | if (chan_opts.attr.num_subbuf == -1) { |
121 | chan_opts.attr.num_subbuf = default_attr.num_subbuf; | |
5edd7e09 | 122 | } |
cf0bcb51 JG |
123 | if (chan_opts.attr.switch_timer_interval == -1) { |
124 | chan_opts.attr.switch_timer_interval = default_attr.switch_timer_interval; | |
5edd7e09 | 125 | } |
cf0bcb51 JG |
126 | if (chan_opts.attr.read_timer_interval == -1) { |
127 | chan_opts.attr.read_timer_interval = default_attr.read_timer_interval; | |
5edd7e09 | 128 | } |
cf0bcb51 JG |
129 | if ((int) chan_opts.attr.output == -1) { |
130 | chan_opts.attr.output = default_attr.output; | |
5edd7e09 | 131 | } |
cf0bcb51 JG |
132 | if (chan_opts.attr.tracefile_count == -1) { |
133 | chan_opts.attr.tracefile_count = default_attr.tracefile_count; | |
1624d5b7 | 134 | } |
cf0bcb51 JG |
135 | if (chan_opts.attr.tracefile_size == -1) { |
136 | chan_opts.attr.tracefile_size = default_attr.tracefile_size; | |
1624d5b7 | 137 | } |
5edd7e09 DG |
138 | } |
139 | ||
d36b8583 | 140 | /* |
0fdd1e2c | 141 | * Adding channel using the lttng API. |
d36b8583 | 142 | */ |
cd80958d | 143 | static int enable_channel(char *session_name) |
d36b8583 | 144 | { |
cf0bcb51 | 145 | struct lttng_channel *channel = NULL; |
acc09215 | 146 | int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0; |
d36b8583 | 147 | char *channel_name; |
7d29a247 | 148 | struct lttng_domain dom; |
d36b8583 | 149 | |
441c16a7 MD |
150 | memset(&dom, 0, sizeof(dom)); |
151 | ||
491d1539 MD |
152 | /* Validate options. */ |
153 | if (opt_kernel) { | |
154 | if (opt_blocking_timeout.set) { | |
155 | ERR("Retry timeout option not supported for kernel domain (-k)"); | |
156 | ret = CMD_ERROR; | |
157 | goto error; | |
158 | } | |
159 | } | |
160 | ||
d78d6610 | 161 | /* Create lttng domain */ |
7d29a247 DG |
162 | if (opt_kernel) { |
163 | dom.type = LTTNG_DOMAIN_KERNEL; | |
7972aab2 | 164 | dom.buf_type = LTTNG_BUFFER_GLOBAL; |
88c5f0d8 DG |
165 | if (opt_buffer_uid || opt_buffer_pid) { |
166 | ERR("Buffer type not supported for domain -k"); | |
167 | ret = CMD_ERROR; | |
168 | goto error; | |
169 | } | |
d78d6610 | 170 | } else if (opt_userspace) { |
f6a9efaa | 171 | dom.type = LTTNG_DOMAIN_UST; |
8692d4e5 DG |
172 | if (opt_buffer_pid) { |
173 | dom.buf_type = LTTNG_BUFFER_PER_PID; | |
7972aab2 | 174 | } else { |
88c5f0d8 DG |
175 | if (opt_buffer_global) { |
176 | ERR("Buffer type not supported for domain -u"); | |
177 | ret = CMD_ERROR; | |
178 | goto error; | |
179 | } | |
8692d4e5 | 180 | dom.buf_type = LTTNG_BUFFER_PER_UID; |
7972aab2 | 181 | } |
0fdd1e2c | 182 | } else { |
3ecec76a PP |
183 | /* Checked by the caller. */ |
184 | assert(0); | |
7d29a247 DG |
185 | } |
186 | ||
5edd7e09 DG |
187 | set_default_attr(&dom); |
188 | ||
cf0bcb51 | 189 | if (chan_opts.attr.tracefile_size == 0 && chan_opts.attr.tracefile_count) { |
d16dee89 DG |
190 | ERR("Missing option --tracefile-size. " |
191 | "A file count without a size won't do anything."); | |
192 | ret = CMD_ERROR; | |
193 | goto error; | |
194 | } | |
195 | ||
cf0bcb51 JG |
196 | if ((chan_opts.attr.tracefile_size > 0) && |
197 | (chan_opts.attr.tracefile_size < chan_opts.attr.subbuf_size)) { | |
de65565a | 198 | WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")", |
cf0bcb51 JG |
199 | chan_opts.attr.tracefile_size, chan_opts.attr.subbuf_size); |
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", | |
211 | opt_output, output_mmap, output_splice); | |
a79d84dd DG |
212 | ret = CMD_ERROR; |
213 | goto error; | |
214 | } | |
215 | } | |
216 | ||
cd80958d DG |
217 | handle = lttng_create_handle(session_name, &dom); |
218 | if (handle == NULL) { | |
219 | ret = -1; | |
220 | goto error; | |
221 | } | |
222 | ||
acc09215 JRJ |
223 | /* Mi open channels element */ |
224 | if (lttng_opt_mi) { | |
225 | assert(writer); | |
226 | ret = mi_lttng_channels_open(writer); | |
227 | if (ret) { | |
228 | ret = CMD_ERROR; | |
229 | goto error; | |
230 | } | |
231 | } | |
232 | ||
0fdd1e2c | 233 | /* Strip channel list (format: chan1,chan2,...) */ |
d36b8583 DG |
234 | channel_name = strtok(opt_channels, ","); |
235 | while (channel_name != NULL) { | |
cf0bcb51 JG |
236 | void *extended_ptr; |
237 | ||
1f345e94 | 238 | /* Validate channel name's length */ |
d6a6a609 | 239 | if (strlen(channel_name) >= sizeof(chan_opts.name)) { |
1f345e94 | 240 | ERR("Channel name is too long (max. %zu characters)", |
cf0bcb51 | 241 | sizeof(chan_opts.name) - 1); |
1f345e94 PP |
242 | error = 1; |
243 | goto skip_enable; | |
244 | } | |
245 | ||
cf0bcb51 JG |
246 | /* |
247 | * A dynamically-allocated channel is used in order to allow | |
248 | * the configuration of extended attributes (post-2.9). | |
249 | */ | |
250 | channel = lttng_channel_create(&dom); | |
251 | if (!channel) { | |
252 | ERR("Unable to create channel object"); | |
253 | error = 1; | |
254 | goto error; | |
255 | } | |
256 | ||
1f345e94 | 257 | /* Copy channel name */ |
cf0bcb51 JG |
258 | strcpy(channel->name, channel_name); |
259 | channel->enabled = 1; | |
260 | extended_ptr = channel->attr.extended.ptr; | |
261 | memcpy(&channel->attr, &chan_opts.attr, sizeof(chan_opts.attr)); | |
262 | channel->attr.extended.ptr = extended_ptr; | |
263 | if (opt_monitor_timer.set) { | |
264 | ret = lttng_channel_set_monitor_timer_interval(channel, | |
265 | opt_monitor_timer.interval); | |
266 | if (ret) { | |
267 | ERR("Failed to set the channel's monitor timer interval"); | |
268 | error = 1; | |
269 | goto error; | |
270 | } | |
271 | } | |
491d1539 MD |
272 | if (opt_blocking_timeout.set) { |
273 | ret = lttng_channel_set_blocking_timeout(channel, | |
274 | opt_blocking_timeout.value); | |
275 | if (ret) { | |
276 | ERR("Failed to set the channel's blocking timeout"); | |
277 | error = 1; | |
278 | goto error; | |
279 | } | |
280 | } | |
0fdd1e2c DG |
281 | |
282 | DBG("Enabling channel %s", channel_name); | |
283 | ||
cf0bcb51 | 284 | ret = lttng_enable_channel(handle, channel); |
0fdd1e2c | 285 | if (ret < 0) { |
acc09215 | 286 | success = 0; |
42224349 | 287 | switch (-ret) { |
f73fabfd DG |
288 | case LTTNG_ERR_KERN_CHAN_EXIST: |
289 | case LTTNG_ERR_UST_CHAN_EXIST: | |
b9dfb167 | 290 | case LTTNG_ERR_CHAN_EXIST: |
88c5f0d8 | 291 | WARN("Channel %s: %s (session %s)", channel_name, |
42224349 | 292 | lttng_strerror(ret), session_name); |
acc09215 JRJ |
293 | warn = 1; |
294 | break; | |
1f345e94 PP |
295 | case LTTNG_ERR_INVALID_CHANNEL_NAME: |
296 | ERR("Invalid channel name: \"%s\". " | |
297 | "Channel names may not start with '.', and " | |
298 | "may not contain '/'.", channel_name); | |
299 | error = 1; | |
300 | break; | |
42224349 DG |
301 | default: |
302 | ERR("Channel %s: %s (session %s)", channel_name, | |
303 | lttng_strerror(ret), session_name); | |
acc09215 | 304 | error = 1; |
42224349 DG |
305 | break; |
306 | } | |
d36b8583 | 307 | } else { |
7885e399 | 308 | MSG("%s channel %s enabled for session %s", |
1ceda6f1 FD |
309 | lttng_domain_type_str(dom.type), |
310 | channel_name, session_name); | |
acc09215 JRJ |
311 | success = 1; |
312 | } | |
313 | ||
1f345e94 | 314 | skip_enable: |
acc09215 JRJ |
315 | if (lttng_opt_mi) { |
316 | /* Mi print the channel element and leave it open */ | |
cf0bcb51 | 317 | ret = mi_lttng_channel(writer, channel, 1); |
acc09215 JRJ |
318 | if (ret) { |
319 | ret = CMD_ERROR; | |
320 | goto error; | |
321 | } | |
322 | ||
323 | /* Individual Success ? */ | |
324 | ret = mi_lttng_writer_write_element_bool(writer, | |
325 | mi_lttng_element_command_success, success); | |
326 | if (ret) { | |
327 | ret = CMD_ERROR; | |
328 | goto error; | |
329 | } | |
330 | ||
331 | /* Close channel element */ | |
332 | ret = mi_lttng_writer_close_element(writer); | |
333 | if (ret) { | |
334 | ret = CMD_ERROR; | |
335 | goto error; | |
336 | } | |
d36b8583 DG |
337 | } |
338 | ||
acc09215 | 339 | /* Next channel */ |
d36b8583 | 340 | channel_name = strtok(NULL, ","); |
cf0bcb51 JG |
341 | lttng_channel_destroy(channel); |
342 | channel = NULL; | |
d36b8583 DG |
343 | } |
344 | ||
acc09215 JRJ |
345 | if (lttng_opt_mi) { |
346 | /* Close channels element */ | |
347 | ret = mi_lttng_writer_close_element(writer); | |
348 | if (ret) { | |
349 | ret = CMD_ERROR; | |
350 | goto error; | |
351 | } | |
352 | } | |
353 | ||
ae856491 DG |
354 | ret = CMD_SUCCESS; |
355 | ||
d36b8583 | 356 | error: |
cf0bcb51 JG |
357 | if (channel) { |
358 | lttng_channel_destroy(channel); | |
359 | } | |
acc09215 JRJ |
360 | /* If more important error happen bypass the warning */ |
361 | if (!ret && warn) { | |
ae856491 DG |
362 | ret = CMD_WARNING; |
363 | } | |
acc09215 JRJ |
364 | /* If more important error happen bypass the warning */ |
365 | if (!ret && error) { | |
366 | ret = CMD_ERROR; | |
367 | } | |
ae856491 | 368 | |
cd80958d DG |
369 | lttng_destroy_handle(handle); |
370 | ||
d36b8583 DG |
371 | return ret; |
372 | } | |
373 | ||
374 | /* | |
0fdd1e2c | 375 | * Default value for channel configuration. |
7d29a247 DG |
376 | */ |
377 | static void init_channel_config(void) | |
378 | { | |
5edd7e09 DG |
379 | /* |
380 | * Put -1 everywhere so we can identify those set by the command line and | |
381 | * those needed to be set by the default values. | |
382 | */ | |
cf0bcb51 JG |
383 | memset(&chan_opts.attr, -1, sizeof(chan_opts.attr)); |
384 | chan_opts.attr.extended.ptr = NULL; | |
7d29a247 DG |
385 | } |
386 | ||
387 | /* | |
0fdd1e2c | 388 | * Add channel to trace session |
d36b8583 DG |
389 | */ |
390 | int cmd_enable_channels(int argc, const char **argv) | |
391 | { | |
acc09215 | 392 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
d36b8583 | 393 | static poptContext pc; |
cd80958d | 394 | char *session_name = NULL; |
70d0b120 | 395 | char *opt_arg = 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); |
1cb514ce MD |
430 | assert(order >= 0); |
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 */ |
cf0bcb51 | 446 | 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); |
1cb514ce MD |
466 | assert(order >= 0); |
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 */ | |
cf0bcb51 | 482 | 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 | } | |
648 | } | |
649 | ||
3533d06b JG |
650 | ret = print_missing_or_multiple_domains( |
651 | opt_kernel + opt_userspace, false); | |
3ecec76a PP |
652 | if (ret) { |
653 | ret = CMD_ERROR; | |
654 | goto end; | |
655 | } | |
656 | ||
ea10baf2 PP |
657 | if (chan_opts.attr.overwrite == 1 && opt_blocking_timeout.set && |
658 | opt_blocking_timeout.value != 0) { | |
659 | ERR("You cannot specify --overwrite and --blocking-timeout=N, " | |
660 | "where N is different than 0"); | |
661 | ret = CMD_ERROR; | |
662 | goto end; | |
663 | } | |
664 | ||
acc09215 JRJ |
665 | /* Mi check */ |
666 | if (lttng_opt_mi) { | |
667 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
668 | if (!writer) { | |
669 | ret = -LTTNG_ERR_NOMEM; | |
670 | goto end; | |
671 | } | |
672 | ||
673 | /* Open command element */ | |
674 | ret = mi_lttng_writer_command_open(writer, | |
675 | mi_lttng_element_command_enable_channels); | |
676 | if (ret) { | |
677 | ret = CMD_ERROR; | |
678 | goto end; | |
679 | } | |
680 | ||
681 | /* Open output element */ | |
682 | ret = mi_lttng_writer_open_element(writer, | |
683 | mi_lttng_element_command_output); | |
684 | if (ret) { | |
685 | ret = CMD_ERROR; | |
686 | goto end; | |
687 | } | |
688 | } | |
689 | ||
d36b8583 DG |
690 | opt_channels = (char*) poptGetArg(pc); |
691 | if (opt_channels == NULL) { | |
7d29a247 | 692 | ERR("Missing channel name.\n"); |
ca1c3607 | 693 | ret = CMD_ERROR; |
acc09215 JRJ |
694 | success = 0; |
695 | goto mi_closing; | |
d36b8583 DG |
696 | } |
697 | ||
68c7f6e5 JD |
698 | leftover = poptGetArg(pc); |
699 | if (leftover) { | |
700 | ERR("Unknown argument: %s", leftover); | |
701 | ret = CMD_ERROR; | |
702 | success = 0; | |
703 | goto mi_closing; | |
704 | } | |
705 | ||
cd80958d DG |
706 | if (!opt_session_name) { |
707 | session_name = get_session_name(); | |
708 | if (session_name == NULL) { | |
acc09215 JRJ |
709 | command_ret = CMD_ERROR; |
710 | success = 0; | |
711 | goto mi_closing; | |
cd80958d DG |
712 | } |
713 | } else { | |
714 | session_name = opt_session_name; | |
715 | } | |
716 | ||
acc09215 JRJ |
717 | command_ret = enable_channel(session_name); |
718 | if (command_ret) { | |
719 | success = 0; | |
720 | } | |
721 | ||
722 | mi_closing: | |
723 | /* Mi closing */ | |
724 | if (lttng_opt_mi) { | |
725 | /* Close output element */ | |
726 | ret = mi_lttng_writer_close_element(writer); | |
727 | if (ret) { | |
728 | goto end; | |
729 | } | |
730 | ||
731 | /* Success ? */ | |
732 | ret = mi_lttng_writer_write_element_bool(writer, | |
733 | mi_lttng_element_command_success, success); | |
734 | if (ret) { | |
735 | goto end; | |
736 | } | |
737 | ||
738 | /* Command element close */ | |
739 | ret = mi_lttng_writer_command_close(writer); | |
740 | if (ret) { | |
741 | goto end; | |
742 | } | |
743 | } | |
d36b8583 DG |
744 | |
745 | end: | |
acc09215 JRJ |
746 | /* Mi clean-up */ |
747 | if (writer && mi_lttng_writer_destroy(writer)) { | |
748 | /* Preserve original error code */ | |
749 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; | |
750 | } | |
751 | ||
5853fd43 DG |
752 | if (!opt_session_name && session_name) { |
753 | free(session_name); | |
754 | } | |
acc09215 JRJ |
755 | |
756 | /* Overwrite ret if an error occurred when enable_channel */ | |
757 | ret = command_ret ? command_ret : ret; | |
ca1c3607 | 758 | poptFreeContext(pc); |
d36b8583 DG |
759 | return ret; |
760 | } |