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
;
166 const char *leftover
= NULL
;
168 pc
= poptGetContext(NULL
, argc
, argv
, load_opts
, 0);
169 poptReadDefaultConfig(pc
, 0);
171 while ((opt
= poptGetNextOpt(pc
)) != -1) {
180 case OPT_LIST_OPTIONS
:
181 list_cmd_options(stdout
, load_opts
);
193 ret
= lttng_session_daemon_alive();
195 ERR("No session daemon is available");
201 session_name
= poptGetArg(pc
);
203 DBG2("Loading session name: %s", session_name
);
205 /* Default to load_all */
210 leftover
= poptGetArg(pc
);
212 ERR("Unknown argument: %s", leftover
);
219 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
225 /* Open command element */
226 ret
= mi_lttng_writer_command_open(writer
,
227 mi_lttng_element_command_load
);
233 /* Open output element */
234 ret
= mi_lttng_writer_open_element(writer
,
235 mi_lttng_element_command_output
);
242 /* Prepare load attributes */
243 session_attr
= lttng_load_session_attr_create();
245 ERR("Failed to create load session attributes");
252 * lttng_load_session_attr_set_input_url only suppports absolute path.
253 * Use realpath to resolve any relative path.
255 if (opt_input_path
) {
256 input_path
= realpath(opt_input_path
, NULL
);
258 PERROR("Invalid input path");
266 ret
= lttng_load_session_attr_set_input_url(session_attr
,
269 ERR("Invalid input path");
274 /* Set the session name. NULL means all sessions should be loaded */
275 ret
= lttng_load_session_attr_set_session_name(session_attr
,
278 ERR("Invalid session name");
283 /* Set the overwrite attribute */
284 ret
= lttng_load_session_attr_set_overwrite(session_attr
, opt_force
);
286 ERR("Force argument could not be applied");
291 /* Set the overrides attributes if any */
292 if (opt_override_url
) {
293 ret
= lttng_load_session_attr_set_override_url(session_attr
,
296 ERR("Url override is invalid");
301 if (opt_override_session_name
) {
303 ERR("Options --all and --override-name cannot be used simultaneously");
307 ret
= lttng_load_session_attr_set_override_session_name(session_attr
,
308 opt_override_session_name
);
310 ERR("Failed to set session name override");
316 ret
= lttng_load_session(session_attr
);
318 ERR("%s", lttng_strerror(ret
));
323 MSG("All sessions have been loaded successfully");
324 } else if (session_name
) {
325 ret
= config_init((char *) session_name
);
327 WARN("Could not set %s as the default session",
330 MSG("Session %s has been loaded successfully", session_name
);
332 MSG("Session has been loaded successfully");
335 if (opt_override_session_name
) {
336 MSG("Session name overridden with %s",
337 opt_override_session_name
);
340 if (opt_override_url
) {
341 MSG("Session output url overridden with %s", opt_override_url
);
347 /* Mi Printing and closing */
350 ret
= mi_load_print(session_name
);
356 /* Close output element */
357 ret
= mi_lttng_writer_close_element(writer
);
364 ret
= mi_lttng_writer_write_element_bool(writer
,
365 mi_lttng_element_command_success
, success
);
371 /* Command element close */
372 ret
= mi_lttng_writer_command_close(writer
);
379 if (writer
&& mi_lttng_writer_destroy(writer
)) {
380 ERR("Failed to destroy mi lttng writer");
383 lttng_load_session_attr_destroy(session_attr
);