Commit | Line | Data |
---|---|---|
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 | ||
1cfc0bc8 | 24 | static struct mi_writer *writer; |
f3ed775e | 25 | |
4fc83d94 PP |
26 | #ifdef LTTNG_EMBED_HELP |
27 | static const char help_msg[] = | |
28 | #include <lttng-start.1.h> | |
28ab034a | 29 | ; |
4fc83d94 PP |
30 | #endif |
31 | ||
f3ed775e DG |
32 | enum { |
33 | OPT_HELP = 1, | |
679b4943 | 34 | OPT_LIST_OPTIONS, |
eaa8ef76 OD |
35 | OPT_ENABLE_GLOB, |
36 | OPT_ALL, | |
f3ed775e DG |
37 | }; |
38 | ||
39 | static struct poptOption long_options[] = { | |
40 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
cd9adb8b JG |
41 | { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, |
42 | { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, | |
eaa8ef76 OD |
43 | { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr }, |
44 | { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr }, | |
cd9adb8b | 45 | { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } |
f3ed775e DG |
46 | }; |
47 | ||
eaa8ef76 | 48 | static int mi_print_session(const char *session_name, int enabled) |
1cfc0bc8 JRJ |
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 */ | |
28ab034a | 59 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, session_name); |
1cfc0bc8 JRJ |
60 | if (ret) { |
61 | goto end; | |
62 | } | |
63 | ||
28ab034a | 64 | ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled); |
1cfc0bc8 JRJ |
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 | ||
f3ed775e DG |
76 | /* |
77 | * start_tracing | |
78 | * | |
79 | * Start tracing for all trace of the session. | |
80 | */ | |
eaa8ef76 | 81 | static int start_tracing(const char *session_name) |
f3ed775e | 82 | { |
ae856491 | 83 | int ret; |
5b915816 | 84 | |
cd9adb8b | 85 | if (session_name == nullptr) { |
5b915816 MJ |
86 | ret = CMD_ERROR; |
87 | goto error; | |
f3ed775e DG |
88 | } |
89 | ||
90 | DBG("Starting tracing for session %s", session_name); | |
91 | ||
6a4f824d | 92 | ret = lttng_start_tracing(session_name); |
f3ed775e | 93 | if (ret < 0) { |
42224349 | 94 | switch (-ret) { |
f73fabfd | 95 | case LTTNG_ERR_TRACE_ALREADY_STARTED: |
42224349 DG |
96 | WARN("Tracing already started for session %s", session_name); |
97 | break; | |
98 | default: | |
99 | ERR("%s", lttng_strerror(ret)); | |
100 | break; | |
101 | } | |
eaa8ef76 | 102 | goto error; |
f3ed775e DG |
103 | } |
104 | ||
ae856491 DG |
105 | ret = CMD_SUCCESS; |
106 | ||
f3ed775e | 107 | MSG("Tracing started for session %s", session_name); |
1cfc0bc8 JRJ |
108 | if (lttng_opt_mi) { |
109 | ret = mi_print_session(session_name, 1); | |
110 | if (ret) { | |
111 | ret = CMD_ERROR; | |
eaa8ef76 | 112 | goto error; |
1cfc0bc8 JRJ |
113 | } |
114 | } | |
f3ed775e | 115 | |
f3ed775e DG |
116 | error: |
117 | return ret; | |
118 | } | |
119 | ||
eaa8ef76 OD |
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 | ||
f3ed775e DG |
152 | /* |
153 | * cmd_start | |
154 | * | |
155 | * The 'start <options>' first level command | |
156 | */ | |
157 | int cmd_start(int argc, const char **argv) | |
158 | { | |
1cfc0bc8 | 159 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
f3ed775e | 160 | static poptContext pc; |
cd9adb8b | 161 | const char *leftover = nullptr; |
eaa8ef76 OD |
162 | struct session_spec session_spec = { |
163 | .type = session_spec::NAME, | |
164 | .value = nullptr, | |
165 | }; | |
f3ed775e | 166 | |
cd9adb8b | 167 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
f3ed775e DG |
168 | poptReadDefaultConfig(pc, 0); |
169 | ||
170 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
171 | switch (opt) { | |
172 | case OPT_HELP: | |
4ba92f18 | 173 | SHOW_HELP(); |
f3ed775e | 174 | goto end; |
679b4943 SM |
175 | case OPT_LIST_OPTIONS: |
176 | list_cmd_options(stdout, long_options); | |
679b4943 | 177 | goto end; |
eaa8ef76 OD |
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; | |
f3ed775e | 184 | default: |
f3ed775e DG |
185 | ret = CMD_UNDEFINED; |
186 | goto end; | |
187 | } | |
188 | } | |
189 | ||
eaa8ef76 | 190 | session_spec.value = poptGetArg(pc); |
f3ed775e | 191 | |
68c7f6e5 JD |
192 | leftover = poptGetArg(pc); |
193 | if (leftover) { | |
194 | ERR("Unknown argument: %s", leftover); | |
195 | ret = CMD_ERROR; | |
196 | goto end; | |
197 | } | |
198 | ||
1cfc0bc8 JRJ |
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 */ | |
28ab034a | 208 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_start); |
1cfc0bc8 JRJ |
209 | if (ret) { |
210 | ret = CMD_ERROR; | |
211 | goto end; | |
212 | } | |
213 | ||
214 | /* Open output element */ | |
28ab034a | 215 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
1cfc0bc8 JRJ |
216 | if (ret) { |
217 | ret = CMD_ERROR; | |
218 | goto end; | |
219 | } | |
220 | ||
221 | /* | |
222 | * Open sessions element | |
223 | * For validation purpose | |
224 | */ | |
28ab034a | 225 | ret = mi_lttng_writer_open_element(writer, config_element_sessions); |
1cfc0bc8 JRJ |
226 | if (ret) { |
227 | ret = CMD_ERROR; | |
228 | goto end; | |
229 | } | |
230 | } | |
231 | ||
eaa8ef76 | 232 | command_ret = start_tracing(session_spec); |
1cfc0bc8 JRJ |
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 ? */ | |
28ab034a JG |
247 | ret = mi_lttng_writer_write_element_bool( |
248 | writer, mi_lttng_element_command_success, success); | |
1cfc0bc8 JRJ |
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 | } | |
f3ed775e DG |
261 | |
262 | end: | |
1cfc0bc8 JRJ |
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; | |
ca1c3607 | 271 | poptFreeContext(pc); |
f3ed775e DG |
272 | return ret; |
273 | } |