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.
27 #include <sys/types.h>
31 #include <common/utils.h>
32 #include <common/mi-lttng.h>
33 #include <lttng/snapshot.h>
35 #include "../command.h"
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
;
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
);
50 static const char *indent4
= " ";
59 static struct mi_writer
*writer
;
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
},
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 */
85 static void usage(FILE *ofp
)
87 fprintf(ofp
, "usage: lttng snapshot [OPTION] ACTION\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");
93 fprintf(ofp
, " del-output ID | NAME [-s <NAME>]\n");
94 fprintf(ofp
, " Delete an output for a session using the ID.\n");
96 fprintf(ofp
, " list-output [-s <NAME>]\n");
97 fprintf(ofp
, " List the output of a session.\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");
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");
118 * Count and return the number of arguments in argv.
120 static int count_arguments(const char **argv
)
126 while (argv
[i
] != NULL
) {
134 * Create a snapshot output object from arguments using the given URL.
136 * Return a newly allocated object or NULL on error.
138 static struct lttng_snapshot_output
*create_output_from_args(const char *url
)
141 struct lttng_snapshot_output
*output
= NULL
;
143 output
= lttng_snapshot_output_create();
149 ret
= lttng_snapshot_output_set_ctrl_url(url
, output
);
153 } else if (opt_ctrl_url
) {
154 ret
= lttng_snapshot_output_set_ctrl_url(opt_ctrl_url
, output
);
161 ret
= lttng_snapshot_output_set_data_url(opt_data_url
, output
);
168 ret
= lttng_snapshot_output_set_size(opt_max_size
, output
);
174 if (opt_output_name
) {
175 ret
= lttng_snapshot_output_set_name(opt_output_name
, output
);
184 lttng_snapshot_output_destroy(output
);
189 static int mi_list_output(void)
192 struct lttng_snapshot_output
*s_iter
;
193 struct lttng_snapshot_output_list
*list
;
197 ret
= lttng_snapshot_list_output(current_session_name
, &list
);
202 ret
= mi_lttng_snapshot_output_session_name(writer
, current_session_name
);
208 while ((s_iter
= lttng_snapshot_output_list_get_next(list
)) != NULL
) {
209 ret
= mi_lttng_snapshot_list_output(writer
, s_iter
);
217 /* Close snapshot snapshots element */
218 ret
= mi_lttng_writer_close_element(writer
);
224 /* Close snapshot session element */
225 ret
= mi_lttng_writer_close_element(writer
);
230 lttng_snapshot_output_list_destroy(list
);
235 static int list_output(void)
237 int ret
, output_seen
= 0;
238 struct lttng_snapshot_output
*s_iter
;
239 struct lttng_snapshot_output_list
*list
;
241 ret
= lttng_snapshot_list_output(current_session_name
, &list
);
246 MSG("Snapshot output list for session %s", current_session_name
);
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
));
257 lttng_snapshot_output_list_destroy(list
);
260 MSG("%sNone", indent4
);
268 * Delete output by ID (machine interface version).
270 static int mi_del_output(uint32_t id
, const char *name
)
273 struct lttng_snapshot_output
*output
= NULL
;
277 output
= lttng_snapshot_output_create();
284 ret
= lttng_snapshot_output_set_name(name
, output
);
285 } else if (id
!= UINT32_MAX
) {
286 ret
= lttng_snapshot_output_set_id(id
, output
);
296 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
);
383 n_ptr
= lttng_snapshot_output_get_name(output
);
384 if (*n_ptr
== '\0') {
386 pret
= snprintf(name
, sizeof(name
), DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
387 lttng_snapshot_output_get_id(output
));
389 PERROR("snprintf add output name");
394 ret
= mi_lttng_snapshot_add_output(writer
, current_session_name
, n_ptr
,
401 lttng_snapshot_output_destroy(output
);
406 * Add output from the user URL.
408 static int add_output(const char *url
)
411 struct lttng_snapshot_output
*output
= NULL
;
415 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
420 output
= create_output_from_args(url
);
426 /* This call, if successful, populates the id of the output object. */
427 ret
= lttng_snapshot_add_output(current_session_name
, output
);
432 n_ptr
= lttng_snapshot_output_get_name(output
);
433 if (*n_ptr
== '\0') {
435 pret
= snprintf(name
, sizeof(name
), DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
436 lttng_snapshot_output_get_id(output
));
438 PERROR("snprintf add output name");
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
));
450 lttng_snapshot_output_destroy(output
);
454 static int cmd_add_output(int argc
, const char **argv
)
458 if (argc
< 2 && (!opt_data_url
|| !opt_ctrl_url
)) {
465 ret
= mi_add_output(argv
[1]);
467 ret
= add_output(argv
[1]);
474 static int cmd_del_output(int argc
, const char **argv
)
487 id
= strtol(argv
[1], &name
, 10);
488 if (id
== 0 && errno
== 0) {
490 ret
= mi_del_output(UINT32_MAX
, name
);
492 ret
= del_output(UINT32_MAX
, name
);
494 } else if (errno
== 0 && *name
== '\0') {
496 ret
= mi_del_output(id
, NULL
);
498 ret
= del_output(id
, NULL
);
501 ERR("Argument %s not recognized", argv
[1]);
510 static int cmd_list_output(int argc
, const char **argv
)
515 ret
= mi_list_output();
524 * Do a snapshot record with the URL if one is given (machine interface).
526 static int mi_record(const char *url
)
529 struct lttng_snapshot_output
*output
= NULL
;
531 output
= create_output_from_args(url
);
537 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
543 ret
= mi_lttng_snapshot_record(writer
, current_session_name
, url
,
544 opt_ctrl_url
, opt_data_url
);
550 lttng_snapshot_output_destroy(output
);
555 * Do a snapshot record with the URL if one is given.
557 static int record(const char *url
)
560 struct lttng_snapshot_output
*output
= NULL
;
562 output
= create_output_from_args(url
);
568 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
570 if (ret
== -LTTNG_ERR_MAX_SIZE_INVALID
) {
571 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
576 MSG("Snapshot recorded successfully for session %s", current_session_name
);
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
,
586 lttng_snapshot_output_destroy(output
);
590 static int cmd_record(int argc
, const char **argv
)
595 /* With a given URL */
597 ret
= mi_record(argv
[1]);
599 ret
= record(argv
[1]);
603 ret
= mi_record(NULL
);
612 static int handle_command(const char **argv
)
614 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
615 struct cmd_struct
*cmd
;
617 if (argv
== NULL
|| (!opt_ctrl_url
&& opt_data_url
) ||
618 (opt_ctrl_url
&& !opt_data_url
)) {
620 command_ret
= CMD_ERROR
;
624 argc
= count_arguments(argv
);
627 while (cmd
->func
!= NULL
) {
629 if (strcmp(argv
[0], cmd
->name
) == 0) {
632 ret
= mi_lttng_writer_open_element(writer
,
633 mi_lttng_element_command_action
);
639 /* Name of the action */
640 ret
= mi_lttng_writer_write_element_string(writer
,
641 config_element_name
, argv
[0]);
647 /* Open output element */
648 ret
= mi_lttng_writer_open_element(writer
,
649 mi_lttng_element_command_output
);
656 command_ret
= cmd
->func(argc
, argv
);
659 /* Close output and action element */
660 ret
= mi_lttng_close_multi_element(writer
, 2);
675 /* Overwrite ret if an error occurred in cmd->func() */
676 ret
= command_ret
? command_ret
: ret
;
680 * The 'snapshot <cmd> <options>' first level command
682 int cmd_snapshot(int argc
, const char **argv
)
684 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
685 char *session_name
= NULL
;
686 static poptContext pc
;
688 pc
= poptGetContext(NULL
, argc
, argv
, snapshot_opts
, 0);
689 poptReadDefaultConfig(pc
, 0);
693 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
695 ret
= -LTTNG_ERR_NOMEM
;
699 /* Open command element */
700 ret
= mi_lttng_writer_command_open(writer
,
701 mi_lttng_element_command_snapshot
);
707 /* Open output element */
708 ret
= mi_lttng_writer_open_element(writer
,
709 mi_lttng_element_command_output
);
716 while ((opt
= poptGetNextOpt(pc
)) != -1) {
721 case OPT_LIST_OPTIONS
:
722 list_cmd_options(stdout
, snapshot_opts
);
724 case OPT_LIST_COMMANDS
:
725 list_commands(actions
, stdout
);
730 const char *opt
= poptGetOptArg(pc
);
732 if (utils_parse_size_suffix((char *) opt
, &val
) < 0) {
733 ERR("Unable to handle max-size value %s", opt
);
749 if (!opt_session_name
) {
750 session_name
= get_session_name();
751 if (session_name
== NULL
) {
755 current_session_name
= session_name
;
757 current_session_name
= opt_session_name
;
760 command_ret
= handle_command(poptGetArgs(pc
));
762 switch (-command_ret
) {
763 case LTTNG_ERR_EPERM
:
764 ERR("The session needs to be set in no output mode (--no-output)");
766 case LTTNG_ERR_SNAPSHOT_NODATA
:
767 WARN("%s", lttng_strerror(command_ret
));
770 ERR("%s", lttng_strerror(command_ret
));
777 /* Close output element */
778 ret
= mi_lttng_writer_close_element(writer
);
785 ret
= mi_lttng_writer_write_element_bool(writer
,
786 mi_lttng_element_command_success
, success
);
792 /* Command element close */
793 ret
= mi_lttng_writer_command_close(writer
);
802 if (writer
&& mi_lttng_writer_destroy(writer
)) {
803 /* Preserve original error code */
804 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
807 if (!opt_session_name
) {
811 /* Overwrite ret if an error occured during handle_command */
812 ret
= command_ret
? command_ret
: ret
;