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
},
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
, const char **argv
)
77 ret
= lttng_regenerate_metadata(session_name
);
79 MSG("Metadata successfully regenerated for session %s", session_name
);
81 ERR("%s", lttng_strerror(ret
));
88 static int handle_command(const char **argv
)
90 struct cmd_struct
*cmd
;
91 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
94 ERR("No action specified for metadata command.");
99 argc
= count_arguments(argv
);
100 LTTNG_ASSERT(argc
>= 1);
103 while (cmd
->func
!= NULL
) {
105 if (strcmp(argv
[0], cmd
->name
) == 0) {
108 ret
= mi_lttng_writer_open_element(writer
,
109 mi_lttng_element_command_metadata_action
);
115 /* Name of the action */
116 ret
= mi_lttng_writer_write_element_string(writer
,
117 config_element_name
, argv
[0]);
123 command_ret
= cmd
->func(argc
, argv
);
125 /* Close output and action element */
126 ret
= mi_lttng_writer_close_element(writer
);
141 /* Overwrite ret if an error occurred in cmd->func() */
142 ret
= command_ret
? command_ret
: ret
;
147 * Metadata command handling.
149 int cmd_metadata(int argc
, const char **argv
)
151 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
152 static poptContext pc
;
160 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
161 poptReadDefaultConfig(pc
, 0);
164 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
166 ret
= -LTTNG_ERR_NOMEM
;
169 /* Open command element */
170 ret
= mi_lttng_writer_command_open(writer
,
171 mi_lttng_element_command_metadata
);
177 /* Open output element */
178 ret
= mi_lttng_writer_open_element(writer
,
179 mi_lttng_element_command_output
);
186 while ((opt
= poptGetNextOpt(pc
)) != -1) {
191 case OPT_LIST_OPTIONS
:
192 list_cmd_options(stdout
, long_options
);
194 case OPT_LIST_COMMANDS
:
195 list_commands(actions
, stdout
);
204 if (!opt_session_name
) {
205 session_name
= get_session_name();
206 if (session_name
== NULL
) {
211 session_name
= opt_session_name
;
214 command_ret
= handle_command(poptGetArgs(pc
));
220 /* Close output element */
221 ret
= mi_lttng_writer_close_element(writer
);
228 ret
= mi_lttng_writer_write_element_bool(writer
,
229 mi_lttng_element_command_success
, success
);
235 /* Command element close */
236 ret
= mi_lttng_writer_command_close(writer
);
245 if (writer
&& mi_lttng_writer_destroy(writer
)) {
246 /* Preserve original error code */
247 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
250 if (!opt_session_name
) {
254 /* Overwrite ret if an error occurred during handle_command() */
255 ret
= command_ret
? command_ret
: ret
;