2 * Copyright (C) 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
16 #include <common/mi-lttng.h>
17 #include <common/config/session-config.h>
18 #include <lttng/lttng.h>
20 #include "../command.h"
22 static char *the_opt_input_path
;
23 static char *the_opt_override_url
;
24 static char *the_opt_override_session_name
;
25 static int the_opt_force
;
26 static int the_opt_load_all
;
28 static const char *the_session_name
;
30 #ifdef LTTNG_EMBED_HELP
31 static const char help_msg
[] =
32 #include <lttng-load.1.h>
43 static struct mi_writer
*the_writer
;
45 static struct poptOption the_load_opts
[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
48 {"all", 'a', POPT_ARG_NONE
, 0, OPT_ALL
, 0, 0},
49 {"input-path", 'i', POPT_ARG_STRING
, &the_opt_input_path
, 0, 0, 0},
50 {"force", 'f', POPT_ARG_NONE
, 0, OPT_FORCE
, 0, 0},
51 {"override-url", 0, POPT_ARG_STRING
, &the_opt_override_url
, 0, 0, 0},
52 {"override-name", 0, POPT_ARG_STRING
, &the_opt_override_session_name
, 0, 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(the_writer
, config_element_session
);
69 ret
= mi_lttng_writer_write_element_string(the_writer
, config_element_name
,
75 /* Closing session element */
76 ret
= mi_lttng_writer_close_element(the_writer
);
82 * Mi print of load command
84 static int mi_load_print(const char *session_name
)
89 if (the_opt_load_all
) {
90 /* We use a wildcard to represent all sessions */
94 /* Print load element */
95 ret
= mi_lttng_writer_open_element(the_writer
, mi_lttng_element_load
);
100 /* Print session element */
101 ret
= mi_partial_session(session_name
);
107 if (the_opt_input_path
) {
108 ret
= mi_lttng_writer_write_element_string(the_writer
, config_element_path
,
115 /* Print override elements */
116 ret
= mi_lttng_writer_open_element(the_writer
, mi_lttng_element_load_overrides
);
121 /* Session name override element */
122 if (the_opt_override_session_name
) {
123 ret
= mi_lttng_writer_write_element_string(the_writer
,
124 config_element_name
, the_opt_override_session_name
);
130 /* Session url override element */
131 if (the_opt_override_url
) {
132 ret
= mi_lttng_writer_write_element_string(the_writer
,
133 mi_lttng_element_load_override_url
,
134 the_opt_override_url
);
140 /* Close override and load element */
141 ret
= mi_lttng_close_multi_element(the_writer
, 2);
147 * The 'load <options>' first level command
149 int cmd_load(int argc
, const char **argv
)
154 struct lttng_load_session_attr
*session_attr
= NULL
;
155 char *input_path
= NULL
;
156 const char *leftover
= NULL
;
158 pc
= poptGetContext(NULL
, argc
, argv
, the_load_opts
, 0);
159 poptReadDefaultConfig(pc
, 0);
161 while ((opt
= poptGetNextOpt(pc
)) != -1) {
168 the_opt_load_all
= 1;
170 case OPT_LIST_OPTIONS
:
171 list_cmd_options(stdout
, the_load_opts
);
183 ret
= lttng_session_daemon_alive();
185 ERR("No session daemon is available");
190 if (!the_opt_load_all
) {
191 the_session_name
= poptGetArg(pc
);
192 if (the_session_name
) {
193 DBG2("Loading session name: %s", the_session_name
);
195 /* Default to load_all */
196 the_opt_load_all
= 1;
200 leftover
= poptGetArg(pc
);
202 ERR("Unknown argument: %s", leftover
);
209 the_writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
215 /* Open command element */
216 ret
= mi_lttng_writer_command_open(the_writer
,
217 mi_lttng_element_command_load
);
223 /* Open output element */
224 ret
= mi_lttng_writer_open_element(the_writer
,
225 mi_lttng_element_command_output
);
232 /* Prepare load attributes */
233 session_attr
= lttng_load_session_attr_create();
235 ERR("Failed to create load session attributes");
242 * lttng_load_session_attr_set_input_url only suppports absolute path.
243 * Use realpath to resolve any relative path.
245 if (the_opt_input_path
) {
246 input_path
= realpath(the_opt_input_path
, NULL
);
248 PERROR("Invalid input path");
256 ret
= lttng_load_session_attr_set_input_url(session_attr
,
259 ERR("Invalid input path");
264 /* Set the session name. NULL means all sessions should be loaded */
265 ret
= lttng_load_session_attr_set_session_name(session_attr
,
268 ERR("Invalid session name");
273 /* Set the overwrite attribute */
274 ret
= lttng_load_session_attr_set_overwrite(session_attr
, the_opt_force
);
276 ERR("Force argument could not be applied");
281 /* Set the overrides attributes if any */
282 if (the_opt_override_url
) {
283 ret
= lttng_load_session_attr_set_override_url(session_attr
,
284 the_opt_override_url
);
286 ERR("Url override is invalid");
291 if (the_opt_override_session_name
) {
292 if (the_opt_load_all
) {
293 ERR("Options --all and --override-name cannot be used simultaneously");
297 ret
= lttng_load_session_attr_set_override_session_name(session_attr
,
298 the_opt_override_session_name
);
300 ERR("Failed to set session name override");
306 ret
= lttng_load_session(session_attr
);
308 ERR("%s", lttng_strerror(ret
));
312 if (the_opt_load_all
) {
313 MSG("All sessions have been loaded successfully");
314 } else if (the_session_name
) {
315 ret
= config_init((char *) the_session_name
);
317 WARN("Could not set %s as the default session",
320 MSG("Session %s has been loaded successfully", the_session_name
);
322 MSG("Session has been loaded successfully");
325 if (the_opt_override_session_name
) {
326 MSG("Session name overridden with %s",
327 the_opt_override_session_name
);
330 if (the_opt_override_url
) {
331 MSG("Session output url overridden with %s", the_opt_override_url
);
337 /* Mi Printing and closing */
340 ret
= mi_load_print(the_session_name
);
346 /* Close output element */
347 ret
= mi_lttng_writer_close_element(the_writer
);
354 ret
= mi_lttng_writer_write_element_bool(the_writer
,
355 mi_lttng_element_command_success
, success
);
361 /* Command element close */
362 ret
= mi_lttng_writer_command_close(the_writer
);
369 if (the_writer
&& mi_lttng_writer_destroy(the_writer
)) {
370 ERR("Failed to destroy mi lttng writer");
373 lttng_load_session_attr_destroy(session_attr
);