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