lttng: remove usage strings from commands
[lttng-tools.git] / src / bin / lttng / commands / snapshot.c
1 /*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <inttypes.h>
21 #include <popt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <assert.h>
29
30 #include <common/utils.h>
31 #include <common/mi-lttng.h>
32 #include <lttng/snapshot.h>
33
34 #include "../command.h"
35
36 static const char *opt_session_name;
37 static const char *opt_output_name;
38 static const char *opt_data_url;
39 static const char *opt_ctrl_url;
40 static const char *current_session_name;
41 static uint64_t opt_max_size;
42
43 /* Stub for the cmd struct actions. */
44 static int cmd_add_output(int argc, const char **argv);
45 static int cmd_del_output(int argc, const char **argv);
46 static int cmd_list_output(int argc, const char **argv);
47 static int cmd_record(int argc, const char **argv);
48
49 static const char *indent4 = " ";
50
51 enum {
52 OPT_HELP = 1,
53 OPT_LIST_OPTIONS,
54 OPT_MAX_SIZE,
55 OPT_LIST_COMMANDS,
56 };
57
58 static struct mi_writer *writer;
59
60 static struct poptOption snapshot_opts[] = {
61 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
62 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
63 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
64 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
65 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
66 {"name", 'n', POPT_ARG_STRING, &opt_output_name, 0, 0, 0},
67 {"max-size", 'm', POPT_ARG_STRING, 0, OPT_MAX_SIZE, 0, 0},
68 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
69 {"list-commands", 0, POPT_ARG_NONE, NULL, OPT_LIST_COMMANDS},
70 {0, 0, 0, 0, 0, 0, 0}
71 };
72
73 static struct cmd_struct actions[] = {
74 { "add-output", cmd_add_output },
75 { "del-output", cmd_del_output },
76 { "list-output", cmd_list_output },
77 { "record", cmd_record },
78 { NULL, NULL } /* Array closure */
79 };
80
81 /*
82 * Count and return the number of arguments in argv.
83 */
84 static int count_arguments(const char **argv)
85 {
86 int i = 0;
87
88 assert(argv);
89
90 while (argv[i] != NULL) {
91 i++;
92 }
93
94 return i;
95 }
96
97 /*
98 * Create a snapshot output object from arguments using the given URL.
99 *
100 * Return a newly allocated object or NULL on error.
101 */
102 static struct lttng_snapshot_output *create_output_from_args(const char *url)
103 {
104 int ret = 0;
105 struct lttng_snapshot_output *output = NULL;
106
107 output = lttng_snapshot_output_create();
108 if (!output) {
109 goto error_create;
110 }
111
112 if (url) {
113 ret = lttng_snapshot_output_set_ctrl_url(url, output);
114 if (ret < 0) {
115 goto error;
116 }
117 } else if (opt_ctrl_url) {
118 ret = lttng_snapshot_output_set_ctrl_url(opt_ctrl_url, output);
119 if (ret < 0) {
120 goto error;
121 }
122 }
123
124 if (opt_data_url) {
125 ret = lttng_snapshot_output_set_data_url(opt_data_url, output);
126 if (ret < 0) {
127 goto error;
128 }
129 }
130
131 if (opt_max_size) {
132 ret = lttng_snapshot_output_set_size(opt_max_size, output);
133 if (ret < 0) {
134 goto error;
135 }
136 }
137
138 if (opt_output_name) {
139 ret = lttng_snapshot_output_set_name(opt_output_name, output);
140 if (ret < 0) {
141 goto error;
142 }
143 }
144
145 return output;
146
147 error:
148 lttng_snapshot_output_destroy(output);
149 error_create:
150 return NULL;
151 }
152
153 static int mi_list_output(void)
154 {
155 int ret;
156 struct lttng_snapshot_output *s_iter;
157 struct lttng_snapshot_output_list *list;
158
159 assert(writer);
160
161 ret = lttng_snapshot_list_output(current_session_name, &list);
162 if (ret < 0) {
163 goto error;
164 }
165
166 ret = mi_lttng_snapshot_output_session_name(writer, current_session_name);
167 if (ret) {
168 ret = CMD_ERROR;
169 goto end;
170 }
171
172 while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) {
173 ret = mi_lttng_snapshot_list_output(writer, s_iter);
174 if (ret) {
175 ret = CMD_ERROR;
176 goto end;
177 }
178 }
179
180
181 /* Close snapshot snapshots element */
182 ret = mi_lttng_writer_close_element(writer);
183 if (ret) {
184 ret = CMD_ERROR;
185 goto end;
186 }
187
188 /* Close snapshot session element */
189 ret = mi_lttng_writer_close_element(writer);
190 if (ret) {
191 ret = CMD_ERROR;
192 }
193 end:
194 lttng_snapshot_output_list_destroy(list);
195 error:
196 return ret;
197 }
198
199 static int list_output(void)
200 {
201 int ret, output_seen = 0;
202 struct lttng_snapshot_output *s_iter;
203 struct lttng_snapshot_output_list *list;
204
205 ret = lttng_snapshot_list_output(current_session_name, &list);
206 if (ret < 0) {
207 goto error;
208 }
209
210 MSG("Snapshot output list for session %s", current_session_name);
211
212 while ((s_iter = lttng_snapshot_output_list_get_next(list)) != NULL) {
213 MSG("%s[%" PRIu32 "] %s: %s (max-size: %" PRId64 ")", indent4,
214 lttng_snapshot_output_get_id(s_iter),
215 lttng_snapshot_output_get_name(s_iter),
216 lttng_snapshot_output_get_ctrl_url(s_iter),
217 lttng_snapshot_output_get_maxsize(s_iter));
218 output_seen = 1;
219 }
220
221 lttng_snapshot_output_list_destroy(list);
222
223 if (!output_seen) {
224 MSG("%sNone", indent4);
225 }
226
227 error:
228 return ret;
229 }
230
231 /*
232 * Delete output by ID (machine interface version).
233 */
234 static int mi_del_output(uint32_t id, const char *name)
235 {
236 int ret;
237 struct lttng_snapshot_output *output = NULL;
238
239 assert(writer);
240
241 output = lttng_snapshot_output_create();
242 if (!output) {
243 ret = CMD_FATAL;
244 goto error;
245 }
246
247 if (name) {
248 ret = lttng_snapshot_output_set_name(name, output);
249 } else if (id != UINT32_MAX) {
250 ret = lttng_snapshot_output_set_id(id, output);
251 } else {
252 ret = CMD_ERROR;
253 goto error;
254 }
255 if (ret < 0) {
256 ret = CMD_FATAL;
257 goto error;
258 }
259
260 ret = lttng_snapshot_del_output(current_session_name, output);
261 if (ret < 0) {
262 goto error;
263 }
264
265 ret = mi_lttng_snapshot_del_output(writer, id, name, current_session_name);
266 if (ret) {
267 ret = CMD_ERROR;
268 }
269
270 error:
271 lttng_snapshot_output_destroy(output);
272 return ret;
273 }
274
275 /*
276 * Delete output by ID.
277 */
278 static int del_output(uint32_t id, const char *name)
279 {
280 int ret;
281 struct lttng_snapshot_output *output = NULL;
282
283 output = lttng_snapshot_output_create();
284 if (!output) {
285 ret = CMD_FATAL;
286 goto error;
287 }
288
289 if (name) {
290 ret = lttng_snapshot_output_set_name(name, output);
291 } else if (id != UINT32_MAX) {
292 ret = lttng_snapshot_output_set_id(id, output);
293 } else {
294 ret = CMD_ERROR;
295 goto error;
296 }
297 if (ret < 0) {
298 ret = CMD_FATAL;
299 goto error;
300 }
301
302 ret = lttng_snapshot_del_output(current_session_name, output);
303 if (ret < 0) {
304 goto error;
305 }
306
307 if (id != UINT32_MAX) {
308 MSG("Snapshot output id %" PRIu32 " successfully deleted for session %s",
309 id, current_session_name);
310 } else {
311 MSG("Snapshot output %s successfully deleted for session %s",
312 name, current_session_name);
313 }
314
315 error:
316 lttng_snapshot_output_destroy(output);
317 return ret;
318 }
319
320 /*
321 * Add output from the user URL (machine interface).
322 */
323 static int mi_add_output(const char *url)
324 {
325 int ret;
326 struct lttng_snapshot_output *output = NULL;
327 char name[NAME_MAX];
328 const char *n_ptr;
329
330 if (!url && (!opt_data_url || !opt_ctrl_url)) {
331 ret = CMD_ERROR;
332 goto error;
333 }
334
335 output = create_output_from_args(url);
336 if (!output) {
337 ret = CMD_FATAL;
338 goto error;
339 }
340
341 /* This call, if successful, populates the id of the output object. */
342 ret = lttng_snapshot_add_output(current_session_name, output);
343 if (ret < 0) {
344 goto error;
345 }
346
347 n_ptr = lttng_snapshot_output_get_name(output);
348 if (*n_ptr == '\0') {
349 int pret;
350 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
351 lttng_snapshot_output_get_id(output));
352 if (pret < 0) {
353 PERROR("snprintf add output name");
354 }
355 n_ptr = name;
356 }
357
358 ret = mi_lttng_snapshot_add_output(writer, current_session_name, n_ptr,
359 output);
360 if (ret) {
361 ret = CMD_ERROR;
362 }
363
364 error:
365 lttng_snapshot_output_destroy(output);
366 return ret;
367 }
368
369 /*
370 * Add output from the user URL.
371 */
372 static int add_output(const char *url)
373 {
374 int ret;
375 struct lttng_snapshot_output *output = NULL;
376 char name[NAME_MAX];
377 const char *n_ptr;
378
379 if (!url && (!opt_data_url || !opt_ctrl_url)) {
380 ret = CMD_ERROR;
381 goto error;
382 }
383
384 output = create_output_from_args(url);
385 if (!output) {
386 ret = CMD_FATAL;
387 goto error;
388 }
389
390 /* This call, if successful, populates the id of the output object. */
391 ret = lttng_snapshot_add_output(current_session_name, output);
392 if (ret < 0) {
393 goto error;
394 }
395
396 n_ptr = lttng_snapshot_output_get_name(output);
397 if (*n_ptr == '\0') {
398 int pret;
399 pret = snprintf(name, sizeof(name), DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
400 lttng_snapshot_output_get_id(output));
401 if (pret < 0) {
402 PERROR("snprintf add output name");
403 }
404 n_ptr = name;
405 }
406
407 MSG("Snapshot output successfully added for session %s",
408 current_session_name);
409 MSG(" [%" PRIu32 "] %s: %s (max-size: %" PRId64 ")",
410 lttng_snapshot_output_get_id(output), n_ptr,
411 lttng_snapshot_output_get_ctrl_url(output),
412 lttng_snapshot_output_get_maxsize(output));
413 error:
414 lttng_snapshot_output_destroy(output);
415 return ret;
416 }
417
418 static int cmd_add_output(int argc, const char **argv)
419 {
420 int ret;
421
422 if (argc < 2 && (!opt_data_url || !opt_ctrl_url)) {
423 ret = CMD_ERROR;
424 goto end;
425 }
426
427 if (lttng_opt_mi) {
428 ret = mi_add_output(argv[1]);
429 } else {
430 ret = add_output(argv[1]);
431 }
432
433 end:
434 return ret;
435 }
436
437 static int cmd_del_output(int argc, const char **argv)
438 {
439 int ret;
440 char *name;
441 long id;
442
443 if (argc < 2) {
444 ret = CMD_ERROR;
445 goto end;
446 }
447
448 errno = 0;
449 id = strtol(argv[1], &name, 10);
450 if (id == 0 && errno == 0) {
451 if (lttng_opt_mi) {
452 ret = mi_del_output(UINT32_MAX, name);
453 } else {
454 ret = del_output(UINT32_MAX, name);
455 }
456 } else if (errno == 0 && *name == '\0') {
457 if (lttng_opt_mi) {
458 ret = mi_del_output(id, NULL);
459 } else {
460 ret = del_output(id, NULL);
461 }
462 } else {
463 ERR("Argument %s not recognized", argv[1]);
464 ret = -1;
465 goto end;
466 }
467
468 end:
469 return ret;
470 }
471
472 static int cmd_list_output(int argc, const char **argv)
473 {
474 int ret;
475
476 if (lttng_opt_mi) {
477 ret = mi_list_output();
478 } else {
479 ret = list_output();
480 }
481
482 return ret;
483 }
484
485 /*
486 * Do a snapshot record with the URL if one is given (machine interface).
487 */
488 static int mi_record(const char *url)
489 {
490 int ret;
491 struct lttng_snapshot_output *output = NULL;
492
493 output = create_output_from_args(url);
494 if (!output) {
495 ret = CMD_FATAL;
496 goto error;
497 }
498
499 ret = lttng_snapshot_record(current_session_name, output, 0);
500 if (ret < 0) {
501 ret = CMD_ERROR;
502 goto error;
503 }
504
505 ret = mi_lttng_snapshot_record(writer, current_session_name, url,
506 opt_ctrl_url, opt_data_url);
507 if (ret) {
508 ret = CMD_ERROR;
509 }
510
511 error:
512 lttng_snapshot_output_destroy(output);
513 return ret;
514 }
515
516 /*
517 * Do a snapshot record with the URL if one is given.
518 */
519 static int record(const char *url)
520 {
521 int ret;
522 struct lttng_snapshot_output *output = NULL;
523
524 output = create_output_from_args(url);
525 if (!output) {
526 ret = CMD_FATAL;
527 goto error;
528 }
529
530 ret = lttng_snapshot_record(current_session_name, output, 0);
531 if (ret < 0) {
532 if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) {
533 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
534 }
535 goto error;
536 }
537
538 MSG("Snapshot recorded successfully for session %s", current_session_name);
539
540 if (url) {
541 MSG("Snapshot written at: %s", url);
542 } else if (opt_ctrl_url) {
543 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
544 opt_data_url);
545 }
546
547 error:
548 lttng_snapshot_output_destroy(output);
549 return ret;
550 }
551
552 static int cmd_record(int argc, const char **argv)
553 {
554 int ret;
555
556 if (argc == 2) {
557 /* With a given URL */
558 if (lttng_opt_mi) {
559 ret = mi_record(argv[1]);
560 } else {
561 ret = record(argv[1]);
562 }
563 } else {
564 if (lttng_opt_mi) {
565 ret = mi_record(NULL);
566 } else {
567 ret = record(NULL);
568 }
569 }
570
571 return ret;
572 }
573
574 static int handle_command(const char **argv)
575 {
576 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
577 struct cmd_struct *cmd;
578
579 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
580 (opt_ctrl_url && !opt_data_url)) {
581 command_ret = CMD_ERROR;
582 goto end;
583 }
584
585 argc = count_arguments(argv);
586
587 cmd = &actions[i];
588 while (cmd->func != NULL) {
589 /* Find command */
590 if (strcmp(argv[0], cmd->name) == 0) {
591 if (lttng_opt_mi) {
592 /* Action element */
593 ret = mi_lttng_writer_open_element(writer,
594 mi_lttng_element_command_action);
595 if (ret) {
596 ret = CMD_ERROR;
597 goto end;
598 }
599
600 /* Name of the action */
601 ret = mi_lttng_writer_write_element_string(writer,
602 config_element_name, argv[0]);
603 if (ret) {
604 ret = CMD_ERROR;
605 goto end;
606 }
607
608 /* Open output element */
609 ret = mi_lttng_writer_open_element(writer,
610 mi_lttng_element_command_output);
611 if (ret) {
612 ret = CMD_ERROR;
613 goto end;
614 }
615 }
616
617 command_ret = cmd->func(argc, argv);
618
619 if (lttng_opt_mi) {
620 /* Close output and action element */
621 ret = mi_lttng_close_multi_element(writer, 2);
622 if (ret) {
623 ret = CMD_ERROR;
624 goto end;
625 }
626 }
627 goto end;
628 }
629 i++;
630 cmd = &actions[i];
631 }
632
633 ret = CMD_UNDEFINED;
634
635 end:
636 /* Overwrite ret if an error occurred in cmd->func() */
637 ret = command_ret ? command_ret : ret;
638 return ret;
639 }
640 /*
641 * The 'snapshot <cmd> <options>' first level command
642 */
643 int cmd_snapshot(int argc, const char **argv)
644 {
645 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
646 char *session_name = NULL;
647 static poptContext pc;
648
649 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
650 poptReadDefaultConfig(pc, 0);
651
652 /* Mi check */
653 if (lttng_opt_mi) {
654 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
655 if (!writer) {
656 ret = -LTTNG_ERR_NOMEM;
657 goto end;
658 }
659
660 /* Open command element */
661 ret = mi_lttng_writer_command_open(writer,
662 mi_lttng_element_command_snapshot);
663 if (ret) {
664 ret = CMD_ERROR;
665 goto end;
666 }
667
668 /* Open output element */
669 ret = mi_lttng_writer_open_element(writer,
670 mi_lttng_element_command_output);
671 if (ret) {
672 ret = CMD_ERROR;
673 goto end;
674 }
675 }
676
677 while ((opt = poptGetNextOpt(pc)) != -1) {
678 switch (opt) {
679 case OPT_HELP:
680 SHOW_HELP();
681 goto end;
682 case OPT_LIST_OPTIONS:
683 list_cmd_options(stdout, snapshot_opts);
684 goto end;
685 case OPT_LIST_COMMANDS:
686 list_commands(actions, stdout);
687 goto end;
688 case OPT_MAX_SIZE:
689 {
690 uint64_t val;
691 const char *opt = poptGetOptArg(pc);
692
693 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
694 ERR("Unable to handle max-size value %s", opt);
695 ret = CMD_ERROR;
696 goto end;
697 }
698
699 opt_max_size = val;
700
701 break;
702 }
703 default:
704 ret = CMD_UNDEFINED;
705 goto end;
706 }
707 }
708
709 if (!opt_session_name) {
710 session_name = get_session_name();
711 if (session_name == NULL) {
712 ret = CMD_ERROR;
713 goto end;
714 }
715 current_session_name = session_name;
716 } else {
717 current_session_name = opt_session_name;
718 }
719
720 command_ret = handle_command(poptGetArgs(pc));
721 if (command_ret) {
722 switch (-command_ret) {
723 case LTTNG_ERR_EPERM:
724 ERR("The session needs to be set in no output mode (--no-output)");
725 break;
726 case LTTNG_ERR_SNAPSHOT_NODATA:
727 WARN("%s", lttng_strerror(command_ret));
728 break;
729 default:
730 ERR("%s", lttng_strerror(command_ret));
731 break;
732 }
733 success = 0;
734 }
735
736 if (lttng_opt_mi) {
737 /* Close output element */
738 ret = mi_lttng_writer_close_element(writer);
739 if (ret) {
740 ret = CMD_ERROR;
741 goto end;
742 }
743
744 /* Success ? */
745 ret = mi_lttng_writer_write_element_bool(writer,
746 mi_lttng_element_command_success, success);
747 if (ret) {
748 ret = CMD_ERROR;
749 goto end;
750 }
751
752 /* Command element close */
753 ret = mi_lttng_writer_command_close(writer);
754 if (ret) {
755 ret = CMD_ERROR;
756 goto end;
757 }
758 }
759
760 end:
761 /* Mi clean-up */
762 if (writer && mi_lttng_writer_destroy(writer)) {
763 /* Preserve original error code */
764 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
765 }
766
767 if (!opt_session_name) {
768 free(session_name);
769 }
770
771 /* Overwrite ret if an error occured during handle_command */
772 ret = command_ret ? command_ret : ret;
773 poptFreeContext(pc);
774 return ret;
775 }
This page took 0.04458 seconds and 5 git commands to generate.