Commit | Line | Data |
---|---|---|
8c42d845 JG |
1 | /* |
2 | * Copyright (C) 2014 - 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 |
8c42d845 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 | 26 | #include <common/mi-lttng.h> |
f40ef1d5 | 27 | #include <common/config/session-config.h> |
b6d4654a | 28 | #include <lttng/load.h> |
1734c658 | 29 | |
8c42d845 JG |
30 | #include "../command.h" |
31 | ||
32 | static char *opt_input_path; | |
8c42d845 JG |
33 | static int opt_force; |
34 | static int opt_load_all; | |
35 | ||
11143783 DG |
36 | static const char *session_name; |
37 | ||
8c42d845 JG |
38 | enum { |
39 | OPT_HELP = 1, | |
40 | OPT_ALL, | |
41 | OPT_FORCE, | |
13a810d5 | 42 | OPT_LIST_OPTIONS, |
8c42d845 JG |
43 | }; |
44 | ||
1734c658 JRJ |
45 | static struct mi_writer *writer; |
46 | ||
8c42d845 JG |
47 | static struct poptOption load_opts[] = { |
48 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
49 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
50 | {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0}, | |
8c42d845 JG |
51 | {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0}, |
52 | {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0}, | |
13a810d5 | 53 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
8c42d845 JG |
54 | {0, 0, 0, 0, 0, 0, 0} |
55 | }; | |
56 | ||
1734c658 JRJ |
57 | static int mi_partial_session(const char *session_name) |
58 | { | |
59 | int ret; | |
60 | assert(writer); | |
61 | assert(session_name); | |
62 | ||
63 | /* Open session element */ | |
64 | ret = mi_lttng_writer_open_element(writer, config_element_session); | |
65 | if (ret) { | |
66 | goto end; | |
67 | } | |
68 | ||
69 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, | |
70 | session_name); | |
71 | if (ret) { | |
72 | goto end; | |
73 | } | |
74 | ||
75 | /* Closing session element */ | |
76 | ret = mi_lttng_writer_close_element(writer); | |
77 | end: | |
78 | return ret; | |
79 | } | |
80 | ||
81 | /* | |
82 | * Mi print of load command | |
83 | */ | |
84 | static int mi_load_print(const char *session_name) | |
85 | { | |
86 | int ret; | |
87 | assert(writer); | |
88 | ||
89 | if (opt_load_all) { | |
90 | /* We use a wildcard to represent all sessions */ | |
91 | session_name = "*"; | |
92 | } | |
93 | ||
94 | /* Print load element */ | |
95 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load); | |
96 | if (ret) { | |
97 | goto end; | |
98 | } | |
99 | ||
100 | /* Print session element */ | |
101 | ret = mi_partial_session(session_name); | |
102 | if (ret) { | |
103 | goto end; | |
104 | } | |
105 | ||
106 | /* Path element */ | |
107 | if (opt_input_path) { | |
108 | ret = mi_lttng_writer_write_element_string(writer, config_element_path, | |
109 | opt_input_path); | |
110 | if (ret) { | |
111 | goto end; | |
112 | } | |
113 | } | |
114 | ||
115 | /* Close load element */ | |
116 | ret = mi_lttng_writer_close_element(writer); | |
117 | ||
118 | end: | |
119 | return ret; | |
120 | } | |
121 | ||
8c42d845 JG |
122 | /* |
123 | * The 'load <options>' first level command | |
124 | */ | |
125 | int cmd_load(int argc, const char **argv) | |
126 | { | |
b6d4654a | 127 | int ret, success; |
8c42d845 JG |
128 | int opt; |
129 | poptContext pc; | |
b6d4654a JR |
130 | struct lttng_load_session_attr *session_attr = NULL; |
131 | char *input_path = NULL; | |
8c42d845 JG |
132 | |
133 | pc = poptGetContext(NULL, argc, argv, load_opts, 0); | |
134 | poptReadDefaultConfig(pc, 0); | |
135 | ||
136 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
137 | switch (opt) { | |
138 | case OPT_HELP: | |
4ba92f18 | 139 | SHOW_HELP(); |
b6d4654a | 140 | ret = CMD_SUCCESS; |
8c42d845 JG |
141 | goto end; |
142 | case OPT_ALL: | |
143 | opt_load_all = 1; | |
144 | break; | |
13a810d5 DG |
145 | case OPT_LIST_OPTIONS: |
146 | list_cmd_options(stdout, load_opts); | |
b6d4654a | 147 | ret = CMD_SUCCESS; |
13a810d5 | 148 | goto end; |
8c42d845 JG |
149 | case OPT_FORCE: |
150 | opt_force = 1; | |
151 | break; | |
152 | default: | |
8c42d845 JG |
153 | ret = CMD_UNDEFINED; |
154 | goto end; | |
155 | } | |
156 | } | |
157 | ||
623bc34c JG |
158 | ret = lttng_session_daemon_alive(); |
159 | if (!ret) { | |
160 | ERR("No session daemon is available"); | |
161 | ret = CMD_ERROR; | |
162 | goto end; | |
163 | } | |
164 | ||
11143783 DG |
165 | if (!opt_load_all) { |
166 | session_name = poptGetArg(pc); | |
732b768a DG |
167 | if (session_name) { |
168 | DBG2("Loading session name: %s", session_name); | |
1734c658 JRJ |
169 | } else { |
170 | /* Default to load_all */ | |
171 | opt_load_all = 1; | |
11143783 | 172 | } |
8c42d845 JG |
173 | } |
174 | ||
1734c658 JRJ |
175 | /* Mi check */ |
176 | if (lttng_opt_mi) { | |
177 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
178 | if (!writer) { | |
b6d4654a | 179 | ret = CMD_ERROR; |
1734c658 JRJ |
180 | goto end; |
181 | } | |
182 | ||
183 | /* Open command element */ | |
184 | ret = mi_lttng_writer_command_open(writer, | |
185 | mi_lttng_element_command_load); | |
186 | if (ret) { | |
187 | ret = CMD_ERROR; | |
188 | goto end; | |
189 | } | |
190 | ||
191 | /* Open output element */ | |
192 | ret = mi_lttng_writer_open_element(writer, | |
193 | mi_lttng_element_command_output); | |
194 | if (ret) { | |
195 | ret = CMD_ERROR; | |
196 | goto end; | |
197 | } | |
198 | } | |
199 | ||
b6d4654a JR |
200 | /* Prepare load attributes */ |
201 | session_attr = lttng_load_session_attr_create(); | |
202 | if (!session_attr) { | |
203 | ERR("Failed to create load session attributes"); | |
204 | ret = CMD_ERROR; | |
205 | goto end; | |
206 | } | |
207 | ||
208 | /* | |
209 | * Set the input url | |
210 | * lttng_load_session_attr_set_input_url only suppports absolute path. | |
211 | * Use realpath to resolve any relative path. | |
212 | * */ | |
213 | if (opt_input_path) { | |
214 | input_path = realpath(opt_input_path, NULL); | |
215 | if (!input_path) { | |
216 | PERROR("Invalid input path"); | |
217 | ret = CMD_ERROR; | |
218 | goto end; | |
219 | } | |
220 | } else { | |
221 | input_path = NULL; | |
222 | } | |
223 | ||
224 | ret = lttng_load_session_attr_set_input_url(session_attr, | |
225 | input_path); | |
226 | if (ret) { | |
227 | ERR("Invalid input path"); | |
228 | ret = CMD_ERROR; | |
229 | goto end; | |
230 | } | |
231 | ||
232 | /* Set the session name. NULL means all sessions should be loaded */ | |
233 | ret = lttng_load_session_attr_set_session_name(session_attr, | |
234 | session_name); | |
235 | if (ret) { | |
236 | ERR("Invalid session name"); | |
237 | ret = CMD_ERROR; | |
238 | goto end; | |
239 | } | |
240 | ||
241 | /* Set the overwrite attribute */ | |
242 | ret = lttng_load_session_attr_set_overwrite(session_attr, opt_force); | |
243 | if (ret) { | |
244 | ERR("Force argument could not be applied"); | |
245 | ret = CMD_ERROR; | |
246 | goto end; | |
247 | } | |
248 | ||
249 | ret = lttng_load_session(session_attr); | |
250 | if (ret) { | |
251 | ERR("%s", lttng_strerror(ret)); | |
1734c658 | 252 | success = 0; |
b6d4654a | 253 | ret = CMD_ERROR; |
279d6193 DG |
254 | } else { |
255 | if (opt_load_all) { | |
256 | MSG("All sessions have been loaded successfully"); | |
732b768a | 257 | } else if (session_name) { |
b6d4654a | 258 | ret = config_init((char *) session_name); |
8e525b95 DG |
259 | if (ret < 0) { |
260 | ret = CMD_WARNING; | |
261 | } | |
279d6193 | 262 | MSG("Session %s has been loaded successfully", session_name); |
732b768a DG |
263 | } else { |
264 | MSG("Session has been loaded successfully"); | |
279d6193 | 265 | } |
1734c658 | 266 | success = 1; |
b6d4654a | 267 | ret = CMD_SUCCESS; |
1734c658 JRJ |
268 | } |
269 | ||
270 | /* Mi Printing and closing */ | |
271 | if (lttng_opt_mi) { | |
272 | /* Mi print */ | |
273 | ret = mi_load_print(session_name); | |
274 | if (ret) { | |
275 | ret = CMD_ERROR; | |
276 | goto end; | |
277 | } | |
278 | ||
279 | /* Close output element */ | |
280 | ret = mi_lttng_writer_close_element(writer); | |
281 | if (ret) { | |
282 | ret = CMD_ERROR; | |
283 | goto end; | |
284 | } | |
285 | ||
286 | /* Success ? */ | |
287 | ret = mi_lttng_writer_write_element_bool(writer, | |
288 | mi_lttng_element_command_success, success); | |
289 | if (ret) { | |
290 | ret = CMD_ERROR; | |
291 | goto end; | |
292 | } | |
293 | ||
294 | /* Command element close */ | |
295 | ret = mi_lttng_writer_command_close(writer); | |
296 | if (ret) { | |
297 | ret = CMD_ERROR; | |
298 | goto end; | |
299 | } | |
8c42d845 JG |
300 | } |
301 | end: | |
1734c658 | 302 | if (writer && mi_lttng_writer_destroy(writer)) { |
b6d4654a | 303 | ERR("Failed to destroy mi lttng writer"); |
1734c658 JRJ |
304 | } |
305 | ||
b6d4654a JR |
306 | lttng_load_session_attr_destroy(session_attr); |
307 | free(input_path); | |
8c42d845 JG |
308 | poptFreeContext(pc); |
309 | return ret; | |
310 | } |