2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <common/mi-lttng.hpp>
17 #include "../command.hpp"
18 #include <lttng/lttng.h>
20 static char *opt_output_path
;
21 static bool opt_force
;
22 static bool opt_save_all
;
23 static struct mi_writer
*writer
;
25 #ifdef LTTNG_EMBED_HELP
26 static const char help_msg
[] =
27 #include <lttng-save.1.h>
38 static struct poptOption save_opts
[] = {
39 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
40 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
41 {"all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
42 {"output-path", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
43 {"force", 'f', POPT_ARG_NONE
, NULL
, OPT_FORCE
, NULL
, NULL
},
44 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
48 static int mi_partial_session(const char *session_name
)
52 LTTNG_ASSERT(session_name
);
54 /* Open session element */
55 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
60 ret
= mi_lttng_writer_write_element_string(writer
, config_element_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(writer
, config_element_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
= NULL
, *leftover
= NULL
;
121 struct lttng_save_session_attr
*attr
;
123 pc
= poptGetContext(NULL
, 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
,
194 mi_lttng_element_command_save
);
200 /* Open output element */
201 ret
= mi_lttng_writer_open_element(writer
,
202 mi_lttng_element_command_output
);
209 command_ret
= lttng_save_session(attr
);
210 if (command_ret
< 0) {
211 ERR("%s", lttng_strerror(command_ret
));
214 /* Inform the user of what just happened on success. */
215 if (arg_session_name
&& opt_output_path
) {
216 MSG("Session %s saved successfully in %s.", arg_session_name
,
218 } else if (arg_session_name
&& !opt_output_path
) {
219 MSG("Session %s saved successfully.", arg_session_name
);
220 } else if (!arg_session_name
&& opt_output_path
) {
221 MSG("All sessions have been saved successfully in %s.",
224 MSG("All sessions have been saved successfully.");
229 /* Mi Printing and closing */
232 ret
= mi_save_print(arg_session_name
);
238 /* Close output element */
239 ret
= mi_lttng_writer_close_element(writer
);
246 ret
= mi_lttng_writer_write_element_bool(writer
,
247 mi_lttng_element_command_success
, success
);
253 /* Command element close */
254 ret
= mi_lttng_writer_command_close(writer
);
261 lttng_save_session_attr_destroy(attr
);
264 if (writer
&& mi_lttng_writer_destroy(writer
)) {
265 /* Preserve original error code */
266 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
269 /* Overwrite ret if command failed */
270 ret
= command_ret
? -command_ret
: ret
;