2 * Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <common/mi-lttng.h>
29 #include "../command.h"
31 static char *opt_session_name
;
32 static char *session_name
= NULL
;
34 static int metadata_regenerate(int argc
, const char **argv
);
42 static struct mi_writer
*writer
;
44 static struct poptOption long_options
[] = {
45 /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */
46 { "help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0, },
47 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
48 { "list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, 0, 0, },
49 { "list-commands", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_COMMANDS
},
50 { 0, 0, 0, 0, 0, 0, 0, },
53 static struct cmd_struct actions
[] = {
54 { "regenerate", metadata_regenerate
},
55 { NULL
, NULL
} /* Array closure */
61 static void usage(FILE *ofp
)
63 fprintf(ofp
, "usage: lttng metadata [OPTION] ACTION\n");
65 fprintf(ofp
, "Actions:\n");
66 fprintf(ofp
, " regenerate\n");
67 fprintf(ofp
, " Regenerate and overwrite the metadata of the session.\n");
68 fprintf(ofp
, "Options:\n");
69 fprintf(ofp
, " -h, --help Show this help.\n");
70 fprintf(ofp
, " --list-options Simple listing of options.\n");
71 fprintf(ofp
, " -s, --session NAME Apply to session name.\n");
76 * Count and return the number of arguments in argv.
78 static int count_arguments(const char **argv
)
84 while (argv
[i
] != NULL
) {
91 static int metadata_regenerate(int argc
, const char **argv
)
95 ret
= lttng_metadata_regenerate(session_name
);
97 MSG("Metadata successfully regenerated for session %s", session_name
);
102 static int handle_command(const char **argv
)
104 struct cmd_struct
*cmd
;
105 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
109 command_ret
= CMD_ERROR
;
113 argc
= count_arguments(argv
);
116 while (cmd
->func
!= NULL
) {
118 if (strcmp(argv
[0], cmd
->name
) == 0) {
121 ret
= mi_lttng_writer_open_element(writer
,
122 mi_lttng_element_command_metadata_action
);
128 /* Name of the action */
129 ret
= mi_lttng_writer_write_element_string(writer
,
130 config_element_name
, argv
[0]);
136 command_ret
= cmd
->func(argc
, argv
);
138 /* Close output and action element */
139 ret
= mi_lttng_writer_close_element(writer
);
154 /* Overwrite ret if an error occurred in cmd->func() */
155 ret
= command_ret
? command_ret
: ret
;
160 * Metadata command handling.
162 int cmd_metadata(int argc
, const char **argv
)
164 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
165 static poptContext pc
;
173 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
174 poptReadDefaultConfig(pc
, 0);
177 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
179 ret
= -LTTNG_ERR_NOMEM
;
182 /* Open command element */
183 ret
= mi_lttng_writer_command_open(writer
,
184 mi_lttng_element_command_metadata
);
190 /* Open output element */
191 ret
= mi_lttng_writer_open_element(writer
,
192 mi_lttng_element_command_output
);
199 while ((opt
= poptGetNextOpt(pc
)) != -1) {
204 case OPT_LIST_OPTIONS
:
205 list_cmd_options(stdout
, long_options
);
207 case OPT_LIST_COMMANDS
:
208 list_commands(actions
, stdout
);
217 if (!opt_session_name
) {
218 session_name
= get_session_name();
219 if (session_name
== NULL
) {
224 session_name
= opt_session_name
;
227 command_ret
= handle_command(poptGetArgs(pc
));
229 switch (-command_ret
) {
231 ERR("%s", lttng_strerror(command_ret
));
238 /* Close output element */
239 ret
= mi_lttng_writer_close_element(writer
);
246 ret
= mi_lttng_writer_write_element_bool(writer
,
247 mi_lttng_element_command_success
, success
);
253 /* Command element close */
254 ret
= mi_lttng_writer_command_close(writer
);
263 if (writer
&& mi_lttng_writer_destroy(writer
)) {
264 /* Preserve original error code */
265 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
268 if (!opt_session_name
) {
272 /* Overwrite ret if an error occurred during handle_command() */
273 ret
= command_ret
? command_ret
: ret
;