2 * Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com>
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.
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.
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.
27 #include <common/mi-lttng.h>
29 #include "../command.h"
31 static char *opt_session_name
;
32 static char *session_name
= NULL
;
34 static int regenerate_metadata(int argc
, const char **argv
);
35 static int regenerate_statedump(int argc
, const char **argv
);
37 #ifdef LTTNG_EMBED_HELP
38 static const char help_msg
[] =
39 #include <lttng-regenerate.1.h>
49 static struct mi_writer
*writer
;
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, },
60 static struct cmd_struct actions
[] = {
61 { "metadata", regenerate_metadata
},
62 { "statedump", regenerate_statedump
},
63 { NULL
, NULL
} /* Array closure */
67 * Count and return the number of arguments in argv.
69 static int count_arguments(const char **argv
)
75 while (argv
[i
] != NULL
) {
82 static int regenerate_metadata(int argc
, const char **argv
)
87 ret
= -LTTNG_ERR_INVALID
;
90 ret
= lttng_regenerate_metadata(session_name
);
92 MSG("Metadata successfully regenerated for session %s", session_name
);
99 static int regenerate_statedump(int argc
, const char **argv
)
104 ret
= -LTTNG_ERR_INVALID
;
107 ret
= lttng_regenerate_statedump(session_name
);
109 MSG("State dump successfully regenerated for session %s", session_name
);
116 static int handle_command(const char **argv
)
118 struct cmd_struct
*cmd
;
119 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
123 command_ret
= CMD_ERROR
;
127 argc
= count_arguments(argv
);
130 while (cmd
->func
!= NULL
) {
132 if (strcmp(argv
[0], cmd
->name
) == 0) {
135 ret
= mi_lttng_writer_open_element(writer
,
136 mi_lttng_element_command_regenerate_action
);
142 /* Name of the action */
143 ret
= mi_lttng_writer_write_element_string(writer
,
144 config_element_name
, argv
[0]);
150 command_ret
= cmd
->func(argc
, argv
);
152 /* Close output and action element */
153 ret
= mi_lttng_writer_close_element(writer
);
168 /* Overwrite ret if an error occurred in cmd->func() */
169 ret
= command_ret
? command_ret
: ret
;
174 * regenerate command handling.
176 int cmd_regenerate(int argc
, const char **argv
)
178 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
179 static poptContext pc
;
187 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
188 poptReadDefaultConfig(pc
, 0);
191 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
193 ret
= -LTTNG_ERR_NOMEM
;
196 /* Open command element */
197 ret
= mi_lttng_writer_command_open(writer
,
198 mi_lttng_element_command_regenerate
);
204 /* Open output element */
205 ret
= mi_lttng_writer_open_element(writer
,
206 mi_lttng_element_command_output
);
213 while ((opt
= poptGetNextOpt(pc
)) != -1) {
218 case OPT_LIST_OPTIONS
:
219 list_cmd_options(stdout
, long_options
);
221 case OPT_LIST_COMMANDS
:
222 list_commands(actions
, stdout
);
231 if (!opt_session_name
) {
232 session_name
= get_session_name();
233 if (session_name
== NULL
) {
238 session_name
= opt_session_name
;
241 command_ret
= handle_command(poptGetArgs(pc
));
243 switch (-command_ret
) {
245 ERR("%s", lttng_strerror(command_ret
));
252 /* Close output element */
253 ret
= mi_lttng_writer_close_element(writer
);
260 ret
= mi_lttng_writer_write_element_bool(writer
,
261 mi_lttng_element_command_success
, success
);
267 /* Command element close */
268 ret
= mi_lttng_writer_command_close(writer
);
277 if (writer
&& mi_lttng_writer_destroy(writer
)) {
278 /* Preserve original error code */
279 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
282 if (!opt_session_name
) {
286 /* Overwrite ret if an error occurred during handle_command() */
287 ret
= command_ret
? command_ret
: ret
;