Commit | Line | Data |
---|---|---|
eded6438 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com> |
eded6438 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
eded6438 | 5 | * |
eded6438 JD |
6 | */ |
7 | ||
8 | #define _LGPL_SOURCE | |
28ab034a JG |
9 | #include "../command.hpp" |
10 | ||
11 | #include <common/mi-lttng.hpp> | |
12 | ||
eded6438 JD |
13 | #include <ctype.h> |
14 | #include <popt.h> | |
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | #include <string.h> | |
18 | #include <unistd.h> | |
19 | ||
eded6438 | 20 | static char *opt_session_name; |
cd9adb8b | 21 | static char *session_name = nullptr; |
eded6438 JD |
22 | |
23 | static int regenerate_metadata(int argc, const char **argv); | |
c2561365 | 24 | static int regenerate_statedump(int argc, const char **argv); |
eded6438 | 25 | |
4fc83d94 PP |
26 | #ifdef LTTNG_EMBED_HELP |
27 | static const char help_msg[] = | |
28 | #include <lttng-regenerate.1.h> | |
28ab034a | 29 | ; |
4fc83d94 PP |
30 | #endif |
31 | ||
eded6438 JD |
32 | enum { |
33 | OPT_HELP = 1, | |
34 | OPT_LIST_OPTIONS, | |
35 | OPT_LIST_COMMANDS, | |
36 | }; | |
37 | ||
38 | static struct mi_writer *writer; | |
39 | ||
40 | static struct poptOption long_options[] = { | |
41 | /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */ | |
28ab034a JG |
42 | { |
43 | "help", | |
44 | 'h', | |
45 | POPT_ARG_NONE, | |
cd9adb8b | 46 | nullptr, |
28ab034a | 47 | OPT_HELP, |
cd9adb8b JG |
48 | nullptr, |
49 | nullptr, | |
28ab034a | 50 | }, |
cd9adb8b | 51 | { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, nullptr, nullptr }, |
28ab034a JG |
52 | { |
53 | "list-options", | |
54 | 0, | |
55 | POPT_ARG_NONE, | |
cd9adb8b | 56 | nullptr, |
28ab034a | 57 | OPT_LIST_OPTIONS, |
cd9adb8b JG |
58 | nullptr, |
59 | nullptr, | |
28ab034a | 60 | }, |
cd9adb8b | 61 | { "list-commands", 0, POPT_ARG_NONE, nullptr, OPT_LIST_COMMANDS, nullptr, nullptr }, |
28ab034a | 62 | { |
cd9adb8b | 63 | nullptr, |
28ab034a JG |
64 | 0, |
65 | 0, | |
cd9adb8b | 66 | nullptr, |
28ab034a | 67 | 0, |
cd9adb8b JG |
68 | nullptr, |
69 | nullptr, | |
28ab034a | 70 | }, |
eded6438 JD |
71 | }; |
72 | ||
73 | static struct cmd_struct actions[] = { | |
74 | { "metadata", regenerate_metadata }, | |
c2561365 | 75 | { "statedump", regenerate_statedump }, |
cd9adb8b | 76 | { nullptr, nullptr } /* Array closure */ |
eded6438 JD |
77 | }; |
78 | ||
79 | /* | |
80 | * Count and return the number of arguments in argv. | |
81 | */ | |
82 | static int count_arguments(const char **argv) | |
83 | { | |
84 | int i = 0; | |
85 | ||
a0377dfe | 86 | LTTNG_ASSERT(argv); |
eded6438 | 87 | |
cd9adb8b | 88 | while (argv[i] != nullptr) { |
eded6438 JD |
89 | i++; |
90 | } | |
91 | ||
92 | return i; | |
93 | } | |
94 | ||
28ab034a | 95 | static int regenerate_metadata(int argc, const char **argv __attribute__((unused))) |
eded6438 JD |
96 | { |
97 | int ret; | |
98 | ||
99 | if (argc > 1) { | |
fcf3687f | 100 | ret = CMD_UNDEFINED; |
eded6438 JD |
101 | goto end; |
102 | } | |
103 | ret = lttng_regenerate_metadata(session_name); | |
104 | if (ret == 0) { | |
105 | MSG("Metadata successfully regenerated for session %s", session_name); | |
fcf3687f JG |
106 | } else { |
107 | ERR("%s", lttng_strerror(ret)); | |
eded6438 JD |
108 | } |
109 | ||
110 | end: | |
111 | return ret; | |
112 | } | |
113 | ||
28ab034a | 114 | static int regenerate_statedump(int argc, const char **argv __attribute__((unused))) |
c2561365 JD |
115 | { |
116 | int ret; | |
117 | ||
118 | if (argc > 1) { | |
119 | ret = -LTTNG_ERR_INVALID; | |
120 | goto end; | |
121 | } | |
122 | ret = lttng_regenerate_statedump(session_name); | |
123 | if (ret == 0) { | |
124 | MSG("State dump successfully regenerated for session %s", session_name); | |
125 | } | |
126 | ||
127 | end: | |
128 | return ret; | |
129 | } | |
130 | ||
eded6438 JD |
131 | static int handle_command(const char **argv) |
132 | { | |
133 | struct cmd_struct *cmd; | |
134 | int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS; | |
135 | ||
cd9adb8b | 136 | if (argv == nullptr) { |
fcf3687f | 137 | ERR("No object specified for regenerate command."); |
eded6438 JD |
138 | command_ret = CMD_ERROR; |
139 | goto end; | |
140 | } | |
141 | ||
142 | argc = count_arguments(argv); | |
a0377dfe | 143 | LTTNG_ASSERT(argc >= 1); |
eded6438 JD |
144 | |
145 | cmd = &actions[i]; | |
cd9adb8b | 146 | while (cmd->func != nullptr) { |
eded6438 JD |
147 | /* Find command */ |
148 | if (strcmp(argv[0], cmd->name) == 0) { | |
149 | if (lttng_opt_mi) { | |
150 | /* Action element */ | |
28ab034a JG |
151 | ret = mi_lttng_writer_open_element( |
152 | writer, mi_lttng_element_command_regenerate_action); | |
eded6438 JD |
153 | if (ret) { |
154 | ret = CMD_ERROR; | |
155 | goto end; | |
156 | } | |
157 | ||
158 | /* Name of the action */ | |
28ab034a JG |
159 | ret = mi_lttng_writer_write_element_string( |
160 | writer, config_element_name, argv[0]); | |
eded6438 JD |
161 | if (ret) { |
162 | ret = CMD_ERROR; | |
163 | goto end; | |
164 | } | |
165 | } | |
166 | command_ret = cmd->func(argc, argv); | |
167 | if (lttng_opt_mi) { | |
168 | /* Close output and action element */ | |
169 | ret = mi_lttng_writer_close_element(writer); | |
170 | if (ret) { | |
171 | ret = CMD_ERROR; | |
172 | goto end; | |
173 | } | |
174 | } | |
175 | goto end; | |
176 | } | |
177 | ||
178 | cmd = &actions[i++]; | |
179 | } | |
180 | ||
181 | ret = CMD_UNDEFINED; | |
182 | ||
183 | end: | |
184 | /* Overwrite ret if an error occurred in cmd->func() */ | |
185 | ret = command_ret ? command_ret : ret; | |
186 | return ret; | |
187 | } | |
188 | ||
189 | /* | |
190 | * regenerate command handling. | |
191 | */ | |
192 | int cmd_regenerate(int argc, const char **argv) | |
193 | { | |
194 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; | |
195 | static poptContext pc; | |
196 | ||
197 | if (argc < 1) { | |
198 | SHOW_HELP(); | |
199 | ret = CMD_ERROR; | |
200 | goto end; | |
201 | } | |
202 | ||
cd9adb8b | 203 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
eded6438 JD |
204 | poptReadDefaultConfig(pc, 0); |
205 | ||
206 | if (lttng_opt_mi) { | |
207 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
208 | if (!writer) { | |
209 | ret = -LTTNG_ERR_NOMEM; | |
210 | goto end; | |
211 | } | |
212 | /* Open command element */ | |
28ab034a | 213 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_regenerate); |
eded6438 JD |
214 | if (ret) { |
215 | ret = CMD_ERROR; | |
216 | goto end; | |
217 | } | |
218 | ||
219 | /* Open output element */ | |
28ab034a | 220 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
eded6438 JD |
221 | if (ret) { |
222 | ret = CMD_ERROR; | |
223 | goto end; | |
224 | } | |
225 | } | |
226 | ||
227 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
228 | switch (opt) { | |
229 | case OPT_HELP: | |
230 | SHOW_HELP(); | |
231 | goto end; | |
232 | case OPT_LIST_OPTIONS: | |
233 | list_cmd_options(stdout, long_options); | |
234 | goto end; | |
235 | case OPT_LIST_COMMANDS: | |
236 | list_commands(actions, stdout); | |
237 | goto end; | |
238 | default: | |
239 | SHOW_HELP(); | |
240 | ret = CMD_UNDEFINED; | |
241 | goto end; | |
242 | } | |
243 | } | |
244 | ||
245 | if (!opt_session_name) { | |
246 | session_name = get_session_name(); | |
cd9adb8b | 247 | if (session_name == nullptr) { |
eded6438 JD |
248 | ret = CMD_ERROR; |
249 | goto end; | |
250 | } | |
251 | } else { | |
252 | session_name = opt_session_name; | |
253 | } | |
254 | ||
255 | command_ret = handle_command(poptGetArgs(pc)); | |
256 | if (command_ret) { | |
fcf3687f | 257 | success = 0; |
eded6438 JD |
258 | } |
259 | ||
260 | if (lttng_opt_mi) { | |
261 | /* Close output element */ | |
262 | ret = mi_lttng_writer_close_element(writer); | |
263 | if (ret) { | |
264 | ret = CMD_ERROR; | |
265 | goto end; | |
266 | } | |
267 | ||
268 | /* Success ? */ | |
28ab034a JG |
269 | ret = mi_lttng_writer_write_element_bool( |
270 | writer, mi_lttng_element_command_success, success); | |
eded6438 JD |
271 | if (ret) { |
272 | ret = CMD_ERROR; | |
273 | goto end; | |
274 | } | |
275 | ||
276 | /* Command element close */ | |
277 | ret = mi_lttng_writer_command_close(writer); | |
278 | if (ret) { | |
279 | ret = CMD_ERROR; | |
280 | goto end; | |
281 | } | |
282 | } | |
283 | ||
284 | end: | |
285 | /* Mi clean-up */ | |
286 | if (writer && mi_lttng_writer_destroy(writer)) { | |
287 | /* Preserve original error code */ | |
288 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
289 | } | |
290 | ||
291 | if (!opt_session_name) { | |
292 | free(session_name); | |
293 | } | |
294 | ||
295 | /* Overwrite ret if an error occurred during handle_command() */ | |
296 | ret = command_ret ? command_ret : ret; | |
297 | ||
298 | poptFreeContext(pc); | |
299 | return ret; | |
300 | } |