2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
10 #include "../utils.hpp"
12 #include <common/exception.hpp>
13 #include <common/mi-lttng.hpp>
14 #include <common/sessiond-comm/sessiond-comm.hpp>
21 #include <sys/types.h>
24 static struct mi_writer
*writer
;
26 #ifdef LTTNG_EMBED_HELP
27 static const char help_msg
[] =
28 #include <lttng-start.1.h>
39 static struct poptOption long_options
[] = {
40 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
41 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
42 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
43 { "glob", 'g', POPT_ARG_NONE
, nullptr, OPT_ENABLE_GLOB
, nullptr, nullptr },
44 { "all", 'a', POPT_ARG_NONE
, nullptr, OPT_ALL
, nullptr, nullptr },
45 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
48 static int mi_print_session(const char *session_name
, int enabled
)
52 /* Open session element */
53 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
58 /* Print session name element */
59 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
, session_name
);
64 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
, enabled
);
69 /* Close session element */
70 ret
= mi_lttng_writer_close_element(writer
);
79 * Start tracing for all trace of the session.
81 static int start_tracing(const char *session_name
)
85 if (session_name
== nullptr) {
90 DBG("Starting tracing for session %s", session_name
);
92 ret
= lttng_start_tracing(session_name
);
95 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
96 WARN("Tracing already started for session %s", session_name
);
99 ERR("%s", lttng_strerror(ret
));
107 MSG("Tracing started for session %s", session_name
);
109 ret
= mi_print_session(session_name
, 1);
120 static int start_tracing(const struct session_spec
& spec
)
122 int ret
= CMD_SUCCESS
;
123 bool had_warning
= false;
126 for (const auto& session
: list_sessions(spec
)) {
127 const auto sub_ret
= start_tracing(session
.name
);
140 } catch (const std::exception
& e
) {
141 ERR_FMT("{}", e
.what());
145 if (ret
== CMD_SUCCESS
&& had_warning
) {
155 * The 'start <options>' first level command
157 int cmd_start(int argc
, const char **argv
)
159 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
160 static poptContext pc
;
161 const char *leftover
= nullptr;
162 struct session_spec session_spec
= {
163 .type
= session_spec::NAME
,
167 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
168 poptReadDefaultConfig(pc
, 0);
170 while ((opt
= poptGetNextOpt(pc
)) != -1) {
175 case OPT_LIST_OPTIONS
:
176 list_cmd_options(stdout
, long_options
);
178 case OPT_ENABLE_GLOB
:
179 session_spec
.type
= session_spec::GLOB_PATTERN
;
182 session_spec
.type
= session_spec::ALL
;
190 session_spec
.value
= poptGetArg(pc
);
192 leftover
= poptGetArg(pc
);
194 ERR("Unknown argument: %s", leftover
);
201 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
203 ret
= -LTTNG_ERR_NOMEM
;
207 /* Open command element */
208 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_start
);
214 /* Open output element */
215 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
222 * Open sessions element
223 * For validation purpose
225 ret
= mi_lttng_writer_open_element(writer
, config_element_sessions
);
232 command_ret
= start_tracing(session_spec
);
239 /* Close sessions and output element */
240 ret
= mi_lttng_close_multi_element(writer
, 2);
247 ret
= mi_lttng_writer_write_element_bool(
248 writer
, mi_lttng_element_command_success
, success
);
254 /* Command element close */
255 ret
= mi_lttng_writer_command_close(writer
);
264 if (writer
&& mi_lttng_writer_destroy(writer
)) {
265 /* Preserve original error code */
266 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
269 /* Overwrite ret if an error occurred with start_tracing */
270 ret
= command_ret
? command_ret
: ret
;