2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
12 #include <common/sessiond-comm/sessiond-comm.hpp>
19 #include <sys/types.h>
22 static struct mi_writer
*writer
;
24 #ifdef LTTNG_EMBED_HELP
25 static const char help_msg
[] =
26 #include <lttng-start.1.h>
35 static struct poptOption long_options
[] = {
36 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
37 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
38 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
39 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
42 static int mi_print_session(char *session_name
, int enabled
)
46 /* Open session element */
47 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
52 /* Print session name element */
53 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
, session_name
);
58 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
, enabled
);
63 /* Close session element */
64 ret
= mi_lttng_writer_close_element(writer
);
73 * Start tracing for all trace of the session.
75 static int start_tracing(const char *arg_session_name
)
80 if (arg_session_name
== nullptr) {
81 session_name
= get_session_name();
83 session_name
= strdup(arg_session_name
);
84 if (session_name
== nullptr) {
85 PERROR("Failed to copy session name");
89 if (session_name
== nullptr) {
94 DBG("Starting tracing for session %s", session_name
);
96 ret
= lttng_start_tracing(session_name
);
99 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
100 WARN("Tracing already started for session %s", session_name
);
103 ERR("%s", lttng_strerror(ret
));
111 MSG("Tracing started for session %s", session_name
);
113 ret
= mi_print_session(session_name
, 1);
129 * The 'start <options>' first level command
131 int cmd_start(int argc
, const char **argv
)
133 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
134 static poptContext pc
;
135 const char *arg_session_name
= nullptr;
136 const char *leftover
= nullptr;
138 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
139 poptReadDefaultConfig(pc
, 0);
141 while ((opt
= poptGetNextOpt(pc
)) != -1) {
146 case OPT_LIST_OPTIONS
:
147 list_cmd_options(stdout
, long_options
);
155 arg_session_name
= poptGetArg(pc
);
157 leftover
= poptGetArg(pc
);
159 ERR("Unknown argument: %s", leftover
);
166 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
168 ret
= -LTTNG_ERR_NOMEM
;
172 /* Open command element */
173 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_start
);
179 /* Open output element */
180 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
187 * Open sessions element
188 * For validation purpose
190 ret
= mi_lttng_writer_open_element(writer
, config_element_sessions
);
197 command_ret
= start_tracing(arg_session_name
);
204 /* Close sessions and output element */
205 ret
= mi_lttng_close_multi_element(writer
, 2);
212 ret
= mi_lttng_writer_write_element_bool(
213 writer
, mi_lttng_element_command_success
, success
);
219 /* Command element close */
220 ret
= mi_lttng_writer_command_close(writer
);
229 if (writer
&& mi_lttng_writer_destroy(writer
)) {
230 /* Preserve original error code */
231 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
234 /* Overwrite ret if an error occurred with start_tracing */
235 ret
= command_ret
? command_ret
: ret
;