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/mi-lttng.hpp>
13 #include <common/sessiond-comm/sessiond-comm.hpp>
14 #include <common/utils.hpp>
16 #include <lttng/lttng.h>
24 #include <sys/types.h>
35 #ifdef LTTNG_EMBED_HELP
36 const char help_msg
[] =
37 #include <lttng-destroy.1.h>
44 struct mi_writer
*writer
;
46 struct poptOption long_options
[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
49 { "all", 'a', POPT_ARG_NONE
, nullptr, OPT_ALL
, nullptr, nullptr },
50 { "glob", 'g', POPT_ARG_NONE
, nullptr, OPT_ENABLE_GLOB
, nullptr, nullptr },
51 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
52 { "no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, nullptr, nullptr },
53 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
59 * Unregister the provided session to the session daemon. On success, removes
60 * the default configuration.
62 int destroy_session(const struct lttng_session
& session
)
65 char *session_name
= nullptr;
66 bool session_was_already_stopped
;
67 enum lttng_error_code ret_code
;
68 struct lttng_destruction_handle
*handle
= nullptr;
69 enum lttng_destruction_handle_status status
;
70 bool newline_needed
= false, printed_destroy_msg
= false;
71 enum lttng_rotation_state rotation_state
;
72 char *stats_str
= nullptr;
74 ret
= lttng_stop_tracing_no_wait(session
.name
);
75 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
76 ERR("%s", lttng_strerror(ret
));
79 session_was_already_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
82 ret
= lttng_data_pending(session
.name
);
84 /* Return the data available call error. */
89 * Data sleep time before retrying (in usec). Don't
90 * sleep if the call returned value indicates
94 if (!printed_destroy_msg
) {
95 _MSG("Destroying session %s", session
.name
);
96 newline_needed
= true;
97 printed_destroy_msg
= true;
101 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
108 if (!session_was_already_stopped
) {
110 * Don't print the event and packet loss warnings since the user
111 * already saw them when stopping the trace.
113 ret
= get_session_stats_str(session
.name
, &stats_str
);
119 ret_code
= lttng_destroy_session_ext(session
.name
, &handle
);
120 if (ret_code
!= LTTNG_OK
) {
126 goto skip_wait_rotation
;
130 status
= lttng_destruction_handle_wait_for_completion(
131 handle
, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
133 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
134 if (!printed_destroy_msg
) {
135 _MSG("Destroying session %s", session
.name
);
136 newline_needed
= true;
137 printed_destroy_msg
= true;
142 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
145 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
146 newline_needed
? "\n" : "",
148 newline_needed
= false;
152 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
154 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
155 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
156 ERR("%sFailed to get the result of session destruction",
157 newline_needed
? "\n" : "");
159 newline_needed
= false;
162 if (ret_code
!= LTTNG_OK
) {
167 status
= lttng_destruction_handle_get_rotation_state(handle
, &rotation_state
);
168 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
169 ERR("%sFailed to get rotation state from destruction handle",
170 newline_needed
? "\n" : "");
171 newline_needed
= false;
172 goto skip_wait_rotation
;
175 switch (rotation_state
) {
176 case LTTNG_ROTATION_STATE_NO_ROTATION
:
178 case LTTNG_ROTATION_STATE_COMPLETED
:
180 const struct lttng_trace_archive_location
*location
;
182 status
= lttng_destruction_handle_get_archive_location(handle
, &location
);
183 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
184 ret
= print_trace_archive_location(location
, session
.name
);
186 ERR("%sFailed to print the location of trace archive",
187 newline_needed
? "\n" : "");
188 newline_needed
= false;
189 goto skip_wait_rotation
;
196 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
197 newline_needed
? "\n" : "");
198 newline_needed
= false;
199 goto skip_wait_rotation
;
202 MSG("%sSession %s destroyed", newline_needed
? "\n" : "", session
.name
);
203 newline_needed
= false;
205 MSG("%s", stats_str
);
208 session_name
= get_session_name_quiet();
209 if (session_name
&& !strncmp(session
.name
, session_name
, NAME_MAX
)) {
210 config_destroy_default();
214 ret
= mi_lttng_session(writer
, &session
, 0);
223 if (newline_needed
) {
226 lttng_destruction_handle_destroy(handle
);
232 cmd_error_code
destroy_sessions(const struct session_spec
& spec
)
234 //bool had_warning = false;
235 //bool had_error = false;
236 bool listing_failed
= false;
238 const auto sessions
= [&listing_failed
, &spec
]() -> session_list
{
240 return list_sessions(spec
);
241 } catch (const lttng::ctl::error
& ctl_exception
) {
242 ERR_FMT("Failed to list sessions ({})",
243 lttng_strerror(-ctl_exception
.code()));
244 listing_failed
= true;
249 if (sessions
.size() == 0) {
251 case session_spec::ALL
:
253 case session_spec::GLOB_PATTERN
:
254 MSG("No session found, nothing to do.");
256 case session_spec::NAME
:
257 ERR("Session name %s not found", spec
.value
);
262 for (const auto& session
: sessions
) {
263 int const sub_ret
= destroy_session(session
);
265 if (sub_ret
!= CMD_SUCCESS
) {
266 ERR("%s during the destruction of session \"%s\"",
267 lttng_strerror(sub_ret
),
278 * The 'destroy <options>' first level command
280 int cmd_destroy(int argc
, const char **argv
)
283 cmd_error_code command_ret
= CMD_SUCCESS
;
285 static poptContext pc
;
286 const char *leftover
= nullptr;
287 struct session_spec spec
= {
288 .type
= session_spec::NAME
,
291 session_list
const sessions
;
293 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
294 poptReadDefaultConfig(pc
, 0);
296 while ((opt
= poptGetNextOpt(pc
)) != -1) {
303 command_ret
= static_cast<cmd_error_code
>(ret
);
306 case OPT_LIST_OPTIONS
:
307 list_cmd_options(stdout
, long_options
);
310 spec
.type
= session_spec::ALL
;
312 case OPT_ENABLE_GLOB
:
313 spec
.type
= session_spec::GLOB_PATTERN
;
316 command_ret
= CMD_UNDEFINED
;
323 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
325 command_ret
= CMD_ERROR
;
329 /* Open command element */
330 if (mi_lttng_writer_command_open(writer
, mi_lttng_element_command_destroy
)) {
331 command_ret
= CMD_ERROR
;
335 /* Open output element */
336 if (mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
)) {
337 command_ret
= CMD_ERROR
;
341 /* For validation and semantic purpose we open a sessions element */
342 if (mi_lttng_sessions_open(writer
)) {
343 command_ret
= CMD_ERROR
;
348 spec
.value
= poptGetArg(pc
);
350 command_ret
= destroy_sessions(spec
);
352 success
= command_ret
== CMD_SUCCESS
;
354 leftover
= poptGetArg(pc
);
356 ERR("Unknown argument: %s", leftover
);
357 command_ret
= CMD_ERROR
;
363 /* Close sessions and output element element */
364 if (mi_lttng_close_multi_element(writer
, 2)) {
365 command_ret
= CMD_ERROR
;
370 if (mi_lttng_writer_write_element_bool(
371 writer
, mi_lttng_element_command_success
, success
)) {
372 command_ret
= CMD_ERROR
;
376 /* Command element close */
377 if (mi_lttng_writer_command_close(writer
)) {
378 command_ret
= CMD_ERROR
;
384 if (writer
&& mi_lttng_writer_destroy(writer
)) {
385 command_ret
= CMD_ERROR
;