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
;
33 static char *opt_override_url
;
34 static char *opt_override_session_name
;
36 static int opt_load_all
;
38 static const char *session_name
;
47 static struct mi_writer
*writer
;
49 static struct poptOption load_opts
[] = {
50 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
51 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
52 {"all", 'a', POPT_ARG_NONE
, 0, OPT_ALL
, 0, 0},
53 {"input-path", 'i', POPT_ARG_STRING
, &opt_input_path
, 0, 0, 0},
54 {"force", 'f', POPT_ARG_NONE
, 0, OPT_FORCE
, 0, 0},
55 {"override-url", 0, POPT_ARG_STRING
, &opt_override_url
, 0, 0, 0},
56 {"override-name", 0, POPT_ARG_STRING
, &opt_override_session_name
, 0, 0, 0},
57 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
61 static int mi_partial_session(const char *session_name
)
67 /* Open session element */
68 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
73 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
79 /* Closing session element */
80 ret
= mi_lttng_writer_close_element(writer
);
86 * Mi print of load command
88 static int mi_load_print(const char *session_name
)
94 /* We use a wildcard to represent all sessions */
98 /* Print load element */
99 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_load
);
104 /* Print session element */
105 ret
= mi_partial_session(session_name
);
111 if (opt_input_path
) {
112 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
119 /* Print override elements */
120 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_load_overrides
);
125 /* Session name override element */
126 if (opt_override_session_name
) {
127 ret
= mi_lttng_writer_write_element_string(writer
,
128 config_element_name
, opt_override_session_name
);
134 /* Session url override element */
135 if (opt_override_url
) {
136 ret
= mi_lttng_writer_write_element_string(writer
,
137 mi_lttng_element_load_override_url
,
144 /* Close override and load element */
145 ret
= mi_lttng_close_multi_element(writer
, 2);
151 * The 'load <options>' first level command
153 int cmd_load(int argc
, const char **argv
)
158 struct lttng_load_session_attr
*session_attr
= NULL
;
159 char *input_path
= NULL
;
161 pc
= poptGetContext(NULL
, argc
, argv
, load_opts
, 0);
162 poptReadDefaultConfig(pc
, 0);
164 while ((opt
= poptGetNextOpt(pc
)) != -1) {
173 case OPT_LIST_OPTIONS
:
174 list_cmd_options(stdout
, load_opts
);
186 ret
= lttng_session_daemon_alive();
188 ERR("No session daemon is available");
194 session_name
= poptGetArg(pc
);
196 DBG2("Loading session name: %s", session_name
);
198 /* Default to load_all */
205 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
211 /* Open command element */
212 ret
= mi_lttng_writer_command_open(writer
,
213 mi_lttng_element_command_load
);
219 /* Open output element */
220 ret
= mi_lttng_writer_open_element(writer
,
221 mi_lttng_element_command_output
);
228 /* Prepare load attributes */
229 session_attr
= lttng_load_session_attr_create();
231 ERR("Failed to create load session attributes");
238 * lttng_load_session_attr_set_input_url only suppports absolute path.
239 * Use realpath to resolve any relative path.
241 if (opt_input_path
) {
242 input_path
= realpath(opt_input_path
, NULL
);
244 PERROR("Invalid input path");
252 ret
= lttng_load_session_attr_set_input_url(session_attr
,
255 ERR("Invalid input path");
260 /* Set the session name. NULL means all sessions should be loaded */
261 ret
= lttng_load_session_attr_set_session_name(session_attr
,
264 ERR("Invalid session name");
269 /* Set the overwrite attribute */
270 ret
= lttng_load_session_attr_set_overwrite(session_attr
, opt_force
);
272 ERR("Force argument could not be applied");
277 /* Set the overrides attributes if any */
278 if (opt_override_url
) {
279 ret
= lttng_load_session_attr_set_override_url(session_attr
,
282 ERR("Url override is invalid");
287 if (opt_override_session_name
) {
289 ERR("Options --all and --override-name cannot be used simultaneously");
293 ret
= lttng_load_session_attr_set_override_session_name(session_attr
,
294 opt_override_session_name
);
296 ERR("Failed to set session name override");
302 ret
= lttng_load_session(session_attr
);
304 ERR("%s", lttng_strerror(ret
));
309 MSG("All sessions have been loaded successfully");
310 } else if (session_name
) {
311 ret
= config_init((char *) session_name
);
313 WARN("Could not set %s as the default session",
316 MSG("Session %s has been loaded successfully", session_name
);
318 MSG("Session has been loaded successfully");
321 if (opt_override_session_name
) {
322 MSG("Session name overridden with %s",
323 opt_override_session_name
);
326 if (opt_override_url
) {
327 MSG("Session output url overridden with %s", opt_override_url
);
333 /* Mi Printing and closing */
336 ret
= mi_load_print(session_name
);
342 /* Close output element */
343 ret
= mi_lttng_writer_close_element(writer
);
350 ret
= mi_lttng_writer_write_element_bool(writer
,
351 mi_lttng_element_command_success
, success
);
357 /* Command element close */
358 ret
= mi_lttng_writer_command_close(writer
);
365 if (writer
&& mi_lttng_writer_destroy(writer
)) {
366 ERR("Failed to destroy mi lttng writer");
369 lttng_load_session_attr_destroy(session_attr
);