2 * Copyright (C) 2014 - 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>
27 #include <common/config/config.h>
29 #include "../command.h"
31 static char *opt_input_path
;
33 static int opt_load_all
;
35 static const char *session_name
;
44 static struct mi_writer
*writer
;
46 static struct poptOption load_opts
[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
49 {"all", 'a', POPT_ARG_NONE
, 0, OPT_ALL
, 0, 0},
50 {"input-path", 'i', POPT_ARG_STRING
, &opt_input_path
, 0, 0, 0},
51 {"force", 'f', POPT_ARG_NONE
, 0, OPT_FORCE
, 0, 0},
52 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
59 static void usage(FILE *ofp
)
61 fprintf(ofp
, "usage: lttng load [OPTIONS] [SESSION]\n");
63 fprintf(ofp
, "Options:\n");
64 fprintf(ofp
, " -h, --help Show this help\n");
65 fprintf(ofp
, " -a, --all Load all sessions (default)\n");
66 fprintf(ofp
, " -i, --input-path PATH Input path of the session file(s).\n");
67 fprintf(ofp
, " If a directory, load all files in it\n");
68 fprintf(ofp
, " else try to load the given file.\n");
69 fprintf(ofp
, " -f, --force Override existing session(s).\n");
70 fprintf(ofp
, " This will destroy existing session(s)\n");
71 fprintf(ofp
, " before creating new one(s).\n");
74 static int mi_partial_session(const char *session_name
)
80 /* Open session element */
81 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
86 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
92 /* Closing session element */
93 ret
= mi_lttng_writer_close_element(writer
);
99 * Mi print of load command
101 static int mi_load_print(const char *session_name
)
107 /* We use a wildcard to represent all sessions */
111 /* Print load element */
112 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_load
);
117 /* Print session element */
118 ret
= mi_partial_session(session_name
);
124 if (opt_input_path
) {
125 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
132 /* Close load element */
133 ret
= mi_lttng_writer_close_element(writer
);
140 * The 'load <options>' first level command
142 int cmd_load(int argc
, const char **argv
)
144 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
;
148 pc
= poptGetContext(NULL
, argc
, argv
, load_opts
, 0);
149 poptReadDefaultConfig(pc
, 0);
151 while ((opt
= poptGetNextOpt(pc
)) != -1) {
159 case OPT_LIST_OPTIONS
:
160 list_cmd_options(stdout
, load_opts
);
173 session_name
= poptGetArg(pc
);
175 DBG2("Loading session name: %s", session_name
);
177 /* Default to load_all */
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_load
);
198 /* Open output element */
199 ret
= mi_lttng_writer_open_element(writer
,
200 mi_lttng_element_command_output
);
207 command_ret
= config_load_session(opt_input_path
, session_name
, opt_force
, 0);
209 ERR("%s", lttng_strerror(command_ret
));
213 MSG("All sessions have been loaded successfully");
214 } else if (session_name
) {
215 ret
= config_init((char *)session_name
);
219 MSG("Session %s has been loaded successfully", session_name
);
221 MSG("Session has been loaded successfully");
226 /* Mi Printing and closing */
229 ret
= mi_load_print(session_name
);
235 /* Close output element */
236 ret
= mi_lttng_writer_close_element(writer
);
243 ret
= mi_lttng_writer_write_element_bool(writer
,
244 mi_lttng_element_command_success
, success
);
250 /* Command element close */
251 ret
= mi_lttng_writer_command_close(writer
);
258 if (writer
&& mi_lttng_writer_destroy(writer
)) {
259 /* Preserve original error code */
260 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
263 /* Overwrite ret if the was an error with the load command */
264 ret
= command_ret
? -command_ret
: ret
;