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
);
300 ret
= mi_lttng_snapshot_del_output(writer
, id
, name
, current_session_name
);
306 lttng_snapshot_output_destroy(output
);
311 * Delete output by ID.
313 static int del_output(uint32_t id
, const char *name
)
316 struct lttng_snapshot_output
*output
= NULL
;
318 output
= lttng_snapshot_output_create();
325 ret
= lttng_snapshot_output_set_name(name
, output
);
326 } else if (id
!= UINT32_MAX
) {
327 ret
= lttng_snapshot_output_set_id(id
, output
);
337 ret
= lttng_snapshot_del_output(current_session_name
, output
);
342 if (id
!= UINT32_MAX
) {
343 MSG("Snapshot output id %" PRIu32
" successfully deleted for session %s",
344 id
, current_session_name
);
346 MSG("Snapshot output %s successfully deleted for session %s",
347 name
, current_session_name
);
351 lttng_snapshot_output_destroy(output
);
356 * Add output from the user URL (machine interface).
358 static int mi_add_output(const char *url
)
361 struct lttng_snapshot_output
*output
= NULL
;
365 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
370 output
= create_output_from_args(url
);
376 /* This call, if successful, populates the id of the output object. */
377 ret
= lttng_snapshot_add_output(current_session_name
, output
);
382 n_ptr
= lttng_snapshot_output_get_name(output
);
383 if (*n_ptr
== '\0') {
385 pret
= snprintf(name
, sizeof(name
), DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
386 lttng_snapshot_output_get_id(output
));
388 PERROR("snprintf add output name");
393 ret
= mi_lttng_snapshot_add_output(writer
, current_session_name
, n_ptr
,
400 lttng_snapshot_output_destroy(output
);
405 * Add output from the user URL.
407 static int add_output(const char *url
)
410 struct lttng_snapshot_output
*output
= NULL
;
414 if (!url
&& (!opt_data_url
|| !opt_ctrl_url
)) {
419 output
= create_output_from_args(url
);
425 /* This call, if successful, populates the id of the output object. */
426 ret
= lttng_snapshot_add_output(current_session_name
, output
);
431 n_ptr
= lttng_snapshot_output_get_name(output
);
432 if (*n_ptr
== '\0') {
434 pret
= snprintf(name
, sizeof(name
), DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
435 lttng_snapshot_output_get_id(output
));
437 PERROR("snprintf add output name");
442 MSG("Snapshot output successfully added for session %s",
443 current_session_name
);
444 MSG(" [%" PRIu32
"] %s: %s (max-size: %" PRId64
")",
445 lttng_snapshot_output_get_id(output
), n_ptr
,
446 lttng_snapshot_output_get_ctrl_url(output
),
447 lttng_snapshot_output_get_maxsize(output
));
449 lttng_snapshot_output_destroy(output
);
453 static int cmd_add_output(int argc
, const char **argv
)
457 if (argc
< 2 && (!opt_data_url
|| !opt_ctrl_url
)) {
464 ret
= mi_add_output(argv
[1]);
466 ret
= add_output(argv
[1]);
473 static int cmd_del_output(int argc
, const char **argv
)
486 id
= strtol(argv
[1], &name
, 10);
487 if (id
== 0 && errno
== 0) {
489 ret
= mi_del_output(UINT32_MAX
, name
);
491 ret
= del_output(UINT32_MAX
, name
);
493 } else if (errno
== 0 && *name
== '\0') {
495 ret
= mi_del_output(id
, NULL
);
497 ret
= del_output(id
, NULL
);
500 ERR("Argument %s not recognized", argv
[1]);
509 static int cmd_list_output(int argc
, const char **argv
)
514 ret
= mi_list_output();
523 * Do a snapshot record with the URL if one is given (machine interface).
525 static int mi_record(const char *url
)
528 struct lttng_snapshot_output
*output
= NULL
;
530 output
= create_output_from_args(url
);
536 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
542 ret
= mi_lttng_snapshot_record(writer
, current_session_name
, url
,
543 opt_ctrl_url
, opt_data_url
);
549 lttng_snapshot_output_destroy(output
);
554 * Do a snapshot record with the URL if one is given.
556 static int record(const char *url
)
559 struct lttng_snapshot_output
*output
= NULL
;
561 output
= create_output_from_args(url
);
567 ret
= lttng_snapshot_record(current_session_name
, output
, 0);
569 if (ret
== -LTTNG_ERR_MAX_SIZE_INVALID
) {
570 ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
575 MSG("Snapshot recorded successfully for session %s", current_session_name
);
578 MSG("Snapshot written at: %s", url
);
579 } else if (opt_ctrl_url
) {
580 MSG("Snapshot written to ctrl: %s, data: %s", opt_ctrl_url
,
585 lttng_snapshot_output_destroy(output
);
589 static int cmd_record(int argc
, const char **argv
)
594 /* With a given URL */
596 ret
= mi_record(argv
[1]);
598 ret
= record(argv
[1]);
602 ret
= mi_record(NULL
);
611 static int handle_command(const char **argv
)
613 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
614 struct cmd_struct
*cmd
;
616 if (argv
== NULL
|| (!opt_ctrl_url
&& opt_data_url
) ||
617 (opt_ctrl_url
&& !opt_data_url
)) {
619 command_ret
= CMD_ERROR
;
623 argc
= count_arguments(argv
);
626 while (cmd
->func
!= NULL
) {
628 if (strcmp(argv
[0], cmd
->name
) == 0) {
631 ret
= mi_lttng_writer_open_element(writer
,
632 mi_lttng_element_command_action
);
638 /* Name of the action */
639 ret
= mi_lttng_writer_write_element_string(writer
,
640 config_element_name
, argv
[0]);
646 /* Open output element */
647 ret
= mi_lttng_writer_open_element(writer
,
648 mi_lttng_element_command_output
);
655 command_ret
= cmd
->func(argc
, argv
);
658 /* Close output and action element */
659 ret
= mi_lttng_close_multi_element(writer
, 2);
674 /* Overwrite ret if an error occurred in cmd->func() */
675 ret
= command_ret
? command_ret
: ret
;
679 * The 'snapshot <cmd> <options>' first level command
681 int cmd_snapshot(int argc
, const char **argv
)
683 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
684 char *session_name
= NULL
;
685 static poptContext pc
;
687 pc
= poptGetContext(NULL
, argc
, argv
, snapshot_opts
, 0);
688 poptReadDefaultConfig(pc
, 0);
692 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
694 ret
= -LTTNG_ERR_NOMEM
;
698 /* Open command element */
699 ret
= mi_lttng_writer_command_open(writer
,
700 mi_lttng_element_command_snapshot
);
706 /* Open output element */
707 ret
= mi_lttng_writer_open_element(writer
,
708 mi_lttng_element_command_output
);
715 while ((opt
= poptGetNextOpt(pc
)) != -1) {
720 case OPT_LIST_OPTIONS
:
721 list_cmd_options(stdout
, snapshot_opts
);
723 case OPT_LIST_COMMANDS
:
724 list_commands(actions
, stdout
);
729 const char *opt
= poptGetOptArg(pc
);
731 if (utils_parse_size_suffix((char *) opt
, &val
) < 0) {
732 ERR("Unable to handle max-size value %s", opt
);
748 if (!opt_session_name
) {
749 session_name
= get_session_name();
750 if (session_name
== NULL
) {
754 current_session_name
= session_name
;
756 current_session_name
= opt_session_name
;
759 command_ret
= handle_command(poptGetArgs(pc
));
761 switch (-command_ret
) {
762 case LTTNG_ERR_EPERM
:
763 ERR("The session needs to be set in no output mode (--no-output)");
765 case LTTNG_ERR_SNAPSHOT_NODATA
:
766 WARN("%s", lttng_strerror(command_ret
));
769 ERR("%s", lttng_strerror(command_ret
));
776 /* Close output element */
777 ret
= mi_lttng_writer_close_element(writer
);
784 ret
= mi_lttng_writer_write_element_bool(writer
,
785 mi_lttng_element_command_success
, success
);
791 /* Command element close */
792 ret
= mi_lttng_writer_command_close(writer
);
801 if (writer
&& mi_lttng_writer_destroy(writer
)) {
802 /* Preserve original error code */
803 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
806 if (!opt_session_name
) {
810 /* Overwrite ret if an error occured during handle_command */
811 ret
= command_ret
? command_ret
: ret
;