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>
33 struct mi_writer
*writer
;
35 #ifdef LTTNG_EMBED_HELP
36 const char help_msg
[] =
37 #include <lttng-stop.1.h>
41 struct poptOption long_options
[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
43 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
44 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
45 { "no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, nullptr, nullptr },
46 { "glob", 'g', POPT_ARG_NONE
, nullptr, OPT_ENABLE_GLOB
, nullptr, nullptr },
47 { "all", 'a', POPT_ARG_NONE
, nullptr, OPT_ALL
, nullptr, nullptr },
48 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
52 * Mi print of partial session
54 int mi_print_session(const char *session_name
, int enabled
)
58 LTTNG_ASSERT(session_name
);
60 /* Open session element */
61 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
66 /* Print session name element */
67 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
, session_name
);
73 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
, enabled
);
78 /* Close session element */
79 ret
= mi_lttng_writer_close_element(writer
);
86 * Start tracing for all trace of the session.
88 cmd_error_code
stop_tracing(const char *session_name
)
92 ret
= lttng_stop_tracing_no_wait(session_name
);
94 LTTNG_THROW_CTL(lttng::format("Failed to start session `{}`", session_name
),
95 static_cast<lttng_error_code
>(-ret
));
99 _MSG("Waiting for data availability");
102 ret
= lttng_data_pending(session_name
);
104 /* Return the data available call error. */
105 ERR_FMT("Failed to check pending data for session `{}` ({})",
107 lttng_strerror(ret
));
112 * Data sleep time before retrying (in usec). Don't sleep if the call
113 * returned value indicates availability.
116 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
124 print_session_stats(session_name
);
125 MSG("Tracing stopped for session `%s`", session_name
);
127 if (mi_print_session(session_name
, 0)) {
135 cmd_error_code
stop_tracing(const lttng::cli::session_spec
& spec
)
137 bool had_warning
= false;
138 bool had_error
= false;
139 bool listing_failed
= false;
141 const auto sessions
= [&listing_failed
, &spec
]() -> lttng::cli::session_list
{
143 return list_sessions(spec
);
144 } catch (const lttng::ctl::error
& ctl_exception
) {
145 ERR_FMT("Failed to list sessions ({})",
146 lttng_strerror(-ctl_exception
.code()));
147 listing_failed
= true;
152 if (!listing_failed
&& sessions
.size() == 0 &&
153 spec
.type_
== lttng::cli::session_spec::type::NAME
) {
154 ERR_FMT("Session `{}` not found", spec
.value
);
158 if (listing_failed
) {
162 for (const auto& session
: sessions
) {
163 cmd_error_code sub_ret
;
166 sub_ret
= stop_tracing(session
.name
);
167 } catch (const lttng::ctl::error
& ctl_exception
) {
168 switch (ctl_exception
.code()) {
169 case LTTNG_ERR_TRACE_ALREADY_STOPPED
:
170 WARN_FMT("Tracing already stopped for session `{}`", session
.name
);
171 sub_ret
= CMD_SUCCESS
;
173 case LTTNG_ERR_NO_SESSION
:
174 if (spec
.type_
!= lttng::cli::session_spec::type::NAME
) {
175 /* Session destroyed during command, ignore and carry-on. */
176 sub_ret
= CMD_SUCCESS
;
182 case LTTNG_ERR_NO_SESSIOND
:
183 /* Don't keep going on a fatal error. */
188 ERR_FMT("Failed to stop session `{}` ({})",
190 lttng_strerror(-ctl_exception
.code()));
195 /* Keep going, but report the most serious state. */
196 had_warning
|= sub_ret
== CMD_WARNING
;
197 had_error
|= sub_ret
== CMD_ERROR
;
202 } else if (had_warning
) {
213 * The 'stop <options>' first level command
215 int cmd_stop(int argc
, const char **argv
)
218 cmd_error_code command_ret
= CMD_SUCCESS
;
220 static poptContext pc
;
221 const char *leftover
= nullptr;
222 lttng::cli::session_spec
session_spec(lttng::cli::session_spec::type::NAME
);
224 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
225 poptReadDefaultConfig(pc
, 0);
227 while ((opt
= poptGetNextOpt(pc
)) != -1) {
234 command_ret
= static_cast<cmd_error_code
>(ret
);
237 case OPT_LIST_OPTIONS
:
238 list_cmd_options(stdout
, long_options
);
240 case OPT_ENABLE_GLOB
:
241 session_spec
.type_
= lttng::cli::session_spec::type::GLOB_PATTERN
;
244 session_spec
.type_
= lttng::cli::session_spec::type::ALL
;
247 command_ret
= CMD_UNDEFINED
;
254 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
256 command_ret
= CMD_ERROR
;
260 /* Open command element */
261 if (mi_lttng_writer_command_open(writer
, mi_lttng_element_command_stop
)) {
262 command_ret
= CMD_ERROR
;
266 /* Open output element */
267 if (mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
)) {
268 command_ret
= CMD_ERROR
;
273 * Open sessions element
276 if (mi_lttng_writer_open_element(writer
, config_element_sessions
)) {
277 command_ret
= CMD_ERROR
;
282 session_spec
.value
= poptGetArg(pc
);
284 leftover
= poptGetArg(pc
);
286 ERR("Unknown argument: %s", leftover
);
287 command_ret
= CMD_ERROR
;
291 command_ret
= stop_tracing(session_spec
);
298 /* Close sessions and output element */
299 if (mi_lttng_close_multi_element(writer
, 2)) {
300 command_ret
= CMD_ERROR
;
305 if (mi_lttng_writer_write_element_bool(
306 writer
, mi_lttng_element_command_success
, success
)) {
307 command_ret
= CMD_ERROR
;
311 /* Command element close */
312 if (mi_lttng_writer_command_close(writer
)) {
313 command_ret
= CMD_ERROR
;
320 if (writer
&& mi_lttng_writer_destroy(writer
)) {
321 command_ret
= CMD_ERROR
;