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 metadata_regenerate(int argc
, const char **argv
);
25 #ifdef LTTNG_EMBED_HELP
26 static const char help_msg
[] =
27 #include <lttng-metadata.1.h>
37 static struct mi_writer
*writer
;
39 static struct poptOption long_options
[] = {
40 /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */
41 { "help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0, },
42 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
43 { "list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, 0, 0, },
44 { "list-commands", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_COMMANDS
, NULL
, NULL
},
45 { 0, 0, 0, 0, 0, 0, 0, },
48 static struct cmd_struct actions
[] = {
49 { "regenerate", metadata_regenerate
},
50 { NULL
, NULL
} /* Array closure */
54 * Count and return the number of arguments in argv.
56 static int count_arguments(const char **argv
)
62 while (argv
[i
] != NULL
) {
69 static int metadata_regenerate(int argc
,
70 const char **argv
__attribute__((unused
)))
78 ret
= lttng_regenerate_metadata(session_name
);
80 MSG("Metadata successfully regenerated for session %s", session_name
);
82 ERR("%s", lttng_strerror(ret
));
89 static int handle_command(const char **argv
)
91 struct cmd_struct
*cmd
;
92 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
95 ERR("No action specified for metadata command.");
100 argc
= count_arguments(argv
);
101 LTTNG_ASSERT(argc
>= 1);
104 while (cmd
->func
!= NULL
) {
106 if (strcmp(argv
[0], cmd
->name
) == 0) {
109 ret
= mi_lttng_writer_open_element(writer
,
110 mi_lttng_element_command_metadata_action
);
116 /* Name of the action */
117 ret
= mi_lttng_writer_write_element_string(writer
,
118 config_element_name
, argv
[0]);
124 command_ret
= cmd
->func(argc
, argv
);
126 /* Close output and action element */
127 ret
= mi_lttng_writer_close_element(writer
);
142 /* Overwrite ret if an error occurred in cmd->func() */
143 ret
= command_ret
? command_ret
: ret
;
148 * Metadata command handling.
150 int cmd_metadata(int argc
, const char **argv
)
152 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
153 static poptContext pc
;
161 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
162 poptReadDefaultConfig(pc
, 0);
165 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
167 ret
= -LTTNG_ERR_NOMEM
;
170 /* Open command element */
171 ret
= mi_lttng_writer_command_open(writer
,
172 mi_lttng_element_command_metadata
);
178 /* Open output element */
179 ret
= mi_lttng_writer_open_element(writer
,
180 mi_lttng_element_command_output
);
187 while ((opt
= poptGetNextOpt(pc
)) != -1) {
192 case OPT_LIST_OPTIONS
:
193 list_cmd_options(stdout
, long_options
);
195 case OPT_LIST_COMMANDS
:
196 list_commands(actions
, stdout
);
205 if (!opt_session_name
) {
206 session_name
= get_session_name();
207 if (session_name
== NULL
) {
212 session_name
= opt_session_name
;
215 command_ret
= handle_command(poptGetArgs(pc
));
221 /* Close output element */
222 ret
= mi_lttng_writer_close_element(writer
);
229 ret
= mi_lttng_writer_write_element_bool(writer
,
230 mi_lttng_element_command_success
, success
);
236 /* Command element close */
237 ret
= mi_lttng_writer_command_close(writer
);
246 if (writer
&& mi_lttng_writer_destroy(writer
)) {
247 /* Preserve original error code */
248 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
251 if (!opt_session_name
) {
255 /* Overwrite ret if an error occurred during handle_command() */
256 ret
= command_ret
? command_ret
: ret
;