lttng: destroy: ensure a cmd_error_code is returned by the command
[lttng-tools.git] / src / bin / lttng / commands / start.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"
eaa8ef76 10#include "../utils.hpp"
28ab034a 11
eaa8ef76 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>
23
f3ed775e
DG
24enum {
25 OPT_HELP = 1,
679b4943 26 OPT_LIST_OPTIONS,
eaa8ef76
OD
27 OPT_ENABLE_GLOB,
28 OPT_ALL,
f3ed775e
DG
29};
30
9183bb27
JG
31namespace {
32struct mi_writer *writer;
33
34#ifdef LTTNG_EMBED_HELP
35const char help_msg[] =
36#include <lttng-start.1.h>
37 ;
38#endif
39
40struct poptOption long_options[] = {
f3ed775e 41 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
cd9adb8b
JG
42 { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
43 { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
eaa8ef76
OD
44 { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr },
45 { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr },
cd9adb8b 46 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
f3ed775e
DG
47};
48
9183bb27 49int mi_print_session(const char *session_name, int enabled)
1cfc0bc8
JRJ
50{
51 int ret;
52
53 /* Open session element */
54 ret = mi_lttng_writer_open_element(writer, config_element_session);
55 if (ret) {
56 goto end;
57 }
58
59 /* Print session name element */
28ab034a 60 ret = mi_lttng_writer_write_element_string(writer, config_element_name, session_name);
1cfc0bc8
JRJ
61 if (ret) {
62 goto end;
63 }
64
28ab034a 65 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled);
1cfc0bc8
JRJ
66 if (ret) {
67 goto end;
68 }
69
70 /* Close session element */
71 ret = mi_lttng_writer_close_element(writer);
72
73end:
74 return ret;
75}
76
f3ed775e
DG
77/*
78 * start_tracing
79 *
80 * Start tracing for all trace of the session.
81 */
9497e2f7 82cmd_error_code start_tracing(const char *session_name)
f3ed775e 83{
cd9adb8b 84 if (session_name == nullptr) {
9497e2f7 85 return CMD_ERROR;
f3ed775e
DG
86 }
87
9497e2f7 88 DBG("Starting tracing for session `%s`", session_name);
f3ed775e 89
9497e2f7 90 const int ret = lttng_start_tracing(session_name);
f3ed775e 91 if (ret < 0) {
9497e2f7
JG
92 LTTNG_THROW_CTL(fmt::format("Failed to start session `{}`", session_name),
93 static_cast<lttng_error_code>(-ret));
f3ed775e
DG
94 }
95
9497e2f7 96 MSG("Tracing started for session `%s`", session_name);
1cfc0bc8 97 if (lttng_opt_mi) {
9497e2f7
JG
98 if (mi_print_session(session_name, 1)) {
99 return CMD_ERROR;
1cfc0bc8
JRJ
100 }
101 }
f3ed775e 102
9497e2f7 103 return CMD_SUCCESS;
f3ed775e
DG
104}
105
9497e2f7 106cmd_error_code start_tracing(const session_spec& spec) noexcept
eaa8ef76 107{
eaa8ef76 108 bool had_warning = false;
9497e2f7
JG
109 bool had_error = false;
110 bool listing_failed = false;
111
112 const auto sessions = [&listing_failed, &spec]() -> session_list {
113 try {
114 return list_sessions(spec);
115 } catch (const lttng::ctl::error& ctl_exception) {
116 ERR_FMT("Failed to list sessions ({})",
117 lttng_strerror(-ctl_exception.code()));
118 listing_failed = true;
119 return {};
120 }
121 }();
122
123 if (!listing_failed && sessions.size() == 0 && spec.type == session_spec::type::NAME) {
124 ERR_FMT("Session `{}` not found", spec.value);
125 return CMD_ERROR;
126 }
eaa8ef76 127
9497e2f7
JG
128 if (listing_failed) {
129 return CMD_FATAL;
130 }
131
132 for (const auto& session : sessions) {
133 cmd_error_code sub_ret;
eaa8ef76 134
9497e2f7
JG
135 try {
136 sub_ret = start_tracing(session.name);
137 } catch (const lttng::ctl::error& ctl_exception) {
138 switch (ctl_exception.code()) {
139 case LTTNG_ERR_TRACE_ALREADY_STARTED:
140 WARN_FMT("Tracing already started for session `{}`", session.name);
141 sub_ret = CMD_SUCCESS;
142 break;
143 case LTTNG_ERR_NO_SESSION:
144 if (spec.type != session_spec::type::NAME) {
145 /* Session destroyed during command, ignore and carry-on. */
146 sub_ret = CMD_SUCCESS;
147 break;
148 } else {
149 sub_ret = CMD_ERROR;
150 break;
151 }
152 case LTTNG_ERR_NO_SESSIOND:
153 /* Don't keep going on a fatal error. */
154 return CMD_FATAL;
eaa8ef76 155 default:
9497e2f7
JG
156 /* Generic error. */
157 sub_ret = CMD_ERROR;
158 ERR_FMT("Failed to start session `{}` ({})",
159 session.name,
160 lttng_strerror(-ctl_exception.code()));
eaa8ef76
OD
161 break;
162 }
163 }
eaa8ef76 164
9497e2f7
JG
165 /* Keep going, but report the most serious state. */
166 had_warning |= sub_ret == CMD_WARNING;
167 had_error |= sub_ret == CMD_ERROR;
eaa8ef76
OD
168 }
169
9497e2f7
JG
170 if (had_error) {
171 return CMD_ERROR;
172 } else if (had_warning) {
173 return CMD_WARNING;
174 } else {
175 return CMD_SUCCESS;
176 }
eaa8ef76 177}
9183bb27 178} /* namespace */
eaa8ef76 179
f3ed775e
DG
180/*
181 * cmd_start
182 *
183 * The 'start <options>' first level command
184 */
185int cmd_start(int argc, const char **argv)
186{
9497e2f7
JG
187 int opt;
188 cmd_error_code command_ret = CMD_SUCCESS;
189 bool success = true;
f3ed775e 190 static poptContext pc;
cd9adb8b 191 const char *leftover = nullptr;
9497e2f7 192 session_spec session_spec = {
eaa8ef76
OD
193 .type = session_spec::NAME,
194 .value = nullptr,
195 };
f3ed775e 196
cd9adb8b 197 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
f3ed775e
DG
198 poptReadDefaultConfig(pc, 0);
199
200 while ((opt = poptGetNextOpt(pc)) != -1) {
201 switch (opt) {
202 case OPT_HELP:
9497e2f7
JG
203 {
204 int ret;
205
4ba92f18 206 SHOW_HELP();
9497e2f7 207 command_ret = static_cast<cmd_error_code>(ret);
f3ed775e 208 goto end;
9497e2f7 209 }
679b4943
SM
210 case OPT_LIST_OPTIONS:
211 list_cmd_options(stdout, long_options);
679b4943 212 goto end;
eaa8ef76
OD
213 case OPT_ENABLE_GLOB:
214 session_spec.type = session_spec::GLOB_PATTERN;
215 break;
216 case OPT_ALL:
217 session_spec.type = session_spec::ALL;
218 break;
f3ed775e 219 default:
9497e2f7 220 command_ret = CMD_UNDEFINED;
f3ed775e
DG
221 goto end;
222 }
223 }
224
eaa8ef76 225 session_spec.value = poptGetArg(pc);
f3ed775e 226
68c7f6e5
JD
227 leftover = poptGetArg(pc);
228 if (leftover) {
229 ERR("Unknown argument: %s", leftover);
9497e2f7 230 command_ret = CMD_ERROR;
68c7f6e5
JD
231 goto end;
232 }
233
1cfc0bc8
JRJ
234 /* Mi check */
235 if (lttng_opt_mi) {
236 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
237 if (!writer) {
9497e2f7 238 command_ret = CMD_ERROR;
1cfc0bc8
JRJ
239 goto end;
240 }
241
242 /* Open command element */
9497e2f7
JG
243 if (mi_lttng_writer_command_open(writer, mi_lttng_element_command_start)) {
244 command_ret = CMD_ERROR;
1cfc0bc8
JRJ
245 goto end;
246 }
247
248 /* Open output element */
9497e2f7
JG
249 if (mi_lttng_writer_open_element(writer, mi_lttng_element_command_output)) {
250 command_ret = CMD_ERROR;
1cfc0bc8
JRJ
251 goto end;
252 }
253
254 /*
255 * Open sessions element
256 * For validation purpose
257 */
9497e2f7
JG
258 if (mi_lttng_writer_open_element(writer, config_element_sessions)) {
259 command_ret = CMD_ERROR;
1cfc0bc8
JRJ
260 goto end;
261 }
262 }
263
eaa8ef76 264 command_ret = start_tracing(session_spec);
9497e2f7
JG
265 if (command_ret != CMD_SUCCESS) {
266 success = false;
1cfc0bc8
JRJ
267 }
268
269 /* Mi closing */
270 if (lttng_opt_mi) {
271 /* Close sessions and output element */
9497e2f7
JG
272 if (mi_lttng_close_multi_element(writer, 2)) {
273 command_ret = CMD_ERROR;
1cfc0bc8
JRJ
274 goto end;
275 }
276
277 /* Success ? */
9497e2f7
JG
278 if (mi_lttng_writer_write_element_bool(
279 writer, mi_lttng_element_command_success, success)) {
280 command_ret = CMD_ERROR;
1cfc0bc8
JRJ
281 goto end;
282 }
283
284 /* Command element close */
9497e2f7
JG
285 if (mi_lttng_writer_command_close(writer)) {
286 command_ret = CMD_ERROR;
1cfc0bc8
JRJ
287 goto end;
288 }
289 }
f3ed775e
DG
290
291end:
1cfc0bc8
JRJ
292 /* Mi clean-up */
293 if (writer && mi_lttng_writer_destroy(writer)) {
9497e2f7 294 command_ret = CMD_ERROR;
1cfc0bc8
JRJ
295 }
296
ca1c3607 297 poptFreeContext(pc);
9497e2f7 298 return command_ret;
f3ed775e 299}
This page took 0.100604 seconds and 4 git commands to generate.