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/session-config.h>
28 #include <lttng/load.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
},
57 static int mi_partial_session(const char *session_name
)
63 /* Open session element */
64 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
69 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
75 /* Closing session element */
76 ret
= mi_lttng_writer_close_element(writer
);
82 * Mi print of load command
84 static int mi_load_print(const char *session_name
)
90 /* We use a wildcard to represent all sessions */
94 /* Print load element */
95 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_load
);
100 /* Print session element */
101 ret
= mi_partial_session(session_name
);
107 if (opt_input_path
) {
108 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
115 /* Close load element */
116 ret
= mi_lttng_writer_close_element(writer
);
123 * The 'load <options>' first level command
125 int cmd_load(int argc
, const char **argv
)
127 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
;
130 struct lttng_load_session_attr
*session_attr
= NULL
;
131 char *input_path
= NULL
;
133 pc
= poptGetContext(NULL
, argc
, argv
, load_opts
, 0);
134 poptReadDefaultConfig(pc
, 0);
136 while ((opt
= poptGetNextOpt(pc
)) != -1) {
144 case OPT_LIST_OPTIONS
:
145 list_cmd_options(stdout
, load_opts
);
157 session_name
= poptGetArg(pc
);
159 DBG2("Loading session name: %s", session_name
);
161 /* Default to load_all */
168 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
170 ret
= -LTTNG_ERR_NOMEM
;
174 /* Open command element */
175 ret
= mi_lttng_writer_command_open(writer
,
176 mi_lttng_element_command_load
);
182 /* Open output element */
183 ret
= mi_lttng_writer_open_element(writer
,
184 mi_lttng_element_command_output
);
191 /* Prepare load attributes */
192 session_attr
= lttng_load_session_attr_create();
194 ERR("Load session attributes creation failed");
195 ret
= -LTTNG_ERR_NOMEM
;
201 * lttng_load_session_attr_set_input_url support only absolute path.
202 * Use realpath to resolve any relative path.
204 if (opt_input_path
) {
205 input_path
= realpath(opt_input_path
, NULL
);
207 PERROR("Input path is invalid");
213 ret
= lttng_load_session_attr_set_input_url(session_attr
,
216 ERR("Input path is invalid");
220 /* Set the session name. NULL means all sessions should be loaded */
221 ret
= lttng_load_session_attr_set_session_name(session_attr
,
224 ERR("Session name is invalid");
228 /* Set the overwrite attribute */
229 ret
= lttng_load_session_attr_set_overwrite(session_attr
, opt_force
);
231 ERR("Force argument could not be applied");
235 command_ret
= lttng_load_session(session_attr
);
237 ERR("%s", lttng_strerror(command_ret
));
241 MSG("All sessions have been loaded successfully");
242 } else if (session_name
) {
243 ret
= config_init((char *)session_name
);
247 MSG("Session %s has been loaded successfully", session_name
);
249 MSG("Session has been loaded successfully");
254 /* Mi Printing and closing */
257 ret
= mi_load_print(session_name
);
263 /* Close output element */
264 ret
= mi_lttng_writer_close_element(writer
);
271 ret
= mi_lttng_writer_write_element_bool(writer
,
272 mi_lttng_element_command_success
, success
);
278 /* Command element close */
279 ret
= mi_lttng_writer_command_close(writer
);
286 if (writer
&& mi_lttng_writer_destroy(writer
)) {
287 /* Preserve original error code */
288 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
291 /* Overwrite ret if the was an error with the load command */
292 ret
= command_ret
? -command_ret
: ret
;
294 lttng_load_session_attr_destroy(session_attr
);