2 * Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
17 #include <common/mi-lttng.h>
19 #include "../command.h"
21 static char *opt_session_name
;
22 static char *session_name
= NULL
;
24 static int regenerate_metadata(int argc
, const char **argv
);
25 static int regenerate_statedump(int argc
, const char **argv
);
27 #ifdef LTTNG_EMBED_HELP
28 static const char help_msg
[] =
29 #include <lttng-regenerate.1.h>
39 static struct mi_writer
*writer
;
41 static struct poptOption long_options
[] = {
42 /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */
43 { "help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0, },
44 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
45 { "list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, 0, 0, },
46 { "list-commands", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_COMMANDS
},
47 { 0, 0, 0, 0, 0, 0, 0, },
50 static struct cmd_struct actions
[] = {
51 { "metadata", regenerate_metadata
},
52 { "statedump", regenerate_statedump
},
53 { NULL
, NULL
} /* Array closure */
57 * Count and return the number of arguments in argv.
59 static int count_arguments(const char **argv
)
65 while (argv
[i
] != NULL
) {
72 static int regenerate_metadata(int argc
, const char **argv
)
80 ret
= lttng_regenerate_metadata(session_name
);
82 MSG("Metadata successfully regenerated for session %s", session_name
);
84 ERR("%s", lttng_strerror(ret
));
91 static int regenerate_statedump(int argc
, const char **argv
)
96 ret
= -LTTNG_ERR_INVALID
;
99 ret
= lttng_regenerate_statedump(session_name
);
101 MSG("State dump successfully regenerated for session %s", session_name
);
108 static int handle_command(const char **argv
)
110 struct cmd_struct
*cmd
;
111 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
114 ERR("No object specified for regenerate command.");
115 command_ret
= CMD_ERROR
;
119 argc
= count_arguments(argv
);
123 while (cmd
->func
!= NULL
) {
125 if (strcmp(argv
[0], cmd
->name
) == 0) {
128 ret
= mi_lttng_writer_open_element(writer
,
129 mi_lttng_element_command_regenerate_action
);
135 /* Name of the action */
136 ret
= mi_lttng_writer_write_element_string(writer
,
137 config_element_name
, argv
[0]);
143 command_ret
= cmd
->func(argc
, argv
);
145 /* Close output and action element */
146 ret
= mi_lttng_writer_close_element(writer
);
161 /* Overwrite ret if an error occurred in cmd->func() */
162 ret
= command_ret
? command_ret
: ret
;
167 * regenerate command handling.
169 int cmd_regenerate(int argc
, const char **argv
)
171 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
172 static poptContext pc
;
180 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
181 poptReadDefaultConfig(pc
, 0);
184 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
186 ret
= -LTTNG_ERR_NOMEM
;
189 /* Open command element */
190 ret
= mi_lttng_writer_command_open(writer
,
191 mi_lttng_element_command_regenerate
);
197 /* Open output element */
198 ret
= mi_lttng_writer_open_element(writer
,
199 mi_lttng_element_command_output
);
206 while ((opt
= poptGetNextOpt(pc
)) != -1) {
211 case OPT_LIST_OPTIONS
:
212 list_cmd_options(stdout
, long_options
);
214 case OPT_LIST_COMMANDS
:
215 list_commands(actions
, stdout
);
224 if (!opt_session_name
) {
225 session_name
= get_session_name();
226 if (session_name
== NULL
) {
231 session_name
= opt_session_name
;
234 command_ret
= handle_command(poptGetArgs(pc
));
240 /* Close output element */
241 ret
= mi_lttng_writer_close_element(writer
);
248 ret
= mi_lttng_writer_write_element_bool(writer
,
249 mi_lttng_element_command_success
, success
);
255 /* Command element close */
256 ret
= mi_lttng_writer_command_close(writer
);
265 if (writer
&& mi_lttng_writer_destroy(writer
)) {
266 /* Preserve original error code */
267 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
270 if (!opt_session_name
) {
274 /* Overwrite ret if an error occurred during handle_command() */
275 ret
= command_ret
? command_ret
: ret
;