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
;
35 static int opt_load_all
;
37 static const char *session_name
;
46 static struct mi_writer
*writer
;
48 static struct poptOption load_opts
[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
51 {"all", 'a', POPT_ARG_NONE
, 0, OPT_ALL
, 0, 0},
52 {"input-path", 'i', POPT_ARG_STRING
, &opt_input_path
, 0, 0, 0},
53 {"force", 'f', POPT_ARG_NONE
, 0, OPT_FORCE
, 0, 0},
54 {"override-url",'U', POPT_ARG_STRING
, &opt_override_url
, 0, 0, 0},
55 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
59 static int mi_partial_session(const char *session_name
)
65 /* Open session element */
66 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
71 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
77 /* Closing session element */
78 ret
= mi_lttng_writer_close_element(writer
);
84 * Mi print of load command
86 static int mi_load_print(const char *session_name
)
92 /* We use a wildcard to represent all sessions */
96 /* Print load element */
97 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_load
);
102 /* Print session element */
103 ret
= mi_partial_session(session_name
);
109 if (opt_input_path
) {
110 ret
= mi_lttng_writer_write_element_string(writer
, config_element_path
,
117 /* Close load element */
118 ret
= mi_lttng_writer_close_element(writer
);
125 * The 'load <options>' first level command
127 int cmd_load(int argc
, const char **argv
)
132 struct lttng_load_session_attr
*session_attr
= NULL
;
133 char *input_path
= NULL
;
135 pc
= poptGetContext(NULL
, argc
, argv
, load_opts
, 0);
136 poptReadDefaultConfig(pc
, 0);
138 while ((opt
= poptGetNextOpt(pc
)) != -1) {
147 case OPT_LIST_OPTIONS
:
148 list_cmd_options(stdout
, load_opts
);
160 ret
= lttng_session_daemon_alive();
162 ERR("No session daemon is available");
168 session_name
= poptGetArg(pc
);
170 DBG2("Loading session name: %s", session_name
);
172 /* Default to load_all */
179 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
185 /* Open command element */
186 ret
= mi_lttng_writer_command_open(writer
,
187 mi_lttng_element_command_load
);
193 /* Open output element */
194 ret
= mi_lttng_writer_open_element(writer
,
195 mi_lttng_element_command_output
);
202 /* Prepare load attributes */
203 session_attr
= lttng_load_session_attr_create();
205 ERR("Failed to create load session attributes");
212 * lttng_load_session_attr_set_input_url only suppports absolute path.
213 * Use realpath to resolve any relative path.
215 if (opt_input_path
) {
216 input_path
= realpath(opt_input_path
, NULL
);
218 PERROR("Invalid input path");
226 ret
= lttng_load_session_attr_set_input_url(session_attr
,
229 ERR("Invalid input path");
234 /* Set the session name. NULL means all sessions should be loaded */
235 ret
= lttng_load_session_attr_set_session_name(session_attr
,
238 ERR("Invalid session name");
243 /* Set the overwrite attribute */
244 ret
= lttng_load_session_attr_set_overwrite(session_attr
, opt_force
);
246 ERR("Force argument could not be applied");
251 /* Set the overrides attributes if any */
252 if (opt_override_url
) {
253 ret
= lttng_load_session_attr_set_override_url(session_attr
,
256 ERR("Url override is invalid");
261 ret
= lttng_load_session(session_attr
);
263 ERR("%s", lttng_strerror(ret
));
268 MSG("All sessions have been loaded successfully");
269 } else if (session_name
) {
270 ret
= config_init((char *) session_name
);
274 MSG("Session %s has been loaded successfully", session_name
);
276 MSG("Session has been loaded successfully");
279 if (opt_override_url
) {
280 MSG("Session output url overridden with %s", opt_override_url
);
286 /* Mi Printing and closing */
289 ret
= mi_load_print(session_name
);
295 /* Close output element */
296 ret
= mi_lttng_writer_close_element(writer
);
303 ret
= mi_lttng_writer_write_element_bool(writer
,
304 mi_lttng_element_command_success
, success
);
310 /* Command element close */
311 ret
= mi_lttng_writer_command_close(writer
);
318 if (writer
&& mi_lttng_writer_destroy(writer
)) {
319 ERR("Failed to destroy mi lttng writer");
322 lttng_load_session_attr_destroy(session_attr
);