lttng: Add --glob option to lttng-destroy
[lttng-tools.git] / src / bin / lttng / commands / start.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 static struct mi_writer *writer;
25
26 #ifdef LTTNG_EMBED_HELP
27 static const char help_msg[] =
28 #include <lttng-start.1.h>
29 ;
30 #endif
31
32 enum {
33 OPT_HELP = 1,
34 OPT_LIST_OPTIONS,
35 OPT_ENABLE_GLOB,
36 OPT_ALL,
37 };
38
39 static struct poptOption long_options[] = {
40 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
41 { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
42 { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
43 { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr },
44 { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr },
45 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
46 };
47
48 static int mi_print_session(const char *session_name, int enabled)
49 {
50 int ret;
51
52 /* Open session element */
53 ret = mi_lttng_writer_open_element(writer, config_element_session);
54 if (ret) {
55 goto end;
56 }
57
58 /* Print session name element */
59 ret = mi_lttng_writer_write_element_string(writer, config_element_name, session_name);
60 if (ret) {
61 goto end;
62 }
63
64 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled);
65 if (ret) {
66 goto end;
67 }
68
69 /* Close session element */
70 ret = mi_lttng_writer_close_element(writer);
71
72 end:
73 return ret;
74 }
75
76 /*
77 * start_tracing
78 *
79 * Start tracing for all trace of the session.
80 */
81 static int start_tracing(const char *session_name)
82 {
83 int ret;
84
85 if (session_name == nullptr) {
86 ret = CMD_ERROR;
87 goto error;
88 }
89
90 DBG("Starting tracing for session %s", session_name);
91
92 ret = lttng_start_tracing(session_name);
93 if (ret < 0) {
94 switch (-ret) {
95 case LTTNG_ERR_TRACE_ALREADY_STARTED:
96 WARN("Tracing already started for session %s", session_name);
97 break;
98 default:
99 ERR("%s", lttng_strerror(ret));
100 break;
101 }
102 goto error;
103 }
104
105 ret = CMD_SUCCESS;
106
107 MSG("Tracing started for session %s", session_name);
108 if (lttng_opt_mi) {
109 ret = mi_print_session(session_name, 1);
110 if (ret) {
111 ret = CMD_ERROR;
112 goto error;
113 }
114 }
115
116 error:
117 return ret;
118 }
119
120 static int start_tracing(const struct session_spec& spec)
121 {
122 int ret = CMD_SUCCESS;
123 bool had_warning = false;
124
125 try {
126 for (const auto& session : list_sessions(spec)) {
127 const auto sub_ret = start_tracing(session.name);
128
129 switch (sub_ret) {
130 case CMD_WARNING:
131 had_warning = true;
132 /* fall-through. */
133 case CMD_SUCCESS:
134 continue;
135 default:
136 ret = sub_ret;
137 break;
138 }
139 }
140 } catch (const std::exception& e) {
141 ERR_FMT("{}", e.what());
142 return CMD_FATAL;
143 }
144
145 if (ret == CMD_SUCCESS && had_warning) {
146 ret = CMD_WARNING;
147 }
148
149 return ret;
150 }
151
152 /*
153 * cmd_start
154 *
155 * The 'start <options>' first level command
156 */
157 int cmd_start(int argc, const char **argv)
158 {
159 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
160 static poptContext pc;
161 const char *leftover = nullptr;
162 struct session_spec session_spec = {
163 .type = session_spec::NAME,
164 .value = nullptr,
165 };
166
167 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
168 poptReadDefaultConfig(pc, 0);
169
170 while ((opt = poptGetNextOpt(pc)) != -1) {
171 switch (opt) {
172 case OPT_HELP:
173 SHOW_HELP();
174 goto end;
175 case OPT_LIST_OPTIONS:
176 list_cmd_options(stdout, long_options);
177 goto end;
178 case OPT_ENABLE_GLOB:
179 session_spec.type = session_spec::GLOB_PATTERN;
180 break;
181 case OPT_ALL:
182 session_spec.type = session_spec::ALL;
183 break;
184 default:
185 ret = CMD_UNDEFINED;
186 goto end;
187 }
188 }
189
190 session_spec.value = poptGetArg(pc);
191
192 leftover = poptGetArg(pc);
193 if (leftover) {
194 ERR("Unknown argument: %s", leftover);
195 ret = CMD_ERROR;
196 goto end;
197 }
198
199 /* Mi check */
200 if (lttng_opt_mi) {
201 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
202 if (!writer) {
203 ret = -LTTNG_ERR_NOMEM;
204 goto end;
205 }
206
207 /* Open command element */
208 ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_start);
209 if (ret) {
210 ret = CMD_ERROR;
211 goto end;
212 }
213
214 /* Open output element */
215 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output);
216 if (ret) {
217 ret = CMD_ERROR;
218 goto end;
219 }
220
221 /*
222 * Open sessions element
223 * For validation purpose
224 */
225 ret = mi_lttng_writer_open_element(writer, config_element_sessions);
226 if (ret) {
227 ret = CMD_ERROR;
228 goto end;
229 }
230 }
231
232 command_ret = start_tracing(session_spec);
233 if (command_ret) {
234 success = 0;
235 }
236
237 /* Mi closing */
238 if (lttng_opt_mi) {
239 /* Close sessions and output element */
240 ret = mi_lttng_close_multi_element(writer, 2);
241 if (ret) {
242 ret = CMD_ERROR;
243 goto end;
244 }
245
246 /* Success ? */
247 ret = mi_lttng_writer_write_element_bool(
248 writer, mi_lttng_element_command_success, success);
249 if (ret) {
250 ret = CMD_ERROR;
251 goto end;
252 }
253
254 /* Command element close */
255 ret = mi_lttng_writer_command_close(writer);
256 if (ret) {
257 ret = CMD_ERROR;
258 goto end;
259 }
260 }
261
262 end:
263 /* Mi clean-up */
264 if (writer && mi_lttng_writer_destroy(writer)) {
265 /* Preserve original error code */
266 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
267 }
268
269 /* Overwrite ret if an error occurred with start_tracing */
270 ret = command_ret ? command_ret : ret;
271 poptFreeContext(pc);
272 return ret;
273 }
This page took 0.037937 seconds and 4 git commands to generate.