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