Rename C++ header files to .hpp
[lttng-tools.git] / src / bin / lttng / commands / save.cpp
CommitLineData
c864d6d7 1/*
ab5be9fa 2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
c864d6d7 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
c864d6d7 5 *
c864d6d7
JG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
c864d6d7
JG
9#include <inttypes.h>
10#include <popt.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
c864d6d7 14
c9e313bc 15#include <common/mi-lttng.hpp>
1734c658 16
c9e313bc 17#include "../command.hpp"
050dd639 18#include <lttng/lttng.h>
c864d6d7
JG
19
20static char *opt_output_path;
c431d5e4
JG
21static bool opt_force;
22static bool opt_save_all;
b04d42ec 23static struct mi_writer *writer;
c864d6d7 24
4fc83d94
PP
25#ifdef LTTNG_EMBED_HELP
26static const char help_msg[] =
27#include <lttng-save.1.h>
28;
29#endif
30
c864d6d7
JG
31enum {
32 OPT_HELP = 1,
33 OPT_ALL,
34 OPT_FORCE,
13a810d5 35 OPT_LIST_OPTIONS,
c864d6d7
JG
36};
37
38static struct poptOption save_opts[] = {
39 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
dff33dda
JG
40 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
41 {"all", 'a', POPT_ARG_NONE, NULL, OPT_ALL, NULL, NULL},
42 {"output-path", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
43 {"force", 'f', POPT_ARG_NONE, NULL, OPT_FORCE, NULL, NULL},
13a810d5 44 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
c864d6d7
JG
45 {0, 0, 0, 0, 0, 0, 0}
46};
47
1734c658
JRJ
48static int mi_partial_session(const char *session_name)
49{
50 int ret;
a0377dfe
FD
51 LTTNG_ASSERT(writer);
52 LTTNG_ASSERT(session_name);
1734c658
JRJ
53
54 /* Open session element */
55 ret = mi_lttng_writer_open_element(writer, config_element_session);
56 if (ret) {
57 goto end;
58 }
59
60 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
61 session_name);
62 if (ret) {
63 goto end;
64 }
65
66 /* Closing session element */
67 ret = mi_lttng_writer_close_element(writer);
68end:
69 return ret;
70}
71
72/*
73 * Mi print of save command
74 */
75static int mi_save_print(const char *session_name)
76{
77 int ret;
a0377dfe 78 LTTNG_ASSERT(writer);
1734c658
JRJ
79
80 if (opt_save_all) {
81 /* We use a wildcard to represent all sessions */
82 session_name = "*";
83 }
84
85 /* Print save element */
86 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_save);
87 if (ret) {
88 goto end;
89 }
90
91 /* Print session element */
92 ret = mi_partial_session(session_name);
93 if (ret) {
94 goto end;
95 }
96
97 /* Path element */
98 if (opt_output_path) {
99 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
100 opt_output_path);
101 if (ret) {
102 goto end;
103 }
104 }
105
106 /* Close save element */
107 ret = mi_lttng_writer_close_element(writer);
108end:
109 return ret;
110}
111
c864d6d7
JG
112/*
113 * The 'save <options>' first level command
114 */
115int cmd_save(int argc, const char **argv)
116{
1734c658 117 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
c864d6d7 118 int opt;
68c7f6e5 119 const char *session_name = NULL, *leftover = NULL;
c864d6d7
JG
120 poptContext pc;
121 struct lttng_save_session_attr *attr;
122
123 pc = poptGetContext(NULL, argc, argv, save_opts, 0);
124 poptReadDefaultConfig(pc, 0);
125
126 while ((opt = poptGetNextOpt(pc)) != -1) {
127 switch (opt) {
128 case OPT_HELP:
4ba92f18 129 SHOW_HELP();
c864d6d7
JG
130 goto end;
131 case OPT_ALL:
c431d5e4 132 opt_save_all = true;
c864d6d7
JG
133 break;
134 case OPT_FORCE:
c431d5e4 135 opt_force = true;
c864d6d7 136 break;
13a810d5
DG
137 case OPT_LIST_OPTIONS:
138 list_cmd_options(stdout, save_opts);
139 goto end;
c864d6d7 140 default:
c864d6d7
JG
141 ret = CMD_UNDEFINED;
142 goto end;
143 }
144 }
145
146 if (!opt_save_all) {
147 session_name = poptGetArg(pc);
59b41aa2
JRJ
148 if (session_name) {
149 DBG2("Session name: %s", session_name);
1734c658
JRJ
150 } else {
151 /* default to opt_save_all */
c431d5e4 152 opt_save_all = true;
c864d6d7 153 }
c864d6d7
JG
154 }
155
68c7f6e5
JD
156 leftover = poptGetArg(pc);
157 if (leftover) {
158 ERR("Unknown argument: %s", leftover);
159 ret = CMD_ERROR;
160 goto end;
161 }
162
c864d6d7
JG
163 attr = lttng_save_session_attr_create();
164 if (!attr) {
165 ret = CMD_FATAL;
166 goto end_destroy;
167 }
168
169 if (lttng_save_session_attr_set_session_name(attr, session_name)) {
170 ret = CMD_ERROR;
171 goto end_destroy;
172 }
173
174 if (lttng_save_session_attr_set_overwrite(attr, opt_force)) {
175 ret = CMD_ERROR;
176 goto end_destroy;
177 }
178
ab85fc7f 179 if (lttng_save_session_attr_set_output_url(attr, opt_output_path)) {
c864d6d7
JG
180 ret = CMD_ERROR;
181 goto end_destroy;
182 }
183
1734c658
JRJ
184 /* Mi check */
185 if (lttng_opt_mi) {
186 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
187 if (!writer) {
188 ret = -LTTNG_ERR_NOMEM;
189 goto end_destroy;
190 }
191
192 /* Open command element */
193 ret = mi_lttng_writer_command_open(writer,
194 mi_lttng_element_command_save);
195 if (ret) {
196 ret = CMD_ERROR;
197 goto end_destroy;
198 }
199
200 /* Open output element */
201 ret = mi_lttng_writer_open_element(writer,
202 mi_lttng_element_command_output);
203 if (ret) {
204 ret = CMD_ERROR;
205 goto end_destroy;
206 }
207 }
208
209 command_ret = lttng_save_session(attr);
210 if (command_ret < 0) {
211 ERR("%s", lttng_strerror(command_ret));
212 success = 0;
ab85fc7f
DG
213 } else {
214 /* Inform the user of what just happened on success. */
215 if (session_name && opt_output_path) {
216 MSG("Session %s saved successfully in %s.", session_name,
217 opt_output_path);
218 } else if (session_name && !opt_output_path) {
219 MSG("Session %s saved successfully.", session_name);
220 } else if (!session_name && opt_output_path) {
221 MSG("All sessions have been saved successfully in %s.",
222 opt_output_path);
223 } else {
224 MSG("All sessions have been saved successfully.");
c864d6d7 225 }
1734c658
JRJ
226 success = 1;
227 }
228
229 /* Mi Printing and closing */
230 if (lttng_opt_mi) {
231 /* Mi print */
232 ret = mi_save_print(session_name);
233 if (ret) {
234 ret = CMD_ERROR;
235 goto end_destroy;
236 }
237
238 /* Close output element */
239 ret = mi_lttng_writer_close_element(writer);
240 if (ret) {
241 ret = CMD_ERROR;
242 goto end_destroy;
243 }
244
245 /* Success ? */
246 ret = mi_lttng_writer_write_element_bool(writer,
247 mi_lttng_element_command_success, success);
248 if (ret) {
249 ret = CMD_ERROR;
250 goto end_destroy;
251 }
252
253 /* Command element close */
254 ret = mi_lttng_writer_command_close(writer);
255 if (ret) {
256 ret = CMD_ERROR;
257 goto end_destroy;
258 }
c864d6d7
JG
259 }
260end_destroy:
261 lttng_save_session_attr_destroy(attr);
262end:
1734c658
JRJ
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 /* Overwrite ret if command failed */
270 ret = command_ret ? -command_ret : ret;
271
c864d6d7
JG
272 poptFreeContext(pc);
273 return ret;
274}
This page took 0.0616 seconds and 4 git commands to generate.