lttng: Add --glob option to lttng-destroy
[lttng-tools.git] / src / bin / lttng / commands / metadata.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 "../command.hpp"
10
11 #include <common/mi-lttng.hpp>
12
13 #include <ctype.h>
14 #include <popt.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 static char *opt_session_name;
21 static char *session_name = nullptr;
22
23 static int metadata_regenerate(int argc, const char **argv);
24
25 #ifdef LTTNG_EMBED_HELP
26 static const char help_msg[] =
27 #include <lttng-metadata.1.h>
28 ;
29 #endif
30
31 enum {
32 OPT_HELP = 1,
33 OPT_LIST_OPTIONS,
34 OPT_LIST_COMMANDS,
35 };
36
37 static struct mi_writer *writer;
38
39 static struct poptOption long_options[] = {
40 /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */
41 {
42 "help",
43 'h',
44 POPT_ARG_NONE,
45 nullptr,
46 OPT_HELP,
47 nullptr,
48 nullptr,
49 },
50 { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, nullptr, nullptr },
51 {
52 "list-options",
53 0,
54 POPT_ARG_NONE,
55 nullptr,
56 OPT_LIST_OPTIONS,
57 nullptr,
58 nullptr,
59 },
60 { "list-commands", 0, POPT_ARG_NONE, nullptr, OPT_LIST_COMMANDS, nullptr, nullptr },
61 {
62 nullptr,
63 0,
64 0,
65 nullptr,
66 0,
67 nullptr,
68 nullptr,
69 },
70 };
71
72 static struct cmd_struct actions[] = {
73 { "regenerate", metadata_regenerate }, { nullptr, nullptr } /* Array closure */
74 };
75
76 /*
77 * Count and return the number of arguments in argv.
78 */
79 static int count_arguments(const char **argv)
80 {
81 int i = 0;
82
83 LTTNG_ASSERT(argv);
84
85 while (argv[i] != nullptr) {
86 i++;
87 }
88
89 return i;
90 }
91
92 static int metadata_regenerate(int argc, const char **argv __attribute__((unused)))
93 {
94 int ret;
95
96 if (argc > 1) {
97 ret = CMD_UNDEFINED;
98 goto end;
99 }
100 ret = lttng_regenerate_metadata(session_name);
101 if (ret == 0) {
102 MSG("Metadata successfully regenerated for session %s", session_name);
103 } else {
104 ERR("%s", lttng_strerror(ret));
105 }
106
107 end:
108 return ret;
109 }
110
111 static int handle_command(const char **argv)
112 {
113 struct cmd_struct *cmd;
114 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
115
116 if (argv == nullptr) {
117 ERR("No action specified for metadata command.");
118 ret = CMD_ERROR;
119 goto end;
120 }
121
122 argc = count_arguments(argv);
123 LTTNG_ASSERT(argc >= 1);
124
125 cmd = &actions[i];
126 while (cmd->func != nullptr) {
127 /* Find command */
128 if (strcmp(argv[0], cmd->name) == 0) {
129 if (lttng_opt_mi) {
130 /* Action element */
131 ret = mi_lttng_writer_open_element(
132 writer, mi_lttng_element_command_metadata_action);
133 if (ret) {
134 ret = CMD_ERROR;
135 goto end;
136 }
137
138 /* Name of the action */
139 ret = mi_lttng_writer_write_element_string(
140 writer, config_element_name, argv[0]);
141 if (ret) {
142 ret = CMD_ERROR;
143 goto end;
144 }
145 }
146 command_ret = cmd->func(argc, argv);
147 if (lttng_opt_mi) {
148 /* Close output and action element */
149 ret = mi_lttng_writer_close_element(writer);
150 if (ret) {
151 ret = CMD_ERROR;
152 goto end;
153 }
154 }
155 goto end;
156 }
157
158 cmd = &actions[i++];
159 }
160
161 ret = CMD_UNDEFINED;
162
163 end:
164 /* Overwrite ret if an error occurred in cmd->func() */
165 ret = command_ret ? command_ret : ret;
166 return ret;
167 }
168
169 /*
170 * Metadata command handling.
171 */
172 int cmd_metadata(int argc, const char **argv)
173 {
174 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
175 static poptContext pc;
176
177 if (argc < 1) {
178 SHOW_HELP();
179 ret = CMD_ERROR;
180 goto end;
181 }
182
183 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
184 poptReadDefaultConfig(pc, 0);
185
186 if (lttng_opt_mi) {
187 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
188 if (!writer) {
189 ret = -LTTNG_ERR_NOMEM;
190 goto end;
191 }
192 /* Open command element */
193 ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_metadata);
194 if (ret) {
195 ret = CMD_ERROR;
196 goto end;
197 }
198
199 /* Open output element */
200 ret = mi_lttng_writer_open_element(writer, 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 == nullptr) {
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 success = 0;
238 }
239
240 if (lttng_opt_mi) {
241 /* Close output element */
242 ret = mi_lttng_writer_close_element(writer);
243 if (ret) {
244 ret = CMD_ERROR;
245 goto end;
246 }
247
248 /* Success ? */
249 ret = mi_lttng_writer_write_element_bool(
250 writer, mi_lttng_element_command_success, success);
251 if (ret) {
252 ret = CMD_ERROR;
253 goto end;
254 }
255
256 /* Command element close */
257 ret = mi_lttng_writer_command_close(writer);
258 if (ret) {
259 ret = CMD_ERROR;
260 goto end;
261 }
262 }
263
264 end:
265 /* Mi clean-up */
266 if (writer && mi_lttng_writer_destroy(writer)) {
267 /* Preserve original error code */
268 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
269 }
270
271 if (!opt_session_name) {
272 free(session_name);
273 }
274
275 /* Overwrite ret if an error occurred during handle_command() */
276 ret = command_ret ? command_ret : ret;
277
278 poptFreeContext(pc);
279 return ret;
280 }
This page took 0.034924 seconds and 4 git commands to generate.