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.
27 #include <common/mi-lttng.h>
28 #include <common/config/config.h>
30 #include "../command.h"
32 static char *opt_input_path
;
34 static int opt_load_all
;
36 static const char *session_name
;
45 static struct mi_writer
*writer
;
47 static struct poptOption load_opts
[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
49 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
50 {"all", 'a', POPT_ARG_NONE
, 0, OPT_ALL
, 0, 0},
51 {"input-path", 'i', POPT_ARG_STRING
, &opt_input_path
, 0, 0, 0},
52 {"force", 'f', POPT_ARG_NONE
, 0, OPT_FORCE
, 0, 0},
53 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
60 static void usage(FILE *ofp
)
62 fprintf(ofp
, "usage: lttng load [OPTIONS] [SESSION]\n");
64 fprintf(ofp
, "Options:\n");
65 fprintf(ofp
, " -h, --help Show this help\n");
66 fprintf(ofp
, " -a, --all Load all sessions (default)\n");
67 fprintf(ofp
, " -i, --input-path PATH Input path of the session file(s).\n");
68 fprintf(ofp
, " If a directory, load all files in it\n");
69 fprintf(ofp
, " else try to load the given file.\n");
70 fprintf(ofp
, " -f, --force Override existing session(s).\n");
71 fprintf(ofp
, " This will destroy existing session(s)\n");
72 fprintf(ofp
, " before creating new one(s).\n");
75 static int mi_partial_session(const char *session_name
)
81 /* Open session element */
82 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
87 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
93 /* Closing session element */
94 ret
= mi_lttng_writer_close_element(writer
);
100 * Mi print of load command
102 static int mi_load_print(const char *session_name
)
108 /* We use a wildcard to represent all sessions */
112 /* Print load element */
113 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_load
);
118 /* Print session element */
119 ret
= mi_partial_session(session_name
);
125 if (opt_input_path
) {
126 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
133 /* Close load element */
134 ret
= mi_lttng_writer_close_element(writer
);
141 * The 'load <options>' first level command
143 int cmd_load(int argc
, const char **argv
)
145 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
;
149 pc
= poptGetContext(NULL
, argc
, argv
, load_opts
, 0);
150 poptReadDefaultConfig(pc
, 0);
152 while ((opt
= poptGetNextOpt(pc
)) != -1) {
160 case OPT_LIST_OPTIONS
:
161 list_cmd_options(stdout
, load_opts
);
174 session_name
= poptGetArg(pc
);
176 DBG2("Loading session name: %s", session_name
);
178 /* Default to load_all */
185 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
187 ret
= -LTTNG_ERR_NOMEM
;
191 /* Open command element */
192 ret
= mi_lttng_writer_command_open(writer
,
193 mi_lttng_element_command_load
);
199 /* Open output element */
200 ret
= mi_lttng_writer_open_element(writer
,
201 mi_lttng_element_command_output
);
208 command_ret
= config_load_session(opt_input_path
, session_name
, opt_force
, 0);
210 ERR("%s", lttng_strerror(command_ret
));
214 MSG("All sessions have been loaded successfully");
215 } else if (session_name
) {
216 ret
= config_init((char *)session_name
);
220 MSG("Session %s has been loaded successfully", session_name
);
222 MSG("Session has been loaded successfully");
227 /* Mi Printing and closing */
230 ret
= mi_load_print(session_name
);
236 /* Close output element */
237 ret
= mi_lttng_writer_close_element(writer
);
244 ret
= mi_lttng_writer_write_element_bool(writer
,
245 mi_lttng_element_command_success
, success
);
251 /* Command element close */
252 ret
= mi_lttng_writer_command_close(writer
);
259 if (writer
&& mi_lttng_writer_destroy(writer
)) {
260 /* Preserve original error code */
261 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
264 /* Overwrite ret if the was an error with the load command */
265 ret
= command_ret
? -command_ret
: ret
;