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