2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/exception.hpp>
12 #include <common/make-unique-wrapper.hpp>
13 #include <common/mi-lttng.hpp>
14 #include <common/scope-exit.hpp>
15 #include <common/sessiond-comm/sessiond-comm.hpp>
16 #include <common/utils.hpp>
18 #include <lttng/lttng.h>
26 #include <sys/types.h>
37 #ifdef LTTNG_EMBED_HELP
38 const char help_msg
[] =
39 #include <lttng-destroy.1.h>
46 struct mi_writer
*writer
;
48 struct poptOption long_options
[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
51 { "all", 'a', POPT_ARG_NONE
, nullptr, OPT_ALL
, nullptr, nullptr },
52 { "glob", 'g', POPT_ARG_NONE
, nullptr, OPT_ENABLE_GLOB
, nullptr, nullptr },
53 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
54 { "no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, nullptr, nullptr },
55 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
61 * Unregister the provided session to the session daemon. On success, removes
62 * the default configuration.
64 cmd_error_code
destroy_session(const lttng_session
& session
)
67 bool newline_needed
= false, printed_destroy_msg
= false;
69 const auto print_trailing_new_line
= lttng::make_scope_exit([&newline_needed
]() noexcept
{
75 ret
= lttng_stop_tracing_no_wait(session
.name
);
76 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
77 LTTNG_THROW_CTL(lttng::format("Failed to stop session `{}`", session
.name
),
78 static_cast<lttng_error_code
>(-ret
));
81 const auto session_was_already_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
84 ret
= lttng_data_pending(session
.name
);
86 /* Return the data available call error. */
87 ERR_FMT("Failed to check pending data for session `{}` ({})",
94 * Data sleep time before retrying (in usec). Don't
95 * sleep if the call returned value indicates
99 if (!printed_destroy_msg
) {
100 _MSG("Destroying session `%s`", session
.name
);
101 newline_needed
= true;
102 printed_destroy_msg
= true;
106 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
113 std::unique_ptr
<char,
114 lttng::memory::create_deleter_class
<char, lttng::memory::free
>::deleter
>
116 if (!session_was_already_stopped
) {
117 char *raw_stats_str
= nullptr;
120 * Don't print the event and packet loss warnings since the user
121 * already saw them when stopping the trace.
123 ret
= get_session_stats_str(session
.name
, &raw_stats_str
);
128 /* May still be null if there are no stats to print. */
129 stats_str
.reset(raw_stats_str
);
132 const auto destruction_handle
= [&session
]() {
133 struct lttng_destruction_handle
*raw_destruction_handle
= nullptr;
136 lttng_destroy_session_ext(session
.name
, &raw_destruction_handle
);
137 if (ctl_ret_code
!= LTTNG_OK
) {
138 LTTNG_THROW_CTL(lttng::format("Failed to destroy session `{}`",
143 return lttng::make_unique_wrapper
<lttng_destruction_handle
,
144 lttng_destruction_handle_destroy
>(
145 raw_destruction_handle
);
149 enum lttng_destruction_handle_status status
;
152 status
= lttng_destruction_handle_wait_for_completion(
153 destruction_handle
.get(),
154 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
156 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
157 if (!printed_destroy_msg
) {
158 _MSG("Destroying session `%s`", session
.name
);
159 newline_needed
= true;
160 printed_destroy_msg
= true;
165 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
168 ERR_FMT("{}An error occurred during the destruction of session `{}`",
169 newline_needed
? "\n" : "",
171 newline_needed
= false;
174 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
176 enum lttng_error_code ctl_ret_code
;
177 status
= lttng_destruction_handle_get_result(destruction_handle
.get(),
179 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
180 ERR_FMT("{}Failed to query the result of the destruction of session `{}`",
181 newline_needed
? "\n" : "",
184 newline_needed
= false;
188 if (ctl_ret_code
!= LTTNG_OK
) {
189 LTTNG_THROW_CTL(lttng::format("Failed to destroy session `{}`",
194 enum lttng_rotation_state rotation_state
;
195 status
= lttng_destruction_handle_get_rotation_state(destruction_handle
.get(),
197 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
198 ERR_FMT("{}Failed to query the rotation state from the destruction handle of session `{}`",
199 newline_needed
? "\n" : "",
201 newline_needed
= false;
203 switch (rotation_state
) {
204 case LTTNG_ROTATION_STATE_NO_ROTATION
:
206 case LTTNG_ROTATION_STATE_COMPLETED
:
208 const struct lttng_trace_archive_location
*location
;
210 status
= lttng_destruction_handle_get_archive_location(
211 destruction_handle
.get(), &location
);
212 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
213 ret
= print_trace_archive_location(location
, session
.name
);
215 ERR_FMT("{}Failed to print the location of the latest trace archive of session `{}`",
216 newline_needed
? "\n" : "",
218 newline_needed
= false;
226 ERR_FMT("{}Failed to get the location of the rotation performed during the destruction of `{}`",
227 newline_needed
? "\n" : "",
229 newline_needed
= false;
235 MSG("%sSession `%s` destroyed", newline_needed
? "\n" : "", session
.name
);
236 newline_needed
= false;
238 MSG("%s", stats_str
.get());
242 * If the session being destroy is the "default" session as defined in the .lttngrc file,
245 const auto session_name
=
246 lttng::make_unique_wrapper
<char, lttng::memory::free
>(get_session_name_quiet());
247 if (session_name
&& !strncmp(session
.name
, session_name
.get(), NAME_MAX
)) {
248 config_destroy_default();
252 ret
= mi_lttng_session(writer
, &session
, 0);
261 cmd_error_code
destroy_sessions(const lttng::cli::session_spec
& spec
)
263 bool had_warning
= false;
264 bool had_error
= false;
265 bool listing_failed
= false;
267 const auto sessions
= [&listing_failed
, &spec
]() -> lttng::cli::session_list
{
269 return list_sessions(spec
);
270 } catch (const lttng::ctl::error
& ctl_exception
) {
271 ERR_FMT("Failed to list sessions ({})",
272 lttng_strerror(-ctl_exception
.code()));
273 listing_failed
= true;
278 if (!listing_failed
&& sessions
.size() == 0 &&
279 spec
.type_
== lttng::cli::session_spec::type::NAME
) {
280 ERR_FMT("Session `{}` not found", spec
.value
);
284 if (listing_failed
) {
288 for (const auto& session
: sessions
) {
289 cmd_error_code sub_ret
;
292 sub_ret
= destroy_session(session
);
293 } catch (const lttng::ctl::error
& ctl_exception
) {
294 switch (ctl_exception
.code()) {
295 case LTTNG_ERR_NO_SESSION
:
296 if (spec
.type_
!= lttng::cli::session_spec::type::NAME
) {
297 /* Session destroyed during command, ignore and carry-on. */
298 sub_ret
= CMD_SUCCESS
;
304 case LTTNG_ERR_NO_SESSIOND
:
305 /* Don't keep going on a fatal error. */
310 ERR_FMT("Failed to destroy session `{}` ({})",
312 lttng_strerror(-ctl_exception
.code()));
317 /* Keep going, but report the most serious state. */
318 had_warning
|= sub_ret
== CMD_WARNING
;
319 had_error
|= sub_ret
== CMD_ERROR
;
324 } else if (had_warning
) {
333 * The 'destroy <options>' first level command
335 int cmd_destroy(int argc
, const char **argv
)
338 cmd_error_code command_ret
= CMD_SUCCESS
;
340 static poptContext pc
;
341 const char *leftover
= nullptr;
342 lttng::cli::session_spec
spec(lttng::cli::session_spec::type::NAME
);
343 lttng::cli::session_list
const sessions
;
345 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
346 poptReadDefaultConfig(pc
, 0);
348 while ((opt
= poptGetNextOpt(pc
)) != -1) {
355 command_ret
= static_cast<cmd_error_code
>(ret
);
358 case OPT_LIST_OPTIONS
:
359 list_cmd_options(stdout
, long_options
);
362 spec
.type_
= lttng::cli::session_spec::type::ALL
;
364 case OPT_ENABLE_GLOB
:
365 spec
.type_
= lttng::cli::session_spec::type::GLOB_PATTERN
;
368 command_ret
= CMD_UNDEFINED
;
375 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
377 command_ret
= CMD_ERROR
;
381 /* Open command element */
382 if (mi_lttng_writer_command_open(writer
, mi_lttng_element_command_destroy
)) {
383 command_ret
= CMD_ERROR
;
387 /* Open output element */
388 if (mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
)) {
389 command_ret
= CMD_ERROR
;
393 /* For validation and semantic purpose we open a sessions element */
394 if (mi_lttng_sessions_open(writer
)) {
395 command_ret
= CMD_ERROR
;
400 spec
.value
= poptGetArg(pc
);
402 command_ret
= destroy_sessions(spec
);
404 success
= command_ret
== CMD_SUCCESS
;
406 leftover
= poptGetArg(pc
);
408 ERR("Unknown argument: %s", leftover
);
409 command_ret
= CMD_ERROR
;
415 /* Close sessions and output element element */
416 if (mi_lttng_close_multi_element(writer
, 2)) {
417 command_ret
= CMD_ERROR
;
422 if (mi_lttng_writer_write_element_bool(
423 writer
, mi_lttng_element_command_success
, success
)) {
424 command_ret
= CMD_ERROR
;
428 /* Command element close */
429 if (mi_lttng_writer_command_close(writer
)) {
430 command_ret
= CMD_ERROR
;
436 if (writer
&& mi_lttng_writer_destroy(writer
)) {
437 command_ret
= CMD_ERROR
;