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
;
40 #ifdef LTTNG_EMBED_HELP
41 static const char help_msg
[] =
42 #include <lttng-load.1.h>
53 static struct mi_writer
*writer
;
55 static struct poptOption load_opts
[] = {
56 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
57 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
58 {"all", 'a', POPT_ARG_NONE
, 0, OPT_ALL
, 0, 0},
59 {"input-path", 'i', POPT_ARG_STRING
, &opt_input_path
, 0, 0, 0},
60 {"force", 'f', POPT_ARG_NONE
, 0, OPT_FORCE
, 0, 0},
61 {"override-url", 0, POPT_ARG_STRING
, &opt_override_url
, 0, 0, 0},
62 {"override-name", 0, POPT_ARG_STRING
, &opt_override_session_name
, 0, 0, 0},
63 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
67 static int mi_partial_session(const char *session_name
)
73 /* Open session element */
74 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
79 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
85 /* Closing session element */
86 ret
= mi_lttng_writer_close_element(writer
);
92 * Mi print of load command
94 static int mi_load_print(const char *session_name
)
100 /* We use a wildcard to represent all sessions */
104 /* Print load element */
105 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_load
);
110 /* Print session element */
111 ret
= mi_partial_session(session_name
);
117 if (opt_input_path
) {
118 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
125 /* Print override elements */
126 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_load_overrides
);
131 /* Session name override element */
132 if (opt_override_session_name
) {
133 ret
= mi_lttng_writer_write_element_string(writer
,
134 config_element_name
, opt_override_session_name
);
140 /* Session url override element */
141 if (opt_override_url
) {
142 ret
= mi_lttng_writer_write_element_string(writer
,
143 mi_lttng_element_load_override_url
,
150 /* Close override and load element */
151 ret
= mi_lttng_close_multi_element(writer
, 2);
157 * The 'load <options>' first level command
159 int cmd_load(int argc
, const char **argv
)
164 struct lttng_load_session_attr
*session_attr
= NULL
;
165 char *input_path
= NULL
;
167 pc
= poptGetContext(NULL
, argc
, argv
, load_opts
, 0);
168 poptReadDefaultConfig(pc
, 0);
170 while ((opt
= poptGetNextOpt(pc
)) != -1) {
179 case OPT_LIST_OPTIONS
:
180 list_cmd_options(stdout
, load_opts
);
192 ret
= lttng_session_daemon_alive();
194 ERR("No session daemon is available");
200 session_name
= poptGetArg(pc
);
202 DBG2("Loading session name: %s", session_name
);
204 /* Default to load_all */
211 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
217 /* Open command element */
218 ret
= mi_lttng_writer_command_open(writer
,
219 mi_lttng_element_command_load
);
225 /* Open output element */
226 ret
= mi_lttng_writer_open_element(writer
,
227 mi_lttng_element_command_output
);
234 /* Prepare load attributes */
235 session_attr
= lttng_load_session_attr_create();
237 ERR("Failed to create load session attributes");
244 * lttng_load_session_attr_set_input_url only suppports absolute path.
245 * Use realpath to resolve any relative path.
247 if (opt_input_path
) {
248 input_path
= realpath(opt_input_path
, NULL
);
250 PERROR("Invalid input path");
258 ret
= lttng_load_session_attr_set_input_url(session_attr
,
261 ERR("Invalid input path");
266 /* Set the session name. NULL means all sessions should be loaded */
267 ret
= lttng_load_session_attr_set_session_name(session_attr
,
270 ERR("Invalid session name");
275 /* Set the overwrite attribute */
276 ret
= lttng_load_session_attr_set_overwrite(session_attr
, opt_force
);
278 ERR("Force argument could not be applied");
283 /* Set the overrides attributes if any */
284 if (opt_override_url
) {
285 ret
= lttng_load_session_attr_set_override_url(session_attr
,
288 ERR("Url override is invalid");
293 if (opt_override_session_name
) {
295 ERR("Options --all and --override-name cannot be used simultaneously");
299 ret
= lttng_load_session_attr_set_override_session_name(session_attr
,
300 opt_override_session_name
);
302 ERR("Failed to set session name override");
308 ret
= lttng_load_session(session_attr
);
310 ERR("%s", lttng_strerror(ret
));
315 MSG("All sessions have been loaded successfully");
316 } else if (session_name
) {
317 ret
= config_init((char *) session_name
);
319 WARN("Could not set %s as the default session",
322 MSG("Session %s has been loaded successfully", session_name
);
324 MSG("Session has been loaded successfully");
327 if (opt_override_session_name
) {
328 MSG("Session name overridden with %s",
329 opt_override_session_name
);
332 if (opt_override_url
) {
333 MSG("Session output url overridden with %s", opt_override_url
);
339 /* Mi Printing and closing */
342 ret
= mi_load_print(session_name
);
348 /* Close output element */
349 ret
= mi_lttng_writer_close_element(writer
);
356 ret
= mi_lttng_writer_write_element_bool(writer
,
357 mi_lttng_element_command_success
, success
);
363 /* Command element close */
364 ret
= mi_lttng_writer_command_close(writer
);
371 if (writer
&& mi_lttng_writer_destroy(writer
)) {
372 ERR("Failed to destroy mi lttng writer");
375 lttng_load_session_attr_destroy(session_attr
);