lttng: list: print `per-user` and `per-process` buffer types
[lttng-tools.git] / src / bin / lttng / commands / metadata.c
CommitLineData
93ec662e 1/*
ab5be9fa 2 * Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com>
93ec662e 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
93ec662e 5 *
93ec662e
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 metadata_regenerate(int argc, const char **argv);
25
4fc83d94
PP
26#ifdef LTTNG_EMBED_HELP
27static const char help_msg[] =
28#include <lttng-metadata.1.h>
29;
30#endif
31
93ec662e
JD
32enum {
33 OPT_HELP = 1,
34 OPT_LIST_OPTIONS,
35 OPT_LIST_COMMANDS,
36};
37
54897b57
JD
38static struct mi_writer *writer;
39
93ec662e
JD
40static 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
49static struct cmd_struct actions[] = {
50 { "regenerate", metadata_regenerate },
51 { NULL, NULL } /* Array closure */
52};
53
93ec662e
JD
54/*
55 * Count and return the number of arguments in argv.
56 */
57static int count_arguments(const char **argv)
58{
59 int i = 0;
60
61 assert(argv);
62
63 while (argv[i] != NULL) {
64 i++;
65 }
66
67 return i;
68}
69
70static int metadata_regenerate(int argc, const char **argv)
71{
72 int ret;
73
fb288d1e 74 if (argc > 1) {
c86592f2 75 ret = CMD_UNDEFINED;
fb288d1e
JD
76 goto end;
77 }
eded6438 78 ret = lttng_regenerate_metadata(session_name);
93ec662e
JD
79 if (ret == 0) {
80 MSG("Metadata successfully regenerated for session %s", session_name);
c86592f2
JG
81 } else {
82 ERR("%s", lttng_strerror(ret));
93ec662e 83 }
fb288d1e
JD
84
85end:
93ec662e
JD
86 return ret;
87}
88
89static 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) {
c86592f2
JG
95 ERR("No action specified for metadata command.");
96 ret = CMD_ERROR;
93ec662e
JD
97 goto end;
98 }
99
100 argc = count_arguments(argv);
9927c13b 101 assert(argc >= 1);
93ec662e
JD
102
103 cmd = &actions[i];
104 while (cmd->func != NULL) {
105 /* Find command */
106 if (strcmp(argv[0], cmd->name) == 0) {
54897b57
JD
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 }
93ec662e 124 command_ret = cmd->func(argc, argv);
54897b57
JD
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 }
93ec662e
JD
133 goto end;
134 }
135
136 cmd = &actions[i++];
137 }
138
139 ret = CMD_UNDEFINED;
140
141end:
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 */
150int cmd_metadata(int argc, const char **argv)
151{
54897b57 152 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
93ec662e
JD
153 static poptContext pc;
154
155 if (argc < 1) {
3b9c7d3f 156 SHOW_HELP();
93ec662e
JD
157 ret = CMD_ERROR;
158 goto end;
159 }
160
161 pc = poptGetContext(NULL, argc, argv, long_options, 0);
162 poptReadDefaultConfig(pc, 0);
163
54897b57
JD
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
93ec662e
JD
187 while ((opt = poptGetNextOpt(pc)) != -1) {
188 switch (opt) {
189 case OPT_HELP:
3b9c7d3f 190 SHOW_HELP();
93ec662e
JD
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:
3b9c7d3f 199 SHOW_HELP();
93ec662e
JD
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) {
c86592f2 217 success = 0;
93ec662e
JD
218 }
219
54897b57
JD
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
93ec662e 244end:
54897b57
JD
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
93ec662e
JD
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.044503 seconds and 4 git commands to generate.