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