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
;
43 static struct mi_writer
*writer
;
45 static struct poptOption load_opts
[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
48 {"all", 'a', POPT_ARG_NONE
, 0, OPT_ALL
, 0, 0},
49 {"input-path", 'i', POPT_ARG_STRING
, &opt_input_path
, 0, 0, 0},
50 {"force", 'f', POPT_ARG_NONE
, 0, OPT_FORCE
, 0, 0},
57 static void usage(FILE *ofp
)
59 fprintf(ofp
, "usage: lttng load [OPTIONS] [SESSION]\n");
61 fprintf(ofp
, "Options:\n");
62 fprintf(ofp
, " -h, --help Show this help\n");
63 fprintf(ofp
, " -a, --all Load all sessions (default)\n");
64 fprintf(ofp
, " -i, --input-path PATH Input path of the session file(s).\n");
65 fprintf(ofp
, " If a directory, load all files in it\n");
66 fprintf(ofp
, " else try to load the given file.\n");
67 fprintf(ofp
, " -f, --force Override existing session(s).\n");
68 fprintf(ofp
, " This will destroy existing session(s)\n");
69 fprintf(ofp
, " before creating new one(s).\n");
72 static int mi_partial_session(const char *session_name
)
78 /* Open session element */
79 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
84 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
90 /* Closing session element */
91 ret
= mi_lttng_writer_close_element(writer
);
97 * Mi print of load command
99 static int mi_load_print(const char *session_name
)
105 /* We use a wildcard to represent all sessions */
109 /* Print load element */
110 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_load
);
115 /* Print session element */
116 ret
= mi_partial_session(session_name
);
122 if (opt_input_path
) {
123 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
130 /* Close load element */
131 ret
= mi_lttng_writer_close_element(writer
);
138 * The 'load <options>' first level command
140 int cmd_load(int argc
, const char **argv
)
142 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
;
146 pc
= poptGetContext(NULL
, argc
, argv
, load_opts
, 0);
147 poptReadDefaultConfig(pc
, 0);
149 while ((opt
= poptGetNextOpt(pc
)) != -1) {
168 session_name
= poptGetArg(pc
);
170 DBG2("Loading session name: %s", session_name
);
172 /* Default to load_all */
179 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
181 ret
= -LTTNG_ERR_NOMEM
;
185 /* Open command element */
186 ret
= mi_lttng_writer_command_open(writer
,
187 mi_lttng_element_command_load
);
193 /* Open output element */
194 ret
= mi_lttng_writer_open_element(writer
,
195 mi_lttng_element_command_output
);
202 command_ret
= config_load_session(opt_input_path
, session_name
, opt_force
, 0);
204 ERR("%s", lttng_strerror(command_ret
));
208 MSG("All sessions have been loaded successfully");
209 } else if (session_name
) {
210 ret
= config_init((char *)session_name
);
214 MSG("Session %s has been loaded successfully", session_name
);
216 MSG("Session has been loaded successfully");
221 /* Mi Printing and closing */
224 ret
= mi_load_print(session_name
);
230 /* Close output element */
231 ret
= mi_lttng_writer_close_element(writer
);
238 ret
= mi_lttng_writer_write_element_bool(writer
,
239 mi_lttng_element_command_success
, success
);
245 /* Command element close */
246 ret
= mi_lttng_writer_command_close(writer
);
253 if (writer
&& mi_lttng_writer_destroy(writer
)) {
254 /* Preserve original error code */
255 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
258 /* Overwrite ret if the was an error with the load command */
259 ret
= command_ret
? -command_ret
: ret
;