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 */
59 * Count and return the number of arguments in argv.
61 static int count_arguments(const char **argv
)
67 while (argv
[i
] != NULL
) {
74 static int metadata_regenerate(int argc
, const char **argv
)
79 ret
= -LTTNG_ERR_INVALID
;
82 ret
= lttng_metadata_regenerate(session_name
);
84 MSG("Metadata successfully regenerated for session %s", session_name
);
91 static int handle_command(const char **argv
)
93 struct cmd_struct
*cmd
;
94 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
98 command_ret
= CMD_ERROR
;
102 argc
= count_arguments(argv
);
105 while (cmd
->func
!= NULL
) {
107 if (strcmp(argv
[0], cmd
->name
) == 0) {
110 ret
= mi_lttng_writer_open_element(writer
,
111 mi_lttng_element_command_metadata_action
);
117 /* Name of the action */
118 ret
= mi_lttng_writer_write_element_string(writer
,
119 config_element_name
, argv
[0]);
125 command_ret
= cmd
->func(argc
, argv
);
127 /* Close output and action element */
128 ret
= mi_lttng_writer_close_element(writer
);
143 /* Overwrite ret if an error occurred in cmd->func() */
144 ret
= command_ret
? command_ret
: ret
;
149 * Metadata command handling.
151 int cmd_metadata(int argc
, const char **argv
)
153 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
154 static poptContext pc
;
162 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
163 poptReadDefaultConfig(pc
, 0);
166 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
168 ret
= -LTTNG_ERR_NOMEM
;
171 /* Open command element */
172 ret
= mi_lttng_writer_command_open(writer
,
173 mi_lttng_element_command_metadata
);
179 /* Open output element */
180 ret
= mi_lttng_writer_open_element(writer
,
181 mi_lttng_element_command_output
);
188 while ((opt
= poptGetNextOpt(pc
)) != -1) {
193 case OPT_LIST_OPTIONS
:
194 list_cmd_options(stdout
, long_options
);
196 case OPT_LIST_COMMANDS
:
197 list_commands(actions
, stdout
);
206 if (!opt_session_name
) {
207 session_name
= get_session_name();
208 if (session_name
== NULL
) {
213 session_name
= opt_session_name
;
216 command_ret
= handle_command(poptGetArgs(pc
));
218 switch (-command_ret
) {
220 ERR("%s", lttng_strerror(command_ret
));
227 /* Close output element */
228 ret
= mi_lttng_writer_close_element(writer
);
235 ret
= mi_lttng_writer_write_element_bool(writer
,
236 mi_lttng_element_command_success
, success
);
242 /* Command element close */
243 ret
= mi_lttng_writer_command_close(writer
);
252 if (writer
&& mi_lttng_writer_destroy(writer
)) {
253 /* Preserve original error code */
254 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
257 if (!opt_session_name
) {
261 /* Overwrite ret if an error occurred during handle_command() */
262 ret
= command_ret
? command_ret
: ret
;