2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <common/sessiond-comm/sessiond-comm.h>
18 #include <common/mi-lttng.h>
20 #include "../command.h"
23 static char *opt_session_name
;
24 static struct mi_writer
*writer
;
26 #ifdef LTTNG_EMBED_HELP
27 static const char help_msg
[] =
28 #include <lttng-start.1.h>
37 static struct poptOption long_options
[] = {
38 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
39 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
40 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
44 static int mi_print_session(char *session_name
, int enabled
)
48 /* Open session element */
49 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
54 /* Print session name element */
55 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
61 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
67 /* Close session element */
68 ret
= mi_lttng_writer_close_element(writer
);
77 * Start tracing for all trace of the session.
79 static int start_tracing(void)
84 if (opt_session_name
== NULL
) {
85 session_name
= get_session_name();
86 if (session_name
== NULL
) {
91 session_name
= opt_session_name
;
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);
121 if (opt_session_name
== NULL
) {
131 * The 'start <options>' first level command
133 int cmd_start(int argc
, const char **argv
)
135 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
136 static poptContext pc
;
137 const char *leftover
= NULL
;
139 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
140 poptReadDefaultConfig(pc
, 0);
142 while ((opt
= poptGetNextOpt(pc
)) != -1) {
147 case OPT_LIST_OPTIONS
:
148 list_cmd_options(stdout
, long_options
);
156 opt_session_name
= (char*) poptGetArg(pc
);
158 leftover
= poptGetArg(pc
);
160 ERR("Unknown argument: %s", leftover
);
167 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
169 ret
= -LTTNG_ERR_NOMEM
;
173 /* Open command element */
174 ret
= mi_lttng_writer_command_open(writer
,
175 mi_lttng_element_command_start
);
181 /* Open output element */
182 ret
= mi_lttng_writer_open_element(writer
,
183 mi_lttng_element_command_output
);
190 * Open sessions element
191 * For validation purpose
193 ret
= mi_lttng_writer_open_element(writer
,
194 config_element_sessions
);
201 command_ret
= start_tracing();
208 /* Close sessions and output element */
209 ret
= mi_lttng_close_multi_element(writer
, 2);
216 ret
= mi_lttng_writer_write_element_bool(writer
,
217 mi_lttng_element_command_success
, success
);
223 /* Command element close */
224 ret
= mi_lttng_writer_command_close(writer
);
233 if (writer
&& mi_lttng_writer_destroy(writer
)) {
234 /* Preserve original error code */
235 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
238 /* Overwrite ret if an error occurred with start_tracing */
239 ret
= command_ret
? command_ret
: ret
;