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
);
36 #ifdef LTTNG_EMBED_HELP
37 static const char help_msg
[] =
38 #include <lttng-metadata.1.h>
48 static struct mi_writer
*writer
;
50 static struct poptOption long_options
[] = {
51 /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */
52 { "help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0, },
53 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
54 { "list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, 0, 0, },
55 { "list-commands", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_COMMANDS
},
56 { 0, 0, 0, 0, 0, 0, 0, },
59 static struct cmd_struct actions
[] = {
60 { "regenerate", metadata_regenerate
},
61 { NULL
, NULL
} /* Array closure */
65 * Count and return the number of arguments in argv.
67 static int count_arguments(const char **argv
)
73 while (argv
[i
] != NULL
) {
80 static int metadata_regenerate(int argc
, const char **argv
)
88 ret
= lttng_regenerate_metadata(session_name
);
90 MSG("Metadata successfully regenerated for session %s", session_name
);
92 ERR("%s", lttng_strerror(ret
));
99 static int handle_command(const char **argv
)
101 struct cmd_struct
*cmd
;
102 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
105 ERR("No action specified for metadata command.");
110 argc
= count_arguments(argv
);
114 while (cmd
->func
!= NULL
) {
116 if (strcmp(argv
[0], cmd
->name
) == 0) {
119 ret
= mi_lttng_writer_open_element(writer
,
120 mi_lttng_element_command_metadata_action
);
126 /* Name of the action */
127 ret
= mi_lttng_writer_write_element_string(writer
,
128 config_element_name
, argv
[0]);
134 command_ret
= cmd
->func(argc
, argv
);
136 /* Close output and action element */
137 ret
= mi_lttng_writer_close_element(writer
);
152 /* Overwrite ret if an error occurred in cmd->func() */
153 ret
= command_ret
? command_ret
: ret
;
158 * Metadata command handling.
160 int cmd_metadata(int argc
, const char **argv
)
162 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
163 static poptContext pc
;
171 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
172 poptReadDefaultConfig(pc
, 0);
175 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
177 ret
= -LTTNG_ERR_NOMEM
;
180 /* Open command element */
181 ret
= mi_lttng_writer_command_open(writer
,
182 mi_lttng_element_command_metadata
);
188 /* Open output element */
189 ret
= mi_lttng_writer_open_element(writer
,
190 mi_lttng_element_command_output
);
197 while ((opt
= poptGetNextOpt(pc
)) != -1) {
202 case OPT_LIST_OPTIONS
:
203 list_cmd_options(stdout
, long_options
);
205 case OPT_LIST_COMMANDS
:
206 list_commands(actions
, stdout
);
215 if (!opt_session_name
) {
216 session_name
= get_session_name();
217 if (session_name
== NULL
) {
222 session_name
= opt_session_name
;
225 command_ret
= handle_command(poptGetArgs(pc
));
231 /* Close output element */
232 ret
= mi_lttng_writer_close_element(writer
);
239 ret
= mi_lttng_writer_write_element_bool(writer
,
240 mi_lttng_element_command_success
, success
);
246 /* Command element close */
247 ret
= mi_lttng_writer_command_close(writer
);
256 if (writer
&& mi_lttng_writer_destroy(writer
)) {
257 /* Preserve original error code */
258 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
261 if (!opt_session_name
) {
265 /* Overwrite ret if an error occurred during handle_command() */
266 ret
= command_ret
? command_ret
: ret
;