2 * Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
20 static char *opt_session_name
;
21 static char *session_name
= nullptr;
23 static int regenerate_metadata(int argc
, const char **argv
);
24 static int regenerate_statedump(int argc
, const char **argv
);
26 #ifdef LTTNG_EMBED_HELP
27 static const char help_msg
[] =
28 #include <lttng-regenerate.1.h>
38 static struct mi_writer
*writer
;
40 static struct poptOption long_options
[] = {
41 /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */
51 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, nullptr, nullptr },
61 { "list-commands", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_COMMANDS
, nullptr, nullptr },
73 static struct cmd_struct actions
[] = {
74 { "metadata", regenerate_metadata
},
75 { "statedump", regenerate_statedump
},
76 { nullptr, nullptr } /* Array closure */
80 * Count and return the number of arguments in argv.
82 static int count_arguments(const char **argv
)
88 while (argv
[i
] != nullptr) {
95 static int regenerate_metadata(int argc
, const char **argv
__attribute__((unused
)))
103 ret
= lttng_regenerate_metadata(session_name
);
105 MSG("Metadata successfully regenerated for session %s", session_name
);
107 ERR("%s", lttng_strerror(ret
));
114 static int regenerate_statedump(int argc
, const char **argv
__attribute__((unused
)))
119 ret
= -LTTNG_ERR_INVALID
;
122 ret
= lttng_regenerate_statedump(session_name
);
124 MSG("State dump successfully regenerated for session %s", session_name
);
131 static int handle_command(const char **argv
)
133 struct cmd_struct
*cmd
;
134 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
136 if (argv
== nullptr) {
137 ERR("No object specified for regenerate command.");
138 command_ret
= CMD_ERROR
;
142 argc
= count_arguments(argv
);
143 LTTNG_ASSERT(argc
>= 1);
146 while (cmd
->func
!= nullptr) {
148 if (strcmp(argv
[0], cmd
->name
) == 0) {
151 ret
= mi_lttng_writer_open_element(
152 writer
, mi_lttng_element_command_regenerate_action
);
158 /* Name of the action */
159 ret
= mi_lttng_writer_write_element_string(
160 writer
, config_element_name
, argv
[0]);
166 command_ret
= cmd
->func(argc
, argv
);
168 /* Close output and action element */
169 ret
= mi_lttng_writer_close_element(writer
);
184 /* Overwrite ret if an error occurred in cmd->func() */
185 ret
= command_ret
? command_ret
: ret
;
190 * regenerate command handling.
192 int cmd_regenerate(int argc
, const char **argv
)
194 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
195 static poptContext pc
;
203 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
204 poptReadDefaultConfig(pc
, 0);
207 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
209 ret
= -LTTNG_ERR_NOMEM
;
212 /* Open command element */
213 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_regenerate
);
219 /* Open output element */
220 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
227 while ((opt
= poptGetNextOpt(pc
)) != -1) {
232 case OPT_LIST_OPTIONS
:
233 list_cmd_options(stdout
, long_options
);
235 case OPT_LIST_COMMANDS
:
236 list_commands(actions
, stdout
);
245 if (!opt_session_name
) {
246 session_name
= get_session_name();
247 if (session_name
== nullptr) {
252 session_name
= opt_session_name
;
255 command_ret
= handle_command(poptGetArgs(pc
));
261 /* Close output element */
262 ret
= mi_lttng_writer_close_element(writer
);
269 ret
= mi_lttng_writer_write_element_bool(
270 writer
, mi_lttng_element_command_success
, success
);
276 /* Command element close */
277 ret
= mi_lttng_writer_command_close(writer
);
286 if (writer
&& mi_lttng_writer_destroy(writer
)) {
287 /* Preserve original error code */
288 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
291 if (!opt_session_name
) {
295 /* Overwrite ret if an error occurred during handle_command() */
296 ret
= command_ret
? command_ret
: ret
;