Commit | Line | Data |
---|---|---|
d36b8583 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
d36b8583 DG |
7 | * |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
d36b8583 DG |
16 | */ |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
d36b8583 DG |
19 | #include <popt.h> |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <sys/stat.h> | |
24 | #include <sys/types.h> | |
25 | #include <unistd.h> | |
5a0de755 | 26 | #include <inttypes.h> |
1cb514ce MD |
27 | #include <assert.h> |
28 | #include <ctype.h> | |
d36b8583 | 29 | |
acc09215 JRJ |
30 | #include <common/sessiond-comm/sessiond-comm.h> |
31 | #include <common/utils.h> | |
32 | #include <common/mi-lttng.h> | |
33 | ||
c399183f | 34 | #include "../command.h" |
1cb514ce | 35 | #include "../utils.h" |
d36b8583 | 36 | |
42224349 | 37 | |
cf0bcb51 | 38 | static struct lttng_channel chan_opts; |
d36b8583 | 39 | static char *opt_channels; |
5edd7e09 | 40 | static int opt_kernel; |
5440dc42 | 41 | static char *opt_session_name; |
d36b8583 | 42 | static int opt_userspace; |
a79d84dd | 43 | static char *opt_output; |
7972aab2 DG |
44 | static int opt_buffer_uid; |
45 | static int opt_buffer_pid; | |
46 | static int opt_buffer_global; | |
cf0bcb51 JG |
47 | static struct { |
48 | bool set; | |
49 | uint32_t interval; | |
50 | } opt_monitor_timer; | |
d36b8583 | 51 | |
acc09215 JRJ |
52 | static struct mi_writer *writer; |
53 | ||
4fc83d94 PP |
54 | #ifdef LTTNG_EMBED_HELP |
55 | static const char help_msg[] = | |
56 | #include <lttng-enable-channel.1.h> | |
57 | ; | |
58 | #endif | |
59 | ||
d36b8583 DG |
60 | enum { |
61 | OPT_HELP = 1, | |
7d29a247 DG |
62 | OPT_DISCARD, |
63 | OPT_OVERWRITE, | |
64 | OPT_SUBBUF_SIZE, | |
65 | OPT_NUM_SUBBUF, | |
66 | OPT_SWITCH_TIMER, | |
cf0bcb51 | 67 | OPT_MONITOR_TIMER, |
7d29a247 | 68 | OPT_READ_TIMER, |
d36b8583 | 69 | OPT_USERSPACE, |
679b4943 | 70 | OPT_LIST_OPTIONS, |
1624d5b7 JD |
71 | OPT_TRACEFILE_SIZE, |
72 | OPT_TRACEFILE_COUNT, | |
d36b8583 DG |
73 | }; |
74 | ||
cd80958d DG |
75 | static struct lttng_handle *handle; |
76 | ||
a79d84dd DG |
77 | const char *output_mmap = "mmap"; |
78 | const char *output_splice = "splice"; | |
79 | ||
d36b8583 DG |
80 | static struct poptOption long_options[] = { |
81 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
82 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
5440dc42 | 83 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
d36b8583 | 84 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
d78d6610 | 85 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
7d29a247 DG |
86 | {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0}, |
87 | {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0}, | |
70d0b120 | 88 | {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0}, |
1b579521 DG |
89 | {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0}, |
90 | {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0}, | |
cf0bcb51 | 91 | {"monitor-timer", 0, POPT_ARG_INT, 0, OPT_MONITOR_TIMER, 0, 0}, |
1b579521 | 92 | {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0}, |
679b4943 | 93 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
a79d84dd | 94 | {"output", 0, POPT_ARG_STRING, &opt_output, 0, 0, 0}, |
7972aab2 DG |
95 | {"buffers-uid", 0, POPT_ARG_VAL, &opt_buffer_uid, 1, 0, 0}, |
96 | {"buffers-pid", 0, POPT_ARG_VAL, &opt_buffer_pid, 1, 0, 0}, | |
97 | {"buffers-global", 0, POPT_ARG_VAL, &opt_buffer_global, 1, 0, 0}, | |
1624d5b7 JD |
98 | {"tracefile-size", 'C', POPT_ARG_INT, 0, OPT_TRACEFILE_SIZE, 0, 0}, |
99 | {"tracefile-count", 'W', POPT_ARG_INT, 0, OPT_TRACEFILE_COUNT, 0, 0}, | |
d36b8583 DG |
100 | {0, 0, 0, 0, 0, 0, 0} |
101 | }; | |
102 | ||
5edd7e09 DG |
103 | /* |
104 | * Set default attributes depending on those already defined from the command | |
105 | * line. | |
106 | */ | |
107 | static void set_default_attr(struct lttng_domain *dom) | |
108 | { | |
109 | struct lttng_channel_attr default_attr; | |
110 | ||
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 | ||
d78d6610 | 152 | /* Create lttng domain */ |
7d29a247 DG |
153 | if (opt_kernel) { |
154 | dom.type = LTTNG_DOMAIN_KERNEL; | |
7972aab2 | 155 | dom.buf_type = LTTNG_BUFFER_GLOBAL; |
88c5f0d8 DG |
156 | if (opt_buffer_uid || opt_buffer_pid) { |
157 | ERR("Buffer type not supported for domain -k"); | |
158 | ret = CMD_ERROR; | |
159 | goto error; | |
160 | } | |
d78d6610 | 161 | } else if (opt_userspace) { |
f6a9efaa | 162 | dom.type = LTTNG_DOMAIN_UST; |
8692d4e5 DG |
163 | if (opt_buffer_pid) { |
164 | dom.buf_type = LTTNG_BUFFER_PER_PID; | |
7972aab2 | 165 | } else { |
88c5f0d8 DG |
166 | if (opt_buffer_global) { |
167 | ERR("Buffer type not supported for domain -u"); | |
168 | ret = CMD_ERROR; | |
169 | goto error; | |
170 | } | |
8692d4e5 | 171 | dom.buf_type = LTTNG_BUFFER_PER_UID; |
7972aab2 | 172 | } |
0fdd1e2c | 173 | } else { |
3ecec76a PP |
174 | /* Checked by the caller. */ |
175 | assert(0); | |
7d29a247 DG |
176 | } |
177 | ||
5edd7e09 DG |
178 | set_default_attr(&dom); |
179 | ||
cf0bcb51 | 180 | if (chan_opts.attr.tracefile_size == 0 && chan_opts.attr.tracefile_count) { |
d16dee89 DG |
181 | ERR("Missing option --tracefile-size. " |
182 | "A file count without a size won't do anything."); | |
183 | ret = CMD_ERROR; | |
184 | goto error; | |
185 | } | |
186 | ||
cf0bcb51 JG |
187 | if ((chan_opts.attr.tracefile_size > 0) && |
188 | (chan_opts.attr.tracefile_size < chan_opts.attr.subbuf_size)) { | |
de65565a | 189 | WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")", |
cf0bcb51 JG |
190 | chan_opts.attr.tracefile_size, chan_opts.attr.subbuf_size); |
191 | chan_opts.attr.tracefile_size = chan_opts.attr.subbuf_size; | |
1624d5b7 JD |
192 | } |
193 | ||
a79d84dd DG |
194 | /* Setting channel output */ |
195 | if (opt_output) { | |
196 | if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) { | |
cf0bcb51 | 197 | chan_opts.attr.output = LTTNG_EVENT_MMAP; |
a79d84dd | 198 | } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) { |
cf0bcb51 | 199 | chan_opts.attr.output = LTTNG_EVENT_SPLICE; |
a79d84dd DG |
200 | } else { |
201 | ERR("Unknown output type %s. Possible values are: %s, %s\n", | |
202 | opt_output, output_mmap, output_splice); | |
a79d84dd DG |
203 | ret = CMD_ERROR; |
204 | goto error; | |
205 | } | |
206 | } | |
207 | ||
cd80958d DG |
208 | handle = lttng_create_handle(session_name, &dom); |
209 | if (handle == NULL) { | |
210 | ret = -1; | |
211 | goto error; | |
212 | } | |
213 | ||
acc09215 JRJ |
214 | /* Mi open channels element */ |
215 | if (lttng_opt_mi) { | |
216 | assert(writer); | |
217 | ret = mi_lttng_channels_open(writer); | |
218 | if (ret) { | |
219 | ret = CMD_ERROR; | |
220 | goto error; | |
221 | } | |
222 | } | |
223 | ||
0fdd1e2c | 224 | /* Strip channel list (format: chan1,chan2,...) */ |
d36b8583 DG |
225 | channel_name = strtok(opt_channels, ","); |
226 | while (channel_name != NULL) { | |
cf0bcb51 JG |
227 | void *extended_ptr; |
228 | ||
1f345e94 PP |
229 | /* Validate channel name's length */ |
230 | if (strlen(channel_name) >= NAME_MAX) { | |
231 | ERR("Channel name is too long (max. %zu characters)", | |
cf0bcb51 | 232 | sizeof(chan_opts.name) - 1); |
1f345e94 PP |
233 | error = 1; |
234 | goto skip_enable; | |
235 | } | |
236 | ||
cf0bcb51 JG |
237 | /* |
238 | * A dynamically-allocated channel is used in order to allow | |
239 | * the configuration of extended attributes (post-2.9). | |
240 | */ | |
241 | channel = lttng_channel_create(&dom); | |
242 | if (!channel) { | |
243 | ERR("Unable to create channel object"); | |
244 | error = 1; | |
245 | goto error; | |
246 | } | |
247 | ||
1f345e94 | 248 | /* Copy channel name */ |
cf0bcb51 JG |
249 | strcpy(channel->name, channel_name); |
250 | channel->enabled = 1; | |
251 | extended_ptr = channel->attr.extended.ptr; | |
252 | memcpy(&channel->attr, &chan_opts.attr, sizeof(chan_opts.attr)); | |
253 | channel->attr.extended.ptr = extended_ptr; | |
254 | if (opt_monitor_timer.set) { | |
255 | ret = lttng_channel_set_monitor_timer_interval(channel, | |
256 | opt_monitor_timer.interval); | |
257 | if (ret) { | |
258 | ERR("Failed to set the channel's monitor timer interval"); | |
259 | error = 1; | |
260 | goto error; | |
261 | } | |
262 | } | |
0fdd1e2c DG |
263 | |
264 | DBG("Enabling channel %s", channel_name); | |
265 | ||
cf0bcb51 | 266 | ret = lttng_enable_channel(handle, channel); |
0fdd1e2c | 267 | if (ret < 0) { |
acc09215 | 268 | success = 0; |
42224349 | 269 | switch (-ret) { |
f73fabfd DG |
270 | case LTTNG_ERR_KERN_CHAN_EXIST: |
271 | case LTTNG_ERR_UST_CHAN_EXIST: | |
b9dfb167 | 272 | case LTTNG_ERR_CHAN_EXIST: |
88c5f0d8 | 273 | WARN("Channel %s: %s (session %s)", channel_name, |
42224349 | 274 | lttng_strerror(ret), session_name); |
acc09215 JRJ |
275 | warn = 1; |
276 | break; | |
1f345e94 PP |
277 | case LTTNG_ERR_INVALID_CHANNEL_NAME: |
278 | ERR("Invalid channel name: \"%s\". " | |
279 | "Channel names may not start with '.', and " | |
280 | "may not contain '/'.", channel_name); | |
281 | error = 1; | |
282 | break; | |
42224349 DG |
283 | default: |
284 | ERR("Channel %s: %s (session %s)", channel_name, | |
285 | lttng_strerror(ret), session_name); | |
acc09215 | 286 | error = 1; |
42224349 DG |
287 | break; |
288 | } | |
d36b8583 | 289 | } else { |
7885e399 | 290 | MSG("%s channel %s enabled for session %s", |
b9dfb167 | 291 | get_domain_str(dom.type), channel_name, session_name); |
acc09215 JRJ |
292 | success = 1; |
293 | } | |
294 | ||
1f345e94 | 295 | skip_enable: |
acc09215 JRJ |
296 | if (lttng_opt_mi) { |
297 | /* Mi print the channel element and leave it open */ | |
cf0bcb51 | 298 | ret = mi_lttng_channel(writer, channel, 1); |
acc09215 JRJ |
299 | if (ret) { |
300 | ret = CMD_ERROR; | |
301 | goto error; | |
302 | } | |
303 | ||
304 | /* Individual Success ? */ | |
305 | ret = mi_lttng_writer_write_element_bool(writer, | |
306 | mi_lttng_element_command_success, success); | |
307 | if (ret) { | |
308 | ret = CMD_ERROR; | |
309 | goto error; | |
310 | } | |
311 | ||
312 | /* Close channel element */ | |
313 | ret = mi_lttng_writer_close_element(writer); | |
314 | if (ret) { | |
315 | ret = CMD_ERROR; | |
316 | goto error; | |
317 | } | |
d36b8583 DG |
318 | } |
319 | ||
acc09215 | 320 | /* Next channel */ |
d36b8583 | 321 | channel_name = strtok(NULL, ","); |
cf0bcb51 JG |
322 | lttng_channel_destroy(channel); |
323 | channel = NULL; | |
d36b8583 DG |
324 | } |
325 | ||
acc09215 JRJ |
326 | if (lttng_opt_mi) { |
327 | /* Close channels element */ | |
328 | ret = mi_lttng_writer_close_element(writer); | |
329 | if (ret) { | |
330 | ret = CMD_ERROR; | |
331 | goto error; | |
332 | } | |
333 | } | |
334 | ||
ae856491 DG |
335 | ret = CMD_SUCCESS; |
336 | ||
d36b8583 | 337 | error: |
cf0bcb51 JG |
338 | if (channel) { |
339 | lttng_channel_destroy(channel); | |
340 | } | |
acc09215 JRJ |
341 | /* If more important error happen bypass the warning */ |
342 | if (!ret && warn) { | |
ae856491 DG |
343 | ret = CMD_WARNING; |
344 | } | |
acc09215 JRJ |
345 | /* If more important error happen bypass the warning */ |
346 | if (!ret && error) { | |
347 | ret = CMD_ERROR; | |
348 | } | |
ae856491 | 349 | |
cd80958d DG |
350 | lttng_destroy_handle(handle); |
351 | ||
d36b8583 DG |
352 | return ret; |
353 | } | |
354 | ||
355 | /* | |
0fdd1e2c | 356 | * Default value for channel configuration. |
7d29a247 DG |
357 | */ |
358 | static void init_channel_config(void) | |
359 | { | |
5edd7e09 DG |
360 | /* |
361 | * Put -1 everywhere so we can identify those set by the command line and | |
362 | * those needed to be set by the default values. | |
363 | */ | |
cf0bcb51 JG |
364 | memset(&chan_opts.attr, -1, sizeof(chan_opts.attr)); |
365 | chan_opts.attr.extended.ptr = NULL; | |
7d29a247 DG |
366 | } |
367 | ||
368 | /* | |
0fdd1e2c | 369 | * Add channel to trace session |
d36b8583 DG |
370 | */ |
371 | int cmd_enable_channels(int argc, const char **argv) | |
372 | { | |
acc09215 | 373 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
d36b8583 | 374 | static poptContext pc; |
cd80958d | 375 | char *session_name = NULL; |
70d0b120 | 376 | char *opt_arg = NULL; |
d36b8583 | 377 | |
7d29a247 DG |
378 | init_channel_config(); |
379 | ||
d36b8583 DG |
380 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
381 | poptReadDefaultConfig(pc, 0); | |
382 | ||
383 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
384 | switch (opt) { | |
385 | case OPT_HELP: | |
4ba92f18 | 386 | SHOW_HELP(); |
d36b8583 | 387 | goto end; |
7d29a247 | 388 | case OPT_DISCARD: |
cf0bcb51 | 389 | chan_opts.attr.overwrite = 0; |
7d29a247 DG |
390 | DBG("Channel set to discard"); |
391 | break; | |
392 | case OPT_OVERWRITE: | |
cf0bcb51 | 393 | chan_opts.attr.overwrite = 1; |
7d29a247 DG |
394 | DBG("Channel set to overwrite"); |
395 | break; | |
396 | case OPT_SUBBUF_SIZE: | |
1cb514ce MD |
397 | { |
398 | uint64_t rounded_size; | |
399 | int order; | |
400 | ||
70d0b120 SM |
401 | /* Parse the size */ |
402 | opt_arg = poptGetOptArg(pc); | |
cf0bcb51 | 403 | if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.subbuf_size) < 0 || !chan_opts.attr.subbuf_size) { |
1cb514ce | 404 | ERR("Wrong value in --subbuf-size parameter: %s", opt_arg); |
70d0b120 SM |
405 | ret = CMD_ERROR; |
406 | goto end; | |
407 | } | |
408 | ||
cf0bcb51 | 409 | order = get_count_order_u64(chan_opts.attr.subbuf_size); |
1cb514ce MD |
410 | assert(order >= 0); |
411 | rounded_size = 1ULL << order; | |
cf0bcb51 | 412 | if (rounded_size < chan_opts.attr.subbuf_size) { |
06c0da96 | 413 | ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!", |
cf0bcb51 | 414 | chan_opts.attr.subbuf_size); |
06c0da96 DG |
415 | ret = CMD_ERROR; |
416 | goto end; | |
417 | } | |
418 | ||
cf0bcb51 | 419 | if (rounded_size != chan_opts.attr.subbuf_size) { |
1cb514ce | 420 | WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")", |
cf0bcb51 JG |
421 | chan_opts.attr.subbuf_size, rounded_size); |
422 | chan_opts.attr.subbuf_size = rounded_size; | |
70d0b120 SM |
423 | } |
424 | ||
1cb514ce | 425 | /* Should now be power of 2 */ |
cf0bcb51 | 426 | assert(!((chan_opts.attr.subbuf_size - 1) & chan_opts.attr.subbuf_size)); |
1cb514ce | 427 | |
cf0bcb51 | 428 | DBG("Channel subbuf size set to %" PRIu64, chan_opts.attr.subbuf_size); |
7d29a247 | 429 | break; |
1cb514ce | 430 | } |
7d29a247 | 431 | case OPT_NUM_SUBBUF: |
1cb514ce MD |
432 | { |
433 | uint64_t rounded_size; | |
434 | int order; | |
435 | ||
16068db5 | 436 | errno = 0; |
1cb514ce | 437 | opt_arg = poptGetOptArg(pc); |
cf0bcb51 JG |
438 | chan_opts.attr.num_subbuf = strtoull(opt_arg, NULL, 0); |
439 | if (errno != 0 || !chan_opts.attr.num_subbuf || !isdigit(opt_arg[0])) { | |
1cb514ce | 440 | ERR("Wrong value in --num-subbuf parameter: %s", opt_arg); |
16068db5 MD |
441 | ret = CMD_ERROR; |
442 | goto end; | |
443 | } | |
444 | ||
cf0bcb51 | 445 | order = get_count_order_u64(chan_opts.attr.num_subbuf); |
1cb514ce MD |
446 | assert(order >= 0); |
447 | rounded_size = 1ULL << order; | |
cf0bcb51 | 448 | if (rounded_size < chan_opts.attr.num_subbuf) { |
06c0da96 | 449 | ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!", |
cf0bcb51 | 450 | chan_opts.attr.num_subbuf); |
06c0da96 DG |
451 | ret = CMD_ERROR; |
452 | goto end; | |
453 | } | |
454 | ||
cf0bcb51 | 455 | if (rounded_size != chan_opts.attr.num_subbuf) { |
1cb514ce | 456 | WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")", |
cf0bcb51 JG |
457 | chan_opts.attr.num_subbuf, rounded_size); |
458 | chan_opts.attr.num_subbuf = rounded_size; | |
1cb514ce MD |
459 | } |
460 | ||
461 | /* Should now be power of 2 */ | |
cf0bcb51 | 462 | assert(!((chan_opts.attr.num_subbuf - 1) & chan_opts.attr.num_subbuf)); |
1cb514ce | 463 | |
cf0bcb51 | 464 | DBG("Channel subbuf num set to %" PRIu64, chan_opts.attr.num_subbuf); |
7d29a247 | 465 | break; |
1cb514ce | 466 | } |
7d29a247 | 467 | case OPT_SWITCH_TIMER: |
16068db5 MD |
468 | { |
469 | unsigned long v; | |
470 | ||
471 | errno = 0; | |
1cb514ce MD |
472 | opt_arg = poptGetOptArg(pc); |
473 | v = strtoul(opt_arg, NULL, 0); | |
474 | if (errno != 0 || !isdigit(opt_arg[0])) { | |
475 | ERR("Wrong value in --switch-timer parameter: %s", opt_arg); | |
16068db5 MD |
476 | ret = CMD_ERROR; |
477 | goto end; | |
478 | } | |
479 | if (v != (uint32_t) v) { | |
480 | ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg); | |
481 | ret = CMD_ERROR; | |
482 | goto end; | |
483 | } | |
cf0bcb51 JG |
484 | chan_opts.attr.switch_timer_interval = (uint32_t) v; |
485 | DBG("Channel switch timer interval set to %d", chan_opts.attr.switch_timer_interval); | |
7d29a247 | 486 | break; |
16068db5 | 487 | } |
7d29a247 | 488 | case OPT_READ_TIMER: |
16068db5 MD |
489 | { |
490 | unsigned long v; | |
491 | ||
492 | errno = 0; | |
1cb514ce MD |
493 | opt_arg = poptGetOptArg(pc); |
494 | v = strtoul(opt_arg, NULL, 0); | |
495 | if (errno != 0 || !isdigit(opt_arg[0])) { | |
496 | ERR("Wrong value in --read-timer parameter: %s", opt_arg); | |
16068db5 MD |
497 | ret = CMD_ERROR; |
498 | goto end; | |
499 | } | |
500 | if (v != (uint32_t) v) { | |
501 | ERR("32-bit overflow in --read-timer parameter: %s", opt_arg); | |
502 | ret = CMD_ERROR; | |
503 | goto end; | |
504 | } | |
cf0bcb51 JG |
505 | chan_opts.attr.read_timer_interval = (uint32_t) v; |
506 | DBG("Channel read timer interval set to %d", chan_opts.attr.read_timer_interval); | |
507 | break; | |
508 | } | |
509 | case OPT_MONITOR_TIMER: | |
510 | { | |
511 | unsigned long v; | |
512 | ||
513 | errno = 0; | |
514 | opt_arg = poptGetOptArg(pc); | |
515 | v = strtoul(opt_arg, NULL, 0); | |
516 | if (errno != 0 || !isdigit(opt_arg[0])) { | |
517 | ERR("Wrong value in --monitor-timer parameter: %s", opt_arg); | |
518 | ret = CMD_ERROR; | |
519 | goto end; | |
520 | } | |
521 | if (v != (uint32_t) v) { | |
522 | ERR("32-bit overflow in --monitor-timer parameter: %s", opt_arg); | |
523 | ret = CMD_ERROR; | |
524 | goto end; | |
525 | } | |
526 | opt_monitor_timer.interval = (uint32_t) v; | |
527 | opt_monitor_timer.set = true; | |
528 | DBG("Channel monitor timer interval set to %d", opt_monitor_timer.interval); | |
d36b8583 | 529 | break; |
16068db5 | 530 | } |
eeac7d46 MD |
531 | case OPT_USERSPACE: |
532 | opt_userspace = 1; | |
eeac7d46 | 533 | break; |
1624d5b7 | 534 | case OPT_TRACEFILE_SIZE: |
1cb514ce | 535 | opt_arg = poptGetOptArg(pc); |
cf0bcb51 | 536 | if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.tracefile_size) < 0) { |
1cb514ce | 537 | ERR("Wrong value in --tracefile-size parameter: %s", opt_arg); |
16068db5 MD |
538 | ret = CMD_ERROR; |
539 | goto end; | |
540 | } | |
1624d5b7 | 541 | DBG("Maximum tracefile size set to %" PRIu64, |
cf0bcb51 | 542 | chan_opts.attr.tracefile_size); |
1624d5b7 JD |
543 | break; |
544 | case OPT_TRACEFILE_COUNT: | |
16068db5 MD |
545 | { |
546 | unsigned long v; | |
547 | ||
548 | errno = 0; | |
1cb514ce MD |
549 | opt_arg = poptGetOptArg(pc); |
550 | v = strtoul(opt_arg, NULL, 0); | |
551 | if (errno != 0 || !isdigit(opt_arg[0])) { | |
552 | ERR("Wrong value in --tracefile-count parameter: %s", opt_arg); | |
16068db5 MD |
553 | ret = CMD_ERROR; |
554 | goto end; | |
555 | } | |
556 | if (v != (uint32_t) v) { | |
557 | ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg); | |
558 | ret = CMD_ERROR; | |
559 | goto end; | |
560 | } | |
cf0bcb51 | 561 | chan_opts.attr.tracefile_count = (uint32_t) v; |
1624d5b7 | 562 | DBG("Maximum tracefile count set to %" PRIu64, |
cf0bcb51 | 563 | chan_opts.attr.tracefile_count); |
1624d5b7 | 564 | break; |
16068db5 | 565 | } |
679b4943 SM |
566 | case OPT_LIST_OPTIONS: |
567 | list_cmd_options(stdout, long_options); | |
679b4943 | 568 | goto end; |
d36b8583 | 569 | default: |
d36b8583 DG |
570 | ret = CMD_UNDEFINED; |
571 | goto end; | |
572 | } | |
573 | } | |
574 | ||
3ecec76a PP |
575 | ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace); |
576 | if (ret) { | |
577 | ret = CMD_ERROR; | |
578 | goto end; | |
579 | } | |
580 | ||
acc09215 JRJ |
581 | /* Mi check */ |
582 | if (lttng_opt_mi) { | |
583 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
584 | if (!writer) { | |
585 | ret = -LTTNG_ERR_NOMEM; | |
586 | goto end; | |
587 | } | |
588 | ||
589 | /* Open command element */ | |
590 | ret = mi_lttng_writer_command_open(writer, | |
591 | mi_lttng_element_command_enable_channels); | |
592 | if (ret) { | |
593 | ret = CMD_ERROR; | |
594 | goto end; | |
595 | } | |
596 | ||
597 | /* Open output element */ | |
598 | ret = mi_lttng_writer_open_element(writer, | |
599 | mi_lttng_element_command_output); | |
600 | if (ret) { | |
601 | ret = CMD_ERROR; | |
602 | goto end; | |
603 | } | |
604 | } | |
605 | ||
d36b8583 DG |
606 | opt_channels = (char*) poptGetArg(pc); |
607 | if (opt_channels == NULL) { | |
7d29a247 | 608 | ERR("Missing channel name.\n"); |
ca1c3607 | 609 | ret = CMD_ERROR; |
acc09215 JRJ |
610 | success = 0; |
611 | goto mi_closing; | |
d36b8583 DG |
612 | } |
613 | ||
cd80958d DG |
614 | if (!opt_session_name) { |
615 | session_name = get_session_name(); | |
616 | if (session_name == NULL) { | |
acc09215 JRJ |
617 | command_ret = CMD_ERROR; |
618 | success = 0; | |
619 | goto mi_closing; | |
cd80958d DG |
620 | } |
621 | } else { | |
622 | session_name = opt_session_name; | |
623 | } | |
624 | ||
acc09215 JRJ |
625 | command_ret = enable_channel(session_name); |
626 | if (command_ret) { | |
627 | success = 0; | |
628 | } | |
629 | ||
630 | mi_closing: | |
631 | /* Mi closing */ | |
632 | if (lttng_opt_mi) { | |
633 | /* Close output element */ | |
634 | ret = mi_lttng_writer_close_element(writer); | |
635 | if (ret) { | |
636 | goto end; | |
637 | } | |
638 | ||
639 | /* Success ? */ | |
640 | ret = mi_lttng_writer_write_element_bool(writer, | |
641 | mi_lttng_element_command_success, success); | |
642 | if (ret) { | |
643 | goto end; | |
644 | } | |
645 | ||
646 | /* Command element close */ | |
647 | ret = mi_lttng_writer_command_close(writer); | |
648 | if (ret) { | |
649 | goto end; | |
650 | } | |
651 | } | |
d36b8583 DG |
652 | |
653 | end: | |
acc09215 JRJ |
654 | /* Mi clean-up */ |
655 | if (writer && mi_lttng_writer_destroy(writer)) { | |
656 | /* Preserve original error code */ | |
657 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; | |
658 | } | |
659 | ||
5853fd43 DG |
660 | if (!opt_session_name && session_name) { |
661 | free(session_name); | |
662 | } | |
acc09215 JRJ |
663 | |
664 | /* Overwrite ret if an error occurred when enable_channel */ | |
665 | ret = command_ret ? command_ret : ret; | |
ca1c3607 | 666 | poptFreeContext(pc); |
d36b8583 DG |
667 | return ret; |
668 | } |