Rename "tracing session" -> "recording session"
[lttng-tools.git] / src / bin / lttng / commands / load.c
CommitLineData
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
34be8e06
SM
22static char *the_opt_input_path;
23static char *the_opt_override_url;
24static char *the_opt_override_session_name;
25static int the_opt_force;
26static int the_opt_load_all;
8c42d845 27
34be8e06 28static const char *the_session_name;
11143783 29
4fc83d94
PP
30#ifdef LTTNG_EMBED_HELP
31static const char help_msg[] =
32#include <lttng-load.1.h>
33;
34#endif
35
8c42d845
JG
36enum {
37 OPT_HELP = 1,
38 OPT_ALL,
39 OPT_FORCE,
13a810d5 40 OPT_LIST_OPTIONS,
8c42d845
JG
41};
42
34be8e06 43static struct mi_writer *the_writer;
1734c658 44
34be8e06 45static struct poptOption the_load_opts[] = {
8c42d845 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},
34be8e06 49 {"input-path", 'i', POPT_ARG_STRING, &the_opt_input_path, 0, 0, 0},
e1f481f6 50 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
34be8e06
SM
51 {"override-url", 0, POPT_ARG_STRING, &the_opt_override_url, 0, 0, 0},
52 {"override-name", 0, POPT_ARG_STRING, &the_opt_override_session_name, 0, 0, 0},
e1f481f6 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
57static int mi_partial_session(const char *session_name)
58{
59 int ret;
34be8e06 60 assert(the_writer);
1734c658
JRJ
61 assert(session_name);
62
63 /* Open session element */
34be8e06 64 ret = mi_lttng_writer_open_element(the_writer, config_element_session);
1734c658
JRJ
65 if (ret) {
66 goto end;
67 }
68
34be8e06 69 ret = mi_lttng_writer_write_element_string(the_writer, config_element_name,
1734c658
JRJ
70 session_name);
71 if (ret) {
72 goto end;
73 }
74
75 /* Closing session element */
34be8e06 76 ret = mi_lttng_writer_close_element(the_writer);
1734c658
JRJ
77end:
78 return ret;
79}
80
81/*
82 * Mi print of load command
83 */
84static int mi_load_print(const char *session_name)
85{
86 int ret;
34be8e06 87 assert(the_writer);
1734c658 88
34be8e06 89 if (the_opt_load_all) {
1734c658
JRJ
90 /* We use a wildcard to represent all sessions */
91 session_name = "*";
92 }
93
94 /* Print load element */
34be8e06 95 ret = mi_lttng_writer_open_element(the_writer, mi_lttng_element_load);
1734c658
JRJ
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 */
34be8e06
SM
107 if (the_opt_input_path) {
108 ret = mi_lttng_writer_write_element_string(the_writer, config_element_path,
109 the_opt_input_path);
1734c658
JRJ
110 if (ret) {
111 goto end;
112 }
113 }
114
23cb2d55 115 /* Print override elements */
34be8e06 116 ret = mi_lttng_writer_open_element(the_writer, mi_lttng_element_load_overrides);
23cb2d55
JR
117 if (ret) {
118 goto end;
119 }
120
121 /* Session name override element */
34be8e06
SM
122 if (the_opt_override_session_name) {
123 ret = mi_lttng_writer_write_element_string(the_writer,
124 config_element_name, the_opt_override_session_name);
23cb2d55
JR
125 if (ret) {
126 goto end;
127 }
128 }
129
130 /* Session url override element */
34be8e06
SM
131 if (the_opt_override_url) {
132 ret = mi_lttng_writer_write_element_string(the_writer,
23cb2d55 133 mi_lttng_element_load_override_url,
34be8e06 134 the_opt_override_url);
23cb2d55
JR
135 if (ret) {
136 goto end;
137 }
138 }
1734c658 139
23cb2d55 140 /* Close override and load element */
34be8e06 141 ret = mi_lttng_close_multi_element(the_writer, 2);
1734c658
JRJ
142end:
143 return ret;
144}
145
8c42d845
JG
146/*
147 * The 'load <options>' first level command
148 */
149int 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 157
34be8e06 158 pc = poptGetContext(NULL, argc, argv, the_load_opts, 0);
8c42d845
JG
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:
34be8e06 168 the_opt_load_all = 1;
8c42d845 169 break;
13a810d5 170 case OPT_LIST_OPTIONS:
34be8e06 171 list_cmd_options(stdout, the_load_opts);
b6d4654a 172 ret = CMD_SUCCESS;
13a810d5 173 goto end;
8c42d845 174 case OPT_FORCE:
34be8e06 175 the_opt_force = 1;
8c42d845
JG
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
34be8e06
SM
190 if (!the_opt_load_all) {
191 the_session_name = poptGetArg(pc);
192 if (the_session_name) {
193 DBG2("Loading session name: %s", the_session_name);
1734c658
JRJ
194 } else {
195 /* Default to load_all */
34be8e06 196 the_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) {
34be8e06
SM
209 the_writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
210 if (!the_writer) {
b6d4654a 211 ret = CMD_ERROR;
1734c658
JRJ
212 goto end;
213 }
214
215 /* Open command element */
34be8e06 216 ret = mi_lttng_writer_command_open(the_writer,
1734c658
JRJ
217 mi_lttng_element_command_load);
218 if (ret) {
219 ret = CMD_ERROR;
220 goto end;
221 }
222
223 /* Open output element */
34be8e06 224 ret = mi_lttng_writer_open_element(the_writer,
1734c658
JRJ
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 * */
34be8e06
SM
245 if (the_opt_input_path) {
246 input_path = realpath(the_opt_input_path, NULL);
b6d4654a
JR
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,
34be8e06 266 the_session_name);
b6d4654a
JR
267 if (ret) {
268 ERR("Invalid session name");
269 ret = CMD_ERROR;
270 goto end;
271 }
272
273 /* Set the overwrite attribute */
34be8e06 274 ret = lttng_load_session_attr_set_overwrite(session_attr, the_opt_force);
b6d4654a
JR
275 if (ret) {
276 ERR("Force argument could not be applied");
277 ret = CMD_ERROR;
278 goto end;
279 }
280
fe559816 281 /* Set the overrides attributes if any */
34be8e06 282 if (the_opt_override_url) {
fe559816 283 ret = lttng_load_session_attr_set_override_url(session_attr,
34be8e06 284 the_opt_override_url);
fe559816
JR
285 if (ret) {
286 ERR("Url override is invalid");
287 goto end;
288 }
289 }
290
34be8e06
SM
291 if (the_opt_override_session_name) {
292 if (the_opt_load_all) {
e1f481f6
JR
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,
34be8e06 298 the_opt_override_session_name);
e1f481f6
JR
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 311 } else {
34be8e06 312 if (the_opt_load_all) {
279d6193 313 MSG("All sessions have been loaded successfully");
34be8e06
SM
314 } else if (the_session_name) {
315 ret = config_init((char *) the_session_name);
8e525b95 316 if (ret < 0) {
96694f0b 317 WARN("Could not set %s as the default session",
34be8e06 318 the_session_name);
8e525b95 319 }
34be8e06 320 MSG("Session %s has been loaded successfully", the_session_name);
732b768a
DG
321 } else {
322 MSG("Session has been loaded successfully");
279d6193 323 }
fe559816 324
34be8e06 325 if (the_opt_override_session_name) {
060e2e09 326 MSG("Session name overridden with %s",
34be8e06 327 the_opt_override_session_name);
060e2e09
JR
328 }
329
34be8e06
SM
330 if (the_opt_override_url) {
331 MSG("Session output url overridden with %s", the_opt_override_url);
fe559816 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 */
34be8e06 340 ret = mi_load_print(the_session_name);
1734c658
JRJ
341 if (ret) {
342 ret = CMD_ERROR;
343 goto end;
344 }
345
346 /* Close output element */
34be8e06 347 ret = mi_lttng_writer_close_element(the_writer);
1734c658
JRJ
348 if (ret) {
349 ret = CMD_ERROR;
350 goto end;
351 }
352
353 /* Success ? */
34be8e06 354 ret = mi_lttng_writer_write_element_bool(the_writer,
1734c658
JRJ
355 mi_lttng_element_command_success, success);
356 if (ret) {
357 ret = CMD_ERROR;
358 goto end;
359 }
360
361 /* Command element close */
34be8e06 362 ret = mi_lttng_writer_command_close(the_writer);
1734c658
JRJ
363 if (ret) {
364 ret = CMD_ERROR;
365 goto end;
366 }
8c42d845
JG
367 }
368end:
34be8e06 369 if (the_writer && mi_lttng_writer_destroy(the_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}
This page took 0.064978 seconds and 4 git commands to generate.