Fix tests: link libpause_consumer on liblttng-ctl
[lttng-tools.git] / src / bin / lttng / commands / regenerate.c
CommitLineData
eded6438
JD
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
31static char *opt_session_name;
32static char *session_name = NULL;
33
34static int regenerate_metadata(int argc, const char **argv);
c2561365 35static int regenerate_statedump(int argc, const char **argv);
eded6438 36
4fc83d94
PP
37#ifdef LTTNG_EMBED_HELP
38static const char help_msg[] =
39#include <lttng-regenerate.1.h>
40;
41#endif
42
eded6438
JD
43enum {
44 OPT_HELP = 1,
45 OPT_LIST_OPTIONS,
46 OPT_LIST_COMMANDS,
47};
48
49static struct mi_writer *writer;
50
51static struct poptOption long_options[] = {
52 /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */
53 { "help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0, },
54 { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
55 { "list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, 0, 0, },
56 { "list-commands", 0, POPT_ARG_NONE, NULL, OPT_LIST_COMMANDS},
57 { 0, 0, 0, 0, 0, 0, 0, },
58};
59
60static struct cmd_struct actions[] = {
61 { "metadata", regenerate_metadata },
c2561365 62 { "statedump", regenerate_statedump },
eded6438
JD
63 { NULL, NULL } /* Array closure */
64};
65
66/*
67 * Count and return the number of arguments in argv.
68 */
69static int count_arguments(const char **argv)
70{
71 int i = 0;
72
73 assert(argv);
74
75 while (argv[i] != NULL) {
76 i++;
77 }
78
79 return i;
80}
81
82static int regenerate_metadata(int argc, const char **argv)
83{
84 int ret;
85
86 if (argc > 1) {
fcf3687f 87 ret = CMD_UNDEFINED;
eded6438
JD
88 goto end;
89 }
90 ret = lttng_regenerate_metadata(session_name);
91 if (ret == 0) {
92 MSG("Metadata successfully regenerated for session %s", session_name);
fcf3687f
JG
93 } else {
94 ERR("%s", lttng_strerror(ret));
eded6438
JD
95 }
96
97end:
98 return ret;
99}
100
c2561365
JD
101static int regenerate_statedump(int argc, const char **argv)
102{
103 int ret;
104
105 if (argc > 1) {
106 ret = -LTTNG_ERR_INVALID;
107 goto end;
108 }
109 ret = lttng_regenerate_statedump(session_name);
110 if (ret == 0) {
111 MSG("State dump successfully regenerated for session %s", session_name);
112 }
113
114end:
115 return ret;
116}
117
eded6438
JD
118static int handle_command(const char **argv)
119{
120 struct cmd_struct *cmd;
121 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
122
123 if (argv == NULL) {
fcf3687f 124 ERR("No object specified for regenerate command.");
eded6438
JD
125 command_ret = CMD_ERROR;
126 goto end;
127 }
128
129 argc = count_arguments(argv);
130
131 cmd = &actions[i];
132 while (cmd->func != NULL) {
133 /* Find command */
134 if (strcmp(argv[0], cmd->name) == 0) {
135 if (lttng_opt_mi) {
136 /* Action element */
137 ret = mi_lttng_writer_open_element(writer,
138 mi_lttng_element_command_regenerate_action);
139 if (ret) {
140 ret = CMD_ERROR;
141 goto end;
142 }
143
144 /* Name of the action */
145 ret = mi_lttng_writer_write_element_string(writer,
146 config_element_name, argv[0]);
147 if (ret) {
148 ret = CMD_ERROR;
149 goto end;
150 }
151 }
152 command_ret = cmd->func(argc, argv);
153 if (lttng_opt_mi) {
154 /* Close output and action element */
155 ret = mi_lttng_writer_close_element(writer);
156 if (ret) {
157 ret = CMD_ERROR;
158 goto end;
159 }
160 }
161 goto end;
162 }
163
164 cmd = &actions[i++];
165 }
166
167 ret = CMD_UNDEFINED;
168
169end:
170 /* Overwrite ret if an error occurred in cmd->func() */
171 ret = command_ret ? command_ret : ret;
172 return ret;
173}
174
175/*
176 * regenerate command handling.
177 */
178int cmd_regenerate(int argc, const char **argv)
179{
180 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
181 static poptContext pc;
182
183 if (argc < 1) {
184 SHOW_HELP();
185 ret = CMD_ERROR;
186 goto end;
187 }
188
189 pc = poptGetContext(NULL, argc, argv, long_options, 0);
190 poptReadDefaultConfig(pc, 0);
191
192 if (lttng_opt_mi) {
193 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
194 if (!writer) {
195 ret = -LTTNG_ERR_NOMEM;
196 goto end;
197 }
198 /* Open command element */
199 ret = mi_lttng_writer_command_open(writer,
200 mi_lttng_element_command_regenerate);
201 if (ret) {
202 ret = CMD_ERROR;
203 goto end;
204 }
205
206 /* Open output element */
207 ret = mi_lttng_writer_open_element(writer,
208 mi_lttng_element_command_output);
209 if (ret) {
210 ret = CMD_ERROR;
211 goto end;
212 }
213 }
214
215 while ((opt = poptGetNextOpt(pc)) != -1) {
216 switch (opt) {
217 case OPT_HELP:
218 SHOW_HELP();
219 goto end;
220 case OPT_LIST_OPTIONS:
221 list_cmd_options(stdout, long_options);
222 goto end;
223 case OPT_LIST_COMMANDS:
224 list_commands(actions, stdout);
225 goto end;
226 default:
227 SHOW_HELP();
228 ret = CMD_UNDEFINED;
229 goto end;
230 }
231 }
232
233 if (!opt_session_name) {
234 session_name = get_session_name();
235 if (session_name == NULL) {
236 ret = CMD_ERROR;
237 goto end;
238 }
239 } else {
240 session_name = opt_session_name;
241 }
242
243 command_ret = handle_command(poptGetArgs(pc));
244 if (command_ret) {
fcf3687f 245 success = 0;
eded6438
JD
246 }
247
248 if (lttng_opt_mi) {
249 /* Close output element */
250 ret = mi_lttng_writer_close_element(writer);
251 if (ret) {
252 ret = CMD_ERROR;
253 goto end;
254 }
255
256 /* Success ? */
257 ret = mi_lttng_writer_write_element_bool(writer,
258 mi_lttng_element_command_success, success);
259 if (ret) {
260 ret = CMD_ERROR;
261 goto end;
262 }
263
264 /* Command element close */
265 ret = mi_lttng_writer_command_close(writer);
266 if (ret) {
267 ret = CMD_ERROR;
268 goto end;
269 }
270 }
271
272end:
273 /* Mi clean-up */
274 if (writer && mi_lttng_writer_destroy(writer)) {
275 /* Preserve original error code */
276 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
277 }
278
279 if (!opt_session_name) {
280 free(session_name);
281 }
282
283 /* Overwrite ret if an error occurred during handle_command() */
284 ret = command_ret ? command_ret : ret;
285
286 poptFreeContext(pc);
287 return ret;
288}
This page took 0.038718 seconds and 4 git commands to generate.