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