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