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
;
43 static struct poptOption save_opts
[] = {
44 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
45 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
46 {"all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
47 {"output-path", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
48 {"force", 'f', POPT_ARG_NONE
, NULL
, OPT_FORCE
, NULL
, NULL
},
49 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
53 static int mi_partial_session(const char *session_name
)
59 /* Open session element */
60 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
65 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
71 /* Closing session element */
72 ret
= mi_lttng_writer_close_element(writer
);
78 * Mi print of save command
80 static int mi_save_print(const char *session_name
)
86 /* We use a wildcard to represent all sessions */
90 /* Print save element */
91 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_save
);
96 /* Print session element */
97 ret
= mi_partial_session(session_name
);
103 if (opt_output_path
) {
104 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
111 /* Close save element */
112 ret
= mi_lttng_writer_close_element(writer
);
118 * The 'save <options>' first level command
120 int cmd_save(int argc
, const char **argv
)
122 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
;
124 const char *session_name
= NULL
;
126 struct lttng_save_session_attr
*attr
;
128 pc
= poptGetContext(NULL
, argc
, argv
, save_opts
, 0);
129 poptReadDefaultConfig(pc
, 0);
131 while ((opt
= poptGetNextOpt(pc
)) != -1) {
142 case OPT_LIST_OPTIONS
:
143 list_cmd_options(stdout
, save_opts
);
152 session_name
= poptGetArg(pc
);
154 DBG2("Session name: %s", session_name
);
156 /* default to opt_save_all */
161 attr
= lttng_save_session_attr_create();
167 if (lttng_save_session_attr_set_session_name(attr
, session_name
)) {
172 if (lttng_save_session_attr_set_overwrite(attr
, opt_force
)) {
177 if (lttng_save_session_attr_set_output_url(attr
, opt_output_path
)) {
184 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
186 ret
= -LTTNG_ERR_NOMEM
;
190 /* Open command element */
191 ret
= mi_lttng_writer_command_open(writer
,
192 mi_lttng_element_command_save
);
198 /* Open output element */
199 ret
= mi_lttng_writer_open_element(writer
,
200 mi_lttng_element_command_output
);
207 command_ret
= lttng_save_session(attr
);
208 if (command_ret
< 0) {
209 ERR("%s", lttng_strerror(command_ret
));
212 /* Inform the user of what just happened on success. */
213 if (session_name
&& opt_output_path
) {
214 MSG("Session %s saved successfully in %s.", session_name
,
216 } else if (session_name
&& !opt_output_path
) {
217 MSG("Session %s saved successfully.", session_name
);
218 } else if (!session_name
&& opt_output_path
) {
219 MSG("All sessions have been saved successfully in %s.",
222 MSG("All sessions have been saved successfully.");
227 /* Mi Printing and closing */
230 ret
= mi_save_print(session_name
);
236 /* Close output element */
237 ret
= mi_lttng_writer_close_element(writer
);
244 ret
= mi_lttng_writer_write_element_bool(writer
,
245 mi_lttng_element_command_success
, success
);
251 /* Command element close */
252 ret
= mi_lttng_writer_command_close(writer
);
259 lttng_save_session_attr_destroy(attr
);
262 if (writer
&& mi_lttng_writer_destroy(writer
)) {
263 /* Preserve original error code */
264 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
267 /* Overwrite ret if command failed */
268 ret
= command_ret
? -command_ret
: ret
;