2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
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.
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
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.
26 #include <sys/types.h>
30 #include <common/utils.h>
31 #include <common/mi-lttng.h>
32 #include <lttng/snapshot.h>
34 #include "../command.h"
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
;
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
);
49 static const char *indent4
= " ";
58 static struct mi_writer
*writer
;
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
},
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 */
84 static void usage(FILE *ofp
)
86 fprintf(ofp
, "usage: lttng snapshot [OPTION] ACTION\n");
88 fprintf(ofp
, "Actions:\n");
89 fprintf(ofp
, " add-output [-m <SIZE>] [-s <NAME>] [-n <NAME>] <URL> | -C <URL> -D <URL>\n");
90 fprintf(ofp
, " Setup and add an snapshot output for a session.\n");
92 fprintf(ofp
, " del-output ID | NAME [-s <NAME>]\n");
93 fprintf(ofp
, " Delete an output for a session using the ID.\n");
95 fprintf(ofp
, " list-output [-s <NAME>]\n");
96 fprintf(ofp
, " List the output of a session.\n");
98 fprintf(ofp
, " record [-m <SIZE>] [-s <NAME>] [-n <NAME>] [<URL> | -C <URL> -D <URL>]\n");
99 fprintf(ofp
, " Snapshot a session's buffer(s) for all domains. If an URL is\n");
100 fprintf(ofp
, " specified, it is used instead of a previously added output.\n");
101 fprintf(ofp
, " Specifying only a name or/a size will override the current output value.\n");
102 fprintf(ofp
, " For instance, you can record a snapshot with a custom maximum size\n");
103 fprintf(ofp
, " or with a different name.\n");
105 fprintf(ofp
, "Options:\n");
106 fprintf(ofp
, " -h, --help Show this help\n");
107 fprintf(ofp
, " --list-options Simple listing of options\n");
108 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
109 fprintf(ofp
, " -n, --name NAME Name of the output or snapshot\n");
110 fprintf(ofp
, " -m, --max-size SIZE Maximum bytes size of the snapshot {+k,+M,+G}\n");
111 fprintf(ofp
, " -C, --ctrl-url URL Set control path URL. (Must use -D also)\n");
112 fprintf(ofp
, " -D, --data-url URL Set data path URL. (Must use -C also)\n");
117 * Count and return the number of arguments in argv.
119 static int count_arguments(const char **argv
)
125 while (argv
[i
] != NULL
) {
133 * Create a snapshot output object from arguments using the given URL.
135 * Return a newly allocated object or NULL on error.
137 static struct lttng_snapshot_output
*create_output_from_args(const char *url
)
140 struct lttng_snapshot_output
*output
= NULL
;
142 output
= lttng_snapshot_output_create();
148 ret
= lttng_snapshot_output_set_ctrl_url(url
, output
);
152 } else if (opt_ctrl_url
) {
153 ret
= lttng_snapshot_output_set_ctrl_url(opt_ctrl_url
, output
);
160 ret
= lttng_snapshot_output_set_data_url(opt_data_url
, output
);
167 ret
= lttng_snapshot_output_set_size(opt_max_size
, output
);
173 if (opt_output_name
) {
174 ret
= lttng_snapshot_output_set_name(opt_output_name
, output
);
183 lttng_snapshot_output_destroy(output
);
188 static int mi_list_output(void)
191 struct lttng_snapshot_output
*s_iter
;
192 struct lttng_snapshot_output_list
*list
;
196 ret
= lttng_snapshot_list_output(current_session_name
, &list
);
201 ret
= mi_lttng_snapshot_output_session_name(writer
, current_session_name
);
207 while ((s_iter
= lttng_snapshot_output_list_get_next(list
)) != NULL
) {
208 ret
= mi_lttng_snapshot_list_output(writer
, s_iter
);
216 /* Close snapshot snapshots element */
217 ret
= mi_lttng_writer_close_element(writer
);
223 /* Close snapshot session element */
224 ret
= mi_lttng_writer_close_element(writer
);
229 lttng_snapshot_output_list_destroy(list
);
234 static int list_output(void)
236 int ret
, output_seen
= 0;
237 struct lttng_snapshot_output
*s_iter
;
238 struct lttng_snapshot_output_list
*list
;
240 ret
= lttng_snapshot_list_output(current_session_name
, &list
);
245 MSG("Snapshot output list for session %s", current_session_name
);
247 while ((s_iter
= lttng_snapshot_output_list_get_next(list
)) != NULL
) {
248 MSG("%s[%" PRIu32
"] %s: %s (max-size: %" PRId64
")", indent4
,
249 lttng_snapshot_output_get_id(s_iter
),
250 lttng_snapshot_output_get_name(s_iter
),
251 lttng_snapshot_output_get_ctrl_url(s_iter
),
252 lttng_snapshot_output_get_maxsize(s_iter
));
256 lttng_snapshot_output_list_destroy(list
);
259 MSG("%sNone", indent4
);
267 * Delete output by ID (machine interface version).
269 static int mi_del_output(uint32_t id
, const char *name
)
272 struct lttng_snapshot_output
*output
= NULL
;
276 output
= lttng_snapshot_output_create();
283 ret
= lttng_snapshot_output_set_name(name
, output
);
284 } else if (id
!= UINT32_MAX
) {
285 ret
= lttng_snapshot_output_set_id(id
, output
);
295 ret
= lttng_snapshot_del_output(current_session_name
, output
);
301 ret
= mi_lttng_snapshot_del_output(writer
, id
, name
, current_session_name
);
307 lttng_snapshot_output_destroy(output
);
312 * Delete output by ID.
314 static int del_output(uint32_t id
, const char *name
)
317 struct lttng_snapshot_output
*output
= NULL
;
319 output
= lttng_snapshot_output_create();
326 ret
= lttng_snapshot_output_set_name(name
, output
);
327 } else if (id
!= UINT32_MAX
) {
328 ret
= lttng_snapshot_output_set_id(id
, output
);
338 ret
= lttng_snapshot_del_output(current_session_name
, output
);
343 if (id
!= UINT32_MAX
) {
344 MSG("Snapshot output id %" PRIu32
" successfully deleted for session %s",
345 id
, current_session_name
);
347 MSG("Snapshot output %s successfully deleted for session %s",
348 name
, current_session_name
);
352 lttng_snapshot_output_destroy(output
);
357 * Add output from the user URL (machine interface).
359 static int mi_add_output(const char *url
)
362 struct lttng_snapshot_output
*output
= NULL
;
366 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
371 output
= create_output_from_args(url
);
377 /* This call, if successful, populates the id of the output object. */
378 ret
= lttng_snapshot_add_output(current_session_name
, output
);
384 n_ptr
= lttng_snapshot_output_get_name(output
);
385 if (*n_ptr
== '\0') {
387 pret
= snprintf(name
, sizeof(name
), DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
388 lttng_snapshot_output_get_id(output
));
390 PERROR("snprintf add output name");
395 ret
= mi_lttng_snapshot_add_output(writer
, current_session_name
, n_ptr
,
402 lttng_snapshot_output_destroy(output
);
407 * Add output from the user URL.
409 static int add_output(const char *url
)
412 struct lttng_snapshot_output
*output
= NULL
;
416 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
421 output
= create_output_from_args(url
);
427 /* This call, if successful, populates the id of the output object. */
428 ret
= lttng_snapshot_add_output(current_session_name
, output
);
433 n_ptr
= lttng_snapshot_output_get_name(output
);
434 if (*n_ptr
== '\0') {
436 pret
= snprintf(name
, sizeof(name
), DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
437 lttng_snapshot_output_get_id(output
));
439 PERROR("snprintf add output name");
444 MSG("Snapshot output successfully added for session %s",
445 current_session_name
);
446 MSG(" [%" PRIu32
"] %s: %s (max-size: %" PRId64
")",
447 lttng_snapshot_output_get_id(output
), n_ptr
,
448 lttng_snapshot_output_get_ctrl_url(output
),
449 lttng_snapshot_output_get_maxsize(output
));
451 lttng_snapshot_output_destroy(output
);
455 static int cmd_add_output(int argc
, const char **argv
)
459 if (argc
< 2 && (!opt_data_url
|| !opt_ctrl_url
)) {
466 ret
= mi_add_output(argv
[1]);
468 ret
= add_output(argv
[1]);
475 static int cmd_del_output(int argc
, const char **argv
)
488 id
= strtol(argv
[1], &name
, 10);
489 if (id
== 0 && errno
== 0) {
491 ret
= mi_del_output(UINT32_MAX
, name
);
493 ret
= del_output(UINT32_MAX
, name
);
495 } else if (errno
== 0 && *name
== '\0') {
497 ret
= mi_del_output(id
, NULL
);
499 ret
= del_output(id
, NULL
);
502 ERR("Argument %s not recognized", argv
[1]);
511 static int cmd_list_output(int argc
, const char **argv
)
516 ret
= mi_list_output();
525 * Do a snapshot record with the URL if one is given (machine interface).
527 static int mi_record(const char *url
)
530 struct lttng_snapshot_output
*output
= NULL
;
532 output
= create_output_from_args(url
);
538 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
544 ret
= mi_lttng_snapshot_record(writer
, current_session_name
, url
,
545 opt_ctrl_url
, opt_data_url
);
551 lttng_snapshot_output_destroy(output
);
556 * Do a snapshot record with the URL if one is given.
558 static int record(const char *url
)
561 struct lttng_snapshot_output
*output
= NULL
;
563 output
= create_output_from_args(url
);
569 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
571 if (ret
== -LTTNG_ERR_MAX_SIZE_INVALID
) {
572 ERR("The minimum size of a snapshot is computed by multiplying "
573 "the total amount of streams with the largest subbuffer "
579 MSG("Snapshot recorded successfully for session %s", current_session_name
);
582 MSG("Snapshot written at: %s", url
);
583 } else if (opt_ctrl_url
) {
584 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url
,
589 lttng_snapshot_output_destroy(output
);
593 static int cmd_record(int argc
, const char **argv
)
598 /* With a given URL */
600 ret
= mi_record(argv
[1]);
602 ret
= record(argv
[1]);
606 ret
= mi_record(NULL
);
615 static int handle_command(const char **argv
)
617 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
618 struct cmd_struct
*cmd
;
620 if (argv
== NULL
|| (!opt_ctrl_url
&& opt_data_url
) ||
621 (opt_ctrl_url
&& !opt_data_url
)) {
623 command_ret
= CMD_ERROR
;
627 argc
= count_arguments(argv
);
630 while (cmd
->func
!= NULL
) {
632 if (strcmp(argv
[0], cmd
->name
) == 0) {
635 ret
= mi_lttng_writer_open_element(writer
,
636 mi_lttng_element_command_action
);
642 /* Name of the action */
643 ret
= mi_lttng_writer_write_element_string(writer
,
644 config_element_name
, argv
[0]);
650 /* Open output element */
651 ret
= mi_lttng_writer_open_element(writer
,
652 mi_lttng_element_command_output
);
659 command_ret
= cmd
->func(argc
, argv
);
662 /* Close output and action element */
663 ret
= mi_lttng_close_multi_element(writer
, 2);
675 ret
= -CMD_UNDEFINED
;
678 /* Overwrite ret if an error occured in cmd->func() */
679 ret
= command_ret
? command_ret
: ret
;
683 * The 'snapshot <cmd> <options>' first level command
685 int cmd_snapshot(int argc
, const char **argv
)
687 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
688 char *session_name
= NULL
;
689 static poptContext pc
;
691 pc
= poptGetContext(NULL
, argc
, argv
, snapshot_opts
, 0);
692 poptReadDefaultConfig(pc
, 0);
696 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
698 ret
= -LTTNG_ERR_NOMEM
;
702 /* Open command element */
703 ret
= mi_lttng_writer_command_open(writer
,
704 mi_lttng_element_command_snapshot
);
710 /* Open output element */
711 ret
= mi_lttng_writer_open_element(writer
,
712 mi_lttng_element_command_output
);
719 while ((opt
= poptGetNextOpt(pc
)) != -1) {
724 case OPT_LIST_OPTIONS
:
725 list_cmd_options(stdout
, snapshot_opts
);
727 case OPT_LIST_COMMANDS
:
728 list_commands(actions
, stdout
);
733 const char *opt
= poptGetOptArg(pc
);
735 if (utils_parse_size_suffix((char *) opt
, &val
) < 0) {
736 ERR("Unable to handle max-size value %s", opt
);
752 if (!opt_session_name
) {
753 session_name
= get_session_name();
754 if (session_name
== NULL
) {
758 current_session_name
= session_name
;
760 current_session_name
= opt_session_name
;
763 command_ret
= handle_command(poptGetArgs(pc
));
764 if (command_ret
< 0) {
765 switch (-command_ret
) {
766 case LTTNG_ERR_EPERM
:
767 ERR("The session needs to be set in no output mode (--no-output)");
769 case LTTNG_ERR_SNAPSHOT_NODATA
:
770 WARN("%s", lttng_strerror(command_ret
));
773 ERR("%s", lttng_strerror(command_ret
));
780 /* Close output element */
781 ret
= mi_lttng_writer_close_element(writer
);
788 ret
= mi_lttng_writer_write_element_bool(writer
,
789 mi_lttng_element_command_success
, success
);
795 /* Command element close */
796 ret
= mi_lttng_writer_command_close(writer
);
805 if (writer
&& mi_lttng_writer_destroy(writer
)) {
806 /* Preserve original error code */
807 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
810 if (!opt_session_name
) {
814 /* Overwrite ret if an error occured during handle_command */
815 ret
= command_ret
? command_ret
: ret
;