lttng: move session_list to the lttng::cli namespace
[lttng-tools.git] / src / bin / lttng / commands / destroy.cpp
CommitLineData
f3ed775e 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
f3ed775e 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
f3ed775e 5 *
f3ed775e
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
28ab034a
JG
9#include "../command.hpp"
10
aabf6773 11#include <common/exception.hpp>
b50fbe86 12#include <common/make-unique-wrapper.hpp>
28ab034a 13#include <common/mi-lttng.hpp>
b50fbe86 14#include <common/scope-exit.hpp>
28ab034a
JG
15#include <common/sessiond-comm/sessiond-comm.hpp>
16#include <common/utils.hpp>
17
18#include <lttng/lttng.h>
19
f3ed775e 20#include <popt.h>
28ab034a 21#include <stdbool.h>
f3ed775e
DG
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
42224349 28
544349a3
JG
29enum {
30 OPT_HELP = 1,
31 OPT_LIST_OPTIONS,
32 OPT_ALL,
33 OPT_ENABLE_GLOB,
34};
f3ed775e 35
544349a3 36namespace {
4fc83d94 37#ifdef LTTNG_EMBED_HELP
544349a3 38const char help_msg[] =
4fc83d94 39#include <lttng-destroy.1.h>
28ab034a 40 ;
4fc83d94
PP
41#endif
42
544349a3 43int opt_no_wait;
65f25c66 44
544349a3
JG
45/* Mi writer */
46struct mi_writer *writer;
f3ed775e 47
544349a3 48struct poptOption long_options[] = {
f3ed775e 49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
cd9adb8b 50 { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
aabf6773
OD
51 { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr },
52 { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr },
cd9adb8b
JG
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 }
f3ed775e
DG
56};
57
f3ed775e 58/*
b09ee5ba
FG
59 * destroy_session
60 *
61 * Unregister the provided session to the session daemon. On success, removes
62 * the default configuration.
f3ed775e 63 */
b50fbe86 64cmd_error_code destroy_session(const lttng_session& session)
f3ed775e
DG
65{
66 int ret;
58f237ca 67 bool newline_needed = false, printed_destroy_msg = false;
b50fbe86
JG
68
69 const auto print_trailing_new_line = lttng::make_scope_exit([&newline_needed]() noexcept {
70 if (newline_needed) {
71 MSG("");
72 }
73 });
f3ed775e 74
aabf6773 75 ret = lttng_stop_tracing_no_wait(session.name);
e20ee7c2 76 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
b50fbe86
JG
77 LTTNG_THROW_CTL(fmt::format("Failed to stop session `{}`", session.name),
78 static_cast<lttng_error_code>(-ret));
e20ee7c2 79 }
58f237ca 80
b50fbe86 81 const auto session_was_already_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
e20ee7c2 82 if (!opt_no_wait) {
e20ee7c2 83 do {
aabf6773 84 ret = lttng_data_pending(session.name);
e20ee7c2
JD
85 if (ret < 0) {
86 /* Return the data available call error. */
b50fbe86
JG
87 ERR_FMT("Failed to check pending data for session `{}` ({})",
88 session.name,
89 lttng_strerror(ret));
90 return CMD_ERROR;
e20ee7c2
JD
91 }
92
93 /*
58f237ca
JG
94 * Data sleep time before retrying (in usec). Don't
95 * sleep if the call returned value indicates
96 * availability.
e20ee7c2
JD
97 */
98 if (ret) {
dec2c8e1 99 if (!printed_destroy_msg) {
b50fbe86 100 _MSG("Destroying session `%s`", session.name);
dec2c8e1
JG
101 newline_needed = true;
102 printed_destroy_msg = true;
103 fflush(stdout);
104 }
01f55be4 105
c8f61fd4 106 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
e20ee7c2
JD
107 _MSG(".");
108 fflush(stdout);
109 }
110 } while (ret != 0);
e20ee7c2 111 }
58f237ca 112
f053d40c 113 std::unique_ptr<char, lttng::memory::create_deleter_class<char, lttng::free>::deleter>
b50fbe86 114 stats_str;
58f237ca 115 if (!session_was_already_stopped) {
b50fbe86
JG
116 char *raw_stats_str = nullptr;
117
20fb9e02
JD
118 /*
119 * Don't print the event and packet loss warnings since the user
120 * already saw them when stopping the trace.
121 */
b50fbe86 122 ret = get_session_stats_str(session.name, &raw_stats_str);
58f237ca 123 if (ret < 0) {
b50fbe86 124 return CMD_ERROR;
58f237ca 125 }
e20ee7c2 126
b50fbe86
JG
127 /* May still be null if there are no stats to print. */
128 stats_str.reset(raw_stats_str);
f3ed775e
DG
129 }
130
b50fbe86
JG
131 const auto destruction_handle = [&session]() {
132 struct lttng_destruction_handle *raw_destruction_handle = nullptr;
133
134 auto ctl_ret_code =
135 lttng_destroy_session_ext(session.name, &raw_destruction_handle);
136 if (ctl_ret_code != LTTNG_OK) {
137 LTTNG_THROW_CTL(fmt::format("Failed to destroy session `{}`", session.name),
138 ctl_ret_code);
139 }
bbbfd849 140
b50fbe86
JG
141 return lttng::make_unique_wrapper<lttng_destruction_handle,
142 lttng_destruction_handle_destroy>(
143 raw_destruction_handle);
144 }();
145
146 if (!opt_no_wait) {
147 enum lttng_destruction_handle_status status;
148
149 do {
150 status = lttng_destruction_handle_wait_for_completion(
151 destruction_handle.get(),
152 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC);
153 switch (status) {
154 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT:
155 if (!printed_destroy_msg) {
156 _MSG("Destroying session `%s`", session.name);
157 newline_needed = true;
158 printed_destroy_msg = true;
159 }
160 _MSG(".");
161 fflush(stdout);
162 break;
163 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED:
164 break;
165 default:
166 ERR_FMT("{}An error occurred during the destruction of session `{}`",
167 newline_needed ? "\n" : "",
168 session.name);
169 newline_needed = false;
170 return CMD_ERROR;
bbbfd849 171 }
b50fbe86
JG
172 } while (status == LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT);
173
174 enum lttng_error_code ctl_ret_code;
175 status = lttng_destruction_handle_get_result(destruction_handle.get(),
176 &ctl_ret_code);
177 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
178 ERR_FMT("{}Failed to query the result of the destruction of session `{}`",
179 newline_needed ? "\n" : "",
180 session.name);
181
58f237ca 182 newline_needed = false;
b50fbe86 183 return CMD_ERROR;
bbbfd849 184 }
bbbfd849 185
b50fbe86
JG
186 if (ctl_ret_code != LTTNG_OK) {
187 LTTNG_THROW_CTL(fmt::format("Failed to destroy session `{}`", session.name),
188 ctl_ret_code);
189 }
bbbfd849 190
b50fbe86
JG
191 enum lttng_rotation_state rotation_state;
192 status = lttng_destruction_handle_get_rotation_state(destruction_handle.get(),
193 &rotation_state);
194 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
195 ERR_FMT("{}Failed to query the rotation state from the destruction handle of session `{}`",
196 newline_needed ? "\n" : "",
197 session.name);
198 newline_needed = false;
199 } else {
200 switch (rotation_state) {
201 case LTTNG_ROTATION_STATE_NO_ROTATION:
202 break;
203 case LTTNG_ROTATION_STATE_COMPLETED:
204 {
205 const struct lttng_trace_archive_location *location;
206
207 status = lttng_destruction_handle_get_archive_location(
208 destruction_handle.get(), &location);
209 if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
210 ret = print_trace_archive_location(location, session.name);
211 if (ret) {
212 ERR_FMT("{}Failed to print the location of the latest trace archive of session `{}`",
213 newline_needed ? "\n" : "",
214 session.name);
215 newline_needed = false;
216 }
217
218 break;
219 }
220 }
221 /* fall-through. */
222 default:
223 ERR_FMT("{}Failed to get the location of the rotation performed during the destruction of `{}`",
224 newline_needed ? "\n" : "",
225 session.name);
58f237ca 226 newline_needed = false;
b50fbe86 227 break;
bbbfd849 228 }
bbbfd849 229 }
58f237ca 230 }
b50fbe86
JG
231
232 MSG("%sSession `%s` destroyed", newline_needed ? "\n" : "", session.name);
58f237ca
JG
233 newline_needed = false;
234 if (stats_str) {
b50fbe86 235 MSG("%s", stats_str.get());
58f237ca 236 }
1dac0189 237
b50fbe86
JG
238 /*
239 * If the session being destroy is the "default" session as defined in the .lttngrc file,
240 * destroy the file.
241 */
242 const auto session_name =
243 lttng::make_unique_wrapper<char, lttng::free>(get_session_name_quiet());
244 if (session_name && !strncmp(session.name, session_name.get(), NAME_MAX)) {
1dac0189
PPM
245 config_destroy_default();
246 }
65f25c66
JRJ
247
248 if (lttng_opt_mi) {
aabf6773 249 ret = mi_lttng_session(writer, &session, 0);
65f25c66 250 if (ret) {
b50fbe86 251 return CMD_ERROR;
65f25c66
JRJ
252 }
253 }
254
b50fbe86 255 return CMD_SUCCESS;
b09ee5ba 256}
f3ed775e 257
6e11909e 258cmd_error_code destroy_sessions(const lttng::cli::session_spec& spec)
b09ee5ba 259{
b50fbe86
JG
260 bool had_warning = false;
261 bool had_error = false;
544349a3
JG
262 bool listing_failed = false;
263
6e11909e 264 const auto sessions = [&listing_failed, &spec]() -> lttng::cli::session_list {
544349a3
JG
265 try {
266 return list_sessions(spec);
267 } catch (const lttng::ctl::error& ctl_exception) {
268 ERR_FMT("Failed to list sessions ({})",
269 lttng_strerror(-ctl_exception.code()));
270 listing_failed = true;
271 return {};
aabf6773 272 }
544349a3
JG
273 }();
274
6e11909e
JG
275 if (!listing_failed && sessions.size() == 0 &&
276 spec.type_ == lttng::cli::session_spec::type::NAME) {
b50fbe86
JG
277 ERR_FMT("Session `{}` not found", spec.value);
278 return CMD_ERROR;
279 }
280
281 if (listing_failed) {
282 return CMD_FATAL;
544349a3 283 }
aabf6773 284
544349a3 285 for (const auto& session : sessions) {
b50fbe86 286 cmd_error_code sub_ret;
aabf6773 287
b50fbe86
JG
288 try {
289 sub_ret = destroy_session(session);
290 } catch (const lttng::ctl::error& ctl_exception) {
291 switch (ctl_exception.code()) {
292 case LTTNG_ERR_NO_SESSION:
6e11909e 293 if (spec.type_ != lttng::cli::session_spec::type::NAME) {
b50fbe86
JG
294 /* Session destroyed during command, ignore and carry-on. */
295 sub_ret = CMD_SUCCESS;
296 break;
297 } else {
298 sub_ret = CMD_ERROR;
299 break;
300 }
301 case LTTNG_ERR_NO_SESSIOND:
302 /* Don't keep going on a fatal error. */
303 return CMD_FATAL;
304 default:
305 /* Generic error. */
306 sub_ret = CMD_ERROR;
307 ERR_FMT("Failed to destroy session `{}` ({})",
308 session.name,
309 lttng_strerror(-ctl_exception.code()));
310 break;
311 }
544349a3 312 }
b50fbe86
JG
313
314 /* Keep going, but report the most serious state. */
315 had_warning |= sub_ret == CMD_WARNING;
316 had_error |= sub_ret == CMD_ERROR;
b73d0b29 317 }
3285a971 318
b50fbe86
JG
319 if (had_error) {
320 return CMD_ERROR;
321 } else if (had_warning) {
322 return CMD_WARNING;
323 } else {
324 return CMD_SUCCESS;
325 }
f3ed775e 326}
544349a3 327} /* namespace */
f3ed775e
DG
328
329/*
843f5df9 330 * The 'destroy <options>' first level command
f3ed775e
DG
331 */
332int cmd_destroy(int argc, const char **argv)
333{
b09ee5ba 334 int opt;
544349a3 335 cmd_error_code command_ret = CMD_SUCCESS;
aabf6773 336 bool success;
f3ed775e 337 static poptContext pc;
cd9adb8b 338 const char *leftover = nullptr;
6e11909e
JG
339 lttng::cli::session_spec spec(lttng::cli::session_spec::type::NAME);
340 lttng::cli::session_list const sessions;
65f25c66 341
cd9adb8b 342 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
f3ed775e
DG
343 poptReadDefaultConfig(pc, 0);
344
345 while ((opt = poptGetNextOpt(pc)) != -1) {
346 switch (opt) {
347 case OPT_HELP:
544349a3
JG
348 {
349 int ret;
350
4ba92f18 351 SHOW_HELP();
544349a3 352 command_ret = static_cast<cmd_error_code>(ret);
aabf6773 353 goto end;
544349a3 354 }
679b4943
SM
355 case OPT_LIST_OPTIONS:
356 list_cmd_options(stdout, long_options);
aabf6773
OD
357 goto end;
358 case OPT_ALL:
6e11909e 359 spec.type_ = lttng::cli::session_spec::type::ALL;
aabf6773
OD
360 break;
361 case OPT_ENABLE_GLOB:
6e11909e 362 spec.type_ = lttng::cli::session_spec::type::GLOB_PATTERN;
b09ee5ba 363 break;
f3ed775e 364 default:
544349a3 365 command_ret = CMD_UNDEFINED;
aabf6773 366 goto end;
f3ed775e
DG
367 }
368 }
369
65f25c66 370 /* Mi preparation */
c7e35b03 371 if (lttng_opt_mi) {
65f25c66
JRJ
372 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
373 if (!writer) {
544349a3 374 command_ret = CMD_ERROR;
65f25c66
JRJ
375 goto end;
376 }
377
378 /* Open command element */
544349a3
JG
379 if (mi_lttng_writer_command_open(writer, mi_lttng_element_command_destroy)) {
380 command_ret = CMD_ERROR;
65f25c66
JRJ
381 goto end;
382 }
383
384 /* Open output element */
544349a3
JG
385 if (mi_lttng_writer_open_element(writer, mi_lttng_element_command_output)) {
386 command_ret = CMD_ERROR;
65f25c66
JRJ
387 goto end;
388 }
389
390 /* For validation and semantic purpose we open a sessions element */
544349a3
JG
391 if (mi_lttng_sessions_open(writer)) {
392 command_ret = CMD_ERROR;
65f25c66
JRJ
393 goto end;
394 }
395 }
396
aabf6773 397 spec.value = poptGetArg(pc);
fd076c09 398
aabf6773 399 command_ret = destroy_sessions(spec);
65f25c66 400
aabf6773 401 success = command_ret == CMD_SUCCESS;
65f25c66 402
68c7f6e5
JD
403 leftover = poptGetArg(pc);
404 if (leftover) {
405 ERR("Unknown argument: %s", leftover);
544349a3 406 command_ret = CMD_ERROR;
aabf6773 407 success = false;
68c7f6e5
JD
408 }
409
65f25c66
JRJ
410 /* Mi closing */
411 if (lttng_opt_mi) {
412 /* Close sessions and output element element */
544349a3 413 if (mi_lttng_close_multi_element(writer, 2)) {
b50fbe86 414 command_ret = CMD_ERROR;
b09ee5ba
FG
415 goto end;
416 }
fd076c09 417
65f25c66 418 /* Success ? */
544349a3 419 if (mi_lttng_writer_write_element_bool(
b50fbe86
JG
420 writer, mi_lttng_element_command_success, success)) {
421 command_ret = CMD_ERROR;
65f25c66
JRJ
422 goto end;
423 }
f3ed775e 424
65f25c66 425 /* Command element close */
544349a3 426 if (mi_lttng_writer_command_close(writer)) {
b50fbe86 427 command_ret = CMD_ERROR;
65f25c66
JRJ
428 goto end;
429 }
430 }
f3ed775e 431end:
65f25c66
JRJ
432 /* Mi clean-up */
433 if (writer && mi_lttng_writer_destroy(writer)) {
544349a3 434 command_ret = CMD_ERROR;
65f25c66
JRJ
435 }
436
fd076c09 437 poptFreeContext(pc);
544349a3 438 return command_ret;
f3ed775e 439}
This page took 0.094336 seconds and 4 git commands to generate.