2 * Copyright (C) 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <common/mi-lttng.h>
16 #include <common/config/session-config.h>
17 #include <lttng/lttng.h>
19 #include "../command.h"
21 static char *the_opt_input_path
;
22 static char *the_opt_override_url
;
23 static char *the_opt_override_session_name
;
24 static int the_opt_force
;
25 static int the_opt_load_all
;
27 static const char *the_session_name
;
29 #ifdef LTTNG_EMBED_HELP
30 static const char help_msg
[] =
31 #include <lttng-load.1.h>
42 static struct mi_writer
*the_writer
;
44 static struct poptOption the_load_opts
[] = {
45 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
46 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
47 {"all", 'a', POPT_ARG_NONE
, 0, OPT_ALL
, 0, 0},
48 {"input-path", 'i', POPT_ARG_STRING
, &the_opt_input_path
, 0, 0, 0},
49 {"force", 'f', POPT_ARG_NONE
, 0, OPT_FORCE
, 0, 0},
50 {"override-url", 0, POPT_ARG_STRING
, &the_opt_override_url
, 0, 0, 0},
51 {"override-name", 0, POPT_ARG_STRING
, &the_opt_override_session_name
, 0, 0, 0},
52 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
56 static int mi_partial_session(const char *session_name
)
59 LTTNG_ASSERT(the_writer
);
60 LTTNG_ASSERT(session_name
);
62 /* Open session element */
63 ret
= mi_lttng_writer_open_element(the_writer
, config_element_session
);
68 ret
= mi_lttng_writer_write_element_string(the_writer
, config_element_name
,
74 /* Closing session element */
75 ret
= mi_lttng_writer_close_element(the_writer
);
81 * Mi print of load command
83 static int mi_load_print(const char *session_name
)
86 LTTNG_ASSERT(the_writer
);
88 if (the_opt_load_all
) {
89 /* We use a wildcard to represent all sessions */
93 /* Print load element */
94 ret
= mi_lttng_writer_open_element(the_writer
, mi_lttng_element_load
);
99 /* Print session element */
100 ret
= mi_partial_session(session_name
);
106 if (the_opt_input_path
) {
107 ret
= mi_lttng_writer_write_element_string(the_writer
, config_element_path
,
114 /* Print override elements */
115 ret
= mi_lttng_writer_open_element(the_writer
, mi_lttng_element_load_overrides
);
120 /* Session name override element */
121 if (the_opt_override_session_name
) {
122 ret
= mi_lttng_writer_write_element_string(the_writer
,
123 config_element_name
, the_opt_override_session_name
);
129 /* Session url override element */
130 if (the_opt_override_url
) {
131 ret
= mi_lttng_writer_write_element_string(the_writer
,
132 mi_lttng_element_load_override_url
,
133 the_opt_override_url
);
139 /* Close override and load element */
140 ret
= mi_lttng_close_multi_element(the_writer
, 2);
146 * The 'load <options>' first level command
148 int cmd_load(int argc
, const char **argv
)
153 struct lttng_load_session_attr
*session_attr
= NULL
;
154 char *input_path
= NULL
;
155 const char *leftover
= NULL
;
157 pc
= poptGetContext(NULL
, argc
, argv
, the_load_opts
, 0);
158 poptReadDefaultConfig(pc
, 0);
160 while ((opt
= poptGetNextOpt(pc
)) != -1) {
167 the_opt_load_all
= 1;
169 case OPT_LIST_OPTIONS
:
170 list_cmd_options(stdout
, the_load_opts
);
182 ret
= lttng_session_daemon_alive();
184 ERR("No session daemon is available");
189 if (!the_opt_load_all
) {
190 the_session_name
= poptGetArg(pc
);
191 if (the_session_name
) {
192 DBG2("Loading session name: %s", the_session_name
);
194 /* Default to load_all */
195 the_opt_load_all
= 1;
199 leftover
= poptGetArg(pc
);
201 ERR("Unknown argument: %s", leftover
);
208 the_writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
214 /* Open command element */
215 ret
= mi_lttng_writer_command_open(the_writer
,
216 mi_lttng_element_command_load
);
222 /* Open output element */
223 ret
= mi_lttng_writer_open_element(the_writer
,
224 mi_lttng_element_command_output
);
231 /* Prepare load attributes */
232 session_attr
= lttng_load_session_attr_create();
234 ERR("Failed to create load session attributes");
241 * lttng_load_session_attr_set_input_url only suppports absolute path.
242 * Use realpath to resolve any relative path.
244 if (the_opt_input_path
) {
245 input_path
= realpath(the_opt_input_path
, NULL
);
247 PERROR("Invalid input path");
255 ret
= lttng_load_session_attr_set_input_url(session_attr
,
258 ERR("Invalid input path");
263 /* Set the session name. NULL means all sessions should be loaded */
264 ret
= lttng_load_session_attr_set_session_name(session_attr
,
267 ERR("Invalid session name");
272 /* Set the overwrite attribute */
273 ret
= lttng_load_session_attr_set_overwrite(session_attr
, the_opt_force
);
275 ERR("Force argument could not be applied");
280 /* Set the overrides attributes if any */
281 if (the_opt_override_url
) {
282 ret
= lttng_load_session_attr_set_override_url(session_attr
,
283 the_opt_override_url
);
285 ERR("Url override is invalid");
290 if (the_opt_override_session_name
) {
291 if (the_opt_load_all
) {
292 ERR("Options --all and --override-name cannot be used simultaneously");
296 ret
= lttng_load_session_attr_set_override_session_name(session_attr
,
297 the_opt_override_session_name
);
299 ERR("Failed to set session name override");
305 ret
= lttng_load_session(session_attr
);
307 ERR("%s", lttng_strerror(ret
));
311 if (the_opt_load_all
) {
312 MSG("All sessions have been loaded successfully");
313 } else if (the_session_name
) {
314 ret
= config_init((char *) the_session_name
);
316 WARN("Could not set %s as the default session",
319 MSG("Session %s has been loaded successfully", the_session_name
);
321 MSG("Session has been loaded successfully");
324 if (the_opt_override_session_name
) {
325 MSG("Session name overridden with %s",
326 the_opt_override_session_name
);
329 if (the_opt_override_url
) {
330 MSG("Session output url overridden with %s", the_opt_override_url
);
336 /* Mi Printing and closing */
339 ret
= mi_load_print(the_session_name
);
345 /* Close output element */
346 ret
= mi_lttng_writer_close_element(the_writer
);
353 ret
= mi_lttng_writer_write_element_bool(the_writer
,
354 mi_lttng_element_command_success
, success
);
360 /* Command element close */
361 ret
= mi_lttng_writer_command_close(the_writer
);
368 if (the_writer
&& mi_lttng_writer_destroy(the_writer
)) {
369 ERR("Failed to destroy mi lttng writer");
372 lttng_load_session_attr_destroy(session_attr
);