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