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