lttng: enable-channel: move kernel tracer status check to util
[lttng-tools.git] / src / bin / lttng / commands / stop.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "../command.hpp"
10 #include "../utils.hpp"
11
12 #include <common/exception.hpp>
13 #include <common/mi-lttng.hpp>
14 #include <common/sessiond-comm/sessiond-comm.hpp>
15
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>
23
24 enum {
25 OPT_HELP = 1,
26 OPT_LIST_OPTIONS,
27 OPT_ENABLE_GLOB,
28 OPT_ALL,
29 };
30
31 namespace {
32 int opt_no_wait;
33 struct mi_writer *writer;
34
35 #ifdef LTTNG_EMBED_HELP
36 const char help_msg[] =
37 #include <lttng-stop.1.h>
38 ;
39 #endif
40
41 struct poptOption long_options[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
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 },
46 { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr },
47 { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr },
48 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
49 };
50
51 /*
52 * Mi print of partial session
53 */
54 int mi_print_session(const char *session_name, int enabled)
55 {
56 int ret;
57 LTTNG_ASSERT(writer);
58 LTTNG_ASSERT(session_name);
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 */
67 ret = mi_lttng_writer_write_element_string(writer, config_element_name, session_name);
68 if (ret) {
69 goto end;
70 }
71
72 /* Is enabled ? */
73 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled);
74 if (ret) {
75 goto end;
76 }
77
78 /* Close session element */
79 ret = mi_lttng_writer_close_element(writer);
80
81 end:
82 return ret;
83 }
84
85 /*
86 * Start tracing for all trace of the session.
87 */
88 cmd_error_code stop_tracing(const char *session_name)
89 {
90 int ret;
91
92 ret = lttng_stop_tracing_no_wait(session_name);
93 if (ret < 0) {
94 LTTNG_THROW_CTL(lttng::format("Failed to start session `{}`", session_name),
95 static_cast<lttng_error_code>(-ret));
96 }
97
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. */
105 ERR_FMT("Failed to check pending data for session `{}` ({})",
106 session_name,
107 lttng_strerror(ret));
108 return CMD_ERROR;
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) {
116 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
117 _MSG(".");
118 fflush(stdout);
119 }
120 } while (ret != 0);
121 MSG("");
122 }
123
124 print_session_stats(session_name);
125 MSG("Tracing stopped for session `%s`", session_name);
126 if (lttng_opt_mi) {
127 if (mi_print_session(session_name, 0)) {
128 return CMD_ERROR;
129 }
130 }
131
132 return CMD_SUCCESS;
133 }
134
135 cmd_error_code stop_tracing(const lttng::cli::session_spec& spec)
136 {
137 bool had_warning = false;
138 bool had_error = false;
139 bool listing_failed = false;
140
141 const auto sessions = [&listing_failed, &spec]() -> lttng::cli::session_list {
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
152 if (!listing_failed && sessions.size() == 0 &&
153 spec.type_ == lttng::cli::session_spec::type::NAME) {
154 ERR_FMT("Session `{}` not found", spec.value);
155 return CMD_ERROR;
156 }
157
158 if (listing_failed) {
159 return CMD_FATAL;
160 }
161
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:
174 if (spec.type_ != lttng::cli::session_spec::type::NAME) {
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;
192 }
193 }
194
195 /* Keep going, but report the most serious state. */
196 had_warning |= sub_ret == CMD_WARNING;
197 had_error |= sub_ret == CMD_ERROR;
198 }
199
200 if (had_error) {
201 return CMD_ERROR;
202 } else if (had_warning) {
203 return CMD_WARNING;
204 } else {
205 return CMD_SUCCESS;
206 }
207 }
208 } /* namespace */
209
210 /*
211 * cmd_stop
212 *
213 * The 'stop <options>' first level command
214 */
215 int cmd_stop(int argc, const char **argv)
216 {
217 int opt;
218 cmd_error_code command_ret = CMD_SUCCESS;
219 bool success = true;
220 static poptContext pc;
221 const char *leftover = nullptr;
222 lttng::cli::session_spec session_spec(lttng::cli::session_spec::type::NAME);
223
224 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
225 poptReadDefaultConfig(pc, 0);
226
227 while ((opt = poptGetNextOpt(pc)) != -1) {
228 switch (opt) {
229 case OPT_HELP:
230 {
231 int ret;
232
233 SHOW_HELP();
234 command_ret = static_cast<cmd_error_code>(ret);
235 goto end;
236 }
237 case OPT_LIST_OPTIONS:
238 list_cmd_options(stdout, long_options);
239 goto end;
240 case OPT_ENABLE_GLOB:
241 session_spec.type_ = lttng::cli::session_spec::type::GLOB_PATTERN;
242 break;
243 case OPT_ALL:
244 session_spec.type_ = lttng::cli::session_spec::type::ALL;
245 break;
246 default:
247 command_ret = CMD_UNDEFINED;
248 goto end;
249 }
250 }
251
252 /* Mi check */
253 if (lttng_opt_mi) {
254 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
255 if (!writer) {
256 command_ret = CMD_ERROR;
257 goto end;
258 }
259
260 /* Open command element */
261 if (mi_lttng_writer_command_open(writer, mi_lttng_element_command_stop)) {
262 command_ret = CMD_ERROR;
263 goto end;
264 }
265
266 /* Open output element */
267 if (mi_lttng_writer_open_element(writer, mi_lttng_element_command_output)) {
268 command_ret = CMD_ERROR;
269 goto end;
270 }
271
272 /*
273 * Open sessions element
274 * For validation
275 */
276 if (mi_lttng_writer_open_element(writer, config_element_sessions)) {
277 command_ret = CMD_ERROR;
278 goto end;
279 }
280 }
281
282 session_spec.value = poptGetArg(pc);
283
284 leftover = poptGetArg(pc);
285 if (leftover) {
286 ERR("Unknown argument: %s", leftover);
287 command_ret = CMD_ERROR;
288 goto end;
289 }
290
291 command_ret = stop_tracing(session_spec);
292 if (command_ret) {
293 success = false;
294 }
295
296 /* Mi closing */
297 if (lttng_opt_mi) {
298 /* Close sessions and output element */
299 if (mi_lttng_close_multi_element(writer, 2)) {
300 command_ret = CMD_ERROR;
301 goto end;
302 }
303
304 /* Success ? */
305 if (mi_lttng_writer_write_element_bool(
306 writer, mi_lttng_element_command_success, success)) {
307 command_ret = CMD_ERROR;
308 goto end;
309 }
310
311 /* Command element close */
312 if (mi_lttng_writer_command_close(writer)) {
313 command_ret = CMD_ERROR;
314 goto end;
315 }
316 }
317
318 end:
319 /* Mi clean-up */
320 if (writer && mi_lttng_writer_destroy(writer)) {
321 command_ret = CMD_ERROR;
322 }
323
324 poptFreeContext(pc);
325 return command_ret;
326 }
This page took 0.036991 seconds and 4 git commands to generate.