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