Add --list-options to save/load command
[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
18#define _GNU_SOURCE
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>
8c42d845 27#include <common/config/config.h>
1734c658 28
8c42d845
JG
29#include "../command.h"
30
31static char *opt_input_path;
8c42d845
JG
32static int opt_force;
33static int opt_load_all;
34
11143783
DG
35static const char *session_name;
36
8c42d845
JG
37enum {
38 OPT_HELP = 1,
39 OPT_ALL,
40 OPT_FORCE,
13a810d5 41 OPT_LIST_OPTIONS,
8c42d845
JG
42};
43
1734c658
JRJ
44static struct mi_writer *writer;
45
8c42d845
JG
46static struct poptOption load_opts[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
49 {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
8c42d845
JG
50 {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0},
51 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
13a810d5 52 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
8c42d845
JG
53 {0, 0, 0, 0, 0, 0, 0}
54};
55
56/*
57 * usage
58 */
59static void usage(FILE *ofp)
60{
11143783 61 fprintf(ofp, "usage: lttng load [OPTIONS] [SESSION]\n");
8c42d845
JG
62 fprintf(ofp, "\n");
63 fprintf(ofp, "Options:\n");
d7c6b799
DG
64 fprintf(ofp, " -h, --help Show this help\n");
65 fprintf(ofp, " -a, --all Load all sessions (default)\n");
66 fprintf(ofp, " -i, --input-path PATH Input path of the session file(s).\n");
67 fprintf(ofp, " If a directory, load all files in it\n");
68 fprintf(ofp, " else try to load the given file.\n");
69 fprintf(ofp, " -f, --force Override existing session(s).\n");
70 fprintf(ofp, " This will destroy existing session(s)\n");
71 fprintf(ofp, " before creating new one(s).\n");
8c42d845
JG
72}
73
1734c658
JRJ
74static int mi_partial_session(const char *session_name)
75{
76 int ret;
77 assert(writer);
78 assert(session_name);
79
80 /* Open session element */
81 ret = mi_lttng_writer_open_element(writer, config_element_session);
82 if (ret) {
83 goto end;
84 }
85
86 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
87 session_name);
88 if (ret) {
89 goto end;
90 }
91
92 /* Closing session element */
93 ret = mi_lttng_writer_close_element(writer);
94end:
95 return ret;
96}
97
98/*
99 * Mi print of load command
100 */
101static int mi_load_print(const char *session_name)
102{
103 int ret;
104 assert(writer);
105
106 if (opt_load_all) {
107 /* We use a wildcard to represent all sessions */
108 session_name = "*";
109 }
110
111 /* Print load element */
112 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load);
113 if (ret) {
114 goto end;
115 }
116
117 /* Print session element */
118 ret = mi_partial_session(session_name);
119 if (ret) {
120 goto end;
121 }
122
123 /* Path element */
124 if (opt_input_path) {
125 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
126 opt_input_path);
127 if (ret) {
128 goto end;
129 }
130 }
131
132 /* Close load element */
133 ret = mi_lttng_writer_close_element(writer);
134
135end:
136 return ret;
137}
138
8c42d845
JG
139/*
140 * The 'load <options>' first level command
141 */
142int cmd_load(int argc, const char **argv)
143{
1734c658 144 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
8c42d845
JG
145 int opt;
146 poptContext pc;
147
148 pc = poptGetContext(NULL, argc, argv, load_opts, 0);
149 poptReadDefaultConfig(pc, 0);
150
151 while ((opt = poptGetNextOpt(pc)) != -1) {
152 switch (opt) {
153 case OPT_HELP:
154 usage(stdout);
155 goto end;
156 case OPT_ALL:
157 opt_load_all = 1;
158 break;
13a810d5
DG
159 case OPT_LIST_OPTIONS:
160 list_cmd_options(stdout, load_opts);
161 goto end;
8c42d845
JG
162 case OPT_FORCE:
163 opt_force = 1;
164 break;
165 default:
166 usage(stderr);
167 ret = CMD_UNDEFINED;
168 goto end;
169 }
170 }
171
11143783
DG
172 if (!opt_load_all) {
173 session_name = poptGetArg(pc);
732b768a
DG
174 if (session_name) {
175 DBG2("Loading session name: %s", session_name);
1734c658
JRJ
176 } else {
177 /* Default to load_all */
178 opt_load_all = 1;
11143783 179 }
8c42d845
JG
180 }
181
1734c658
JRJ
182 /* Mi check */
183 if (lttng_opt_mi) {
184 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
185 if (!writer) {
186 ret = -LTTNG_ERR_NOMEM;
187 goto end;
188 }
189
190 /* Open command element */
191 ret = mi_lttng_writer_command_open(writer,
192 mi_lttng_element_command_load);
193 if (ret) {
194 ret = CMD_ERROR;
195 goto end;
196 }
197
198 /* Open output element */
199 ret = mi_lttng_writer_open_element(writer,
200 mi_lttng_element_command_output);
201 if (ret) {
202 ret = CMD_ERROR;
203 goto end;
204 }
205 }
206
207 command_ret = config_load_session(opt_input_path, session_name, opt_force, 0);
208 if (command_ret) {
209 ERR("%s", lttng_strerror(command_ret));
210 success = 0;
279d6193
DG
211 } else {
212 if (opt_load_all) {
213 MSG("All sessions have been loaded successfully");
732b768a 214 } else if (session_name) {
8e525b95
DG
215 ret = config_init((char *)session_name);
216 if (ret < 0) {
217 ret = CMD_WARNING;
218 }
279d6193 219 MSG("Session %s has been loaded successfully", session_name);
732b768a
DG
220 } else {
221 MSG("Session has been loaded successfully");
279d6193 222 }
1734c658
JRJ
223 success = 1;
224 }
225
226 /* Mi Printing and closing */
227 if (lttng_opt_mi) {
228 /* Mi print */
229 ret = mi_load_print(session_name);
230 if (ret) {
231 ret = CMD_ERROR;
232 goto end;
233 }
234
235 /* Close output element */
236 ret = mi_lttng_writer_close_element(writer);
237 if (ret) {
238 ret = CMD_ERROR;
239 goto end;
240 }
241
242 /* Success ? */
243 ret = mi_lttng_writer_write_element_bool(writer,
244 mi_lttng_element_command_success, success);
245 if (ret) {
246 ret = CMD_ERROR;
247 goto end;
248 }
249
250 /* Command element close */
251 ret = mi_lttng_writer_command_close(writer);
252 if (ret) {
253 ret = CMD_ERROR;
254 goto end;
255 }
8c42d845
JG
256 }
257end:
1734c658
JRJ
258 if (writer && mi_lttng_writer_destroy(writer)) {
259 /* Preserve original error code */
260 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
261 }
262
263 /* Overwrite ret if the was an error with the load command */
264 ret = command_ret ? -command_ret : ret;
265
8c42d845
JG
266 poptFreeContext(pc);
267 return ret;
268}
This page took 0.036531 seconds and 5 git commands to generate.