846e9badc168c07da31dfea7e20579b8871d4dc2
[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("Invalid snapshot size. Cannot fit at least one packet per stream.");
572 }
573 goto error;
574 }
575
576 MSG("Snapshot recorded successfully for session %s", current_session_name);
577
578 if (url) {
579 MSG("Snapshot written at: %s", url);
580 } else if (opt_ctrl_url) {
581 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url,
582 opt_data_url);
583 }
584
585 error:
586 lttng_snapshot_output_destroy(output);
587 return ret;
588 }
589
590 static int cmd_record(int argc, const char **argv)
591 {
592 int ret;
593
594 if (argc == 2) {
595 /* With a given URL */
596 if (lttng_opt_mi) {
597 ret = mi_record(argv[1]);
598 } else {
599 ret = record(argv[1]);
600 }
601 } else {
602 if (lttng_opt_mi) {
603 ret = mi_record(NULL);
604 } else {
605 ret = record(NULL);
606 }
607 }
608
609 return ret;
610 }
611
612 static int handle_command(const char **argv)
613 {
614 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
615 struct cmd_struct *cmd;
616
617 if (argv == NULL || (!opt_ctrl_url && opt_data_url) ||
618 (opt_ctrl_url && !opt_data_url)) {
619 usage(stderr);
620 command_ret = CMD_ERROR;
621 goto end;
622 }
623
624 argc = count_arguments(argv);
625
626 cmd = &actions[i];
627 while (cmd->func != NULL) {
628 /* Find command */
629 if (strcmp(argv[0], cmd->name) == 0) {
630 if (lttng_opt_mi) {
631 /* Action element */
632 ret = mi_lttng_writer_open_element(writer,
633 mi_lttng_element_command_action);
634 if (ret) {
635 ret = CMD_ERROR;
636 goto end;
637 }
638
639 /* Name of the action */
640 ret = mi_lttng_writer_write_element_string(writer,
641 config_element_name, argv[0]);
642 if (ret) {
643 ret = CMD_ERROR;
644 goto end;
645 }
646
647 /* Open output element */
648 ret = mi_lttng_writer_open_element(writer,
649 mi_lttng_element_command_output);
650 if (ret) {
651 ret = CMD_ERROR;
652 goto end;
653 }
654 }
655
656 command_ret = cmd->func(argc, argv);
657
658 if (lttng_opt_mi) {
659 /* Close output and action element */
660 ret = mi_lttng_close_multi_element(writer, 2);
661 if (ret) {
662 ret = CMD_ERROR;
663 goto end;
664 }
665 }
666 goto end;
667 }
668 i++;
669 cmd = &actions[i];
670 }
671
672 ret = CMD_UNDEFINED;
673
674 end:
675 /* Overwrite ret if an error occurred in cmd->func() */
676 ret = command_ret ? command_ret : ret;
677 return ret;
678 }
679 /*
680 * The 'snapshot <cmd> <options>' first level command
681 */
682 int cmd_snapshot(int argc, const char **argv)
683 {
684 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
685 char *session_name = NULL;
686 static poptContext pc;
687
688 pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
689 poptReadDefaultConfig(pc, 0);
690
691 /* Mi check */
692 if (lttng_opt_mi) {
693 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
694 if (!writer) {
695 ret = -LTTNG_ERR_NOMEM;
696 goto end;
697 }
698
699 /* Open command element */
700 ret = mi_lttng_writer_command_open(writer,
701 mi_lttng_element_command_snapshot);
702 if (ret) {
703 ret = CMD_ERROR;
704 goto end;
705 }
706
707 /* Open output element */
708 ret = mi_lttng_writer_open_element(writer,
709 mi_lttng_element_command_output);
710 if (ret) {
711 ret = CMD_ERROR;
712 goto end;
713 }
714 }
715
716 while ((opt = poptGetNextOpt(pc)) != -1) {
717 switch (opt) {
718 case OPT_HELP:
719 usage(stdout);
720 goto end;
721 case OPT_LIST_OPTIONS:
722 list_cmd_options(stdout, snapshot_opts);
723 goto end;
724 case OPT_LIST_COMMANDS:
725 list_commands(actions, stdout);
726 goto end;
727 case OPT_MAX_SIZE:
728 {
729 uint64_t val;
730 const char *opt = poptGetOptArg(pc);
731
732 if (utils_parse_size_suffix((char *) opt, &val) < 0) {
733 ERR("Unable to handle max-size value %s", opt);
734 ret = CMD_ERROR;
735 goto end;
736 }
737
738 opt_max_size = val;
739
740 break;
741 }
742 default:
743 usage(stderr);
744 ret = CMD_UNDEFINED;
745 goto end;
746 }
747 }
748
749 if (!opt_session_name) {
750 session_name = get_session_name();
751 if (session_name == NULL) {
752 ret = CMD_ERROR;
753 goto end;
754 }
755 current_session_name = session_name;
756 } else {
757 current_session_name = opt_session_name;
758 }
759
760 command_ret = handle_command(poptGetArgs(pc));
761 if (command_ret) {
762 switch (-command_ret) {
763 case LTTNG_ERR_EPERM:
764 ERR("The session needs to be set in no output mode (--no-output)");
765 break;
766 case LTTNG_ERR_SNAPSHOT_NODATA:
767 WARN("%s", lttng_strerror(command_ret));
768 break;
769 default:
770 ERR("%s", lttng_strerror(command_ret));
771 break;
772 }
773 success = 0;
774 }
775
776 if (lttng_opt_mi) {
777 /* Close output element */
778 ret = mi_lttng_writer_close_element(writer);
779 if (ret) {
780 ret = CMD_ERROR;
781 goto end;
782 }
783
784 /* Success ? */
785 ret = mi_lttng_writer_write_element_bool(writer,
786 mi_lttng_element_command_success, success);
787 if (ret) {
788 ret = CMD_ERROR;
789 goto end;
790 }
791
792 /* Command element close */
793 ret = mi_lttng_writer_command_close(writer);
794 if (ret) {
795 ret = CMD_ERROR;
796 goto end;
797 }
798 }
799
800 end:
801 /* Mi clean-up */
802 if (writer && mi_lttng_writer_destroy(writer)) {
803 /* Preserve original error code */
804 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
805 }
806
807 if (!opt_session_name) {
808 free(session_name);
809 }
810
811 /* Overwrite ret if an error occured during handle_command */
812 ret = command_ret ? command_ret : ret;
813 poptFreeContext(pc);
814 return ret;
815 }
This page took 0.046421 seconds and 4 git commands to generate.