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