2 * Copyright (C) 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/config/session-config.hpp>
12 #include <common/mi-lttng.hpp>
14 #include <lttng/lttng.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
, nullptr, OPT_HELP
, nullptr, nullptr },
48 { "all", 'a', POPT_ARG_NONE
, nullptr, OPT_ALL
, nullptr, nullptr },
49 { "input-path", 'i', POPT_ARG_STRING
, &the_opt_input_path
, 0, nullptr, nullptr },
50 { "force", 'f', POPT_ARG_NONE
, nullptr, OPT_FORCE
, nullptr, nullptr },
51 { "override-url", 0, POPT_ARG_STRING
, &the_opt_override_url
, 0, nullptr, nullptr },
52 { "override-name", 0, POPT_ARG_STRING
, &the_opt_override_session_name
, 0, nullptr, nullptr },
53 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
54 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
57 static int mi_partial_session(const char *session_name
)
60 LTTNG_ASSERT(the_writer
);
61 LTTNG_ASSERT(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
, session_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(
108 the_writer
, config_element_path
, the_opt_input_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(
123 the_writer
, 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(
132 the_writer
, mi_lttng_element_load_override_url
, the_opt_override_url
);
138 /* Close override and load element */
139 ret
= mi_lttng_close_multi_element(the_writer
, 2);
145 * The 'load <options>' first level command
147 int cmd_load(int argc
, const char **argv
)
152 struct lttng_load_session_attr
*session_attr
= nullptr;
153 char *input_path
= nullptr;
154 const char *leftover
= nullptr;
156 pc
= poptGetContext(nullptr, argc
, argv
, the_load_opts
, 0);
157 poptReadDefaultConfig(pc
, 0);
159 while ((opt
= poptGetNextOpt(pc
)) != -1) {
166 the_opt_load_all
= 1;
168 case OPT_LIST_OPTIONS
:
169 list_cmd_options(stdout
, the_load_opts
);
181 ret
= lttng_session_daemon_alive();
183 ERR("No session daemon is available");
188 if (!the_opt_load_all
) {
189 the_session_name
= poptGetArg(pc
);
190 if (the_session_name
) {
191 DBG2("Loading session name: %s", the_session_name
);
193 /* Default to load_all */
194 the_opt_load_all
= 1;
198 leftover
= poptGetArg(pc
);
200 ERR("Unknown argument: %s", leftover
);
207 the_writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
213 /* Open command element */
214 ret
= mi_lttng_writer_command_open(the_writer
, mi_lttng_element_command_load
);
220 /* Open output element */
221 ret
= mi_lttng_writer_open_element(the_writer
, mi_lttng_element_command_output
);
228 /* Prepare load attributes */
229 session_attr
= lttng_load_session_attr_create();
231 ERR("Failed to create load session attributes");
238 * lttng_load_session_attr_set_input_url only suppports absolute path.
239 * Use realpath to resolve any relative path.
241 if (the_opt_input_path
) {
242 input_path
= realpath(the_opt_input_path
, nullptr);
244 PERROR("Invalid input path");
249 input_path
= nullptr;
252 ret
= lttng_load_session_attr_set_input_url(session_attr
, input_path
);
254 ERR("Invalid input path");
259 /* Set the session name. NULL means all sessions should be loaded */
260 ret
= lttng_load_session_attr_set_session_name(session_attr
, the_session_name
);
262 ERR("Invalid session name");
267 /* Set the overwrite attribute */
268 ret
= lttng_load_session_attr_set_overwrite(session_attr
, the_opt_force
);
270 ERR("Force argument could not be applied");
275 /* Set the overrides attributes if any */
276 if (the_opt_override_url
) {
277 ret
= lttng_load_session_attr_set_override_url(session_attr
, the_opt_override_url
);
279 ERR("Url override is invalid");
284 if (the_opt_override_session_name
) {
285 if (the_opt_load_all
) {
286 ERR("Options --all and --override-name cannot be used simultaneously");
290 ret
= lttng_load_session_attr_set_override_session_name(
291 session_attr
, the_opt_override_session_name
);
293 ERR("Failed to set session name override");
299 ret
= lttng_load_session(session_attr
);
301 ERR("%s", lttng_strerror(ret
));
305 if (the_opt_load_all
) {
306 MSG("All sessions have been loaded successfully");
307 } else if (the_session_name
) {
308 ret
= config_init((char *) the_session_name
);
310 WARN("Could not set %s as the default session", the_session_name
);
312 MSG("Session %s has been loaded successfully", the_session_name
);
314 MSG("Session has been loaded successfully");
317 if (the_opt_override_session_name
) {
318 MSG("Session name overridden with %s", the_opt_override_session_name
);
321 if (the_opt_override_url
) {
322 MSG("Session output url overridden with %s", the_opt_override_url
);
328 /* Mi Printing and closing */
331 ret
= mi_load_print(the_session_name
);
337 /* Close output element */
338 ret
= mi_lttng_writer_close_element(the_writer
);
345 ret
= mi_lttng_writer_write_element_bool(
346 the_writer
, mi_lttng_element_command_success
, success
);
352 /* Command element close */
353 ret
= mi_lttng_writer_command_close(the_writer
);
360 if (the_writer
&& mi_lttng_writer_destroy(the_writer
)) {
361 ERR("Failed to destroy mi lttng writer");
364 lttng_load_session_attr_destroy(session_attr
);