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
;
33 static int opt_save_all
;
42 static struct poptOption save_opts
[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
45 {"all", 'a', POPT_ARG_NONE
, 0, OPT_ALL
, 0, 0},
46 {"output-path", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, 0, 0},
47 {"force", 'f', POPT_ARG_NONE
, 0, OPT_FORCE
, 0, 0},
48 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
52 static struct mi_writer
*writer
;
54 static int mi_partial_session(const char *session_name
)
60 /* Open session element */
61 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
66 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
72 /* Closing session element */
73 ret
= mi_lttng_writer_close_element(writer
);
79 * Mi print of save command
81 static int mi_save_print(const char *session_name
)
87 /* We use a wildcard to represent all sessions */
91 /* Print save element */
92 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_save
);
97 /* Print session element */
98 ret
= mi_partial_session(session_name
);
104 if (opt_output_path
) {
105 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
112 /* Close save element */
113 ret
= mi_lttng_writer_close_element(writer
);
119 * The 'save <options>' first level command
121 int cmd_save(int argc
, const char **argv
)
123 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
;
125 const char *session_name
= NULL
;
127 struct lttng_save_session_attr
*attr
;
129 pc
= poptGetContext(NULL
, argc
, argv
, save_opts
, 0);
130 poptReadDefaultConfig(pc
, 0);
132 while ((opt
= poptGetNextOpt(pc
)) != -1) {
143 case OPT_LIST_OPTIONS
:
144 list_cmd_options(stdout
, save_opts
);
153 session_name
= poptGetArg(pc
);
155 DBG2("Session name: %s", session_name
);
157 /* default to opt_save_all */
162 attr
= lttng_save_session_attr_create();
168 if (lttng_save_session_attr_set_session_name(attr
, session_name
)) {
173 if (lttng_save_session_attr_set_overwrite(attr
, opt_force
)) {
178 if (lttng_save_session_attr_set_output_url(attr
, opt_output_path
)) {
185 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
187 ret
= -LTTNG_ERR_NOMEM
;
191 /* Open command element */
192 ret
= mi_lttng_writer_command_open(writer
,
193 mi_lttng_element_command_save
);
199 /* Open output element */
200 ret
= mi_lttng_writer_open_element(writer
,
201 mi_lttng_element_command_output
);
208 command_ret
= lttng_save_session(attr
);
209 if (command_ret
< 0) {
210 ERR("%s", lttng_strerror(command_ret
));
213 /* Inform the user of what just happened on success. */
214 if (session_name
&& opt_output_path
) {
215 MSG("Session %s saved successfully in %s.", session_name
,
217 } else if (session_name
&& !opt_output_path
) {
218 MSG("Session %s saved successfully.", session_name
);
219 } else if (!session_name
&& opt_output_path
) {
220 MSG("All sessions have been saved successfully in %s.",
223 MSG("All sessions have been saved successfully.");
228 /* Mi Printing and closing */
231 ret
= mi_save_print(session_name
);
237 /* Close output element */
238 ret
= mi_lttng_writer_close_element(writer
);
245 ret
= mi_lttng_writer_write_element_bool(writer
,
246 mi_lttng_element_command_success
, success
);
252 /* Command element close */
253 ret
= mi_lttng_writer_command_close(writer
);
260 lttng_save_session_attr_destroy(attr
);
263 if (writer
&& mi_lttng_writer_destroy(writer
)) {
264 /* Preserve original error code */
265 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
268 /* Overwrite ret if command failed */
269 ret
= command_ret
? -command_ret
: ret
;