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; | |
fe559816 | 33 | static char *opt_override_url; |
e1f481f6 | 34 | static char *opt_override_session_name; |
8c42d845 JG |
35 | static int opt_force; |
36 | static int opt_load_all; | |
37 | ||
11143783 DG |
38 | static const char *session_name; |
39 | ||
4fc83d94 PP |
40 | #ifdef LTTNG_EMBED_HELP |
41 | static const char help_msg[] = | |
42 | #include <lttng-load.1.h> | |
43 | ; | |
44 | #endif | |
45 | ||
8c42d845 JG |
46 | enum { |
47 | OPT_HELP = 1, | |
48 | OPT_ALL, | |
49 | OPT_FORCE, | |
13a810d5 | 50 | OPT_LIST_OPTIONS, |
8c42d845 JG |
51 | }; |
52 | ||
1734c658 JRJ |
53 | static struct mi_writer *writer; |
54 | ||
8c42d845 JG |
55 | static struct poptOption load_opts[] = { |
56 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
e1f481f6 JR |
57 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
58 | {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0}, | |
59 | {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0}, | |
60 | {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0}, | |
61 | {"override-url", 0, POPT_ARG_STRING, &opt_override_url, 0, 0, 0}, | |
62 | {"override-name", 0, POPT_ARG_STRING, &opt_override_session_name, 0, 0, 0}, | |
63 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
8c42d845 JG |
64 | {0, 0, 0, 0, 0, 0, 0} |
65 | }; | |
66 | ||
1734c658 JRJ |
67 | static int mi_partial_session(const char *session_name) |
68 | { | |
69 | int ret; | |
70 | assert(writer); | |
71 | assert(session_name); | |
72 | ||
73 | /* Open session element */ | |
74 | ret = mi_lttng_writer_open_element(writer, config_element_session); | |
75 | if (ret) { | |
76 | goto end; | |
77 | } | |
78 | ||
79 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, | |
80 | session_name); | |
81 | if (ret) { | |
82 | goto end; | |
83 | } | |
84 | ||
85 | /* Closing session element */ | |
86 | ret = mi_lttng_writer_close_element(writer); | |
87 | end: | |
88 | return ret; | |
89 | } | |
90 | ||
91 | /* | |
92 | * Mi print of load command | |
93 | */ | |
94 | static int mi_load_print(const char *session_name) | |
95 | { | |
96 | int ret; | |
97 | assert(writer); | |
98 | ||
99 | if (opt_load_all) { | |
100 | /* We use a wildcard to represent all sessions */ | |
101 | session_name = "*"; | |
102 | } | |
103 | ||
104 | /* Print load element */ | |
105 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load); | |
106 | if (ret) { | |
107 | goto end; | |
108 | } | |
109 | ||
110 | /* Print session element */ | |
111 | ret = mi_partial_session(session_name); | |
112 | if (ret) { | |
113 | goto end; | |
114 | } | |
115 | ||
116 | /* Path element */ | |
117 | if (opt_input_path) { | |
118 | ret = mi_lttng_writer_write_element_string(writer, config_element_path, | |
119 | opt_input_path); | |
120 | if (ret) { | |
121 | goto end; | |
122 | } | |
123 | } | |
124 | ||
23cb2d55 JR |
125 | /* Print override elements */ |
126 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load_overrides); | |
127 | if (ret) { | |
128 | goto end; | |
129 | } | |
130 | ||
131 | /* Session name override element */ | |
132 | if (opt_override_session_name) { | |
133 | ret = mi_lttng_writer_write_element_string(writer, | |
134 | config_element_name, opt_override_session_name); | |
135 | if (ret) { | |
136 | goto end; | |
137 | } | |
138 | } | |
139 | ||
140 | /* Session url override element */ | |
141 | if (opt_override_url) { | |
142 | ret = mi_lttng_writer_write_element_string(writer, | |
143 | mi_lttng_element_load_override_url, | |
144 | opt_override_url); | |
145 | if (ret) { | |
146 | goto end; | |
147 | } | |
148 | } | |
1734c658 | 149 | |
23cb2d55 JR |
150 | /* Close override and load element */ |
151 | ret = mi_lttng_close_multi_element(writer, 2); | |
1734c658 JRJ |
152 | end: |
153 | return ret; | |
154 | } | |
155 | ||
8c42d845 JG |
156 | /* |
157 | * The 'load <options>' first level command | |
158 | */ | |
159 | int cmd_load(int argc, const char **argv) | |
160 | { | |
b6d4654a | 161 | int ret, success; |
8c42d845 JG |
162 | int opt; |
163 | poptContext pc; | |
b6d4654a JR |
164 | struct lttng_load_session_attr *session_attr = NULL; |
165 | char *input_path = NULL; | |
68c7f6e5 | 166 | const char *leftover = NULL; |
8c42d845 JG |
167 | |
168 | pc = poptGetContext(NULL, argc, argv, load_opts, 0); | |
169 | poptReadDefaultConfig(pc, 0); | |
170 | ||
171 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
172 | switch (opt) { | |
173 | case OPT_HELP: | |
4ba92f18 | 174 | SHOW_HELP(); |
b6d4654a | 175 | ret = CMD_SUCCESS; |
8c42d845 JG |
176 | goto end; |
177 | case OPT_ALL: | |
178 | opt_load_all = 1; | |
179 | break; | |
13a810d5 DG |
180 | case OPT_LIST_OPTIONS: |
181 | list_cmd_options(stdout, load_opts); | |
b6d4654a | 182 | ret = CMD_SUCCESS; |
13a810d5 | 183 | goto end; |
8c42d845 JG |
184 | case OPT_FORCE: |
185 | opt_force = 1; | |
186 | break; | |
187 | default: | |
8c42d845 JG |
188 | ret = CMD_UNDEFINED; |
189 | goto end; | |
190 | } | |
191 | } | |
192 | ||
623bc34c JG |
193 | ret = lttng_session_daemon_alive(); |
194 | if (!ret) { | |
195 | ERR("No session daemon is available"); | |
196 | ret = CMD_ERROR; | |
197 | goto end; | |
198 | } | |
199 | ||
11143783 DG |
200 | if (!opt_load_all) { |
201 | session_name = poptGetArg(pc); | |
732b768a DG |
202 | if (session_name) { |
203 | DBG2("Loading session name: %s", session_name); | |
1734c658 JRJ |
204 | } else { |
205 | /* Default to load_all */ | |
206 | opt_load_all = 1; | |
11143783 | 207 | } |
8c42d845 JG |
208 | } |
209 | ||
68c7f6e5 JD |
210 | leftover = poptGetArg(pc); |
211 | if (leftover) { | |
212 | ERR("Unknown argument: %s", leftover); | |
213 | ret = CMD_ERROR; | |
214 | goto end; | |
215 | } | |
216 | ||
1734c658 JRJ |
217 | /* Mi check */ |
218 | if (lttng_opt_mi) { | |
219 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
220 | if (!writer) { | |
b6d4654a | 221 | ret = CMD_ERROR; |
1734c658 JRJ |
222 | goto end; |
223 | } | |
224 | ||
225 | /* Open command element */ | |
226 | ret = mi_lttng_writer_command_open(writer, | |
227 | mi_lttng_element_command_load); | |
228 | if (ret) { | |
229 | ret = CMD_ERROR; | |
230 | goto end; | |
231 | } | |
232 | ||
233 | /* Open output element */ | |
234 | ret = mi_lttng_writer_open_element(writer, | |
235 | mi_lttng_element_command_output); | |
236 | if (ret) { | |
237 | ret = CMD_ERROR; | |
238 | goto end; | |
239 | } | |
240 | } | |
241 | ||
b6d4654a JR |
242 | /* Prepare load attributes */ |
243 | session_attr = lttng_load_session_attr_create(); | |
244 | if (!session_attr) { | |
245 | ERR("Failed to create load session attributes"); | |
246 | ret = CMD_ERROR; | |
247 | goto end; | |
248 | } | |
249 | ||
250 | /* | |
251 | * Set the input url | |
252 | * lttng_load_session_attr_set_input_url only suppports absolute path. | |
253 | * Use realpath to resolve any relative path. | |
254 | * */ | |
255 | if (opt_input_path) { | |
256 | input_path = realpath(opt_input_path, NULL); | |
257 | if (!input_path) { | |
258 | PERROR("Invalid input path"); | |
259 | ret = CMD_ERROR; | |
260 | goto end; | |
261 | } | |
262 | } else { | |
263 | input_path = NULL; | |
264 | } | |
265 | ||
266 | ret = lttng_load_session_attr_set_input_url(session_attr, | |
267 | input_path); | |
268 | if (ret) { | |
269 | ERR("Invalid input path"); | |
270 | ret = CMD_ERROR; | |
271 | goto end; | |
272 | } | |
273 | ||
274 | /* Set the session name. NULL means all sessions should be loaded */ | |
275 | ret = lttng_load_session_attr_set_session_name(session_attr, | |
276 | session_name); | |
277 | if (ret) { | |
278 | ERR("Invalid session name"); | |
279 | ret = CMD_ERROR; | |
280 | goto end; | |
281 | } | |
282 | ||
283 | /* Set the overwrite attribute */ | |
284 | ret = lttng_load_session_attr_set_overwrite(session_attr, opt_force); | |
285 | if (ret) { | |
286 | ERR("Force argument could not be applied"); | |
287 | ret = CMD_ERROR; | |
288 | goto end; | |
289 | } | |
290 | ||
fe559816 JR |
291 | /* Set the overrides attributes if any */ |
292 | if (opt_override_url) { | |
293 | ret = lttng_load_session_attr_set_override_url(session_attr, | |
294 | opt_override_url); | |
295 | if (ret) { | |
296 | ERR("Url override is invalid"); | |
297 | goto end; | |
298 | } | |
299 | } | |
300 | ||
e1f481f6 JR |
301 | if (opt_override_session_name) { |
302 | if (opt_load_all) { | |
303 | ERR("Options --all and --override-name cannot be used simultaneously"); | |
304 | ret = CMD_ERROR; | |
305 | goto end; | |
306 | } | |
307 | ret = lttng_load_session_attr_set_override_session_name(session_attr, | |
308 | opt_override_session_name); | |
309 | if (ret) { | |
310 | ERR("Failed to set session name override"); | |
311 | ret = CMD_ERROR; | |
312 | goto end; | |
313 | } | |
314 | } | |
315 | ||
b6d4654a JR |
316 | ret = lttng_load_session(session_attr); |
317 | if (ret) { | |
318 | ERR("%s", lttng_strerror(ret)); | |
1734c658 | 319 | success = 0; |
b6d4654a | 320 | ret = CMD_ERROR; |
279d6193 DG |
321 | } else { |
322 | if (opt_load_all) { | |
323 | MSG("All sessions have been loaded successfully"); | |
732b768a | 324 | } else if (session_name) { |
b6d4654a | 325 | ret = config_init((char *) session_name); |
8e525b95 | 326 | if (ret < 0) { |
96694f0b JG |
327 | WARN("Could not set %s as the default session", |
328 | session_name); | |
8e525b95 | 329 | } |
279d6193 | 330 | MSG("Session %s has been loaded successfully", session_name); |
732b768a DG |
331 | } else { |
332 | MSG("Session has been loaded successfully"); | |
279d6193 | 333 | } |
fe559816 | 334 | |
060e2e09 JR |
335 | if (opt_override_session_name) { |
336 | MSG("Session name overridden with %s", | |
337 | opt_override_session_name); | |
338 | } | |
339 | ||
fe559816 JR |
340 | if (opt_override_url) { |
341 | MSG("Session output url overridden with %s", opt_override_url); | |
342 | } | |
1734c658 | 343 | success = 1; |
b6d4654a | 344 | ret = CMD_SUCCESS; |
1734c658 JRJ |
345 | } |
346 | ||
347 | /* Mi Printing and closing */ | |
348 | if (lttng_opt_mi) { | |
349 | /* Mi print */ | |
350 | ret = mi_load_print(session_name); | |
351 | if (ret) { | |
352 | ret = CMD_ERROR; | |
353 | goto end; | |
354 | } | |
355 | ||
356 | /* Close output element */ | |
357 | ret = mi_lttng_writer_close_element(writer); | |
358 | if (ret) { | |
359 | ret = CMD_ERROR; | |
360 | goto end; | |
361 | } | |
362 | ||
363 | /* Success ? */ | |
364 | ret = mi_lttng_writer_write_element_bool(writer, | |
365 | mi_lttng_element_command_success, success); | |
366 | if (ret) { | |
367 | ret = CMD_ERROR; | |
368 | goto end; | |
369 | } | |
370 | ||
371 | /* Command element close */ | |
372 | ret = mi_lttng_writer_command_close(writer); | |
373 | if (ret) { | |
374 | ret = CMD_ERROR; | |
375 | goto end; | |
376 | } | |
8c42d845 JG |
377 | } |
378 | end: | |
1734c658 | 379 | if (writer && mi_lttng_writer_destroy(writer)) { |
b6d4654a | 380 | ERR("Failed to destroy mi lttng writer"); |
1734c658 JRJ |
381 | } |
382 | ||
b6d4654a JR |
383 | lttng_load_session_attr_destroy(session_attr); |
384 | free(input_path); | |
8c42d845 JG |
385 | poptFreeContext(pc); |
386 | return ret; | |
387 | } |