2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <common/mi-lttng.h>
28 #include "../command.h"
29 #include <lttng/save.h>
31 static char *opt_output_path
;
32 static bool opt_force
;
33 static bool opt_save_all
;
34 static struct mi_writer
*writer
;
36 #ifdef LTTNG_EMBED_HELP
37 static const char help_msg
[] =
38 #include <lttng-save.1.h>
49 static struct poptOption save_opts
[] = {
50 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
51 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
52 {"all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
53 {"output-path", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
54 {"force", 'f', POPT_ARG_NONE
, NULL
, OPT_FORCE
, NULL
, NULL
},
55 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
59 static int mi_partial_session(const char *session_name
)
65 /* Open session element */
66 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
71 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
77 /* Closing session element */
78 ret
= mi_lttng_writer_close_element(writer
);
84 * Mi print of save command
86 static int mi_save_print(const char *session_name
)
92 /* We use a wildcard to represent all sessions */
96 /* Print save element */
97 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_save
);
102 /* Print session element */
103 ret
= mi_partial_session(session_name
);
109 if (opt_output_path
) {
110 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
117 /* Close save element */
118 ret
= mi_lttng_writer_close_element(writer
);
124 * The 'save <options>' first level command
126 int cmd_save(int argc
, const char **argv
)
128 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
;
130 const char *session_name
= NULL
, *leftover
= NULL
;
132 struct lttng_save_session_attr
*attr
;
134 pc
= poptGetContext(NULL
, argc
, argv
, save_opts
, 0);
135 poptReadDefaultConfig(pc
, 0);
137 while ((opt
= poptGetNextOpt(pc
)) != -1) {
148 case OPT_LIST_OPTIONS
:
149 list_cmd_options(stdout
, save_opts
);
158 session_name
= poptGetArg(pc
);
160 DBG2("Session name: %s", session_name
);
162 /* default to opt_save_all */
167 leftover
= poptGetArg(pc
);
169 ERR("Unknown argument: %s", leftover
);
174 attr
= lttng_save_session_attr_create();
180 if (lttng_save_session_attr_set_session_name(attr
, session_name
)) {
185 if (lttng_save_session_attr_set_overwrite(attr
, opt_force
)) {
190 if (lttng_save_session_attr_set_output_url(attr
, opt_output_path
)) {
197 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
199 ret
= -LTTNG_ERR_NOMEM
;
203 /* Open command element */
204 ret
= mi_lttng_writer_command_open(writer
,
205 mi_lttng_element_command_save
);
211 /* Open output element */
212 ret
= mi_lttng_writer_open_element(writer
,
213 mi_lttng_element_command_output
);
220 command_ret
= lttng_save_session(attr
);
221 if (command_ret
< 0) {
222 ERR("%s", lttng_strerror(command_ret
));
225 /* Inform the user of what just happened on success. */
226 if (session_name
&& opt_output_path
) {
227 MSG("Session %s saved successfully in %s.", session_name
,
229 } else if (session_name
&& !opt_output_path
) {
230 MSG("Session %s saved successfully.", session_name
);
231 } else if (!session_name
&& opt_output_path
) {
232 MSG("All sessions have been saved successfully in %s.",
235 MSG("All sessions have been saved successfully.");
240 /* Mi Printing and closing */
243 ret
= mi_save_print(session_name
);
249 /* Close output element */
250 ret
= mi_lttng_writer_close_element(writer
);
257 ret
= mi_lttng_writer_write_element_bool(writer
,
258 mi_lttng_element_command_success
, success
);
264 /* Command element close */
265 ret
= mi_lttng_writer_command_close(writer
);
272 lttng_save_session_attr_destroy(attr
);
275 if (writer
&& mi_lttng_writer_destroy(writer
)) {
276 /* Preserve original error code */
277 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
280 /* Overwrite ret if command failed */
281 ret
= command_ret
? -command_ret
: ret
;