2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
16 #include <common/mi-lttng.h>
18 #include "../command.h"
19 #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
, NULL
, OPT_HELP
, NULL
, NULL
},
42 {"all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
43 {"output-path", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
44 {"force", 'f', POPT_ARG_NONE
, NULL
, OPT_FORCE
, NULL
, NULL
},
45 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
49 static int mi_partial_session(const char *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
,
67 /* Closing session element */
68 ret
= mi_lttng_writer_close_element(writer
);
74 * Mi print of save command
76 static int mi_save_print(const char *session_name
)
82 /* We use a wildcard to represent all sessions */
86 /* Print save element */
87 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_save
);
92 /* Print session element */
93 ret
= mi_partial_session(session_name
);
99 if (opt_output_path
) {
100 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
107 /* Close save element */
108 ret
= mi_lttng_writer_close_element(writer
);
114 * The 'save <options>' first level command
116 int cmd_save(int argc
, const char **argv
)
118 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
;
120 const char *session_name
= NULL
, *leftover
= NULL
;
122 struct lttng_save_session_attr
*attr
;
124 pc
= poptGetContext(NULL
, argc
, argv
, save_opts
, 0);
125 poptReadDefaultConfig(pc
, 0);
127 while ((opt
= poptGetNextOpt(pc
)) != -1) {
138 case OPT_LIST_OPTIONS
:
139 list_cmd_options(stdout
, save_opts
);
148 session_name
= poptGetArg(pc
);
150 DBG2("Session name: %s", session_name
);
152 /* default to opt_save_all */
157 leftover
= poptGetArg(pc
);
159 ERR("Unknown argument: %s", leftover
);
164 attr
= lttng_save_session_attr_create();
170 if (lttng_save_session_attr_set_session_name(attr
, session_name
)) {
175 if (lttng_save_session_attr_set_overwrite(attr
, opt_force
)) {
180 if (lttng_save_session_attr_set_output_url(attr
, opt_output_path
)) {
187 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
189 ret
= -LTTNG_ERR_NOMEM
;
193 /* Open command element */
194 ret
= mi_lttng_writer_command_open(writer
,
195 mi_lttng_element_command_save
);
201 /* Open output element */
202 ret
= mi_lttng_writer_open_element(writer
,
203 mi_lttng_element_command_output
);
210 command_ret
= lttng_save_session(attr
);
211 if (command_ret
< 0) {
212 ERR("%s", lttng_strerror(command_ret
));
215 /* Inform the user of what just happened on success. */
216 if (session_name
&& opt_output_path
) {
217 MSG("Session %s saved successfully in %s.", session_name
,
219 } else if (session_name
&& !opt_output_path
) {
220 MSG("Session %s saved successfully.", session_name
);
221 } else if (!session_name
&& opt_output_path
) {
222 MSG("All sessions have been saved successfully in %s.",
225 MSG("All sessions have been saved successfully.");
230 /* Mi Printing and closing */
233 ret
= mi_save_print(session_name
);
239 /* Close output element */
240 ret
= mi_lttng_writer_close_element(writer
);
247 ret
= mi_lttng_writer_write_element_bool(writer
,
248 mi_lttng_element_command_success
, success
);
254 /* Command element close */
255 ret
= mi_lttng_writer_command_close(writer
);
262 lttng_save_session_attr_destroy(attr
);
265 if (writer
&& mi_lttng_writer_destroy(writer
)) {
266 /* Preserve original error code */
267 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
270 /* Overwrite ret if command failed */
271 ret
= command_ret
? -command_ret
: ret
;