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