2 * Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
16 #include <common/mi-lttng.h>
18 #include "../command.h"
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
},
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
, const char **argv
)
79 ret
= lttng_regenerate_metadata(session_name
);
81 MSG("Metadata successfully regenerated for session %s", session_name
);
83 ERR("%s", lttng_strerror(ret
));
90 static int regenerate_statedump(int argc
, const char **argv
)
95 ret
= -LTTNG_ERR_INVALID
;
98 ret
= lttng_regenerate_statedump(session_name
);
100 MSG("State dump successfully regenerated for session %s", session_name
);
107 static int handle_command(const char **argv
)
109 struct cmd_struct
*cmd
;
110 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
113 ERR("No object specified for regenerate command.");
114 command_ret
= CMD_ERROR
;
118 argc
= count_arguments(argv
);
119 LTTNG_ASSERT(argc
>= 1);
122 while (cmd
->func
!= NULL
) {
124 if (strcmp(argv
[0], cmd
->name
) == 0) {
127 ret
= mi_lttng_writer_open_element(writer
,
128 mi_lttng_element_command_regenerate_action
);
134 /* Name of the action */
135 ret
= mi_lttng_writer_write_element_string(writer
,
136 config_element_name
, argv
[0]);
142 command_ret
= cmd
->func(argc
, argv
);
144 /* Close output and action element */
145 ret
= mi_lttng_writer_close_element(writer
);
160 /* Overwrite ret if an error occurred in cmd->func() */
161 ret
= command_ret
? command_ret
: ret
;
166 * regenerate command handling.
168 int cmd_regenerate(int argc
, const char **argv
)
170 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
171 static poptContext pc
;
179 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
180 poptReadDefaultConfig(pc
, 0);
183 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
185 ret
= -LTTNG_ERR_NOMEM
;
188 /* Open command element */
189 ret
= mi_lttng_writer_command_open(writer
,
190 mi_lttng_element_command_regenerate
);
196 /* Open output element */
197 ret
= mi_lttng_writer_open_element(writer
,
198 mi_lttng_element_command_output
);
205 while ((opt
= poptGetNextOpt(pc
)) != -1) {
210 case OPT_LIST_OPTIONS
:
211 list_cmd_options(stdout
, long_options
);
213 case OPT_LIST_COMMANDS
:
214 list_commands(actions
, stdout
);
223 if (!opt_session_name
) {
224 session_name
= get_session_name();
225 if (session_name
== NULL
) {
230 session_name
= opt_session_name
;
233 command_ret
= handle_command(poptGetArgs(pc
));
239 /* Close output element */
240 ret
= mi_lttng_writer_close_element(writer
);
247 ret
= mi_lttng_writer_write_element_bool(writer
,
248 mi_lttng_element_command_success
, success
);
254 /* Command element close */
255 ret
= mi_lttng_writer_command_close(writer
);
264 if (writer
&& mi_lttng_writer_destroy(writer
)) {
265 /* Preserve original error code */
266 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
269 if (!opt_session_name
) {
273 /* Overwrite ret if an error occurred during handle_command() */
274 ret
= command_ret
? command_ret
: ret
;