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