2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
10 #include "../exception.hpp"
12 #include <common/exception.hpp>
13 #include <common/make-unique-wrapper.hpp>
14 #include <common/mi-lttng.hpp>
15 #include <common/scope-exit.hpp>
16 #include <common/sessiond-comm/sessiond-comm.hpp>
17 #include <common/utils.hpp>
19 #include <lttng/lttng.h>
27 #include <sys/types.h>
38 #ifdef LTTNG_EMBED_HELP
39 const char help_msg
[] =
40 #include <lttng-destroy.1.h>
47 struct mi_writer
*writer
;
49 struct poptOption long_options
[] = {
50 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
51 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
52 { "all", 'a', POPT_ARG_NONE
, nullptr, OPT_ALL
, nullptr, nullptr },
53 { "glob", 'g', POPT_ARG_NONE
, nullptr, OPT_ENABLE_GLOB
, nullptr, nullptr },
54 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
55 { "no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, nullptr, nullptr },
56 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
62 * Unregister the provided session to the session daemon. On success, removes
63 * the default configuration.
65 cmd_error_code
destroy_session(const lttng_session
& session
)
68 bool newline_needed
= false, printed_destroy_msg
= false;
70 const auto print_trailing_new_line
= lttng::make_scope_exit([&newline_needed
]() noexcept
{
76 ret
= lttng_stop_tracing_no_wait(session
.name
);
77 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
78 LTTNG_THROW_CTL(lttng::format("Failed to stop session `{}`", session
.name
),
79 static_cast<lttng_error_code
>(-ret
));
82 const auto session_was_already_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
85 ret
= lttng_data_pending(session
.name
);
87 /* Return the data available call error. */
88 ERR_FMT("Failed to check pending data for session `{}` ({})",
95 * Data sleep time before retrying (in usec). Don't
96 * sleep if the call returned value indicates
100 if (!printed_destroy_msg
) {
101 _MSG("Destroying session `%s`", session
.name
);
102 newline_needed
= true;
103 printed_destroy_msg
= true;
107 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
114 std::unique_ptr
<char,
115 lttng::memory::create_deleter_class
<char, lttng::memory::free
>::deleter
>
117 if (!session_was_already_stopped
) {
118 char *raw_stats_str
= nullptr;
121 * Don't print the event and packet loss warnings since the user
122 * already saw them when stopping the trace.
124 ret
= get_session_stats_str(session
.name
, &raw_stats_str
);
129 /* May still be null if there are no stats to print. */
130 stats_str
.reset(raw_stats_str
);
133 const auto destruction_handle
= [&session
]() {
134 struct lttng_destruction_handle
*raw_destruction_handle
= nullptr;
137 lttng_destroy_session_ext(session
.name
, &raw_destruction_handle
);
138 if (ctl_ret_code
!= LTTNG_OK
) {
139 LTTNG_THROW_CTL(lttng::format("Failed to destroy session `{}`",
144 return lttng::make_unique_wrapper
<lttng_destruction_handle
,
145 lttng_destruction_handle_destroy
>(
146 raw_destruction_handle
);
150 enum lttng_destruction_handle_status status
;
153 status
= lttng_destruction_handle_wait_for_completion(
154 destruction_handle
.get(),
155 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
157 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
158 if (!printed_destroy_msg
) {
159 _MSG("Destroying session `%s`", session
.name
);
160 newline_needed
= true;
161 printed_destroy_msg
= true;
166 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
169 ERR_FMT("{}An error occurred during the destruction of session `{}`",
170 newline_needed
? "\n" : "",
172 newline_needed
= false;
175 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
177 enum lttng_error_code ctl_ret_code
;
178 status
= lttng_destruction_handle_get_result(destruction_handle
.get(),
180 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
181 ERR_FMT("{}Failed to query the result of the destruction of session `{}`",
182 newline_needed
? "\n" : "",
185 newline_needed
= false;
189 if (ctl_ret_code
!= LTTNG_OK
) {
190 LTTNG_THROW_CTL(lttng::format("Failed to destroy session `{}`",
195 enum lttng_rotation_state rotation_state
;
196 status
= lttng_destruction_handle_get_rotation_state(destruction_handle
.get(),
198 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
199 ERR_FMT("{}Failed to query the rotation state from the destruction handle of session `{}`",
200 newline_needed
? "\n" : "",
202 newline_needed
= false;
204 switch (rotation_state
) {
205 case LTTNG_ROTATION_STATE_NO_ROTATION
:
207 case LTTNG_ROTATION_STATE_COMPLETED
:
209 const struct lttng_trace_archive_location
*location
;
211 status
= lttng_destruction_handle_get_archive_location(
212 destruction_handle
.get(), &location
);
213 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
214 ret
= print_trace_archive_location(location
, session
.name
);
216 ERR_FMT("{}Failed to print the location of the latest trace archive of session `{}`",
217 newline_needed
? "\n" : "",
219 newline_needed
= false;
227 ERR_FMT("{}Failed to get the location of the rotation performed during the destruction of `{}`",
228 newline_needed
? "\n" : "",
230 newline_needed
= false;
236 MSG("%sSession `%s` destroyed", newline_needed
? "\n" : "", session
.name
);
237 newline_needed
= false;
239 MSG("%s", stats_str
.get());
243 * If the session being destroy is the "default" session as defined in the .lttngrc file,
246 const auto session_name
=
247 lttng::make_unique_wrapper
<char, lttng::memory::free
>(get_session_name_quiet());
248 if (session_name
&& !strncmp(session
.name
, session_name
.get(), NAME_MAX
)) {
249 config_destroy_default();
253 ret
= mi_lttng_session(writer
, &session
, 0);
262 cmd_error_code
destroy_sessions(const lttng::cli::session_spec
& spec
)
264 bool had_warning
= false;
265 bool had_error
= false;
266 bool listing_failed
= false;
268 const auto sessions
= [&listing_failed
, &spec
]() -> lttng::cli::session_list
{
270 return list_sessions(spec
);
271 } catch (const lttng::ctl::error
& ctl_exception
) {
272 ERR_FMT("Failed to list sessions ({})",
273 lttng_strerror(-ctl_exception
.code()));
274 listing_failed
= true;
276 } catch (const lttng::cli::no_default_session_error
& cli_exception
) {
278 * The retrieval of the default session name already logs
279 * an error when it fails. There is no value in printing
280 * anything about this exception.
282 listing_failed
= true;
287 if (!listing_failed
&& sessions
.size() == 0 &&
288 spec
.type_
== lttng::cli::session_spec::type::NAME
) {
289 ERR_FMT("Session `{}` not found", spec
.value
);
293 if (listing_failed
) {
297 for (const auto& session
: sessions
) {
298 cmd_error_code sub_ret
;
301 sub_ret
= destroy_session(session
);
302 } catch (const lttng::ctl::error
& ctl_exception
) {
303 switch (ctl_exception
.code()) {
304 case LTTNG_ERR_NO_SESSION
:
305 if (spec
.type_
!= lttng::cli::session_spec::type::NAME
) {
306 /* Session destroyed during command, ignore and carry-on. */
307 sub_ret
= CMD_SUCCESS
;
313 case LTTNG_ERR_NO_SESSIOND
:
314 /* Don't keep going on a fatal error. */
319 ERR_FMT("Failed to destroy session `{}` ({})",
321 lttng_strerror(-ctl_exception
.code()));
326 /* Keep going, but report the most serious state. */
327 had_warning
|= sub_ret
== CMD_WARNING
;
328 had_error
|= sub_ret
== CMD_ERROR
;
333 } else if (had_warning
) {
342 * The 'destroy <options>' first level command
344 int cmd_destroy(int argc
, const char **argv
)
347 cmd_error_code command_ret
= CMD_SUCCESS
;
349 static poptContext pc
;
350 const char *leftover
= nullptr;
351 lttng::cli::session_spec
spec(lttng::cli::session_spec::type::NAME
);
352 lttng::cli::session_list
const sessions
;
354 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
355 poptReadDefaultConfig(pc
, 0);
357 while ((opt
= poptGetNextOpt(pc
)) != -1) {
364 command_ret
= static_cast<cmd_error_code
>(ret
);
367 case OPT_LIST_OPTIONS
:
368 list_cmd_options(stdout
, long_options
);
371 spec
.type_
= lttng::cli::session_spec::type::ALL
;
373 case OPT_ENABLE_GLOB
:
374 spec
.type_
= lttng::cli::session_spec::type::GLOB_PATTERN
;
377 command_ret
= CMD_UNDEFINED
;
384 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
386 command_ret
= CMD_ERROR
;
390 /* Open command element */
391 if (mi_lttng_writer_command_open(writer
, mi_lttng_element_command_destroy
)) {
392 command_ret
= CMD_ERROR
;
396 /* Open output element */
397 if (mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
)) {
398 command_ret
= CMD_ERROR
;
402 /* For validation and semantic purpose we open a sessions element */
403 if (mi_lttng_sessions_open(writer
)) {
404 command_ret
= CMD_ERROR
;
409 spec
.value
= poptGetArg(pc
);
411 command_ret
= destroy_sessions(spec
);
413 success
= command_ret
== CMD_SUCCESS
;
415 leftover
= poptGetArg(pc
);
417 ERR("Unknown argument: %s", leftover
);
418 command_ret
= CMD_ERROR
;
424 /* Close sessions and output element element */
425 if (mi_lttng_close_multi_element(writer
, 2)) {
426 command_ret
= CMD_ERROR
;
431 if (mi_lttng_writer_write_element_bool(
432 writer
, mi_lttng_element_command_success
, success
)) {
433 command_ret
= CMD_ERROR
;
437 /* Command element close */
438 if (mi_lttng_writer_command_close(writer
)) {
439 command_ret
= CMD_ERROR
;
445 if (writer
&& mi_lttng_writer_destroy(writer
)) {
446 command_ret
= CMD_ERROR
;