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