lttng: enable-channel: move kernel tracer status check to util
[lttng-tools.git] / src / bin / lttng / commands / stop.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 9#include "../command.hpp"
2a8d72ed 10#include "../utils.hpp"
28ab034a 11
2a8d72ed 12#include <common/exception.hpp>
28ab034a
JG
13#include <common/mi-lttng.hpp>
14#include <common/sessiond-comm/sessiond-comm.hpp>
15
f3ed775e
DG
16#include <popt.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
f3ed775e 23
5330763e
JG
24enum {
25 OPT_HELP = 1,
26 OPT_LIST_OPTIONS,
27 OPT_ENABLE_GLOB,
28 OPT_ALL,
29};
f3ed775e 30
5330763e 31namespace {
75723cd7
JG
32int opt_no_wait;
33struct mi_writer *writer;
34
4fc83d94 35#ifdef LTTNG_EMBED_HELP
75723cd7 36const char help_msg[] =
4fc83d94 37#include <lttng-stop.1.h>
28ab034a 38 ;
4fc83d94
PP
39#endif
40
5330763e 41struct poptOption long_options[] = {
f3ed775e 42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
cd9adb8b
JG
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 },
2a8d72ed
OD
46 { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr },
47 { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr },
cd9adb8b 48 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
f3ed775e
DG
49};
50
e5b83100
JRJ
51/*
52 * Mi print of partial session
53 */
5330763e 54int mi_print_session(const char *session_name, int enabled)
e5b83100
JRJ
55{
56 int ret;
a0377dfe
FD
57 LTTNG_ASSERT(writer);
58 LTTNG_ASSERT(session_name);
e5b83100
JRJ
59
60 /* Open session element */
61 ret = mi_lttng_writer_open_element(writer, config_element_session);
62 if (ret) {
63 goto end;
64 }
65
66 /* Print session name element */
28ab034a 67 ret = mi_lttng_writer_write_element_string(writer, config_element_name, session_name);
e5b83100
JRJ
68 if (ret) {
69 goto end;
70 }
71
72 /* Is enabled ? */
28ab034a 73 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled);
e5b83100
JRJ
74 if (ret) {
75 goto end;
76 }
77
78 /* Close session element */
79 ret = mi_lttng_writer_close_element(writer);
80
81end:
82 return ret;
83}
f3ed775e
DG
84
85/*
cd80958d 86 * Start tracing for all trace of the session.
f3ed775e 87 */
75723cd7 88cmd_error_code stop_tracing(const char *session_name)
f3ed775e 89{
ae856491 90 int ret;
f3ed775e 91
8eb7a5e2 92 ret = lttng_stop_tracing_no_wait(session_name);
f3ed775e 93 if (ret < 0) {
f9a41357 94 LTTNG_THROW_CTL(lttng::format("Failed to start session `{}`", session_name),
75723cd7 95 static_cast<lttng_error_code>(-ret));
f3ed775e
DG
96 }
97
8eb7a5e2
JG
98 if (!opt_no_wait) {
99 _MSG("Waiting for data availability");
100 fflush(stdout);
101 do {
102 ret = lttng_data_pending(session_name);
103 if (ret < 0) {
104 /* Return the data available call error. */
75723cd7
JG
105 ERR_FMT("Failed to check pending data for session `{}` ({})",
106 session_name,
107 lttng_strerror(ret));
108 return CMD_ERROR;
8eb7a5e2
JG
109 }
110
111 /*
112 * Data sleep time before retrying (in usec). Don't sleep if the call
113 * returned value indicates availability.
114 */
115 if (ret) {
c8f61fd4 116 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
8eb7a5e2
JG
117 _MSG(".");
118 fflush(stdout);
119 }
120 } while (ret != 0);
121 MSG("");
122 }
123
20fb9e02 124 print_session_stats(session_name);
75723cd7 125 MSG("Tracing stopped for session `%s`", session_name);
e5b83100 126 if (lttng_opt_mi) {
75723cd7
JG
127 if (mi_print_session(session_name, 0)) {
128 return CMD_ERROR;
129 }
e5b83100 130 }
75723cd7
JG
131
132 return CMD_SUCCESS;
2a8d72ed 133}
f3ed775e 134
d5ed3e6f 135cmd_error_code stop_tracing(const lttng::cli::session_spec& spec)
2a8d72ed 136{
75723cd7
JG
137 bool had_warning = false;
138 bool had_error = false;
139 bool listing_failed = false;
140
6e11909e 141 const auto sessions = [&listing_failed, &spec]() -> lttng::cli::session_list {
75723cd7
JG
142 try {
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;
148 return {};
149 }
150 }();
151
6e11909e
JG
152 if (!listing_failed && sessions.size() == 0 &&
153 spec.type_ == lttng::cli::session_spec::type::NAME) {
75723cd7
JG
154 ERR_FMT("Session `{}` not found", spec.value);
155 return CMD_ERROR;
156 }
2a8d72ed 157
75723cd7
JG
158 if (listing_failed) {
159 return CMD_FATAL;
160 }
2a8d72ed 161
75723cd7
JG
162 for (const auto& session : sessions) {
163 cmd_error_code sub_ret;
164
165 try {
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;
172 break;
173 case LTTNG_ERR_NO_SESSION:
6e11909e 174 if (spec.type_ != lttng::cli::session_spec::type::NAME) {
75723cd7
JG
175 /* Session destroyed during command, ignore and carry-on. */
176 sub_ret = CMD_SUCCESS;
177 break;
178 } else {
179 sub_ret = CMD_ERROR;
180 break;
181 }
182 case LTTNG_ERR_NO_SESSIOND:
183 /* Don't keep going on a fatal error. */
184 return CMD_FATAL;
185 default:
186 /* Generic error. */
187 sub_ret = CMD_ERROR;
188 ERR_FMT("Failed to stop session `{}` ({})",
189 session.name,
190 lttng_strerror(-ctl_exception.code()));
191 break;
2a8d72ed
OD
192 }
193 }
75723cd7
JG
194
195 /* Keep going, but report the most serious state. */
196 had_warning |= sub_ret == CMD_WARNING;
197 had_error |= sub_ret == CMD_ERROR;
2a8d72ed 198 }
cd80958d 199
75723cd7
JG
200 if (had_error) {
201 return CMD_ERROR;
202 } else if (had_warning) {
203 return CMD_WARNING;
204 } else {
205 return CMD_SUCCESS;
206 }
f3ed775e 207}
5330763e 208} /* namespace */
f3ed775e
DG
209
210/*
211 * cmd_stop
212 *
213 * The 'stop <options>' first level command
214 */
215int cmd_stop(int argc, const char **argv)
216{
75723cd7
JG
217 int opt;
218 cmd_error_code command_ret = CMD_SUCCESS;
219 bool success = true;
f3ed775e 220 static poptContext pc;
cd9adb8b 221 const char *leftover = nullptr;
6e11909e 222 lttng::cli::session_spec session_spec(lttng::cli::session_spec::type::NAME);
f3ed775e 223
cd9adb8b 224 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
f3ed775e
DG
225 poptReadDefaultConfig(pc, 0);
226
227 while ((opt = poptGetNextOpt(pc)) != -1) {
228 switch (opt) {
229 case OPT_HELP:
75723cd7
JG
230 {
231 int ret;
232
4ba92f18 233 SHOW_HELP();
75723cd7 234 command_ret = static_cast<cmd_error_code>(ret);
f3ed775e 235 goto end;
75723cd7 236 }
679b4943
SM
237 case OPT_LIST_OPTIONS:
238 list_cmd_options(stdout, long_options);
679b4943 239 goto end;
2a8d72ed 240 case OPT_ENABLE_GLOB:
6e11909e 241 session_spec.type_ = lttng::cli::session_spec::type::GLOB_PATTERN;
2a8d72ed
OD
242 break;
243 case OPT_ALL:
6e11909e 244 session_spec.type_ = lttng::cli::session_spec::type::ALL;
2a8d72ed 245 break;
f3ed775e 246 default:
75723cd7 247 command_ret = CMD_UNDEFINED;
f3ed775e
DG
248 goto end;
249 }
250 }
251
e5b83100
JRJ
252 /* Mi check */
253 if (lttng_opt_mi) {
254 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
255 if (!writer) {
75723cd7 256 command_ret = CMD_ERROR;
e5b83100
JRJ
257 goto end;
258 }
259
260 /* Open command element */
75723cd7
JG
261 if (mi_lttng_writer_command_open(writer, mi_lttng_element_command_stop)) {
262 command_ret = CMD_ERROR;
e5b83100
JRJ
263 goto end;
264 }
265
266 /* Open output element */
75723cd7
JG
267 if (mi_lttng_writer_open_element(writer, mi_lttng_element_command_output)) {
268 command_ret = CMD_ERROR;
e5b83100
JRJ
269 goto end;
270 }
271
272 /*
273 * Open sessions element
274 * For validation
275 */
75723cd7
JG
276 if (mi_lttng_writer_open_element(writer, config_element_sessions)) {
277 command_ret = CMD_ERROR;
e5b83100
JRJ
278 goto end;
279 }
280 }
281
2a8d72ed 282 session_spec.value = poptGetArg(pc);
f3ed775e 283
68c7f6e5
JD
284 leftover = poptGetArg(pc);
285 if (leftover) {
286 ERR("Unknown argument: %s", leftover);
75723cd7 287 command_ret = CMD_ERROR;
68c7f6e5
JD
288 goto end;
289 }
290
2a8d72ed 291 command_ret = stop_tracing(session_spec);
e5b83100 292 if (command_ret) {
75723cd7 293 success = false;
e5b83100
JRJ
294 }
295
296 /* Mi closing */
297 if (lttng_opt_mi) {
298 /* Close sessions and output element */
75723cd7
JG
299 if (mi_lttng_close_multi_element(writer, 2)) {
300 command_ret = CMD_ERROR;
e5b83100
JRJ
301 goto end;
302 }
303
304 /* Success ? */
75723cd7
JG
305 if (mi_lttng_writer_write_element_bool(
306 writer, mi_lttng_element_command_success, success)) {
307 command_ret = CMD_ERROR;
e5b83100
JRJ
308 goto end;
309 }
310
311 /* Command element close */
75723cd7
JG
312 if (mi_lttng_writer_command_close(writer)) {
313 command_ret = CMD_ERROR;
e5b83100
JRJ
314 goto end;
315 }
316 }
f3ed775e
DG
317
318end:
e5b83100
JRJ
319 /* Mi clean-up */
320 if (writer && mi_lttng_writer_destroy(writer)) {
75723cd7 321 command_ret = CMD_ERROR;
e5b83100
JRJ
322 }
323
ca1c3607 324 poptFreeContext(pc);
75723cd7 325 return command_ret;
f3ed775e 326}
This page took 0.0907520000000001 seconds and 4 git commands to generate.