2 * Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
16 #include <common/mi-lttng.hpp>
18 #include "../command.hpp"
20 static char *opt_session_name
;
21 static char *session_name
= NULL
;
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, } */
42 { "help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0, },
43 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
44 { "list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, 0, 0, },
45 { "list-commands", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_COMMANDS
, NULL
, NULL
},
46 { 0, 0, 0, 0, 0, 0, 0, },
49 static struct cmd_struct actions
[] = {
50 { "metadata", regenerate_metadata
},
51 { "statedump", regenerate_statedump
},
52 { NULL
, NULL
} /* Array closure */
56 * Count and return the number of arguments in argv.
58 static int count_arguments(const char **argv
)
64 while (argv
[i
] != NULL
) {
71 static int regenerate_metadata(int argc
,
72 const char **argv
__attribute__((unused
)))
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
,
92 const char **argv
__attribute__((unused
)))
97 ret
= -LTTNG_ERR_INVALID
;
100 ret
= lttng_regenerate_statedump(session_name
);
102 MSG("State dump successfully regenerated for session %s", session_name
);
109 static int handle_command(const char **argv
)
111 struct cmd_struct
*cmd
;
112 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
115 ERR("No object specified for regenerate command.");
116 command_ret
= CMD_ERROR
;
120 argc
= count_arguments(argv
);
121 LTTNG_ASSERT(argc
>= 1);
124 while (cmd
->func
!= NULL
) {
126 if (strcmp(argv
[0], cmd
->name
) == 0) {
129 ret
= mi_lttng_writer_open_element(writer
,
130 mi_lttng_element_command_regenerate_action
);
136 /* Name of the action */
137 ret
= mi_lttng_writer_write_element_string(writer
,
138 config_element_name
, argv
[0]);
144 command_ret
= cmd
->func(argc
, argv
);
146 /* Close output and action element */
147 ret
= mi_lttng_writer_close_element(writer
);
162 /* Overwrite ret if an error occurred in cmd->func() */
163 ret
= command_ret
? command_ret
: ret
;
168 * regenerate command handling.
170 int cmd_regenerate(int argc
, const char **argv
)
172 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
173 static poptContext pc
;
181 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
182 poptReadDefaultConfig(pc
, 0);
185 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
187 ret
= -LTTNG_ERR_NOMEM
;
190 /* Open command element */
191 ret
= mi_lttng_writer_command_open(writer
,
192 mi_lttng_element_command_regenerate
);
198 /* Open output element */
199 ret
= mi_lttng_writer_open_element(writer
,
200 mi_lttng_element_command_output
);
207 while ((opt
= poptGetNextOpt(pc
)) != -1) {
212 case OPT_LIST_OPTIONS
:
213 list_cmd_options(stdout
, long_options
);
215 case OPT_LIST_COMMANDS
:
216 list_commands(actions
, stdout
);
225 if (!opt_session_name
) {
226 session_name
= get_session_name();
227 if (session_name
== NULL
) {
232 session_name
= opt_session_name
;
235 command_ret
= handle_command(poptGetArgs(pc
));
241 /* Close output element */
242 ret
= mi_lttng_writer_close_element(writer
);
249 ret
= mi_lttng_writer_write_element_bool(writer
,
250 mi_lttng_element_command_success
, success
);
256 /* Command element close */
257 ret
= mi_lttng_writer_command_close(writer
);
266 if (writer
&& mi_lttng_writer_destroy(writer
)) {
267 /* Preserve original error code */
268 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
271 if (!opt_session_name
) {
275 /* Overwrite ret if an error occurred during handle_command() */
276 ret
= command_ret
? command_ret
: ret
;