Add --omit-name and --omit-output options to the save command
[lttng-tools.git] / src / bin / lttng / commands / save.c
CommitLineData
c864d6d7
JG
1/*
2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
6c1c0768 18#define _LGPL_SOURCE
c864d6d7
JG
19#include <inttypes.h>
20#include <popt.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
25
1734c658
JRJ
26#include <common/mi-lttng.h>
27
c864d6d7
JG
28#include "../command.h"
29#include <lttng/save.h>
30
31static char *opt_output_path;
c431d5e4
JG
32static bool opt_force;
33static bool opt_save_all;
7f66d5e7
JG
34static bool opt_omit_name;
35static bool opt_omit_output;
b04d42ec 36static struct mi_writer *writer;
c864d6d7
JG
37
38enum {
39 OPT_HELP = 1,
40 OPT_ALL,
41 OPT_FORCE,
13a810d5 42 OPT_LIST_OPTIONS,
7f66d5e7
JG
43 OPT_OMIT_NAME,
44 OPT_OMIT_OUTPUT,
c864d6d7
JG
45};
46
47static struct poptOption save_opts[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
dff33dda
JG
49 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
50 {"all", 'a', POPT_ARG_NONE, NULL, OPT_ALL, NULL, NULL},
51 {"output-path", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
52 {"force", 'f', POPT_ARG_NONE, NULL, OPT_FORCE, NULL, NULL},
13a810d5 53 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
7f66d5e7
JG
54 {"omit-name", 0, POPT_ARG_NONE, NULL, OPT_OMIT_NAME, NULL, NULL},
55 {"omit-output", 0, POPT_ARG_NONE, NULL, OPT_OMIT_OUTPUT, NULL, NULL},
c864d6d7
JG
56 {0, 0, 0, 0, 0, 0, 0}
57};
58
1734c658
JRJ
59static int mi_partial_session(const char *session_name)
60{
61 int ret;
62 assert(writer);
63 assert(session_name);
64
65 /* Open session element */
66 ret = mi_lttng_writer_open_element(writer, config_element_session);
67 if (ret) {
68 goto end;
69 }
70
71 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
72 session_name);
73 if (ret) {
74 goto end;
75 }
76
77 /* Closing session element */
78 ret = mi_lttng_writer_close_element(writer);
79end:
80 return ret;
81}
82
83/*
84 * Mi print of save command
85 */
86static int mi_save_print(const char *session_name)
87{
88 int ret;
89 assert(writer);
90
91 if (opt_save_all) {
92 /* We use a wildcard to represent all sessions */
93 session_name = "*";
94 }
95
96 /* Print save element */
97 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_save);
98 if (ret) {
99 goto end;
100 }
101
102 /* Print session element */
103 ret = mi_partial_session(session_name);
104 if (ret) {
105 goto end;
106 }
107
108 /* Path element */
109 if (opt_output_path) {
110 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
111 opt_output_path);
112 if (ret) {
113 goto end;
114 }
115 }
116
7f66d5e7
JG
117 /* Omit session name. */
118 if (opt_omit_name) {
119 /* Only print if true; assume default value otherwise. */
120 ret = mi_lttng_writer_write_element_bool(writer,
121 config_element_omit_name, opt_omit_name);
122 if (ret) {
123 goto end;
124 }
125 }
126
127 /* Omit session output. */
128 if (opt_omit_output) {
129 /* Only print if true; assume default value otherwise. */
130 ret = mi_lttng_writer_write_element_bool(writer,
131 config_element_omit_output, opt_omit_output);
132 if (ret) {
133 goto end;
134 }
135 }
136
1734c658
JRJ
137 /* Close save element */
138 ret = mi_lttng_writer_close_element(writer);
139end:
140 return ret;
141}
142
c864d6d7
JG
143/*
144 * The 'save <options>' first level command
145 */
146int cmd_save(int argc, const char **argv)
147{
1734c658 148 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
c864d6d7
JG
149 int opt;
150 const char *session_name = NULL;
151 poptContext pc;
152 struct lttng_save_session_attr *attr;
153
154 pc = poptGetContext(NULL, argc, argv, save_opts, 0);
155 poptReadDefaultConfig(pc, 0);
156
157 while ((opt = poptGetNextOpt(pc)) != -1) {
158 switch (opt) {
159 case OPT_HELP:
4ba92f18 160 SHOW_HELP();
c864d6d7
JG
161 goto end;
162 case OPT_ALL:
c431d5e4 163 opt_save_all = true;
c864d6d7
JG
164 break;
165 case OPT_FORCE:
c431d5e4 166 opt_force = true;
c864d6d7 167 break;
13a810d5
DG
168 case OPT_LIST_OPTIONS:
169 list_cmd_options(stdout, save_opts);
170 goto end;
7f66d5e7
JG
171 case OPT_OMIT_NAME:
172 opt_omit_name = true;
173 break;
174 case OPT_OMIT_OUTPUT:
175 opt_omit_output = true;
176 break;
c864d6d7 177 default:
c864d6d7
JG
178 ret = CMD_UNDEFINED;
179 goto end;
180 }
181 }
182
183 if (!opt_save_all) {
184 session_name = poptGetArg(pc);
59b41aa2
JRJ
185 if (session_name) {
186 DBG2("Session name: %s", session_name);
1734c658
JRJ
187 } else {
188 /* default to opt_save_all */
c431d5e4 189 opt_save_all = true;
c864d6d7 190 }
c864d6d7
JG
191 }
192
193 attr = lttng_save_session_attr_create();
194 if (!attr) {
195 ret = CMD_FATAL;
196 goto end_destroy;
197 }
198
199 if (lttng_save_session_attr_set_session_name(attr, session_name)) {
200 ret = CMD_ERROR;
201 goto end_destroy;
202 }
203
204 if (lttng_save_session_attr_set_overwrite(attr, opt_force)) {
205 ret = CMD_ERROR;
206 goto end_destroy;
207 }
208
ab85fc7f 209 if (lttng_save_session_attr_set_output_url(attr, opt_output_path)) {
c864d6d7
JG
210 ret = CMD_ERROR;
211 goto end_destroy;
212 }
213
7f66d5e7
JG
214 if (opt_omit_name) {
215 if (lttng_save_session_attr_set_omit_name(attr, true)) {
216 ret = CMD_ERROR;
217 goto end_destroy;
218 }
219 }
220
221 if (opt_omit_output) {
222 if (lttng_save_session_attr_set_omit_output(attr, true)) {
223 ret = CMD_ERROR;
224 goto end_destroy;
225 }
226 }
227
1734c658
JRJ
228 /* Mi check */
229 if (lttng_opt_mi) {
230 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
231 if (!writer) {
232 ret = -LTTNG_ERR_NOMEM;
233 goto end_destroy;
234 }
235
236 /* Open command element */
237 ret = mi_lttng_writer_command_open(writer,
238 mi_lttng_element_command_save);
239 if (ret) {
240 ret = CMD_ERROR;
241 goto end_destroy;
242 }
243
244 /* Open output element */
245 ret = mi_lttng_writer_open_element(writer,
246 mi_lttng_element_command_output);
247 if (ret) {
248 ret = CMD_ERROR;
249 goto end_destroy;
250 }
251 }
252
253 command_ret = lttng_save_session(attr);
254 if (command_ret < 0) {
255 ERR("%s", lttng_strerror(command_ret));
256 success = 0;
ab85fc7f
DG
257 } else {
258 /* Inform the user of what just happened on success. */
259 if (session_name && opt_output_path) {
260 MSG("Session %s saved successfully in %s.", session_name,
261 opt_output_path);
262 } else if (session_name && !opt_output_path) {
263 MSG("Session %s saved successfully.", session_name);
264 } else if (!session_name && opt_output_path) {
265 MSG("All sessions have been saved successfully in %s.",
266 opt_output_path);
267 } else {
268 MSG("All sessions have been saved successfully.");
c864d6d7 269 }
1734c658
JRJ
270 success = 1;
271 }
272
273 /* Mi Printing and closing */
274 if (lttng_opt_mi) {
275 /* Mi print */
276 ret = mi_save_print(session_name);
277 if (ret) {
278 ret = CMD_ERROR;
279 goto end_destroy;
280 }
281
282 /* Close output element */
283 ret = mi_lttng_writer_close_element(writer);
284 if (ret) {
285 ret = CMD_ERROR;
286 goto end_destroy;
287 }
288
289 /* Success ? */
290 ret = mi_lttng_writer_write_element_bool(writer,
291 mi_lttng_element_command_success, success);
292 if (ret) {
293 ret = CMD_ERROR;
294 goto end_destroy;
295 }
296
297 /* Command element close */
298 ret = mi_lttng_writer_command_close(writer);
299 if (ret) {
300 ret = CMD_ERROR;
301 goto end_destroy;
302 }
c864d6d7
JG
303 }
304end_destroy:
305 lttng_save_session_attr_destroy(attr);
306end:
1734c658
JRJ
307 /* Mi clean-up */
308 if (writer && mi_lttng_writer_destroy(writer)) {
309 /* Preserve original error code */
310 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
311 }
312
313 /* Overwrite ret if command failed */
314 ret = command_ret ? -command_ret : ret;
315
c864d6d7
JG
316 poptFreeContext(pc);
317 return ret;
318}
This page took 0.051985 seconds and 5 git commands to generate.