2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
13 #include <lttng/lttng.h>
21 static char *opt_output_path
;
22 static bool opt_force
;
23 static bool opt_save_all
;
24 static struct mi_writer
*writer
;
26 #ifdef LTTNG_EMBED_HELP
27 static const char help_msg
[] =
28 #include <lttng-save.1.h>
39 static struct poptOption save_opts
[] = {
40 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
41 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
42 { "all", 'a', POPT_ARG_NONE
, nullptr, OPT_ALL
, nullptr, nullptr },
43 { "output-path", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, nullptr, nullptr },
44 { "force", 'f', POPT_ARG_NONE
, nullptr, OPT_FORCE
, nullptr, nullptr },
45 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
46 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
49 static int mi_partial_session(const char *session_name
)
53 LTTNG_ASSERT(session_name
);
55 /* Open session element */
56 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
61 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
, session_name
);
66 /* Closing session element */
67 ret
= mi_lttng_writer_close_element(writer
);
73 * Mi print of save command
75 static int mi_save_print(const char *session_name
)
81 /* We use a wildcard to represent all sessions */
85 /* Print save element */
86 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_save
);
91 /* Print session element */
92 ret
= mi_partial_session(session_name
);
98 if (opt_output_path
) {
99 ret
= mi_lttng_writer_write_element_string(
100 writer
, config_element_path
, opt_output_path
);
106 /* Close save element */
107 ret
= mi_lttng_writer_close_element(writer
);
113 * The 'save <options>' first level command
115 int cmd_save(int argc
, const char **argv
)
117 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
;
119 const char *arg_session_name
= nullptr, *leftover
= nullptr;
121 struct lttng_save_session_attr
*attr
;
123 pc
= poptGetContext(nullptr, argc
, argv
, save_opts
, 0);
124 poptReadDefaultConfig(pc
, 0);
126 while ((opt
= poptGetNextOpt(pc
)) != -1) {
137 case OPT_LIST_OPTIONS
:
138 list_cmd_options(stdout
, save_opts
);
147 arg_session_name
= poptGetArg(pc
);
148 if (arg_session_name
) {
149 DBG2("Session name: %s", arg_session_name
);
151 /* default to opt_save_all */
156 leftover
= poptGetArg(pc
);
158 ERR("Unknown argument: %s", leftover
);
163 attr
= lttng_save_session_attr_create();
169 if (lttng_save_session_attr_set_session_name(attr
, arg_session_name
)) {
174 if (lttng_save_session_attr_set_overwrite(attr
, opt_force
)) {
179 if (lttng_save_session_attr_set_output_url(attr
, opt_output_path
)) {
186 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
188 ret
= -LTTNG_ERR_NOMEM
;
192 /* Open command element */
193 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_save
);
199 /* Open output element */
200 ret
= mi_lttng_writer_open_element(writer
, 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 (arg_session_name
&& opt_output_path
) {
214 MSG("Session %s saved successfully in %s.",
217 } else if (arg_session_name
&& !opt_output_path
) {
218 MSG("Session %s saved successfully.", arg_session_name
);
219 } else if (!arg_session_name
&& opt_output_path
) {
220 MSG("All sessions have been saved successfully in %s.", opt_output_path
);
222 MSG("All sessions have been saved successfully.");
227 /* Mi Printing and closing */
230 ret
= mi_save_print(arg_session_name
);
236 /* Close output element */
237 ret
= mi_lttng_writer_close_element(writer
);
244 ret
= mi_lttng_writer_write_element_bool(
245 writer
, 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
;