Show lttng-metadata man page in command's --help
[lttng-tools.git] / src / bin / lttng / commands / metadata.c
1 /*
2 * Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <ctype.h>
21 #include <popt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <common/mi-lttng.h>
28
29 #include "../command.h"
30
31 static char *opt_session_name;
32 static char *session_name = NULL;
33
34 static int metadata_regenerate(int argc, const char **argv);
35
36 enum {
37 OPT_HELP = 1,
38 OPT_LIST_OPTIONS,
39 OPT_LIST_COMMANDS,
40 };
41
42 static struct mi_writer *writer;
43
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, },
51 };
52
53 static struct cmd_struct actions[] = {
54 { "regenerate", metadata_regenerate },
55 { NULL, NULL } /* Array closure */
56 };
57
58 /*
59 * Count and return the number of arguments in argv.
60 */
61 static int count_arguments(const char **argv)
62 {
63 int i = 0;
64
65 assert(argv);
66
67 while (argv[i] != NULL) {
68 i++;
69 }
70
71 return i;
72 }
73
74 static int metadata_regenerate(int argc, const char **argv)
75 {
76 int ret;
77
78 ret = lttng_metadata_regenerate(session_name);
79 if (ret == 0) {
80 MSG("Metadata successfully regenerated for session %s", session_name);
81 }
82 return ret;
83 }
84
85 static int handle_command(const char **argv)
86 {
87 struct cmd_struct *cmd;
88 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
89
90 if (argv == NULL) {
91 SHOW_HELP();
92 command_ret = CMD_ERROR;
93 goto end;
94 }
95
96 argc = count_arguments(argv);
97
98 cmd = &actions[i];
99 while (cmd->func != NULL) {
100 /* Find command */
101 if (strcmp(argv[0], cmd->name) == 0) {
102 if (lttng_opt_mi) {
103 /* Action element */
104 ret = mi_lttng_writer_open_element(writer,
105 mi_lttng_element_command_metadata_action);
106 if (ret) {
107 ret = CMD_ERROR;
108 goto end;
109 }
110
111 /* Name of the action */
112 ret = mi_lttng_writer_write_element_string(writer,
113 config_element_name, argv[0]);
114 if (ret) {
115 ret = CMD_ERROR;
116 goto end;
117 }
118 }
119 command_ret = cmd->func(argc, argv);
120 if (lttng_opt_mi) {
121 /* Close output and action element */
122 ret = mi_lttng_writer_close_element(writer);
123 if (ret) {
124 ret = CMD_ERROR;
125 goto end;
126 }
127 }
128 goto end;
129 }
130
131 cmd = &actions[i++];
132 }
133
134 ret = CMD_UNDEFINED;
135
136 end:
137 /* Overwrite ret if an error occurred in cmd->func() */
138 ret = command_ret ? command_ret : ret;
139 return ret;
140 }
141
142 /*
143 * Metadata command handling.
144 */
145 int cmd_metadata(int argc, const char **argv)
146 {
147 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
148 static poptContext pc;
149
150 if (argc < 1) {
151 SHOW_HELP();
152 ret = CMD_ERROR;
153 goto end;
154 }
155
156 pc = poptGetContext(NULL, argc, argv, long_options, 0);
157 poptReadDefaultConfig(pc, 0);
158
159 if (lttng_opt_mi) {
160 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
161 if (!writer) {
162 ret = -LTTNG_ERR_NOMEM;
163 goto end;
164 }
165 /* Open command element */
166 ret = mi_lttng_writer_command_open(writer,
167 mi_lttng_element_command_metadata);
168 if (ret) {
169 ret = CMD_ERROR;
170 goto end;
171 }
172
173 /* Open output element */
174 ret = mi_lttng_writer_open_element(writer,
175 mi_lttng_element_command_output);
176 if (ret) {
177 ret = CMD_ERROR;
178 goto end;
179 }
180 }
181
182 while ((opt = poptGetNextOpt(pc)) != -1) {
183 switch (opt) {
184 case OPT_HELP:
185 SHOW_HELP();
186 goto end;
187 case OPT_LIST_OPTIONS:
188 list_cmd_options(stdout, long_options);
189 goto end;
190 case OPT_LIST_COMMANDS:
191 list_commands(actions, stdout);
192 goto end;
193 default:
194 SHOW_HELP();
195 ret = CMD_UNDEFINED;
196 goto end;
197 }
198 }
199
200 if (!opt_session_name) {
201 session_name = get_session_name();
202 if (session_name == NULL) {
203 ret = CMD_ERROR;
204 goto end;
205 }
206 } else {
207 session_name = opt_session_name;
208 }
209
210 command_ret = handle_command(poptGetArgs(pc));
211 if (command_ret) {
212 switch (-command_ret) {
213 default:
214 ERR("%s", lttng_strerror(command_ret));
215 success = 0;
216 break;
217 }
218 }
219
220 if (lttng_opt_mi) {
221 /* Close output element */
222 ret = mi_lttng_writer_close_element(writer);
223 if (ret) {
224 ret = CMD_ERROR;
225 goto end;
226 }
227
228 /* Success ? */
229 ret = mi_lttng_writer_write_element_bool(writer,
230 mi_lttng_element_command_success, success);
231 if (ret) {
232 ret = CMD_ERROR;
233 goto end;
234 }
235
236 /* Command element close */
237 ret = mi_lttng_writer_command_close(writer);
238 if (ret) {
239 ret = CMD_ERROR;
240 goto end;
241 }
242 }
243
244 end:
245 /* Mi clean-up */
246 if (writer && mi_lttng_writer_destroy(writer)) {
247 /* Preserve original error code */
248 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
249 }
250
251 if (!opt_session_name) {
252 free(session_name);
253 }
254
255 /* Overwrite ret if an error occurred during handle_command() */
256 ret = command_ret ? command_ret : ret;
257
258 poptFreeContext(pc);
259 return ret;
260 }
This page took 0.036488 seconds and 4 git commands to generate.