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